text_server_adv.cpp 90 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562
  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-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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.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() == "ttf" || p_filename.get_extension() == "otf" || p_filename.get_extension() == "woff") {
  480. fd = memnew(DynamicFontDataAdvanced);
  481. } else if (p_filename.get_extension() == "fnt" || p_filename.get_extension() == "font") {
  482. fd = memnew(BitmapFontDataAdvanced);
  483. } else {
  484. return RID();
  485. }
  486. Error err = fd->load_from_file(p_filename, p_base_size);
  487. if (err != OK) {
  488. memdelete(fd);
  489. return RID();
  490. }
  491. return font_owner.make_rid(fd);
  492. }
  493. RID TextServerAdvanced::create_font_memory(const uint8_t *p_data, size_t p_size, const String &p_type, int p_base_size) {
  494. _THREAD_SAFE_METHOD_
  495. FontDataAdvanced *fd = nullptr;
  496. if (p_type == "ttf" || p_type == "otf" || p_type == "woff") {
  497. fd = memnew(DynamicFontDataAdvanced);
  498. } else if (p_type == "fnt" || p_type == "font") {
  499. fd = memnew(BitmapFontDataAdvanced);
  500. } else {
  501. return RID();
  502. }
  503. Error err = fd->load_from_memory(p_data, p_size, p_base_size);
  504. if (err != OK) {
  505. memdelete(fd);
  506. return RID();
  507. }
  508. return font_owner.make_rid(fd);
  509. }
  510. float TextServerAdvanced::font_get_height(RID p_font, int p_size) const {
  511. _THREAD_SAFE_METHOD_
  512. const FontDataAdvanced *fd = font_owner.getornull(p_font);
  513. ERR_FAIL_COND_V(!fd, 0.f);
  514. return fd->get_height(p_size);
  515. }
  516. float TextServerAdvanced::font_get_ascent(RID p_font, int p_size) const {
  517. _THREAD_SAFE_METHOD_
  518. const FontDataAdvanced *fd = font_owner.getornull(p_font);
  519. ERR_FAIL_COND_V(!fd, 0.f);
  520. return fd->get_ascent(p_size);
  521. }
  522. float TextServerAdvanced::font_get_descent(RID p_font, int p_size) const {
  523. _THREAD_SAFE_METHOD_
  524. const FontDataAdvanced *fd = font_owner.getornull(p_font);
  525. ERR_FAIL_COND_V(!fd, 0.f);
  526. return fd->get_descent(p_size);
  527. }
  528. float TextServerAdvanced::font_get_underline_position(RID p_font, int p_size) const {
  529. _THREAD_SAFE_METHOD_
  530. const FontDataAdvanced *fd = font_owner.getornull(p_font);
  531. ERR_FAIL_COND_V(!fd, 0.f);
  532. return fd->get_underline_position(p_size);
  533. }
  534. float TextServerAdvanced::font_get_underline_thickness(RID p_font, int p_size) const {
  535. _THREAD_SAFE_METHOD_
  536. const FontDataAdvanced *fd = font_owner.getornull(p_font);
  537. ERR_FAIL_COND_V(!fd, 0.f);
  538. return fd->get_underline_thickness(p_size);
  539. }
  540. void TextServerAdvanced::font_set_antialiased(RID p_font, bool p_antialiased) {
  541. _THREAD_SAFE_METHOD_
  542. FontDataAdvanced *fd = font_owner.getornull(p_font);
  543. ERR_FAIL_COND(!fd);
  544. fd->set_antialiased(p_antialiased);
  545. }
  546. Dictionary TextServerAdvanced::font_get_feature_list(RID p_font) const {
  547. _THREAD_SAFE_METHOD_
  548. const FontDataAdvanced *fd = font_owner.getornull(p_font);
  549. ERR_FAIL_COND_V(!fd, Dictionary());
  550. return fd->get_feature_list();
  551. }
  552. bool TextServerAdvanced::font_get_antialiased(RID p_font) const {
  553. _THREAD_SAFE_METHOD_
  554. const FontDataAdvanced *fd = font_owner.getornull(p_font);
  555. ERR_FAIL_COND_V(!fd, false);
  556. return fd->get_antialiased();
  557. }
  558. Dictionary TextServerAdvanced::font_get_variation_list(RID p_font) const {
  559. _THREAD_SAFE_METHOD_
  560. const FontDataAdvanced *fd = font_owner.getornull(p_font);
  561. ERR_FAIL_COND_V(!fd, Dictionary());
  562. return fd->get_variation_list();
  563. }
  564. void TextServerAdvanced::font_set_variation(RID p_font, const String &p_name, double p_value) {
  565. _THREAD_SAFE_METHOD_
  566. FontDataAdvanced *fd = font_owner.getornull(p_font);
  567. ERR_FAIL_COND(!fd);
  568. fd->set_variation(p_name, p_value);
  569. }
  570. double TextServerAdvanced::font_get_variation(RID p_font, const String &p_name) const {
  571. _THREAD_SAFE_METHOD_
  572. const FontDataAdvanced *fd = font_owner.getornull(p_font);
  573. ERR_FAIL_COND_V(!fd, 0);
  574. return fd->get_variation(p_name);
  575. }
  576. void TextServerAdvanced::font_set_distance_field_hint(RID p_font, bool p_distance_field) {
  577. _THREAD_SAFE_METHOD_
  578. FontDataAdvanced *fd = font_owner.getornull(p_font);
  579. ERR_FAIL_COND(!fd);
  580. fd->set_distance_field_hint(p_distance_field);
  581. }
  582. bool TextServerAdvanced::font_get_distance_field_hint(RID p_font) const {
  583. _THREAD_SAFE_METHOD_
  584. const FontDataAdvanced *fd = font_owner.getornull(p_font);
  585. ERR_FAIL_COND_V(!fd, false);
  586. return fd->get_distance_field_hint();
  587. }
  588. void TextServerAdvanced::font_set_hinting(RID p_font, TextServer::Hinting p_hinting) {
  589. _THREAD_SAFE_METHOD_
  590. FontDataAdvanced *fd = font_owner.getornull(p_font);
  591. ERR_FAIL_COND(!fd);
  592. fd->set_hinting(p_hinting);
  593. }
  594. TextServer::Hinting TextServerAdvanced::font_get_hinting(RID p_font) const {
  595. _THREAD_SAFE_METHOD_
  596. const FontDataAdvanced *fd = font_owner.getornull(p_font);
  597. ERR_FAIL_COND_V(!fd, TextServer::HINTING_NONE);
  598. return fd->get_hinting();
  599. }
  600. void TextServerAdvanced::font_set_force_autohinter(RID p_font, bool p_enabeld) {
  601. _THREAD_SAFE_METHOD_
  602. FontDataAdvanced *fd = font_owner.getornull(p_font);
  603. ERR_FAIL_COND(!fd);
  604. fd->set_force_autohinter(p_enabeld);
  605. }
  606. bool TextServerAdvanced::font_get_force_autohinter(RID p_font) const {
  607. _THREAD_SAFE_METHOD_
  608. const FontDataAdvanced *fd = font_owner.getornull(p_font);
  609. ERR_FAIL_COND_V(!fd, false);
  610. return fd->get_force_autohinter();
  611. }
  612. bool TextServerAdvanced::font_has_char(RID p_font, char32_t p_char) const {
  613. _THREAD_SAFE_METHOD_
  614. const FontDataAdvanced *fd = font_owner.getornull(p_font);
  615. ERR_FAIL_COND_V(!fd, false);
  616. return fd->has_char(p_char);
  617. }
  618. String TextServerAdvanced::font_get_supported_chars(RID p_font) const {
  619. _THREAD_SAFE_METHOD_
  620. const FontDataAdvanced *fd = font_owner.getornull(p_font);
  621. ERR_FAIL_COND_V(!fd, String());
  622. return fd->get_supported_chars();
  623. }
  624. bool TextServerAdvanced::font_has_outline(RID p_font) const {
  625. _THREAD_SAFE_METHOD_
  626. const FontDataAdvanced *fd = font_owner.getornull(p_font);
  627. ERR_FAIL_COND_V(!fd, false);
  628. return fd->has_outline();
  629. }
  630. float TextServerAdvanced::font_get_base_size(RID p_font) const {
  631. _THREAD_SAFE_METHOD_
  632. const FontDataAdvanced *fd = font_owner.getornull(p_font);
  633. ERR_FAIL_COND_V(!fd, 0.f);
  634. return fd->get_base_size();
  635. }
  636. bool TextServerAdvanced::font_is_language_supported(RID p_font, const String &p_language) const {
  637. _THREAD_SAFE_METHOD_
  638. const FontDataAdvanced *fd = font_owner.getornull(p_font);
  639. ERR_FAIL_COND_V(!fd, false);
  640. if (fd->lang_support_overrides.has(p_language)) {
  641. return fd->lang_support_overrides[p_language];
  642. } else {
  643. Vector<String> tags = p_language.replace("-", "_").split("_");
  644. if (tags.size() > 0) {
  645. if (fd->lang_support_overrides.has(tags[0])) {
  646. return fd->lang_support_overrides[tags[0]];
  647. }
  648. }
  649. return fd->is_lang_supported(p_language);
  650. }
  651. }
  652. void TextServerAdvanced::font_set_language_support_override(RID p_font, const String &p_language, bool p_supported) {
  653. _THREAD_SAFE_METHOD_
  654. FontDataAdvanced *fd = font_owner.getornull(p_font);
  655. ERR_FAIL_COND(!fd);
  656. fd->lang_support_overrides[p_language] = p_supported;
  657. }
  658. bool TextServerAdvanced::font_get_language_support_override(RID p_font, const String &p_language) {
  659. _THREAD_SAFE_METHOD_
  660. FontDataAdvanced *fd = font_owner.getornull(p_font);
  661. ERR_FAIL_COND_V(!fd, false);
  662. return fd->lang_support_overrides[p_language];
  663. }
  664. void TextServerAdvanced::font_remove_language_support_override(RID p_font, const String &p_language) {
  665. _THREAD_SAFE_METHOD_
  666. FontDataAdvanced *fd = font_owner.getornull(p_font);
  667. ERR_FAIL_COND(!fd);
  668. fd->lang_support_overrides.erase(p_language);
  669. }
  670. Vector<String> TextServerAdvanced::font_get_language_support_overrides(RID p_font) {
  671. _THREAD_SAFE_METHOD_
  672. FontDataAdvanced *fd = font_owner.getornull(p_font);
  673. ERR_FAIL_COND_V(!fd, Vector<String>());
  674. Vector<String> ret;
  675. for (Map<String, bool>::Element *E = fd->lang_support_overrides.front(); E; E = E->next()) {
  676. ret.push_back(E->key());
  677. }
  678. return ret;
  679. }
  680. bool TextServerAdvanced::font_is_script_supported(RID p_font, const String &p_script) const {
  681. _THREAD_SAFE_METHOD_
  682. const FontDataAdvanced *fd = font_owner.getornull(p_font);
  683. ERR_FAIL_COND_V(!fd, false);
  684. if (fd->script_support_overrides.has(p_script)) {
  685. return fd->script_support_overrides[p_script];
  686. } else {
  687. hb_script_t scr = hb_script_from_string(p_script.ascii().get_data(), -1);
  688. return fd->is_script_supported(scr);
  689. }
  690. }
  691. void TextServerAdvanced::font_set_script_support_override(RID p_font, const String &p_script, bool p_supported) {
  692. _THREAD_SAFE_METHOD_
  693. FontDataAdvanced *fd = font_owner.getornull(p_font);
  694. ERR_FAIL_COND(!fd);
  695. fd->script_support_overrides[p_script] = p_supported;
  696. }
  697. bool TextServerAdvanced::font_get_script_support_override(RID p_font, const String &p_script) {
  698. _THREAD_SAFE_METHOD_
  699. FontDataAdvanced *fd = font_owner.getornull(p_font);
  700. ERR_FAIL_COND_V(!fd, false);
  701. return fd->script_support_overrides[p_script];
  702. }
  703. void TextServerAdvanced::font_remove_script_support_override(RID p_font, const String &p_script) {
  704. _THREAD_SAFE_METHOD_
  705. FontDataAdvanced *fd = font_owner.getornull(p_font);
  706. ERR_FAIL_COND(!fd);
  707. fd->script_support_overrides.erase(p_script);
  708. }
  709. Vector<String> TextServerAdvanced::font_get_script_support_overrides(RID p_font) {
  710. _THREAD_SAFE_METHOD_
  711. FontDataAdvanced *fd = font_owner.getornull(p_font);
  712. ERR_FAIL_COND_V(!fd, Vector<String>());
  713. Vector<String> ret;
  714. for (Map<String, bool>::Element *E = fd->script_support_overrides.front(); E; E = E->next()) {
  715. ret.push_back(E->key());
  716. }
  717. return ret;
  718. }
  719. uint32_t TextServerAdvanced::font_get_glyph_index(RID p_font, char32_t p_char, char32_t p_variation_selector) const {
  720. _THREAD_SAFE_METHOD_
  721. const FontDataAdvanced *fd = font_owner.getornull(p_font);
  722. ERR_FAIL_COND_V(!fd, 0);
  723. return fd->get_glyph_index(p_char, p_variation_selector);
  724. }
  725. Vector2 TextServerAdvanced::font_get_glyph_advance(RID p_font, uint32_t p_index, int p_size) const {
  726. _THREAD_SAFE_METHOD_
  727. const FontDataAdvanced *fd = font_owner.getornull(p_font);
  728. ERR_FAIL_COND_V(!fd, Vector2());
  729. return fd->get_advance(p_index, p_size);
  730. }
  731. Vector2 TextServerAdvanced::font_get_glyph_kerning(RID p_font, uint32_t p_index_a, uint32_t p_index_b, int p_size) const {
  732. _THREAD_SAFE_METHOD_
  733. const FontDataAdvanced *fd = font_owner.getornull(p_font);
  734. ERR_FAIL_COND_V(!fd, Vector2());
  735. return fd->get_kerning(p_index_a, p_index_b, p_size);
  736. }
  737. 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 {
  738. _THREAD_SAFE_METHOD_
  739. const FontDataAdvanced *fd = font_owner.getornull(p_font);
  740. ERR_FAIL_COND_V(!fd, Vector2());
  741. return fd->draw_glyph(p_canvas, p_size, p_pos, p_index, p_color);
  742. }
  743. 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 {
  744. _THREAD_SAFE_METHOD_
  745. const FontDataAdvanced *fd = font_owner.getornull(p_font);
  746. ERR_FAIL_COND_V(!fd, Vector2());
  747. return fd->draw_glyph_outline(p_canvas, p_size, p_outline_size, p_pos, p_index, p_color);
  748. }
  749. float TextServerAdvanced::font_get_oversampling() const {
  750. return oversampling;
  751. }
  752. void TextServerAdvanced::font_set_oversampling(float p_oversampling) {
  753. _THREAD_SAFE_METHOD_
  754. if (oversampling != p_oversampling) {
  755. oversampling = p_oversampling;
  756. List<RID> fonts;
  757. font_owner.get_owned_list(&fonts);
  758. for (List<RID>::Element *E = fonts.front(); E; E = E->next()) {
  759. font_owner.getornull(E->get())->clear_cache();
  760. }
  761. List<RID> text_bufs;
  762. shaped_owner.get_owned_list(&text_bufs);
  763. for (List<RID>::Element *E = text_bufs.front(); E; E = E->next()) {
  764. invalidate(shaped_owner.getornull(E->get()));
  765. }
  766. }
  767. }
  768. Vector<String> TextServerAdvanced::get_system_fonts() const {
  769. return Vector<String>();
  770. }
  771. /*************************************************************************/
  772. /* Shaped text buffer interface */
  773. /*************************************************************************/
  774. int TextServerAdvanced::_convert_pos(const ShapedTextDataAdvanced *p_sd, int p_pos) const {
  775. int32_t limit = p_pos;
  776. if (p_sd->text.length() != p_sd->utf16.length()) {
  777. const UChar *data = p_sd->utf16.ptr();
  778. for (int i = 0; i < p_pos; i++) {
  779. if (U16_IS_LEAD(data[i])) {
  780. limit--;
  781. }
  782. }
  783. }
  784. return limit;
  785. }
  786. int TextServerAdvanced::_convert_pos_inv(const ShapedTextDataAdvanced *p_sd, int p_pos) const {
  787. int32_t limit = p_pos;
  788. if (p_sd->text.length() != p_sd->utf16.length()) {
  789. for (int i = 0; i < p_pos; i++) {
  790. if (p_sd->text[i] > 0xFFFF) {
  791. limit++;
  792. }
  793. }
  794. }
  795. return limit;
  796. }
  797. void TextServerAdvanced::invalidate(TextServerAdvanced::ShapedTextDataAdvanced *p_shaped) {
  798. p_shaped->valid = false;
  799. p_shaped->sort_valid = false;
  800. p_shaped->line_breaks_valid = false;
  801. p_shaped->justification_ops_valid = false;
  802. p_shaped->ascent = 0.f;
  803. p_shaped->descent = 0.f;
  804. p_shaped->width = 0.f;
  805. p_shaped->upos = 0.f;
  806. p_shaped->uthk = 0.f;
  807. p_shaped->glyphs.clear();
  808. p_shaped->glyphs_logical.clear();
  809. p_shaped->utf16 = Char16String();
  810. if (p_shaped->script_iter != nullptr) {
  811. memdelete(p_shaped->script_iter);
  812. p_shaped->script_iter = nullptr;
  813. }
  814. for (int i = 0; i < p_shaped->bidi_iter.size(); i++) {
  815. ubidi_close(p_shaped->bidi_iter[i]);
  816. }
  817. p_shaped->bidi_iter.clear();
  818. }
  819. void TextServerAdvanced::full_copy(ShapedTextDataAdvanced *p_shaped) {
  820. ShapedTextDataAdvanced *parent = shaped_owner.getornull(p_shaped->parent);
  821. for (Map<Variant, ShapedTextData::EmbeddedObject>::Element *E = parent->objects.front(); E; E = E->next()) {
  822. if (E->get().pos >= p_shaped->start && E->get().pos < p_shaped->end) {
  823. p_shaped->objects[E->key()] = E->get();
  824. }
  825. }
  826. for (int k = 0; k < parent->spans.size(); k++) {
  827. ShapedTextDataAdvanced::Span span = parent->spans[k];
  828. if (span.start >= p_shaped->end || span.end <= p_shaped->start) {
  829. continue;
  830. }
  831. span.start = MAX(p_shaped->start, span.start);
  832. span.end = MIN(p_shaped->end, span.end);
  833. p_shaped->spans.push_back(span);
  834. }
  835. p_shaped->parent = RID();
  836. }
  837. RID TextServerAdvanced::create_shaped_text(TextServer::Direction p_direction, TextServer::Orientation p_orientation) {
  838. _THREAD_SAFE_METHOD_
  839. ShapedTextDataAdvanced *sd = memnew(ShapedTextDataAdvanced);
  840. sd->hb_buffer = hb_buffer_create();
  841. sd->direction = p_direction;
  842. sd->orientation = p_orientation;
  843. return shaped_owner.make_rid(sd);
  844. }
  845. void TextServerAdvanced::shaped_text_clear(RID p_shaped) {
  846. _THREAD_SAFE_METHOD_
  847. ShapedTextDataAdvanced *sd = shaped_owner.getornull(p_shaped);
  848. ERR_FAIL_COND(!sd);
  849. sd->parent = RID();
  850. sd->start = 0;
  851. sd->end = 0;
  852. sd->text = String();
  853. sd->spans.clear();
  854. sd->objects.clear();
  855. sd->bidi_override.clear();
  856. invalidate(sd);
  857. }
  858. void TextServerAdvanced::shaped_text_set_direction(RID p_shaped, TextServer::Direction p_direction) {
  859. _THREAD_SAFE_METHOD_
  860. ShapedTextDataAdvanced *sd = shaped_owner.getornull(p_shaped);
  861. ERR_FAIL_COND(!sd);
  862. if (sd->direction != p_direction) {
  863. if (sd->parent != RID()) {
  864. full_copy(sd);
  865. }
  866. sd->direction = p_direction;
  867. invalidate(sd);
  868. }
  869. }
  870. TextServer::Direction TextServerAdvanced::shaped_text_get_direction(RID p_shaped) const {
  871. _THREAD_SAFE_METHOD_
  872. const ShapedTextDataAdvanced *sd = shaped_owner.getornull(p_shaped);
  873. ERR_FAIL_COND_V(!sd, TextServer::DIRECTION_LTR);
  874. return sd->direction;
  875. }
  876. void TextServerAdvanced::shaped_text_set_bidi_override(RID p_shaped, const Vector<Vector2i> &p_override) {
  877. _THREAD_SAFE_METHOD_
  878. ShapedTextDataAdvanced *sd = shaped_owner.getornull(p_shaped);
  879. ERR_FAIL_COND(!sd);
  880. if (sd->parent != RID()) {
  881. full_copy(sd);
  882. }
  883. sd->bidi_override = p_override;
  884. invalidate(sd);
  885. }
  886. void TextServerAdvanced::shaped_text_set_orientation(RID p_shaped, TextServer::Orientation p_orientation) {
  887. _THREAD_SAFE_METHOD_
  888. ShapedTextDataAdvanced *sd = shaped_owner.getornull(p_shaped);
  889. ERR_FAIL_COND(!sd);
  890. if (sd->orientation != p_orientation) {
  891. if (sd->parent != RID()) {
  892. full_copy(sd);
  893. }
  894. sd->orientation = p_orientation;
  895. invalidate(sd);
  896. }
  897. }
  898. void TextServerAdvanced::shaped_text_set_preserve_invalid(RID p_shaped, bool p_enabled) {
  899. _THREAD_SAFE_METHOD_
  900. ShapedTextDataAdvanced *sd = shaped_owner.getornull(p_shaped);
  901. ERR_FAIL_COND(!sd);
  902. ERR_FAIL_COND(sd->parent != RID());
  903. if (sd->preserve_invalid != p_enabled) {
  904. sd->preserve_invalid = p_enabled;
  905. invalidate(sd);
  906. }
  907. }
  908. bool TextServerAdvanced::shaped_text_get_preserve_invalid(RID p_shaped) const {
  909. _THREAD_SAFE_METHOD_
  910. const ShapedTextDataAdvanced *sd = shaped_owner.getornull(p_shaped);
  911. ERR_FAIL_COND_V(!sd, false);
  912. return sd->preserve_invalid;
  913. }
  914. void TextServerAdvanced::shaped_text_set_preserve_control(RID p_shaped, bool p_enabled) {
  915. _THREAD_SAFE_METHOD_
  916. ShapedTextDataAdvanced *sd = shaped_owner.getornull(p_shaped);
  917. ERR_FAIL_COND(!sd);
  918. if (sd->preserve_control != p_enabled) {
  919. if (sd->parent != RID()) {
  920. full_copy(sd);
  921. }
  922. sd->preserve_control = p_enabled;
  923. invalidate(sd);
  924. }
  925. }
  926. bool TextServerAdvanced::shaped_text_get_preserve_control(RID p_shaped) const {
  927. _THREAD_SAFE_METHOD_
  928. const ShapedTextDataAdvanced *sd = shaped_owner.getornull(p_shaped);
  929. ERR_FAIL_COND_V(!sd, false);
  930. return sd->preserve_control;
  931. }
  932. TextServer::Orientation TextServerAdvanced::shaped_text_get_orientation(RID p_shaped) const {
  933. _THREAD_SAFE_METHOD_
  934. const ShapedTextDataAdvanced *sd = shaped_owner.getornull(p_shaped);
  935. ERR_FAIL_COND_V(!sd, TextServer::ORIENTATION_HORIZONTAL);
  936. return sd->orientation;
  937. }
  938. 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) {
  939. _THREAD_SAFE_METHOD_
  940. ShapedTextDataAdvanced *sd = shaped_owner.getornull(p_shaped);
  941. ERR_FAIL_COND_V(!sd, false);
  942. ERR_FAIL_COND_V(p_size <= 0, false);
  943. if (p_text.empty()) {
  944. return true;
  945. }
  946. if (sd->parent != RID()) {
  947. full_copy(sd);
  948. }
  949. ShapedTextDataAdvanced::Span span;
  950. span.start = sd->text.length();
  951. span.end = span.start + p_text.length();
  952. span.fonts = p_fonts; // Do not pre-sort, spans will be divided to subruns later.
  953. span.font_size = p_size;
  954. span.language = p_language;
  955. span.features = p_opentype_features;
  956. sd->spans.push_back(span);
  957. sd->text += p_text;
  958. sd->end += p_text.length();
  959. invalidate(sd);
  960. return true;
  961. }
  962. bool TextServerAdvanced::shaped_text_add_object(RID p_shaped, Variant p_key, const Size2 &p_size, VAlign p_inline_align, int p_length) {
  963. _THREAD_SAFE_METHOD_
  964. ShapedTextDataAdvanced *sd = shaped_owner.getornull(p_shaped);
  965. ERR_FAIL_COND_V(!sd, false);
  966. ERR_FAIL_COND_V(p_key == Variant(), false);
  967. ERR_FAIL_COND_V(sd->objects.has(p_key), false);
  968. if (sd->parent != RID()) {
  969. full_copy(sd);
  970. }
  971. ShapedTextDataAdvanced::Span span;
  972. span.start = sd->text.length();
  973. span.end = span.start + p_length;
  974. span.embedded_key = p_key;
  975. ShapedTextDataAdvanced::EmbeddedObject obj;
  976. obj.inline_align = p_inline_align;
  977. obj.rect.size = p_size;
  978. obj.pos = span.start;
  979. sd->spans.push_back(span);
  980. sd->text += String::chr(0xfffc).repeat(p_length);
  981. sd->end += p_length;
  982. sd->objects[p_key] = obj;
  983. invalidate(sd);
  984. return true;
  985. }
  986. bool TextServerAdvanced::shaped_text_resize_object(RID p_shaped, Variant p_key, const Size2 &p_size, VAlign p_inline_align) {
  987. _THREAD_SAFE_METHOD_
  988. ShapedTextData *sd = shaped_owner.getornull(p_shaped);
  989. ERR_FAIL_COND_V(!sd, false);
  990. ERR_FAIL_COND_V(!sd->objects.has(p_key), false);
  991. sd->objects[p_key].rect.size = p_size;
  992. sd->objects[p_key].inline_align = p_inline_align;
  993. if (sd->valid) {
  994. // Recalc string metrics.
  995. sd->ascent = 0;
  996. sd->descent = 0;
  997. sd->width = 0;
  998. sd->upos = 0;
  999. sd->uthk = 0;
  1000. int sd_size = sd->glyphs.size();
  1001. for (int i = 0; i < sd_size; i++) {
  1002. Glyph gl = sd->glyphs[i];
  1003. Variant key;
  1004. if (gl.count == 1) {
  1005. for (Map<Variant, ShapedTextData::EmbeddedObject>::Element *E = sd->objects.front(); E; E = E->next()) {
  1006. if (E->get().pos == gl.start) {
  1007. key = E->key();
  1008. break;
  1009. }
  1010. }
  1011. }
  1012. if (key != Variant()) {
  1013. if (sd->orientation == ORIENTATION_HORIZONTAL) {
  1014. sd->objects[key].rect.position.x = sd->width;
  1015. sd->width += sd->objects[key].rect.size.x;
  1016. switch (sd->objects[key].inline_align) {
  1017. case VALIGN_TOP: {
  1018. sd->ascent = MAX(sd->ascent, sd->objects[key].rect.size.y);
  1019. } break;
  1020. case VALIGN_CENTER: {
  1021. sd->ascent = MAX(sd->ascent, Math::round(sd->objects[key].rect.size.y / 2));
  1022. sd->descent = MAX(sd->descent, Math::round(sd->objects[key].rect.size.y / 2));
  1023. } break;
  1024. case VALIGN_BOTTOM: {
  1025. sd->descent = MAX(sd->descent, sd->objects[key].rect.size.y);
  1026. } break;
  1027. }
  1028. sd->glyphs.write[i].advance = sd->objects[key].rect.size.x;
  1029. } else {
  1030. sd->objects[key].rect.position.y = sd->width;
  1031. sd->width += sd->objects[key].rect.size.y;
  1032. switch (sd->objects[key].inline_align) {
  1033. case VALIGN_TOP: {
  1034. sd->ascent = MAX(sd->ascent, sd->objects[key].rect.size.x);
  1035. } break;
  1036. case VALIGN_CENTER: {
  1037. sd->ascent = MAX(sd->ascent, Math::round(sd->objects[key].rect.size.x / 2));
  1038. sd->descent = MAX(sd->descent, Math::round(sd->objects[key].rect.size.x / 2));
  1039. } break;
  1040. case VALIGN_BOTTOM: {
  1041. sd->descent = MAX(sd->descent, sd->objects[key].rect.size.x);
  1042. } break;
  1043. }
  1044. sd->glyphs.write[i].advance = sd->objects[key].rect.size.y;
  1045. }
  1046. } else {
  1047. const FontDataAdvanced *fd = font_owner.getornull(gl.font_rid);
  1048. if (fd != nullptr) {
  1049. if (sd->orientation == ORIENTATION_HORIZONTAL) {
  1050. sd->ascent = MAX(sd->ascent, MAX(fd->get_ascent(gl.font_size), -gl.y_off));
  1051. sd->descent = MAX(sd->descent, MAX(fd->get_descent(gl.font_size), gl.y_off));
  1052. } else {
  1053. sd->ascent = MAX(sd->ascent, Math::round(fd->get_advance(gl.index, gl.font_size).x * 0.5));
  1054. sd->descent = MAX(sd->descent, Math::round(fd->get_advance(gl.index, gl.font_size).x * 0.5));
  1055. }
  1056. sd->upos = MAX(sd->upos, font_get_underline_position(gl.font_rid, gl.font_size));
  1057. sd->uthk = MAX(sd->uthk, font_get_underline_thickness(gl.font_rid, gl.font_size));
  1058. } else if (sd->preserve_invalid || (sd->preserve_control && is_control(gl.index))) {
  1059. // Glyph not found, replace with hex code box.
  1060. if (sd->orientation == ORIENTATION_HORIZONTAL) {
  1061. sd->ascent = MAX(sd->ascent, Math::round(get_hex_code_box_size(gl.font_size, gl.index).y * 0.75f));
  1062. sd->descent = MAX(sd->descent, Math::round(get_hex_code_box_size(gl.font_size, gl.index).y * 0.25f));
  1063. } else {
  1064. sd->ascent = MAX(sd->ascent, Math::round(get_hex_code_box_size(gl.font_size, gl.index).x * 0.5f));
  1065. sd->descent = MAX(sd->descent, Math::round(get_hex_code_box_size(gl.font_size, gl.index).x * 0.5f));
  1066. }
  1067. }
  1068. sd->width += gl.advance * gl.repeat;
  1069. }
  1070. }
  1071. // Align embedded objects to baseline.
  1072. for (Map<Variant, ShapedTextData::EmbeddedObject>::Element *E = sd->objects.front(); E; E = E->next()) {
  1073. if ((E->get().pos >= sd->start) && (E->get().pos < sd->end)) {
  1074. if (sd->orientation == ORIENTATION_HORIZONTAL) {
  1075. switch (E->get().inline_align) {
  1076. case VALIGN_TOP: {
  1077. E->get().rect.position.y = -sd->ascent;
  1078. } break;
  1079. case VALIGN_CENTER: {
  1080. E->get().rect.position.y = -(E->get().rect.size.y / 2);
  1081. } break;
  1082. case VALIGN_BOTTOM: {
  1083. E->get().rect.position.y = sd->descent - E->get().rect.size.y;
  1084. } break;
  1085. }
  1086. } else {
  1087. switch (E->get().inline_align) {
  1088. case VALIGN_TOP: {
  1089. E->get().rect.position.x = -sd->ascent;
  1090. } break;
  1091. case VALIGN_CENTER: {
  1092. E->get().rect.position.x = -(E->get().rect.size.x / 2);
  1093. } break;
  1094. case VALIGN_BOTTOM: {
  1095. E->get().rect.position.x = sd->descent - E->get().rect.size.x;
  1096. } break;
  1097. }
  1098. }
  1099. }
  1100. }
  1101. }
  1102. return true;
  1103. }
  1104. RID TextServerAdvanced::shaped_text_substr(RID p_shaped, int p_start, int p_length) const {
  1105. _THREAD_SAFE_METHOD_
  1106. const ShapedTextDataAdvanced *sd = shaped_owner.getornull(p_shaped);
  1107. ERR_FAIL_COND_V(!sd, RID());
  1108. if (sd->parent != RID()) {
  1109. return shaped_text_substr(sd->parent, p_start, p_length);
  1110. }
  1111. if (!sd->valid) {
  1112. const_cast<TextServerAdvanced *>(this)->shaped_text_shape(p_shaped);
  1113. }
  1114. ERR_FAIL_COND_V(p_start < 0 || p_length < 0, RID());
  1115. ERR_FAIL_COND_V(sd->start > p_start || sd->end < p_start, RID());
  1116. ERR_FAIL_COND_V(sd->end < p_start + p_length, RID());
  1117. ShapedTextDataAdvanced *new_sd = memnew(ShapedTextDataAdvanced);
  1118. new_sd->hb_buffer = hb_buffer_create();
  1119. new_sd->parent = p_shaped;
  1120. new_sd->start = p_start;
  1121. new_sd->end = p_start + p_length;
  1122. new_sd->orientation = sd->orientation;
  1123. new_sd->direction = sd->direction;
  1124. new_sd->para_direction = sd->para_direction;
  1125. new_sd->line_breaks_valid = sd->line_breaks_valid;
  1126. new_sd->justification_ops_valid = sd->justification_ops_valid;
  1127. new_sd->sort_valid = false;
  1128. new_sd->upos = sd->upos;
  1129. new_sd->uthk = sd->uthk;
  1130. if (p_length > 0) {
  1131. new_sd->text = sd->text.substr(p_start, p_length);
  1132. new_sd->utf16 = new_sd->text.utf16();
  1133. new_sd->script_iter = memnew(ScriptIterator(new_sd->text, 0, new_sd->text.length()));
  1134. int sd_size = sd->glyphs.size();
  1135. const Glyph *sd_glyphs = sd->glyphs.ptr();
  1136. const FontDataAdvanced *fd = nullptr;
  1137. RID prev_rid = RID();
  1138. for (int ov = 0; ov < sd->bidi_override.size(); ov++) {
  1139. UErrorCode err = U_ZERO_ERROR;
  1140. if (sd->bidi_override[ov].x >= p_start + p_length || sd->bidi_override[ov].y <= p_start) {
  1141. continue;
  1142. }
  1143. int start = _convert_pos_inv(sd, MAX(0, p_start - sd->bidi_override[ov].x));
  1144. int end = _convert_pos_inv(sd, MIN(p_start + p_length, sd->bidi_override[ov].y) - sd->bidi_override[ov].x);
  1145. ERR_FAIL_COND_V_MSG((start < 0 || end - start > new_sd->utf16.length()), RID(), "Invalid BiDi override range.");
  1146. //Create temporary line bidi & shape
  1147. UBiDi *bidi_iter = ubidi_openSized(end - start, 0, &err);
  1148. ERR_FAIL_COND_V_MSG(U_FAILURE(err), RID(), u_errorName(err));
  1149. ubidi_setLine(sd->bidi_iter[ov], start, end, bidi_iter, &err);
  1150. if (U_FAILURE(err)) {
  1151. ubidi_close(bidi_iter);
  1152. ERR_FAIL_V_MSG(RID(), u_errorName(err));
  1153. }
  1154. new_sd->bidi_iter.push_back(bidi_iter);
  1155. err = U_ZERO_ERROR;
  1156. int bidi_run_count = ubidi_countRuns(bidi_iter, &err);
  1157. ERR_FAIL_COND_V_MSG(U_FAILURE(err), RID(), u_errorName(err));
  1158. for (int i = 0; i < bidi_run_count; i++) {
  1159. int32_t _bidi_run_start = 0;
  1160. int32_t _bidi_run_length = 0;
  1161. ubidi_getVisualRun(bidi_iter, i, &_bidi_run_start, &_bidi_run_length);
  1162. int32_t bidi_run_start = _convert_pos(sd, sd->bidi_override[ov].x + start + _bidi_run_start);
  1163. int32_t bidi_run_end = _convert_pos(sd, sd->bidi_override[ov].x + start + _bidi_run_start + _bidi_run_length);
  1164. for (int j = 0; j < sd_size; j++) {
  1165. if ((sd_glyphs[j].start >= bidi_run_start) && (sd_glyphs[j].end <= bidi_run_end)) {
  1166. // Copy glyphs.
  1167. Glyph gl = sd_glyphs[j];
  1168. Variant key;
  1169. bool find_embedded = false;
  1170. if (gl.count == 1) {
  1171. for (Map<Variant, ShapedTextData::EmbeddedObject>::Element *E = sd->objects.front(); E; E = E->next()) {
  1172. if (E->get().pos == gl.start) {
  1173. find_embedded = true;
  1174. key = E->key();
  1175. new_sd->objects[key] = E->get();
  1176. break;
  1177. }
  1178. }
  1179. }
  1180. if (find_embedded) {
  1181. if (new_sd->orientation == ORIENTATION_HORIZONTAL) {
  1182. new_sd->objects[key].rect.position.x = new_sd->width;
  1183. new_sd->width += new_sd->objects[key].rect.size.x;
  1184. switch (new_sd->objects[key].inline_align) {
  1185. case VALIGN_TOP: {
  1186. new_sd->ascent = MAX(new_sd->ascent, new_sd->objects[key].rect.size.y);
  1187. } break;
  1188. case VALIGN_CENTER: {
  1189. new_sd->ascent = MAX(new_sd->ascent, Math::round(new_sd->objects[key].rect.size.y / 2));
  1190. new_sd->descent = MAX(new_sd->descent, Math::round(new_sd->objects[key].rect.size.y / 2));
  1191. } break;
  1192. case VALIGN_BOTTOM: {
  1193. new_sd->descent = MAX(new_sd->descent, new_sd->objects[key].rect.size.y);
  1194. } break;
  1195. }
  1196. } else {
  1197. new_sd->objects[key].rect.position.y = new_sd->width;
  1198. new_sd->width += new_sd->objects[key].rect.size.y;
  1199. switch (new_sd->objects[key].inline_align) {
  1200. case VALIGN_TOP: {
  1201. new_sd->ascent = MAX(new_sd->ascent, new_sd->objects[key].rect.size.x);
  1202. } break;
  1203. case VALIGN_CENTER: {
  1204. new_sd->ascent = MAX(new_sd->ascent, Math::round(new_sd->objects[key].rect.size.x / 2));
  1205. new_sd->descent = MAX(new_sd->descent, Math::round(new_sd->objects[key].rect.size.x / 2));
  1206. } break;
  1207. case VALIGN_BOTTOM: {
  1208. new_sd->descent = MAX(new_sd->descent, new_sd->objects[key].rect.size.x);
  1209. } break;
  1210. }
  1211. }
  1212. } else {
  1213. if (prev_rid != gl.font_rid) {
  1214. fd = font_owner.getornull(gl.font_rid);
  1215. prev_rid = gl.font_rid;
  1216. }
  1217. if (fd != nullptr) {
  1218. if (new_sd->orientation == ORIENTATION_HORIZONTAL) {
  1219. new_sd->ascent = MAX(new_sd->ascent, MAX(fd->get_ascent(gl.font_size), -gl.y_off));
  1220. new_sd->descent = MAX(new_sd->descent, MAX(fd->get_descent(gl.font_size), gl.y_off));
  1221. } else {
  1222. new_sd->ascent = MAX(new_sd->ascent, Math::round(fd->get_advance(gl.index, gl.font_size).x * 0.5));
  1223. new_sd->descent = MAX(new_sd->descent, Math::round(fd->get_advance(gl.index, gl.font_size).x * 0.5));
  1224. }
  1225. } else if (new_sd->preserve_invalid || (new_sd->preserve_control && is_control(gl.index))) {
  1226. // Glyph not found, replace with hex code box.
  1227. if (new_sd->orientation == ORIENTATION_HORIZONTAL) {
  1228. new_sd->ascent = MAX(new_sd->ascent, Math::round(get_hex_code_box_size(gl.font_size, gl.index).y * 0.75f));
  1229. new_sd->descent = MAX(new_sd->descent, Math::round(get_hex_code_box_size(gl.font_size, gl.index).y * 0.25f));
  1230. } else {
  1231. new_sd->ascent = MAX(new_sd->ascent, Math::round(get_hex_code_box_size(gl.font_size, gl.index).x * 0.5f));
  1232. new_sd->descent = MAX(new_sd->descent, Math::round(get_hex_code_box_size(gl.font_size, gl.index).x * 0.5f));
  1233. }
  1234. }
  1235. new_sd->width += gl.advance * gl.repeat;
  1236. }
  1237. new_sd->glyphs.push_back(gl);
  1238. }
  1239. }
  1240. }
  1241. }
  1242. // Align embedded objects to baseline.
  1243. for (Map<Variant, ShapedTextData::EmbeddedObject>::Element *E = new_sd->objects.front(); E; E = E->next()) {
  1244. if ((E->get().pos >= new_sd->start) && (E->get().pos < new_sd->end)) {
  1245. if (sd->orientation == ORIENTATION_HORIZONTAL) {
  1246. switch (E->get().inline_align) {
  1247. case VALIGN_TOP: {
  1248. E->get().rect.position.y = -new_sd->ascent;
  1249. } break;
  1250. case VALIGN_CENTER: {
  1251. E->get().rect.position.y = -(E->get().rect.size.y / 2);
  1252. } break;
  1253. case VALIGN_BOTTOM: {
  1254. E->get().rect.position.y = new_sd->descent - E->get().rect.size.y;
  1255. } break;
  1256. }
  1257. } else {
  1258. switch (E->get().inline_align) {
  1259. case VALIGN_TOP: {
  1260. E->get().rect.position.x = -new_sd->ascent;
  1261. } break;
  1262. case VALIGN_CENTER: {
  1263. E->get().rect.position.x = -(E->get().rect.size.x / 2);
  1264. } break;
  1265. case VALIGN_BOTTOM: {
  1266. E->get().rect.position.x = new_sd->descent - E->get().rect.size.x;
  1267. } break;
  1268. }
  1269. }
  1270. }
  1271. }
  1272. }
  1273. new_sd->valid = true;
  1274. return shaped_owner.make_rid(new_sd);
  1275. }
  1276. RID TextServerAdvanced::shaped_text_get_parent(RID p_shaped) const {
  1277. _THREAD_SAFE_METHOD_
  1278. ShapedTextDataAdvanced *sd = shaped_owner.getornull(p_shaped);
  1279. ERR_FAIL_COND_V(!sd, RID());
  1280. return sd->parent;
  1281. }
  1282. float TextServerAdvanced::shaped_text_fit_to_width(RID p_shaped, float p_width, uint8_t /*JustificationFlag*/ p_jst_flags) {
  1283. _THREAD_SAFE_METHOD_
  1284. ShapedTextDataAdvanced *sd = shaped_owner.getornull(p_shaped);
  1285. ERR_FAIL_COND_V(!sd, 0.f);
  1286. if (!sd->valid) {
  1287. const_cast<TextServerAdvanced *>(this)->shaped_text_shape(p_shaped);
  1288. }
  1289. if (!sd->justification_ops_valid) {
  1290. const_cast<TextServerAdvanced *>(this)->shaped_text_update_justification_ops(p_shaped);
  1291. }
  1292. int start_pos = 0;
  1293. int end_pos = sd->glyphs.size() - 1;
  1294. if ((p_jst_flags & JUSTIFICATION_AFTER_LAST_TAB) == JUSTIFICATION_AFTER_LAST_TAB) {
  1295. int start, end, delta;
  1296. if (sd->para_direction == DIRECTION_LTR) {
  1297. start = sd->glyphs.size() - 1;
  1298. end = -1;
  1299. delta = -1;
  1300. } else {
  1301. start = 0;
  1302. end = sd->glyphs.size();
  1303. delta = +1;
  1304. }
  1305. for (int i = start; i != end; i += delta) {
  1306. if ((sd->glyphs[i].flags & GRAPHEME_IS_TAB) == GRAPHEME_IS_TAB) {
  1307. if (sd->para_direction == DIRECTION_LTR) {
  1308. start_pos = i;
  1309. break;
  1310. } else {
  1311. end_pos = i;
  1312. break;
  1313. }
  1314. }
  1315. }
  1316. }
  1317. if ((p_jst_flags & JUSTIFICATION_TRIM_EDGE_SPACES) == JUSTIFICATION_TRIM_EDGE_SPACES) {
  1318. 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)) {
  1319. sd->width -= sd->glyphs[start_pos].advance * sd->glyphs[start_pos].repeat;
  1320. sd->glyphs.write[start_pos].advance = 0;
  1321. start_pos += sd->glyphs[start_pos].count;
  1322. }
  1323. 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)) {
  1324. sd->width -= sd->glyphs[end_pos].advance * sd->glyphs[end_pos].repeat;
  1325. sd->glyphs.write[end_pos].advance = 0;
  1326. end_pos -= sd->glyphs[end_pos].count;
  1327. }
  1328. }
  1329. int space_count = 0;
  1330. int elongation_count = 0;
  1331. for (int i = start_pos; i <= end_pos; i++) {
  1332. const Glyph &gl = sd->glyphs[i];
  1333. if (gl.count > 0) {
  1334. if ((gl.flags & GRAPHEME_IS_ELONGATION) == GRAPHEME_IS_ELONGATION) {
  1335. elongation_count++;
  1336. }
  1337. if ((gl.flags & GRAPHEME_IS_SPACE) == GRAPHEME_IS_SPACE) {
  1338. space_count++;
  1339. }
  1340. }
  1341. }
  1342. if ((elongation_count > 0) && ((p_jst_flags & JUSTIFICATION_KASHIDA) == JUSTIFICATION_KASHIDA)) {
  1343. float delta_width_per_kashida = (p_width - sd->width) / elongation_count;
  1344. for (int i = start_pos; i <= end_pos; i++) {
  1345. Glyph &gl = sd->glyphs.write[i];
  1346. if (gl.count > 0) {
  1347. if (((gl.flags & GRAPHEME_IS_ELONGATION) == GRAPHEME_IS_ELONGATION) && (gl.advance > 0)) {
  1348. int count = delta_width_per_kashida / gl.advance;
  1349. int prev_count = gl.repeat;
  1350. if ((gl.flags & GRAPHEME_IS_VIRTUAL) == GRAPHEME_IS_VIRTUAL) {
  1351. gl.repeat = count;
  1352. } else {
  1353. gl.repeat = count + 1;
  1354. }
  1355. sd->width += (gl.repeat - prev_count) * gl.advance;
  1356. }
  1357. }
  1358. }
  1359. }
  1360. if ((space_count > 0) && ((p_jst_flags & JUSTIFICATION_WORD_BOUND) == JUSTIFICATION_WORD_BOUND)) {
  1361. float delta_width_per_space = (p_width - sd->width) / space_count;
  1362. for (int i = start_pos; i <= end_pos; i++) {
  1363. Glyph &gl = sd->glyphs.write[i];
  1364. if (gl.count > 0) {
  1365. if ((gl.flags & GRAPHEME_IS_SPACE) == GRAPHEME_IS_SPACE) {
  1366. float old_adv = gl.advance;
  1367. if ((gl.flags & GRAPHEME_IS_VIRTUAL) == GRAPHEME_IS_VIRTUAL) {
  1368. gl.advance = Math::round(MAX(gl.advance + delta_width_per_space, 0.f));
  1369. } else {
  1370. gl.advance = Math::round(MAX(gl.advance + delta_width_per_space, 0.05 * gl.font_size));
  1371. }
  1372. sd->width += (gl.advance - old_adv);
  1373. }
  1374. }
  1375. }
  1376. }
  1377. return sd->width;
  1378. }
  1379. float TextServerAdvanced::shaped_text_tab_align(RID p_shaped, const Vector<float> &p_tab_stops) {
  1380. _THREAD_SAFE_METHOD_
  1381. ShapedTextDataAdvanced *sd = shaped_owner.getornull(p_shaped);
  1382. ERR_FAIL_COND_V(!sd, 0.f);
  1383. if (!sd->valid) {
  1384. const_cast<TextServerAdvanced *>(this)->shaped_text_shape(p_shaped);
  1385. }
  1386. if (!sd->line_breaks_valid) {
  1387. const_cast<TextServerAdvanced *>(this)->shaped_text_update_breaks(p_shaped);
  1388. }
  1389. int tab_index = 0;
  1390. float off = 0.f;
  1391. int start, end, delta;
  1392. if (sd->para_direction == DIRECTION_LTR) {
  1393. start = 0;
  1394. end = sd->glyphs.size();
  1395. delta = +1;
  1396. } else {
  1397. start = sd->glyphs.size() - 1;
  1398. end = -1;
  1399. delta = -1;
  1400. }
  1401. Glyph *gl = sd->glyphs.ptrw();
  1402. for (int i = start; i != end; i += delta) {
  1403. if ((gl[i].flags & GRAPHEME_IS_TAB) == GRAPHEME_IS_TAB) {
  1404. float tab_off = 0.f;
  1405. while (tab_off <= off) {
  1406. tab_off += p_tab_stops[tab_index];
  1407. tab_index++;
  1408. if (tab_index >= p_tab_stops.size()) {
  1409. tab_index = 0;
  1410. }
  1411. }
  1412. float old_adv = gl[i].advance;
  1413. gl[i].advance = tab_off - off;
  1414. sd->width += gl[i].advance - old_adv;
  1415. off = 0;
  1416. continue;
  1417. }
  1418. off += gl[i].advance * gl[i].repeat;
  1419. }
  1420. return 0.f;
  1421. }
  1422. bool TextServerAdvanced::shaped_text_update_breaks(RID p_shaped) {
  1423. _THREAD_SAFE_METHOD_
  1424. ShapedTextDataAdvanced *sd = shaped_owner.getornull(p_shaped);
  1425. ERR_FAIL_COND_V(!sd, false);
  1426. if (!sd->valid) {
  1427. shaped_text_shape(p_shaped);
  1428. }
  1429. if (sd->line_breaks_valid) {
  1430. return true; // Noting to do.
  1431. }
  1432. const UChar *data = sd->utf16.ptr();
  1433. HashMap<int, bool> breaks;
  1434. UErrorCode err = U_ZERO_ERROR;
  1435. for (int i = 0; i < sd->spans.size(); i++) {
  1436. UBreakIterator *bi = ubrk_open(UBRK_LINE, sd->spans[i].language.ascii().get_data(), data + _convert_pos_inv(sd, sd->spans[i].start), _convert_pos_inv(sd, sd->spans[i].end - sd->spans[i].start), &err);
  1437. if (U_FAILURE(err)) {
  1438. //No data loaded - use fallback.
  1439. for (int j = sd->spans[i].start; j < sd->spans[i].end; j++) {
  1440. char32_t c = sd->text[j - sd->start];
  1441. if (is_whitespace(c)) {
  1442. breaks[j] = false;
  1443. }
  1444. if (is_linebreak(c)) {
  1445. breaks[j] = true;
  1446. }
  1447. }
  1448. } else {
  1449. while (ubrk_next(bi) != UBRK_DONE) {
  1450. int pos = _convert_pos(sd, ubrk_current(bi)) + sd->spans[i].start - 1;
  1451. if (pos != sd->spans[i].end) {
  1452. if ((ubrk_getRuleStatus(bi) >= UBRK_LINE_HARD) && (ubrk_getRuleStatus(bi) < UBRK_LINE_HARD_LIMIT)) {
  1453. breaks[pos] = true;
  1454. } else if ((ubrk_getRuleStatus(bi) >= UBRK_LINE_SOFT) && (ubrk_getRuleStatus(bi) < UBRK_LINE_SOFT_LIMIT)) {
  1455. breaks[pos] = false;
  1456. }
  1457. }
  1458. }
  1459. }
  1460. ubrk_close(bi);
  1461. }
  1462. sd->sort_valid = false;
  1463. sd->glyphs_logical.clear();
  1464. int sd_size = sd->glyphs.size();
  1465. const char32_t *ch = sd->text.ptr();
  1466. Glyph *sd_glyphs = sd->glyphs.ptrw();
  1467. for (int i = 0; i < sd_size; i++) {
  1468. if (sd_glyphs[i].count > 0) {
  1469. char32_t c = ch[sd_glyphs[i].start - sd->start];
  1470. if (c == 0xfffc) {
  1471. continue;
  1472. }
  1473. if (c == 0x0009 || c == 0x000b) {
  1474. sd_glyphs[i].flags |= GRAPHEME_IS_TAB;
  1475. }
  1476. if (breaks.has(sd->glyphs[i].start)) {
  1477. if (breaks[sd->glyphs[i].start]) {
  1478. sd_glyphs[i].flags |= GRAPHEME_IS_BREAK_HARD;
  1479. } else {
  1480. if (is_whitespace(c)) {
  1481. sd_glyphs[i].flags |= GRAPHEME_IS_BREAK_SOFT;
  1482. sd_glyphs[i].flags |= GRAPHEME_IS_SPACE;
  1483. } else {
  1484. TextServer::Glyph gl;
  1485. gl.start = sd_glyphs[i].start;
  1486. gl.end = sd_glyphs[i].end;
  1487. gl.count = 1;
  1488. gl.font_rid = sd_glyphs[i].font_rid;
  1489. gl.font_size = sd_glyphs[i].font_size;
  1490. gl.flags = GRAPHEME_IS_BREAK_SOFT | GRAPHEME_IS_VIRTUAL;
  1491. sd->glyphs.insert(i + sd_glyphs[i].count, gl); // insert after
  1492. // Update write pointer and size.
  1493. sd_size = sd->glyphs.size();
  1494. sd_glyphs = sd->glyphs.ptrw();
  1495. i += sd_glyphs[i].count;
  1496. continue;
  1497. }
  1498. }
  1499. }
  1500. i += (sd_glyphs[i].count - 1);
  1501. }
  1502. }
  1503. sd->line_breaks_valid = true;
  1504. return sd->line_breaks_valid;
  1505. }
  1506. _FORCE_INLINE_ int _generate_kashida_justification_opportunies(const String &p_data, int p_start, int p_end) {
  1507. int kashida_pos = -1;
  1508. int8_t priority = 100;
  1509. int i = p_start;
  1510. char32_t pc = 0;
  1511. while ((p_end > p_start) && is_transparent(p_data[p_end - 1])) {
  1512. p_end--;
  1513. }
  1514. while (i < p_end) {
  1515. uint32_t c = p_data[i];
  1516. if (c == 0x0640) {
  1517. kashida_pos = i;
  1518. priority = 0;
  1519. }
  1520. if (priority >= 1 && i < p_end - 1) {
  1521. if (is_seen_sad(c) && (p_data[i + 1] != 0x200C)) {
  1522. kashida_pos = i;
  1523. priority = 1;
  1524. }
  1525. }
  1526. if (priority >= 2 && i > p_start) {
  1527. if (is_teh_marbuta(c) || is_dal(c) || (is_heh(c) && i == p_end - 1)) {
  1528. if (is_connected_to_prev(c, pc)) {
  1529. kashida_pos = i - 1;
  1530. priority = 2;
  1531. }
  1532. }
  1533. }
  1534. if (priority >= 3 && i > p_start) {
  1535. if (is_alef(c) || ((is_lam(c) || is_tah(c) || is_kaf(c) || is_gaf(c)) && i == p_end - 1)) {
  1536. if (is_connected_to_prev(c, pc)) {
  1537. kashida_pos = i - 1;
  1538. priority = 3;
  1539. }
  1540. }
  1541. }
  1542. if (priority >= 4 && i > p_start && i < p_end - 1) {
  1543. if (is_beh(c)) {
  1544. if (is_reh(p_data[i + 1]) || is_yeh(p_data[i + 1])) {
  1545. if (is_connected_to_prev(c, pc)) {
  1546. kashida_pos = i - 1;
  1547. priority = 4;
  1548. }
  1549. }
  1550. }
  1551. }
  1552. if (priority >= 5 && i > p_start) {
  1553. if (is_waw(c) || ((is_ain(c) || is_qaf(c) || is_feh(c)) && i == p_end - 1)) {
  1554. if (is_connected_to_prev(c, pc)) {
  1555. kashida_pos = i - 1;
  1556. priority = 5;
  1557. }
  1558. }
  1559. }
  1560. if (priority >= 6 && i > p_start) {
  1561. if (is_reh(c)) {
  1562. if (is_connected_to_prev(c, pc)) {
  1563. kashida_pos = i - 1;
  1564. priority = 6;
  1565. }
  1566. }
  1567. }
  1568. if (!is_transparent(c))
  1569. pc = c;
  1570. i++;
  1571. }
  1572. return kashida_pos;
  1573. }
  1574. bool TextServerAdvanced::shaped_text_update_justification_ops(RID p_shaped) {
  1575. _THREAD_SAFE_METHOD_
  1576. ShapedTextDataAdvanced *sd = shaped_owner.getornull(p_shaped);
  1577. ERR_FAIL_COND_V(!sd, false);
  1578. if (!sd->valid) {
  1579. shaped_text_shape(p_shaped);
  1580. }
  1581. if (!sd->line_breaks_valid) {
  1582. shaped_text_update_breaks(p_shaped);
  1583. }
  1584. const UChar *data = sd->utf16.ptr();
  1585. int32_t data_size = sd->utf16.length();
  1586. Map<int, bool> jstops;
  1587. // Use ICU word iterator and custom kashida detection.
  1588. UErrorCode err = U_ZERO_ERROR;
  1589. UBreakIterator *bi = ubrk_open(UBRK_WORD, "", data, data_size, &err);
  1590. if (U_FAILURE(err)) {
  1591. // No data - use fallback
  1592. int limit = 0;
  1593. for (int i = 0; i < sd->text.length(); i++) {
  1594. if (is_whitespace(data[i])) {
  1595. int ks = _generate_kashida_justification_opportunies(sd->text, limit, i) + sd->start;
  1596. if (ks != -1) {
  1597. jstops[ks] = true;
  1598. }
  1599. limit = i + 1;
  1600. }
  1601. }
  1602. int ks = _generate_kashida_justification_opportunies(sd->text, limit, sd->text.length()) + sd->start;
  1603. if (ks != -1) {
  1604. jstops[ks] = true;
  1605. }
  1606. } else {
  1607. int limit = 0;
  1608. while (ubrk_next(bi) != UBRK_DONE) {
  1609. if (ubrk_getRuleStatus(bi) != UBRK_WORD_NONE) {
  1610. int i = _convert_pos(sd, ubrk_current(bi));
  1611. jstops[i + sd->start] = false;
  1612. int ks = _generate_kashida_justification_opportunies(sd->text, limit, i) + sd->start;
  1613. if (ks != -1) {
  1614. jstops[ks] = true;
  1615. }
  1616. limit = i;
  1617. }
  1618. }
  1619. ubrk_close(bi);
  1620. }
  1621. sd->sort_valid = false;
  1622. sd->glyphs_logical.clear();
  1623. int sd_size = sd->glyphs.size();
  1624. if (jstops.size() > 0) {
  1625. for (int i = 0; i < sd_size; i++) {
  1626. if (sd->glyphs[i].count > 0) {
  1627. if (jstops.has(sd->glyphs[i].start)) {
  1628. char32_t c = sd->text[sd->glyphs[i].start - sd->start];
  1629. if (c == 0xfffc) {
  1630. continue;
  1631. }
  1632. if (jstops[sd->glyphs[i].start]) {
  1633. if (c == 0x0640) {
  1634. sd->glyphs.write[i].flags |= GRAPHEME_IS_ELONGATION;
  1635. } else {
  1636. if (sd->glyphs[i].font_rid != RID()) {
  1637. TextServer::Glyph gl = _shape_single_glyph(sd, 0x0640, HB_SCRIPT_ARABIC, HB_DIRECTION_RTL, sd->glyphs[i].font_rid, sd->glyphs[i].font_size);
  1638. if ((gl.flags & GRAPHEME_IS_VALID) == GRAPHEME_IS_VALID) {
  1639. gl.start = sd->glyphs[i].start;
  1640. gl.end = sd->glyphs[i].end;
  1641. gl.repeat = 0;
  1642. gl.count = 1;
  1643. if (sd->orientation == ORIENTATION_HORIZONTAL) {
  1644. gl.y_off = sd->glyphs[i].y_off;
  1645. } else {
  1646. gl.x_off = sd->glyphs[i].x_off;
  1647. }
  1648. gl.flags |= GRAPHEME_IS_ELONGATION | GRAPHEME_IS_VIRTUAL;
  1649. sd->glyphs.insert(i, gl);
  1650. i++;
  1651. }
  1652. }
  1653. }
  1654. } else if (!is_whitespace(c)) {
  1655. TextServer::Glyph gl;
  1656. gl.start = sd->glyphs[i].start;
  1657. gl.end = sd->glyphs[i].end;
  1658. gl.count = 1;
  1659. gl.font_rid = sd->glyphs[i].font_rid;
  1660. gl.font_size = sd->glyphs[i].font_size;
  1661. gl.flags = GRAPHEME_IS_SPACE | GRAPHEME_IS_VIRTUAL;
  1662. sd->glyphs.insert(i + sd->glyphs[i].count, gl); // insert after
  1663. i += sd->glyphs[i].count;
  1664. continue;
  1665. }
  1666. }
  1667. }
  1668. }
  1669. }
  1670. sd->justification_ops_valid = true;
  1671. return sd->justification_ops_valid;
  1672. }
  1673. 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) {
  1674. FontDataAdvanced *fd = font_owner.getornull(p_font);
  1675. hb_font_t *hb_font = fd->get_hb_handle(p_font_size);
  1676. hb_buffer_clear_contents(p_sd->hb_buffer);
  1677. hb_buffer_set_direction(p_sd->hb_buffer, p_direction);
  1678. hb_buffer_set_flags(p_sd->hb_buffer, (hb_buffer_flags_t)(HB_BUFFER_FLAG_DEFAULT));
  1679. hb_buffer_set_script(p_sd->hb_buffer, p_script);
  1680. hb_buffer_add_utf32(p_sd->hb_buffer, (const uint32_t *)&p_char, 1, 0, 1);
  1681. hb_shape(hb_font, p_sd->hb_buffer, nullptr, 0);
  1682. unsigned int glyph_count = 0;
  1683. hb_glyph_info_t *glyph_info = hb_buffer_get_glyph_infos(p_sd->hb_buffer, &glyph_count);
  1684. hb_glyph_position_t *glyph_pos = hb_buffer_get_glyph_positions(p_sd->hb_buffer, &glyph_count);
  1685. // Process glyphs.
  1686. TextServer::Glyph gl;
  1687. if (p_direction == HB_DIRECTION_RTL || p_direction == HB_DIRECTION_BTT) {
  1688. gl.flags |= TextServer::GRAPHEME_IS_RTL;
  1689. }
  1690. gl.font_rid = p_font;
  1691. gl.font_size = p_font_size;
  1692. if (glyph_count > 0) {
  1693. if (p_sd->orientation == ORIENTATION_HORIZONTAL) {
  1694. gl.advance = Math::round(glyph_pos[0].x_advance / (64.0 / fd->get_font_scale(p_font_size)));
  1695. } else {
  1696. gl.advance = -Math::round(glyph_pos[0].y_advance / (64.0 / fd->get_font_scale(p_font_size)));
  1697. }
  1698. gl.count = 1;
  1699. gl.index = glyph_info[0].codepoint;
  1700. gl.x_off = Math::round(glyph_pos[0].x_offset / (64.0 / fd->get_font_scale(p_font_size)));
  1701. gl.y_off = -Math::round(glyph_pos[0].y_offset / (64.0 / fd->get_font_scale(p_font_size)));
  1702. if ((glyph_info[0].codepoint != 0) || !u_isgraph(p_char)) {
  1703. gl.flags |= GRAPHEME_IS_VALID;
  1704. }
  1705. }
  1706. return gl;
  1707. }
  1708. 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) {
  1709. FontDataAdvanced *fd = nullptr;
  1710. if (p_fb_index < p_fonts.size()) {
  1711. fd = font_owner.getornull(p_fonts[p_fb_index]);
  1712. }
  1713. int fs = p_sd->spans[p_span].font_size;
  1714. if (fd == nullptr) {
  1715. // Add fallback glyphs
  1716. for (int i = p_start; i < p_end; i++) {
  1717. if (p_sd->preserve_invalid || (p_sd->preserve_control && is_control(p_sd->text[i]))) {
  1718. TextServer::Glyph gl;
  1719. gl.start = i;
  1720. gl.end = i + 1;
  1721. gl.count = 1;
  1722. gl.index = p_sd->text[i];
  1723. gl.font_size = fs;
  1724. gl.font_rid = RID();
  1725. if (p_direction == HB_DIRECTION_RTL || p_direction == HB_DIRECTION_BTT) {
  1726. gl.flags |= TextServer::GRAPHEME_IS_RTL;
  1727. }
  1728. if (p_sd->orientation == ORIENTATION_HORIZONTAL) {
  1729. gl.advance = get_hex_code_box_size(fs, gl.index).x;
  1730. p_sd->ascent = MAX(p_sd->ascent, Math::round(get_hex_code_box_size(fs, gl.index).y * 0.75f));
  1731. p_sd->descent = MAX(p_sd->descent, Math::round(get_hex_code_box_size(fs, gl.index).y * 0.25f));
  1732. } else {
  1733. gl.advance = get_hex_code_box_size(fs, gl.index).y;
  1734. p_sd->ascent = MAX(p_sd->ascent, Math::round(get_hex_code_box_size(fs, gl.index).x * 0.5f));
  1735. p_sd->descent = MAX(p_sd->descent, Math::round(get_hex_code_box_size(fs, gl.index).x * 0.5f));
  1736. }
  1737. p_sd->width += gl.advance;
  1738. p_sd->glyphs.push_back(gl);
  1739. }
  1740. }
  1741. return;
  1742. }
  1743. hb_font_t *hb_font = fd->get_hb_handle(fs);
  1744. ERR_FAIL_COND(hb_font == nullptr);
  1745. hb_buffer_clear_contents(p_sd->hb_buffer);
  1746. hb_buffer_set_direction(p_sd->hb_buffer, p_direction);
  1747. if (p_sd->preserve_control) {
  1748. 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)));
  1749. } else {
  1750. 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)));
  1751. }
  1752. hb_buffer_set_script(p_sd->hb_buffer, p_script);
  1753. if (p_sd->spans[p_span].language != String()) {
  1754. hb_language_t lang = hb_language_from_string(p_sd->spans[p_span].language.ascii().get_data(), -1);
  1755. hb_buffer_set_language(p_sd->hb_buffer, lang);
  1756. }
  1757. 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);
  1758. Vector<hb_feature_t> ftrs;
  1759. for (const Variant *ftr = p_sd->spans[p_span].features.next(nullptr); ftr != nullptr; ftr = p_sd->spans[p_span].features.next(ftr)) {
  1760. double values = p_sd->spans[p_span].features[*ftr];
  1761. if (values >= 0) {
  1762. hb_feature_t feature;
  1763. feature.tag = *ftr;
  1764. feature.value = values;
  1765. feature.start = 0;
  1766. feature.end = -1;
  1767. ftrs.push_back(feature);
  1768. }
  1769. }
  1770. hb_shape(hb_font, p_sd->hb_buffer, ftrs.empty() ? nullptr : &ftrs[0], ftrs.size());
  1771. unsigned int glyph_count = 0;
  1772. hb_glyph_info_t *glyph_info = hb_buffer_get_glyph_infos(p_sd->hb_buffer, &glyph_count);
  1773. hb_glyph_position_t *glyph_pos = hb_buffer_get_glyph_positions(p_sd->hb_buffer, &glyph_count);
  1774. // Process glyphs.
  1775. if (glyph_count > 0) {
  1776. TextServer::Glyph *w = (TextServer::Glyph *)memalloc(glyph_count * sizeof(TextServer::Glyph));
  1777. int end = (p_direction == HB_DIRECTION_RTL || p_direction == HB_DIRECTION_BTT) ? p_end : 0;
  1778. uint32_t last_cluster_id = UINT32_MAX;
  1779. unsigned int last_cluster_index = 0;
  1780. bool last_cluster_valid = true;
  1781. for (unsigned int i = 0; i < glyph_count; i++) {
  1782. if ((i > 0) && (last_cluster_id != glyph_info[i].cluster)) {
  1783. if (p_direction == HB_DIRECTION_RTL || p_direction == HB_DIRECTION_BTT) {
  1784. end = w[last_cluster_index].start;
  1785. } else {
  1786. for (unsigned int j = last_cluster_index; j < i; j++) {
  1787. w[j].end = glyph_info[i].cluster;
  1788. }
  1789. }
  1790. if (p_direction == HB_DIRECTION_RTL || p_direction == HB_DIRECTION_BTT) {
  1791. w[last_cluster_index].flags |= TextServer::GRAPHEME_IS_RTL;
  1792. }
  1793. if (last_cluster_valid) {
  1794. w[last_cluster_index].flags |= GRAPHEME_IS_VALID;
  1795. }
  1796. w[last_cluster_index].count = i - last_cluster_index;
  1797. last_cluster_index = i;
  1798. last_cluster_valid = true;
  1799. }
  1800. last_cluster_id = glyph_info[i].cluster;
  1801. TextServer::Glyph &gl = w[i];
  1802. gl = TextServer::Glyph();
  1803. gl.start = glyph_info[i].cluster;
  1804. gl.end = end;
  1805. gl.count = 0;
  1806. gl.font_rid = p_fonts[p_fb_index];
  1807. gl.font_size = fs;
  1808. gl.index = glyph_info[i].codepoint;
  1809. if (gl.index != 0) {
  1810. if (p_sd->orientation == ORIENTATION_HORIZONTAL) {
  1811. gl.advance = Math::round(glyph_pos[i].x_advance / (64.0 / fd->get_font_scale(fs)));
  1812. } else {
  1813. gl.advance = -Math::round(glyph_pos[i].y_advance / (64.0 / fd->get_font_scale(fs)));
  1814. }
  1815. gl.x_off = Math::round(glyph_pos[i].x_offset / (64.0 / fd->get_font_scale(fs)));
  1816. gl.y_off = -Math::round(glyph_pos[i].y_offset / (64.0 / fd->get_font_scale(fs)));
  1817. }
  1818. if (p_sd->preserve_control) {
  1819. 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]));
  1820. } else {
  1821. last_cluster_valid = last_cluster_valid && ((glyph_info[i].codepoint != 0) || !u_isgraph(p_sd->text[glyph_info[i].cluster]));
  1822. }
  1823. }
  1824. if (p_direction == HB_DIRECTION_LTR || p_direction == HB_DIRECTION_TTB) {
  1825. for (unsigned int j = last_cluster_index; j < glyph_count; j++) {
  1826. w[j].end = p_end;
  1827. }
  1828. }
  1829. w[last_cluster_index].count = glyph_count - last_cluster_index;
  1830. if (p_direction == HB_DIRECTION_RTL || p_direction == HB_DIRECTION_BTT) {
  1831. w[last_cluster_index].flags |= TextServer::GRAPHEME_IS_RTL;
  1832. }
  1833. if (last_cluster_valid) {
  1834. w[last_cluster_index].flags |= GRAPHEME_IS_VALID;
  1835. }
  1836. //Fallback.
  1837. int failed_subrun_start = p_end + 1;
  1838. int failed_subrun_end = p_start;
  1839. for (unsigned int i = 0; i < glyph_count; i++) {
  1840. if ((w[i].flags & GRAPHEME_IS_VALID) == GRAPHEME_IS_VALID) {
  1841. if (failed_subrun_start != p_end + 1) {
  1842. _shape_run(p_sd, failed_subrun_start, failed_subrun_end, p_script, p_direction, p_fonts, p_span, p_fb_index + 1);
  1843. failed_subrun_start = p_end + 1;
  1844. failed_subrun_end = p_start;
  1845. }
  1846. for (int j = 0; j < w[i].count; j++) {
  1847. if (p_sd->orientation == ORIENTATION_HORIZONTAL) {
  1848. p_sd->ascent = MAX(p_sd->ascent, -w[i + j].y_off);
  1849. p_sd->descent = MAX(p_sd->descent, w[i + j].y_off);
  1850. } else {
  1851. p_sd->ascent = MAX(p_sd->ascent, Math::round(fd->get_advance(w[i + j].index, fs).x * 0.5));
  1852. p_sd->descent = MAX(p_sd->descent, Math::round(fd->get_advance(w[i + j].index, fs).x * 0.5));
  1853. }
  1854. p_sd->width += w[i + j].advance;
  1855. p_sd->glyphs.push_back(w[i + j]);
  1856. }
  1857. } else {
  1858. if (failed_subrun_start >= w[i].start) {
  1859. failed_subrun_start = w[i].start;
  1860. }
  1861. if (failed_subrun_end <= w[i].end) {
  1862. failed_subrun_end = w[i].end;
  1863. }
  1864. }
  1865. i += w[i].count - 1;
  1866. }
  1867. memfree(w);
  1868. if (failed_subrun_start != p_end + 1) {
  1869. _shape_run(p_sd, failed_subrun_start, failed_subrun_end, p_script, p_direction, p_fonts, p_span, p_fb_index + 1);
  1870. }
  1871. p_sd->ascent = MAX(p_sd->ascent, fd->get_ascent(fs));
  1872. p_sd->descent = MAX(p_sd->descent, fd->get_descent(fs));
  1873. p_sd->upos = MAX(p_sd->upos, fd->get_underline_position(fs));
  1874. p_sd->uthk = MAX(p_sd->uthk, fd->get_underline_thickness(fs));
  1875. }
  1876. }
  1877. bool TextServerAdvanced::shaped_text_shape(RID p_shaped) {
  1878. _THREAD_SAFE_METHOD_
  1879. ShapedTextDataAdvanced *sd = shaped_owner.getornull(p_shaped);
  1880. ERR_FAIL_COND_V(!sd, false);
  1881. if (sd->valid) {
  1882. return true;
  1883. }
  1884. if (sd->parent != RID()) {
  1885. full_copy(sd);
  1886. }
  1887. invalidate(sd);
  1888. if (sd->text.length() == 0) {
  1889. sd->valid = true;
  1890. return true;
  1891. }
  1892. sd->utf16 = sd->text.utf16();
  1893. const UChar *data = sd->utf16.ptr();
  1894. // Create script iterator.
  1895. if (sd->script_iter == nullptr) {
  1896. sd->script_iter = memnew(ScriptIterator(sd->text, 0, sd->text.length()));
  1897. }
  1898. if (sd->bidi_override.empty()) {
  1899. sd->bidi_override.push_back(Vector2i(0, sd->end));
  1900. }
  1901. for (int ov = 0; ov < sd->bidi_override.size(); ov++) {
  1902. // Create BiDi iterator.
  1903. int start = _convert_pos_inv(sd, sd->bidi_override[ov].x);
  1904. int end = _convert_pos_inv(sd, sd->bidi_override[ov].y);
  1905. ERR_FAIL_COND_V_MSG((start < 0 || end - start > sd->utf16.length()), false, "Invalid BiDi override range.");
  1906. UErrorCode err = U_ZERO_ERROR;
  1907. UBiDi *bidi_iter = ubidi_openSized(end, 0, &err);
  1908. ERR_FAIL_COND_V_MSG(U_FAILURE(err), false, u_errorName(err));
  1909. switch (sd->direction) {
  1910. case DIRECTION_LTR: {
  1911. ubidi_setPara(bidi_iter, data + start, end - start, UBIDI_LTR, nullptr, &err);
  1912. sd->para_direction = DIRECTION_LTR;
  1913. } break;
  1914. case DIRECTION_RTL: {
  1915. ubidi_setPara(bidi_iter, data + start, end - start, UBIDI_RTL, nullptr, &err);
  1916. sd->para_direction = DIRECTION_RTL;
  1917. } break;
  1918. case DIRECTION_AUTO: {
  1919. UBiDiDirection direction = ubidi_getBaseDirection(data + start, end - start);
  1920. if (direction != UBIDI_NEUTRAL) {
  1921. ubidi_setPara(bidi_iter, data + start, end - start, direction, nullptr, &err);
  1922. sd->para_direction = (direction == UBIDI_RTL) ? DIRECTION_RTL : DIRECTION_LTR;
  1923. } else {
  1924. ubidi_setPara(bidi_iter, data + start, end - start, UBIDI_DEFAULT_LTR, nullptr, &err);
  1925. sd->para_direction = DIRECTION_LTR;
  1926. }
  1927. } break;
  1928. }
  1929. ERR_FAIL_COND_V_MSG(U_FAILURE(err), false, u_errorName(err));
  1930. sd->bidi_iter.push_back(bidi_iter);
  1931. err = U_ZERO_ERROR;
  1932. int bidi_run_count = ubidi_countRuns(bidi_iter, &err);
  1933. ERR_FAIL_COND_V_MSG(U_FAILURE(err), false, u_errorName(err));
  1934. for (int i = 0; i < bidi_run_count; i++) {
  1935. int32_t _bidi_run_start = 0;
  1936. int32_t _bidi_run_length = 0;
  1937. hb_direction_t bidi_run_direction = HB_DIRECTION_INVALID;
  1938. bool is_rtl = (ubidi_getVisualRun(bidi_iter, i, &_bidi_run_start, &_bidi_run_length) == UBIDI_LTR);
  1939. switch (sd->orientation) {
  1940. case ORIENTATION_HORIZONTAL: {
  1941. if (is_rtl) {
  1942. bidi_run_direction = HB_DIRECTION_LTR;
  1943. } else {
  1944. bidi_run_direction = HB_DIRECTION_RTL;
  1945. }
  1946. } break;
  1947. case ORIENTATION_VERTICAL: {
  1948. if (is_rtl) {
  1949. bidi_run_direction = HB_DIRECTION_TTB;
  1950. } else {
  1951. bidi_run_direction = HB_DIRECTION_BTT;
  1952. }
  1953. }
  1954. }
  1955. int32_t bidi_run_start = _convert_pos(sd, sd->bidi_override[ov].x + _bidi_run_start);
  1956. int32_t bidi_run_end = _convert_pos(sd, sd->bidi_override[ov].x + _bidi_run_start + _bidi_run_length);
  1957. // Shape runs.
  1958. int scr_from = (is_rtl) ? 0 : sd->script_iter->script_ranges.size() - 1;
  1959. int scr_to = (is_rtl) ? sd->script_iter->script_ranges.size() : -1;
  1960. int scr_delta = (is_rtl) ? +1 : -1;
  1961. for (int j = scr_from; j != scr_to; j += scr_delta) {
  1962. if ((sd->script_iter->script_ranges[j].start < bidi_run_end) && (sd->script_iter->script_ranges[j].end > bidi_run_start)) {
  1963. int32_t script_run_start = MAX(sd->script_iter->script_ranges[j].start, bidi_run_start);
  1964. int32_t script_run_end = MIN(sd->script_iter->script_ranges[j].end, bidi_run_end);
  1965. char scr_buffer[5] = { 0, 0, 0, 0, 0 };
  1966. hb_tag_to_string(hb_script_to_iso15924_tag(sd->script_iter->script_ranges[j].script), scr_buffer);
  1967. String script = String(scr_buffer);
  1968. int spn_from = (is_rtl) ? 0 : sd->spans.size() - 1;
  1969. int spn_to = (is_rtl) ? sd->spans.size() : -1;
  1970. int spn_delta = (is_rtl) ? +1 : -1;
  1971. for (int k = spn_from; k != spn_to; k += spn_delta) {
  1972. const ShapedTextDataAdvanced::Span &span = sd->spans[k];
  1973. if (span.start >= script_run_end || span.end <= script_run_start) {
  1974. continue;
  1975. }
  1976. if (span.embedded_key != Variant()) {
  1977. // Embedded object.
  1978. if (sd->orientation == ORIENTATION_HORIZONTAL) {
  1979. sd->objects[span.embedded_key].rect.position.x = sd->width;
  1980. sd->width += sd->objects[span.embedded_key].rect.size.x;
  1981. switch (sd->objects[span.embedded_key].inline_align) {
  1982. case VALIGN_TOP: {
  1983. sd->ascent = MAX(sd->ascent, sd->objects[span.embedded_key].rect.size.y);
  1984. } break;
  1985. case VALIGN_CENTER: {
  1986. sd->ascent = MAX(sd->ascent, Math::round(sd->objects[span.embedded_key].rect.size.y / 2));
  1987. sd->descent = MAX(sd->descent, Math::round(sd->objects[span.embedded_key].rect.size.y / 2));
  1988. } break;
  1989. case VALIGN_BOTTOM: {
  1990. sd->descent = MAX(sd->descent, sd->objects[span.embedded_key].rect.size.y);
  1991. } break;
  1992. }
  1993. } else {
  1994. sd->objects[span.embedded_key].rect.position.y = sd->width;
  1995. sd->width += sd->objects[span.embedded_key].rect.size.y;
  1996. switch (sd->objects[span.embedded_key].inline_align) {
  1997. case VALIGN_TOP: {
  1998. sd->ascent = MAX(sd->ascent, sd->objects[span.embedded_key].rect.size.x);
  1999. } break;
  2000. case VALIGN_CENTER: {
  2001. sd->ascent = MAX(sd->ascent, Math::round(sd->objects[span.embedded_key].rect.size.x / 2));
  2002. sd->descent = MAX(sd->descent, Math::round(sd->objects[span.embedded_key].rect.size.x / 2));
  2003. } break;
  2004. case VALIGN_BOTTOM: {
  2005. sd->descent = MAX(sd->descent, sd->objects[span.embedded_key].rect.size.x);
  2006. } break;
  2007. }
  2008. }
  2009. Glyph gl;
  2010. gl.start = span.start;
  2011. gl.end = span.end;
  2012. gl.count = 1;
  2013. gl.flags = GRAPHEME_IS_VALID | GRAPHEME_IS_VIRTUAL;
  2014. if (sd->orientation == ORIENTATION_HORIZONTAL) {
  2015. gl.advance = sd->objects[span.embedded_key].rect.size.x;
  2016. } else {
  2017. gl.advance = sd->objects[span.embedded_key].rect.size.y;
  2018. }
  2019. sd->glyphs.push_back(gl);
  2020. } else {
  2021. Vector<RID> fonts;
  2022. // Push fonts with the language and script support first.
  2023. for (int l = 0; l < span.fonts.size(); l++) {
  2024. if ((font_is_language_supported(span.fonts[l], span.language)) && (font_is_script_supported(span.fonts[l], script))) {
  2025. fonts.push_back(sd->spans[k].fonts[l]);
  2026. }
  2027. }
  2028. // Push fonts with the script support.
  2029. for (int l = 0; l < sd->spans[k].fonts.size(); l++) {
  2030. if (!(font_is_language_supported(span.fonts[l], span.language)) && (font_is_script_supported(span.fonts[l], script))) {
  2031. fonts.push_back(sd->spans[k].fonts[l]);
  2032. }
  2033. }
  2034. // Push the rest valid fonts.
  2035. for (int l = 0; l < sd->spans[k].fonts.size(); l++) {
  2036. if (!(font_is_language_supported(span.fonts[l], span.language)) && !(font_is_script_supported(span.fonts[l], script))) {
  2037. fonts.push_back(sd->spans[k].fonts[l]);
  2038. }
  2039. }
  2040. _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);
  2041. }
  2042. }
  2043. }
  2044. }
  2045. }
  2046. }
  2047. // Align embedded objects to baseline.
  2048. for (Map<Variant, ShapedTextData::EmbeddedObject>::Element *E = sd->objects.front(); E; E = E->next()) {
  2049. if (sd->orientation == ORIENTATION_HORIZONTAL) {
  2050. switch (E->get().inline_align) {
  2051. case VALIGN_TOP: {
  2052. E->get().rect.position.y = -sd->ascent;
  2053. } break;
  2054. case VALIGN_CENTER: {
  2055. E->get().rect.position.y = -(E->get().rect.size.y / 2);
  2056. } break;
  2057. case VALIGN_BOTTOM: {
  2058. E->get().rect.position.y = sd->descent - E->get().rect.size.y;
  2059. } break;
  2060. }
  2061. } else {
  2062. switch (E->get().inline_align) {
  2063. case VALIGN_TOP: {
  2064. E->get().rect.position.x = -sd->ascent;
  2065. } break;
  2066. case VALIGN_CENTER: {
  2067. E->get().rect.position.x = -(E->get().rect.size.x / 2);
  2068. } break;
  2069. case VALIGN_BOTTOM: {
  2070. E->get().rect.position.x = sd->descent - E->get().rect.size.x;
  2071. } break;
  2072. }
  2073. }
  2074. }
  2075. sd->valid = true;
  2076. return sd->valid;
  2077. }
  2078. bool TextServerAdvanced::shaped_text_is_ready(RID p_shaped) const {
  2079. _THREAD_SAFE_METHOD_
  2080. const ShapedTextDataAdvanced *sd = shaped_owner.getornull(p_shaped);
  2081. ERR_FAIL_COND_V(!sd, false);
  2082. return sd->valid;
  2083. }
  2084. Vector<TextServer::Glyph> TextServerAdvanced::shaped_text_get_glyphs(RID p_shaped) const {
  2085. _THREAD_SAFE_METHOD_
  2086. const ShapedTextDataAdvanced *sd = shaped_owner.getornull(p_shaped);
  2087. ERR_FAIL_COND_V(!sd, Vector<TextServer::Glyph>());
  2088. if (!sd->valid) {
  2089. const_cast<TextServerAdvanced *>(this)->shaped_text_shape(p_shaped);
  2090. }
  2091. return sd->glyphs;
  2092. }
  2093. Vector2i TextServerAdvanced::shaped_text_get_range(RID p_shaped) const {
  2094. _THREAD_SAFE_METHOD_
  2095. const ShapedTextDataAdvanced *sd = shaped_owner.getornull(p_shaped);
  2096. ERR_FAIL_COND_V(!sd, Vector2i());
  2097. return Vector2(sd->start, sd->end);
  2098. }
  2099. Vector<TextServer::Glyph> TextServerAdvanced::shaped_text_sort_logical(RID p_shaped) {
  2100. _THREAD_SAFE_METHOD_
  2101. ShapedTextDataAdvanced *sd = shaped_owner.getornull(p_shaped);
  2102. ERR_FAIL_COND_V(!sd, Vector<TextServer::Glyph>());
  2103. if (!sd->valid) {
  2104. const_cast<TextServerAdvanced *>(this)->shaped_text_shape(p_shaped);
  2105. }
  2106. if (!sd->sort_valid) {
  2107. sd->glyphs_logical = sd->glyphs;
  2108. sd->glyphs_logical.sort_custom<TextServer::GlyphCompare>();
  2109. sd->sort_valid = true;
  2110. }
  2111. return sd->glyphs_logical;
  2112. }
  2113. Array TextServerAdvanced::shaped_text_get_objects(RID p_shaped) const {
  2114. _THREAD_SAFE_METHOD_
  2115. Array ret;
  2116. const ShapedTextDataAdvanced *sd = shaped_owner.getornull(p_shaped);
  2117. ERR_FAIL_COND_V(!sd, ret);
  2118. for (const Map<Variant, ShapedTextData::EmbeddedObject>::Element *E = sd->objects.front(); E; E = E->next()) {
  2119. ret.push_back(E->key());
  2120. }
  2121. return ret;
  2122. }
  2123. Rect2 TextServerAdvanced::shaped_text_get_object_rect(RID p_shaped, Variant p_key) const {
  2124. _THREAD_SAFE_METHOD_
  2125. const ShapedTextDataAdvanced *sd = shaped_owner.getornull(p_shaped);
  2126. ERR_FAIL_COND_V(!sd, Rect2());
  2127. ERR_FAIL_COND_V(!sd->objects.has(p_key), Rect2());
  2128. if (!sd->valid) {
  2129. const_cast<TextServerAdvanced *>(this)->shaped_text_shape(p_shaped);
  2130. }
  2131. return sd->objects[p_key].rect;
  2132. }
  2133. Size2 TextServerAdvanced::shaped_text_get_size(RID p_shaped) const {
  2134. _THREAD_SAFE_METHOD_
  2135. const ShapedTextDataAdvanced *sd = shaped_owner.getornull(p_shaped);
  2136. ERR_FAIL_COND_V(!sd, Size2());
  2137. if (!sd->valid) {
  2138. const_cast<TextServerAdvanced *>(this)->shaped_text_shape(p_shaped);
  2139. }
  2140. if (sd->orientation == TextServer::ORIENTATION_HORIZONTAL) {
  2141. return Size2(sd->width, sd->ascent + sd->descent);
  2142. } else {
  2143. return Size2(sd->ascent + sd->descent, sd->width);
  2144. }
  2145. }
  2146. float TextServerAdvanced::shaped_text_get_ascent(RID p_shaped) const {
  2147. _THREAD_SAFE_METHOD_
  2148. const ShapedTextDataAdvanced *sd = shaped_owner.getornull(p_shaped);
  2149. ERR_FAIL_COND_V(!sd, 0.f);
  2150. if (!sd->valid) {
  2151. const_cast<TextServerAdvanced *>(this)->shaped_text_shape(p_shaped);
  2152. }
  2153. return sd->ascent;
  2154. }
  2155. float TextServerAdvanced::shaped_text_get_descent(RID p_shaped) const {
  2156. _THREAD_SAFE_METHOD_
  2157. const ShapedTextDataAdvanced *sd = shaped_owner.getornull(p_shaped);
  2158. ERR_FAIL_COND_V(!sd, 0.f);
  2159. if (!sd->valid) {
  2160. const_cast<TextServerAdvanced *>(this)->shaped_text_shape(p_shaped);
  2161. }
  2162. return sd->descent;
  2163. }
  2164. float TextServerAdvanced::shaped_text_get_width(RID p_shaped) const {
  2165. _THREAD_SAFE_METHOD_
  2166. const ShapedTextDataAdvanced *sd = shaped_owner.getornull(p_shaped);
  2167. ERR_FAIL_COND_V(!sd, 0.f);
  2168. if (!sd->valid) {
  2169. const_cast<TextServerAdvanced *>(this)->shaped_text_shape(p_shaped);
  2170. }
  2171. return sd->width;
  2172. }
  2173. float TextServerAdvanced::shaped_text_get_underline_position(RID p_shaped) const {
  2174. _THREAD_SAFE_METHOD_
  2175. const ShapedTextDataAdvanced *sd = shaped_owner.getornull(p_shaped);
  2176. ERR_FAIL_COND_V(!sd, 0.f);
  2177. if (!sd->valid) {
  2178. const_cast<TextServerAdvanced *>(this)->shaped_text_shape(p_shaped);
  2179. }
  2180. return sd->upos;
  2181. }
  2182. float TextServerAdvanced::shaped_text_get_underline_thickness(RID p_shaped) const {
  2183. _THREAD_SAFE_METHOD_
  2184. const ShapedTextDataAdvanced *sd = shaped_owner.getornull(p_shaped);
  2185. ERR_FAIL_COND_V(!sd, 0.f);
  2186. if (!sd->valid) {
  2187. const_cast<TextServerAdvanced *>(this)->shaped_text_shape(p_shaped);
  2188. }
  2189. return sd->uthk;
  2190. }
  2191. struct num_system_data {
  2192. String lang;
  2193. String digits;
  2194. String percent_sign;
  2195. String exp;
  2196. };
  2197. static num_system_data num_systems[]{
  2198. { "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"اس" },
  2199. { "fa,ks,pa_Arab,ps,ug,ur_IN,ur,uz_Arab", U"۰۱۲۳۴۵۶۷۸۹٫", U"٪", U"اس" },
  2200. { "as,bn,mni", U"০১২৩৪৫৬৭৮৯.", U"%", U"e" },
  2201. { "mr,ne", U"०१२३४५६७८९.", U"%", U"e" },
  2202. { "dz", U"༠༡༢༣༤༥༦༧༨༩.", U"%", U"e" },
  2203. { "sat", U"᱐᱑᱒᱓᱔᱕᱖᱗᱘᱙.", U"%", U"e" },
  2204. { "my", U"၀၁၂၃၄၅၆၇၈၉.", U"%", U"e" },
  2205. { String(), String(), String(), String() },
  2206. };
  2207. String TextServerAdvanced::format_number(const String &p_string, const String &p_language) const {
  2208. _THREAD_SAFE_METHOD_
  2209. String lang = (p_language == "") ? TranslationServer::get_singleton()->get_tool_locale() : p_language;
  2210. String res = p_string;
  2211. for (int i = 0; num_systems[i].lang != String(); i++) {
  2212. Vector<String> langs = num_systems[i].lang.split(",");
  2213. if (langs.has(lang)) {
  2214. if (num_systems[i].digits == String()) {
  2215. return p_string;
  2216. }
  2217. res.replace("e", num_systems[i].exp);
  2218. res.replace("E", num_systems[i].exp);
  2219. char32_t *data = res.ptrw();
  2220. for (int j = 0; j < res.size(); j++) {
  2221. if (data[j] >= 0x30 && data[j] <= 0x39) {
  2222. data[j] = num_systems[i].digits[data[j] - 0x30];
  2223. } else if (data[j] == '.' || data[j] == ',') {
  2224. data[j] = num_systems[i].digits[10];
  2225. }
  2226. }
  2227. break;
  2228. }
  2229. }
  2230. return res;
  2231. }
  2232. String TextServerAdvanced::parse_number(const String &p_string, const String &p_language) const {
  2233. _THREAD_SAFE_METHOD_
  2234. String lang = (p_language == "") ? TranslationServer::get_singleton()->get_tool_locale() : p_language;
  2235. String res = p_string;
  2236. for (int i = 0; num_systems[i].lang != String(); i++) {
  2237. Vector<String> langs = num_systems[i].lang.split(",");
  2238. if (langs.has(lang)) {
  2239. if (num_systems[i].digits == String()) {
  2240. return p_string;
  2241. }
  2242. res.replace(num_systems[i].exp, "e");
  2243. char32_t *data = res.ptrw();
  2244. for (int j = 0; j < res.size(); j++) {
  2245. if (data[j] == num_systems[i].digits[10]) {
  2246. data[j] = '.';
  2247. } else {
  2248. for (int k = 0; k < 10; k++) {
  2249. if (data[j] == num_systems[i].digits[k]) {
  2250. data[j] = 0x30 + k;
  2251. }
  2252. }
  2253. }
  2254. }
  2255. break;
  2256. }
  2257. }
  2258. return res;
  2259. }
  2260. String TextServerAdvanced::percent_sign(const String &p_language) const {
  2261. _THREAD_SAFE_METHOD_
  2262. String lang = (p_language == "") ? TranslationServer::get_singleton()->get_tool_locale() : p_language;
  2263. for (int i = 0; num_systems[i].lang != String(); i++) {
  2264. Vector<String> langs = num_systems[i].lang.split(",");
  2265. if (langs.has(lang)) {
  2266. if (num_systems[i].percent_sign == String()) {
  2267. return "%";
  2268. }
  2269. return num_systems[i].percent_sign;
  2270. }
  2271. }
  2272. return "%";
  2273. }
  2274. TextServer *TextServerAdvanced::create_func(Error &r_error, void *p_user_data) {
  2275. r_error = OK;
  2276. return memnew(TextServerAdvanced());
  2277. }
  2278. void TextServerAdvanced::register_server() {
  2279. TextServerManager::register_create_function(interface_name, interface_features, create_func, nullptr);
  2280. }
  2281. TextServerAdvanced::TextServerAdvanced() {
  2282. hb_bmp_create_font_funcs();
  2283. }
  2284. TextServerAdvanced::~TextServerAdvanced() {
  2285. hb_bmp_free_font_funcs();
  2286. u_cleanup();
  2287. #ifndef ICU_STATIC_DATA
  2288. if (icu_data != nullptr) {
  2289. memfree(icu_data);
  2290. icu_data = nullptr;
  2291. }
  2292. #endif
  2293. }