text_server_fb.cpp 139 KB

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