12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083 |
- /*************************************************************************/
- /* font.cpp */
- /*************************************************************************/
- /* This file is part of: */
- /* GODOT ENGINE */
- /* https://godotengine.org */
- /*************************************************************************/
- /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
- /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
- /* */
- /* Permission is hereby granted, free of charge, to any person obtaining */
- /* a copy of this software and associated documentation files (the */
- /* "Software"), to deal in the Software without restriction, including */
- /* without limitation the rights to use, copy, modify, merge, publish, */
- /* distribute, sublicense, and/or sell copies of the Software, and to */
- /* permit persons to whom the Software is furnished to do so, subject to */
- /* the following conditions: */
- /* */
- /* The above copyright notice and this permission notice shall be */
- /* included in all copies or substantial portions of the Software. */
- /* */
- /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
- /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
- /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
- /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
- /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
- /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
- /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
- /*************************************************************************/
- #include "font.h"
- #include "core/core_string_names.h"
- #include "core/io/image_loader.h"
- #include "core/io/resource_loader.h"
- #include "core/string/translation.h"
- #include "core/templates/hash_map.h"
- #include "core/templates/hashfuncs.h"
- #include "scene/resources/text_line.h"
- #include "scene/resources/text_paragraph.h"
- #include "scene/resources/theme.h"
- #include "scene/theme/theme_db.h"
- /*************************************************************************/
- /* Font */
- /*************************************************************************/
- void Font::_bind_methods() {
- ClassDB::bind_method(D_METHOD("set_fallbacks", "fallbacks"), &Font::set_fallbacks);
- ClassDB::bind_method(D_METHOD("get_fallbacks"), &Font::get_fallbacks);
- // Output.
- ClassDB::bind_method(D_METHOD("find_variation", "variation_coordinates", "face_index", "strength", "transform"), &Font::find_variation, DEFVAL(0), DEFVAL(0.0), DEFVAL(Transform2D()));
- ClassDB::bind_method(D_METHOD("get_rids"), &Font::get_rids);
- // Font metrics.
- ClassDB::bind_method(D_METHOD("get_height", "font_size"), &Font::get_height, DEFVAL(DEFAULT_FONT_SIZE));
- ClassDB::bind_method(D_METHOD("get_ascent", "font_size"), &Font::get_ascent, DEFVAL(DEFAULT_FONT_SIZE));
- ClassDB::bind_method(D_METHOD("get_descent", "font_size"), &Font::get_descent, DEFVAL(DEFAULT_FONT_SIZE));
- ClassDB::bind_method(D_METHOD("get_underline_position", "font_size"), &Font::get_underline_position, DEFVAL(DEFAULT_FONT_SIZE));
- ClassDB::bind_method(D_METHOD("get_underline_thickness", "font_size"), &Font::get_underline_thickness, DEFVAL(DEFAULT_FONT_SIZE));
- ClassDB::bind_method(D_METHOD("get_font_name"), &Font::get_font_name);
- ClassDB::bind_method(D_METHOD("get_font_style_name"), &Font::get_font_style_name);
- ClassDB::bind_method(D_METHOD("get_font_style"), &Font::get_font_style);
- ClassDB::bind_method(D_METHOD("get_spacing", "spacing"), &Font::get_spacing);
- ClassDB::bind_method(D_METHOD("get_opentype_features"), &Font::get_opentype_features);
- // Drawing string.
- ClassDB::bind_method(D_METHOD("set_cache_capacity", "single_line", "multi_line"), &Font::set_cache_capacity);
- ClassDB::bind_method(D_METHOD("get_string_size", "text", "alignment", "width", "font_size", "jst_flags", "direction", "orientation"), &Font::get_string_size, DEFVAL(HORIZONTAL_ALIGNMENT_LEFT), DEFVAL(-1), DEFVAL(DEFAULT_FONT_SIZE), DEFVAL(TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND), DEFVAL(TextServer::DIRECTION_AUTO), DEFVAL(TextServer::ORIENTATION_HORIZONTAL));
- ClassDB::bind_method(D_METHOD("get_multiline_string_size", "text", "alignment", "width", "font_size", "max_lines", "brk_flags", "jst_flags", "direction", "orientation"), &Font::get_multiline_string_size, DEFVAL(HORIZONTAL_ALIGNMENT_LEFT), DEFVAL(-1), DEFVAL(DEFAULT_FONT_SIZE), DEFVAL(-1), DEFVAL(TextServer::BREAK_MANDATORY | TextServer::BREAK_WORD_BOUND), DEFVAL(TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND), DEFVAL(TextServer::DIRECTION_AUTO), DEFVAL(TextServer::ORIENTATION_HORIZONTAL));
- ClassDB::bind_method(D_METHOD("draw_string", "canvas_item", "pos", "text", "alignment", "width", "font_size", "modulate", "jst_flags", "direction", "orientation"), &Font::draw_string, DEFVAL(HORIZONTAL_ALIGNMENT_LEFT), DEFVAL(-1), DEFVAL(DEFAULT_FONT_SIZE), DEFVAL(Color(1.0, 1.0, 1.0)), DEFVAL(TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND), DEFVAL(TextServer::DIRECTION_AUTO), DEFVAL(TextServer::ORIENTATION_HORIZONTAL));
- ClassDB::bind_method(D_METHOD("draw_multiline_string", "canvas_item", "pos", "text", "alignment", "width", "font_size", "max_lines", "modulate", "brk_flags", "jst_flags", "direction", "orientation"), &Font::draw_multiline_string, DEFVAL(HORIZONTAL_ALIGNMENT_LEFT), DEFVAL(-1), DEFVAL(DEFAULT_FONT_SIZE), DEFVAL(-1), DEFVAL(Color(1.0, 1.0, 1.0)), DEFVAL(TextServer::BREAK_MANDATORY | TextServer::BREAK_WORD_BOUND), DEFVAL(TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND), DEFVAL(TextServer::DIRECTION_AUTO), DEFVAL(TextServer::ORIENTATION_HORIZONTAL));
- ClassDB::bind_method(D_METHOD("draw_string_outline", "canvas_item", "pos", "text", "alignment", "width", "font_size", "size", "modulate", "jst_flags", "direction", "orientation"), &Font::draw_string_outline, DEFVAL(HORIZONTAL_ALIGNMENT_LEFT), DEFVAL(-1), DEFVAL(DEFAULT_FONT_SIZE), DEFVAL(1), DEFVAL(Color(1.0, 1.0, 1.0)), DEFVAL(TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND), DEFVAL(TextServer::DIRECTION_AUTO), DEFVAL(TextServer::ORIENTATION_HORIZONTAL));
- ClassDB::bind_method(D_METHOD("draw_multiline_string_outline", "canvas_item", "pos", "text", "alignment", "width", "font_size", "max_lines", "size", "modulate", "brk_flags", "jst_flags", "direction", "orientation"), &Font::draw_multiline_string_outline, DEFVAL(HORIZONTAL_ALIGNMENT_LEFT), DEFVAL(-1), DEFVAL(DEFAULT_FONT_SIZE), DEFVAL(-1), DEFVAL(1), DEFVAL(Color(1.0, 1.0, 1.0)), DEFVAL(TextServer::BREAK_MANDATORY | TextServer::BREAK_WORD_BOUND), DEFVAL(TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND), DEFVAL(TextServer::DIRECTION_AUTO), DEFVAL(TextServer::ORIENTATION_HORIZONTAL));
- // Drawing char.
- ClassDB::bind_method(D_METHOD("get_char_size", "char", "font_size"), &Font::get_char_size);
- ClassDB::bind_method(D_METHOD("draw_char", "canvas_item", "pos", "char", "font_size", "modulate"), &Font::draw_char, DEFVAL(Color(1.0, 1.0, 1.0)));
- ClassDB::bind_method(D_METHOD("draw_char_outline", "canvas_item", "pos", "char", "font_size", "size", "modulate"), &Font::draw_char_outline, DEFVAL(-1), DEFVAL(Color(1.0, 1.0, 1.0)));
- // Helper functions.
- ClassDB::bind_method(D_METHOD("has_char", "char"), &Font::has_char);
- ClassDB::bind_method(D_METHOD("get_supported_chars"), &Font::get_supported_chars);
- ClassDB::bind_method(D_METHOD("is_language_supported", "language"), &Font::is_language_supported);
- ClassDB::bind_method(D_METHOD("is_script_supported", "script"), &Font::is_script_supported);
- ClassDB::bind_method(D_METHOD("get_supported_feature_list"), &Font::get_supported_feature_list);
- ClassDB::bind_method(D_METHOD("get_supported_variation_list"), &Font::get_supported_variation_list);
- ClassDB::bind_method(D_METHOD("get_face_count"), &Font::get_face_count);
- }
- void Font::_update_rids_fb(const Ref<Font> &p_f, int p_depth) const {
- ERR_FAIL_COND(p_depth > MAX_FALLBACK_DEPTH);
- if (p_f.is_valid()) {
- RID rid = p_f->_get_rid();
- if (rid.is_valid()) {
- rids.push_back(rid);
- }
- const TypedArray<Font> &_fallbacks = p_f->get_fallbacks();
- for (int i = 0; i < _fallbacks.size(); i++) {
- _update_rids_fb(_fallbacks[i], p_depth + 1);
- }
- }
- }
- void Font::_update_rids() const {
- rids.clear();
- _update_rids_fb(const_cast<Font *>(this), 0);
- dirty_rids = false;
- }
- void Font::_invalidate_rids() {
- rids.clear();
- dirty_rids = true;
- cache.clear();
- cache_wrap.clear();
- emit_changed();
- }
- bool Font::_is_cyclic(const Ref<Font> &p_f, int p_depth) const {
- ERR_FAIL_COND_V(p_depth > MAX_FALLBACK_DEPTH, false);
- if (p_f.is_null()) {
- return false;
- }
- for (int i = 0; i < p_f->fallbacks.size(); i++) {
- const Ref<Font> &f = p_f->fallbacks[i];
- if (f == this) {
- return true;
- }
- return _is_cyclic(f, p_depth + 1);
- }
- return false;
- }
- void Font::reset_state() {
- _invalidate_rids();
- }
- // Fallbacks.
- void Font::set_fallbacks(const TypedArray<Font> &p_fallbacks) {
- ERR_FAIL_COND(_is_cyclic(this, 0));
- for (int i = 0; i < fallbacks.size(); i++) {
- Ref<Font> f = fallbacks[i];
- if (f.is_valid()) {
- f->disconnect(CoreStringNames::get_singleton()->changed, callable_mp(this, &Font::_invalidate_rids));
- }
- }
- fallbacks = p_fallbacks;
- for (int i = 0; i < fallbacks.size(); i++) {
- Ref<Font> f = fallbacks[i];
- if (f.is_valid()) {
- f->connect(CoreStringNames::get_singleton()->changed, callable_mp(this, &Font::_invalidate_rids), CONNECT_REFERENCE_COUNTED);
- }
- }
- _invalidate_rids();
- }
- TypedArray<Font> Font::get_fallbacks() const {
- return fallbacks;
- }
- // Output.
- TypedArray<RID> Font::get_rids() const {
- if (dirty_rids) {
- _update_rids();
- }
- return rids;
- }
- // Drawing string.
- real_t Font::get_height(int p_font_size) const {
- if (dirty_rids) {
- _update_rids();
- }
- real_t ret = 0.f;
- for (int i = 0; i < rids.size(); i++) {
- ret = MAX(ret, TS->font_get_ascent(rids[i], p_font_size) + TS->font_get_descent(rids[i], p_font_size));
- }
- return ret + get_spacing(TextServer::SPACING_BOTTOM) + get_spacing(TextServer::SPACING_TOP);
- }
- real_t Font::get_ascent(int p_font_size) const {
- if (dirty_rids) {
- _update_rids();
- }
- real_t ret = 0.f;
- for (int i = 0; i < rids.size(); i++) {
- ret = MAX(ret, TS->font_get_ascent(rids[i], p_font_size));
- }
- return ret + get_spacing(TextServer::SPACING_TOP);
- }
- real_t Font::get_descent(int p_font_size) const {
- if (dirty_rids) {
- _update_rids();
- }
- real_t ret = 0.f;
- for (int i = 0; i < rids.size(); i++) {
- ret = MAX(ret, TS->font_get_descent(rids[i], p_font_size));
- }
- return ret + get_spacing(TextServer::SPACING_BOTTOM);
- }
- real_t Font::get_underline_position(int p_font_size) const {
- if (dirty_rids) {
- _update_rids();
- }
- real_t ret = 0.f;
- for (int i = 0; i < rids.size(); i++) {
- ret = MAX(ret, TS->font_get_underline_position(rids[i], p_font_size));
- }
- return ret + get_spacing(TextServer::SPACING_TOP);
- }
- real_t Font::get_underline_thickness(int p_font_size) const {
- if (dirty_rids) {
- _update_rids();
- }
- real_t ret = 0.f;
- for (int i = 0; i < rids.size(); i++) {
- ret = MAX(ret, TS->font_get_underline_thickness(rids[i], p_font_size));
- }
- return ret;
- }
- String Font::get_font_name() const {
- return TS->font_get_name(_get_rid());
- }
- String Font::get_font_style_name() const {
- return TS->font_get_style_name(_get_rid());
- }
- BitField<TextServer::FontStyle> Font::get_font_style() const {
- return TS->font_get_style(_get_rid());
- }
- Dictionary Font::get_opentype_features() const {
- return Dictionary();
- }
- // Drawing string.
- void Font::set_cache_capacity(int p_single_line, int p_multi_line) {
- cache.set_capacity(p_single_line);
- cache_wrap.set_capacity(p_multi_line);
- }
- Size2 Font::get_string_size(const String &p_text, HorizontalAlignment p_alignment, float p_width, int p_font_size, BitField<TextServer::JustificationFlag> p_jst_flags, TextServer::Direction p_direction, TextServer::Orientation p_orientation) const {
- uint64_t hash = p_text.hash64();
- hash = hash_djb2_one_64(p_font_size, hash);
- if (p_alignment == HORIZONTAL_ALIGNMENT_FILL) {
- hash = hash_djb2_one_64(hash_murmur3_one_float(p_width), hash);
- hash = hash_djb2_one_64(p_jst_flags.operator uint32_t(), hash);
- }
- hash = hash_djb2_one_64(p_direction, hash);
- hash = hash_djb2_one_64(p_orientation, hash);
- Ref<TextLine> buffer;
- if (cache.has(hash)) {
- buffer = cache.get(hash);
- } else {
- buffer.instantiate();
- buffer->set_direction(p_direction);
- buffer->set_orientation(p_orientation);
- buffer->add_string(p_text, Ref<Font>(this), p_font_size);
- cache.insert(hash, buffer);
- }
- buffer->set_width(p_width);
- buffer->set_horizontal_alignment(p_alignment);
- if (p_alignment == HORIZONTAL_ALIGNMENT_FILL) {
- buffer->set_flags(p_jst_flags);
- }
- return buffer->get_size();
- }
- Size2 Font::get_multiline_string_size(const String &p_text, HorizontalAlignment p_alignment, float p_width, int p_font_size, int p_max_lines, BitField<TextServer::LineBreakFlag> p_brk_flags, BitField<TextServer::JustificationFlag> p_jst_flags, TextServer::Direction p_direction, TextServer::Orientation p_orientation) const {
- uint64_t hash = p_text.hash64();
- hash = hash_djb2_one_64(p_font_size, hash);
- hash = hash_djb2_one_64(hash_murmur3_one_float(p_width), hash);
- hash = hash_djb2_one_64(p_brk_flags.operator uint32_t(), hash);
- hash = hash_djb2_one_64(p_jst_flags.operator uint32_t(), hash);
- hash = hash_djb2_one_64(p_direction, hash);
- hash = hash_djb2_one_64(p_orientation, hash);
- Ref<TextParagraph> lines_buffer;
- if (cache_wrap.has(hash)) {
- lines_buffer = cache_wrap.get(hash);
- } else {
- lines_buffer.instantiate();
- lines_buffer->set_direction(p_direction);
- lines_buffer->set_orientation(p_orientation);
- lines_buffer->add_string(p_text, Ref<Font>(this), p_font_size);
- lines_buffer->set_width(p_width);
- lines_buffer->set_break_flags(p_brk_flags);
- lines_buffer->set_justification_flags(p_jst_flags);
- cache_wrap.insert(hash, lines_buffer);
- }
- lines_buffer->set_alignment(p_alignment);
- lines_buffer->set_max_lines_visible(p_max_lines);
- return lines_buffer->get_size();
- }
- void Font::draw_string(RID p_canvas_item, const Point2 &p_pos, const String &p_text, HorizontalAlignment p_alignment, float p_width, int p_font_size, const Color &p_modulate, BitField<TextServer::JustificationFlag> p_jst_flags, TextServer::Direction p_direction, TextServer::Orientation p_orientation) const {
- uint64_t hash = p_text.hash64();
- hash = hash_djb2_one_64(p_font_size, hash);
- if (p_alignment == HORIZONTAL_ALIGNMENT_FILL) {
- hash = hash_djb2_one_64(hash_murmur3_one_float(p_width), hash);
- hash = hash_djb2_one_64(p_jst_flags.operator uint32_t(), hash);
- }
- hash = hash_djb2_one_64(p_direction, hash);
- hash = hash_djb2_one_64(p_orientation, hash);
- Ref<TextLine> buffer;
- if (cache.has(hash)) {
- buffer = cache.get(hash);
- } else {
- buffer.instantiate();
- buffer->set_direction(p_direction);
- buffer->set_orientation(p_orientation);
- buffer->add_string(p_text, Ref<Font>(this), p_font_size);
- cache.insert(hash, buffer);
- }
- Vector2 ofs = p_pos;
- if (p_orientation == TextServer::ORIENTATION_HORIZONTAL) {
- ofs.y -= buffer->get_line_ascent();
- } else {
- ofs.x -= buffer->get_line_ascent();
- }
- buffer->set_width(p_width);
- buffer->set_horizontal_alignment(p_alignment);
- if (p_alignment == HORIZONTAL_ALIGNMENT_FILL) {
- buffer->set_flags(p_jst_flags);
- }
- buffer->draw(p_canvas_item, ofs, p_modulate);
- }
- void Font::draw_multiline_string(RID p_canvas_item, const Point2 &p_pos, const String &p_text, HorizontalAlignment p_alignment, float p_width, int p_font_size, int p_max_lines, const Color &p_modulate, BitField<TextServer::LineBreakFlag> p_brk_flags, BitField<TextServer::JustificationFlag> p_jst_flags, TextServer::Direction p_direction, TextServer::Orientation p_orientation) const {
- uint64_t hash = p_text.hash64();
- hash = hash_djb2_one_64(p_font_size, hash);
- hash = hash_djb2_one_64(hash_murmur3_one_float(p_width), hash);
- hash = hash_djb2_one_64(p_brk_flags.operator uint32_t(), hash);
- hash = hash_djb2_one_64(p_jst_flags.operator uint32_t(), hash);
- hash = hash_djb2_one_64(p_direction, hash);
- hash = hash_djb2_one_64(p_orientation, hash);
- Ref<TextParagraph> lines_buffer;
- if (cache_wrap.has(hash)) {
- lines_buffer = cache_wrap.get(hash);
- } else {
- lines_buffer.instantiate();
- lines_buffer->set_direction(p_direction);
- lines_buffer->set_orientation(p_orientation);
- lines_buffer->add_string(p_text, Ref<Font>(this), p_font_size);
- lines_buffer->set_width(p_width);
- lines_buffer->set_break_flags(p_brk_flags);
- lines_buffer->set_justification_flags(p_jst_flags);
- cache_wrap.insert(hash, lines_buffer);
- }
- Vector2 ofs = p_pos;
- if (p_orientation == TextServer::ORIENTATION_HORIZONTAL) {
- ofs.y -= lines_buffer->get_line_ascent(0);
- } else {
- ofs.x -= lines_buffer->get_line_ascent(0);
- }
- lines_buffer->set_alignment(p_alignment);
- lines_buffer->set_max_lines_visible(p_max_lines);
- lines_buffer->draw(p_canvas_item, ofs, p_modulate);
- }
- void Font::draw_string_outline(RID p_canvas_item, const Point2 &p_pos, const String &p_text, HorizontalAlignment p_alignment, float p_width, int p_font_size, int p_size, const Color &p_modulate, BitField<TextServer::JustificationFlag> p_jst_flags, TextServer::Direction p_direction, TextServer::Orientation p_orientation) const {
- uint64_t hash = p_text.hash64();
- hash = hash_djb2_one_64(p_font_size, hash);
- if (p_alignment == HORIZONTAL_ALIGNMENT_FILL) {
- hash = hash_djb2_one_64(hash_murmur3_one_float(p_width), hash);
- hash = hash_djb2_one_64(p_jst_flags.operator uint32_t(), hash);
- }
- hash = hash_djb2_one_64(p_direction, hash);
- hash = hash_djb2_one_64(p_orientation, hash);
- Ref<TextLine> buffer;
- if (cache.has(hash)) {
- buffer = cache.get(hash);
- } else {
- buffer.instantiate();
- buffer->set_direction(p_direction);
- buffer->set_orientation(p_orientation);
- buffer->add_string(p_text, Ref<Font>(this), p_font_size);
- cache.insert(hash, buffer);
- }
- Vector2 ofs = p_pos;
- if (p_orientation == TextServer::ORIENTATION_HORIZONTAL) {
- ofs.y -= buffer->get_line_ascent();
- } else {
- ofs.x -= buffer->get_line_ascent();
- }
- buffer->set_width(p_width);
- buffer->set_horizontal_alignment(p_alignment);
- if (p_alignment == HORIZONTAL_ALIGNMENT_FILL) {
- buffer->set_flags(p_jst_flags);
- }
- buffer->draw_outline(p_canvas_item, ofs, p_size, p_modulate);
- }
- void Font::draw_multiline_string_outline(RID p_canvas_item, const Point2 &p_pos, const String &p_text, HorizontalAlignment p_alignment, float p_width, int p_font_size, int p_max_lines, int p_size, const Color &p_modulate, BitField<TextServer::LineBreakFlag> p_brk_flags, BitField<TextServer::JustificationFlag> p_jst_flags, TextServer::Direction p_direction, TextServer::Orientation p_orientation) const {
- uint64_t hash = p_text.hash64();
- hash = hash_djb2_one_64(p_font_size, hash);
- hash = hash_djb2_one_64(hash_murmur3_one_float(p_width), hash);
- hash = hash_djb2_one_64(p_brk_flags.operator uint32_t(), hash);
- hash = hash_djb2_one_64(p_jst_flags.operator uint32_t(), hash);
- hash = hash_djb2_one_64(p_direction, hash);
- hash = hash_djb2_one_64(p_orientation, hash);
- Ref<TextParagraph> lines_buffer;
- if (cache_wrap.has(hash)) {
- lines_buffer = cache_wrap.get(hash);
- } else {
- lines_buffer.instantiate();
- lines_buffer->set_direction(p_direction);
- lines_buffer->set_orientation(p_orientation);
- lines_buffer->add_string(p_text, Ref<Font>(this), p_font_size);
- lines_buffer->set_width(p_width);
- lines_buffer->set_break_flags(p_brk_flags);
- lines_buffer->set_justification_flags(p_jst_flags);
- cache_wrap.insert(hash, lines_buffer);
- }
- Vector2 ofs = p_pos;
- if (p_orientation == TextServer::ORIENTATION_HORIZONTAL) {
- ofs.y -= lines_buffer->get_line_ascent(0);
- } else {
- ofs.x -= lines_buffer->get_line_ascent(0);
- }
- lines_buffer->set_alignment(p_alignment);
- lines_buffer->set_max_lines_visible(p_max_lines);
- lines_buffer->draw_outline(p_canvas_item, ofs, p_size, p_modulate);
- }
- // Drawing char.
- Size2 Font::get_char_size(char32_t p_char, int p_font_size) const {
- if (dirty_rids) {
- _update_rids();
- }
- for (int i = 0; i < rids.size(); i++) {
- if (TS->font_has_char(rids[i], p_char)) {
- int32_t glyph = TS->font_get_glyph_index(rids[i], p_font_size, p_char, 0);
- return Size2(TS->font_get_glyph_advance(rids[i], p_font_size, glyph).x, get_height(p_font_size));
- }
- }
- return Size2();
- }
- real_t Font::draw_char(RID p_canvas_item, const Point2 &p_pos, char32_t p_char, int p_font_size, const Color &p_modulate) const {
- if (dirty_rids) {
- _update_rids();
- }
- for (int i = 0; i < rids.size(); i++) {
- if (TS->font_has_char(rids[i], p_char)) {
- int32_t glyph = TS->font_get_glyph_index(rids[i], p_font_size, p_char, 0);
- TS->font_draw_glyph(rids[i], p_canvas_item, p_font_size, p_pos, glyph, p_modulate);
- return TS->font_get_glyph_advance(rids[i], p_font_size, glyph).x;
- }
- }
- return 0.f;
- }
- real_t Font::draw_char_outline(RID p_canvas_item, const Point2 &p_pos, char32_t p_char, int p_font_size, int p_size, const Color &p_modulate) const {
- if (dirty_rids) {
- _update_rids();
- }
- for (int i = 0; i < rids.size(); i++) {
- if (TS->font_has_char(rids[i], p_char)) {
- int32_t glyph = TS->font_get_glyph_index(rids[i], p_font_size, p_char, 0);
- TS->font_draw_glyph_outline(rids[i], p_canvas_item, p_font_size, p_size, p_pos, glyph, p_modulate);
- return TS->font_get_glyph_advance(rids[i], p_font_size, glyph).x;
- }
- }
- return 0.f;
- }
- // Helper functions.
- bool Font::has_char(char32_t p_char) const {
- if (dirty_rids) {
- _update_rids();
- }
- for (int i = 0; i < rids.size(); i++) {
- if (TS->font_has_char(rids[i], p_char)) {
- return true;
- }
- }
- return false;
- }
- String Font::get_supported_chars() const {
- if (dirty_rids) {
- _update_rids();
- }
- String chars;
- for (int i = 0; i < rids.size(); i++) {
- String data_chars = TS->font_get_supported_chars(rids[i]);
- for (int j = 0; j < data_chars.length(); j++) {
- if (chars.find_char(data_chars[j]) == -1) {
- chars += data_chars[j];
- }
- }
- }
- return chars;
- }
- bool Font::is_language_supported(const String &p_language) const {
- return TS->font_is_language_supported(_get_rid(), p_language);
- }
- bool Font::is_script_supported(const String &p_script) const {
- return TS->font_is_script_supported(_get_rid(), p_script);
- }
- Dictionary Font::get_supported_feature_list() const {
- return TS->font_supported_feature_list(_get_rid());
- }
- Dictionary Font::get_supported_variation_list() const {
- return TS->font_supported_variation_list(_get_rid());
- }
- int64_t Font::get_face_count() const {
- return TS->font_get_face_count(_get_rid());
- }
- Font::Font() {
- cache.set_capacity(64);
- cache_wrap.set_capacity(16);
- }
- Font::~Font() {
- }
- /*************************************************************************/
- /* FontFile */
- /*************************************************************************/
- _FORCE_INLINE_ void FontFile::_clear_cache() {
- for (int i = 0; i < cache.size(); i++) {
- if (cache[i].is_valid()) {
- TS->free_rid(cache[i]);
- cache.write[i] = RID();
- }
- }
- }
- _FORCE_INLINE_ void FontFile::_ensure_rid(int p_cache_index) const {
- if (unlikely(p_cache_index >= cache.size())) {
- cache.resize(p_cache_index + 1);
- }
- if (unlikely(!cache[p_cache_index].is_valid())) {
- cache.write[p_cache_index] = TS->create_font();
- TS->font_set_data_ptr(cache[p_cache_index], data_ptr, data_size);
- TS->font_set_antialiasing(cache[p_cache_index], antialiasing);
- TS->font_set_generate_mipmaps(cache[p_cache_index], mipmaps);
- TS->font_set_multichannel_signed_distance_field(cache[p_cache_index], msdf);
- TS->font_set_msdf_pixel_range(cache[p_cache_index], msdf_pixel_range);
- TS->font_set_msdf_size(cache[p_cache_index], msdf_size);
- TS->font_set_fixed_size(cache[p_cache_index], fixed_size);
- TS->font_set_force_autohinter(cache[p_cache_index], force_autohinter);
- TS->font_set_hinting(cache[p_cache_index], hinting);
- TS->font_set_subpixel_positioning(cache[p_cache_index], subpixel_positioning);
- TS->font_set_oversampling(cache[p_cache_index], oversampling);
- }
- }
- void FontFile::_convert_packed_8bit(Ref<Image> &p_source, int p_page, int p_sz) {
- int w = p_source->get_width();
- int h = p_source->get_height();
- PackedByteArray imgdata = p_source->get_data();
- const uint8_t *r = imgdata.ptr();
- PackedByteArray imgdata_r;
- imgdata_r.resize(w * h * 2);
- uint8_t *wr = imgdata_r.ptrw();
- PackedByteArray imgdata_g;
- imgdata_g.resize(w * h * 2);
- uint8_t *wg = imgdata_g.ptrw();
- PackedByteArray imgdata_b;
- imgdata_b.resize(w * h * 2);
- uint8_t *wb = imgdata_b.ptrw();
- PackedByteArray imgdata_a;
- imgdata_a.resize(w * h * 2);
- uint8_t *wa = imgdata_a.ptrw();
- for (int i = 0; i < h; i++) {
- for (int j = 0; j < w; j++) {
- int ofs_src = (i * w + j) * 4;
- int ofs_dst = (i * w + j) * 2;
- wr[ofs_dst + 0] = 255;
- wr[ofs_dst + 1] = r[ofs_src + 0];
- wg[ofs_dst + 0] = 255;
- wg[ofs_dst + 1] = r[ofs_src + 1];
- wb[ofs_dst + 0] = 255;
- wb[ofs_dst + 1] = r[ofs_src + 2];
- wa[ofs_dst + 0] = 255;
- wa[ofs_dst + 1] = r[ofs_src + 3];
- }
- }
- Ref<Image> img_r = memnew(Image(w, h, 0, Image::FORMAT_LA8, imgdata_r));
- set_texture_image(0, Vector2i(p_sz, 0), p_page * 4 + 0, img_r);
- Ref<Image> img_g = memnew(Image(w, h, 0, Image::FORMAT_LA8, imgdata_g));
- set_texture_image(0, Vector2i(p_sz, 0), p_page * 4 + 1, img_g);
- Ref<Image> img_b = memnew(Image(w, h, 0, Image::FORMAT_LA8, imgdata_b));
- set_texture_image(0, Vector2i(p_sz, 0), p_page * 4 + 2, img_b);
- Ref<Image> img_a = memnew(Image(w, h, 0, Image::FORMAT_LA8, imgdata_a));
- set_texture_image(0, Vector2i(p_sz, 0), p_page * 4 + 3, img_a);
- }
- void FontFile::_convert_packed_4bit(Ref<Image> &p_source, int p_page, int p_sz) {
- int w = p_source->get_width();
- int h = p_source->get_height();
- PackedByteArray imgdata = p_source->get_data();
- const uint8_t *r = imgdata.ptr();
- PackedByteArray imgdata_r;
- imgdata_r.resize(w * h * 2);
- uint8_t *wr = imgdata_r.ptrw();
- PackedByteArray imgdata_g;
- imgdata_g.resize(w * h * 2);
- uint8_t *wg = imgdata_g.ptrw();
- PackedByteArray imgdata_b;
- imgdata_b.resize(w * h * 2);
- uint8_t *wb = imgdata_b.ptrw();
- PackedByteArray imgdata_a;
- imgdata_a.resize(w * h * 2);
- uint8_t *wa = imgdata_a.ptrw();
- PackedByteArray imgdata_ro;
- imgdata_ro.resize(w * h * 2);
- uint8_t *wro = imgdata_ro.ptrw();
- PackedByteArray imgdata_go;
- imgdata_go.resize(w * h * 2);
- uint8_t *wgo = imgdata_go.ptrw();
- PackedByteArray imgdata_bo;
- imgdata_bo.resize(w * h * 2);
- uint8_t *wbo = imgdata_bo.ptrw();
- PackedByteArray imgdata_ao;
- imgdata_ao.resize(w * h * 2);
- uint8_t *wao = imgdata_ao.ptrw();
- for (int i = 0; i < h; i++) {
- for (int j = 0; j < w; j++) {
- int ofs_src = (i * w + j) * 4;
- int ofs_dst = (i * w + j) * 2;
- wr[ofs_dst + 0] = 255;
- wro[ofs_dst + 0] = 255;
- if (r[ofs_src + 0] > 0x0F) {
- wr[ofs_dst + 1] = (r[ofs_src + 0] - 0x0F) * 2;
- wro[ofs_dst + 1] = 0;
- } else {
- wr[ofs_dst + 1] = 0;
- wro[ofs_dst + 1] = r[ofs_src + 0] * 2;
- }
- wg[ofs_dst + 0] = 255;
- wgo[ofs_dst + 0] = 255;
- if (r[ofs_src + 1] > 0x0F) {
- wg[ofs_dst + 1] = (r[ofs_src + 1] - 0x0F) * 2;
- wgo[ofs_dst + 1] = 0;
- } else {
- wg[ofs_dst + 1] = 0;
- wgo[ofs_dst + 1] = r[ofs_src + 1] * 2;
- }
- wb[ofs_dst + 0] = 255;
- wbo[ofs_dst + 0] = 255;
- if (r[ofs_src + 2] > 0x0F) {
- wb[ofs_dst + 1] = (r[ofs_src + 2] - 0x0F) * 2;
- wbo[ofs_dst + 1] = 0;
- } else {
- wb[ofs_dst + 1] = 0;
- wbo[ofs_dst + 1] = r[ofs_src + 2] * 2;
- }
- wa[ofs_dst + 0] = 255;
- wao[ofs_dst + 0] = 255;
- if (r[ofs_src + 3] > 0x0F) {
- wa[ofs_dst + 1] = (r[ofs_src + 3] - 0x0F) * 2;
- wao[ofs_dst + 1] = 0;
- } else {
- wa[ofs_dst + 1] = 0;
- wao[ofs_dst + 1] = r[ofs_src + 3] * 2;
- }
- }
- }
- Ref<Image> img_r = memnew(Image(w, h, 0, Image::FORMAT_LA8, imgdata_r));
- set_texture_image(0, Vector2i(p_sz, 0), p_page * 4 + 0, img_r);
- Ref<Image> img_g = memnew(Image(w, h, 0, Image::FORMAT_LA8, imgdata_g));
- set_texture_image(0, Vector2i(p_sz, 0), p_page * 4 + 1, img_g);
- Ref<Image> img_b = memnew(Image(w, h, 0, Image::FORMAT_LA8, imgdata_b));
- set_texture_image(0, Vector2i(p_sz, 0), p_page * 4 + 2, img_b);
- Ref<Image> img_a = memnew(Image(w, h, 0, Image::FORMAT_LA8, imgdata_a));
- set_texture_image(0, Vector2i(p_sz, 0), p_page * 4 + 3, img_a);
- Ref<Image> img_ro = memnew(Image(w, h, 0, Image::FORMAT_LA8, imgdata_ro));
- set_texture_image(0, Vector2i(p_sz, 1), p_page * 4 + 0, img_ro);
- Ref<Image> img_go = memnew(Image(w, h, 0, Image::FORMAT_LA8, imgdata_go));
- set_texture_image(0, Vector2i(p_sz, 1), p_page * 4 + 1, img_go);
- Ref<Image> img_bo = memnew(Image(w, h, 0, Image::FORMAT_LA8, imgdata_bo));
- set_texture_image(0, Vector2i(p_sz, 1), p_page * 4 + 2, img_bo);
- Ref<Image> img_ao = memnew(Image(w, h, 0, Image::FORMAT_LA8, imgdata_ao));
- set_texture_image(0, Vector2i(p_sz, 1), p_page * 4 + 3, img_ao);
- }
- void FontFile::_convert_rgba_4bit(Ref<Image> &p_source, int p_page, int p_sz) {
- int w = p_source->get_width();
- int h = p_source->get_height();
- PackedByteArray imgdata = p_source->get_data();
- const uint8_t *r = imgdata.ptr();
- PackedByteArray imgdata_g;
- imgdata_g.resize(w * h * 4);
- uint8_t *wg = imgdata_g.ptrw();
- PackedByteArray imgdata_o;
- imgdata_o.resize(w * h * 4);
- uint8_t *wo = imgdata_o.ptrw();
- for (int i = 0; i < h; i++) {
- for (int j = 0; j < w; j++) {
- int ofs = (i * w + j) * 4;
- if (r[ofs + 0] > 0x7F) {
- wg[ofs + 0] = r[ofs + 0];
- wo[ofs + 0] = 0;
- } else {
- wg[ofs + 0] = 0;
- wo[ofs + 0] = r[ofs + 0] * 2;
- }
- if (r[ofs + 1] > 0x7F) {
- wg[ofs + 1] = r[ofs + 1];
- wo[ofs + 1] = 0;
- } else {
- wg[ofs + 1] = 0;
- wo[ofs + 1] = r[ofs + 1] * 2;
- }
- if (r[ofs + 2] > 0x7F) {
- wg[ofs + 2] = r[ofs + 2];
- wo[ofs + 2] = 0;
- } else {
- wg[ofs + 2] = 0;
- wo[ofs + 2] = r[ofs + 2] * 2;
- }
- if (r[ofs + 3] > 0x7F) {
- wg[ofs + 3] = r[ofs + 3];
- wo[ofs + 3] = 0;
- } else {
- wg[ofs + 3] = 0;
- wo[ofs + 3] = r[ofs + 3] * 2;
- }
- }
- }
- Ref<Image> img_g = memnew(Image(w, h, 0, Image::FORMAT_RGBA8, imgdata_g));
- set_texture_image(0, Vector2i(p_sz, 0), p_page, img_g);
- Ref<Image> img_o = memnew(Image(w, h, 0, Image::FORMAT_RGBA8, imgdata_o));
- set_texture_image(0, Vector2i(p_sz, 1), p_page, img_o);
- }
- void FontFile::_convert_mono_8bit(Ref<Image> &p_source, int p_page, int p_ch, int p_sz, int p_ol) {
- int w = p_source->get_width();
- int h = p_source->get_height();
- PackedByteArray imgdata = p_source->get_data();
- const uint8_t *r = imgdata.ptr();
- int size = 4;
- if (p_source->get_format() == Image::FORMAT_L8) {
- size = 1;
- p_ch = 0;
- }
- PackedByteArray imgdata_g;
- imgdata_g.resize(w * h * 2);
- uint8_t *wg = imgdata_g.ptrw();
- for (int i = 0; i < h; i++) {
- for (int j = 0; j < w; j++) {
- int ofs_src = (i * w + j) * size;
- int ofs_dst = (i * w + j) * 2;
- wg[ofs_dst + 0] = 255;
- wg[ofs_dst + 1] = r[ofs_src + p_ch];
- }
- }
- Ref<Image> img_g = memnew(Image(w, h, 0, Image::FORMAT_LA8, imgdata_g));
- set_texture_image(0, Vector2i(p_sz, p_ol), p_page, img_g);
- }
- void FontFile::_convert_mono_4bit(Ref<Image> &p_source, int p_page, int p_ch, int p_sz, int p_ol) {
- int w = p_source->get_width();
- int h = p_source->get_height();
- PackedByteArray imgdata = p_source->get_data();
- const uint8_t *r = imgdata.ptr();
- int size = 4;
- if (p_source->get_format() == Image::FORMAT_L8) {
- size = 1;
- p_ch = 0;
- }
- PackedByteArray imgdata_g;
- imgdata_g.resize(w * h * 2);
- uint8_t *wg = imgdata_g.ptrw();
- PackedByteArray imgdata_o;
- imgdata_o.resize(w * h * 2);
- uint8_t *wo = imgdata_o.ptrw();
- for (int i = 0; i < h; i++) {
- for (int j = 0; j < w; j++) {
- int ofs_src = (i * w + j) * size;
- int ofs_dst = (i * w + j) * 2;
- wg[ofs_dst + 0] = 255;
- wo[ofs_dst + 0] = 255;
- if (r[ofs_src + p_ch] > 0x7F) {
- wg[ofs_dst + 1] = r[ofs_src + p_ch];
- wo[ofs_dst + 1] = 0;
- } else {
- wg[ofs_dst + 1] = 0;
- wo[ofs_dst + 1] = r[ofs_src + p_ch] * 2;
- }
- }
- }
- Ref<Image> img_g = memnew(Image(w, h, 0, Image::FORMAT_LA8, imgdata_g));
- set_texture_image(0, Vector2i(p_sz, 0), p_page, img_g);
- Ref<Image> img_o = memnew(Image(w, h, 0, Image::FORMAT_LA8, imgdata_o));
- set_texture_image(0, Vector2i(p_sz, p_ol), p_page, img_o);
- }
- void FontFile::_bind_methods() {
- ClassDB::bind_method(D_METHOD("load_bitmap_font", "path"), &FontFile::load_bitmap_font);
- ClassDB::bind_method(D_METHOD("load_dynamic_font", "path"), &FontFile::load_dynamic_font);
- ClassDB::bind_method(D_METHOD("set_data", "data"), &FontFile::set_data);
- ClassDB::bind_method(D_METHOD("get_data"), &FontFile::get_data);
- ClassDB::bind_method(D_METHOD("set_font_name", "name"), &FontFile::set_font_name);
- ClassDB::bind_method(D_METHOD("set_font_style_name", "name"), &FontFile::set_font_style_name);
- ClassDB::bind_method(D_METHOD("set_font_style", "style"), &FontFile::set_font_style);
- ClassDB::bind_method(D_METHOD("set_antialiasing", "antialiasing"), &FontFile::set_antialiasing);
- ClassDB::bind_method(D_METHOD("get_antialiasing"), &FontFile::get_antialiasing);
- ClassDB::bind_method(D_METHOD("set_generate_mipmaps", "generate_mipmaps"), &FontFile::set_generate_mipmaps);
- ClassDB::bind_method(D_METHOD("get_generate_mipmaps"), &FontFile::get_generate_mipmaps);
- ClassDB::bind_method(D_METHOD("set_multichannel_signed_distance_field", "msdf"), &FontFile::set_multichannel_signed_distance_field);
- ClassDB::bind_method(D_METHOD("is_multichannel_signed_distance_field"), &FontFile::is_multichannel_signed_distance_field);
- ClassDB::bind_method(D_METHOD("set_msdf_pixel_range", "msdf_pixel_range"), &FontFile::set_msdf_pixel_range);
- ClassDB::bind_method(D_METHOD("get_msdf_pixel_range"), &FontFile::get_msdf_pixel_range);
- ClassDB::bind_method(D_METHOD("set_msdf_size", "msdf_size"), &FontFile::set_msdf_size);
- ClassDB::bind_method(D_METHOD("get_msdf_size"), &FontFile::get_msdf_size);
- ClassDB::bind_method(D_METHOD("set_fixed_size", "fixed_size"), &FontFile::set_fixed_size);
- ClassDB::bind_method(D_METHOD("get_fixed_size"), &FontFile::get_fixed_size);
- ClassDB::bind_method(D_METHOD("set_force_autohinter", "force_autohinter"), &FontFile::set_force_autohinter);
- ClassDB::bind_method(D_METHOD("is_force_autohinter"), &FontFile::is_force_autohinter);
- ClassDB::bind_method(D_METHOD("set_hinting", "hinting"), &FontFile::set_hinting);
- ClassDB::bind_method(D_METHOD("get_hinting"), &FontFile::get_hinting);
- ClassDB::bind_method(D_METHOD("set_subpixel_positioning", "subpixel_positioning"), &FontFile::set_subpixel_positioning);
- ClassDB::bind_method(D_METHOD("get_subpixel_positioning"), &FontFile::get_subpixel_positioning);
- ClassDB::bind_method(D_METHOD("set_oversampling", "oversampling"), &FontFile::set_oversampling);
- ClassDB::bind_method(D_METHOD("get_oversampling"), &FontFile::get_oversampling);
- ClassDB::bind_method(D_METHOD("get_cache_count"), &FontFile::get_cache_count);
- ClassDB::bind_method(D_METHOD("clear_cache"), &FontFile::clear_cache);
- ClassDB::bind_method(D_METHOD("remove_cache", "cache_index"), &FontFile::remove_cache);
- ClassDB::bind_method(D_METHOD("get_size_cache_list", "cache_index"), &FontFile::get_size_cache_list);
- ClassDB::bind_method(D_METHOD("clear_size_cache", "cache_index"), &FontFile::clear_size_cache);
- ClassDB::bind_method(D_METHOD("remove_size_cache", "cache_index", "size"), &FontFile::remove_size_cache);
- ClassDB::bind_method(D_METHOD("set_variation_coordinates", "cache_index", "variation_coordinates"), &FontFile::set_variation_coordinates);
- ClassDB::bind_method(D_METHOD("get_variation_coordinates", "cache_index"), &FontFile::get_variation_coordinates);
- ClassDB::bind_method(D_METHOD("set_embolden", "cache_index", "strength"), &FontFile::set_embolden);
- ClassDB::bind_method(D_METHOD("get_embolden", "cache_index"), &FontFile::get_embolden);
- ClassDB::bind_method(D_METHOD("set_transform", "cache_index", "transform"), &FontFile::set_transform);
- ClassDB::bind_method(D_METHOD("get_transform", "cache_index"), &FontFile::get_transform);
- ClassDB::bind_method(D_METHOD("set_face_index", "cache_index", "face_index"), &FontFile::set_face_index);
- ClassDB::bind_method(D_METHOD("get_face_index", "cache_index"), &FontFile::get_face_index);
- ClassDB::bind_method(D_METHOD("set_cache_ascent", "cache_index", "size", "ascent"), &FontFile::set_cache_ascent);
- ClassDB::bind_method(D_METHOD("get_cache_ascent", "cache_index", "size"), &FontFile::get_cache_ascent);
- ClassDB::bind_method(D_METHOD("set_cache_descent", "cache_index", "size", "descent"), &FontFile::set_cache_descent);
- ClassDB::bind_method(D_METHOD("get_cache_descent", "cache_index", "size"), &FontFile::get_cache_descent);
- ClassDB::bind_method(D_METHOD("set_cache_underline_position", "cache_index", "size", "underline_position"), &FontFile::set_cache_underline_position);
- ClassDB::bind_method(D_METHOD("get_cache_underline_position", "cache_index", "size"), &FontFile::get_cache_underline_position);
- ClassDB::bind_method(D_METHOD("set_cache_underline_thickness", "cache_index", "size", "underline_thickness"), &FontFile::set_cache_underline_thickness);
- ClassDB::bind_method(D_METHOD("get_cache_underline_thickness", "cache_index", "size"), &FontFile::get_cache_underline_thickness);
- ClassDB::bind_method(D_METHOD("set_cache_scale", "cache_index", "size", "scale"), &FontFile::set_cache_scale);
- ClassDB::bind_method(D_METHOD("get_cache_scale", "cache_index", "size"), &FontFile::get_cache_scale);
- ClassDB::bind_method(D_METHOD("get_texture_count", "cache_index", "size"), &FontFile::get_texture_count);
- ClassDB::bind_method(D_METHOD("clear_textures", "cache_index", "size"), &FontFile::clear_textures);
- ClassDB::bind_method(D_METHOD("remove_texture", "cache_index", "size", "texture_index"), &FontFile::remove_texture);
- ClassDB::bind_method(D_METHOD("set_texture_image", "cache_index", "size", "texture_index", "image"), &FontFile::set_texture_image);
- ClassDB::bind_method(D_METHOD("get_texture_image", "cache_index", "size", "texture_index"), &FontFile::get_texture_image);
- ClassDB::bind_method(D_METHOD("set_texture_offsets", "cache_index", "size", "texture_index", "offset"), &FontFile::set_texture_offsets);
- ClassDB::bind_method(D_METHOD("get_texture_offsets", "cache_index", "size", "texture_index"), &FontFile::get_texture_offsets);
- ClassDB::bind_method(D_METHOD("get_glyph_list", "cache_index", "size"), &FontFile::get_glyph_list);
- ClassDB::bind_method(D_METHOD("clear_glyphs", "cache_index", "size"), &FontFile::clear_glyphs);
- ClassDB::bind_method(D_METHOD("remove_glyph", "cache_index", "size", "glyph"), &FontFile::remove_glyph);
- ClassDB::bind_method(D_METHOD("set_glyph_advance", "cache_index", "size", "glyph", "advance"), &FontFile::set_glyph_advance);
- ClassDB::bind_method(D_METHOD("get_glyph_advance", "cache_index", "size", "glyph"), &FontFile::get_glyph_advance);
- ClassDB::bind_method(D_METHOD("set_glyph_offset", "cache_index", "size", "glyph", "offset"), &FontFile::set_glyph_offset);
- ClassDB::bind_method(D_METHOD("get_glyph_offset", "cache_index", "size", "glyph"), &FontFile::get_glyph_offset);
- ClassDB::bind_method(D_METHOD("set_glyph_size", "cache_index", "size", "glyph", "gl_size"), &FontFile::set_glyph_size);
- ClassDB::bind_method(D_METHOD("get_glyph_size", "cache_index", "size", "glyph"), &FontFile::get_glyph_size);
- ClassDB::bind_method(D_METHOD("set_glyph_uv_rect", "cache_index", "size", "glyph", "uv_rect"), &FontFile::set_glyph_uv_rect);
- ClassDB::bind_method(D_METHOD("get_glyph_uv_rect", "cache_index", "size", "glyph"), &FontFile::get_glyph_uv_rect);
- ClassDB::bind_method(D_METHOD("set_glyph_texture_idx", "cache_index", "size", "glyph", "texture_idx"), &FontFile::set_glyph_texture_idx);
- ClassDB::bind_method(D_METHOD("get_glyph_texture_idx", "cache_index", "size", "glyph"), &FontFile::get_glyph_texture_idx);
- ClassDB::bind_method(D_METHOD("get_kerning_list", "cache_index", "size"), &FontFile::get_kerning_list);
- ClassDB::bind_method(D_METHOD("clear_kerning_map", "cache_index", "size"), &FontFile::clear_kerning_map);
- ClassDB::bind_method(D_METHOD("remove_kerning", "cache_index", "size", "glyph_pair"), &FontFile::remove_kerning);
- ClassDB::bind_method(D_METHOD("set_kerning", "cache_index", "size", "glyph_pair", "kerning"), &FontFile::set_kerning);
- ClassDB::bind_method(D_METHOD("get_kerning", "cache_index", "size", "glyph_pair"), &FontFile::get_kerning);
- ClassDB::bind_method(D_METHOD("render_range", "cache_index", "size", "start", "end"), &FontFile::render_range);
- ClassDB::bind_method(D_METHOD("render_glyph", "cache_index", "size", "index"), &FontFile::render_glyph);
- ClassDB::bind_method(D_METHOD("set_language_support_override", "language", "supported"), &FontFile::set_language_support_override);
- ClassDB::bind_method(D_METHOD("get_language_support_override", "language"), &FontFile::get_language_support_override);
- ClassDB::bind_method(D_METHOD("remove_language_support_override", "language"), &FontFile::remove_language_support_override);
- ClassDB::bind_method(D_METHOD("get_language_support_overrides"), &FontFile::get_language_support_overrides);
- ClassDB::bind_method(D_METHOD("set_script_support_override", "script", "supported"), &FontFile::set_script_support_override);
- ClassDB::bind_method(D_METHOD("get_script_support_override", "script"), &FontFile::get_script_support_override);
- ClassDB::bind_method(D_METHOD("remove_script_support_override", "script"), &FontFile::remove_script_support_override);
- ClassDB::bind_method(D_METHOD("get_script_support_overrides"), &FontFile::get_script_support_overrides);
- ClassDB::bind_method(D_METHOD("set_opentype_feature_overrides", "overrides"), &FontFile::set_opentype_feature_overrides);
- ClassDB::bind_method(D_METHOD("get_opentype_feature_overrides"), &FontFile::get_opentype_feature_overrides);
- ClassDB::bind_method(D_METHOD("get_glyph_index", "size", "char", "variation_selector"), &FontFile::get_glyph_index);
- ADD_PROPERTY(PropertyInfo(Variant::PACKED_BYTE_ARRAY, "data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), "set_data", "get_data");
- ADD_PROPERTY(PropertyInfo(Variant::BOOL, "generate_mipmaps", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), "set_generate_mipmaps", "get_generate_mipmaps");
- ADD_PROPERTY(PropertyInfo(Variant::INT, "antialiasing", PROPERTY_HINT_ENUM, "None,Grayscale,LCD sub-pixel", PROPERTY_USAGE_STORAGE), "set_antialiasing", "get_antialiasing");
- ADD_PROPERTY(PropertyInfo(Variant::STRING, "font_name", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), "set_font_name", "get_font_name");
- ADD_PROPERTY(PropertyInfo(Variant::STRING, "style_name", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), "set_font_style_name", "get_font_style_name");
- ADD_PROPERTY(PropertyInfo(Variant::INT, "font_style", PROPERTY_HINT_FLAGS, "Bold,Italic,Fixed Size", PROPERTY_USAGE_STORAGE), "set_font_style", "get_font_style");
- ADD_PROPERTY(PropertyInfo(Variant::INT, "subpixel_positioning", PROPERTY_HINT_ENUM, "Disabled,Auto,One half of a pixel,One quarter of a pixel", PROPERTY_USAGE_STORAGE), "set_subpixel_positioning", "get_subpixel_positioning");
- ADD_PROPERTY(PropertyInfo(Variant::BOOL, "multichannel_signed_distance_field", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), "set_multichannel_signed_distance_field", "is_multichannel_signed_distance_field");
- ADD_PROPERTY(PropertyInfo(Variant::INT, "msdf_pixel_range", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), "set_msdf_pixel_range", "get_msdf_pixel_range");
- ADD_PROPERTY(PropertyInfo(Variant::INT, "msdf_size", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), "set_msdf_size", "get_msdf_size");
- ADD_PROPERTY(PropertyInfo(Variant::BOOL, "force_autohinter", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), "set_force_autohinter", "is_force_autohinter");
- ADD_PROPERTY(PropertyInfo(Variant::INT, "hinting", PROPERTY_HINT_ENUM, "None,Light,Normal", PROPERTY_USAGE_STORAGE), "set_hinting", "get_hinting");
- ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "oversampling", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), "set_oversampling", "get_oversampling");
- ADD_PROPERTY(PropertyInfo(Variant::INT, "fixed_size", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), "set_fixed_size", "get_fixed_size");
- ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "opentype_feature_overrides", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), "set_opentype_feature_overrides", "get_opentype_feature_overrides");
- ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "fallbacks", PROPERTY_HINT_ARRAY_TYPE, vformat("%s/%s:%s", Variant::OBJECT, PROPERTY_HINT_RESOURCE_TYPE, "Font"), PROPERTY_USAGE_STORAGE), "set_fallbacks", "get_fallbacks");
- }
- bool FontFile::_set(const StringName &p_name, const Variant &p_value) {
- Vector<String> tokens = p_name.operator String().split("/");
- #ifndef DISABLE_DEPRECATED
- if (tokens.size() == 1 && tokens[0] == "font_path") {
- // Compatibility, DynamicFontData.
- load_dynamic_font(p_value);
- } else if (tokens.size() == 1 && tokens[0] == "override_oversampling") {
- set_oversampling(p_value);
- }
- if (tokens.size() == 1 && tokens[0] == "font_data") {
- // Compatibility, DynamicFont.
- Ref<Font> f = p_value;
- if (f.is_valid()) {
- fallbacks.push_back(f);
- return true;
- }
- return false;
- } else if (tokens.size() == 2 && tokens[0] == "fallback") {
- // Compatibility, DynamicFont.
- Ref<FontFile> f = p_value;
- if (f.is_valid()) {
- fallbacks.push_back(f);
- return true;
- }
- return false;
- } else if (tokens.size() == 1 && tokens[0] == "textures") {
- // Compatibility, BitmapFont.
- set_fixed_size(16);
- Array textures = p_value;
- for (int i = 0; i < textures.size(); i++) {
- Ref<ImageTexture> tex = textures[i];
- ERR_CONTINUE(!tex.is_valid());
- set_texture_image(0, Vector2i(16, 0), i, tex->get_image());
- }
- } else if (tokens.size() == 1 && tokens[0] == "chars") {
- // Compatibility, BitmapFont.
- set_fixed_size(16);
- PackedInt32Array arr = p_value;
- int len = arr.size();
- ERR_FAIL_COND_V(len % 9, false);
- if (!len) {
- return false;
- }
- int chars = len / 9;
- for (int i = 0; i < chars; i++) {
- const int32_t *data = &arr[i * 9];
- char32_t c = data[0];
- set_glyph_texture_idx(0, Vector2i(16, 0), c, data[1]);
- set_glyph_uv_rect(0, Vector2i(16, 0), c, Rect2(data[2], data[3], data[4], data[5]));
- set_glyph_offset(0, Vector2i(16, 0), c, Size2(data[6], data[7]));
- set_glyph_advance(0, 16, c, Vector2(data[8], 0));
- }
- } else if (tokens.size() == 1 && tokens[0] == "kernings") {
- // Compatibility, BitmapFont.
- set_fixed_size(16);
- PackedInt32Array arr = p_value;
- int len = arr.size();
- ERR_FAIL_COND_V(len % 3, false);
- if (!len) {
- return false;
- }
- for (int i = 0; i < len / 3; i++) {
- const int32_t *data = &arr[i * 3];
- set_kerning(0, 16, Vector2i(data[0], data[1]), Vector2(data[2], 0));
- }
- } else if (tokens.size() == 1 && tokens[0] == "height") {
- // Compatibility, BitmapFont.
- bmp_height = p_value;
- set_fixed_size(16);
- set_cache_descent(0, 16, bmp_height - bmp_ascent);
- } else if (tokens.size() == 1 && tokens[0] == "ascent") {
- // Compatibility, BitmapFont.
- bmp_ascent = p_value;
- set_fixed_size(16);
- set_cache_ascent(0, 16, bmp_ascent);
- set_cache_descent(0, 16, bmp_height - bmp_ascent);
- } else if (tokens.size() == 1 && tokens[0] == "fallback") {
- // Compatibility, BitmapFont.
- Ref<Font> f = p_value;
- if (f.is_valid()) {
- fallbacks.push_back(f);
- return true;
- }
- return false;
- }
- #endif // DISABLE_DEPRECATED
- if (tokens.size() == 2 && tokens[0] == "language_support_override") {
- String lang = tokens[1];
- set_language_support_override(lang, p_value);
- return true;
- } else if (tokens.size() == 2 && tokens[0] == "script_support_override") {
- String script = tokens[1];
- set_script_support_override(script, p_value);
- return true;
- } else if (tokens.size() >= 3 && tokens[0] == "cache") {
- int cache_index = tokens[1].to_int();
- if (tokens.size() == 3 && tokens[2] == "variation_coordinates") {
- set_variation_coordinates(cache_index, p_value);
- return true;
- } else if (tokens.size() == 3 && tokens[2] == "embolden") {
- set_embolden(cache_index, p_value);
- return true;
- } else if (tokens.size() == 3 && tokens[2] == "face_index") {
- set_face_index(cache_index, p_value);
- return true;
- } else if (tokens.size() == 3 && tokens[2] == "transform") {
- set_transform(cache_index, p_value);
- return true;
- }
- if (tokens.size() >= 5) {
- Vector2i sz = Vector2i(tokens[2].to_int(), tokens[3].to_int());
- if (tokens[4] == "ascent") {
- set_cache_ascent(cache_index, sz.x, p_value);
- return true;
- } else if (tokens[4] == "descent") {
- set_cache_descent(cache_index, sz.x, p_value);
- return true;
- } else if (tokens[4] == "underline_position") {
- set_cache_underline_position(cache_index, sz.x, p_value);
- return true;
- } else if (tokens[4] == "underline_thickness") {
- set_cache_underline_thickness(cache_index, sz.x, p_value);
- return true;
- } else if (tokens[4] == "scale") {
- set_cache_scale(cache_index, sz.x, p_value);
- return true;
- } else if (tokens.size() == 7 && tokens[4] == "textures") {
- int texture_index = tokens[5].to_int();
- if (tokens[6] == "image") {
- set_texture_image(cache_index, sz, texture_index, p_value);
- return true;
- } else if (tokens[6] == "offsets") {
- set_texture_offsets(cache_index, sz, texture_index, p_value);
- return true;
- }
- } else if (tokens.size() == 7 && tokens[4] == "glyphs") {
- int32_t glyph_index = tokens[5].to_int();
- if (tokens[6] == "advance") {
- set_glyph_advance(cache_index, sz.x, glyph_index, p_value);
- return true;
- } else if (tokens[6] == "offset") {
- set_glyph_offset(cache_index, sz, glyph_index, p_value);
- return true;
- } else if (tokens[6] == "size") {
- set_glyph_size(cache_index, sz, glyph_index, p_value);
- return true;
- } else if (tokens[6] == "uv_rect") {
- set_glyph_uv_rect(cache_index, sz, glyph_index, p_value);
- return true;
- } else if (tokens[6] == "texture_idx") {
- set_glyph_texture_idx(cache_index, sz, glyph_index, p_value);
- return true;
- }
- } else if (tokens.size() == 7 && tokens[4] == "kerning_overrides") {
- Vector2i gp = Vector2i(tokens[5].to_int(), tokens[6].to_int());
- set_kerning(cache_index, sz.x, gp, p_value);
- return true;
- }
- }
- }
- return false;
- }
- bool FontFile::_get(const StringName &p_name, Variant &r_ret) const {
- Vector<String> tokens = p_name.operator String().split("/");
- if (tokens.size() == 2 && tokens[0] == "language_support_override") {
- String lang = tokens[1];
- r_ret = get_language_support_override(lang);
- return true;
- } else if (tokens.size() == 2 && tokens[0] == "script_support_override") {
- String script = tokens[1];
- r_ret = get_script_support_override(script);
- return true;
- } else if (tokens.size() >= 3 && tokens[0] == "cache") {
- int cache_index = tokens[1].to_int();
- if (tokens.size() == 3 && tokens[2] == "variation_coordinates") {
- r_ret = get_variation_coordinates(cache_index);
- return true;
- } else if (tokens.size() == 3 && tokens[2] == "embolden") {
- r_ret = get_embolden(cache_index);
- return true;
- } else if (tokens.size() == 3 && tokens[2] == "face_index") {
- r_ret = get_face_index(cache_index);
- return true;
- } else if (tokens.size() == 3 && tokens[2] == "transform") {
- r_ret = get_transform(cache_index);
- return true;
- }
- if (tokens.size() >= 5) {
- Vector2i sz = Vector2i(tokens[2].to_int(), tokens[3].to_int());
- if (tokens[4] == "ascent") {
- r_ret = get_cache_ascent(cache_index, sz.x);
- return true;
- } else if (tokens[4] == "descent") {
- r_ret = get_cache_descent(cache_index, sz.x);
- return true;
- } else if (tokens[4] == "underline_position") {
- r_ret = get_cache_underline_position(cache_index, sz.x);
- return true;
- } else if (tokens[4] == "underline_thickness") {
- r_ret = get_cache_underline_thickness(cache_index, sz.x);
- return true;
- } else if (tokens[4] == "scale") {
- r_ret = get_cache_scale(cache_index, sz.x);
- return true;
- } else if (tokens.size() == 7 && tokens[4] == "textures") {
- int texture_index = tokens[5].to_int();
- if (tokens[6] == "image") {
- r_ret = get_texture_image(cache_index, sz, texture_index);
- return true;
- } else if (tokens[6] == "offsets") {
- r_ret = get_texture_offsets(cache_index, sz, texture_index);
- return true;
- }
- } else if (tokens.size() == 7 && tokens[4] == "glyphs") {
- int32_t glyph_index = tokens[5].to_int();
- if (tokens[6] == "advance") {
- r_ret = get_glyph_advance(cache_index, sz.x, glyph_index);
- return true;
- } else if (tokens[6] == "offset") {
- r_ret = get_glyph_offset(cache_index, sz, glyph_index);
- return true;
- } else if (tokens[6] == "size") {
- r_ret = get_glyph_size(cache_index, sz, glyph_index);
- return true;
- } else if (tokens[6] == "uv_rect") {
- r_ret = get_glyph_uv_rect(cache_index, sz, glyph_index);
- return true;
- } else if (tokens[6] == "texture_idx") {
- r_ret = get_glyph_texture_idx(cache_index, sz, glyph_index);
- return true;
- }
- } else if (tokens.size() == 7 && tokens[4] == "kerning_overrides") {
- Vector2i gp = Vector2i(tokens[5].to_int(), tokens[6].to_int());
- r_ret = get_kerning(cache_index, sz.x, gp);
- return true;
- }
- }
- }
- return false;
- }
- void FontFile::_get_property_list(List<PropertyInfo> *p_list) const {
- Vector<String> lang_over = get_language_support_overrides();
- for (int i = 0; i < lang_over.size(); i++) {
- p_list->push_back(PropertyInfo(Variant::BOOL, "language_support_override/" + lang_over[i], PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
- }
- Vector<String> scr_over = get_script_support_overrides();
- for (int i = 0; i < scr_over.size(); i++) {
- p_list->push_back(PropertyInfo(Variant::BOOL, "script_support_override/" + scr_over[i], PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
- }
- for (int i = 0; i < cache.size(); i++) {
- String prefix = "cache/" + itos(i) + "/";
- TypedArray<Vector2i> sizes = get_size_cache_list(i);
- p_list->push_back(PropertyInfo(Variant::DICTIONARY, prefix + "variation_coordinates", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
- p_list->push_back(PropertyInfo(Variant::INT, "face_index", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
- p_list->push_back(PropertyInfo(Variant::FLOAT, "embolden", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
- p_list->push_back(PropertyInfo(Variant::TRANSFORM2D, "transform", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
- for (int j = 0; j < sizes.size(); j++) {
- Vector2i sz = sizes[j];
- String prefix_sz = prefix + itos(sz.x) + "/" + itos(sz.y) + "/";
- if (sz.y == 0) {
- p_list->push_back(PropertyInfo(Variant::FLOAT, prefix_sz + "ascent", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
- p_list->push_back(PropertyInfo(Variant::FLOAT, prefix_sz + "descent", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
- p_list->push_back(PropertyInfo(Variant::FLOAT, prefix_sz + "underline_position", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
- p_list->push_back(PropertyInfo(Variant::FLOAT, prefix_sz + "underline_thickness", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
- p_list->push_back(PropertyInfo(Variant::FLOAT, prefix_sz + "scale", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
- }
- int tx_cnt = get_texture_count(i, sz);
- for (int k = 0; k < tx_cnt; k++) {
- p_list->push_back(PropertyInfo(Variant::PACKED_INT32_ARRAY, prefix_sz + "textures/" + itos(k) + "/offsets", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
- p_list->push_back(PropertyInfo(Variant::OBJECT, prefix_sz + "textures/" + itos(k) + "/image", PROPERTY_HINT_RESOURCE_TYPE, "Image", PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_RESOURCE_NOT_PERSISTENT));
- }
- PackedInt32Array glyphs = get_glyph_list(i, sz);
- for (int k = 0; k < glyphs.size(); k++) {
- const int32_t &gl = glyphs[k];
- if (sz.y == 0) {
- p_list->push_back(PropertyInfo(Variant::VECTOR2, prefix_sz + "glyphs/" + itos(gl) + "/advance", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
- }
- p_list->push_back(PropertyInfo(Variant::VECTOR2, prefix_sz + "glyphs/" + itos(gl) + "/offset", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
- p_list->push_back(PropertyInfo(Variant::VECTOR2, prefix_sz + "glyphs/" + itos(gl) + "/size", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
- p_list->push_back(PropertyInfo(Variant::RECT2, prefix_sz + "glyphs/" + itos(gl) + "/uv_rect", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
- p_list->push_back(PropertyInfo(Variant::INT, prefix_sz + "glyphs/" + itos(gl) + "/texture_idx", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
- }
- if (sz.y == 0) {
- TypedArray<Vector2i> kerning_map = get_kerning_list(i, sz.x);
- for (int k = 0; k < kerning_map.size(); k++) {
- const Vector2i &gl_pair = kerning_map[k];
- p_list->push_back(PropertyInfo(Variant::VECTOR2, prefix_sz + "kerning_overrides/" + itos(gl_pair.x) + "/" + itos(gl_pair.y), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
- }
- }
- }
- }
- }
- void FontFile::reset_state() {
- _clear_cache();
- data.clear();
- data_ptr = nullptr;
- data_size = 0;
- cache.clear();
- antialiasing = TextServer::FONT_ANTIALIASING_GRAY;
- mipmaps = false;
- msdf = false;
- force_autohinter = false;
- hinting = TextServer::HINTING_LIGHT;
- subpixel_positioning = TextServer::SUBPIXEL_POSITIONING_DISABLED;
- msdf_pixel_range = 14;
- msdf_size = 128;
- fixed_size = 0;
- oversampling = 0.f;
- Font::reset_state();
- }
- /*************************************************************************/
- Error FontFile::load_bitmap_font(const String &p_path) {
- reset_state();
- antialiasing = TextServer::FONT_ANTIALIASING_NONE;
- mipmaps = false;
- msdf = false;
- force_autohinter = false;
- hinting = TextServer::HINTING_NONE;
- oversampling = 1.0f;
- Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ);
- ERR_FAIL_COND_V_MSG(f.is_null(), ERR_CANT_CREATE, vformat(RTR("Cannot open font from file: %s."), p_path));
- int base_size = 16;
- int height = 0;
- int ascent = 0;
- int outline = 0;
- BitField<TextServer::FontStyle> st_flags = 0;
- String font_name;
- bool packed = false;
- uint8_t ch[4] = { 0, 0, 0, 0 }; // RGBA
- int first_gl_ch = -1;
- int first_ol_ch = -1;
- int first_cm_ch = -1;
- unsigned char magic[4];
- f->get_buffer((unsigned char *)&magic, 4);
- if (magic[0] == 'B' && magic[1] == 'M' && magic[2] == 'F') {
- // Binary BMFont file.
- ERR_FAIL_COND_V_MSG(magic[3] != 3, ERR_CANT_CREATE, vformat(RTR("Version %d of BMFont is not supported."), (int)magic[3]));
- uint8_t block_type = f->get_8();
- uint32_t block_size = f->get_32();
- while (!f->eof_reached()) {
- uint64_t off = f->get_position();
- switch (block_type) {
- case 1: /* info */ {
- ERR_FAIL_COND_V_MSG(block_size < 15, ERR_CANT_CREATE, RTR("Invalid BMFont info block size."));
- base_size = f->get_16();
- uint8_t flags = f->get_8();
- ERR_FAIL_COND_V_MSG(flags & 0x02, ERR_CANT_CREATE, RTR("Non-unicode version of BMFont is not supported."));
- if (flags & (1 << 3)) {
- st_flags.set_flag(TextServer::FONT_BOLD);
- }
- if (flags & (1 << 2)) {
- st_flags.set_flag(TextServer::FONT_ITALIC);
- }
- f->get_8(); // non-unicode charset, skip
- f->get_16(); // stretch_h, skip
- f->get_8(); // aa, skip
- f->get_32(); // padding, skip
- f->get_16(); // spacing, skip
- outline = f->get_8();
- // font name
- PackedByteArray name_data;
- name_data.resize(block_size - 14);
- f->get_buffer(name_data.ptrw(), block_size - 14);
- font_name = String::utf8((const char *)name_data.ptr(), block_size - 14);
- set_fixed_size(base_size);
- } break;
- case 2: /* common */ {
- ERR_FAIL_COND_V_MSG(block_size != 15, ERR_CANT_CREATE, RTR("Invalid BMFont common block size."));
- height = f->get_16();
- ascent = f->get_16();
- f->get_32(); // scale, skip
- f->get_16(); // pages, skip
- uint8_t flags = f->get_8();
- packed = (flags & 0x01);
- ch[3] = f->get_8();
- ch[0] = f->get_8();
- ch[1] = f->get_8();
- ch[2] = f->get_8();
- for (int i = 0; i < 4; i++) {
- if (ch[i] == 0 && first_gl_ch == -1) {
- first_gl_ch = i;
- }
- if (ch[i] == 1 && first_ol_ch == -1) {
- first_ol_ch = i;
- }
- if (ch[i] == 2 && first_cm_ch == -1) {
- first_cm_ch = i;
- }
- }
- } break;
- case 3: /* pages */ {
- int page = 0;
- CharString cs;
- char32_t c = f->get_8();
- while (!f->eof_reached() && f->get_position() <= off + block_size) {
- if (c == '\0') {
- String base_dir = p_path.get_base_dir();
- String file = base_dir.path_join(String::utf8(cs.ptr(), cs.length()));
- if (RenderingServer::get_singleton() != nullptr) {
- Ref<Image> img;
- img.instantiate();
- Error err = ImageLoader::load_image(file, img);
- ERR_FAIL_COND_V_MSG(err != OK, ERR_FILE_CANT_READ, vformat(RTR("Can't load font texture: %s."), file));
- if (packed) {
- if (ch[3] == 0) { // 4 x 8 bit monochrome, no outline
- outline = 0;
- ERR_FAIL_COND_V_MSG(img->get_format() != Image::FORMAT_RGBA8, ERR_FILE_CANT_READ, RTR("Unsupported BMFont texture format."));
- _convert_packed_8bit(img, page, base_size);
- } else if ((ch[3] == 2) && (outline > 0)) { // 4 x 4 bit monochrome, gl + outline
- ERR_FAIL_COND_V_MSG(img->get_format() != Image::FORMAT_RGBA8, ERR_FILE_CANT_READ, RTR("Unsupported BMFont texture format."));
- _convert_packed_4bit(img, page, base_size);
- } else {
- ERR_FAIL_V_MSG(ERR_CANT_CREATE, RTR("Unsupported BMFont texture format."));
- }
- } else {
- if ((ch[0] == 0) && (ch[1] == 0) && (ch[2] == 0) && (ch[3] == 0)) { // RGBA8 color, no outline
- outline = 0;
- ERR_FAIL_COND_V_MSG(img->get_format() != Image::FORMAT_RGBA8, ERR_FILE_CANT_READ, RTR("Unsupported BMFont texture format."));
- set_texture_image(0, Vector2i(base_size, 0), page, img);
- } else if ((ch[0] == 2) && (ch[1] == 2) && (ch[2] == 2) && (ch[3] == 2) && (outline > 0)) { // RGBA4 color, gl + outline
- ERR_FAIL_COND_V_MSG(img->get_format() != Image::FORMAT_RGBA8, ERR_FILE_CANT_READ, RTR("Unsupported BMFont texture format."));
- _convert_rgba_4bit(img, page, base_size);
- } else if ((first_gl_ch >= 0) && (first_ol_ch >= 0) && (outline > 0)) { // 1 x 8 bit monochrome, gl + outline
- ERR_FAIL_COND_V_MSG(img->get_format() != Image::FORMAT_RGBA8 && img->get_format() != Image::FORMAT_L8, ERR_FILE_CANT_READ, RTR("Unsupported BMFont texture format."));
- _convert_mono_8bit(img, page, first_gl_ch, base_size, 0);
- _convert_mono_8bit(img, page, first_ol_ch, base_size, 1);
- } else if ((first_cm_ch >= 0) && (outline > 0)) { // 1 x 4 bit monochrome, gl + outline
- ERR_FAIL_COND_V_MSG(img->get_format() != Image::FORMAT_RGBA8 && img->get_format() != Image::FORMAT_L8, ERR_FILE_CANT_READ, RTR("Unsupported BMFont texture format."));
- _convert_mono_4bit(img, page, first_cm_ch, base_size, 1);
- } else if (first_gl_ch >= 0) { // 1 x 8 bit monochrome, no outline
- outline = 0;
- ERR_FAIL_COND_V_MSG(img->get_format() != Image::FORMAT_RGBA8 && img->get_format() != Image::FORMAT_L8, ERR_FILE_CANT_READ, RTR("Unsupported BMFont texture format."));
- _convert_mono_8bit(img, page, first_gl_ch, base_size, 0);
- } else {
- ERR_FAIL_V_MSG(ERR_CANT_CREATE, RTR("Unsupported BMFont texture format."));
- }
- }
- }
- page++;
- cs = "";
- } else {
- cs += c;
- }
- c = f->get_8();
- }
- } break;
- case 4: /* chars */ {
- int char_count = block_size / 20;
- for (int i = 0; i < char_count; i++) {
- Vector2 advance;
- Vector2 size;
- Vector2 offset;
- Rect2 uv_rect;
- char32_t idx = f->get_32();
- uv_rect.position.x = (int16_t)f->get_16();
- uv_rect.position.y = (int16_t)f->get_16();
- uv_rect.size.width = (int16_t)f->get_16();
- size.width = uv_rect.size.width;
- uv_rect.size.height = (int16_t)f->get_16();
- size.height = uv_rect.size.height;
- offset.x = (int16_t)f->get_16();
- offset.y = (int16_t)f->get_16() - ascent;
- advance.x = (int16_t)f->get_16();
- if (advance.x < 0) {
- advance.x = size.width + 1;
- }
- int texture_idx = f->get_8();
- uint8_t channel = f->get_8();
- ERR_FAIL_COND_V_MSG(!packed && channel != 15, ERR_CANT_CREATE, RTR("Invalid glyph channel."));
- int ch_off = 0;
- switch (channel) {
- case 1:
- ch_off = 2;
- break; // B
- case 2:
- ch_off = 1;
- break; // G
- case 4:
- ch_off = 0;
- break; // R
- case 8:
- ch_off = 3;
- break; // A
- default:
- ch_off = 0;
- break;
- }
- set_glyph_advance(0, base_size, idx, advance);
- set_glyph_offset(0, Vector2i(base_size, 0), idx, offset);
- set_glyph_size(0, Vector2i(base_size, 0), idx, size);
- set_glyph_uv_rect(0, Vector2i(base_size, 0), idx, uv_rect);
- set_glyph_texture_idx(0, Vector2i(base_size, 0), idx, texture_idx * (packed ? 4 : 1) + ch_off);
- if (outline > 0) {
- set_glyph_offset(0, Vector2i(base_size, 1), idx, offset);
- set_glyph_size(0, Vector2i(base_size, 1), idx, size);
- set_glyph_uv_rect(0, Vector2i(base_size, 1), idx, uv_rect);
- set_glyph_texture_idx(0, Vector2i(base_size, 1), idx, texture_idx * (packed ? 4 : 1) + ch_off);
- }
- }
- } break;
- case 5: /* kerning */ {
- int pair_count = block_size / 10;
- for (int i = 0; i < pair_count; i++) {
- Vector2i kpk;
- kpk.x = f->get_32();
- kpk.y = f->get_32();
- set_kerning(0, base_size, kpk, Vector2((int16_t)f->get_16(), 0));
- }
- } break;
- default: {
- ERR_FAIL_V_MSG(ERR_CANT_CREATE, RTR("Invalid BMFont block type."));
- } break;
- }
- f->seek(off + block_size);
- block_type = f->get_8();
- block_size = f->get_32();
- }
- } else {
- // Text BMFont file.
- f->seek(0);
- while (true) {
- String line = f->get_line();
- int delimiter = line.find(" ");
- String type = line.substr(0, delimiter);
- int pos = delimiter + 1;
- HashMap<String, String> keys;
- while (pos < line.size() && line[pos] == ' ') {
- pos++;
- }
- while (pos < line.size()) {
- int eq = line.find("=", pos);
- if (eq == -1) {
- break;
- }
- String key = line.substr(pos, eq - pos);
- int end = -1;
- String value;
- if (line[eq + 1] == '"') {
- end = line.find("\"", eq + 2);
- if (end == -1) {
- break;
- }
- value = line.substr(eq + 2, end - 1 - eq - 1);
- pos = end + 1;
- } else {
- end = line.find(" ", eq + 1);
- if (end == -1) {
- end = line.size();
- }
- value = line.substr(eq + 1, end - eq);
- pos = end;
- }
- while (pos < line.size() && line[pos] == ' ') {
- pos++;
- }
- keys[key] = value;
- }
- if (type == "info") {
- if (keys.has("size")) {
- base_size = keys["size"].to_int();
- set_fixed_size(base_size);
- }
- if (keys.has("outline")) {
- outline = keys["outline"].to_int();
- }
- if (keys.has("bold")) {
- if (keys["bold"].to_int()) {
- st_flags.set_flag(TextServer::FONT_BOLD);
- }
- }
- if (keys.has("italic")) {
- if (keys["italic"].to_int()) {
- st_flags.set_flag(TextServer::FONT_ITALIC);
- }
- }
- if (keys.has("face")) {
- font_name = keys["face"];
- }
- ERR_FAIL_COND_V_MSG((!keys.has("unicode") || keys["unicode"].to_int() != 1), ERR_CANT_CREATE, RTR("Non-unicode version of BMFont is not supported."));
- } else if (type == "common") {
- if (keys.has("lineHeight")) {
- height = keys["lineHeight"].to_int();
- }
- if (keys.has("base")) {
- ascent = keys["base"].to_int();
- }
- if (keys.has("packed")) {
- packed = (keys["packed"].to_int() == 1);
- }
- if (keys.has("alphaChnl")) {
- ch[3] = keys["alphaChnl"].to_int();
- }
- if (keys.has("redChnl")) {
- ch[0] = keys["redChnl"].to_int();
- }
- if (keys.has("greenChnl")) {
- ch[1] = keys["greenChnl"].to_int();
- }
- if (keys.has("blueChnl")) {
- ch[2] = keys["blueChnl"].to_int();
- }
- for (int i = 0; i < 4; i++) {
- if (ch[i] == 0 && first_gl_ch == -1) {
- first_gl_ch = i;
- }
- if (ch[i] == 1 && first_ol_ch == -1) {
- first_ol_ch = i;
- }
- if (ch[i] == 2 && first_cm_ch == -1) {
- first_cm_ch = i;
- }
- }
- } else if (type == "page") {
- int page = 0;
- if (keys.has("id")) {
- page = keys["id"].to_int();
- }
- if (keys.has("file")) {
- String base_dir = p_path.get_base_dir();
- String file = base_dir.path_join(keys["file"]);
- if (RenderingServer::get_singleton() != nullptr) {
- Ref<Image> img;
- img.instantiate();
- Error err = ImageLoader::load_image(file, img);
- ERR_FAIL_COND_V_MSG(err != OK, ERR_FILE_CANT_READ, vformat(RTR("Can't load font texture: %s."), file));
- if (packed) {
- if (ch[3] == 0) { // 4 x 8 bit monochrome, no outline
- outline = 0;
- ERR_FAIL_COND_V_MSG(img->get_format() != Image::FORMAT_RGBA8, ERR_FILE_CANT_READ, RTR("Unsupported BMFont texture format."));
- _convert_packed_8bit(img, page, base_size);
- } else if ((ch[3] == 2) && (outline > 0)) { // 4 x 4 bit monochrome, gl + outline
- ERR_FAIL_COND_V_MSG(img->get_format() != Image::FORMAT_RGBA8, ERR_FILE_CANT_READ, RTR("Unsupported BMFont texture format."));
- _convert_packed_4bit(img, page, base_size);
- } else {
- ERR_FAIL_V_MSG(ERR_CANT_CREATE, RTR("Unsupported BMFont texture format."));
- }
- } else {
- if ((ch[0] == 0) && (ch[1] == 0) && (ch[2] == 0) && (ch[3] == 0)) { // RGBA8 color, no outline
- outline = 0;
- ERR_FAIL_COND_V_MSG(img->get_format() != Image::FORMAT_RGBA8, ERR_FILE_CANT_READ, RTR("Unsupported BMFont texture format."));
- set_texture_image(0, Vector2i(base_size, 0), page, img);
- } else if ((ch[0] == 2) && (ch[1] == 2) && (ch[2] == 2) && (ch[3] == 2) && (outline > 0)) { // RGBA4 color, gl + outline
- ERR_FAIL_COND_V_MSG(img->get_format() != Image::FORMAT_RGBA8, ERR_FILE_CANT_READ, RTR("Unsupported BMFont texture format."));
- _convert_rgba_4bit(img, page, base_size);
- } else if ((first_gl_ch >= 0) && (first_ol_ch >= 0) && (outline > 0)) { // 1 x 8 bit monochrome, gl + outline
- ERR_FAIL_COND_V_MSG(img->get_format() != Image::FORMAT_RGBA8 && img->get_format() != Image::FORMAT_L8, ERR_FILE_CANT_READ, RTR("Unsupported BMFont texture format."));
- _convert_mono_8bit(img, page, first_gl_ch, base_size, 0);
- _convert_mono_8bit(img, page, first_ol_ch, base_size, 1);
- } else if ((first_cm_ch >= 0) && (outline > 0)) { // 1 x 4 bit monochrome, gl + outline
- ERR_FAIL_COND_V_MSG(img->get_format() != Image::FORMAT_RGBA8 && img->get_format() != Image::FORMAT_L8, ERR_FILE_CANT_READ, RTR("Unsupported BMFont texture format."));
- _convert_mono_4bit(img, page, first_cm_ch, base_size, 1);
- } else if (first_gl_ch >= 0) { // 1 x 8 bit monochrome, no outline
- outline = 0;
- ERR_FAIL_COND_V_MSG(img->get_format() != Image::FORMAT_RGBA8 && img->get_format() != Image::FORMAT_L8, ERR_FILE_CANT_READ, RTR("Unsupported BMFont texture format."));
- _convert_mono_8bit(img, page, first_gl_ch, base_size, 0);
- } else {
- ERR_FAIL_V_MSG(ERR_CANT_CREATE, RTR("Unsupported BMFont texture format."));
- }
- }
- }
- }
- } else if (type == "char") {
- char32_t idx = 0;
- Vector2 advance;
- Vector2 size;
- Vector2 offset;
- Rect2 uv_rect;
- int texture_idx = -1;
- uint8_t channel = 15;
- if (keys.has("id")) {
- idx = keys["id"].to_int();
- }
- if (keys.has("x")) {
- uv_rect.position.x = keys["x"].to_int();
- }
- if (keys.has("y")) {
- uv_rect.position.y = keys["y"].to_int();
- }
- if (keys.has("width")) {
- uv_rect.size.width = keys["width"].to_int();
- size.width = keys["width"].to_int();
- }
- if (keys.has("height")) {
- uv_rect.size.height = keys["height"].to_int();
- size.height = keys["height"].to_int();
- }
- if (keys.has("xoffset")) {
- offset.x = keys["xoffset"].to_int();
- }
- if (keys.has("yoffset")) {
- offset.y = keys["yoffset"].to_int() - ascent;
- }
- if (keys.has("page")) {
- texture_idx = keys["page"].to_int();
- }
- if (keys.has("xadvance")) {
- advance.x = keys["xadvance"].to_int();
- }
- if (advance.x < 0) {
- advance.x = size.width + 1;
- }
- if (keys.has("chnl")) {
- channel = keys["chnl"].to_int();
- }
- ERR_FAIL_COND_V_MSG(!packed && channel != 15, ERR_CANT_CREATE, RTR("Invalid glyph channel."));
- int ch_off = 0;
- switch (channel) {
- case 1:
- ch_off = 2;
- break; // B
- case 2:
- ch_off = 1;
- break; // G
- case 4:
- ch_off = 0;
- break; // R
- case 8:
- ch_off = 3;
- break; // A
- default:
- ch_off = 0;
- break;
- }
- set_glyph_advance(0, base_size, idx, advance);
- set_glyph_offset(0, Vector2i(base_size, 0), idx, offset);
- set_glyph_size(0, Vector2i(base_size, 0), idx, size);
- set_glyph_uv_rect(0, Vector2i(base_size, 0), idx, uv_rect);
- set_glyph_texture_idx(0, Vector2i(base_size, 0), idx, texture_idx * (packed ? 4 : 1) + ch_off);
- if (outline > 0) {
- set_glyph_offset(0, Vector2i(base_size, 1), idx, offset);
- set_glyph_size(0, Vector2i(base_size, 1), idx, size);
- set_glyph_uv_rect(0, Vector2i(base_size, 1), idx, uv_rect);
- set_glyph_texture_idx(0, Vector2i(base_size, 1), idx, texture_idx * (packed ? 4 : 1) + ch_off);
- }
- } else if (type == "kerning") {
- Vector2i kpk;
- if (keys.has("first")) {
- kpk.x = keys["first"].to_int();
- }
- if (keys.has("second")) {
- kpk.y = keys["second"].to_int();
- }
- if (keys.has("amount")) {
- set_kerning(0, base_size, kpk, Vector2(keys["amount"].to_int(), 0));
- }
- }
- if (f->eof_reached()) {
- break;
- }
- }
- }
- set_font_name(font_name);
- set_font_style(st_flags);
- set_cache_ascent(0, base_size, ascent);
- set_cache_descent(0, base_size, height - ascent);
- return OK;
- }
- Error FontFile::load_dynamic_font(const String &p_path) {
- reset_state();
- Vector<uint8_t> data = FileAccess::get_file_as_array(p_path);
- set_data(data);
- return OK;
- }
- void FontFile::set_data_ptr(const uint8_t *p_data, size_t p_size) {
- data.clear();
- data_ptr = p_data;
- data_size = p_size;
- for (int i = 0; i < cache.size(); i++) {
- if (cache[i].is_valid()) {
- TS->font_set_data_ptr(cache[i], data_ptr, data_size);
- }
- }
- }
- void FontFile::set_data(const PackedByteArray &p_data) {
- data = p_data;
- data_ptr = data.ptr();
- data_size = data.size();
- for (int i = 0; i < cache.size(); i++) {
- if (cache[i].is_valid()) {
- TS->font_set_data_ptr(cache[i], data_ptr, data_size);
- }
- }
- }
- PackedByteArray FontFile::get_data() const {
- if (unlikely((size_t)data.size() != data_size)) {
- PackedByteArray *data_w = const_cast<PackedByteArray *>(&data);
- data_w->resize(data_size);
- memcpy(data_w->ptrw(), data_ptr, data_size);
- }
- return data;
- }
- void FontFile::set_font_name(const String &p_name) {
- _ensure_rid(0);
- TS->font_set_name(cache[0], p_name);
- }
- void FontFile::set_font_style_name(const String &p_name) {
- _ensure_rid(0);
- TS->font_set_style_name(cache[0], p_name);
- }
- void FontFile::set_font_style(BitField<TextServer::FontStyle> p_style) {
- _ensure_rid(0);
- TS->font_set_style(cache[0], p_style);
- }
- void FontFile::set_antialiasing(TextServer::FontAntialiasing p_antialiasing) {
- if (antialiasing != p_antialiasing) {
- antialiasing = p_antialiasing;
- for (int i = 0; i < cache.size(); i++) {
- _ensure_rid(i);
- TS->font_set_antialiasing(cache[i], antialiasing);
- }
- emit_changed();
- }
- }
- TextServer::FontAntialiasing FontFile::get_antialiasing() const {
- return antialiasing;
- }
- void FontFile::set_generate_mipmaps(bool p_generate_mipmaps) {
- if (mipmaps != p_generate_mipmaps) {
- mipmaps = p_generate_mipmaps;
- for (int i = 0; i < cache.size(); i++) {
- _ensure_rid(i);
- TS->font_set_generate_mipmaps(cache[i], mipmaps);
- }
- emit_changed();
- }
- }
- bool FontFile::get_generate_mipmaps() const {
- return mipmaps;
- }
- void FontFile::set_multichannel_signed_distance_field(bool p_msdf) {
- if (msdf != p_msdf) {
- msdf = p_msdf;
- for (int i = 0; i < cache.size(); i++) {
- _ensure_rid(i);
- TS->font_set_multichannel_signed_distance_field(cache[i], msdf);
- }
- emit_changed();
- }
- }
- bool FontFile::is_multichannel_signed_distance_field() const {
- return msdf;
- }
- void FontFile::set_msdf_pixel_range(int p_msdf_pixel_range) {
- if (msdf_pixel_range != p_msdf_pixel_range) {
- msdf_pixel_range = p_msdf_pixel_range;
- for (int i = 0; i < cache.size(); i++) {
- _ensure_rid(i);
- TS->font_set_msdf_pixel_range(cache[i], msdf_pixel_range);
- }
- emit_changed();
- }
- }
- int FontFile::get_msdf_pixel_range() const {
- return msdf_pixel_range;
- }
- void FontFile::set_msdf_size(int p_msdf_size) {
- if (msdf_size != p_msdf_size) {
- msdf_size = p_msdf_size;
- for (int i = 0; i < cache.size(); i++) {
- _ensure_rid(i);
- TS->font_set_msdf_size(cache[i], msdf_size);
- }
- emit_changed();
- }
- }
- int FontFile::get_msdf_size() const {
- return msdf_size;
- }
- void FontFile::set_fixed_size(int p_fixed_size) {
- if (fixed_size != p_fixed_size) {
- fixed_size = p_fixed_size;
- for (int i = 0; i < cache.size(); i++) {
- _ensure_rid(i);
- TS->font_set_fixed_size(cache[i], fixed_size);
- }
- emit_changed();
- }
- }
- int FontFile::get_fixed_size() const {
- return fixed_size;
- }
- void FontFile::set_force_autohinter(bool p_force_autohinter) {
- if (force_autohinter != p_force_autohinter) {
- force_autohinter = p_force_autohinter;
- for (int i = 0; i < cache.size(); i++) {
- _ensure_rid(i);
- TS->font_set_force_autohinter(cache[i], force_autohinter);
- }
- emit_changed();
- }
- }
- bool FontFile::is_force_autohinter() const {
- return force_autohinter;
- }
- void FontFile::set_hinting(TextServer::Hinting p_hinting) {
- if (hinting != p_hinting) {
- hinting = p_hinting;
- for (int i = 0; i < cache.size(); i++) {
- _ensure_rid(i);
- TS->font_set_hinting(cache[i], hinting);
- }
- emit_changed();
- }
- }
- TextServer::Hinting FontFile::get_hinting() const {
- return hinting;
- }
- void FontFile::set_subpixel_positioning(TextServer::SubpixelPositioning p_subpixel) {
- if (subpixel_positioning != p_subpixel) {
- subpixel_positioning = p_subpixel;
- for (int i = 0; i < cache.size(); i++) {
- _ensure_rid(i);
- TS->font_set_subpixel_positioning(cache[i], subpixel_positioning);
- }
- emit_changed();
- }
- }
- TextServer::SubpixelPositioning FontFile::get_subpixel_positioning() const {
- return subpixel_positioning;
- }
- void FontFile::set_oversampling(real_t p_oversampling) {
- if (oversampling != p_oversampling) {
- oversampling = p_oversampling;
- for (int i = 0; i < cache.size(); i++) {
- _ensure_rid(i);
- TS->font_set_oversampling(cache[i], oversampling);
- }
- emit_changed();
- }
- }
- real_t FontFile::get_oversampling() const {
- return oversampling;
- }
- RID FontFile::find_variation(const Dictionary &p_variation_coordinates, int p_face_index, float p_strength, Transform2D p_transform) const {
- // Find existing variation cache.
- const Dictionary &supported_coords = get_supported_variation_list();
- for (int i = 0; i < cache.size(); i++) {
- if (cache[i].is_valid()) {
- const Dictionary &cache_var = TS->font_get_variation_coordinates(cache[i]);
- bool match = true;
- match = match && (TS->font_get_face_index(cache[i]) == p_face_index);
- match = match && (TS->font_get_embolden(cache[i]) == p_strength);
- match = match && (TS->font_get_transform(cache[i]) == p_transform);
- for (const Variant *V = supported_coords.next(nullptr); V && match; V = supported_coords.next(V)) {
- const Vector3 &def = supported_coords[*V];
- real_t c_v = def.z;
- if (cache_var.has(*V)) {
- real_t val = cache_var[*V];
- c_v = CLAMP(val, def.x, def.y);
- }
- if (cache_var.has(TS->tag_to_name(*V))) {
- real_t val = cache_var[TS->tag_to_name(*V)];
- c_v = CLAMP(val, def.x, def.y);
- }
- real_t s_v = def.z;
- if (p_variation_coordinates.has(*V)) {
- real_t val = p_variation_coordinates[*V];
- s_v = CLAMP(val, def.x, def.y);
- }
- if (p_variation_coordinates.has(TS->tag_to_name(*V))) {
- real_t val = p_variation_coordinates[TS->tag_to_name(*V)];
- s_v = CLAMP(val, def.x, def.y);
- }
- match = match && (c_v == s_v);
- }
- if (match) {
- return cache[i];
- }
- }
- }
- // Create new variation cache.
- int idx = cache.size();
- _ensure_rid(idx);
- TS->font_set_variation_coordinates(cache[idx], p_variation_coordinates);
- TS->font_set_face_index(cache[idx], p_face_index);
- TS->font_set_embolden(cache[idx], p_strength);
- TS->font_set_transform(cache[idx], p_transform);
- return cache[idx];
- }
- RID FontFile::_get_rid() const {
- _ensure_rid(0);
- return cache[0];
- }
- int FontFile::get_cache_count() const {
- return cache.size();
- }
- void FontFile::clear_cache() {
- _clear_cache();
- cache.clear();
- emit_changed();
- }
- void FontFile::remove_cache(int p_cache_index) {
- ERR_FAIL_INDEX(p_cache_index, cache.size());
- if (cache[p_cache_index].is_valid()) {
- TS->free_rid(cache.write[p_cache_index]);
- }
- cache.remove_at(p_cache_index);
- emit_changed();
- }
- TypedArray<Vector2i> FontFile::get_size_cache_list(int p_cache_index) const {
- ERR_FAIL_COND_V(p_cache_index < 0, Array());
- _ensure_rid(p_cache_index);
- return TS->font_get_size_cache_list(cache[p_cache_index]);
- }
- void FontFile::clear_size_cache(int p_cache_index) {
- ERR_FAIL_COND(p_cache_index < 0);
- _ensure_rid(p_cache_index);
- TS->font_clear_size_cache(cache[p_cache_index]);
- }
- void FontFile::remove_size_cache(int p_cache_index, const Vector2i &p_size) {
- ERR_FAIL_COND(p_cache_index < 0);
- _ensure_rid(p_cache_index);
- TS->font_remove_size_cache(cache[p_cache_index], p_size);
- }
- void FontFile::set_variation_coordinates(int p_cache_index, const Dictionary &p_variation_coordinates) {
- ERR_FAIL_COND(p_cache_index < 0);
- _ensure_rid(p_cache_index);
- TS->font_set_variation_coordinates(cache[p_cache_index], p_variation_coordinates);
- }
- Dictionary FontFile::get_variation_coordinates(int p_cache_index) const {
- ERR_FAIL_COND_V(p_cache_index < 0, Dictionary());
- _ensure_rid(p_cache_index);
- return TS->font_get_variation_coordinates(cache[p_cache_index]);
- }
- void FontFile::set_embolden(int p_cache_index, float p_strength) {
- ERR_FAIL_COND(p_cache_index < 0);
- _ensure_rid(p_cache_index);
- TS->font_set_embolden(cache[p_cache_index], p_strength);
- }
- float FontFile::get_embolden(int p_cache_index) const {
- ERR_FAIL_COND_V(p_cache_index < 0, 0.f);
- _ensure_rid(p_cache_index);
- return TS->font_get_embolden(cache[p_cache_index]);
- }
- void FontFile::set_transform(int p_cache_index, Transform2D p_transform) {
- ERR_FAIL_COND(p_cache_index < 0);
- _ensure_rid(p_cache_index);
- TS->font_set_transform(cache[p_cache_index], p_transform);
- }
- Transform2D FontFile::get_transform(int p_cache_index) const {
- ERR_FAIL_COND_V(p_cache_index < 0, Transform2D());
- _ensure_rid(p_cache_index);
- return TS->font_get_transform(cache[p_cache_index]);
- }
- void FontFile::set_face_index(int p_cache_index, int64_t p_index) {
- ERR_FAIL_COND(p_cache_index < 0);
- ERR_FAIL_COND(p_index < 0);
- ERR_FAIL_COND(p_index >= 0x7FFF);
- _ensure_rid(p_cache_index);
- TS->font_set_face_index(cache[p_cache_index], p_index);
- }
- int64_t FontFile::get_face_index(int p_cache_index) const {
- ERR_FAIL_COND_V(p_cache_index < 0, 0);
- _ensure_rid(p_cache_index);
- return TS->font_get_face_index(cache[p_cache_index]);
- }
- void FontFile::set_cache_ascent(int p_cache_index, int p_size, real_t p_ascent) {
- ERR_FAIL_COND(p_cache_index < 0);
- _ensure_rid(p_cache_index);
- TS->font_set_ascent(cache[p_cache_index], p_size, p_ascent);
- }
- real_t FontFile::get_cache_ascent(int p_cache_index, int p_size) const {
- ERR_FAIL_COND_V(p_cache_index < 0, 0.f);
- _ensure_rid(p_cache_index);
- return TS->font_get_ascent(cache[p_cache_index], p_size);
- }
- void FontFile::set_cache_descent(int p_cache_index, int p_size, real_t p_descent) {
- ERR_FAIL_COND(p_cache_index < 0);
- _ensure_rid(p_cache_index);
- TS->font_set_descent(cache[p_cache_index], p_size, p_descent);
- }
- real_t FontFile::get_cache_descent(int p_cache_index, int p_size) const {
- ERR_FAIL_COND_V(p_cache_index < 0, 0.f);
- _ensure_rid(p_cache_index);
- return TS->font_get_descent(cache[p_cache_index], p_size);
- }
- void FontFile::set_cache_underline_position(int p_cache_index, int p_size, real_t p_underline_position) {
- ERR_FAIL_COND(p_cache_index < 0);
- _ensure_rid(p_cache_index);
- TS->font_set_underline_position(cache[p_cache_index], p_size, p_underline_position);
- }
- real_t FontFile::get_cache_underline_position(int p_cache_index, int p_size) const {
- ERR_FAIL_COND_V(p_cache_index < 0, 0.f);
- _ensure_rid(p_cache_index);
- return TS->font_get_underline_position(cache[p_cache_index], p_size);
- }
- void FontFile::set_cache_underline_thickness(int p_cache_index, int p_size, real_t p_underline_thickness) {
- ERR_FAIL_COND(p_cache_index < 0);
- _ensure_rid(p_cache_index);
- TS->font_set_underline_thickness(cache[p_cache_index], p_size, p_underline_thickness);
- }
- real_t FontFile::get_cache_underline_thickness(int p_cache_index, int p_size) const {
- ERR_FAIL_COND_V(p_cache_index < 0, 0.f);
- _ensure_rid(p_cache_index);
- return TS->font_get_underline_thickness(cache[p_cache_index], p_size);
- }
- void FontFile::set_cache_scale(int p_cache_index, int p_size, real_t p_scale) {
- ERR_FAIL_COND(p_cache_index < 0);
- _ensure_rid(p_cache_index);
- TS->font_set_scale(cache[p_cache_index], p_size, p_scale);
- }
- real_t FontFile::get_cache_scale(int p_cache_index, int p_size) const {
- ERR_FAIL_COND_V(p_cache_index < 0, 0.f);
- _ensure_rid(p_cache_index);
- return TS->font_get_scale(cache[p_cache_index], p_size);
- }
- int FontFile::get_texture_count(int p_cache_index, const Vector2i &p_size) const {
- ERR_FAIL_COND_V(p_cache_index < 0, 0);
- _ensure_rid(p_cache_index);
- return TS->font_get_texture_count(cache[p_cache_index], p_size);
- }
- void FontFile::clear_textures(int p_cache_index, const Vector2i &p_size) {
- ERR_FAIL_COND(p_cache_index < 0);
- _ensure_rid(p_cache_index);
- TS->font_clear_textures(cache[p_cache_index], p_size);
- }
- void FontFile::remove_texture(int p_cache_index, const Vector2i &p_size, int p_texture_index) {
- ERR_FAIL_COND(p_cache_index < 0);
- _ensure_rid(p_cache_index);
- TS->font_remove_texture(cache[p_cache_index], p_size, p_texture_index);
- }
- void FontFile::set_texture_image(int p_cache_index, const Vector2i &p_size, int p_texture_index, const Ref<Image> &p_image) {
- ERR_FAIL_COND(p_cache_index < 0);
- _ensure_rid(p_cache_index);
- TS->font_set_texture_image(cache[p_cache_index], p_size, p_texture_index, p_image);
- }
- Ref<Image> FontFile::get_texture_image(int p_cache_index, const Vector2i &p_size, int p_texture_index) const {
- ERR_FAIL_COND_V(p_cache_index < 0, Ref<Image>());
- _ensure_rid(p_cache_index);
- return TS->font_get_texture_image(cache[p_cache_index], p_size, p_texture_index);
- }
- void FontFile::set_texture_offsets(int p_cache_index, const Vector2i &p_size, int p_texture_index, const PackedInt32Array &p_offset) {
- ERR_FAIL_COND(p_cache_index < 0);
- _ensure_rid(p_cache_index);
- TS->font_set_texture_offsets(cache[p_cache_index], p_size, p_texture_index, p_offset);
- }
- PackedInt32Array FontFile::get_texture_offsets(int p_cache_index, const Vector2i &p_size, int p_texture_index) const {
- ERR_FAIL_COND_V(p_cache_index < 0, PackedInt32Array());
- _ensure_rid(p_cache_index);
- return TS->font_get_texture_offsets(cache[p_cache_index], p_size, p_texture_index);
- }
- PackedInt32Array FontFile::get_glyph_list(int p_cache_index, const Vector2i &p_size) const {
- ERR_FAIL_COND_V(p_cache_index < 0, PackedInt32Array());
- _ensure_rid(p_cache_index);
- return TS->font_get_glyph_list(cache[p_cache_index], p_size);
- }
- void FontFile::clear_glyphs(int p_cache_index, const Vector2i &p_size) {
- ERR_FAIL_COND(p_cache_index < 0);
- _ensure_rid(p_cache_index);
- TS->font_clear_glyphs(cache[p_cache_index], p_size);
- }
- void FontFile::remove_glyph(int p_cache_index, const Vector2i &p_size, int32_t p_glyph) {
- ERR_FAIL_COND(p_cache_index < 0);
- _ensure_rid(p_cache_index);
- TS->font_remove_glyph(cache[p_cache_index], p_size, p_glyph);
- }
- void FontFile::set_glyph_advance(int p_cache_index, int p_size, int32_t p_glyph, const Vector2 &p_advance) {
- ERR_FAIL_COND(p_cache_index < 0);
- _ensure_rid(p_cache_index);
- TS->font_set_glyph_advance(cache[p_cache_index], p_size, p_glyph, p_advance);
- }
- Vector2 FontFile::get_glyph_advance(int p_cache_index, int p_size, int32_t p_glyph) const {
- ERR_FAIL_COND_V(p_cache_index < 0, Vector2());
- _ensure_rid(p_cache_index);
- return TS->font_get_glyph_advance(cache[p_cache_index], p_size, p_glyph);
- }
- void FontFile::set_glyph_offset(int p_cache_index, const Vector2i &p_size, int32_t p_glyph, const Vector2 &p_offset) {
- ERR_FAIL_COND(p_cache_index < 0);
- _ensure_rid(p_cache_index);
- TS->font_set_glyph_offset(cache[p_cache_index], p_size, p_glyph, p_offset);
- }
- Vector2 FontFile::get_glyph_offset(int p_cache_index, const Vector2i &p_size, int32_t p_glyph) const {
- ERR_FAIL_COND_V(p_cache_index < 0, Vector2());
- _ensure_rid(p_cache_index);
- return TS->font_get_glyph_offset(cache[p_cache_index], p_size, p_glyph);
- }
- void FontFile::set_glyph_size(int p_cache_index, const Vector2i &p_size, int32_t p_glyph, const Vector2 &p_gl_size) {
- ERR_FAIL_COND(p_cache_index < 0);
- _ensure_rid(p_cache_index);
- TS->font_set_glyph_size(cache[p_cache_index], p_size, p_glyph, p_gl_size);
- }
- Vector2 FontFile::get_glyph_size(int p_cache_index, const Vector2i &p_size, int32_t p_glyph) const {
- ERR_FAIL_COND_V(p_cache_index < 0, Vector2());
- _ensure_rid(p_cache_index);
- return TS->font_get_glyph_size(cache[p_cache_index], p_size, p_glyph);
- }
- void FontFile::set_glyph_uv_rect(int p_cache_index, const Vector2i &p_size, int32_t p_glyph, const Rect2 &p_uv_rect) {
- ERR_FAIL_COND(p_cache_index < 0);
- _ensure_rid(p_cache_index);
- TS->font_set_glyph_uv_rect(cache[p_cache_index], p_size, p_glyph, p_uv_rect);
- }
- Rect2 FontFile::get_glyph_uv_rect(int p_cache_index, const Vector2i &p_size, int32_t p_glyph) const {
- ERR_FAIL_COND_V(p_cache_index < 0, Rect2());
- _ensure_rid(p_cache_index);
- return TS->font_get_glyph_uv_rect(cache[p_cache_index], p_size, p_glyph);
- }
- void FontFile::set_glyph_texture_idx(int p_cache_index, const Vector2i &p_size, int32_t p_glyph, int p_texture_idx) {
- ERR_FAIL_COND(p_cache_index < 0);
- _ensure_rid(p_cache_index);
- TS->font_set_glyph_texture_idx(cache[p_cache_index], p_size, p_glyph, p_texture_idx);
- }
- int FontFile::get_glyph_texture_idx(int p_cache_index, const Vector2i &p_size, int32_t p_glyph) const {
- ERR_FAIL_COND_V(p_cache_index < 0, 0);
- _ensure_rid(p_cache_index);
- return TS->font_get_glyph_texture_idx(cache[p_cache_index], p_size, p_glyph);
- }
- TypedArray<Vector2i> FontFile::get_kerning_list(int p_cache_index, int p_size) const {
- ERR_FAIL_COND_V(p_cache_index < 0, Array());
- _ensure_rid(p_cache_index);
- return TS->font_get_kerning_list(cache[p_cache_index], p_size);
- }
- void FontFile::clear_kerning_map(int p_cache_index, int p_size) {
- ERR_FAIL_COND(p_cache_index < 0);
- _ensure_rid(p_cache_index);
- TS->font_clear_kerning_map(cache[p_cache_index], p_size);
- }
- void FontFile::remove_kerning(int p_cache_index, int p_size, const Vector2i &p_glyph_pair) {
- ERR_FAIL_COND(p_cache_index < 0);
- _ensure_rid(p_cache_index);
- TS->font_remove_kerning(cache[p_cache_index], p_size, p_glyph_pair);
- }
- void FontFile::set_kerning(int p_cache_index, int p_size, const Vector2i &p_glyph_pair, const Vector2 &p_kerning) {
- ERR_FAIL_COND(p_cache_index < 0);
- _ensure_rid(p_cache_index);
- TS->font_set_kerning(cache[p_cache_index], p_size, p_glyph_pair, p_kerning);
- }
- Vector2 FontFile::get_kerning(int p_cache_index, int p_size, const Vector2i &p_glyph_pair) const {
- ERR_FAIL_COND_V(p_cache_index < 0, Vector2());
- _ensure_rid(p_cache_index);
- return TS->font_get_kerning(cache[p_cache_index], p_size, p_glyph_pair);
- }
- void FontFile::render_range(int p_cache_index, const Vector2i &p_size, char32_t p_start, char32_t p_end) {
- ERR_FAIL_COND(p_cache_index < 0);
- _ensure_rid(p_cache_index);
- TS->font_render_range(cache[p_cache_index], p_size, p_start, p_end);
- }
- void FontFile::render_glyph(int p_cache_index, const Vector2i &p_size, int32_t p_index) {
- ERR_FAIL_COND(p_cache_index < 0);
- _ensure_rid(p_cache_index);
- TS->font_render_glyph(cache[p_cache_index], p_size, p_index);
- }
- void FontFile::set_language_support_override(const String &p_language, bool p_supported) {
- _ensure_rid(0);
- TS->font_set_language_support_override(cache[0], p_language, p_supported);
- }
- bool FontFile::get_language_support_override(const String &p_language) const {
- _ensure_rid(0);
- return TS->font_get_language_support_override(cache[0], p_language);
- }
- void FontFile::remove_language_support_override(const String &p_language) {
- _ensure_rid(0);
- TS->font_remove_language_support_override(cache[0], p_language);
- }
- Vector<String> FontFile::get_language_support_overrides() const {
- _ensure_rid(0);
- return TS->font_get_language_support_overrides(cache[0]);
- }
- void FontFile::set_script_support_override(const String &p_script, bool p_supported) {
- _ensure_rid(0);
- TS->font_set_script_support_override(cache[0], p_script, p_supported);
- }
- bool FontFile::get_script_support_override(const String &p_script) const {
- _ensure_rid(0);
- return TS->font_get_script_support_override(cache[0], p_script);
- }
- void FontFile::remove_script_support_override(const String &p_script) {
- _ensure_rid(0);
- TS->font_remove_script_support_override(cache[0], p_script);
- }
- Vector<String> FontFile::get_script_support_overrides() const {
- _ensure_rid(0);
- return TS->font_get_script_support_overrides(cache[0]);
- }
- void FontFile::set_opentype_feature_overrides(const Dictionary &p_overrides) {
- _ensure_rid(0);
- TS->font_set_opentype_feature_overrides(cache[0], p_overrides);
- }
- Dictionary FontFile::get_opentype_feature_overrides() const {
- _ensure_rid(0);
- return TS->font_get_opentype_feature_overrides(cache[0]);
- }
- int32_t FontFile::get_glyph_index(int p_size, char32_t p_char, char32_t p_variation_selector) const {
- _ensure_rid(0);
- return TS->font_get_glyph_index(cache[0], p_size, p_char, p_variation_selector);
- }
- FontFile::FontFile() {
- }
- FontFile::~FontFile() {
- _clear_cache();
- }
- /*************************************************************************/
- /* FontVariation */
- /*************************************************************************/
- void FontVariation::_bind_methods() {
- ClassDB::bind_method(D_METHOD("set_base_font", "font"), &FontVariation::set_base_font);
- ClassDB::bind_method(D_METHOD("get_base_font"), &FontVariation::get_base_font);
- ClassDB::bind_method(D_METHOD("set_variation_opentype", "coords"), &FontVariation::set_variation_opentype);
- ClassDB::bind_method(D_METHOD("get_variation_opentype"), &FontVariation::get_variation_opentype);
- ClassDB::bind_method(D_METHOD("set_variation_embolden", "strength"), &FontVariation::set_variation_embolden);
- ClassDB::bind_method(D_METHOD("get_variation_embolden"), &FontVariation::get_variation_embolden);
- ClassDB::bind_method(D_METHOD("set_variation_face_index", "face_index"), &FontVariation::set_variation_face_index);
- ClassDB::bind_method(D_METHOD("get_variation_face_index"), &FontVariation::get_variation_face_index);
- ClassDB::bind_method(D_METHOD("set_variation_transform", "transform"), &FontVariation::set_variation_transform);
- ClassDB::bind_method(D_METHOD("get_variation_transform"), &FontVariation::get_variation_transform);
- ClassDB::bind_method(D_METHOD("set_opentype_features", "features"), &FontVariation::set_opentype_features);
- ClassDB::bind_method(D_METHOD("set_spacing", "spacing", "value"), &FontVariation::set_spacing);
- ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "base_font", PROPERTY_HINT_RESOURCE_TYPE, "Font"), "set_base_font", "get_base_font");
- ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "fallbacks", PROPERTY_HINT_ARRAY_TYPE, vformat("%s/%s:%s", Variant::OBJECT, PROPERTY_HINT_RESOURCE_TYPE, "Font")), "set_fallbacks", "get_fallbacks");
- ADD_GROUP("Variation", "variation");
- ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "variation_opentype"), "set_variation_opentype", "get_variation_opentype");
- ADD_PROPERTY(PropertyInfo(Variant::INT, "variation_face_index"), "set_variation_face_index", "get_variation_face_index");
- ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "variation_embolden", PROPERTY_HINT_RANGE, "-2,2,0.01"), "set_variation_embolden", "get_variation_embolden");
- ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM2D, "variation_transform", PROPERTY_HINT_NONE, "suffix:px"), "set_variation_transform", "get_variation_transform");
- ADD_GROUP("OpenType Features", "opentype");
- ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "opentype_features"), "set_opentype_features", "get_opentype_features");
- ADD_GROUP("Extra Spacing", "spacing");
- ADD_PROPERTYI(PropertyInfo(Variant::INT, "spacing_glyph", PROPERTY_HINT_NONE, "suffix:px"), "set_spacing", "get_spacing", TextServer::SPACING_GLYPH);
- ADD_PROPERTYI(PropertyInfo(Variant::INT, "spacing_space", PROPERTY_HINT_NONE, "suffix:px"), "set_spacing", "get_spacing", TextServer::SPACING_SPACE);
- ADD_PROPERTYI(PropertyInfo(Variant::INT, "spacing_top", PROPERTY_HINT_NONE, "suffix:px"), "set_spacing", "get_spacing", TextServer::SPACING_TOP);
- ADD_PROPERTYI(PropertyInfo(Variant::INT, "spacing_bottom", PROPERTY_HINT_NONE, "suffix:px"), "set_spacing", "get_spacing", TextServer::SPACING_BOTTOM);
- }
- void FontVariation::_update_rids() const {
- Ref<Font> f = _get_base_font_or_default();
- rids.clear();
- if (fallbacks.is_empty() && f.is_valid()) {
- RID rid = _get_rid();
- if (rid.is_valid()) {
- rids.push_back(rid);
- }
- const TypedArray<Font> &base_fallbacks = f->get_fallbacks();
- for (int i = 0; i < base_fallbacks.size(); i++) {
- _update_rids_fb(base_fallbacks[i], 0);
- }
- } else {
- _update_rids_fb(const_cast<FontVariation *>(this), 0);
- }
- dirty_rids = false;
- }
- void FontVariation::reset_state() {
- if (base_font.is_valid()) {
- base_font->disconnect(CoreStringNames::get_singleton()->changed, callable_mp(reinterpret_cast<Font *>(this), &Font::_invalidate_rids));
- base_font.unref();
- }
- if (theme_font.is_valid()) {
- theme_font->disconnect(CoreStringNames::get_singleton()->changed, callable_mp(reinterpret_cast<Font *>(this), &Font::_invalidate_rids));
- theme_font.unref();
- }
- variation = Variation();
- opentype_features = Dictionary();
- for (int i = 0; i < TextServer::SPACING_MAX; i++) {
- extra_spacing[i] = 0;
- }
- Font::reset_state();
- }
- void FontVariation::set_base_font(const Ref<Font> &p_font) {
- if (base_font != p_font) {
- if (base_font.is_valid()) {
- base_font->disconnect(CoreStringNames::get_singleton()->changed, callable_mp(reinterpret_cast<Font *>(this), &Font::_invalidate_rids));
- }
- base_font = p_font;
- if (base_font.is_valid()) {
- base_font->connect(CoreStringNames::get_singleton()->changed, callable_mp(reinterpret_cast<Font *>(this), &Font::_invalidate_rids), CONNECT_REFERENCE_COUNTED);
- }
- _invalidate_rids();
- notify_property_list_changed();
- }
- }
- Ref<Font> FontVariation::get_base_font() const {
- return base_font;
- }
- Ref<Font> FontVariation::_get_base_font_or_default() const {
- if (theme_font.is_valid()) {
- theme_font->disconnect(CoreStringNames::get_singleton()->changed, callable_mp(reinterpret_cast<Font *>(const_cast<FontVariation *>(this)), &Font::_invalidate_rids));
- theme_font.unref();
- }
- if (base_font.is_valid()) {
- return base_font;
- }
- // Check the project-defined Theme resource.
- if (ThemeDB::get_singleton()->get_project_theme().is_valid()) {
- List<StringName> theme_types;
- ThemeDB::get_singleton()->get_project_theme()->get_type_dependencies(get_class_name(), StringName(), &theme_types);
- for (const StringName &E : theme_types) {
- if (ThemeDB::get_singleton()->get_project_theme()->has_theme_item(Theme::DATA_TYPE_FONT, "font", E)) {
- Ref<Font> f = ThemeDB::get_singleton()->get_project_theme()->get_theme_item(Theme::DATA_TYPE_FONT, "font", E);
- if (f.is_valid()) {
- theme_font = f;
- theme_font->connect(CoreStringNames::get_singleton()->changed, callable_mp(reinterpret_cast<Font *>(const_cast<FontVariation *>(this)), &Font::_invalidate_rids), CONNECT_REFERENCE_COUNTED);
- }
- return f;
- }
- }
- }
- // Lastly, fall back on the items defined in the default Theme, if they exist.
- if (ThemeDB::get_singleton()->get_default_theme().is_valid()) {
- List<StringName> theme_types;
- ThemeDB::get_singleton()->get_default_theme()->get_type_dependencies(get_class_name(), StringName(), &theme_types);
- for (const StringName &E : theme_types) {
- if (ThemeDB::get_singleton()->get_default_theme()->has_theme_item(Theme::DATA_TYPE_FONT, "font", E)) {
- Ref<Font> f = ThemeDB::get_singleton()->get_default_theme()->get_theme_item(Theme::DATA_TYPE_FONT, "font", E);
- if (f.is_valid()) {
- theme_font = f;
- theme_font->connect(CoreStringNames::get_singleton()->changed, callable_mp(reinterpret_cast<Font *>(const_cast<FontVariation *>(this)), &Font::_invalidate_rids), CONNECT_REFERENCE_COUNTED);
- }
- return f;
- }
- }
- // If they don't exist, use any type to return the default/empty value.
- Ref<Font> f = ThemeDB::get_singleton()->get_default_theme()->get_theme_item(Theme::DATA_TYPE_FONT, "font", StringName());
- if (f.is_valid()) {
- theme_font = f;
- theme_font->connect(CoreStringNames::get_singleton()->changed, callable_mp(reinterpret_cast<Font *>(const_cast<FontVariation *>(this)), &Font::_invalidate_rids), CONNECT_REFERENCE_COUNTED);
- }
- return f;
- }
- return Ref<Font>();
- }
- void FontVariation::set_variation_opentype(const Dictionary &p_coords) {
- if (variation.opentype != p_coords) {
- variation.opentype = p_coords;
- _invalidate_rids();
- }
- }
- Dictionary FontVariation::get_variation_opentype() const {
- return variation.opentype;
- }
- void FontVariation::set_variation_embolden(float p_strength) {
- if (variation.embolden != p_strength) {
- variation.embolden = p_strength;
- _invalidate_rids();
- }
- }
- float FontVariation::get_variation_embolden() const {
- return variation.embolden;
- }
- void FontVariation::set_variation_transform(Transform2D p_transform) {
- if (variation.transform != p_transform) {
- variation.transform = p_transform;
- _invalidate_rids();
- }
- }
- Transform2D FontVariation::get_variation_transform() const {
- return variation.transform;
- }
- void FontVariation::set_variation_face_index(int p_face_index) {
- if (variation.face_index != p_face_index) {
- variation.face_index = p_face_index;
- _invalidate_rids();
- }
- }
- int FontVariation::get_variation_face_index() const {
- return variation.face_index;
- }
- void FontVariation::set_opentype_features(const Dictionary &p_features) {
- if (opentype_features != p_features) {
- opentype_features = p_features;
- _invalidate_rids();
- }
- }
- Dictionary FontVariation::get_opentype_features() const {
- return opentype_features;
- }
- void FontVariation::set_spacing(TextServer::SpacingType p_spacing, int p_value) {
- ERR_FAIL_INDEX((int)p_spacing, TextServer::SPACING_MAX);
- if (extra_spacing[p_spacing] != p_value) {
- extra_spacing[p_spacing] = p_value;
- _invalidate_rids();
- }
- }
- int FontVariation::get_spacing(TextServer::SpacingType p_spacing) const {
- ERR_FAIL_INDEX_V((int)p_spacing, TextServer::SPACING_MAX, 0);
- return extra_spacing[p_spacing];
- }
- RID FontVariation::find_variation(const Dictionary &p_variation_coordinates, int p_face_index, float p_strength, Transform2D p_transform) const {
- Ref<Font> f = _get_base_font_or_default();
- if (f.is_valid()) {
- return f->find_variation(p_variation_coordinates, p_face_index, p_strength, p_transform);
- }
- return RID();
- }
- RID FontVariation::_get_rid() const {
- Ref<Font> f = _get_base_font_or_default();
- if (f.is_valid()) {
- return f->find_variation(variation.opentype, variation.face_index, variation.embolden, variation.transform);
- }
- return RID();
- }
- FontVariation::FontVariation() {
- for (int i = 0; i < TextServer::SPACING_MAX; i++) {
- extra_spacing[i] = 0;
- }
- }
- FontVariation::~FontVariation() {
- }
- /*************************************************************************/
- /* SystemFont */
- /*************************************************************************/
- void SystemFont::_bind_methods() {
- ClassDB::bind_method(D_METHOD("set_antialiasing", "antialiasing"), &SystemFont::set_antialiasing);
- ClassDB::bind_method(D_METHOD("get_antialiasing"), &SystemFont::get_antialiasing);
- ClassDB::bind_method(D_METHOD("set_generate_mipmaps", "generate_mipmaps"), &SystemFont::set_generate_mipmaps);
- ClassDB::bind_method(D_METHOD("get_generate_mipmaps"), &SystemFont::get_generate_mipmaps);
- ClassDB::bind_method(D_METHOD("set_force_autohinter", "force_autohinter"), &SystemFont::set_force_autohinter);
- ClassDB::bind_method(D_METHOD("is_force_autohinter"), &SystemFont::is_force_autohinter);
- ClassDB::bind_method(D_METHOD("set_hinting", "hinting"), &SystemFont::set_hinting);
- ClassDB::bind_method(D_METHOD("get_hinting"), &SystemFont::get_hinting);
- ClassDB::bind_method(D_METHOD("set_subpixel_positioning", "subpixel_positioning"), &SystemFont::set_subpixel_positioning);
- ClassDB::bind_method(D_METHOD("get_subpixel_positioning"), &SystemFont::get_subpixel_positioning);
- ClassDB::bind_method(D_METHOD("set_multichannel_signed_distance_field", "msdf"), &SystemFont::set_multichannel_signed_distance_field);
- ClassDB::bind_method(D_METHOD("is_multichannel_signed_distance_field"), &SystemFont::is_multichannel_signed_distance_field);
- ClassDB::bind_method(D_METHOD("set_oversampling", "oversampling"), &SystemFont::set_oversampling);
- ClassDB::bind_method(D_METHOD("get_oversampling"), &SystemFont::get_oversampling);
- ClassDB::bind_method(D_METHOD("get_font_names"), &SystemFont::get_font_names);
- ClassDB::bind_method(D_METHOD("set_font_names", "names"), &SystemFont::set_font_names);
- ClassDB::bind_method(D_METHOD("set_font_style", "style"), &SystemFont::set_font_style);
- ADD_PROPERTY(PropertyInfo(Variant::PACKED_STRING_ARRAY, "font_names"), "set_font_names", "get_font_names");
- ADD_PROPERTY(PropertyInfo(Variant::INT, "font_style", PROPERTY_HINT_FLAGS, "Bold,Italic"), "set_font_style", "get_font_style");
- ADD_PROPERTY(PropertyInfo(Variant::INT, "antialiasing", PROPERTY_HINT_ENUM, "None,Grayscale,LCD sub-pixel", PROPERTY_USAGE_STORAGE), "set_antialiasing", "get_antialiasing");
- ADD_PROPERTY(PropertyInfo(Variant::BOOL, "generate_mipmaps"), "set_generate_mipmaps", "get_generate_mipmaps");
- ADD_PROPERTY(PropertyInfo(Variant::BOOL, "force_autohinter"), "set_force_autohinter", "is_force_autohinter");
- ADD_PROPERTY(PropertyInfo(Variant::INT, "hinting", PROPERTY_HINT_ENUM, "None,Light,Normal"), "set_hinting", "get_hinting");
- ADD_PROPERTY(PropertyInfo(Variant::INT, "subpixel_positioning", PROPERTY_HINT_ENUM, "Disabled,Auto,One half of a pixel,One quarter of a pixel"), "set_subpixel_positioning", "get_subpixel_positioning");
- ADD_PROPERTY(PropertyInfo(Variant::BOOL, "multichannel_signed_distance_field"), "set_multichannel_signed_distance_field", "is_multichannel_signed_distance_field");
- ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "oversampling", PROPERTY_HINT_RANGE, "0,10,0.1"), "set_oversampling", "get_oversampling");
- ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "fallbacks", PROPERTY_HINT_ARRAY_TYPE, vformat("%s/%s:%s", Variant::OBJECT, PROPERTY_HINT_RESOURCE_TYPE, "Font")), "set_fallbacks", "get_fallbacks");
- }
- void SystemFont::_update_rids() const {
- Ref<Font> f = _get_base_font_or_default();
- rids.clear();
- if (fallbacks.is_empty() && f.is_valid()) {
- RID rid = _get_rid();
- if (rid.is_valid()) {
- rids.push_back(rid);
- }
- const TypedArray<Font> &base_fallbacks = f->get_fallbacks();
- for (int i = 0; i < base_fallbacks.size(); i++) {
- _update_rids_fb(base_fallbacks[i], 0);
- }
- } else {
- _update_rids_fb(const_cast<SystemFont *>(this), 0);
- }
- dirty_rids = false;
- }
- void SystemFont::_update_base_font() {
- if (base_font.is_valid()) {
- base_font->disconnect(CoreStringNames::get_singleton()->changed, callable_mp(reinterpret_cast<Font *>(this), &Font::_invalidate_rids));
- base_font.unref();
- }
- face_indeces.clear();
- ftr_weight = 0;
- ftr_italic = 0;
- for (const String &E : names) {
- if (E.is_empty()) {
- continue;
- }
- String path = OS::get_singleton()->get_system_font_path(E, style & TextServer::FONT_BOLD, style & TextServer::FONT_ITALIC);
- if (path.is_empty()) {
- continue;
- }
- Ref<FontFile> file;
- file.instantiate();
- Error err = file->load_dynamic_font(path);
- if (err != OK) {
- continue;
- }
- // If it's a font collection check all faces to match requested style.
- for (int i = 0; i < file->get_face_count(); i++) {
- file->set_face_index(0, i);
- if (((file->get_font_style() & TextServer::FONT_BOLD) == (style & TextServer::FONT_BOLD)) && ((file->get_font_style() & TextServer::FONT_ITALIC) == (style & TextServer::FONT_ITALIC))) {
- face_indeces.push_back(i);
- }
- }
- if (face_indeces.is_empty()) {
- face_indeces.push_back(0);
- }
- file->set_face_index(0, face_indeces[0]);
- // If it's a variable font, apply weight and italic coordinates to match requested style.
- Dictionary ftr = file->get_supported_variation_list();
- if ((style & TextServer::FONT_BOLD) && ftr.has(TS->name_to_tag("weight"))) {
- ftr_weight = 700;
- }
- if ((style & TextServer::FONT_ITALIC) && ftr.has(TS->name_to_tag("italic"))) {
- ftr_italic = 1;
- }
- // Apply font rendering settings.
- file->set_antialiasing(antialiasing);
- file->set_generate_mipmaps(mipmaps);
- file->set_force_autohinter(force_autohinter);
- file->set_hinting(hinting);
- file->set_subpixel_positioning(subpixel_positioning);
- file->set_multichannel_signed_distance_field(msdf);
- file->set_oversampling(oversampling);
- base_font = file;
- break;
- }
- if (base_font.is_valid()) {
- base_font->connect(CoreStringNames::get_singleton()->changed, callable_mp(reinterpret_cast<Font *>(this), &Font::_invalidate_rids), CONNECT_REFERENCE_COUNTED);
- }
- _invalidate_rids();
- notify_property_list_changed();
- }
- void SystemFont::reset_state() {
- if (base_font.is_valid()) {
- base_font->disconnect(CoreStringNames::get_singleton()->changed, callable_mp(reinterpret_cast<Font *>(this), &Font::_invalidate_rids));
- base_font.unref();
- }
- if (theme_font.is_valid()) {
- theme_font->disconnect(CoreStringNames::get_singleton()->changed, callable_mp(reinterpret_cast<Font *>(this), &Font::_invalidate_rids));
- theme_font.unref();
- }
- names.clear();
- face_indeces.clear();
- ftr_weight = 0;
- ftr_italic = 0;
- style = 0;
- antialiasing = TextServer::FONT_ANTIALIASING_GRAY;
- mipmaps = false;
- force_autohinter = false;
- hinting = TextServer::HINTING_LIGHT;
- subpixel_positioning = TextServer::SUBPIXEL_POSITIONING_DISABLED;
- oversampling = 0.f;
- msdf = false;
- Font::reset_state();
- }
- Ref<Font> SystemFont::_get_base_font_or_default() const {
- if (theme_font.is_valid()) {
- theme_font->disconnect(CoreStringNames::get_singleton()->changed, callable_mp(reinterpret_cast<Font *>(const_cast<SystemFont *>(this)), &Font::_invalidate_rids));
- theme_font.unref();
- }
- if (base_font.is_valid()) {
- return base_font;
- }
- // Check the project-defined Theme resource.
- if (ThemeDB::get_singleton()->get_project_theme().is_valid()) {
- List<StringName> theme_types;
- ThemeDB::get_singleton()->get_project_theme()->get_type_dependencies(get_class_name(), StringName(), &theme_types);
- for (const StringName &E : theme_types) {
- if (ThemeDB::get_singleton()->get_project_theme()->has_theme_item(Theme::DATA_TYPE_FONT, "font", E)) {
- Ref<Font> f = ThemeDB::get_singleton()->get_project_theme()->get_theme_item(Theme::DATA_TYPE_FONT, "font", E);
- if (f.is_valid()) {
- theme_font = f;
- theme_font->connect(CoreStringNames::get_singleton()->changed, callable_mp(reinterpret_cast<Font *>(const_cast<SystemFont *>(this)), &Font::_invalidate_rids), CONNECT_REFERENCE_COUNTED);
- }
- return f;
- }
- }
- }
- // Lastly, fall back on the items defined in the default Theme, if they exist.
- if (ThemeDB::get_singleton()->get_default_theme().is_valid()) {
- List<StringName> theme_types;
- ThemeDB::get_singleton()->get_default_theme()->get_type_dependencies(get_class_name(), StringName(), &theme_types);
- for (const StringName &E : theme_types) {
- if (ThemeDB::get_singleton()->get_default_theme()->has_theme_item(Theme::DATA_TYPE_FONT, "font", E)) {
- Ref<Font> f = ThemeDB::get_singleton()->get_default_theme()->get_theme_item(Theme::DATA_TYPE_FONT, "font", E);
- if (f.is_valid()) {
- theme_font = f;
- theme_font->connect(CoreStringNames::get_singleton()->changed, callable_mp(reinterpret_cast<Font *>(const_cast<SystemFont *>(this)), &Font::_invalidate_rids), CONNECT_REFERENCE_COUNTED);
- }
- return f;
- }
- }
- // If they don't exist, use any type to return the default/empty value.
- Ref<Font> f = ThemeDB::get_singleton()->get_default_theme()->get_theme_item(Theme::DATA_TYPE_FONT, "font", StringName());
- if (f.is_valid()) {
- theme_font = f;
- theme_font->connect(CoreStringNames::get_singleton()->changed, callable_mp(reinterpret_cast<Font *>(const_cast<SystemFont *>(this)), &Font::_invalidate_rids), CONNECT_REFERENCE_COUNTED);
- }
- return f;
- }
- return Ref<Font>();
- }
- void SystemFont::set_antialiasing(TextServer::FontAntialiasing p_antialiasing) {
- if (antialiasing != p_antialiasing) {
- antialiasing = p_antialiasing;
- if (base_font.is_valid()) {
- base_font->set_antialiasing(antialiasing);
- }
- emit_changed();
- }
- }
- TextServer::FontAntialiasing SystemFont::get_antialiasing() const {
- return antialiasing;
- }
- void SystemFont::set_generate_mipmaps(bool p_generate_mipmaps) {
- if (mipmaps != p_generate_mipmaps) {
- mipmaps = p_generate_mipmaps;
- if (base_font.is_valid()) {
- base_font->set_generate_mipmaps(mipmaps);
- }
- emit_changed();
- }
- }
- bool SystemFont::get_generate_mipmaps() const {
- return mipmaps;
- }
- void SystemFont::set_force_autohinter(bool p_force_autohinter) {
- if (force_autohinter != p_force_autohinter) {
- force_autohinter = p_force_autohinter;
- if (base_font.is_valid()) {
- base_font->set_force_autohinter(force_autohinter);
- }
- emit_changed();
- }
- }
- bool SystemFont::is_force_autohinter() const {
- return force_autohinter;
- }
- void SystemFont::set_hinting(TextServer::Hinting p_hinting) {
- if (hinting != p_hinting) {
- hinting = p_hinting;
- if (base_font.is_valid()) {
- base_font->set_hinting(hinting);
- }
- emit_changed();
- }
- }
- TextServer::Hinting SystemFont::get_hinting() const {
- return hinting;
- }
- void SystemFont::set_subpixel_positioning(TextServer::SubpixelPositioning p_subpixel) {
- if (subpixel_positioning != p_subpixel) {
- subpixel_positioning = p_subpixel;
- if (base_font.is_valid()) {
- base_font->set_subpixel_positioning(subpixel_positioning);
- }
- emit_changed();
- }
- }
- TextServer::SubpixelPositioning SystemFont::get_subpixel_positioning() const {
- return subpixel_positioning;
- }
- void SystemFont::set_multichannel_signed_distance_field(bool p_msdf) {
- if (msdf != p_msdf) {
- msdf = p_msdf;
- if (base_font.is_valid()) {
- base_font->set_multichannel_signed_distance_field(msdf);
- }
- emit_changed();
- }
- }
- bool SystemFont::is_multichannel_signed_distance_field() const {
- return msdf;
- }
- void SystemFont::set_oversampling(real_t p_oversampling) {
- if (oversampling != p_oversampling) {
- oversampling = p_oversampling;
- if (base_font.is_valid()) {
- base_font->set_oversampling(oversampling);
- }
- emit_changed();
- }
- }
- real_t SystemFont::get_oversampling() const {
- return oversampling;
- }
- void SystemFont::set_font_names(const PackedStringArray &p_names) {
- if (names != p_names) {
- names = p_names;
- _update_base_font();
- }
- }
- PackedStringArray SystemFont::get_font_names() const {
- return names;
- }
- void SystemFont::set_font_style(BitField<TextServer::FontStyle> p_style) {
- if (style != p_style) {
- style = p_style;
- _update_base_font();
- }
- }
- BitField<TextServer::FontStyle> SystemFont::get_font_style() const {
- return style;
- }
- int SystemFont::get_spacing(TextServer::SpacingType p_spacing) const {
- if (base_font.is_valid()) {
- return base_font->get_spacing(p_spacing);
- } else {
- return 0;
- }
- }
- RID SystemFont::find_variation(const Dictionary &p_variation_coordinates, int p_face_index, float p_strength, Transform2D p_transform) const {
- Ref<Font> f = _get_base_font_or_default();
- if (f.is_valid()) {
- Dictionary var = p_variation_coordinates;
- if (ftr_weight > 0 && !var.has(TS->name_to_tag("weight"))) {
- var[TS->name_to_tag("weight")] = ftr_weight;
- }
- if (ftr_italic > 0 && !var.has(TS->name_to_tag("italic"))) {
- var[TS->name_to_tag("italic")] = ftr_italic;
- }
- if (!face_indeces.is_empty()) {
- int face_index = CLAMP(p_face_index, 0, face_indeces.size() - 1);
- return f->find_variation(var, face_indeces[face_index], p_strength, p_transform);
- } else {
- return f->find_variation(var, 0, p_strength, p_transform);
- }
- }
- return RID();
- }
- RID SystemFont::_get_rid() const {
- Ref<Font> f = _get_base_font_or_default();
- if (f.is_valid()) {
- if (!face_indeces.is_empty()) {
- Dictionary var;
- if (ftr_weight > 0) {
- var[TS->name_to_tag("weight")] = ftr_weight;
- }
- if (ftr_italic > 0) {
- var[TS->name_to_tag("italic")] = ftr_italic;
- }
- return f->find_variation(var, face_indeces[0]);
- } else {
- return f->_get_rid();
- }
- }
- return RID();
- }
- int64_t SystemFont::get_face_count() const {
- return face_indeces.size();
- }
- SystemFont::SystemFont() {
- /* NOP */
- }
- SystemFont::~SystemFont() {
- }
|