text_server_fb.cpp 105 KB

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