text_server_adv.cpp 91 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616
  1. /*************************************************************************/
  2. /* text_server_adv.cpp */
  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. #include "text_server_adv.h"
  31. #include "bitmap_font_adv.h"
  32. #include "dynamic_font_adv.h"
  33. #include "core/string/translation.h"
  34. #ifdef ICU_STATIC_DATA
  35. #include "thirdparty/icu4c/icudata.gen.h"
  36. #endif
  37. _FORCE_INLINE_ bool is_ain(char32_t p_chr) {
  38. return u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_AIN;
  39. }
  40. _FORCE_INLINE_ bool is_alef(char32_t p_chr) {
  41. return u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_ALEF;
  42. }
  43. _FORCE_INLINE_ bool is_beh(char32_t p_chr) {
  44. int32_t prop = u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP);
  45. return (prop == U_JG_BEH) || (prop == U_JG_NOON) || (prop == U_JG_AFRICAN_NOON) || (prop == U_JG_NYA) || (prop == U_JG_YEH) || (prop == U_JG_FARSI_YEH);
  46. }
  47. _FORCE_INLINE_ bool is_dal(char32_t p_chr) {
  48. return u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_DAL;
  49. }
  50. _FORCE_INLINE_ bool is_feh(char32_t p_chr) {
  51. return (u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_FEH) || (u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_AFRICAN_FEH);
  52. }
  53. _FORCE_INLINE_ bool is_gaf(char32_t p_chr) {
  54. return u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_GAF;
  55. }
  56. _FORCE_INLINE_ bool is_heh(char32_t p_chr) {
  57. return u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_HEH;
  58. }
  59. _FORCE_INLINE_ bool is_kaf(char32_t p_chr) {
  60. return u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_KAF;
  61. }
  62. _FORCE_INLINE_ bool is_lam(char32_t p_chr) {
  63. return u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_LAM;
  64. }
  65. _FORCE_INLINE_ bool is_qaf(char32_t p_chr) {
  66. return (u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_QAF) || (u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_AFRICAN_QAF);
  67. }
  68. _FORCE_INLINE_ bool is_reh(char32_t p_chr) {
  69. return u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_REH;
  70. }
  71. _FORCE_INLINE_ bool is_seen_sad(char32_t p_chr) {
  72. return (u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_SAD) || (u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_SEEN);
  73. }
  74. _FORCE_INLINE_ bool is_tah(char32_t p_chr) {
  75. return u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_TAH;
  76. }
  77. _FORCE_INLINE_ bool is_teh_marbuta(char32_t p_chr) {
  78. return u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_TEH_MARBUTA;
  79. }
  80. _FORCE_INLINE_ bool is_yeh(char32_t p_chr) {
  81. int32_t prop = u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP);
  82. return (prop == U_JG_YEH) || (prop == U_JG_FARSI_YEH) || (prop == U_JG_YEH_BARREE) || (prop == U_JG_BURUSHASKI_YEH_BARREE) || (prop == U_JG_YEH_WITH_TAIL);
  83. }
  84. _FORCE_INLINE_ bool is_waw(char32_t p_chr) {
  85. return u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_WAW;
  86. }
  87. _FORCE_INLINE_ bool is_transparent(char32_t p_chr) {
  88. return u_getIntPropertyValue(p_chr, UCHAR_JOINING_TYPE) == U_JT_TRANSPARENT;
  89. }
  90. _FORCE_INLINE_ bool is_ligature(char32_t p_chr, char32_t p_nchr) {
  91. return (is_lam(p_chr) && is_alef(p_nchr));
  92. }
  93. _FORCE_INLINE_ bool is_connected_to_prev(char32_t p_chr, char32_t p_pchr) {
  94. int32_t prop = u_getIntPropertyValue(p_pchr, UCHAR_JOINING_TYPE);
  95. return (prop != U_JT_RIGHT_JOINING) && (prop != U_JT_NON_JOINING) ? !is_ligature(p_pchr, p_chr) : false;
  96. }
  97. _FORCE_INLINE_ bool is_control(char32_t p_char) {
  98. return (p_char <= 0x001f) || (p_char >= 0x007f && p_char <= 0x009F);
  99. }
  100. _FORCE_INLINE_ bool is_whitespace(char32_t p_char) {
  101. return (p_char == 0x0020) || (p_char == 0x00A0) || (p_char == 0x1680) || (p_char >= 0x2000 && p_char <= 0x200a) || (p_char == 0x202f) || (p_char == 0x205f) || (p_char == 0x3000) || (p_char == 0x2028) || (p_char == 0x2029) || (p_char >= 0x0009 && p_char <= 0x000d) || (p_char == 0x0085);
  102. }
  103. _FORCE_INLINE_ bool is_linebreak(char32_t p_char) {
  104. return (p_char >= 0x000a && p_char <= 0x000d) || (p_char == 0x0085) || (p_char == 0x2028) || (p_char == 0x2029);
  105. }
  106. /*************************************************************************/
  107. String TextServerAdvanced::interface_name = "ICU / HarfBuzz / Graphite";
  108. uint32_t TextServerAdvanced::interface_features = FEATURE_BIDI_LAYOUT | FEATURE_VERTICAL_LAYOUT | FEATURE_SHAPING | FEATURE_KASHIDA_JUSTIFICATION | FEATURE_BREAK_ITERATORS | FEATURE_USE_SUPPORT_DATA | FEATURE_FONT_VARIABLE;
  109. bool TextServerAdvanced::has_feature(Feature p_feature) {
  110. return (interface_features & p_feature) == p_feature;
  111. }
  112. String TextServerAdvanced::get_name() const {
  113. return interface_name;
  114. }
  115. void TextServerAdvanced::free(RID p_rid) {
  116. _THREAD_SAFE_METHOD_
  117. if (font_owner.owns(p_rid)) {
  118. FontDataAdvanced *fd = font_owner.getornull(p_rid);
  119. font_owner.free(p_rid);
  120. memdelete(fd);
  121. } else if (shaped_owner.owns(p_rid)) {
  122. ShapedTextDataAdvanced *sd = shaped_owner.getornull(p_rid);
  123. shaped_owner.free(p_rid);
  124. memdelete(sd);
  125. }
  126. }
  127. bool TextServerAdvanced::has(RID p_rid) {
  128. _THREAD_SAFE_METHOD_
  129. return font_owner.owns(p_rid) || shaped_owner.owns(p_rid);
  130. }
  131. bool TextServerAdvanced::load_support_data(const String &p_filename) {
  132. _THREAD_SAFE_METHOD_
  133. #ifdef ICU_STATIC_DATA
  134. if (icu_data == nullptr) {
  135. UErrorCode err = U_ZERO_ERROR;
  136. u_init(&err); // Do not check for errors, since we only load part the of data.
  137. icu_data = (uint8_t *)&U_ICUDATA_ENTRY_POINT;
  138. }
  139. #else
  140. if (icu_data == nullptr) {
  141. String filename = (p_filename.is_empty()) ? String("res://") + _MKSTR(ICU_DATA_NAME) : p_filename;
  142. FileAccess *f = FileAccess::open(filename, FileAccess::READ);
  143. if (!f) {
  144. return false;
  145. }
  146. UErrorCode err = U_ZERO_ERROR;
  147. // ICU data found.
  148. size_t len = f->get_len();
  149. icu_data = (uint8_t *)memalloc(len);
  150. f->get_buffer(icu_data, len);
  151. f->close();
  152. memdelete(f);
  153. udata_setCommonData(icu_data, &err);
  154. if (U_FAILURE(err)) {
  155. memfree(icu_data);
  156. icu_data = nullptr;
  157. ERR_FAIL_V_MSG(false, u_errorName(err));
  158. }
  159. err = U_ZERO_ERROR;
  160. u_init(&err);
  161. if (U_FAILURE(err)) {
  162. memfree(icu_data);
  163. icu_data = nullptr;
  164. ERR_FAIL_V_MSG(false, u_errorName(err));
  165. }
  166. }
  167. #endif
  168. return true;
  169. }
  170. #ifdef TOOLS_ENABLED
  171. bool TextServerAdvanced::save_support_data(const String &p_filename) {
  172. _THREAD_SAFE_METHOD_
  173. #ifdef ICU_STATIC_DATA
  174. // Store data to the res file if it's available.
  175. FileAccess *f = FileAccess::open(p_filename, FileAccess::WRITE);
  176. if (!f) {
  177. return false;
  178. }
  179. f->store_buffer(U_ICUDATA_ENTRY_POINT, U_ICUDATA_SIZE);
  180. f->close();
  181. memdelete(f);
  182. return true;
  183. #else
  184. return false;
  185. #endif
  186. }
  187. #endif
  188. bool TextServerAdvanced::is_locale_right_to_left(const String &p_locale) {
  189. String l = p_locale.get_slicec('_', 0);
  190. if ((l == "ar") || (l == "dv") || (l == "he") || (l == "fa") || (l == "ff") || (l == "ku") || (l == "ur")) {
  191. return true;
  192. } else {
  193. return false;
  194. }
  195. }
  196. struct FeatureInfo {
  197. int32_t tag;
  198. String name;
  199. };
  200. static FeatureInfo feature_set[] = {
  201. // Registred OpenType feature tags.
  202. { HB_TAG('a', 'a', 'l', 't'), "access_all_alternates" },
  203. { HB_TAG('a', 'b', 'v', 'f'), "above_base_forms" },
  204. { HB_TAG('a', 'b', 'v', 'm'), "above_base_mark_positioning" },
  205. { HB_TAG('a', 'b', 'v', 's'), "above_base_substitutions" },
  206. { HB_TAG('a', 'f', 'r', 'c'), "alternative_fractions" },
  207. { HB_TAG('a', 'k', 'h', 'n'), "akhands" },
  208. { HB_TAG('b', 'l', 'w', 'f'), "below_base_forms" },
  209. { HB_TAG('b', 'l', 'w', 'm'), "below_base_mark_positioning" },
  210. { HB_TAG('b', 'l', 'w', 's'), "below_base_substitutions" },
  211. { HB_TAG('c', 'a', 'l', 't'), "contextual_alternates" },
  212. { HB_TAG('c', 'a', 's', 'e'), "case_sensitive_forms" },
  213. { HB_TAG('c', 'c', 'm', 'p'), "glyph_composition" },
  214. { HB_TAG('c', 'f', 'a', 'r'), "conjunct_form_after_ro" },
  215. { HB_TAG('c', 'j', 'c', 't'), "conjunct_forms" },
  216. { HB_TAG('c', 'l', 'i', 'g'), "contextual_ligatures" },
  217. { HB_TAG('c', 'p', 'c', 't'), "centered_cjk_punctuation" },
  218. { HB_TAG('c', 'p', 's', 'p'), "capital_spacing" },
  219. { HB_TAG('c', 's', 'w', 'h'), "contextual_swash" },
  220. { HB_TAG('c', 'u', 'r', 's'), "cursive_positioning" },
  221. { HB_TAG('c', 'v', '0', '1'), "character_variant_01" },
  222. { HB_TAG('c', 'v', '0', '2'), "character_variant_02" },
  223. { HB_TAG('c', 'v', '0', '3'), "character_variant_03" },
  224. { HB_TAG('c', 'v', '0', '4'), "character_variant_04" },
  225. { HB_TAG('c', 'v', '0', '5'), "character_variant_05" },
  226. { HB_TAG('c', 'v', '0', '6'), "character_variant_06" },
  227. { HB_TAG('c', 'v', '0', '7'), "character_variant_07" },
  228. { HB_TAG('c', 'v', '0', '8'), "character_variant_08" },
  229. { HB_TAG('c', 'v', '0', '9'), "character_variant_09" },
  230. { HB_TAG('c', 'v', '1', '0'), "character_variant_10" },
  231. { HB_TAG('c', 'v', '1', '1'), "character_variant_11" },
  232. { HB_TAG('c', 'v', '1', '2'), "character_variant_12" },
  233. { HB_TAG('c', 'v', '1', '3'), "character_variant_13" },
  234. { HB_TAG('c', 'v', '1', '4'), "character_variant_14" },
  235. { HB_TAG('c', 'v', '1', '5'), "character_variant_15" },
  236. { HB_TAG('c', 'v', '1', '6'), "character_variant_16" },
  237. { HB_TAG('c', 'v', '1', '7'), "character_variant_17" },
  238. { HB_TAG('c', 'v', '1', '8'), "character_variant_18" },
  239. { HB_TAG('c', 'v', '1', '9'), "character_variant_19" },
  240. { HB_TAG('c', 'v', '2', '0'), "character_variant_20" },
  241. { HB_TAG('c', 'v', '2', '1'), "character_variant_21" },
  242. { HB_TAG('c', 'v', '2', '2'), "character_variant_22" },
  243. { HB_TAG('c', 'v', '2', '3'), "character_variant_23" },
  244. { HB_TAG('c', 'v', '2', '4'), "character_variant_24" },
  245. { HB_TAG('c', 'v', '2', '5'), "character_variant_25" },
  246. { HB_TAG('c', 'v', '2', '6'), "character_variant_26" },
  247. { HB_TAG('c', 'v', '2', '7'), "character_variant_27" },
  248. { HB_TAG('c', 'v', '2', '8'), "character_variant_28" },
  249. { HB_TAG('c', 'v', '2', '9'), "character_variant_29" },
  250. { HB_TAG('c', 'v', '3', '0'), "character_variant_30" },
  251. { HB_TAG('c', 'v', '3', '1'), "character_variant_31" },
  252. { HB_TAG('c', 'v', '3', '2'), "character_variant_32" },
  253. { HB_TAG('c', 'v', '3', '3'), "character_variant_33" },
  254. { HB_TAG('c', 'v', '3', '4'), "character_variant_34" },
  255. { HB_TAG('c', 'v', '3', '5'), "character_variant_35" },
  256. { HB_TAG('c', 'v', '3', '6'), "character_variant_36" },
  257. { HB_TAG('c', 'v', '3', '7'), "character_variant_37" },
  258. { HB_TAG('c', 'v', '3', '8'), "character_variant_38" },
  259. { HB_TAG('c', 'v', '3', '9'), "character_variant_39" },
  260. { HB_TAG('c', 'v', '4', '0'), "character_variant_40" },
  261. { HB_TAG('c', 'v', '4', '1'), "character_variant_41" },
  262. { HB_TAG('c', 'v', '4', '2'), "character_variant_42" },
  263. { HB_TAG('c', 'v', '4', '3'), "character_variant_43" },
  264. { HB_TAG('c', 'v', '4', '4'), "character_variant_44" },
  265. { HB_TAG('c', 'v', '4', '5'), "character_variant_45" },
  266. { HB_TAG('c', 'v', '4', '6'), "character_variant_46" },
  267. { HB_TAG('c', 'v', '4', '7'), "character_variant_47" },
  268. { HB_TAG('c', 'v', '4', '8'), "character_variant_48" },
  269. { HB_TAG('c', 'v', '4', '9'), "character_variant_49" },
  270. { HB_TAG('c', 'v', '5', '0'), "character_variant_50" },
  271. { HB_TAG('c', 'v', '5', '1'), "character_variant_51" },
  272. { HB_TAG('c', 'v', '5', '2'), "character_variant_52" },
  273. { HB_TAG('c', 'v', '5', '3'), "character_variant_53" },
  274. { HB_TAG('c', 'v', '5', '4'), "character_variant_54" },
  275. { HB_TAG('c', 'v', '5', '5'), "character_variant_55" },
  276. { HB_TAG('c', 'v', '5', '6'), "character_variant_56" },
  277. { HB_TAG('c', 'v', '5', '7'), "character_variant_57" },
  278. { HB_TAG('c', 'v', '5', '8'), "character_variant_58" },
  279. { HB_TAG('c', 'v', '5', '9'), "character_variant_59" },
  280. { HB_TAG('c', 'v', '6', '0'), "character_variant_60" },
  281. { HB_TAG('c', 'v', '6', '1'), "character_variant_61" },
  282. { HB_TAG('c', 'v', '6', '2'), "character_variant_62" },
  283. { HB_TAG('c', 'v', '6', '3'), "character_variant_63" },
  284. { HB_TAG('c', 'v', '6', '4'), "character_variant_64" },
  285. { HB_TAG('c', 'v', '6', '5'), "character_variant_65" },
  286. { HB_TAG('c', 'v', '6', '6'), "character_variant_66" },
  287. { HB_TAG('c', 'v', '6', '7'), "character_variant_67" },
  288. { HB_TAG('c', 'v', '6', '8'), "character_variant_68" },
  289. { HB_TAG('c', 'v', '6', '9'), "character_variant_69" },
  290. { HB_TAG('c', 'v', '7', '0'), "character_variant_70" },
  291. { HB_TAG('c', 'v', '7', '1'), "character_variant_71" },
  292. { HB_TAG('c', 'v', '7', '2'), "character_variant_72" },
  293. { HB_TAG('c', 'v', '7', '3'), "character_variant_73" },
  294. { HB_TAG('c', 'v', '7', '4'), "character_variant_74" },
  295. { HB_TAG('c', 'v', '7', '5'), "character_variant_75" },
  296. { HB_TAG('c', 'v', '7', '6'), "character_variant_76" },
  297. { HB_TAG('c', 'v', '7', '7'), "character_variant_77" },
  298. { HB_TAG('c', 'v', '7', '8'), "character_variant_78" },
  299. { HB_TAG('c', 'v', '7', '9'), "character_variant_79" },
  300. { HB_TAG('c', 'v', '8', '0'), "character_variant_80" },
  301. { HB_TAG('c', 'v', '8', '1'), "character_variant_81" },
  302. { HB_TAG('c', 'v', '8', '2'), "character_variant_82" },
  303. { HB_TAG('c', 'v', '8', '3'), "character_variant_83" },
  304. { HB_TAG('c', 'v', '8', '4'), "character_variant_84" },
  305. { HB_TAG('c', 'v', '8', '5'), "character_variant_85" },
  306. { HB_TAG('c', 'v', '8', '6'), "character_variant_86" },
  307. { HB_TAG('c', 'v', '8', '7'), "character_variant_87" },
  308. { HB_TAG('c', 'v', '8', '8'), "character_variant_88" },
  309. { HB_TAG('c', 'v', '8', '9'), "character_variant_89" },
  310. { HB_TAG('c', 'v', '9', '0'), "character_variant_90" },
  311. { HB_TAG('c', 'v', '9', '1'), "character_variant_91" },
  312. { HB_TAG('c', 'v', '9', '2'), "character_variant_92" },
  313. { HB_TAG('c', 'v', '9', '3'), "character_variant_93" },
  314. { HB_TAG('c', 'v', '9', '4'), "character_variant_94" },
  315. { HB_TAG('c', 'v', '9', '5'), "character_variant_95" },
  316. { HB_TAG('c', 'v', '9', '6'), "character_variant_96" },
  317. { HB_TAG('c', 'v', '9', '7'), "character_variant_97" },
  318. { HB_TAG('c', 'v', '9', '8'), "character_variant_98" },
  319. { HB_TAG('c', 'v', '9', '9'), "character_variant_99" },
  320. { HB_TAG('c', '2', 'p', 'c'), "petite_capitals_from_capitals" },
  321. { HB_TAG('c', '2', 's', 'c'), "small_capitals_from_capitals" },
  322. { HB_TAG('d', 'i', 's', 't'), "distances" },
  323. { HB_TAG('d', 'l', 'i', 'g'), "discretionary_ligatures" },
  324. { HB_TAG('d', 'n', 'o', 'm'), "denominators" },
  325. { HB_TAG('d', 't', 'l', 's'), "dotless_forms" },
  326. { HB_TAG('e', 'x', 'p', 't'), "expert_forms" },
  327. { HB_TAG('f', 'a', 'l', 't'), "final_glyph_on_line_alternates" },
  328. { HB_TAG('f', 'i', 'n', '2'), "terminal_forms_2" },
  329. { HB_TAG('f', 'i', 'n', '3'), "terminal_forms_3" },
  330. { HB_TAG('f', 'i', 'n', 'a'), "terminal_forms" },
  331. { HB_TAG('f', 'l', 'a', 'c'), "flattened_accent_forms" },
  332. { HB_TAG('f', 'r', 'a', 'c'), "fractions" },
  333. { HB_TAG('f', 'w', 'i', 'd'), "full_widths" },
  334. { HB_TAG('h', 'a', 'l', 'f'), "half_forms" },
  335. { HB_TAG('h', 'a', 'l', 'n'), "halant_forms" },
  336. { HB_TAG('h', 'a', 'l', 't'), "alternate_half_widths" },
  337. { HB_TAG('h', 'i', 's', 't'), "historical_forms" },
  338. { HB_TAG('h', 'k', 'n', 'a'), "horizontal_kana_alternates" },
  339. { HB_TAG('h', 'l', 'i', 'g'), "historical_ligatures" },
  340. { HB_TAG('h', 'n', 'g', 'l'), "hangul" },
  341. { HB_TAG('h', 'o', 'j', 'o'), "hojo_kanji_forms" },
  342. { HB_TAG('h', 'w', 'i', 'd'), "half_widths" },
  343. { HB_TAG('i', 'n', 'i', 't'), "initial_forms" },
  344. { HB_TAG('i', 's', 'o', 'l'), "isolated_forms" },
  345. { HB_TAG('i', 't', 'a', 'l'), "italics" },
  346. { HB_TAG('j', 'a', 'l', 't'), "justification_alternates" },
  347. { HB_TAG('j', 'p', '7', '8'), "jis78_forms" },
  348. { HB_TAG('j', 'p', '8', '3'), "jis83_forms" },
  349. { HB_TAG('j', 'p', '9', '0'), "jis90_forms" },
  350. { HB_TAG('j', 'p', '0', '4'), "jis2004_forms" },
  351. { HB_TAG('k', 'e', 'r', 'n'), "kerning" },
  352. { HB_TAG('l', 'f', 'b', 'd'), "left_bounds" },
  353. { HB_TAG('l', 'i', 'g', 'a'), "standard_ligatures" },
  354. { HB_TAG('l', 'j', 'm', 'o'), "leading_jamo_forms" },
  355. { HB_TAG('l', 'n', 'u', 'm'), "lining_figures" },
  356. { HB_TAG('l', 'o', 'c', 'l'), "localized_forms" },
  357. { HB_TAG('l', 't', 'r', 'a'), "left_to_right_alternates" },
  358. { HB_TAG('l', 't', 'r', 'm'), "left_to_right_mirrored_forms" },
  359. { HB_TAG('m', 'a', 'r', 'k'), "mark_positioning" },
  360. { HB_TAG('m', 'e', 'd', '2'), "medial_forms_2" },
  361. { HB_TAG('m', 'e', 'd', 'i'), "medial_forms" },
  362. { HB_TAG('m', 'g', 'r', 'k'), "mathematical_greek" },
  363. { HB_TAG('m', 'k', 'm', 'k'), "mark_to_mark_positioning" },
  364. { HB_TAG('m', 's', 'e', 't'), "mark_positioning_via_substitution" },
  365. { HB_TAG('n', 'a', 'l', 't'), "alternate_annotation_forms" },
  366. { HB_TAG('n', 'l', 'c', 'k'), "nlc_kanji_forms" },
  367. { HB_TAG('n', 'u', 'k', 't'), "nukta_forms" },
  368. { HB_TAG('n', 'u', 'm', 'r'), "numerators" },
  369. { HB_TAG('o', 'n', 'u', 'm'), "oldstyle_figures" },
  370. { HB_TAG('o', 'p', 'b', 'd'), "optical_bounds" },
  371. { HB_TAG('o', 'r', 'd', 'n'), "ordinals" },
  372. { HB_TAG('o', 'r', 'n', 'm'), "ornaments" },
  373. { HB_TAG('p', 'a', 'l', 't'), "proportional_alternate_widths" },
  374. { HB_TAG('p', 'c', 'a', 'p'), "petite_capitals" },
  375. { HB_TAG('p', 'k', 'n', 'a'), "proportional_kana" },
  376. { HB_TAG('p', 'n', 'u', 'm'), "proportional_figures" },
  377. { HB_TAG('p', 'r', 'e', 'f'), "pre_base_forms" },
  378. { HB_TAG('p', 'r', 'e', 's'), "pre_base_substitutions" },
  379. { HB_TAG('p', 's', 't', 'f'), "post_base_forms" },
  380. { HB_TAG('p', 's', 't', 's'), "post_base_substitutions" },
  381. { HB_TAG('p', 'w', 'i', 'd'), "proportional_widths" },
  382. { HB_TAG('q', 'w', 'i', 'd'), "quarter_widths" },
  383. { HB_TAG('r', 'a', 'n', 'd'), "randomize" },
  384. { HB_TAG('r', 'c', 'l', 't'), "required_contextual_alternates" },
  385. { HB_TAG('r', 'k', 'r', 'f'), "rakar_forms" },
  386. { HB_TAG('r', 'l', 'i', 'g'), "required_ligatures" },
  387. { HB_TAG('r', 'p', 'h', 'f'), "reph_forms" },
  388. { HB_TAG('r', 't', 'b', 'd'), "right_bounds" },
  389. { HB_TAG('r', 't', 'l', 'a'), "right_to_left_alternates" },
  390. { HB_TAG('r', 't', 'l', 'm'), "right_to_left_mirrored_forms" },
  391. { HB_TAG('r', 'u', 'b', 'y'), "ruby_notation_forms" },
  392. { HB_TAG('r', 'v', 'r', 'n'), "required_variation_alternates" },
  393. { HB_TAG('s', 'a', 'l', 't'), "stylistic_alternates" },
  394. { HB_TAG('s', 'i', 'n', 'f'), "scientific_inferiors" },
  395. { HB_TAG('s', 'i', 'z', 'e'), "optical_size" },
  396. { HB_TAG('s', 'm', 'c', 'p'), "small_capitals" },
  397. { HB_TAG('s', 'm', 'p', 'l'), "simplified_forms" },
  398. { HB_TAG('s', 's', '0', '1'), "stylistic_set_01" },
  399. { HB_TAG('s', 's', '0', '2'), "stylistic_set_02" },
  400. { HB_TAG('s', 's', '0', '3'), "stylistic_set_03" },
  401. { HB_TAG('s', 's', '0', '4'), "stylistic_set_04" },
  402. { HB_TAG('s', 's', '0', '5'), "stylistic_set_05" },
  403. { HB_TAG('s', 's', '0', '6'), "stylistic_set_06" },
  404. { HB_TAG('s', 's', '0', '7'), "stylistic_set_07" },
  405. { HB_TAG('s', 's', '0', '8'), "stylistic_set_08" },
  406. { HB_TAG('s', 's', '0', '9'), "stylistic_set_09" },
  407. { HB_TAG('s', 's', '1', '0'), "stylistic_set_10" },
  408. { HB_TAG('s', 's', '1', '1'), "stylistic_set_11" },
  409. { HB_TAG('s', 's', '1', '2'), "stylistic_set_12" },
  410. { HB_TAG('s', 's', '1', '3'), "stylistic_set_13" },
  411. { HB_TAG('s', 's', '1', '4'), "stylistic_set_14" },
  412. { HB_TAG('s', 's', '1', '5'), "stylistic_set_15" },
  413. { HB_TAG('s', 's', '1', '6'), "stylistic_set_16" },
  414. { HB_TAG('s', 's', '1', '7'), "stylistic_set_17" },
  415. { HB_TAG('s', 's', '1', '8'), "stylistic_set_18" },
  416. { HB_TAG('s', 's', '1', '9'), "stylistic_set_19" },
  417. { HB_TAG('s', 's', '2', '0'), "stylistic_set_20" },
  418. { HB_TAG('s', 's', 't', 'y'), "math_script_style_alternates" },
  419. { HB_TAG('s', 't', 'c', 'h'), "stretching_glyph_decomposition" },
  420. { HB_TAG('s', 'u', 'b', 's'), "subscript" },
  421. { HB_TAG('s', 'u', 'p', 's'), "superscript" },
  422. { HB_TAG('s', 'w', 's', 'h'), "swash" },
  423. { HB_TAG('t', 'i', 't', 'l'), "titling" },
  424. { HB_TAG('t', 'j', 'm', 'o'), "trailing_jamo_forms" },
  425. { HB_TAG('t', 'n', 'a', 'm'), "traditional_name_forms" },
  426. { HB_TAG('t', 'n', 'u', 'm'), "tabular_figures" },
  427. { HB_TAG('t', 'r', 'a', 'd'), "traditional_forms" },
  428. { HB_TAG('t', 'w', 'i', 'd'), "third_widths" },
  429. { HB_TAG('u', 'n', 'i', 'c'), "unicase" },
  430. { HB_TAG('v', 'a', 'l', 't'), "alternate_vertical_metrics" },
  431. { HB_TAG('v', 'a', 't', 'u'), "vattu_variants" },
  432. { HB_TAG('v', 'e', 'r', 't'), "vertical_writing" },
  433. { HB_TAG('v', 'h', 'a', 'l'), "alternate_vertical_half_metrics" },
  434. { HB_TAG('v', 'j', 'm', 'o'), "vowel_jamo_forms" },
  435. { HB_TAG('v', 'k', 'n', 'a'), "vertical_kana_alternates" },
  436. { HB_TAG('v', 'k', 'r', 'n'), "vertical_kerning" },
  437. { HB_TAG('v', 'p', 'a', 'l'), "proportional_alternate_vertical_metrics" },
  438. { HB_TAG('v', 'r', 't', '2'), "vertical_alternates_and_rotation" },
  439. { HB_TAG('v', 'r', 't', 'r'), "vertical_alternates_for_rotation" },
  440. { HB_TAG('z', 'e', 'r', 'o'), "slashed_zero" },
  441. // Registred OpenType variation tags.
  442. { HB_TAG('i', 't', 'a', 'l'), "italic" },
  443. { HB_TAG('o', 'p', 's', 'z'), "optical_size" },
  444. { HB_TAG('s', 'l', 'n', 't'), "slant" },
  445. { HB_TAG('w', 'd', 't', 'h'), "width" },
  446. { HB_TAG('w', 'g', 'h', 't'), "weight" },
  447. { 0, String() },
  448. };
  449. int32_t TextServerAdvanced::name_to_tag(const String &p_name) {
  450. for (int i = 0; feature_set[i].tag != 0; i++) {
  451. if (feature_set[i].name == p_name) {
  452. return feature_set[i].tag;
  453. }
  454. }
  455. // No readable name, use tag string.
  456. return hb_tag_from_string(p_name.replace("custom_", "").ascii().get_data(), -1);
  457. }
  458. String TextServerAdvanced::tag_to_name(int32_t p_tag) {
  459. for (int i = 0; feature_set[i].tag != 0; i++) {
  460. if (feature_set[i].tag == p_tag) {
  461. return feature_set[i].name;
  462. }
  463. }
  464. // No readable name, use tag string.
  465. char name[5];
  466. memset(name, 0, 5);
  467. hb_tag_to_string(p_tag, name);
  468. return String("custom_") + String(name);
  469. }
  470. /*************************************************************************/
  471. /* Font interface */
  472. /*************************************************************************/
  473. RID TextServerAdvanced::create_font_system(const String &p_name, int p_base_size) {
  474. ERR_FAIL_V_MSG(RID(), "System fonts are not supported by this text server.");
  475. }
  476. RID TextServerAdvanced::create_font_resource(const String &p_filename, int p_base_size) {
  477. _THREAD_SAFE_METHOD_
  478. FontDataAdvanced *fd = nullptr;
  479. if (p_filename.get_extension() == "fnt" || p_filename.get_extension() == "font") {
  480. fd = memnew(BitmapFontDataAdvanced);
  481. #ifdef MODULE_FREETYPE_ENABLED
  482. } else if (p_filename.get_extension() == "ttf" || p_filename.get_extension() == "otf" || p_filename.get_extension() == "woff") {
  483. fd = memnew(DynamicFontDataAdvanced);
  484. #endif
  485. } else {
  486. return RID();
  487. }
  488. Error err = fd->load_from_file(p_filename, p_base_size);
  489. if (err != OK) {
  490. memdelete(fd);
  491. return RID();
  492. }
  493. return font_owner.make_rid(fd);
  494. }
  495. RID TextServerAdvanced::create_font_memory(const uint8_t *p_data, size_t p_size, const String &p_type, int p_base_size) {
  496. _THREAD_SAFE_METHOD_
  497. FontDataAdvanced *fd = nullptr;
  498. if (p_type == "fnt" || p_type == "font") {
  499. fd = memnew(BitmapFontDataAdvanced);
  500. #ifdef MODULE_FREETYPE_ENABLED
  501. } else if (p_type == "ttf" || p_type == "otf" || p_type == "woff") {
  502. fd = memnew(DynamicFontDataAdvanced);
  503. #endif
  504. } else {
  505. return RID();
  506. }
  507. Error err = fd->load_from_memory(p_data, p_size, p_base_size);
  508. if (err != OK) {
  509. memdelete(fd);
  510. return RID();
  511. }
  512. return font_owner.make_rid(fd);
  513. }
  514. float TextServerAdvanced::font_get_height(RID p_font, int p_size) const {
  515. _THREAD_SAFE_METHOD_
  516. const FontDataAdvanced *fd = font_owner.getornull(p_font);
  517. ERR_FAIL_COND_V(!fd, 0.f);
  518. return fd->get_height(p_size);
  519. }
  520. float TextServerAdvanced::font_get_ascent(RID p_font, int p_size) const {
  521. _THREAD_SAFE_METHOD_
  522. const FontDataAdvanced *fd = font_owner.getornull(p_font);
  523. ERR_FAIL_COND_V(!fd, 0.f);
  524. return fd->get_ascent(p_size);
  525. }
  526. float TextServerAdvanced::font_get_descent(RID p_font, int p_size) const {
  527. _THREAD_SAFE_METHOD_
  528. const FontDataAdvanced *fd = font_owner.getornull(p_font);
  529. ERR_FAIL_COND_V(!fd, 0.f);
  530. return fd->get_descent(p_size);
  531. }
  532. float TextServerAdvanced::font_get_underline_position(RID p_font, int p_size) const {
  533. _THREAD_SAFE_METHOD_
  534. const FontDataAdvanced *fd = font_owner.getornull(p_font);
  535. ERR_FAIL_COND_V(!fd, 0.f);
  536. return fd->get_underline_position(p_size);
  537. }
  538. float TextServerAdvanced::font_get_underline_thickness(RID p_font, int p_size) const {
  539. _THREAD_SAFE_METHOD_
  540. const FontDataAdvanced *fd = font_owner.getornull(p_font);
  541. ERR_FAIL_COND_V(!fd, 0.f);
  542. return fd->get_underline_thickness(p_size);
  543. }
  544. int TextServerAdvanced::font_get_spacing_space(RID p_font) const {
  545. _THREAD_SAFE_METHOD_
  546. const FontDataAdvanced *fd = font_owner.getornull(p_font);
  547. ERR_FAIL_COND_V(!fd, 0);
  548. return fd->get_spacing_space();
  549. }
  550. void TextServerAdvanced::font_set_spacing_space(RID p_font, int p_value) {
  551. _THREAD_SAFE_METHOD_
  552. FontDataAdvanced *fd = font_owner.getornull(p_font);
  553. ERR_FAIL_COND(!fd);
  554. fd->set_spacing_space(p_value);
  555. }
  556. int TextServerAdvanced::font_get_spacing_glyph(RID p_font) const {
  557. _THREAD_SAFE_METHOD_
  558. const FontDataAdvanced *fd = font_owner.getornull(p_font);
  559. ERR_FAIL_COND_V(!fd, 0);
  560. return fd->get_spacing_glyph();
  561. }
  562. void TextServerAdvanced::font_set_spacing_glyph(RID p_font, int p_value) {
  563. _THREAD_SAFE_METHOD_
  564. FontDataAdvanced *fd = font_owner.getornull(p_font);
  565. ERR_FAIL_COND(!fd);
  566. fd->set_spacing_glyph(p_value);
  567. }
  568. void TextServerAdvanced::font_set_antialiased(RID p_font, bool p_antialiased) {
  569. _THREAD_SAFE_METHOD_
  570. FontDataAdvanced *fd = font_owner.getornull(p_font);
  571. ERR_FAIL_COND(!fd);
  572. fd->set_antialiased(p_antialiased);
  573. }
  574. Dictionary TextServerAdvanced::font_get_feature_list(RID p_font) const {
  575. _THREAD_SAFE_METHOD_
  576. const FontDataAdvanced *fd = font_owner.getornull(p_font);
  577. ERR_FAIL_COND_V(!fd, Dictionary());
  578. return fd->get_feature_list();
  579. }
  580. bool TextServerAdvanced::font_get_antialiased(RID p_font) const {
  581. _THREAD_SAFE_METHOD_
  582. const FontDataAdvanced *fd = font_owner.getornull(p_font);
  583. ERR_FAIL_COND_V(!fd, false);
  584. return fd->get_antialiased();
  585. }
  586. Dictionary TextServerAdvanced::font_get_variation_list(RID p_font) const {
  587. _THREAD_SAFE_METHOD_
  588. const FontDataAdvanced *fd = font_owner.getornull(p_font);
  589. ERR_FAIL_COND_V(!fd, Dictionary());
  590. return fd->get_variation_list();
  591. }
  592. void TextServerAdvanced::font_set_variation(RID p_font, const String &p_name, double p_value) {
  593. _THREAD_SAFE_METHOD_
  594. FontDataAdvanced *fd = font_owner.getornull(p_font);
  595. ERR_FAIL_COND(!fd);
  596. fd->set_variation(p_name, p_value);
  597. }
  598. double TextServerAdvanced::font_get_variation(RID p_font, const String &p_name) const {
  599. _THREAD_SAFE_METHOD_
  600. const FontDataAdvanced *fd = font_owner.getornull(p_font);
  601. ERR_FAIL_COND_V(!fd, 0);
  602. return fd->get_variation(p_name);
  603. }
  604. void TextServerAdvanced::font_set_distance_field_hint(RID p_font, bool p_distance_field) {
  605. _THREAD_SAFE_METHOD_
  606. FontDataAdvanced *fd = font_owner.getornull(p_font);
  607. ERR_FAIL_COND(!fd);
  608. fd->set_distance_field_hint(p_distance_field);
  609. }
  610. bool TextServerAdvanced::font_get_distance_field_hint(RID p_font) const {
  611. _THREAD_SAFE_METHOD_
  612. const FontDataAdvanced *fd = font_owner.getornull(p_font);
  613. ERR_FAIL_COND_V(!fd, false);
  614. return fd->get_distance_field_hint();
  615. }
  616. void TextServerAdvanced::font_set_hinting(RID p_font, TextServer::Hinting p_hinting) {
  617. _THREAD_SAFE_METHOD_
  618. FontDataAdvanced *fd = font_owner.getornull(p_font);
  619. ERR_FAIL_COND(!fd);
  620. fd->set_hinting(p_hinting);
  621. }
  622. TextServer::Hinting TextServerAdvanced::font_get_hinting(RID p_font) const {
  623. _THREAD_SAFE_METHOD_
  624. const FontDataAdvanced *fd = font_owner.getornull(p_font);
  625. ERR_FAIL_COND_V(!fd, TextServer::HINTING_NONE);
  626. return fd->get_hinting();
  627. }
  628. void TextServerAdvanced::font_set_force_autohinter(RID p_font, bool p_enabeld) {
  629. _THREAD_SAFE_METHOD_
  630. FontDataAdvanced *fd = font_owner.getornull(p_font);
  631. ERR_FAIL_COND(!fd);
  632. fd->set_force_autohinter(p_enabeld);
  633. }
  634. bool TextServerAdvanced::font_get_force_autohinter(RID p_font) const {
  635. _THREAD_SAFE_METHOD_
  636. const FontDataAdvanced *fd = font_owner.getornull(p_font);
  637. ERR_FAIL_COND_V(!fd, false);
  638. return fd->get_force_autohinter();
  639. }
  640. bool TextServerAdvanced::font_has_char(RID p_font, char32_t p_char) const {
  641. _THREAD_SAFE_METHOD_
  642. const FontDataAdvanced *fd = font_owner.getornull(p_font);
  643. ERR_FAIL_COND_V(!fd, false);
  644. return fd->has_char(p_char);
  645. }
  646. String TextServerAdvanced::font_get_supported_chars(RID p_font) const {
  647. _THREAD_SAFE_METHOD_
  648. const FontDataAdvanced *fd = font_owner.getornull(p_font);
  649. ERR_FAIL_COND_V(!fd, String());
  650. return fd->get_supported_chars();
  651. }
  652. bool TextServerAdvanced::font_has_outline(RID p_font) const {
  653. _THREAD_SAFE_METHOD_
  654. const FontDataAdvanced *fd = font_owner.getornull(p_font);
  655. ERR_FAIL_COND_V(!fd, false);
  656. return fd->has_outline();
  657. }
  658. float TextServerAdvanced::font_get_base_size(RID p_font) const {
  659. _THREAD_SAFE_METHOD_
  660. const FontDataAdvanced *fd = font_owner.getornull(p_font);
  661. ERR_FAIL_COND_V(!fd, 0.f);
  662. return fd->get_base_size();
  663. }
  664. bool TextServerAdvanced::font_is_language_supported(RID p_font, const String &p_language) const {
  665. _THREAD_SAFE_METHOD_
  666. const FontDataAdvanced *fd = font_owner.getornull(p_font);
  667. ERR_FAIL_COND_V(!fd, false);
  668. if (fd->lang_support_overrides.has(p_language)) {
  669. return fd->lang_support_overrides[p_language];
  670. } else {
  671. Vector<String> tags = p_language.replace("-", "_").split("_");
  672. if (tags.size() > 0) {
  673. if (fd->lang_support_overrides.has(tags[0])) {
  674. return fd->lang_support_overrides[tags[0]];
  675. }
  676. }
  677. return fd->is_lang_supported(p_language);
  678. }
  679. }
  680. void TextServerAdvanced::font_set_language_support_override(RID p_font, const String &p_language, bool p_supported) {
  681. _THREAD_SAFE_METHOD_
  682. FontDataAdvanced *fd = font_owner.getornull(p_font);
  683. ERR_FAIL_COND(!fd);
  684. fd->lang_support_overrides[p_language] = p_supported;
  685. }
  686. bool TextServerAdvanced::font_get_language_support_override(RID p_font, const String &p_language) {
  687. _THREAD_SAFE_METHOD_
  688. FontDataAdvanced *fd = font_owner.getornull(p_font);
  689. ERR_FAIL_COND_V(!fd, false);
  690. return fd->lang_support_overrides[p_language];
  691. }
  692. void TextServerAdvanced::font_remove_language_support_override(RID p_font, const String &p_language) {
  693. _THREAD_SAFE_METHOD_
  694. FontDataAdvanced *fd = font_owner.getornull(p_font);
  695. ERR_FAIL_COND(!fd);
  696. fd->lang_support_overrides.erase(p_language);
  697. }
  698. Vector<String> TextServerAdvanced::font_get_language_support_overrides(RID p_font) {
  699. _THREAD_SAFE_METHOD_
  700. FontDataAdvanced *fd = font_owner.getornull(p_font);
  701. ERR_FAIL_COND_V(!fd, Vector<String>());
  702. Vector<String> ret;
  703. for (Map<String, bool>::Element *E = fd->lang_support_overrides.front(); E; E = E->next()) {
  704. ret.push_back(E->key());
  705. }
  706. return ret;
  707. }
  708. bool TextServerAdvanced::font_is_script_supported(RID p_font, const String &p_script) const {
  709. _THREAD_SAFE_METHOD_
  710. const FontDataAdvanced *fd = font_owner.getornull(p_font);
  711. ERR_FAIL_COND_V(!fd, false);
  712. if (fd->script_support_overrides.has(p_script)) {
  713. return fd->script_support_overrides[p_script];
  714. } else {
  715. hb_script_t scr = hb_script_from_string(p_script.ascii().get_data(), -1);
  716. return fd->is_script_supported(scr);
  717. }
  718. }
  719. void TextServerAdvanced::font_set_script_support_override(RID p_font, const String &p_script, bool p_supported) {
  720. _THREAD_SAFE_METHOD_
  721. FontDataAdvanced *fd = font_owner.getornull(p_font);
  722. ERR_FAIL_COND(!fd);
  723. fd->script_support_overrides[p_script] = p_supported;
  724. }
  725. bool TextServerAdvanced::font_get_script_support_override(RID p_font, const String &p_script) {
  726. _THREAD_SAFE_METHOD_
  727. FontDataAdvanced *fd = font_owner.getornull(p_font);
  728. ERR_FAIL_COND_V(!fd, false);
  729. return fd->script_support_overrides[p_script];
  730. }
  731. void TextServerAdvanced::font_remove_script_support_override(RID p_font, const String &p_script) {
  732. _THREAD_SAFE_METHOD_
  733. FontDataAdvanced *fd = font_owner.getornull(p_font);
  734. ERR_FAIL_COND(!fd);
  735. fd->script_support_overrides.erase(p_script);
  736. }
  737. Vector<String> TextServerAdvanced::font_get_script_support_overrides(RID p_font) {
  738. _THREAD_SAFE_METHOD_
  739. FontDataAdvanced *fd = font_owner.getornull(p_font);
  740. ERR_FAIL_COND_V(!fd, Vector<String>());
  741. Vector<String> ret;
  742. for (Map<String, bool>::Element *E = fd->script_support_overrides.front(); E; E = E->next()) {
  743. ret.push_back(E->key());
  744. }
  745. return ret;
  746. }
  747. uint32_t TextServerAdvanced::font_get_glyph_index(RID p_font, char32_t p_char, char32_t p_variation_selector) const {
  748. _THREAD_SAFE_METHOD_
  749. const FontDataAdvanced *fd = font_owner.getornull(p_font);
  750. ERR_FAIL_COND_V(!fd, 0);
  751. return fd->get_glyph_index(p_char, p_variation_selector);
  752. }
  753. Vector2 TextServerAdvanced::font_get_glyph_advance(RID p_font, uint32_t p_index, int p_size) const {
  754. _THREAD_SAFE_METHOD_
  755. const FontDataAdvanced *fd = font_owner.getornull(p_font);
  756. ERR_FAIL_COND_V(!fd, Vector2());
  757. return fd->get_advance(p_index, p_size);
  758. }
  759. Vector2 TextServerAdvanced::font_get_glyph_kerning(RID p_font, uint32_t p_index_a, uint32_t p_index_b, int p_size) const {
  760. _THREAD_SAFE_METHOD_
  761. const FontDataAdvanced *fd = font_owner.getornull(p_font);
  762. ERR_FAIL_COND_V(!fd, Vector2());
  763. return fd->get_kerning(p_index_a, p_index_b, p_size);
  764. }
  765. Vector2 TextServerAdvanced::font_draw_glyph(RID p_font, RID p_canvas, int p_size, const Vector2 &p_pos, uint32_t p_index, const Color &p_color) const {
  766. _THREAD_SAFE_METHOD_
  767. const FontDataAdvanced *fd = font_owner.getornull(p_font);
  768. ERR_FAIL_COND_V(!fd, Vector2());
  769. return fd->draw_glyph(p_canvas, p_size, p_pos, p_index, p_color);
  770. }
  771. Vector2 TextServerAdvanced::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) const {
  772. _THREAD_SAFE_METHOD_
  773. const FontDataAdvanced *fd = font_owner.getornull(p_font);
  774. ERR_FAIL_COND_V(!fd, Vector2());
  775. return fd->draw_glyph_outline(p_canvas, p_size, p_outline_size, p_pos, p_index, p_color);
  776. }
  777. float TextServerAdvanced::font_get_oversampling() const {
  778. return oversampling;
  779. }
  780. void TextServerAdvanced::font_set_oversampling(float p_oversampling) {
  781. _THREAD_SAFE_METHOD_
  782. if (oversampling != p_oversampling) {
  783. oversampling = p_oversampling;
  784. List<RID> fonts;
  785. font_owner.get_owned_list(&fonts);
  786. for (List<RID>::Element *E = fonts.front(); E; E = E->next()) {
  787. font_owner.getornull(E->get())->clear_cache();
  788. }
  789. List<RID> text_bufs;
  790. shaped_owner.get_owned_list(&text_bufs);
  791. for (List<RID>::Element *E = text_bufs.front(); E; E = E->next()) {
  792. invalidate(shaped_owner.getornull(E->get()));
  793. }
  794. }
  795. }
  796. Vector<String> TextServerAdvanced::get_system_fonts() const {
  797. return Vector<String>();
  798. }
  799. /*************************************************************************/
  800. /* Shaped text buffer interface */
  801. /*************************************************************************/
  802. int TextServerAdvanced::_convert_pos(const ShapedTextDataAdvanced *p_sd, int p_pos) const {
  803. int32_t limit = p_pos;
  804. if (p_sd->text.length() != p_sd->utf16.length()) {
  805. const UChar *data = p_sd->utf16.ptr();
  806. for (int i = 0; i < p_pos; i++) {
  807. if (U16_IS_LEAD(data[i])) {
  808. limit--;
  809. }
  810. }
  811. }
  812. return limit;
  813. }
  814. int TextServerAdvanced::_convert_pos_inv(const ShapedTextDataAdvanced *p_sd, int p_pos) const {
  815. int32_t limit = p_pos;
  816. if (p_sd->text.length() != p_sd->utf16.length()) {
  817. for (int i = 0; i < p_pos; i++) {
  818. if (p_sd->text[i] > 0xFFFF) {
  819. limit++;
  820. }
  821. }
  822. }
  823. return limit;
  824. }
  825. void TextServerAdvanced::invalidate(TextServerAdvanced::ShapedTextDataAdvanced *p_shaped) {
  826. p_shaped->valid = false;
  827. p_shaped->sort_valid = false;
  828. p_shaped->line_breaks_valid = false;
  829. p_shaped->justification_ops_valid = false;
  830. p_shaped->ascent = 0.f;
  831. p_shaped->descent = 0.f;
  832. p_shaped->width = 0.f;
  833. p_shaped->upos = 0.f;
  834. p_shaped->uthk = 0.f;
  835. p_shaped->glyphs.clear();
  836. p_shaped->glyphs_logical.clear();
  837. p_shaped->utf16 = Char16String();
  838. if (p_shaped->script_iter != nullptr) {
  839. memdelete(p_shaped->script_iter);
  840. p_shaped->script_iter = nullptr;
  841. }
  842. for (int i = 0; i < p_shaped->bidi_iter.size(); i++) {
  843. ubidi_close(p_shaped->bidi_iter[i]);
  844. }
  845. p_shaped->bidi_iter.clear();
  846. }
  847. void TextServerAdvanced::full_copy(ShapedTextDataAdvanced *p_shaped) {
  848. ShapedTextDataAdvanced *parent = shaped_owner.getornull(p_shaped->parent);
  849. for (Map<Variant, ShapedTextData::EmbeddedObject>::Element *E = parent->objects.front(); E; E = E->next()) {
  850. if (E->get().pos >= p_shaped->start && E->get().pos < p_shaped->end) {
  851. p_shaped->objects[E->key()] = E->get();
  852. }
  853. }
  854. for (int k = 0; k < parent->spans.size(); k++) {
  855. ShapedTextDataAdvanced::Span span = parent->spans[k];
  856. if (span.start >= p_shaped->end || span.end <= p_shaped->start) {
  857. continue;
  858. }
  859. span.start = MAX(p_shaped->start, span.start);
  860. span.end = MIN(p_shaped->end, span.end);
  861. p_shaped->spans.push_back(span);
  862. }
  863. p_shaped->parent = RID();
  864. }
  865. RID TextServerAdvanced::create_shaped_text(TextServer::Direction p_direction, TextServer::Orientation p_orientation) {
  866. _THREAD_SAFE_METHOD_
  867. ShapedTextDataAdvanced *sd = memnew(ShapedTextDataAdvanced);
  868. sd->hb_buffer = hb_buffer_create();
  869. sd->direction = p_direction;
  870. sd->orientation = p_orientation;
  871. return shaped_owner.make_rid(sd);
  872. }
  873. void TextServerAdvanced::shaped_text_clear(RID p_shaped) {
  874. _THREAD_SAFE_METHOD_
  875. ShapedTextDataAdvanced *sd = shaped_owner.getornull(p_shaped);
  876. ERR_FAIL_COND(!sd);
  877. sd->parent = RID();
  878. sd->start = 0;
  879. sd->end = 0;
  880. sd->text = String();
  881. sd->spans.clear();
  882. sd->objects.clear();
  883. sd->bidi_override.clear();
  884. invalidate(sd);
  885. }
  886. void TextServerAdvanced::shaped_text_set_direction(RID p_shaped, TextServer::Direction p_direction) {
  887. _THREAD_SAFE_METHOD_
  888. ShapedTextDataAdvanced *sd = shaped_owner.getornull(p_shaped);
  889. ERR_FAIL_COND(!sd);
  890. if (sd->direction != p_direction) {
  891. if (sd->parent != RID()) {
  892. full_copy(sd);
  893. }
  894. sd->direction = p_direction;
  895. invalidate(sd);
  896. }
  897. }
  898. TextServer::Direction TextServerAdvanced::shaped_text_get_direction(RID p_shaped) const {
  899. _THREAD_SAFE_METHOD_
  900. const ShapedTextDataAdvanced *sd = shaped_owner.getornull(p_shaped);
  901. ERR_FAIL_COND_V(!sd, TextServer::DIRECTION_LTR);
  902. return sd->direction;
  903. }
  904. void TextServerAdvanced::shaped_text_set_bidi_override(RID p_shaped, const Vector<Vector2i> &p_override) {
  905. _THREAD_SAFE_METHOD_
  906. ShapedTextDataAdvanced *sd = shaped_owner.getornull(p_shaped);
  907. ERR_FAIL_COND(!sd);
  908. if (sd->parent != RID()) {
  909. full_copy(sd);
  910. }
  911. sd->bidi_override = p_override;
  912. invalidate(sd);
  913. }
  914. void TextServerAdvanced::shaped_text_set_orientation(RID p_shaped, TextServer::Orientation p_orientation) {
  915. _THREAD_SAFE_METHOD_
  916. ShapedTextDataAdvanced *sd = shaped_owner.getornull(p_shaped);
  917. ERR_FAIL_COND(!sd);
  918. if (sd->orientation != p_orientation) {
  919. if (sd->parent != RID()) {
  920. full_copy(sd);
  921. }
  922. sd->orientation = p_orientation;
  923. invalidate(sd);
  924. }
  925. }
  926. void TextServerAdvanced::shaped_text_set_preserve_invalid(RID p_shaped, bool p_enabled) {
  927. _THREAD_SAFE_METHOD_
  928. ShapedTextDataAdvanced *sd = shaped_owner.getornull(p_shaped);
  929. ERR_FAIL_COND(!sd);
  930. ERR_FAIL_COND(sd->parent != RID());
  931. if (sd->preserve_invalid != p_enabled) {
  932. sd->preserve_invalid = p_enabled;
  933. invalidate(sd);
  934. }
  935. }
  936. bool TextServerAdvanced::shaped_text_get_preserve_invalid(RID p_shaped) const {
  937. _THREAD_SAFE_METHOD_
  938. const ShapedTextDataAdvanced *sd = shaped_owner.getornull(p_shaped);
  939. ERR_FAIL_COND_V(!sd, false);
  940. return sd->preserve_invalid;
  941. }
  942. void TextServerAdvanced::shaped_text_set_preserve_control(RID p_shaped, bool p_enabled) {
  943. _THREAD_SAFE_METHOD_
  944. ShapedTextDataAdvanced *sd = shaped_owner.getornull(p_shaped);
  945. ERR_FAIL_COND(!sd);
  946. if (sd->preserve_control != p_enabled) {
  947. if (sd->parent != RID()) {
  948. full_copy(sd);
  949. }
  950. sd->preserve_control = p_enabled;
  951. invalidate(sd);
  952. }
  953. }
  954. bool TextServerAdvanced::shaped_text_get_preserve_control(RID p_shaped) const {
  955. _THREAD_SAFE_METHOD_
  956. const ShapedTextDataAdvanced *sd = shaped_owner.getornull(p_shaped);
  957. ERR_FAIL_COND_V(!sd, false);
  958. return sd->preserve_control;
  959. }
  960. TextServer::Orientation TextServerAdvanced::shaped_text_get_orientation(RID p_shaped) const {
  961. _THREAD_SAFE_METHOD_
  962. const ShapedTextDataAdvanced *sd = shaped_owner.getornull(p_shaped);
  963. ERR_FAIL_COND_V(!sd, TextServer::ORIENTATION_HORIZONTAL);
  964. return sd->orientation;
  965. }
  966. bool TextServerAdvanced::shaped_text_add_string(RID p_shaped, const String &p_text, const Vector<RID> &p_fonts, int p_size, const Dictionary &p_opentype_features, const String &p_language) {
  967. _THREAD_SAFE_METHOD_
  968. ShapedTextDataAdvanced *sd = shaped_owner.getornull(p_shaped);
  969. ERR_FAIL_COND_V(!sd, false);
  970. ERR_FAIL_COND_V(p_size <= 0, false);
  971. if (p_text.is_empty()) {
  972. return true;
  973. }
  974. if (sd->parent != RID()) {
  975. full_copy(sd);
  976. }
  977. ShapedTextDataAdvanced::Span span;
  978. span.start = sd->text.length();
  979. span.end = span.start + p_text.length();
  980. span.fonts = p_fonts; // Do not pre-sort, spans will be divided to subruns later.
  981. span.font_size = p_size;
  982. span.language = p_language;
  983. span.features = p_opentype_features;
  984. sd->spans.push_back(span);
  985. sd->text += p_text;
  986. sd->end += p_text.length();
  987. invalidate(sd);
  988. return true;
  989. }
  990. bool TextServerAdvanced::shaped_text_add_object(RID p_shaped, Variant p_key, const Size2 &p_size, VAlign p_inline_align, int p_length) {
  991. _THREAD_SAFE_METHOD_
  992. ShapedTextDataAdvanced *sd = shaped_owner.getornull(p_shaped);
  993. ERR_FAIL_COND_V(!sd, false);
  994. ERR_FAIL_COND_V(p_key == Variant(), false);
  995. ERR_FAIL_COND_V(sd->objects.has(p_key), false);
  996. if (sd->parent != RID()) {
  997. full_copy(sd);
  998. }
  999. ShapedTextDataAdvanced::Span span;
  1000. span.start = sd->text.length();
  1001. span.end = span.start + p_length;
  1002. span.embedded_key = p_key;
  1003. ShapedTextDataAdvanced::EmbeddedObject obj;
  1004. obj.inline_align = p_inline_align;
  1005. obj.rect.size = p_size;
  1006. obj.pos = span.start;
  1007. sd->spans.push_back(span);
  1008. sd->text += String::chr(0xfffc).repeat(p_length);
  1009. sd->end += p_length;
  1010. sd->objects[p_key] = obj;
  1011. invalidate(sd);
  1012. return true;
  1013. }
  1014. bool TextServerAdvanced::shaped_text_resize_object(RID p_shaped, Variant p_key, const Size2 &p_size, VAlign p_inline_align) {
  1015. _THREAD_SAFE_METHOD_
  1016. ShapedTextData *sd = shaped_owner.getornull(p_shaped);
  1017. ERR_FAIL_COND_V(!sd, false);
  1018. ERR_FAIL_COND_V(!sd->objects.has(p_key), false);
  1019. sd->objects[p_key].rect.size = p_size;
  1020. sd->objects[p_key].inline_align = p_inline_align;
  1021. if (sd->valid) {
  1022. // Recalc string metrics.
  1023. sd->ascent = 0;
  1024. sd->descent = 0;
  1025. sd->width = 0;
  1026. sd->upos = 0;
  1027. sd->uthk = 0;
  1028. int sd_size = sd->glyphs.size();
  1029. for (int i = 0; i < sd_size; i++) {
  1030. Glyph gl = sd->glyphs[i];
  1031. Variant key;
  1032. if (gl.count == 1) {
  1033. for (Map<Variant, ShapedTextData::EmbeddedObject>::Element *E = sd->objects.front(); E; E = E->next()) {
  1034. if (E->get().pos == gl.start) {
  1035. key = E->key();
  1036. break;
  1037. }
  1038. }
  1039. }
  1040. if (key != Variant()) {
  1041. if (sd->orientation == ORIENTATION_HORIZONTAL) {
  1042. sd->objects[key].rect.position.x = sd->width;
  1043. sd->width += sd->objects[key].rect.size.x;
  1044. switch (sd->objects[key].inline_align) {
  1045. case VALIGN_TOP: {
  1046. sd->ascent = MAX(sd->ascent, sd->objects[key].rect.size.y);
  1047. } break;
  1048. case VALIGN_CENTER: {
  1049. sd->ascent = MAX(sd->ascent, Math::round(sd->objects[key].rect.size.y / 2));
  1050. sd->descent = MAX(sd->descent, Math::round(sd->objects[key].rect.size.y / 2));
  1051. } break;
  1052. case VALIGN_BOTTOM: {
  1053. sd->descent = MAX(sd->descent, sd->objects[key].rect.size.y);
  1054. } break;
  1055. }
  1056. sd->glyphs.write[i].advance = sd->objects[key].rect.size.x;
  1057. } else {
  1058. sd->objects[key].rect.position.y = sd->width;
  1059. sd->width += sd->objects[key].rect.size.y;
  1060. switch (sd->objects[key].inline_align) {
  1061. case VALIGN_TOP: {
  1062. sd->ascent = MAX(sd->ascent, sd->objects[key].rect.size.x);
  1063. } break;
  1064. case VALIGN_CENTER: {
  1065. sd->ascent = MAX(sd->ascent, Math::round(sd->objects[key].rect.size.x / 2));
  1066. sd->descent = MAX(sd->descent, Math::round(sd->objects[key].rect.size.x / 2));
  1067. } break;
  1068. case VALIGN_BOTTOM: {
  1069. sd->descent = MAX(sd->descent, sd->objects[key].rect.size.x);
  1070. } break;
  1071. }
  1072. sd->glyphs.write[i].advance = sd->objects[key].rect.size.y;
  1073. }
  1074. } else {
  1075. const FontDataAdvanced *fd = font_owner.getornull(gl.font_rid);
  1076. if (fd != nullptr) {
  1077. if (sd->orientation == ORIENTATION_HORIZONTAL) {
  1078. sd->ascent = MAX(sd->ascent, MAX(fd->get_ascent(gl.font_size), -gl.y_off));
  1079. sd->descent = MAX(sd->descent, MAX(fd->get_descent(gl.font_size), gl.y_off));
  1080. } else {
  1081. sd->ascent = MAX(sd->ascent, Math::round(fd->get_advance(gl.index, gl.font_size).x * 0.5));
  1082. sd->descent = MAX(sd->descent, Math::round(fd->get_advance(gl.index, gl.font_size).x * 0.5));
  1083. }
  1084. sd->upos = MAX(sd->upos, font_get_underline_position(gl.font_rid, gl.font_size));
  1085. sd->uthk = MAX(sd->uthk, font_get_underline_thickness(gl.font_rid, gl.font_size));
  1086. } else if (sd->preserve_invalid || (sd->preserve_control && is_control(gl.index))) {
  1087. // Glyph not found, replace with hex code box.
  1088. if (sd->orientation == ORIENTATION_HORIZONTAL) {
  1089. sd->ascent = MAX(sd->ascent, Math::round(get_hex_code_box_size(gl.font_size, gl.index).y * 0.75f));
  1090. sd->descent = MAX(sd->descent, Math::round(get_hex_code_box_size(gl.font_size, gl.index).y * 0.25f));
  1091. } else {
  1092. sd->ascent = MAX(sd->ascent, Math::round(get_hex_code_box_size(gl.font_size, gl.index).x * 0.5f));
  1093. sd->descent = MAX(sd->descent, Math::round(get_hex_code_box_size(gl.font_size, gl.index).x * 0.5f));
  1094. }
  1095. }
  1096. sd->width += gl.advance * gl.repeat;
  1097. }
  1098. }
  1099. // Align embedded objects to baseline.
  1100. for (Map<Variant, ShapedTextData::EmbeddedObject>::Element *E = sd->objects.front(); E; E = E->next()) {
  1101. if ((E->get().pos >= sd->start) && (E->get().pos < sd->end)) {
  1102. if (sd->orientation == ORIENTATION_HORIZONTAL) {
  1103. switch (E->get().inline_align) {
  1104. case VALIGN_TOP: {
  1105. E->get().rect.position.y = -sd->ascent;
  1106. } break;
  1107. case VALIGN_CENTER: {
  1108. E->get().rect.position.y = -(E->get().rect.size.y / 2);
  1109. } break;
  1110. case VALIGN_BOTTOM: {
  1111. E->get().rect.position.y = sd->descent - E->get().rect.size.y;
  1112. } break;
  1113. }
  1114. } else {
  1115. switch (E->get().inline_align) {
  1116. case VALIGN_TOP: {
  1117. E->get().rect.position.x = -sd->ascent;
  1118. } break;
  1119. case VALIGN_CENTER: {
  1120. E->get().rect.position.x = -(E->get().rect.size.x / 2);
  1121. } break;
  1122. case VALIGN_BOTTOM: {
  1123. E->get().rect.position.x = sd->descent - E->get().rect.size.x;
  1124. } break;
  1125. }
  1126. }
  1127. }
  1128. }
  1129. }
  1130. return true;
  1131. }
  1132. RID TextServerAdvanced::shaped_text_substr(RID p_shaped, int p_start, int p_length) const {
  1133. _THREAD_SAFE_METHOD_
  1134. const ShapedTextDataAdvanced *sd = shaped_owner.getornull(p_shaped);
  1135. ERR_FAIL_COND_V(!sd, RID());
  1136. if (sd->parent != RID()) {
  1137. return shaped_text_substr(sd->parent, p_start, p_length);
  1138. }
  1139. if (!sd->valid) {
  1140. const_cast<TextServerAdvanced *>(this)->shaped_text_shape(p_shaped);
  1141. }
  1142. ERR_FAIL_COND_V(p_start < 0 || p_length < 0, RID());
  1143. ERR_FAIL_COND_V(sd->start > p_start || sd->end < p_start, RID());
  1144. ERR_FAIL_COND_V(sd->end < p_start + p_length, RID());
  1145. ShapedTextDataAdvanced *new_sd = memnew(ShapedTextDataAdvanced);
  1146. new_sd->hb_buffer = hb_buffer_create();
  1147. new_sd->parent = p_shaped;
  1148. new_sd->start = p_start;
  1149. new_sd->end = p_start + p_length;
  1150. new_sd->orientation = sd->orientation;
  1151. new_sd->direction = sd->direction;
  1152. new_sd->para_direction = sd->para_direction;
  1153. new_sd->line_breaks_valid = sd->line_breaks_valid;
  1154. new_sd->justification_ops_valid = sd->justification_ops_valid;
  1155. new_sd->sort_valid = false;
  1156. new_sd->upos = sd->upos;
  1157. new_sd->uthk = sd->uthk;
  1158. if (p_length > 0) {
  1159. new_sd->text = sd->text.substr(p_start, p_length);
  1160. new_sd->utf16 = new_sd->text.utf16();
  1161. new_sd->script_iter = memnew(ScriptIterator(new_sd->text, 0, new_sd->text.length()));
  1162. int sd_size = sd->glyphs.size();
  1163. const Glyph *sd_glyphs = sd->glyphs.ptr();
  1164. const FontDataAdvanced *fd = nullptr;
  1165. RID prev_rid = RID();
  1166. for (int ov = 0; ov < sd->bidi_override.size(); ov++) {
  1167. UErrorCode err = U_ZERO_ERROR;
  1168. if (sd->bidi_override[ov].x >= p_start + p_length || sd->bidi_override[ov].y <= p_start) {
  1169. continue;
  1170. }
  1171. int start = _convert_pos_inv(sd, MAX(0, p_start - sd->bidi_override[ov].x));
  1172. int end = _convert_pos_inv(sd, MIN(p_start + p_length, sd->bidi_override[ov].y) - sd->bidi_override[ov].x);
  1173. ERR_FAIL_COND_V_MSG((start < 0 || end - start > new_sd->utf16.length()), RID(), "Invalid BiDi override range.");
  1174. //Create temporary line bidi & shape
  1175. UBiDi *bidi_iter = ubidi_openSized(end - start, 0, &err);
  1176. ERR_FAIL_COND_V_MSG(U_FAILURE(err), RID(), u_errorName(err));
  1177. ubidi_setLine(sd->bidi_iter[ov], start, end, bidi_iter, &err);
  1178. if (U_FAILURE(err)) {
  1179. ubidi_close(bidi_iter);
  1180. ERR_FAIL_V_MSG(RID(), u_errorName(err));
  1181. }
  1182. new_sd->bidi_iter.push_back(bidi_iter);
  1183. err = U_ZERO_ERROR;
  1184. int bidi_run_count = ubidi_countRuns(bidi_iter, &err);
  1185. ERR_FAIL_COND_V_MSG(U_FAILURE(err), RID(), u_errorName(err));
  1186. for (int i = 0; i < bidi_run_count; i++) {
  1187. int32_t _bidi_run_start = 0;
  1188. int32_t _bidi_run_length = 0;
  1189. ubidi_getVisualRun(bidi_iter, i, &_bidi_run_start, &_bidi_run_length);
  1190. int32_t bidi_run_start = _convert_pos(sd, sd->bidi_override[ov].x + start + _bidi_run_start);
  1191. int32_t bidi_run_end = _convert_pos(sd, sd->bidi_override[ov].x + start + _bidi_run_start + _bidi_run_length);
  1192. for (int j = 0; j < sd_size; j++) {
  1193. if ((sd_glyphs[j].start >= bidi_run_start) && (sd_glyphs[j].end <= bidi_run_end)) {
  1194. // Copy glyphs.
  1195. Glyph gl = sd_glyphs[j];
  1196. Variant key;
  1197. bool find_embedded = false;
  1198. if (gl.count == 1) {
  1199. for (Map<Variant, ShapedTextData::EmbeddedObject>::Element *E = sd->objects.front(); E; E = E->next()) {
  1200. if (E->get().pos == gl.start) {
  1201. find_embedded = true;
  1202. key = E->key();
  1203. new_sd->objects[key] = E->get();
  1204. break;
  1205. }
  1206. }
  1207. }
  1208. if (find_embedded) {
  1209. if (new_sd->orientation == ORIENTATION_HORIZONTAL) {
  1210. new_sd->objects[key].rect.position.x = new_sd->width;
  1211. new_sd->width += new_sd->objects[key].rect.size.x;
  1212. switch (new_sd->objects[key].inline_align) {
  1213. case VALIGN_TOP: {
  1214. new_sd->ascent = MAX(new_sd->ascent, new_sd->objects[key].rect.size.y);
  1215. } break;
  1216. case VALIGN_CENTER: {
  1217. new_sd->ascent = MAX(new_sd->ascent, Math::round(new_sd->objects[key].rect.size.y / 2));
  1218. new_sd->descent = MAX(new_sd->descent, Math::round(new_sd->objects[key].rect.size.y / 2));
  1219. } break;
  1220. case VALIGN_BOTTOM: {
  1221. new_sd->descent = MAX(new_sd->descent, new_sd->objects[key].rect.size.y);
  1222. } break;
  1223. }
  1224. } else {
  1225. new_sd->objects[key].rect.position.y = new_sd->width;
  1226. new_sd->width += new_sd->objects[key].rect.size.y;
  1227. switch (new_sd->objects[key].inline_align) {
  1228. case VALIGN_TOP: {
  1229. new_sd->ascent = MAX(new_sd->ascent, new_sd->objects[key].rect.size.x);
  1230. } break;
  1231. case VALIGN_CENTER: {
  1232. new_sd->ascent = MAX(new_sd->ascent, Math::round(new_sd->objects[key].rect.size.x / 2));
  1233. new_sd->descent = MAX(new_sd->descent, Math::round(new_sd->objects[key].rect.size.x / 2));
  1234. } break;
  1235. case VALIGN_BOTTOM: {
  1236. new_sd->descent = MAX(new_sd->descent, new_sd->objects[key].rect.size.x);
  1237. } break;
  1238. }
  1239. }
  1240. } else {
  1241. if (prev_rid != gl.font_rid) {
  1242. fd = font_owner.getornull(gl.font_rid);
  1243. prev_rid = gl.font_rid;
  1244. }
  1245. if (fd != nullptr) {
  1246. if (new_sd->orientation == ORIENTATION_HORIZONTAL) {
  1247. new_sd->ascent = MAX(new_sd->ascent, MAX(fd->get_ascent(gl.font_size), -gl.y_off));
  1248. new_sd->descent = MAX(new_sd->descent, MAX(fd->get_descent(gl.font_size), gl.y_off));
  1249. } else {
  1250. new_sd->ascent = MAX(new_sd->ascent, Math::round(fd->get_advance(gl.index, gl.font_size).x * 0.5));
  1251. new_sd->descent = MAX(new_sd->descent, Math::round(fd->get_advance(gl.index, gl.font_size).x * 0.5));
  1252. }
  1253. } else if (new_sd->preserve_invalid || (new_sd->preserve_control && is_control(gl.index))) {
  1254. // Glyph not found, replace with hex code box.
  1255. if (new_sd->orientation == ORIENTATION_HORIZONTAL) {
  1256. new_sd->ascent = MAX(new_sd->ascent, Math::round(get_hex_code_box_size(gl.font_size, gl.index).y * 0.75f));
  1257. new_sd->descent = MAX(new_sd->descent, Math::round(get_hex_code_box_size(gl.font_size, gl.index).y * 0.25f));
  1258. } else {
  1259. new_sd->ascent = MAX(new_sd->ascent, Math::round(get_hex_code_box_size(gl.font_size, gl.index).x * 0.5f));
  1260. new_sd->descent = MAX(new_sd->descent, Math::round(get_hex_code_box_size(gl.font_size, gl.index).x * 0.5f));
  1261. }
  1262. }
  1263. new_sd->width += gl.advance * gl.repeat;
  1264. }
  1265. new_sd->glyphs.push_back(gl);
  1266. }
  1267. }
  1268. }
  1269. }
  1270. // Align embedded objects to baseline.
  1271. for (Map<Variant, ShapedTextData::EmbeddedObject>::Element *E = new_sd->objects.front(); E; E = E->next()) {
  1272. if ((E->get().pos >= new_sd->start) && (E->get().pos < new_sd->end)) {
  1273. if (sd->orientation == ORIENTATION_HORIZONTAL) {
  1274. switch (E->get().inline_align) {
  1275. case VALIGN_TOP: {
  1276. E->get().rect.position.y = -new_sd->ascent;
  1277. } break;
  1278. case VALIGN_CENTER: {
  1279. E->get().rect.position.y = -(E->get().rect.size.y / 2);
  1280. } break;
  1281. case VALIGN_BOTTOM: {
  1282. E->get().rect.position.y = new_sd->descent - E->get().rect.size.y;
  1283. } break;
  1284. }
  1285. } else {
  1286. switch (E->get().inline_align) {
  1287. case VALIGN_TOP: {
  1288. E->get().rect.position.x = -new_sd->ascent;
  1289. } break;
  1290. case VALIGN_CENTER: {
  1291. E->get().rect.position.x = -(E->get().rect.size.x / 2);
  1292. } break;
  1293. case VALIGN_BOTTOM: {
  1294. E->get().rect.position.x = new_sd->descent - E->get().rect.size.x;
  1295. } break;
  1296. }
  1297. }
  1298. }
  1299. }
  1300. }
  1301. new_sd->valid = true;
  1302. return shaped_owner.make_rid(new_sd);
  1303. }
  1304. RID TextServerAdvanced::shaped_text_get_parent(RID p_shaped) const {
  1305. _THREAD_SAFE_METHOD_
  1306. ShapedTextDataAdvanced *sd = shaped_owner.getornull(p_shaped);
  1307. ERR_FAIL_COND_V(!sd, RID());
  1308. return sd->parent;
  1309. }
  1310. float TextServerAdvanced::shaped_text_fit_to_width(RID p_shaped, float p_width, uint8_t /*JustificationFlag*/ p_jst_flags) {
  1311. _THREAD_SAFE_METHOD_
  1312. ShapedTextDataAdvanced *sd = shaped_owner.getornull(p_shaped);
  1313. ERR_FAIL_COND_V(!sd, 0.f);
  1314. if (!sd->valid) {
  1315. const_cast<TextServerAdvanced *>(this)->shaped_text_shape(p_shaped);
  1316. }
  1317. if (!sd->justification_ops_valid) {
  1318. const_cast<TextServerAdvanced *>(this)->shaped_text_update_justification_ops(p_shaped);
  1319. }
  1320. int start_pos = 0;
  1321. int end_pos = sd->glyphs.size() - 1;
  1322. if ((p_jst_flags & JUSTIFICATION_AFTER_LAST_TAB) == JUSTIFICATION_AFTER_LAST_TAB) {
  1323. int start, end, delta;
  1324. if (sd->para_direction == DIRECTION_LTR) {
  1325. start = sd->glyphs.size() - 1;
  1326. end = -1;
  1327. delta = -1;
  1328. } else {
  1329. start = 0;
  1330. end = sd->glyphs.size();
  1331. delta = +1;
  1332. }
  1333. for (int i = start; i != end; i += delta) {
  1334. if ((sd->glyphs[i].flags & GRAPHEME_IS_TAB) == GRAPHEME_IS_TAB) {
  1335. if (sd->para_direction == DIRECTION_LTR) {
  1336. start_pos = i;
  1337. break;
  1338. } else {
  1339. end_pos = i;
  1340. break;
  1341. }
  1342. }
  1343. }
  1344. }
  1345. if ((p_jst_flags & JUSTIFICATION_TRIM_EDGE_SPACES) == JUSTIFICATION_TRIM_EDGE_SPACES) {
  1346. while ((start_pos < end_pos) && ((sd->glyphs[start_pos].flags & GRAPHEME_IS_SPACE) == GRAPHEME_IS_SPACE || (sd->glyphs[start_pos].flags & GRAPHEME_IS_BREAK_HARD) == GRAPHEME_IS_BREAK_HARD || (sd->glyphs[start_pos].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT)) {
  1347. sd->width -= sd->glyphs[start_pos].advance * sd->glyphs[start_pos].repeat;
  1348. sd->glyphs.write[start_pos].advance = 0;
  1349. start_pos += sd->glyphs[start_pos].count;
  1350. }
  1351. while ((start_pos < end_pos) && ((sd->glyphs[end_pos].flags & GRAPHEME_IS_SPACE) == GRAPHEME_IS_SPACE || (sd->glyphs[end_pos].flags & GRAPHEME_IS_BREAK_HARD) == GRAPHEME_IS_BREAK_HARD || (sd->glyphs[end_pos].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT)) {
  1352. sd->width -= sd->glyphs[end_pos].advance * sd->glyphs[end_pos].repeat;
  1353. sd->glyphs.write[end_pos].advance = 0;
  1354. end_pos -= sd->glyphs[end_pos].count;
  1355. }
  1356. }
  1357. int space_count = 0;
  1358. int elongation_count = 0;
  1359. for (int i = start_pos; i <= end_pos; i++) {
  1360. const Glyph &gl = sd->glyphs[i];
  1361. if (gl.count > 0) {
  1362. if ((gl.flags & GRAPHEME_IS_ELONGATION) == GRAPHEME_IS_ELONGATION) {
  1363. elongation_count++;
  1364. }
  1365. if ((gl.flags & GRAPHEME_IS_SPACE) == GRAPHEME_IS_SPACE) {
  1366. space_count++;
  1367. }
  1368. }
  1369. }
  1370. if ((elongation_count > 0) && ((p_jst_flags & JUSTIFICATION_KASHIDA) == JUSTIFICATION_KASHIDA)) {
  1371. float delta_width_per_kashida = (p_width - sd->width) / elongation_count;
  1372. for (int i = start_pos; i <= end_pos; i++) {
  1373. Glyph &gl = sd->glyphs.write[i];
  1374. if (gl.count > 0) {
  1375. if (((gl.flags & GRAPHEME_IS_ELONGATION) == GRAPHEME_IS_ELONGATION) && (gl.advance > 0)) {
  1376. int count = delta_width_per_kashida / gl.advance;
  1377. int prev_count = gl.repeat;
  1378. if ((gl.flags & GRAPHEME_IS_VIRTUAL) == GRAPHEME_IS_VIRTUAL) {
  1379. gl.repeat = count;
  1380. } else {
  1381. gl.repeat = count + 1;
  1382. }
  1383. sd->width += (gl.repeat - prev_count) * gl.advance;
  1384. }
  1385. }
  1386. }
  1387. }
  1388. if ((space_count > 0) && ((p_jst_flags & JUSTIFICATION_WORD_BOUND) == JUSTIFICATION_WORD_BOUND)) {
  1389. float delta_width_per_space = (p_width - sd->width) / space_count;
  1390. for (int i = start_pos; i <= end_pos; i++) {
  1391. Glyph &gl = sd->glyphs.write[i];
  1392. if (gl.count > 0) {
  1393. if ((gl.flags & GRAPHEME_IS_SPACE) == GRAPHEME_IS_SPACE) {
  1394. float old_adv = gl.advance;
  1395. if ((gl.flags & GRAPHEME_IS_VIRTUAL) == GRAPHEME_IS_VIRTUAL) {
  1396. gl.advance = Math::round(MAX(gl.advance + delta_width_per_space, 0.f));
  1397. } else {
  1398. gl.advance = Math::round(MAX(gl.advance + delta_width_per_space, 0.05 * gl.font_size));
  1399. }
  1400. sd->width += (gl.advance - old_adv);
  1401. }
  1402. }
  1403. }
  1404. }
  1405. return sd->width;
  1406. }
  1407. float TextServerAdvanced::shaped_text_tab_align(RID p_shaped, const Vector<float> &p_tab_stops) {
  1408. _THREAD_SAFE_METHOD_
  1409. ShapedTextDataAdvanced *sd = shaped_owner.getornull(p_shaped);
  1410. ERR_FAIL_COND_V(!sd, 0.f);
  1411. if (!sd->valid) {
  1412. const_cast<TextServerAdvanced *>(this)->shaped_text_shape(p_shaped);
  1413. }
  1414. if (!sd->line_breaks_valid) {
  1415. const_cast<TextServerAdvanced *>(this)->shaped_text_update_breaks(p_shaped);
  1416. }
  1417. int tab_index = 0;
  1418. float off = 0.f;
  1419. int start, end, delta;
  1420. if (sd->para_direction == DIRECTION_LTR) {
  1421. start = 0;
  1422. end = sd->glyphs.size();
  1423. delta = +1;
  1424. } else {
  1425. start = sd->glyphs.size() - 1;
  1426. end = -1;
  1427. delta = -1;
  1428. }
  1429. Glyph *gl = sd->glyphs.ptrw();
  1430. for (int i = start; i != end; i += delta) {
  1431. if ((gl[i].flags & GRAPHEME_IS_TAB) == GRAPHEME_IS_TAB) {
  1432. float tab_off = 0.f;
  1433. while (tab_off <= off) {
  1434. tab_off += p_tab_stops[tab_index];
  1435. tab_index++;
  1436. if (tab_index >= p_tab_stops.size()) {
  1437. tab_index = 0;
  1438. }
  1439. }
  1440. float old_adv = gl[i].advance;
  1441. gl[i].advance = tab_off - off;
  1442. sd->width += gl[i].advance - old_adv;
  1443. off = 0;
  1444. continue;
  1445. }
  1446. off += gl[i].advance * gl[i].repeat;
  1447. }
  1448. return 0.f;
  1449. }
  1450. bool TextServerAdvanced::shaped_text_update_breaks(RID p_shaped) {
  1451. _THREAD_SAFE_METHOD_
  1452. ShapedTextDataAdvanced *sd = shaped_owner.getornull(p_shaped);
  1453. ERR_FAIL_COND_V(!sd, false);
  1454. if (!sd->valid) {
  1455. shaped_text_shape(p_shaped);
  1456. }
  1457. if (sd->line_breaks_valid) {
  1458. return true; // Noting to do.
  1459. }
  1460. const UChar *data = sd->utf16.ptr();
  1461. HashMap<int, bool> breaks;
  1462. UErrorCode err = U_ZERO_ERROR;
  1463. int i = 0;
  1464. while (i < sd->spans.size()) {
  1465. String language = sd->spans[i].language;
  1466. int r_start = sd->spans[i].start;
  1467. while (i + 1 < sd->spans.size() && language == sd->spans[i + 1].language) {
  1468. i++;
  1469. }
  1470. int r_end = sd->spans[i].end;
  1471. UBreakIterator *bi = ubrk_open(UBRK_LINE, language.ascii().get_data(), data + _convert_pos_inv(sd, r_start), _convert_pos_inv(sd, r_end - r_start), &err);
  1472. if (U_FAILURE(err)) {
  1473. //No data loaded - use fallback.
  1474. for (int j = r_start; j < r_end; j++) {
  1475. char32_t c = sd->text[j - sd->start];
  1476. if (is_whitespace(c)) {
  1477. breaks[j] = false;
  1478. }
  1479. if (is_linebreak(c)) {
  1480. breaks[j] = true;
  1481. }
  1482. }
  1483. } else {
  1484. while (ubrk_next(bi) != UBRK_DONE) {
  1485. int pos = _convert_pos(sd, ubrk_current(bi)) + r_start - 1;
  1486. if (pos != r_end) {
  1487. if ((ubrk_getRuleStatus(bi) >= UBRK_LINE_HARD) && (ubrk_getRuleStatus(bi) < UBRK_LINE_HARD_LIMIT)) {
  1488. breaks[pos] = true;
  1489. } else if ((ubrk_getRuleStatus(bi) >= UBRK_LINE_SOFT) && (ubrk_getRuleStatus(bi) < UBRK_LINE_SOFT_LIMIT)) {
  1490. breaks[pos] = false;
  1491. }
  1492. }
  1493. }
  1494. }
  1495. ubrk_close(bi);
  1496. i++;
  1497. }
  1498. sd->sort_valid = false;
  1499. sd->glyphs_logical.clear();
  1500. int sd_size = sd->glyphs.size();
  1501. const char32_t *ch = sd->text.ptr();
  1502. Glyph *sd_glyphs = sd->glyphs.ptrw();
  1503. for (i = 0; i < sd_size; i++) {
  1504. if (sd_glyphs[i].count > 0) {
  1505. char32_t c = ch[sd_glyphs[i].start - sd->start];
  1506. if (c == 0xfffc) {
  1507. continue;
  1508. }
  1509. if (c == 0x0009 || c == 0x000b) {
  1510. sd_glyphs[i].flags |= GRAPHEME_IS_TAB;
  1511. }
  1512. if (is_whitespace(c)) {
  1513. sd_glyphs[i].flags |= GRAPHEME_IS_SPACE;
  1514. }
  1515. if (u_ispunct(c)) {
  1516. sd_glyphs[i].flags |= GRAPHEME_IS_PUNCTUATION;
  1517. }
  1518. if (breaks.has(sd->glyphs[i].start)) {
  1519. if (breaks[sd->glyphs[i].start]) {
  1520. sd_glyphs[i].flags |= GRAPHEME_IS_BREAK_HARD;
  1521. } else {
  1522. if (is_whitespace(c)) {
  1523. sd_glyphs[i].flags |= GRAPHEME_IS_BREAK_SOFT;
  1524. } else {
  1525. TextServer::Glyph gl;
  1526. gl.start = sd_glyphs[i].start;
  1527. gl.end = sd_glyphs[i].end;
  1528. gl.count = 1;
  1529. gl.font_rid = sd_glyphs[i].font_rid;
  1530. gl.font_size = sd_glyphs[i].font_size;
  1531. gl.flags = GRAPHEME_IS_BREAK_SOFT | GRAPHEME_IS_VIRTUAL;
  1532. sd->glyphs.insert(i + sd_glyphs[i].count, gl); // insert after
  1533. // Update write pointer and size.
  1534. sd_size = sd->glyphs.size();
  1535. sd_glyphs = sd->glyphs.ptrw();
  1536. i += sd_glyphs[i].count;
  1537. continue;
  1538. }
  1539. }
  1540. }
  1541. i += (sd_glyphs[i].count - 1);
  1542. }
  1543. }
  1544. sd->line_breaks_valid = true;
  1545. return sd->line_breaks_valid;
  1546. }
  1547. _FORCE_INLINE_ int _generate_kashida_justification_opportunies(const String &p_data, int p_start, int p_end) {
  1548. int kashida_pos = -1;
  1549. int8_t priority = 100;
  1550. int i = p_start;
  1551. char32_t pc = 0;
  1552. while ((p_end > p_start) && is_transparent(p_data[p_end - 1])) {
  1553. p_end--;
  1554. }
  1555. while (i < p_end) {
  1556. uint32_t c = p_data[i];
  1557. if (c == 0x0640) {
  1558. kashida_pos = i;
  1559. priority = 0;
  1560. }
  1561. if (priority >= 1 && i < p_end - 1) {
  1562. if (is_seen_sad(c) && (p_data[i + 1] != 0x200C)) {
  1563. kashida_pos = i;
  1564. priority = 1;
  1565. }
  1566. }
  1567. if (priority >= 2 && i > p_start) {
  1568. if (is_teh_marbuta(c) || is_dal(c) || (is_heh(c) && i == p_end - 1)) {
  1569. if (is_connected_to_prev(c, pc)) {
  1570. kashida_pos = i - 1;
  1571. priority = 2;
  1572. }
  1573. }
  1574. }
  1575. if (priority >= 3 && i > p_start) {
  1576. if (is_alef(c) || ((is_lam(c) || is_tah(c) || is_kaf(c) || is_gaf(c)) && i == p_end - 1)) {
  1577. if (is_connected_to_prev(c, pc)) {
  1578. kashida_pos = i - 1;
  1579. priority = 3;
  1580. }
  1581. }
  1582. }
  1583. if (priority >= 4 && i > p_start && i < p_end - 1) {
  1584. if (is_beh(c)) {
  1585. if (is_reh(p_data[i + 1]) || is_yeh(p_data[i + 1])) {
  1586. if (is_connected_to_prev(c, pc)) {
  1587. kashida_pos = i - 1;
  1588. priority = 4;
  1589. }
  1590. }
  1591. }
  1592. }
  1593. if (priority >= 5 && i > p_start) {
  1594. if (is_waw(c) || ((is_ain(c) || is_qaf(c) || is_feh(c)) && i == p_end - 1)) {
  1595. if (is_connected_to_prev(c, pc)) {
  1596. kashida_pos = i - 1;
  1597. priority = 5;
  1598. }
  1599. }
  1600. }
  1601. if (priority >= 6 && i > p_start) {
  1602. if (is_reh(c)) {
  1603. if (is_connected_to_prev(c, pc)) {
  1604. kashida_pos = i - 1;
  1605. priority = 6;
  1606. }
  1607. }
  1608. }
  1609. if (!is_transparent(c))
  1610. pc = c;
  1611. i++;
  1612. }
  1613. return kashida_pos;
  1614. }
  1615. bool TextServerAdvanced::shaped_text_update_justification_ops(RID p_shaped) {
  1616. _THREAD_SAFE_METHOD_
  1617. ShapedTextDataAdvanced *sd = shaped_owner.getornull(p_shaped);
  1618. ERR_FAIL_COND_V(!sd, false);
  1619. if (!sd->valid) {
  1620. shaped_text_shape(p_shaped);
  1621. }
  1622. if (!sd->line_breaks_valid) {
  1623. shaped_text_update_breaks(p_shaped);
  1624. }
  1625. if (sd->justification_ops_valid) {
  1626. return true; // Noting to do.
  1627. }
  1628. const UChar *data = sd->utf16.ptr();
  1629. int32_t data_size = sd->utf16.length();
  1630. Map<int, bool> jstops;
  1631. // Use ICU word iterator and custom kashida detection.
  1632. UErrorCode err = U_ZERO_ERROR;
  1633. UBreakIterator *bi = ubrk_open(UBRK_WORD, "", data, data_size, &err);
  1634. if (U_FAILURE(err)) {
  1635. // No data - use fallback
  1636. int limit = 0;
  1637. for (int i = 0; i < sd->text.length(); i++) {
  1638. if (is_whitespace(data[i])) {
  1639. int ks = _generate_kashida_justification_opportunies(sd->text, limit, i) + sd->start;
  1640. if (ks != -1) {
  1641. jstops[ks] = true;
  1642. }
  1643. limit = i + 1;
  1644. }
  1645. }
  1646. int ks = _generate_kashida_justification_opportunies(sd->text, limit, sd->text.length()) + sd->start;
  1647. if (ks != -1) {
  1648. jstops[ks] = true;
  1649. }
  1650. } else {
  1651. int limit = 0;
  1652. while (ubrk_next(bi) != UBRK_DONE) {
  1653. if (ubrk_getRuleStatus(bi) != UBRK_WORD_NONE) {
  1654. int i = _convert_pos(sd, ubrk_current(bi));
  1655. jstops[i + sd->start] = false;
  1656. int ks = _generate_kashida_justification_opportunies(sd->text, limit, i);
  1657. if (ks != -1) {
  1658. jstops[ks + sd->start] = true;
  1659. }
  1660. limit = i;
  1661. }
  1662. }
  1663. ubrk_close(bi);
  1664. }
  1665. sd->sort_valid = false;
  1666. sd->glyphs_logical.clear();
  1667. int sd_size = sd->glyphs.size();
  1668. if (jstops.size() > 0) {
  1669. for (int i = 0; i < sd_size; i++) {
  1670. if (sd->glyphs[i].count > 0) {
  1671. if (jstops.has(sd->glyphs[i].start)) {
  1672. char32_t c = sd->text[sd->glyphs[i].start - sd->start];
  1673. if (c == 0xfffc) {
  1674. continue;
  1675. }
  1676. if (jstops[sd->glyphs[i].start]) {
  1677. if (c == 0x0640) {
  1678. sd->glyphs.write[i].flags |= GRAPHEME_IS_ELONGATION;
  1679. } else {
  1680. if (sd->glyphs[i].font_rid != RID()) {
  1681. TextServer::Glyph gl = _shape_single_glyph(sd, 0x0640, HB_SCRIPT_ARABIC, HB_DIRECTION_RTL, sd->glyphs[i].font_rid, sd->glyphs[i].font_size);
  1682. if ((gl.flags & GRAPHEME_IS_VALID) == GRAPHEME_IS_VALID) {
  1683. gl.start = sd->glyphs[i].start;
  1684. gl.end = sd->glyphs[i].end;
  1685. gl.repeat = 0;
  1686. gl.count = 1;
  1687. if (sd->orientation == ORIENTATION_HORIZONTAL) {
  1688. gl.y_off = sd->glyphs[i].y_off;
  1689. } else {
  1690. gl.x_off = sd->glyphs[i].x_off;
  1691. }
  1692. gl.flags |= GRAPHEME_IS_ELONGATION | GRAPHEME_IS_VIRTUAL;
  1693. sd->glyphs.insert(i, gl);
  1694. i++;
  1695. }
  1696. }
  1697. }
  1698. } else if (!is_whitespace(c)) {
  1699. TextServer::Glyph gl;
  1700. gl.start = sd->glyphs[i].start;
  1701. gl.end = sd->glyphs[i].end;
  1702. gl.count = 1;
  1703. gl.font_rid = sd->glyphs[i].font_rid;
  1704. gl.font_size = sd->glyphs[i].font_size;
  1705. gl.flags = GRAPHEME_IS_SPACE | GRAPHEME_IS_VIRTUAL;
  1706. sd->glyphs.insert(i + sd->glyphs[i].count, gl); // insert after
  1707. i += sd->glyphs[i].count;
  1708. continue;
  1709. }
  1710. }
  1711. }
  1712. }
  1713. }
  1714. sd->justification_ops_valid = true;
  1715. return sd->justification_ops_valid;
  1716. }
  1717. TextServer::Glyph TextServerAdvanced::_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) {
  1718. FontDataAdvanced *fd = font_owner.getornull(p_font);
  1719. hb_font_t *hb_font = fd->get_hb_handle(p_font_size);
  1720. hb_buffer_clear_contents(p_sd->hb_buffer);
  1721. hb_buffer_set_direction(p_sd->hb_buffer, p_direction);
  1722. hb_buffer_set_flags(p_sd->hb_buffer, (hb_buffer_flags_t)(HB_BUFFER_FLAG_DEFAULT));
  1723. hb_buffer_set_script(p_sd->hb_buffer, p_script);
  1724. hb_buffer_add_utf32(p_sd->hb_buffer, (const uint32_t *)&p_char, 1, 0, 1);
  1725. hb_shape(hb_font, p_sd->hb_buffer, nullptr, 0);
  1726. unsigned int glyph_count = 0;
  1727. hb_glyph_info_t *glyph_info = hb_buffer_get_glyph_infos(p_sd->hb_buffer, &glyph_count);
  1728. hb_glyph_position_t *glyph_pos = hb_buffer_get_glyph_positions(p_sd->hb_buffer, &glyph_count);
  1729. // Process glyphs.
  1730. TextServer::Glyph gl;
  1731. if (p_direction == HB_DIRECTION_RTL || p_direction == HB_DIRECTION_BTT) {
  1732. gl.flags |= TextServer::GRAPHEME_IS_RTL;
  1733. }
  1734. gl.font_rid = p_font;
  1735. gl.font_size = p_font_size;
  1736. if (glyph_count > 0) {
  1737. if (p_sd->orientation == ORIENTATION_HORIZONTAL) {
  1738. gl.advance = Math::round(glyph_pos[0].x_advance / (64.0 / fd->get_font_scale(p_font_size)));
  1739. } else {
  1740. gl.advance = -Math::round(glyph_pos[0].y_advance / (64.0 / fd->get_font_scale(p_font_size)));
  1741. }
  1742. gl.count = 1;
  1743. gl.index = glyph_info[0].codepoint;
  1744. gl.x_off = Math::round(glyph_pos[0].x_offset / (64.0 / fd->get_font_scale(p_font_size)));
  1745. gl.y_off = -Math::round(glyph_pos[0].y_offset / (64.0 / fd->get_font_scale(p_font_size)));
  1746. if ((glyph_info[0].codepoint != 0) || !u_isgraph(p_char)) {
  1747. gl.flags |= GRAPHEME_IS_VALID;
  1748. }
  1749. }
  1750. return gl;
  1751. }
  1752. void TextServerAdvanced::_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) {
  1753. FontDataAdvanced *fd = nullptr;
  1754. if (p_fb_index < p_fonts.size()) {
  1755. fd = font_owner.getornull(p_fonts[p_fb_index]);
  1756. }
  1757. int fs = p_sd->spans[p_span].font_size;
  1758. if (fd == nullptr) {
  1759. // Add fallback glyphs
  1760. for (int i = p_start; i < p_end; i++) {
  1761. if (p_sd->preserve_invalid || (p_sd->preserve_control && is_control(p_sd->text[i]))) {
  1762. TextServer::Glyph gl;
  1763. gl.start = i;
  1764. gl.end = i + 1;
  1765. gl.count = 1;
  1766. gl.index = p_sd->text[i];
  1767. gl.font_size = fs;
  1768. gl.font_rid = RID();
  1769. if (p_direction == HB_DIRECTION_RTL || p_direction == HB_DIRECTION_BTT) {
  1770. gl.flags |= TextServer::GRAPHEME_IS_RTL;
  1771. }
  1772. if (p_sd->orientation == ORIENTATION_HORIZONTAL) {
  1773. gl.advance = get_hex_code_box_size(fs, gl.index).x;
  1774. p_sd->ascent = MAX(p_sd->ascent, Math::round(get_hex_code_box_size(fs, gl.index).y * 0.75f));
  1775. p_sd->descent = MAX(p_sd->descent, Math::round(get_hex_code_box_size(fs, gl.index).y * 0.25f));
  1776. } else {
  1777. gl.advance = get_hex_code_box_size(fs, gl.index).y;
  1778. p_sd->ascent = MAX(p_sd->ascent, Math::round(get_hex_code_box_size(fs, gl.index).x * 0.5f));
  1779. p_sd->descent = MAX(p_sd->descent, Math::round(get_hex_code_box_size(fs, gl.index).x * 0.5f));
  1780. }
  1781. p_sd->width += gl.advance;
  1782. p_sd->glyphs.push_back(gl);
  1783. }
  1784. }
  1785. return;
  1786. }
  1787. hb_font_t *hb_font = fd->get_hb_handle(fs);
  1788. ERR_FAIL_COND(hb_font == nullptr);
  1789. hb_buffer_clear_contents(p_sd->hb_buffer);
  1790. hb_buffer_set_direction(p_sd->hb_buffer, p_direction);
  1791. if (p_sd->preserve_control) {
  1792. hb_buffer_set_flags(p_sd->hb_buffer, (hb_buffer_flags_t)(HB_BUFFER_FLAG_PRESERVE_DEFAULT_IGNORABLES | (p_start == 0 ? HB_BUFFER_FLAG_BOT : 0) | (p_end == p_sd->text.length() ? HB_BUFFER_FLAG_EOT : 0)));
  1793. } else {
  1794. hb_buffer_set_flags(p_sd->hb_buffer, (hb_buffer_flags_t)(HB_BUFFER_FLAG_DEFAULT | (p_start == 0 ? HB_BUFFER_FLAG_BOT : 0) | (p_end == p_sd->text.length() ? HB_BUFFER_FLAG_EOT : 0)));
  1795. }
  1796. hb_buffer_set_script(p_sd->hb_buffer, p_script);
  1797. if (p_sd->spans[p_span].language != String()) {
  1798. hb_language_t lang = hb_language_from_string(p_sd->spans[p_span].language.ascii().get_data(), -1);
  1799. hb_buffer_set_language(p_sd->hb_buffer, lang);
  1800. }
  1801. hb_buffer_add_utf32(p_sd->hb_buffer, (const uint32_t *)p_sd->text.ptr(), p_sd->text.length(), p_start, p_end - p_start);
  1802. Vector<hb_feature_t> ftrs;
  1803. for (const Variant *ftr = p_sd->spans[p_span].features.next(nullptr); ftr != nullptr; ftr = p_sd->spans[p_span].features.next(ftr)) {
  1804. double values = p_sd->spans[p_span].features[*ftr];
  1805. if (values >= 0) {
  1806. hb_feature_t feature;
  1807. feature.tag = *ftr;
  1808. feature.value = values;
  1809. feature.start = 0;
  1810. feature.end = -1;
  1811. ftrs.push_back(feature);
  1812. }
  1813. }
  1814. hb_shape(hb_font, p_sd->hb_buffer, ftrs.is_empty() ? nullptr : &ftrs[0], ftrs.size());
  1815. unsigned int glyph_count = 0;
  1816. hb_glyph_info_t *glyph_info = hb_buffer_get_glyph_infos(p_sd->hb_buffer, &glyph_count);
  1817. hb_glyph_position_t *glyph_pos = hb_buffer_get_glyph_positions(p_sd->hb_buffer, &glyph_count);
  1818. // Process glyphs.
  1819. if (glyph_count > 0) {
  1820. TextServer::Glyph *w = (TextServer::Glyph *)memalloc(glyph_count * sizeof(TextServer::Glyph));
  1821. int end = (p_direction == HB_DIRECTION_RTL || p_direction == HB_DIRECTION_BTT) ? p_end : 0;
  1822. uint32_t last_cluster_id = UINT32_MAX;
  1823. unsigned int last_cluster_index = 0;
  1824. bool last_cluster_valid = true;
  1825. for (unsigned int i = 0; i < glyph_count; i++) {
  1826. if ((i > 0) && (last_cluster_id != glyph_info[i].cluster)) {
  1827. if (p_direction == HB_DIRECTION_RTL || p_direction == HB_DIRECTION_BTT) {
  1828. end = w[last_cluster_index].start;
  1829. } else {
  1830. for (unsigned int j = last_cluster_index; j < i; j++) {
  1831. w[j].end = glyph_info[i].cluster;
  1832. }
  1833. }
  1834. if (p_direction == HB_DIRECTION_RTL || p_direction == HB_DIRECTION_BTT) {
  1835. w[last_cluster_index].flags |= TextServer::GRAPHEME_IS_RTL;
  1836. }
  1837. if (last_cluster_valid) {
  1838. w[last_cluster_index].flags |= GRAPHEME_IS_VALID;
  1839. }
  1840. w[last_cluster_index].count = i - last_cluster_index;
  1841. last_cluster_index = i;
  1842. last_cluster_valid = true;
  1843. }
  1844. last_cluster_id = glyph_info[i].cluster;
  1845. TextServer::Glyph &gl = w[i];
  1846. gl = TextServer::Glyph();
  1847. gl.start = glyph_info[i].cluster;
  1848. gl.end = end;
  1849. gl.count = 0;
  1850. gl.font_rid = p_fonts[p_fb_index];
  1851. gl.font_size = fs;
  1852. gl.index = glyph_info[i].codepoint;
  1853. if (gl.index != 0) {
  1854. if (p_sd->orientation == ORIENTATION_HORIZONTAL) {
  1855. gl.advance = Math::round(glyph_pos[i].x_advance / (64.0 / fd->get_font_scale(fs)));
  1856. } else {
  1857. gl.advance = -Math::round(glyph_pos[i].y_advance / (64.0 / fd->get_font_scale(fs)));
  1858. }
  1859. gl.x_off = Math::round(glyph_pos[i].x_offset / (64.0 / fd->get_font_scale(fs)));
  1860. gl.y_off = -Math::round(glyph_pos[i].y_offset / (64.0 / fd->get_font_scale(fs)));
  1861. }
  1862. if (fd->get_spacing_space() && is_whitespace(p_sd->text[glyph_info[i].cluster])) {
  1863. gl.advance += fd->get_spacing_space();
  1864. } else {
  1865. gl.advance += fd->get_spacing_glyph();
  1866. }
  1867. if (p_sd->preserve_control) {
  1868. last_cluster_valid = last_cluster_valid && ((glyph_info[i].codepoint != 0) || is_whitespace(p_sd->text[glyph_info[i].cluster]) || is_linebreak(p_sd->text[glyph_info[i].cluster]));
  1869. } else {
  1870. last_cluster_valid = last_cluster_valid && ((glyph_info[i].codepoint != 0) || !u_isgraph(p_sd->text[glyph_info[i].cluster]));
  1871. }
  1872. }
  1873. if (p_direction == HB_DIRECTION_LTR || p_direction == HB_DIRECTION_TTB) {
  1874. for (unsigned int j = last_cluster_index; j < glyph_count; j++) {
  1875. w[j].end = p_end;
  1876. }
  1877. }
  1878. w[last_cluster_index].count = glyph_count - last_cluster_index;
  1879. if (p_direction == HB_DIRECTION_RTL || p_direction == HB_DIRECTION_BTT) {
  1880. w[last_cluster_index].flags |= TextServer::GRAPHEME_IS_RTL;
  1881. }
  1882. if (last_cluster_valid) {
  1883. w[last_cluster_index].flags |= GRAPHEME_IS_VALID;
  1884. }
  1885. //Fallback.
  1886. int failed_subrun_start = p_end + 1;
  1887. int failed_subrun_end = p_start;
  1888. for (unsigned int i = 0; i < glyph_count; i++) {
  1889. if ((w[i].flags & GRAPHEME_IS_VALID) == GRAPHEME_IS_VALID) {
  1890. if (failed_subrun_start != p_end + 1) {
  1891. _shape_run(p_sd, failed_subrun_start, failed_subrun_end, p_script, p_direction, p_fonts, p_span, p_fb_index + 1);
  1892. failed_subrun_start = p_end + 1;
  1893. failed_subrun_end = p_start;
  1894. }
  1895. for (int j = 0; j < w[i].count; j++) {
  1896. if (p_sd->orientation == ORIENTATION_HORIZONTAL) {
  1897. p_sd->ascent = MAX(p_sd->ascent, -w[i + j].y_off);
  1898. p_sd->descent = MAX(p_sd->descent, w[i + j].y_off);
  1899. } else {
  1900. p_sd->ascent = MAX(p_sd->ascent, Math::round(fd->get_advance(w[i + j].index, fs).x * 0.5));
  1901. p_sd->descent = MAX(p_sd->descent, Math::round(fd->get_advance(w[i + j].index, fs).x * 0.5));
  1902. }
  1903. p_sd->width += w[i + j].advance;
  1904. p_sd->glyphs.push_back(w[i + j]);
  1905. }
  1906. } else {
  1907. if (failed_subrun_start >= w[i].start) {
  1908. failed_subrun_start = w[i].start;
  1909. }
  1910. if (failed_subrun_end <= w[i].end) {
  1911. failed_subrun_end = w[i].end;
  1912. }
  1913. }
  1914. i += w[i].count - 1;
  1915. }
  1916. memfree(w);
  1917. if (failed_subrun_start != p_end + 1) {
  1918. _shape_run(p_sd, failed_subrun_start, failed_subrun_end, p_script, p_direction, p_fonts, p_span, p_fb_index + 1);
  1919. }
  1920. p_sd->ascent = MAX(p_sd->ascent, fd->get_ascent(fs));
  1921. p_sd->descent = MAX(p_sd->descent, fd->get_descent(fs));
  1922. p_sd->upos = MAX(p_sd->upos, fd->get_underline_position(fs));
  1923. p_sd->uthk = MAX(p_sd->uthk, fd->get_underline_thickness(fs));
  1924. }
  1925. }
  1926. bool TextServerAdvanced::shaped_text_shape(RID p_shaped) {
  1927. _THREAD_SAFE_METHOD_
  1928. ShapedTextDataAdvanced *sd = shaped_owner.getornull(p_shaped);
  1929. ERR_FAIL_COND_V(!sd, false);
  1930. if (sd->valid) {
  1931. return true;
  1932. }
  1933. if (sd->parent != RID()) {
  1934. full_copy(sd);
  1935. }
  1936. invalidate(sd);
  1937. if (sd->text.length() == 0) {
  1938. sd->valid = true;
  1939. return true;
  1940. }
  1941. sd->utf16 = sd->text.utf16();
  1942. const UChar *data = sd->utf16.ptr();
  1943. // Create script iterator.
  1944. if (sd->script_iter == nullptr) {
  1945. sd->script_iter = memnew(ScriptIterator(sd->text, 0, sd->text.length()));
  1946. }
  1947. if (sd->bidi_override.is_empty()) {
  1948. sd->bidi_override.push_back(Vector2i(0, sd->end));
  1949. }
  1950. for (int ov = 0; ov < sd->bidi_override.size(); ov++) {
  1951. // Create BiDi iterator.
  1952. int start = _convert_pos_inv(sd, sd->bidi_override[ov].x);
  1953. int end = _convert_pos_inv(sd, sd->bidi_override[ov].y);
  1954. ERR_FAIL_COND_V_MSG((start < 0 || end - start > sd->utf16.length()), false, "Invalid BiDi override range.");
  1955. UErrorCode err = U_ZERO_ERROR;
  1956. UBiDi *bidi_iter = ubidi_openSized(end, 0, &err);
  1957. ERR_FAIL_COND_V_MSG(U_FAILURE(err), false, u_errorName(err));
  1958. switch (sd->direction) {
  1959. case DIRECTION_LTR: {
  1960. ubidi_setPara(bidi_iter, data + start, end - start, UBIDI_LTR, nullptr, &err);
  1961. sd->para_direction = DIRECTION_LTR;
  1962. } break;
  1963. case DIRECTION_RTL: {
  1964. ubidi_setPara(bidi_iter, data + start, end - start, UBIDI_RTL, nullptr, &err);
  1965. sd->para_direction = DIRECTION_RTL;
  1966. } break;
  1967. case DIRECTION_AUTO: {
  1968. UBiDiDirection direction = ubidi_getBaseDirection(data + start, end - start);
  1969. if (direction != UBIDI_NEUTRAL) {
  1970. ubidi_setPara(bidi_iter, data + start, end - start, direction, nullptr, &err);
  1971. sd->para_direction = (direction == UBIDI_RTL) ? DIRECTION_RTL : DIRECTION_LTR;
  1972. } else {
  1973. ubidi_setPara(bidi_iter, data + start, end - start, UBIDI_DEFAULT_LTR, nullptr, &err);
  1974. sd->para_direction = DIRECTION_LTR;
  1975. }
  1976. } break;
  1977. }
  1978. ERR_FAIL_COND_V_MSG(U_FAILURE(err), false, u_errorName(err));
  1979. sd->bidi_iter.push_back(bidi_iter);
  1980. err = U_ZERO_ERROR;
  1981. int bidi_run_count = ubidi_countRuns(bidi_iter, &err);
  1982. ERR_FAIL_COND_V_MSG(U_FAILURE(err), false, u_errorName(err));
  1983. for (int i = 0; i < bidi_run_count; i++) {
  1984. int32_t _bidi_run_start = 0;
  1985. int32_t _bidi_run_length = 0;
  1986. hb_direction_t bidi_run_direction = HB_DIRECTION_INVALID;
  1987. bool is_rtl = (ubidi_getVisualRun(bidi_iter, i, &_bidi_run_start, &_bidi_run_length) == UBIDI_LTR);
  1988. switch (sd->orientation) {
  1989. case ORIENTATION_HORIZONTAL: {
  1990. if (is_rtl) {
  1991. bidi_run_direction = HB_DIRECTION_LTR;
  1992. } else {
  1993. bidi_run_direction = HB_DIRECTION_RTL;
  1994. }
  1995. } break;
  1996. case ORIENTATION_VERTICAL: {
  1997. if (is_rtl) {
  1998. bidi_run_direction = HB_DIRECTION_TTB;
  1999. } else {
  2000. bidi_run_direction = HB_DIRECTION_BTT;
  2001. }
  2002. }
  2003. }
  2004. int32_t bidi_run_start = _convert_pos(sd, sd->bidi_override[ov].x + _bidi_run_start);
  2005. int32_t bidi_run_end = _convert_pos(sd, sd->bidi_override[ov].x + _bidi_run_start + _bidi_run_length);
  2006. // Shape runs.
  2007. int scr_from = (is_rtl) ? 0 : sd->script_iter->script_ranges.size() - 1;
  2008. int scr_to = (is_rtl) ? sd->script_iter->script_ranges.size() : -1;
  2009. int scr_delta = (is_rtl) ? +1 : -1;
  2010. for (int j = scr_from; j != scr_to; j += scr_delta) {
  2011. if ((sd->script_iter->script_ranges[j].start < bidi_run_end) && (sd->script_iter->script_ranges[j].end > bidi_run_start)) {
  2012. int32_t script_run_start = MAX(sd->script_iter->script_ranges[j].start, bidi_run_start);
  2013. int32_t script_run_end = MIN(sd->script_iter->script_ranges[j].end, bidi_run_end);
  2014. char scr_buffer[5] = { 0, 0, 0, 0, 0 };
  2015. hb_tag_to_string(hb_script_to_iso15924_tag(sd->script_iter->script_ranges[j].script), scr_buffer);
  2016. String script = String(scr_buffer);
  2017. int spn_from = (is_rtl) ? 0 : sd->spans.size() - 1;
  2018. int spn_to = (is_rtl) ? sd->spans.size() : -1;
  2019. int spn_delta = (is_rtl) ? +1 : -1;
  2020. for (int k = spn_from; k != spn_to; k += spn_delta) {
  2021. const ShapedTextDataAdvanced::Span &span = sd->spans[k];
  2022. if (span.start >= script_run_end || span.end <= script_run_start) {
  2023. continue;
  2024. }
  2025. if (span.embedded_key != Variant()) {
  2026. // Embedded object.
  2027. if (sd->orientation == ORIENTATION_HORIZONTAL) {
  2028. sd->objects[span.embedded_key].rect.position.x = sd->width;
  2029. sd->width += sd->objects[span.embedded_key].rect.size.x;
  2030. switch (sd->objects[span.embedded_key].inline_align) {
  2031. case VALIGN_TOP: {
  2032. sd->ascent = MAX(sd->ascent, sd->objects[span.embedded_key].rect.size.y);
  2033. } break;
  2034. case VALIGN_CENTER: {
  2035. sd->ascent = MAX(sd->ascent, Math::round(sd->objects[span.embedded_key].rect.size.y / 2));
  2036. sd->descent = MAX(sd->descent, Math::round(sd->objects[span.embedded_key].rect.size.y / 2));
  2037. } break;
  2038. case VALIGN_BOTTOM: {
  2039. sd->descent = MAX(sd->descent, sd->objects[span.embedded_key].rect.size.y);
  2040. } break;
  2041. }
  2042. } else {
  2043. sd->objects[span.embedded_key].rect.position.y = sd->width;
  2044. sd->width += sd->objects[span.embedded_key].rect.size.y;
  2045. switch (sd->objects[span.embedded_key].inline_align) {
  2046. case VALIGN_TOP: {
  2047. sd->ascent = MAX(sd->ascent, sd->objects[span.embedded_key].rect.size.x);
  2048. } break;
  2049. case VALIGN_CENTER: {
  2050. sd->ascent = MAX(sd->ascent, Math::round(sd->objects[span.embedded_key].rect.size.x / 2));
  2051. sd->descent = MAX(sd->descent, Math::round(sd->objects[span.embedded_key].rect.size.x / 2));
  2052. } break;
  2053. case VALIGN_BOTTOM: {
  2054. sd->descent = MAX(sd->descent, sd->objects[span.embedded_key].rect.size.x);
  2055. } break;
  2056. }
  2057. }
  2058. Glyph gl;
  2059. gl.start = span.start;
  2060. gl.end = span.end;
  2061. gl.count = 1;
  2062. gl.flags = GRAPHEME_IS_VALID | GRAPHEME_IS_VIRTUAL;
  2063. if (sd->orientation == ORIENTATION_HORIZONTAL) {
  2064. gl.advance = sd->objects[span.embedded_key].rect.size.x;
  2065. } else {
  2066. gl.advance = sd->objects[span.embedded_key].rect.size.y;
  2067. }
  2068. sd->glyphs.push_back(gl);
  2069. } else {
  2070. Vector<RID> fonts;
  2071. // Push fonts with the language and script support first.
  2072. for (int l = 0; l < span.fonts.size(); l++) {
  2073. if ((font_is_language_supported(span.fonts[l], span.language)) && (font_is_script_supported(span.fonts[l], script))) {
  2074. fonts.push_back(sd->spans[k].fonts[l]);
  2075. }
  2076. }
  2077. // Push fonts with the script support.
  2078. for (int l = 0; l < sd->spans[k].fonts.size(); l++) {
  2079. if (!(font_is_language_supported(span.fonts[l], span.language)) && (font_is_script_supported(span.fonts[l], script))) {
  2080. fonts.push_back(sd->spans[k].fonts[l]);
  2081. }
  2082. }
  2083. // Push the rest valid fonts.
  2084. for (int l = 0; l < sd->spans[k].fonts.size(); l++) {
  2085. if (!(font_is_language_supported(span.fonts[l], span.language)) && !(font_is_script_supported(span.fonts[l], script))) {
  2086. fonts.push_back(sd->spans[k].fonts[l]);
  2087. }
  2088. }
  2089. _shape_run(sd, MAX(sd->spans[k].start, script_run_start), MIN(sd->spans[k].end, script_run_end), sd->script_iter->script_ranges[j].script, bidi_run_direction, fonts, k, 0);
  2090. }
  2091. }
  2092. }
  2093. }
  2094. }
  2095. }
  2096. // Align embedded objects to baseline.
  2097. for (Map<Variant, ShapedTextData::EmbeddedObject>::Element *E = sd->objects.front(); E; E = E->next()) {
  2098. if (sd->orientation == ORIENTATION_HORIZONTAL) {
  2099. switch (E->get().inline_align) {
  2100. case VALIGN_TOP: {
  2101. E->get().rect.position.y = -sd->ascent;
  2102. } break;
  2103. case VALIGN_CENTER: {
  2104. E->get().rect.position.y = -(E->get().rect.size.y / 2);
  2105. } break;
  2106. case VALIGN_BOTTOM: {
  2107. E->get().rect.position.y = sd->descent - E->get().rect.size.y;
  2108. } break;
  2109. }
  2110. } else {
  2111. switch (E->get().inline_align) {
  2112. case VALIGN_TOP: {
  2113. E->get().rect.position.x = -sd->ascent;
  2114. } break;
  2115. case VALIGN_CENTER: {
  2116. E->get().rect.position.x = -(E->get().rect.size.x / 2);
  2117. } break;
  2118. case VALIGN_BOTTOM: {
  2119. E->get().rect.position.x = sd->descent - E->get().rect.size.x;
  2120. } break;
  2121. }
  2122. }
  2123. }
  2124. sd->valid = true;
  2125. return sd->valid;
  2126. }
  2127. bool TextServerAdvanced::shaped_text_is_ready(RID p_shaped) const {
  2128. _THREAD_SAFE_METHOD_
  2129. const ShapedTextDataAdvanced *sd = shaped_owner.getornull(p_shaped);
  2130. ERR_FAIL_COND_V(!sd, false);
  2131. return sd->valid;
  2132. }
  2133. Vector<TextServer::Glyph> TextServerAdvanced::shaped_text_get_glyphs(RID p_shaped) const {
  2134. _THREAD_SAFE_METHOD_
  2135. const ShapedTextDataAdvanced *sd = shaped_owner.getornull(p_shaped);
  2136. ERR_FAIL_COND_V(!sd, Vector<TextServer::Glyph>());
  2137. if (!sd->valid) {
  2138. const_cast<TextServerAdvanced *>(this)->shaped_text_shape(p_shaped);
  2139. }
  2140. return sd->glyphs;
  2141. }
  2142. Vector2i TextServerAdvanced::shaped_text_get_range(RID p_shaped) const {
  2143. _THREAD_SAFE_METHOD_
  2144. const ShapedTextDataAdvanced *sd = shaped_owner.getornull(p_shaped);
  2145. ERR_FAIL_COND_V(!sd, Vector2i());
  2146. return Vector2(sd->start, sd->end);
  2147. }
  2148. Vector<TextServer::Glyph> TextServerAdvanced::shaped_text_sort_logical(RID p_shaped) {
  2149. _THREAD_SAFE_METHOD_
  2150. ShapedTextDataAdvanced *sd = shaped_owner.getornull(p_shaped);
  2151. ERR_FAIL_COND_V(!sd, Vector<TextServer::Glyph>());
  2152. if (!sd->valid) {
  2153. const_cast<TextServerAdvanced *>(this)->shaped_text_shape(p_shaped);
  2154. }
  2155. if (!sd->sort_valid) {
  2156. sd->glyphs_logical = sd->glyphs;
  2157. sd->glyphs_logical.sort_custom<TextServer::GlyphCompare>();
  2158. sd->sort_valid = true;
  2159. }
  2160. return sd->glyphs_logical;
  2161. }
  2162. Array TextServerAdvanced::shaped_text_get_objects(RID p_shaped) const {
  2163. _THREAD_SAFE_METHOD_
  2164. Array ret;
  2165. const ShapedTextDataAdvanced *sd = shaped_owner.getornull(p_shaped);
  2166. ERR_FAIL_COND_V(!sd, ret);
  2167. for (const Map<Variant, ShapedTextData::EmbeddedObject>::Element *E = sd->objects.front(); E; E = E->next()) {
  2168. ret.push_back(E->key());
  2169. }
  2170. return ret;
  2171. }
  2172. Rect2 TextServerAdvanced::shaped_text_get_object_rect(RID p_shaped, Variant p_key) const {
  2173. _THREAD_SAFE_METHOD_
  2174. const ShapedTextDataAdvanced *sd = shaped_owner.getornull(p_shaped);
  2175. ERR_FAIL_COND_V(!sd, Rect2());
  2176. ERR_FAIL_COND_V(!sd->objects.has(p_key), Rect2());
  2177. if (!sd->valid) {
  2178. const_cast<TextServerAdvanced *>(this)->shaped_text_shape(p_shaped);
  2179. }
  2180. return sd->objects[p_key].rect;
  2181. }
  2182. Size2 TextServerAdvanced::shaped_text_get_size(RID p_shaped) const {
  2183. _THREAD_SAFE_METHOD_
  2184. const ShapedTextDataAdvanced *sd = shaped_owner.getornull(p_shaped);
  2185. ERR_FAIL_COND_V(!sd, Size2());
  2186. if (!sd->valid) {
  2187. const_cast<TextServerAdvanced *>(this)->shaped_text_shape(p_shaped);
  2188. }
  2189. if (sd->orientation == TextServer::ORIENTATION_HORIZONTAL) {
  2190. return Size2(sd->width, sd->ascent + sd->descent);
  2191. } else {
  2192. return Size2(sd->ascent + sd->descent, sd->width);
  2193. }
  2194. }
  2195. float TextServerAdvanced::shaped_text_get_ascent(RID p_shaped) const {
  2196. _THREAD_SAFE_METHOD_
  2197. const ShapedTextDataAdvanced *sd = shaped_owner.getornull(p_shaped);
  2198. ERR_FAIL_COND_V(!sd, 0.f);
  2199. if (!sd->valid) {
  2200. const_cast<TextServerAdvanced *>(this)->shaped_text_shape(p_shaped);
  2201. }
  2202. return sd->ascent;
  2203. }
  2204. float TextServerAdvanced::shaped_text_get_descent(RID p_shaped) const {
  2205. _THREAD_SAFE_METHOD_
  2206. const ShapedTextDataAdvanced *sd = shaped_owner.getornull(p_shaped);
  2207. ERR_FAIL_COND_V(!sd, 0.f);
  2208. if (!sd->valid) {
  2209. const_cast<TextServerAdvanced *>(this)->shaped_text_shape(p_shaped);
  2210. }
  2211. return sd->descent;
  2212. }
  2213. float TextServerAdvanced::shaped_text_get_width(RID p_shaped) const {
  2214. _THREAD_SAFE_METHOD_
  2215. const ShapedTextDataAdvanced *sd = shaped_owner.getornull(p_shaped);
  2216. ERR_FAIL_COND_V(!sd, 0.f);
  2217. if (!sd->valid) {
  2218. const_cast<TextServerAdvanced *>(this)->shaped_text_shape(p_shaped);
  2219. }
  2220. return sd->width;
  2221. }
  2222. float TextServerAdvanced::shaped_text_get_underline_position(RID p_shaped) const {
  2223. _THREAD_SAFE_METHOD_
  2224. const ShapedTextDataAdvanced *sd = shaped_owner.getornull(p_shaped);
  2225. ERR_FAIL_COND_V(!sd, 0.f);
  2226. if (!sd->valid) {
  2227. const_cast<TextServerAdvanced *>(this)->shaped_text_shape(p_shaped);
  2228. }
  2229. return sd->upos;
  2230. }
  2231. float TextServerAdvanced::shaped_text_get_underline_thickness(RID p_shaped) const {
  2232. _THREAD_SAFE_METHOD_
  2233. const ShapedTextDataAdvanced *sd = shaped_owner.getornull(p_shaped);
  2234. ERR_FAIL_COND_V(!sd, 0.f);
  2235. if (!sd->valid) {
  2236. const_cast<TextServerAdvanced *>(this)->shaped_text_shape(p_shaped);
  2237. }
  2238. return sd->uthk;
  2239. }
  2240. struct num_system_data {
  2241. String lang;
  2242. String digits;
  2243. String percent_sign;
  2244. String exp;
  2245. };
  2246. static num_system_data num_systems[]{
  2247. { "ar,ar_AR,ar_BH,ar_DJ,ar_EG,ar_ER,ar_IL,ar_IQ,ar_JO,ar_KM,ar_KW,ar_LB,ar_MR,ar_OM,ar_PS,ar_QA,ar_SA,ar_SD,ar_SO,ar_SS,ar_SY,ar_TD,ar_YE", U"٠١٢٣٤٥٦٧٨٩٫", U"٪", U"اس" },
  2248. { "fa,ks,pa_Arab,ps,ug,ur_IN,ur,uz_Arab", U"۰۱۲۳۴۵۶۷۸۹٫", U"٪", U"اس" },
  2249. { "as,bn,mni", U"০১২৩৪৫৬৭৮৯.", U"%", U"e" },
  2250. { "mr,ne", U"०१२३४५६७८९.", U"%", U"e" },
  2251. { "dz", U"༠༡༢༣༤༥༦༧༨༩.", U"%", U"e" },
  2252. { "sat", U"᱐᱑᱒᱓᱔᱕᱖᱗᱘᱙.", U"%", U"e" },
  2253. { "my", U"၀၁၂၃၄၅၆၇၈၉.", U"%", U"e" },
  2254. { String(), String(), String(), String() },
  2255. };
  2256. String TextServerAdvanced::format_number(const String &p_string, const String &p_language) const {
  2257. _THREAD_SAFE_METHOD_
  2258. String lang = (p_language == "") ? TranslationServer::get_singleton()->get_tool_locale() : p_language;
  2259. String res = p_string;
  2260. for (int i = 0; num_systems[i].lang != String(); i++) {
  2261. Vector<String> langs = num_systems[i].lang.split(",");
  2262. if (langs.has(lang)) {
  2263. if (num_systems[i].digits == String()) {
  2264. return p_string;
  2265. }
  2266. res.replace("e", num_systems[i].exp);
  2267. res.replace("E", num_systems[i].exp);
  2268. char32_t *data = res.ptrw();
  2269. for (int j = 0; j < res.size(); j++) {
  2270. if (data[j] >= 0x30 && data[j] <= 0x39) {
  2271. data[j] = num_systems[i].digits[data[j] - 0x30];
  2272. } else if (data[j] == '.' || data[j] == ',') {
  2273. data[j] = num_systems[i].digits[10];
  2274. }
  2275. }
  2276. break;
  2277. }
  2278. }
  2279. return res;
  2280. }
  2281. String TextServerAdvanced::parse_number(const String &p_string, const String &p_language) const {
  2282. _THREAD_SAFE_METHOD_
  2283. String lang = (p_language == "") ? TranslationServer::get_singleton()->get_tool_locale() : p_language;
  2284. String res = p_string;
  2285. for (int i = 0; num_systems[i].lang != String(); i++) {
  2286. Vector<String> langs = num_systems[i].lang.split(",");
  2287. if (langs.has(lang)) {
  2288. if (num_systems[i].digits == String()) {
  2289. return p_string;
  2290. }
  2291. res.replace(num_systems[i].exp, "e");
  2292. char32_t *data = res.ptrw();
  2293. for (int j = 0; j < res.size(); j++) {
  2294. if (data[j] == num_systems[i].digits[10]) {
  2295. data[j] = '.';
  2296. } else {
  2297. for (int k = 0; k < 10; k++) {
  2298. if (data[j] == num_systems[i].digits[k]) {
  2299. data[j] = 0x30 + k;
  2300. }
  2301. }
  2302. }
  2303. }
  2304. break;
  2305. }
  2306. }
  2307. return res;
  2308. }
  2309. String TextServerAdvanced::percent_sign(const String &p_language) const {
  2310. _THREAD_SAFE_METHOD_
  2311. String lang = (p_language == "") ? TranslationServer::get_singleton()->get_tool_locale() : p_language;
  2312. for (int i = 0; num_systems[i].lang != String(); i++) {
  2313. Vector<String> langs = num_systems[i].lang.split(",");
  2314. if (langs.has(lang)) {
  2315. if (num_systems[i].percent_sign == String()) {
  2316. return "%";
  2317. }
  2318. return num_systems[i].percent_sign;
  2319. }
  2320. }
  2321. return "%";
  2322. }
  2323. TextServer *TextServerAdvanced::create_func(Error &r_error, void *p_user_data) {
  2324. r_error = OK;
  2325. return memnew(TextServerAdvanced());
  2326. }
  2327. void TextServerAdvanced::register_server() {
  2328. TextServerManager::register_create_function(interface_name, interface_features, create_func, nullptr);
  2329. }
  2330. TextServerAdvanced::TextServerAdvanced() {
  2331. hb_bmp_create_font_funcs();
  2332. }
  2333. TextServerAdvanced::~TextServerAdvanced() {
  2334. hb_bmp_free_font_funcs();
  2335. u_cleanup();
  2336. #ifndef ICU_STATIC_DATA
  2337. if (icu_data != nullptr) {
  2338. memfree(icu_data);
  2339. icu_data = nullptr;
  2340. }
  2341. #endif
  2342. }