text_server_adv.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*************************************************************************/
  2. /* text_server_adv.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_ADV_H
  31. #define TEXT_SERVER_ADV_H
  32. /*************************************************************************/
  33. /* ICU/HarfBuzz/Graphite backed Text Server implementation with BiDi, */
  34. /* shaping and advanced font features support. */
  35. /*************************************************************************/
  36. #include "servers/text_server.h"
  37. #include "core/templates/rid_owner.h"
  38. #include "scene/resources/texture.h"
  39. #include "script_iterator.h"
  40. #include <unicode/ubidi.h>
  41. #include <unicode/ubrk.h>
  42. #include <unicode/uchar.h>
  43. #include <unicode/uclean.h>
  44. #include <unicode/udata.h>
  45. #include <unicode/uiter.h>
  46. #include <unicode/uloc.h>
  47. #include <unicode/uscript.h>
  48. #include <unicode/ustring.h>
  49. #include <unicode/utypes.h>
  50. #include <hb-icu.h>
  51. #include <hb.h>
  52. #include "font_adv.h"
  53. class TextServerAdvanced : public TextServer {
  54. GDCLASS(TextServerAdvanced, TextServer);
  55. _THREAD_SAFE_CLASS_
  56. static String interface_name;
  57. static uint32_t interface_features;
  58. uint8_t *icu_data = nullptr;
  59. struct ShapedTextDataAdvanced : public ShapedTextData {
  60. /* Intermediate data */
  61. Char16String utf16;
  62. Vector<UBiDi *> bidi_iter;
  63. Vector<Vector2i> bidi_override;
  64. ScriptIterator *script_iter = nullptr;
  65. hb_buffer_t *hb_buffer = nullptr;
  66. ~ShapedTextDataAdvanced() {
  67. for (int i = 0; i < bidi_iter.size(); i++) {
  68. ubidi_close(bidi_iter[i]);
  69. }
  70. if (script_iter) {
  71. memdelete(script_iter);
  72. }
  73. if (hb_buffer) {
  74. hb_buffer_destroy(hb_buffer);
  75. }
  76. }
  77. };
  78. float oversampling = 1.f;
  79. mutable RID_PtrOwner<FontDataAdvanced> font_owner;
  80. mutable RID_PtrOwner<ShapedTextDataAdvanced> shaped_owner;
  81. int _convert_pos(const ShapedTextDataAdvanced *p_sd, int p_pos) const;
  82. int _convert_pos_inv(const ShapedTextDataAdvanced *p_sd, int p_pos) const;
  83. void _shape_run(ShapedTextDataAdvanced *p_sd, int32_t p_start, int32_t p_end, hb_script_t p_script, hb_direction_t p_direction, Vector<RID> p_fonts, int p_span, int p_fb_index);
  84. TextServer::Glyph _shape_single_glyph(ShapedTextDataAdvanced *p_sd, char32_t p_char, hb_script_t p_script, hb_direction_t p_direction, RID p_font, int p_font_size);
  85. protected:
  86. static void _bind_methods(){};
  87. void full_copy(ShapedTextDataAdvanced *p_shaped);
  88. void invalidate(ShapedTextDataAdvanced *p_shaped);
  89. public:
  90. virtual bool has_feature(Feature p_feature) override;
  91. virtual String get_name() const override;
  92. virtual void free(RID p_rid) override;
  93. virtual bool has(RID p_rid) override;
  94. virtual bool load_support_data(const String &p_filename) override;
  95. #ifdef TOOLS_ENABLED
  96. virtual String get_support_data_filename() override { return _MKSTR(ICU_DATA_NAME); };
  97. virtual String get_support_data_info() override { return String("ICU break iteration data (") + _MKSTR(ICU_DATA_NAME) + String(")."); };
  98. virtual bool save_support_data(const String &p_filename) override;
  99. #endif
  100. virtual bool is_locale_right_to_left(const String &p_locale) override;
  101. virtual int32_t name_to_tag(const String &p_name) override;
  102. virtual String tag_to_name(int32_t p_tag) override;
  103. /* Font interface */
  104. virtual RID create_font_system(const String &p_name, int p_base_size = 16) override;
  105. virtual RID create_font_resource(const String &p_filename, int p_base_size = 16) override;
  106. virtual RID create_font_memory(const uint8_t *p_data, size_t p_size, const String &p_type, int p_base_size = 16) override;
  107. virtual float font_get_height(RID p_font, int p_size) const override;
  108. virtual float font_get_ascent(RID p_font, int p_size) const override;
  109. virtual float font_get_descent(RID p_font, int p_size) const override;
  110. virtual float font_get_underline_position(RID p_font, int p_size) const override;
  111. virtual float font_get_underline_thickness(RID p_font, int p_size) const override;
  112. virtual int font_get_spacing_space(RID p_font) const override;
  113. virtual void font_set_spacing_space(RID p_font, int p_value) override;
  114. virtual int font_get_spacing_glyph(RID p_font) const override;
  115. virtual void font_set_spacing_glyph(RID p_font, int p_value) override;
  116. virtual void font_set_antialiased(RID p_font, bool p_antialiased) override;
  117. virtual bool font_get_antialiased(RID p_font) const override;
  118. virtual Dictionary font_get_feature_list(RID p_font) const override;
  119. virtual Dictionary font_get_variation_list(RID p_font) const override;
  120. virtual void font_set_variation(RID p_font, const String &p_name, double p_value) override;
  121. virtual double font_get_variation(RID p_font, const String &p_name) const override;
  122. virtual void font_set_hinting(RID p_font, Hinting p_hinting) override;
  123. virtual Hinting font_get_hinting(RID p_font) const override;
  124. virtual void font_set_distance_field_hint(RID p_font, bool p_distance_field) override;
  125. virtual bool font_get_distance_field_hint(RID p_font) const override;
  126. virtual void font_set_force_autohinter(RID p_font, bool p_enabeld) override;
  127. virtual bool font_get_force_autohinter(RID p_font) const override;
  128. virtual bool font_has_char(RID p_font, char32_t p_char) const override;
  129. virtual String font_get_supported_chars(RID p_font) const override;
  130. virtual bool font_has_outline(RID p_font) const override;
  131. virtual float font_get_base_size(RID p_font) const override;
  132. virtual bool font_is_language_supported(RID p_font, const String &p_language) const override;
  133. virtual void font_set_language_support_override(RID p_font, const String &p_language, bool p_supported) override;
  134. virtual bool font_get_language_support_override(RID p_font, const String &p_language) override;
  135. virtual void font_remove_language_support_override(RID p_font, const String &p_language) override;
  136. Vector<String> font_get_language_support_overrides(RID p_font) override;
  137. virtual bool font_is_script_supported(RID p_font, const String &p_script) const override;
  138. virtual void font_set_script_support_override(RID p_font, const String &p_script, bool p_supported) override;
  139. virtual bool font_get_script_support_override(RID p_font, const String &p_script) override;
  140. virtual void font_remove_script_support_override(RID p_font, const String &p_script) override;
  141. Vector<String> font_get_script_support_overrides(RID p_font) override;
  142. virtual uint32_t font_get_glyph_index(RID p_font, char32_t p_char, char32_t p_variation_selector = 0x0000) const override;
  143. virtual Vector2 font_get_glyph_advance(RID p_font, uint32_t p_index, int p_size) const override;
  144. virtual Vector2 font_get_glyph_kerning(RID p_font, uint32_t p_index_a, uint32_t p_index_b, int p_size) const override;
  145. 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 override;
  146. 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 override;
  147. virtual float font_get_oversampling() const override;
  148. virtual void font_set_oversampling(float p_oversampling) override;
  149. virtual Vector<String> get_system_fonts() const override;
  150. /* Shaped text buffer interface */
  151. virtual RID create_shaped_text(Direction p_direction = DIRECTION_AUTO, Orientation p_orientation = ORIENTATION_HORIZONTAL) override;
  152. virtual void shaped_text_clear(RID p_shaped) override;
  153. virtual void shaped_text_set_direction(RID p_shaped, Direction p_direction = DIRECTION_AUTO) override;
  154. virtual Direction shaped_text_get_direction(RID p_shaped) const override;
  155. virtual void shaped_text_set_bidi_override(RID p_shaped, const Vector<Vector2i> &p_override) override;
  156. virtual void shaped_text_set_orientation(RID p_shaped, Orientation p_orientation = ORIENTATION_HORIZONTAL) override;
  157. virtual Orientation shaped_text_get_orientation(RID p_shaped) const override;
  158. virtual void shaped_text_set_preserve_invalid(RID p_shaped, bool p_enabled) override;
  159. virtual bool shaped_text_get_preserve_invalid(RID p_shaped) const override;
  160. virtual void shaped_text_set_preserve_control(RID p_shaped, bool p_enabled) override;
  161. virtual bool shaped_text_get_preserve_control(RID p_shaped) const override;
  162. 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 = "") override;
  163. 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) override;
  164. virtual bool shaped_text_resize_object(RID p_shaped, Variant p_key, const Size2 &p_size, VAlign p_inline_align = VALIGN_CENTER) override;
  165. virtual RID shaped_text_substr(RID p_shaped, int p_start, int p_length) const override;
  166. virtual RID shaped_text_get_parent(RID p_shaped) const override;
  167. virtual float shaped_text_fit_to_width(RID p_shaped, float p_width, uint8_t /*JustificationFlag*/ p_jst_flags = JUSTIFICATION_WORD_BOUND | JUSTIFICATION_KASHIDA) override;
  168. virtual float shaped_text_tab_align(RID p_shaped, const Vector<float> &p_tab_stops) override;
  169. virtual bool shaped_text_shape(RID p_shaped) override;
  170. virtual bool shaped_text_update_breaks(RID p_shaped) override;
  171. virtual bool shaped_text_update_justification_ops(RID p_shaped) override;
  172. virtual bool shaped_text_is_ready(RID p_shaped) const override;
  173. virtual Vector<Glyph> shaped_text_get_glyphs(RID p_shaped) const override;
  174. virtual Vector2i shaped_text_get_range(RID p_shaped) const override;
  175. virtual Vector<Glyph> shaped_text_sort_logical(RID p_shaped) override;
  176. virtual Array shaped_text_get_objects(RID p_shaped) const override;
  177. virtual Rect2 shaped_text_get_object_rect(RID p_shaped, Variant p_key) const override;
  178. virtual Size2 shaped_text_get_size(RID p_shaped) const override;
  179. virtual float shaped_text_get_ascent(RID p_shaped) const override;
  180. virtual float shaped_text_get_descent(RID p_shaped) const override;
  181. virtual float shaped_text_get_width(RID p_shaped) const override;
  182. virtual float shaped_text_get_underline_position(RID p_shaped) const override;
  183. virtual float shaped_text_get_underline_thickness(RID p_shaped) const override;
  184. virtual String format_number(const String &p_string, const String &p_language = "") const override;
  185. virtual String parse_number(const String &p_string, const String &p_language = "") const override;
  186. virtual String percent_sign(const String &p_language = "") const override;
  187. static TextServer *create_func(Error &r_error, void *p_user_data);
  188. static void register_server();
  189. TextServerAdvanced();
  190. ~TextServerAdvanced();
  191. };
  192. #endif // TEXT_SERVER_ADV_H