text_server.h 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  1. /**************************************************************************/
  2. /* text_server.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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. #pragma once
  31. #include "core/io/image.h"
  32. #include "core/object/ref_counted.h"
  33. #include "core/templates/rid.h"
  34. #include "core/variant/native_ptr.h"
  35. #include "core/variant/variant.h"
  36. template <typename T>
  37. class TypedArray;
  38. struct Glyph;
  39. struct CaretInfo;
  40. #define OT_TAG(m_c1, m_c2, m_c3, m_c4) ((int32_t)((((uint32_t)(m_c1) & 0xff) << 24) | (((uint32_t)(m_c2) & 0xff) << 16) | (((uint32_t)(m_c3) & 0xff) << 8) | ((uint32_t)(m_c4) & 0xff)))
  41. class TextServer : public RefCounted {
  42. GDCLASS(TextServer, RefCounted);
  43. public:
  44. enum FontAntialiasing {
  45. FONT_ANTIALIASING_NONE,
  46. FONT_ANTIALIASING_GRAY,
  47. FONT_ANTIALIASING_LCD,
  48. };
  49. enum FontLCDSubpixelLayout {
  50. FONT_LCD_SUBPIXEL_LAYOUT_NONE,
  51. FONT_LCD_SUBPIXEL_LAYOUT_HRGB,
  52. FONT_LCD_SUBPIXEL_LAYOUT_HBGR,
  53. FONT_LCD_SUBPIXEL_LAYOUT_VRGB,
  54. FONT_LCD_SUBPIXEL_LAYOUT_VBGR,
  55. FONT_LCD_SUBPIXEL_LAYOUT_MAX,
  56. };
  57. enum Direction {
  58. DIRECTION_AUTO,
  59. DIRECTION_LTR,
  60. DIRECTION_RTL,
  61. DIRECTION_INHERITED,
  62. };
  63. enum Orientation {
  64. ORIENTATION_HORIZONTAL,
  65. ORIENTATION_VERTICAL
  66. };
  67. enum JustificationFlag {
  68. JUSTIFICATION_NONE = 0,
  69. JUSTIFICATION_KASHIDA = 1 << 0,
  70. JUSTIFICATION_WORD_BOUND = 1 << 1,
  71. JUSTIFICATION_TRIM_EDGE_SPACES = 1 << 2,
  72. JUSTIFICATION_AFTER_LAST_TAB = 1 << 3,
  73. JUSTIFICATION_CONSTRAIN_ELLIPSIS = 1 << 4,
  74. JUSTIFICATION_SKIP_LAST_LINE = 1 << 5,
  75. JUSTIFICATION_SKIP_LAST_LINE_WITH_VISIBLE_CHARS = 1 << 6,
  76. JUSTIFICATION_DO_NOT_SKIP_SINGLE_LINE = 1 << 7,
  77. };
  78. enum VisibleCharactersBehavior {
  79. VC_CHARS_BEFORE_SHAPING,
  80. VC_CHARS_AFTER_SHAPING,
  81. VC_GLYPHS_AUTO,
  82. VC_GLYPHS_LTR,
  83. VC_GLYPHS_RTL,
  84. };
  85. enum AutowrapMode {
  86. AUTOWRAP_OFF,
  87. AUTOWRAP_ARBITRARY,
  88. AUTOWRAP_WORD,
  89. AUTOWRAP_WORD_SMART
  90. };
  91. enum LineBreakFlag {
  92. BREAK_NONE = 0,
  93. BREAK_MANDATORY = 1 << 0,
  94. BREAK_WORD_BOUND = 1 << 1,
  95. BREAK_GRAPHEME_BOUND = 1 << 2,
  96. BREAK_ADAPTIVE = 1 << 3,
  97. #ifndef DISABLE_DEPRECATED
  98. BREAK_TRIM_EDGE_SPACES = 1 << 4,
  99. #else
  100. // RESERVED = 1 << 4,
  101. #endif
  102. BREAK_TRIM_INDENT = 1 << 5,
  103. BREAK_TRIM_START_EDGE_SPACES = 1 << 6,
  104. BREAK_TRIM_END_EDGE_SPACES = 1 << 7,
  105. BREAK_TRIM_MASK = BREAK_TRIM_INDENT | BREAK_TRIM_START_EDGE_SPACES | BREAK_TRIM_END_EDGE_SPACES,
  106. };
  107. enum OverrunBehavior {
  108. OVERRUN_NO_TRIMMING,
  109. OVERRUN_TRIM_CHAR,
  110. OVERRUN_TRIM_WORD,
  111. OVERRUN_TRIM_ELLIPSIS,
  112. OVERRUN_TRIM_WORD_ELLIPSIS,
  113. OVERRUN_TRIM_ELLIPSIS_FORCE,
  114. OVERRUN_TRIM_WORD_ELLIPSIS_FORCE,
  115. };
  116. enum TextOverrunFlag {
  117. OVERRUN_NO_TRIM = 0,
  118. OVERRUN_TRIM = 1 << 0,
  119. OVERRUN_TRIM_WORD_ONLY = 1 << 1,
  120. OVERRUN_ADD_ELLIPSIS = 1 << 2,
  121. OVERRUN_ENFORCE_ELLIPSIS = 1 << 3,
  122. OVERRUN_JUSTIFICATION_AWARE = 1 << 4,
  123. };
  124. enum GraphemeFlag {
  125. GRAPHEME_IS_VALID = 1 << 0, // Grapheme is valid.
  126. GRAPHEME_IS_RTL = 1 << 1, // Grapheme is right-to-left.
  127. GRAPHEME_IS_VIRTUAL = 1 << 2, // Grapheme is not part of source string (added by fit_to_width function, do not affect caret movement).
  128. GRAPHEME_IS_SPACE = 1 << 3, // Is whitespace (for justification and word breaks).
  129. GRAPHEME_IS_BREAK_HARD = 1 << 4, // Is line break (mandatory break, e.g. "\n").
  130. GRAPHEME_IS_BREAK_SOFT = 1 << 5, // Is line break (optional break, e.g. space).
  131. GRAPHEME_IS_TAB = 1 << 6, // Is tab or vertical tab.
  132. GRAPHEME_IS_ELONGATION = 1 << 7, // Elongation (e.g. kashida), grapheme can be duplicated or truncated to fit line to width.
  133. GRAPHEME_IS_PUNCTUATION = 1 << 8, // Punctuation, except underscore (can be used as word break, but not line break or justifiction).
  134. GRAPHEME_IS_UNDERSCORE = 1 << 9, // Underscore (can be used as word break).
  135. GRAPHEME_IS_CONNECTED = 1 << 10, // Connected to previous grapheme.
  136. GRAPHEME_IS_SAFE_TO_INSERT_TATWEEL = 1 << 11, // It is safe to insert a U+0640 before this grapheme for elongation.
  137. GRAPHEME_IS_EMBEDDED_OBJECT = 1 << 12, // Grapheme is an object replacement character for the embedded object.
  138. GRAPHEME_IS_SOFT_HYPHEN = 1 << 13, // Grapheme is a soft hyphen.
  139. };
  140. enum Hinting {
  141. HINTING_NONE,
  142. HINTING_LIGHT,
  143. HINTING_NORMAL
  144. };
  145. enum SubpixelPositioning {
  146. SUBPIXEL_POSITIONING_DISABLED = 0,
  147. SUBPIXEL_POSITIONING_AUTO = 1,
  148. SUBPIXEL_POSITIONING_ONE_HALF = 2,
  149. SUBPIXEL_POSITIONING_ONE_QUARTER = 3,
  150. SUBPIXEL_POSITIONING_ONE_HALF_MAX_SIZE = 20,
  151. SUBPIXEL_POSITIONING_ONE_QUARTER_MAX_SIZE = 16,
  152. };
  153. enum Feature {
  154. FEATURE_SIMPLE_LAYOUT = 1 << 0,
  155. FEATURE_BIDI_LAYOUT = 1 << 1,
  156. FEATURE_VERTICAL_LAYOUT = 1 << 2,
  157. FEATURE_SHAPING = 1 << 3,
  158. FEATURE_KASHIDA_JUSTIFICATION = 1 << 4,
  159. FEATURE_BREAK_ITERATORS = 1 << 5,
  160. FEATURE_FONT_BITMAP = 1 << 6,
  161. FEATURE_FONT_DYNAMIC = 1 << 7,
  162. FEATURE_FONT_MSDF = 1 << 8,
  163. FEATURE_FONT_SYSTEM = 1 << 9,
  164. FEATURE_FONT_VARIABLE = 1 << 10,
  165. FEATURE_CONTEXT_SENSITIVE_CASE_CONVERSION = 1 << 11,
  166. FEATURE_USE_SUPPORT_DATA = 1 << 12,
  167. FEATURE_UNICODE_IDENTIFIERS = 1 << 13,
  168. FEATURE_UNICODE_SECURITY = 1 << 14,
  169. };
  170. enum ContourPointTag {
  171. CONTOUR_CURVE_TAG_ON = 0x01,
  172. CONTOUR_CURVE_TAG_OFF_CONIC = 0x00,
  173. CONTOUR_CURVE_TAG_OFF_CUBIC = 0x02
  174. };
  175. enum SpacingType {
  176. SPACING_GLYPH,
  177. SPACING_SPACE,
  178. SPACING_TOP,
  179. SPACING_BOTTOM,
  180. SPACING_MAX,
  181. };
  182. enum FontStyle {
  183. FONT_BOLD = 1 << 0,
  184. FONT_ITALIC = 1 << 1,
  185. FONT_FIXED_WIDTH = 1 << 2,
  186. };
  187. enum StructuredTextParser {
  188. STRUCTURED_TEXT_DEFAULT,
  189. STRUCTURED_TEXT_URI,
  190. STRUCTURED_TEXT_FILE,
  191. STRUCTURED_TEXT_EMAIL,
  192. STRUCTURED_TEXT_LIST,
  193. STRUCTURED_TEXT_GDSCRIPT,
  194. STRUCTURED_TEXT_CUSTOM
  195. };
  196. enum FixedSizeScaleMode {
  197. FIXED_SIZE_SCALE_DISABLE,
  198. FIXED_SIZE_SCALE_INTEGER_ONLY,
  199. FIXED_SIZE_SCALE_ENABLED,
  200. };
  201. void _draw_hex_code_box_number(const RID &p_canvas, int64_t p_size, const Vector2 &p_pos, uint8_t p_index, const Color &p_color) const;
  202. protected:
  203. double vp_oversampling = 0.0;
  204. HashMap<char32_t, char32_t> diacritics_map;
  205. void _diacritics_map_add(const String &p_from, char32_t p_to);
  206. void _init_diacritics_map();
  207. static void _bind_methods();
  208. #ifndef DISABLE_DEPRECATED
  209. void _font_draw_glyph_bind_compat_104872(const RID &p_font, const RID &p_canvas, int64_t p_size, const Vector2 &p_pos, int64_t p_index, const Color &p_color = Color(1, 1, 1)) const;
  210. void _font_draw_glyph_outline_bind_compat_104872(const RID &p_font, const RID &p_canvas, int64_t p_size, int64_t p_outline_size, const Vector2 &p_pos, int64_t p_index, const Color &p_color = Color(1, 1, 1)) const;
  211. void _shaped_text_draw_bind_compat_104872(const RID &p_shaped, const RID &p_canvas, const Vector2 &p_pos, double p_clip_l = -1.0, double p_clip_r = -1.0, const Color &p_color = Color(1, 1, 1)) const;
  212. void _shaped_text_draw_outline_bind_compat_104872(const RID &p_shaped, const RID &p_canvas, const Vector2 &p_pos, double p_clip_l = -1.0, double p_clip_r = -1.0, int64_t p_outline_size = 1, const Color &p_color = Color(1, 1, 1)) const;
  213. PackedInt32Array _shaped_text_get_word_breaks_bind_compat_90732(const RID &p_shaped, BitField<TextServer::GraphemeFlag> p_grapheme_flags = GRAPHEME_IS_SPACE | GRAPHEME_IS_PUNCTUATION) const;
  214. static void _bind_compatibility_methods();
  215. #endif
  216. public:
  217. virtual bool has_feature(Feature p_feature) const = 0;
  218. virtual String get_name() const = 0;
  219. virtual int64_t get_features() const = 0;
  220. virtual void free_rid(const RID &p_rid) = 0;
  221. virtual bool has(const RID &p_rid) = 0;
  222. virtual bool load_support_data(const String &p_filename) = 0;
  223. virtual String get_support_data_filename() const = 0;
  224. virtual String get_support_data_info() const = 0;
  225. virtual bool save_support_data(const String &p_filename) const = 0;
  226. virtual PackedByteArray get_support_data() const = 0;
  227. virtual bool is_locale_right_to_left(const String &p_locale) const = 0;
  228. virtual int64_t name_to_tag(const String &p_name) const;
  229. virtual String tag_to_name(int64_t p_tag) const;
  230. /* Font interface */
  231. virtual RID create_font() = 0;
  232. virtual RID create_font_linked_variation(const RID &p_font_rid) = 0;
  233. virtual void font_set_data(const RID &p_font_rid, const PackedByteArray &p_data) = 0;
  234. virtual void font_set_data_ptr(const RID &p_font_rid, const uint8_t *p_data_ptr, int64_t p_data_size) = 0;
  235. virtual void font_set_face_index(const RID &p_font_rid, int64_t p_index) = 0;
  236. virtual int64_t font_get_face_index(const RID &p_font_rid) const = 0;
  237. virtual int64_t font_get_face_count(const RID &p_font_rid) const = 0;
  238. virtual void font_set_style(const RID &p_font_rid, BitField<FontStyle> p_style) = 0;
  239. virtual BitField<FontStyle> font_get_style(const RID &p_font_rid) const = 0;
  240. virtual void font_set_name(const RID &p_font_rid, const String &p_name) = 0;
  241. virtual String font_get_name(const RID &p_font_rid) const = 0;
  242. virtual Dictionary font_get_ot_name_strings(const RID &p_font_rid) const { return Dictionary(); }
  243. virtual void font_set_style_name(const RID &p_font_rid, const String &p_name) = 0;
  244. virtual String font_get_style_name(const RID &p_font_rid) const = 0;
  245. virtual void font_set_weight(const RID &p_font_rid, int64_t p_weight) = 0;
  246. virtual int64_t font_get_weight(const RID &p_font_rid) const = 0;
  247. virtual void font_set_stretch(const RID &p_font_rid, int64_t p_stretch) = 0;
  248. virtual int64_t font_get_stretch(const RID &p_font_rid) const = 0;
  249. virtual void font_set_antialiasing(const RID &p_font_rid, FontAntialiasing p_antialiasing) = 0;
  250. virtual FontAntialiasing font_get_antialiasing(const RID &p_font_rid) const = 0;
  251. virtual void font_set_disable_embedded_bitmaps(const RID &p_font_rid, bool p_disable_embedded_bitmaps) = 0;
  252. virtual bool font_get_disable_embedded_bitmaps(const RID &p_font_rid) const = 0;
  253. virtual void font_set_generate_mipmaps(const RID &p_font_rid, bool p_generate_mipmaps) = 0;
  254. virtual bool font_get_generate_mipmaps(const RID &p_font_rid) const = 0;
  255. virtual void font_set_multichannel_signed_distance_field(const RID &p_font_rid, bool p_msdf) = 0;
  256. virtual bool font_is_multichannel_signed_distance_field(const RID &p_font_rid) const = 0;
  257. virtual void font_set_msdf_pixel_range(const RID &p_font_rid, int64_t p_msdf_pixel_range) = 0;
  258. virtual int64_t font_get_msdf_pixel_range(const RID &p_font_rid) const = 0;
  259. virtual void font_set_msdf_size(const RID &p_font_rid, int64_t p_msdf_size) = 0;
  260. virtual int64_t font_get_msdf_size(const RID &p_font_rid) const = 0;
  261. virtual void font_set_fixed_size(const RID &p_font_rid, int64_t p_fixed_size) = 0;
  262. virtual int64_t font_get_fixed_size(const RID &p_font_rid) const = 0;
  263. virtual void font_set_fixed_size_scale_mode(const RID &p_font_rid, FixedSizeScaleMode p_fixed_size_scale) = 0;
  264. virtual FixedSizeScaleMode font_get_fixed_size_scale_mode(const RID &p_font_rid) const = 0;
  265. virtual void font_set_allow_system_fallback(const RID &p_font_rid, bool p_allow_system_fallback) = 0;
  266. virtual bool font_is_allow_system_fallback(const RID &p_font_rid) const = 0;
  267. virtual void font_clear_system_fallback_cache() {}
  268. virtual void font_set_force_autohinter(const RID &p_font_rid, bool p_force_autohinter) = 0;
  269. virtual bool font_is_force_autohinter(const RID &p_font_rid) const = 0;
  270. virtual void font_set_modulate_color_glyphs(const RID &p_font_rid, bool p_modulate) = 0;
  271. virtual bool font_is_modulate_color_glyphs(const RID &p_font_rid) const = 0;
  272. virtual void font_set_hinting(const RID &p_font_rid, Hinting p_hinting) = 0;
  273. virtual Hinting font_get_hinting(const RID &p_font_rid) const = 0;
  274. virtual void font_set_subpixel_positioning(const RID &p_font_rid, SubpixelPositioning p_subpixel) = 0;
  275. virtual SubpixelPositioning font_get_subpixel_positioning(const RID &p_font_rid) const = 0;
  276. virtual void font_set_keep_rounding_remainders(const RID &p_font_rid, bool p_keep_rounding_remainders) = 0;
  277. virtual bool font_get_keep_rounding_remainders(const RID &p_font_rid) const = 0;
  278. virtual void font_set_embolden(const RID &p_font_rid, double p_strength) = 0;
  279. virtual double font_get_embolden(const RID &p_font_rid) const = 0;
  280. virtual void font_set_spacing(const RID &p_font_rid, SpacingType p_spacing, int64_t p_value) = 0;
  281. virtual int64_t font_get_spacing(const RID &p_font_rid, SpacingType p_spacing) const = 0;
  282. virtual void font_set_baseline_offset(const RID &p_font_rid, double p_baseline_offset) = 0;
  283. virtual double font_get_baseline_offset(const RID &p_font_rid) const = 0;
  284. virtual void font_set_transform(const RID &p_font_rid, const Transform2D &p_transform) = 0;
  285. virtual Transform2D font_get_transform(const RID &p_font_rid) const = 0;
  286. virtual void font_set_variation_coordinates(const RID &p_font_rid, const Dictionary &p_variation_coordinates) = 0;
  287. virtual Dictionary font_get_variation_coordinates(const RID &p_font_rid) const = 0;
  288. #ifndef DISABLE_DEPRECATED
  289. virtual void font_set_oversampling(const RID &p_font_rid, double p_oversampling) {}
  290. virtual double font_get_oversampling(const RID &p_font_rid) const { return 1.0; }
  291. #endif
  292. virtual TypedArray<Vector2i> font_get_size_cache_list(const RID &p_font_rid) const = 0;
  293. virtual void font_clear_size_cache(const RID &p_font_rid) = 0;
  294. virtual void font_remove_size_cache(const RID &p_font_rid, const Vector2i &p_size) = 0;
  295. virtual TypedArray<Dictionary> font_get_size_cache_info(const RID &p_font_rid) const = 0;
  296. virtual void font_set_ascent(const RID &p_font_rid, int64_t p_size, double p_ascent) = 0;
  297. virtual double font_get_ascent(const RID &p_font_rid, int64_t p_size) const = 0;
  298. virtual void font_set_descent(const RID &p_font_rid, int64_t p_size, double p_descent) = 0;
  299. virtual double font_get_descent(const RID &p_font_rid, int64_t p_size) const = 0;
  300. virtual void font_set_underline_position(const RID &p_font_rid, int64_t p_size, double p_underline_position) = 0;
  301. virtual double font_get_underline_position(const RID &p_font_rid, int64_t p_size) const = 0;
  302. virtual void font_set_underline_thickness(const RID &p_font_rid, int64_t p_size, double p_underline_thickness) = 0;
  303. virtual double font_get_underline_thickness(const RID &p_font_rid, int64_t p_size) const = 0;
  304. virtual void font_set_scale(const RID &p_font_rid, int64_t p_size, double p_scale) = 0;
  305. virtual double font_get_scale(const RID &p_font_rid, int64_t p_size) const = 0;
  306. virtual int64_t font_get_texture_count(const RID &p_font_rid, const Vector2i &p_size) const = 0;
  307. virtual void font_clear_textures(const RID &p_font_rid, const Vector2i &p_size) = 0;
  308. virtual void font_remove_texture(const RID &p_font_rid, const Vector2i &p_size, int64_t p_texture_index) = 0;
  309. virtual void font_set_texture_image(const RID &p_font_rid, const Vector2i &p_size, int64_t p_texture_index, const Ref<Image> &p_image) = 0;
  310. virtual Ref<Image> font_get_texture_image(const RID &p_font_rid, const Vector2i &p_size, int64_t p_texture_index) const = 0;
  311. virtual void font_set_texture_offsets(const RID &p_font_rid, const Vector2i &p_size, int64_t p_texture_index, const PackedInt32Array &p_offset) = 0;
  312. virtual PackedInt32Array font_get_texture_offsets(const RID &p_font_rid, const Vector2i &p_size, int64_t p_texture_index) const = 0;
  313. virtual PackedInt32Array font_get_glyph_list(const RID &p_font_rid, const Vector2i &p_size) const = 0;
  314. virtual void font_clear_glyphs(const RID &p_font_rid, const Vector2i &p_size) = 0;
  315. virtual void font_remove_glyph(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph) = 0;
  316. virtual Vector2 font_get_glyph_advance(const RID &p_font_rid, int64_t p_size, int64_t p_glyph) const = 0;
  317. virtual void font_set_glyph_advance(const RID &p_font_rid, int64_t p_size, int64_t p_glyph, const Vector2 &p_advance) = 0;
  318. virtual Vector2 font_get_glyph_offset(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph) const = 0;
  319. virtual void font_set_glyph_offset(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph, const Vector2 &p_offset) = 0;
  320. virtual Vector2 font_get_glyph_size(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph) const = 0;
  321. virtual void font_set_glyph_size(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph, const Vector2 &p_gl_size) = 0;
  322. virtual Rect2 font_get_glyph_uv_rect(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph) const = 0;
  323. virtual void font_set_glyph_uv_rect(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph, const Rect2 &p_uv_rect) = 0;
  324. virtual int64_t font_get_glyph_texture_idx(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph) const = 0;
  325. virtual void font_set_glyph_texture_idx(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph, int64_t p_texture_idx) = 0;
  326. virtual RID font_get_glyph_texture_rid(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph) const = 0;
  327. virtual Size2 font_get_glyph_texture_size(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph) const = 0;
  328. virtual Dictionary font_get_glyph_contours(const RID &p_font, int64_t p_size, int64_t p_index) const = 0;
  329. virtual TypedArray<Vector2i> font_get_kerning_list(const RID &p_font_rid, int64_t p_size) const = 0;
  330. virtual void font_clear_kerning_map(const RID &p_font_rid, int64_t p_size) = 0;
  331. virtual void font_remove_kerning(const RID &p_font_rid, int64_t p_size, const Vector2i &p_glyph_pair) = 0;
  332. virtual void font_set_kerning(const RID &p_font_rid, int64_t p_size, const Vector2i &p_glyph_pair, const Vector2 &p_kerning) = 0;
  333. virtual Vector2 font_get_kerning(const RID &p_font_rid, int64_t p_size, const Vector2i &p_glyph_pair) const = 0;
  334. virtual int64_t font_get_glyph_index(const RID &p_font_rid, int64_t p_size, int64_t p_char, int64_t p_variation_selector) const = 0;
  335. virtual int64_t font_get_char_from_glyph_index(const RID &p_font_rid, int64_t p_size, int64_t p_glyph_index) const = 0;
  336. virtual bool font_has_char(const RID &p_font_rid, int64_t p_char) const = 0;
  337. virtual String font_get_supported_chars(const RID &p_font_rid) const = 0;
  338. virtual PackedInt32Array font_get_supported_glyphs(const RID &p_font_rid) const = 0;
  339. virtual void font_render_range(const RID &p_font, const Vector2i &p_size, int64_t p_start, int64_t p_end) = 0;
  340. virtual void font_render_glyph(const RID &p_font_rid, const Vector2i &p_size, int64_t p_index) = 0;
  341. virtual void font_draw_glyph(const RID &p_font, const RID &p_canvas, int64_t p_size, const Vector2 &p_pos, int64_t p_index, const Color &p_color = Color(1, 1, 1), float p_oversampling = 0.0) const = 0;
  342. virtual void font_draw_glyph_outline(const RID &p_font, const RID &p_canvas, int64_t p_size, int64_t p_outline_size, const Vector2 &p_pos, int64_t p_index, const Color &p_color = Color(1, 1, 1), float p_oversampling = 0.0) const = 0;
  343. virtual bool font_is_language_supported(const RID &p_font_rid, const String &p_language) const = 0;
  344. virtual void font_set_language_support_override(const RID &p_font_rid, const String &p_language, bool p_supported) = 0;
  345. virtual bool font_get_language_support_override(const RID &p_font_rid, const String &p_language) = 0;
  346. virtual void font_remove_language_support_override(const RID &p_font_rid, const String &p_language) = 0;
  347. virtual PackedStringArray font_get_language_support_overrides(const RID &p_font_rid) = 0;
  348. virtual bool font_is_script_supported(const RID &p_font_rid, const String &p_script) const = 0;
  349. virtual void font_set_script_support_override(const RID &p_font_rid, const String &p_script, bool p_supported) = 0;
  350. virtual bool font_get_script_support_override(const RID &p_font_rid, const String &p_script) = 0;
  351. virtual void font_remove_script_support_override(const RID &p_font_rid, const String &p_script) = 0;
  352. virtual PackedStringArray font_get_script_support_overrides(const RID &p_font_rid) = 0;
  353. virtual void font_set_opentype_feature_overrides(const RID &p_font_rid, const Dictionary &p_overrides) = 0;
  354. virtual Dictionary font_get_opentype_feature_overrides(const RID &p_font_rid) const = 0;
  355. virtual Dictionary font_supported_feature_list(const RID &p_font_rid) const = 0;
  356. virtual Dictionary font_supported_variation_list(const RID &p_font_rid) const = 0;
  357. #ifndef DISABLE_DEPRECATED
  358. virtual double font_get_global_oversampling() const { return 1.0; }
  359. virtual void font_set_global_oversampling(double p_oversampling) {}
  360. #endif
  361. virtual void reference_oversampling_level(double p_oversampling) {}
  362. virtual void unreference_oversampling_level(double p_oversampling) {}
  363. virtual Vector2 get_hex_code_box_size(int64_t p_size, int64_t p_index) const;
  364. virtual void draw_hex_code_box(const RID &p_canvas, int64_t p_size, const Vector2 &p_pos, int64_t p_index, const Color &p_color) const;
  365. /* Shaped text buffer interface */
  366. virtual RID create_shaped_text(Direction p_direction = DIRECTION_AUTO, Orientation p_orientation = ORIENTATION_HORIZONTAL) = 0;
  367. virtual void shaped_text_clear(const RID &p_shaped) = 0;
  368. virtual void shaped_text_set_direction(const RID &p_shaped, Direction p_direction = DIRECTION_AUTO) = 0;
  369. virtual Direction shaped_text_get_direction(const RID &p_shaped) const = 0;
  370. virtual Direction shaped_text_get_inferred_direction(const RID &p_shaped) const = 0;
  371. virtual void shaped_text_set_bidi_override(const RID &p_shaped, const Array &p_override) = 0;
  372. virtual void shaped_text_set_custom_punctuation(const RID &p_shaped, const String &p_punct) = 0;
  373. virtual String shaped_text_get_custom_punctuation(const RID &p_shaped) const = 0;
  374. virtual void shaped_text_set_custom_ellipsis(const RID &p_shaped, int64_t p_char) = 0;
  375. virtual int64_t shaped_text_get_custom_ellipsis(const RID &p_shaped) const = 0;
  376. virtual void shaped_text_set_orientation(const RID &p_shaped, Orientation p_orientation = ORIENTATION_HORIZONTAL) = 0;
  377. virtual Orientation shaped_text_get_orientation(const RID &p_shaped) const = 0;
  378. virtual void shaped_text_set_preserve_invalid(const RID &p_shaped, bool p_enabled) = 0;
  379. virtual bool shaped_text_get_preserve_invalid(const RID &p_shaped) const = 0;
  380. virtual void shaped_text_set_preserve_control(const RID &p_shaped, bool p_enabled) = 0;
  381. virtual bool shaped_text_get_preserve_control(const RID &p_shaped) const = 0;
  382. virtual void shaped_text_set_spacing(const RID &p_shaped, SpacingType p_spacing, int64_t p_value) = 0;
  383. virtual int64_t shaped_text_get_spacing(const RID &p_shaped, SpacingType p_spacing) const = 0;
  384. virtual bool shaped_text_add_string(const RID &p_shaped, const String &p_text, const TypedArray<RID> &p_fonts, int64_t p_size, const Dictionary &p_opentype_features = Dictionary(), const String &p_language = "", const Variant &p_meta = Variant()) = 0;
  385. virtual bool shaped_text_add_object(const RID &p_shaped, const Variant &p_key, const Size2 &p_size, InlineAlignment p_inline_align = INLINE_ALIGNMENT_CENTER, int64_t p_length = 1, double p_baseline = 0.0) = 0;
  386. virtual bool shaped_text_resize_object(const RID &p_shaped, const Variant &p_key, const Size2 &p_size, InlineAlignment p_inline_align = INLINE_ALIGNMENT_CENTER, double p_baseline = 0.0) = 0;
  387. virtual String shaped_get_text(const RID &p_shaped) const = 0;
  388. virtual int64_t shaped_get_span_count(const RID &p_shaped) const = 0;
  389. virtual Variant shaped_get_span_meta(const RID &p_shaped, int64_t p_index) const = 0;
  390. virtual Variant shaped_get_span_embedded_object(const RID &p_shaped, int64_t p_index) const = 0;
  391. virtual String shaped_get_span_text(const RID &p_shaped, int64_t p_index) const = 0;
  392. virtual Variant shaped_get_span_object(const RID &p_shaped, int64_t p_index) const = 0;
  393. virtual void shaped_set_span_update_font(const RID &p_shaped, int64_t p_index, const TypedArray<RID> &p_fonts, int64_t p_size, const Dictionary &p_opentype_features = Dictionary()) = 0;
  394. virtual int64_t shaped_get_run_count(const RID &p_shaped) const = 0;
  395. virtual String shaped_get_run_text(const RID &p_shaped, int64_t p_index) const = 0;
  396. virtual Vector2i shaped_get_run_range(const RID &p_shaped, int64_t p_index) const = 0;
  397. virtual RID shaped_get_run_font_rid(const RID &p_shaped, int64_t p_index) const = 0;
  398. virtual int shaped_get_run_font_size(const RID &p_shaped, int64_t p_index) const = 0;
  399. virtual String shaped_get_run_language(const RID &p_shaped, int64_t p_index) const = 0;
  400. virtual Direction shaped_get_run_direction(const RID &p_shaped, int64_t p_index) const = 0;
  401. virtual Variant shaped_get_run_object(const RID &p_shaped, int64_t p_index) const = 0;
  402. virtual RID shaped_text_substr(const RID &p_shaped, int64_t p_start, int64_t p_length) const = 0; // Copy shaped substring (e.g. line break) without reshaping, but correctly reordered, preservers range.
  403. virtual RID shaped_text_get_parent(const RID &p_shaped) const = 0;
  404. virtual double shaped_text_fit_to_width(const RID &p_shaped, double p_width, BitField<TextServer::JustificationFlag> p_jst_flags = JUSTIFICATION_WORD_BOUND | JUSTIFICATION_KASHIDA) = 0;
  405. virtual double shaped_text_tab_align(const RID &p_shaped, const PackedFloat32Array &p_tab_stops) = 0;
  406. virtual bool shaped_text_shape(const RID &p_shaped) = 0;
  407. virtual bool shaped_text_update_breaks(const RID &p_shaped) = 0;
  408. virtual bool shaped_text_update_justification_ops(const RID &p_shaped) = 0;
  409. virtual bool shaped_text_is_ready(const RID &p_shaped) const = 0;
  410. bool shaped_text_has_visible_chars(const RID &p_shaped) const;
  411. virtual const Glyph *shaped_text_get_glyphs(const RID &p_shaped) const = 0;
  412. TypedArray<Dictionary> _shaped_text_get_glyphs_wrapper(const RID &p_shaped) const;
  413. virtual const Glyph *shaped_text_sort_logical(const RID &p_shaped) = 0;
  414. TypedArray<Dictionary> _shaped_text_sort_logical_wrapper(const RID &p_shaped);
  415. virtual int64_t shaped_text_get_glyph_count(const RID &p_shaped) const = 0;
  416. virtual Vector2i shaped_text_get_range(const RID &p_shaped) const = 0;
  417. virtual PackedInt32Array shaped_text_get_line_breaks_adv(const RID &p_shaped, const PackedFloat32Array &p_width, int64_t p_start = 0, bool p_once = true, BitField<TextServer::LineBreakFlag> p_break_flags = BREAK_MANDATORY | BREAK_WORD_BOUND) const;
  418. virtual PackedInt32Array shaped_text_get_line_breaks(const RID &p_shaped, double p_width, int64_t p_start = 0, BitField<TextServer::LineBreakFlag> p_break_flags = BREAK_MANDATORY | BREAK_WORD_BOUND) const;
  419. virtual PackedInt32Array shaped_text_get_word_breaks(const RID &p_shaped, BitField<TextServer::GraphemeFlag> p_grapheme_flags = GRAPHEME_IS_SPACE | GRAPHEME_IS_PUNCTUATION, BitField<TextServer::GraphemeFlag> p_skip_grapheme_flags = GRAPHEME_IS_VIRTUAL) const;
  420. virtual int64_t shaped_text_get_trim_pos(const RID &p_shaped) const = 0;
  421. virtual int64_t shaped_text_get_ellipsis_pos(const RID &p_shaped) const = 0;
  422. virtual const Glyph *shaped_text_get_ellipsis_glyphs(const RID &p_shaped) const = 0;
  423. TypedArray<Dictionary> _shaped_text_get_ellipsis_glyphs_wrapper(const RID &p_shaped) const;
  424. virtual int64_t shaped_text_get_ellipsis_glyph_count(const RID &p_shaped) const = 0;
  425. virtual void shaped_text_overrun_trim_to_width(const RID &p_shaped, double p_width, BitField<TextServer::TextOverrunFlag> p_trim_flags) = 0;
  426. virtual Array shaped_text_get_objects(const RID &p_shaped) const = 0;
  427. virtual Rect2 shaped_text_get_object_rect(const RID &p_shaped, const Variant &p_key) const = 0;
  428. virtual Vector2i shaped_text_get_object_range(const RID &p_shaped, const Variant &p_key) const = 0;
  429. virtual int64_t shaped_text_get_object_glyph(const RID &p_shaped, const Variant &p_key) const = 0;
  430. virtual Size2 shaped_text_get_size(const RID &p_shaped) const = 0;
  431. virtual double shaped_text_get_ascent(const RID &p_shaped) const = 0;
  432. virtual double shaped_text_get_descent(const RID &p_shaped) const = 0;
  433. virtual double shaped_text_get_width(const RID &p_shaped) const = 0;
  434. virtual double shaped_text_get_underline_position(const RID &p_shaped) const = 0;
  435. virtual double shaped_text_get_underline_thickness(const RID &p_shaped) const = 0;
  436. virtual Direction shaped_text_get_dominant_direction_in_range(const RID &p_shaped, int64_t p_start, int64_t p_end) const;
  437. virtual CaretInfo shaped_text_get_carets(const RID &p_shaped, int64_t p_position) const;
  438. Dictionary _shaped_text_get_carets_wrapper(const RID &p_shaped, int64_t p_position) const;
  439. virtual Vector<Vector2> shaped_text_get_selection(const RID &p_shaped, int64_t p_start, int64_t p_end) const;
  440. virtual int64_t shaped_text_hit_test_grapheme(const RID &p_shaped, double p_coords) const; // Return grapheme index.
  441. virtual int64_t shaped_text_hit_test_position(const RID &p_shaped, double p_coords) const; // Return caret/selection position.
  442. virtual Vector2 shaped_text_get_grapheme_bounds(const RID &p_shaped, int64_t p_pos) const;
  443. virtual int64_t shaped_text_next_grapheme_pos(const RID &p_shaped, int64_t p_pos) const;
  444. virtual int64_t shaped_text_prev_grapheme_pos(const RID &p_shaped, int64_t p_pos) const;
  445. virtual PackedInt32Array shaped_text_get_character_breaks(const RID &p_shaped) const = 0;
  446. virtual int64_t shaped_text_next_character_pos(const RID &p_shaped, int64_t p_pos) const;
  447. virtual int64_t shaped_text_prev_character_pos(const RID &p_shaped, int64_t p_pos) const;
  448. virtual int64_t shaped_text_closest_character_pos(const RID &p_shaped, int64_t p_pos) const;
  449. // The pen position is always placed on the baseline and moving left to right.
  450. virtual void shaped_text_draw(const RID &p_shaped, const RID &p_canvas, const Vector2 &p_pos, double p_clip_l = -1.0, double p_clip_r = -1.0, const Color &p_color = Color(1, 1, 1), float p_oversampling = 0.0) const;
  451. virtual void shaped_text_draw_outline(const RID &p_shaped, const RID &p_canvas, const Vector2 &p_pos, double p_clip_l = -1.0, double p_clip_r = -1.0, int64_t p_outline_size = 1, const Color &p_color = Color(1, 1, 1), float p_oversampling = 0.0) const;
  452. #ifdef DEBUG_ENABLED
  453. void debug_print_glyph(int p_idx, const Glyph &p_glyph) const;
  454. void shaped_text_debug_print(const RID &p_shaped) const;
  455. #endif
  456. // Number conversion.
  457. virtual String format_number(const String &p_string, const String &p_language = "") const = 0;
  458. virtual String parse_number(const String &p_string, const String &p_language = "") const = 0;
  459. virtual String percent_sign(const String &p_language = "") const = 0;
  460. // String functions.
  461. virtual PackedInt32Array string_get_word_breaks(const String &p_string, const String &p_language = "", int64_t p_chars_per_line = 0) const = 0;
  462. virtual PackedInt32Array string_get_character_breaks(const String &p_string, const String &p_language = "") const;
  463. virtual int64_t is_confusable(const String &p_string, const PackedStringArray &p_dict) const { return -1; }
  464. virtual bool spoof_check(const String &p_string) const { return false; }
  465. virtual String strip_diacritics(const String &p_string) const;
  466. virtual bool is_valid_identifier(const String &p_string) const;
  467. virtual bool is_valid_letter(uint64_t p_unicode) const;
  468. // Other string operations.
  469. virtual String string_to_upper(const String &p_string, const String &p_language = "") const = 0;
  470. virtual String string_to_lower(const String &p_string, const String &p_language = "") const = 0;
  471. virtual String string_to_title(const String &p_string, const String &p_language = "") const = 0;
  472. TypedArray<Vector3i> parse_structured_text(StructuredTextParser p_parser_type, const Array &p_args, const String &p_text) const;
  473. virtual void set_current_drawn_item_oversampling(double p_vp_oversampling) { vp_oversampling = p_vp_oversampling; }
  474. virtual void cleanup() {}
  475. TextServer();
  476. ~TextServer();
  477. };
  478. /*************************************************************************/
  479. struct Glyph {
  480. int start = -1; // Start offset in the source string.
  481. int end = -1; // End offset in the source string.
  482. uint8_t count = 0; // Number of glyphs in the grapheme, set in the first glyph only.
  483. uint8_t repeat = 1; // Draw multiple times in the row.
  484. uint16_t flags = 0; // Grapheme flags (valid, rtl, virtual), set in the first glyph only.
  485. float x_off = 0.f; // Offset from the origin of the glyph on baseline.
  486. float y_off = 0.f;
  487. float advance = 0.f; // Advance to the next glyph along baseline(x for horizontal layout, y for vertical).
  488. RID font_rid; // Font resource.
  489. int font_size = 0; // Font size.
  490. int32_t index = 0; // Glyph index (font specific) or UTF-32 codepoint (for the invalid glyphs).
  491. int span_index = -1;
  492. bool operator==(const Glyph &p_a) const;
  493. bool operator!=(const Glyph &p_a) const;
  494. bool operator<(const Glyph &p_a) const;
  495. bool operator>(const Glyph &p_a) const;
  496. };
  497. struct CaretInfo {
  498. Rect2 l_caret;
  499. Rect2 t_caret;
  500. TextServer::Direction l_dir;
  501. TextServer::Direction t_dir;
  502. };
  503. /*************************************************************************/
  504. class TextServerManager : public Object {
  505. GDCLASS(TextServerManager, Object);
  506. protected:
  507. static void _bind_methods();
  508. private:
  509. static TextServerManager *singleton;
  510. Ref<TextServer> primary_interface;
  511. Vector<Ref<TextServer>> interfaces;
  512. public:
  513. _FORCE_INLINE_ static TextServerManager *get_singleton() {
  514. return singleton;
  515. }
  516. void add_interface(const Ref<TextServer> &p_interface);
  517. void remove_interface(const Ref<TextServer> &p_interface);
  518. int get_interface_count() const;
  519. Ref<TextServer> get_interface(int p_index) const;
  520. Ref<TextServer> find_interface(const String &p_name) const;
  521. TypedArray<Dictionary> get_interfaces() const;
  522. _FORCE_INLINE_ Ref<TextServer> get_primary_interface() const {
  523. return primary_interface;
  524. }
  525. void set_primary_interface(const Ref<TextServer> &p_primary_interface);
  526. TextServerManager();
  527. ~TextServerManager();
  528. };
  529. /*************************************************************************/
  530. #define TS TextServerManager::get_singleton()->get_primary_interface()
  531. VARIANT_ENUM_CAST(TextServer::VisibleCharactersBehavior);
  532. VARIANT_ENUM_CAST(TextServer::AutowrapMode);
  533. VARIANT_ENUM_CAST(TextServer::OverrunBehavior);
  534. VARIANT_ENUM_CAST(TextServer::Direction);
  535. VARIANT_ENUM_CAST(TextServer::Orientation);
  536. VARIANT_BITFIELD_CAST(TextServer::JustificationFlag);
  537. VARIANT_BITFIELD_CAST(TextServer::LineBreakFlag);
  538. VARIANT_BITFIELD_CAST(TextServer::TextOverrunFlag);
  539. VARIANT_BITFIELD_CAST(TextServer::GraphemeFlag);
  540. VARIANT_ENUM_CAST(TextServer::Hinting);
  541. VARIANT_ENUM_CAST(TextServer::SubpixelPositioning);
  542. VARIANT_ENUM_CAST(TextServer::Feature);
  543. VARIANT_ENUM_CAST(TextServer::ContourPointTag);
  544. VARIANT_ENUM_CAST(TextServer::SpacingType);
  545. VARIANT_BITFIELD_CAST(TextServer::FontStyle);
  546. VARIANT_ENUM_CAST(TextServer::StructuredTextParser);
  547. VARIANT_ENUM_CAST(TextServer::FontAntialiasing);
  548. VARIANT_ENUM_CAST(TextServer::FontLCDSubpixelLayout);
  549. VARIANT_ENUM_CAST(TextServer::FixedSizeScaleMode);
  550. GDVIRTUAL_NATIVE_PTR(Glyph);
  551. GDVIRTUAL_NATIVE_PTR(CaretInfo);