text_server_fb.cpp 102 KB

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