text_server_fb.cpp 109 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399
  1. /*************************************************************************/
  2. /* text_server_fb.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "text_server_fb.h"
  31. #include "core/error/error_macros.h"
  32. #include "core/string/print_string.h"
  33. #include "core/string/ucaps.h"
  34. #include "modules/modules_enabled.gen.h" // For freetype, msdfgen.
  35. #ifdef MODULE_MSDFGEN_ENABLED
  36. #include "core/ShapeDistanceFinder.h"
  37. #include "core/contour-combiners.h"
  38. #include "core/edge-selectors.h"
  39. #include "msdfgen.h"
  40. #endif
  41. /*************************************************************************/
  42. /* Character properties. */
  43. /*************************************************************************/
  44. _FORCE_INLINE_ bool is_control(char32_t p_char) {
  45. return (p_char <= 0x001f) || (p_char >= 0x007f && p_char <= 0x009F);
  46. }
  47. _FORCE_INLINE_ bool is_whitespace(char32_t p_char) {
  48. 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);
  49. }
  50. _FORCE_INLINE_ bool is_linebreak(char32_t p_char) {
  51. return (p_char >= 0x000a && p_char <= 0x000d) || (p_char == 0x0085) || (p_char == 0x2028) || (p_char == 0x2029);
  52. }
  53. _FORCE_INLINE_ bool is_punct(char32_t p_char) {
  54. return (p_char >= 0x0020 && p_char <= 0x002F) || (p_char >= 0x003A && p_char <= 0x0040) || (p_char >= 0x005B && p_char <= 0x005E) || (p_char == 0x0060) || (p_char >= 0x007B && p_char <= 0x007E) || (p_char >= 0x2000 && p_char <= 0x206F) || (p_char >= 0x3000 && p_char <= 0x303F);
  55. }
  56. _FORCE_INLINE_ bool is_underscore(char32_t p_char) {
  57. return (p_char == 0x005F);
  58. }
  59. /*************************************************************************/
  60. String TextServerFallback::interface_name = "Fallback";
  61. uint32_t TextServerFallback::interface_features = 0; // Nothing is supported.
  62. bool TextServerFallback::has_feature(Feature p_feature) const {
  63. return (interface_features & p_feature) == p_feature;
  64. }
  65. String TextServerFallback::get_name() const {
  66. return interface_name;
  67. }
  68. uint32_t TextServerFallback::get_features() const {
  69. return interface_features;
  70. }
  71. void TextServerFallback::free(RID p_rid) {
  72. _THREAD_SAFE_METHOD_
  73. if (font_owner.owns(p_rid)) {
  74. FontDataFallback *fd = font_owner.get_or_null(p_rid);
  75. font_owner.free(p_rid);
  76. memdelete(fd);
  77. } else if (shaped_owner.owns(p_rid)) {
  78. ShapedTextData *sd = shaped_owner.get_or_null(p_rid);
  79. shaped_owner.free(p_rid);
  80. memdelete(sd);
  81. }
  82. }
  83. bool TextServerFallback::has(RID p_rid) {
  84. _THREAD_SAFE_METHOD_
  85. return font_owner.owns(p_rid) || shaped_owner.owns(p_rid);
  86. }
  87. bool TextServerFallback::load_support_data(const String &p_filename) {
  88. return false; // No extra data used.
  89. }
  90. bool TextServerFallback::save_support_data(const String &p_filename) const {
  91. return false; // No extra data used.
  92. }
  93. bool TextServerFallback::is_locale_right_to_left(const String &p_locale) const {
  94. return false; // No RTL support.
  95. }
  96. void TextServerFallback::_insert_feature_sets() {
  97. // Registered OpenType variation tag.
  98. feature_sets.insert("italic", OT_TAG('i', 't', 'a', 'l'));
  99. feature_sets.insert("optical_size", OT_TAG('o', 'p', 's', 'z'));
  100. feature_sets.insert("slant", OT_TAG('s', 'l', 'n', 't'));
  101. feature_sets.insert("width", OT_TAG('w', 'd', 't', 'h'));
  102. feature_sets.insert("weight", OT_TAG('w', 'g', 'h', 't'));
  103. }
  104. _FORCE_INLINE_ int32_t ot_tag_from_string(const char *p_str, int p_len) {
  105. char tag[4];
  106. uint32_t i;
  107. if (!p_str || !p_len || !*p_str) {
  108. return OT_TAG(0, 0, 0, 0);
  109. }
  110. if (p_len < 0 || p_len > 4) {
  111. p_len = 4;
  112. }
  113. for (i = 0; i < (uint32_t)p_len && p_str[i]; i++) {
  114. tag[i] = p_str[i];
  115. }
  116. for (; i < 4; i++) {
  117. tag[i] = ' ';
  118. }
  119. return OT_TAG(tag[0], tag[1], tag[2], tag[3]);
  120. }
  121. int32_t TextServerFallback::name_to_tag(const String &p_name) const {
  122. if (feature_sets.has(p_name)) {
  123. return feature_sets[p_name];
  124. }
  125. // No readable name, use tag string.
  126. return ot_tag_from_string(p_name.replace("custom_", "").ascii().get_data(), -1);
  127. }
  128. _FORCE_INLINE_ void ot_tag_to_string(int32_t p_tag, char *p_buf) {
  129. p_buf[0] = (char)(uint8_t)(p_tag >> 24);
  130. p_buf[1] = (char)(uint8_t)(p_tag >> 16);
  131. p_buf[2] = (char)(uint8_t)(p_tag >> 8);
  132. p_buf[3] = (char)(uint8_t)(p_tag >> 0);
  133. }
  134. String TextServerFallback::tag_to_name(int32_t p_tag) const {
  135. for (const KeyValue<StringName, int32_t> &E : feature_sets) {
  136. if (E.value == p_tag) {
  137. return E.key;
  138. }
  139. }
  140. // No readable name, use tag string.
  141. char name[5];
  142. memset(name, 0, 5);
  143. ot_tag_to_string(p_tag, name);
  144. return String("custom_") + String(name);
  145. }
  146. /*************************************************************************/
  147. /* Font Glyph Rendering */
  148. /*************************************************************************/
  149. _FORCE_INLINE_ TextServerFallback::FontTexturePosition TextServerFallback::find_texture_pos_for_glyph(FontDataForSizeFallback *p_data, int p_color_size, Image::Format p_image_format, int p_width, int p_height) const {
  150. FontTexturePosition ret;
  151. ret.index = -1;
  152. int mw = p_width;
  153. int mh = p_height;
  154. for (int i = 0; i < p_data->textures.size(); i++) {
  155. const FontTexture &ct = p_data->textures[i];
  156. if (RenderingServer::get_singleton() != nullptr) {
  157. if (ct.texture->get_format() != p_image_format) {
  158. continue;
  159. }
  160. }
  161. if (mw > ct.texture_w || mh > ct.texture_h) { // Too big for this texture.
  162. continue;
  163. }
  164. if (ct.offsets.size() < ct.texture_w) {
  165. continue;
  166. }
  167. ret.y = 0x7FFFFFFF;
  168. ret.x = 0;
  169. for (int j = 0; j < ct.texture_w - mw; j++) {
  170. int max_y = 0;
  171. for (int k = j; k < j + mw; k++) {
  172. int y = ct.offsets[k];
  173. if (y > max_y) {
  174. max_y = y;
  175. }
  176. }
  177. if (max_y < ret.y) {
  178. ret.y = max_y;
  179. ret.x = j;
  180. }
  181. }
  182. if (ret.y == 0x7FFFFFFF || ret.y + mh > ct.texture_h) {
  183. continue; // Fail, could not fit it here.
  184. }
  185. ret.index = i;
  186. break;
  187. }
  188. if (ret.index == -1) {
  189. // Could not find texture to fit, create one.
  190. ret.x = 0;
  191. ret.y = 0;
  192. int texsize = MAX(p_data->size.x * p_data->oversampling * 8, 256);
  193. if (mw > texsize) {
  194. texsize = mw; // Special case, adapt to it?
  195. }
  196. if (mh > texsize) {
  197. texsize = mh; // Special case, adapt to it?
  198. }
  199. texsize = next_power_of_2(texsize);
  200. texsize = MIN(texsize, 4096);
  201. FontTexture tex;
  202. tex.texture_w = texsize;
  203. tex.texture_h = texsize;
  204. tex.format = p_image_format;
  205. tex.imgdata.resize(texsize * texsize * p_color_size);
  206. {
  207. // Zero texture.
  208. uint8_t *w = tex.imgdata.ptrw();
  209. ERR_FAIL_COND_V(texsize * texsize * p_color_size > tex.imgdata.size(), ret);
  210. // Initialize the texture to all-white pixels to prevent artifacts when the
  211. // font is displayed at a non-default scale with filtering enabled.
  212. if (p_color_size == 2) {
  213. for (int i = 0; i < texsize * texsize * p_color_size; i += 2) { // FORMAT_LA8, BW font.
  214. w[i + 0] = 255;
  215. w[i + 1] = 0;
  216. }
  217. } else if (p_color_size == 4) {
  218. for (int i = 0; i < texsize * texsize * p_color_size; i += 4) { // FORMAT_RGBA8, Color font, Multichannel(+True) SDF.
  219. w[i + 0] = 255;
  220. w[i + 1] = 255;
  221. w[i + 2] = 255;
  222. w[i + 3] = 0;
  223. }
  224. } else {
  225. ERR_FAIL_V(ret);
  226. }
  227. }
  228. tex.offsets.resize(texsize);
  229. for (int i = 0; i < texsize; i++) { // Zero offsets.
  230. tex.offsets.write[i] = 0;
  231. }
  232. p_data->textures.push_back(tex);
  233. ret.index = p_data->textures.size() - 1;
  234. }
  235. return ret;
  236. }
  237. #ifdef MODULE_MSDFGEN_ENABLED
  238. struct MSContext {
  239. msdfgen::Point2 position;
  240. msdfgen::Shape *shape;
  241. msdfgen::Contour *contour;
  242. };
  243. class DistancePixelConversion {
  244. double invRange;
  245. public:
  246. _FORCE_INLINE_ explicit DistancePixelConversion(double range) :
  247. invRange(1 / range) {}
  248. _FORCE_INLINE_ void operator()(float *pixels, const msdfgen::MultiAndTrueDistance &distance) const {
  249. pixels[0] = float(invRange * distance.r + .5);
  250. pixels[1] = float(invRange * distance.g + .5);
  251. pixels[2] = float(invRange * distance.b + .5);
  252. pixels[3] = float(invRange * distance.a + .5);
  253. }
  254. };
  255. struct MSDFThreadData {
  256. msdfgen::Bitmap<float, 4> *output;
  257. msdfgen::Shape *shape;
  258. msdfgen::Projection *projection;
  259. DistancePixelConversion *distancePixelConversion;
  260. };
  261. static msdfgen::Point2 ft_point2(const FT_Vector &vector) {
  262. return msdfgen::Point2(vector.x / 60.0f, vector.y / 60.0f);
  263. }
  264. static int ft_move_to(const FT_Vector *to, void *user) {
  265. MSContext *context = reinterpret_cast<MSContext *>(user);
  266. if (!(context->contour && context->contour->edges.empty())) {
  267. context->contour = &context->shape->addContour();
  268. }
  269. context->position = ft_point2(*to);
  270. return 0;
  271. }
  272. static int ft_line_to(const FT_Vector *to, void *user) {
  273. MSContext *context = reinterpret_cast<MSContext *>(user);
  274. msdfgen::Point2 endpoint = ft_point2(*to);
  275. if (endpoint != context->position) {
  276. context->contour->addEdge(new msdfgen::LinearSegment(context->position, endpoint));
  277. context->position = endpoint;
  278. }
  279. return 0;
  280. }
  281. static int ft_conic_to(const FT_Vector *control, const FT_Vector *to, void *user) {
  282. MSContext *context = reinterpret_cast<MSContext *>(user);
  283. context->contour->addEdge(new msdfgen::QuadraticSegment(context->position, ft_point2(*control), ft_point2(*to)));
  284. context->position = ft_point2(*to);
  285. return 0;
  286. }
  287. static int ft_cubic_to(const FT_Vector *control1, const FT_Vector *control2, const FT_Vector *to, void *user) {
  288. MSContext *context = reinterpret_cast<MSContext *>(user);
  289. context->contour->addEdge(new msdfgen::CubicSegment(context->position, ft_point2(*control1), ft_point2(*control2), ft_point2(*to)));
  290. context->position = ft_point2(*to);
  291. return 0;
  292. }
  293. void TextServerFallback::_generateMTSDF_threaded(uint32_t y, void *p_td) const {
  294. MSDFThreadData *td = (MSDFThreadData *)p_td;
  295. msdfgen::ShapeDistanceFinder<msdfgen::OverlappingContourCombiner<msdfgen::MultiAndTrueDistanceSelector>> distanceFinder(*td->shape);
  296. int row = td->shape->inverseYAxis ? td->output->height() - y - 1 : y;
  297. for (int col = 0; col < td->output->width(); ++col) {
  298. int x = (y % 2) ? td->output->width() - col - 1 : col;
  299. msdfgen::Point2 p = td->projection->unproject(msdfgen::Point2(x + .5, y + .5));
  300. msdfgen::MultiAndTrueDistance distance = distanceFinder.distance(p);
  301. td->distancePixelConversion->operator()(td->output->operator()(x, row), distance);
  302. }
  303. }
  304. _FORCE_INLINE_ TextServerFallback::FontGlyph TextServerFallback::rasterize_msdf(FontDataFallback *p_font_data, FontDataForSizeFallback *p_data, int p_pixel_range, int p_rect_margin, FT_Outline *outline, const Vector2 &advance) const {
  305. msdfgen::Shape shape;
  306. shape.contours.clear();
  307. shape.inverseYAxis = false;
  308. MSContext context = {};
  309. context.shape = &shape;
  310. FT_Outline_Funcs ft_functions;
  311. ft_functions.move_to = &ft_move_to;
  312. ft_functions.line_to = &ft_line_to;
  313. ft_functions.conic_to = &ft_conic_to;
  314. ft_functions.cubic_to = &ft_cubic_to;
  315. ft_functions.shift = 0;
  316. ft_functions.delta = 0;
  317. int error = FT_Outline_Decompose(outline, &ft_functions, &context);
  318. ERR_FAIL_COND_V_MSG(error, FontGlyph(), "FreeType: Outline decomposition error: '" + String(FT_Error_String(error)) + "'.");
  319. if (!shape.contours.empty() && shape.contours.back().edges.empty()) {
  320. shape.contours.pop_back();
  321. }
  322. if (FT_Outline_Get_Orientation(outline) == 1) {
  323. for (int i = 0; i < (int)shape.contours.size(); ++i) {
  324. shape.contours[i].reverse();
  325. }
  326. }
  327. shape.inverseYAxis = true;
  328. shape.normalize();
  329. msdfgen::Shape::Bounds bounds = shape.getBounds(p_pixel_range);
  330. FontGlyph chr;
  331. chr.found = true;
  332. chr.advance = advance;
  333. if (shape.validate() && shape.contours.size() > 0) {
  334. int w = (bounds.r - bounds.l);
  335. int h = (bounds.t - bounds.b);
  336. int mw = w + p_rect_margin * 2;
  337. int mh = h + p_rect_margin * 2;
  338. ERR_FAIL_COND_V(mw > 4096, FontGlyph());
  339. ERR_FAIL_COND_V(mh > 4096, FontGlyph());
  340. FontTexturePosition tex_pos = find_texture_pos_for_glyph(p_data, 4, Image::FORMAT_RGBA8, mw, mh);
  341. ERR_FAIL_COND_V(tex_pos.index < 0, FontGlyph());
  342. FontTexture &tex = p_data->textures.write[tex_pos.index];
  343. edgeColoringSimple(shape, 3.0); // Max. angle.
  344. msdfgen::Bitmap<float, 4> image(w, h); // Texture size.
  345. DistancePixelConversion distancePixelConversion(p_pixel_range);
  346. msdfgen::Projection projection(msdfgen::Vector2(1.0, 1.0), msdfgen::Vector2(-bounds.l, -bounds.b));
  347. msdfgen::MSDFGeneratorConfig config(true, msdfgen::ErrorCorrectionConfig());
  348. MSDFThreadData td;
  349. td.output = &image;
  350. td.shape = &shape;
  351. td.projection = &projection;
  352. td.distancePixelConversion = &distancePixelConversion;
  353. if (p_font_data->work_pool.get_thread_count() == 0) {
  354. p_font_data->work_pool.init();
  355. }
  356. p_font_data->work_pool.do_work(h, this, &TextServerFallback::_generateMTSDF_threaded, &td);
  357. msdfgen::msdfErrorCorrection(image, shape, projection, p_pixel_range, config);
  358. {
  359. uint8_t *wr = tex.imgdata.ptrw();
  360. for (int i = 0; i < h; i++) {
  361. for (int j = 0; j < w; j++) {
  362. int ofs = ((i + tex_pos.y + p_rect_margin) * tex.texture_w + j + tex_pos.x + p_rect_margin) * 4;
  363. ERR_FAIL_COND_V(ofs >= tex.imgdata.size(), FontGlyph());
  364. wr[ofs + 0] = (uint8_t)(CLAMP(image(j, i)[0] * 256.f, 0.f, 255.f));
  365. wr[ofs + 1] = (uint8_t)(CLAMP(image(j, i)[1] * 256.f, 0.f, 255.f));
  366. wr[ofs + 2] = (uint8_t)(CLAMP(image(j, i)[2] * 256.f, 0.f, 255.f));
  367. wr[ofs + 3] = (uint8_t)(CLAMP(image(j, i)[3] * 256.f, 0.f, 255.f));
  368. }
  369. }
  370. }
  371. // Blit to image and texture.
  372. {
  373. if (RenderingServer::get_singleton() != nullptr) {
  374. Ref<Image> img = memnew(Image(tex.texture_w, tex.texture_h, 0, Image::FORMAT_RGBA8, tex.imgdata));
  375. if (tex.texture.is_null()) {
  376. tex.texture.instantiate();
  377. tex.texture->create_from_image(img);
  378. } else {
  379. tex.texture->update(img);
  380. }
  381. }
  382. }
  383. // Update height array.
  384. for (int k = tex_pos.x; k < tex_pos.x + mw; k++) {
  385. tex.offsets.write[k] = tex_pos.y + mh;
  386. }
  387. chr.texture_idx = tex_pos.index;
  388. chr.uv_rect = Rect2(tex_pos.x + p_rect_margin, tex_pos.y + p_rect_margin, w, h);
  389. chr.rect.position = Vector2(bounds.l, -bounds.t);
  390. chr.rect.size = chr.uv_rect.size;
  391. }
  392. return chr;
  393. }
  394. #endif
  395. #ifdef MODULE_FREETYPE_ENABLED
  396. _FORCE_INLINE_ TextServerFallback::FontGlyph TextServerFallback::rasterize_bitmap(FontDataForSizeFallback *p_data, int p_rect_margin, FT_Bitmap bitmap, int yofs, int xofs, const Vector2 &advance) const {
  397. int w = bitmap.width;
  398. int h = bitmap.rows;
  399. int mw = w + p_rect_margin * 2;
  400. int mh = h + p_rect_margin * 2;
  401. ERR_FAIL_COND_V(mw > 4096, FontGlyph());
  402. ERR_FAIL_COND_V(mh > 4096, FontGlyph());
  403. int color_size = bitmap.pixel_mode == FT_PIXEL_MODE_BGRA ? 4 : 2;
  404. Image::Format require_format = color_size == 4 ? Image::FORMAT_RGBA8 : Image::FORMAT_LA8;
  405. FontTexturePosition tex_pos = find_texture_pos_for_glyph(p_data, color_size, require_format, mw, mh);
  406. ERR_FAIL_COND_V(tex_pos.index < 0, FontGlyph());
  407. // Fit character in char texture.
  408. FontTexture &tex = p_data->textures.write[tex_pos.index];
  409. {
  410. uint8_t *wr = tex.imgdata.ptrw();
  411. for (int i = 0; i < h; i++) {
  412. for (int j = 0; j < w; j++) {
  413. int ofs = ((i + tex_pos.y + p_rect_margin) * tex.texture_w + j + tex_pos.x + p_rect_margin) * color_size;
  414. ERR_FAIL_COND_V(ofs >= tex.imgdata.size(), FontGlyph());
  415. switch (bitmap.pixel_mode) {
  416. case FT_PIXEL_MODE_MONO: {
  417. int byte = i * bitmap.pitch + (j >> 3);
  418. int bit = 1 << (7 - (j % 8));
  419. wr[ofs + 0] = 255; // grayscale as 1
  420. wr[ofs + 1] = (bitmap.buffer[byte] & bit) ? 255 : 0;
  421. } break;
  422. case FT_PIXEL_MODE_GRAY:
  423. wr[ofs + 0] = 255; // grayscale as 1
  424. wr[ofs + 1] = bitmap.buffer[i * bitmap.pitch + j];
  425. break;
  426. case FT_PIXEL_MODE_BGRA: {
  427. int ofs_color = i * bitmap.pitch + (j << 2);
  428. wr[ofs + 2] = bitmap.buffer[ofs_color + 0];
  429. wr[ofs + 1] = bitmap.buffer[ofs_color + 1];
  430. wr[ofs + 0] = bitmap.buffer[ofs_color + 2];
  431. wr[ofs + 3] = bitmap.buffer[ofs_color + 3];
  432. } break;
  433. default:
  434. ERR_FAIL_V_MSG(FontGlyph(), "Font uses unsupported pixel format: " + itos(bitmap.pixel_mode) + ".");
  435. break;
  436. }
  437. }
  438. }
  439. }
  440. // Blit to image and texture.
  441. {
  442. if (RenderingServer::get_singleton() != nullptr) {
  443. Ref<Image> img = memnew(Image(tex.texture_w, tex.texture_h, 0, require_format, tex.imgdata));
  444. if (tex.texture.is_null()) {
  445. tex.texture.instantiate();
  446. tex.texture->create_from_image(img);
  447. } else {
  448. tex.texture->update(img);
  449. }
  450. }
  451. }
  452. // Update height array.
  453. for (int k = tex_pos.x; k < tex_pos.x + mw; k++) {
  454. tex.offsets.write[k] = tex_pos.y + mh;
  455. }
  456. FontGlyph chr;
  457. chr.advance = advance * p_data->scale / p_data->oversampling;
  458. chr.texture_idx = tex_pos.index;
  459. chr.found = true;
  460. chr.uv_rect = Rect2(tex_pos.x + p_rect_margin, tex_pos.y + p_rect_margin, w, h);
  461. chr.rect.position = Vector2(xofs, -yofs) * p_data->scale / p_data->oversampling;
  462. chr.rect.size = chr.uv_rect.size * p_data->scale / p_data->oversampling;
  463. return chr;
  464. }
  465. #endif
  466. /*************************************************************************/
  467. /* Font Cache */
  468. /*************************************************************************/
  469. _FORCE_INLINE_ bool TextServerFallback::_ensure_glyph(FontDataFallback *p_font_data, const Vector2i &p_size, int32_t p_glyph) const {
  470. ERR_FAIL_COND_V(!_ensure_cache_for_size(p_font_data, p_size), false);
  471. FontDataForSizeFallback *fd = p_font_data->cache[p_size];
  472. if (fd->glyph_map.has(p_glyph)) {
  473. return fd->glyph_map[p_glyph].found;
  474. }
  475. if (p_glyph == 0) { // Non graphical or invalid glyph, do not render.
  476. fd->glyph_map[p_glyph] = FontGlyph();
  477. return true;
  478. }
  479. #ifdef MODULE_FREETYPE_ENABLED
  480. FontGlyph gl;
  481. if (fd->face) {
  482. FT_Int32 flags = FT_LOAD_DEFAULT;
  483. bool outline = p_size.y > 0;
  484. switch (p_font_data->hinting) {
  485. case TextServer::HINTING_NONE:
  486. flags |= FT_LOAD_NO_HINTING;
  487. break;
  488. case TextServer::HINTING_LIGHT:
  489. flags |= FT_LOAD_TARGET_LIGHT;
  490. break;
  491. default:
  492. flags |= FT_LOAD_TARGET_NORMAL;
  493. break;
  494. }
  495. if (p_font_data->force_autohinter) {
  496. flags |= FT_LOAD_FORCE_AUTOHINT;
  497. }
  498. if (outline) {
  499. flags |= FT_LOAD_NO_BITMAP;
  500. } else if (FT_HAS_COLOR(fd->face)) {
  501. flags |= FT_LOAD_COLOR;
  502. }
  503. int32_t glyph_index = FT_Get_Char_Index(fd->face, p_glyph);
  504. FT_Fixed v, h;
  505. FT_Get_Advance(fd->face, glyph_index, flags, &h);
  506. FT_Get_Advance(fd->face, glyph_index, flags | FT_LOAD_VERTICAL_LAYOUT, &v);
  507. int error = FT_Load_Glyph(fd->face, glyph_index, flags);
  508. if (error) {
  509. fd->glyph_map[p_glyph] = FontGlyph();
  510. return false;
  511. }
  512. if (!outline) {
  513. if (!p_font_data->msdf) {
  514. error = FT_Render_Glyph(fd->face->glyph, p_font_data->antialiased ? FT_RENDER_MODE_NORMAL : FT_RENDER_MODE_MONO);
  515. }
  516. FT_GlyphSlot slot = fd->face->glyph;
  517. if (!error) {
  518. if (p_font_data->msdf) {
  519. #ifdef MODULE_MSDFGEN_ENABLED
  520. gl = rasterize_msdf(p_font_data, fd, p_font_data->msdf_range, rect_range, &slot->outline, Vector2((h + (1 << 9)) >> 10, (v + (1 << 9)) >> 10) / 64.0);
  521. #else
  522. fd->glyph_map[p_glyph] = FontGlyph();
  523. ERR_FAIL_V_MSG(false, "Compiled without MSDFGEN support!");
  524. #endif
  525. } else {
  526. gl = rasterize_bitmap(fd, rect_range, slot->bitmap, slot->bitmap_top, slot->bitmap_left, Vector2((h + (1 << 9)) >> 10, (v + (1 << 9)) >> 10) / 64.0);
  527. }
  528. }
  529. } else {
  530. FT_Stroker stroker;
  531. if (FT_Stroker_New(library, &stroker) != 0) {
  532. fd->glyph_map[p_glyph] = FontGlyph();
  533. ERR_FAIL_V_MSG(false, "FreeType: Failed to load glyph stroker.");
  534. }
  535. FT_Stroker_Set(stroker, (int)(fd->size.y * fd->oversampling * 16.0), FT_STROKER_LINECAP_BUTT, FT_STROKER_LINEJOIN_ROUND, 0);
  536. FT_Glyph glyph;
  537. FT_BitmapGlyph glyph_bitmap;
  538. if (FT_Get_Glyph(fd->face->glyph, &glyph) != 0) {
  539. goto cleanup_stroker;
  540. }
  541. if (FT_Glyph_Stroke(&glyph, stroker, 1) != 0) {
  542. goto cleanup_glyph;
  543. }
  544. if (FT_Glyph_To_Bitmap(&glyph, p_font_data->antialiased ? FT_RENDER_MODE_NORMAL : FT_RENDER_MODE_MONO, nullptr, 1) != 0) {
  545. goto cleanup_glyph;
  546. }
  547. glyph_bitmap = (FT_BitmapGlyph)glyph;
  548. gl = rasterize_bitmap(fd, rect_range, glyph_bitmap->bitmap, glyph_bitmap->top, glyph_bitmap->left, Vector2());
  549. cleanup_glyph:
  550. FT_Done_Glyph(glyph);
  551. cleanup_stroker:
  552. FT_Stroker_Done(stroker);
  553. }
  554. fd->glyph_map[p_glyph] = gl;
  555. return gl.found;
  556. }
  557. #endif
  558. fd->glyph_map[p_glyph] = FontGlyph();
  559. return false;
  560. }
  561. _FORCE_INLINE_ bool TextServerFallback::_ensure_cache_for_size(FontDataFallback *p_font_data, const Vector2i &p_size) const {
  562. ERR_FAIL_COND_V(p_size.x <= 0, false);
  563. if (p_font_data->cache.has(p_size)) {
  564. return true;
  565. }
  566. FontDataForSizeFallback *fd = memnew(FontDataForSizeFallback);
  567. fd->size = p_size;
  568. if (p_font_data->data_ptr && (p_font_data->data_size > 0)) {
  569. // Init dynamic font.
  570. #ifdef MODULE_FREETYPE_ENABLED
  571. int error = 0;
  572. if (!library) {
  573. error = FT_Init_FreeType(&library);
  574. ERR_FAIL_COND_V_MSG(error != 0, false, "FreeType: Error initializing library: '" + String(FT_Error_String(error)) + "'.");
  575. }
  576. memset(&fd->stream, 0, sizeof(FT_StreamRec));
  577. fd->stream.base = (unsigned char *)p_font_data->data_ptr;
  578. fd->stream.size = p_font_data->data_size;
  579. fd->stream.pos = 0;
  580. FT_Open_Args fargs;
  581. memset(&fargs, 0, sizeof(FT_Open_Args));
  582. fargs.memory_base = (unsigned char *)p_font_data->data_ptr;
  583. fargs.memory_size = p_font_data->data_size;
  584. fargs.flags = FT_OPEN_MEMORY;
  585. fargs.stream = &fd->stream;
  586. error = FT_Open_Face(library, &fargs, 0, &fd->face);
  587. if (error) {
  588. FT_Done_Face(fd->face);
  589. fd->face = nullptr;
  590. ERR_FAIL_V_MSG(false, "FreeType: Error loading font: '" + String(FT_Error_String(error)) + "'.");
  591. }
  592. if (p_font_data->msdf) {
  593. fd->oversampling = 1.0f;
  594. fd->size.x = p_font_data->msdf_source_size;
  595. } else if (p_font_data->oversampling <= 0.0f) {
  596. fd->oversampling = font_get_global_oversampling();
  597. } else {
  598. fd->oversampling = p_font_data->oversampling;
  599. }
  600. if (FT_HAS_COLOR(fd->face) && fd->face->num_fixed_sizes > 0) {
  601. int best_match = 0;
  602. int diff = ABS(fd->size.x - ((int64_t)fd->face->available_sizes[0].width));
  603. fd->scale = float(fd->size.x * fd->oversampling) / fd->face->available_sizes[0].width;
  604. for (int i = 1; i < fd->face->num_fixed_sizes; i++) {
  605. int ndiff = ABS(fd->size.x - ((int64_t)fd->face->available_sizes[i].width));
  606. if (ndiff < diff) {
  607. best_match = i;
  608. diff = ndiff;
  609. fd->scale = float(fd->size.x * fd->oversampling) / fd->face->available_sizes[i].width;
  610. }
  611. }
  612. FT_Select_Size(fd->face, best_match);
  613. } else {
  614. FT_Set_Pixel_Sizes(fd->face, 0, Math::round(fd->size.x * fd->oversampling));
  615. fd->scale = ((float)fd->size.x * fd->oversampling) / (float)fd->face->size->metrics.y_ppem;
  616. }
  617. fd->ascent = (fd->face->size->metrics.ascender / 64.0) / fd->oversampling * fd->scale;
  618. fd->descent = (-fd->face->size->metrics.descender / 64.0) / fd->oversampling * fd->scale;
  619. fd->underline_position = (-FT_MulFix(fd->face->underline_position, fd->face->size->metrics.y_scale) / 64.0) / fd->oversampling * fd->scale;
  620. fd->underline_thickness = (FT_MulFix(fd->face->underline_thickness, fd->face->size->metrics.y_scale) / 64.0) / fd->oversampling * fd->scale;
  621. if (!p_font_data->face_init) {
  622. // Get style flags and name.
  623. if (fd->face->family_name != nullptr) {
  624. p_font_data->font_name = String::utf8((const char *)fd->face->family_name);
  625. }
  626. if (fd->face->style_name != nullptr) {
  627. p_font_data->style_name = String::utf8((const char *)fd->face->style_name);
  628. }
  629. p_font_data->style_flags = 0;
  630. if (fd->face->style_flags & FT_STYLE_FLAG_BOLD) {
  631. p_font_data->style_flags |= FONT_BOLD;
  632. }
  633. if (fd->face->style_flags & FT_STYLE_FLAG_ITALIC) {
  634. p_font_data->style_flags |= FONT_ITALIC;
  635. }
  636. if (fd->face->face_flags & FT_FACE_FLAG_FIXED_WIDTH) {
  637. p_font_data->style_flags |= FONT_FIXED_WIDTH;
  638. }
  639. // Read OpenType variations.
  640. p_font_data->supported_varaitions.clear();
  641. if (fd->face->face_flags & FT_FACE_FLAG_MULTIPLE_MASTERS) {
  642. FT_MM_Var *amaster;
  643. FT_Get_MM_Var(fd->face, &amaster);
  644. for (FT_UInt i = 0; i < amaster->num_axis; i++) {
  645. p_font_data->supported_varaitions[(int32_t)amaster->axis[i].tag] = Vector3i(amaster->axis[i].minimum / 65536, amaster->axis[i].maximum / 65536, amaster->axis[i].def / 65536);
  646. }
  647. FT_Done_MM_Var(library, amaster);
  648. }
  649. p_font_data->face_init = true;
  650. }
  651. // Write variations.
  652. if (fd->face->face_flags & FT_FACE_FLAG_MULTIPLE_MASTERS) {
  653. FT_MM_Var *amaster;
  654. FT_Get_MM_Var(fd->face, &amaster);
  655. Vector<FT_Fixed> coords;
  656. coords.resize(amaster->num_axis);
  657. FT_Get_Var_Design_Coordinates(fd->face, coords.size(), coords.ptrw());
  658. for (FT_UInt i = 0; i < amaster->num_axis; i++) {
  659. // Reset to default.
  660. int32_t var_tag = amaster->axis[i].tag;
  661. float var_value = (double)amaster->axis[i].def / 65536.f;
  662. coords.write[i] = amaster->axis[i].def;
  663. if (p_font_data->variation_coordinates.has(var_tag)) {
  664. var_value = p_font_data->variation_coordinates[var_tag];
  665. coords.write[i] = CLAMP(var_value * 65536.f, amaster->axis[i].minimum, amaster->axis[i].maximum);
  666. }
  667. if (p_font_data->variation_coordinates.has(tag_to_name(var_tag))) {
  668. var_value = p_font_data->variation_coordinates[tag_to_name(var_tag)];
  669. coords.write[i] = CLAMP(var_value * 65536.f, amaster->axis[i].minimum, amaster->axis[i].maximum);
  670. }
  671. }
  672. FT_Set_Var_Design_Coordinates(fd->face, coords.size(), coords.ptrw());
  673. FT_Done_MM_Var(library, amaster);
  674. }
  675. #else
  676. ERR_FAIL_V_MSG(false, "FreeType: Can't load dynamic font, engine is compiled without FreeType support!");
  677. #endif
  678. }
  679. p_font_data->cache[p_size] = fd;
  680. return true;
  681. }
  682. _FORCE_INLINE_ void TextServerFallback::_font_clear_cache(FontDataFallback *p_font_data) {
  683. for (const KeyValue<Vector2i, FontDataForSizeFallback *> &E : p_font_data->cache) {
  684. memdelete(E.value);
  685. }
  686. p_font_data->cache.clear();
  687. p_font_data->face_init = false;
  688. p_font_data->supported_varaitions.clear();
  689. }
  690. RID TextServerFallback::create_font() {
  691. FontDataFallback *fd = memnew(FontDataFallback);
  692. return font_owner.make_rid(fd);
  693. }
  694. void TextServerFallback::font_set_data(RID p_font_rid, const PackedByteArray &p_data) {
  695. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  696. ERR_FAIL_COND(!fd);
  697. MutexLock lock(fd->mutex);
  698. _font_clear_cache(fd);
  699. fd->data = p_data;
  700. fd->data_ptr = fd->data.ptr();
  701. fd->data_size = fd->data.size();
  702. }
  703. void TextServerFallback::font_set_data_ptr(RID p_font_rid, const uint8_t *p_data_ptr, size_t p_data_size) {
  704. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  705. ERR_FAIL_COND(!fd);
  706. MutexLock lock(fd->mutex);
  707. _font_clear_cache(fd);
  708. fd->data.clear();
  709. fd->data_ptr = p_data_ptr;
  710. fd->data_size = p_data_size;
  711. }
  712. void TextServerFallback::font_set_style(RID p_font_rid, uint32_t /*FontStyle*/ p_style) {
  713. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  714. ERR_FAIL_COND(!fd);
  715. MutexLock lock(fd->mutex);
  716. Vector2i size = _get_size(fd, 16);
  717. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  718. fd->style_flags = p_style;
  719. }
  720. uint32_t /*FontStyle*/ TextServerFallback::font_get_style(RID p_font_rid) const {
  721. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  722. ERR_FAIL_COND_V(!fd, 0);
  723. MutexLock lock(fd->mutex);
  724. Vector2i size = _get_size(fd, 16);
  725. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), 0);
  726. return fd->style_flags;
  727. }
  728. void TextServerFallback::font_set_style_name(RID p_font_rid, const String &p_name) {
  729. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  730. ERR_FAIL_COND(!fd);
  731. MutexLock lock(fd->mutex);
  732. Vector2i size = _get_size(fd, 16);
  733. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  734. fd->style_name = p_name;
  735. }
  736. String TextServerFallback::font_get_style_name(RID p_font_rid) const {
  737. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  738. ERR_FAIL_COND_V(!fd, String());
  739. MutexLock lock(fd->mutex);
  740. Vector2i size = _get_size(fd, 16);
  741. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), String());
  742. return fd->style_name;
  743. }
  744. void TextServerFallback::font_set_name(RID p_font_rid, const String &p_name) {
  745. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  746. ERR_FAIL_COND(!fd);
  747. MutexLock lock(fd->mutex);
  748. Vector2i size = _get_size(fd, 16);
  749. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  750. fd->font_name = p_name;
  751. }
  752. String TextServerFallback::font_get_name(RID p_font_rid) const {
  753. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  754. ERR_FAIL_COND_V(!fd, String());
  755. MutexLock lock(fd->mutex);
  756. Vector2i size = _get_size(fd, 16);
  757. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), String());
  758. return fd->font_name;
  759. }
  760. void TextServerFallback::font_set_antialiased(RID p_font_rid, bool p_antialiased) {
  761. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  762. ERR_FAIL_COND(!fd);
  763. MutexLock lock(fd->mutex);
  764. if (fd->antialiased != p_antialiased) {
  765. _font_clear_cache(fd);
  766. fd->antialiased = p_antialiased;
  767. }
  768. }
  769. bool TextServerFallback::font_is_antialiased(RID p_font_rid) const {
  770. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  771. ERR_FAIL_COND_V(!fd, false);
  772. MutexLock lock(fd->mutex);
  773. return fd->antialiased;
  774. }
  775. void TextServerFallback::font_set_multichannel_signed_distance_field(RID p_font_rid, bool p_msdf) {
  776. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  777. ERR_FAIL_COND(!fd);
  778. MutexLock lock(fd->mutex);
  779. if (fd->msdf != p_msdf) {
  780. _font_clear_cache(fd);
  781. fd->msdf = p_msdf;
  782. }
  783. }
  784. bool TextServerFallback::font_is_multichannel_signed_distance_field(RID p_font_rid) const {
  785. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  786. ERR_FAIL_COND_V(!fd, false);
  787. MutexLock lock(fd->mutex);
  788. return fd->msdf;
  789. }
  790. void TextServerFallback::font_set_msdf_pixel_range(RID p_font_rid, int p_msdf_pixel_range) {
  791. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  792. ERR_FAIL_COND(!fd);
  793. MutexLock lock(fd->mutex);
  794. if (fd->msdf_range != p_msdf_pixel_range) {
  795. _font_clear_cache(fd);
  796. fd->msdf_range = p_msdf_pixel_range;
  797. }
  798. }
  799. int TextServerFallback::font_get_msdf_pixel_range(RID p_font_rid) const {
  800. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  801. ERR_FAIL_COND_V(!fd, false);
  802. MutexLock lock(fd->mutex);
  803. return fd->msdf_range;
  804. }
  805. void TextServerFallback::font_set_msdf_size(RID p_font_rid, int p_msdf_size) {
  806. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  807. ERR_FAIL_COND(!fd);
  808. MutexLock lock(fd->mutex);
  809. if (fd->msdf_source_size != p_msdf_size) {
  810. _font_clear_cache(fd);
  811. fd->msdf_source_size = p_msdf_size;
  812. }
  813. }
  814. int TextServerFallback::font_get_msdf_size(RID p_font_rid) const {
  815. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  816. ERR_FAIL_COND_V(!fd, false);
  817. MutexLock lock(fd->mutex);
  818. return fd->msdf_source_size;
  819. }
  820. void TextServerFallback::font_set_fixed_size(RID p_font_rid, int p_fixed_size) {
  821. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  822. ERR_FAIL_COND(!fd);
  823. MutexLock lock(fd->mutex);
  824. if (fd->fixed_size != p_fixed_size) {
  825. fd->fixed_size = p_fixed_size;
  826. }
  827. }
  828. int TextServerFallback::font_get_fixed_size(RID p_font_rid) const {
  829. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  830. ERR_FAIL_COND_V(!fd, false);
  831. MutexLock lock(fd->mutex);
  832. return fd->fixed_size;
  833. }
  834. void TextServerFallback::font_set_force_autohinter(RID p_font_rid, bool p_force_autohinter) {
  835. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  836. ERR_FAIL_COND(!fd);
  837. MutexLock lock(fd->mutex);
  838. if (fd->force_autohinter != p_force_autohinter) {
  839. _font_clear_cache(fd);
  840. fd->force_autohinter = p_force_autohinter;
  841. }
  842. }
  843. bool TextServerFallback::font_is_force_autohinter(RID p_font_rid) const {
  844. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  845. ERR_FAIL_COND_V(!fd, false);
  846. MutexLock lock(fd->mutex);
  847. return fd->force_autohinter;
  848. }
  849. void TextServerFallback::font_set_hinting(RID p_font_rid, TextServer::Hinting p_hinting) {
  850. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  851. ERR_FAIL_COND(!fd);
  852. MutexLock lock(fd->mutex);
  853. if (fd->hinting != p_hinting) {
  854. _font_clear_cache(fd);
  855. fd->hinting = p_hinting;
  856. }
  857. }
  858. TextServer::Hinting TextServerFallback::font_get_hinting(RID p_font_rid) const {
  859. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  860. ERR_FAIL_COND_V(!fd, HINTING_NONE);
  861. MutexLock lock(fd->mutex);
  862. return fd->hinting;
  863. }
  864. void TextServerFallback::font_set_variation_coordinates(RID p_font_rid, const Dictionary &p_variation_coordinates) {
  865. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  866. ERR_FAIL_COND(!fd);
  867. MutexLock lock(fd->mutex);
  868. if (fd->variation_coordinates != p_variation_coordinates) {
  869. _font_clear_cache(fd);
  870. fd->variation_coordinates = p_variation_coordinates;
  871. }
  872. }
  873. Dictionary TextServerFallback::font_get_variation_coordinates(RID p_font_rid) const {
  874. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  875. ERR_FAIL_COND_V(!fd, Dictionary());
  876. MutexLock lock(fd->mutex);
  877. return fd->variation_coordinates;
  878. }
  879. void TextServerFallback::font_set_oversampling(RID p_font_rid, float p_oversampling) {
  880. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  881. ERR_FAIL_COND(!fd);
  882. MutexLock lock(fd->mutex);
  883. if (fd->oversampling != p_oversampling) {
  884. _font_clear_cache(fd);
  885. fd->oversampling = p_oversampling;
  886. }
  887. }
  888. float TextServerFallback::font_get_oversampling(RID p_font_rid) const {
  889. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  890. ERR_FAIL_COND_V(!fd, 0.f);
  891. MutexLock lock(fd->mutex);
  892. return fd->oversampling;
  893. }
  894. Array TextServerFallback::font_get_size_cache_list(RID p_font_rid) const {
  895. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  896. ERR_FAIL_COND_V(!fd, Array());
  897. MutexLock lock(fd->mutex);
  898. Array ret;
  899. for (const KeyValue<Vector2i, FontDataForSizeFallback *> &E : fd->cache) {
  900. ret.push_back(E.key);
  901. }
  902. return ret;
  903. }
  904. void TextServerFallback::font_clear_size_cache(RID p_font_rid) {
  905. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  906. ERR_FAIL_COND(!fd);
  907. MutexLock lock(fd->mutex);
  908. for (const KeyValue<Vector2i, FontDataForSizeFallback *> &E : fd->cache) {
  909. memdelete(E.value);
  910. }
  911. fd->cache.clear();
  912. }
  913. void TextServerFallback::font_remove_size_cache(RID p_font_rid, const Vector2i &p_size) {
  914. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  915. ERR_FAIL_COND(!fd);
  916. MutexLock lock(fd->mutex);
  917. if (fd->cache.has(p_size)) {
  918. memdelete(fd->cache[p_size]);
  919. fd->cache.erase(p_size);
  920. }
  921. }
  922. void TextServerFallback::font_set_ascent(RID p_font_rid, int p_size, float p_ascent) {
  923. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  924. ERR_FAIL_COND(!fd);
  925. MutexLock lock(fd->mutex);
  926. Vector2i size = _get_size(fd, p_size);
  927. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  928. fd->cache[size]->ascent = p_ascent;
  929. }
  930. float TextServerFallback::font_get_ascent(RID p_font_rid, int p_size) const {
  931. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  932. ERR_FAIL_COND_V(!fd, 0.f);
  933. MutexLock lock(fd->mutex);
  934. Vector2i size = _get_size(fd, p_size);
  935. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), 0.f);
  936. if (fd->msdf) {
  937. return fd->cache[size]->ascent * (float)p_size / (float)fd->msdf_source_size;
  938. } else {
  939. return fd->cache[size]->ascent;
  940. }
  941. }
  942. void TextServerFallback::font_set_descent(RID p_font_rid, int p_size, float p_descent) {
  943. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  944. ERR_FAIL_COND(!fd);
  945. Vector2i size = _get_size(fd, p_size);
  946. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  947. fd->cache[size]->descent = p_descent;
  948. }
  949. float TextServerFallback::font_get_descent(RID p_font_rid, int p_size) const {
  950. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  951. ERR_FAIL_COND_V(!fd, 0.f);
  952. MutexLock lock(fd->mutex);
  953. Vector2i size = _get_size(fd, p_size);
  954. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), 0.f);
  955. if (fd->msdf) {
  956. return fd->cache[size]->descent * (float)p_size / (float)fd->msdf_source_size;
  957. } else {
  958. return fd->cache[size]->descent;
  959. }
  960. }
  961. void TextServerFallback::font_set_underline_position(RID p_font_rid, int p_size, float p_underline_position) {
  962. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  963. ERR_FAIL_COND(!fd);
  964. MutexLock lock(fd->mutex);
  965. Vector2i size = _get_size(fd, p_size);
  966. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  967. fd->cache[size]->underline_position = p_underline_position;
  968. }
  969. float TextServerFallback::font_get_underline_position(RID p_font_rid, int p_size) const {
  970. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  971. ERR_FAIL_COND_V(!fd, 0.f);
  972. MutexLock lock(fd->mutex);
  973. Vector2i size = _get_size(fd, p_size);
  974. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), 0.f);
  975. if (fd->msdf) {
  976. return fd->cache[size]->underline_position * (float)p_size / (float)fd->msdf_source_size;
  977. } else {
  978. return fd->cache[size]->underline_position;
  979. }
  980. }
  981. void TextServerFallback::font_set_underline_thickness(RID p_font_rid, int p_size, float p_underline_thickness) {
  982. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  983. ERR_FAIL_COND(!fd);
  984. MutexLock lock(fd->mutex);
  985. Vector2i size = _get_size(fd, p_size);
  986. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  987. fd->cache[size]->underline_thickness = p_underline_thickness;
  988. }
  989. float TextServerFallback::font_get_underline_thickness(RID p_font_rid, int p_size) const {
  990. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  991. ERR_FAIL_COND_V(!fd, 0.f);
  992. MutexLock lock(fd->mutex);
  993. Vector2i size = _get_size(fd, p_size);
  994. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), 0.f);
  995. if (fd->msdf) {
  996. return fd->cache[size]->underline_thickness * (float)p_size / (float)fd->msdf_source_size;
  997. } else {
  998. return fd->cache[size]->underline_thickness;
  999. }
  1000. }
  1001. void TextServerFallback::font_set_scale(RID p_font_rid, int p_size, float p_scale) {
  1002. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  1003. ERR_FAIL_COND(!fd);
  1004. MutexLock lock(fd->mutex);
  1005. Vector2i size = _get_size(fd, p_size);
  1006. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1007. fd->cache[size]->scale = p_scale;
  1008. }
  1009. float TextServerFallback::font_get_scale(RID p_font_rid, int p_size) const {
  1010. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  1011. ERR_FAIL_COND_V(!fd, 0.f);
  1012. MutexLock lock(fd->mutex);
  1013. Vector2i size = _get_size(fd, p_size);
  1014. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), 0.f);
  1015. if (fd->msdf) {
  1016. return fd->cache[size]->scale * (float)p_size / (float)fd->msdf_source_size;
  1017. } else {
  1018. return fd->cache[size]->scale / fd->cache[size]->oversampling;
  1019. }
  1020. }
  1021. void TextServerFallback::font_set_spacing(RID p_font_rid, int p_size, TextServer::SpacingType p_spacing, int p_value) {
  1022. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  1023. ERR_FAIL_COND(!fd);
  1024. MutexLock lock(fd->mutex);
  1025. Vector2i size = _get_size(fd, p_size);
  1026. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1027. switch (p_spacing) {
  1028. case TextServer::SPACING_GLYPH: {
  1029. fd->cache[size]->spacing_glyph = p_value;
  1030. } break;
  1031. case TextServer::SPACING_SPACE: {
  1032. fd->cache[size]->spacing_space = p_value;
  1033. } break;
  1034. default: {
  1035. ERR_FAIL_MSG("Invalid spacing type: " + itos(p_spacing));
  1036. } break;
  1037. }
  1038. }
  1039. int TextServerFallback::font_get_spacing(RID p_font_rid, int p_size, TextServer::SpacingType p_spacing) const {
  1040. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  1041. ERR_FAIL_COND_V(!fd, 0);
  1042. MutexLock lock(fd->mutex);
  1043. Vector2i size = _get_size(fd, p_size);
  1044. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), 0);
  1045. switch (p_spacing) {
  1046. case TextServer::SPACING_GLYPH: {
  1047. if (fd->msdf) {
  1048. return fd->cache[size]->spacing_glyph * (float)p_size / (float)fd->msdf_source_size;
  1049. } else {
  1050. return fd->cache[size]->spacing_glyph;
  1051. }
  1052. } break;
  1053. case TextServer::SPACING_SPACE: {
  1054. if (fd->msdf) {
  1055. return fd->cache[size]->spacing_space * (float)p_size / (float)fd->msdf_source_size;
  1056. } else {
  1057. return fd->cache[size]->spacing_space;
  1058. }
  1059. } break;
  1060. default: {
  1061. ERR_FAIL_V_MSG(0, "Invalid spacing type: " + itos(p_spacing));
  1062. } break;
  1063. }
  1064. return 0;
  1065. }
  1066. int TextServerFallback::font_get_texture_count(RID p_font_rid, const Vector2i &p_size) const {
  1067. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  1068. ERR_FAIL_COND_V(!fd, 0);
  1069. MutexLock lock(fd->mutex);
  1070. Vector2i size = _get_size_outline(fd, p_size);
  1071. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), 0);
  1072. return fd->cache[size]->textures.size();
  1073. }
  1074. void TextServerFallback::font_clear_textures(RID p_font_rid, const Vector2i &p_size) {
  1075. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  1076. ERR_FAIL_COND(!fd);
  1077. MutexLock lock(fd->mutex);
  1078. Vector2i size = _get_size_outline(fd, p_size);
  1079. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1080. fd->cache[size]->textures.clear();
  1081. }
  1082. void TextServerFallback::font_remove_texture(RID p_font_rid, const Vector2i &p_size, int p_texture_index) {
  1083. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  1084. ERR_FAIL_COND(!fd);
  1085. MutexLock lock(fd->mutex);
  1086. Vector2i size = _get_size_outline(fd, p_size);
  1087. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1088. ERR_FAIL_INDEX(p_texture_index, fd->cache[size]->textures.size());
  1089. fd->cache[size]->textures.remove_at(p_texture_index);
  1090. }
  1091. void TextServerFallback::font_set_texture_image(RID p_font_rid, const Vector2i &p_size, int p_texture_index, const Ref<Image> &p_image) {
  1092. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  1093. ERR_FAIL_COND(!fd);
  1094. ERR_FAIL_COND(p_image.is_null());
  1095. MutexLock lock(fd->mutex);
  1096. Vector2i size = _get_size_outline(fd, p_size);
  1097. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1098. ERR_FAIL_COND(p_texture_index < 0);
  1099. if (p_texture_index >= fd->cache[size]->textures.size()) {
  1100. fd->cache[size]->textures.resize(p_texture_index + 1);
  1101. }
  1102. FontTexture &tex = fd->cache[size]->textures.write[p_texture_index];
  1103. tex.imgdata = p_image->get_data();
  1104. tex.texture_w = p_image->get_width();
  1105. tex.texture_h = p_image->get_height();
  1106. tex.format = p_image->get_format();
  1107. Ref<Image> img = memnew(Image(tex.texture_w, tex.texture_h, 0, tex.format, tex.imgdata));
  1108. tex.texture = Ref<ImageTexture>();
  1109. tex.texture.instantiate();
  1110. tex.texture->create_from_image(img);
  1111. }
  1112. Ref<Image> TextServerFallback::font_get_texture_image(RID p_font_rid, const Vector2i &p_size, int p_texture_index) const {
  1113. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  1114. ERR_FAIL_COND_V(!fd, Ref<Image>());
  1115. MutexLock lock(fd->mutex);
  1116. Vector2i size = _get_size_outline(fd, p_size);
  1117. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), Ref<Image>());
  1118. ERR_FAIL_INDEX_V(p_texture_index, fd->cache[size]->textures.size(), Ref<Image>());
  1119. const FontTexture &tex = fd->cache[size]->textures.write[p_texture_index];
  1120. Ref<Image> img = memnew(Image(tex.texture_w, tex.texture_h, 0, tex.format, tex.imgdata));
  1121. return img;
  1122. }
  1123. void TextServerFallback::font_set_texture_offsets(RID p_font_rid, const Vector2i &p_size, int p_texture_index, const PackedInt32Array &p_offset) {
  1124. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  1125. ERR_FAIL_COND(!fd);
  1126. MutexLock lock(fd->mutex);
  1127. Vector2i size = _get_size_outline(fd, p_size);
  1128. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1129. ERR_FAIL_COND(p_texture_index < 0);
  1130. if (p_texture_index >= fd->cache[size]->textures.size()) {
  1131. fd->cache[size]->textures.resize(p_texture_index + 1);
  1132. }
  1133. FontTexture &tex = fd->cache[size]->textures.write[p_texture_index];
  1134. tex.offsets = p_offset;
  1135. }
  1136. PackedInt32Array TextServerFallback::font_get_texture_offsets(RID p_font_rid, const Vector2i &p_size, int p_texture_index) const {
  1137. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  1138. ERR_FAIL_COND_V(!fd, PackedInt32Array());
  1139. MutexLock lock(fd->mutex);
  1140. Vector2i size = _get_size_outline(fd, p_size);
  1141. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), PackedInt32Array());
  1142. ERR_FAIL_INDEX_V(p_texture_index, fd->cache[size]->textures.size(), PackedInt32Array());
  1143. const FontTexture &tex = fd->cache[size]->textures.write[p_texture_index];
  1144. return tex.offsets;
  1145. }
  1146. Array TextServerFallback::font_get_glyph_list(RID p_font_rid, const Vector2i &p_size) const {
  1147. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  1148. ERR_FAIL_COND_V(!fd, Array());
  1149. MutexLock lock(fd->mutex);
  1150. Vector2i size = _get_size_outline(fd, p_size);
  1151. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), Array());
  1152. Array ret;
  1153. const HashMap<int32_t, FontGlyph> &gl = fd->cache[size]->glyph_map;
  1154. const int32_t *E = nullptr;
  1155. while ((E = gl.next(E))) {
  1156. ret.push_back(*E);
  1157. }
  1158. return ret;
  1159. }
  1160. void TextServerFallback::font_clear_glyphs(RID p_font_rid, const Vector2i &p_size) {
  1161. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  1162. ERR_FAIL_COND(!fd);
  1163. MutexLock lock(fd->mutex);
  1164. Vector2i size = _get_size_outline(fd, p_size);
  1165. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1166. fd->cache[size]->glyph_map.clear();
  1167. }
  1168. void TextServerFallback::font_remove_glyph(RID p_font_rid, const Vector2i &p_size, int32_t p_glyph) {
  1169. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  1170. ERR_FAIL_COND(!fd);
  1171. MutexLock lock(fd->mutex);
  1172. Vector2i size = _get_size_outline(fd, p_size);
  1173. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1174. fd->cache[size]->glyph_map.erase(p_glyph);
  1175. }
  1176. Vector2 TextServerFallback::font_get_glyph_advance(RID p_font_rid, int p_size, int32_t p_glyph) const {
  1177. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  1178. ERR_FAIL_COND_V(!fd, Vector2());
  1179. MutexLock lock(fd->mutex);
  1180. Vector2i size = _get_size(fd, p_size);
  1181. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), Vector2());
  1182. if (!_ensure_glyph(fd, size, p_glyph)) {
  1183. return Vector2(); // Invalid or non graphicl glyph, do not display errors.
  1184. }
  1185. const HashMap<int32_t, FontGlyph> &gl = fd->cache[size]->glyph_map;
  1186. if (fd->msdf) {
  1187. return gl[p_glyph].advance * (float)p_size / (float)fd->msdf_source_size;
  1188. } else {
  1189. return gl[p_glyph].advance;
  1190. }
  1191. }
  1192. void TextServerFallback::font_set_glyph_advance(RID p_font_rid, int p_size, int32_t p_glyph, const Vector2 &p_advance) {
  1193. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  1194. ERR_FAIL_COND(!fd);
  1195. MutexLock lock(fd->mutex);
  1196. Vector2i size = _get_size(fd, p_size);
  1197. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1198. HashMap<int32_t, FontGlyph> &gl = fd->cache[size]->glyph_map;
  1199. gl[p_glyph].advance = p_advance;
  1200. gl[p_glyph].found = true;
  1201. }
  1202. Vector2 TextServerFallback::font_get_glyph_offset(RID p_font_rid, const Vector2i &p_size, int32_t p_glyph) const {
  1203. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  1204. ERR_FAIL_COND_V(!fd, Vector2());
  1205. MutexLock lock(fd->mutex);
  1206. Vector2i size = _get_size_outline(fd, p_size);
  1207. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), Vector2());
  1208. if (!_ensure_glyph(fd, size, p_glyph)) {
  1209. return Vector2(); // Invalid or non graphicl glyph, do not display errors.
  1210. }
  1211. const HashMap<int32_t, FontGlyph> &gl = fd->cache[size]->glyph_map;
  1212. if (fd->msdf) {
  1213. return gl[p_glyph].rect.position * (float)p_size.x / (float)fd->msdf_source_size;
  1214. } else {
  1215. return gl[p_glyph].rect.position;
  1216. }
  1217. }
  1218. void TextServerFallback::font_set_glyph_offset(RID p_font_rid, const Vector2i &p_size, int32_t p_glyph, const Vector2 &p_offset) {
  1219. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  1220. ERR_FAIL_COND(!fd);
  1221. MutexLock lock(fd->mutex);
  1222. Vector2i size = _get_size_outline(fd, p_size);
  1223. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1224. HashMap<int32_t, FontGlyph> &gl = fd->cache[size]->glyph_map;
  1225. gl[p_glyph].rect.position = p_offset;
  1226. gl[p_glyph].found = true;
  1227. }
  1228. Vector2 TextServerFallback::font_get_glyph_size(RID p_font_rid, const Vector2i &p_size, int32_t p_glyph) const {
  1229. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  1230. ERR_FAIL_COND_V(!fd, Vector2());
  1231. MutexLock lock(fd->mutex);
  1232. Vector2i size = _get_size_outline(fd, p_size);
  1233. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), Vector2());
  1234. if (!_ensure_glyph(fd, size, p_glyph)) {
  1235. return Vector2(); // Invalid or non graphicl glyph, do not display errors.
  1236. }
  1237. const HashMap<int32_t, FontGlyph> &gl = fd->cache[size]->glyph_map;
  1238. if (fd->msdf) {
  1239. return gl[p_glyph].rect.size * (float)p_size.x / (float)fd->msdf_source_size;
  1240. } else {
  1241. return gl[p_glyph].rect.size;
  1242. }
  1243. }
  1244. void TextServerFallback::font_set_glyph_size(RID p_font_rid, const Vector2i &p_size, int32_t p_glyph, const Vector2 &p_gl_size) {
  1245. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  1246. ERR_FAIL_COND(!fd);
  1247. MutexLock lock(fd->mutex);
  1248. Vector2i size = _get_size_outline(fd, p_size);
  1249. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1250. HashMap<int32_t, FontGlyph> &gl = fd->cache[size]->glyph_map;
  1251. gl[p_glyph].rect.size = p_gl_size;
  1252. gl[p_glyph].found = true;
  1253. }
  1254. Rect2 TextServerFallback::font_get_glyph_uv_rect(RID p_font_rid, const Vector2i &p_size, int32_t p_glyph) const {
  1255. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  1256. ERR_FAIL_COND_V(!fd, Rect2());
  1257. MutexLock lock(fd->mutex);
  1258. Vector2i size = _get_size_outline(fd, p_size);
  1259. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), Rect2());
  1260. if (!_ensure_glyph(fd, size, p_glyph)) {
  1261. return Rect2(); // Invalid or non graphicl glyph, do not display errors.
  1262. }
  1263. const HashMap<int32_t, FontGlyph> &gl = fd->cache[size]->glyph_map;
  1264. return gl[p_glyph].uv_rect;
  1265. }
  1266. void TextServerFallback::font_set_glyph_uv_rect(RID p_font_rid, const Vector2i &p_size, int32_t p_glyph, const Rect2 &p_uv_rect) {
  1267. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  1268. ERR_FAIL_COND(!fd);
  1269. MutexLock lock(fd->mutex);
  1270. Vector2i size = _get_size_outline(fd, p_size);
  1271. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1272. HashMap<int32_t, FontGlyph> &gl = fd->cache[size]->glyph_map;
  1273. gl[p_glyph].uv_rect = p_uv_rect;
  1274. gl[p_glyph].found = true;
  1275. }
  1276. int TextServerFallback::font_get_glyph_texture_idx(RID p_font_rid, const Vector2i &p_size, int32_t p_glyph) const {
  1277. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  1278. ERR_FAIL_COND_V(!fd, -1);
  1279. MutexLock lock(fd->mutex);
  1280. Vector2i size = _get_size_outline(fd, p_size);
  1281. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), -1);
  1282. if (!_ensure_glyph(fd, size, p_glyph)) {
  1283. return -1; // Invalid or non graphicl glyph, do not display errors.
  1284. }
  1285. const HashMap<int32_t, FontGlyph> &gl = fd->cache[size]->glyph_map;
  1286. return gl[p_glyph].texture_idx;
  1287. }
  1288. void TextServerFallback::font_set_glyph_texture_idx(RID p_font_rid, const Vector2i &p_size, int32_t p_glyph, int p_texture_idx) {
  1289. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  1290. ERR_FAIL_COND(!fd);
  1291. MutexLock lock(fd->mutex);
  1292. Vector2i size = _get_size_outline(fd, p_size);
  1293. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1294. HashMap<int32_t, FontGlyph> &gl = fd->cache[size]->glyph_map;
  1295. gl[p_glyph].texture_idx = p_texture_idx;
  1296. gl[p_glyph].found = true;
  1297. }
  1298. Dictionary TextServerFallback::font_get_glyph_contours(RID p_font_rid, int p_size, int32_t p_index) const {
  1299. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  1300. ERR_FAIL_COND_V(!fd, Dictionary());
  1301. MutexLock lock(fd->mutex);
  1302. Vector2i size = _get_size(fd, p_size);
  1303. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), Dictionary());
  1304. Vector<Vector3> points;
  1305. Vector<int32_t> contours;
  1306. bool orientation;
  1307. #ifdef MODULE_FREETYPE_ENABLED
  1308. int error = FT_Load_Glyph(fd->cache[size]->face, FT_Get_Char_Index(fd->cache[size]->face, p_index), FT_LOAD_NO_BITMAP | (fd->force_autohinter ? FT_LOAD_FORCE_AUTOHINT : 0));
  1309. ERR_FAIL_COND_V(error, Dictionary());
  1310. points.clear();
  1311. contours.clear();
  1312. float h = fd->cache[size]->ascent;
  1313. float scale = (1.0 / 64.0) / fd->cache[size]->oversampling * fd->cache[size]->scale;
  1314. if (fd->msdf) {
  1315. scale = scale * (float)p_size / (float)fd->msdf_source_size;
  1316. }
  1317. for (short i = 0; i < fd->cache[size]->face->glyph->outline.n_points; i++) {
  1318. points.push_back(Vector3(fd->cache[size]->face->glyph->outline.points[i].x * scale, h - fd->cache[size]->face->glyph->outline.points[i].y * scale, FT_CURVE_TAG(fd->cache[size]->face->glyph->outline.tags[i])));
  1319. }
  1320. for (short i = 0; i < fd->cache[size]->face->glyph->outline.n_contours; i++) {
  1321. contours.push_back(fd->cache[size]->face->glyph->outline.contours[i]);
  1322. }
  1323. orientation = (FT_Outline_Get_Orientation(&fd->cache[size]->face->glyph->outline) == FT_ORIENTATION_FILL_RIGHT);
  1324. #else
  1325. return Dictionary();
  1326. #endif
  1327. Dictionary out;
  1328. out["points"] = points;
  1329. out["contours"] = contours;
  1330. out["orientation"] = orientation;
  1331. return out;
  1332. }
  1333. Array TextServerFallback::font_get_kerning_list(RID p_font_rid, int p_size) const {
  1334. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  1335. ERR_FAIL_COND_V(!fd, Array());
  1336. MutexLock lock(fd->mutex);
  1337. Vector2i size = _get_size(fd, p_size);
  1338. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), Array());
  1339. Array ret;
  1340. for (const KeyValue<Vector2i, Vector2> &E : fd->cache[size]->kerning_map) {
  1341. ret.push_back(E.key);
  1342. }
  1343. return ret;
  1344. }
  1345. void TextServerFallback::font_clear_kerning_map(RID p_font_rid, int p_size) {
  1346. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  1347. ERR_FAIL_COND(!fd);
  1348. MutexLock lock(fd->mutex);
  1349. Vector2i size = _get_size(fd, p_size);
  1350. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1351. fd->cache[size]->kerning_map.clear();
  1352. }
  1353. void TextServerFallback::font_remove_kerning(RID p_font_rid, int p_size, const Vector2i &p_glyph_pair) {
  1354. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  1355. ERR_FAIL_COND(!fd);
  1356. MutexLock lock(fd->mutex);
  1357. Vector2i size = _get_size(fd, p_size);
  1358. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1359. fd->cache[size]->kerning_map.erase(p_glyph_pair);
  1360. }
  1361. void TextServerFallback::font_set_kerning(RID p_font_rid, int p_size, const Vector2i &p_glyph_pair, const Vector2 &p_kerning) {
  1362. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  1363. ERR_FAIL_COND(!fd);
  1364. MutexLock lock(fd->mutex);
  1365. Vector2i size = _get_size(fd, p_size);
  1366. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1367. fd->cache[size]->kerning_map[p_glyph_pair] = p_kerning;
  1368. }
  1369. Vector2 TextServerFallback::font_get_kerning(RID p_font_rid, int p_size, const Vector2i &p_glyph_pair) const {
  1370. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  1371. ERR_FAIL_COND_V(!fd, Vector2());
  1372. MutexLock lock(fd->mutex);
  1373. Vector2i size = _get_size(fd, p_size);
  1374. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), Vector2());
  1375. const Map<Vector2i, Vector2> &kern = fd->cache[size]->kerning_map;
  1376. if (kern.has(p_glyph_pair)) {
  1377. if (fd->msdf) {
  1378. return kern[p_glyph_pair] * (float)p_size / (float)fd->msdf_source_size;
  1379. } else {
  1380. return kern[p_glyph_pair];
  1381. }
  1382. } else {
  1383. #ifdef MODULE_FREETYPE_ENABLED
  1384. if (fd->cache[size]->face) {
  1385. FT_Vector delta;
  1386. int32_t glyph_a = FT_Get_Char_Index(fd->cache[size]->face, p_glyph_pair.x);
  1387. int32_t glyph_b = FT_Get_Char_Index(fd->cache[size]->face, p_glyph_pair.y);
  1388. FT_Get_Kerning(fd->cache[size]->face, glyph_a, glyph_b, FT_KERNING_DEFAULT, &delta);
  1389. if (fd->msdf) {
  1390. return Vector2(delta.x, delta.y) * (float)p_size / (float)fd->msdf_source_size;
  1391. } else {
  1392. return Vector2(delta.x, delta.y);
  1393. }
  1394. }
  1395. #endif
  1396. }
  1397. return Vector2();
  1398. }
  1399. int32_t TextServerFallback::font_get_glyph_index(RID p_font_rid, int p_size, char32_t p_char, char32_t p_variation_selector) const {
  1400. ERR_FAIL_COND_V_MSG((p_char >= 0xd800 && p_char <= 0xdfff) || (p_char > 0x10ffff), 0, "Unicode parsing error: Invalid unicode codepoint " + String::num_int64(p_char, 16) + ".");
  1401. return (int32_t)p_char;
  1402. }
  1403. bool TextServerFallback::font_has_char(RID p_font_rid, char32_t p_char) const {
  1404. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  1405. ERR_FAIL_COND_V(!fd, false);
  1406. ERR_FAIL_COND_V_MSG((p_char >= 0xd800 && p_char <= 0xdfff) || (p_char > 0x10ffff), false, "Unicode parsing error: Invalid unicode codepoint " + String::num_int64(p_char, 16) + ".");
  1407. MutexLock lock(fd->mutex);
  1408. if (fd->cache.is_empty()) {
  1409. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, fd->msdf ? Vector2i(fd->msdf_source_size, 0) : Vector2i(16, 0)), false);
  1410. }
  1411. FontDataForSizeFallback *at_size = fd->cache.front()->get();
  1412. #ifdef MODULE_FREETYPE_ENABLED
  1413. if (at_size && at_size->face) {
  1414. return FT_Get_Char_Index(at_size->face, p_char) != 0;
  1415. }
  1416. #endif
  1417. return (at_size) ? at_size->glyph_map.has((int32_t)p_char) : false;
  1418. }
  1419. String TextServerFallback::font_get_supported_chars(RID p_font_rid) const {
  1420. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  1421. ERR_FAIL_COND_V(!fd, String());
  1422. MutexLock lock(fd->mutex);
  1423. if (fd->cache.is_empty()) {
  1424. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, fd->msdf ? Vector2i(fd->msdf_source_size, 0) : Vector2i(16, 0)), String());
  1425. }
  1426. FontDataForSizeFallback *at_size = fd->cache.front()->get();
  1427. String chars;
  1428. #ifdef MODULE_FREETYPE_ENABLED
  1429. if (at_size && at_size->face) {
  1430. FT_UInt gindex;
  1431. FT_ULong charcode = FT_Get_First_Char(at_size->face, &gindex);
  1432. while (gindex != 0) {
  1433. if (charcode != 0) {
  1434. chars += char32_t(charcode);
  1435. }
  1436. charcode = FT_Get_Next_Char(at_size->face, charcode, &gindex);
  1437. }
  1438. return chars;
  1439. }
  1440. #endif
  1441. if (at_size) {
  1442. const HashMap<int32_t, FontGlyph> &gl = at_size->glyph_map;
  1443. const int32_t *E = nullptr;
  1444. while ((E = gl.next(E))) {
  1445. chars += char32_t(*E);
  1446. }
  1447. }
  1448. return chars;
  1449. }
  1450. void TextServerFallback::font_render_range(RID p_font_rid, const Vector2i &p_size, char32_t p_start, char32_t p_end) {
  1451. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  1452. ERR_FAIL_COND(!fd);
  1453. ERR_FAIL_COND_MSG((p_start >= 0xd800 && p_start <= 0xdfff) || (p_start > 0x10ffff), "Unicode parsing error: Invalid unicode codepoint " + String::num_int64(p_start, 16) + ".");
  1454. ERR_FAIL_COND_MSG((p_end >= 0xd800 && p_end <= 0xdfff) || (p_end > 0x10ffff), "Unicode parsing error: Invalid unicode codepoint " + String::num_int64(p_end, 16) + ".");
  1455. MutexLock lock(fd->mutex);
  1456. Vector2i size = _get_size_outline(fd, p_size);
  1457. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1458. for (char32_t i = p_start; i <= p_end; i++) {
  1459. _ensure_glyph(fd, size, (int32_t)i);
  1460. }
  1461. }
  1462. void TextServerFallback::font_render_glyph(RID p_font_rid, const Vector2i &p_size, int32_t p_index) {
  1463. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  1464. ERR_FAIL_COND(!fd);
  1465. MutexLock lock(fd->mutex);
  1466. Vector2i size = _get_size_outline(fd, p_size);
  1467. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1468. ERR_FAIL_COND(!_ensure_glyph(fd, size, p_index));
  1469. }
  1470. void TextServerFallback::font_draw_glyph(RID p_font_rid, RID p_canvas, int p_size, const Vector2 &p_pos, int32_t p_index, const Color &p_color) const {
  1471. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  1472. ERR_FAIL_COND(!fd);
  1473. MutexLock lock(fd->mutex);
  1474. Vector2i size = _get_size(fd, p_size);
  1475. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1476. if (!_ensure_glyph(fd, size, p_index)) {
  1477. return; // Invalid or non-graphical glyph, do not display errors, nothing to draw.
  1478. }
  1479. const FontGlyph &gl = fd->cache[size]->glyph_map[p_index];
  1480. if (gl.found) {
  1481. ERR_FAIL_COND(gl.texture_idx < -1 || gl.texture_idx >= fd->cache[size]->textures.size());
  1482. if (gl.texture_idx != -1) {
  1483. Color modulate = p_color;
  1484. #ifdef MODULE_FREETYPE_ENABLED
  1485. if (fd->cache[size]->face && FT_HAS_COLOR(fd->cache[size]->face)) {
  1486. modulate.r = modulate.g = modulate.b = 1.0;
  1487. }
  1488. #endif
  1489. if (RenderingServer::get_singleton() != nullptr) {
  1490. RID texture = fd->cache[size]->textures[gl.texture_idx].texture->get_rid();
  1491. if (fd->msdf) {
  1492. Point2 cpos = p_pos;
  1493. cpos += gl.rect.position * (float)p_size / (float)fd->msdf_source_size;
  1494. Size2 csize = gl.rect.size * (float)p_size / (float)fd->msdf_source_size;
  1495. RenderingServer::get_singleton()->canvas_item_add_msdf_texture_rect_region(p_canvas, Rect2(cpos, csize), texture, gl.uv_rect, modulate, 0, fd->msdf_range);
  1496. } else {
  1497. Point2 cpos = p_pos.floor();
  1498. cpos += gl.rect.position;
  1499. Size2 csize = gl.rect.size;
  1500. RenderingServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas, Rect2(cpos, csize), texture, gl.uv_rect, modulate, false, false);
  1501. }
  1502. }
  1503. }
  1504. }
  1505. }
  1506. void TextServerFallback::font_draw_glyph_outline(RID p_font_rid, RID p_canvas, int p_size, int p_outline_size, const Vector2 &p_pos, int32_t p_index, const Color &p_color) const {
  1507. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  1508. ERR_FAIL_COND(!fd);
  1509. MutexLock lock(fd->mutex);
  1510. Vector2i size = _get_size_outline(fd, Vector2i(p_size, p_outline_size));
  1511. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1512. if (!_ensure_glyph(fd, size, p_index)) {
  1513. return; // Invalid or non-graphical glyph, do not display errors, nothing to draw.
  1514. }
  1515. const FontGlyph &gl = fd->cache[size]->glyph_map[p_index];
  1516. if (gl.found) {
  1517. ERR_FAIL_COND(gl.texture_idx < -1 || gl.texture_idx >= fd->cache[size]->textures.size());
  1518. if (gl.texture_idx != -1) {
  1519. Color modulate = p_color;
  1520. #ifdef MODULE_FREETYPE_ENABLED
  1521. if (fd->cache[size]->face && FT_HAS_COLOR(fd->cache[size]->face)) {
  1522. modulate.r = modulate.g = modulate.b = 1.0;
  1523. }
  1524. #endif
  1525. if (RenderingServer::get_singleton() != nullptr) {
  1526. RID texture = fd->cache[size]->textures[gl.texture_idx].texture->get_rid();
  1527. if (fd->msdf) {
  1528. Point2 cpos = p_pos;
  1529. cpos += gl.rect.position * (float)p_size / (float)fd->msdf_source_size;
  1530. Size2 csize = gl.rect.size * (float)p_size / (float)fd->msdf_source_size;
  1531. RenderingServer::get_singleton()->canvas_item_add_msdf_texture_rect_region(p_canvas, Rect2(cpos, csize), texture, gl.uv_rect, modulate, p_outline_size * 2, fd->msdf_range);
  1532. } else {
  1533. Point2 cpos = p_pos.floor();
  1534. cpos += gl.rect.position;
  1535. Size2 csize = gl.rect.size;
  1536. RenderingServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas, Rect2(cpos, csize), texture, gl.uv_rect, modulate, false, false);
  1537. }
  1538. }
  1539. }
  1540. }
  1541. }
  1542. bool TextServerFallback::font_is_language_supported(RID p_font_rid, const String &p_language) const {
  1543. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  1544. ERR_FAIL_COND_V(!fd, false);
  1545. MutexLock lock(fd->mutex);
  1546. if (fd->language_support_overrides.has(p_language)) {
  1547. return fd->language_support_overrides[p_language];
  1548. } else {
  1549. return true;
  1550. }
  1551. }
  1552. void TextServerFallback::font_set_language_support_override(RID p_font_rid, const String &p_language, bool p_supported) {
  1553. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  1554. ERR_FAIL_COND(!fd);
  1555. MutexLock lock(fd->mutex);
  1556. fd->language_support_overrides[p_language] = p_supported;
  1557. }
  1558. bool TextServerFallback::font_get_language_support_override(RID p_font_rid, const String &p_language) {
  1559. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  1560. ERR_FAIL_COND_V(!fd, false);
  1561. MutexLock lock(fd->mutex);
  1562. return fd->language_support_overrides[p_language];
  1563. }
  1564. void TextServerFallback::font_remove_language_support_override(RID p_font_rid, const String &p_language) {
  1565. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  1566. ERR_FAIL_COND(!fd);
  1567. MutexLock lock(fd->mutex);
  1568. fd->language_support_overrides.erase(p_language);
  1569. }
  1570. Vector<String> TextServerFallback::font_get_language_support_overrides(RID p_font_rid) {
  1571. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  1572. ERR_FAIL_COND_V(!fd, Vector<String>());
  1573. MutexLock lock(fd->mutex);
  1574. Vector<String> out;
  1575. for (const KeyValue<String, bool> &E : fd->language_support_overrides) {
  1576. out.push_back(E.key);
  1577. }
  1578. return out;
  1579. }
  1580. bool TextServerFallback::font_is_script_supported(RID p_font_rid, const String &p_script) const {
  1581. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  1582. ERR_FAIL_COND_V(!fd, false);
  1583. MutexLock lock(fd->mutex);
  1584. if (fd->script_support_overrides.has(p_script)) {
  1585. return fd->script_support_overrides[p_script];
  1586. } else {
  1587. return true;
  1588. }
  1589. }
  1590. void TextServerFallback::font_set_script_support_override(RID p_font_rid, const String &p_script, bool p_supported) {
  1591. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  1592. ERR_FAIL_COND(!fd);
  1593. MutexLock lock(fd->mutex);
  1594. fd->script_support_overrides[p_script] = p_supported;
  1595. }
  1596. bool TextServerFallback::font_get_script_support_override(RID p_font_rid, const String &p_script) {
  1597. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  1598. ERR_FAIL_COND_V(!fd, false);
  1599. MutexLock lock(fd->mutex);
  1600. return fd->script_support_overrides[p_script];
  1601. }
  1602. void TextServerFallback::font_remove_script_support_override(RID p_font_rid, const String &p_script) {
  1603. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  1604. ERR_FAIL_COND(!fd);
  1605. MutexLock lock(fd->mutex);
  1606. Vector2i size = _get_size(fd, 16);
  1607. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1608. fd->script_support_overrides.erase(p_script);
  1609. }
  1610. Vector<String> TextServerFallback::font_get_script_support_overrides(RID p_font_rid) {
  1611. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  1612. ERR_FAIL_COND_V(!fd, Vector<String>());
  1613. MutexLock lock(fd->mutex);
  1614. Vector<String> out;
  1615. for (const KeyValue<String, bool> &E : fd->script_support_overrides) {
  1616. out.push_back(E.key);
  1617. }
  1618. return out;
  1619. }
  1620. void TextServerFallback::font_set_opentype_feature_overrides(RID p_font_rid, const Dictionary &p_overrides) {
  1621. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  1622. ERR_FAIL_COND(!fd);
  1623. MutexLock lock(fd->mutex);
  1624. Vector2i size = _get_size(fd, 16);
  1625. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1626. fd->feature_overrides = p_overrides;
  1627. }
  1628. Dictionary TextServerFallback::font_get_opentype_feature_overrides(RID p_font_rid) const {
  1629. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  1630. ERR_FAIL_COND_V(!fd, Dictionary());
  1631. MutexLock lock(fd->mutex);
  1632. return fd->feature_overrides;
  1633. }
  1634. Dictionary TextServerFallback::font_supported_feature_list(RID p_font_rid) const {
  1635. return Dictionary();
  1636. }
  1637. Dictionary TextServerFallback::font_supported_variation_list(RID p_font_rid) const {
  1638. FontDataFallback *fd = font_owner.get_or_null(p_font_rid);
  1639. ERR_FAIL_COND_V(!fd, Dictionary());
  1640. MutexLock lock(fd->mutex);
  1641. Vector2i size = _get_size(fd, 16);
  1642. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), Dictionary());
  1643. return fd->supported_varaitions;
  1644. }
  1645. float TextServerFallback::font_get_global_oversampling() const {
  1646. return oversampling;
  1647. }
  1648. void TextServerFallback::font_set_global_oversampling(float p_oversampling) {
  1649. _THREAD_SAFE_METHOD_
  1650. if (oversampling != p_oversampling) {
  1651. oversampling = p_oversampling;
  1652. List<RID> fonts;
  1653. font_owner.get_owned_list(&fonts);
  1654. bool font_cleared = false;
  1655. for (const RID &E : fonts) {
  1656. if (!font_is_multichannel_signed_distance_field(E) && font_get_oversampling(E) <= 0) {
  1657. font_clear_size_cache(E);
  1658. font_cleared = true;
  1659. }
  1660. }
  1661. if (font_cleared) {
  1662. List<RID> text_bufs;
  1663. shaped_owner.get_owned_list(&text_bufs);
  1664. for (const RID &E : text_bufs) {
  1665. invalidate(shaped_owner.get_or_null(E));
  1666. }
  1667. }
  1668. }
  1669. }
  1670. /*************************************************************************/
  1671. /* Shaped text buffer interface */
  1672. /*************************************************************************/
  1673. void TextServerFallback::invalidate(ShapedTextData *p_shaped) {
  1674. p_shaped->valid = false;
  1675. p_shaped->sort_valid = false;
  1676. p_shaped->line_breaks_valid = false;
  1677. p_shaped->justification_ops_valid = false;
  1678. p_shaped->ascent = 0.f;
  1679. p_shaped->descent = 0.f;
  1680. p_shaped->width = 0.f;
  1681. p_shaped->upos = 0.f;
  1682. p_shaped->uthk = 0.f;
  1683. p_shaped->glyphs.clear();
  1684. p_shaped->glyphs_logical.clear();
  1685. }
  1686. void TextServerFallback::full_copy(ShapedTextData *p_shaped) {
  1687. ShapedTextData *parent = shaped_owner.get_or_null(p_shaped->parent);
  1688. for (const KeyValue<Variant, ShapedTextData::EmbeddedObject> &E : parent->objects) {
  1689. if (E.value.pos >= p_shaped->start && E.value.pos < p_shaped->end) {
  1690. p_shaped->objects[E.key] = E.value;
  1691. }
  1692. }
  1693. for (int k = 0; k < parent->spans.size(); k++) {
  1694. ShapedTextData::Span span = parent->spans[k];
  1695. if (span.start >= p_shaped->end || span.end <= p_shaped->start) {
  1696. continue;
  1697. }
  1698. span.start = MAX(p_shaped->start, span.start);
  1699. span.end = MIN(p_shaped->end, span.end);
  1700. p_shaped->spans.push_back(span);
  1701. }
  1702. p_shaped->parent = RID();
  1703. }
  1704. RID TextServerFallback::create_shaped_text(TextServer::Direction p_direction, TextServer::Orientation p_orientation) {
  1705. _THREAD_SAFE_METHOD_
  1706. ShapedTextData *sd = memnew(ShapedTextData);
  1707. sd->direction = p_direction;
  1708. sd->orientation = p_orientation;
  1709. return shaped_owner.make_rid(sd);
  1710. }
  1711. void TextServerFallback::shaped_text_clear(RID p_shaped) {
  1712. ShapedTextData *sd = shaped_owner.get_or_null(p_shaped);
  1713. ERR_FAIL_COND(!sd);
  1714. MutexLock lock(sd->mutex);
  1715. sd->parent = RID();
  1716. sd->start = 0;
  1717. sd->end = 0;
  1718. sd->text = String();
  1719. sd->spans.clear();
  1720. sd->objects.clear();
  1721. invalidate(sd);
  1722. }
  1723. void TextServerFallback::shaped_text_set_direction(RID p_shaped, TextServer::Direction p_direction) {
  1724. if (p_direction == DIRECTION_RTL) {
  1725. ERR_PRINT_ONCE("Right-to-left layout is not supported by this text server.");
  1726. }
  1727. }
  1728. TextServer::Direction TextServerFallback::shaped_text_get_direction(RID p_shaped) const {
  1729. return TextServer::DIRECTION_LTR;
  1730. }
  1731. TextServer::Direction TextServerFallback::shaped_text_get_inferred_direction(RID p_shaped) const {
  1732. return TextServer::DIRECTION_LTR;
  1733. }
  1734. void TextServerFallback::shaped_text_set_custom_punctuation(RID p_shaped, const String &p_punct) {
  1735. _THREAD_SAFE_METHOD_
  1736. ShapedTextData *sd = shaped_owner.get_or_null(p_shaped);
  1737. ERR_FAIL_COND(!sd);
  1738. if (sd->custom_punct != p_punct) {
  1739. if (sd->parent != RID()) {
  1740. full_copy(sd);
  1741. }
  1742. sd->custom_punct = p_punct;
  1743. invalidate(sd);
  1744. }
  1745. }
  1746. String TextServerFallback::shaped_text_get_custom_punctuation(RID p_shaped) const {
  1747. _THREAD_SAFE_METHOD_
  1748. const ShapedTextData *sd = shaped_owner.get_or_null(p_shaped);
  1749. ERR_FAIL_COND_V(!sd, String());
  1750. return sd->custom_punct;
  1751. }
  1752. void TextServerFallback::shaped_text_set_orientation(RID p_shaped, TextServer::Orientation p_orientation) {
  1753. ShapedTextData *sd = shaped_owner.get_or_null(p_shaped);
  1754. ERR_FAIL_COND(!sd);
  1755. MutexLock lock(sd->mutex);
  1756. if (sd->orientation != p_orientation) {
  1757. if (sd->parent != RID()) {
  1758. full_copy(sd);
  1759. }
  1760. sd->orientation = p_orientation;
  1761. invalidate(sd);
  1762. }
  1763. }
  1764. void TextServerFallback::shaped_text_set_bidi_override(RID p_shaped, const Array &p_override) {
  1765. // No BiDi support, ignore.
  1766. }
  1767. TextServer::Orientation TextServerFallback::shaped_text_get_orientation(RID p_shaped) const {
  1768. const ShapedTextData *sd = shaped_owner.get_or_null(p_shaped);
  1769. ERR_FAIL_COND_V(!sd, TextServer::ORIENTATION_HORIZONTAL);
  1770. MutexLock lock(sd->mutex);
  1771. return sd->orientation;
  1772. }
  1773. void TextServerFallback::shaped_text_set_preserve_invalid(RID p_shaped, bool p_enabled) {
  1774. ShapedTextData *sd = shaped_owner.get_or_null(p_shaped);
  1775. MutexLock lock(sd->mutex);
  1776. ERR_FAIL_COND(!sd);
  1777. if (sd->preserve_invalid != p_enabled) {
  1778. if (sd->parent != RID()) {
  1779. full_copy(sd);
  1780. }
  1781. sd->preserve_invalid = p_enabled;
  1782. invalidate(sd);
  1783. }
  1784. }
  1785. bool TextServerFallback::shaped_text_get_preserve_invalid(RID p_shaped) const {
  1786. const ShapedTextData *sd = shaped_owner.get_or_null(p_shaped);
  1787. ERR_FAIL_COND_V(!sd, false);
  1788. MutexLock lock(sd->mutex);
  1789. return sd->preserve_invalid;
  1790. }
  1791. void TextServerFallback::shaped_text_set_preserve_control(RID p_shaped, bool p_enabled) {
  1792. ShapedTextData *sd = shaped_owner.get_or_null(p_shaped);
  1793. ERR_FAIL_COND(!sd);
  1794. MutexLock lock(sd->mutex);
  1795. if (sd->preserve_control != p_enabled) {
  1796. if (sd->parent != RID()) {
  1797. full_copy(sd);
  1798. }
  1799. sd->preserve_control = p_enabled;
  1800. invalidate(sd);
  1801. }
  1802. }
  1803. bool TextServerFallback::shaped_text_get_preserve_control(RID p_shaped) const {
  1804. const ShapedTextData *sd = shaped_owner.get_or_null(p_shaped);
  1805. ERR_FAIL_COND_V(!sd, false);
  1806. MutexLock lock(sd->mutex);
  1807. return sd->preserve_control;
  1808. }
  1809. bool TextServerFallback::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) {
  1810. ShapedTextData *sd = shaped_owner.get_or_null(p_shaped);
  1811. ERR_FAIL_COND_V(!sd, false);
  1812. MutexLock lock(sd->mutex);
  1813. ERR_FAIL_COND_V(p_size <= 0, false);
  1814. for (int i = 0; i < p_fonts.size(); i++) {
  1815. ERR_FAIL_COND_V(!font_owner.get_or_null(p_fonts[i]), false);
  1816. }
  1817. if (p_text.is_empty()) {
  1818. return true;
  1819. }
  1820. if (sd->parent != RID()) {
  1821. full_copy(sd);
  1822. }
  1823. ShapedTextData::Span span;
  1824. span.start = sd->text.length();
  1825. span.end = span.start + p_text.length();
  1826. // Pre-sort fonts, push fonts with the language support first.
  1827. Vector<RID> fonts_no_match;
  1828. int font_count = p_fonts.size();
  1829. for (int i = 0; i < font_count; i++) {
  1830. if (font_is_language_supported(p_fonts[i], p_language)) {
  1831. span.fonts.push_back(p_fonts[i]);
  1832. } else {
  1833. fonts_no_match.push_back(p_fonts[i]);
  1834. }
  1835. }
  1836. span.fonts.append_array(fonts_no_match);
  1837. ERR_FAIL_COND_V(span.fonts.is_empty(), false);
  1838. span.font_size = p_size;
  1839. span.language = p_language;
  1840. sd->spans.push_back(span);
  1841. sd->text += p_text;
  1842. sd->end += p_text.length();
  1843. invalidate(sd);
  1844. return true;
  1845. }
  1846. bool TextServerFallback::shaped_text_add_object(RID p_shaped, Variant p_key, const Size2 &p_size, InlineAlignment p_inline_align, int p_length) {
  1847. ShapedTextData *sd = shaped_owner.get_or_null(p_shaped);
  1848. ERR_FAIL_COND_V(!sd, false);
  1849. MutexLock lock(sd->mutex);
  1850. ERR_FAIL_COND_V(p_key == Variant(), false);
  1851. ERR_FAIL_COND_V(sd->objects.has(p_key), false);
  1852. if (sd->parent != RID()) {
  1853. full_copy(sd);
  1854. }
  1855. ShapedTextData::Span span;
  1856. span.start = sd->start + sd->text.length();
  1857. span.end = span.start + p_length;
  1858. span.embedded_key = p_key;
  1859. ShapedTextData::EmbeddedObject obj;
  1860. obj.inline_align = p_inline_align;
  1861. obj.rect.size = p_size;
  1862. obj.pos = span.start;
  1863. sd->spans.push_back(span);
  1864. sd->text += String::chr(0xfffc).repeat(p_length);
  1865. sd->end += p_length;
  1866. sd->objects[p_key] = obj;
  1867. invalidate(sd);
  1868. return true;
  1869. }
  1870. bool TextServerFallback::shaped_text_resize_object(RID p_shaped, Variant p_key, const Size2 &p_size, InlineAlignment p_inline_align) {
  1871. ShapedTextData *sd = shaped_owner.get_or_null(p_shaped);
  1872. ERR_FAIL_COND_V(!sd, false);
  1873. MutexLock lock(sd->mutex);
  1874. ERR_FAIL_COND_V(!sd->objects.has(p_key), false);
  1875. sd->objects[p_key].rect.size = p_size;
  1876. sd->objects[p_key].inline_align = p_inline_align;
  1877. if (sd->valid) {
  1878. // Recalc string metrics.
  1879. sd->ascent = 0;
  1880. sd->descent = 0;
  1881. sd->width = 0;
  1882. sd->upos = 0;
  1883. sd->uthk = 0;
  1884. int sd_size = sd->glyphs.size();
  1885. for (int i = 0; i < sd_size; i++) {
  1886. Glyph gl = sd->glyphs[i];
  1887. Variant key;
  1888. if (gl.count == 1) {
  1889. for (const KeyValue<Variant, ShapedTextData::EmbeddedObject> &E : sd->objects) {
  1890. if (E.value.pos == gl.start) {
  1891. key = E.key;
  1892. break;
  1893. }
  1894. }
  1895. }
  1896. if (key != Variant()) {
  1897. if (sd->orientation == ORIENTATION_HORIZONTAL) {
  1898. sd->objects[key].rect.position.x = sd->width;
  1899. sd->width += sd->objects[key].rect.size.x;
  1900. sd->glyphs.write[i].advance = sd->objects[key].rect.size.x;
  1901. } else {
  1902. sd->objects[key].rect.position.y = sd->width;
  1903. sd->width += sd->objects[key].rect.size.y;
  1904. sd->glyphs.write[i].advance = sd->objects[key].rect.size.y;
  1905. }
  1906. } else {
  1907. if (gl.font_rid.is_valid()) {
  1908. if (sd->orientation == ORIENTATION_HORIZONTAL) {
  1909. sd->ascent = MAX(sd->ascent, font_get_ascent(gl.font_rid, gl.font_size));
  1910. sd->descent = MAX(sd->descent, font_get_descent(gl.font_rid, gl.font_size));
  1911. } else {
  1912. sd->ascent = MAX(sd->ascent, Math::round(font_get_glyph_advance(gl.font_rid, gl.font_size, gl.index).x * 0.5));
  1913. sd->descent = MAX(sd->descent, Math::round(font_get_glyph_advance(gl.font_rid, gl.font_size, gl.index).x * 0.5));
  1914. }
  1915. sd->upos = MAX(sd->upos, font_get_underline_position(gl.font_rid, gl.font_size));
  1916. sd->uthk = MAX(sd->uthk, font_get_underline_thickness(gl.font_rid, gl.font_size));
  1917. } else if (sd->preserve_invalid || (sd->preserve_control && is_control(gl.index))) {
  1918. // Glyph not found, replace with hex code box.
  1919. if (sd->orientation == ORIENTATION_HORIZONTAL) {
  1920. sd->ascent = MAX(sd->ascent, get_hex_code_box_size(gl.font_size, gl.index).y);
  1921. } else {
  1922. sd->ascent = MAX(sd->ascent, Math::round(get_hex_code_box_size(gl.font_size, gl.index).x * 0.5f));
  1923. sd->descent = MAX(sd->descent, Math::round(get_hex_code_box_size(gl.font_size, gl.index).x * 0.5f));
  1924. }
  1925. }
  1926. sd->width += gl.advance * gl.repeat;
  1927. }
  1928. }
  1929. // Align embedded objects to baseline.
  1930. float full_ascent = sd->ascent;
  1931. float full_descent = sd->descent;
  1932. for (KeyValue<Variant, ShapedTextData::EmbeddedObject> &E : sd->objects) {
  1933. if ((E.value.pos >= sd->start) && (E.value.pos < sd->end)) {
  1934. if (sd->orientation == ORIENTATION_HORIZONTAL) {
  1935. switch (E.value.inline_align & INLINE_ALIGNMENT_TEXT_MASK) {
  1936. case INLINE_ALIGNMENT_TO_TOP: {
  1937. E.value.rect.position.y = -sd->ascent;
  1938. } break;
  1939. case INLINE_ALIGNMENT_TO_CENTER: {
  1940. E.value.rect.position.y = (-sd->ascent + sd->descent) / 2;
  1941. } break;
  1942. case INLINE_ALIGNMENT_TO_BASELINE: {
  1943. E.value.rect.position.y = 0;
  1944. } break;
  1945. case INLINE_ALIGNMENT_TO_BOTTOM: {
  1946. E.value.rect.position.y = sd->descent;
  1947. } break;
  1948. }
  1949. switch (E.value.inline_align & INLINE_ALIGNMENT_IMAGE_MASK) {
  1950. case INLINE_ALIGNMENT_BOTTOM_TO: {
  1951. E.value.rect.position.y -= E.value.rect.size.y;
  1952. } break;
  1953. case INLINE_ALIGNMENT_CENTER_TO: {
  1954. E.value.rect.position.y -= E.value.rect.size.y / 2;
  1955. } break;
  1956. case INLINE_ALIGNMENT_TOP_TO: {
  1957. // NOP
  1958. } break;
  1959. }
  1960. full_ascent = MAX(full_ascent, -E.value.rect.position.y);
  1961. full_descent = MAX(full_descent, E.value.rect.position.y + E.value.rect.size.y);
  1962. } else {
  1963. switch (E.value.inline_align & INLINE_ALIGNMENT_TEXT_MASK) {
  1964. case INLINE_ALIGNMENT_TO_TOP: {
  1965. E.value.rect.position.x = -sd->ascent;
  1966. } break;
  1967. case INLINE_ALIGNMENT_TO_CENTER: {
  1968. E.value.rect.position.x = (-sd->ascent + sd->descent) / 2;
  1969. } break;
  1970. case INLINE_ALIGNMENT_TO_BASELINE: {
  1971. E.value.rect.position.x = 0;
  1972. } break;
  1973. case INLINE_ALIGNMENT_TO_BOTTOM: {
  1974. E.value.rect.position.x = sd->descent;
  1975. } break;
  1976. }
  1977. switch (E.value.inline_align & INLINE_ALIGNMENT_IMAGE_MASK) {
  1978. case INLINE_ALIGNMENT_BOTTOM_TO: {
  1979. E.value.rect.position.x -= E.value.rect.size.x;
  1980. } break;
  1981. case INLINE_ALIGNMENT_CENTER_TO: {
  1982. E.value.rect.position.x -= E.value.rect.size.x / 2;
  1983. } break;
  1984. case INLINE_ALIGNMENT_TOP_TO: {
  1985. // NOP
  1986. } break;
  1987. }
  1988. full_ascent = MAX(full_ascent, -E.value.rect.position.x);
  1989. full_descent = MAX(full_descent, E.value.rect.position.x + E.value.rect.size.x);
  1990. }
  1991. }
  1992. }
  1993. sd->ascent = full_ascent;
  1994. sd->descent = full_descent;
  1995. }
  1996. return true;
  1997. }
  1998. RID TextServerFallback::shaped_text_substr(RID p_shaped, int p_start, int p_length) const {
  1999. const ShapedTextData *sd = shaped_owner.get_or_null(p_shaped);
  2000. ERR_FAIL_COND_V(!sd, RID());
  2001. MutexLock lock(sd->mutex);
  2002. if (sd->parent != RID()) {
  2003. return shaped_text_substr(sd->parent, p_start, p_length);
  2004. }
  2005. if (!sd->valid) {
  2006. const_cast<TextServerFallback *>(this)->shaped_text_shape(p_shaped);
  2007. }
  2008. ERR_FAIL_COND_V(p_start < 0 || p_length < 0, RID());
  2009. ERR_FAIL_COND_V(sd->start > p_start || sd->end < p_start, RID());
  2010. ERR_FAIL_COND_V(sd->end < p_start + p_length, RID());
  2011. ShapedTextData *new_sd = memnew(ShapedTextData);
  2012. new_sd->parent = p_shaped;
  2013. new_sd->start = p_start;
  2014. new_sd->end = p_start + p_length;
  2015. new_sd->orientation = sd->orientation;
  2016. new_sd->direction = sd->direction;
  2017. new_sd->custom_punct = sd->custom_punct;
  2018. new_sd->para_direction = sd->para_direction;
  2019. new_sd->line_breaks_valid = sd->line_breaks_valid;
  2020. new_sd->justification_ops_valid = sd->justification_ops_valid;
  2021. new_sd->sort_valid = false;
  2022. new_sd->upos = sd->upos;
  2023. new_sd->uthk = sd->uthk;
  2024. if (p_length > 0) {
  2025. new_sd->text = sd->text.substr(p_start - sd->start, p_length);
  2026. int sd_size = sd->glyphs.size();
  2027. const Glyph *sd_glyphs = sd->glyphs.ptr();
  2028. for (int i = 0; i < sd_size; i++) {
  2029. if ((sd_glyphs[i].start >= new_sd->start) && (sd_glyphs[i].end <= new_sd->end)) {
  2030. Glyph gl = sd_glyphs[i];
  2031. Variant key;
  2032. bool find_embedded = false;
  2033. if (gl.count == 1) {
  2034. for (const KeyValue<Variant, ShapedTextData::EmbeddedObject> &E : sd->objects) {
  2035. if (E.value.pos == gl.start) {
  2036. find_embedded = true;
  2037. key = E.key;
  2038. new_sd->objects[key] = E.value;
  2039. break;
  2040. }
  2041. }
  2042. }
  2043. if (find_embedded) {
  2044. if (new_sd->orientation == ORIENTATION_HORIZONTAL) {
  2045. new_sd->objects[key].rect.position.x = new_sd->width;
  2046. new_sd->width += new_sd->objects[key].rect.size.x;
  2047. } else {
  2048. new_sd->objects[key].rect.position.y = new_sd->width;
  2049. new_sd->width += new_sd->objects[key].rect.size.y;
  2050. }
  2051. } else {
  2052. if (gl.font_rid.is_valid()) {
  2053. if (new_sd->orientation == ORIENTATION_HORIZONTAL) {
  2054. new_sd->ascent = MAX(new_sd->ascent, font_get_ascent(gl.font_rid, gl.font_size));
  2055. new_sd->descent = MAX(new_sd->descent, font_get_descent(gl.font_rid, gl.font_size));
  2056. } else {
  2057. new_sd->ascent = MAX(new_sd->ascent, Math::round(font_get_glyph_advance(gl.font_rid, gl.font_size, gl.index).x * 0.5));
  2058. new_sd->descent = MAX(new_sd->descent, Math::round(font_get_glyph_advance(gl.font_rid, gl.font_size, gl.index).x * 0.5));
  2059. }
  2060. } else if (new_sd->preserve_invalid || (new_sd->preserve_control && is_control(gl.index))) {
  2061. // Glyph not found, replace with hex code box.
  2062. if (new_sd->orientation == ORIENTATION_HORIZONTAL) {
  2063. new_sd->ascent = MAX(new_sd->ascent, get_hex_code_box_size(gl.font_size, gl.index).y);
  2064. } else {
  2065. new_sd->ascent = MAX(new_sd->ascent, Math::round(get_hex_code_box_size(gl.font_size, gl.index).x * 0.5f));
  2066. new_sd->descent = MAX(new_sd->descent, Math::round(get_hex_code_box_size(gl.font_size, gl.index).x * 0.5f));
  2067. }
  2068. }
  2069. new_sd->width += gl.advance * gl.repeat;
  2070. }
  2071. new_sd->glyphs.push_back(gl);
  2072. }
  2073. }
  2074. // Align embedded objects to baseline.
  2075. float full_ascent = new_sd->ascent;
  2076. float full_descent = new_sd->descent;
  2077. for (KeyValue<Variant, ShapedTextData::EmbeddedObject> &E : new_sd->objects) {
  2078. if ((E.value.pos >= new_sd->start) && (E.value.pos < new_sd->end)) {
  2079. if (sd->orientation == ORIENTATION_HORIZONTAL) {
  2080. switch (E.value.inline_align & INLINE_ALIGNMENT_TEXT_MASK) {
  2081. case INLINE_ALIGNMENT_TO_TOP: {
  2082. E.value.rect.position.y = -new_sd->ascent;
  2083. } break;
  2084. case INLINE_ALIGNMENT_TO_CENTER: {
  2085. E.value.rect.position.y = (-new_sd->ascent + new_sd->descent) / 2;
  2086. } break;
  2087. case INLINE_ALIGNMENT_TO_BASELINE: {
  2088. E.value.rect.position.y = 0;
  2089. } break;
  2090. case INLINE_ALIGNMENT_TO_BOTTOM: {
  2091. E.value.rect.position.y = new_sd->descent;
  2092. } break;
  2093. }
  2094. switch (E.value.inline_align & INLINE_ALIGNMENT_IMAGE_MASK) {
  2095. case INLINE_ALIGNMENT_BOTTOM_TO: {
  2096. E.value.rect.position.y -= E.value.rect.size.y;
  2097. } break;
  2098. case INLINE_ALIGNMENT_CENTER_TO: {
  2099. E.value.rect.position.y -= E.value.rect.size.y / 2;
  2100. } break;
  2101. case INLINE_ALIGNMENT_TOP_TO: {
  2102. // NOP
  2103. } break;
  2104. }
  2105. full_ascent = MAX(full_ascent, -E.value.rect.position.y);
  2106. full_descent = MAX(full_descent, E.value.rect.position.y + E.value.rect.size.y);
  2107. } else {
  2108. switch (E.value.inline_align & INLINE_ALIGNMENT_TEXT_MASK) {
  2109. case INLINE_ALIGNMENT_TO_TOP: {
  2110. E.value.rect.position.x = -new_sd->ascent;
  2111. } break;
  2112. case INLINE_ALIGNMENT_TO_CENTER: {
  2113. E.value.rect.position.x = (-new_sd->ascent + new_sd->descent) / 2;
  2114. } break;
  2115. case INLINE_ALIGNMENT_TO_BASELINE: {
  2116. E.value.rect.position.x = 0;
  2117. } break;
  2118. case INLINE_ALIGNMENT_TO_BOTTOM: {
  2119. E.value.rect.position.x = new_sd->descent;
  2120. } break;
  2121. }
  2122. switch (E.value.inline_align & INLINE_ALIGNMENT_IMAGE_MASK) {
  2123. case INLINE_ALIGNMENT_BOTTOM_TO: {
  2124. E.value.rect.position.x -= E.value.rect.size.x;
  2125. } break;
  2126. case INLINE_ALIGNMENT_CENTER_TO: {
  2127. E.value.rect.position.x -= E.value.rect.size.x / 2;
  2128. } break;
  2129. case INLINE_ALIGNMENT_TOP_TO: {
  2130. // NOP
  2131. } break;
  2132. }
  2133. full_ascent = MAX(full_ascent, -E.value.rect.position.x);
  2134. full_descent = MAX(full_descent, E.value.rect.position.x + E.value.rect.size.x);
  2135. }
  2136. }
  2137. }
  2138. new_sd->ascent = full_ascent;
  2139. new_sd->descent = full_descent;
  2140. }
  2141. new_sd->valid = true;
  2142. return shaped_owner.make_rid(new_sd);
  2143. }
  2144. RID TextServerFallback::shaped_text_get_parent(RID p_shaped) const {
  2145. ShapedTextData *sd = shaped_owner.get_or_null(p_shaped);
  2146. ERR_FAIL_COND_V(!sd, RID());
  2147. MutexLock lock(sd->mutex);
  2148. return sd->parent;
  2149. }
  2150. float TextServerFallback::shaped_text_fit_to_width(RID p_shaped, float p_width, uint16_t /*JustificationFlag*/ p_jst_flags) {
  2151. ShapedTextData *sd = shaped_owner.get_or_null(p_shaped);
  2152. ERR_FAIL_COND_V(!sd, 0.f);
  2153. MutexLock lock(sd->mutex);
  2154. if (!sd->valid) {
  2155. const_cast<TextServerFallback *>(this)->shaped_text_shape(p_shaped);
  2156. }
  2157. if (!sd->justification_ops_valid) {
  2158. const_cast<TextServerFallback *>(this)->shaped_text_update_justification_ops(p_shaped);
  2159. }
  2160. int start_pos = 0;
  2161. int end_pos = sd->glyphs.size() - 1;
  2162. if ((p_jst_flags & JUSTIFICATION_AFTER_LAST_TAB) == JUSTIFICATION_AFTER_LAST_TAB) {
  2163. int start, end, delta;
  2164. if (sd->para_direction == DIRECTION_LTR) {
  2165. start = sd->glyphs.size() - 1;
  2166. end = -1;
  2167. delta = -1;
  2168. } else {
  2169. start = 0;
  2170. end = sd->glyphs.size();
  2171. delta = +1;
  2172. }
  2173. for (int i = start; i != end; i += delta) {
  2174. if ((sd->glyphs[i].flags & GRAPHEME_IS_TAB) == GRAPHEME_IS_TAB) {
  2175. if (sd->para_direction == DIRECTION_LTR) {
  2176. start_pos = i;
  2177. break;
  2178. } else {
  2179. end_pos = i;
  2180. break;
  2181. }
  2182. }
  2183. }
  2184. }
  2185. float justification_width;
  2186. if ((p_jst_flags & JUSTIFICATION_CONSTRAIN_ELLIPSIS) == JUSTIFICATION_CONSTRAIN_ELLIPSIS) {
  2187. if (sd->overrun_trim_data.trim_pos >= 0) {
  2188. end_pos = sd->overrun_trim_data.trim_pos;
  2189. justification_width = sd->width_trimmed;
  2190. } else {
  2191. return sd->width;
  2192. }
  2193. } else {
  2194. justification_width = sd->width;
  2195. }
  2196. if ((p_jst_flags & JUSTIFICATION_TRIM_EDGE_SPACES) == JUSTIFICATION_TRIM_EDGE_SPACES) {
  2197. // Trim spaces.
  2198. 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)) {
  2199. justification_width -= sd->glyphs[start_pos].advance * sd->glyphs[start_pos].repeat;
  2200. sd->glyphs.write[start_pos].advance = 0;
  2201. start_pos += sd->glyphs[start_pos].count;
  2202. }
  2203. 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)) {
  2204. justification_width -= sd->glyphs[end_pos].advance * sd->glyphs[end_pos].repeat;
  2205. sd->glyphs.write[end_pos].advance = 0;
  2206. end_pos -= sd->glyphs[end_pos].count;
  2207. }
  2208. } else {
  2209. // Skip breaks, but do not reset size.
  2210. while ((start_pos < end_pos) && ((sd->glyphs[start_pos].flags & GRAPHEME_IS_BREAK_HARD) == GRAPHEME_IS_BREAK_HARD)) {
  2211. start_pos += sd->glyphs[start_pos].count;
  2212. }
  2213. while ((start_pos < end_pos) && ((sd->glyphs[end_pos].flags & GRAPHEME_IS_BREAK_HARD) == GRAPHEME_IS_BREAK_HARD)) {
  2214. end_pos -= sd->glyphs[end_pos].count;
  2215. }
  2216. }
  2217. int space_count = 0;
  2218. for (int i = start_pos; i <= end_pos; i++) {
  2219. const Glyph &gl = sd->glyphs[i];
  2220. if (gl.count > 0) {
  2221. if ((gl.flags & GRAPHEME_IS_SPACE) == GRAPHEME_IS_SPACE) {
  2222. space_count++;
  2223. }
  2224. }
  2225. }
  2226. if ((space_count > 0) && ((p_jst_flags & JUSTIFICATION_WORD_BOUND) == JUSTIFICATION_WORD_BOUND)) {
  2227. float delta_width_per_space = (p_width - justification_width) / space_count;
  2228. for (int i = start_pos; i <= end_pos; i++) {
  2229. Glyph &gl = sd->glyphs.write[i];
  2230. if (gl.count > 0) {
  2231. if ((gl.flags & GRAPHEME_IS_SPACE) == GRAPHEME_IS_SPACE) {
  2232. float old_adv = gl.advance;
  2233. gl.advance = MAX(gl.advance + delta_width_per_space, Math::round(0.1 * gl.font_size));
  2234. justification_width += (gl.advance - old_adv);
  2235. }
  2236. }
  2237. }
  2238. }
  2239. if (Math::floor(p_width) < Math::floor(justification_width)) {
  2240. sd->fit_width_minimum_reached = true;
  2241. }
  2242. if ((p_jst_flags & JUSTIFICATION_CONSTRAIN_ELLIPSIS) != JUSTIFICATION_CONSTRAIN_ELLIPSIS) {
  2243. sd->width = justification_width;
  2244. }
  2245. return justification_width;
  2246. }
  2247. float TextServerFallback::shaped_text_tab_align(RID p_shaped, const PackedFloat32Array &p_tab_stops) {
  2248. ShapedTextData *sd = shaped_owner.get_or_null(p_shaped);
  2249. ERR_FAIL_COND_V(!sd, 0.f);
  2250. MutexLock lock(sd->mutex);
  2251. if (!sd->valid) {
  2252. const_cast<TextServerFallback *>(this)->shaped_text_shape(p_shaped);
  2253. }
  2254. if (!sd->line_breaks_valid) {
  2255. const_cast<TextServerFallback *>(this)->shaped_text_update_breaks(p_shaped);
  2256. }
  2257. for (int i = 0; i < p_tab_stops.size(); i++) {
  2258. if (p_tab_stops[i] <= 0) {
  2259. return 0.f;
  2260. }
  2261. }
  2262. int tab_index = 0;
  2263. float off = 0.f;
  2264. int start, end, delta;
  2265. if (sd->para_direction == DIRECTION_LTR) {
  2266. start = 0;
  2267. end = sd->glyphs.size();
  2268. delta = +1;
  2269. } else {
  2270. start = sd->glyphs.size() - 1;
  2271. end = -1;
  2272. delta = -1;
  2273. }
  2274. Glyph *gl = sd->glyphs.ptrw();
  2275. for (int i = start; i != end; i += delta) {
  2276. if ((gl[i].flags & GRAPHEME_IS_TAB) == GRAPHEME_IS_TAB) {
  2277. float tab_off = 0.f;
  2278. while (tab_off <= off) {
  2279. tab_off += p_tab_stops[tab_index];
  2280. tab_index++;
  2281. if (tab_index >= p_tab_stops.size()) {
  2282. tab_index = 0;
  2283. }
  2284. }
  2285. float old_adv = gl[i].advance;
  2286. gl[i].advance = tab_off - off;
  2287. sd->width += gl[i].advance - old_adv;
  2288. off = 0;
  2289. continue;
  2290. }
  2291. off += gl[i].advance * gl[i].repeat;
  2292. }
  2293. return 0.f;
  2294. }
  2295. bool TextServerFallback::shaped_text_update_breaks(RID p_shaped) {
  2296. ShapedTextData *sd = shaped_owner.get_or_null(p_shaped);
  2297. ERR_FAIL_COND_V(!sd, false);
  2298. MutexLock lock(sd->mutex);
  2299. if (!sd->valid) {
  2300. shaped_text_shape(p_shaped);
  2301. }
  2302. if (sd->line_breaks_valid) {
  2303. return true; // Nothing to do.
  2304. }
  2305. int sd_size = sd->glyphs.size();
  2306. Glyph *sd_glyphs = sd->glyphs.ptrw();
  2307. int c_punct_size = sd->custom_punct.length();
  2308. const char32_t *c_punct = sd->custom_punct.ptr();
  2309. for (int i = 0; i < sd_size; i++) {
  2310. if (sd_glyphs[i].count > 0) {
  2311. char32_t c = sd->text[sd_glyphs[i].start - sd->start];
  2312. if (c_punct_size == 0) {
  2313. if (is_punct(c)) {
  2314. sd_glyphs[i].flags |= GRAPHEME_IS_PUNCTUATION;
  2315. }
  2316. } else {
  2317. for (int j = 0; j < c_punct_size; j++) {
  2318. if (c_punct[j] == c) {
  2319. sd_glyphs[i].flags |= GRAPHEME_IS_PUNCTUATION;
  2320. break;
  2321. }
  2322. }
  2323. }
  2324. if (is_underscore(c)) {
  2325. sd->glyphs.write[i].flags |= GRAPHEME_IS_UNDERSCORE;
  2326. }
  2327. if (is_whitespace(c) && !is_linebreak(c)) {
  2328. sd_glyphs[i].flags |= GRAPHEME_IS_SPACE;
  2329. sd_glyphs[i].flags |= GRAPHEME_IS_BREAK_SOFT;
  2330. }
  2331. if (is_linebreak(c)) {
  2332. sd_glyphs[i].flags |= GRAPHEME_IS_SPACE;
  2333. sd_glyphs[i].flags |= GRAPHEME_IS_BREAK_HARD;
  2334. }
  2335. if (c == 0x0009 || c == 0x000b) {
  2336. sd_glyphs[i].flags |= GRAPHEME_IS_TAB;
  2337. }
  2338. i += (sd_glyphs[i].count - 1);
  2339. }
  2340. }
  2341. sd->line_breaks_valid = true;
  2342. return sd->line_breaks_valid;
  2343. }
  2344. bool TextServerFallback::shaped_text_update_justification_ops(RID p_shaped) {
  2345. ShapedTextData *sd = shaped_owner.get_or_null(p_shaped);
  2346. ERR_FAIL_COND_V(!sd, false);
  2347. MutexLock lock(sd->mutex);
  2348. if (!sd->valid) {
  2349. shaped_text_shape(p_shaped);
  2350. }
  2351. if (!sd->line_breaks_valid) {
  2352. shaped_text_update_breaks(p_shaped);
  2353. }
  2354. sd->justification_ops_valid = true; // Not supported by fallback server.
  2355. return true;
  2356. }
  2357. void TextServerFallback::shaped_text_overrun_trim_to_width(RID p_shaped_line, float p_width, uint16_t p_trim_flags) {
  2358. ShapedTextData *sd = shaped_owner.get_or_null(p_shaped_line);
  2359. ERR_FAIL_COND_MSG(!sd, "ShapedTextDataFallback invalid.");
  2360. MutexLock lock(sd->mutex);
  2361. if (!sd->valid) {
  2362. shaped_text_shape(p_shaped_line);
  2363. }
  2364. sd->text_trimmed = false;
  2365. sd->overrun_trim_data.ellipsis_glyph_buf.clear();
  2366. bool add_ellipsis = (p_trim_flags & OVERRUN_ADD_ELLIPSIS) == OVERRUN_ADD_ELLIPSIS;
  2367. bool cut_per_word = (p_trim_flags & OVERRUN_TRIM_WORD_ONLY) == OVERRUN_TRIM_WORD_ONLY;
  2368. bool enforce_ellipsis = (p_trim_flags & OVERRUN_ENFORCE_ELLIPSIS) == OVERRUN_ENFORCE_ELLIPSIS;
  2369. bool justification_aware = (p_trim_flags & OVERRUN_JUSTIFICATION_AWARE) == OVERRUN_JUSTIFICATION_AWARE;
  2370. Glyph *sd_glyphs = sd->glyphs.ptrw();
  2371. if ((p_trim_flags & OVERRUN_TRIM) == OVERRUN_NO_TRIMMING || sd_glyphs == nullptr || p_width <= 0 || !(sd->width > p_width || enforce_ellipsis)) {
  2372. sd->overrun_trim_data.trim_pos = -1;
  2373. sd->overrun_trim_data.ellipsis_pos = -1;
  2374. return;
  2375. }
  2376. if (justification_aware && !sd->fit_width_minimum_reached) {
  2377. return;
  2378. }
  2379. Vector<ShapedTextData::Span> &spans = sd->spans;
  2380. if (sd->parent != RID()) {
  2381. ShapedTextData *parent_sd = shaped_owner.get_or_null(sd->parent);
  2382. ERR_FAIL_COND(!parent_sd->valid);
  2383. spans = parent_sd->spans;
  2384. }
  2385. if (spans.size() == 0) {
  2386. return;
  2387. }
  2388. int sd_size = sd->glyphs.size();
  2389. int last_gl_font_size = sd_glyphs[sd_size - 1].font_size;
  2390. // Find usable fonts, if fonts from the last glyph do not have required chars.
  2391. RID dot_gl_font_rid = sd_glyphs[sd_size - 1].font_rid;
  2392. if (!font_has_char(dot_gl_font_rid, '.')) {
  2393. const Vector<RID> &fonts = spans[spans.size() - 1].fonts;
  2394. for (const RID &font : fonts) {
  2395. if (font_has_char(font, '.')) {
  2396. dot_gl_font_rid = font;
  2397. break;
  2398. }
  2399. }
  2400. }
  2401. RID whitespace_gl_font_rid = sd_glyphs[sd_size - 1].font_rid;
  2402. if (!font_has_char(whitespace_gl_font_rid, '.')) {
  2403. const Vector<RID> &fonts = spans[spans.size() - 1].fonts;
  2404. for (const RID &font : fonts) {
  2405. if (font_has_char(font, ' ')) {
  2406. whitespace_gl_font_rid = font;
  2407. break;
  2408. }
  2409. }
  2410. }
  2411. int32_t dot_gl_idx = dot_gl_font_rid.is_valid() ? font_get_glyph_index(dot_gl_font_rid, last_gl_font_size, '.') : -10;
  2412. Vector2 dot_adv = dot_gl_font_rid.is_valid() ? font_get_glyph_advance(dot_gl_font_rid, last_gl_font_size, dot_gl_idx) : Vector2();
  2413. int32_t whitespace_gl_idx = whitespace_gl_font_rid.is_valid() ? font_get_glyph_index(whitespace_gl_font_rid, last_gl_font_size, ' ') : -10;
  2414. Vector2 whitespace_adv = whitespace_gl_font_rid.is_valid() ? font_get_glyph_advance(whitespace_gl_font_rid, last_gl_font_size, whitespace_gl_idx) : Vector2();
  2415. int ellipsis_width = 0;
  2416. if (add_ellipsis && whitespace_gl_font_rid.is_valid()) {
  2417. ellipsis_width = 3 * dot_adv.x + font_get_spacing(whitespace_gl_font_rid, last_gl_font_size, SPACING_GLYPH) + (cut_per_word ? whitespace_adv.x : 0);
  2418. }
  2419. int ell_min_characters = 6;
  2420. float width = sd->width;
  2421. int trim_pos = 0;
  2422. int ellipsis_pos = (enforce_ellipsis) ? 0 : -1;
  2423. int last_valid_cut = 0;
  2424. bool found = false;
  2425. for (int i = sd_size - 1; i != -1; i--) {
  2426. width -= sd_glyphs[i].advance * sd_glyphs[i].repeat;
  2427. if (sd_glyphs[i].count > 0) {
  2428. bool above_min_char_threshold = (i >= ell_min_characters);
  2429. if (width + (((above_min_char_threshold && add_ellipsis) || enforce_ellipsis) ? ellipsis_width : 0) <= p_width) {
  2430. if (cut_per_word && above_min_char_threshold) {
  2431. if ((sd_glyphs[i].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT) {
  2432. last_valid_cut = i;
  2433. found = true;
  2434. }
  2435. } else {
  2436. last_valid_cut = i;
  2437. found = true;
  2438. }
  2439. if (found) {
  2440. trim_pos = last_valid_cut;
  2441. if (add_ellipsis && (above_min_char_threshold || enforce_ellipsis) && width - ellipsis_width <= p_width) {
  2442. ellipsis_pos = trim_pos;
  2443. }
  2444. break;
  2445. }
  2446. }
  2447. }
  2448. }
  2449. sd->overrun_trim_data.trim_pos = trim_pos;
  2450. sd->overrun_trim_data.ellipsis_pos = ellipsis_pos;
  2451. if (trim_pos == 0 && enforce_ellipsis && add_ellipsis) {
  2452. sd->overrun_trim_data.ellipsis_pos = 0;
  2453. }
  2454. if ((trim_pos >= 0 && sd->width > p_width) || enforce_ellipsis) {
  2455. if (add_ellipsis && (ellipsis_pos > 0 || enforce_ellipsis)) {
  2456. // Insert an additional space when cutting word bound for aesthetics.
  2457. if (cut_per_word && (ellipsis_pos > 0)) {
  2458. Glyph gl;
  2459. gl.count = 1;
  2460. gl.advance = whitespace_adv.x;
  2461. gl.index = whitespace_gl_idx;
  2462. gl.font_rid = whitespace_gl_font_rid;
  2463. gl.font_size = last_gl_font_size;
  2464. gl.flags = GRAPHEME_IS_SPACE | GRAPHEME_IS_BREAK_SOFT | GRAPHEME_IS_VIRTUAL;
  2465. sd->overrun_trim_data.ellipsis_glyph_buf.append(gl);
  2466. }
  2467. // Add ellipsis dots.
  2468. if (dot_gl_idx != 0) {
  2469. Glyph gl;
  2470. gl.count = 1;
  2471. gl.repeat = 3;
  2472. gl.advance = dot_adv.x;
  2473. gl.index = dot_gl_idx;
  2474. gl.font_rid = dot_gl_font_rid;
  2475. gl.font_size = last_gl_font_size;
  2476. gl.flags = GRAPHEME_IS_PUNCTUATION | GRAPHEME_IS_VIRTUAL;
  2477. sd->overrun_trim_data.ellipsis_glyph_buf.append(gl);
  2478. }
  2479. }
  2480. sd->text_trimmed = true;
  2481. sd->width_trimmed = width + ((ellipsis_pos != -1) ? ellipsis_width : 0);
  2482. }
  2483. }
  2484. int TextServerFallback::shaped_text_get_trim_pos(RID p_shaped) const {
  2485. ShapedTextData *sd = shaped_owner.get_or_null(p_shaped);
  2486. ERR_FAIL_COND_V_MSG(!sd, -1, "ShapedTextData invalid.");
  2487. MutexLock lock(sd->mutex);
  2488. return sd->overrun_trim_data.trim_pos;
  2489. }
  2490. int TextServerFallback::shaped_text_get_ellipsis_pos(RID p_shaped) const {
  2491. ShapedTextData *sd = shaped_owner.get_or_null(p_shaped);
  2492. ERR_FAIL_COND_V_MSG(!sd, -1, "ShapedTextData invalid.");
  2493. MutexLock lock(sd->mutex);
  2494. return sd->overrun_trim_data.ellipsis_pos;
  2495. }
  2496. const Glyph *TextServerFallback::shaped_text_get_ellipsis_glyphs(RID p_shaped) const {
  2497. ShapedTextData *sd = shaped_owner.get_or_null(p_shaped);
  2498. ERR_FAIL_COND_V_MSG(!sd, nullptr, "ShapedTextData invalid.");
  2499. MutexLock lock(sd->mutex);
  2500. return sd->overrun_trim_data.ellipsis_glyph_buf.ptr();
  2501. }
  2502. int TextServerFallback::shaped_text_get_ellipsis_glyph_count(RID p_shaped) const {
  2503. ShapedTextData *sd = shaped_owner.get_or_null(p_shaped);
  2504. ERR_FAIL_COND_V_MSG(!sd, 0, "ShapedTextData invalid.");
  2505. MutexLock lock(sd->mutex);
  2506. return sd->overrun_trim_data.ellipsis_glyph_buf.size();
  2507. }
  2508. bool TextServerFallback::shaped_text_shape(RID p_shaped) {
  2509. ShapedTextData *sd = shaped_owner.get_or_null(p_shaped);
  2510. ERR_FAIL_COND_V(!sd, false);
  2511. MutexLock lock(sd->mutex);
  2512. if (sd->valid) {
  2513. return true;
  2514. }
  2515. if (sd->parent != RID()) {
  2516. full_copy(sd);
  2517. }
  2518. // Cleanup.
  2519. sd->justification_ops_valid = false;
  2520. sd->line_breaks_valid = false;
  2521. sd->ascent = 0.f;
  2522. sd->descent = 0.f;
  2523. sd->width = 0.f;
  2524. sd->glyphs.clear();
  2525. if (sd->text.length() == 0) {
  2526. sd->valid = true;
  2527. return true;
  2528. }
  2529. // "Shape" string.
  2530. for (int i = 0; i < sd->spans.size(); i++) {
  2531. const ShapedTextData::Span &span = sd->spans[i];
  2532. if (span.embedded_key != Variant()) {
  2533. // Embedded object.
  2534. if (sd->orientation == ORIENTATION_HORIZONTAL) {
  2535. sd->objects[span.embedded_key].rect.position.x = sd->width;
  2536. sd->width += sd->objects[span.embedded_key].rect.size.x;
  2537. } else {
  2538. sd->objects[span.embedded_key].rect.position.y = sd->width;
  2539. sd->width += sd->objects[span.embedded_key].rect.size.y;
  2540. }
  2541. Glyph gl;
  2542. gl.start = span.start;
  2543. gl.end = span.end;
  2544. gl.count = 1;
  2545. gl.index = 0;
  2546. gl.flags = GRAPHEME_IS_VALID | GRAPHEME_IS_VIRTUAL;
  2547. if (sd->orientation == ORIENTATION_HORIZONTAL) {
  2548. gl.advance = sd->objects[span.embedded_key].rect.size.x;
  2549. } else {
  2550. gl.advance = sd->objects[span.embedded_key].rect.size.y;
  2551. }
  2552. sd->glyphs.push_back(gl);
  2553. } else {
  2554. // Text span.
  2555. for (int j = span.start; j < span.end; j++) {
  2556. Glyph gl;
  2557. gl.start = j;
  2558. gl.end = j + 1;
  2559. gl.count = 1;
  2560. gl.font_size = span.font_size;
  2561. gl.index = (int32_t)sd->text[j - sd->start]; // Use codepoint.
  2562. if (gl.index == 0x0009 || gl.index == 0x000b) {
  2563. gl.index = 0x0020;
  2564. }
  2565. if (!sd->preserve_control && is_control(gl.index)) {
  2566. gl.index = 0x0020;
  2567. }
  2568. // Select first font which has character (font are already sorted by span language).
  2569. for (int k = 0; k < span.fonts.size(); k++) {
  2570. if (font_has_char(span.fonts[k], gl.index)) {
  2571. gl.font_rid = span.fonts[k];
  2572. break;
  2573. }
  2574. }
  2575. if (gl.font_rid.is_valid()) {
  2576. if (sd->text[j - sd->start] != 0 && !is_linebreak(sd->text[j - sd->start])) {
  2577. if (sd->orientation == ORIENTATION_HORIZONTAL) {
  2578. gl.advance = Math::round(font_get_glyph_advance(gl.font_rid, gl.font_size, gl.index).x);
  2579. gl.x_off = 0;
  2580. gl.y_off = 0;
  2581. sd->ascent = MAX(sd->ascent, font_get_ascent(gl.font_rid, gl.font_size));
  2582. sd->descent = MAX(sd->descent, font_get_descent(gl.font_rid, gl.font_size));
  2583. } else {
  2584. gl.advance = Math::round(font_get_glyph_advance(gl.font_rid, gl.font_size, gl.index).y);
  2585. gl.x_off = -Math::round(font_get_glyph_advance(gl.font_rid, gl.font_size, gl.index).x * 0.5);
  2586. gl.y_off = font_get_ascent(gl.font_rid, gl.font_size);
  2587. sd->ascent = MAX(sd->ascent, Math::round(font_get_glyph_advance(gl.font_rid, gl.font_size, gl.index).x * 0.5));
  2588. sd->descent = MAX(sd->descent, Math::round(font_get_glyph_advance(gl.font_rid, gl.font_size, gl.index).x * 0.5));
  2589. }
  2590. }
  2591. if (font_get_spacing(gl.font_rid, gl.font_size, TextServer::SPACING_SPACE) && is_whitespace(sd->text[j - sd->start])) {
  2592. gl.advance += font_get_spacing(gl.font_rid, gl.font_size, TextServer::SPACING_SPACE);
  2593. } else {
  2594. gl.advance += font_get_spacing(gl.font_rid, gl.font_size, TextServer::SPACING_GLYPH);
  2595. }
  2596. sd->upos = MAX(sd->upos, font_get_underline_position(gl.font_rid, gl.font_size));
  2597. sd->uthk = MAX(sd->uthk, font_get_underline_thickness(gl.font_rid, gl.font_size));
  2598. // Add kerning to previous glyph.
  2599. if (sd->glyphs.size() > 0) {
  2600. Glyph &prev_gl = sd->glyphs.write[sd->glyphs.size() - 1];
  2601. if (prev_gl.font_rid == gl.font_rid && prev_gl.font_size == gl.font_size) {
  2602. if (sd->orientation == ORIENTATION_HORIZONTAL) {
  2603. prev_gl.advance += font_get_kerning(gl.font_rid, gl.font_size, Vector2i(prev_gl.index, gl.index)).x;
  2604. } else {
  2605. prev_gl.advance += font_get_kerning(gl.font_rid, gl.font_size, Vector2i(prev_gl.index, gl.index)).y;
  2606. }
  2607. }
  2608. }
  2609. } else if (sd->preserve_invalid || (sd->preserve_control && is_control(gl.index))) {
  2610. // Glyph not found, replace with hex code box.
  2611. if (sd->orientation == ORIENTATION_HORIZONTAL) {
  2612. gl.advance = get_hex_code_box_size(gl.font_size, gl.index).x;
  2613. sd->ascent = MAX(sd->ascent, get_hex_code_box_size(gl.font_size, gl.index).y);
  2614. } else {
  2615. gl.advance = get_hex_code_box_size(gl.font_size, gl.index).y;
  2616. sd->ascent = MAX(sd->ascent, Math::round(get_hex_code_box_size(gl.font_size, gl.index).x * 0.5f));
  2617. sd->descent = MAX(sd->descent, Math::round(get_hex_code_box_size(gl.font_size, gl.index).x * 0.5f));
  2618. }
  2619. }
  2620. sd->width += gl.advance;
  2621. sd->glyphs.push_back(gl);
  2622. }
  2623. }
  2624. }
  2625. // Align embedded objects to baseline.
  2626. float full_ascent = sd->ascent;
  2627. float full_descent = sd->descent;
  2628. for (KeyValue<Variant, ShapedTextData::EmbeddedObject> &E : sd->objects) {
  2629. if (sd->orientation == ORIENTATION_HORIZONTAL) {
  2630. switch (E.value.inline_align & INLINE_ALIGNMENT_TEXT_MASK) {
  2631. case INLINE_ALIGNMENT_TO_TOP: {
  2632. E.value.rect.position.y = -sd->ascent;
  2633. } break;
  2634. case INLINE_ALIGNMENT_TO_CENTER: {
  2635. E.value.rect.position.y = (-sd->ascent + sd->descent) / 2;
  2636. } break;
  2637. case INLINE_ALIGNMENT_TO_BASELINE: {
  2638. E.value.rect.position.y = 0;
  2639. } break;
  2640. case INLINE_ALIGNMENT_TO_BOTTOM: {
  2641. E.value.rect.position.y = sd->descent;
  2642. } break;
  2643. }
  2644. switch (E.value.inline_align & INLINE_ALIGNMENT_IMAGE_MASK) {
  2645. case INLINE_ALIGNMENT_BOTTOM_TO: {
  2646. E.value.rect.position.y -= E.value.rect.size.y;
  2647. } break;
  2648. case INLINE_ALIGNMENT_CENTER_TO: {
  2649. E.value.rect.position.y -= E.value.rect.size.y / 2;
  2650. } break;
  2651. case INLINE_ALIGNMENT_TOP_TO: {
  2652. // NOP
  2653. } break;
  2654. }
  2655. full_ascent = MAX(full_ascent, -E.value.rect.position.y);
  2656. full_descent = MAX(full_descent, E.value.rect.position.y + E.value.rect.size.y);
  2657. } else {
  2658. switch (E.value.inline_align & INLINE_ALIGNMENT_TEXT_MASK) {
  2659. case INLINE_ALIGNMENT_TO_TOP: {
  2660. E.value.rect.position.x = -sd->ascent;
  2661. } break;
  2662. case INLINE_ALIGNMENT_TO_CENTER: {
  2663. E.value.rect.position.x = (-sd->ascent + sd->descent) / 2;
  2664. } break;
  2665. case INLINE_ALIGNMENT_TO_BASELINE: {
  2666. E.value.rect.position.x = 0;
  2667. } break;
  2668. case INLINE_ALIGNMENT_TO_BOTTOM: {
  2669. E.value.rect.position.x = sd->descent;
  2670. } break;
  2671. }
  2672. switch (E.value.inline_align & INLINE_ALIGNMENT_IMAGE_MASK) {
  2673. case INLINE_ALIGNMENT_BOTTOM_TO: {
  2674. E.value.rect.position.x -= E.value.rect.size.x;
  2675. } break;
  2676. case INLINE_ALIGNMENT_CENTER_TO: {
  2677. E.value.rect.position.x -= E.value.rect.size.x / 2;
  2678. } break;
  2679. case INLINE_ALIGNMENT_TOP_TO: {
  2680. // NOP
  2681. } break;
  2682. }
  2683. full_ascent = MAX(full_ascent, -E.value.rect.position.x);
  2684. full_descent = MAX(full_descent, E.value.rect.position.x + E.value.rect.size.x);
  2685. }
  2686. }
  2687. sd->ascent = full_ascent;
  2688. sd->descent = full_descent;
  2689. sd->valid = true;
  2690. return sd->valid;
  2691. }
  2692. bool TextServerFallback::shaped_text_is_ready(RID p_shaped) const {
  2693. const ShapedTextData *sd = shaped_owner.get_or_null(p_shaped);
  2694. ERR_FAIL_COND_V(!sd, false);
  2695. MutexLock lock(sd->mutex);
  2696. return sd->valid;
  2697. }
  2698. const Glyph *TextServerFallback::shaped_text_get_glyphs(RID p_shaped) const {
  2699. const ShapedTextData *sd = shaped_owner.get_or_null(p_shaped);
  2700. ERR_FAIL_COND_V(!sd, nullptr);
  2701. MutexLock lock(sd->mutex);
  2702. if (!sd->valid) {
  2703. const_cast<TextServerFallback *>(this)->shaped_text_shape(p_shaped);
  2704. }
  2705. return sd->glyphs.ptr();
  2706. }
  2707. int TextServerFallback::shaped_text_get_glyph_count(RID p_shaped) const {
  2708. const ShapedTextData *sd = shaped_owner.get_or_null(p_shaped);
  2709. ERR_FAIL_COND_V(!sd, 0);
  2710. MutexLock lock(sd->mutex);
  2711. if (!sd->valid) {
  2712. const_cast<TextServerFallback *>(this)->shaped_text_shape(p_shaped);
  2713. }
  2714. return sd->glyphs.size();
  2715. }
  2716. const Glyph *TextServerFallback::shaped_text_sort_logical(RID p_shaped) {
  2717. const ShapedTextData *sd = shaped_owner.get_or_null(p_shaped);
  2718. ERR_FAIL_COND_V(!sd, nullptr);
  2719. MutexLock lock(sd->mutex);
  2720. if (!sd->valid) {
  2721. const_cast<TextServerFallback *>(this)->shaped_text_shape(p_shaped);
  2722. }
  2723. return sd->glyphs.ptr(); // Already in the logical order, return as is.
  2724. }
  2725. Vector2i TextServerFallback::shaped_text_get_range(RID p_shaped) const {
  2726. const ShapedTextData *sd = shaped_owner.get_or_null(p_shaped);
  2727. ERR_FAIL_COND_V(!sd, Vector2i());
  2728. MutexLock lock(sd->mutex);
  2729. return Vector2(sd->start, sd->end);
  2730. }
  2731. Array TextServerFallback::shaped_text_get_objects(RID p_shaped) const {
  2732. Array ret;
  2733. const ShapedTextData *sd = shaped_owner.get_or_null(p_shaped);
  2734. ERR_FAIL_COND_V(!sd, ret);
  2735. MutexLock lock(sd->mutex);
  2736. for (const KeyValue<Variant, ShapedTextData::EmbeddedObject> &E : sd->objects) {
  2737. ret.push_back(E.key);
  2738. }
  2739. return ret;
  2740. }
  2741. Rect2 TextServerFallback::shaped_text_get_object_rect(RID p_shaped, Variant p_key) const {
  2742. const ShapedTextData *sd = shaped_owner.get_or_null(p_shaped);
  2743. ERR_FAIL_COND_V(!sd, Rect2());
  2744. MutexLock lock(sd->mutex);
  2745. ERR_FAIL_COND_V(!sd->objects.has(p_key), Rect2());
  2746. if (!sd->valid) {
  2747. const_cast<TextServerFallback *>(this)->shaped_text_shape(p_shaped);
  2748. }
  2749. return sd->objects[p_key].rect;
  2750. }
  2751. Size2 TextServerFallback::shaped_text_get_size(RID p_shaped) const {
  2752. const ShapedTextData *sd = shaped_owner.get_or_null(p_shaped);
  2753. ERR_FAIL_COND_V(!sd, Size2());
  2754. MutexLock lock(sd->mutex);
  2755. if (!sd->valid) {
  2756. const_cast<TextServerFallback *>(this)->shaped_text_shape(p_shaped);
  2757. }
  2758. if (sd->orientation == TextServer::ORIENTATION_HORIZONTAL) {
  2759. return Size2(sd->width, sd->ascent + sd->descent);
  2760. } else {
  2761. return Size2(sd->ascent + sd->descent, sd->width);
  2762. }
  2763. }
  2764. float TextServerFallback::shaped_text_get_ascent(RID p_shaped) const {
  2765. const ShapedTextData *sd = shaped_owner.get_or_null(p_shaped);
  2766. ERR_FAIL_COND_V(!sd, 0.f);
  2767. MutexLock lock(sd->mutex);
  2768. if (!sd->valid) {
  2769. const_cast<TextServerFallback *>(this)->shaped_text_shape(p_shaped);
  2770. }
  2771. return sd->ascent;
  2772. }
  2773. float TextServerFallback::shaped_text_get_descent(RID p_shaped) const {
  2774. const ShapedTextData *sd = shaped_owner.get_or_null(p_shaped);
  2775. ERR_FAIL_COND_V(!sd, 0.f);
  2776. MutexLock lock(sd->mutex);
  2777. if (!sd->valid) {
  2778. const_cast<TextServerFallback *>(this)->shaped_text_shape(p_shaped);
  2779. }
  2780. return sd->descent;
  2781. }
  2782. float TextServerFallback::shaped_text_get_width(RID p_shaped) const {
  2783. const ShapedTextData *sd = shaped_owner.get_or_null(p_shaped);
  2784. ERR_FAIL_COND_V(!sd, 0.f);
  2785. MutexLock lock(sd->mutex);
  2786. if (!sd->valid) {
  2787. const_cast<TextServerFallback *>(this)->shaped_text_shape(p_shaped);
  2788. }
  2789. return sd->width;
  2790. }
  2791. float TextServerFallback::shaped_text_get_underline_position(RID p_shaped) const {
  2792. const ShapedTextData *sd = shaped_owner.get_or_null(p_shaped);
  2793. ERR_FAIL_COND_V(!sd, 0.f);
  2794. MutexLock lock(sd->mutex);
  2795. if (!sd->valid) {
  2796. const_cast<TextServerFallback *>(this)->shaped_text_shape(p_shaped);
  2797. }
  2798. return sd->upos;
  2799. }
  2800. float TextServerFallback::shaped_text_get_underline_thickness(RID p_shaped) const {
  2801. const ShapedTextData *sd = shaped_owner.get_or_null(p_shaped);
  2802. ERR_FAIL_COND_V(!sd, 0.f);
  2803. MutexLock lock(sd->mutex);
  2804. if (!sd->valid) {
  2805. const_cast<TextServerFallback *>(this)->shaped_text_shape(p_shaped);
  2806. }
  2807. return sd->uthk;
  2808. }
  2809. String TextServerFallback::string_to_upper(const String &p_string, const String &p_language) const {
  2810. String upper = p_string;
  2811. for (int i = 0; i < upper.size(); i++) {
  2812. const char32_t s = upper[i];
  2813. const char32_t t = _find_upper(s);
  2814. if (s != t) { // avoid copy on write
  2815. upper[i] = t;
  2816. }
  2817. }
  2818. return upper;
  2819. }
  2820. String TextServerFallback::string_to_lower(const String &p_string, const String &p_language) const {
  2821. String lower = p_string;
  2822. for (int i = 0; i < lower.size(); i++) {
  2823. const char32_t s = lower[i];
  2824. const char32_t t = _find_lower(s);
  2825. if (s != t) { // avoid copy on write
  2826. lower[i] = t;
  2827. }
  2828. }
  2829. return lower;
  2830. }
  2831. TextServerFallback::TextServerFallback() {
  2832. _insert_feature_sets();
  2833. };
  2834. TextServerFallback::~TextServerFallback() {
  2835. #ifdef MODULE_FREETYPE_ENABLED
  2836. if (library != nullptr) {
  2837. FT_Done_FreeType(library);
  2838. }
  2839. #endif
  2840. };