text_server.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /*************************************************************************/
  2. /* text_server.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef TEXT_SERVER_H
  31. #define TEXT_SERVER_H
  32. #include "core/object/reference.h"
  33. #include "core/os/os.h"
  34. #include "core/templates/rid.h"
  35. #include "core/variant/variant.h"
  36. #include "scene/resources/texture.h"
  37. class CanvasTexture;
  38. class TextServer : public Object {
  39. GDCLASS(TextServer, Object);
  40. public:
  41. enum Direction {
  42. DIRECTION_AUTO,
  43. DIRECTION_LTR,
  44. DIRECTION_RTL
  45. };
  46. enum Orientation {
  47. ORIENTATION_HORIZONTAL,
  48. ORIENTATION_VERTICAL
  49. };
  50. enum JustificationFlag {
  51. JUSTIFICATION_NONE = 0,
  52. JUSTIFICATION_KASHIDA = 1 << 0,
  53. JUSTIFICATION_WORD_BOUND = 1 << 1,
  54. JUSTIFICATION_TRIM_EDGE_SPACES = 1 << 2,
  55. JUSTIFICATION_AFTER_LAST_TAB = 1 << 3
  56. };
  57. enum LineBreakFlag {
  58. BREAK_NONE = 0,
  59. BREAK_MANDATORY = 1 << 4,
  60. BREAK_WORD_BOUND = 1 << 5,
  61. BREAK_GRAPHEME_BOUND = 1 << 6
  62. //RESERVED = 1 << 7
  63. };
  64. enum GraphemeFlag {
  65. GRAPHEME_IS_VALID = 1 << 0, // Glyph is valid.
  66. GRAPHEME_IS_RTL = 1 << 1, // Glyph is right-to-left.
  67. GRAPHEME_IS_VIRTUAL = 1 << 2, // Glyph is not part of source string (added by fit_to_width function, do not affect caret movement).
  68. GRAPHEME_IS_SPACE = 1 << 3, // Is whitespace (for justification).
  69. GRAPHEME_IS_BREAK_HARD = 1 << 4, // Is line break (mandatory break, e.g "\n")
  70. GRAPHEME_IS_BREAK_SOFT = 1 << 5, // Is line break (optional break, e.g space)
  71. GRAPHEME_IS_TAB = 1 << 6, // Is tab or vertical tab
  72. GRAPHEME_IS_ELONGATION = 1 << 7 // Elongation (e.g kashida), glyph can be duplicated or truncated to fit line to width.
  73. };
  74. enum Hinting {
  75. HINTING_NONE,
  76. HINTING_LIGHT,
  77. HINTING_NORMAL
  78. };
  79. enum Feature {
  80. FEATURE_BIDI_LAYOUT = 1 << 0,
  81. FEATURE_VERTICAL_LAYOUT = 1 << 1,
  82. FEATURE_SHAPING = 1 << 2,
  83. FEATURE_KASHIDA_JUSTIFICATION = 1 << 3,
  84. FEATURE_BREAK_ITERATORS = 1 << 4,
  85. FEATURE_FONT_SYSTEM = 1 << 5,
  86. FEATURE_USE_SUPPORT_DATA = 1 << 6
  87. };
  88. struct Glyph {
  89. int start = -1; // Start offset in the source string.
  90. int end = -1; // End offset in the source string.
  91. uint8_t count = 0; // Number of glyphs in the grapheme, set in the first glyph only.
  92. uint8_t repeat = 1; // Draw multiple times in the row.
  93. uint8_t flags = 0; // Grapheme flags (valid, rtl, virtual), set in the first glyph only.
  94. float x_off = 0.f; // Offset from the origin of the glyph on baseline.
  95. float y_off = 0.f;
  96. float advance = 0.f; // Advance to the next glyph along baseline(x for horizontal layout, y for vertical).
  97. RID font_rid; // Font resource.
  98. int font_size = 0; // Font size;
  99. uint32_t index = 0; // Glyph index (font specific) or UTF-32 codepoint (for the invalid glyphs).
  100. bool operator==(const Glyph &p_a) const;
  101. bool operator!=(const Glyph &p_a) const;
  102. bool operator<(const Glyph &p_a) const;
  103. bool operator>(const Glyph &p_a) const;
  104. };
  105. struct GlyphCompare { // For line breaking reordering.
  106. _FORCE_INLINE_ bool operator()(const Glyph &l, const Glyph &r) const {
  107. if (l.start == r.start) {
  108. if (l.count == r.count) {
  109. if ((l.flags & GRAPHEME_IS_VIRTUAL) == GRAPHEME_IS_VIRTUAL) {
  110. return false;
  111. } else {
  112. return true;
  113. }
  114. }
  115. return l.count > r.count; // Sort first glyoh with count & flags, order of the rest are irrelevant.
  116. } else {
  117. return l.start < r.start;
  118. }
  119. }
  120. };
  121. struct ShapedTextData {
  122. /* Source data */
  123. RID parent; // Substring parent ShapedTextData.
  124. int start = 0; // Substring start offset in the parent string.
  125. int end = 0; // Substring end offset in the parent string.
  126. String text;
  127. TextServer::Direction direction = DIRECTION_LTR; // Desired text direction.
  128. TextServer::Orientation orientation = ORIENTATION_HORIZONTAL;
  129. struct Span {
  130. int start = -1;
  131. int end = -1;
  132. Vector<RID> fonts;
  133. int font_size = 0;
  134. Variant embedded_key;
  135. String language;
  136. Dictionary features;
  137. };
  138. Vector<Span> spans;
  139. struct EmbeddedObject {
  140. int pos = 0;
  141. VAlign inline_align = VALIGN_TOP;
  142. Rect2 rect;
  143. };
  144. Map<Variant, EmbeddedObject> objects;
  145. /* Shaped data */
  146. TextServer::Direction para_direction = DIRECTION_LTR; // Detected text direction.
  147. bool valid = false; // String is shaped.
  148. bool line_breaks_valid = false; // Line and word break flags are populated (and virtual zero width spaces inserted).
  149. bool justification_ops_valid = false; // Virtual elongation glyphs are added to the string.
  150. bool sort_valid = false;
  151. bool preserve_invalid = true; // Draw hex code box instead of missing characters.
  152. bool preserve_control = false; // Draw control characters.
  153. float ascent = 0.f; // Ascent for horizontal layout, 1/2 of width for vertical.
  154. float descent = 0.f; // Descent for horizontal layout, 1/2 of width for vertical.
  155. float width = 0.f; // Width for horizontal layout, height for vertical.
  156. float upos = 0.f;
  157. float uthk = 0.f;
  158. Vector<TextServer::Glyph> glyphs;
  159. Vector<TextServer::Glyph> glyphs_logical;
  160. };
  161. struct BitmapFontData {
  162. int height = 0;
  163. int ascent = 0;
  164. int charcount = 0;
  165. const int *char_rects = nullptr;
  166. int kerning_count = 0;
  167. const int *kernings = nullptr;
  168. int w = 0;
  169. int h = 0;
  170. const unsigned char *img = nullptr;
  171. };
  172. protected:
  173. static void _bind_methods();
  174. static Vector3 hex_code_box_font_size[2];
  175. static Ref<CanvasTexture> hex_code_box_font_tex[2];
  176. public:
  177. static void initialize_hex_code_box_fonts();
  178. static void finish_hex_code_box_fonts();
  179. virtual bool has_feature(Feature p_feature) = 0;
  180. virtual String get_name() const = 0;
  181. virtual void free(RID p_rid) = 0;
  182. virtual bool has(RID p_rid) = 0;
  183. virtual bool load_support_data(const String &p_filename) = 0;
  184. #ifdef TOOLS_ENABLED
  185. virtual String get_support_data_filename() = 0;
  186. virtual String get_support_data_info() = 0;
  187. virtual bool save_support_data(const String &p_filename) = 0;
  188. #endif
  189. virtual bool is_locale_right_to_left(const String &p_locale) = 0;
  190. virtual int32_t name_to_tag(const String &p_name) { return 0; };
  191. virtual String tag_to_name(int32_t p_tag) { return ""; };
  192. /* Font interface */
  193. virtual RID create_font_system(const String &p_name, int p_base_size = 16) = 0;
  194. virtual RID create_font_resource(const String &p_filename, int p_base_size = 16) = 0;
  195. virtual RID create_font_memory(const uint8_t *p_data, size_t p_size, const String &p_type, int p_base_size = 16) = 0;
  196. virtual float font_get_height(RID p_font, int p_size) const = 0;
  197. virtual float font_get_ascent(RID p_font, int p_size) const = 0;
  198. virtual float font_get_descent(RID p_font, int p_size) const = 0;
  199. virtual float font_get_underline_position(RID p_font, int p_size) const = 0;
  200. virtual float font_get_underline_thickness(RID p_font, int p_size) const = 0;
  201. virtual void font_set_antialiased(RID p_font, bool p_antialiased) = 0;
  202. virtual bool font_get_antialiased(RID p_font) const = 0;
  203. virtual Dictionary font_get_feature_list(RID p_font) const { return Dictionary(); };
  204. virtual void font_set_distance_field_hint(RID p_font, bool p_distance_field) = 0;
  205. virtual bool font_get_distance_field_hint(RID p_font) const = 0;
  206. virtual void font_set_hinting(RID p_font, Hinting p_hinting) = 0;
  207. virtual Hinting font_get_hinting(RID p_font) const = 0;
  208. virtual void font_set_force_autohinter(RID p_font, bool p_enabeld) = 0;
  209. virtual bool font_get_force_autohinter(RID p_font) const = 0;
  210. virtual bool font_has_char(RID p_font, char32_t p_char) const = 0;
  211. virtual String font_get_supported_chars(RID p_font) const = 0;
  212. virtual bool font_has_outline(RID p_font) const = 0;
  213. virtual float font_get_base_size(RID p_font) const = 0;
  214. virtual bool font_is_language_supported(RID p_font, const String &p_language) const = 0;
  215. virtual void font_set_language_support_override(RID p_font, const String &p_language, bool p_supported) = 0;
  216. virtual bool font_get_language_support_override(RID p_font, const String &p_language) = 0;
  217. virtual void font_remove_language_support_override(RID p_font, const String &p_language) = 0;
  218. virtual Vector<String> font_get_language_support_overrides(RID p_font) = 0;
  219. virtual bool font_is_script_supported(RID p_font, const String &p_script) const = 0;
  220. virtual void font_set_script_support_override(RID p_font, const String &p_script, bool p_supported) = 0;
  221. virtual bool font_get_script_support_override(RID p_font, const String &p_script) = 0;
  222. virtual void font_remove_script_support_override(RID p_font, const String &p_script) = 0;
  223. virtual Vector<String> font_get_script_support_overrides(RID p_font) = 0;
  224. virtual uint32_t font_get_glyph_index(RID p_font, char32_t p_char, char32_t p_variation_selector = 0x0000) const = 0;
  225. virtual Vector2 font_get_glyph_advance(RID p_font, uint32_t p_index, int p_size) const = 0;
  226. virtual Vector2 font_get_glyph_kerning(RID p_font, uint32_t p_index_a, uint32_t p_index_b, int p_size) const = 0;
  227. virtual Vector2 font_draw_glyph(RID p_font, RID p_canvas, int p_size, const Vector2 &p_pos, uint32_t p_index, const Color &p_color = Color(1, 1, 1)) const = 0;
  228. virtual Vector2 font_draw_glyph_outline(RID p_font, RID p_canvas, int p_size, int p_outline_size, const Vector2 &p_pos, uint32_t p_index, const Color &p_color = Color(1, 1, 1)) const = 0;
  229. virtual float font_get_oversampling() const = 0;
  230. virtual void font_set_oversampling(float p_oversampling) = 0;
  231. Vector2 get_hex_code_box_size(int p_size, char32_t p_index) const;
  232. void draw_hex_code_box(RID p_canvas, int p_size, const Vector2 &p_pos, char32_t p_index, const Color &p_color) const;
  233. virtual Vector<String> get_system_fonts() const = 0;
  234. /* Shaped text buffer interface */
  235. virtual RID create_shaped_text(Direction p_direction = DIRECTION_AUTO, Orientation p_orientation = ORIENTATION_HORIZONTAL) = 0;
  236. virtual void shaped_text_clear(RID p_shaped) = 0;
  237. virtual void shaped_text_set_direction(RID p_shaped, Direction p_direction = DIRECTION_AUTO) = 0;
  238. virtual Direction shaped_text_get_direction(RID p_shaped) const = 0;
  239. virtual void shaped_text_set_bidi_override(RID p_shaped, const Vector<Vector2i> &p_override) = 0;
  240. virtual void shaped_text_set_orientation(RID p_shaped, Orientation p_orientation = ORIENTATION_HORIZONTAL) = 0;
  241. virtual Orientation shaped_text_get_orientation(RID p_shaped) const = 0;
  242. virtual void shaped_text_set_preserve_invalid(RID p_shaped, bool p_enabled) = 0;
  243. virtual bool shaped_text_get_preserve_invalid(RID p_shaped) const = 0;
  244. virtual void shaped_text_set_preserve_control(RID p_shaped, bool p_enabled) = 0;
  245. virtual bool shaped_text_get_preserve_control(RID p_shaped) const = 0;
  246. virtual bool shaped_text_add_string(RID p_shaped, const String &p_text, const Vector<RID> &p_fonts, int p_size, const Dictionary &p_opentype_features = Dictionary(), const String &p_language = "") = 0;
  247. virtual bool shaped_text_add_object(RID p_shaped, Variant p_key, const Size2 &p_size, VAlign p_inline_align = VALIGN_CENTER, int p_length = 1) = 0;
  248. virtual bool shaped_text_resize_object(RID p_shaped, Variant p_key, const Size2 &p_size, VAlign p_inline_align = VALIGN_CENTER) = 0;
  249. virtual RID shaped_text_substr(RID p_shaped, int p_start, int p_length) const = 0; // Copy shaped substring (e.g. line break) without reshaping, but correctly reordered, preservers range.
  250. virtual RID shaped_text_get_parent(RID p_shaped) const = 0;
  251. virtual float shaped_text_fit_to_width(RID p_shaped, float p_width, uint8_t /*JustificationFlag*/ p_jst_flags = JUSTIFICATION_WORD_BOUND | JUSTIFICATION_KASHIDA) = 0;
  252. virtual float shaped_text_tab_align(RID p_shaped, const Vector<float> &p_tab_stops) = 0;
  253. virtual bool shaped_text_shape(RID p_shaped) = 0;
  254. virtual bool shaped_text_update_breaks(RID p_shaped) = 0;
  255. virtual bool shaped_text_update_justification_ops(RID p_shaped) = 0;
  256. virtual bool shaped_text_is_ready(RID p_shaped) const = 0;
  257. virtual Vector<Glyph> shaped_text_get_glyphs(RID p_shaped) const = 0;
  258. virtual Vector2i shaped_text_get_range(RID p_shaped) const = 0;
  259. virtual Vector<Glyph> shaped_text_sort_logical(RID p_shaped) = 0;
  260. virtual Vector<Vector2i> shaped_text_get_line_breaks_adv(RID p_shaped, const Vector<float> &p_width, int p_start = 0, bool p_once = true, uint8_t /*TextBreakFlag*/ p_break_flags = BREAK_MANDATORY | BREAK_WORD_BOUND) const;
  261. virtual Vector<Vector2i> shaped_text_get_line_breaks(RID p_shaped, float p_width, int p_start = 0, uint8_t /*TextBreakFlag*/ p_break_flags = BREAK_MANDATORY | BREAK_WORD_BOUND) const;
  262. virtual Vector<Vector2i> shaped_text_get_word_breaks(RID p_shaped) const;
  263. virtual Array shaped_text_get_objects(RID p_shaped) const = 0;
  264. virtual Rect2 shaped_text_get_object_rect(RID p_shaped, Variant p_key) const = 0;
  265. virtual Size2 shaped_text_get_size(RID p_shaped) const = 0;
  266. virtual float shaped_text_get_ascent(RID p_shaped) const = 0;
  267. virtual float shaped_text_get_descent(RID p_shaped) const = 0;
  268. virtual float shaped_text_get_width(RID p_shaped) const = 0;
  269. virtual float shaped_text_get_underline_position(RID p_shaped) const = 0;
  270. virtual float shaped_text_get_underline_thickness(RID p_shaped) const = 0;
  271. virtual Direction shaped_text_get_dominant_direciton_in_range(RID p_shaped, int p_start, int p_end) const;
  272. virtual void shaped_text_get_carets(RID p_shaped, int p_position, Rect2 &p_leading_caret, Direction &p_leading_dir, Rect2 &p_trailing_caret, Direction &p_trailing_dir) const;
  273. virtual Vector<Vector2> shaped_text_get_selection(RID p_shaped, int p_start, int p_end) const;
  274. virtual int shaped_text_hit_test_grapheme(RID p_shaped, float p_coords) const; // Return grapheme index.
  275. virtual int shaped_text_hit_test_position(RID p_shaped, float p_coords) const; // Return caret/selection position.
  276. virtual int shaped_text_next_grapheme_pos(RID p_shaped, int p_pos);
  277. virtual int shaped_text_prev_grapheme_pos(RID p_shaped, int p_pos);
  278. // The pen position is always placed on the baseline and moveing left to right.
  279. virtual void shaped_text_draw(RID p_shaped, RID p_canvas, const Vector2 &p_pos, float p_clip_l = -1.f, float p_clip_r = -1.f, const Color &p_color = Color(1, 1, 1)) const;
  280. virtual void shaped_text_draw_outline(RID p_shaped, RID p_canvas, const Vector2 &p_pos, float p_clip_l = -1.f, float p_clip_r = -1.f, int p_outline_size = 1, const Color &p_color = Color(1, 1, 1)) const;
  281. // Number conversion.
  282. virtual String format_number(const String &p_string, const String &p_language = "") const { return p_string; };
  283. virtual String parse_number(const String &p_string, const String &p_language = "") const { return p_string; };
  284. virtual String percent_sign(const String &p_language = "") const { return "%"; };
  285. /* GDScript wrappers */
  286. RID _create_font_memory(const PackedByteArray &p_data, const String &p_type, int p_base_size = 16);
  287. Array _shaped_text_get_glyphs(RID p_shaped) const;
  288. Dictionary _shaped_text_get_carets(RID p_shaped, int p_position) const;
  289. void _shaped_text_set_bidi_override(RID p_shaped, const Array &p_override);
  290. Array _shaped_text_get_line_breaks_adv(RID p_shaped, const PackedFloat32Array &p_width, int p_start, bool p_once, uint8_t p_break_flags) const;
  291. Array _shaped_text_get_line_breaks(RID p_shaped, float p_width, int p_start, uint8_t p_break_flags) const;
  292. Array _shaped_text_get_word_breaks(RID p_shaped) const;
  293. Array _shaped_text_get_selection(RID p_shaped, int p_start, int p_end) const;
  294. TextServer();
  295. ~TextServer();
  296. };
  297. /*************************************************************************/
  298. class TextServerManager : public Object {
  299. GDCLASS(TextServerManager, Object);
  300. public:
  301. typedef TextServer *(*CreateFunction)(Error &r_error, void *p_user_data);
  302. protected:
  303. static void _bind_methods();
  304. private:
  305. static TextServerManager *singleton;
  306. static TextServer *server;
  307. enum {
  308. MAX_SERVERS = 64
  309. };
  310. struct TextServerCreate {
  311. String name;
  312. CreateFunction create_function = nullptr;
  313. uint32_t features = 0;
  314. TextServer *instance = nullptr;
  315. void *user_data = nullptr;
  316. };
  317. static TextServerCreate server_create_functions[MAX_SERVERS];
  318. static int server_create_count;
  319. public:
  320. _FORCE_INLINE_ static TextServerManager *get_singleton() {
  321. return singleton;
  322. }
  323. static void register_create_function(const String &p_name, uint32_t p_features, CreateFunction p_function, void *p_user_data);
  324. static int get_interface_count();
  325. static String get_interface_name(int p_index);
  326. static uint32_t get_interface_features(int p_index);
  327. static TextServer *initialize(int p_index, Error &r_error);
  328. static TextServer *get_primary_interface();
  329. /* GDScript wrappers */
  330. int _get_interface_count() const;
  331. String _get_interface_name(int p_index) const;
  332. uint32_t _get_interface_features(int p_index) const;
  333. TextServer *_get_interface(int p_index) const;
  334. Array _get_interfaces() const;
  335. TextServer *_find_interface(const String &p_name) const;
  336. bool _set_primary_interface(int p_index);
  337. TextServer *_get_primary_interface() const;
  338. TextServerManager();
  339. ~TextServerManager();
  340. };
  341. /*************************************************************************/
  342. #define TS TextServerManager::get_primary_interface()
  343. VARIANT_ENUM_CAST(TextServer::Direction);
  344. VARIANT_ENUM_CAST(TextServer::Orientation);
  345. VARIANT_ENUM_CAST(TextServer::JustificationFlag);
  346. VARIANT_ENUM_CAST(TextServer::LineBreakFlag);
  347. VARIANT_ENUM_CAST(TextServer::GraphemeFlag);
  348. VARIANT_ENUM_CAST(TextServer::Hinting);
  349. VARIANT_ENUM_CAST(TextServer::Feature);
  350. #endif // TEXT_SERVER_H