2
0

text_server_fb.cpp 129 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902
  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. #ifdef GDEXTENSION
  32. // Headers for building as GDExtension plug-in.
  33. #include <godot_cpp/classes/file_access.hpp>
  34. #include <godot_cpp/classes/project_settings.hpp>
  35. #include <godot_cpp/classes/rendering_server.hpp>
  36. #include <godot_cpp/classes/translation_server.hpp>
  37. #include <godot_cpp/core/error_macros.hpp>
  38. using namespace godot;
  39. #define GLOBAL_GET(m_var) ProjectSettings::get_singleton()->get(m_var)
  40. #else
  41. // Headers for building as built-in module.
  42. #include "core/config/project_settings.h"
  43. #include "core/error/error_macros.h"
  44. #include "core/string/print_string.h"
  45. #include "core/string/ucaps.h"
  46. #include "modules/modules_enabled.gen.h" // For freetype, msdfgen, svg.
  47. #endif
  48. // Thirdparty headers.
  49. #ifdef MODULE_MSDFGEN_ENABLED
  50. #include "core/ShapeDistanceFinder.h"
  51. #include "core/contour-combiners.h"
  52. #include "core/edge-selectors.h"
  53. #include "msdfgen.h"
  54. #endif
  55. #ifdef MODULE_SVG_ENABLED
  56. #include "thorvg_svg_in_ot.h"
  57. #endif
  58. /*************************************************************************/
  59. #define OT_TAG(c1, c2, c3, c4) ((int32_t)((((uint32_t)(c1)&0xff) << 24) | (((uint32_t)(c2)&0xff) << 16) | (((uint32_t)(c3)&0xff) << 8) | ((uint32_t)(c4)&0xff)))
  60. bool TextServerFallback::_has_feature(Feature p_feature) const {
  61. switch (p_feature) {
  62. case FEATURE_SIMPLE_LAYOUT:
  63. case FEATURE_FONT_BITMAP:
  64. #ifdef MODULE_FREETYPE_ENABLED
  65. case FEATURE_FONT_DYNAMIC:
  66. #endif
  67. #ifdef MODULE_MSDFGEN_ENABLED
  68. case FEATURE_FONT_MSDF:
  69. #endif
  70. return true;
  71. default: {
  72. }
  73. }
  74. return false;
  75. }
  76. String TextServerFallback::_get_name() const {
  77. #ifdef GDEXTENSION
  78. return "Fallback (GDExtension)";
  79. #else
  80. return "Fallback (Built-in)";
  81. #endif
  82. }
  83. int64_t TextServerFallback::_get_features() const {
  84. int64_t interface_features = FEATURE_SIMPLE_LAYOUT | FEATURE_FONT_BITMAP;
  85. #ifdef MODULE_FREETYPE_ENABLED
  86. interface_features |= FEATURE_FONT_DYNAMIC;
  87. #endif
  88. #ifdef MODULE_MSDFGEN_ENABLED
  89. interface_features |= FEATURE_FONT_MSDF;
  90. #endif
  91. return interface_features;
  92. }
  93. void TextServerFallback::_free_rid(const RID &p_rid) {
  94. _THREAD_SAFE_METHOD_
  95. if (font_owner.owns(p_rid)) {
  96. FontFallback *fd = font_owner.get_or_null(p_rid);
  97. font_owner.free(p_rid);
  98. memdelete(fd);
  99. } else if (shaped_owner.owns(p_rid)) {
  100. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_rid);
  101. shaped_owner.free(p_rid);
  102. memdelete(sd);
  103. }
  104. }
  105. bool TextServerFallback::_has(const RID &p_rid) {
  106. _THREAD_SAFE_METHOD_
  107. return font_owner.owns(p_rid) || shaped_owner.owns(p_rid);
  108. }
  109. String TextServerFallback::_get_support_data_filename() const {
  110. return "";
  111. };
  112. String TextServerFallback::_get_support_data_info() const {
  113. return "Not supported";
  114. };
  115. bool TextServerFallback::_load_support_data(const String &p_filename) {
  116. return false; // No extra data used.
  117. }
  118. bool TextServerFallback::_save_support_data(const String &p_filename) const {
  119. return false; // No extra data used.
  120. }
  121. bool TextServerFallback::_is_locale_right_to_left(const String &p_locale) const {
  122. return false; // No RTL support.
  123. }
  124. _FORCE_INLINE_ void TextServerFallback::_insert_feature(const StringName &p_name, int32_t p_tag) {
  125. feature_sets.insert(p_name, p_tag);
  126. feature_sets_inv.insert(p_tag, p_name);
  127. }
  128. void TextServerFallback::_insert_feature_sets() {
  129. // Registered OpenType variation tag.
  130. _insert_feature("italic", OT_TAG('i', 't', 'a', 'l'));
  131. _insert_feature("optical_size", OT_TAG('o', 'p', 's', 'z'));
  132. _insert_feature("slant", OT_TAG('s', 'l', 'n', 't'));
  133. _insert_feature("width", OT_TAG('w', 'd', 't', 'h'));
  134. _insert_feature("weight", OT_TAG('w', 'g', 'h', 't'));
  135. }
  136. _FORCE_INLINE_ int32_t ot_tag_from_string(const char *p_str, int p_len) {
  137. char tag[4];
  138. uint32_t i;
  139. if (!p_str || !p_len || !*p_str) {
  140. return OT_TAG(0, 0, 0, 0);
  141. }
  142. if (p_len < 0 || p_len > 4) {
  143. p_len = 4;
  144. }
  145. for (i = 0; i < (uint32_t)p_len && p_str[i]; i++) {
  146. tag[i] = p_str[i];
  147. }
  148. for (; i < 4; i++) {
  149. tag[i] = ' ';
  150. }
  151. return OT_TAG(tag[0], tag[1], tag[2], tag[3]);
  152. }
  153. int64_t TextServerFallback::_name_to_tag(const String &p_name) const {
  154. if (feature_sets.has(p_name)) {
  155. return feature_sets[p_name];
  156. }
  157. // No readable name, use tag string.
  158. return ot_tag_from_string(p_name.replace("custom_", "").ascii().get_data(), -1);
  159. }
  160. _FORCE_INLINE_ void ot_tag_to_string(int32_t p_tag, char *p_buf) {
  161. p_buf[0] = (char)(uint8_t)(p_tag >> 24);
  162. p_buf[1] = (char)(uint8_t)(p_tag >> 16);
  163. p_buf[2] = (char)(uint8_t)(p_tag >> 8);
  164. p_buf[3] = (char)(uint8_t)(p_tag >> 0);
  165. }
  166. String TextServerFallback::_tag_to_name(int64_t p_tag) const {
  167. if (feature_sets_inv.has(p_tag)) {
  168. return feature_sets_inv[p_tag];
  169. }
  170. // No readable name, use tag string.
  171. char name[5];
  172. memset(name, 0, 5);
  173. ot_tag_to_string(p_tag, name);
  174. return String("custom_") + String(name);
  175. }
  176. /*************************************************************************/
  177. /* Font Glyph Rendering */
  178. /*************************************************************************/
  179. _FORCE_INLINE_ TextServerFallback::FontTexturePosition TextServerFallback::find_texture_pos_for_glyph(FontForSizeFallback *p_data, int p_color_size, Image::Format p_image_format, int p_width, int p_height, bool p_msdf) const {
  180. FontTexturePosition ret;
  181. int mw = p_width;
  182. int mh = p_height;
  183. ShelfPackTexture *ct = p_data->textures.ptrw();
  184. for (int32_t i = 0; i < p_data->textures.size(); i++) {
  185. if (p_image_format != ct[i].format) {
  186. continue;
  187. }
  188. if (mw > ct[i].texture_w || mh > ct[i].texture_h) { // Too big for this texture.
  189. continue;
  190. }
  191. ret = ct[i].pack_rect(i, mh, mw);
  192. if (ret.index != -1) {
  193. break;
  194. }
  195. }
  196. if (ret.index == -1) {
  197. // Could not find texture to fit, create one.
  198. int texsize = MAX(p_data->size.x * p_data->oversampling * 8, 256);
  199. #ifdef GDEXTENSION
  200. texsize = Math::next_power_of_2(texsize);
  201. #else
  202. texsize = next_power_of_2(texsize);
  203. #endif
  204. if (p_msdf) {
  205. texsize = MIN(texsize, 2048);
  206. } else {
  207. texsize = MIN(texsize, 1024);
  208. }
  209. if (mw > texsize) { // Special case, adapt to it?
  210. #ifdef GDEXTENSION
  211. texsize = Math::next_power_of_2(mw);
  212. #else
  213. texsize = next_power_of_2(mw);
  214. #endif
  215. }
  216. if (mh > texsize) { // Special case, adapt to it?
  217. #ifdef GDEXTENSION
  218. texsize = Math::next_power_of_2(mh);
  219. #else
  220. texsize = next_power_of_2(mh);
  221. #endif
  222. }
  223. ShelfPackTexture tex = ShelfPackTexture(texsize, texsize);
  224. tex.format = p_image_format;
  225. tex.imgdata.resize(texsize * texsize * p_color_size);
  226. {
  227. // Zero texture.
  228. uint8_t *w = tex.imgdata.ptrw();
  229. ERR_FAIL_COND_V(texsize * texsize * p_color_size > tex.imgdata.size(), ret);
  230. // Initialize the texture to all-white pixels to prevent artifacts when the
  231. // font is displayed at a non-default scale with filtering enabled.
  232. if (p_color_size == 2) {
  233. for (int i = 0; i < texsize * texsize * p_color_size; i += 2) { // FORMAT_LA8, BW font.
  234. w[i + 0] = 255;
  235. w[i + 1] = 0;
  236. }
  237. } else if (p_color_size == 4) {
  238. for (int i = 0; i < texsize * texsize * p_color_size; i += 4) { // FORMAT_RGBA8, Color font, Multichannel(+True) SDF.
  239. w[i + 0] = 255;
  240. w[i + 1] = 255;
  241. w[i + 2] = 255;
  242. w[i + 3] = 0;
  243. }
  244. } else {
  245. ERR_FAIL_V(ret);
  246. }
  247. }
  248. p_data->textures.push_back(tex);
  249. int32_t idx = p_data->textures.size() - 1;
  250. ret = p_data->textures.write[idx].pack_rect(idx, mh, mw);
  251. }
  252. return ret;
  253. }
  254. #ifdef MODULE_MSDFGEN_ENABLED
  255. struct MSContext {
  256. msdfgen::Point2 position;
  257. msdfgen::Shape *shape = nullptr;
  258. msdfgen::Contour *contour = nullptr;
  259. };
  260. class DistancePixelConversion {
  261. double invRange;
  262. public:
  263. _FORCE_INLINE_ explicit DistancePixelConversion(double range) :
  264. invRange(1 / range) {}
  265. _FORCE_INLINE_ void operator()(float *pixels, const msdfgen::MultiAndTrueDistance &distance) const {
  266. pixels[0] = float(invRange * distance.r + .5);
  267. pixels[1] = float(invRange * distance.g + .5);
  268. pixels[2] = float(invRange * distance.b + .5);
  269. pixels[3] = float(invRange * distance.a + .5);
  270. }
  271. };
  272. struct MSDFThreadData {
  273. msdfgen::Bitmap<float, 4> *output;
  274. msdfgen::Shape *shape;
  275. msdfgen::Projection *projection;
  276. DistancePixelConversion *distancePixelConversion;
  277. };
  278. static msdfgen::Point2 ft_point2(const FT_Vector &vector) {
  279. return msdfgen::Point2(vector.x / 60.0f, vector.y / 60.0f);
  280. }
  281. static int ft_move_to(const FT_Vector *to, void *user) {
  282. MSContext *context = static_cast<MSContext *>(user);
  283. if (!(context->contour && context->contour->edges.empty())) {
  284. context->contour = &context->shape->addContour();
  285. }
  286. context->position = ft_point2(*to);
  287. return 0;
  288. }
  289. static int ft_line_to(const FT_Vector *to, void *user) {
  290. MSContext *context = static_cast<MSContext *>(user);
  291. msdfgen::Point2 endpoint = ft_point2(*to);
  292. if (endpoint != context->position) {
  293. context->contour->addEdge(new msdfgen::LinearSegment(context->position, endpoint));
  294. context->position = endpoint;
  295. }
  296. return 0;
  297. }
  298. static int ft_conic_to(const FT_Vector *control, const FT_Vector *to, void *user) {
  299. MSContext *context = static_cast<MSContext *>(user);
  300. context->contour->addEdge(new msdfgen::QuadraticSegment(context->position, ft_point2(*control), ft_point2(*to)));
  301. context->position = ft_point2(*to);
  302. return 0;
  303. }
  304. static int ft_cubic_to(const FT_Vector *control1, const FT_Vector *control2, const FT_Vector *to, void *user) {
  305. MSContext *context = static_cast<MSContext *>(user);
  306. context->contour->addEdge(new msdfgen::CubicSegment(context->position, ft_point2(*control1), ft_point2(*control2), ft_point2(*to)));
  307. context->position = ft_point2(*to);
  308. return 0;
  309. }
  310. void TextServerFallback::_generateMTSDF_threaded(uint32_t y, void *p_td) const {
  311. MSDFThreadData *td = static_cast<MSDFThreadData *>(p_td);
  312. msdfgen::ShapeDistanceFinder<msdfgen::OverlappingContourCombiner<msdfgen::MultiAndTrueDistanceSelector>> distanceFinder(*td->shape);
  313. int row = td->shape->inverseYAxis ? td->output->height() - y - 1 : y;
  314. for (int col = 0; col < td->output->width(); ++col) {
  315. int x = (y % 2) ? td->output->width() - col - 1 : col;
  316. msdfgen::Point2 p = td->projection->unproject(msdfgen::Point2(x + .5, y + .5));
  317. msdfgen::MultiAndTrueDistance distance = distanceFinder.distance(p);
  318. td->distancePixelConversion->operator()(td->output->operator()(x, row), distance);
  319. }
  320. }
  321. _FORCE_INLINE_ TextServerFallback::FontGlyph TextServerFallback::rasterize_msdf(FontFallback *p_font_data, FontForSizeFallback *p_data, int p_pixel_range, int p_rect_margin, FT_Outline *outline, const Vector2 &advance) const {
  322. msdfgen::Shape shape;
  323. shape.contours.clear();
  324. shape.inverseYAxis = false;
  325. MSContext context = {};
  326. context.shape = &shape;
  327. FT_Outline_Funcs ft_functions;
  328. ft_functions.move_to = &ft_move_to;
  329. ft_functions.line_to = &ft_line_to;
  330. ft_functions.conic_to = &ft_conic_to;
  331. ft_functions.cubic_to = &ft_cubic_to;
  332. ft_functions.shift = 0;
  333. ft_functions.delta = 0;
  334. int error = FT_Outline_Decompose(outline, &ft_functions, &context);
  335. ERR_FAIL_COND_V_MSG(error, FontGlyph(), "FreeType: Outline decomposition error: '" + String(FT_Error_String(error)) + "'.");
  336. if (!shape.contours.empty() && shape.contours.back().edges.empty()) {
  337. shape.contours.pop_back();
  338. }
  339. if (FT_Outline_Get_Orientation(outline) == 1) {
  340. for (int i = 0; i < (int)shape.contours.size(); ++i) {
  341. shape.contours[i].reverse();
  342. }
  343. }
  344. shape.inverseYAxis = true;
  345. shape.normalize();
  346. msdfgen::Shape::Bounds bounds = shape.getBounds(p_pixel_range);
  347. FontGlyph chr;
  348. chr.found = true;
  349. chr.advance = advance;
  350. if (shape.validate() && shape.contours.size() > 0) {
  351. int w = (bounds.r - bounds.l);
  352. int h = (bounds.t - bounds.b);
  353. int mw = w + p_rect_margin * 4;
  354. int mh = h + p_rect_margin * 4;
  355. ERR_FAIL_COND_V(mw > 4096, FontGlyph());
  356. ERR_FAIL_COND_V(mh > 4096, FontGlyph());
  357. FontTexturePosition tex_pos = find_texture_pos_for_glyph(p_data, 4, Image::FORMAT_RGBA8, mw, mh, true);
  358. ERR_FAIL_COND_V(tex_pos.index < 0, FontGlyph());
  359. ShelfPackTexture &tex = p_data->textures.write[tex_pos.index];
  360. edgeColoringSimple(shape, 3.0); // Max. angle.
  361. msdfgen::Bitmap<float, 4> image(w, h); // Texture size.
  362. DistancePixelConversion distancePixelConversion(p_pixel_range);
  363. msdfgen::Projection projection(msdfgen::Vector2(1.0, 1.0), msdfgen::Vector2(-bounds.l, -bounds.b));
  364. msdfgen::MSDFGeneratorConfig config(true, msdfgen::ErrorCorrectionConfig());
  365. MSDFThreadData td;
  366. td.output = &image;
  367. td.shape = &shape;
  368. td.projection = &projection;
  369. td.distancePixelConversion = &distancePixelConversion;
  370. #ifdef GDEXTENSION
  371. for (int i = 0; i < h; i++) {
  372. _generateMTSDF_threaded(i, &td);
  373. }
  374. #else
  375. WorkerThreadPool::GroupID group_id = WorkerThreadPool::get_singleton()->add_template_group_task(this, &TextServerFallback::_generateMTSDF_threaded, &td, h, -1, true, SNAME("TextServerFBRenderMSDF"));
  376. WorkerThreadPool::get_singleton()->wait_for_group_task_completion(group_id);
  377. #endif
  378. msdfgen::msdfErrorCorrection(image, shape, projection, p_pixel_range, config);
  379. {
  380. uint8_t *wr = tex.imgdata.ptrw();
  381. for (int i = 0; i < h; i++) {
  382. for (int j = 0; j < w; j++) {
  383. int ofs = ((i + tex_pos.y + p_rect_margin * 2) * tex.texture_w + j + tex_pos.x + p_rect_margin * 2) * 4;
  384. ERR_FAIL_COND_V(ofs >= tex.imgdata.size(), FontGlyph());
  385. wr[ofs + 0] = (uint8_t)(CLAMP(image(j, i)[0] * 256.f, 0.f, 255.f));
  386. wr[ofs + 1] = (uint8_t)(CLAMP(image(j, i)[1] * 256.f, 0.f, 255.f));
  387. wr[ofs + 2] = (uint8_t)(CLAMP(image(j, i)[2] * 256.f, 0.f, 255.f));
  388. wr[ofs + 3] = (uint8_t)(CLAMP(image(j, i)[3] * 256.f, 0.f, 255.f));
  389. }
  390. }
  391. }
  392. tex.dirty = true;
  393. chr.texture_idx = tex_pos.index;
  394. chr.uv_rect = Rect2(tex_pos.x + p_rect_margin, tex_pos.y + p_rect_margin, w + p_rect_margin * 2, h + p_rect_margin * 2);
  395. chr.rect.position = Vector2(bounds.l - p_rect_margin, -bounds.t - p_rect_margin);
  396. chr.rect.size = chr.uv_rect.size;
  397. }
  398. return chr;
  399. }
  400. #endif
  401. #ifdef MODULE_FREETYPE_ENABLED
  402. _FORCE_INLINE_ TextServerFallback::FontGlyph TextServerFallback::rasterize_bitmap(FontForSizeFallback *p_data, int p_rect_margin, FT_Bitmap bitmap, int yofs, int xofs, const Vector2 &advance, bool p_bgra) const {
  403. int w = bitmap.width;
  404. int h = bitmap.rows;
  405. int color_size = 2;
  406. switch (bitmap.pixel_mode) {
  407. case FT_PIXEL_MODE_MONO:
  408. case FT_PIXEL_MODE_GRAY: {
  409. color_size = 2;
  410. } break;
  411. case FT_PIXEL_MODE_BGRA: {
  412. color_size = 4;
  413. } break;
  414. case FT_PIXEL_MODE_LCD: {
  415. color_size = 4;
  416. w /= 3;
  417. } break;
  418. case FT_PIXEL_MODE_LCD_V: {
  419. color_size = 4;
  420. h /= 3;
  421. } break;
  422. }
  423. int mw = w + p_rect_margin * 4;
  424. int mh = h + p_rect_margin * 4;
  425. ERR_FAIL_COND_V(mw > 4096, FontGlyph());
  426. ERR_FAIL_COND_V(mh > 4096, FontGlyph());
  427. Image::Format require_format = color_size == 4 ? Image::FORMAT_RGBA8 : Image::FORMAT_LA8;
  428. FontTexturePosition tex_pos = find_texture_pos_for_glyph(p_data, color_size, require_format, mw, mh, false);
  429. ERR_FAIL_COND_V(tex_pos.index < 0, FontGlyph());
  430. // Fit character in char texture.
  431. ShelfPackTexture &tex = p_data->textures.write[tex_pos.index];
  432. {
  433. uint8_t *wr = tex.imgdata.ptrw();
  434. for (int i = 0; i < h; i++) {
  435. for (int j = 0; j < w; j++) {
  436. int ofs = ((i + tex_pos.y + p_rect_margin * 2) * tex.texture_w + j + tex_pos.x + p_rect_margin * 2) * color_size;
  437. ERR_FAIL_COND_V(ofs >= tex.imgdata.size(), FontGlyph());
  438. switch (bitmap.pixel_mode) {
  439. case FT_PIXEL_MODE_MONO: {
  440. int byte = i * bitmap.pitch + (j >> 3);
  441. int bit = 1 << (7 - (j % 8));
  442. wr[ofs + 0] = 255; // grayscale as 1
  443. wr[ofs + 1] = (bitmap.buffer[byte] & bit) ? 255 : 0;
  444. } break;
  445. case FT_PIXEL_MODE_GRAY:
  446. wr[ofs + 0] = 255; // grayscale as 1
  447. wr[ofs + 1] = bitmap.buffer[i * bitmap.pitch + j];
  448. break;
  449. case FT_PIXEL_MODE_BGRA: {
  450. int ofs_color = i * bitmap.pitch + (j << 2);
  451. wr[ofs + 2] = bitmap.buffer[ofs_color + 0];
  452. wr[ofs + 1] = bitmap.buffer[ofs_color + 1];
  453. wr[ofs + 0] = bitmap.buffer[ofs_color + 2];
  454. wr[ofs + 3] = bitmap.buffer[ofs_color + 3];
  455. } break;
  456. case FT_PIXEL_MODE_LCD: {
  457. int ofs_color = i * bitmap.pitch + (j * 3);
  458. if (p_bgra) {
  459. wr[ofs + 0] = bitmap.buffer[ofs_color + 0];
  460. wr[ofs + 1] = bitmap.buffer[ofs_color + 1];
  461. wr[ofs + 2] = bitmap.buffer[ofs_color + 2];
  462. wr[ofs + 3] = 255;
  463. } else {
  464. wr[ofs + 0] = bitmap.buffer[ofs_color + 2];
  465. wr[ofs + 1] = bitmap.buffer[ofs_color + 1];
  466. wr[ofs + 2] = bitmap.buffer[ofs_color + 0];
  467. wr[ofs + 3] = 255;
  468. }
  469. } break;
  470. case FT_PIXEL_MODE_LCD_V: {
  471. int ofs_color = i * bitmap.pitch * 3 + j;
  472. if (p_bgra) {
  473. wr[ofs + 0] = bitmap.buffer[ofs_color + bitmap.pitch * 2];
  474. wr[ofs + 1] = bitmap.buffer[ofs_color + bitmap.pitch];
  475. wr[ofs + 2] = bitmap.buffer[ofs_color + 0];
  476. wr[ofs + 3] = 255;
  477. } else {
  478. wr[ofs + 0] = bitmap.buffer[ofs_color + 0];
  479. wr[ofs + 1] = bitmap.buffer[ofs_color + bitmap.pitch];
  480. wr[ofs + 2] = bitmap.buffer[ofs_color + bitmap.pitch * 2];
  481. wr[ofs + 3] = 255;
  482. }
  483. } break;
  484. default:
  485. ERR_FAIL_V_MSG(FontGlyph(), "Font uses unsupported pixel format: " + String::num_int64(bitmap.pixel_mode) + ".");
  486. break;
  487. }
  488. }
  489. }
  490. }
  491. tex.dirty = true;
  492. FontGlyph chr;
  493. chr.advance = advance * p_data->scale / p_data->oversampling;
  494. chr.texture_idx = tex_pos.index;
  495. chr.found = true;
  496. chr.uv_rect = Rect2(tex_pos.x + p_rect_margin, tex_pos.y + p_rect_margin, w + p_rect_margin * 2, h + p_rect_margin * 2);
  497. chr.rect.position = Vector2(xofs - p_rect_margin, -yofs - p_rect_margin) * p_data->scale / p_data->oversampling;
  498. chr.rect.size = chr.uv_rect.size * p_data->scale / p_data->oversampling;
  499. return chr;
  500. }
  501. #endif
  502. /*************************************************************************/
  503. /* Font Cache */
  504. /*************************************************************************/
  505. _FORCE_INLINE_ bool TextServerFallback::_ensure_glyph(FontFallback *p_font_data, const Vector2i &p_size, int32_t p_glyph) const {
  506. ERR_FAIL_COND_V(!_ensure_cache_for_size(p_font_data, p_size), false);
  507. int32_t glyph_index = p_glyph & 0xffffff; // Remove subpixel shifts.
  508. FontForSizeFallback *fd = p_font_data->cache[p_size];
  509. if (fd->glyph_map.has(p_glyph)) {
  510. return fd->glyph_map[p_glyph].found;
  511. }
  512. if (glyph_index == 0) { // Non graphical or invalid glyph, do not render.
  513. fd->glyph_map[p_glyph] = FontGlyph();
  514. return true;
  515. }
  516. #ifdef MODULE_FREETYPE_ENABLED
  517. FontGlyph gl;
  518. if (fd->face) {
  519. FT_Int32 flags = FT_LOAD_DEFAULT;
  520. bool outline = p_size.y > 0;
  521. switch (p_font_data->hinting) {
  522. case TextServer::HINTING_NONE:
  523. flags |= FT_LOAD_NO_HINTING;
  524. break;
  525. case TextServer::HINTING_LIGHT:
  526. flags |= FT_LOAD_TARGET_LIGHT;
  527. break;
  528. default:
  529. flags |= FT_LOAD_TARGET_NORMAL;
  530. break;
  531. }
  532. if (p_font_data->force_autohinter) {
  533. flags |= FT_LOAD_FORCE_AUTOHINT;
  534. }
  535. if (outline) {
  536. flags |= FT_LOAD_NO_BITMAP;
  537. } else if (FT_HAS_COLOR(fd->face)) {
  538. flags |= FT_LOAD_COLOR;
  539. }
  540. glyph_index = FT_Get_Char_Index(fd->face, glyph_index);
  541. FT_Fixed v, h;
  542. FT_Get_Advance(fd->face, glyph_index, flags, &h);
  543. FT_Get_Advance(fd->face, glyph_index, flags | FT_LOAD_VERTICAL_LAYOUT, &v);
  544. int error = FT_Load_Glyph(fd->face, glyph_index, flags);
  545. if (error) {
  546. fd->glyph_map[p_glyph] = FontGlyph();
  547. return false;
  548. }
  549. if (!p_font_data->msdf) {
  550. if ((p_font_data->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_QUARTER) || (p_font_data->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && p_size.x <= SUBPIXEL_POSITIONING_ONE_QUARTER_MAX_SIZE)) {
  551. FT_Pos xshift = (int)((p_glyph >> 27) & 3) << 4;
  552. FT_Outline_Translate(&fd->face->glyph->outline, xshift, 0);
  553. } else if ((p_font_data->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_HALF) || (p_font_data->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && p_size.x <= SUBPIXEL_POSITIONING_ONE_HALF_MAX_SIZE)) {
  554. FT_Pos xshift = (int)((p_glyph >> 27) & 3) << 5;
  555. FT_Outline_Translate(&fd->face->glyph->outline, xshift, 0);
  556. }
  557. }
  558. if (p_font_data->embolden != 0.f) {
  559. FT_Pos strength = p_font_data->embolden * p_size.x * 4; // 26.6 fractional units (1 / 64).
  560. FT_Outline_Embolden(&fd->face->glyph->outline, strength);
  561. }
  562. if (p_font_data->transform != Transform2D()) {
  563. FT_Matrix mat = { FT_Fixed(p_font_data->transform[0][0] * 65536), FT_Fixed(p_font_data->transform[0][1] * 65536), FT_Fixed(p_font_data->transform[1][0] * 65536), FT_Fixed(p_font_data->transform[1][1] * 65536) }; // 16.16 fractional units (1 / 65536).
  564. FT_Outline_Transform(&fd->face->glyph->outline, &mat);
  565. }
  566. FT_Render_Mode aa_mode = FT_RENDER_MODE_NORMAL;
  567. bool bgra = false;
  568. switch (p_font_data->antialiasing) {
  569. case FONT_ANTIALIASING_NONE: {
  570. aa_mode = FT_RENDER_MODE_MONO;
  571. } break;
  572. case FONT_ANTIALIASING_GRAY: {
  573. aa_mode = FT_RENDER_MODE_NORMAL;
  574. } break;
  575. case FONT_ANTIALIASING_LCD: {
  576. int aa_layout = (int)((p_glyph >> 24) & 7);
  577. switch (aa_layout) {
  578. case FONT_LCD_SUBPIXEL_LAYOUT_HRGB: {
  579. aa_mode = FT_RENDER_MODE_LCD;
  580. bgra = false;
  581. } break;
  582. case FONT_LCD_SUBPIXEL_LAYOUT_HBGR: {
  583. aa_mode = FT_RENDER_MODE_LCD;
  584. bgra = true;
  585. } break;
  586. case FONT_LCD_SUBPIXEL_LAYOUT_VRGB: {
  587. aa_mode = FT_RENDER_MODE_LCD_V;
  588. bgra = false;
  589. } break;
  590. case FONT_LCD_SUBPIXEL_LAYOUT_VBGR: {
  591. aa_mode = FT_RENDER_MODE_LCD_V;
  592. bgra = true;
  593. } break;
  594. default: {
  595. aa_mode = FT_RENDER_MODE_NORMAL;
  596. } break;
  597. }
  598. } break;
  599. }
  600. if (!outline) {
  601. if (!p_font_data->msdf) {
  602. error = FT_Render_Glyph(fd->face->glyph, aa_mode);
  603. }
  604. FT_GlyphSlot slot = fd->face->glyph;
  605. if (!error) {
  606. if (p_font_data->msdf) {
  607. #ifdef MODULE_MSDFGEN_ENABLED
  608. 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);
  609. #else
  610. fd->glyph_map[p_glyph] = FontGlyph();
  611. ERR_FAIL_V_MSG(false, "Compiled without MSDFGEN support!");
  612. #endif
  613. } else {
  614. 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, bgra);
  615. }
  616. }
  617. } else {
  618. FT_Stroker stroker;
  619. if (FT_Stroker_New(ft_library, &stroker) != 0) {
  620. fd->glyph_map[p_glyph] = FontGlyph();
  621. ERR_FAIL_V_MSG(false, "FreeType: Failed to load glyph stroker.");
  622. }
  623. FT_Stroker_Set(stroker, (int)(fd->size.y * fd->oversampling * 16.0), FT_STROKER_LINECAP_BUTT, FT_STROKER_LINEJOIN_ROUND, 0);
  624. FT_Glyph glyph;
  625. FT_BitmapGlyph glyph_bitmap;
  626. if (FT_Get_Glyph(fd->face->glyph, &glyph) != 0) {
  627. goto cleanup_stroker;
  628. }
  629. if (FT_Glyph_Stroke(&glyph, stroker, 1) != 0) {
  630. goto cleanup_glyph;
  631. }
  632. if (FT_Glyph_To_Bitmap(&glyph, aa_mode, nullptr, 1) != 0) {
  633. goto cleanup_glyph;
  634. }
  635. glyph_bitmap = (FT_BitmapGlyph)glyph;
  636. gl = rasterize_bitmap(fd, rect_range, glyph_bitmap->bitmap, glyph_bitmap->top, glyph_bitmap->left, Vector2(), bgra);
  637. cleanup_glyph:
  638. FT_Done_Glyph(glyph);
  639. cleanup_stroker:
  640. FT_Stroker_Done(stroker);
  641. }
  642. fd->glyph_map[p_glyph] = gl;
  643. return gl.found;
  644. }
  645. #endif
  646. fd->glyph_map[p_glyph] = FontGlyph();
  647. return false;
  648. }
  649. _FORCE_INLINE_ bool TextServerFallback::_ensure_cache_for_size(FontFallback *p_font_data, const Vector2i &p_size) const {
  650. ERR_FAIL_COND_V(p_size.x <= 0, false);
  651. if (p_font_data->cache.has(p_size)) {
  652. return true;
  653. }
  654. FontForSizeFallback *fd = memnew(FontForSizeFallback);
  655. fd->size = p_size;
  656. if (p_font_data->data_ptr && (p_font_data->data_size > 0)) {
  657. // Init dynamic font.
  658. #ifdef MODULE_FREETYPE_ENABLED
  659. int error = 0;
  660. if (!ft_library) {
  661. error = FT_Init_FreeType(&ft_library);
  662. if (error != 0) {
  663. memdelete(fd);
  664. ERR_FAIL_V_MSG(false, "FreeType: Error initializing library: '" + String(FT_Error_String(error)) + "'.");
  665. }
  666. #ifdef MODULE_SVG_ENABLED
  667. FT_Property_Set(ft_library, "ot-svg", "svg-hooks", get_tvg_svg_in_ot_hooks());
  668. #endif
  669. }
  670. memset(&fd->stream, 0, sizeof(FT_StreamRec));
  671. fd->stream.base = (unsigned char *)p_font_data->data_ptr;
  672. fd->stream.size = p_font_data->data_size;
  673. fd->stream.pos = 0;
  674. FT_Open_Args fargs;
  675. memset(&fargs, 0, sizeof(FT_Open_Args));
  676. fargs.memory_base = (unsigned char *)p_font_data->data_ptr;
  677. fargs.memory_size = p_font_data->data_size;
  678. fargs.flags = FT_OPEN_MEMORY;
  679. fargs.stream = &fd->stream;
  680. int max_index = 0;
  681. FT_Face tmp_face = nullptr;
  682. error = FT_Open_Face(ft_library, &fargs, -1, &tmp_face);
  683. if (tmp_face && error == 0) {
  684. max_index = tmp_face->num_faces - 1;
  685. }
  686. if (tmp_face) {
  687. FT_Done_Face(tmp_face);
  688. }
  689. error = FT_Open_Face(ft_library, &fargs, CLAMP(p_font_data->face_index, 0, max_index), &fd->face);
  690. if (error) {
  691. FT_Done_Face(fd->face);
  692. fd->face = nullptr;
  693. memdelete(fd);
  694. ERR_FAIL_V_MSG(false, "FreeType: Error loading font: '" + String(FT_Error_String(error)) + "'.");
  695. }
  696. if (p_font_data->msdf) {
  697. fd->oversampling = 1.0;
  698. fd->size.x = p_font_data->msdf_source_size;
  699. } else if (p_font_data->oversampling <= 0.0) {
  700. fd->oversampling = _font_get_global_oversampling();
  701. } else {
  702. fd->oversampling = p_font_data->oversampling;
  703. }
  704. if (FT_HAS_COLOR(fd->face) && fd->face->num_fixed_sizes > 0) {
  705. int best_match = 0;
  706. int diff = ABS(fd->size.x - ((int64_t)fd->face->available_sizes[0].width));
  707. fd->scale = double(fd->size.x * fd->oversampling) / fd->face->available_sizes[0].width;
  708. for (int i = 1; i < fd->face->num_fixed_sizes; i++) {
  709. int ndiff = ABS(fd->size.x - ((int64_t)fd->face->available_sizes[i].width));
  710. if (ndiff < diff) {
  711. best_match = i;
  712. diff = ndiff;
  713. fd->scale = double(fd->size.x * fd->oversampling) / fd->face->available_sizes[i].width;
  714. }
  715. }
  716. FT_Select_Size(fd->face, best_match);
  717. } else {
  718. FT_Set_Pixel_Sizes(fd->face, 0, Math::round(fd->size.x * fd->oversampling));
  719. fd->scale = ((double)fd->size.x * fd->oversampling) / (double)fd->face->size->metrics.y_ppem;
  720. }
  721. fd->ascent = (fd->face->size->metrics.ascender / 64.0) / fd->oversampling * fd->scale;
  722. fd->descent = (-fd->face->size->metrics.descender / 64.0) / fd->oversampling * fd->scale;
  723. fd->underline_position = (-FT_MulFix(fd->face->underline_position, fd->face->size->metrics.y_scale) / 64.0) / fd->oversampling * fd->scale;
  724. fd->underline_thickness = (FT_MulFix(fd->face->underline_thickness, fd->face->size->metrics.y_scale) / 64.0) / fd->oversampling * fd->scale;
  725. if (!p_font_data->face_init) {
  726. // Get style flags and name.
  727. if (fd->face->family_name != nullptr) {
  728. p_font_data->font_name = String::utf8((const char *)fd->face->family_name);
  729. }
  730. if (fd->face->style_name != nullptr) {
  731. p_font_data->style_name = String::utf8((const char *)fd->face->style_name);
  732. }
  733. p_font_data->style_flags = 0;
  734. if (fd->face->style_flags & FT_STYLE_FLAG_BOLD) {
  735. p_font_data->style_flags.set_flag(FONT_BOLD);
  736. }
  737. if (fd->face->style_flags & FT_STYLE_FLAG_ITALIC) {
  738. p_font_data->style_flags.set_flag(FONT_ITALIC);
  739. }
  740. if (fd->face->face_flags & FT_FACE_FLAG_FIXED_WIDTH) {
  741. p_font_data->style_flags.set_flag(FONT_FIXED_WIDTH);
  742. }
  743. // Read OpenType variations.
  744. p_font_data->supported_varaitions.clear();
  745. if (fd->face->face_flags & FT_FACE_FLAG_MULTIPLE_MASTERS) {
  746. FT_MM_Var *amaster;
  747. FT_Get_MM_Var(fd->face, &amaster);
  748. for (FT_UInt i = 0; i < amaster->num_axis; i++) {
  749. 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);
  750. }
  751. FT_Done_MM_Var(ft_library, amaster);
  752. }
  753. p_font_data->face_init = true;
  754. }
  755. // Write variations.
  756. if (fd->face->face_flags & FT_FACE_FLAG_MULTIPLE_MASTERS) {
  757. FT_MM_Var *amaster;
  758. FT_Get_MM_Var(fd->face, &amaster);
  759. Vector<FT_Fixed> coords;
  760. coords.resize(amaster->num_axis);
  761. FT_Get_Var_Design_Coordinates(fd->face, coords.size(), coords.ptrw());
  762. for (FT_UInt i = 0; i < amaster->num_axis; i++) {
  763. // Reset to default.
  764. int32_t var_tag = amaster->axis[i].tag;
  765. double var_value = (double)amaster->axis[i].def / 65536.0;
  766. coords.write[i] = amaster->axis[i].def;
  767. if (p_font_data->variation_coordinates.has(var_tag)) {
  768. var_value = p_font_data->variation_coordinates[var_tag];
  769. coords.write[i] = CLAMP(var_value * 65536.0, amaster->axis[i].minimum, amaster->axis[i].maximum);
  770. }
  771. if (p_font_data->variation_coordinates.has(_tag_to_name(var_tag))) {
  772. var_value = p_font_data->variation_coordinates[_tag_to_name(var_tag)];
  773. coords.write[i] = CLAMP(var_value * 65536.0, amaster->axis[i].minimum, amaster->axis[i].maximum);
  774. }
  775. }
  776. FT_Set_Var_Design_Coordinates(fd->face, coords.size(), coords.ptrw());
  777. FT_Done_MM_Var(ft_library, amaster);
  778. }
  779. #else
  780. memdelete(fd);
  781. ERR_FAIL_V_MSG(false, "FreeType: Can't load dynamic font, engine is compiled without FreeType support!");
  782. #endif
  783. }
  784. p_font_data->cache[p_size] = fd;
  785. return true;
  786. }
  787. _FORCE_INLINE_ void TextServerFallback::_font_clear_cache(FontFallback *p_font_data) {
  788. for (const KeyValue<Vector2i, FontForSizeFallback *> &E : p_font_data->cache) {
  789. memdelete(E.value);
  790. }
  791. p_font_data->cache.clear();
  792. p_font_data->face_init = false;
  793. p_font_data->supported_varaitions.clear();
  794. }
  795. RID TextServerFallback::_create_font() {
  796. _THREAD_SAFE_METHOD_
  797. FontFallback *fd = memnew(FontFallback);
  798. return font_owner.make_rid(fd);
  799. }
  800. void TextServerFallback::_font_set_data(const RID &p_font_rid, const PackedByteArray &p_data) {
  801. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  802. ERR_FAIL_COND(!fd);
  803. MutexLock lock(fd->mutex);
  804. _font_clear_cache(fd);
  805. fd->data = p_data;
  806. fd->data_ptr = fd->data.ptr();
  807. fd->data_size = fd->data.size();
  808. }
  809. void TextServerFallback::_font_set_data_ptr(const RID &p_font_rid, const uint8_t *p_data_ptr, int64_t p_data_size) {
  810. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  811. ERR_FAIL_COND(!fd);
  812. MutexLock lock(fd->mutex);
  813. _font_clear_cache(fd);
  814. fd->data.resize(0);
  815. fd->data_ptr = p_data_ptr;
  816. fd->data_size = p_data_size;
  817. }
  818. void TextServerFallback::_font_set_style(const RID &p_font_rid, BitField<FontStyle> p_style) {
  819. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  820. ERR_FAIL_COND(!fd);
  821. MutexLock lock(fd->mutex);
  822. Vector2i size = _get_size(fd, 16);
  823. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  824. fd->style_flags = p_style;
  825. }
  826. void TextServerFallback::_font_set_face_index(const RID &p_font_rid, int64_t p_face_index) {
  827. ERR_FAIL_COND(p_face_index < 0);
  828. ERR_FAIL_COND(p_face_index >= 0x7FFF);
  829. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  830. ERR_FAIL_COND(!fd);
  831. MutexLock lock(fd->mutex);
  832. if (fd->face_index != p_face_index) {
  833. fd->face_index = p_face_index;
  834. _font_clear_cache(fd);
  835. }
  836. }
  837. int64_t TextServerFallback::_font_get_face_index(const RID &p_font_rid) const {
  838. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  839. ERR_FAIL_COND_V(!fd, 0);
  840. MutexLock lock(fd->mutex);
  841. return fd->face_index;
  842. }
  843. int64_t TextServerFallback::_font_get_face_count(const RID &p_font_rid) const {
  844. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  845. ERR_FAIL_COND_V(!fd, 0);
  846. MutexLock lock(fd->mutex);
  847. int face_count = 0;
  848. if (fd->data_ptr && (fd->data_size > 0)) {
  849. // Init dynamic font.
  850. #ifdef MODULE_FREETYPE_ENABLED
  851. int error = 0;
  852. if (!ft_library) {
  853. error = FT_Init_FreeType(&ft_library);
  854. ERR_FAIL_COND_V_MSG(error != 0, false, "FreeType: Error initializing library: '" + String(FT_Error_String(error)) + "'.");
  855. #ifdef MODULE_SVG_ENABLED
  856. FT_Property_Set(ft_library, "ot-svg", "svg-hooks", get_tvg_svg_in_ot_hooks());
  857. #endif
  858. }
  859. FT_StreamRec stream;
  860. memset(&stream, 0, sizeof(FT_StreamRec));
  861. stream.base = (unsigned char *)fd->data_ptr;
  862. stream.size = fd->data_size;
  863. stream.pos = 0;
  864. FT_Open_Args fargs;
  865. memset(&fargs, 0, sizeof(FT_Open_Args));
  866. fargs.memory_base = (unsigned char *)fd->data_ptr;
  867. fargs.memory_size = fd->data_size;
  868. fargs.flags = FT_OPEN_MEMORY;
  869. fargs.stream = &stream;
  870. FT_Face tmp_face = nullptr;
  871. error = FT_Open_Face(ft_library, &fargs, -1, &tmp_face);
  872. if (error == 0) {
  873. face_count = tmp_face->num_faces;
  874. FT_Done_Face(tmp_face);
  875. }
  876. #endif
  877. }
  878. return face_count;
  879. }
  880. BitField<TextServer::FontStyle> TextServerFallback::_font_get_style(const RID &p_font_rid) const {
  881. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  882. ERR_FAIL_COND_V(!fd, 0);
  883. MutexLock lock(fd->mutex);
  884. Vector2i size = _get_size(fd, 16);
  885. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), 0);
  886. return fd->style_flags;
  887. }
  888. void TextServerFallback::_font_set_style_name(const RID &p_font_rid, const String &p_name) {
  889. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  890. ERR_FAIL_COND(!fd);
  891. MutexLock lock(fd->mutex);
  892. Vector2i size = _get_size(fd, 16);
  893. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  894. fd->style_name = p_name;
  895. }
  896. String TextServerFallback::_font_get_style_name(const RID &p_font_rid) const {
  897. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  898. ERR_FAIL_COND_V(!fd, String());
  899. MutexLock lock(fd->mutex);
  900. Vector2i size = _get_size(fd, 16);
  901. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), String());
  902. return fd->style_name;
  903. }
  904. void TextServerFallback::_font_set_name(const RID &p_font_rid, const String &p_name) {
  905. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  906. ERR_FAIL_COND(!fd);
  907. MutexLock lock(fd->mutex);
  908. Vector2i size = _get_size(fd, 16);
  909. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  910. fd->font_name = p_name;
  911. }
  912. String TextServerFallback::_font_get_name(const RID &p_font_rid) const {
  913. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  914. ERR_FAIL_COND_V(!fd, String());
  915. MutexLock lock(fd->mutex);
  916. Vector2i size = _get_size(fd, 16);
  917. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), String());
  918. return fd->font_name;
  919. }
  920. void TextServerFallback::_font_set_antialiasing(const RID &p_font_rid, TextServer::FontAntialiasing p_antialiasing) {
  921. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  922. ERR_FAIL_COND(!fd);
  923. MutexLock lock(fd->mutex);
  924. if (fd->antialiasing != p_antialiasing) {
  925. _font_clear_cache(fd);
  926. fd->antialiasing = p_antialiasing;
  927. }
  928. }
  929. TextServer::FontAntialiasing TextServerFallback::_font_get_antialiasing(const RID &p_font_rid) const {
  930. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  931. ERR_FAIL_COND_V(!fd, TextServer::FONT_ANTIALIASING_NONE);
  932. MutexLock lock(fd->mutex);
  933. return fd->antialiasing;
  934. }
  935. void TextServerFallback::_font_set_generate_mipmaps(const RID &p_font_rid, bool p_generate_mipmaps) {
  936. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  937. ERR_FAIL_COND(!fd);
  938. MutexLock lock(fd->mutex);
  939. if (fd->mipmaps != p_generate_mipmaps) {
  940. for (KeyValue<Vector2i, FontForSizeFallback *> &E : fd->cache) {
  941. for (int i = 0; i < E.value->textures.size(); i++) {
  942. E.value->textures.write[i].dirty = true;
  943. E.value->textures.write[i].texture = Ref<ImageTexture>();
  944. }
  945. }
  946. fd->mipmaps = p_generate_mipmaps;
  947. }
  948. }
  949. bool TextServerFallback::_font_get_generate_mipmaps(const RID &p_font_rid) const {
  950. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  951. ERR_FAIL_COND_V(!fd, false);
  952. MutexLock lock(fd->mutex);
  953. return fd->mipmaps;
  954. }
  955. void TextServerFallback::_font_set_multichannel_signed_distance_field(const RID &p_font_rid, bool p_msdf) {
  956. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  957. ERR_FAIL_COND(!fd);
  958. MutexLock lock(fd->mutex);
  959. if (fd->msdf != p_msdf) {
  960. _font_clear_cache(fd);
  961. fd->msdf = p_msdf;
  962. }
  963. }
  964. bool TextServerFallback::_font_is_multichannel_signed_distance_field(const RID &p_font_rid) const {
  965. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  966. ERR_FAIL_COND_V(!fd, false);
  967. MutexLock lock(fd->mutex);
  968. return fd->msdf;
  969. }
  970. void TextServerFallback::_font_set_msdf_pixel_range(const RID &p_font_rid, int64_t p_msdf_pixel_range) {
  971. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  972. ERR_FAIL_COND(!fd);
  973. MutexLock lock(fd->mutex);
  974. if (fd->msdf_range != p_msdf_pixel_range) {
  975. _font_clear_cache(fd);
  976. fd->msdf_range = p_msdf_pixel_range;
  977. }
  978. }
  979. int64_t TextServerFallback::_font_get_msdf_pixel_range(const RID &p_font_rid) const {
  980. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  981. ERR_FAIL_COND_V(!fd, false);
  982. MutexLock lock(fd->mutex);
  983. return fd->msdf_range;
  984. }
  985. void TextServerFallback::_font_set_msdf_size(const RID &p_font_rid, int64_t p_msdf_size) {
  986. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  987. ERR_FAIL_COND(!fd);
  988. MutexLock lock(fd->mutex);
  989. if (fd->msdf_source_size != p_msdf_size) {
  990. _font_clear_cache(fd);
  991. fd->msdf_source_size = p_msdf_size;
  992. }
  993. }
  994. int64_t TextServerFallback::_font_get_msdf_size(const RID &p_font_rid) const {
  995. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  996. ERR_FAIL_COND_V(!fd, false);
  997. MutexLock lock(fd->mutex);
  998. return fd->msdf_source_size;
  999. }
  1000. void TextServerFallback::_font_set_fixed_size(const RID &p_font_rid, int64_t p_fixed_size) {
  1001. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  1002. ERR_FAIL_COND(!fd);
  1003. MutexLock lock(fd->mutex);
  1004. fd->fixed_size = p_fixed_size;
  1005. }
  1006. int64_t TextServerFallback::_font_get_fixed_size(const RID &p_font_rid) const {
  1007. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  1008. ERR_FAIL_COND_V(!fd, false);
  1009. MutexLock lock(fd->mutex);
  1010. return fd->fixed_size;
  1011. }
  1012. void TextServerFallback::_font_set_force_autohinter(const RID &p_font_rid, bool p_force_autohinter) {
  1013. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  1014. ERR_FAIL_COND(!fd);
  1015. MutexLock lock(fd->mutex);
  1016. if (fd->force_autohinter != p_force_autohinter) {
  1017. _font_clear_cache(fd);
  1018. fd->force_autohinter = p_force_autohinter;
  1019. }
  1020. }
  1021. bool TextServerFallback::_font_is_force_autohinter(const RID &p_font_rid) const {
  1022. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  1023. ERR_FAIL_COND_V(!fd, false);
  1024. MutexLock lock(fd->mutex);
  1025. return fd->force_autohinter;
  1026. }
  1027. void TextServerFallback::_font_set_hinting(const RID &p_font_rid, TextServer::Hinting p_hinting) {
  1028. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  1029. ERR_FAIL_COND(!fd);
  1030. MutexLock lock(fd->mutex);
  1031. if (fd->hinting != p_hinting) {
  1032. _font_clear_cache(fd);
  1033. fd->hinting = p_hinting;
  1034. }
  1035. }
  1036. TextServer::Hinting TextServerFallback::_font_get_hinting(const RID &p_font_rid) const {
  1037. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  1038. ERR_FAIL_COND_V(!fd, HINTING_NONE);
  1039. MutexLock lock(fd->mutex);
  1040. return fd->hinting;
  1041. }
  1042. void TextServerFallback::_font_set_subpixel_positioning(const RID &p_font_rid, TextServer::SubpixelPositioning p_subpixel) {
  1043. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  1044. ERR_FAIL_COND(!fd);
  1045. MutexLock lock(fd->mutex);
  1046. fd->subpixel_positioning = p_subpixel;
  1047. }
  1048. TextServer::SubpixelPositioning TextServerFallback::_font_get_subpixel_positioning(const RID &p_font_rid) const {
  1049. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  1050. ERR_FAIL_COND_V(!fd, SUBPIXEL_POSITIONING_DISABLED);
  1051. MutexLock lock(fd->mutex);
  1052. return fd->subpixel_positioning;
  1053. }
  1054. void TextServerFallback::_font_set_embolden(const RID &p_font_rid, double p_strength) {
  1055. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  1056. ERR_FAIL_COND(!fd);
  1057. MutexLock lock(fd->mutex);
  1058. if (fd->embolden != p_strength) {
  1059. _font_clear_cache(fd);
  1060. fd->embolden = p_strength;
  1061. }
  1062. }
  1063. double TextServerFallback::_font_get_embolden(const RID &p_font_rid) const {
  1064. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  1065. ERR_FAIL_COND_V(!fd, 0.0);
  1066. MutexLock lock(fd->mutex);
  1067. return fd->embolden;
  1068. }
  1069. void TextServerFallback::_font_set_transform(const RID &p_font_rid, const Transform2D &p_transform) {
  1070. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  1071. ERR_FAIL_COND(!fd);
  1072. MutexLock lock(fd->mutex);
  1073. if (fd->transform != p_transform) {
  1074. _font_clear_cache(fd);
  1075. fd->transform = p_transform;
  1076. }
  1077. }
  1078. Transform2D TextServerFallback::_font_get_transform(const RID &p_font_rid) const {
  1079. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  1080. ERR_FAIL_COND_V(!fd, Transform2D());
  1081. MutexLock lock(fd->mutex);
  1082. return fd->transform;
  1083. }
  1084. void TextServerFallback::_font_set_variation_coordinates(const RID &p_font_rid, const Dictionary &p_variation_coordinates) {
  1085. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  1086. ERR_FAIL_COND(!fd);
  1087. MutexLock lock(fd->mutex);
  1088. if (fd->variation_coordinates != p_variation_coordinates) {
  1089. _font_clear_cache(fd);
  1090. fd->variation_coordinates = p_variation_coordinates;
  1091. }
  1092. }
  1093. Dictionary TextServerFallback::_font_get_variation_coordinates(const RID &p_font_rid) const {
  1094. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  1095. ERR_FAIL_COND_V(!fd, Dictionary());
  1096. MutexLock lock(fd->mutex);
  1097. return fd->variation_coordinates;
  1098. }
  1099. void TextServerFallback::_font_set_oversampling(const RID &p_font_rid, double p_oversampling) {
  1100. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  1101. ERR_FAIL_COND(!fd);
  1102. MutexLock lock(fd->mutex);
  1103. if (fd->oversampling != p_oversampling) {
  1104. _font_clear_cache(fd);
  1105. fd->oversampling = p_oversampling;
  1106. }
  1107. }
  1108. double TextServerFallback::_font_get_oversampling(const RID &p_font_rid) const {
  1109. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  1110. ERR_FAIL_COND_V(!fd, 0.0);
  1111. MutexLock lock(fd->mutex);
  1112. return fd->oversampling;
  1113. }
  1114. TypedArray<Vector2i> TextServerFallback::_font_get_size_cache_list(const RID &p_font_rid) const {
  1115. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  1116. ERR_FAIL_COND_V(!fd, TypedArray<Vector2i>());
  1117. MutexLock lock(fd->mutex);
  1118. TypedArray<Vector2i> ret;
  1119. for (const KeyValue<Vector2i, FontForSizeFallback *> &E : fd->cache) {
  1120. ret.push_back(E.key);
  1121. }
  1122. return ret;
  1123. }
  1124. void TextServerFallback::_font_clear_size_cache(const RID &p_font_rid) {
  1125. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  1126. ERR_FAIL_COND(!fd);
  1127. MutexLock lock(fd->mutex);
  1128. for (const KeyValue<Vector2i, FontForSizeFallback *> &E : fd->cache) {
  1129. memdelete(E.value);
  1130. }
  1131. fd->cache.clear();
  1132. }
  1133. void TextServerFallback::_font_remove_size_cache(const RID &p_font_rid, const Vector2i &p_size) {
  1134. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  1135. ERR_FAIL_COND(!fd);
  1136. MutexLock lock(fd->mutex);
  1137. if (fd->cache.has(p_size)) {
  1138. memdelete(fd->cache[p_size]);
  1139. fd->cache.erase(p_size);
  1140. }
  1141. }
  1142. void TextServerFallback::_font_set_ascent(const RID &p_font_rid, int64_t p_size, double p_ascent) {
  1143. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  1144. ERR_FAIL_COND(!fd);
  1145. MutexLock lock(fd->mutex);
  1146. Vector2i size = _get_size(fd, p_size);
  1147. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1148. fd->cache[size]->ascent = p_ascent;
  1149. }
  1150. double TextServerFallback::_font_get_ascent(const RID &p_font_rid, int64_t p_size) const {
  1151. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  1152. ERR_FAIL_COND_V(!fd, 0.0);
  1153. MutexLock lock(fd->mutex);
  1154. Vector2i size = _get_size(fd, p_size);
  1155. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), 0.0);
  1156. if (fd->msdf) {
  1157. return fd->cache[size]->ascent * (double)p_size / (double)fd->msdf_source_size;
  1158. } else {
  1159. return fd->cache[size]->ascent;
  1160. }
  1161. }
  1162. void TextServerFallback::_font_set_descent(const RID &p_font_rid, int64_t p_size, double p_descent) {
  1163. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  1164. ERR_FAIL_COND(!fd);
  1165. Vector2i size = _get_size(fd, p_size);
  1166. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1167. fd->cache[size]->descent = p_descent;
  1168. }
  1169. double TextServerFallback::_font_get_descent(const RID &p_font_rid, int64_t p_size) const {
  1170. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  1171. ERR_FAIL_COND_V(!fd, 0.0);
  1172. MutexLock lock(fd->mutex);
  1173. Vector2i size = _get_size(fd, p_size);
  1174. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), 0.0);
  1175. if (fd->msdf) {
  1176. return fd->cache[size]->descent * (double)p_size / (double)fd->msdf_source_size;
  1177. } else {
  1178. return fd->cache[size]->descent;
  1179. }
  1180. }
  1181. void TextServerFallback::_font_set_underline_position(const RID &p_font_rid, int64_t p_size, double p_underline_position) {
  1182. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  1183. ERR_FAIL_COND(!fd);
  1184. MutexLock lock(fd->mutex);
  1185. Vector2i size = _get_size(fd, p_size);
  1186. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1187. fd->cache[size]->underline_position = p_underline_position;
  1188. }
  1189. double TextServerFallback::_font_get_underline_position(const RID &p_font_rid, int64_t p_size) const {
  1190. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  1191. ERR_FAIL_COND_V(!fd, 0.0);
  1192. MutexLock lock(fd->mutex);
  1193. Vector2i size = _get_size(fd, p_size);
  1194. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), 0.0);
  1195. if (fd->msdf) {
  1196. return fd->cache[size]->underline_position * (double)p_size / (double)fd->msdf_source_size;
  1197. } else {
  1198. return fd->cache[size]->underline_position;
  1199. }
  1200. }
  1201. void TextServerFallback::_font_set_underline_thickness(const RID &p_font_rid, int64_t p_size, double p_underline_thickness) {
  1202. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  1203. ERR_FAIL_COND(!fd);
  1204. MutexLock lock(fd->mutex);
  1205. Vector2i size = _get_size(fd, p_size);
  1206. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1207. fd->cache[size]->underline_thickness = p_underline_thickness;
  1208. }
  1209. double TextServerFallback::_font_get_underline_thickness(const RID &p_font_rid, int64_t p_size) const {
  1210. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  1211. ERR_FAIL_COND_V(!fd, 0.0);
  1212. MutexLock lock(fd->mutex);
  1213. Vector2i size = _get_size(fd, p_size);
  1214. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), 0.0);
  1215. if (fd->msdf) {
  1216. return fd->cache[size]->underline_thickness * (double)p_size / (double)fd->msdf_source_size;
  1217. } else {
  1218. return fd->cache[size]->underline_thickness;
  1219. }
  1220. }
  1221. void TextServerFallback::_font_set_scale(const RID &p_font_rid, int64_t p_size, double p_scale) {
  1222. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  1223. ERR_FAIL_COND(!fd);
  1224. MutexLock lock(fd->mutex);
  1225. Vector2i size = _get_size(fd, p_size);
  1226. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1227. #ifdef MODULE_FREETYPE_ENABLED
  1228. if (fd->cache[size]->face) {
  1229. return; // Do not override scale for dynamic fonts, it's calculated automatically.
  1230. }
  1231. #endif
  1232. fd->cache[size]->scale = p_scale;
  1233. }
  1234. double TextServerFallback::_font_get_scale(const RID &p_font_rid, int64_t p_size) const {
  1235. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  1236. ERR_FAIL_COND_V(!fd, 0.0);
  1237. MutexLock lock(fd->mutex);
  1238. Vector2i size = _get_size(fd, p_size);
  1239. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), 0.0);
  1240. if (fd->msdf) {
  1241. return fd->cache[size]->scale * (double)p_size / (double)fd->msdf_source_size;
  1242. } else {
  1243. return fd->cache[size]->scale / fd->cache[size]->oversampling;
  1244. }
  1245. }
  1246. int64_t TextServerFallback::_font_get_texture_count(const RID &p_font_rid, const Vector2i &p_size) const {
  1247. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  1248. ERR_FAIL_COND_V(!fd, 0);
  1249. MutexLock lock(fd->mutex);
  1250. Vector2i size = _get_size_outline(fd, p_size);
  1251. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), 0);
  1252. return fd->cache[size]->textures.size();
  1253. }
  1254. void TextServerFallback::_font_clear_textures(const RID &p_font_rid, const Vector2i &p_size) {
  1255. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  1256. ERR_FAIL_COND(!fd);
  1257. MutexLock lock(fd->mutex);
  1258. Vector2i size = _get_size_outline(fd, p_size);
  1259. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1260. fd->cache[size]->textures.clear();
  1261. }
  1262. void TextServerFallback::_font_remove_texture(const RID &p_font_rid, const Vector2i &p_size, int64_t p_texture_index) {
  1263. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  1264. ERR_FAIL_COND(!fd);
  1265. MutexLock lock(fd->mutex);
  1266. Vector2i size = _get_size_outline(fd, p_size);
  1267. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1268. ERR_FAIL_INDEX(p_texture_index, fd->cache[size]->textures.size());
  1269. fd->cache[size]->textures.remove_at(p_texture_index);
  1270. }
  1271. void TextServerFallback::_font_set_texture_image(const RID &p_font_rid, const Vector2i &p_size, int64_t p_texture_index, const Ref<Image> &p_image) {
  1272. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  1273. ERR_FAIL_COND(!fd);
  1274. ERR_FAIL_COND(p_image.is_null());
  1275. MutexLock lock(fd->mutex);
  1276. Vector2i size = _get_size_outline(fd, p_size);
  1277. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1278. ERR_FAIL_COND(p_texture_index < 0);
  1279. if (p_texture_index >= fd->cache[size]->textures.size()) {
  1280. fd->cache[size]->textures.resize(p_texture_index + 1);
  1281. }
  1282. ShelfPackTexture &tex = fd->cache[size]->textures.write[p_texture_index];
  1283. tex.imgdata = p_image->get_data();
  1284. tex.texture_w = p_image->get_width();
  1285. tex.texture_h = p_image->get_height();
  1286. tex.format = p_image->get_format();
  1287. Ref<Image> img = Image::create_from_data(tex.texture_w, tex.texture_h, false, tex.format, tex.imgdata);
  1288. if (fd->mipmaps) {
  1289. img->generate_mipmaps();
  1290. }
  1291. tex.texture = ImageTexture::create_from_image(img);
  1292. tex.dirty = false;
  1293. }
  1294. Ref<Image> TextServerFallback::_font_get_texture_image(const RID &p_font_rid, const Vector2i &p_size, int64_t p_texture_index) const {
  1295. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  1296. ERR_FAIL_COND_V(!fd, Ref<Image>());
  1297. MutexLock lock(fd->mutex);
  1298. Vector2i size = _get_size_outline(fd, p_size);
  1299. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), Ref<Image>());
  1300. ERR_FAIL_INDEX_V(p_texture_index, fd->cache[size]->textures.size(), Ref<Image>());
  1301. const ShelfPackTexture &tex = fd->cache[size]->textures[p_texture_index];
  1302. return Image::create_from_data(tex.texture_w, tex.texture_h, false, tex.format, tex.imgdata);
  1303. }
  1304. void TextServerFallback::_font_set_texture_offsets(const RID &p_font_rid, const Vector2i &p_size, int64_t p_texture_index, const PackedInt32Array &p_offsets) {
  1305. ERR_FAIL_COND(p_offsets.size() % 4 != 0);
  1306. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  1307. ERR_FAIL_COND(!fd);
  1308. MutexLock lock(fd->mutex);
  1309. Vector2i size = _get_size_outline(fd, p_size);
  1310. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1311. ERR_FAIL_COND(p_texture_index < 0);
  1312. if (p_texture_index >= fd->cache[size]->textures.size()) {
  1313. fd->cache[size]->textures.resize(p_texture_index + 1);
  1314. }
  1315. ShelfPackTexture &tex = fd->cache[size]->textures.write[p_texture_index];
  1316. tex.shelves.clear();
  1317. for (int32_t i = 0; i < p_offsets.size(); i += 4) {
  1318. tex.shelves.push_back(Shelf(p_offsets[i], p_offsets[i + 1], p_offsets[i + 2], p_offsets[i + 3]));
  1319. }
  1320. }
  1321. PackedInt32Array TextServerFallback::_font_get_texture_offsets(const RID &p_font_rid, const Vector2i &p_size, int64_t p_texture_index) const {
  1322. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  1323. ERR_FAIL_COND_V(!fd, PackedInt32Array());
  1324. MutexLock lock(fd->mutex);
  1325. Vector2i size = _get_size_outline(fd, p_size);
  1326. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), PackedInt32Array());
  1327. ERR_FAIL_INDEX_V(p_texture_index, fd->cache[size]->textures.size(), PackedInt32Array());
  1328. const ShelfPackTexture &tex = fd->cache[size]->textures[p_texture_index];
  1329. PackedInt32Array ret;
  1330. ret.resize(tex.shelves.size() * 4);
  1331. int32_t *wr = ret.ptrw();
  1332. int32_t i = 0;
  1333. for (const Shelf &E : tex.shelves) {
  1334. wr[i * 4] = E.x;
  1335. wr[i * 4 + 1] = E.y;
  1336. wr[i * 4 + 2] = E.w;
  1337. wr[i * 4 + 3] = E.h;
  1338. i++;
  1339. }
  1340. return ret;
  1341. }
  1342. PackedInt32Array TextServerFallback::_font_get_glyph_list(const RID &p_font_rid, const Vector2i &p_size) const {
  1343. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  1344. ERR_FAIL_COND_V(!fd, PackedInt32Array());
  1345. MutexLock lock(fd->mutex);
  1346. Vector2i size = _get_size_outline(fd, p_size);
  1347. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), PackedInt32Array());
  1348. PackedInt32Array ret;
  1349. const HashMap<int32_t, FontGlyph> &gl = fd->cache[size]->glyph_map;
  1350. for (const KeyValue<int32_t, FontGlyph> &E : gl) {
  1351. ret.push_back(E.key);
  1352. }
  1353. return ret;
  1354. }
  1355. void TextServerFallback::_font_clear_glyphs(const RID &p_font_rid, const Vector2i &p_size) {
  1356. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  1357. ERR_FAIL_COND(!fd);
  1358. MutexLock lock(fd->mutex);
  1359. Vector2i size = _get_size_outline(fd, p_size);
  1360. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1361. fd->cache[size]->glyph_map.clear();
  1362. }
  1363. void TextServerFallback::_font_remove_glyph(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph) {
  1364. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  1365. ERR_FAIL_COND(!fd);
  1366. MutexLock lock(fd->mutex);
  1367. Vector2i size = _get_size_outline(fd, p_size);
  1368. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1369. fd->cache[size]->glyph_map.erase(p_glyph);
  1370. }
  1371. Vector2 TextServerFallback::_font_get_glyph_advance(const RID &p_font_rid, int64_t p_size, int64_t p_glyph) const {
  1372. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  1373. ERR_FAIL_COND_V(!fd, Vector2());
  1374. MutexLock lock(fd->mutex);
  1375. Vector2i size = _get_size(fd, p_size);
  1376. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), Vector2());
  1377. int mod = 0;
  1378. if (fd->antialiasing == FONT_ANTIALIASING_LCD) {
  1379. TextServer::FontLCDSubpixelLayout layout = (TextServer::FontLCDSubpixelLayout)(int)GLOBAL_GET("gui/theme/lcd_subpixel_layout");
  1380. if (layout != FONT_LCD_SUBPIXEL_LAYOUT_NONE) {
  1381. mod = (layout << 24);
  1382. }
  1383. }
  1384. if (!_ensure_glyph(fd, size, p_glyph | mod)) {
  1385. return Vector2(); // Invalid or non graphicl glyph, do not display errors.
  1386. }
  1387. const HashMap<int32_t, FontGlyph> &gl = fd->cache[size]->glyph_map;
  1388. Vector2 ea;
  1389. if (fd->embolden != 0.0) {
  1390. ea.x = fd->embolden * double(size.x) / 64.0;
  1391. }
  1392. double scale = _font_get_scale(p_font_rid, p_size);
  1393. if (fd->msdf) {
  1394. return (gl[p_glyph | mod].advance + ea) * (double)p_size / (double)fd->msdf_source_size;
  1395. } else if ((scale == 1.0) && ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_DISABLED) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x > SUBPIXEL_POSITIONING_ONE_HALF_MAX_SIZE))) {
  1396. return (gl[p_glyph | mod].advance + ea).round();
  1397. } else {
  1398. return gl[p_glyph | mod].advance + ea;
  1399. }
  1400. }
  1401. void TextServerFallback::_font_set_glyph_advance(const RID &p_font_rid, int64_t p_size, int64_t p_glyph, const Vector2 &p_advance) {
  1402. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  1403. ERR_FAIL_COND(!fd);
  1404. MutexLock lock(fd->mutex);
  1405. Vector2i size = _get_size(fd, p_size);
  1406. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1407. HashMap<int32_t, FontGlyph> &gl = fd->cache[size]->glyph_map;
  1408. gl[p_glyph].advance = p_advance;
  1409. gl[p_glyph].found = true;
  1410. }
  1411. Vector2 TextServerFallback::_font_get_glyph_offset(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph) const {
  1412. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  1413. ERR_FAIL_COND_V(!fd, Vector2());
  1414. MutexLock lock(fd->mutex);
  1415. Vector2i size = _get_size_outline(fd, p_size);
  1416. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), Vector2());
  1417. int mod = 0;
  1418. if (fd->antialiasing == FONT_ANTIALIASING_LCD) {
  1419. TextServer::FontLCDSubpixelLayout layout = (TextServer::FontLCDSubpixelLayout)(int)GLOBAL_GET("gui/theme/lcd_subpixel_layout");
  1420. if (layout != FONT_LCD_SUBPIXEL_LAYOUT_NONE) {
  1421. mod = (layout << 24);
  1422. }
  1423. }
  1424. if (!_ensure_glyph(fd, size, p_glyph | mod)) {
  1425. return Vector2(); // Invalid or non graphicl glyph, do not display errors.
  1426. }
  1427. const HashMap<int32_t, FontGlyph> &gl = fd->cache[size]->glyph_map;
  1428. if (fd->msdf) {
  1429. return gl[p_glyph | mod].rect.position * (double)p_size.x / (double)fd->msdf_source_size;
  1430. } else {
  1431. return gl[p_glyph | mod].rect.position;
  1432. }
  1433. }
  1434. void TextServerFallback::_font_set_glyph_offset(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph, const Vector2 &p_offset) {
  1435. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  1436. ERR_FAIL_COND(!fd);
  1437. MutexLock lock(fd->mutex);
  1438. Vector2i size = _get_size_outline(fd, p_size);
  1439. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1440. HashMap<int32_t, FontGlyph> &gl = fd->cache[size]->glyph_map;
  1441. gl[p_glyph].rect.position = p_offset;
  1442. gl[p_glyph].found = true;
  1443. }
  1444. Vector2 TextServerFallback::_font_get_glyph_size(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph) const {
  1445. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  1446. ERR_FAIL_COND_V(!fd, Vector2());
  1447. MutexLock lock(fd->mutex);
  1448. Vector2i size = _get_size_outline(fd, p_size);
  1449. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), Vector2());
  1450. int mod = 0;
  1451. if (fd->antialiasing == FONT_ANTIALIASING_LCD) {
  1452. TextServer::FontLCDSubpixelLayout layout = (TextServer::FontLCDSubpixelLayout)(int)GLOBAL_GET("gui/theme/lcd_subpixel_layout");
  1453. if (layout != FONT_LCD_SUBPIXEL_LAYOUT_NONE) {
  1454. mod = (layout << 24);
  1455. }
  1456. }
  1457. if (!_ensure_glyph(fd, size, p_glyph | mod)) {
  1458. return Vector2(); // Invalid or non graphicl glyph, do not display errors.
  1459. }
  1460. const HashMap<int32_t, FontGlyph> &gl = fd->cache[size]->glyph_map;
  1461. if (fd->msdf) {
  1462. return gl[p_glyph | mod].rect.size * (double)p_size.x / (double)fd->msdf_source_size;
  1463. } else {
  1464. return gl[p_glyph | mod].rect.size;
  1465. }
  1466. }
  1467. void TextServerFallback::_font_set_glyph_size(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph, const Vector2 &p_gl_size) {
  1468. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  1469. ERR_FAIL_COND(!fd);
  1470. MutexLock lock(fd->mutex);
  1471. Vector2i size = _get_size_outline(fd, p_size);
  1472. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1473. HashMap<int32_t, FontGlyph> &gl = fd->cache[size]->glyph_map;
  1474. gl[p_glyph].rect.size = p_gl_size;
  1475. gl[p_glyph].found = true;
  1476. }
  1477. Rect2 TextServerFallback::_font_get_glyph_uv_rect(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph) const {
  1478. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  1479. ERR_FAIL_COND_V(!fd, Rect2());
  1480. MutexLock lock(fd->mutex);
  1481. Vector2i size = _get_size_outline(fd, p_size);
  1482. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), Rect2());
  1483. int mod = 0;
  1484. if (fd->antialiasing == FONT_ANTIALIASING_LCD) {
  1485. TextServer::FontLCDSubpixelLayout layout = (TextServer::FontLCDSubpixelLayout)(int)GLOBAL_GET("gui/theme/lcd_subpixel_layout");
  1486. if (layout != FONT_LCD_SUBPIXEL_LAYOUT_NONE) {
  1487. mod = (layout << 24);
  1488. }
  1489. }
  1490. if (!_ensure_glyph(fd, size, p_glyph | mod)) {
  1491. return Rect2(); // Invalid or non graphicl glyph, do not display errors.
  1492. }
  1493. const HashMap<int32_t, FontGlyph> &gl = fd->cache[size]->glyph_map;
  1494. return gl[p_glyph | mod].uv_rect;
  1495. }
  1496. void TextServerFallback::_font_set_glyph_uv_rect(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph, const Rect2 &p_uv_rect) {
  1497. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  1498. ERR_FAIL_COND(!fd);
  1499. MutexLock lock(fd->mutex);
  1500. Vector2i size = _get_size_outline(fd, p_size);
  1501. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1502. HashMap<int32_t, FontGlyph> &gl = fd->cache[size]->glyph_map;
  1503. gl[p_glyph].uv_rect = p_uv_rect;
  1504. gl[p_glyph].found = true;
  1505. }
  1506. int64_t TextServerFallback::_font_get_glyph_texture_idx(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph) const {
  1507. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  1508. ERR_FAIL_COND_V(!fd, -1);
  1509. MutexLock lock(fd->mutex);
  1510. Vector2i size = _get_size_outline(fd, p_size);
  1511. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), -1);
  1512. int mod = 0;
  1513. if (fd->antialiasing == FONT_ANTIALIASING_LCD) {
  1514. TextServer::FontLCDSubpixelLayout layout = (TextServer::FontLCDSubpixelLayout)(int)GLOBAL_GET("gui/theme/lcd_subpixel_layout");
  1515. if (layout != FONT_LCD_SUBPIXEL_LAYOUT_NONE) {
  1516. mod = (layout << 24);
  1517. }
  1518. }
  1519. if (!_ensure_glyph(fd, size, p_glyph | mod)) {
  1520. return -1; // Invalid or non graphicl glyph, do not display errors.
  1521. }
  1522. const HashMap<int32_t, FontGlyph> &gl = fd->cache[size]->glyph_map;
  1523. return gl[p_glyph | mod].texture_idx;
  1524. }
  1525. void TextServerFallback::_font_set_glyph_texture_idx(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph, int64_t p_texture_idx) {
  1526. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  1527. ERR_FAIL_COND(!fd);
  1528. MutexLock lock(fd->mutex);
  1529. Vector2i size = _get_size_outline(fd, p_size);
  1530. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1531. HashMap<int32_t, FontGlyph> &gl = fd->cache[size]->glyph_map;
  1532. gl[p_glyph].texture_idx = p_texture_idx;
  1533. gl[p_glyph].found = true;
  1534. }
  1535. RID TextServerFallback::_font_get_glyph_texture_rid(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph) const {
  1536. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  1537. ERR_FAIL_COND_V(!fd, RID());
  1538. MutexLock lock(fd->mutex);
  1539. Vector2i size = _get_size_outline(fd, p_size);
  1540. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), RID());
  1541. int mod = 0;
  1542. if (fd->antialiasing == FONT_ANTIALIASING_LCD) {
  1543. TextServer::FontLCDSubpixelLayout layout = (TextServer::FontLCDSubpixelLayout)(int)GLOBAL_GET("gui/theme/lcd_subpixel_layout");
  1544. if (layout != FONT_LCD_SUBPIXEL_LAYOUT_NONE) {
  1545. mod = (layout << 24);
  1546. }
  1547. }
  1548. if (!_ensure_glyph(fd, size, p_glyph | mod)) {
  1549. return RID(); // Invalid or non graphicl glyph, do not display errors.
  1550. }
  1551. const HashMap<int32_t, FontGlyph> &gl = fd->cache[size]->glyph_map;
  1552. ERR_FAIL_COND_V(gl[p_glyph | mod].texture_idx < -1 || gl[p_glyph | mod].texture_idx >= fd->cache[size]->textures.size(), RID());
  1553. if (RenderingServer::get_singleton() != nullptr) {
  1554. if (gl[p_glyph | mod].texture_idx != -1) {
  1555. if (fd->cache[size]->textures[gl[p_glyph | mod].texture_idx].dirty) {
  1556. ShelfPackTexture &tex = fd->cache[size]->textures.write[gl[p_glyph | mod].texture_idx];
  1557. Ref<Image> img = Image::create_from_data(tex.texture_w, tex.texture_h, false, tex.format, tex.imgdata);
  1558. if (fd->mipmaps) {
  1559. img->generate_mipmaps();
  1560. }
  1561. if (tex.texture.is_null()) {
  1562. tex.texture = ImageTexture::create_from_image(img);
  1563. } else {
  1564. tex.texture->update(img);
  1565. }
  1566. tex.dirty = false;
  1567. }
  1568. return fd->cache[size]->textures[gl[p_glyph | mod].texture_idx].texture->get_rid();
  1569. }
  1570. }
  1571. return RID();
  1572. }
  1573. Size2 TextServerFallback::_font_get_glyph_texture_size(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph) const {
  1574. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  1575. ERR_FAIL_COND_V(!fd, Size2());
  1576. MutexLock lock(fd->mutex);
  1577. Vector2i size = _get_size_outline(fd, p_size);
  1578. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), Size2());
  1579. int mod = 0;
  1580. if (fd->antialiasing == FONT_ANTIALIASING_LCD) {
  1581. TextServer::FontLCDSubpixelLayout layout = (TextServer::FontLCDSubpixelLayout)(int)GLOBAL_GET("gui/theme/lcd_subpixel_layout");
  1582. if (layout != FONT_LCD_SUBPIXEL_LAYOUT_NONE) {
  1583. mod = (layout << 24);
  1584. }
  1585. }
  1586. if (!_ensure_glyph(fd, size, p_glyph | mod)) {
  1587. return Size2(); // Invalid or non graphicl glyph, do not display errors.
  1588. }
  1589. const HashMap<int32_t, FontGlyph> &gl = fd->cache[size]->glyph_map;
  1590. ERR_FAIL_COND_V(gl[p_glyph | mod].texture_idx < -1 || gl[p_glyph | mod].texture_idx >= fd->cache[size]->textures.size(), Size2());
  1591. if (RenderingServer::get_singleton() != nullptr) {
  1592. if (gl[p_glyph | mod].texture_idx != -1) {
  1593. if (fd->cache[size]->textures[gl[p_glyph | mod].texture_idx].dirty) {
  1594. ShelfPackTexture &tex = fd->cache[size]->textures.write[gl[p_glyph | mod].texture_idx];
  1595. Ref<Image> img = Image::create_from_data(tex.texture_w, tex.texture_h, false, tex.format, tex.imgdata);
  1596. if (fd->mipmaps) {
  1597. img->generate_mipmaps();
  1598. }
  1599. if (tex.texture.is_null()) {
  1600. tex.texture = ImageTexture::create_from_image(img);
  1601. } else {
  1602. tex.texture->update(img);
  1603. }
  1604. tex.dirty = false;
  1605. }
  1606. return fd->cache[size]->textures[gl[p_glyph | mod].texture_idx].texture->get_size();
  1607. }
  1608. }
  1609. return Size2();
  1610. }
  1611. Dictionary TextServerFallback::_font_get_glyph_contours(const RID &p_font_rid, int64_t p_size, int64_t p_index) const {
  1612. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  1613. ERR_FAIL_COND_V(!fd, Dictionary());
  1614. MutexLock lock(fd->mutex);
  1615. Vector2i size = _get_size(fd, p_size);
  1616. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), Dictionary());
  1617. #ifdef MODULE_FREETYPE_ENABLED
  1618. PackedVector3Array points;
  1619. PackedInt32Array contours;
  1620. int32_t index = p_index & 0xffffff; // Remove subpixel shifts.
  1621. int error = FT_Load_Glyph(fd->cache[size]->face, FT_Get_Char_Index(fd->cache[size]->face, index), FT_LOAD_NO_BITMAP | (fd->force_autohinter ? FT_LOAD_FORCE_AUTOHINT : 0));
  1622. ERR_FAIL_COND_V(error, Dictionary());
  1623. if (fd->embolden != 0.f) {
  1624. FT_Pos strength = fd->embolden * p_size * 4; // 26.6 fractional units (1 / 64).
  1625. FT_Outline_Embolden(&fd->cache[size]->face->glyph->outline, strength);
  1626. }
  1627. if (fd->transform != Transform2D()) {
  1628. FT_Matrix mat = { FT_Fixed(fd->transform[0][0] * 65536), FT_Fixed(fd->transform[0][1] * 65536), FT_Fixed(fd->transform[1][0] * 65536), FT_Fixed(fd->transform[1][1] * 65536) }; // 16.16 fractional units (1 / 65536).
  1629. FT_Outline_Transform(&fd->cache[size]->face->glyph->outline, &mat);
  1630. }
  1631. double scale = (1.0 / 64.0) / fd->cache[size]->oversampling * fd->cache[size]->scale;
  1632. if (fd->msdf) {
  1633. scale = scale * (double)p_size / (double)fd->msdf_source_size;
  1634. }
  1635. for (short i = 0; i < fd->cache[size]->face->glyph->outline.n_points; i++) {
  1636. points.push_back(Vector3(fd->cache[size]->face->glyph->outline.points[i].x * scale, -fd->cache[size]->face->glyph->outline.points[i].y * scale, FT_CURVE_TAG(fd->cache[size]->face->glyph->outline.tags[i])));
  1637. }
  1638. for (short i = 0; i < fd->cache[size]->face->glyph->outline.n_contours; i++) {
  1639. contours.push_back(fd->cache[size]->face->glyph->outline.contours[i]);
  1640. }
  1641. bool orientation = (FT_Outline_Get_Orientation(&fd->cache[size]->face->glyph->outline) == FT_ORIENTATION_FILL_RIGHT);
  1642. Dictionary out;
  1643. out["points"] = points;
  1644. out["contours"] = contours;
  1645. out["orientation"] = orientation;
  1646. return out;
  1647. #else
  1648. return Dictionary();
  1649. #endif
  1650. }
  1651. TypedArray<Vector2i> TextServerFallback::_font_get_kerning_list(const RID &p_font_rid, int64_t p_size) const {
  1652. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  1653. ERR_FAIL_COND_V(!fd, TypedArray<Vector2i>());
  1654. MutexLock lock(fd->mutex);
  1655. Vector2i size = _get_size(fd, p_size);
  1656. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), TypedArray<Vector2i>());
  1657. TypedArray<Vector2i> ret;
  1658. for (const KeyValue<Vector2i, Vector2> &E : fd->cache[size]->kerning_map) {
  1659. ret.push_back(E.key);
  1660. }
  1661. return ret;
  1662. }
  1663. void TextServerFallback::_font_clear_kerning_map(const RID &p_font_rid, int64_t p_size) {
  1664. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  1665. ERR_FAIL_COND(!fd);
  1666. MutexLock lock(fd->mutex);
  1667. Vector2i size = _get_size(fd, p_size);
  1668. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1669. fd->cache[size]->kerning_map.clear();
  1670. }
  1671. void TextServerFallback::_font_remove_kerning(const RID &p_font_rid, int64_t p_size, const Vector2i &p_glyph_pair) {
  1672. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  1673. ERR_FAIL_COND(!fd);
  1674. MutexLock lock(fd->mutex);
  1675. Vector2i size = _get_size(fd, p_size);
  1676. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1677. fd->cache[size]->kerning_map.erase(p_glyph_pair);
  1678. }
  1679. void TextServerFallback::_font_set_kerning(const RID &p_font_rid, int64_t p_size, const Vector2i &p_glyph_pair, const Vector2 &p_kerning) {
  1680. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  1681. ERR_FAIL_COND(!fd);
  1682. MutexLock lock(fd->mutex);
  1683. Vector2i size = _get_size(fd, p_size);
  1684. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1685. fd->cache[size]->kerning_map[p_glyph_pair] = p_kerning;
  1686. }
  1687. Vector2 TextServerFallback::_font_get_kerning(const RID &p_font_rid, int64_t p_size, const Vector2i &p_glyph_pair) const {
  1688. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  1689. ERR_FAIL_COND_V(!fd, Vector2());
  1690. MutexLock lock(fd->mutex);
  1691. Vector2i size = _get_size(fd, p_size);
  1692. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), Vector2());
  1693. const HashMap<Vector2i, Vector2> &kern = fd->cache[size]->kerning_map;
  1694. if (kern.has(p_glyph_pair)) {
  1695. if (fd->msdf) {
  1696. return kern[p_glyph_pair] * (double)p_size / (double)fd->msdf_source_size;
  1697. } else {
  1698. return kern[p_glyph_pair];
  1699. }
  1700. } else {
  1701. #ifdef MODULE_FREETYPE_ENABLED
  1702. if (fd->cache[size]->face) {
  1703. FT_Vector delta;
  1704. int32_t glyph_a = FT_Get_Char_Index(fd->cache[size]->face, p_glyph_pair.x);
  1705. int32_t glyph_b = FT_Get_Char_Index(fd->cache[size]->face, p_glyph_pair.y);
  1706. FT_Get_Kerning(fd->cache[size]->face, glyph_a, glyph_b, FT_KERNING_DEFAULT, &delta);
  1707. if (fd->msdf) {
  1708. return Vector2(delta.x, delta.y) * (double)p_size / (double)fd->msdf_source_size;
  1709. } else {
  1710. return Vector2(delta.x, delta.y);
  1711. }
  1712. }
  1713. #endif
  1714. }
  1715. return Vector2();
  1716. }
  1717. int64_t TextServerFallback::_font_get_glyph_index(const RID &p_font_rid, int64_t p_size, int64_t p_char, int64_t p_variation_selector) const {
  1718. 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) + ".");
  1719. return (int64_t)p_char;
  1720. }
  1721. bool TextServerFallback::_font_has_char(const RID &p_font_rid, int64_t p_char) const {
  1722. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  1723. 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) + ".");
  1724. if (!fd) {
  1725. return false;
  1726. }
  1727. MutexLock lock(fd->mutex);
  1728. if (fd->cache.is_empty()) {
  1729. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, fd->msdf ? Vector2i(fd->msdf_source_size, 0) : Vector2i(16, 0)), false);
  1730. }
  1731. FontForSizeFallback *at_size = fd->cache.begin()->value;
  1732. #ifdef MODULE_FREETYPE_ENABLED
  1733. if (at_size && at_size->face) {
  1734. return FT_Get_Char_Index(at_size->face, p_char) != 0;
  1735. }
  1736. #endif
  1737. return (at_size) ? at_size->glyph_map.has((int32_t)p_char) : false;
  1738. }
  1739. String TextServerFallback::_font_get_supported_chars(const RID &p_font_rid) const {
  1740. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  1741. ERR_FAIL_COND_V(!fd, String());
  1742. MutexLock lock(fd->mutex);
  1743. if (fd->cache.is_empty()) {
  1744. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, fd->msdf ? Vector2i(fd->msdf_source_size, 0) : Vector2i(16, 0)), String());
  1745. }
  1746. FontForSizeFallback *at_size = fd->cache.begin()->value;
  1747. String chars;
  1748. #ifdef MODULE_FREETYPE_ENABLED
  1749. if (at_size && at_size->face) {
  1750. FT_UInt gindex;
  1751. FT_ULong charcode = FT_Get_First_Char(at_size->face, &gindex);
  1752. while (gindex != 0) {
  1753. if (charcode != 0) {
  1754. chars = chars + String::chr(charcode);
  1755. }
  1756. charcode = FT_Get_Next_Char(at_size->face, charcode, &gindex);
  1757. }
  1758. return chars;
  1759. }
  1760. #endif
  1761. if (at_size) {
  1762. const HashMap<int32_t, FontGlyph> &gl = at_size->glyph_map;
  1763. for (const KeyValue<int32_t, FontGlyph> &E : gl) {
  1764. chars = chars + String::chr(E.key);
  1765. }
  1766. }
  1767. return chars;
  1768. }
  1769. void TextServerFallback::_font_render_range(const RID &p_font_rid, const Vector2i &p_size, int64_t p_start, int64_t p_end) {
  1770. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  1771. ERR_FAIL_COND(!fd);
  1772. 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) + ".");
  1773. 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) + ".");
  1774. MutexLock lock(fd->mutex);
  1775. Vector2i size = _get_size_outline(fd, p_size);
  1776. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1777. for (int64_t i = p_start; i <= p_end; i++) {
  1778. #ifdef MODULE_FREETYPE_ENABLED
  1779. int32_t idx = i;
  1780. if (fd->cache[size]->face) {
  1781. if (fd->msdf) {
  1782. _ensure_glyph(fd, size, (int32_t)idx);
  1783. } else {
  1784. for (int aa = 0; aa < ((fd->antialiasing == FONT_ANTIALIASING_LCD) ? FONT_LCD_SUBPIXEL_LAYOUT_MAX : 1); aa++) {
  1785. if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_QUARTER) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_QUARTER_MAX_SIZE)) {
  1786. _ensure_glyph(fd, size, (int32_t)idx | (0 << 27) | (aa << 24));
  1787. _ensure_glyph(fd, size, (int32_t)idx | (1 << 27) | (aa << 24));
  1788. _ensure_glyph(fd, size, (int32_t)idx | (2 << 27) | (aa << 24));
  1789. _ensure_glyph(fd, size, (int32_t)idx | (3 << 27) | (aa << 24));
  1790. } else if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_HALF) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_HALF_MAX_SIZE)) {
  1791. _ensure_glyph(fd, size, (int32_t)idx | (1 << 27) | (aa << 24));
  1792. _ensure_glyph(fd, size, (int32_t)idx | (0 << 27) | (aa << 24));
  1793. } else {
  1794. _ensure_glyph(fd, size, (int32_t)idx | (aa << 24));
  1795. }
  1796. }
  1797. }
  1798. }
  1799. #endif
  1800. }
  1801. }
  1802. void TextServerFallback::_font_render_glyph(const RID &p_font_rid, const Vector2i &p_size, int64_t p_index) {
  1803. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  1804. ERR_FAIL_COND(!fd);
  1805. MutexLock lock(fd->mutex);
  1806. Vector2i size = _get_size_outline(fd, p_size);
  1807. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1808. #ifdef MODULE_FREETYPE_ENABLED
  1809. int32_t idx = p_index & 0xffffff; // Remove subpixel shifts.
  1810. if (fd->cache[size]->face) {
  1811. if (fd->msdf) {
  1812. _ensure_glyph(fd, size, (int32_t)idx);
  1813. } else {
  1814. for (int aa = 0; aa < ((fd->antialiasing == FONT_ANTIALIASING_LCD) ? FONT_LCD_SUBPIXEL_LAYOUT_MAX : 1); aa++) {
  1815. if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_QUARTER) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_QUARTER_MAX_SIZE)) {
  1816. _ensure_glyph(fd, size, (int32_t)idx | (0 << 27) | (aa << 24));
  1817. _ensure_glyph(fd, size, (int32_t)idx | (1 << 27) | (aa << 24));
  1818. _ensure_glyph(fd, size, (int32_t)idx | (2 << 27) | (aa << 24));
  1819. _ensure_glyph(fd, size, (int32_t)idx | (3 << 27) | (aa << 24));
  1820. } else if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_HALF) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_HALF_MAX_SIZE)) {
  1821. _ensure_glyph(fd, size, (int32_t)idx | (1 << 27) | (aa << 24));
  1822. _ensure_glyph(fd, size, (int32_t)idx | (0 << 27) | (aa << 24));
  1823. } else {
  1824. _ensure_glyph(fd, size, (int32_t)idx | (aa << 24));
  1825. }
  1826. }
  1827. }
  1828. }
  1829. #endif
  1830. }
  1831. void TextServerFallback::_font_draw_glyph(const RID &p_font_rid, const RID &p_canvas, int64_t p_size, const Vector2 &p_pos, int64_t p_index, const Color &p_color) const {
  1832. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  1833. ERR_FAIL_COND(!fd);
  1834. MutexLock lock(fd->mutex);
  1835. Vector2i size = _get_size(fd, p_size);
  1836. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1837. int32_t index = p_index & 0xffffff; // Remove subpixel shifts.
  1838. bool lcd_aa = false;
  1839. #ifdef MODULE_FREETYPE_ENABLED
  1840. if (!fd->msdf && fd->cache[size]->face) {
  1841. // LCD layout, bits 24, 25, 26
  1842. if (fd->antialiasing == FONT_ANTIALIASING_LCD) {
  1843. TextServer::FontLCDSubpixelLayout layout = (TextServer::FontLCDSubpixelLayout)(int)GLOBAL_GET("gui/theme/lcd_subpixel_layout");
  1844. if (layout != FONT_LCD_SUBPIXEL_LAYOUT_NONE) {
  1845. lcd_aa = true;
  1846. index = index | (layout << 24);
  1847. }
  1848. }
  1849. // Subpixel X-shift, bits 27, 28
  1850. if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_QUARTER) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_QUARTER_MAX_SIZE)) {
  1851. int xshift = (int)(Math::floor(4 * (p_pos.x + 0.125)) - 4 * Math::floor(p_pos.x + 0.125));
  1852. index = index | (xshift << 27);
  1853. } else if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_HALF) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_HALF_MAX_SIZE)) {
  1854. int xshift = (int)(Math::floor(2 * (p_pos.x + 0.25)) - 2 * Math::floor(p_pos.x + 0.25));
  1855. index = index | (xshift << 27);
  1856. }
  1857. }
  1858. #endif
  1859. if (!_ensure_glyph(fd, size, index)) {
  1860. return; // Invalid or non-graphical glyph, do not display errors, nothing to draw.
  1861. }
  1862. const FontGlyph &gl = fd->cache[size]->glyph_map[index];
  1863. if (gl.found) {
  1864. ERR_FAIL_COND(gl.texture_idx < -1 || gl.texture_idx >= fd->cache[size]->textures.size());
  1865. if (gl.texture_idx != -1) {
  1866. Color modulate = p_color;
  1867. #ifdef MODULE_FREETYPE_ENABLED
  1868. if (fd->cache[size]->face && (fd->cache[size]->textures[gl.texture_idx].format == Image::FORMAT_RGBA8) && !lcd_aa && !fd->msdf) {
  1869. modulate.r = modulate.g = modulate.b = 1.0;
  1870. }
  1871. #endif
  1872. if (RenderingServer::get_singleton() != nullptr) {
  1873. if (fd->cache[size]->textures[gl.texture_idx].dirty) {
  1874. ShelfPackTexture &tex = fd->cache[size]->textures.write[gl.texture_idx];
  1875. Ref<Image> img = Image::create_from_data(tex.texture_w, tex.texture_h, false, tex.format, tex.imgdata);
  1876. if (fd->mipmaps) {
  1877. img->generate_mipmaps();
  1878. }
  1879. if (tex.texture.is_null()) {
  1880. tex.texture = ImageTexture::create_from_image(img);
  1881. } else {
  1882. tex.texture->update(img);
  1883. }
  1884. tex.dirty = false;
  1885. }
  1886. RID texture = fd->cache[size]->textures[gl.texture_idx].texture->get_rid();
  1887. if (fd->msdf) {
  1888. Point2 cpos = p_pos;
  1889. cpos += gl.rect.position * (double)p_size / (double)fd->msdf_source_size;
  1890. Size2 csize = gl.rect.size * (double)p_size / (double)fd->msdf_source_size;
  1891. RenderingServer::get_singleton()->canvas_item_add_msdf_texture_rect_region(p_canvas, Rect2(cpos, csize), texture, gl.uv_rect, modulate, 0, fd->msdf_range);
  1892. } else {
  1893. Point2 cpos = p_pos;
  1894. double scale = _font_get_scale(p_font_rid, p_size);
  1895. if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_QUARTER) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_QUARTER_MAX_SIZE)) {
  1896. cpos.x = cpos.x + 0.125;
  1897. } else if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_HALF) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_HALF_MAX_SIZE)) {
  1898. cpos.x = cpos.x + 0.25;
  1899. }
  1900. if (scale == 1.0) {
  1901. cpos.y = Math::floor(cpos.y);
  1902. cpos.x = Math::floor(cpos.x);
  1903. }
  1904. cpos += gl.rect.position;
  1905. Size2 csize = gl.rect.size;
  1906. if (lcd_aa) {
  1907. RenderingServer::get_singleton()->canvas_item_add_lcd_texture_rect_region(p_canvas, Rect2(cpos, csize), texture, gl.uv_rect, modulate);
  1908. } else {
  1909. RenderingServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas, Rect2(cpos, csize), texture, gl.uv_rect, modulate, false, false);
  1910. }
  1911. }
  1912. }
  1913. }
  1914. }
  1915. }
  1916. void TextServerFallback::_font_draw_glyph_outline(const RID &p_font_rid, const RID &p_canvas, int64_t p_size, int64_t p_outline_size, const Vector2 &p_pos, int64_t p_index, const Color &p_color) const {
  1917. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  1918. ERR_FAIL_COND(!fd);
  1919. MutexLock lock(fd->mutex);
  1920. Vector2i size = _get_size_outline(fd, Vector2i(p_size, p_outline_size));
  1921. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1922. int32_t index = p_index & 0xffffff; // Remove subpixel shifts.
  1923. bool lcd_aa = false;
  1924. #ifdef MODULE_FREETYPE_ENABLED
  1925. if (!fd->msdf && fd->cache[size]->face) {
  1926. // LCD layout, bits 24, 25, 26
  1927. if (fd->antialiasing == FONT_ANTIALIASING_LCD) {
  1928. TextServer::FontLCDSubpixelLayout layout = (TextServer::FontLCDSubpixelLayout)(int)GLOBAL_GET("gui/theme/lcd_subpixel_layout");
  1929. if (layout != FONT_LCD_SUBPIXEL_LAYOUT_NONE) {
  1930. lcd_aa = true;
  1931. index = index | (layout << 24);
  1932. }
  1933. }
  1934. // Subpixel X-shift, bits 27, 28
  1935. if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_QUARTER) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_QUARTER_MAX_SIZE)) {
  1936. int xshift = (int)(Math::floor(4 * (p_pos.x + 0.125)) - 4 * Math::floor(p_pos.x + 0.125));
  1937. index = index | (xshift << 27);
  1938. } else if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_HALF) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_HALF_MAX_SIZE)) {
  1939. int xshift = (int)(Math::floor(2 * (p_pos.x + 0.25)) - 2 * Math::floor(p_pos.x + 0.25));
  1940. index = index | (xshift << 27);
  1941. }
  1942. }
  1943. #endif
  1944. if (!_ensure_glyph(fd, size, index)) {
  1945. return; // Invalid or non-graphical glyph, do not display errors, nothing to draw.
  1946. }
  1947. const FontGlyph &gl = fd->cache[size]->glyph_map[index];
  1948. if (gl.found) {
  1949. ERR_FAIL_COND(gl.texture_idx < -1 || gl.texture_idx >= fd->cache[size]->textures.size());
  1950. if (gl.texture_idx != -1) {
  1951. Color modulate = p_color;
  1952. #ifdef MODULE_FREETYPE_ENABLED
  1953. if (fd->cache[size]->face && (fd->cache[size]->textures[gl.texture_idx].format == Image::FORMAT_RGBA8) && !lcd_aa && !fd->msdf) {
  1954. modulate.r = modulate.g = modulate.b = 1.0;
  1955. }
  1956. #endif
  1957. if (RenderingServer::get_singleton() != nullptr) {
  1958. if (fd->cache[size]->textures[gl.texture_idx].dirty) {
  1959. ShelfPackTexture &tex = fd->cache[size]->textures.write[gl.texture_idx];
  1960. Ref<Image> img = Image::create_from_data(tex.texture_w, tex.texture_h, false, tex.format, tex.imgdata);
  1961. if (fd->mipmaps) {
  1962. img->generate_mipmaps();
  1963. }
  1964. if (tex.texture.is_null()) {
  1965. tex.texture = ImageTexture::create_from_image(img);
  1966. } else {
  1967. tex.texture->update(img);
  1968. }
  1969. tex.dirty = false;
  1970. }
  1971. RID texture = fd->cache[size]->textures[gl.texture_idx].texture->get_rid();
  1972. if (fd->msdf) {
  1973. Point2 cpos = p_pos;
  1974. cpos += gl.rect.position * (double)p_size / (double)fd->msdf_source_size;
  1975. Size2 csize = gl.rect.size * (double)p_size / (double)fd->msdf_source_size;
  1976. 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);
  1977. } else {
  1978. Point2 cpos = p_pos;
  1979. double scale = _font_get_scale(p_font_rid, p_size);
  1980. if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_QUARTER) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_QUARTER_MAX_SIZE)) {
  1981. cpos.x = cpos.x + 0.125;
  1982. } else if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_HALF) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_HALF_MAX_SIZE)) {
  1983. cpos.x = cpos.x + 0.25;
  1984. }
  1985. if (scale == 1.0) {
  1986. cpos.y = Math::floor(cpos.y);
  1987. cpos.x = Math::floor(cpos.x);
  1988. }
  1989. cpos += gl.rect.position;
  1990. Size2 csize = gl.rect.size;
  1991. if (lcd_aa) {
  1992. RenderingServer::get_singleton()->canvas_item_add_lcd_texture_rect_region(p_canvas, Rect2(cpos, csize), texture, gl.uv_rect, modulate);
  1993. } else {
  1994. RenderingServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas, Rect2(cpos, csize), texture, gl.uv_rect, modulate, false, false);
  1995. }
  1996. }
  1997. }
  1998. }
  1999. }
  2000. }
  2001. bool TextServerFallback::_font_is_language_supported(const RID &p_font_rid, const String &p_language) const {
  2002. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  2003. ERR_FAIL_COND_V(!fd, false);
  2004. MutexLock lock(fd->mutex);
  2005. if (fd->language_support_overrides.has(p_language)) {
  2006. return fd->language_support_overrides[p_language];
  2007. } else {
  2008. return true;
  2009. }
  2010. }
  2011. void TextServerFallback::_font_set_language_support_override(const RID &p_font_rid, const String &p_language, bool p_supported) {
  2012. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  2013. ERR_FAIL_COND(!fd);
  2014. MutexLock lock(fd->mutex);
  2015. fd->language_support_overrides[p_language] = p_supported;
  2016. }
  2017. bool TextServerFallback::_font_get_language_support_override(const RID &p_font_rid, const String &p_language) {
  2018. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  2019. ERR_FAIL_COND_V(!fd, false);
  2020. MutexLock lock(fd->mutex);
  2021. return fd->language_support_overrides[p_language];
  2022. }
  2023. void TextServerFallback::_font_remove_language_support_override(const RID &p_font_rid, const String &p_language) {
  2024. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  2025. ERR_FAIL_COND(!fd);
  2026. MutexLock lock(fd->mutex);
  2027. fd->language_support_overrides.erase(p_language);
  2028. }
  2029. PackedStringArray TextServerFallback::_font_get_language_support_overrides(const RID &p_font_rid) {
  2030. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  2031. ERR_FAIL_COND_V(!fd, PackedStringArray());
  2032. MutexLock lock(fd->mutex);
  2033. PackedStringArray out;
  2034. for (const KeyValue<String, bool> &E : fd->language_support_overrides) {
  2035. out.push_back(E.key);
  2036. }
  2037. return out;
  2038. }
  2039. bool TextServerFallback::_font_is_script_supported(const RID &p_font_rid, const String &p_script) const {
  2040. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  2041. ERR_FAIL_COND_V(!fd, false);
  2042. MutexLock lock(fd->mutex);
  2043. if (fd->script_support_overrides.has(p_script)) {
  2044. return fd->script_support_overrides[p_script];
  2045. } else {
  2046. return true;
  2047. }
  2048. }
  2049. void TextServerFallback::_font_set_script_support_override(const RID &p_font_rid, const String &p_script, bool p_supported) {
  2050. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  2051. ERR_FAIL_COND(!fd);
  2052. MutexLock lock(fd->mutex);
  2053. fd->script_support_overrides[p_script] = p_supported;
  2054. }
  2055. bool TextServerFallback::_font_get_script_support_override(const RID &p_font_rid, const String &p_script) {
  2056. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  2057. ERR_FAIL_COND_V(!fd, false);
  2058. MutexLock lock(fd->mutex);
  2059. return fd->script_support_overrides[p_script];
  2060. }
  2061. void TextServerFallback::_font_remove_script_support_override(const RID &p_font_rid, const String &p_script) {
  2062. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  2063. ERR_FAIL_COND(!fd);
  2064. MutexLock lock(fd->mutex);
  2065. Vector2i size = _get_size(fd, 16);
  2066. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  2067. fd->script_support_overrides.erase(p_script);
  2068. }
  2069. PackedStringArray TextServerFallback::_font_get_script_support_overrides(const RID &p_font_rid) {
  2070. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  2071. ERR_FAIL_COND_V(!fd, PackedStringArray());
  2072. MutexLock lock(fd->mutex);
  2073. PackedStringArray out;
  2074. for (const KeyValue<String, bool> &E : fd->script_support_overrides) {
  2075. out.push_back(E.key);
  2076. }
  2077. return out;
  2078. }
  2079. void TextServerFallback::_font_set_opentype_feature_overrides(const RID &p_font_rid, const Dictionary &p_overrides) {
  2080. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  2081. ERR_FAIL_COND(!fd);
  2082. MutexLock lock(fd->mutex);
  2083. Vector2i size = _get_size(fd, 16);
  2084. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  2085. fd->feature_overrides = p_overrides;
  2086. }
  2087. Dictionary TextServerFallback::_font_get_opentype_feature_overrides(const RID &p_font_rid) const {
  2088. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  2089. ERR_FAIL_COND_V(!fd, Dictionary());
  2090. MutexLock lock(fd->mutex);
  2091. return fd->feature_overrides;
  2092. }
  2093. Dictionary TextServerFallback::_font_supported_feature_list(const RID &p_font_rid) const {
  2094. return Dictionary();
  2095. }
  2096. Dictionary TextServerFallback::_font_supported_variation_list(const RID &p_font_rid) const {
  2097. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  2098. ERR_FAIL_COND_V(!fd, Dictionary());
  2099. MutexLock lock(fd->mutex);
  2100. Vector2i size = _get_size(fd, 16);
  2101. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), Dictionary());
  2102. return fd->supported_varaitions;
  2103. }
  2104. double TextServerFallback::_font_get_global_oversampling() const {
  2105. return oversampling;
  2106. }
  2107. void TextServerFallback::_font_set_global_oversampling(double p_oversampling) {
  2108. _THREAD_SAFE_METHOD_
  2109. if (oversampling != p_oversampling) {
  2110. oversampling = p_oversampling;
  2111. List<RID> fonts;
  2112. font_owner.get_owned_list(&fonts);
  2113. bool font_cleared = false;
  2114. for (const RID &E : fonts) {
  2115. if (!_font_is_multichannel_signed_distance_field(E) && _font_get_oversampling(E) <= 0) {
  2116. _font_clear_size_cache(E);
  2117. font_cleared = true;
  2118. }
  2119. }
  2120. if (font_cleared) {
  2121. List<RID> text_bufs;
  2122. shaped_owner.get_owned_list(&text_bufs);
  2123. for (const RID &E : text_bufs) {
  2124. invalidate(shaped_owner.get_or_null(E));
  2125. }
  2126. }
  2127. }
  2128. }
  2129. /*************************************************************************/
  2130. /* Shaped text buffer interface */
  2131. /*************************************************************************/
  2132. void TextServerFallback::invalidate(ShapedTextDataFallback *p_shaped) {
  2133. p_shaped->valid = false;
  2134. p_shaped->sort_valid = false;
  2135. p_shaped->line_breaks_valid = false;
  2136. p_shaped->justification_ops_valid = false;
  2137. p_shaped->ascent = 0.0;
  2138. p_shaped->descent = 0.0;
  2139. p_shaped->width = 0.0;
  2140. p_shaped->upos = 0.0;
  2141. p_shaped->uthk = 0.0;
  2142. p_shaped->glyphs.clear();
  2143. p_shaped->glyphs_logical.clear();
  2144. }
  2145. void TextServerFallback::full_copy(ShapedTextDataFallback *p_shaped) {
  2146. ShapedTextDataFallback *parent = shaped_owner.get_or_null(p_shaped->parent);
  2147. for (const KeyValue<Variant, ShapedTextDataFallback::EmbeddedObject> &E : parent->objects) {
  2148. if (E.value.pos >= p_shaped->start && E.value.pos < p_shaped->end) {
  2149. p_shaped->objects[E.key] = E.value;
  2150. }
  2151. }
  2152. for (int k = 0; k < parent->spans.size(); k++) {
  2153. ShapedTextDataFallback::Span span = parent->spans[k];
  2154. if (span.start >= p_shaped->end || span.end <= p_shaped->start) {
  2155. continue;
  2156. }
  2157. span.start = MAX(p_shaped->start, span.start);
  2158. span.end = MIN(p_shaped->end, span.end);
  2159. p_shaped->spans.push_back(span);
  2160. }
  2161. p_shaped->parent = RID();
  2162. }
  2163. RID TextServerFallback::_create_shaped_text(TextServer::Direction p_direction, TextServer::Orientation p_orientation) {
  2164. _THREAD_SAFE_METHOD_
  2165. ShapedTextDataFallback *sd = memnew(ShapedTextDataFallback);
  2166. sd->direction = p_direction;
  2167. sd->orientation = p_orientation;
  2168. return shaped_owner.make_rid(sd);
  2169. }
  2170. void TextServerFallback::_shaped_text_clear(const RID &p_shaped) {
  2171. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  2172. ERR_FAIL_COND(!sd);
  2173. MutexLock lock(sd->mutex);
  2174. sd->parent = RID();
  2175. sd->start = 0;
  2176. sd->end = 0;
  2177. sd->text = String();
  2178. sd->spans.clear();
  2179. sd->objects.clear();
  2180. invalidate(sd);
  2181. }
  2182. void TextServerFallback::_shaped_text_set_direction(const RID &p_shaped, TextServer::Direction p_direction) {
  2183. if (p_direction == DIRECTION_RTL) {
  2184. ERR_PRINT_ONCE("Right-to-left layout is not supported by this text server.");
  2185. }
  2186. }
  2187. TextServer::Direction TextServerFallback::_shaped_text_get_direction(const RID &p_shaped) const {
  2188. return TextServer::DIRECTION_LTR;
  2189. }
  2190. TextServer::Direction TextServerFallback::_shaped_text_get_inferred_direction(const RID &p_shaped) const {
  2191. return TextServer::DIRECTION_LTR;
  2192. }
  2193. void TextServerFallback::_shaped_text_set_custom_punctuation(const RID &p_shaped, const String &p_punct) {
  2194. _THREAD_SAFE_METHOD_
  2195. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  2196. ERR_FAIL_COND(!sd);
  2197. if (sd->custom_punct != p_punct) {
  2198. if (sd->parent != RID()) {
  2199. full_copy(sd);
  2200. }
  2201. sd->custom_punct = p_punct;
  2202. invalidate(sd);
  2203. }
  2204. }
  2205. String TextServerFallback::_shaped_text_get_custom_punctuation(const RID &p_shaped) const {
  2206. _THREAD_SAFE_METHOD_
  2207. const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  2208. ERR_FAIL_COND_V(!sd, String());
  2209. return sd->custom_punct;
  2210. }
  2211. void TextServerFallback::_shaped_text_set_orientation(const RID &p_shaped, TextServer::Orientation p_orientation) {
  2212. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  2213. ERR_FAIL_COND(!sd);
  2214. MutexLock lock(sd->mutex);
  2215. if (sd->orientation != p_orientation) {
  2216. if (sd->parent != RID()) {
  2217. full_copy(sd);
  2218. }
  2219. sd->orientation = p_orientation;
  2220. invalidate(sd);
  2221. }
  2222. }
  2223. void TextServerFallback::_shaped_text_set_bidi_override(const RID &p_shaped, const Array &p_override) {
  2224. // No BiDi support, ignore.
  2225. }
  2226. TextServer::Orientation TextServerFallback::_shaped_text_get_orientation(const RID &p_shaped) const {
  2227. const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  2228. ERR_FAIL_COND_V(!sd, TextServer::ORIENTATION_HORIZONTAL);
  2229. MutexLock lock(sd->mutex);
  2230. return sd->orientation;
  2231. }
  2232. void TextServerFallback::_shaped_text_set_preserve_invalid(const RID &p_shaped, bool p_enabled) {
  2233. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  2234. MutexLock lock(sd->mutex);
  2235. ERR_FAIL_COND(!sd);
  2236. if (sd->preserve_invalid != p_enabled) {
  2237. if (sd->parent != RID()) {
  2238. full_copy(sd);
  2239. }
  2240. sd->preserve_invalid = p_enabled;
  2241. invalidate(sd);
  2242. }
  2243. }
  2244. bool TextServerFallback::_shaped_text_get_preserve_invalid(const RID &p_shaped) const {
  2245. const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  2246. ERR_FAIL_COND_V(!sd, false);
  2247. MutexLock lock(sd->mutex);
  2248. return sd->preserve_invalid;
  2249. }
  2250. void TextServerFallback::_shaped_text_set_preserve_control(const RID &p_shaped, bool p_enabled) {
  2251. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  2252. ERR_FAIL_COND(!sd);
  2253. MutexLock lock(sd->mutex);
  2254. if (sd->preserve_control != p_enabled) {
  2255. if (sd->parent != RID()) {
  2256. full_copy(sd);
  2257. }
  2258. sd->preserve_control = p_enabled;
  2259. invalidate(sd);
  2260. }
  2261. }
  2262. bool TextServerFallback::_shaped_text_get_preserve_control(const RID &p_shaped) const {
  2263. const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  2264. ERR_FAIL_COND_V(!sd, false);
  2265. MutexLock lock(sd->mutex);
  2266. return sd->preserve_control;
  2267. }
  2268. void TextServerFallback::_shaped_text_set_spacing(const RID &p_shaped, SpacingType p_spacing, int64_t p_value) {
  2269. ERR_FAIL_INDEX((int)p_spacing, 4);
  2270. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  2271. ERR_FAIL_COND(!sd);
  2272. MutexLock lock(sd->mutex);
  2273. if (sd->extra_spacing[p_spacing] != p_value) {
  2274. if (sd->parent != RID()) {
  2275. full_copy(sd);
  2276. }
  2277. sd->extra_spacing[p_spacing] = p_value;
  2278. invalidate(sd);
  2279. }
  2280. }
  2281. int64_t TextServerFallback::_shaped_text_get_spacing(const RID &p_shaped, SpacingType p_spacing) const {
  2282. ERR_FAIL_INDEX_V((int)p_spacing, 4, 0);
  2283. const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  2284. ERR_FAIL_COND_V(!sd, 0);
  2285. MutexLock lock(sd->mutex);
  2286. return sd->extra_spacing[p_spacing];
  2287. }
  2288. int64_t TextServerFallback::_shaped_get_span_count(const RID &p_shaped) const {
  2289. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  2290. ERR_FAIL_COND_V(!sd, 0);
  2291. return sd->spans.size();
  2292. }
  2293. Variant TextServerFallback::_shaped_get_span_meta(const RID &p_shaped, int64_t p_index) const {
  2294. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  2295. ERR_FAIL_COND_V(!sd, Variant());
  2296. ERR_FAIL_INDEX_V(p_index, sd->spans.size(), Variant());
  2297. return sd->spans[p_index].meta;
  2298. }
  2299. void TextServerFallback::_shaped_set_span_update_font(const RID &p_shaped, int64_t p_index, const TypedArray<RID> &p_fonts, int64_t p_size, const Dictionary &p_opentype_features) {
  2300. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  2301. ERR_FAIL_COND(!sd);
  2302. ERR_FAIL_INDEX(p_index, sd->spans.size());
  2303. ShapedTextDataFallback::Span &span = sd->spans.ptrw()[p_index];
  2304. span.fonts.clear();
  2305. // Pre-sort fonts, push fonts with the language support first.
  2306. Array fonts_no_match;
  2307. int font_count = p_fonts.size();
  2308. for (int i = 0; i < font_count; i++) {
  2309. if (_font_is_language_supported(p_fonts[i], span.language)) {
  2310. span.fonts.push_back(p_fonts[i]);
  2311. } else {
  2312. fonts_no_match.push_back(p_fonts[i]);
  2313. }
  2314. }
  2315. span.fonts.append_array(fonts_no_match);
  2316. span.font_size = p_size;
  2317. span.features = p_opentype_features;
  2318. sd->valid = false;
  2319. }
  2320. bool TextServerFallback::_shaped_text_add_string(const RID &p_shaped, const String &p_text, const TypedArray<RID> &p_fonts, int64_t p_size, const Dictionary &p_opentype_features, const String &p_language, const Variant &p_meta) {
  2321. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  2322. ERR_FAIL_COND_V(!sd, false);
  2323. MutexLock lock(sd->mutex);
  2324. ERR_FAIL_COND_V(p_size <= 0, false);
  2325. for (int i = 0; i < p_fonts.size(); i++) {
  2326. ERR_FAIL_COND_V(!font_owner.get_or_null(p_fonts[i]), false);
  2327. }
  2328. if (p_text.is_empty()) {
  2329. return true;
  2330. }
  2331. if (sd->parent != RID()) {
  2332. full_copy(sd);
  2333. }
  2334. ShapedTextDataFallback::Span span;
  2335. span.start = sd->text.length();
  2336. span.end = span.start + p_text.length();
  2337. // Pre-sort fonts, push fonts with the language support first.
  2338. Array fonts_no_match;
  2339. int font_count = p_fonts.size();
  2340. if (font_count > 0) {
  2341. span.fonts.push_back(p_fonts[0]);
  2342. }
  2343. for (int i = 1; i < font_count; i++) {
  2344. if (_font_is_language_supported(p_fonts[i], p_language)) {
  2345. span.fonts.push_back(p_fonts[i]);
  2346. } else {
  2347. fonts_no_match.push_back(p_fonts[i]);
  2348. }
  2349. }
  2350. span.fonts.append_array(fonts_no_match);
  2351. ERR_FAIL_COND_V(span.fonts.is_empty(), false);
  2352. span.font_size = p_size;
  2353. span.language = p_language;
  2354. span.meta = p_meta;
  2355. sd->spans.push_back(span);
  2356. sd->text = sd->text + p_text;
  2357. sd->end += p_text.length();
  2358. invalidate(sd);
  2359. return true;
  2360. }
  2361. bool TextServerFallback::_shaped_text_add_object(const RID &p_shaped, const Variant &p_key, const Size2 &p_size, InlineAlignment p_inline_align, int64_t p_length) {
  2362. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  2363. ERR_FAIL_COND_V(!sd, false);
  2364. MutexLock lock(sd->mutex);
  2365. ERR_FAIL_COND_V(p_key == Variant(), false);
  2366. ERR_FAIL_COND_V(sd->objects.has(p_key), false);
  2367. if (sd->parent != RID()) {
  2368. full_copy(sd);
  2369. }
  2370. ShapedTextDataFallback::Span span;
  2371. span.start = sd->start + sd->text.length();
  2372. span.end = span.start + p_length;
  2373. span.embedded_key = p_key;
  2374. ShapedTextDataFallback::EmbeddedObject obj;
  2375. obj.inline_align = p_inline_align;
  2376. obj.rect.size = p_size;
  2377. obj.pos = span.start;
  2378. sd->spans.push_back(span);
  2379. sd->text = sd->text + String::chr(0xfffc).repeat(p_length);
  2380. sd->end += p_length;
  2381. sd->objects[p_key] = obj;
  2382. invalidate(sd);
  2383. return true;
  2384. }
  2385. bool TextServerFallback::_shaped_text_resize_object(const RID &p_shaped, const Variant &p_key, const Size2 &p_size, InlineAlignment p_inline_align) {
  2386. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  2387. ERR_FAIL_COND_V(!sd, false);
  2388. MutexLock lock(sd->mutex);
  2389. ERR_FAIL_COND_V(!sd->objects.has(p_key), false);
  2390. sd->objects[p_key].rect.size = p_size;
  2391. sd->objects[p_key].inline_align = p_inline_align;
  2392. if (sd->valid) {
  2393. // Recalc string metrics.
  2394. sd->ascent = 0;
  2395. sd->descent = 0;
  2396. sd->width = 0;
  2397. sd->upos = 0;
  2398. sd->uthk = 0;
  2399. int sd_size = sd->glyphs.size();
  2400. for (int i = 0; i < sd_size; i++) {
  2401. Glyph gl = sd->glyphs[i];
  2402. Variant key;
  2403. if (gl.count == 1) {
  2404. for (const KeyValue<Variant, ShapedTextDataFallback::EmbeddedObject> &E : sd->objects) {
  2405. if (E.value.pos == gl.start) {
  2406. key = E.key;
  2407. break;
  2408. }
  2409. }
  2410. }
  2411. if (key != Variant()) {
  2412. if (sd->orientation == ORIENTATION_HORIZONTAL) {
  2413. sd->objects[key].rect.position.x = sd->width;
  2414. sd->width += sd->objects[key].rect.size.x;
  2415. sd->glyphs.write[i].advance = sd->objects[key].rect.size.x;
  2416. } else {
  2417. sd->objects[key].rect.position.y = sd->width;
  2418. sd->width += sd->objects[key].rect.size.y;
  2419. sd->glyphs.write[i].advance = sd->objects[key].rect.size.y;
  2420. }
  2421. } else {
  2422. if (gl.font_rid.is_valid()) {
  2423. if (sd->orientation == ORIENTATION_HORIZONTAL) {
  2424. sd->ascent = MAX(sd->ascent, _font_get_ascent(gl.font_rid, gl.font_size));
  2425. sd->descent = MAX(sd->descent, _font_get_descent(gl.font_rid, gl.font_size));
  2426. } else {
  2427. sd->ascent = MAX(sd->ascent, Math::round(_font_get_glyph_advance(gl.font_rid, gl.font_size, gl.index).x * 0.5));
  2428. sd->descent = MAX(sd->descent, Math::round(_font_get_glyph_advance(gl.font_rid, gl.font_size, gl.index).x * 0.5));
  2429. }
  2430. sd->upos = MAX(sd->upos, _font_get_underline_position(gl.font_rid, gl.font_size));
  2431. sd->uthk = MAX(sd->uthk, _font_get_underline_thickness(gl.font_rid, gl.font_size));
  2432. } else if (sd->preserve_invalid || (sd->preserve_control && is_control(gl.index))) {
  2433. // Glyph not found, replace with hex code box.
  2434. if (sd->orientation == ORIENTATION_HORIZONTAL) {
  2435. sd->ascent = MAX(sd->ascent, get_hex_code_box_size(gl.font_size, gl.index).y);
  2436. } else {
  2437. sd->ascent = MAX(sd->ascent, Math::round(get_hex_code_box_size(gl.font_size, gl.index).x * 0.5));
  2438. sd->descent = MAX(sd->descent, Math::round(get_hex_code_box_size(gl.font_size, gl.index).x * 0.5));
  2439. }
  2440. }
  2441. sd->width += gl.advance * gl.repeat;
  2442. }
  2443. }
  2444. _realign(sd);
  2445. }
  2446. return true;
  2447. }
  2448. void TextServerFallback::_realign(ShapedTextDataFallback *p_sd) const {
  2449. // Align embedded objects to baseline.
  2450. double full_ascent = p_sd->ascent;
  2451. double full_descent = p_sd->descent;
  2452. for (KeyValue<Variant, ShapedTextDataFallback::EmbeddedObject> &E : p_sd->objects) {
  2453. if ((E.value.pos >= p_sd->start) && (E.value.pos < p_sd->end)) {
  2454. if (p_sd->orientation == ORIENTATION_HORIZONTAL) {
  2455. switch (E.value.inline_align & INLINE_ALIGNMENT_TEXT_MASK) {
  2456. case INLINE_ALIGNMENT_TO_TOP: {
  2457. E.value.rect.position.y = -p_sd->ascent;
  2458. } break;
  2459. case INLINE_ALIGNMENT_TO_CENTER: {
  2460. E.value.rect.position.y = (-p_sd->ascent + p_sd->descent) / 2;
  2461. } break;
  2462. case INLINE_ALIGNMENT_TO_BASELINE: {
  2463. E.value.rect.position.y = 0;
  2464. } break;
  2465. case INLINE_ALIGNMENT_TO_BOTTOM: {
  2466. E.value.rect.position.y = p_sd->descent;
  2467. } break;
  2468. }
  2469. switch (E.value.inline_align & INLINE_ALIGNMENT_IMAGE_MASK) {
  2470. case INLINE_ALIGNMENT_BOTTOM_TO: {
  2471. E.value.rect.position.y -= E.value.rect.size.y;
  2472. } break;
  2473. case INLINE_ALIGNMENT_CENTER_TO: {
  2474. E.value.rect.position.y -= E.value.rect.size.y / 2;
  2475. } break;
  2476. case INLINE_ALIGNMENT_TOP_TO: {
  2477. // NOP
  2478. } break;
  2479. }
  2480. full_ascent = MAX(full_ascent, -E.value.rect.position.y);
  2481. full_descent = MAX(full_descent, E.value.rect.position.y + E.value.rect.size.y);
  2482. } else {
  2483. switch (E.value.inline_align & INLINE_ALIGNMENT_TEXT_MASK) {
  2484. case INLINE_ALIGNMENT_TO_TOP: {
  2485. E.value.rect.position.x = -p_sd->ascent;
  2486. } break;
  2487. case INLINE_ALIGNMENT_TO_CENTER: {
  2488. E.value.rect.position.x = (-p_sd->ascent + p_sd->descent) / 2;
  2489. } break;
  2490. case INLINE_ALIGNMENT_TO_BASELINE: {
  2491. E.value.rect.position.x = 0;
  2492. } break;
  2493. case INLINE_ALIGNMENT_TO_BOTTOM: {
  2494. E.value.rect.position.x = p_sd->descent;
  2495. } break;
  2496. }
  2497. switch (E.value.inline_align & INLINE_ALIGNMENT_IMAGE_MASK) {
  2498. case INLINE_ALIGNMENT_BOTTOM_TO: {
  2499. E.value.rect.position.x -= E.value.rect.size.x;
  2500. } break;
  2501. case INLINE_ALIGNMENT_CENTER_TO: {
  2502. E.value.rect.position.x -= E.value.rect.size.x / 2;
  2503. } break;
  2504. case INLINE_ALIGNMENT_TOP_TO: {
  2505. // NOP
  2506. } break;
  2507. }
  2508. full_ascent = MAX(full_ascent, -E.value.rect.position.x);
  2509. full_descent = MAX(full_descent, E.value.rect.position.x + E.value.rect.size.x);
  2510. }
  2511. }
  2512. }
  2513. p_sd->ascent = full_ascent;
  2514. p_sd->descent = full_descent;
  2515. }
  2516. RID TextServerFallback::_shaped_text_substr(const RID &p_shaped, int64_t p_start, int64_t p_length) const {
  2517. _THREAD_SAFE_METHOD_
  2518. const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  2519. ERR_FAIL_COND_V(!sd, RID());
  2520. MutexLock lock(sd->mutex);
  2521. if (sd->parent != RID()) {
  2522. return _shaped_text_substr(sd->parent, p_start, p_length);
  2523. }
  2524. if (!sd->valid) {
  2525. const_cast<TextServerFallback *>(this)->_shaped_text_shape(p_shaped);
  2526. }
  2527. ERR_FAIL_COND_V(p_start < 0 || p_length < 0, RID());
  2528. ERR_FAIL_COND_V(sd->start > p_start || sd->end < p_start, RID());
  2529. ERR_FAIL_COND_V(sd->end < p_start + p_length, RID());
  2530. ShapedTextDataFallback *new_sd = memnew(ShapedTextDataFallback);
  2531. new_sd->parent = p_shaped;
  2532. new_sd->start = p_start;
  2533. new_sd->end = p_start + p_length;
  2534. new_sd->orientation = sd->orientation;
  2535. new_sd->direction = sd->direction;
  2536. new_sd->custom_punct = sd->custom_punct;
  2537. new_sd->para_direction = sd->para_direction;
  2538. new_sd->line_breaks_valid = sd->line_breaks_valid;
  2539. new_sd->justification_ops_valid = sd->justification_ops_valid;
  2540. new_sd->sort_valid = false;
  2541. new_sd->upos = sd->upos;
  2542. new_sd->uthk = sd->uthk;
  2543. for (int i = 0; i < TextServer::SPACING_MAX; i++) {
  2544. new_sd->extra_spacing[i] = sd->extra_spacing[i];
  2545. }
  2546. if (p_length > 0) {
  2547. new_sd->text = sd->text.substr(p_start - sd->start, p_length);
  2548. int sd_size = sd->glyphs.size();
  2549. const Glyph *sd_glyphs = sd->glyphs.ptr();
  2550. for (int i = 0; i < sd_size; i++) {
  2551. if ((sd_glyphs[i].start >= new_sd->start) && (sd_glyphs[i].end <= new_sd->end)) {
  2552. Glyph gl = sd_glyphs[i];
  2553. Variant key;
  2554. bool find_embedded = false;
  2555. if (gl.count == 1) {
  2556. for (const KeyValue<Variant, ShapedTextDataFallback::EmbeddedObject> &E : sd->objects) {
  2557. if (E.value.pos == gl.start) {
  2558. find_embedded = true;
  2559. key = E.key;
  2560. new_sd->objects[key] = E.value;
  2561. break;
  2562. }
  2563. }
  2564. }
  2565. if (find_embedded) {
  2566. if (new_sd->orientation == ORIENTATION_HORIZONTAL) {
  2567. new_sd->objects[key].rect.position.x = new_sd->width;
  2568. new_sd->width += new_sd->objects[key].rect.size.x;
  2569. } else {
  2570. new_sd->objects[key].rect.position.y = new_sd->width;
  2571. new_sd->width += new_sd->objects[key].rect.size.y;
  2572. }
  2573. } else {
  2574. if (gl.font_rid.is_valid()) {
  2575. if (new_sd->orientation == ORIENTATION_HORIZONTAL) {
  2576. new_sd->ascent = MAX(new_sd->ascent, _font_get_ascent(gl.font_rid, gl.font_size));
  2577. new_sd->descent = MAX(new_sd->descent, _font_get_descent(gl.font_rid, gl.font_size));
  2578. } else {
  2579. new_sd->ascent = MAX(new_sd->ascent, Math::round(_font_get_glyph_advance(gl.font_rid, gl.font_size, gl.index).x * 0.5));
  2580. new_sd->descent = MAX(new_sd->descent, Math::round(_font_get_glyph_advance(gl.font_rid, gl.font_size, gl.index).x * 0.5));
  2581. }
  2582. } else if (new_sd->preserve_invalid || (new_sd->preserve_control && is_control(gl.index))) {
  2583. // Glyph not found, replace with hex code box.
  2584. if (new_sd->orientation == ORIENTATION_HORIZONTAL) {
  2585. new_sd->ascent = MAX(new_sd->ascent, get_hex_code_box_size(gl.font_size, gl.index).y);
  2586. } else {
  2587. new_sd->ascent = MAX(new_sd->ascent, Math::round(get_hex_code_box_size(gl.font_size, gl.index).x * 0.5));
  2588. new_sd->descent = MAX(new_sd->descent, Math::round(get_hex_code_box_size(gl.font_size, gl.index).x * 0.5));
  2589. }
  2590. }
  2591. new_sd->width += gl.advance * gl.repeat;
  2592. }
  2593. new_sd->glyphs.push_back(gl);
  2594. }
  2595. }
  2596. _realign(new_sd);
  2597. }
  2598. new_sd->valid = true;
  2599. return shaped_owner.make_rid(new_sd);
  2600. }
  2601. RID TextServerFallback::_shaped_text_get_parent(const RID &p_shaped) const {
  2602. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  2603. ERR_FAIL_COND_V(!sd, RID());
  2604. MutexLock lock(sd->mutex);
  2605. return sd->parent;
  2606. }
  2607. double TextServerFallback::_shaped_text_fit_to_width(const RID &p_shaped, double p_width, BitField<JustificationFlag> p_jst_flags) {
  2608. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  2609. ERR_FAIL_COND_V(!sd, 0.0);
  2610. MutexLock lock(sd->mutex);
  2611. if (!sd->valid) {
  2612. const_cast<TextServerFallback *>(this)->_shaped_text_shape(p_shaped);
  2613. }
  2614. if (!sd->justification_ops_valid) {
  2615. const_cast<TextServerFallback *>(this)->_shaped_text_update_justification_ops(p_shaped);
  2616. }
  2617. int start_pos = 0;
  2618. int end_pos = sd->glyphs.size() - 1;
  2619. if (p_jst_flags.has_flag(JUSTIFICATION_AFTER_LAST_TAB)) {
  2620. int start, end, delta;
  2621. if (sd->para_direction == DIRECTION_LTR) {
  2622. start = sd->glyphs.size() - 1;
  2623. end = -1;
  2624. delta = -1;
  2625. } else {
  2626. start = 0;
  2627. end = sd->glyphs.size();
  2628. delta = +1;
  2629. }
  2630. for (int i = start; i != end; i += delta) {
  2631. if ((sd->glyphs[i].flags & GRAPHEME_IS_TAB) == GRAPHEME_IS_TAB) {
  2632. if (sd->para_direction == DIRECTION_LTR) {
  2633. start_pos = i;
  2634. break;
  2635. } else {
  2636. end_pos = i;
  2637. break;
  2638. }
  2639. }
  2640. }
  2641. }
  2642. double justification_width;
  2643. if (p_jst_flags.has_flag(JUSTIFICATION_CONSTRAIN_ELLIPSIS)) {
  2644. if (sd->overrun_trim_data.trim_pos >= 0) {
  2645. end_pos = sd->overrun_trim_data.trim_pos;
  2646. justification_width = sd->width_trimmed;
  2647. } else {
  2648. return Math::ceil(sd->width);
  2649. }
  2650. } else {
  2651. justification_width = sd->width;
  2652. }
  2653. if (p_jst_flags.has_flag(JUSTIFICATION_TRIM_EDGE_SPACES)) {
  2654. // Trim spaces.
  2655. 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)) {
  2656. justification_width -= sd->glyphs[start_pos].advance * sd->glyphs[start_pos].repeat;
  2657. sd->glyphs.write[start_pos].advance = 0;
  2658. start_pos += sd->glyphs[start_pos].count;
  2659. }
  2660. 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)) {
  2661. justification_width -= sd->glyphs[end_pos].advance * sd->glyphs[end_pos].repeat;
  2662. sd->glyphs.write[end_pos].advance = 0;
  2663. end_pos -= sd->glyphs[end_pos].count;
  2664. }
  2665. } else {
  2666. // Skip breaks, but do not reset size.
  2667. while ((start_pos < end_pos) && ((sd->glyphs[start_pos].flags & GRAPHEME_IS_BREAK_HARD) == GRAPHEME_IS_BREAK_HARD)) {
  2668. start_pos += sd->glyphs[start_pos].count;
  2669. }
  2670. while ((start_pos < end_pos) && ((sd->glyphs[end_pos].flags & GRAPHEME_IS_BREAK_HARD) == GRAPHEME_IS_BREAK_HARD)) {
  2671. end_pos -= sd->glyphs[end_pos].count;
  2672. }
  2673. }
  2674. int space_count = 0;
  2675. for (int i = start_pos; i <= end_pos; i++) {
  2676. const Glyph &gl = sd->glyphs[i];
  2677. if (gl.count > 0) {
  2678. if ((gl.flags & GRAPHEME_IS_SPACE) == GRAPHEME_IS_SPACE) {
  2679. space_count++;
  2680. }
  2681. }
  2682. }
  2683. if ((space_count > 0) && p_jst_flags.has_flag(JUSTIFICATION_WORD_BOUND)) {
  2684. double delta_width_per_space = (p_width - justification_width) / space_count;
  2685. for (int i = start_pos; i <= end_pos; i++) {
  2686. Glyph &gl = sd->glyphs.write[i];
  2687. if (gl.count > 0) {
  2688. if ((gl.flags & GRAPHEME_IS_SPACE) == GRAPHEME_IS_SPACE) {
  2689. double old_adv = gl.advance;
  2690. gl.advance = MAX(gl.advance + delta_width_per_space, Math::round(0.1 * gl.font_size));
  2691. justification_width += (gl.advance - old_adv);
  2692. }
  2693. }
  2694. }
  2695. }
  2696. if (Math::floor(p_width) < Math::floor(justification_width)) {
  2697. sd->fit_width_minimum_reached = true;
  2698. }
  2699. if (!p_jst_flags.has_flag(JUSTIFICATION_CONSTRAIN_ELLIPSIS)) {
  2700. sd->width = justification_width;
  2701. }
  2702. return Math::ceil(justification_width);
  2703. }
  2704. double TextServerFallback::_shaped_text_tab_align(const RID &p_shaped, const PackedFloat32Array &p_tab_stops) {
  2705. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  2706. ERR_FAIL_COND_V(!sd, 0.0);
  2707. MutexLock lock(sd->mutex);
  2708. if (!sd->valid) {
  2709. const_cast<TextServerFallback *>(this)->_shaped_text_shape(p_shaped);
  2710. }
  2711. if (!sd->line_breaks_valid) {
  2712. const_cast<TextServerFallback *>(this)->_shaped_text_update_breaks(p_shaped);
  2713. }
  2714. for (int i = 0; i < p_tab_stops.size(); i++) {
  2715. if (p_tab_stops[i] <= 0) {
  2716. return 0.0;
  2717. }
  2718. }
  2719. int tab_index = 0;
  2720. double off = 0.0;
  2721. int start, end, delta;
  2722. if (sd->para_direction == DIRECTION_LTR) {
  2723. start = 0;
  2724. end = sd->glyphs.size();
  2725. delta = +1;
  2726. } else {
  2727. start = sd->glyphs.size() - 1;
  2728. end = -1;
  2729. delta = -1;
  2730. }
  2731. Glyph *gl = sd->glyphs.ptrw();
  2732. for (int i = start; i != end; i += delta) {
  2733. if ((gl[i].flags & GRAPHEME_IS_TAB) == GRAPHEME_IS_TAB) {
  2734. double tab_off = 0.0;
  2735. while (tab_off <= off) {
  2736. tab_off += p_tab_stops[tab_index];
  2737. tab_index++;
  2738. if (tab_index >= p_tab_stops.size()) {
  2739. tab_index = 0;
  2740. }
  2741. }
  2742. double old_adv = gl[i].advance;
  2743. gl[i].advance = tab_off - off;
  2744. sd->width += gl[i].advance - old_adv;
  2745. off = 0;
  2746. continue;
  2747. }
  2748. off += gl[i].advance * gl[i].repeat;
  2749. }
  2750. return 0.0;
  2751. }
  2752. bool TextServerFallback::_shaped_text_update_breaks(const RID &p_shaped) {
  2753. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  2754. ERR_FAIL_COND_V(!sd, false);
  2755. MutexLock lock(sd->mutex);
  2756. if (!sd->valid) {
  2757. _shaped_text_shape(p_shaped);
  2758. }
  2759. if (sd->line_breaks_valid) {
  2760. return true; // Nothing to do.
  2761. }
  2762. int sd_size = sd->glyphs.size();
  2763. Glyph *sd_glyphs = sd->glyphs.ptrw();
  2764. int c_punct_size = sd->custom_punct.length();
  2765. const char32_t *c_punct = sd->custom_punct.ptr();
  2766. for (int i = 0; i < sd_size; i++) {
  2767. if (sd_glyphs[i].count > 0) {
  2768. char32_t c = sd->text[sd_glyphs[i].start - sd->start];
  2769. if (c_punct_size == 0) {
  2770. if (is_punct(c) && c != 0x005F) {
  2771. sd_glyphs[i].flags |= GRAPHEME_IS_PUNCTUATION;
  2772. }
  2773. } else {
  2774. for (int j = 0; j < c_punct_size; j++) {
  2775. if (c_punct[j] == c) {
  2776. sd_glyphs[i].flags |= GRAPHEME_IS_PUNCTUATION;
  2777. break;
  2778. }
  2779. }
  2780. }
  2781. if (is_underscore(c)) {
  2782. sd->glyphs.write[i].flags |= GRAPHEME_IS_UNDERSCORE;
  2783. }
  2784. if (is_whitespace(c) && !is_linebreak(c)) {
  2785. sd_glyphs[i].flags |= GRAPHEME_IS_SPACE;
  2786. sd_glyphs[i].flags |= GRAPHEME_IS_BREAK_SOFT;
  2787. }
  2788. if (is_linebreak(c)) {
  2789. sd_glyphs[i].flags |= GRAPHEME_IS_SPACE;
  2790. sd_glyphs[i].flags |= GRAPHEME_IS_BREAK_HARD;
  2791. }
  2792. if (c == 0x0009 || c == 0x000b) {
  2793. sd_glyphs[i].flags |= GRAPHEME_IS_TAB;
  2794. }
  2795. i += (sd_glyphs[i].count - 1);
  2796. }
  2797. }
  2798. sd->line_breaks_valid = true;
  2799. return sd->line_breaks_valid;
  2800. }
  2801. bool TextServerFallback::_shaped_text_update_justification_ops(const RID &p_shaped) {
  2802. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  2803. ERR_FAIL_COND_V(!sd, false);
  2804. MutexLock lock(sd->mutex);
  2805. if (!sd->valid) {
  2806. _shaped_text_shape(p_shaped);
  2807. }
  2808. if (!sd->line_breaks_valid) {
  2809. _shaped_text_update_breaks(p_shaped);
  2810. }
  2811. sd->justification_ops_valid = true; // Not supported by fallback server.
  2812. return true;
  2813. }
  2814. void TextServerFallback::_shaped_text_overrun_trim_to_width(const RID &p_shaped_line, double p_width, BitField<TextServer::TextOverrunFlag> p_trim_flags) {
  2815. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped_line);
  2816. ERR_FAIL_COND_MSG(!sd, "ShapedTextDataFallback invalid.");
  2817. MutexLock lock(sd->mutex);
  2818. if (!sd->valid) {
  2819. _shaped_text_shape(p_shaped_line);
  2820. }
  2821. sd->text_trimmed = false;
  2822. sd->overrun_trim_data.ellipsis_glyph_buf.clear();
  2823. bool add_ellipsis = p_trim_flags.has_flag(OVERRUN_ADD_ELLIPSIS);
  2824. bool cut_per_word = p_trim_flags.has_flag(OVERRUN_TRIM_WORD_ONLY);
  2825. bool enforce_ellipsis = p_trim_flags.has_flag(OVERRUN_ENFORCE_ELLIPSIS);
  2826. bool justification_aware = p_trim_flags.has_flag(OVERRUN_JUSTIFICATION_AWARE);
  2827. Glyph *sd_glyphs = sd->glyphs.ptrw();
  2828. if ((p_trim_flags & OVERRUN_TRIM) == OVERRUN_NO_TRIM || sd_glyphs == nullptr || p_width <= 0 || !(sd->width > p_width || enforce_ellipsis)) {
  2829. sd->overrun_trim_data.trim_pos = -1;
  2830. sd->overrun_trim_data.ellipsis_pos = -1;
  2831. return;
  2832. }
  2833. if (justification_aware && !sd->fit_width_minimum_reached) {
  2834. return;
  2835. }
  2836. Vector<ShapedTextDataFallback::Span> &spans = sd->spans;
  2837. if (sd->parent != RID()) {
  2838. ShapedTextDataFallback *parent_sd = shaped_owner.get_or_null(sd->parent);
  2839. ERR_FAIL_COND(!parent_sd->valid);
  2840. spans = parent_sd->spans;
  2841. }
  2842. if (spans.size() == 0) {
  2843. return;
  2844. }
  2845. int sd_size = sd->glyphs.size();
  2846. int last_gl_font_size = sd_glyphs[sd_size - 1].font_size;
  2847. // Find usable fonts, if fonts from the last glyph do not have required chars.
  2848. RID dot_gl_font_rid = sd_glyphs[sd_size - 1].font_rid;
  2849. if (!_font_has_char(dot_gl_font_rid, '.')) {
  2850. const Array &fonts = spans[spans.size() - 1].fonts;
  2851. for (int i = 0; i < fonts.size(); i++) {
  2852. if (_font_has_char(fonts[i], '.')) {
  2853. dot_gl_font_rid = fonts[i];
  2854. break;
  2855. }
  2856. }
  2857. }
  2858. RID whitespace_gl_font_rid = sd_glyphs[sd_size - 1].font_rid;
  2859. if (!_font_has_char(whitespace_gl_font_rid, '.')) {
  2860. const Array &fonts = spans[spans.size() - 1].fonts;
  2861. for (int i = 0; i < fonts.size(); i++) {
  2862. if (_font_has_char(fonts[i], ' ')) {
  2863. whitespace_gl_font_rid = fonts[i];
  2864. break;
  2865. }
  2866. }
  2867. }
  2868. int32_t dot_gl_idx = dot_gl_font_rid.is_valid() ? _font_get_glyph_index(dot_gl_font_rid, last_gl_font_size, '.', 0) : -10;
  2869. 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();
  2870. int32_t whitespace_gl_idx = whitespace_gl_font_rid.is_valid() ? _font_get_glyph_index(whitespace_gl_font_rid, last_gl_font_size, ' ', 0) : -10;
  2871. 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();
  2872. int ellipsis_width = 0;
  2873. if (add_ellipsis && whitespace_gl_font_rid.is_valid()) {
  2874. ellipsis_width = 3 * dot_adv.x + sd->extra_spacing[SPACING_GLYPH] + (cut_per_word ? whitespace_adv.x : 0);
  2875. }
  2876. int ell_min_characters = 6;
  2877. double width = sd->width;
  2878. int trim_pos = 0;
  2879. int ellipsis_pos = (enforce_ellipsis) ? 0 : -1;
  2880. int last_valid_cut = 0;
  2881. bool found = false;
  2882. if (enforce_ellipsis && (width + ellipsis_width <= p_width)) {
  2883. trim_pos = -1;
  2884. ellipsis_pos = sd_size;
  2885. } else {
  2886. for (int i = sd_size - 1; i != -1; i--) {
  2887. width -= sd_glyphs[i].advance * sd_glyphs[i].repeat;
  2888. if (sd_glyphs[i].count > 0) {
  2889. bool above_min_char_threshold = (i >= ell_min_characters);
  2890. if (width + (((above_min_char_threshold && add_ellipsis) || enforce_ellipsis) ? ellipsis_width : 0) <= p_width) {
  2891. if (cut_per_word && above_min_char_threshold) {
  2892. if ((sd_glyphs[i].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT) {
  2893. last_valid_cut = i;
  2894. found = true;
  2895. }
  2896. } else {
  2897. last_valid_cut = i;
  2898. found = true;
  2899. }
  2900. if (found) {
  2901. trim_pos = last_valid_cut;
  2902. if (add_ellipsis && (above_min_char_threshold || enforce_ellipsis) && width - ellipsis_width <= p_width) {
  2903. ellipsis_pos = trim_pos;
  2904. }
  2905. break;
  2906. }
  2907. }
  2908. }
  2909. }
  2910. }
  2911. sd->overrun_trim_data.trim_pos = trim_pos;
  2912. sd->overrun_trim_data.ellipsis_pos = ellipsis_pos;
  2913. if (trim_pos == 0 && enforce_ellipsis && add_ellipsis) {
  2914. sd->overrun_trim_data.ellipsis_pos = 0;
  2915. }
  2916. if ((trim_pos >= 0 && sd->width > p_width) || enforce_ellipsis) {
  2917. if (add_ellipsis && (ellipsis_pos > 0 || enforce_ellipsis)) {
  2918. // Insert an additional space when cutting word bound for aesthetics.
  2919. if (cut_per_word && (ellipsis_pos > 0)) {
  2920. Glyph gl;
  2921. gl.count = 1;
  2922. gl.advance = whitespace_adv.x;
  2923. gl.index = whitespace_gl_idx;
  2924. gl.font_rid = whitespace_gl_font_rid;
  2925. gl.font_size = last_gl_font_size;
  2926. gl.flags = GRAPHEME_IS_SPACE | GRAPHEME_IS_BREAK_SOFT | GRAPHEME_IS_VIRTUAL;
  2927. sd->overrun_trim_data.ellipsis_glyph_buf.append(gl);
  2928. }
  2929. // Add ellipsis dots.
  2930. if (dot_gl_idx != 0) {
  2931. Glyph gl;
  2932. gl.count = 1;
  2933. gl.repeat = 3;
  2934. gl.advance = dot_adv.x;
  2935. gl.index = dot_gl_idx;
  2936. gl.font_rid = dot_gl_font_rid;
  2937. gl.font_size = last_gl_font_size;
  2938. gl.flags = GRAPHEME_IS_PUNCTUATION | GRAPHEME_IS_VIRTUAL;
  2939. sd->overrun_trim_data.ellipsis_glyph_buf.append(gl);
  2940. }
  2941. }
  2942. sd->text_trimmed = true;
  2943. sd->width_trimmed = width + ((ellipsis_pos != -1) ? ellipsis_width : 0);
  2944. }
  2945. }
  2946. int64_t TextServerFallback::_shaped_text_get_trim_pos(const RID &p_shaped) const {
  2947. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  2948. ERR_FAIL_COND_V_MSG(!sd, -1, "ShapedTextDataFallback invalid.");
  2949. MutexLock lock(sd->mutex);
  2950. return sd->overrun_trim_data.trim_pos;
  2951. }
  2952. int64_t TextServerFallback::_shaped_text_get_ellipsis_pos(const RID &p_shaped) const {
  2953. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  2954. ERR_FAIL_COND_V_MSG(!sd, -1, "ShapedTextDataFallback invalid.");
  2955. MutexLock lock(sd->mutex);
  2956. return sd->overrun_trim_data.ellipsis_pos;
  2957. }
  2958. const Glyph *TextServerFallback::_shaped_text_get_ellipsis_glyphs(const RID &p_shaped) const {
  2959. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  2960. ERR_FAIL_COND_V_MSG(!sd, nullptr, "ShapedTextDataFallback invalid.");
  2961. MutexLock lock(sd->mutex);
  2962. return sd->overrun_trim_data.ellipsis_glyph_buf.ptr();
  2963. }
  2964. int64_t TextServerFallback::_shaped_text_get_ellipsis_glyph_count(const RID &p_shaped) const {
  2965. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  2966. ERR_FAIL_COND_V_MSG(!sd, 0, "ShapedTextDataFallback invalid.");
  2967. MutexLock lock(sd->mutex);
  2968. return sd->overrun_trim_data.ellipsis_glyph_buf.size();
  2969. }
  2970. bool TextServerFallback::_shaped_text_shape(const RID &p_shaped) {
  2971. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  2972. ERR_FAIL_COND_V(!sd, false);
  2973. MutexLock lock(sd->mutex);
  2974. if (sd->valid) {
  2975. return true;
  2976. }
  2977. if (sd->parent != RID()) {
  2978. full_copy(sd);
  2979. }
  2980. // Cleanup.
  2981. sd->justification_ops_valid = false;
  2982. sd->line_breaks_valid = false;
  2983. sd->ascent = 0.0;
  2984. sd->descent = 0.0;
  2985. sd->width = 0.0;
  2986. sd->glyphs.clear();
  2987. if (sd->text.length() == 0) {
  2988. sd->valid = true;
  2989. return true;
  2990. }
  2991. // "Shape" string.
  2992. for (int i = 0; i < sd->spans.size(); i++) {
  2993. const ShapedTextDataFallback::Span &span = sd->spans[i];
  2994. if (span.embedded_key != Variant()) {
  2995. // Embedded object.
  2996. if (sd->orientation == ORIENTATION_HORIZONTAL) {
  2997. sd->objects[span.embedded_key].rect.position.x = sd->width;
  2998. sd->width += sd->objects[span.embedded_key].rect.size.x;
  2999. } else {
  3000. sd->objects[span.embedded_key].rect.position.y = sd->width;
  3001. sd->width += sd->objects[span.embedded_key].rect.size.y;
  3002. }
  3003. Glyph gl;
  3004. gl.start = span.start;
  3005. gl.end = span.end;
  3006. gl.count = 1;
  3007. gl.index = 0;
  3008. gl.flags = GRAPHEME_IS_VALID | GRAPHEME_IS_VIRTUAL;
  3009. if (sd->orientation == ORIENTATION_HORIZONTAL) {
  3010. gl.advance = sd->objects[span.embedded_key].rect.size.x;
  3011. } else {
  3012. gl.advance = sd->objects[span.embedded_key].rect.size.y;
  3013. }
  3014. sd->glyphs.push_back(gl);
  3015. } else {
  3016. // Text span.
  3017. for (int j = span.start; j < span.end; j++) {
  3018. Glyph gl;
  3019. gl.start = j;
  3020. gl.end = j + 1;
  3021. gl.count = 1;
  3022. gl.font_size = span.font_size;
  3023. gl.index = (int32_t)sd->text[j - sd->start]; // Use codepoint.
  3024. if (gl.index == 0x0009 || gl.index == 0x000b) {
  3025. gl.index = 0x0020;
  3026. }
  3027. if (!sd->preserve_control && is_control(gl.index)) {
  3028. gl.index = 0x0020;
  3029. }
  3030. // Select first font which has character (font are already sorted by span language).
  3031. for (int k = 0; k < span.fonts.size(); k++) {
  3032. if (_font_has_char(span.fonts[k], gl.index)) {
  3033. gl.font_rid = span.fonts[k];
  3034. break;
  3035. }
  3036. }
  3037. double scale = _font_get_scale(gl.font_rid, gl.font_size);
  3038. if (gl.font_rid.is_valid()) {
  3039. bool subpos = (scale != 1.0) || (_font_get_subpixel_positioning(gl.font_rid) == SUBPIXEL_POSITIONING_ONE_HALF) || (_font_get_subpixel_positioning(gl.font_rid) == SUBPIXEL_POSITIONING_ONE_QUARTER) || (_font_get_subpixel_positioning(gl.font_rid) == SUBPIXEL_POSITIONING_AUTO && gl.font_size <= SUBPIXEL_POSITIONING_ONE_HALF_MAX_SIZE);
  3040. if (sd->text[j - sd->start] != 0 && !is_linebreak(sd->text[j - sd->start])) {
  3041. if (sd->orientation == ORIENTATION_HORIZONTAL) {
  3042. gl.advance = _font_get_glyph_advance(gl.font_rid, gl.font_size, gl.index).x;
  3043. gl.x_off = 0;
  3044. gl.y_off = 0;
  3045. sd->ascent = MAX(sd->ascent, _font_get_ascent(gl.font_rid, gl.font_size));
  3046. sd->descent = MAX(sd->descent, _font_get_descent(gl.font_rid, gl.font_size));
  3047. } else {
  3048. gl.advance = _font_get_glyph_advance(gl.font_rid, gl.font_size, gl.index).y;
  3049. gl.x_off = -Math::round(_font_get_glyph_advance(gl.font_rid, gl.font_size, gl.index).x * 0.5);
  3050. gl.y_off = _font_get_ascent(gl.font_rid, gl.font_size);
  3051. sd->ascent = MAX(sd->ascent, Math::round(_font_get_glyph_advance(gl.font_rid, gl.font_size, gl.index).x * 0.5));
  3052. sd->descent = MAX(sd->descent, Math::round(_font_get_glyph_advance(gl.font_rid, gl.font_size, gl.index).x * 0.5));
  3053. }
  3054. }
  3055. if (j < sd->end - 1) {
  3056. // Do not add extra spacing to the last glyph of the string.
  3057. if (is_whitespace(sd->text[j - sd->start])) {
  3058. gl.advance += sd->extra_spacing[SPACING_SPACE];
  3059. } else {
  3060. gl.advance += sd->extra_spacing[SPACING_GLYPH];
  3061. }
  3062. }
  3063. sd->upos = MAX(sd->upos, _font_get_underline_position(gl.font_rid, gl.font_size));
  3064. sd->uthk = MAX(sd->uthk, _font_get_underline_thickness(gl.font_rid, gl.font_size));
  3065. // Add kerning to previous glyph.
  3066. if (sd->glyphs.size() > 0) {
  3067. Glyph &prev_gl = sd->glyphs.write[sd->glyphs.size() - 1];
  3068. if (prev_gl.font_rid == gl.font_rid && prev_gl.font_size == gl.font_size) {
  3069. if (sd->orientation == ORIENTATION_HORIZONTAL) {
  3070. prev_gl.advance += _font_get_kerning(gl.font_rid, gl.font_size, Vector2i(prev_gl.index, gl.index)).x;
  3071. } else {
  3072. prev_gl.advance += _font_get_kerning(gl.font_rid, gl.font_size, Vector2i(prev_gl.index, gl.index)).y;
  3073. }
  3074. }
  3075. }
  3076. if (sd->orientation == ORIENTATION_HORIZONTAL && !subpos) {
  3077. gl.advance = Math::round(gl.advance);
  3078. }
  3079. } else if (sd->preserve_invalid || (sd->preserve_control && is_control(gl.index))) {
  3080. // Glyph not found, replace with hex code box.
  3081. if (sd->orientation == ORIENTATION_HORIZONTAL) {
  3082. gl.advance = get_hex_code_box_size(gl.font_size, gl.index).x;
  3083. sd->ascent = MAX(sd->ascent, get_hex_code_box_size(gl.font_size, gl.index).y);
  3084. } else {
  3085. gl.advance = get_hex_code_box_size(gl.font_size, gl.index).y;
  3086. sd->ascent = MAX(sd->ascent, Math::round(get_hex_code_box_size(gl.font_size, gl.index).x * 0.5));
  3087. sd->descent = MAX(sd->descent, Math::round(get_hex_code_box_size(gl.font_size, gl.index).x * 0.5));
  3088. }
  3089. }
  3090. sd->width += gl.advance;
  3091. sd->glyphs.push_back(gl);
  3092. }
  3093. }
  3094. }
  3095. // Align embedded objects to baseline.
  3096. _realign(sd);
  3097. sd->valid = true;
  3098. return sd->valid;
  3099. }
  3100. bool TextServerFallback::_shaped_text_is_ready(const RID &p_shaped) const {
  3101. const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  3102. ERR_FAIL_COND_V(!sd, false);
  3103. MutexLock lock(sd->mutex);
  3104. return sd->valid;
  3105. }
  3106. const Glyph *TextServerFallback::_shaped_text_get_glyphs(const RID &p_shaped) const {
  3107. const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  3108. ERR_FAIL_COND_V(!sd, nullptr);
  3109. MutexLock lock(sd->mutex);
  3110. if (!sd->valid) {
  3111. const_cast<TextServerFallback *>(this)->_shaped_text_shape(p_shaped);
  3112. }
  3113. return sd->glyphs.ptr();
  3114. }
  3115. int64_t TextServerFallback::_shaped_text_get_glyph_count(const RID &p_shaped) const {
  3116. const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  3117. ERR_FAIL_COND_V(!sd, 0);
  3118. MutexLock lock(sd->mutex);
  3119. if (!sd->valid) {
  3120. const_cast<TextServerFallback *>(this)->_shaped_text_shape(p_shaped);
  3121. }
  3122. return sd->glyphs.size();
  3123. }
  3124. const Glyph *TextServerFallback::_shaped_text_sort_logical(const RID &p_shaped) {
  3125. const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  3126. ERR_FAIL_COND_V(!sd, nullptr);
  3127. MutexLock lock(sd->mutex);
  3128. if (!sd->valid) {
  3129. const_cast<TextServerFallback *>(this)->_shaped_text_shape(p_shaped);
  3130. }
  3131. return sd->glyphs.ptr(); // Already in the logical order, return as is.
  3132. }
  3133. Vector2i TextServerFallback::_shaped_text_get_range(const RID &p_shaped) const {
  3134. const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  3135. ERR_FAIL_COND_V(!sd, Vector2i());
  3136. MutexLock lock(sd->mutex);
  3137. return Vector2(sd->start, sd->end);
  3138. }
  3139. Array TextServerFallback::_shaped_text_get_objects(const RID &p_shaped) const {
  3140. Array ret;
  3141. const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  3142. ERR_FAIL_COND_V(!sd, ret);
  3143. MutexLock lock(sd->mutex);
  3144. for (const KeyValue<Variant, ShapedTextDataFallback::EmbeddedObject> &E : sd->objects) {
  3145. ret.push_back(E.key);
  3146. }
  3147. return ret;
  3148. }
  3149. Rect2 TextServerFallback::_shaped_text_get_object_rect(const RID &p_shaped, const Variant &p_key) const {
  3150. const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  3151. ERR_FAIL_COND_V(!sd, Rect2());
  3152. MutexLock lock(sd->mutex);
  3153. ERR_FAIL_COND_V(!sd->objects.has(p_key), Rect2());
  3154. if (!sd->valid) {
  3155. const_cast<TextServerFallback *>(this)->_shaped_text_shape(p_shaped);
  3156. }
  3157. return sd->objects[p_key].rect;
  3158. }
  3159. Size2 TextServerFallback::_shaped_text_get_size(const RID &p_shaped) const {
  3160. const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  3161. ERR_FAIL_COND_V(!sd, Size2());
  3162. MutexLock lock(sd->mutex);
  3163. if (!sd->valid) {
  3164. const_cast<TextServerFallback *>(this)->_shaped_text_shape(p_shaped);
  3165. }
  3166. if (sd->orientation == TextServer::ORIENTATION_HORIZONTAL) {
  3167. return Size2(sd->width, sd->ascent + sd->descent + sd->extra_spacing[SPACING_TOP] + sd->extra_spacing[SPACING_BOTTOM]).ceil();
  3168. } else {
  3169. return Size2(sd->ascent + sd->descent + sd->extra_spacing[SPACING_TOP] + sd->extra_spacing[SPACING_BOTTOM], sd->width).ceil();
  3170. }
  3171. }
  3172. double TextServerFallback::_shaped_text_get_ascent(const RID &p_shaped) const {
  3173. const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  3174. ERR_FAIL_COND_V(!sd, 0.0);
  3175. MutexLock lock(sd->mutex);
  3176. if (!sd->valid) {
  3177. const_cast<TextServerFallback *>(this)->_shaped_text_shape(p_shaped);
  3178. }
  3179. return sd->ascent + sd->extra_spacing[SPACING_TOP];
  3180. }
  3181. double TextServerFallback::_shaped_text_get_descent(const RID &p_shaped) const {
  3182. const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  3183. ERR_FAIL_COND_V(!sd, 0.0);
  3184. MutexLock lock(sd->mutex);
  3185. if (!sd->valid) {
  3186. const_cast<TextServerFallback *>(this)->_shaped_text_shape(p_shaped);
  3187. }
  3188. return sd->descent + sd->extra_spacing[SPACING_BOTTOM];
  3189. }
  3190. double TextServerFallback::_shaped_text_get_width(const RID &p_shaped) const {
  3191. const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  3192. ERR_FAIL_COND_V(!sd, 0.0);
  3193. MutexLock lock(sd->mutex);
  3194. if (!sd->valid) {
  3195. const_cast<TextServerFallback *>(this)->_shaped_text_shape(p_shaped);
  3196. }
  3197. return Math::ceil(sd->width);
  3198. }
  3199. double TextServerFallback::_shaped_text_get_underline_position(const RID &p_shaped) const {
  3200. const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  3201. ERR_FAIL_COND_V(!sd, 0.0);
  3202. MutexLock lock(sd->mutex);
  3203. if (!sd->valid) {
  3204. const_cast<TextServerFallback *>(this)->_shaped_text_shape(p_shaped);
  3205. }
  3206. return sd->upos;
  3207. }
  3208. double TextServerFallback::_shaped_text_get_underline_thickness(const RID &p_shaped) const {
  3209. const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  3210. ERR_FAIL_COND_V(!sd, 0.0);
  3211. MutexLock lock(sd->mutex);
  3212. if (!sd->valid) {
  3213. const_cast<TextServerFallback *>(this)->_shaped_text_shape(p_shaped);
  3214. }
  3215. return sd->uthk;
  3216. }
  3217. String TextServerFallback::_string_to_upper(const String &p_string, const String &p_language) const {
  3218. String upper = p_string;
  3219. for (int i = 0; i <= upper.length(); i++) {
  3220. const char32_t s = upper[i];
  3221. const char32_t t = _find_upper(s);
  3222. if (s != t) { // avoid copy on write
  3223. upper[i] = t;
  3224. }
  3225. }
  3226. return upper;
  3227. }
  3228. String TextServerFallback::_string_to_lower(const String &p_string, const String &p_language) const {
  3229. String lower = p_string;
  3230. for (int i = 0; i <= lower.length(); i++) {
  3231. const char32_t s = lower[i];
  3232. const char32_t t = _find_lower(s);
  3233. if (s != t) { // avoid copy on write
  3234. lower[i] = t;
  3235. }
  3236. }
  3237. return lower;
  3238. }
  3239. PackedInt32Array TextServerFallback::_string_get_word_breaks(const String &p_string, const String &p_language) const {
  3240. PackedInt32Array ret;
  3241. for (int i = 0; i < p_string.length(); i++) {
  3242. char32_t c = p_string[i];
  3243. if (c == 0xfffc) {
  3244. continue;
  3245. }
  3246. if (is_punct(c) && c != 0x005F) {
  3247. ret.push_back(i);
  3248. continue;
  3249. }
  3250. if (is_underscore(c)) {
  3251. ret.push_back(i);
  3252. continue;
  3253. }
  3254. if (is_whitespace(c) || is_linebreak(c)) {
  3255. ret.push_back(i);
  3256. continue;
  3257. }
  3258. }
  3259. return ret;
  3260. }
  3261. TextServerFallback::TextServerFallback() {
  3262. _insert_feature_sets();
  3263. };
  3264. TextServerFallback::~TextServerFallback() {
  3265. #ifdef MODULE_FREETYPE_ENABLED
  3266. if (ft_library != nullptr) {
  3267. FT_Done_FreeType(ft_library);
  3268. }
  3269. #endif
  3270. };