text_line.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /**************************************************************************/
  2. /* text_line.cpp */
  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. #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", "baseline"), &TextLine::add_object, DEFVAL(INLINE_ALIGNMENT_CENTER), DEFVAL(1), DEFVAL(0.0));
  48. ClassDB::bind_method(D_METHOD("resize_object", "key", "size", "inline_align", "baseline"), &TextLine::resize_object, DEFVAL(INLINE_ALIGNMENT_CENTER), DEFVAL(0.0));
  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. // When a shaped text is invalidated by an external source, we want to reshape it.
  77. if (!TS->shaped_text_is_ready(rid)) {
  78. dirty = true;
  79. }
  80. if (dirty) {
  81. if (!tab_stops.is_empty()) {
  82. TS->shaped_text_tab_align(rid, tab_stops);
  83. }
  84. BitField<TextServer::TextOverrunFlag> overrun_flags = TextServer::OVERRUN_NO_TRIM;
  85. if (overrun_behavior != TextServer::OVERRUN_NO_TRIMMING) {
  86. switch (overrun_behavior) {
  87. case TextServer::OVERRUN_TRIM_WORD_ELLIPSIS:
  88. overrun_flags.set_flag(TextServer::OVERRUN_TRIM);
  89. overrun_flags.set_flag(TextServer::OVERRUN_TRIM_WORD_ONLY);
  90. overrun_flags.set_flag(TextServer::OVERRUN_ADD_ELLIPSIS);
  91. break;
  92. case TextServer::OVERRUN_TRIM_ELLIPSIS:
  93. overrun_flags.set_flag(TextServer::OVERRUN_TRIM);
  94. overrun_flags.set_flag(TextServer::OVERRUN_ADD_ELLIPSIS);
  95. break;
  96. case TextServer::OVERRUN_TRIM_WORD:
  97. overrun_flags.set_flag(TextServer::OVERRUN_TRIM);
  98. overrun_flags.set_flag(TextServer::OVERRUN_TRIM_WORD_ONLY);
  99. break;
  100. case TextServer::OVERRUN_TRIM_CHAR:
  101. overrun_flags.set_flag(TextServer::OVERRUN_TRIM);
  102. break;
  103. case TextServer::OVERRUN_NO_TRIMMING:
  104. break;
  105. }
  106. if (alignment == HORIZONTAL_ALIGNMENT_FILL) {
  107. TS->shaped_text_fit_to_width(rid, width, flags);
  108. overrun_flags.set_flag(TextServer::OVERRUN_JUSTIFICATION_AWARE);
  109. TS->shaped_text_overrun_trim_to_width(rid, width, overrun_flags);
  110. } else {
  111. TS->shaped_text_overrun_trim_to_width(rid, width, overrun_flags);
  112. }
  113. } else if (alignment == HORIZONTAL_ALIGNMENT_FILL) {
  114. TS->shaped_text_fit_to_width(rid, width, flags);
  115. }
  116. dirty = false;
  117. }
  118. }
  119. RID TextLine::get_rid() const {
  120. return rid;
  121. }
  122. void TextLine::clear() {
  123. TS->shaped_text_clear(rid);
  124. }
  125. void TextLine::set_preserve_invalid(bool p_enabled) {
  126. TS->shaped_text_set_preserve_invalid(rid, p_enabled);
  127. dirty = true;
  128. }
  129. bool TextLine::get_preserve_invalid() const {
  130. return TS->shaped_text_get_preserve_invalid(rid);
  131. }
  132. void TextLine::set_preserve_control(bool p_enabled) {
  133. TS->shaped_text_set_preserve_control(rid, p_enabled);
  134. dirty = true;
  135. }
  136. bool TextLine::get_preserve_control() const {
  137. return TS->shaped_text_get_preserve_control(rid);
  138. }
  139. void TextLine::set_direction(TextServer::Direction p_direction) {
  140. TS->shaped_text_set_direction(rid, p_direction);
  141. dirty = true;
  142. }
  143. TextServer::Direction TextLine::get_direction() const {
  144. return TS->shaped_text_get_direction(rid);
  145. }
  146. void TextLine::set_orientation(TextServer::Orientation p_orientation) {
  147. TS->shaped_text_set_orientation(rid, p_orientation);
  148. dirty = true;
  149. }
  150. TextServer::Orientation TextLine::get_orientation() const {
  151. return TS->shaped_text_get_orientation(rid);
  152. }
  153. void TextLine::set_bidi_override(const Array &p_override) {
  154. TS->shaped_text_set_bidi_override(rid, p_override);
  155. dirty = true;
  156. }
  157. 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) {
  158. ERR_FAIL_COND_V(p_font.is_null(), false);
  159. 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);
  160. dirty = true;
  161. return res;
  162. }
  163. bool TextLine::add_object(Variant p_key, const Size2 &p_size, InlineAlignment p_inline_align, int p_length, float p_baseline) {
  164. bool res = TS->shaped_text_add_object(rid, p_key, p_size, p_inline_align, p_length, p_baseline);
  165. dirty = true;
  166. return res;
  167. }
  168. bool TextLine::resize_object(Variant p_key, const Size2 &p_size, InlineAlignment p_inline_align, float p_baseline) {
  169. const_cast<TextLine *>(this)->_shape();
  170. return TS->shaped_text_resize_object(rid, p_key, p_size, p_inline_align, p_baseline);
  171. }
  172. Array TextLine::get_objects() const {
  173. return TS->shaped_text_get_objects(rid);
  174. }
  175. Rect2 TextLine::get_object_rect(Variant p_key) const {
  176. Vector2 ofs;
  177. float length = TS->shaped_text_get_width(rid);
  178. if (width > 0) {
  179. switch (alignment) {
  180. case HORIZONTAL_ALIGNMENT_FILL:
  181. case HORIZONTAL_ALIGNMENT_LEFT:
  182. break;
  183. case HORIZONTAL_ALIGNMENT_CENTER: {
  184. if (length <= width) {
  185. if (TS->shaped_text_get_orientation(rid) == TextServer::ORIENTATION_HORIZONTAL) {
  186. ofs.x += Math::floor((width - length) / 2.0);
  187. } else {
  188. ofs.y += Math::floor((width - length) / 2.0);
  189. }
  190. } else if (TS->shaped_text_get_inferred_direction(rid) == TextServer::DIRECTION_RTL) {
  191. if (TS->shaped_text_get_orientation(rid) == TextServer::ORIENTATION_HORIZONTAL) {
  192. ofs.x += width - length;
  193. } else {
  194. ofs.y += width - length;
  195. }
  196. }
  197. } break;
  198. case HORIZONTAL_ALIGNMENT_RIGHT: {
  199. if (TS->shaped_text_get_orientation(rid) == TextServer::ORIENTATION_HORIZONTAL) {
  200. ofs.x += width - length;
  201. } else {
  202. ofs.y += width - length;
  203. }
  204. } break;
  205. }
  206. }
  207. if (TS->shaped_text_get_orientation(rid) == TextServer::ORIENTATION_HORIZONTAL) {
  208. ofs.y += TS->shaped_text_get_ascent(rid);
  209. } else {
  210. ofs.x += TS->shaped_text_get_ascent(rid);
  211. }
  212. Rect2 rect = TS->shaped_text_get_object_rect(rid, p_key);
  213. rect.position += ofs;
  214. return rect;
  215. }
  216. void TextLine::set_horizontal_alignment(HorizontalAlignment p_alignment) {
  217. if (alignment != p_alignment) {
  218. if (alignment == HORIZONTAL_ALIGNMENT_FILL || p_alignment == HORIZONTAL_ALIGNMENT_FILL) {
  219. alignment = p_alignment;
  220. dirty = true;
  221. } else {
  222. alignment = p_alignment;
  223. }
  224. }
  225. }
  226. HorizontalAlignment TextLine::get_horizontal_alignment() const {
  227. return alignment;
  228. }
  229. void TextLine::tab_align(const Vector<float> &p_tab_stops) {
  230. tab_stops = p_tab_stops;
  231. dirty = true;
  232. }
  233. void TextLine::set_flags(BitField<TextServer::JustificationFlag> p_flags) {
  234. if (flags != p_flags) {
  235. flags = p_flags;
  236. dirty = true;
  237. }
  238. }
  239. BitField<TextServer::JustificationFlag> TextLine::get_flags() const {
  240. return flags;
  241. }
  242. void TextLine::set_text_overrun_behavior(TextServer::OverrunBehavior p_behavior) {
  243. if (overrun_behavior != p_behavior) {
  244. overrun_behavior = p_behavior;
  245. dirty = true;
  246. }
  247. }
  248. TextServer::OverrunBehavior TextLine::get_text_overrun_behavior() const {
  249. return overrun_behavior;
  250. }
  251. void TextLine::set_width(float p_width) {
  252. width = p_width;
  253. if (alignment == HORIZONTAL_ALIGNMENT_FILL || overrun_behavior != TextServer::OVERRUN_NO_TRIMMING) {
  254. dirty = true;
  255. }
  256. }
  257. float TextLine::get_width() const {
  258. return width;
  259. }
  260. Size2 TextLine::get_size() const {
  261. const_cast<TextLine *>(this)->_shape();
  262. return TS->shaped_text_get_size(rid);
  263. }
  264. float TextLine::get_line_ascent() const {
  265. const_cast<TextLine *>(this)->_shape();
  266. return TS->shaped_text_get_ascent(rid);
  267. }
  268. float TextLine::get_line_descent() const {
  269. const_cast<TextLine *>(this)->_shape();
  270. return TS->shaped_text_get_descent(rid);
  271. }
  272. float TextLine::get_line_width() const {
  273. const_cast<TextLine *>(this)->_shape();
  274. return TS->shaped_text_get_width(rid);
  275. }
  276. float TextLine::get_line_underline_position() const {
  277. const_cast<TextLine *>(this)->_shape();
  278. return TS->shaped_text_get_underline_position(rid);
  279. }
  280. float TextLine::get_line_underline_thickness() const {
  281. const_cast<TextLine *>(this)->_shape();
  282. return TS->shaped_text_get_underline_thickness(rid);
  283. }
  284. void TextLine::draw(RID p_canvas, const Vector2 &p_pos, const Color &p_color) const {
  285. const_cast<TextLine *>(this)->_shape();
  286. Vector2 ofs = p_pos;
  287. float length = TS->shaped_text_get_width(rid);
  288. if (width > 0) {
  289. switch (alignment) {
  290. case HORIZONTAL_ALIGNMENT_FILL:
  291. case HORIZONTAL_ALIGNMENT_LEFT:
  292. break;
  293. case HORIZONTAL_ALIGNMENT_CENTER: {
  294. if (length <= width) {
  295. if (TS->shaped_text_get_orientation(rid) == TextServer::ORIENTATION_HORIZONTAL) {
  296. ofs.x += Math::floor((width - length) / 2.0);
  297. } else {
  298. ofs.y += Math::floor((width - length) / 2.0);
  299. }
  300. } else if (TS->shaped_text_get_inferred_direction(rid) == TextServer::DIRECTION_RTL) {
  301. if (TS->shaped_text_get_orientation(rid) == TextServer::ORIENTATION_HORIZONTAL) {
  302. ofs.x += width - length;
  303. } else {
  304. ofs.y += width - length;
  305. }
  306. }
  307. } break;
  308. case HORIZONTAL_ALIGNMENT_RIGHT: {
  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. } break;
  315. }
  316. }
  317. float clip_l;
  318. if (TS->shaped_text_get_orientation(rid) == TextServer::ORIENTATION_HORIZONTAL) {
  319. ofs.y += TS->shaped_text_get_ascent(rid);
  320. clip_l = MAX(0, p_pos.x - ofs.x);
  321. } else {
  322. ofs.x += TS->shaped_text_get_ascent(rid);
  323. clip_l = MAX(0, p_pos.y - ofs.y);
  324. }
  325. return TS->shaped_text_draw(rid, p_canvas, ofs, clip_l, clip_l + width, p_color);
  326. }
  327. void TextLine::draw_outline(RID p_canvas, const Vector2 &p_pos, int p_outline_size, const Color &p_color) const {
  328. const_cast<TextLine *>(this)->_shape();
  329. Vector2 ofs = p_pos;
  330. float length = TS->shaped_text_get_width(rid);
  331. if (width > 0) {
  332. switch (alignment) {
  333. case HORIZONTAL_ALIGNMENT_FILL:
  334. case HORIZONTAL_ALIGNMENT_LEFT:
  335. break;
  336. case HORIZONTAL_ALIGNMENT_CENTER: {
  337. if (length <= width) {
  338. if (TS->shaped_text_get_orientation(rid) == TextServer::ORIENTATION_HORIZONTAL) {
  339. ofs.x += Math::floor((width - length) / 2.0);
  340. } else {
  341. ofs.y += Math::floor((width - length) / 2.0);
  342. }
  343. } else if (TS->shaped_text_get_inferred_direction(rid) == TextServer::DIRECTION_RTL) {
  344. if (TS->shaped_text_get_orientation(rid) == TextServer::ORIENTATION_HORIZONTAL) {
  345. ofs.x += width - length;
  346. } else {
  347. ofs.y += width - length;
  348. }
  349. }
  350. } break;
  351. case HORIZONTAL_ALIGNMENT_RIGHT: {
  352. if (TS->shaped_text_get_orientation(rid) == TextServer::ORIENTATION_HORIZONTAL) {
  353. ofs.x += width - length;
  354. } else {
  355. ofs.y += width - length;
  356. }
  357. } break;
  358. }
  359. }
  360. float clip_l;
  361. if (TS->shaped_text_get_orientation(rid) == TextServer::ORIENTATION_HORIZONTAL) {
  362. ofs.y += TS->shaped_text_get_ascent(rid);
  363. clip_l = MAX(0, p_pos.x - ofs.x);
  364. } else {
  365. ofs.x += TS->shaped_text_get_ascent(rid);
  366. clip_l = MAX(0, p_pos.y - ofs.y);
  367. }
  368. return TS->shaped_text_draw_outline(rid, p_canvas, ofs, clip_l, clip_l + width, p_outline_size, p_color);
  369. }
  370. int TextLine::hit_test(float p_coords) const {
  371. const_cast<TextLine *>(this)->_shape();
  372. return TS->shaped_text_hit_test_position(rid, p_coords);
  373. }
  374. 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) {
  375. rid = TS->create_shaped_text(p_direction, p_orientation);
  376. if (p_font.is_valid()) {
  377. TS->shaped_text_add_string(rid, p_text, p_font->get_rids(), p_font_size, p_font->get_opentype_features(), p_language);
  378. }
  379. }
  380. TextLine::TextLine() {
  381. rid = TS->create_shaped_text();
  382. }
  383. TextLine::~TextLine() {
  384. TS->free_rid(rid);
  385. }