rich_text_label.cpp 187 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888
  1. /**************************************************************************/
  2. /* rich_text_label.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "rich_text_label.h"
  31. #include "core/input/input_map.h"
  32. #include "core/math/math_defs.h"
  33. #include "core/os/keyboard.h"
  34. #include "core/os/os.h"
  35. #include "label.h"
  36. #include "scene/scene_string_names.h"
  37. #include "servers/display_server.h"
  38. #include "modules/modules_enabled.gen.h" // For regex.
  39. #ifdef MODULE_REGEX_ENABLED
  40. #include "modules/regex/regex.h"
  41. #endif
  42. RichTextLabel::Item *RichTextLabel::_get_next_item(Item *p_item, bool p_free) const {
  43. if (p_free) {
  44. if (p_item->subitems.size()) {
  45. return p_item->subitems.front()->get();
  46. } else if (!p_item->parent) {
  47. return nullptr;
  48. } else if (p_item->E->next()) {
  49. return p_item->E->next()->get();
  50. } else {
  51. // Go up until something with a next is found.
  52. while (p_item->parent && !p_item->E->next()) {
  53. p_item = p_item->parent;
  54. }
  55. if (p_item->parent) {
  56. return p_item->E->next()->get();
  57. } else {
  58. return nullptr;
  59. }
  60. }
  61. } else {
  62. if (p_item->subitems.size() && p_item->type != ITEM_TABLE) {
  63. return p_item->subitems.front()->get();
  64. } else if (p_item->type == ITEM_FRAME) {
  65. return nullptr;
  66. } else if (p_item->E->next()) {
  67. return p_item->E->next()->get();
  68. } else {
  69. // Go up until something with a next is found.
  70. while (p_item->type != ITEM_FRAME && !p_item->E->next()) {
  71. p_item = p_item->parent;
  72. }
  73. if (p_item->type != ITEM_FRAME) {
  74. return p_item->E->next()->get();
  75. } else {
  76. return nullptr;
  77. }
  78. }
  79. }
  80. }
  81. RichTextLabel::Item *RichTextLabel::_get_prev_item(Item *p_item, bool p_free) const {
  82. if (p_free) {
  83. if (p_item->subitems.size()) {
  84. return p_item->subitems.back()->get();
  85. } else if (!p_item->parent) {
  86. return nullptr;
  87. } else if (p_item->E->prev()) {
  88. return p_item->E->prev()->get();
  89. } else {
  90. // Go back until something with a prev is found.
  91. while (p_item->parent && !p_item->E->prev()) {
  92. p_item = p_item->parent;
  93. }
  94. if (p_item->parent) {
  95. return p_item->E->prev()->get();
  96. } else {
  97. return nullptr;
  98. }
  99. }
  100. } else {
  101. if (p_item->subitems.size() && p_item->type != ITEM_TABLE) {
  102. return p_item->subitems.back()->get();
  103. } else if (p_item->type == ITEM_FRAME) {
  104. return nullptr;
  105. } else if (p_item->E->prev()) {
  106. return p_item->E->prev()->get();
  107. } else {
  108. // Go back until something with a prev is found.
  109. while (p_item->type != ITEM_FRAME && !p_item->E->prev()) {
  110. p_item = p_item->parent;
  111. }
  112. if (p_item->type != ITEM_FRAME) {
  113. return p_item->E->prev()->get();
  114. } else {
  115. return nullptr;
  116. }
  117. }
  118. }
  119. }
  120. Rect2 RichTextLabel::_get_text_rect() {
  121. return Rect2(theme_cache.normal_style->get_offset(), get_size() - theme_cache.normal_style->get_minimum_size());
  122. }
  123. RichTextLabel::Item *RichTextLabel::_get_item_at_pos(RichTextLabel::Item *p_item_from, RichTextLabel::Item *p_item_to, int p_position) {
  124. int offset = 0;
  125. for (Item *it = p_item_from; it && it != p_item_to; it = _get_next_item(it)) {
  126. switch (it->type) {
  127. case ITEM_TEXT: {
  128. ItemText *t = static_cast<ItemText *>(it);
  129. offset += t->text.length();
  130. if (offset > p_position) {
  131. return it;
  132. }
  133. } break;
  134. case ITEM_NEWLINE: {
  135. offset += 1;
  136. if (offset == p_position) {
  137. return it;
  138. }
  139. } break;
  140. case ITEM_IMAGE: {
  141. offset += 1;
  142. if (offset > p_position) {
  143. return it;
  144. }
  145. } break;
  146. case ITEM_TABLE: {
  147. offset += 1;
  148. } break;
  149. default:
  150. break;
  151. }
  152. }
  153. return p_item_from;
  154. }
  155. String RichTextLabel::_roman(int p_num, bool p_capitalize) const {
  156. if (p_num > 3999) {
  157. return "ERR";
  158. };
  159. String s;
  160. if (p_capitalize) {
  161. const String roman_M[] = { "", "M", "MM", "MMM" };
  162. const String roman_C[] = { "", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM" };
  163. const String roman_X[] = { "", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC" };
  164. const String roman_I[] = { "", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX" };
  165. s = roman_M[p_num / 1000] + roman_C[(p_num % 1000) / 100] + roman_X[(p_num % 100) / 10] + roman_I[p_num % 10];
  166. } else {
  167. const String roman_M[] = { "", "m", "mm", "mmm" };
  168. const String roman_C[] = { "", "c", "cc", "ccc", "cd", "d", "dc", "dcc", "dccc", "cm" };
  169. const String roman_X[] = { "", "x", "xx", "xxx", "xl", "l", "lx", "lxx", "lxxx", "xc" };
  170. const String roman_I[] = { "", "i", "ii", "iii", "iv", "v", "vi", "vii", "viii", "ix" };
  171. s = roman_M[p_num / 1000] + roman_C[(p_num % 1000) / 100] + roman_X[(p_num % 100) / 10] + roman_I[p_num % 10];
  172. }
  173. return s;
  174. }
  175. String RichTextLabel::_letters(int p_num, bool p_capitalize) const {
  176. int64_t n = p_num;
  177. int chars = 0;
  178. do {
  179. n /= 24;
  180. chars++;
  181. } while (n);
  182. String s;
  183. s.resize(chars + 1);
  184. char32_t *c = s.ptrw();
  185. c[chars] = 0;
  186. n = p_num;
  187. do {
  188. int mod = ABS(n % 24);
  189. char a = (p_capitalize ? 'A' : 'a');
  190. c[--chars] = a + mod - 1;
  191. n /= 24;
  192. } while (n);
  193. return s;
  194. }
  195. void RichTextLabel::_update_line_font(ItemFrame *p_frame, int p_line, const Ref<Font> &p_base_font, int p_base_font_size) {
  196. ERR_FAIL_COND(p_frame == nullptr);
  197. ERR_FAIL_COND(p_line < 0 || p_line >= (int)p_frame->lines.size());
  198. Line &l = p_frame->lines[p_line];
  199. MutexLock lock(l.text_buf->get_mutex());
  200. RID t = l.text_buf->get_rid();
  201. int spans = TS->shaped_get_span_count(t);
  202. for (int i = 0; i < spans; i++) {
  203. ItemText *it = reinterpret_cast<ItemText *>((uint64_t)TS->shaped_get_span_meta(t, i));
  204. if (it) {
  205. Ref<Font> font = p_base_font;
  206. int font_size = p_base_font_size;
  207. ItemFont *font_it = _find_font(it);
  208. if (font_it) {
  209. if (font_it->font.is_valid()) {
  210. font = font_it->font;
  211. }
  212. if (font_it->font_size > 0) {
  213. font_size = font_it->font_size;
  214. }
  215. }
  216. ItemFontSize *font_size_it = _find_font_size(it);
  217. if (font_size_it && font_size_it->font_size > 0) {
  218. font_size = font_size_it->font_size;
  219. }
  220. TS->shaped_set_span_update_font(t, i, font->get_rids(), font_size, font->get_opentype_features());
  221. for (int j = 0; j < TextServer::SPACING_MAX; j++) {
  222. TS->shaped_text_set_spacing(t, TextServer::SpacingType(j), font->get_spacing(TextServer::SpacingType(j)));
  223. }
  224. }
  225. }
  226. Item *it_to = (p_line + 1 < (int)p_frame->lines.size()) ? p_frame->lines[p_line + 1].from : nullptr;
  227. for (Item *it = l.from; it && it != it_to; it = _get_next_item(it)) {
  228. switch (it->type) {
  229. case ITEM_TABLE: {
  230. ItemTable *table = static_cast<ItemTable *>(it);
  231. for (Item *E : table->subitems) {
  232. ERR_CONTINUE(E->type != ITEM_FRAME); // Children should all be frames.
  233. ItemFrame *frame = static_cast<ItemFrame *>(E);
  234. for (int i = 0; i < (int)frame->lines.size(); i++) {
  235. _update_line_font(frame, i, p_base_font, p_base_font_size);
  236. }
  237. }
  238. } break;
  239. default:
  240. break;
  241. }
  242. }
  243. }
  244. float RichTextLabel::_resize_line(ItemFrame *p_frame, int p_line, const Ref<Font> &p_base_font, int p_base_font_size, int p_width, float p_h) {
  245. ERR_FAIL_COND_V(p_frame == nullptr, p_h);
  246. ERR_FAIL_COND_V(p_line < 0 || p_line >= (int)p_frame->lines.size(), p_h);
  247. Line &l = p_frame->lines[p_line];
  248. MutexLock lock(l.text_buf->get_mutex());
  249. l.offset.x = _find_margin(l.from, p_base_font, p_base_font_size);
  250. l.text_buf->set_width(p_width - l.offset.x);
  251. if (tab_size > 0) { // Align inline tabs.
  252. Vector<float> tabs;
  253. tabs.push_back(tab_size * p_base_font->get_char_size(' ', p_base_font_size).width);
  254. l.text_buf->tab_align(tabs);
  255. }
  256. Item *it_to = (p_line + 1 < (int)p_frame->lines.size()) ? p_frame->lines[p_line + 1].from : nullptr;
  257. for (Item *it = l.from; it && it != it_to; it = _get_next_item(it)) {
  258. switch (it->type) {
  259. case ITEM_TABLE: {
  260. ItemTable *table = static_cast<ItemTable *>(it);
  261. int col_count = table->columns.size();
  262. for (int i = 0; i < col_count; i++) {
  263. table->columns[i].width = 0;
  264. }
  265. int idx = 0;
  266. for (Item *E : table->subitems) {
  267. ERR_CONTINUE(E->type != ITEM_FRAME); // Children should all be frames.
  268. ItemFrame *frame = static_cast<ItemFrame *>(E);
  269. float prev_h = 0;
  270. for (int i = 0; i < (int)frame->lines.size(); i++) {
  271. MutexLock sub_lock(frame->lines[i].text_buf->get_mutex());
  272. int w = _find_margin(frame->lines[i].from, p_base_font, p_base_font_size) + 1;
  273. prev_h = _resize_line(frame, i, p_base_font, p_base_font_size, w, prev_h);
  274. }
  275. idx++;
  276. }
  277. // Compute minimum width for each cell.
  278. const int available_width = p_width - theme_cache.table_h_separation * (col_count - 1);
  279. // Compute available width and total ratio (for expanders).
  280. int total_ratio = 0;
  281. int remaining_width = available_width;
  282. table->total_width = theme_cache.table_h_separation;
  283. for (int i = 0; i < col_count; i++) {
  284. remaining_width -= table->columns[i].min_width;
  285. if (table->columns[i].max_width > table->columns[i].min_width) {
  286. table->columns[i].expand = true;
  287. }
  288. if (table->columns[i].expand) {
  289. total_ratio += table->columns[i].expand_ratio;
  290. }
  291. }
  292. // Assign actual widths.
  293. for (int i = 0; i < col_count; i++) {
  294. table->columns[i].width = table->columns[i].min_width;
  295. if (table->columns[i].expand && total_ratio > 0 && remaining_width > 0) {
  296. table->columns[i].width += table->columns[i].expand_ratio * remaining_width / total_ratio;
  297. }
  298. if (i != col_count - 1) {
  299. table->total_width += table->columns[i].width + theme_cache.table_h_separation;
  300. } else {
  301. table->total_width += table->columns[i].width;
  302. }
  303. }
  304. // Resize to max_width if needed and distribute the remaining space.
  305. bool table_need_fit = true;
  306. while (table_need_fit) {
  307. table_need_fit = false;
  308. // Fit slim.
  309. for (int i = 0; i < col_count; i++) {
  310. if (!table->columns[i].expand) {
  311. continue;
  312. }
  313. int dif = table->columns[i].width - table->columns[i].max_width;
  314. if (dif > 0) {
  315. table_need_fit = true;
  316. table->columns[i].width = table->columns[i].max_width;
  317. table->total_width -= dif;
  318. total_ratio -= table->columns[i].expand_ratio;
  319. }
  320. }
  321. // Grow.
  322. remaining_width = available_width - table->total_width;
  323. if (remaining_width > 0 && total_ratio > 0) {
  324. for (int i = 0; i < col_count; i++) {
  325. if (table->columns[i].expand) {
  326. int dif = table->columns[i].max_width - table->columns[i].width;
  327. if (dif > 0) {
  328. int slice = table->columns[i].expand_ratio * remaining_width / total_ratio;
  329. int incr = MIN(dif, slice);
  330. table->columns[i].width += incr;
  331. table->total_width += incr;
  332. }
  333. }
  334. }
  335. }
  336. }
  337. // Update line width and get total height.
  338. idx = 0;
  339. table->total_height = 0;
  340. table->rows.clear();
  341. table->rows_baseline.clear();
  342. Vector2 offset;
  343. float row_height = 0.0;
  344. for (Item *E : table->subitems) {
  345. ERR_CONTINUE(E->type != ITEM_FRAME); // Children should all be frames.
  346. ItemFrame *frame = static_cast<ItemFrame *>(E);
  347. int column = idx % col_count;
  348. offset.x += frame->padding.position.x;
  349. float yofs = frame->padding.position.y;
  350. float prev_h = 0;
  351. float row_baseline = 0.0;
  352. for (int i = 0; i < (int)frame->lines.size(); i++) {
  353. MutexLock sub_lock(frame->lines[i].text_buf->get_mutex());
  354. frame->lines[i].text_buf->set_width(table->columns[column].width);
  355. table->columns[column].width = MAX(table->columns[column].width, ceil(frame->lines[i].text_buf->get_size().x));
  356. frame->lines[i].offset.y = prev_h;
  357. float h = frame->lines[i].text_buf->get_size().y + (frame->lines[i].text_buf->get_line_count() - 1) * theme_cache.line_separation;
  358. if (i > 0) {
  359. h += theme_cache.line_separation;
  360. }
  361. if (frame->min_size_over.y > 0) {
  362. h = MAX(h, frame->min_size_over.y);
  363. }
  364. if (frame->max_size_over.y > 0) {
  365. h = MIN(h, frame->max_size_over.y);
  366. }
  367. yofs += h;
  368. prev_h = frame->lines[i].offset.y + frame->lines[i].text_buf->get_size().y + frame->lines[i].text_buf->get_line_count() * theme_cache.line_separation;
  369. frame->lines[i].offset += offset;
  370. row_baseline = MAX(row_baseline, frame->lines[i].text_buf->get_line_ascent(frame->lines[i].text_buf->get_line_count() - 1));
  371. }
  372. yofs += frame->padding.size.y;
  373. offset.x += table->columns[column].width + theme_cache.table_h_separation + frame->padding.size.x;
  374. row_height = MAX(yofs, row_height);
  375. if (column == col_count - 1) {
  376. offset.x = 0;
  377. row_height += theme_cache.table_v_separation;
  378. table->total_height += row_height;
  379. offset.y += row_height;
  380. table->rows.push_back(row_height);
  381. table->rows_baseline.push_back(table->total_height - row_height + row_baseline);
  382. row_height = 0;
  383. }
  384. idx++;
  385. }
  386. int row_idx = (table->align_to_row < 0) ? table->rows_baseline.size() - 1 : table->align_to_row;
  387. if (table->rows_baseline.size() != 0 && row_idx < (int)table->rows_baseline.size() - 1) {
  388. l.text_buf->resize_object((uint64_t)it, Size2(table->total_width, table->total_height), table->inline_align, Math::round(table->rows_baseline[row_idx]));
  389. } else {
  390. l.text_buf->resize_object((uint64_t)it, Size2(table->total_width, table->total_height), table->inline_align);
  391. }
  392. } break;
  393. default:
  394. break;
  395. }
  396. }
  397. l.offset.y = p_h;
  398. return _calculate_line_vertical_offset(l);
  399. }
  400. float RichTextLabel::_shape_line(ItemFrame *p_frame, int p_line, const Ref<Font> &p_base_font, int p_base_font_size, int p_width, float p_h, int *r_char_offset) {
  401. ERR_FAIL_COND_V(p_frame == nullptr, p_h);
  402. ERR_FAIL_COND_V(p_line < 0 || p_line >= (int)p_frame->lines.size(), p_h);
  403. Line &l = p_frame->lines[p_line];
  404. MutexLock lock(l.text_buf->get_mutex());
  405. BitField<TextServer::LineBreakFlag> autowrap_flags = TextServer::BREAK_MANDATORY;
  406. switch (autowrap_mode) {
  407. case TextServer::AUTOWRAP_WORD_SMART:
  408. autowrap_flags = TextServer::BREAK_WORD_BOUND | TextServer::BREAK_ADAPTIVE | TextServer::BREAK_MANDATORY;
  409. break;
  410. case TextServer::AUTOWRAP_WORD:
  411. autowrap_flags = TextServer::BREAK_WORD_BOUND | TextServer::BREAK_MANDATORY;
  412. break;
  413. case TextServer::AUTOWRAP_ARBITRARY:
  414. autowrap_flags = TextServer::BREAK_GRAPHEME_BOUND | TextServer::BREAK_MANDATORY;
  415. break;
  416. case TextServer::AUTOWRAP_OFF:
  417. break;
  418. }
  419. autowrap_flags = autowrap_flags | TextServer::BREAK_TRIM_EDGE_SPACES;
  420. // Clear cache.
  421. l.text_buf->clear();
  422. l.text_buf->set_break_flags(autowrap_flags);
  423. l.text_buf->set_justification_flags(TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND | TextServer::JUSTIFICATION_TRIM_EDGE_SPACES);
  424. l.char_offset = *r_char_offset;
  425. l.char_count = 0;
  426. // Add indent.
  427. l.offset.x = _find_margin(l.from, p_base_font, p_base_font_size);
  428. l.text_buf->set_width(p_width - l.offset.x);
  429. l.text_buf->set_alignment(_find_alignment(l.from));
  430. l.text_buf->set_direction(_find_direction(l.from));
  431. if (tab_size > 0) { // Align inline tabs.
  432. Vector<float> tabs;
  433. tabs.push_back(tab_size * p_base_font->get_char_size(' ', p_base_font_size).width);
  434. l.text_buf->tab_align(tabs);
  435. }
  436. // Shape current paragraph.
  437. String txt;
  438. Item *it_to = (p_line + 1 < (int)p_frame->lines.size()) ? p_frame->lines[p_line + 1].from : nullptr;
  439. int remaining_characters = visible_characters - l.char_offset;
  440. for (Item *it = l.from; it && it != it_to; it = _get_next_item(it)) {
  441. if (visible_chars_behavior == TextServer::VC_CHARS_BEFORE_SHAPING && visible_characters >= 0 && remaining_characters <= 0) {
  442. break;
  443. }
  444. switch (it->type) {
  445. case ITEM_DROPCAP: {
  446. // Add dropcap.
  447. const ItemDropcap *dc = static_cast<ItemDropcap *>(it);
  448. l.text_buf->set_dropcap(dc->text, dc->font, dc->font_size, dc->dropcap_margins);
  449. l.dc_color = dc->color;
  450. l.dc_ol_size = dc->ol_size;
  451. l.dc_ol_color = dc->ol_color;
  452. } break;
  453. case ITEM_NEWLINE: {
  454. Ref<Font> font = p_base_font;
  455. int font_size = p_base_font_size;
  456. ItemFont *font_it = _find_font(it);
  457. if (font_it) {
  458. if (font_it->font.is_valid()) {
  459. font = font_it->font;
  460. }
  461. if (font_it->font_size > 0) {
  462. font_size = font_it->font_size;
  463. }
  464. }
  465. ItemFontSize *font_size_it = _find_font_size(it);
  466. if (font_size_it && font_size_it->font_size > 0) {
  467. font_size = font_size_it->font_size;
  468. }
  469. l.text_buf->add_string("\n", font, font_size);
  470. txt += "\n";
  471. l.char_count++;
  472. remaining_characters--;
  473. } break;
  474. case ITEM_TEXT: {
  475. ItemText *t = static_cast<ItemText *>(it);
  476. Ref<Font> font = p_base_font;
  477. int font_size = p_base_font_size;
  478. ItemFont *font_it = _find_font(it);
  479. if (font_it) {
  480. if (font_it->font.is_valid()) {
  481. font = font_it->font;
  482. }
  483. if (font_it->font_size > 0) {
  484. font_size = font_it->font_size;
  485. }
  486. }
  487. ItemFontSize *font_size_it = _find_font_size(it);
  488. if (font_size_it && font_size_it->font_size > 0) {
  489. font_size = font_size_it->font_size;
  490. }
  491. String lang = _find_language(it);
  492. String tx = t->text;
  493. if (visible_chars_behavior == TextServer::VC_CHARS_BEFORE_SHAPING && visible_characters >= 0 && remaining_characters >= 0) {
  494. tx = tx.substr(0, remaining_characters);
  495. }
  496. remaining_characters -= tx.length();
  497. l.text_buf->add_string(tx, font, font_size, lang, (uint64_t)it);
  498. txt += tx;
  499. l.char_count += tx.length();
  500. } break;
  501. case ITEM_IMAGE: {
  502. ItemImage *img = static_cast<ItemImage *>(it);
  503. l.text_buf->add_object((uint64_t)it, img->size, img->inline_align, 1);
  504. txt += String::chr(0xfffc);
  505. l.char_count++;
  506. remaining_characters--;
  507. } break;
  508. case ITEM_TABLE: {
  509. ItemTable *table = static_cast<ItemTable *>(it);
  510. int col_count = table->columns.size();
  511. int t_char_count = 0;
  512. // Set minimums to zero.
  513. for (int i = 0; i < col_count; i++) {
  514. table->columns[i].min_width = 0;
  515. table->columns[i].max_width = 0;
  516. table->columns[i].width = 0;
  517. }
  518. // Compute minimum width for each cell.
  519. const int available_width = p_width - theme_cache.table_h_separation * (col_count - 1);
  520. int idx = 0;
  521. for (Item *E : table->subitems) {
  522. ERR_CONTINUE(E->type != ITEM_FRAME); // Children should all be frames.
  523. ItemFrame *frame = static_cast<ItemFrame *>(E);
  524. int column = idx % col_count;
  525. float prev_h = 0;
  526. for (int i = 0; i < (int)frame->lines.size(); i++) {
  527. MutexLock sub_lock(frame->lines[i].text_buf->get_mutex());
  528. int char_offset = l.char_offset + l.char_count;
  529. int w = _find_margin(frame->lines[i].from, p_base_font, p_base_font_size) + 1;
  530. prev_h = _shape_line(frame, i, p_base_font, p_base_font_size, w, prev_h, &char_offset);
  531. int cell_ch = (char_offset - (l.char_offset + l.char_count));
  532. l.char_count += cell_ch;
  533. t_char_count += cell_ch;
  534. remaining_characters -= cell_ch;
  535. table->columns[column].min_width = MAX(table->columns[column].min_width, ceil(frame->lines[i].text_buf->get_size().x));
  536. table->columns[column].max_width = MAX(table->columns[column].max_width, ceil(frame->lines[i].text_buf->get_non_wrapped_size().x));
  537. }
  538. idx++;
  539. }
  540. // Compute available width and total ratio (for expanders).
  541. int total_ratio = 0;
  542. int remaining_width = available_width;
  543. table->total_width = theme_cache.table_h_separation;
  544. for (int i = 0; i < col_count; i++) {
  545. remaining_width -= table->columns[i].min_width;
  546. if (table->columns[i].max_width > table->columns[i].min_width) {
  547. table->columns[i].expand = true;
  548. }
  549. if (table->columns[i].expand) {
  550. total_ratio += table->columns[i].expand_ratio;
  551. }
  552. }
  553. // Assign actual widths.
  554. for (int i = 0; i < col_count; i++) {
  555. table->columns[i].width = table->columns[i].min_width;
  556. if (table->columns[i].expand && total_ratio > 0 && remaining_width > 0) {
  557. table->columns[i].width += table->columns[i].expand_ratio * remaining_width / total_ratio;
  558. }
  559. if (i != col_count - 1) {
  560. table->total_width += table->columns[i].width + theme_cache.table_h_separation;
  561. } else {
  562. table->total_width += table->columns[i].width;
  563. }
  564. }
  565. // Resize to max_width if needed and distribute the remaining space.
  566. bool table_need_fit = true;
  567. while (table_need_fit) {
  568. table_need_fit = false;
  569. // Fit slim.
  570. for (int i = 0; i < col_count; i++) {
  571. if (!table->columns[i].expand) {
  572. continue;
  573. }
  574. int dif = table->columns[i].width - table->columns[i].max_width;
  575. if (dif > 0) {
  576. table_need_fit = true;
  577. table->columns[i].width = table->columns[i].max_width;
  578. table->total_width -= dif;
  579. total_ratio -= table->columns[i].expand_ratio;
  580. }
  581. }
  582. // Grow.
  583. remaining_width = available_width - table->total_width;
  584. if (remaining_width > 0 && total_ratio > 0) {
  585. for (int i = 0; i < col_count; i++) {
  586. if (table->columns[i].expand) {
  587. int dif = table->columns[i].max_width - table->columns[i].width;
  588. if (dif > 0) {
  589. int slice = table->columns[i].expand_ratio * remaining_width / total_ratio;
  590. int incr = MIN(dif, slice);
  591. table->columns[i].width += incr;
  592. table->total_width += incr;
  593. }
  594. }
  595. }
  596. }
  597. }
  598. // Update line width and get total height.
  599. idx = 0;
  600. table->total_height = 0;
  601. table->rows.clear();
  602. table->rows_baseline.clear();
  603. Vector2 offset;
  604. float row_height = 0.0;
  605. for (const List<Item *>::Element *E = table->subitems.front(); E; E = E->next()) {
  606. ERR_CONTINUE(E->get()->type != ITEM_FRAME); // Children should all be frames.
  607. ItemFrame *frame = static_cast<ItemFrame *>(E->get());
  608. int column = idx % col_count;
  609. offset.x += frame->padding.position.x;
  610. float yofs = frame->padding.position.y;
  611. float prev_h = 0;
  612. float row_baseline = 0.0;
  613. for (int i = 0; i < (int)frame->lines.size(); i++) {
  614. MutexLock sub_lock(frame->lines[i].text_buf->get_mutex());
  615. frame->lines[i].text_buf->set_width(table->columns[column].width);
  616. table->columns[column].width = MAX(table->columns[column].width, ceil(frame->lines[i].text_buf->get_size().x));
  617. frame->lines[i].offset.y = prev_h;
  618. float h = frame->lines[i].text_buf->get_size().y + (frame->lines[i].text_buf->get_line_count() - 1) * theme_cache.line_separation;
  619. if (i > 0) {
  620. h += theme_cache.line_separation;
  621. }
  622. if (frame->min_size_over.y > 0) {
  623. h = MAX(h, frame->min_size_over.y);
  624. }
  625. if (frame->max_size_over.y > 0) {
  626. h = MIN(h, frame->max_size_over.y);
  627. }
  628. yofs += h;
  629. prev_h = frame->lines[i].offset.y + frame->lines[i].text_buf->get_size().y + frame->lines[i].text_buf->get_line_count() * theme_cache.line_separation;
  630. frame->lines[i].offset += offset;
  631. row_baseline = MAX(row_baseline, frame->lines[i].text_buf->get_line_ascent(frame->lines[i].text_buf->get_line_count() - 1));
  632. }
  633. yofs += frame->padding.size.y;
  634. offset.x += table->columns[column].width + theme_cache.table_h_separation + frame->padding.size.x;
  635. row_height = MAX(yofs, row_height);
  636. // Add row height after last column of the row or last cell of the table.
  637. if (column == col_count - 1 || E->next() == nullptr) {
  638. offset.x = 0;
  639. row_height += theme_cache.table_v_separation;
  640. table->total_height += row_height;
  641. offset.y += row_height;
  642. table->rows.push_back(row_height);
  643. table->rows_baseline.push_back(table->total_height - row_height + row_baseline);
  644. row_height = 0;
  645. }
  646. idx++;
  647. }
  648. int row_idx = (table->align_to_row < 0) ? table->rows_baseline.size() - 1 : table->align_to_row;
  649. if (table->rows_baseline.size() != 0 && row_idx < (int)table->rows_baseline.size() - 1) {
  650. l.text_buf->add_object((uint64_t)it, Size2(table->total_width, table->total_height), table->inline_align, t_char_count, Math::round(table->rows_baseline[row_idx]));
  651. } else {
  652. l.text_buf->add_object((uint64_t)it, Size2(table->total_width, table->total_height), table->inline_align, t_char_count);
  653. }
  654. txt += String::chr(0xfffc).repeat(t_char_count);
  655. } break;
  656. default:
  657. break;
  658. }
  659. }
  660. // Apply BiDi override.
  661. l.text_buf->set_bidi_override(structured_text_parser(_find_stt(l.from), st_args, txt));
  662. *r_char_offset = l.char_offset + l.char_count;
  663. l.offset.y = p_h;
  664. return _calculate_line_vertical_offset(l);
  665. }
  666. int RichTextLabel::_draw_line(ItemFrame *p_frame, int p_line, const Vector2 &p_ofs, int p_width, const Color &p_base_color, int p_outline_size, const Color &p_outline_color, const Color &p_font_shadow_color, int p_shadow_outline_size, const Point2 &p_shadow_ofs, int &r_processed_glyphs) {
  667. ERR_FAIL_COND_V(p_frame == nullptr, 0);
  668. ERR_FAIL_COND_V(p_line < 0 || p_line >= (int)p_frame->lines.size(), 0);
  669. Vector2 off;
  670. Line &l = p_frame->lines[p_line];
  671. MutexLock lock(l.text_buf->get_mutex());
  672. Item *it_from = l.from;
  673. Item *it_to = (p_line + 1 < (int)p_frame->lines.size()) ? p_frame->lines[p_line + 1].from : nullptr;
  674. if (it_from == nullptr) {
  675. return 0;
  676. }
  677. RID ci = get_canvas_item();
  678. bool rtl = (l.text_buf->get_direction() == TextServer::DIRECTION_RTL);
  679. bool lrtl = is_layout_rtl();
  680. bool trim_chars = (visible_characters >= 0) && (visible_chars_behavior == TextServer::VC_CHARS_AFTER_SHAPING);
  681. bool trim_glyphs_ltr = (visible_characters >= 0) && ((visible_chars_behavior == TextServer::VC_GLYPHS_LTR) || ((visible_chars_behavior == TextServer::VC_GLYPHS_AUTO) && !lrtl));
  682. bool trim_glyphs_rtl = (visible_characters >= 0) && ((visible_chars_behavior == TextServer::VC_GLYPHS_RTL) || ((visible_chars_behavior == TextServer::VC_GLYPHS_AUTO) && lrtl));
  683. int total_glyphs = (trim_glyphs_ltr || trim_glyphs_rtl) ? get_total_glyph_count() : 0;
  684. int visible_glyphs = total_glyphs * visible_ratio;
  685. Vector<int> list_index;
  686. Vector<ItemList *> list_items;
  687. _find_list(l.from, list_index, list_items);
  688. String prefix;
  689. for (int i = 0; i < list_index.size(); i++) {
  690. if (rtl) {
  691. prefix = prefix + ".";
  692. } else {
  693. prefix = "." + prefix;
  694. }
  695. String segment;
  696. if (list_items[i]->list_type == LIST_DOTS) {
  697. static const char32_t _prefix[2] = { 0x25CF, 0 };
  698. prefix = _prefix;
  699. break;
  700. } else if (list_items[i]->list_type == LIST_NUMBERS) {
  701. segment = itos(list_index[i]);
  702. if (is_localizing_numeral_system()) {
  703. segment = TS->format_number(segment, _find_language(l.from));
  704. }
  705. } else if (list_items[i]->list_type == LIST_LETTERS) {
  706. segment = _letters(list_index[i], list_items[i]->capitalize);
  707. } else if (list_items[i]->list_type == LIST_ROMAN) {
  708. segment = _roman(list_index[i], list_items[i]->capitalize);
  709. }
  710. if (rtl) {
  711. prefix = prefix + segment;
  712. } else {
  713. prefix = segment + prefix;
  714. }
  715. }
  716. if (!prefix.is_empty()) {
  717. Ref<Font> font = theme_cache.normal_font;
  718. int font_size = theme_cache.normal_font_size;
  719. ItemFont *font_it = _find_font(l.from);
  720. if (font_it) {
  721. if (font_it->font.is_valid()) {
  722. font = font_it->font;
  723. }
  724. if (font_it->font_size > 0) {
  725. font_size = font_it->font_size;
  726. }
  727. }
  728. ItemFontSize *font_size_it = _find_font_size(l.from);
  729. if (font_size_it && font_size_it->font_size > 0) {
  730. font_size = font_size_it->font_size;
  731. }
  732. if (rtl) {
  733. float offx = 0.0f;
  734. if (!lrtl && p_frame == main) { // Skip Scrollbar.
  735. offx -= scroll_w;
  736. }
  737. font->draw_string(ci, p_ofs + Vector2(p_width - l.offset.x + offx, l.text_buf->get_line_ascent(0)), " " + prefix, HORIZONTAL_ALIGNMENT_LEFT, l.offset.x, font_size, _find_color(l.from, p_base_color));
  738. } else {
  739. float offx = 0.0f;
  740. if (lrtl && p_frame == main) { // Skip Scrollbar.
  741. offx += scroll_w;
  742. }
  743. font->draw_string(ci, p_ofs + Vector2(offx, l.text_buf->get_line_ascent(0)), prefix + " ", HORIZONTAL_ALIGNMENT_RIGHT, l.offset.x, font_size, _find_color(l.from, p_base_color));
  744. }
  745. }
  746. // Draw dropcap.
  747. int dc_lines = l.text_buf->get_dropcap_lines();
  748. float h_off = l.text_buf->get_dropcap_size().x;
  749. if (l.dc_ol_size > 0) {
  750. l.text_buf->draw_dropcap_outline(ci, p_ofs + ((rtl) ? Vector2() : Vector2(l.offset.x, 0)), l.dc_ol_size, l.dc_ol_color);
  751. }
  752. l.text_buf->draw_dropcap(ci, p_ofs + ((rtl) ? Vector2() : Vector2(l.offset.x, 0)), l.dc_color);
  753. int line_count = 0;
  754. Size2 ctrl_size = get_size();
  755. // Draw text.
  756. for (int line = 0; line < l.text_buf->get_line_count(); line++) {
  757. if (line > 0) {
  758. off.y += theme_cache.line_separation;
  759. }
  760. if (p_ofs.y + off.y >= ctrl_size.height) {
  761. break;
  762. }
  763. const Size2 line_size = l.text_buf->get_line_size(line);
  764. if (p_ofs.y + off.y + line_size.y <= 0) {
  765. off.y += line_size.y;
  766. continue;
  767. }
  768. float width = l.text_buf->get_width();
  769. float length = line_size.x;
  770. // Draw line.
  771. line_count++;
  772. if (rtl) {
  773. off.x = p_width - l.offset.x - width;
  774. if (!lrtl && p_frame == main) { // Skip Scrollbar.
  775. off.x -= scroll_w;
  776. }
  777. } else {
  778. off.x = l.offset.x;
  779. if (lrtl && p_frame == main) { // Skip Scrollbar.
  780. off.x += scroll_w;
  781. }
  782. }
  783. // Draw text.
  784. switch (l.text_buf->get_alignment()) {
  785. case HORIZONTAL_ALIGNMENT_FILL:
  786. case HORIZONTAL_ALIGNMENT_LEFT: {
  787. if (rtl) {
  788. off.x += width - length;
  789. }
  790. } break;
  791. case HORIZONTAL_ALIGNMENT_CENTER: {
  792. off.x += Math::floor((width - length) / 2.0);
  793. } break;
  794. case HORIZONTAL_ALIGNMENT_RIGHT: {
  795. if (!rtl) {
  796. off.x += width - length;
  797. }
  798. } break;
  799. }
  800. if (line <= dc_lines) {
  801. if (rtl) {
  802. off.x -= h_off;
  803. } else {
  804. off.x += h_off;
  805. }
  806. }
  807. RID rid = l.text_buf->get_line_rid(line);
  808. //draw_rect(Rect2(p_ofs + off, TS->shaped_text_get_size(rid)), Color(1,0,0), false, 2); //DEBUG_RECTS
  809. off.y += TS->shaped_text_get_ascent(rid);
  810. // Draw inlined objects.
  811. Array objects = TS->shaped_text_get_objects(rid);
  812. for (int i = 0; i < objects.size(); i++) {
  813. Item *it = reinterpret_cast<Item *>((uint64_t)objects[i]);
  814. if (it != nullptr) {
  815. Rect2 rect = TS->shaped_text_get_object_rect(rid, objects[i]);
  816. //draw_rect(rect, Color(1,0,0), false, 2); //DEBUG_RECTS
  817. switch (it->type) {
  818. case ITEM_IMAGE: {
  819. ItemImage *img = static_cast<ItemImage *>(it);
  820. img->image->draw_rect(ci, Rect2(p_ofs + rect.position + off, rect.size), false, img->color);
  821. } break;
  822. case ITEM_TABLE: {
  823. ItemTable *table = static_cast<ItemTable *>(it);
  824. Color odd_row_bg = theme_cache.table_odd_row_bg;
  825. Color even_row_bg = theme_cache.table_even_row_bg;
  826. Color border = theme_cache.table_border;
  827. int h_separation = theme_cache.table_h_separation;
  828. int col_count = table->columns.size();
  829. int row_count = table->rows.size();
  830. int idx = 0;
  831. for (Item *E : table->subitems) {
  832. ItemFrame *frame = static_cast<ItemFrame *>(E);
  833. int col = idx % col_count;
  834. int row = idx / col_count;
  835. if (frame->lines.size() != 0 && row < row_count) {
  836. Vector2 coff = frame->lines[0].offset;
  837. if (rtl) {
  838. coff.x = rect.size.width - table->columns[col].width - coff.x;
  839. }
  840. if (row % 2 == 0) {
  841. draw_rect(Rect2(p_ofs + rect.position + off + coff - frame->padding.position, Size2(table->columns[col].width + h_separation + frame->padding.position.x + frame->padding.size.x, table->rows[row])), (frame->odd_row_bg != Color(0, 0, 0, 0) ? frame->odd_row_bg : odd_row_bg), true);
  842. } else {
  843. draw_rect(Rect2(p_ofs + rect.position + off + coff - frame->padding.position, Size2(table->columns[col].width + h_separation + frame->padding.position.x + frame->padding.size.x, table->rows[row])), (frame->even_row_bg != Color(0, 0, 0, 0) ? frame->even_row_bg : even_row_bg), true);
  844. }
  845. draw_rect(Rect2(p_ofs + rect.position + off + coff - frame->padding.position, Size2(table->columns[col].width + h_separation + frame->padding.position.x + frame->padding.size.x, table->rows[row])), (frame->border != Color(0, 0, 0, 0) ? frame->border : border), false);
  846. }
  847. for (int j = 0; j < (int)frame->lines.size(); j++) {
  848. _draw_line(frame, j, p_ofs + rect.position + off + Vector2(0, frame->lines[j].offset.y), rect.size.x, p_base_color, p_outline_size, p_outline_color, p_font_shadow_color, p_shadow_outline_size, p_shadow_ofs, r_processed_glyphs);
  849. }
  850. idx++;
  851. }
  852. } break;
  853. default:
  854. break;
  855. }
  856. }
  857. }
  858. const Glyph *glyphs = TS->shaped_text_get_glyphs(rid);
  859. int gl_size = TS->shaped_text_get_glyph_count(rid);
  860. Vector2 gloff = off;
  861. // Draw oulines and shadow.
  862. int processed_glyphs_ol = r_processed_glyphs;
  863. for (int i = 0; i < gl_size; i++) {
  864. Item *it = _get_item_at_pos(it_from, it_to, glyphs[i].start);
  865. int size = _find_outline_size(it, p_outline_size);
  866. Color font_color = _find_color(it, p_base_color);
  867. Color font_outline_color = _find_outline_color(it, p_outline_color);
  868. Color font_shadow_color = p_font_shadow_color;
  869. if ((size <= 0 || font_outline_color.a == 0) && (font_shadow_color.a == 0)) {
  870. gloff.x += glyphs[i].advance;
  871. continue;
  872. }
  873. // Get FX.
  874. ItemFade *fade = nullptr;
  875. Item *fade_item = it;
  876. while (fade_item) {
  877. if (fade_item->type == ITEM_FADE) {
  878. fade = static_cast<ItemFade *>(fade_item);
  879. break;
  880. }
  881. fade_item = fade_item->parent;
  882. }
  883. Vector<ItemFX *> fx_stack;
  884. _fetch_item_fx_stack(it, fx_stack);
  885. bool custom_fx_ok = true;
  886. Point2 fx_offset = Vector2(glyphs[i].x_off, glyphs[i].y_off);
  887. RID frid = glyphs[i].font_rid;
  888. uint32_t gl = glyphs[i].index;
  889. uint16_t gl_fl = glyphs[i].flags;
  890. uint8_t gl_cn = glyphs[i].count;
  891. bool cprev_cluster = false;
  892. bool cprev_conn = false;
  893. if (gl_cn == 0) { // Parts of the same cluster, always connected.
  894. cprev_cluster = true;
  895. }
  896. if (gl_fl & TextServer::GRAPHEME_IS_RTL) { // Check if previous grapheme cluster is connected.
  897. if (i > 0 && (glyphs[i - 1].flags & TextServer::GRAPHEME_IS_CONNECTED)) {
  898. cprev_conn = true;
  899. }
  900. } else {
  901. if (glyphs[i].flags & TextServer::GRAPHEME_IS_CONNECTED) {
  902. cprev_conn = true;
  903. }
  904. }
  905. //Apply fx.
  906. if (fade) {
  907. float faded_visibility = 1.0f;
  908. if (glyphs[i].start >= fade->starting_index) {
  909. faded_visibility -= (float)(glyphs[i].start - fade->starting_index) / (float)fade->length;
  910. faded_visibility = faded_visibility < 0.0f ? 0.0f : faded_visibility;
  911. }
  912. font_outline_color.a = faded_visibility;
  913. font_shadow_color.a = faded_visibility;
  914. }
  915. bool txt_visible = (font_outline_color.a != 0) || (font_shadow_color.a != 0);
  916. for (int j = 0; j < fx_stack.size(); j++) {
  917. ItemFX *item_fx = fx_stack[j];
  918. bool cn = cprev_cluster || (cprev_conn && item_fx->connected);
  919. if (item_fx->type == ITEM_CUSTOMFX && custom_fx_ok) {
  920. ItemCustomFX *item_custom = static_cast<ItemCustomFX *>(item_fx);
  921. Ref<CharFXTransform> charfx = item_custom->char_fx_transform;
  922. Ref<RichTextEffect> custom_effect = item_custom->custom_effect;
  923. if (!custom_effect.is_null()) {
  924. charfx->elapsed_time = item_custom->elapsed_time;
  925. charfx->range = Vector2i(l.char_offset + glyphs[i].start, l.char_offset + glyphs[i].end);
  926. charfx->relative_index = l.char_offset + glyphs[i].start - item_fx->char_ofs;
  927. charfx->visibility = txt_visible;
  928. charfx->outline = true;
  929. charfx->font = frid;
  930. charfx->glyph_index = gl;
  931. charfx->glyph_flags = gl_fl;
  932. charfx->glyph_count = gl_cn;
  933. charfx->offset = fx_offset;
  934. charfx->color = font_color;
  935. bool effect_status = custom_effect->_process_effect_impl(charfx);
  936. custom_fx_ok = effect_status;
  937. fx_offset += charfx->offset;
  938. font_color = charfx->color;
  939. frid = charfx->font;
  940. gl = charfx->glyph_index;
  941. txt_visible &= charfx->visibility;
  942. }
  943. } else if (item_fx->type == ITEM_SHAKE) {
  944. ItemShake *item_shake = static_cast<ItemShake *>(item_fx);
  945. if (!cn) {
  946. uint64_t char_current_rand = item_shake->offset_random(glyphs[i].start);
  947. uint64_t char_previous_rand = item_shake->offset_previous_random(glyphs[i].start);
  948. uint64_t max_rand = 2147483647;
  949. double current_offset = Math::remap(char_current_rand % max_rand, 0, max_rand, 0.0f, 2.f * (float)Math_PI);
  950. double previous_offset = Math::remap(char_previous_rand % max_rand, 0, max_rand, 0.0f, 2.f * (float)Math_PI);
  951. double n_time = (double)(item_shake->elapsed_time / (0.5f / item_shake->rate));
  952. n_time = (n_time > 1.0) ? 1.0 : n_time;
  953. item_shake->prev_off = Point2(Math::lerp(Math::sin(previous_offset), Math::sin(current_offset), n_time), Math::lerp(Math::cos(previous_offset), Math::cos(current_offset), n_time)) * (float)item_shake->strength / 10.0f;
  954. }
  955. fx_offset += item_shake->prev_off;
  956. } else if (item_fx->type == ITEM_WAVE) {
  957. ItemWave *item_wave = static_cast<ItemWave *>(item_fx);
  958. if (!cn) {
  959. double value = Math::sin(item_wave->frequency * item_wave->elapsed_time + ((p_ofs.x + gloff.x) / 50)) * (item_wave->amplitude / 10.0f);
  960. item_wave->prev_off = Point2(0, 1) * value;
  961. }
  962. fx_offset += item_wave->prev_off;
  963. } else if (item_fx->type == ITEM_TORNADO) {
  964. ItemTornado *item_tornado = static_cast<ItemTornado *>(item_fx);
  965. if (!cn) {
  966. double torn_x = Math::sin(item_tornado->frequency * item_tornado->elapsed_time + ((p_ofs.x + gloff.x) / 50)) * (item_tornado->radius);
  967. double torn_y = Math::cos(item_tornado->frequency * item_tornado->elapsed_time + ((p_ofs.x + gloff.x) / 50)) * (item_tornado->radius);
  968. item_tornado->prev_off = Point2(torn_x, torn_y);
  969. }
  970. fx_offset += item_tornado->prev_off;
  971. } else if (item_fx->type == ITEM_RAINBOW) {
  972. ItemRainbow *item_rainbow = static_cast<ItemRainbow *>(item_fx);
  973. font_color = font_color.from_hsv(item_rainbow->frequency * (item_rainbow->elapsed_time + ((p_ofs.x + gloff.x) / 50)), item_rainbow->saturation, item_rainbow->value, font_color.a);
  974. }
  975. }
  976. // Draw glyph outlines.
  977. const Color modulated_outline_color = font_outline_color * Color(1, 1, 1, font_color.a);
  978. const Color modulated_shadow_color = font_shadow_color * Color(1, 1, 1, font_color.a);
  979. for (int j = 0; j < glyphs[i].repeat; j++) {
  980. if (txt_visible) {
  981. bool skip = (trim_chars && l.char_offset + glyphs[i].end > visible_characters) || (trim_glyphs_ltr && (processed_glyphs_ol >= visible_glyphs)) || (trim_glyphs_rtl && (processed_glyphs_ol < total_glyphs - visible_glyphs));
  982. if (!skip && frid != RID()) {
  983. if (modulated_shadow_color.a > 0) {
  984. TS->font_draw_glyph(frid, ci, glyphs[i].font_size, p_ofs + fx_offset + gloff + p_shadow_ofs, gl, modulated_shadow_color);
  985. }
  986. if (modulated_shadow_color.a > 0 && p_shadow_outline_size > 0) {
  987. TS->font_draw_glyph_outline(frid, ci, glyphs[i].font_size, p_shadow_outline_size, p_ofs + fx_offset + gloff + p_shadow_ofs, gl, modulated_shadow_color);
  988. }
  989. if (modulated_outline_color.a != 0.0 && size > 0) {
  990. TS->font_draw_glyph_outline(frid, ci, glyphs[i].font_size, size, p_ofs + fx_offset + gloff, gl, modulated_outline_color);
  991. }
  992. }
  993. processed_glyphs_ol++;
  994. }
  995. gloff.x += glyphs[i].advance;
  996. }
  997. }
  998. Vector2 fbg_line_off = off + p_ofs;
  999. // Draw background color box
  1000. Vector2i chr_range = TS->shaped_text_get_range(rid);
  1001. _draw_fbg_boxes(ci, rid, fbg_line_off, it_from, it_to, chr_range.x, chr_range.y, 0);
  1002. // Draw main text.
  1003. Color selection_bg = theme_cache.selection_color;
  1004. int sel_start = -1;
  1005. int sel_end = -1;
  1006. if (selection.active && (selection.from_frame->lines[selection.from_line].char_offset + selection.from_char) <= (l.char_offset + TS->shaped_text_get_range(rid).y) && (selection.to_frame->lines[selection.to_line].char_offset + selection.to_char) >= (l.char_offset + TS->shaped_text_get_range(rid).x)) {
  1007. sel_start = MAX(TS->shaped_text_get_range(rid).x, (selection.from_frame->lines[selection.from_line].char_offset + selection.from_char) - l.char_offset);
  1008. sel_end = MIN(TS->shaped_text_get_range(rid).y, (selection.to_frame->lines[selection.to_line].char_offset + selection.to_char) - l.char_offset);
  1009. Vector<Vector2> sel = TS->shaped_text_get_selection(rid, sel_start, sel_end);
  1010. for (int i = 0; i < sel.size(); i++) {
  1011. Rect2 rect = Rect2(sel[i].x + p_ofs.x + off.x, p_ofs.y + off.y - TS->shaped_text_get_ascent(rid), sel[i].y - sel[i].x, TS->shaped_text_get_size(rid).y);
  1012. RenderingServer::get_singleton()->canvas_item_add_rect(ci, rect, selection_bg);
  1013. }
  1014. }
  1015. Vector2 ul_start;
  1016. bool ul_started = false;
  1017. Color ul_color;
  1018. Vector2 dot_ul_start;
  1019. bool dot_ul_started = false;
  1020. Color dot_ul_color;
  1021. Vector2 st_start;
  1022. bool st_started = false;
  1023. Color st_color;
  1024. for (int i = 0; i < gl_size; i++) {
  1025. bool selected = selection.active && (sel_start != -1) && (glyphs[i].start >= sel_start) && (glyphs[i].end <= sel_end);
  1026. Item *it = _get_item_at_pos(it_from, it_to, glyphs[i].start);
  1027. Color font_color = _find_color(it, p_base_color);
  1028. if (_find_underline(it) || (_find_meta(it, nullptr) && underline_meta)) {
  1029. if (!ul_started) {
  1030. ul_started = true;
  1031. ul_start = p_ofs + Vector2(off.x, off.y);
  1032. ul_color = font_color;
  1033. ul_color.a *= 0.5;
  1034. }
  1035. } else if (ul_started) {
  1036. ul_started = false;
  1037. float y_off = TS->shaped_text_get_underline_position(rid);
  1038. float underline_width = MAX(1.0, TS->shaped_text_get_underline_thickness(rid) * theme_cache.base_scale);
  1039. draw_line(ul_start + Vector2(0, y_off), p_ofs + Vector2(off.x, off.y + y_off), ul_color, underline_width);
  1040. }
  1041. if (_find_hint(it, nullptr) && underline_hint) {
  1042. if (!dot_ul_started) {
  1043. dot_ul_started = true;
  1044. dot_ul_start = p_ofs + Vector2(off.x, off.y);
  1045. dot_ul_color = font_color;
  1046. dot_ul_color.a *= 0.5;
  1047. }
  1048. } else if (dot_ul_started) {
  1049. dot_ul_started = false;
  1050. float y_off = TS->shaped_text_get_underline_position(rid);
  1051. float underline_width = MAX(1.0, TS->shaped_text_get_underline_thickness(rid) * theme_cache.base_scale);
  1052. draw_dashed_line(dot_ul_start + Vector2(0, y_off), p_ofs + Vector2(off.x, off.y + y_off), dot_ul_color, underline_width, MAX(2.0, underline_width * 2));
  1053. }
  1054. if (_find_strikethrough(it)) {
  1055. if (!st_started) {
  1056. st_started = true;
  1057. st_start = p_ofs + Vector2(off.x, off.y);
  1058. st_color = font_color;
  1059. st_color.a *= 0.5;
  1060. }
  1061. } else if (st_started) {
  1062. st_started = false;
  1063. float y_off = -TS->shaped_text_get_ascent(rid) + TS->shaped_text_get_size(rid).y / 2;
  1064. float underline_width = MAX(1.0, TS->shaped_text_get_underline_thickness(rid) * theme_cache.base_scale);
  1065. draw_line(st_start + Vector2(0, y_off), p_ofs + Vector2(off.x, off.y + y_off), st_color, underline_width);
  1066. }
  1067. // Get FX.
  1068. ItemFade *fade = nullptr;
  1069. Item *fade_item = it;
  1070. while (fade_item) {
  1071. if (fade_item->type == ITEM_FADE) {
  1072. fade = static_cast<ItemFade *>(fade_item);
  1073. break;
  1074. }
  1075. fade_item = fade_item->parent;
  1076. }
  1077. Vector<ItemFX *> fx_stack;
  1078. _fetch_item_fx_stack(it, fx_stack);
  1079. bool custom_fx_ok = true;
  1080. Point2 fx_offset = Vector2(glyphs[i].x_off, glyphs[i].y_off);
  1081. RID frid = glyphs[i].font_rid;
  1082. uint32_t gl = glyphs[i].index;
  1083. uint16_t gl_fl = glyphs[i].flags;
  1084. uint8_t gl_cn = glyphs[i].count;
  1085. bool cprev_cluster = false;
  1086. bool cprev_conn = false;
  1087. if (gl_cn == 0) { // Parts of the same grapheme cluster, always connected.
  1088. cprev_cluster = true;
  1089. }
  1090. if (gl_fl & TextServer::GRAPHEME_IS_RTL) { // Check if previous grapheme cluster is connected.
  1091. if (i > 0 && (glyphs[i - 1].flags & TextServer::GRAPHEME_IS_CONNECTED)) {
  1092. cprev_conn = true;
  1093. }
  1094. } else {
  1095. if (glyphs[i].flags & TextServer::GRAPHEME_IS_CONNECTED) {
  1096. cprev_conn = true;
  1097. }
  1098. }
  1099. //Apply fx.
  1100. if (fade) {
  1101. float faded_visibility = 1.0f;
  1102. if (glyphs[i].start >= fade->starting_index) {
  1103. faded_visibility -= (float)(glyphs[i].start - fade->starting_index) / (float)fade->length;
  1104. faded_visibility = faded_visibility < 0.0f ? 0.0f : faded_visibility;
  1105. }
  1106. font_color.a = faded_visibility;
  1107. }
  1108. bool txt_visible = (font_color.a != 0);
  1109. for (int j = 0; j < fx_stack.size(); j++) {
  1110. ItemFX *item_fx = fx_stack[j];
  1111. bool cn = cprev_cluster || (cprev_conn && item_fx->connected);
  1112. if (item_fx->type == ITEM_CUSTOMFX && custom_fx_ok) {
  1113. ItemCustomFX *item_custom = static_cast<ItemCustomFX *>(item_fx);
  1114. Ref<CharFXTransform> charfx = item_custom->char_fx_transform;
  1115. Ref<RichTextEffect> custom_effect = item_custom->custom_effect;
  1116. if (!custom_effect.is_null()) {
  1117. charfx->elapsed_time = item_custom->elapsed_time;
  1118. charfx->range = Vector2i(l.char_offset + glyphs[i].start, l.char_offset + glyphs[i].end);
  1119. charfx->relative_index = l.char_offset + glyphs[i].start - item_fx->char_ofs;
  1120. charfx->visibility = txt_visible;
  1121. charfx->outline = false;
  1122. charfx->font = frid;
  1123. charfx->glyph_index = gl;
  1124. charfx->glyph_flags = gl_fl;
  1125. charfx->glyph_count = gl_cn;
  1126. charfx->offset = fx_offset;
  1127. charfx->color = font_color;
  1128. bool effect_status = custom_effect->_process_effect_impl(charfx);
  1129. custom_fx_ok = effect_status;
  1130. fx_offset += charfx->offset;
  1131. font_color = charfx->color;
  1132. frid = charfx->font;
  1133. gl = charfx->glyph_index;
  1134. txt_visible &= charfx->visibility;
  1135. }
  1136. } else if (item_fx->type == ITEM_SHAKE) {
  1137. ItemShake *item_shake = static_cast<ItemShake *>(item_fx);
  1138. if (!cn) {
  1139. uint64_t char_current_rand = item_shake->offset_random(glyphs[i].start);
  1140. uint64_t char_previous_rand = item_shake->offset_previous_random(glyphs[i].start);
  1141. uint64_t max_rand = 2147483647;
  1142. double current_offset = Math::remap(char_current_rand % max_rand, 0, max_rand, 0.0f, 2.f * (float)Math_PI);
  1143. double previous_offset = Math::remap(char_previous_rand % max_rand, 0, max_rand, 0.0f, 2.f * (float)Math_PI);
  1144. double n_time = (double)(item_shake->elapsed_time / (0.5f / item_shake->rate));
  1145. n_time = (n_time > 1.0) ? 1.0 : n_time;
  1146. item_shake->prev_off = Point2(Math::lerp(Math::sin(previous_offset), Math::sin(current_offset), n_time), Math::lerp(Math::cos(previous_offset), Math::cos(current_offset), n_time)) * (float)item_shake->strength / 10.0f;
  1147. }
  1148. fx_offset += item_shake->prev_off;
  1149. } else if (item_fx->type == ITEM_WAVE) {
  1150. ItemWave *item_wave = static_cast<ItemWave *>(item_fx);
  1151. if (!cn) {
  1152. double value = Math::sin(item_wave->frequency * item_wave->elapsed_time + ((p_ofs.x + off.x) / 50)) * (item_wave->amplitude / 10.0f);
  1153. item_wave->prev_off = Point2(0, 1) * value;
  1154. }
  1155. fx_offset += item_wave->prev_off;
  1156. } else if (item_fx->type == ITEM_TORNADO) {
  1157. ItemTornado *item_tornado = static_cast<ItemTornado *>(item_fx);
  1158. if (!cn) {
  1159. double torn_x = Math::sin(item_tornado->frequency * item_tornado->elapsed_time + ((p_ofs.x + off.x) / 50)) * (item_tornado->radius);
  1160. double torn_y = Math::cos(item_tornado->frequency * item_tornado->elapsed_time + ((p_ofs.x + off.x) / 50)) * (item_tornado->radius);
  1161. item_tornado->prev_off = Point2(torn_x, torn_y);
  1162. }
  1163. fx_offset += item_tornado->prev_off;
  1164. } else if (item_fx->type == ITEM_RAINBOW) {
  1165. ItemRainbow *item_rainbow = static_cast<ItemRainbow *>(item_fx);
  1166. font_color = font_color.from_hsv(item_rainbow->frequency * (item_rainbow->elapsed_time + ((p_ofs.x + off.x) / 50)), item_rainbow->saturation, item_rainbow->value, font_color.a);
  1167. }
  1168. }
  1169. if (selected && use_selected_font_color) {
  1170. font_color = theme_cache.font_selected_color;
  1171. }
  1172. // Draw glyphs.
  1173. for (int j = 0; j < glyphs[i].repeat; j++) {
  1174. bool skip = (trim_chars && l.char_offset + glyphs[i].end > visible_characters) || (trim_glyphs_ltr && (r_processed_glyphs >= visible_glyphs)) || (trim_glyphs_rtl && (r_processed_glyphs < total_glyphs - visible_glyphs));
  1175. if (txt_visible) {
  1176. if (!skip) {
  1177. if (frid != RID()) {
  1178. TS->font_draw_glyph(frid, ci, glyphs[i].font_size, p_ofs + fx_offset + off, gl, font_color);
  1179. } else if ((glyphs[i].flags & TextServer::GRAPHEME_IS_VIRTUAL) != TextServer::GRAPHEME_IS_VIRTUAL) {
  1180. TS->draw_hex_code_box(ci, glyphs[i].font_size, p_ofs + fx_offset + off, gl, font_color);
  1181. }
  1182. }
  1183. r_processed_glyphs++;
  1184. }
  1185. if (skip) {
  1186. // End underline/overline/strikethrough is previous glyph is skipped.
  1187. if (ul_started) {
  1188. ul_started = false;
  1189. float y_off = TS->shaped_text_get_underline_position(rid);
  1190. float underline_width = MAX(1.0, TS->shaped_text_get_underline_thickness(rid) * theme_cache.base_scale);
  1191. draw_line(ul_start + Vector2(0, y_off), p_ofs + Vector2(off.x, off.y + y_off), ul_color, underline_width);
  1192. }
  1193. if (dot_ul_started) {
  1194. dot_ul_started = false;
  1195. float y_off = TS->shaped_text_get_underline_position(rid);
  1196. float underline_width = MAX(1.0, TS->shaped_text_get_underline_thickness(rid) * theme_cache.base_scale);
  1197. draw_dashed_line(dot_ul_start + Vector2(0, y_off), p_ofs + Vector2(off.x, off.y + y_off), dot_ul_color, underline_width, MAX(2.0, underline_width * 2));
  1198. }
  1199. if (st_started) {
  1200. st_started = false;
  1201. float y_off = -TS->shaped_text_get_ascent(rid) + TS->shaped_text_get_size(rid).y / 2;
  1202. float underline_width = MAX(1.0, TS->shaped_text_get_underline_thickness(rid) * theme_cache.base_scale);
  1203. draw_line(st_start + Vector2(0, y_off), p_ofs + Vector2(off.x, off.y + y_off), st_color, underline_width);
  1204. }
  1205. }
  1206. off.x += glyphs[i].advance;
  1207. }
  1208. }
  1209. if (ul_started) {
  1210. ul_started = false;
  1211. float y_off = TS->shaped_text_get_underline_position(rid);
  1212. float underline_width = MAX(1.0, TS->shaped_text_get_underline_thickness(rid) * theme_cache.base_scale);
  1213. draw_line(ul_start + Vector2(0, y_off), p_ofs + Vector2(off.x, off.y + y_off), ul_color, underline_width);
  1214. }
  1215. if (dot_ul_started) {
  1216. dot_ul_started = false;
  1217. float y_off = TS->shaped_text_get_underline_position(rid);
  1218. float underline_width = MAX(1.0, TS->shaped_text_get_underline_thickness(rid) * theme_cache.base_scale);
  1219. draw_dashed_line(dot_ul_start + Vector2(0, y_off), p_ofs + Vector2(off.x, off.y + y_off), dot_ul_color, underline_width, MAX(2.0, underline_width * 2));
  1220. }
  1221. if (st_started) {
  1222. st_started = false;
  1223. float y_off = -TS->shaped_text_get_ascent(rid) + TS->shaped_text_get_size(rid).y / 2;
  1224. float underline_width = MAX(1.0, TS->shaped_text_get_underline_thickness(rid) * theme_cache.base_scale);
  1225. draw_line(st_start + Vector2(0, y_off), p_ofs + Vector2(off.x, off.y + y_off), st_color, underline_width);
  1226. }
  1227. // Draw foreground color box
  1228. _draw_fbg_boxes(ci, rid, fbg_line_off, it_from, it_to, chr_range.x, chr_range.y, 1);
  1229. off.y += TS->shaped_text_get_descent(rid);
  1230. }
  1231. return line_count;
  1232. }
  1233. void RichTextLabel::_find_click(ItemFrame *p_frame, const Point2i &p_click, ItemFrame **r_click_frame, int *r_click_line, Item **r_click_item, int *r_click_char, bool *r_outside, bool p_meta) {
  1234. if (r_click_item) {
  1235. *r_click_item = nullptr;
  1236. }
  1237. if (r_click_char != nullptr) {
  1238. *r_click_char = 0;
  1239. }
  1240. if (r_outside != nullptr) {
  1241. *r_outside = true;
  1242. }
  1243. Size2 size = get_size();
  1244. Rect2 text_rect = _get_text_rect();
  1245. int vofs = vscroll->get_value();
  1246. // Search for the first line.
  1247. int to_line = main->first_invalid_line.load();
  1248. int from_line = _find_first_line(0, to_line, vofs);
  1249. Point2 ofs = text_rect.get_position() + Vector2(0, main->lines[from_line].offset.y - vofs);
  1250. while (ofs.y < size.height && from_line < to_line) {
  1251. MutexLock lock(main->lines[from_line].text_buf->get_mutex());
  1252. _find_click_in_line(p_frame, from_line, ofs, text_rect.size.x, p_click, r_click_frame, r_click_line, r_click_item, r_click_char, false, p_meta);
  1253. ofs.y += main->lines[from_line].text_buf->get_size().y + main->lines[from_line].text_buf->get_line_count() * theme_cache.line_separation;
  1254. if (((r_click_item != nullptr) && ((*r_click_item) != nullptr)) || ((r_click_frame != nullptr) && ((*r_click_frame) != nullptr))) {
  1255. if (r_outside != nullptr) {
  1256. *r_outside = false;
  1257. }
  1258. return;
  1259. }
  1260. from_line++;
  1261. }
  1262. }
  1263. float RichTextLabel::_find_click_in_line(ItemFrame *p_frame, int p_line, const Vector2 &p_ofs, int p_width, const Point2i &p_click, ItemFrame **r_click_frame, int *r_click_line, Item **r_click_item, int *r_click_char, bool p_table, bool p_meta) {
  1264. Vector2 off;
  1265. bool line_clicked = false;
  1266. float text_rect_begin = 0.0;
  1267. int char_pos = -1;
  1268. Line &l = p_frame->lines[p_line];
  1269. MutexLock lock(l.text_buf->get_mutex());
  1270. bool rtl = (l.text_buf->get_direction() == TextServer::DIRECTION_RTL);
  1271. bool lrtl = is_layout_rtl();
  1272. // Table hit test results.
  1273. bool table_hit = false;
  1274. Vector2i table_range;
  1275. float table_offy = 0.f;
  1276. ItemFrame *table_click_frame = nullptr;
  1277. int table_click_line = -1;
  1278. Item *table_click_item = nullptr;
  1279. int table_click_char = -1;
  1280. for (int line = 0; line < l.text_buf->get_line_count(); line++) {
  1281. RID rid = l.text_buf->get_line_rid(line);
  1282. float width = l.text_buf->get_width();
  1283. float length = TS->shaped_text_get_width(rid);
  1284. if (rtl) {
  1285. off.x = p_width - l.offset.x - width;
  1286. if (!lrtl && p_frame == main) { // Skip Scrollbar.
  1287. off.x -= scroll_w;
  1288. }
  1289. } else {
  1290. off.x = l.offset.x;
  1291. if (lrtl && p_frame == main) { // Skip Scrollbar.
  1292. off.x += scroll_w;
  1293. }
  1294. }
  1295. switch (l.text_buf->get_alignment()) {
  1296. case HORIZONTAL_ALIGNMENT_FILL:
  1297. case HORIZONTAL_ALIGNMENT_LEFT: {
  1298. if (rtl) {
  1299. off.x += width - length;
  1300. }
  1301. } break;
  1302. case HORIZONTAL_ALIGNMENT_CENTER: {
  1303. off.x += Math::floor((width - length) / 2.0);
  1304. } break;
  1305. case HORIZONTAL_ALIGNMENT_RIGHT: {
  1306. if (!rtl) {
  1307. off.x += width - length;
  1308. }
  1309. } break;
  1310. }
  1311. off.y += TS->shaped_text_get_ascent(rid);
  1312. Array objects = TS->shaped_text_get_objects(rid);
  1313. for (int i = 0; i < objects.size(); i++) {
  1314. Item *it = reinterpret_cast<Item *>((uint64_t)objects[i]);
  1315. if (it != nullptr) {
  1316. Rect2 rect = TS->shaped_text_get_object_rect(rid, objects[i]);
  1317. rect.position += p_ofs + off;
  1318. if (p_click.y >= rect.position.y && p_click.y <= rect.position.y + rect.size.y) {
  1319. switch (it->type) {
  1320. case ITEM_TABLE: {
  1321. ItemTable *table = static_cast<ItemTable *>(it);
  1322. int idx = 0;
  1323. int col_count = table->columns.size();
  1324. int row_count = table->rows.size();
  1325. for (Item *E : table->subitems) {
  1326. ItemFrame *frame = static_cast<ItemFrame *>(E);
  1327. int col = idx % col_count;
  1328. int row = idx / col_count;
  1329. if (frame->lines.size() != 0 && row < row_count) {
  1330. Vector2 coff = frame->lines[0].offset;
  1331. if (rtl) {
  1332. coff.x = rect.size.width - table->columns[col].width - coff.x;
  1333. }
  1334. Rect2 crect = Rect2(rect.position + coff - frame->padding.position, Size2(table->columns[col].width + theme_cache.table_h_separation, table->rows[row] + theme_cache.table_v_separation) + frame->padding.position + frame->padding.size);
  1335. if (col == col_count - 1) {
  1336. if (rtl) {
  1337. crect.size.x = crect.position.x + crect.size.x;
  1338. crect.position.x = 0;
  1339. } else {
  1340. crect.size.x = get_size().x;
  1341. }
  1342. }
  1343. if (crect.has_point(p_click)) {
  1344. for (int j = 0; j < (int)frame->lines.size(); j++) {
  1345. _find_click_in_line(frame, j, rect.position + Vector2(0, frame->lines[j].offset.y), rect.size.x, p_click, &table_click_frame, &table_click_line, &table_click_item, &table_click_char, true, p_meta);
  1346. if (table_click_frame && table_click_item) {
  1347. // Save cell detected cell hit data.
  1348. table_range = Vector2i(INT32_MAX, 0);
  1349. for (Item *F : table->subitems) {
  1350. ItemFrame *sub_frame = static_cast<ItemFrame *>(F);
  1351. for (int k = 0; k < (int)sub_frame->lines.size(); k++) {
  1352. table_range.x = MIN(table_range.x, sub_frame->lines[k].char_offset);
  1353. table_range.y = MAX(table_range.y, sub_frame->lines[k].char_offset + sub_frame->lines[k].char_count);
  1354. }
  1355. }
  1356. table_offy = off.y;
  1357. table_hit = true;
  1358. }
  1359. }
  1360. }
  1361. }
  1362. idx++;
  1363. }
  1364. } break;
  1365. default:
  1366. break;
  1367. }
  1368. }
  1369. }
  1370. }
  1371. Rect2 rect = Rect2(p_ofs + off - Vector2(0, TS->shaped_text_get_ascent(rid)) - p_frame->padding.position, TS->shaped_text_get_size(rid) + p_frame->padding.position + p_frame->padding.size);
  1372. if (p_table) {
  1373. rect.size.y += theme_cache.table_v_separation;
  1374. }
  1375. if (p_click.y >= rect.position.y && p_click.y <= rect.position.y + rect.size.y) {
  1376. if ((!rtl && p_click.x >= rect.position.x) || (rtl && p_click.x <= rect.position.x + rect.size.x)) {
  1377. if (p_meta) {
  1378. int64_t glyph_idx = TS->shaped_text_hit_test_grapheme(rid, p_click.x - rect.position.x);
  1379. if (glyph_idx >= 0) {
  1380. const Glyph *glyphs = TS->shaped_text_get_glyphs(rid);
  1381. char_pos = glyphs[glyph_idx].start;
  1382. }
  1383. } else {
  1384. char_pos = TS->shaped_text_hit_test_position(rid, p_click.x - rect.position.x);
  1385. }
  1386. }
  1387. line_clicked = true;
  1388. text_rect_begin = rtl ? rect.position.x + rect.size.x : rect.position.x;
  1389. }
  1390. // If table hit was detected, and line hit is in the table bounds use table hit.
  1391. if (table_hit && (((char_pos + p_frame->lines[p_line].char_offset) >= table_range.x && (char_pos + p_frame->lines[p_line].char_offset) <= table_range.y) || char_pos == -1)) {
  1392. if (r_click_frame != nullptr) {
  1393. *r_click_frame = table_click_frame;
  1394. }
  1395. if (r_click_line != nullptr) {
  1396. *r_click_line = table_click_line;
  1397. }
  1398. if (r_click_item != nullptr) {
  1399. *r_click_item = table_click_item;
  1400. }
  1401. if (r_click_char != nullptr) {
  1402. *r_click_char = table_click_char;
  1403. }
  1404. return table_offy;
  1405. }
  1406. off.y += TS->shaped_text_get_descent(rid) + theme_cache.line_separation;
  1407. }
  1408. // Text line hit.
  1409. if (line_clicked) {
  1410. // Find item.
  1411. if (r_click_item != nullptr) {
  1412. Item *it = p_frame->lines[p_line].from;
  1413. Item *it_to = (p_line + 1 < (int)p_frame->lines.size()) ? p_frame->lines[p_line + 1].from : nullptr;
  1414. if (char_pos >= 0) {
  1415. *r_click_item = _get_item_at_pos(it, it_to, char_pos);
  1416. } else {
  1417. int stop = text_rect_begin;
  1418. *r_click_item = _find_indentable(it);
  1419. while (*r_click_item) {
  1420. Ref<Font> font = theme_cache.normal_font;
  1421. int font_size = theme_cache.normal_font_size;
  1422. ItemFont *font_it = _find_font(*r_click_item);
  1423. if (font_it) {
  1424. if (font_it->font.is_valid()) {
  1425. font = font_it->font;
  1426. }
  1427. if (font_it->font_size > 0) {
  1428. font_size = font_it->font_size;
  1429. }
  1430. }
  1431. ItemFontSize *font_size_it = _find_font_size(*r_click_item);
  1432. if (font_size_it && font_size_it->font_size > 0) {
  1433. font_size = font_size_it->font_size;
  1434. }
  1435. if (rtl) {
  1436. stop += tab_size * font->get_char_size(' ', font_size).width;
  1437. if (stop > p_click.x) {
  1438. break;
  1439. }
  1440. } else {
  1441. stop -= tab_size * font->get_char_size(' ', font_size).width;
  1442. if (stop < p_click.x) {
  1443. break;
  1444. }
  1445. }
  1446. *r_click_item = _find_indentable((*r_click_item)->parent);
  1447. }
  1448. }
  1449. }
  1450. if (r_click_frame != nullptr) {
  1451. *r_click_frame = p_frame;
  1452. }
  1453. if (r_click_line != nullptr) {
  1454. *r_click_line = p_line;
  1455. }
  1456. if (r_click_char != nullptr) {
  1457. *r_click_char = char_pos;
  1458. }
  1459. }
  1460. return off.y;
  1461. }
  1462. void RichTextLabel::_scroll_changed(double) {
  1463. if (updating_scroll) {
  1464. return;
  1465. }
  1466. if (scroll_follow && vscroll->get_value() >= (vscroll->get_max() - vscroll->get_page())) {
  1467. scroll_following = true;
  1468. } else {
  1469. scroll_following = false;
  1470. }
  1471. scroll_updated = true;
  1472. queue_redraw();
  1473. }
  1474. void RichTextLabel::_update_fx(RichTextLabel::ItemFrame *p_frame, double p_delta_time) {
  1475. Item *it = p_frame;
  1476. while (it) {
  1477. ItemFX *ifx = nullptr;
  1478. if (it->type == ITEM_CUSTOMFX || it->type == ITEM_SHAKE || it->type == ITEM_WAVE || it->type == ITEM_TORNADO || it->type == ITEM_RAINBOW) {
  1479. ifx = static_cast<ItemFX *>(it);
  1480. }
  1481. if (!ifx) {
  1482. it = _get_next_item(it, true);
  1483. continue;
  1484. }
  1485. ifx->elapsed_time += p_delta_time;
  1486. ItemShake *shake = nullptr;
  1487. if (it->type == ITEM_SHAKE) {
  1488. shake = static_cast<ItemShake *>(it);
  1489. }
  1490. if (shake) {
  1491. bool cycle = (shake->elapsed_time > (1.0f / shake->rate));
  1492. if (cycle) {
  1493. shake->elapsed_time -= (1.0f / shake->rate);
  1494. shake->reroll_random();
  1495. }
  1496. }
  1497. it = _get_next_item(it, true);
  1498. }
  1499. }
  1500. int RichTextLabel::_find_first_line(int p_from, int p_to, int p_vofs) const {
  1501. int l = p_from;
  1502. int r = p_to;
  1503. while (l < r) {
  1504. int m = Math::floor(double(l + r) / 2.0);
  1505. MutexLock lock(main->lines[m].text_buf->get_mutex());
  1506. int ofs = _calculate_line_vertical_offset(main->lines[m]);
  1507. if (ofs < p_vofs) {
  1508. l = m + 1;
  1509. } else {
  1510. r = m;
  1511. }
  1512. }
  1513. return MIN(l, (int)main->lines.size() - 1);
  1514. }
  1515. _FORCE_INLINE_ float RichTextLabel::_calculate_line_vertical_offset(const RichTextLabel::Line &line) const {
  1516. return line.get_height(theme_cache.line_separation);
  1517. }
  1518. void RichTextLabel::_update_theme_item_cache() {
  1519. Control::_update_theme_item_cache();
  1520. theme_cache.normal_style = get_theme_stylebox(SNAME("normal"));
  1521. theme_cache.focus_style = get_theme_stylebox(SNAME("focus"));
  1522. theme_cache.progress_bg_style = get_theme_stylebox(SNAME("background"), SNAME("ProgressBar"));
  1523. theme_cache.progress_fg_style = get_theme_stylebox(SNAME("fill"), SNAME("ProgressBar"));
  1524. theme_cache.line_separation = get_theme_constant(SNAME("line_separation"));
  1525. theme_cache.normal_font = get_theme_font(SNAME("normal_font"));
  1526. theme_cache.normal_font_size = get_theme_font_size(SNAME("normal_font_size"));
  1527. theme_cache.default_color = get_theme_color(SNAME("default_color"));
  1528. theme_cache.font_selected_color = get_theme_color(SNAME("font_selected_color"));
  1529. use_selected_font_color = theme_cache.font_selected_color != Color(0, 0, 0, 0);
  1530. theme_cache.selection_color = get_theme_color(SNAME("selection_color"));
  1531. theme_cache.font_outline_color = get_theme_color(SNAME("font_outline_color"));
  1532. theme_cache.font_shadow_color = get_theme_color(SNAME("font_shadow_color"));
  1533. theme_cache.shadow_outline_size = get_theme_constant(SNAME("shadow_outline_size"));
  1534. theme_cache.shadow_offset_x = get_theme_constant(SNAME("shadow_offset_x"));
  1535. theme_cache.shadow_offset_y = get_theme_constant(SNAME("shadow_offset_y"));
  1536. theme_cache.outline_size = get_theme_constant(SNAME("outline_size"));
  1537. theme_cache.bold_font = get_theme_font(SNAME("bold_font"));
  1538. theme_cache.bold_font_size = get_theme_font_size(SNAME("bold_font_size"));
  1539. theme_cache.bold_italics_font = get_theme_font(SNAME("bold_italics_font"));
  1540. theme_cache.bold_italics_font_size = get_theme_font_size(SNAME("bold_italics_font_size"));
  1541. theme_cache.italics_font = get_theme_font(SNAME("italics_font"));
  1542. theme_cache.italics_font_size = get_theme_font_size(SNAME("italics_font_size"));
  1543. theme_cache.mono_font = get_theme_font(SNAME("mono_font"));
  1544. theme_cache.mono_font_size = get_theme_font_size(SNAME("mono_font_size"));
  1545. theme_cache.table_h_separation = get_theme_constant(SNAME("table_h_separation"));
  1546. theme_cache.table_v_separation = get_theme_constant(SNAME("table_v_separation"));
  1547. theme_cache.table_odd_row_bg = get_theme_color(SNAME("table_odd_row_bg"));
  1548. theme_cache.table_even_row_bg = get_theme_color(SNAME("table_even_row_bg"));
  1549. theme_cache.table_border = get_theme_color(SNAME("table_border"));
  1550. theme_cache.base_scale = get_theme_default_base_scale();
  1551. }
  1552. void RichTextLabel::_notification(int p_what) {
  1553. switch (p_what) {
  1554. case NOTIFICATION_MOUSE_EXIT: {
  1555. if (meta_hovering) {
  1556. meta_hovering = nullptr;
  1557. emit_signal(SNAME("meta_hover_ended"), current_meta);
  1558. current_meta = false;
  1559. queue_redraw();
  1560. }
  1561. } break;
  1562. case NOTIFICATION_RESIZED: {
  1563. _stop_thread();
  1564. main->first_resized_line.store(0); //invalidate ALL
  1565. queue_redraw();
  1566. } break;
  1567. case NOTIFICATION_THEME_CHANGED: {
  1568. _stop_thread();
  1569. main->first_invalid_font_line.store(0); //invalidate ALL
  1570. queue_redraw();
  1571. } break;
  1572. case NOTIFICATION_ENTER_TREE: {
  1573. _stop_thread();
  1574. if (!text.is_empty()) {
  1575. set_text(text);
  1576. }
  1577. main->first_invalid_line.store(0); //invalidate ALL
  1578. queue_redraw();
  1579. } break;
  1580. case NOTIFICATION_PREDELETE:
  1581. case NOTIFICATION_EXIT_TREE: {
  1582. _stop_thread();
  1583. } break;
  1584. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED:
  1585. case NOTIFICATION_TRANSLATION_CHANGED: {
  1586. _stop_thread();
  1587. main->first_invalid_line.store(0); //invalidate ALL
  1588. queue_redraw();
  1589. } break;
  1590. case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
  1591. if (is_visible_in_tree()) {
  1592. queue_redraw();
  1593. }
  1594. } break;
  1595. case NOTIFICATION_DRAW: {
  1596. RID ci = get_canvas_item();
  1597. Size2 size = get_size();
  1598. draw_style_box(theme_cache.normal_style, Rect2(Point2(), size));
  1599. if (has_focus()) {
  1600. RenderingServer::get_singleton()->canvas_item_add_clip_ignore(ci, true);
  1601. draw_style_box(theme_cache.focus_style, Rect2(Point2(), size));
  1602. RenderingServer::get_singleton()->canvas_item_add_clip_ignore(ci, false);
  1603. }
  1604. // Start text shaping.
  1605. if (_validate_line_caches()) {
  1606. set_physics_process_internal(false); // Disable auto refresh, if text is fully processed.
  1607. } else {
  1608. // Draw loading progress bar.
  1609. if ((progress_delay > 0) && (OS::get_singleton()->get_ticks_msec() - loading_started >= (uint64_t)progress_delay)) {
  1610. Vector2 p_size = Vector2(size.width - (theme_cache.normal_style->get_offset().x + vscroll->get_combined_minimum_size().width) * 2, vscroll->get_combined_minimum_size().width);
  1611. Vector2 p_pos = Vector2(theme_cache.normal_style->get_offset().x, size.height - theme_cache.normal_style->get_offset().y - vscroll->get_combined_minimum_size().width);
  1612. draw_style_box(theme_cache.progress_bg_style, Rect2(p_pos, p_size));
  1613. bool right_to_left = is_layout_rtl();
  1614. double r = loaded.load();
  1615. int mp = theme_cache.progress_fg_style->get_minimum_size().width;
  1616. int p = round(r * (p_size.width - mp));
  1617. if (right_to_left) {
  1618. int p_remaining = round((1.0 - r) * (p_size.width - mp));
  1619. draw_style_box(theme_cache.progress_fg_style, Rect2(p_pos + Point2(p_remaining, 0), Size2(p + theme_cache.progress_fg_style->get_minimum_size().width, p_size.height)));
  1620. } else {
  1621. draw_style_box(theme_cache.progress_fg_style, Rect2(p_pos, Size2(p + theme_cache.progress_fg_style->get_minimum_size().width, p_size.height)));
  1622. }
  1623. }
  1624. }
  1625. // Draw main text.
  1626. Rect2 text_rect = _get_text_rect();
  1627. float vofs = vscroll->get_value();
  1628. // Search for the first line.
  1629. int to_line = main->first_invalid_line.load();
  1630. int from_line = _find_first_line(0, to_line, vofs);
  1631. Point2 shadow_ofs(theme_cache.shadow_offset_x, theme_cache.shadow_offset_y);
  1632. visible_paragraph_count = 0;
  1633. visible_line_count = 0;
  1634. // New cache draw.
  1635. Point2 ofs = text_rect.get_position() + Vector2(0, main->lines[from_line].offset.y - vofs);
  1636. int processed_glyphs = 0;
  1637. while (ofs.y < size.height && from_line < to_line) {
  1638. MutexLock lock(main->lines[from_line].text_buf->get_mutex());
  1639. visible_paragraph_count++;
  1640. visible_line_count += _draw_line(main, from_line, ofs, text_rect.size.x, theme_cache.default_color, theme_cache.outline_size, theme_cache.font_outline_color, theme_cache.font_shadow_color, theme_cache.shadow_outline_size, shadow_ofs, processed_glyphs);
  1641. ofs.y += main->lines[from_line].text_buf->get_size().y + main->lines[from_line].text_buf->get_line_count() * theme_cache.line_separation;
  1642. from_line++;
  1643. }
  1644. } break;
  1645. case NOTIFICATION_INTERNAL_PROCESS: {
  1646. if (is_visible_in_tree()) {
  1647. if (!is_ready()) {
  1648. return;
  1649. }
  1650. double dt = get_process_delta_time();
  1651. _update_fx(main, dt);
  1652. queue_redraw();
  1653. }
  1654. } break;
  1655. case NOTIFICATION_FOCUS_EXIT: {
  1656. if (deselect_on_focus_loss_enabled) {
  1657. deselect();
  1658. }
  1659. } break;
  1660. case NOTIFICATION_DRAG_END: {
  1661. selection.drag_attempt = false;
  1662. } break;
  1663. }
  1664. }
  1665. Control::CursorShape RichTextLabel::get_cursor_shape(const Point2 &p_pos) const {
  1666. if (selection.click_item) {
  1667. return CURSOR_IBEAM;
  1668. }
  1669. Item *item = nullptr;
  1670. bool outside = true;
  1671. const_cast<RichTextLabel *>(this)->_find_click(main, p_pos, nullptr, nullptr, &item, nullptr, &outside, true);
  1672. if (item && !outside && const_cast<RichTextLabel *>(this)->_find_meta(item, nullptr)) {
  1673. return CURSOR_POINTING_HAND;
  1674. }
  1675. return get_default_cursor_shape();
  1676. }
  1677. void RichTextLabel::gui_input(const Ref<InputEvent> &p_event) {
  1678. ERR_FAIL_COND(p_event.is_null());
  1679. Ref<InputEventMouseButton> b = p_event;
  1680. if (b.is_valid()) {
  1681. if (b->get_button_index() == MouseButton::LEFT) {
  1682. if (b->is_pressed() && !b->is_double_click()) {
  1683. scroll_updated = false;
  1684. ItemFrame *c_frame = nullptr;
  1685. int c_line = 0;
  1686. Item *c_item = nullptr;
  1687. int c_index = 0;
  1688. bool outside;
  1689. selection.drag_attempt = false;
  1690. _find_click(main, b->get_position(), &c_frame, &c_line, &c_item, &c_index, &outside, false);
  1691. if (c_item != nullptr) {
  1692. if (selection.enabled) {
  1693. selection.click_frame = c_frame;
  1694. selection.click_item = c_item;
  1695. selection.click_line = c_line;
  1696. selection.click_char = c_index;
  1697. // Erase previous selection.
  1698. if (selection.active) {
  1699. if (_is_click_inside_selection()) {
  1700. selection.drag_attempt = true;
  1701. selection.click_item = nullptr;
  1702. } else {
  1703. selection.from_frame = nullptr;
  1704. selection.from_line = 0;
  1705. selection.from_item = nullptr;
  1706. selection.from_char = 0;
  1707. selection.to_frame = nullptr;
  1708. selection.to_line = 0;
  1709. selection.to_item = nullptr;
  1710. selection.to_char = 0;
  1711. deselect();
  1712. }
  1713. }
  1714. }
  1715. }
  1716. } else if (b->is_pressed() && b->is_double_click() && selection.enabled) {
  1717. //double_click: select word
  1718. ItemFrame *c_frame = nullptr;
  1719. int c_line = 0;
  1720. Item *c_item = nullptr;
  1721. int c_index = 0;
  1722. bool outside;
  1723. selection.drag_attempt = false;
  1724. _find_click(main, b->get_position(), &c_frame, &c_line, &c_item, &c_index, &outside, false);
  1725. if (c_frame) {
  1726. const Line &l = c_frame->lines[c_line];
  1727. MutexLock lock(l.text_buf->get_mutex());
  1728. PackedInt32Array words = TS->shaped_text_get_word_breaks(l.text_buf->get_rid());
  1729. for (int i = 0; i < words.size(); i = i + 2) {
  1730. if (c_index >= words[i] && c_index < words[i + 1]) {
  1731. selection.from_frame = c_frame;
  1732. selection.from_line = c_line;
  1733. selection.from_item = c_item;
  1734. selection.from_char = words[i];
  1735. selection.to_frame = c_frame;
  1736. selection.to_line = c_line;
  1737. selection.to_item = c_item;
  1738. selection.to_char = words[i + 1];
  1739. selection.active = true;
  1740. if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_CLIPBOARD_PRIMARY)) {
  1741. DisplayServer::get_singleton()->clipboard_set_primary(get_selected_text());
  1742. }
  1743. queue_redraw();
  1744. break;
  1745. }
  1746. }
  1747. }
  1748. } else if (!b->is_pressed()) {
  1749. if (selection.enabled && DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_CLIPBOARD_PRIMARY)) {
  1750. DisplayServer::get_singleton()->clipboard_set_primary(get_selected_text());
  1751. }
  1752. selection.click_item = nullptr;
  1753. if (selection.drag_attempt) {
  1754. selection.drag_attempt = false;
  1755. if (_is_click_inside_selection()) {
  1756. selection.from_frame = nullptr;
  1757. selection.from_line = 0;
  1758. selection.from_item = nullptr;
  1759. selection.from_char = 0;
  1760. selection.to_frame = nullptr;
  1761. selection.to_line = 0;
  1762. selection.to_item = nullptr;
  1763. selection.to_char = 0;
  1764. deselect();
  1765. }
  1766. }
  1767. if (!b->is_double_click() && !scroll_updated && !selection.active) {
  1768. Item *c_item = nullptr;
  1769. bool outside = true;
  1770. _find_click(main, b->get_position(), nullptr, nullptr, &c_item, nullptr, &outside, true);
  1771. if (c_item) {
  1772. Variant meta;
  1773. if (!outside && _find_meta(c_item, &meta)) {
  1774. //meta clicked
  1775. emit_signal(SNAME("meta_clicked"), meta);
  1776. }
  1777. }
  1778. }
  1779. }
  1780. }
  1781. if (b->get_button_index() == MouseButton::WHEEL_UP) {
  1782. if (scroll_active) {
  1783. vscroll->set_value(vscroll->get_value() - vscroll->get_page() * b->get_factor() * 0.5 / 8);
  1784. }
  1785. }
  1786. if (b->get_button_index() == MouseButton::WHEEL_DOWN) {
  1787. if (scroll_active) {
  1788. vscroll->set_value(vscroll->get_value() + vscroll->get_page() * b->get_factor() * 0.5 / 8);
  1789. }
  1790. }
  1791. if (b->get_button_index() == MouseButton::RIGHT && context_menu_enabled) {
  1792. _update_context_menu();
  1793. menu->set_position(get_screen_position() + b->get_position());
  1794. menu->reset_size();
  1795. menu->popup();
  1796. grab_focus();
  1797. }
  1798. }
  1799. Ref<InputEventPanGesture> pan_gesture = p_event;
  1800. if (pan_gesture.is_valid()) {
  1801. if (scroll_active) {
  1802. vscroll->set_value(vscroll->get_value() + vscroll->get_page() * pan_gesture->get_delta().y * 0.5 / 8);
  1803. }
  1804. return;
  1805. }
  1806. Ref<InputEventKey> k = p_event;
  1807. if (k.is_valid()) {
  1808. if (k->is_pressed()) {
  1809. bool handled = false;
  1810. if (k->is_action("ui_page_up", true) && vscroll->is_visible_in_tree()) {
  1811. vscroll->set_value(vscroll->get_value() - vscroll->get_page());
  1812. handled = true;
  1813. }
  1814. if (k->is_action("ui_page_down", true) && vscroll->is_visible_in_tree()) {
  1815. vscroll->set_value(vscroll->get_value() + vscroll->get_page());
  1816. handled = true;
  1817. }
  1818. if (k->is_action("ui_up", true) && vscroll->is_visible_in_tree()) {
  1819. vscroll->set_value(vscroll->get_value() - theme_cache.normal_font->get_height(theme_cache.normal_font_size));
  1820. handled = true;
  1821. }
  1822. if (k->is_action("ui_down", true) && vscroll->is_visible_in_tree()) {
  1823. vscroll->set_value(vscroll->get_value() + theme_cache.normal_font->get_height(theme_cache.normal_font_size));
  1824. handled = true;
  1825. }
  1826. if (k->is_action("ui_home", true) && vscroll->is_visible_in_tree()) {
  1827. vscroll->set_value(0);
  1828. handled = true;
  1829. }
  1830. if (k->is_action("ui_end", true) && vscroll->is_visible_in_tree()) {
  1831. vscroll->set_value(vscroll->get_max());
  1832. handled = true;
  1833. }
  1834. if (is_shortcut_keys_enabled()) {
  1835. if (k->is_action("ui_text_select_all", true)) {
  1836. select_all();
  1837. handled = true;
  1838. }
  1839. if (k->is_action("ui_copy", true)) {
  1840. selection_copy();
  1841. handled = true;
  1842. }
  1843. }
  1844. if (k->is_action("ui_menu", true)) {
  1845. if (context_menu_enabled) {
  1846. _update_context_menu();
  1847. menu->set_position(get_screen_position());
  1848. menu->reset_size();
  1849. menu->popup();
  1850. menu->grab_focus();
  1851. }
  1852. handled = true;
  1853. }
  1854. if (handled) {
  1855. accept_event();
  1856. }
  1857. }
  1858. }
  1859. Ref<InputEventMouseMotion> m = p_event;
  1860. if (m.is_valid()) {
  1861. ItemFrame *c_frame = nullptr;
  1862. int c_line = 0;
  1863. Item *c_item = nullptr;
  1864. int c_index = 0;
  1865. bool outside;
  1866. _find_click(main, m->get_position(), &c_frame, &c_line, &c_item, &c_index, &outside, false);
  1867. if (selection.click_item && c_item) {
  1868. selection.from_frame = selection.click_frame;
  1869. selection.from_line = selection.click_line;
  1870. selection.from_item = selection.click_item;
  1871. selection.from_char = selection.click_char;
  1872. selection.to_frame = c_frame;
  1873. selection.to_line = c_line;
  1874. selection.to_item = c_item;
  1875. selection.to_char = c_index;
  1876. bool swap = false;
  1877. if (selection.click_frame && c_frame) {
  1878. const Line &l1 = c_frame->lines[c_line];
  1879. const Line &l2 = selection.click_frame->lines[selection.click_line];
  1880. if (l1.char_offset + c_index < l2.char_offset + selection.click_char) {
  1881. swap = true;
  1882. } else if (l1.char_offset + c_index == l2.char_offset + selection.click_char) {
  1883. deselect();
  1884. return;
  1885. }
  1886. }
  1887. if (swap) {
  1888. SWAP(selection.from_frame, selection.to_frame);
  1889. SWAP(selection.from_line, selection.to_line);
  1890. SWAP(selection.from_item, selection.to_item);
  1891. SWAP(selection.from_char, selection.to_char);
  1892. }
  1893. selection.active = true;
  1894. queue_redraw();
  1895. }
  1896. Variant meta;
  1897. ItemMeta *item_meta;
  1898. if (c_item && !outside && _find_meta(c_item, &meta, &item_meta)) {
  1899. if (meta_hovering != item_meta) {
  1900. if (meta_hovering) {
  1901. emit_signal(SNAME("meta_hover_ended"), current_meta);
  1902. }
  1903. meta_hovering = item_meta;
  1904. current_meta = meta;
  1905. emit_signal(SNAME("meta_hover_started"), meta);
  1906. }
  1907. } else if (meta_hovering) {
  1908. meta_hovering = nullptr;
  1909. emit_signal(SNAME("meta_hover_ended"), current_meta);
  1910. current_meta = false;
  1911. }
  1912. }
  1913. }
  1914. String RichTextLabel::get_tooltip(const Point2 &p_pos) const {
  1915. Item *c_item = nullptr;
  1916. bool outside;
  1917. const_cast<RichTextLabel *>(this)->_find_click(main, p_pos, nullptr, nullptr, &c_item, nullptr, &outside, true);
  1918. String description;
  1919. if (c_item && !outside && const_cast<RichTextLabel *>(this)->_find_hint(c_item, &description)) {
  1920. return description;
  1921. } else {
  1922. return Control::get_tooltip(p_pos);
  1923. }
  1924. }
  1925. void RichTextLabel::_find_frame(Item *p_item, ItemFrame **r_frame, int *r_line) {
  1926. if (r_frame != nullptr) {
  1927. *r_frame = nullptr;
  1928. }
  1929. if (r_line != nullptr) {
  1930. *r_line = 0;
  1931. }
  1932. Item *item = p_item;
  1933. while (item) {
  1934. if (item->parent != nullptr && item->parent->type == ITEM_FRAME) {
  1935. if (r_frame != nullptr) {
  1936. *r_frame = static_cast<ItemFrame *>(item->parent);
  1937. }
  1938. if (r_line != nullptr) {
  1939. *r_line = item->line;
  1940. }
  1941. return;
  1942. }
  1943. item = item->parent;
  1944. }
  1945. }
  1946. RichTextLabel::Item *RichTextLabel::_find_indentable(Item *p_item) {
  1947. Item *indentable = p_item;
  1948. while (indentable) {
  1949. if (indentable->type == ITEM_INDENT || indentable->type == ITEM_LIST) {
  1950. return indentable;
  1951. }
  1952. indentable = indentable->parent;
  1953. }
  1954. return indentable;
  1955. }
  1956. RichTextLabel::ItemFont *RichTextLabel::_find_font(Item *p_item) {
  1957. Item *fontitem = p_item;
  1958. while (fontitem) {
  1959. if (fontitem->type == ITEM_FONT) {
  1960. ItemFont *fi = static_cast<ItemFont *>(fontitem);
  1961. switch (fi->def_font) {
  1962. case NORMAL_FONT: {
  1963. if (fi->variation) {
  1964. Ref<FontVariation> fc = fi->font;
  1965. if (fc.is_valid()) {
  1966. fc->set_base_font(theme_cache.normal_font);
  1967. }
  1968. } else {
  1969. fi->font = theme_cache.normal_font;
  1970. }
  1971. if (fi->def_size) {
  1972. fi->font_size = theme_cache.normal_font_size;
  1973. }
  1974. } break;
  1975. case BOLD_FONT: {
  1976. if (fi->variation) {
  1977. Ref<FontVariation> fc = fi->font;
  1978. if (fc.is_valid()) {
  1979. fc->set_base_font(theme_cache.bold_font);
  1980. }
  1981. } else {
  1982. fi->font = theme_cache.bold_font;
  1983. }
  1984. if (fi->def_size) {
  1985. fi->font_size = theme_cache.bold_font_size;
  1986. }
  1987. } break;
  1988. case ITALICS_FONT: {
  1989. if (fi->variation) {
  1990. Ref<FontVariation> fc = fi->font;
  1991. if (fc.is_valid()) {
  1992. fc->set_base_font(theme_cache.italics_font);
  1993. }
  1994. } else {
  1995. fi->font = theme_cache.italics_font;
  1996. }
  1997. if (fi->def_size) {
  1998. fi->font_size = theme_cache.italics_font_size;
  1999. }
  2000. } break;
  2001. case BOLD_ITALICS_FONT: {
  2002. if (fi->variation) {
  2003. Ref<FontVariation> fc = fi->font;
  2004. if (fc.is_valid()) {
  2005. fc->set_base_font(theme_cache.bold_italics_font);
  2006. }
  2007. } else {
  2008. fi->font = theme_cache.bold_italics_font;
  2009. }
  2010. if (fi->def_size) {
  2011. fi->font_size = theme_cache.bold_italics_font_size;
  2012. }
  2013. } break;
  2014. case MONO_FONT: {
  2015. if (fi->variation) {
  2016. Ref<FontVariation> fc = fi->font;
  2017. if (fc.is_valid()) {
  2018. fc->set_base_font(theme_cache.mono_font);
  2019. }
  2020. } else {
  2021. fi->font = theme_cache.mono_font;
  2022. }
  2023. if (fi->def_size) {
  2024. fi->font_size = theme_cache.mono_font_size;
  2025. }
  2026. } break;
  2027. default: {
  2028. } break;
  2029. }
  2030. return fi;
  2031. }
  2032. fontitem = fontitem->parent;
  2033. }
  2034. return nullptr;
  2035. }
  2036. RichTextLabel::ItemFontSize *RichTextLabel::_find_font_size(Item *p_item) {
  2037. Item *sizeitem = p_item;
  2038. while (sizeitem) {
  2039. if (sizeitem->type == ITEM_FONT_SIZE) {
  2040. ItemFontSize *fi = static_cast<ItemFontSize *>(sizeitem);
  2041. return fi;
  2042. }
  2043. sizeitem = sizeitem->parent;
  2044. }
  2045. return nullptr;
  2046. }
  2047. int RichTextLabel::_find_outline_size(Item *p_item, int p_default) {
  2048. Item *sizeitem = p_item;
  2049. while (sizeitem) {
  2050. if (sizeitem->type == ITEM_OUTLINE_SIZE) {
  2051. ItemOutlineSize *fi = static_cast<ItemOutlineSize *>(sizeitem);
  2052. return fi->outline_size;
  2053. }
  2054. sizeitem = sizeitem->parent;
  2055. }
  2056. return p_default;
  2057. }
  2058. RichTextLabel::ItemDropcap *RichTextLabel::_find_dc_item(Item *p_item) {
  2059. Item *item = p_item;
  2060. while (item) {
  2061. if (item->type == ITEM_DROPCAP) {
  2062. return static_cast<ItemDropcap *>(item);
  2063. }
  2064. item = item->parent;
  2065. }
  2066. return nullptr;
  2067. }
  2068. RichTextLabel::ItemList *RichTextLabel::_find_list_item(Item *p_item) {
  2069. Item *item = p_item;
  2070. while (item) {
  2071. if (item->type == ITEM_LIST) {
  2072. return static_cast<ItemList *>(item);
  2073. }
  2074. item = item->parent;
  2075. }
  2076. return nullptr;
  2077. }
  2078. int RichTextLabel::_find_list(Item *p_item, Vector<int> &r_index, Vector<ItemList *> &r_list) {
  2079. Item *item = p_item;
  2080. Item *prev_item = p_item;
  2081. int level = 0;
  2082. while (item) {
  2083. if (item->type == ITEM_LIST) {
  2084. ItemList *list = static_cast<ItemList *>(item);
  2085. ItemFrame *frame = nullptr;
  2086. int line = -1;
  2087. _find_frame(list, &frame, &line);
  2088. int index = 1;
  2089. if (frame != nullptr) {
  2090. for (int i = list->line + 1; i <= prev_item->line && i < (int)frame->lines.size(); i++) {
  2091. if (_find_list_item(frame->lines[i].from) == list) {
  2092. index++;
  2093. }
  2094. }
  2095. }
  2096. r_index.push_back(index);
  2097. r_list.push_back(list);
  2098. prev_item = item;
  2099. }
  2100. level++;
  2101. item = item->parent;
  2102. }
  2103. return level;
  2104. }
  2105. int RichTextLabel::_find_margin(Item *p_item, const Ref<Font> &p_base_font, int p_base_font_size) {
  2106. Item *item = p_item;
  2107. float margin = 0.0;
  2108. while (item) {
  2109. if (item->type == ITEM_INDENT) {
  2110. Ref<Font> font = p_base_font;
  2111. int font_size = p_base_font_size;
  2112. ItemFont *font_it = _find_font(item);
  2113. if (font_it) {
  2114. if (font_it->font.is_valid()) {
  2115. font = font_it->font;
  2116. }
  2117. if (font_it->font_size > 0) {
  2118. font_size = font_it->font_size;
  2119. }
  2120. }
  2121. ItemFontSize *font_size_it = _find_font_size(item);
  2122. if (font_size_it && font_size_it->font_size > 0) {
  2123. font_size = font_size_it->font_size;
  2124. }
  2125. margin += tab_size * font->get_char_size(' ', font_size).width;
  2126. } else if (item->type == ITEM_LIST) {
  2127. Ref<Font> font = p_base_font;
  2128. int font_size = p_base_font_size;
  2129. ItemFont *font_it = _find_font(item);
  2130. if (font_it) {
  2131. if (font_it->font.is_valid()) {
  2132. font = font_it->font;
  2133. }
  2134. if (font_it->font_size > 0) {
  2135. font_size = font_it->font_size;
  2136. }
  2137. }
  2138. ItemFontSize *font_size_it = _find_font_size(item);
  2139. if (font_size_it && font_size_it->font_size > 0) {
  2140. font_size = font_size_it->font_size;
  2141. }
  2142. margin += tab_size * font->get_char_size(' ', font_size).width;
  2143. }
  2144. item = item->parent;
  2145. }
  2146. return margin;
  2147. }
  2148. HorizontalAlignment RichTextLabel::_find_alignment(Item *p_item) {
  2149. Item *item = p_item;
  2150. while (item) {
  2151. if (item->type == ITEM_PARAGRAPH) {
  2152. ItemParagraph *p = static_cast<ItemParagraph *>(item);
  2153. return p->alignment;
  2154. }
  2155. item = item->parent;
  2156. }
  2157. return default_alignment;
  2158. }
  2159. TextServer::Direction RichTextLabel::_find_direction(Item *p_item) {
  2160. Item *item = p_item;
  2161. while (item) {
  2162. if (item->type == ITEM_PARAGRAPH) {
  2163. ItemParagraph *p = static_cast<ItemParagraph *>(item);
  2164. if (p->direction != Control::TEXT_DIRECTION_INHERITED) {
  2165. return (TextServer::Direction)p->direction;
  2166. }
  2167. }
  2168. item = item->parent;
  2169. }
  2170. if (text_direction == Control::TEXT_DIRECTION_INHERITED) {
  2171. return is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR;
  2172. } else {
  2173. return (TextServer::Direction)text_direction;
  2174. }
  2175. }
  2176. TextServer::StructuredTextParser RichTextLabel::_find_stt(Item *p_item) {
  2177. Item *item = p_item;
  2178. while (item) {
  2179. if (item->type == ITEM_PARAGRAPH) {
  2180. ItemParagraph *p = static_cast<ItemParagraph *>(item);
  2181. return p->st_parser;
  2182. }
  2183. item = item->parent;
  2184. }
  2185. return st_parser;
  2186. }
  2187. String RichTextLabel::_find_language(Item *p_item) {
  2188. Item *item = p_item;
  2189. while (item) {
  2190. if (item->type == ITEM_PARAGRAPH) {
  2191. ItemParagraph *p = static_cast<ItemParagraph *>(item);
  2192. return p->language;
  2193. }
  2194. item = item->parent;
  2195. }
  2196. return language;
  2197. }
  2198. Color RichTextLabel::_find_color(Item *p_item, const Color &p_default_color) {
  2199. Item *item = p_item;
  2200. while (item) {
  2201. if (item->type == ITEM_COLOR) {
  2202. ItemColor *color = static_cast<ItemColor *>(item);
  2203. return color->color;
  2204. }
  2205. item = item->parent;
  2206. }
  2207. return p_default_color;
  2208. }
  2209. Color RichTextLabel::_find_outline_color(Item *p_item, const Color &p_default_color) {
  2210. Item *item = p_item;
  2211. while (item) {
  2212. if (item->type == ITEM_OUTLINE_COLOR) {
  2213. ItemOutlineColor *color = static_cast<ItemOutlineColor *>(item);
  2214. return color->color;
  2215. }
  2216. item = item->parent;
  2217. }
  2218. return p_default_color;
  2219. }
  2220. bool RichTextLabel::_find_underline(Item *p_item) {
  2221. Item *item = p_item;
  2222. while (item) {
  2223. if (item->type == ITEM_UNDERLINE) {
  2224. return true;
  2225. }
  2226. item = item->parent;
  2227. }
  2228. return false;
  2229. }
  2230. bool RichTextLabel::_find_strikethrough(Item *p_item) {
  2231. Item *item = p_item;
  2232. while (item) {
  2233. if (item->type == ITEM_STRIKETHROUGH) {
  2234. return true;
  2235. }
  2236. item = item->parent;
  2237. }
  2238. return false;
  2239. }
  2240. void RichTextLabel::_fetch_item_fx_stack(Item *p_item, Vector<ItemFX *> &r_stack) {
  2241. Item *item = p_item;
  2242. while (item) {
  2243. if (item->type == ITEM_CUSTOMFX || item->type == ITEM_SHAKE || item->type == ITEM_WAVE || item->type == ITEM_TORNADO || item->type == ITEM_RAINBOW) {
  2244. r_stack.push_back(static_cast<ItemFX *>(item));
  2245. }
  2246. item = item->parent;
  2247. }
  2248. }
  2249. bool RichTextLabel::_find_meta(Item *p_item, Variant *r_meta, ItemMeta **r_item) {
  2250. Item *item = p_item;
  2251. while (item) {
  2252. if (item->type == ITEM_META) {
  2253. ItemMeta *meta = static_cast<ItemMeta *>(item);
  2254. if (r_meta) {
  2255. *r_meta = meta->meta;
  2256. }
  2257. if (r_item) {
  2258. *r_item = meta;
  2259. }
  2260. return true;
  2261. }
  2262. item = item->parent;
  2263. }
  2264. return false;
  2265. }
  2266. bool RichTextLabel::_find_hint(Item *p_item, String *r_description) {
  2267. Item *item = p_item;
  2268. while (item) {
  2269. if (item->type == ITEM_HINT) {
  2270. ItemHint *hint = static_cast<ItemHint *>(item);
  2271. if (r_description) {
  2272. *r_description = hint->description;
  2273. }
  2274. return true;
  2275. }
  2276. item = item->parent;
  2277. }
  2278. return false;
  2279. }
  2280. Color RichTextLabel::_find_bgcolor(Item *p_item) {
  2281. Item *item = p_item;
  2282. while (item) {
  2283. if (item->type == ITEM_BGCOLOR) {
  2284. ItemBGColor *color = static_cast<ItemBGColor *>(item);
  2285. return color->color;
  2286. }
  2287. item = item->parent;
  2288. }
  2289. return Color(0, 0, 0, 0);
  2290. }
  2291. Color RichTextLabel::_find_fgcolor(Item *p_item) {
  2292. Item *item = p_item;
  2293. while (item) {
  2294. if (item->type == ITEM_FGCOLOR) {
  2295. ItemFGColor *color = static_cast<ItemFGColor *>(item);
  2296. return color->color;
  2297. }
  2298. item = item->parent;
  2299. }
  2300. return Color(0, 0, 0, 0);
  2301. }
  2302. bool RichTextLabel::_find_layout_subitem(Item *from, Item *to) {
  2303. if (from && from != to) {
  2304. if (from->type != ITEM_FONT && from->type != ITEM_COLOR && from->type != ITEM_UNDERLINE && from->type != ITEM_STRIKETHROUGH) {
  2305. return true;
  2306. }
  2307. for (Item *E : from->subitems) {
  2308. bool layout = _find_layout_subitem(E, to);
  2309. if (layout) {
  2310. return true;
  2311. }
  2312. }
  2313. }
  2314. return false;
  2315. }
  2316. void RichTextLabel::_thread_function(void *p_userdata) {
  2317. _process_line_caches();
  2318. updating.store(false);
  2319. call_deferred(SNAME("thread_end"));
  2320. }
  2321. void RichTextLabel::_thread_end() {
  2322. set_physics_process_internal(false);
  2323. if (is_visible_in_tree()) {
  2324. queue_redraw();
  2325. }
  2326. }
  2327. void RichTextLabel::_stop_thread() {
  2328. if (threaded) {
  2329. stop_thread.store(true);
  2330. if (task != WorkerThreadPool::INVALID_TASK_ID) {
  2331. WorkerThreadPool::get_singleton()->wait_for_task_completion(task);
  2332. task = WorkerThreadPool::INVALID_TASK_ID;
  2333. }
  2334. }
  2335. }
  2336. bool RichTextLabel::is_ready() const {
  2337. if (updating.load()) {
  2338. return false;
  2339. }
  2340. return (main->first_invalid_line.load() == (int)main->lines.size() && main->first_resized_line.load() == (int)main->lines.size() && main->first_invalid_font_line.load() == (int)main->lines.size());
  2341. }
  2342. void RichTextLabel::set_threaded(bool p_threaded) {
  2343. if (threaded != p_threaded) {
  2344. _stop_thread();
  2345. threaded = p_threaded;
  2346. queue_redraw();
  2347. }
  2348. }
  2349. bool RichTextLabel::is_threaded() const {
  2350. return threaded;
  2351. }
  2352. void RichTextLabel::set_progress_bar_delay(int p_delay_ms) {
  2353. progress_delay = p_delay_ms;
  2354. }
  2355. int RichTextLabel::get_progress_bar_delay() const {
  2356. return progress_delay;
  2357. }
  2358. bool RichTextLabel::_validate_line_caches() {
  2359. if (updating.load()) {
  2360. return false;
  2361. }
  2362. if (main->first_invalid_line.load() == (int)main->lines.size()) {
  2363. MutexLock data_lock(data_mutex);
  2364. Rect2 text_rect = _get_text_rect();
  2365. int ctrl_height = get_size().height;
  2366. // Update fonts.
  2367. float old_scroll = vscroll->get_value();
  2368. if (main->first_invalid_font_line.load() != (int)main->lines.size()) {
  2369. for (int i = main->first_invalid_font_line.load(); i < (int)main->lines.size(); i++) {
  2370. _update_line_font(main, i, theme_cache.normal_font, theme_cache.normal_font_size);
  2371. }
  2372. main->first_resized_line.store(main->first_invalid_font_line.load());
  2373. main->first_invalid_font_line.store(main->lines.size());
  2374. }
  2375. if (main->first_resized_line.load() == (int)main->lines.size()) {
  2376. vscroll->set_value(old_scroll);
  2377. return true;
  2378. }
  2379. // Resize lines without reshaping.
  2380. int fi = main->first_resized_line.load();
  2381. float total_height = (fi == 0) ? 0 : _calculate_line_vertical_offset(main->lines[fi - 1]);
  2382. for (int i = fi; i < (int)main->lines.size(); i++) {
  2383. total_height = _resize_line(main, i, theme_cache.normal_font, theme_cache.normal_font_size, text_rect.get_size().width - scroll_w, total_height);
  2384. updating_scroll = true;
  2385. bool exceeds = total_height > ctrl_height && scroll_active;
  2386. if (exceeds != scroll_visible) {
  2387. if (exceeds) {
  2388. scroll_visible = true;
  2389. scroll_w = vscroll->get_combined_minimum_size().width;
  2390. vscroll->show();
  2391. vscroll->set_anchor_and_offset(SIDE_LEFT, ANCHOR_END, -scroll_w);
  2392. } else {
  2393. scroll_visible = false;
  2394. scroll_w = 0;
  2395. vscroll->hide();
  2396. }
  2397. main->first_resized_line.store(0);
  2398. total_height = 0;
  2399. for (int j = 0; j <= i; j++) {
  2400. total_height = _resize_line(main, j, theme_cache.normal_font, theme_cache.normal_font_size, text_rect.get_size().width - scroll_w, total_height);
  2401. main->first_resized_line.store(j);
  2402. }
  2403. }
  2404. vscroll->set_max(total_height);
  2405. vscroll->set_page(text_rect.size.height);
  2406. if (scroll_follow && scroll_following) {
  2407. vscroll->set_value(total_height);
  2408. } else {
  2409. vscroll->set_value(old_scroll);
  2410. }
  2411. updating_scroll = false;
  2412. main->first_resized_line.store(i);
  2413. }
  2414. main->first_resized_line.store(main->lines.size());
  2415. if (fit_content) {
  2416. update_minimum_size();
  2417. }
  2418. return true;
  2419. }
  2420. stop_thread.store(false);
  2421. if (threaded) {
  2422. updating.store(true);
  2423. loaded.store(true);
  2424. task = WorkerThreadPool::get_singleton()->add_template_task(this, &RichTextLabel::_thread_function, nullptr, true, vformat("RichTextLabelShape:%x", (int64_t)get_instance_id()));
  2425. set_physics_process_internal(true);
  2426. loading_started = OS::get_singleton()->get_ticks_msec();
  2427. return false;
  2428. } else {
  2429. _process_line_caches();
  2430. queue_redraw();
  2431. return true;
  2432. }
  2433. }
  2434. void RichTextLabel::_process_line_caches() {
  2435. // Shape invalid lines.
  2436. if (!is_inside_tree()) {
  2437. return;
  2438. }
  2439. MutexLock data_lock(data_mutex);
  2440. Rect2 text_rect = _get_text_rect();
  2441. int ctrl_height = get_size().height;
  2442. int fi = main->first_invalid_line.load();
  2443. int total_chars = main->lines[fi].char_offset;
  2444. float total_height = (fi == 0) ? 0 : _calculate_line_vertical_offset(main->lines[fi - 1]);
  2445. for (int i = fi; i < (int)main->lines.size(); i++) {
  2446. total_height = _shape_line(main, i, theme_cache.normal_font, theme_cache.normal_font_size, text_rect.get_size().width - scroll_w, total_height, &total_chars);
  2447. updating_scroll = true;
  2448. bool exceeds = total_height > ctrl_height && scroll_active;
  2449. if (exceeds != scroll_visible) {
  2450. if (exceeds) {
  2451. scroll_visible = true;
  2452. scroll_w = vscroll->get_combined_minimum_size().width;
  2453. vscroll->show();
  2454. vscroll->set_anchor_and_offset(SIDE_LEFT, ANCHOR_END, -scroll_w);
  2455. } else {
  2456. scroll_visible = false;
  2457. scroll_w = 0;
  2458. vscroll->hide();
  2459. }
  2460. main->first_invalid_line.store(0);
  2461. main->first_resized_line.store(0);
  2462. main->first_invalid_font_line.store(0);
  2463. // since scroll was added or removed we need to resize all lines
  2464. total_height = 0;
  2465. for (int j = 0; j <= i; j++) {
  2466. total_height = _resize_line(main, j, theme_cache.normal_font, theme_cache.normal_font_size, text_rect.get_size().width - scroll_w, total_height);
  2467. main->first_invalid_line.store(j);
  2468. main->first_resized_line.store(j);
  2469. main->first_invalid_font_line.store(j);
  2470. }
  2471. }
  2472. vscroll->set_max(total_height);
  2473. vscroll->set_page(text_rect.size.height);
  2474. if (scroll_follow && scroll_following) {
  2475. vscroll->set_value(total_height);
  2476. }
  2477. updating_scroll = false;
  2478. main->first_invalid_line.store(i);
  2479. main->first_resized_line.store(i);
  2480. main->first_invalid_font_line.store(i);
  2481. if (stop_thread.load()) {
  2482. return;
  2483. }
  2484. loaded.store(double(i) / double(main->lines.size()));
  2485. }
  2486. main->first_invalid_line.store(main->lines.size());
  2487. main->first_resized_line.store(main->lines.size());
  2488. main->first_invalid_font_line.store(main->lines.size());
  2489. if (fit_content) {
  2490. update_minimum_size();
  2491. }
  2492. emit_signal(SNAME("finished"));
  2493. }
  2494. void RichTextLabel::_invalidate_current_line(ItemFrame *p_frame) {
  2495. if ((int)p_frame->lines.size() - 1 <= p_frame->first_invalid_line) {
  2496. p_frame->first_invalid_line = (int)p_frame->lines.size() - 1;
  2497. }
  2498. }
  2499. void RichTextLabel::add_text(const String &p_text) {
  2500. _stop_thread();
  2501. MutexLock data_lock(data_mutex);
  2502. if (current->type == ITEM_TABLE) {
  2503. return; //can't add anything here
  2504. }
  2505. int pos = 0;
  2506. while (pos < p_text.length()) {
  2507. int end = p_text.find("\n", pos);
  2508. String line;
  2509. bool eol = false;
  2510. if (end == -1) {
  2511. end = p_text.length();
  2512. } else {
  2513. eol = true;
  2514. }
  2515. if (pos == 0 && end == p_text.length()) {
  2516. line = p_text;
  2517. } else {
  2518. line = p_text.substr(pos, end - pos);
  2519. }
  2520. if (line.length() > 0) {
  2521. if (current->subitems.size() && current->subitems.back()->get()->type == ITEM_TEXT) {
  2522. //append text condition!
  2523. ItemText *ti = static_cast<ItemText *>(current->subitems.back()->get());
  2524. ti->text += line;
  2525. _invalidate_current_line(main);
  2526. } else {
  2527. //append item condition
  2528. ItemText *item = memnew(ItemText);
  2529. item->text = line;
  2530. _add_item(item, false);
  2531. }
  2532. }
  2533. if (eol) {
  2534. ItemNewline *item = memnew(ItemNewline);
  2535. item->line = current_frame->lines.size();
  2536. _add_item(item, false);
  2537. current_frame->lines.resize(current_frame->lines.size() + 1);
  2538. if (item->type != ITEM_NEWLINE) {
  2539. current_frame->lines[current_frame->lines.size() - 1].from = item;
  2540. }
  2541. _invalidate_current_line(current_frame);
  2542. }
  2543. pos = end + 1;
  2544. }
  2545. queue_redraw();
  2546. }
  2547. void RichTextLabel::_add_item(Item *p_item, bool p_enter, bool p_ensure_newline) {
  2548. p_item->parent = current;
  2549. p_item->E = current->subitems.push_back(p_item);
  2550. p_item->index = current_idx++;
  2551. p_item->char_ofs = current_char_ofs;
  2552. if (p_item->type == ITEM_TEXT) {
  2553. ItemText *t = static_cast<ItemText *>(p_item);
  2554. current_char_ofs += t->text.length();
  2555. } else if (p_item->type == ITEM_IMAGE) {
  2556. current_char_ofs++;
  2557. }
  2558. if (p_enter) {
  2559. current = p_item;
  2560. }
  2561. if (p_ensure_newline) {
  2562. Item *from = current_frame->lines[current_frame->lines.size() - 1].from;
  2563. // only create a new line for Item types that generate content/layout, ignore those that represent formatting/styling
  2564. if (_find_layout_subitem(from, p_item)) {
  2565. _invalidate_current_line(current_frame);
  2566. current_frame->lines.resize(current_frame->lines.size() + 1);
  2567. }
  2568. }
  2569. if (current_frame->lines[current_frame->lines.size() - 1].from == nullptr) {
  2570. current_frame->lines[current_frame->lines.size() - 1].from = p_item;
  2571. }
  2572. p_item->line = current_frame->lines.size() - 1;
  2573. _invalidate_current_line(current_frame);
  2574. if (fit_content) {
  2575. update_minimum_size();
  2576. }
  2577. queue_redraw();
  2578. }
  2579. void RichTextLabel::_remove_item(Item *p_item, const int p_line, const int p_subitem_line) {
  2580. int size = p_item->subitems.size();
  2581. if (size == 0) {
  2582. p_item->parent->subitems.erase(p_item);
  2583. // If a newline was erased, all lines AFTER the newline need to be decremented.
  2584. if (p_item->type == ITEM_NEWLINE) {
  2585. current_frame->lines.remove_at(p_line);
  2586. for (int i = 0; i < current->subitems.size(); i++) {
  2587. if (current->subitems[i]->line > p_subitem_line) {
  2588. current->subitems[i]->line--;
  2589. }
  2590. }
  2591. }
  2592. } else {
  2593. // First, remove all child items for the provided item.
  2594. for (int i = 0; i < size; i++) {
  2595. _remove_item(p_item->subitems.front()->get(), p_line, p_subitem_line);
  2596. }
  2597. // Then remove the provided item itself.
  2598. p_item->parent->subitems.erase(p_item);
  2599. }
  2600. memdelete(p_item);
  2601. }
  2602. void RichTextLabel::add_image(const Ref<Texture2D> &p_image, const int p_width, const int p_height, const Color &p_color, InlineAlignment p_alignment, const Rect2 &p_region) {
  2603. _stop_thread();
  2604. MutexLock data_lock(data_mutex);
  2605. if (current->type == ITEM_TABLE) {
  2606. return;
  2607. }
  2608. ERR_FAIL_COND(p_image.is_null());
  2609. ERR_FAIL_COND(p_image->get_width() == 0);
  2610. ERR_FAIL_COND(p_image->get_height() == 0);
  2611. ItemImage *item = memnew(ItemImage);
  2612. if (p_region.has_area()) {
  2613. Ref<AtlasTexture> atlas_tex = memnew(AtlasTexture);
  2614. atlas_tex->set_atlas(p_image);
  2615. atlas_tex->set_region(p_region);
  2616. item->image = atlas_tex;
  2617. } else {
  2618. item->image = p_image;
  2619. }
  2620. item->color = p_color;
  2621. item->inline_align = p_alignment;
  2622. if (p_width > 0) {
  2623. // custom width
  2624. item->size.width = p_width;
  2625. if (p_height > 0) {
  2626. // custom height
  2627. item->size.height = p_height;
  2628. } else {
  2629. // calculate height to keep aspect ratio
  2630. if (p_region.has_area()) {
  2631. item->size.height = p_region.get_size().height * p_width / p_region.get_size().width;
  2632. } else {
  2633. item->size.height = p_image->get_height() * p_width / p_image->get_width();
  2634. }
  2635. }
  2636. } else {
  2637. if (p_height > 0) {
  2638. // custom height
  2639. item->size.height = p_height;
  2640. // calculate width to keep aspect ratio
  2641. if (p_region.has_area()) {
  2642. item->size.width = p_region.get_size().width * p_height / p_region.get_size().height;
  2643. } else {
  2644. item->size.width = p_image->get_width() * p_height / p_image->get_height();
  2645. }
  2646. } else {
  2647. if (p_region.has_area()) {
  2648. // if the image has a region, keep the region size
  2649. item->size = p_region.get_size();
  2650. } else {
  2651. // keep original width and height
  2652. item->size = p_image->get_size();
  2653. }
  2654. }
  2655. }
  2656. _add_item(item, false);
  2657. }
  2658. void RichTextLabel::add_newline() {
  2659. _stop_thread();
  2660. MutexLock data_lock(data_mutex);
  2661. if (current->type == ITEM_TABLE) {
  2662. return;
  2663. }
  2664. ItemNewline *item = memnew(ItemNewline);
  2665. item->line = current_frame->lines.size();
  2666. _add_item(item, false);
  2667. current_frame->lines.resize(current_frame->lines.size() + 1);
  2668. _invalidate_current_line(current_frame);
  2669. queue_redraw();
  2670. }
  2671. bool RichTextLabel::remove_paragraph(const int p_paragraph) {
  2672. _stop_thread();
  2673. MutexLock data_lock(data_mutex);
  2674. if (p_paragraph >= (int)current_frame->lines.size() || p_paragraph < 0) {
  2675. return false;
  2676. }
  2677. // Remove all subitems with the same line as that provided.
  2678. Vector<int> subitem_indices_to_remove;
  2679. for (int i = 0; i < current->subitems.size(); i++) {
  2680. if (current->subitems[i]->line == p_paragraph) {
  2681. subitem_indices_to_remove.push_back(i);
  2682. }
  2683. }
  2684. bool had_newline = false;
  2685. // Reverse for loop to remove items from the end first.
  2686. for (int i = subitem_indices_to_remove.size() - 1; i >= 0; i--) {
  2687. int subitem_idx = subitem_indices_to_remove[i];
  2688. had_newline = had_newline || current->subitems[subitem_idx]->type == ITEM_NEWLINE;
  2689. _remove_item(current->subitems[subitem_idx], current->subitems[subitem_idx]->line, p_paragraph);
  2690. }
  2691. if (!had_newline) {
  2692. current_frame->lines.remove_at(p_paragraph);
  2693. if (current_frame->lines.size() == 0) {
  2694. current_frame->lines.resize(1);
  2695. }
  2696. }
  2697. if (p_paragraph == 0 && current->subitems.size() > 0) {
  2698. main->lines[0].from = main;
  2699. }
  2700. main->first_invalid_line.store(0);
  2701. queue_redraw();
  2702. return true;
  2703. }
  2704. void RichTextLabel::push_dropcap(const String &p_string, const Ref<Font> &p_font, int p_size, const Rect2 &p_dropcap_margins, const Color &p_color, int p_ol_size, const Color &p_ol_color) {
  2705. _stop_thread();
  2706. MutexLock data_lock(data_mutex);
  2707. ERR_FAIL_COND(current->type == ITEM_TABLE);
  2708. ERR_FAIL_COND(p_string.is_empty());
  2709. ERR_FAIL_COND(p_font.is_null());
  2710. ERR_FAIL_COND(p_size <= 0);
  2711. ItemDropcap *item = memnew(ItemDropcap);
  2712. item->text = p_string;
  2713. item->font = p_font;
  2714. item->font_size = p_size;
  2715. item->color = p_color;
  2716. item->ol_size = p_ol_size;
  2717. item->ol_color = p_ol_color;
  2718. item->dropcap_margins = p_dropcap_margins;
  2719. _add_item(item, false);
  2720. }
  2721. void RichTextLabel::_push_def_font_var(DefaultFont p_def_font, const Ref<Font> &p_font, int p_size) {
  2722. _stop_thread();
  2723. MutexLock data_lock(data_mutex);
  2724. ERR_FAIL_COND(current->type == ITEM_TABLE);
  2725. ItemFont *item = memnew(ItemFont);
  2726. item->def_font = p_def_font;
  2727. item->variation = true;
  2728. item->font = p_font;
  2729. item->font_size = p_size;
  2730. item->def_size = (p_size <= 0);
  2731. _add_item(item, true);
  2732. }
  2733. void RichTextLabel::_push_def_font(DefaultFont p_def_font) {
  2734. _stop_thread();
  2735. MutexLock data_lock(data_mutex);
  2736. ERR_FAIL_COND(current->type == ITEM_TABLE);
  2737. ItemFont *item = memnew(ItemFont);
  2738. item->def_font = p_def_font;
  2739. item->def_size = true;
  2740. _add_item(item, true);
  2741. }
  2742. void RichTextLabel::push_font(const Ref<Font> &p_font, int p_size) {
  2743. _stop_thread();
  2744. MutexLock data_lock(data_mutex);
  2745. ERR_FAIL_COND(current->type == ITEM_TABLE);
  2746. ERR_FAIL_COND(p_font.is_null());
  2747. ItemFont *item = memnew(ItemFont);
  2748. item->font = p_font;
  2749. item->font_size = p_size;
  2750. _add_item(item, true);
  2751. }
  2752. void RichTextLabel::push_normal() {
  2753. ERR_FAIL_COND(theme_cache.normal_font.is_null());
  2754. _push_def_font(NORMAL_FONT);
  2755. }
  2756. void RichTextLabel::push_bold() {
  2757. ERR_FAIL_COND(theme_cache.bold_font.is_null());
  2758. ItemFont *item_font = _find_font(current);
  2759. _push_def_font((item_font && item_font->def_font == ITALICS_FONT) ? BOLD_ITALICS_FONT : BOLD_FONT);
  2760. }
  2761. void RichTextLabel::push_bold_italics() {
  2762. ERR_FAIL_COND(theme_cache.bold_italics_font.is_null());
  2763. _push_def_font(BOLD_ITALICS_FONT);
  2764. }
  2765. void RichTextLabel::push_italics() {
  2766. ERR_FAIL_COND(theme_cache.italics_font.is_null());
  2767. ItemFont *item_font = _find_font(current);
  2768. _push_def_font((item_font && item_font->def_font == BOLD_FONT) ? BOLD_ITALICS_FONT : ITALICS_FONT);
  2769. }
  2770. void RichTextLabel::push_mono() {
  2771. ERR_FAIL_COND(theme_cache.mono_font.is_null());
  2772. _push_def_font(MONO_FONT);
  2773. }
  2774. void RichTextLabel::push_font_size(int p_font_size) {
  2775. _stop_thread();
  2776. MutexLock data_lock(data_mutex);
  2777. ERR_FAIL_COND(current->type == ITEM_TABLE);
  2778. ItemFontSize *item = memnew(ItemFontSize);
  2779. item->font_size = p_font_size;
  2780. _add_item(item, true);
  2781. }
  2782. void RichTextLabel::push_outline_size(int p_ol_size) {
  2783. _stop_thread();
  2784. MutexLock data_lock(data_mutex);
  2785. ERR_FAIL_COND(current->type == ITEM_TABLE);
  2786. ItemOutlineSize *item = memnew(ItemOutlineSize);
  2787. item->outline_size = p_ol_size;
  2788. _add_item(item, true);
  2789. }
  2790. void RichTextLabel::push_color(const Color &p_color) {
  2791. _stop_thread();
  2792. MutexLock data_lock(data_mutex);
  2793. ERR_FAIL_COND(current->type == ITEM_TABLE);
  2794. ItemColor *item = memnew(ItemColor);
  2795. item->color = p_color;
  2796. _add_item(item, true);
  2797. }
  2798. void RichTextLabel::push_outline_color(const Color &p_color) {
  2799. _stop_thread();
  2800. MutexLock data_lock(data_mutex);
  2801. ERR_FAIL_COND(current->type == ITEM_TABLE);
  2802. ItemOutlineColor *item = memnew(ItemOutlineColor);
  2803. item->color = p_color;
  2804. _add_item(item, true);
  2805. }
  2806. void RichTextLabel::push_underline() {
  2807. _stop_thread();
  2808. MutexLock data_lock(data_mutex);
  2809. ERR_FAIL_COND(current->type == ITEM_TABLE);
  2810. ItemUnderline *item = memnew(ItemUnderline);
  2811. _add_item(item, true);
  2812. }
  2813. void RichTextLabel::push_strikethrough() {
  2814. _stop_thread();
  2815. MutexLock data_lock(data_mutex);
  2816. ERR_FAIL_COND(current->type == ITEM_TABLE);
  2817. ItemStrikethrough *item = memnew(ItemStrikethrough);
  2818. _add_item(item, true);
  2819. }
  2820. void RichTextLabel::push_paragraph(HorizontalAlignment p_alignment, Control::TextDirection p_direction, const String &p_language, TextServer::StructuredTextParser p_st_parser) {
  2821. _stop_thread();
  2822. MutexLock data_lock(data_mutex);
  2823. ERR_FAIL_COND(current->type == ITEM_TABLE);
  2824. ItemParagraph *item = memnew(ItemParagraph);
  2825. item->alignment = p_alignment;
  2826. item->direction = p_direction;
  2827. item->language = p_language;
  2828. item->st_parser = p_st_parser;
  2829. _add_item(item, true, true);
  2830. }
  2831. void RichTextLabel::push_indent(int p_level) {
  2832. _stop_thread();
  2833. MutexLock data_lock(data_mutex);
  2834. ERR_FAIL_COND(current->type == ITEM_TABLE);
  2835. ERR_FAIL_COND(p_level < 0);
  2836. ItemIndent *item = memnew(ItemIndent);
  2837. item->level = p_level;
  2838. _add_item(item, true, true);
  2839. }
  2840. void RichTextLabel::push_list(int p_level, ListType p_list, bool p_capitalize) {
  2841. _stop_thread();
  2842. MutexLock data_lock(data_mutex);
  2843. ERR_FAIL_COND(current->type == ITEM_TABLE);
  2844. ERR_FAIL_COND(p_level < 0);
  2845. ItemList *item = memnew(ItemList);
  2846. item->list_type = p_list;
  2847. item->level = p_level;
  2848. item->capitalize = p_capitalize;
  2849. _add_item(item, true, true);
  2850. }
  2851. void RichTextLabel::push_meta(const Variant &p_meta) {
  2852. _stop_thread();
  2853. MutexLock data_lock(data_mutex);
  2854. ERR_FAIL_COND(current->type == ITEM_TABLE);
  2855. ItemMeta *item = memnew(ItemMeta);
  2856. item->meta = p_meta;
  2857. _add_item(item, true);
  2858. }
  2859. void RichTextLabel::push_hint(const String &p_string) {
  2860. _stop_thread();
  2861. MutexLock data_lock(data_mutex);
  2862. ERR_FAIL_COND(current->type == ITEM_TABLE);
  2863. ItemHint *item = memnew(ItemHint);
  2864. item->description = p_string;
  2865. _add_item(item, true);
  2866. }
  2867. void RichTextLabel::push_table(int p_columns, InlineAlignment p_alignment, int p_align_to_row) {
  2868. _stop_thread();
  2869. MutexLock data_lock(data_mutex);
  2870. ERR_FAIL_COND(current->type == ITEM_TABLE);
  2871. ERR_FAIL_COND(p_columns < 1);
  2872. ItemTable *item = memnew(ItemTable);
  2873. item->columns.resize(p_columns);
  2874. item->total_width = 0;
  2875. item->inline_align = p_alignment;
  2876. item->align_to_row = p_align_to_row;
  2877. for (int i = 0; i < (int)item->columns.size(); i++) {
  2878. item->columns[i].expand = false;
  2879. item->columns[i].expand_ratio = 1;
  2880. }
  2881. _add_item(item, true, false);
  2882. }
  2883. void RichTextLabel::push_fade(int p_start_index, int p_length) {
  2884. _stop_thread();
  2885. MutexLock data_lock(data_mutex);
  2886. ItemFade *item = memnew(ItemFade);
  2887. item->starting_index = p_start_index;
  2888. item->length = p_length;
  2889. _add_item(item, true);
  2890. }
  2891. void RichTextLabel::push_shake(int p_strength = 10, float p_rate = 24.0f, bool p_connected = true) {
  2892. _stop_thread();
  2893. MutexLock data_lock(data_mutex);
  2894. ItemShake *item = memnew(ItemShake);
  2895. item->strength = p_strength;
  2896. item->rate = p_rate;
  2897. item->connected = p_connected;
  2898. _add_item(item, true);
  2899. }
  2900. void RichTextLabel::push_wave(float p_frequency = 1.0f, float p_amplitude = 10.0f, bool p_connected = true) {
  2901. _stop_thread();
  2902. MutexLock data_lock(data_mutex);
  2903. ItemWave *item = memnew(ItemWave);
  2904. item->frequency = p_frequency;
  2905. item->amplitude = p_amplitude;
  2906. item->connected = p_connected;
  2907. _add_item(item, true);
  2908. }
  2909. void RichTextLabel::push_tornado(float p_frequency = 1.0f, float p_radius = 10.0f, bool p_connected = true) {
  2910. _stop_thread();
  2911. MutexLock data_lock(data_mutex);
  2912. ItemTornado *item = memnew(ItemTornado);
  2913. item->frequency = p_frequency;
  2914. item->radius = p_radius;
  2915. item->connected = p_connected;
  2916. _add_item(item, true);
  2917. }
  2918. void RichTextLabel::push_rainbow(float p_saturation, float p_value, float p_frequency) {
  2919. _stop_thread();
  2920. MutexLock data_lock(data_mutex);
  2921. ItemRainbow *item = memnew(ItemRainbow);
  2922. item->frequency = p_frequency;
  2923. item->saturation = p_saturation;
  2924. item->value = p_value;
  2925. _add_item(item, true);
  2926. }
  2927. void RichTextLabel::push_bgcolor(const Color &p_color) {
  2928. _stop_thread();
  2929. MutexLock data_lock(data_mutex);
  2930. ERR_FAIL_COND(current->type == ITEM_TABLE);
  2931. ItemBGColor *item = memnew(ItemBGColor);
  2932. item->color = p_color;
  2933. _add_item(item, true);
  2934. }
  2935. void RichTextLabel::push_fgcolor(const Color &p_color) {
  2936. _stop_thread();
  2937. MutexLock data_lock(data_mutex);
  2938. ERR_FAIL_COND(current->type == ITEM_TABLE);
  2939. ItemFGColor *item = memnew(ItemFGColor);
  2940. item->color = p_color;
  2941. _add_item(item, true);
  2942. }
  2943. void RichTextLabel::push_customfx(Ref<RichTextEffect> p_custom_effect, Dictionary p_environment) {
  2944. _stop_thread();
  2945. MutexLock data_lock(data_mutex);
  2946. ItemCustomFX *item = memnew(ItemCustomFX);
  2947. item->custom_effect = p_custom_effect;
  2948. item->char_fx_transform->environment = p_environment;
  2949. _add_item(item, true);
  2950. set_process_internal(true);
  2951. }
  2952. void RichTextLabel::set_table_column_expand(int p_column, bool p_expand, int p_ratio) {
  2953. _stop_thread();
  2954. MutexLock data_lock(data_mutex);
  2955. ERR_FAIL_COND(current->type != ITEM_TABLE);
  2956. ItemTable *table = static_cast<ItemTable *>(current);
  2957. ERR_FAIL_INDEX(p_column, (int)table->columns.size());
  2958. table->columns[p_column].expand = p_expand;
  2959. table->columns[p_column].expand_ratio = p_ratio;
  2960. }
  2961. void RichTextLabel::set_cell_row_background_color(const Color &p_odd_row_bg, const Color &p_even_row_bg) {
  2962. _stop_thread();
  2963. MutexLock data_lock(data_mutex);
  2964. ERR_FAIL_COND(current->type != ITEM_FRAME);
  2965. ItemFrame *cell = static_cast<ItemFrame *>(current);
  2966. ERR_FAIL_COND(!cell->cell);
  2967. cell->odd_row_bg = p_odd_row_bg;
  2968. cell->even_row_bg = p_even_row_bg;
  2969. }
  2970. void RichTextLabel::set_cell_border_color(const Color &p_color) {
  2971. _stop_thread();
  2972. MutexLock data_lock(data_mutex);
  2973. ERR_FAIL_COND(current->type != ITEM_FRAME);
  2974. ItemFrame *cell = static_cast<ItemFrame *>(current);
  2975. ERR_FAIL_COND(!cell->cell);
  2976. cell->border = p_color;
  2977. }
  2978. void RichTextLabel::set_cell_size_override(const Size2 &p_min_size, const Size2 &p_max_size) {
  2979. _stop_thread();
  2980. MutexLock data_lock(data_mutex);
  2981. ERR_FAIL_COND(current->type != ITEM_FRAME);
  2982. ItemFrame *cell = static_cast<ItemFrame *>(current);
  2983. ERR_FAIL_COND(!cell->cell);
  2984. cell->min_size_over = p_min_size;
  2985. cell->max_size_over = p_max_size;
  2986. }
  2987. void RichTextLabel::set_cell_padding(const Rect2 &p_padding) {
  2988. _stop_thread();
  2989. MutexLock data_lock(data_mutex);
  2990. ERR_FAIL_COND(current->type != ITEM_FRAME);
  2991. ItemFrame *cell = static_cast<ItemFrame *>(current);
  2992. ERR_FAIL_COND(!cell->cell);
  2993. cell->padding = p_padding;
  2994. }
  2995. void RichTextLabel::push_cell() {
  2996. _stop_thread();
  2997. MutexLock data_lock(data_mutex);
  2998. ERR_FAIL_COND(current->type != ITEM_TABLE);
  2999. ItemFrame *item = memnew(ItemFrame);
  3000. item->parent_frame = current_frame;
  3001. _add_item(item, true);
  3002. current_frame = item;
  3003. item->cell = true;
  3004. item->lines.resize(1);
  3005. item->lines[0].from = nullptr;
  3006. item->first_invalid_line.store(0); // parent frame last line ???
  3007. }
  3008. int RichTextLabel::get_current_table_column() const {
  3009. ERR_FAIL_COND_V(current->type != ITEM_TABLE, -1);
  3010. ItemTable *table = static_cast<ItemTable *>(current);
  3011. return table->subitems.size() % table->columns.size();
  3012. }
  3013. void RichTextLabel::pop() {
  3014. _stop_thread();
  3015. MutexLock data_lock(data_mutex);
  3016. ERR_FAIL_COND(!current->parent);
  3017. if (current->type == ITEM_FRAME) {
  3018. current_frame = static_cast<ItemFrame *>(current)->parent_frame;
  3019. }
  3020. current = current->parent;
  3021. }
  3022. void RichTextLabel::clear() {
  3023. _stop_thread();
  3024. MutexLock data_lock(data_mutex);
  3025. main->_clear_children();
  3026. current = main;
  3027. current_frame = main;
  3028. main->lines.clear();
  3029. main->lines.resize(1);
  3030. main->first_invalid_line.store(0);
  3031. selection.click_frame = nullptr;
  3032. selection.click_item = nullptr;
  3033. deselect();
  3034. current_idx = 1;
  3035. current_char_ofs = 0;
  3036. if (scroll_follow) {
  3037. scroll_following = true;
  3038. }
  3039. if (fit_content) {
  3040. update_minimum_size();
  3041. }
  3042. }
  3043. void RichTextLabel::set_tab_size(int p_spaces) {
  3044. if (tab_size == p_spaces) {
  3045. return;
  3046. }
  3047. _stop_thread();
  3048. tab_size = p_spaces;
  3049. main->first_resized_line.store(0);
  3050. queue_redraw();
  3051. }
  3052. int RichTextLabel::get_tab_size() const {
  3053. return tab_size;
  3054. }
  3055. void RichTextLabel::set_fit_content(bool p_enabled) {
  3056. if (p_enabled == fit_content) {
  3057. return;
  3058. }
  3059. fit_content = p_enabled;
  3060. update_minimum_size();
  3061. }
  3062. bool RichTextLabel::is_fit_content_enabled() const {
  3063. return fit_content;
  3064. }
  3065. void RichTextLabel::set_meta_underline(bool p_underline) {
  3066. if (underline_meta == p_underline) {
  3067. return;
  3068. }
  3069. underline_meta = p_underline;
  3070. queue_redraw();
  3071. }
  3072. bool RichTextLabel::is_meta_underlined() const {
  3073. return underline_meta;
  3074. }
  3075. void RichTextLabel::set_hint_underline(bool p_underline) {
  3076. underline_hint = p_underline;
  3077. queue_redraw();
  3078. }
  3079. bool RichTextLabel::is_hint_underlined() const {
  3080. return underline_hint;
  3081. }
  3082. void RichTextLabel::set_offset(int p_pixel) {
  3083. vscroll->set_value(p_pixel);
  3084. }
  3085. void RichTextLabel::set_scroll_active(bool p_active) {
  3086. if (scroll_active == p_active) {
  3087. return;
  3088. }
  3089. scroll_active = p_active;
  3090. vscroll->set_drag_node_enabled(p_active);
  3091. queue_redraw();
  3092. }
  3093. bool RichTextLabel::is_scroll_active() const {
  3094. return scroll_active;
  3095. }
  3096. void RichTextLabel::set_scroll_follow(bool p_follow) {
  3097. scroll_follow = p_follow;
  3098. if (!vscroll->is_visible_in_tree() || vscroll->get_value() >= (vscroll->get_max() - vscroll->get_page())) {
  3099. scroll_following = true;
  3100. }
  3101. }
  3102. bool RichTextLabel::is_scroll_following() const {
  3103. return scroll_follow;
  3104. }
  3105. void RichTextLabel::parse_bbcode(const String &p_bbcode) {
  3106. clear();
  3107. append_text(p_bbcode);
  3108. }
  3109. void RichTextLabel::append_text(const String &p_bbcode) {
  3110. _stop_thread();
  3111. MutexLock data_lock(data_mutex);
  3112. int pos = 0;
  3113. List<String> tag_stack;
  3114. int indent_level = 0;
  3115. bool in_bold = false;
  3116. bool in_italics = false;
  3117. bool after_list_open_tag = false;
  3118. bool after_list_close_tag = false;
  3119. set_process_internal(false);
  3120. while (pos <= p_bbcode.length()) {
  3121. int brk_pos = p_bbcode.find("[", pos);
  3122. if (brk_pos < 0) {
  3123. brk_pos = p_bbcode.length();
  3124. }
  3125. String txt = brk_pos > pos ? p_bbcode.substr(pos, brk_pos - pos) : "";
  3126. // Trim the first newline character, it may be added later as needed.
  3127. if (after_list_close_tag || after_list_open_tag) {
  3128. txt = txt.trim_prefix("\n");
  3129. }
  3130. if (brk_pos == p_bbcode.length()) {
  3131. // For tags that are not properly closed.
  3132. if (txt.is_empty() && after_list_open_tag) {
  3133. txt = "\n";
  3134. }
  3135. if (!txt.is_empty()) {
  3136. add_text(txt);
  3137. }
  3138. break; //nothing else to add
  3139. }
  3140. int brk_end = p_bbcode.find("]", brk_pos + 1);
  3141. if (brk_end == -1) {
  3142. //no close, add the rest
  3143. txt += p_bbcode.substr(brk_pos, p_bbcode.length() - brk_pos);
  3144. add_text(txt);
  3145. break;
  3146. }
  3147. String tag = p_bbcode.substr(brk_pos + 1, brk_end - brk_pos - 1);
  3148. Vector<String> split_tag_block = tag.split(" ", false);
  3149. // Find optional parameters.
  3150. String bbcode_name;
  3151. typedef HashMap<String, String> OptionMap;
  3152. OptionMap bbcode_options;
  3153. if (!split_tag_block.is_empty()) {
  3154. bbcode_name = split_tag_block[0];
  3155. for (int i = 1; i < split_tag_block.size(); i++) {
  3156. const String &expr = split_tag_block[i];
  3157. int value_pos = expr.find("=");
  3158. if (value_pos > -1) {
  3159. bbcode_options[expr.substr(0, value_pos)] = expr.substr(value_pos + 1);
  3160. }
  3161. }
  3162. } else {
  3163. bbcode_name = tag;
  3164. }
  3165. // Find main parameter.
  3166. String bbcode_value;
  3167. int main_value_pos = bbcode_name.find("=");
  3168. if (main_value_pos > -1) {
  3169. bbcode_value = bbcode_name.substr(main_value_pos + 1);
  3170. bbcode_name = bbcode_name.substr(0, main_value_pos);
  3171. }
  3172. if (tag.begins_with("/") && tag_stack.size()) {
  3173. bool tag_ok = tag_stack.size() && tag_stack.front()->get() == tag.substr(1, tag.length());
  3174. if (tag_stack.front()->get() == "b") {
  3175. in_bold = false;
  3176. }
  3177. if (tag_stack.front()->get() == "i") {
  3178. in_italics = false;
  3179. }
  3180. if ((tag_stack.front()->get() == "indent") || (tag_stack.front()->get() == "ol") || (tag_stack.front()->get() == "ul")) {
  3181. indent_level--;
  3182. }
  3183. if (!tag_ok) {
  3184. txt += "[" + tag;
  3185. add_text(txt);
  3186. after_list_open_tag = false;
  3187. after_list_close_tag = false;
  3188. pos = brk_end;
  3189. continue;
  3190. }
  3191. if (txt.is_empty() && after_list_open_tag) {
  3192. txt = "\n"; // Make empty list have at least one item.
  3193. }
  3194. after_list_open_tag = false;
  3195. if (tag == "/ol" || tag == "/ul") {
  3196. if (!txt.is_empty()) {
  3197. // Make sure text ends with a newline character, that is, the last item
  3198. // will wrap at the end of block.
  3199. if (!txt.ends_with("\n")) {
  3200. txt += "\n";
  3201. }
  3202. } else if (!after_list_close_tag) {
  3203. txt = "\n"; // Make the innermost list item wrap at the end of lists.
  3204. }
  3205. after_list_close_tag = true;
  3206. } else {
  3207. after_list_close_tag = false;
  3208. }
  3209. if (!txt.is_empty()) {
  3210. add_text(txt);
  3211. }
  3212. tag_stack.pop_front();
  3213. pos = brk_end + 1;
  3214. if (tag != "/img" && tag != "/dropcap") {
  3215. pop();
  3216. }
  3217. continue;
  3218. }
  3219. if (tag == "ol" || tag.begins_with("ol ") || tag == "ul" || tag.begins_with("ul ")) {
  3220. if (txt.is_empty() && after_list_open_tag) {
  3221. txt = "\n"; // Make each list have at least one item at the beginning.
  3222. }
  3223. after_list_open_tag = true;
  3224. } else {
  3225. after_list_open_tag = false;
  3226. }
  3227. if (!txt.is_empty()) {
  3228. add_text(txt);
  3229. }
  3230. after_list_close_tag = false;
  3231. if (tag == "b") {
  3232. //use bold font
  3233. in_bold = true;
  3234. if (in_italics) {
  3235. _push_def_font(BOLD_ITALICS_FONT);
  3236. } else {
  3237. _push_def_font(BOLD_FONT);
  3238. }
  3239. pos = brk_end + 1;
  3240. tag_stack.push_front(tag);
  3241. } else if (tag == "i") {
  3242. //use italics font
  3243. in_italics = true;
  3244. if (in_bold) {
  3245. _push_def_font(BOLD_ITALICS_FONT);
  3246. } else {
  3247. _push_def_font(ITALICS_FONT);
  3248. }
  3249. pos = brk_end + 1;
  3250. tag_stack.push_front(tag);
  3251. } else if (tag == "code") {
  3252. //use monospace font
  3253. _push_def_font(MONO_FONT);
  3254. pos = brk_end + 1;
  3255. tag_stack.push_front(tag);
  3256. } else if (tag.begins_with("table=")) {
  3257. Vector<String> subtag = tag.substr(6, tag.length()).split(",");
  3258. int columns = subtag[0].to_int();
  3259. if (columns < 1) {
  3260. columns = 1;
  3261. }
  3262. int alignment = INLINE_ALIGNMENT_TOP;
  3263. if (subtag.size() > 2) {
  3264. if (subtag[1] == "top" || subtag[1] == "t") {
  3265. alignment = INLINE_ALIGNMENT_TOP_TO;
  3266. } else if (subtag[1] == "center" || subtag[1] == "c") {
  3267. alignment = INLINE_ALIGNMENT_CENTER_TO;
  3268. } else if (subtag[1] == "baseline" || subtag[1] == "l") {
  3269. alignment = INLINE_ALIGNMENT_BASELINE_TO;
  3270. } else if (subtag[1] == "bottom" || subtag[1] == "b") {
  3271. alignment = INLINE_ALIGNMENT_BOTTOM_TO;
  3272. }
  3273. if (subtag[2] == "top" || subtag[2] == "t") {
  3274. alignment |= INLINE_ALIGNMENT_TO_TOP;
  3275. } else if (subtag[2] == "center" || subtag[2] == "c") {
  3276. alignment |= INLINE_ALIGNMENT_TO_CENTER;
  3277. } else if (subtag[2] == "baseline" || subtag[2] == "l") {
  3278. alignment |= INLINE_ALIGNMENT_TO_BASELINE;
  3279. } else if (subtag[2] == "bottom" || subtag[2] == "b") {
  3280. alignment |= INLINE_ALIGNMENT_TO_BOTTOM;
  3281. }
  3282. } else if (subtag.size() > 1) {
  3283. if (subtag[1] == "top" || subtag[1] == "t") {
  3284. alignment = INLINE_ALIGNMENT_TOP;
  3285. } else if (subtag[1] == "center" || subtag[1] == "c") {
  3286. alignment = INLINE_ALIGNMENT_CENTER;
  3287. } else if (subtag[1] == "bottom" || subtag[1] == "b") {
  3288. alignment = INLINE_ALIGNMENT_BOTTOM;
  3289. }
  3290. }
  3291. int row = -1;
  3292. if (subtag.size() > 3) {
  3293. row = subtag[3].to_int();
  3294. }
  3295. push_table(columns, (InlineAlignment)alignment, row);
  3296. pos = brk_end + 1;
  3297. tag_stack.push_front("table");
  3298. } else if (tag == "cell") {
  3299. push_cell();
  3300. pos = brk_end + 1;
  3301. tag_stack.push_front(tag);
  3302. } else if (tag.begins_with("cell=")) {
  3303. int ratio = tag.substr(5, tag.length()).to_int();
  3304. if (ratio < 1) {
  3305. ratio = 1;
  3306. }
  3307. set_table_column_expand(get_current_table_column(), true, ratio);
  3308. push_cell();
  3309. pos = brk_end + 1;
  3310. tag_stack.push_front("cell");
  3311. } else if (tag.begins_with("cell ")) {
  3312. Vector<String> subtag = tag.substr(5, tag.length()).split(" ");
  3313. for (int i = 0; i < subtag.size(); i++) {
  3314. Vector<String> subtag_a = subtag[i].split("=");
  3315. if (subtag_a.size() == 2) {
  3316. if (subtag_a[0] == "expand") {
  3317. int ratio = subtag_a[1].to_int();
  3318. if (ratio < 1) {
  3319. ratio = 1;
  3320. }
  3321. set_table_column_expand(get_current_table_column(), true, ratio);
  3322. }
  3323. }
  3324. }
  3325. push_cell();
  3326. const Color fallback_color = Color(0, 0, 0, 0);
  3327. for (int i = 0; i < subtag.size(); i++) {
  3328. Vector<String> subtag_a = subtag[i].split("=");
  3329. if (subtag_a.size() == 2) {
  3330. if (subtag_a[0] == "border") {
  3331. Color color = Color::from_string(subtag_a[1], fallback_color);
  3332. set_cell_border_color(color);
  3333. } else if (subtag_a[0] == "bg") {
  3334. Vector<String> subtag_b = subtag_a[1].split(",");
  3335. if (subtag_b.size() == 2) {
  3336. Color color1 = Color::from_string(subtag_b[0], fallback_color);
  3337. Color color2 = Color::from_string(subtag_b[1], fallback_color);
  3338. set_cell_row_background_color(color1, color2);
  3339. }
  3340. if (subtag_b.size() == 1) {
  3341. Color color1 = Color::from_string(subtag_a[1], fallback_color);
  3342. set_cell_row_background_color(color1, color1);
  3343. }
  3344. } else if (subtag_a[0] == "padding") {
  3345. Vector<String> subtag_b = subtag_a[1].split(",");
  3346. if (subtag_b.size() == 4) {
  3347. set_cell_padding(Rect2(subtag_b[0].to_float(), subtag_b[1].to_float(), subtag_b[2].to_float(), subtag_b[3].to_float()));
  3348. }
  3349. }
  3350. }
  3351. }
  3352. pos = brk_end + 1;
  3353. tag_stack.push_front("cell");
  3354. } else if (tag == "u") {
  3355. //use underline
  3356. push_underline();
  3357. pos = brk_end + 1;
  3358. tag_stack.push_front(tag);
  3359. } else if (tag == "s") {
  3360. //use strikethrough
  3361. push_strikethrough();
  3362. pos = brk_end + 1;
  3363. tag_stack.push_front(tag);
  3364. } else if (tag == "lb") {
  3365. add_text("[");
  3366. pos = brk_end + 1;
  3367. } else if (tag == "rb") {
  3368. add_text("]");
  3369. pos = brk_end + 1;
  3370. } else if (tag == "lrm") {
  3371. add_text(String::chr(0x200E));
  3372. pos = brk_end + 1;
  3373. } else if (tag == "rlm") {
  3374. add_text(String::chr(0x200F));
  3375. pos = brk_end + 1;
  3376. } else if (tag == "lre") {
  3377. add_text(String::chr(0x202A));
  3378. pos = brk_end + 1;
  3379. } else if (tag == "rle") {
  3380. add_text(String::chr(0x202B));
  3381. pos = brk_end + 1;
  3382. } else if (tag == "lro") {
  3383. add_text(String::chr(0x202D));
  3384. pos = brk_end + 1;
  3385. } else if (tag == "rlo") {
  3386. add_text(String::chr(0x202E));
  3387. pos = brk_end + 1;
  3388. } else if (tag == "pdf") {
  3389. add_text(String::chr(0x202C));
  3390. pos = brk_end + 1;
  3391. } else if (tag == "alm") {
  3392. add_text(String::chr(0x061c));
  3393. pos = brk_end + 1;
  3394. } else if (tag == "lri") {
  3395. add_text(String::chr(0x2066));
  3396. pos = brk_end + 1;
  3397. } else if (tag == "rli") {
  3398. add_text(String::chr(0x2027));
  3399. pos = brk_end + 1;
  3400. } else if (tag == "fsi") {
  3401. add_text(String::chr(0x2068));
  3402. pos = brk_end + 1;
  3403. } else if (tag == "pdi") {
  3404. add_text(String::chr(0x2069));
  3405. pos = brk_end + 1;
  3406. } else if (tag == "zwj") {
  3407. add_text(String::chr(0x200D));
  3408. pos = brk_end + 1;
  3409. } else if (tag == "zwnj") {
  3410. add_text(String::chr(0x200C));
  3411. pos = brk_end + 1;
  3412. } else if (tag == "wj") {
  3413. add_text(String::chr(0x2060));
  3414. pos = brk_end + 1;
  3415. } else if (tag == "shy") {
  3416. add_text(String::chr(0x00AD));
  3417. pos = brk_end + 1;
  3418. } else if (tag == "center") {
  3419. push_paragraph(HORIZONTAL_ALIGNMENT_CENTER);
  3420. pos = brk_end + 1;
  3421. tag_stack.push_front(tag);
  3422. } else if (tag == "fill") {
  3423. push_paragraph(HORIZONTAL_ALIGNMENT_FILL);
  3424. pos = brk_end + 1;
  3425. tag_stack.push_front(tag);
  3426. } else if (tag == "left") {
  3427. push_paragraph(HORIZONTAL_ALIGNMENT_LEFT);
  3428. pos = brk_end + 1;
  3429. tag_stack.push_front(tag);
  3430. } else if (tag == "right") {
  3431. push_paragraph(HORIZONTAL_ALIGNMENT_RIGHT);
  3432. pos = brk_end + 1;
  3433. tag_stack.push_front(tag);
  3434. } else if (tag == "ul") {
  3435. indent_level++;
  3436. push_list(indent_level, LIST_DOTS, false);
  3437. pos = brk_end + 1;
  3438. tag_stack.push_front(tag);
  3439. } else if ((tag == "ol") || (tag == "ol type=1")) {
  3440. indent_level++;
  3441. push_list(indent_level, LIST_NUMBERS, false);
  3442. pos = brk_end + 1;
  3443. tag_stack.push_front("ol");
  3444. } else if (tag == "ol type=a") {
  3445. indent_level++;
  3446. push_list(indent_level, LIST_LETTERS, false);
  3447. pos = brk_end + 1;
  3448. tag_stack.push_front("ol");
  3449. } else if (tag == "ol type=A") {
  3450. indent_level++;
  3451. push_list(indent_level, LIST_LETTERS, true);
  3452. pos = brk_end + 1;
  3453. tag_stack.push_front("ol");
  3454. } else if (tag == "ol type=i") {
  3455. indent_level++;
  3456. push_list(indent_level, LIST_ROMAN, false);
  3457. pos = brk_end + 1;
  3458. tag_stack.push_front("ol");
  3459. } else if (tag == "ol type=I") {
  3460. indent_level++;
  3461. push_list(indent_level, LIST_ROMAN, true);
  3462. pos = brk_end + 1;
  3463. tag_stack.push_front("ol");
  3464. } else if (tag == "indent") {
  3465. indent_level++;
  3466. push_indent(indent_level);
  3467. pos = brk_end + 1;
  3468. tag_stack.push_front(tag);
  3469. } else if (tag == "p") {
  3470. push_paragraph(HORIZONTAL_ALIGNMENT_LEFT);
  3471. pos = brk_end + 1;
  3472. tag_stack.push_front("p");
  3473. } else if (tag.begins_with("p ")) {
  3474. Vector<String> subtag = tag.substr(2, tag.length()).split(" ");
  3475. HorizontalAlignment alignment = HORIZONTAL_ALIGNMENT_LEFT;
  3476. Control::TextDirection dir = Control::TEXT_DIRECTION_INHERITED;
  3477. String lang;
  3478. TextServer::StructuredTextParser st_parser_type = TextServer::STRUCTURED_TEXT_DEFAULT;
  3479. for (int i = 0; i < subtag.size(); i++) {
  3480. Vector<String> subtag_a = subtag[i].split("=");
  3481. if (subtag_a.size() == 2) {
  3482. if (subtag_a[0] == "align") {
  3483. if (subtag_a[1] == "l" || subtag_a[1] == "left") {
  3484. alignment = HORIZONTAL_ALIGNMENT_LEFT;
  3485. } else if (subtag_a[1] == "c" || subtag_a[1] == "center") {
  3486. alignment = HORIZONTAL_ALIGNMENT_CENTER;
  3487. } else if (subtag_a[1] == "r" || subtag_a[1] == "right") {
  3488. alignment = HORIZONTAL_ALIGNMENT_RIGHT;
  3489. } else if (subtag_a[1] == "f" || subtag_a[1] == "fill") {
  3490. alignment = HORIZONTAL_ALIGNMENT_FILL;
  3491. }
  3492. } else if (subtag_a[0] == "dir" || subtag_a[0] == "direction") {
  3493. if (subtag_a[1] == "a" || subtag_a[1] == "auto") {
  3494. dir = Control::TEXT_DIRECTION_AUTO;
  3495. } else if (subtag_a[1] == "l" || subtag_a[1] == "ltr") {
  3496. dir = Control::TEXT_DIRECTION_LTR;
  3497. } else if (subtag_a[1] == "r" || subtag_a[1] == "rtl") {
  3498. dir = Control::TEXT_DIRECTION_RTL;
  3499. }
  3500. } else if (subtag_a[0] == "lang" || subtag_a[0] == "language") {
  3501. lang = subtag_a[1];
  3502. } else if (subtag_a[0] == "st" || subtag_a[0] == "bidi_override") {
  3503. if (subtag_a[1] == "d" || subtag_a[1] == "default") {
  3504. st_parser_type = TextServer::STRUCTURED_TEXT_DEFAULT;
  3505. } else if (subtag_a[1] == "u" || subtag_a[1] == "uri") {
  3506. st_parser_type = TextServer::STRUCTURED_TEXT_URI;
  3507. } else if (subtag_a[1] == "f" || subtag_a[1] == "file") {
  3508. st_parser_type = TextServer::STRUCTURED_TEXT_FILE;
  3509. } else if (subtag_a[1] == "e" || subtag_a[1] == "email") {
  3510. st_parser_type = TextServer::STRUCTURED_TEXT_EMAIL;
  3511. } else if (subtag_a[1] == "l" || subtag_a[1] == "list") {
  3512. st_parser_type = TextServer::STRUCTURED_TEXT_LIST;
  3513. } else if (subtag_a[1] == "n" || subtag_a[1] == "gdscript") {
  3514. st_parser_type = TextServer::STRUCTURED_TEXT_GDSCRIPT;
  3515. } else if (subtag_a[1] == "c" || subtag_a[1] == "custom") {
  3516. st_parser_type = TextServer::STRUCTURED_TEXT_CUSTOM;
  3517. }
  3518. }
  3519. }
  3520. }
  3521. push_paragraph(alignment, dir, lang, st_parser_type);
  3522. pos = brk_end + 1;
  3523. tag_stack.push_front("p");
  3524. } else if (tag == "url") {
  3525. int end = p_bbcode.find("[", brk_end);
  3526. if (end == -1) {
  3527. end = p_bbcode.length();
  3528. }
  3529. String url = p_bbcode.substr(brk_end + 1, end - brk_end - 1);
  3530. push_meta(url);
  3531. pos = brk_end + 1;
  3532. tag_stack.push_front(tag);
  3533. } else if (tag.begins_with("url=")) {
  3534. String url = tag.substr(4, tag.length());
  3535. push_meta(url);
  3536. pos = brk_end + 1;
  3537. tag_stack.push_front("url");
  3538. } else if (tag.begins_with("hint=")) {
  3539. String description = tag.substr(5, tag.length());
  3540. push_hint(description);
  3541. pos = brk_end + 1;
  3542. tag_stack.push_front("hint");
  3543. } else if (tag.begins_with("dropcap")) {
  3544. Vector<String> subtag = tag.substr(5, tag.length()).split(" ");
  3545. int fs = theme_cache.normal_font_size * 3;
  3546. Ref<Font> f = theme_cache.normal_font;
  3547. Color color = theme_cache.default_color;
  3548. Color outline_color = theme_cache.font_outline_color;
  3549. int outline_size = theme_cache.outline_size;
  3550. Rect2 dropcap_margins;
  3551. for (int i = 0; i < subtag.size(); i++) {
  3552. Vector<String> subtag_a = subtag[i].split("=");
  3553. if (subtag_a.size() == 2) {
  3554. if (subtag_a[0] == "font" || subtag_a[0] == "f") {
  3555. String fnt = subtag_a[1];
  3556. Ref<Font> font = ResourceLoader::load(fnt, "Font");
  3557. if (font.is_valid()) {
  3558. f = font;
  3559. }
  3560. } else if (subtag_a[0] == "font_size") {
  3561. fs = subtag_a[1].to_int();
  3562. } else if (subtag_a[0] == "margins") {
  3563. Vector<String> subtag_b = subtag_a[1].split(",");
  3564. if (subtag_b.size() == 4) {
  3565. dropcap_margins.position.x = subtag_b[0].to_float();
  3566. dropcap_margins.position.y = subtag_b[1].to_float();
  3567. dropcap_margins.size.x = subtag_b[2].to_float();
  3568. dropcap_margins.size.y = subtag_b[3].to_float();
  3569. }
  3570. } else if (subtag_a[0] == "outline_size") {
  3571. outline_size = subtag_a[1].to_int();
  3572. } else if (subtag_a[0] == "color") {
  3573. color = Color::from_string(subtag_a[1], color);
  3574. } else if (subtag_a[0] == "outline_color") {
  3575. outline_color = Color::from_string(subtag_a[1], outline_color);
  3576. }
  3577. }
  3578. }
  3579. int end = p_bbcode.find("[", brk_end);
  3580. if (end == -1) {
  3581. end = p_bbcode.length();
  3582. }
  3583. String dc_txt = p_bbcode.substr(brk_end + 1, end - brk_end - 1);
  3584. push_dropcap(dc_txt, f, fs, dropcap_margins, color, outline_size, outline_color);
  3585. pos = end;
  3586. tag_stack.push_front(bbcode_name);
  3587. } else if (tag.begins_with("img")) {
  3588. int alignment = INLINE_ALIGNMENT_CENTER;
  3589. if (tag.begins_with("img=")) {
  3590. Vector<String> subtag = tag.substr(4, tag.length()).split(",");
  3591. if (subtag.size() > 1) {
  3592. if (subtag[0] == "top" || subtag[0] == "t") {
  3593. alignment = INLINE_ALIGNMENT_TOP_TO;
  3594. } else if (subtag[0] == "center" || subtag[0] == "c") {
  3595. alignment = INLINE_ALIGNMENT_CENTER_TO;
  3596. } else if (subtag[0] == "bottom" || subtag[0] == "b") {
  3597. alignment = INLINE_ALIGNMENT_BOTTOM_TO;
  3598. }
  3599. if (subtag[1] == "top" || subtag[1] == "t") {
  3600. alignment |= INLINE_ALIGNMENT_TO_TOP;
  3601. } else if (subtag[1] == "center" || subtag[1] == "c") {
  3602. alignment |= INLINE_ALIGNMENT_TO_CENTER;
  3603. } else if (subtag[1] == "baseline" || subtag[1] == "l") {
  3604. alignment |= INLINE_ALIGNMENT_TO_BASELINE;
  3605. } else if (subtag[1] == "bottom" || subtag[1] == "b") {
  3606. alignment |= INLINE_ALIGNMENT_TO_BOTTOM;
  3607. }
  3608. } else if (subtag.size() > 0) {
  3609. if (subtag[0] == "top" || subtag[0] == "t") {
  3610. alignment = INLINE_ALIGNMENT_TOP;
  3611. } else if (subtag[0] == "center" || subtag[0] == "c") {
  3612. alignment = INLINE_ALIGNMENT_CENTER;
  3613. } else if (subtag[0] == "bottom" || subtag[0] == "b") {
  3614. alignment = INLINE_ALIGNMENT_BOTTOM;
  3615. }
  3616. }
  3617. }
  3618. int end = p_bbcode.find("[", brk_end);
  3619. if (end == -1) {
  3620. end = p_bbcode.length();
  3621. }
  3622. String image = p_bbcode.substr(brk_end + 1, end - brk_end - 1);
  3623. Ref<Texture2D> texture = ResourceLoader::load(image, "Texture2D");
  3624. if (texture.is_valid()) {
  3625. Rect2 region;
  3626. OptionMap::Iterator region_option = bbcode_options.find("region");
  3627. if (region_option) {
  3628. Vector<String> region_values = region_option->value.split(",", false);
  3629. if (region_values.size() == 4) {
  3630. region.position.x = region_values[0].to_float();
  3631. region.position.y = region_values[1].to_float();
  3632. region.size.x = region_values[2].to_float();
  3633. region.size.y = region_values[3].to_float();
  3634. }
  3635. }
  3636. Color color = Color(1.0, 1.0, 1.0);
  3637. OptionMap::Iterator color_option = bbcode_options.find("color");
  3638. if (color_option) {
  3639. color = Color::from_string(color_option->value, color);
  3640. }
  3641. int width = 0;
  3642. int height = 0;
  3643. if (!bbcode_value.is_empty()) {
  3644. int sep = bbcode_value.find("x");
  3645. if (sep == -1) {
  3646. width = bbcode_value.to_int();
  3647. } else {
  3648. width = bbcode_value.substr(0, sep).to_int();
  3649. height = bbcode_value.substr(sep + 1).to_int();
  3650. }
  3651. } else {
  3652. OptionMap::Iterator width_option = bbcode_options.find("width");
  3653. if (width_option) {
  3654. width = width_option->value.to_int();
  3655. }
  3656. OptionMap::Iterator height_option = bbcode_options.find("height");
  3657. if (height_option) {
  3658. height = height_option->value.to_int();
  3659. }
  3660. }
  3661. add_image(texture, width, height, color, (InlineAlignment)alignment, region);
  3662. }
  3663. pos = end;
  3664. tag_stack.push_front(bbcode_name);
  3665. } else if (tag.begins_with("color=")) {
  3666. String color_str = tag.substr(6, tag.length());
  3667. Color color = Color::from_string(color_str, theme_cache.default_color);
  3668. push_color(color);
  3669. pos = brk_end + 1;
  3670. tag_stack.push_front("color");
  3671. } else if (tag.begins_with("outline_color=")) {
  3672. String color_str = tag.substr(14, tag.length());
  3673. Color color = Color::from_string(color_str, theme_cache.default_color);
  3674. push_outline_color(color);
  3675. pos = brk_end + 1;
  3676. tag_stack.push_front("outline_color");
  3677. } else if (tag.begins_with("font_size=")) {
  3678. int fnt_size = tag.substr(10, tag.length()).to_int();
  3679. push_font_size(fnt_size);
  3680. pos = brk_end + 1;
  3681. tag_stack.push_front("font_size");
  3682. } else if (tag.begins_with("opentype_features=") || tag.begins_with("otf=")) {
  3683. int value_pos = tag.find("=");
  3684. String fnt_ftr = tag.substr(value_pos + 1);
  3685. Vector<String> subtag = fnt_ftr.split(",");
  3686. if (subtag.size() > 0) {
  3687. Ref<Font> font = theme_cache.normal_font;
  3688. DefaultFont def_font = NORMAL_FONT;
  3689. ItemFont *font_it = _find_font(current);
  3690. if (font_it) {
  3691. if (font_it->font.is_valid()) {
  3692. font = font_it->font;
  3693. def_font = font_it->def_font;
  3694. }
  3695. }
  3696. Dictionary features;
  3697. for (int i = 0; i < subtag.size(); i++) {
  3698. Vector<String> subtag_a = subtag[i].split("=");
  3699. if (subtag_a.size() == 2) {
  3700. features[TS->name_to_tag(subtag_a[0])] = subtag_a[1].to_int();
  3701. } else if (subtag_a.size() == 1) {
  3702. features[TS->name_to_tag(subtag_a[0])] = 1;
  3703. }
  3704. }
  3705. Ref<FontVariation> fc;
  3706. fc.instantiate();
  3707. fc->set_base_font(font);
  3708. fc->set_opentype_features(features);
  3709. if (def_font != CUSTOM_FONT) {
  3710. _push_def_font_var(def_font, fc);
  3711. } else {
  3712. push_font(fc);
  3713. }
  3714. }
  3715. pos = brk_end + 1;
  3716. tag_stack.push_front(tag.substr(0, value_pos));
  3717. } else if (tag.begins_with("font=")) {
  3718. String fnt = tag.substr(5, tag.length());
  3719. Ref<Font> fc = ResourceLoader::load(fnt, "Font");
  3720. if (fc.is_valid()) {
  3721. push_font(fc);
  3722. }
  3723. pos = brk_end + 1;
  3724. tag_stack.push_front("font");
  3725. } else if (tag.begins_with("font ")) {
  3726. Vector<String> subtag = tag.substr(2, tag.length()).split(" ");
  3727. Ref<Font> font = theme_cache.normal_font;
  3728. DefaultFont def_font = NORMAL_FONT;
  3729. ItemFont *font_it = _find_font(current);
  3730. if (font_it) {
  3731. if (font_it->font.is_valid()) {
  3732. font = font_it->font;
  3733. def_font = font_it->def_font;
  3734. }
  3735. }
  3736. Ref<FontVariation> fc;
  3737. fc.instantiate();
  3738. int fnt_size = -1;
  3739. for (int i = 1; i < subtag.size(); i++) {
  3740. Vector<String> subtag_a = subtag[i].split("=", true, 2);
  3741. if (subtag_a.size() == 2) {
  3742. if (subtag_a[0] == "name" || subtag_a[0] == "n") {
  3743. String fnt = subtag_a[1];
  3744. Ref<Font> font_data = ResourceLoader::load(fnt, "Font");
  3745. if (font_data.is_valid()) {
  3746. font = font_data;
  3747. def_font = CUSTOM_FONT;
  3748. }
  3749. } else if (subtag_a[0] == "size" || subtag_a[0] == "s") {
  3750. fnt_size = subtag_a[1].to_int();
  3751. } else if (subtag_a[0] == "glyph_spacing" || subtag_a[0] == "gl") {
  3752. int spacing = subtag_a[1].to_int();
  3753. fc->set_spacing(TextServer::SPACING_GLYPH, spacing);
  3754. } else if (subtag_a[0] == "space_spacing" || subtag_a[0] == "sp") {
  3755. int spacing = subtag_a[1].to_int();
  3756. fc->set_spacing(TextServer::SPACING_SPACE, spacing);
  3757. } else if (subtag_a[0] == "top_spacing" || subtag_a[0] == "top") {
  3758. int spacing = subtag_a[1].to_int();
  3759. fc->set_spacing(TextServer::SPACING_TOP, spacing);
  3760. } else if (subtag_a[0] == "bottom_spacing" || subtag_a[0] == "bt") {
  3761. int spacing = subtag_a[1].to_int();
  3762. fc->set_spacing(TextServer::SPACING_BOTTOM, spacing);
  3763. } else if (subtag_a[0] == "embolden" || subtag_a[0] == "emb") {
  3764. float emb = subtag_a[1].to_float();
  3765. fc->set_variation_embolden(emb);
  3766. } else if (subtag_a[0] == "face_index" || subtag_a[0] == "fi") {
  3767. int fi = subtag_a[1].to_int();
  3768. fc->set_variation_face_index(fi);
  3769. } else if (subtag_a[0] == "slant" || subtag_a[0] == "sln") {
  3770. float slant = subtag_a[1].to_float();
  3771. fc->set_variation_transform(Transform2D(1.0, slant, 0.0, 1.0, 0.0, 0.0));
  3772. } else if (subtag_a[0] == "opentype_variation" || subtag_a[0] == "otv") {
  3773. Dictionary variations;
  3774. if (!subtag_a[1].is_empty()) {
  3775. Vector<String> variation_tags = subtag_a[1].split(",");
  3776. for (int j = 0; j < variation_tags.size(); j++) {
  3777. Vector<String> subtag_b = variation_tags[j].split("=");
  3778. if (subtag_b.size() == 2) {
  3779. variations[TS->name_to_tag(subtag_b[0])] = subtag_b[1].to_float();
  3780. }
  3781. }
  3782. fc->set_variation_opentype(variations);
  3783. }
  3784. } else if (subtag_a[0] == "opentype_features" || subtag_a[0] == "otf") {
  3785. Dictionary features;
  3786. if (!subtag_a[1].is_empty()) {
  3787. Vector<String> feature_tags = subtag_a[1].split(",");
  3788. for (int j = 0; j < feature_tags.size(); j++) {
  3789. Vector<String> subtag_b = feature_tags[j].split("=");
  3790. if (subtag_b.size() == 2) {
  3791. features[TS->name_to_tag(subtag_b[0])] = subtag_b[1].to_float();
  3792. } else if (subtag_b.size() == 1) {
  3793. features[TS->name_to_tag(subtag_b[0])] = 1;
  3794. }
  3795. }
  3796. fc->set_opentype_features(features);
  3797. }
  3798. }
  3799. }
  3800. }
  3801. fc->set_base_font(font);
  3802. if (def_font != CUSTOM_FONT) {
  3803. _push_def_font_var(def_font, fc, fnt_size);
  3804. } else {
  3805. push_font(fc, fnt_size);
  3806. }
  3807. pos = brk_end + 1;
  3808. tag_stack.push_front("font");
  3809. } else if (tag.begins_with("outline_size=")) {
  3810. int fnt_size = tag.substr(13, tag.length()).to_int();
  3811. if (fnt_size > 0) {
  3812. push_outline_size(fnt_size);
  3813. }
  3814. pos = brk_end + 1;
  3815. tag_stack.push_front("outline_size");
  3816. } else if (bbcode_name == "fade") {
  3817. int start_index = 0;
  3818. OptionMap::Iterator start_option = bbcode_options.find("start");
  3819. if (start_option) {
  3820. start_index = start_option->value.to_int();
  3821. }
  3822. int length = 10;
  3823. OptionMap::Iterator length_option = bbcode_options.find("length");
  3824. if (length_option) {
  3825. length = length_option->value.to_int();
  3826. }
  3827. push_fade(start_index, length);
  3828. pos = brk_end + 1;
  3829. tag_stack.push_front("fade");
  3830. } else if (bbcode_name == "shake") {
  3831. int strength = 5;
  3832. OptionMap::Iterator strength_option = bbcode_options.find("level");
  3833. if (strength_option) {
  3834. strength = strength_option->value.to_int();
  3835. }
  3836. float rate = 20.0f;
  3837. OptionMap::Iterator rate_option = bbcode_options.find("rate");
  3838. if (rate_option) {
  3839. rate = rate_option->value.to_float();
  3840. }
  3841. bool connected = true;
  3842. OptionMap::Iterator connected_option = bbcode_options.find("connected");
  3843. if (connected_option) {
  3844. connected = connected_option->value.to_int();
  3845. }
  3846. push_shake(strength, rate, connected);
  3847. pos = brk_end + 1;
  3848. tag_stack.push_front("shake");
  3849. set_process_internal(true);
  3850. } else if (bbcode_name == "wave") {
  3851. float amplitude = 20.0f;
  3852. OptionMap::Iterator amplitude_option = bbcode_options.find("amp");
  3853. if (amplitude_option) {
  3854. amplitude = amplitude_option->value.to_float();
  3855. }
  3856. float period = 5.0f;
  3857. OptionMap::Iterator period_option = bbcode_options.find("freq");
  3858. if (period_option) {
  3859. period = period_option->value.to_float();
  3860. }
  3861. bool connected = true;
  3862. OptionMap::Iterator connected_option = bbcode_options.find("connected");
  3863. if (connected_option) {
  3864. connected = connected_option->value.to_int();
  3865. }
  3866. push_wave(period, amplitude, connected);
  3867. pos = brk_end + 1;
  3868. tag_stack.push_front("wave");
  3869. set_process_internal(true);
  3870. } else if (bbcode_name == "tornado") {
  3871. float radius = 10.0f;
  3872. OptionMap::Iterator radius_option = bbcode_options.find("radius");
  3873. if (radius_option) {
  3874. radius = radius_option->value.to_float();
  3875. }
  3876. float frequency = 1.0f;
  3877. OptionMap::Iterator frequency_option = bbcode_options.find("freq");
  3878. if (frequency_option) {
  3879. frequency = frequency_option->value.to_float();
  3880. }
  3881. bool connected = true;
  3882. OptionMap::Iterator connected_option = bbcode_options.find("connected");
  3883. if (connected_option) {
  3884. connected = connected_option->value.to_int();
  3885. }
  3886. push_tornado(frequency, radius, connected);
  3887. pos = brk_end + 1;
  3888. tag_stack.push_front("tornado");
  3889. set_process_internal(true);
  3890. } else if (bbcode_name == "rainbow") {
  3891. float saturation = 0.8f;
  3892. OptionMap::Iterator saturation_option = bbcode_options.find("sat");
  3893. if (saturation_option) {
  3894. saturation = saturation_option->value.to_float();
  3895. }
  3896. float value = 0.8f;
  3897. OptionMap::Iterator value_option = bbcode_options.find("val");
  3898. if (value_option) {
  3899. value = value_option->value.to_float();
  3900. }
  3901. float frequency = 1.0f;
  3902. OptionMap::Iterator frequency_option = bbcode_options.find("freq");
  3903. if (frequency_option) {
  3904. frequency = frequency_option->value.to_float();
  3905. }
  3906. push_rainbow(saturation, value, frequency);
  3907. pos = brk_end + 1;
  3908. tag_stack.push_front("rainbow");
  3909. set_process_internal(true);
  3910. } else if (tag.begins_with("bgcolor=")) {
  3911. String color_str = tag.substr(8, tag.length());
  3912. Color color = Color::from_string(color_str, theme_cache.default_color);
  3913. push_bgcolor(color);
  3914. pos = brk_end + 1;
  3915. tag_stack.push_front("bgcolor");
  3916. } else if (tag.begins_with("fgcolor=")) {
  3917. String color_str = tag.substr(8, tag.length());
  3918. Color color = Color::from_string(color_str, theme_cache.default_color);
  3919. push_fgcolor(color);
  3920. pos = brk_end + 1;
  3921. tag_stack.push_front("fgcolor");
  3922. } else {
  3923. Vector<String> &expr = split_tag_block;
  3924. if (expr.size() < 1) {
  3925. add_text("[");
  3926. pos = brk_pos + 1;
  3927. } else {
  3928. String identifier = expr[0];
  3929. expr.remove_at(0);
  3930. Dictionary properties = parse_expressions_for_values(expr);
  3931. Ref<RichTextEffect> effect = _get_custom_effect_by_code(identifier);
  3932. if (!effect.is_null()) {
  3933. push_customfx(effect, properties);
  3934. pos = brk_end + 1;
  3935. tag_stack.push_front(identifier);
  3936. } else {
  3937. add_text("["); //ignore
  3938. pos = brk_pos + 1;
  3939. }
  3940. }
  3941. }
  3942. }
  3943. Vector<ItemFX *> fx_items;
  3944. for (Item *E : main->subitems) {
  3945. Item *subitem = static_cast<Item *>(E);
  3946. _fetch_item_fx_stack(subitem, fx_items);
  3947. if (fx_items.size()) {
  3948. set_process_internal(true);
  3949. break;
  3950. }
  3951. }
  3952. }
  3953. void RichTextLabel::scroll_to_selection() {
  3954. if (selection.active && selection.from_frame && selection.from_line >= 0 && selection.from_line < (int)selection.from_frame->lines.size()) {
  3955. // Selected frame paragraph offset.
  3956. float line_offset = selection.from_frame->lines[selection.from_line].offset.y;
  3957. // Add wrapped line offset.
  3958. for (int i = 0; i < selection.from_frame->lines[selection.from_line].text_buf->get_line_count(); i++) {
  3959. Vector2i range = selection.from_frame->lines[selection.from_line].text_buf->get_line_range(i);
  3960. if (range.x <= selection.from_char && range.y >= selection.from_char) {
  3961. break;
  3962. }
  3963. line_offset += selection.from_frame->lines[selection.from_line].text_buf->get_line_size(i).y + theme_cache.line_separation;
  3964. }
  3965. // Add nested frame (e.g. table cell) offset.
  3966. ItemFrame *it = selection.from_frame;
  3967. while (it->parent_frame != nullptr) {
  3968. line_offset += it->parent_frame->lines[it->line].offset.y;
  3969. it = it->parent_frame;
  3970. }
  3971. vscroll->set_value(line_offset);
  3972. }
  3973. }
  3974. void RichTextLabel::scroll_to_paragraph(int p_paragraph) {
  3975. _validate_line_caches();
  3976. if (p_paragraph <= 0) {
  3977. vscroll->set_value(0);
  3978. } else if (p_paragraph >= main->first_invalid_line.load()) {
  3979. vscroll->set_value(vscroll->get_max());
  3980. } else {
  3981. vscroll->set_value(main->lines[p_paragraph].offset.y);
  3982. }
  3983. }
  3984. int RichTextLabel::get_paragraph_count() const {
  3985. return current_frame->lines.size();
  3986. }
  3987. int RichTextLabel::get_visible_paragraph_count() const {
  3988. if (!is_visible()) {
  3989. return 0;
  3990. }
  3991. const_cast<RichTextLabel *>(this)->_validate_line_caches();
  3992. return visible_paragraph_count;
  3993. }
  3994. void RichTextLabel::scroll_to_line(int p_line) {
  3995. if (p_line <= 0) {
  3996. vscroll->set_value(0);
  3997. return;
  3998. }
  3999. _validate_line_caches();
  4000. int line_count = 0;
  4001. int to_line = main->first_invalid_line.load();
  4002. for (int i = 0; i < to_line; i++) {
  4003. MutexLock lock(main->lines[i].text_buf->get_mutex());
  4004. if ((line_count <= p_line) && (line_count + main->lines[i].text_buf->get_line_count() >= p_line)) {
  4005. float line_offset = 0.f;
  4006. for (int j = 0; j < p_line - line_count; j++) {
  4007. line_offset += main->lines[i].text_buf->get_line_size(j).y + theme_cache.line_separation;
  4008. }
  4009. vscroll->set_value(main->lines[i].offset.y + line_offset);
  4010. return;
  4011. }
  4012. line_count += main->lines[i].text_buf->get_line_count();
  4013. }
  4014. vscroll->set_value(vscroll->get_max());
  4015. }
  4016. float RichTextLabel::get_line_offset(int p_line) {
  4017. _validate_line_caches();
  4018. int line_count = 0;
  4019. int to_line = main->first_invalid_line.load();
  4020. for (int i = 0; i < to_line; i++) {
  4021. MutexLock lock(main->lines[i].text_buf->get_mutex());
  4022. if ((line_count <= p_line) && (p_line <= line_count + main->lines[i].text_buf->get_line_count())) {
  4023. float line_offset = 0.f;
  4024. for (int j = 0; j < p_line - line_count; j++) {
  4025. line_offset += main->lines[i].text_buf->get_line_size(j).y + theme_cache.line_separation;
  4026. }
  4027. return main->lines[i].offset.y + line_offset;
  4028. }
  4029. line_count += main->lines[i].text_buf->get_line_count();
  4030. }
  4031. return 0;
  4032. }
  4033. float RichTextLabel::get_paragraph_offset(int p_paragraph) {
  4034. _validate_line_caches();
  4035. int to_line = main->first_invalid_line.load();
  4036. if (0 <= p_paragraph && p_paragraph < to_line) {
  4037. return main->lines[p_paragraph].offset.y;
  4038. }
  4039. return 0;
  4040. }
  4041. int RichTextLabel::get_line_count() const {
  4042. const_cast<RichTextLabel *>(this)->_validate_line_caches();
  4043. int line_count = 0;
  4044. int to_line = main->first_invalid_line.load();
  4045. for (int i = 0; i < to_line; i++) {
  4046. MutexLock lock(main->lines[i].text_buf->get_mutex());
  4047. line_count += main->lines[i].text_buf->get_line_count();
  4048. }
  4049. return line_count;
  4050. }
  4051. int RichTextLabel::get_visible_line_count() const {
  4052. if (!is_visible()) {
  4053. return 0;
  4054. }
  4055. const_cast<RichTextLabel *>(this)->_validate_line_caches();
  4056. return visible_line_count;
  4057. }
  4058. void RichTextLabel::set_selection_enabled(bool p_enabled) {
  4059. if (selection.enabled == p_enabled) {
  4060. return;
  4061. }
  4062. selection.enabled = p_enabled;
  4063. if (!p_enabled) {
  4064. if (selection.active) {
  4065. deselect();
  4066. }
  4067. set_focus_mode(FOCUS_NONE);
  4068. } else {
  4069. set_focus_mode(FOCUS_ALL);
  4070. }
  4071. }
  4072. void RichTextLabel::set_deselect_on_focus_loss_enabled(const bool p_enabled) {
  4073. if (deselect_on_focus_loss_enabled == p_enabled) {
  4074. return;
  4075. }
  4076. deselect_on_focus_loss_enabled = p_enabled;
  4077. if (p_enabled && selection.active && !has_focus()) {
  4078. deselect();
  4079. }
  4080. }
  4081. Variant RichTextLabel::get_drag_data(const Point2 &p_point) {
  4082. if (selection.drag_attempt && selection.enabled) {
  4083. String t = get_selected_text();
  4084. Label *l = memnew(Label);
  4085. l->set_text(t);
  4086. set_drag_preview(l);
  4087. return t;
  4088. }
  4089. return Variant();
  4090. }
  4091. bool RichTextLabel::_is_click_inside_selection() const {
  4092. if (selection.active && selection.enabled && selection.click_frame && selection.from_frame && selection.to_frame) {
  4093. const Line &l_click = selection.click_frame->lines[selection.click_line];
  4094. const Line &l_from = selection.from_frame->lines[selection.from_line];
  4095. const Line &l_to = selection.to_frame->lines[selection.to_line];
  4096. return (l_click.char_offset + selection.click_char >= l_from.char_offset + selection.from_char) && (l_click.char_offset + selection.click_char <= l_to.char_offset + selection.to_char);
  4097. } else {
  4098. return false;
  4099. }
  4100. }
  4101. bool RichTextLabel::_search_table(ItemTable *p_table, List<Item *>::Element *p_from, const String &p_string, bool p_reverse_search) {
  4102. List<Item *>::Element *E = p_from;
  4103. while (E != nullptr) {
  4104. ERR_CONTINUE(E->get()->type != ITEM_FRAME); // Children should all be frames.
  4105. ItemFrame *frame = static_cast<ItemFrame *>(E->get());
  4106. if (p_reverse_search) {
  4107. for (int i = (int)frame->lines.size() - 1; i >= 0; i--) {
  4108. if (_search_line(frame, i, p_string, -1, p_reverse_search)) {
  4109. return true;
  4110. }
  4111. }
  4112. } else {
  4113. for (int i = 0; i < (int)frame->lines.size(); i++) {
  4114. if (_search_line(frame, i, p_string, 0, p_reverse_search)) {
  4115. return true;
  4116. }
  4117. }
  4118. }
  4119. E = p_reverse_search ? E->prev() : E->next();
  4120. }
  4121. return false;
  4122. }
  4123. bool RichTextLabel::_search_line(ItemFrame *p_frame, int p_line, const String &p_string, int p_char_idx, bool p_reverse_search) {
  4124. ERR_FAIL_COND_V(p_frame == nullptr, false);
  4125. ERR_FAIL_COND_V(p_line < 0 || p_line >= (int)p_frame->lines.size(), false);
  4126. Line &l = p_frame->lines[p_line];
  4127. String txt;
  4128. Item *it_to = (p_line + 1 < (int)p_frame->lines.size()) ? p_frame->lines[p_line + 1].from : nullptr;
  4129. for (Item *it = l.from; it && it != it_to; it = _get_next_item(it)) {
  4130. switch (it->type) {
  4131. case ITEM_NEWLINE: {
  4132. txt += "\n";
  4133. } break;
  4134. case ITEM_TEXT: {
  4135. ItemText *t = static_cast<ItemText *>(it);
  4136. txt += t->text;
  4137. } break;
  4138. case ITEM_IMAGE: {
  4139. txt += " ";
  4140. } break;
  4141. case ITEM_TABLE: {
  4142. ItemTable *table = static_cast<ItemTable *>(it);
  4143. List<Item *>::Element *E = p_reverse_search ? table->subitems.back() : table->subitems.front();
  4144. if (_search_table(table, E, p_string, p_reverse_search)) {
  4145. return true;
  4146. }
  4147. } break;
  4148. default:
  4149. break;
  4150. }
  4151. }
  4152. int sp = -1;
  4153. if (p_reverse_search) {
  4154. sp = txt.rfindn(p_string, p_char_idx);
  4155. } else {
  4156. sp = txt.findn(p_string, p_char_idx);
  4157. }
  4158. if (sp != -1) {
  4159. selection.from_frame = p_frame;
  4160. selection.from_line = p_line;
  4161. selection.from_item = _get_item_at_pos(l.from, it_to, sp);
  4162. selection.from_char = sp;
  4163. selection.to_frame = p_frame;
  4164. selection.to_line = p_line;
  4165. selection.to_item = _get_item_at_pos(l.from, it_to, sp + p_string.length());
  4166. selection.to_char = sp + p_string.length();
  4167. selection.active = true;
  4168. return true;
  4169. }
  4170. return false;
  4171. }
  4172. bool RichTextLabel::search(const String &p_string, bool p_from_selection, bool p_search_previous) {
  4173. ERR_FAIL_COND_V(!selection.enabled, false);
  4174. if (p_string.size() == 0) {
  4175. selection.active = false;
  4176. return false;
  4177. }
  4178. int char_idx = p_search_previous ? -1 : 0;
  4179. int current_line = 0;
  4180. int to_line = main->first_invalid_line.load();
  4181. int ending_line = to_line - 1;
  4182. if (p_from_selection && selection.active) {
  4183. // First check to see if other results exist in current line
  4184. char_idx = p_search_previous ? selection.from_char - 1 : selection.to_char;
  4185. if (!(p_search_previous && char_idx < 0) &&
  4186. _search_line(selection.from_frame, selection.from_line, p_string, char_idx, p_search_previous)) {
  4187. scroll_to_selection();
  4188. queue_redraw();
  4189. return true;
  4190. }
  4191. char_idx = p_search_previous ? -1 : 0;
  4192. // Next, check to see if the current search result is in a table
  4193. if (selection.from_frame->parent != nullptr && selection.from_frame->parent->type == ITEM_TABLE) {
  4194. // Find last search result in table
  4195. ItemTable *parent_table = static_cast<ItemTable *>(selection.from_frame->parent);
  4196. List<Item *>::Element *parent_element = p_search_previous ? parent_table->subitems.back() : parent_table->subitems.front();
  4197. while (parent_element->get() != selection.from_frame) {
  4198. parent_element = p_search_previous ? parent_element->prev() : parent_element->next();
  4199. ERR_FAIL_COND_V(parent_element == nullptr, false);
  4200. }
  4201. // Search remainder of table
  4202. if (!(p_search_previous && parent_element == parent_table->subitems.front()) &&
  4203. parent_element != parent_table->subitems.back()) {
  4204. parent_element = p_search_previous ? parent_element->prev() : parent_element->next(); // Don't want to search current item
  4205. ERR_FAIL_COND_V(parent_element == nullptr, false);
  4206. // Search for next element
  4207. if (_search_table(parent_table, parent_element, p_string, p_search_previous)) {
  4208. scroll_to_selection();
  4209. queue_redraw();
  4210. return true;
  4211. }
  4212. }
  4213. }
  4214. ending_line = selection.from_frame->line + selection.from_line;
  4215. current_line = p_search_previous ? ending_line - 1 : ending_line + 1;
  4216. } else if (p_search_previous) {
  4217. current_line = ending_line;
  4218. ending_line = 0;
  4219. }
  4220. // Search remainder of the file
  4221. while (current_line != ending_line) {
  4222. // Wrap around
  4223. if (current_line < 0) {
  4224. current_line = to_line - 1;
  4225. } else if (current_line >= to_line) {
  4226. current_line = 0;
  4227. }
  4228. if (_search_line(main, current_line, p_string, char_idx, p_search_previous)) {
  4229. scroll_to_selection();
  4230. queue_redraw();
  4231. return true;
  4232. }
  4233. if (current_line != ending_line) {
  4234. p_search_previous ? current_line-- : current_line++;
  4235. }
  4236. }
  4237. if (p_from_selection && selection.active) {
  4238. // Check contents of selection
  4239. return _search_line(main, current_line, p_string, char_idx, p_search_previous);
  4240. } else {
  4241. return false;
  4242. }
  4243. }
  4244. String RichTextLabel::_get_line_text(ItemFrame *p_frame, int p_line, Selection p_selection) const {
  4245. String txt;
  4246. ERR_FAIL_COND_V(p_frame == nullptr, txt);
  4247. ERR_FAIL_COND_V(p_line < 0 || p_line >= (int)p_frame->lines.size(), txt);
  4248. Line &l = p_frame->lines[p_line];
  4249. Item *it_to = (p_line + 1 < (int)p_frame->lines.size()) ? p_frame->lines[p_line + 1].from : nullptr;
  4250. int end_idx = 0;
  4251. if (it_to != nullptr) {
  4252. end_idx = it_to->index;
  4253. } else {
  4254. for (Item *it = l.from; it; it = _get_next_item(it)) {
  4255. end_idx = it->index + 1;
  4256. }
  4257. }
  4258. for (Item *it = l.from; it && it != it_to; it = _get_next_item(it)) {
  4259. if (it->type == ITEM_TABLE) {
  4260. ItemTable *table = static_cast<ItemTable *>(it);
  4261. for (Item *E : table->subitems) {
  4262. ERR_CONTINUE(E->type != ITEM_FRAME); // Children should all be frames.
  4263. ItemFrame *frame = static_cast<ItemFrame *>(E);
  4264. for (int i = 0; i < (int)frame->lines.size(); i++) {
  4265. txt += _get_line_text(frame, i, p_selection);
  4266. }
  4267. }
  4268. }
  4269. if ((p_selection.to_item != nullptr) && (p_selection.to_item->index < l.from->index)) {
  4270. continue;
  4271. }
  4272. if ((p_selection.from_item != nullptr) && (p_selection.from_item->index >= end_idx)) {
  4273. continue;
  4274. }
  4275. if (it->type == ITEM_DROPCAP) {
  4276. const ItemDropcap *dc = static_cast<ItemDropcap *>(it);
  4277. txt += dc->text;
  4278. } else if (it->type == ITEM_TEXT) {
  4279. const ItemText *t = static_cast<ItemText *>(it);
  4280. txt += t->text;
  4281. } else if (it->type == ITEM_NEWLINE) {
  4282. txt += "\n";
  4283. } else if (it->type == ITEM_IMAGE) {
  4284. txt += " ";
  4285. }
  4286. }
  4287. if ((l.from != nullptr) && (p_frame == p_selection.to_frame) && (p_selection.to_item != nullptr) && (p_selection.to_item->index >= l.from->index) && (p_selection.to_item->index < end_idx)) {
  4288. txt = txt.substr(0, p_selection.to_char);
  4289. }
  4290. if ((l.from != nullptr) && (p_frame == p_selection.from_frame) && (p_selection.from_item != nullptr) && (p_selection.from_item->index >= l.from->index) && (p_selection.from_item->index < end_idx)) {
  4291. txt = txt.substr(p_selection.from_char, -1);
  4292. }
  4293. return txt;
  4294. }
  4295. void RichTextLabel::set_context_menu_enabled(bool p_enabled) {
  4296. context_menu_enabled = p_enabled;
  4297. }
  4298. bool RichTextLabel::is_context_menu_enabled() const {
  4299. return context_menu_enabled;
  4300. }
  4301. void RichTextLabel::set_shortcut_keys_enabled(bool p_enabled) {
  4302. shortcut_keys_enabled = p_enabled;
  4303. }
  4304. bool RichTextLabel::is_shortcut_keys_enabled() const {
  4305. return shortcut_keys_enabled;
  4306. }
  4307. // Context menu.
  4308. PopupMenu *RichTextLabel::get_menu() const {
  4309. if (!menu) {
  4310. const_cast<RichTextLabel *>(this)->_generate_context_menu();
  4311. }
  4312. return menu;
  4313. }
  4314. bool RichTextLabel::is_menu_visible() const {
  4315. return menu && menu->is_visible();
  4316. }
  4317. String RichTextLabel::get_selected_text() const {
  4318. if (!selection.active || !selection.enabled) {
  4319. return "";
  4320. }
  4321. String txt;
  4322. int to_line = main->first_invalid_line.load();
  4323. for (int i = 0; i < to_line; i++) {
  4324. txt += _get_line_text(main, i, selection);
  4325. }
  4326. return txt;
  4327. }
  4328. void RichTextLabel::deselect() {
  4329. selection.active = false;
  4330. queue_redraw();
  4331. }
  4332. void RichTextLabel::selection_copy() {
  4333. String txt = get_selected_text();
  4334. if (!txt.is_empty()) {
  4335. DisplayServer::get_singleton()->clipboard_set(txt);
  4336. }
  4337. }
  4338. void RichTextLabel::select_all() {
  4339. if (!selection.enabled) {
  4340. return;
  4341. }
  4342. Item *it = main;
  4343. Item *from_item = nullptr;
  4344. Item *to_item = nullptr;
  4345. while (it) {
  4346. if (it->type != ITEM_FRAME) {
  4347. if (!from_item) {
  4348. from_item = it;
  4349. } else {
  4350. to_item = it;
  4351. }
  4352. }
  4353. it = _get_next_item(it, true);
  4354. }
  4355. if (!from_item || !to_item) {
  4356. return;
  4357. }
  4358. ItemFrame *from_frame = nullptr;
  4359. int from_line = 0;
  4360. _find_frame(from_item, &from_frame, &from_line);
  4361. if (!from_frame) {
  4362. return;
  4363. }
  4364. ItemFrame *to_frame = nullptr;
  4365. int to_line = 0;
  4366. _find_frame(to_item, &to_frame, &to_line);
  4367. if (!to_frame) {
  4368. return;
  4369. }
  4370. selection.from_line = from_line;
  4371. selection.from_frame = from_frame;
  4372. selection.from_char = 0;
  4373. selection.from_item = from_item;
  4374. selection.to_line = to_line;
  4375. selection.to_frame = to_frame;
  4376. selection.to_char = to_frame->lines[to_line].char_count;
  4377. selection.to_item = to_item;
  4378. selection.active = true;
  4379. queue_redraw();
  4380. }
  4381. bool RichTextLabel::is_selection_enabled() const {
  4382. return selection.enabled;
  4383. }
  4384. bool RichTextLabel::is_deselect_on_focus_loss_enabled() const {
  4385. return deselect_on_focus_loss_enabled;
  4386. }
  4387. int RichTextLabel::get_selection_from() const {
  4388. if (!selection.active || !selection.enabled) {
  4389. return -1;
  4390. }
  4391. return selection.from_frame->lines[selection.from_line].char_offset + selection.from_char;
  4392. }
  4393. int RichTextLabel::get_selection_to() const {
  4394. if (!selection.active || !selection.enabled) {
  4395. return -1;
  4396. }
  4397. return selection.to_frame->lines[selection.to_line].char_offset + selection.to_char - 1;
  4398. }
  4399. void RichTextLabel::set_text(const String &p_bbcode) {
  4400. if (text == p_bbcode) {
  4401. return;
  4402. }
  4403. text = p_bbcode;
  4404. if (use_bbcode) {
  4405. parse_bbcode(p_bbcode);
  4406. } else { // raw text
  4407. clear();
  4408. add_text(p_bbcode);
  4409. }
  4410. }
  4411. String RichTextLabel::get_text() const {
  4412. return text;
  4413. }
  4414. void RichTextLabel::set_use_bbcode(bool p_enable) {
  4415. if (use_bbcode == p_enable) {
  4416. return;
  4417. }
  4418. use_bbcode = p_enable;
  4419. notify_property_list_changed();
  4420. const String current_text = text;
  4421. if (use_bbcode) {
  4422. parse_bbcode(current_text);
  4423. } else { // raw text
  4424. clear();
  4425. add_text(current_text);
  4426. }
  4427. }
  4428. bool RichTextLabel::is_using_bbcode() const {
  4429. return use_bbcode;
  4430. }
  4431. String RichTextLabel::get_parsed_text() const {
  4432. String txt = "";
  4433. Item *it = main;
  4434. while (it) {
  4435. if (it->type == ITEM_DROPCAP) {
  4436. ItemDropcap *dc = static_cast<ItemDropcap *>(it);
  4437. txt += dc->text;
  4438. } else if (it->type == ITEM_TEXT) {
  4439. ItemText *t = static_cast<ItemText *>(it);
  4440. txt += t->text;
  4441. } else if (it->type == ITEM_NEWLINE) {
  4442. txt += "\n";
  4443. } else if (it->type == ITEM_IMAGE) {
  4444. txt += " ";
  4445. } else if (it->type == ITEM_INDENT || it->type == ITEM_LIST) {
  4446. txt += "\t";
  4447. }
  4448. it = _get_next_item(it, true);
  4449. }
  4450. return txt;
  4451. }
  4452. void RichTextLabel::set_text_direction(Control::TextDirection p_text_direction) {
  4453. ERR_FAIL_COND((int)p_text_direction < -1 || (int)p_text_direction > 3);
  4454. _stop_thread();
  4455. if (text_direction != p_text_direction) {
  4456. text_direction = p_text_direction;
  4457. main->first_invalid_line.store(0); //invalidate ALL
  4458. _validate_line_caches();
  4459. queue_redraw();
  4460. }
  4461. }
  4462. void RichTextLabel::set_structured_text_bidi_override(TextServer::StructuredTextParser p_parser) {
  4463. if (st_parser != p_parser) {
  4464. _stop_thread();
  4465. st_parser = p_parser;
  4466. main->first_invalid_line.store(0); //invalidate ALL
  4467. _validate_line_caches();
  4468. queue_redraw();
  4469. }
  4470. }
  4471. TextServer::StructuredTextParser RichTextLabel::get_structured_text_bidi_override() const {
  4472. return st_parser;
  4473. }
  4474. void RichTextLabel::set_structured_text_bidi_override_options(Array p_args) {
  4475. if (st_args != p_args) {
  4476. _stop_thread();
  4477. st_args = p_args;
  4478. main->first_invalid_line.store(0); //invalidate ALL
  4479. _validate_line_caches();
  4480. queue_redraw();
  4481. }
  4482. }
  4483. Array RichTextLabel::get_structured_text_bidi_override_options() const {
  4484. return st_args;
  4485. }
  4486. Control::TextDirection RichTextLabel::get_text_direction() const {
  4487. return text_direction;
  4488. }
  4489. void RichTextLabel::set_language(const String &p_language) {
  4490. if (language != p_language) {
  4491. _stop_thread();
  4492. language = p_language;
  4493. main->first_invalid_line.store(0); //invalidate ALL
  4494. _validate_line_caches();
  4495. queue_redraw();
  4496. }
  4497. }
  4498. String RichTextLabel::get_language() const {
  4499. return language;
  4500. }
  4501. void RichTextLabel::set_autowrap_mode(TextServer::AutowrapMode p_mode) {
  4502. if (autowrap_mode != p_mode) {
  4503. _stop_thread();
  4504. autowrap_mode = p_mode;
  4505. main->first_invalid_line = 0; //invalidate ALL
  4506. _validate_line_caches();
  4507. queue_redraw();
  4508. }
  4509. }
  4510. TextServer::AutowrapMode RichTextLabel::get_autowrap_mode() const {
  4511. return autowrap_mode;
  4512. }
  4513. void RichTextLabel::set_visible_ratio(float p_ratio) {
  4514. if (visible_ratio != p_ratio) {
  4515. _stop_thread();
  4516. if (p_ratio >= 1.0) {
  4517. visible_characters = -1;
  4518. visible_ratio = 1.0;
  4519. } else if (p_ratio < 0.0) {
  4520. visible_characters = 0;
  4521. visible_ratio = 0.0;
  4522. } else {
  4523. visible_characters = get_total_character_count() * p_ratio;
  4524. visible_ratio = p_ratio;
  4525. }
  4526. if (visible_chars_behavior == TextServer::VC_CHARS_BEFORE_SHAPING) {
  4527. main->first_invalid_line.store(0); // Invalidate ALL.
  4528. _validate_line_caches();
  4529. }
  4530. queue_redraw();
  4531. }
  4532. }
  4533. float RichTextLabel::get_visible_ratio() const {
  4534. return visible_ratio;
  4535. }
  4536. void RichTextLabel::set_effects(Array p_effects) {
  4537. custom_effects = p_effects;
  4538. if ((!text.is_empty()) && use_bbcode) {
  4539. parse_bbcode(text);
  4540. }
  4541. }
  4542. Array RichTextLabel::get_effects() {
  4543. return custom_effects;
  4544. }
  4545. void RichTextLabel::install_effect(const Variant effect) {
  4546. Ref<RichTextEffect> rteffect;
  4547. rteffect = effect;
  4548. ERR_FAIL_COND_MSG(rteffect.is_null(), "Invalid RichTextEffect resource.");
  4549. custom_effects.push_back(effect);
  4550. if ((!text.is_empty()) && use_bbcode) {
  4551. parse_bbcode(text);
  4552. }
  4553. }
  4554. int RichTextLabel::get_content_height() const {
  4555. const_cast<RichTextLabel *>(this)->_validate_line_caches();
  4556. int total_height = 0;
  4557. int to_line = main->first_invalid_line.load();
  4558. if (to_line) {
  4559. MutexLock lock(main->lines[to_line - 1].text_buf->get_mutex());
  4560. if (theme_cache.line_separation < 0) {
  4561. // Do not apply to the last line to avoid cutting text.
  4562. total_height = main->lines[to_line - 1].offset.y + main->lines[to_line - 1].text_buf->get_size().y + (main->lines[to_line - 1].text_buf->get_line_count() - 1) * theme_cache.line_separation;
  4563. } else {
  4564. total_height = main->lines[to_line - 1].offset.y + main->lines[to_line - 1].text_buf->get_size().y + main->lines[to_line - 1].text_buf->get_line_count() * theme_cache.line_separation;
  4565. }
  4566. }
  4567. return total_height;
  4568. }
  4569. int RichTextLabel::get_content_width() const {
  4570. const_cast<RichTextLabel *>(this)->_validate_line_caches();
  4571. int total_width = 0;
  4572. int to_line = main->first_invalid_line.load();
  4573. for (int i = 0; i < to_line; i++) {
  4574. MutexLock lock(main->lines[i].text_buf->get_mutex());
  4575. total_width = MAX(total_width, main->lines[i].offset.x + main->lines[i].text_buf->get_size().x);
  4576. }
  4577. return total_width;
  4578. }
  4579. #ifndef DISABLE_DEPRECATED
  4580. // People will be very angry, if their texts get erased, because of #39148. (3.x -> 4.0)
  4581. // Although some people may not used bbcode_text, so we only overwrite, if bbcode_text is not empty.
  4582. bool RichTextLabel::_set(const StringName &p_name, const Variant &p_value) {
  4583. if (p_name == "bbcode_text" && !((String)p_value).is_empty()) {
  4584. set_text(p_value);
  4585. return true;
  4586. }
  4587. return false;
  4588. }
  4589. #endif
  4590. void RichTextLabel::_bind_methods() {
  4591. ClassDB::bind_method(D_METHOD("get_parsed_text"), &RichTextLabel::get_parsed_text);
  4592. ClassDB::bind_method(D_METHOD("add_text", "text"), &RichTextLabel::add_text);
  4593. ClassDB::bind_method(D_METHOD("set_text", "text"), &RichTextLabel::set_text);
  4594. ClassDB::bind_method(D_METHOD("add_image", "image", "width", "height", "color", "inline_align", "region"), &RichTextLabel::add_image, DEFVAL(0), DEFVAL(0), DEFVAL(Color(1.0, 1.0, 1.0)), DEFVAL(INLINE_ALIGNMENT_CENTER), DEFVAL(Rect2(0, 0, 0, 0)));
  4595. ClassDB::bind_method(D_METHOD("newline"), &RichTextLabel::add_newline);
  4596. ClassDB::bind_method(D_METHOD("remove_paragraph", "paragraph"), &RichTextLabel::remove_paragraph);
  4597. ClassDB::bind_method(D_METHOD("push_font", "font", "font_size"), &RichTextLabel::push_font);
  4598. ClassDB::bind_method(D_METHOD("push_font_size", "font_size"), &RichTextLabel::push_font_size);
  4599. ClassDB::bind_method(D_METHOD("push_normal"), &RichTextLabel::push_normal);
  4600. ClassDB::bind_method(D_METHOD("push_bold"), &RichTextLabel::push_bold);
  4601. ClassDB::bind_method(D_METHOD("push_bold_italics"), &RichTextLabel::push_bold_italics);
  4602. ClassDB::bind_method(D_METHOD("push_italics"), &RichTextLabel::push_italics);
  4603. ClassDB::bind_method(D_METHOD("push_mono"), &RichTextLabel::push_mono);
  4604. ClassDB::bind_method(D_METHOD("push_color", "color"), &RichTextLabel::push_color);
  4605. ClassDB::bind_method(D_METHOD("push_outline_size", "outline_size"), &RichTextLabel::push_outline_size);
  4606. ClassDB::bind_method(D_METHOD("push_outline_color", "color"), &RichTextLabel::push_outline_color);
  4607. ClassDB::bind_method(D_METHOD("push_paragraph", "alignment", "base_direction", "language", "st_parser"), &RichTextLabel::push_paragraph, DEFVAL(TextServer::DIRECTION_AUTO), DEFVAL(""), DEFVAL(TextServer::STRUCTURED_TEXT_DEFAULT));
  4608. ClassDB::bind_method(D_METHOD("push_indent", "level"), &RichTextLabel::push_indent);
  4609. ClassDB::bind_method(D_METHOD("push_list", "level", "type", "capitalize"), &RichTextLabel::push_list);
  4610. ClassDB::bind_method(D_METHOD("push_meta", "data"), &RichTextLabel::push_meta);
  4611. ClassDB::bind_method(D_METHOD("push_hint", "description"), &RichTextLabel::push_hint);
  4612. ClassDB::bind_method(D_METHOD("push_underline"), &RichTextLabel::push_underline);
  4613. ClassDB::bind_method(D_METHOD("push_strikethrough"), &RichTextLabel::push_strikethrough);
  4614. ClassDB::bind_method(D_METHOD("push_table", "columns", "inline_align", "align_to_row"), &RichTextLabel::push_table, DEFVAL(INLINE_ALIGNMENT_TOP), DEFVAL(-1));
  4615. ClassDB::bind_method(D_METHOD("push_dropcap", "string", "font", "size", "dropcap_margins", "color", "outline_size", "outline_color"), &RichTextLabel::push_dropcap, DEFVAL(Rect2()), DEFVAL(Color(1, 1, 1)), DEFVAL(0), DEFVAL(Color(0, 0, 0, 0)));
  4616. ClassDB::bind_method(D_METHOD("set_table_column_expand", "column", "expand", "ratio"), &RichTextLabel::set_table_column_expand);
  4617. ClassDB::bind_method(D_METHOD("set_cell_row_background_color", "odd_row_bg", "even_row_bg"), &RichTextLabel::set_cell_row_background_color);
  4618. ClassDB::bind_method(D_METHOD("set_cell_border_color", "color"), &RichTextLabel::set_cell_border_color);
  4619. ClassDB::bind_method(D_METHOD("set_cell_size_override", "min_size", "max_size"), &RichTextLabel::set_cell_size_override);
  4620. ClassDB::bind_method(D_METHOD("set_cell_padding", "padding"), &RichTextLabel::set_cell_padding);
  4621. ClassDB::bind_method(D_METHOD("push_cell"), &RichTextLabel::push_cell);
  4622. ClassDB::bind_method(D_METHOD("push_fgcolor", "fgcolor"), &RichTextLabel::push_fgcolor);
  4623. ClassDB::bind_method(D_METHOD("push_bgcolor", "bgcolor"), &RichTextLabel::push_bgcolor);
  4624. ClassDB::bind_method(D_METHOD("push_customfx", "effect", "env"), &RichTextLabel::push_customfx);
  4625. ClassDB::bind_method(D_METHOD("pop"), &RichTextLabel::pop);
  4626. ClassDB::bind_method(D_METHOD("clear"), &RichTextLabel::clear);
  4627. ClassDB::bind_method(D_METHOD("set_structured_text_bidi_override", "parser"), &RichTextLabel::set_structured_text_bidi_override);
  4628. ClassDB::bind_method(D_METHOD("get_structured_text_bidi_override"), &RichTextLabel::get_structured_text_bidi_override);
  4629. ClassDB::bind_method(D_METHOD("set_structured_text_bidi_override_options", "args"), &RichTextLabel::set_structured_text_bidi_override_options);
  4630. ClassDB::bind_method(D_METHOD("get_structured_text_bidi_override_options"), &RichTextLabel::get_structured_text_bidi_override_options);
  4631. ClassDB::bind_method(D_METHOD("set_text_direction", "direction"), &RichTextLabel::set_text_direction);
  4632. ClassDB::bind_method(D_METHOD("get_text_direction"), &RichTextLabel::get_text_direction);
  4633. ClassDB::bind_method(D_METHOD("set_language", "language"), &RichTextLabel::set_language);
  4634. ClassDB::bind_method(D_METHOD("get_language"), &RichTextLabel::get_language);
  4635. ClassDB::bind_method(D_METHOD("set_autowrap_mode", "autowrap_mode"), &RichTextLabel::set_autowrap_mode);
  4636. ClassDB::bind_method(D_METHOD("get_autowrap_mode"), &RichTextLabel::get_autowrap_mode);
  4637. ClassDB::bind_method(D_METHOD("set_meta_underline", "enable"), &RichTextLabel::set_meta_underline);
  4638. ClassDB::bind_method(D_METHOD("is_meta_underlined"), &RichTextLabel::is_meta_underlined);
  4639. ClassDB::bind_method(D_METHOD("set_hint_underline", "enable"), &RichTextLabel::set_hint_underline);
  4640. ClassDB::bind_method(D_METHOD("is_hint_underlined"), &RichTextLabel::is_hint_underlined);
  4641. ClassDB::bind_method(D_METHOD("set_scroll_active", "active"), &RichTextLabel::set_scroll_active);
  4642. ClassDB::bind_method(D_METHOD("is_scroll_active"), &RichTextLabel::is_scroll_active);
  4643. ClassDB::bind_method(D_METHOD("set_scroll_follow", "follow"), &RichTextLabel::set_scroll_follow);
  4644. ClassDB::bind_method(D_METHOD("is_scroll_following"), &RichTextLabel::is_scroll_following);
  4645. ClassDB::bind_method(D_METHOD("get_v_scroll_bar"), &RichTextLabel::get_v_scroll_bar);
  4646. ClassDB::bind_method(D_METHOD("scroll_to_line", "line"), &RichTextLabel::scroll_to_line);
  4647. ClassDB::bind_method(D_METHOD("scroll_to_paragraph", "paragraph"), &RichTextLabel::scroll_to_paragraph);
  4648. ClassDB::bind_method(D_METHOD("scroll_to_selection"), &RichTextLabel::scroll_to_selection);
  4649. ClassDB::bind_method(D_METHOD("set_tab_size", "spaces"), &RichTextLabel::set_tab_size);
  4650. ClassDB::bind_method(D_METHOD("get_tab_size"), &RichTextLabel::get_tab_size);
  4651. ClassDB::bind_method(D_METHOD("set_fit_content", "enabled"), &RichTextLabel::set_fit_content);
  4652. ClassDB::bind_method(D_METHOD("is_fit_content_enabled"), &RichTextLabel::is_fit_content_enabled);
  4653. ClassDB::bind_method(D_METHOD("set_selection_enabled", "enabled"), &RichTextLabel::set_selection_enabled);
  4654. ClassDB::bind_method(D_METHOD("is_selection_enabled"), &RichTextLabel::is_selection_enabled);
  4655. ClassDB::bind_method(D_METHOD("set_context_menu_enabled", "enabled"), &RichTextLabel::set_context_menu_enabled);
  4656. ClassDB::bind_method(D_METHOD("is_context_menu_enabled"), &RichTextLabel::is_context_menu_enabled);
  4657. ClassDB::bind_method(D_METHOD("set_shortcut_keys_enabled", "enabled"), &RichTextLabel::set_shortcut_keys_enabled);
  4658. ClassDB::bind_method(D_METHOD("is_shortcut_keys_enabled"), &RichTextLabel::is_shortcut_keys_enabled);
  4659. ClassDB::bind_method(D_METHOD("set_deselect_on_focus_loss_enabled", "enable"), &RichTextLabel::set_deselect_on_focus_loss_enabled);
  4660. ClassDB::bind_method(D_METHOD("is_deselect_on_focus_loss_enabled"), &RichTextLabel::is_deselect_on_focus_loss_enabled);
  4661. ClassDB::bind_method(D_METHOD("get_selection_from"), &RichTextLabel::get_selection_from);
  4662. ClassDB::bind_method(D_METHOD("get_selection_to"), &RichTextLabel::get_selection_to);
  4663. ClassDB::bind_method(D_METHOD("select_all"), &RichTextLabel::select_all);
  4664. ClassDB::bind_method(D_METHOD("get_selected_text"), &RichTextLabel::get_selected_text);
  4665. ClassDB::bind_method(D_METHOD("deselect"), &RichTextLabel::deselect);
  4666. ClassDB::bind_method(D_METHOD("parse_bbcode", "bbcode"), &RichTextLabel::parse_bbcode);
  4667. ClassDB::bind_method(D_METHOD("append_text", "bbcode"), &RichTextLabel::append_text);
  4668. ClassDB::bind_method(D_METHOD("get_text"), &RichTextLabel::get_text);
  4669. ClassDB::bind_method(D_METHOD("is_ready"), &RichTextLabel::is_ready);
  4670. ClassDB::bind_method(D_METHOD("set_threaded", "threaded"), &RichTextLabel::set_threaded);
  4671. ClassDB::bind_method(D_METHOD("is_threaded"), &RichTextLabel::is_threaded);
  4672. ClassDB::bind_method(D_METHOD("set_progress_bar_delay", "delay_ms"), &RichTextLabel::set_progress_bar_delay);
  4673. ClassDB::bind_method(D_METHOD("get_progress_bar_delay"), &RichTextLabel::get_progress_bar_delay);
  4674. ClassDB::bind_method(D_METHOD("set_visible_characters", "amount"), &RichTextLabel::set_visible_characters);
  4675. ClassDB::bind_method(D_METHOD("get_visible_characters"), &RichTextLabel::get_visible_characters);
  4676. ClassDB::bind_method(D_METHOD("get_visible_characters_behavior"), &RichTextLabel::get_visible_characters_behavior);
  4677. ClassDB::bind_method(D_METHOD("set_visible_characters_behavior", "behavior"), &RichTextLabel::set_visible_characters_behavior);
  4678. ClassDB::bind_method(D_METHOD("set_visible_ratio", "ratio"), &RichTextLabel::set_visible_ratio);
  4679. ClassDB::bind_method(D_METHOD("get_visible_ratio"), &RichTextLabel::get_visible_ratio);
  4680. ClassDB::bind_method(D_METHOD("get_character_line", "character"), &RichTextLabel::get_character_line);
  4681. ClassDB::bind_method(D_METHOD("get_character_paragraph", "character"), &RichTextLabel::get_character_paragraph);
  4682. ClassDB::bind_method(D_METHOD("get_total_character_count"), &RichTextLabel::get_total_character_count);
  4683. ClassDB::bind_method(D_METHOD("set_use_bbcode", "enable"), &RichTextLabel::set_use_bbcode);
  4684. ClassDB::bind_method(D_METHOD("is_using_bbcode"), &RichTextLabel::is_using_bbcode);
  4685. ClassDB::bind_method(D_METHOD("get_line_count"), &RichTextLabel::get_line_count);
  4686. ClassDB::bind_method(D_METHOD("get_visible_line_count"), &RichTextLabel::get_visible_line_count);
  4687. ClassDB::bind_method(D_METHOD("get_paragraph_count"), &RichTextLabel::get_paragraph_count);
  4688. ClassDB::bind_method(D_METHOD("get_visible_paragraph_count"), &RichTextLabel::get_visible_paragraph_count);
  4689. ClassDB::bind_method(D_METHOD("get_content_height"), &RichTextLabel::get_content_height);
  4690. ClassDB::bind_method(D_METHOD("get_content_width"), &RichTextLabel::get_content_width);
  4691. ClassDB::bind_method(D_METHOD("get_line_offset", "line"), &RichTextLabel::get_line_offset);
  4692. ClassDB::bind_method(D_METHOD("get_paragraph_offset", "paragraph"), &RichTextLabel::get_paragraph_offset);
  4693. ClassDB::bind_method(D_METHOD("parse_expressions_for_values", "expressions"), &RichTextLabel::parse_expressions_for_values);
  4694. ClassDB::bind_method(D_METHOD("set_effects", "effects"), &RichTextLabel::set_effects);
  4695. ClassDB::bind_method(D_METHOD("get_effects"), &RichTextLabel::get_effects);
  4696. ClassDB::bind_method(D_METHOD("install_effect", "effect"), &RichTextLabel::install_effect);
  4697. ClassDB::bind_method(D_METHOD("get_menu"), &RichTextLabel::get_menu);
  4698. ClassDB::bind_method(D_METHOD("is_menu_visible"), &RichTextLabel::is_menu_visible);
  4699. ClassDB::bind_method(D_METHOD("menu_option", "option"), &RichTextLabel::menu_option);
  4700. ClassDB::bind_method(D_METHOD("_thread_end"), &RichTextLabel::_thread_end);
  4701. // Note: set "bbcode_enabled" first, to avoid unnecessary "text" resets.
  4702. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "bbcode_enabled"), "set_use_bbcode", "is_using_bbcode");
  4703. ADD_PROPERTY(PropertyInfo(Variant::STRING, "text", PROPERTY_HINT_MULTILINE_TEXT), "set_text", "get_text");
  4704. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "fit_content"), "set_fit_content", "is_fit_content_enabled");
  4705. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_active"), "set_scroll_active", "is_scroll_active");
  4706. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_following"), "set_scroll_follow", "is_scroll_following");
  4707. ADD_PROPERTY(PropertyInfo(Variant::INT, "autowrap_mode", PROPERTY_HINT_ENUM, "Off,Arbitrary,Word,Word (Smart)"), "set_autowrap_mode", "get_autowrap_mode");
  4708. ADD_PROPERTY(PropertyInfo(Variant::INT, "tab_size", PROPERTY_HINT_RANGE, "0,24,1"), "set_tab_size", "get_tab_size");
  4709. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "context_menu_enabled"), "set_context_menu_enabled", "is_context_menu_enabled");
  4710. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shortcut_keys_enabled"), "set_shortcut_keys_enabled", "is_shortcut_keys_enabled");
  4711. ADD_GROUP("Markup", "");
  4712. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "custom_effects", PROPERTY_HINT_ARRAY_TYPE, MAKE_RESOURCE_TYPE_HINT("RichTextEffect"), (PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE)), "set_effects", "get_effects");
  4713. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "meta_underlined"), "set_meta_underline", "is_meta_underlined");
  4714. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hint_underlined"), "set_hint_underline", "is_hint_underlined");
  4715. ADD_GROUP("Threading", "");
  4716. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "threaded"), "set_threaded", "is_threaded");
  4717. ADD_PROPERTY(PropertyInfo(Variant::INT, "progress_bar_delay", PROPERTY_HINT_NONE, "suffix:ms"), "set_progress_bar_delay", "get_progress_bar_delay");
  4718. ADD_GROUP("Text Selection", "");
  4719. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "selection_enabled"), "set_selection_enabled", "is_selection_enabled");
  4720. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "deselect_on_focus_loss_enabled"), "set_deselect_on_focus_loss_enabled", "is_deselect_on_focus_loss_enabled");
  4721. ADD_GROUP("Displayed Text", "");
  4722. // Note: "visible_characters" and "visible_ratio" should be set after "text" to be correctly applied.
  4723. ADD_PROPERTY(PropertyInfo(Variant::INT, "visible_characters", PROPERTY_HINT_RANGE, "-1,128000,1"), "set_visible_characters", "get_visible_characters");
  4724. ADD_PROPERTY(PropertyInfo(Variant::INT, "visible_characters_behavior", PROPERTY_HINT_ENUM, "Characters Before Shaping,Characters After Shaping,Glyphs (Layout Direction),Glyphs (Left-to-Right),Glyphs (Right-to-Left)"), "set_visible_characters_behavior", "get_visible_characters_behavior");
  4725. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "visible_ratio", PROPERTY_HINT_RANGE, "0,1,0.001"), "set_visible_ratio", "get_visible_ratio");
  4726. ADD_GROUP("BiDi", "");
  4727. ADD_PROPERTY(PropertyInfo(Variant::INT, "text_direction", PROPERTY_HINT_ENUM, "Auto,Left-to-Right,Right-to-Left,Inherited"), "set_text_direction", "get_text_direction");
  4728. ADD_PROPERTY(PropertyInfo(Variant::STRING, "language", PROPERTY_HINT_LOCALE_ID, ""), "set_language", "get_language");
  4729. ADD_PROPERTY(PropertyInfo(Variant::INT, "structured_text_bidi_override", PROPERTY_HINT_ENUM, "Default,URI,File,Email,List,None,Custom"), "set_structured_text_bidi_override", "get_structured_text_bidi_override");
  4730. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "structured_text_bidi_override_options"), "set_structured_text_bidi_override_options", "get_structured_text_bidi_override_options");
  4731. ADD_SIGNAL(MethodInfo("meta_clicked", PropertyInfo(Variant::NIL, "meta", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NIL_IS_VARIANT)));
  4732. ADD_SIGNAL(MethodInfo("meta_hover_started", PropertyInfo(Variant::NIL, "meta", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NIL_IS_VARIANT)));
  4733. ADD_SIGNAL(MethodInfo("meta_hover_ended", PropertyInfo(Variant::NIL, "meta", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NIL_IS_VARIANT)));
  4734. ADD_SIGNAL(MethodInfo("finished"));
  4735. BIND_ENUM_CONSTANT(LIST_NUMBERS);
  4736. BIND_ENUM_CONSTANT(LIST_LETTERS);
  4737. BIND_ENUM_CONSTANT(LIST_ROMAN);
  4738. BIND_ENUM_CONSTANT(LIST_DOTS);
  4739. BIND_ENUM_CONSTANT(MENU_COPY);
  4740. BIND_ENUM_CONSTANT(MENU_SELECT_ALL);
  4741. BIND_ENUM_CONSTANT(MENU_MAX);
  4742. }
  4743. TextServer::VisibleCharactersBehavior RichTextLabel::get_visible_characters_behavior() const {
  4744. return visible_chars_behavior;
  4745. }
  4746. void RichTextLabel::set_visible_characters_behavior(TextServer::VisibleCharactersBehavior p_behavior) {
  4747. if (visible_chars_behavior != p_behavior) {
  4748. _stop_thread();
  4749. visible_chars_behavior = p_behavior;
  4750. main->first_invalid_line.store(0); //invalidate ALL
  4751. _validate_line_caches();
  4752. queue_redraw();
  4753. }
  4754. }
  4755. void RichTextLabel::set_visible_characters(int p_visible) {
  4756. if (visible_characters != p_visible) {
  4757. _stop_thread();
  4758. visible_characters = p_visible;
  4759. if (p_visible == -1) {
  4760. visible_ratio = 1;
  4761. } else {
  4762. int total_char_count = get_total_character_count();
  4763. if (total_char_count > 0) {
  4764. visible_ratio = (float)p_visible / (float)total_char_count;
  4765. }
  4766. }
  4767. if (visible_chars_behavior == TextServer::VC_CHARS_BEFORE_SHAPING) {
  4768. main->first_invalid_line.store(0); //invalidate ALL
  4769. _validate_line_caches();
  4770. }
  4771. queue_redraw();
  4772. }
  4773. }
  4774. int RichTextLabel::get_visible_characters() const {
  4775. return visible_characters;
  4776. }
  4777. int RichTextLabel::get_character_line(int p_char) {
  4778. _validate_line_caches();
  4779. int line_count = 0;
  4780. int to_line = main->first_invalid_line.load();
  4781. for (int i = 0; i < to_line; i++) {
  4782. MutexLock lock(main->lines[i].text_buf->get_mutex());
  4783. if (main->lines[i].char_offset < p_char && p_char <= main->lines[i].char_offset + main->lines[i].char_count) {
  4784. for (int j = 0; j < main->lines[i].text_buf->get_line_count(); j++) {
  4785. Vector2i range = main->lines[i].text_buf->get_line_range(j);
  4786. if (main->lines[i].char_offset + range.x < p_char && p_char <= main->lines[i].char_offset + range.y) {
  4787. return line_count;
  4788. }
  4789. line_count++;
  4790. }
  4791. } else {
  4792. line_count += main->lines[i].text_buf->get_line_count();
  4793. }
  4794. }
  4795. return -1;
  4796. }
  4797. int RichTextLabel::get_character_paragraph(int p_char) {
  4798. _validate_line_caches();
  4799. int para_count = 0;
  4800. int to_line = main->first_invalid_line.load();
  4801. for (int i = 0; i < to_line; i++) {
  4802. if (main->lines[i].char_offset < p_char && p_char <= main->lines[i].char_offset + main->lines[i].char_count) {
  4803. return para_count;
  4804. } else {
  4805. para_count++;
  4806. }
  4807. }
  4808. return -1;
  4809. }
  4810. int RichTextLabel::get_total_character_count() const {
  4811. // Note: Do not use line buffer "char_count", it includes only visible characters.
  4812. int tc = 0;
  4813. Item *it = main;
  4814. while (it) {
  4815. if (it->type == ITEM_TEXT) {
  4816. ItemText *t = static_cast<ItemText *>(it);
  4817. tc += t->text.length();
  4818. } else if (it->type == ITEM_NEWLINE) {
  4819. tc++;
  4820. } else if (it->type == ITEM_IMAGE) {
  4821. tc++;
  4822. }
  4823. it = _get_next_item(it, true);
  4824. }
  4825. return tc;
  4826. }
  4827. int RichTextLabel::get_total_glyph_count() const {
  4828. const_cast<RichTextLabel *>(this)->_validate_line_caches();
  4829. int tg = 0;
  4830. Item *it = main;
  4831. while (it) {
  4832. if (it->type == ITEM_FRAME) {
  4833. ItemFrame *f = static_cast<ItemFrame *>(it);
  4834. for (int i = 0; i < (int)f->lines.size(); i++) {
  4835. MutexLock lock(f->lines[i].text_buf->get_mutex());
  4836. tg += TS->shaped_text_get_glyph_count(f->lines[i].text_buf->get_rid());
  4837. }
  4838. }
  4839. it = _get_next_item(it, true);
  4840. }
  4841. return tg;
  4842. }
  4843. Size2 RichTextLabel::get_minimum_size() const {
  4844. Size2 sb_min_size = theme_cache.normal_style->get_minimum_size();
  4845. Size2 min_size;
  4846. if (fit_content) {
  4847. min_size.x = get_content_width();
  4848. min_size.y = get_content_height();
  4849. }
  4850. return sb_min_size +
  4851. ((autowrap_mode != TextServer::AUTOWRAP_OFF) ? Size2(1, min_size.height) : min_size);
  4852. }
  4853. // Context menu.
  4854. void RichTextLabel::_generate_context_menu() {
  4855. menu = memnew(PopupMenu);
  4856. add_child(menu, false, INTERNAL_MODE_FRONT);
  4857. menu->connect("id_pressed", callable_mp(this, &RichTextLabel::menu_option));
  4858. menu->add_item(RTR("Copy"), MENU_COPY);
  4859. menu->add_item(RTR("Select All"), MENU_SELECT_ALL);
  4860. }
  4861. void RichTextLabel::_update_context_menu() {
  4862. if (!menu) {
  4863. _generate_context_menu();
  4864. }
  4865. int idx = -1;
  4866. #define MENU_ITEM_ACTION_DISABLED(m_menu, m_id, m_action, m_disabled) \
  4867. idx = m_menu->get_item_index(m_id); \
  4868. if (idx >= 0) { \
  4869. m_menu->set_item_accelerator(idx, shortcut_keys_enabled ? _get_menu_action_accelerator(m_action) : Key::NONE); \
  4870. m_menu->set_item_disabled(idx, m_disabled); \
  4871. }
  4872. MENU_ITEM_ACTION_DISABLED(menu, MENU_COPY, "ui_copy", !selection.enabled)
  4873. MENU_ITEM_ACTION_DISABLED(menu, MENU_SELECT_ALL, "ui_text_select_all", !selection.enabled)
  4874. #undef MENU_ITEM_ACTION_DISABLED
  4875. }
  4876. Key RichTextLabel::_get_menu_action_accelerator(const String &p_action) {
  4877. const List<Ref<InputEvent>> *events = InputMap::get_singleton()->action_get_events(p_action);
  4878. if (!events) {
  4879. return Key::NONE;
  4880. }
  4881. // Use first event in the list for the accelerator.
  4882. const List<Ref<InputEvent>>::Element *first_event = events->front();
  4883. if (!first_event) {
  4884. return Key::NONE;
  4885. }
  4886. const Ref<InputEventKey> event = first_event->get();
  4887. if (event.is_null()) {
  4888. return Key::NONE;
  4889. }
  4890. // Use physical keycode if non-zero
  4891. if (event->get_physical_keycode() != Key::NONE) {
  4892. return event->get_physical_keycode_with_modifiers();
  4893. } else {
  4894. return event->get_keycode_with_modifiers();
  4895. }
  4896. }
  4897. void RichTextLabel::menu_option(int p_option) {
  4898. switch (p_option) {
  4899. case MENU_COPY: {
  4900. selection_copy();
  4901. } break;
  4902. case MENU_SELECT_ALL: {
  4903. select_all();
  4904. } break;
  4905. }
  4906. }
  4907. void RichTextLabel::_draw_fbg_boxes(RID p_ci, RID p_rid, Vector2 line_off, Item *it_from, Item *it_to, int start, int end, int fbg_flag) {
  4908. Vector2i fbg_index = Vector2i(end, start);
  4909. Color last_color = Color(0, 0, 0, 0);
  4910. bool draw_box = false;
  4911. int hpad = get_theme_constant(SNAME("text_highlight_h_padding"));
  4912. int vpad = get_theme_constant(SNAME("text_highlight_v_padding"));
  4913. // Draw a box based on color tags associated with glyphs
  4914. for (int i = start; i < end; i++) {
  4915. Item *it = _get_item_at_pos(it_from, it_to, i);
  4916. Color color;
  4917. if (fbg_flag == 0) {
  4918. color = _find_bgcolor(it);
  4919. } else {
  4920. color = _find_fgcolor(it);
  4921. }
  4922. bool change_to_color = ((color.a > 0) && ((last_color.a - 0.0) < 0.01));
  4923. bool change_from_color = (((color.a - 0.0) < 0.01) && (last_color.a > 0.0));
  4924. bool change_color = (((color.a > 0) == (last_color.a > 0)) && (color != last_color));
  4925. if (change_to_color) {
  4926. fbg_index.x = MIN(i, fbg_index.x);
  4927. fbg_index.y = MAX(i, fbg_index.y);
  4928. }
  4929. if (change_from_color || change_color) {
  4930. fbg_index.x = MIN(i, fbg_index.x);
  4931. fbg_index.y = MAX(i, fbg_index.y);
  4932. draw_box = true;
  4933. }
  4934. if (draw_box) {
  4935. Vector<Vector2> sel = TS->shaped_text_get_selection(p_rid, fbg_index.x, fbg_index.y);
  4936. for (int j = 0; j < sel.size(); j++) {
  4937. Vector2 rect_off = line_off + Vector2(sel[j].x - hpad, -TS->shaped_text_get_ascent(p_rid) - vpad);
  4938. Vector2 rect_size = Vector2(sel[j].y - sel[j].x + 2 * hpad, TS->shaped_text_get_size(p_rid).y + 2 * vpad);
  4939. RenderingServer::get_singleton()->canvas_item_add_rect(p_ci, Rect2(rect_off, rect_size), last_color);
  4940. }
  4941. fbg_index = Vector2i(end, start);
  4942. draw_box = false;
  4943. }
  4944. if (change_color) {
  4945. fbg_index.x = MIN(i, fbg_index.x);
  4946. fbg_index.y = MAX(i, fbg_index.y);
  4947. }
  4948. last_color = color;
  4949. }
  4950. if (last_color.a > 0) {
  4951. Vector<Vector2> sel = TS->shaped_text_get_selection(p_rid, fbg_index.x, end);
  4952. for (int i = 0; i < sel.size(); i++) {
  4953. Vector2 rect_off = line_off + Vector2(sel[i].x - hpad, -TS->shaped_text_get_ascent(p_rid) - vpad);
  4954. Vector2 rect_size = Vector2(sel[i].y - sel[i].x + 2 * hpad, TS->shaped_text_get_size(p_rid).y + 2 * vpad);
  4955. RenderingServer::get_singleton()->canvas_item_add_rect(p_ci, Rect2(rect_off, rect_size), last_color);
  4956. }
  4957. }
  4958. }
  4959. Ref<RichTextEffect> RichTextLabel::_get_custom_effect_by_code(String p_bbcode_identifier) {
  4960. for (int i = 0; i < custom_effects.size(); i++) {
  4961. Ref<RichTextEffect> effect = custom_effects[i];
  4962. if (!effect.is_valid()) {
  4963. continue;
  4964. }
  4965. if (effect->get_bbcode() == p_bbcode_identifier) {
  4966. return effect;
  4967. }
  4968. }
  4969. return Ref<RichTextEffect>();
  4970. }
  4971. Dictionary RichTextLabel::parse_expressions_for_values(Vector<String> p_expressions) {
  4972. Dictionary d;
  4973. for (int i = 0; i < p_expressions.size(); i++) {
  4974. String expression = p_expressions[i];
  4975. Array a;
  4976. Vector<String> parts = expression.split("=", true);
  4977. String key = parts[0];
  4978. if (parts.size() != 2) {
  4979. return d;
  4980. }
  4981. Vector<String> values = parts[1].split(",", false);
  4982. #ifdef MODULE_REGEX_ENABLED
  4983. RegEx color = RegEx();
  4984. color.compile("^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$");
  4985. RegEx nodepath = RegEx();
  4986. nodepath.compile("^\\$");
  4987. RegEx boolean = RegEx();
  4988. boolean.compile("^(true|false)$");
  4989. RegEx decimal = RegEx();
  4990. decimal.compile("^-?^.?\\d+(\\.\\d+?)?$");
  4991. RegEx numerical = RegEx();
  4992. numerical.compile("^\\d+$");
  4993. for (int j = 0; j < values.size(); j++) {
  4994. if (!color.search(values[j]).is_null()) {
  4995. a.append(Color::html(values[j]));
  4996. } else if (!nodepath.search(values[j]).is_null()) {
  4997. if (values[j].begins_with("$")) {
  4998. String v = values[j].substr(1, values[j].length());
  4999. a.append(NodePath(v));
  5000. }
  5001. } else if (!boolean.search(values[j]).is_null()) {
  5002. if (values[j] == "true") {
  5003. a.append(true);
  5004. } else if (values[j] == "false") {
  5005. a.append(false);
  5006. }
  5007. } else if (!decimal.search(values[j]).is_null()) {
  5008. a.append(values[j].to_float());
  5009. } else if (!numerical.search(values[j]).is_null()) {
  5010. a.append(values[j].to_int());
  5011. } else {
  5012. a.append(values[j]);
  5013. }
  5014. }
  5015. #endif
  5016. if (values.size() > 1) {
  5017. d[key] = a;
  5018. } else if (values.size() == 1) {
  5019. d[key] = a[0];
  5020. }
  5021. }
  5022. return d;
  5023. }
  5024. RichTextLabel::RichTextLabel(const String &p_text) {
  5025. main = memnew(ItemFrame);
  5026. main->index = 0;
  5027. current = main;
  5028. main->lines.resize(1);
  5029. main->lines[0].from = main;
  5030. main->first_invalid_line.store(0);
  5031. main->first_resized_line.store(0);
  5032. main->first_invalid_font_line.store(0);
  5033. current_frame = main;
  5034. vscroll = memnew(VScrollBar);
  5035. add_child(vscroll, false, INTERNAL_MODE_FRONT);
  5036. vscroll->set_drag_node(String(".."));
  5037. vscroll->set_step(1);
  5038. vscroll->set_anchor_and_offset(SIDE_TOP, ANCHOR_BEGIN, 0);
  5039. vscroll->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, 0);
  5040. vscroll->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_END, 0);
  5041. vscroll->connect("value_changed", callable_mp(this, &RichTextLabel::_scroll_changed));
  5042. vscroll->set_step(1);
  5043. vscroll->hide();
  5044. set_text(p_text);
  5045. updating.store(false);
  5046. stop_thread.store(false);
  5047. set_clip_contents(true);
  5048. }
  5049. RichTextLabel::~RichTextLabel() {
  5050. _stop_thread();
  5051. memdelete(main);
  5052. }