text_line.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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. for (int i = 0; i < TextServer::SPACING_MAX; i++) {
  161. TS->shaped_text_set_spacing(rid, TextServer::SpacingType(i), p_font->get_spacing(TextServer::SpacingType(i)));
  162. }
  163. dirty = true;
  164. return res;
  165. }
  166. bool TextLine::add_object(Variant p_key, const Size2 &p_size, InlineAlignment p_inline_align, int p_length, float p_baseline) {
  167. bool res = TS->shaped_text_add_object(rid, p_key, p_size, p_inline_align, p_length, p_baseline);
  168. dirty = true;
  169. return res;
  170. }
  171. bool TextLine::resize_object(Variant p_key, const Size2 &p_size, InlineAlignment p_inline_align, float p_baseline) {
  172. const_cast<TextLine *>(this)->_shape();
  173. return TS->shaped_text_resize_object(rid, p_key, p_size, p_inline_align, p_baseline);
  174. }
  175. Array TextLine::get_objects() const {
  176. return TS->shaped_text_get_objects(rid);
  177. }
  178. Rect2 TextLine::get_object_rect(Variant p_key) const {
  179. Vector2 ofs;
  180. float length = TS->shaped_text_get_width(rid);
  181. if (width > 0) {
  182. switch (alignment) {
  183. case HORIZONTAL_ALIGNMENT_FILL:
  184. case HORIZONTAL_ALIGNMENT_LEFT:
  185. break;
  186. case HORIZONTAL_ALIGNMENT_CENTER: {
  187. if (length <= width) {
  188. if (TS->shaped_text_get_orientation(rid) == TextServer::ORIENTATION_HORIZONTAL) {
  189. ofs.x += Math::floor((width - length) / 2.0);
  190. } else {
  191. ofs.y += Math::floor((width - length) / 2.0);
  192. }
  193. } else if (TS->shaped_text_get_inferred_direction(rid) == TextServer::DIRECTION_RTL) {
  194. if (TS->shaped_text_get_orientation(rid) == TextServer::ORIENTATION_HORIZONTAL) {
  195. ofs.x += width - length;
  196. } else {
  197. ofs.y += width - length;
  198. }
  199. }
  200. } break;
  201. case HORIZONTAL_ALIGNMENT_RIGHT: {
  202. if (TS->shaped_text_get_orientation(rid) == TextServer::ORIENTATION_HORIZONTAL) {
  203. ofs.x += width - length;
  204. } else {
  205. ofs.y += width - length;
  206. }
  207. } break;
  208. }
  209. }
  210. if (TS->shaped_text_get_orientation(rid) == TextServer::ORIENTATION_HORIZONTAL) {
  211. ofs.y += TS->shaped_text_get_ascent(rid);
  212. } else {
  213. ofs.x += TS->shaped_text_get_ascent(rid);
  214. }
  215. Rect2 rect = TS->shaped_text_get_object_rect(rid, p_key);
  216. rect.position += ofs;
  217. return rect;
  218. }
  219. void TextLine::set_horizontal_alignment(HorizontalAlignment p_alignment) {
  220. if (alignment != p_alignment) {
  221. if (alignment == HORIZONTAL_ALIGNMENT_FILL || p_alignment == HORIZONTAL_ALIGNMENT_FILL) {
  222. alignment = p_alignment;
  223. dirty = true;
  224. } else {
  225. alignment = p_alignment;
  226. }
  227. }
  228. }
  229. HorizontalAlignment TextLine::get_horizontal_alignment() const {
  230. return alignment;
  231. }
  232. void TextLine::tab_align(const Vector<float> &p_tab_stops) {
  233. tab_stops = p_tab_stops;
  234. dirty = true;
  235. }
  236. void TextLine::set_flags(BitField<TextServer::JustificationFlag> p_flags) {
  237. if (flags != p_flags) {
  238. flags = p_flags;
  239. dirty = true;
  240. }
  241. }
  242. BitField<TextServer::JustificationFlag> TextLine::get_flags() const {
  243. return flags;
  244. }
  245. void TextLine::set_text_overrun_behavior(TextServer::OverrunBehavior p_behavior) {
  246. if (overrun_behavior != p_behavior) {
  247. overrun_behavior = p_behavior;
  248. dirty = true;
  249. }
  250. }
  251. TextServer::OverrunBehavior TextLine::get_text_overrun_behavior() const {
  252. return overrun_behavior;
  253. }
  254. void TextLine::set_width(float p_width) {
  255. width = p_width;
  256. if (alignment == HORIZONTAL_ALIGNMENT_FILL || overrun_behavior != TextServer::OVERRUN_NO_TRIMMING) {
  257. dirty = true;
  258. }
  259. }
  260. float TextLine::get_width() const {
  261. return width;
  262. }
  263. Size2 TextLine::get_size() const {
  264. const_cast<TextLine *>(this)->_shape();
  265. return TS->shaped_text_get_size(rid);
  266. }
  267. float TextLine::get_line_ascent() const {
  268. const_cast<TextLine *>(this)->_shape();
  269. return TS->shaped_text_get_ascent(rid);
  270. }
  271. float TextLine::get_line_descent() const {
  272. const_cast<TextLine *>(this)->_shape();
  273. return TS->shaped_text_get_descent(rid);
  274. }
  275. float TextLine::get_line_width() const {
  276. const_cast<TextLine *>(this)->_shape();
  277. return TS->shaped_text_get_width(rid);
  278. }
  279. float TextLine::get_line_underline_position() const {
  280. const_cast<TextLine *>(this)->_shape();
  281. return TS->shaped_text_get_underline_position(rid);
  282. }
  283. float TextLine::get_line_underline_thickness() const {
  284. const_cast<TextLine *>(this)->_shape();
  285. return TS->shaped_text_get_underline_thickness(rid);
  286. }
  287. void TextLine::draw(RID p_canvas, const Vector2 &p_pos, const Color &p_color) const {
  288. const_cast<TextLine *>(this)->_shape();
  289. Vector2 ofs = p_pos;
  290. float length = TS->shaped_text_get_width(rid);
  291. if (width > 0) {
  292. switch (alignment) {
  293. case HORIZONTAL_ALIGNMENT_FILL:
  294. case HORIZONTAL_ALIGNMENT_LEFT:
  295. break;
  296. case HORIZONTAL_ALIGNMENT_CENTER: {
  297. if (length <= width) {
  298. if (TS->shaped_text_get_orientation(rid) == TextServer::ORIENTATION_HORIZONTAL) {
  299. ofs.x += Math::floor((width - length) / 2.0);
  300. } else {
  301. ofs.y += Math::floor((width - length) / 2.0);
  302. }
  303. } else if (TS->shaped_text_get_inferred_direction(rid) == TextServer::DIRECTION_RTL) {
  304. if (TS->shaped_text_get_orientation(rid) == TextServer::ORIENTATION_HORIZONTAL) {
  305. ofs.x += width - length;
  306. } else {
  307. ofs.y += width - length;
  308. }
  309. }
  310. } break;
  311. case HORIZONTAL_ALIGNMENT_RIGHT: {
  312. if (TS->shaped_text_get_orientation(rid) == TextServer::ORIENTATION_HORIZONTAL) {
  313. ofs.x += width - length;
  314. } else {
  315. ofs.y += width - length;
  316. }
  317. } break;
  318. }
  319. }
  320. float clip_l;
  321. if (TS->shaped_text_get_orientation(rid) == TextServer::ORIENTATION_HORIZONTAL) {
  322. ofs.y += TS->shaped_text_get_ascent(rid);
  323. clip_l = MAX(0, p_pos.x - ofs.x);
  324. } else {
  325. ofs.x += TS->shaped_text_get_ascent(rid);
  326. clip_l = MAX(0, p_pos.y - ofs.y);
  327. }
  328. return TS->shaped_text_draw(rid, p_canvas, ofs, clip_l, clip_l + width, p_color);
  329. }
  330. void TextLine::draw_outline(RID p_canvas, const Vector2 &p_pos, int p_outline_size, const Color &p_color) const {
  331. const_cast<TextLine *>(this)->_shape();
  332. Vector2 ofs = p_pos;
  333. float length = TS->shaped_text_get_width(rid);
  334. if (width > 0) {
  335. switch (alignment) {
  336. case HORIZONTAL_ALIGNMENT_FILL:
  337. case HORIZONTAL_ALIGNMENT_LEFT:
  338. break;
  339. case HORIZONTAL_ALIGNMENT_CENTER: {
  340. if (length <= width) {
  341. if (TS->shaped_text_get_orientation(rid) == TextServer::ORIENTATION_HORIZONTAL) {
  342. ofs.x += Math::floor((width - length) / 2.0);
  343. } else {
  344. ofs.y += Math::floor((width - length) / 2.0);
  345. }
  346. } else if (TS->shaped_text_get_inferred_direction(rid) == TextServer::DIRECTION_RTL) {
  347. if (TS->shaped_text_get_orientation(rid) == TextServer::ORIENTATION_HORIZONTAL) {
  348. ofs.x += width - length;
  349. } else {
  350. ofs.y += width - length;
  351. }
  352. }
  353. } break;
  354. case HORIZONTAL_ALIGNMENT_RIGHT: {
  355. if (TS->shaped_text_get_orientation(rid) == TextServer::ORIENTATION_HORIZONTAL) {
  356. ofs.x += width - length;
  357. } else {
  358. ofs.y += width - length;
  359. }
  360. } break;
  361. }
  362. }
  363. float clip_l;
  364. if (TS->shaped_text_get_orientation(rid) == TextServer::ORIENTATION_HORIZONTAL) {
  365. ofs.y += TS->shaped_text_get_ascent(rid);
  366. clip_l = MAX(0, p_pos.x - ofs.x);
  367. } else {
  368. ofs.x += TS->shaped_text_get_ascent(rid);
  369. clip_l = MAX(0, p_pos.y - ofs.y);
  370. }
  371. return TS->shaped_text_draw_outline(rid, p_canvas, ofs, clip_l, clip_l + width, p_outline_size, p_color);
  372. }
  373. int TextLine::hit_test(float p_coords) const {
  374. const_cast<TextLine *>(this)->_shape();
  375. return TS->shaped_text_hit_test_position(rid, p_coords);
  376. }
  377. 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) {
  378. rid = TS->create_shaped_text(p_direction, p_orientation);
  379. if (p_font.is_valid()) {
  380. TS->shaped_text_add_string(rid, p_text, p_font->get_rids(), p_font_size, p_font->get_opentype_features(), p_language);
  381. for (int i = 0; i < TextServer::SPACING_MAX; i++) {
  382. TS->shaped_text_set_spacing(rid, TextServer::SpacingType(i), p_font->get_spacing(TextServer::SpacingType(i)));
  383. }
  384. }
  385. }
  386. TextLine::TextLine() {
  387. rid = TS->create_shaped_text();
  388. }
  389. TextLine::~TextLine() {
  390. TS->free_rid(rid);
  391. }