text_line.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /*************************************************************************/
  2. /* text_line.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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. #include "text_line.h"
  31. void TextLine::_bind_methods() {
  32. ClassDB::bind_method(D_METHOD("clear"), &TextLine::clear);
  33. ClassDB::bind_method(D_METHOD("set_direction", "direction"), &TextLine::set_direction);
  34. ClassDB::bind_method(D_METHOD("get_direction"), &TextLine::get_direction);
  35. ADD_PROPERTY(PropertyInfo(Variant::INT, "direction", PROPERTY_HINT_ENUM, "Auto,Left-to-right,Right-to-left"), "set_direction", "get_direction");
  36. ClassDB::bind_method(D_METHOD("set_orientation", "orientation"), &TextLine::set_orientation);
  37. ClassDB::bind_method(D_METHOD("get_orientation"), &TextLine::get_orientation);
  38. ADD_PROPERTY(PropertyInfo(Variant::INT, "orientation", PROPERTY_HINT_ENUM, "Horizontal,Orientation"), "set_orientation", "get_orientation");
  39. ClassDB::bind_method(D_METHOD("set_preserve_invalid", "enabled"), &TextLine::set_preserve_invalid);
  40. ClassDB::bind_method(D_METHOD("get_preserve_invalid"), &TextLine::get_preserve_invalid);
  41. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "preserve_invalid"), "set_preserve_invalid", "get_preserve_invalid");
  42. ClassDB::bind_method(D_METHOD("set_preserve_control", "enabled"), &TextLine::set_preserve_control);
  43. ClassDB::bind_method(D_METHOD("get_preserve_control"), &TextLine::get_preserve_control);
  44. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "preserve_control"), "set_preserve_control", "get_preserve_control");
  45. ClassDB::bind_method(D_METHOD("set_bidi_override", "override"), &TextLine::set_bidi_override);
  46. ClassDB::bind_method(D_METHOD("add_string", "text", "font", "font_size", "language", "meta"), &TextLine::add_string, DEFVAL(""), DEFVAL(Variant()));
  47. ClassDB::bind_method(D_METHOD("add_object", "key", "size", "inline_align", "length"), &TextLine::add_object, DEFVAL(INLINE_ALIGNMENT_CENTER), DEFVAL(1));
  48. ClassDB::bind_method(D_METHOD("resize_object", "key", "size", "inline_align"), &TextLine::resize_object, DEFVAL(INLINE_ALIGNMENT_CENTER));
  49. ClassDB::bind_method(D_METHOD("set_width", "width"), &TextLine::set_width);
  50. ClassDB::bind_method(D_METHOD("get_width"), &TextLine::get_width);
  51. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "width"), "set_width", "get_width");
  52. ClassDB::bind_method(D_METHOD("set_horizontal_alignment", "alignment"), &TextLine::set_horizontal_alignment);
  53. ClassDB::bind_method(D_METHOD("get_horizontal_alignment"), &TextLine::get_horizontal_alignment);
  54. ADD_PROPERTY(PropertyInfo(Variant::INT, "alignment", PROPERTY_HINT_ENUM, "Left,Center,Right,Fill"), "set_horizontal_alignment", "get_horizontal_alignment");
  55. ClassDB::bind_method(D_METHOD("tab_align", "tab_stops"), &TextLine::tab_align);
  56. ClassDB::bind_method(D_METHOD("set_flags", "flags"), &TextLine::set_flags);
  57. ClassDB::bind_method(D_METHOD("get_flags"), &TextLine::get_flags);
  58. ADD_PROPERTY(PropertyInfo(Variant::INT, "flags", PROPERTY_HINT_FLAGS, "Kashida Justification,Word Justification,Trim Edge Spaces After Justification,Justify Only After Last Tab,Constrain Ellipsis"), "set_flags", "get_flags");
  59. ClassDB::bind_method(D_METHOD("set_text_overrun_behavior", "overrun_behavior"), &TextLine::set_text_overrun_behavior);
  60. ClassDB::bind_method(D_METHOD("get_text_overrun_behavior"), &TextLine::get_text_overrun_behavior);
  61. ADD_PROPERTY(PropertyInfo(Variant::INT, "text_overrun_behavior", PROPERTY_HINT_ENUM, "Trim Nothing,Trim Characters,Trim Words,Ellipsis,Word Ellipsis"), "set_text_overrun_behavior", "get_text_overrun_behavior");
  62. ClassDB::bind_method(D_METHOD("get_objects"), &TextLine::get_objects);
  63. ClassDB::bind_method(D_METHOD("get_object_rect", "key"), &TextLine::get_object_rect);
  64. ClassDB::bind_method(D_METHOD("get_size"), &TextLine::get_size);
  65. ClassDB::bind_method(D_METHOD("get_rid"), &TextLine::get_rid);
  66. ClassDB::bind_method(D_METHOD("get_line_ascent"), &TextLine::get_line_ascent);
  67. ClassDB::bind_method(D_METHOD("get_line_descent"), &TextLine::get_line_descent);
  68. ClassDB::bind_method(D_METHOD("get_line_width"), &TextLine::get_line_width);
  69. ClassDB::bind_method(D_METHOD("get_line_underline_position"), &TextLine::get_line_underline_position);
  70. ClassDB::bind_method(D_METHOD("get_line_underline_thickness"), &TextLine::get_line_underline_thickness);
  71. ClassDB::bind_method(D_METHOD("draw", "canvas", "pos", "color"), &TextLine::draw, DEFVAL(Color(1, 1, 1)));
  72. ClassDB::bind_method(D_METHOD("draw_outline", "canvas", "pos", "outline_size", "color"), &TextLine::draw_outline, DEFVAL(1), DEFVAL(Color(1, 1, 1)));
  73. ClassDB::bind_method(D_METHOD("hit_test", "coords"), &TextLine::hit_test);
  74. }
  75. void TextLine::_shape() {
  76. if (dirty) {
  77. if (!tab_stops.is_empty()) {
  78. TS->shaped_text_tab_align(rid, tab_stops);
  79. }
  80. BitField<TextServer::TextOverrunFlag> overrun_flags = TextServer::OVERRUN_NO_TRIM;
  81. if (overrun_behavior != TextServer::OVERRUN_NO_TRIMMING) {
  82. switch (overrun_behavior) {
  83. case TextServer::OVERRUN_TRIM_WORD_ELLIPSIS:
  84. overrun_flags.set_flag(TextServer::OVERRUN_TRIM);
  85. overrun_flags.set_flag(TextServer::OVERRUN_TRIM_WORD_ONLY);
  86. overrun_flags.set_flag(TextServer::OVERRUN_ADD_ELLIPSIS);
  87. break;
  88. case TextServer::OVERRUN_TRIM_ELLIPSIS:
  89. overrun_flags.set_flag(TextServer::OVERRUN_TRIM);
  90. overrun_flags.set_flag(TextServer::OVERRUN_ADD_ELLIPSIS);
  91. break;
  92. case TextServer::OVERRUN_TRIM_WORD:
  93. overrun_flags.set_flag(TextServer::OVERRUN_TRIM);
  94. overrun_flags.set_flag(TextServer::OVERRUN_TRIM_WORD_ONLY);
  95. break;
  96. case TextServer::OVERRUN_TRIM_CHAR:
  97. overrun_flags.set_flag(TextServer::OVERRUN_TRIM);
  98. break;
  99. case TextServer::OVERRUN_NO_TRIMMING:
  100. break;
  101. }
  102. if (alignment == HORIZONTAL_ALIGNMENT_FILL) {
  103. TS->shaped_text_fit_to_width(rid, width, flags);
  104. overrun_flags.set_flag(TextServer::OVERRUN_JUSTIFICATION_AWARE);
  105. TS->shaped_text_overrun_trim_to_width(rid, width, overrun_flags);
  106. } else {
  107. TS->shaped_text_overrun_trim_to_width(rid, width, overrun_flags);
  108. }
  109. } else if (alignment == HORIZONTAL_ALIGNMENT_FILL) {
  110. TS->shaped_text_fit_to_width(rid, width, flags);
  111. }
  112. dirty = false;
  113. }
  114. }
  115. RID TextLine::get_rid() const {
  116. return rid;
  117. }
  118. void TextLine::clear() {
  119. TS->shaped_text_clear(rid);
  120. }
  121. void TextLine::set_preserve_invalid(bool p_enabled) {
  122. TS->shaped_text_set_preserve_invalid(rid, p_enabled);
  123. dirty = true;
  124. }
  125. bool TextLine::get_preserve_invalid() const {
  126. return TS->shaped_text_get_preserve_invalid(rid);
  127. }
  128. void TextLine::set_preserve_control(bool p_enabled) {
  129. TS->shaped_text_set_preserve_control(rid, p_enabled);
  130. dirty = true;
  131. }
  132. bool TextLine::get_preserve_control() const {
  133. return TS->shaped_text_get_preserve_control(rid);
  134. }
  135. void TextLine::set_direction(TextServer::Direction p_direction) {
  136. TS->shaped_text_set_direction(rid, p_direction);
  137. dirty = true;
  138. }
  139. TextServer::Direction TextLine::get_direction() const {
  140. return TS->shaped_text_get_direction(rid);
  141. }
  142. void TextLine::set_orientation(TextServer::Orientation p_orientation) {
  143. TS->shaped_text_set_orientation(rid, p_orientation);
  144. dirty = true;
  145. }
  146. TextServer::Orientation TextLine::get_orientation() const {
  147. return TS->shaped_text_get_orientation(rid);
  148. }
  149. void TextLine::set_bidi_override(const Array &p_override) {
  150. TS->shaped_text_set_bidi_override(rid, p_override);
  151. dirty = true;
  152. }
  153. bool TextLine::add_string(const String &p_text, const Ref<Font> &p_font, int p_font_size, const String &p_language, const Variant &p_meta) {
  154. ERR_FAIL_COND_V(p_font.is_null(), false);
  155. bool res = TS->shaped_text_add_string(rid, p_text, p_font->get_rids(), p_font_size, p_font->get_opentype_features(), p_language, p_meta);
  156. for (int i = 0; i < TextServer::SPACING_MAX; i++) {
  157. TS->shaped_text_set_spacing(rid, TextServer::SpacingType(i), p_font->get_spacing(TextServer::SpacingType(i)));
  158. }
  159. dirty = true;
  160. return res;
  161. }
  162. bool TextLine::add_object(Variant p_key, const Size2 &p_size, InlineAlignment p_inline_align, int p_length) {
  163. bool res = TS->shaped_text_add_object(rid, p_key, p_size, p_inline_align, p_length);
  164. dirty = true;
  165. return res;
  166. }
  167. bool TextLine::resize_object(Variant p_key, const Size2 &p_size, InlineAlignment p_inline_align) {
  168. const_cast<TextLine *>(this)->_shape();
  169. return TS->shaped_text_resize_object(rid, p_key, p_size, p_inline_align);
  170. }
  171. Array TextLine::get_objects() const {
  172. return TS->shaped_text_get_objects(rid);
  173. }
  174. Rect2 TextLine::get_object_rect(Variant p_key) const {
  175. return TS->shaped_text_get_object_rect(rid, p_key);
  176. }
  177. void TextLine::set_horizontal_alignment(HorizontalAlignment p_alignment) {
  178. if (alignment != p_alignment) {
  179. if (alignment == HORIZONTAL_ALIGNMENT_FILL || p_alignment == HORIZONTAL_ALIGNMENT_FILL) {
  180. alignment = p_alignment;
  181. dirty = true;
  182. } else {
  183. alignment = p_alignment;
  184. }
  185. }
  186. }
  187. HorizontalAlignment TextLine::get_horizontal_alignment() const {
  188. return alignment;
  189. }
  190. void TextLine::tab_align(const Vector<float> &p_tab_stops) {
  191. tab_stops = p_tab_stops;
  192. dirty = true;
  193. }
  194. void TextLine::set_flags(BitField<TextServer::JustificationFlag> p_flags) {
  195. if (flags != p_flags) {
  196. flags = p_flags;
  197. dirty = true;
  198. }
  199. }
  200. BitField<TextServer::JustificationFlag> TextLine::get_flags() const {
  201. return flags;
  202. }
  203. void TextLine::set_text_overrun_behavior(TextServer::OverrunBehavior p_behavior) {
  204. if (overrun_behavior != p_behavior) {
  205. overrun_behavior = p_behavior;
  206. dirty = true;
  207. }
  208. }
  209. TextServer::OverrunBehavior TextLine::get_text_overrun_behavior() const {
  210. return overrun_behavior;
  211. }
  212. void TextLine::set_width(float p_width) {
  213. width = p_width;
  214. if (alignment == HORIZONTAL_ALIGNMENT_FILL || overrun_behavior != TextServer::OVERRUN_NO_TRIMMING) {
  215. dirty = true;
  216. }
  217. }
  218. float TextLine::get_width() const {
  219. return width;
  220. }
  221. Size2 TextLine::get_size() const {
  222. const_cast<TextLine *>(this)->_shape();
  223. if (TS->shaped_text_get_orientation(rid) == TextServer::ORIENTATION_HORIZONTAL) {
  224. return Size2(TS->shaped_text_get_size(rid).x, TS->shaped_text_get_size(rid).y);
  225. } else {
  226. return Size2(TS->shaped_text_get_size(rid).x, TS->shaped_text_get_size(rid).y);
  227. }
  228. }
  229. float TextLine::get_line_ascent() const {
  230. const_cast<TextLine *>(this)->_shape();
  231. return TS->shaped_text_get_ascent(rid);
  232. }
  233. float TextLine::get_line_descent() const {
  234. const_cast<TextLine *>(this)->_shape();
  235. return TS->shaped_text_get_descent(rid);
  236. }
  237. float TextLine::get_line_width() const {
  238. const_cast<TextLine *>(this)->_shape();
  239. return TS->shaped_text_get_width(rid);
  240. }
  241. float TextLine::get_line_underline_position() const {
  242. const_cast<TextLine *>(this)->_shape();
  243. return TS->shaped_text_get_underline_position(rid);
  244. }
  245. float TextLine::get_line_underline_thickness() const {
  246. const_cast<TextLine *>(this)->_shape();
  247. return TS->shaped_text_get_underline_thickness(rid);
  248. }
  249. void TextLine::draw(RID p_canvas, const Vector2 &p_pos, const Color &p_color) const {
  250. const_cast<TextLine *>(this)->_shape();
  251. Vector2 ofs = p_pos;
  252. float length = TS->shaped_text_get_width(rid);
  253. if (width > 0) {
  254. switch (alignment) {
  255. case HORIZONTAL_ALIGNMENT_FILL:
  256. case HORIZONTAL_ALIGNMENT_LEFT:
  257. break;
  258. case HORIZONTAL_ALIGNMENT_CENTER: {
  259. if (length <= width) {
  260. if (TS->shaped_text_get_orientation(rid) == TextServer::ORIENTATION_HORIZONTAL) {
  261. ofs.x += Math::floor((width - length) / 2.0);
  262. } else {
  263. ofs.y += Math::floor((width - length) / 2.0);
  264. }
  265. } else if (TS->shaped_text_get_inferred_direction(rid) == TextServer::DIRECTION_RTL) {
  266. if (TS->shaped_text_get_orientation(rid) == TextServer::ORIENTATION_HORIZONTAL) {
  267. ofs.x += width - length;
  268. } else {
  269. ofs.y += width - length;
  270. }
  271. }
  272. } break;
  273. case HORIZONTAL_ALIGNMENT_RIGHT: {
  274. if (TS->shaped_text_get_orientation(rid) == TextServer::ORIENTATION_HORIZONTAL) {
  275. ofs.x += width - length;
  276. } else {
  277. ofs.y += width - length;
  278. }
  279. } break;
  280. }
  281. }
  282. float clip_l;
  283. if (TS->shaped_text_get_orientation(rid) == TextServer::ORIENTATION_HORIZONTAL) {
  284. ofs.y += TS->shaped_text_get_ascent(rid);
  285. clip_l = MAX(0, p_pos.x - ofs.x);
  286. } else {
  287. ofs.x += TS->shaped_text_get_ascent(rid);
  288. clip_l = MAX(0, p_pos.y - ofs.y);
  289. }
  290. return TS->shaped_text_draw(rid, p_canvas, ofs, clip_l, clip_l + width, p_color);
  291. }
  292. void TextLine::draw_outline(RID p_canvas, const Vector2 &p_pos, int p_outline_size, const Color &p_color) const {
  293. const_cast<TextLine *>(this)->_shape();
  294. Vector2 ofs = p_pos;
  295. float length = TS->shaped_text_get_width(rid);
  296. if (width > 0) {
  297. switch (alignment) {
  298. case HORIZONTAL_ALIGNMENT_FILL:
  299. case HORIZONTAL_ALIGNMENT_LEFT:
  300. break;
  301. case HORIZONTAL_ALIGNMENT_CENTER: {
  302. if (length <= width) {
  303. if (TS->shaped_text_get_orientation(rid) == TextServer::ORIENTATION_HORIZONTAL) {
  304. ofs.x += Math::floor((width - length) / 2.0);
  305. } else {
  306. ofs.y += Math::floor((width - length) / 2.0);
  307. }
  308. } else if (TS->shaped_text_get_inferred_direction(rid) == TextServer::DIRECTION_RTL) {
  309. if (TS->shaped_text_get_orientation(rid) == TextServer::ORIENTATION_HORIZONTAL) {
  310. ofs.x += width - length;
  311. } else {
  312. ofs.y += width - length;
  313. }
  314. }
  315. } break;
  316. case HORIZONTAL_ALIGNMENT_RIGHT: {
  317. if (TS->shaped_text_get_orientation(rid) == TextServer::ORIENTATION_HORIZONTAL) {
  318. ofs.x += width - length;
  319. } else {
  320. ofs.y += width - length;
  321. }
  322. } break;
  323. }
  324. }
  325. float clip_l;
  326. if (TS->shaped_text_get_orientation(rid) == TextServer::ORIENTATION_HORIZONTAL) {
  327. ofs.y += TS->shaped_text_get_ascent(rid);
  328. clip_l = MAX(0, p_pos.x - ofs.x);
  329. } else {
  330. ofs.x += TS->shaped_text_get_ascent(rid);
  331. clip_l = MAX(0, p_pos.y - ofs.y);
  332. }
  333. return TS->shaped_text_draw_outline(rid, p_canvas, ofs, clip_l, clip_l + width, p_outline_size, p_color);
  334. }
  335. int TextLine::hit_test(float p_coords) const {
  336. const_cast<TextLine *>(this)->_shape();
  337. return TS->shaped_text_hit_test_position(rid, p_coords);
  338. }
  339. TextLine::TextLine(const String &p_text, const Ref<Font> &p_font, int p_font_size, const String &p_language, TextServer::Direction p_direction, TextServer::Orientation p_orientation) {
  340. rid = TS->create_shaped_text(p_direction, p_orientation);
  341. if (p_font.is_valid()) {
  342. TS->shaped_text_add_string(rid, p_text, p_font->get_rids(), p_font_size, p_font->get_opentype_features(), p_language);
  343. for (int i = 0; i < TextServer::SPACING_MAX; i++) {
  344. TS->shaped_text_set_spacing(rid, TextServer::SpacingType(i), p_font->get_spacing(TextServer::SpacingType(i)));
  345. }
  346. }
  347. }
  348. TextLine::TextLine() {
  349. rid = TS->create_shaped_text();
  350. }
  351. TextLine::~TextLine() {
  352. TS->free_rid(rid);
  353. }