text_server.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. /*************************************************************************/
  2. /* text_server.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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/ref_counted.h"
  33. #include "core/os/os.h"
  34. #include "core/templates/rid.h"
  35. #include "core/variant/native_ptr.h"
  36. #include "core/variant/variant.h"
  37. struct Glyph;
  38. struct CaretInfo;
  39. class TextServer : public RefCounted {
  40. GDCLASS(TextServer, RefCounted);
  41. public:
  42. enum Direction {
  43. DIRECTION_AUTO,
  44. DIRECTION_LTR,
  45. DIRECTION_RTL
  46. };
  47. enum Orientation {
  48. ORIENTATION_HORIZONTAL,
  49. ORIENTATION_VERTICAL
  50. };
  51. enum JustificationFlag {
  52. JUSTIFICATION_NONE = 0,
  53. JUSTIFICATION_KASHIDA = 1 << 0,
  54. JUSTIFICATION_WORD_BOUND = 1 << 1,
  55. JUSTIFICATION_TRIM_EDGE_SPACES = 1 << 2,
  56. JUSTIFICATION_AFTER_LAST_TAB = 1 << 3,
  57. JUSTIFICATION_CONSTRAIN_ELLIPSIS = 1 << 4,
  58. };
  59. enum LineBreakFlag { // LineBreakFlag can be passed in the same value as the JustificationFlag, do not use the same values.
  60. BREAK_NONE = 0,
  61. BREAK_MANDATORY = 1 << 5,
  62. BREAK_WORD_BOUND = 1 << 6,
  63. BREAK_GRAPHEME_BOUND = 1 << 7,
  64. BREAK_WORD_BOUND_ADAPTIVE = 1 << 6 | 1 << 8,
  65. };
  66. enum TextOverrunFlag {
  67. OVERRUN_NO_TRIMMING = 0,
  68. OVERRUN_TRIM = 1 << 0,
  69. OVERRUN_TRIM_WORD_ONLY = 1 << 1,
  70. OVERRUN_ADD_ELLIPSIS = 1 << 2,
  71. OVERRUN_ENFORCE_ELLIPSIS = 1 << 3,
  72. OVERRUN_JUSTIFICATION_AWARE = 1 << 4,
  73. };
  74. enum GraphemeFlag {
  75. GRAPHEME_IS_VALID = 1 << 0, // Glyph is valid.
  76. GRAPHEME_IS_RTL = 1 << 1, // Glyph is right-to-left.
  77. GRAPHEME_IS_VIRTUAL = 1 << 2, // Glyph is not part of source string (added by fit_to_width function, do not affect caret movement).
  78. GRAPHEME_IS_SPACE = 1 << 3, // Is whitespace (for justification and word breaks).
  79. GRAPHEME_IS_BREAK_HARD = 1 << 4, // Is line break (mandatory break, e.g. "\n").
  80. GRAPHEME_IS_BREAK_SOFT = 1 << 5, // Is line break (optional break, e.g. space).
  81. GRAPHEME_IS_TAB = 1 << 6, // Is tab or vertical tab.
  82. GRAPHEME_IS_ELONGATION = 1 << 7, // Elongation (e.g. kashida), glyph can be duplicated or truncated to fit line to width.
  83. GRAPHEME_IS_PUNCTUATION = 1 << 8, // Punctuation, except underscore (can be used as word break, but not line break or justifiction).
  84. GRAPHEME_IS_UNDERSCORE = 1 << 9, // Underscore (can be used as word break).
  85. GRAPHEME_IS_CONNECTED = 1 << 10, // Connected to previous grapheme.
  86. };
  87. enum Hinting {
  88. HINTING_NONE,
  89. HINTING_LIGHT,
  90. HINTING_NORMAL
  91. };
  92. enum Feature {
  93. FEATURE_BIDI_LAYOUT = 1 << 0,
  94. FEATURE_VERTICAL_LAYOUT = 1 << 1,
  95. FEATURE_SHAPING = 1 << 2,
  96. FEATURE_KASHIDA_JUSTIFICATION = 1 << 3,
  97. FEATURE_BREAK_ITERATORS = 1 << 4,
  98. FEATURE_FONT_SYSTEM = 1 << 5,
  99. FEATURE_FONT_VARIABLE = 1 << 6,
  100. FEATURE_USE_SUPPORT_DATA = 1 << 7
  101. };
  102. enum ContourPointTag {
  103. CONTOUR_CURVE_TAG_ON = 0x01,
  104. CONTOUR_CURVE_TAG_OFF_CONIC = 0x00,
  105. CONTOUR_CURVE_TAG_OFF_CUBIC = 0x02
  106. };
  107. enum SpacingType {
  108. SPACING_GLYPH,
  109. SPACING_SPACE,
  110. SPACING_TOP,
  111. SPACING_BOTTOM,
  112. };
  113. void _draw_hex_code_box_number(RID p_canvas, int p_size, const Vector2 &p_pos, uint8_t p_index, const Color &p_color) const;
  114. protected:
  115. struct TrimData {
  116. int trim_pos = -1;
  117. int ellipsis_pos = -1;
  118. Vector<Glyph> ellipsis_glyph_buf;
  119. };
  120. struct ShapedTextData {
  121. Mutex mutex;
  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. InlineAlign inline_align = INLINE_ALIGN_CENTER;
  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 text_trimmed = false;
  152. bool preserve_invalid = true; // Draw hex code box instead of missing characters.
  153. bool preserve_control = false; // Draw control characters.
  154. float ascent = 0.f; // Ascent for horizontal layout, 1/2 of width for vertical.
  155. float descent = 0.f; // Descent for horizontal layout, 1/2 of width for vertical.
  156. float width = 0.f; // Width for horizontal layout, height for vertical.
  157. float width_trimmed = 0.f;
  158. float upos = 0.f;
  159. float uthk = 0.f;
  160. TrimData overrun_trim_data;
  161. bool fit_width_minimum_reached = false;
  162. Vector<Glyph> glyphs;
  163. Vector<Glyph> glyphs_logical;
  164. };
  165. Map<char32_t, char32_t> diacritics_map;
  166. void _diacritics_map_add(const String &p_from, char32_t p_to);
  167. void _init_diacritics_map();
  168. static void _bind_methods();
  169. public:
  170. virtual bool has_feature(Feature p_feature) const = 0;
  171. virtual String get_name() const = 0;
  172. virtual uint32_t get_features() const = 0;
  173. virtual void free(RID p_rid) = 0;
  174. virtual bool has(RID p_rid) = 0;
  175. virtual bool load_support_data(const String &p_filename) = 0;
  176. virtual String get_support_data_filename() const = 0;
  177. virtual String get_support_data_info() const = 0;
  178. virtual bool save_support_data(const String &p_filename) const = 0;
  179. virtual bool is_locale_right_to_left(const String &p_locale) const = 0;
  180. virtual int32_t name_to_tag(const String &p_name) const { return 0; };
  181. virtual String tag_to_name(int32_t p_tag) const { return ""; };
  182. /* Font interface */
  183. virtual RID create_font() = 0;
  184. virtual void font_set_data(RID p_font_rid, const PackedByteArray &p_data) = 0;
  185. virtual void font_set_data_ptr(RID p_font_rid, const uint8_t *p_data_ptr, size_t p_data_size) = 0;
  186. virtual void font_set_antialiased(RID p_font_rid, bool p_antialiased) = 0;
  187. virtual bool font_is_antialiased(RID p_font_rid) const = 0;
  188. virtual void font_set_multichannel_signed_distance_field(RID p_font_rid, bool p_msdf) = 0;
  189. virtual bool font_is_multichannel_signed_distance_field(RID p_font_rid) const = 0;
  190. virtual void font_set_msdf_pixel_range(RID p_font_rid, int p_msdf_pixel_range) = 0;
  191. virtual int font_get_msdf_pixel_range(RID p_font_rid) const = 0;
  192. virtual void font_set_msdf_size(RID p_font_rid, int p_msdf_size) = 0;
  193. virtual int font_get_msdf_size(RID p_font_rid) const = 0;
  194. virtual void font_set_fixed_size(RID p_font_rid, int p_fixed_size) = 0;
  195. virtual int font_get_fixed_size(RID p_font_rid) const = 0;
  196. virtual void font_set_force_autohinter(RID p_font_rid, bool p_force_autohinter) = 0;
  197. virtual bool font_is_force_autohinter(RID p_font_rid) const = 0;
  198. virtual void font_set_hinting(RID p_font_rid, Hinting p_hinting) = 0;
  199. virtual Hinting font_get_hinting(RID p_font_rid) const = 0;
  200. virtual void font_set_variation_coordinates(RID p_font_rid, const Dictionary &p_variation_coordinates) = 0;
  201. virtual Dictionary font_get_variation_coordinates(RID p_font_rid) const = 0;
  202. virtual void font_set_oversampling(RID p_font_rid, float p_oversampling) = 0;
  203. virtual float font_get_oversampling(RID p_font_rid) const = 0;
  204. virtual Array font_get_size_cache_list(RID p_font_rid) const = 0;
  205. virtual void font_clear_size_cache(RID p_font_rid) = 0;
  206. virtual void font_remove_size_cache(RID p_font_rid, const Vector2i &p_size) = 0;
  207. virtual void font_set_ascent(RID p_font_rid, int p_size, float p_ascent) = 0;
  208. virtual float font_get_ascent(RID p_font_rid, int p_size) const = 0;
  209. virtual void font_set_descent(RID p_font_rid, int p_size, float p_descent) = 0;
  210. virtual float font_get_descent(RID p_font_rid, int p_size) const = 0;
  211. virtual void font_set_underline_position(RID p_font_rid, int p_size, float p_underline_position) = 0;
  212. virtual float font_get_underline_position(RID p_font_rid, int p_size) const = 0;
  213. virtual void font_set_underline_thickness(RID p_font_rid, int p_size, float p_underline_thickness) = 0;
  214. virtual float font_get_underline_thickness(RID p_font_rid, int p_size) const = 0;
  215. virtual void font_set_scale(RID p_font_rid, int p_size, float p_scale) = 0;
  216. virtual float font_get_scale(RID p_font_rid, int p_size) const = 0;
  217. virtual void font_set_spacing(RID p_font_rid, int p_size, SpacingType p_spacing, int p_value) = 0;
  218. virtual int font_get_spacing(RID p_font_rid, int p_size, SpacingType p_spacing) const = 0;
  219. virtual int font_get_texture_count(RID p_font_rid, const Vector2i &p_size) const = 0;
  220. virtual void font_clear_textures(RID p_font_rid, const Vector2i &p_size) = 0;
  221. virtual void font_remove_texture(RID p_font_rid, const Vector2i &p_size, int p_texture_index) = 0;
  222. virtual void font_set_texture_image(RID p_font_rid, const Vector2i &p_size, int p_texture_index, const Ref<Image> &p_image) = 0;
  223. virtual Ref<Image> font_get_texture_image(RID p_font_rid, const Vector2i &p_size, int p_texture_index) const = 0;
  224. virtual void font_set_texture_offsets(RID p_font_rid, const Vector2i &p_size, int p_texture_index, const PackedInt32Array &p_offset) = 0;
  225. virtual PackedInt32Array font_get_texture_offsets(RID p_font_rid, const Vector2i &p_size, int p_texture_index) const = 0;
  226. virtual Array font_get_glyph_list(RID p_font_rid, const Vector2i &p_size) const = 0;
  227. virtual void font_clear_glyphs(RID p_font_rid, const Vector2i &p_size) = 0;
  228. virtual void font_remove_glyph(RID p_font_rid, const Vector2i &p_size, int32_t p_glyph) = 0;
  229. virtual Vector2 font_get_glyph_advance(RID p_font_rid, int p_size, int32_t p_glyph) const = 0;
  230. virtual void font_set_glyph_advance(RID p_font_rid, int p_size, int32_t p_glyph, const Vector2 &p_advance) = 0;
  231. virtual Vector2 font_get_glyph_offset(RID p_font_rid, const Vector2i &p_size, int32_t p_glyph) const = 0;
  232. virtual void font_set_glyph_offset(RID p_font_rid, const Vector2i &p_size, int32_t p_glyph, const Vector2 &p_offset) = 0;
  233. virtual Vector2 font_get_glyph_size(RID p_font_rid, const Vector2i &p_size, int32_t p_glyph) const = 0;
  234. virtual void font_set_glyph_size(RID p_font_rid, const Vector2i &p_size, int32_t p_glyph, const Vector2 &p_gl_size) = 0;
  235. virtual Rect2 font_get_glyph_uv_rect(RID p_font_rid, const Vector2i &p_size, int32_t p_glyph) const = 0;
  236. virtual void font_set_glyph_uv_rect(RID p_font_rid, const Vector2i &p_size, int32_t p_glyph, const Rect2 &p_uv_rect) = 0;
  237. virtual int font_get_glyph_texture_idx(RID p_font_rid, const Vector2i &p_size, int32_t p_glyph) const = 0;
  238. virtual void font_set_glyph_texture_idx(RID p_font_rid, const Vector2i &p_size, int32_t p_glyph, int p_texture_idx) = 0;
  239. virtual Dictionary font_get_glyph_contours(RID p_font, int p_size, int32_t p_index) const = 0;
  240. virtual Array font_get_kerning_list(RID p_font_rid, int p_size) const = 0;
  241. virtual void font_clear_kerning_map(RID p_font_rid, int p_size) = 0;
  242. virtual void font_remove_kerning(RID p_font_rid, int p_size, const Vector2i &p_glyph_pair) = 0;
  243. virtual void font_set_kerning(RID p_font_rid, int p_size, const Vector2i &p_glyph_pair, const Vector2 &p_kerning) = 0;
  244. virtual Vector2 font_get_kerning(RID p_font_rid, int p_size, const Vector2i &p_glyph_pair) const = 0;
  245. virtual int32_t font_get_glyph_index(RID p_font_rid, int p_size, char32_t p_char, char32_t p_variation_selector) const = 0;
  246. virtual bool font_has_char(RID p_font_rid, char32_t p_char) const = 0;
  247. virtual String font_get_supported_chars(RID p_font_rid) const = 0;
  248. virtual void font_render_range(RID p_font, const Vector2i &p_size, char32_t p_start, char32_t p_end) = 0;
  249. virtual void font_render_glyph(RID p_font_rid, const Vector2i &p_size, int32_t p_index) = 0;
  250. virtual void font_draw_glyph(RID p_font, RID p_canvas, int p_size, const Vector2 &p_pos, int32_t p_index, const Color &p_color = Color(1, 1, 1)) const = 0;
  251. virtual void font_draw_glyph_outline(RID p_font, RID p_canvas, int p_size, int p_outline_size, const Vector2 &p_pos, int32_t p_index, const Color &p_color = Color(1, 1, 1)) const = 0;
  252. virtual bool font_is_language_supported(RID p_font_rid, const String &p_language) const = 0;
  253. virtual void font_set_language_support_override(RID p_font_rid, const String &p_language, bool p_supported) = 0;
  254. virtual bool font_get_language_support_override(RID p_font_rid, const String &p_language) = 0;
  255. virtual void font_remove_language_support_override(RID p_font_rid, const String &p_language) = 0;
  256. virtual Vector<String> font_get_language_support_overrides(RID p_font_rid) = 0;
  257. virtual bool font_is_script_supported(RID p_font_rid, const String &p_script) const = 0;
  258. virtual void font_set_script_support_override(RID p_font_rid, const String &p_script, bool p_supported) = 0;
  259. virtual bool font_get_script_support_override(RID p_font_rid, const String &p_script) = 0;
  260. virtual void font_remove_script_support_override(RID p_font_rid, const String &p_script) = 0;
  261. virtual Vector<String> font_get_script_support_overrides(RID p_font_rid) = 0;
  262. virtual Dictionary font_supported_feature_list(RID p_font_rid) const = 0;
  263. virtual Dictionary font_supported_variation_list(RID p_font_rid) const = 0;
  264. virtual float font_get_global_oversampling() const = 0;
  265. virtual void font_set_global_oversampling(float p_oversampling) = 0;
  266. virtual Vector2 get_hex_code_box_size(int p_size, char32_t p_index) const;
  267. virtual void draw_hex_code_box(RID p_canvas, int p_size, const Vector2 &p_pos, char32_t p_index, const Color &p_color) const;
  268. /* Shaped text buffer interface */
  269. virtual RID create_shaped_text(Direction p_direction = DIRECTION_AUTO, Orientation p_orientation = ORIENTATION_HORIZONTAL) = 0;
  270. virtual void shaped_text_clear(RID p_shaped) = 0;
  271. virtual void shaped_text_set_direction(RID p_shaped, Direction p_direction = DIRECTION_AUTO) = 0;
  272. virtual Direction shaped_text_get_direction(RID p_shaped) const = 0;
  273. virtual void shaped_text_set_bidi_override(RID p_shaped, const Array &p_override) = 0;
  274. virtual void shaped_text_set_orientation(RID p_shaped, Orientation p_orientation = ORIENTATION_HORIZONTAL) = 0;
  275. virtual Orientation shaped_text_get_orientation(RID p_shaped) const = 0;
  276. virtual void shaped_text_set_preserve_invalid(RID p_shaped, bool p_enabled) = 0;
  277. virtual bool shaped_text_get_preserve_invalid(RID p_shaped) const = 0;
  278. virtual void shaped_text_set_preserve_control(RID p_shaped, bool p_enabled) = 0;
  279. virtual bool shaped_text_get_preserve_control(RID p_shaped) const = 0;
  280. 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;
  281. virtual bool shaped_text_add_object(RID p_shaped, Variant p_key, const Size2 &p_size, InlineAlign p_inline_align = INLINE_ALIGN_CENTER, int p_length = 1) = 0;
  282. virtual bool shaped_text_resize_object(RID p_shaped, Variant p_key, const Size2 &p_size, InlineAlign p_inline_align = INLINE_ALIGN_CENTER) = 0;
  283. 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.
  284. virtual RID shaped_text_get_parent(RID p_shaped) const = 0;
  285. virtual float shaped_text_fit_to_width(RID p_shaped, float p_width, uint16_t /*JustificationFlag*/ p_jst_flags = JUSTIFICATION_WORD_BOUND | JUSTIFICATION_KASHIDA) = 0;
  286. virtual float shaped_text_tab_align(RID p_shaped, const PackedFloat32Array &p_tab_stops) = 0;
  287. virtual bool shaped_text_shape(RID p_shaped) = 0;
  288. virtual bool shaped_text_update_breaks(RID p_shaped) = 0;
  289. virtual bool shaped_text_update_justification_ops(RID p_shaped) = 0;
  290. virtual bool shaped_text_is_ready(RID p_shaped) const = 0;
  291. virtual const Glyph *shaped_text_get_glyphs(RID p_shaped) const = 0;
  292. Array _shaped_text_get_glyphs_wrapper(RID p_shaped) const;
  293. virtual const Glyph *shaped_text_sort_logical(RID p_shaped) = 0;
  294. Array _shaped_text_sort_logical_wrapper(RID p_shaped);
  295. virtual int shaped_text_get_glyph_count(RID p_shaped) const = 0;
  296. virtual Vector2i shaped_text_get_range(RID p_shaped) const = 0;
  297. virtual PackedInt32Array shaped_text_get_line_breaks_adv(RID p_shaped, const PackedFloat32Array &p_width, int p_start = 0, bool p_once = true, uint16_t /*TextBreakFlag*/ p_break_flags = BREAK_MANDATORY | BREAK_WORD_BOUND) const;
  298. virtual PackedInt32Array shaped_text_get_line_breaks(RID p_shaped, float p_width, int p_start = 0, uint16_t /*TextBreakFlag*/ p_break_flags = BREAK_MANDATORY | BREAK_WORD_BOUND) const;
  299. virtual PackedInt32Array shaped_text_get_word_breaks(RID p_shaped, int p_grapheme_flags = GRAPHEME_IS_SPACE | GRAPHEME_IS_PUNCTUATION) const;
  300. virtual int shaped_text_get_trim_pos(RID p_shaped) const = 0;
  301. virtual int shaped_text_get_ellipsis_pos(RID p_shaped) const = 0;
  302. virtual const Glyph *shaped_text_get_ellipsis_glyphs(RID p_shaped) const = 0;
  303. Array _shaped_text_get_ellipsis_glyphs_wrapper(RID p_shaped) const;
  304. virtual int shaped_text_get_ellipsis_glyph_count(RID p_shaped) const = 0;
  305. virtual void shaped_text_overrun_trim_to_width(RID p_shaped, float p_width, uint16_t p_trim_flags) = 0;
  306. virtual Array shaped_text_get_objects(RID p_shaped) const = 0;
  307. virtual Rect2 shaped_text_get_object_rect(RID p_shaped, Variant p_key) const = 0;
  308. virtual Size2 shaped_text_get_size(RID p_shaped) const = 0;
  309. virtual float shaped_text_get_ascent(RID p_shaped) const = 0;
  310. virtual float shaped_text_get_descent(RID p_shaped) const = 0;
  311. virtual float shaped_text_get_width(RID p_shaped) const = 0;
  312. virtual float shaped_text_get_underline_position(RID p_shaped) const = 0;
  313. virtual float shaped_text_get_underline_thickness(RID p_shaped) const = 0;
  314. virtual Direction shaped_text_get_dominant_direction_in_range(RID p_shaped, int p_start, int p_end) const;
  315. virtual CaretInfo shaped_text_get_carets(RID p_shaped, int p_position) const;
  316. Dictionary _shaped_text_get_carets_wrapper(RID p_shaped, int p_position) const;
  317. virtual Vector<Vector2> shaped_text_get_selection(RID p_shaped, int p_start, int p_end) const;
  318. virtual int shaped_text_hit_test_grapheme(RID p_shaped, float p_coords) const; // Return grapheme index.
  319. virtual int shaped_text_hit_test_position(RID p_shaped, float p_coords) const; // Return caret/selection position.
  320. virtual int shaped_text_next_grapheme_pos(RID p_shaped, int p_pos) const;
  321. virtual int shaped_text_prev_grapheme_pos(RID p_shaped, int p_pos) const;
  322. // The pen position is always placed on the baseline and moveing left to right.
  323. 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;
  324. 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;
  325. // Number conversion.
  326. virtual String format_number(const String &p_string, const String &p_language = "") const { return p_string; };
  327. virtual String parse_number(const String &p_string, const String &p_language = "") const { return p_string; };
  328. virtual String percent_sign(const String &p_language = "") const { return "%"; };
  329. virtual String strip_diacritics(const String &p_string) const;
  330. TextServer();
  331. ~TextServer();
  332. };
  333. /*************************************************************************/
  334. struct Glyph {
  335. int start = -1; // Start offset in the source string.
  336. int end = -1; // End offset in the source string.
  337. uint8_t count = 0; // Number of glyphs in the grapheme, set in the first glyph only.
  338. uint8_t repeat = 1; // Draw multiple times in the row.
  339. uint16_t flags = 0; // Grapheme flags (valid, rtl, virtual), set in the first glyph only.
  340. float x_off = 0.f; // Offset from the origin of the glyph on baseline.
  341. float y_off = 0.f;
  342. float advance = 0.f; // Advance to the next glyph along baseline(x for horizontal layout, y for vertical).
  343. RID font_rid; // Font resource.
  344. int font_size = 0; // Font size;
  345. int32_t index = 0; // Glyph index (font specific) or UTF-32 codepoint (for the invalid glyphs).
  346. bool operator==(const Glyph &p_a) const;
  347. bool operator!=(const Glyph &p_a) const;
  348. bool operator<(const Glyph &p_a) const;
  349. bool operator>(const Glyph &p_a) const;
  350. };
  351. struct CaretInfo {
  352. Rect2 l_caret;
  353. Rect2 t_caret;
  354. TextServer::Direction l_dir;
  355. TextServer::Direction t_dir;
  356. };
  357. struct GlyphCompare { // For line breaking reordering.
  358. _FORCE_INLINE_ bool operator()(const Glyph &l, const Glyph &r) const {
  359. if (l.start == r.start) {
  360. if (l.count == r.count) {
  361. if ((l.flags & TextServer::GRAPHEME_IS_VIRTUAL) == TextServer::GRAPHEME_IS_VIRTUAL) {
  362. return false;
  363. } else {
  364. return true;
  365. }
  366. }
  367. return l.count > r.count; // Sort first glyph with count & flags, order of the rest are irrelevant.
  368. } else {
  369. return l.start < r.start;
  370. }
  371. }
  372. };
  373. /*************************************************************************/
  374. class TextServerManager : public Object {
  375. GDCLASS(TextServerManager, Object);
  376. protected:
  377. static void _bind_methods();
  378. private:
  379. static TextServerManager *singleton;
  380. Ref<TextServer> primary_interface;
  381. Vector<Ref<TextServer>> interfaces;
  382. public:
  383. _FORCE_INLINE_ static TextServerManager *get_singleton() {
  384. return singleton;
  385. }
  386. void add_interface(const Ref<TextServer> &p_interface);
  387. void remove_interface(const Ref<TextServer> &p_interface);
  388. int get_interface_count() const;
  389. Ref<TextServer> get_interface(int p_index) const;
  390. Ref<TextServer> find_interface(const String &p_name) const;
  391. Array get_interfaces() const;
  392. _FORCE_INLINE_ Ref<TextServer> get_primary_interface() const {
  393. return primary_interface;
  394. }
  395. void set_primary_interface(const Ref<TextServer> &p_primary_interface);
  396. TextServerManager();
  397. ~TextServerManager();
  398. };
  399. /*************************************************************************/
  400. #define TS TextServerManager::get_singleton()->get_primary_interface()
  401. VARIANT_ENUM_CAST(TextServer::Direction);
  402. VARIANT_ENUM_CAST(TextServer::Orientation);
  403. VARIANT_ENUM_CAST(TextServer::JustificationFlag);
  404. VARIANT_ENUM_CAST(TextServer::LineBreakFlag);
  405. VARIANT_ENUM_CAST(TextServer::TextOverrunFlag);
  406. VARIANT_ENUM_CAST(TextServer::GraphemeFlag);
  407. VARIANT_ENUM_CAST(TextServer::Hinting);
  408. VARIANT_ENUM_CAST(TextServer::Feature);
  409. VARIANT_ENUM_CAST(TextServer::ContourPointTag);
  410. VARIANT_ENUM_CAST(TextServer::SpacingType);
  411. GDVIRTUAL_NATIVE_PTR(Glyph);
  412. GDVIRTUAL_NATIVE_PTR(Glyph *);
  413. GDVIRTUAL_NATIVE_PTR(CaretInfo);
  414. #endif // TEXT_SERVER_H