rich_text_label.cpp 255 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743
  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 "rich_text_label.compat.inc"
  32. #include "core/input/input_map.h"
  33. #include "core/math/math_defs.h"
  34. #include "core/os/keyboard.h"
  35. #include "core/os/os.h"
  36. #include "scene/gui/label.h"
  37. #include "scene/gui/rich_text_effect.h"
  38. #include "scene/resources/atlas_texture.h"
  39. #include "scene/theme/theme_db.h"
  40. #include "servers/display_server.h"
  41. #include "modules/modules_enabled.gen.h" // For regex.
  42. #ifdef MODULE_REGEX_ENABLED
  43. #include "modules/regex/regex.h"
  44. #endif
  45. RichTextLabel::ItemCustomFX::ItemCustomFX() {
  46. type = ITEM_CUSTOMFX;
  47. char_fx_transform.instantiate();
  48. }
  49. RichTextLabel::ItemCustomFX::~ItemCustomFX() {
  50. _clear_children();
  51. char_fx_transform.unref();
  52. custom_effect.unref();
  53. }
  54. RichTextLabel::Item *RichTextLabel::_get_next_item(Item *p_item, bool p_free) const {
  55. if (!p_item) {
  56. return nullptr;
  57. }
  58. if (p_free) {
  59. if (p_item->subitems.size()) {
  60. return p_item->subitems.front()->get();
  61. } else if (!p_item->parent) {
  62. return nullptr;
  63. } else if (p_item->E->next()) {
  64. return p_item->E->next()->get();
  65. } else {
  66. // Go up until something with a next is found.
  67. while (p_item->parent && !p_item->E->next()) {
  68. p_item = p_item->parent;
  69. }
  70. if (p_item->parent) {
  71. return p_item->E->next()->get();
  72. } else {
  73. return nullptr;
  74. }
  75. }
  76. } else {
  77. if (p_item->subitems.size() && p_item->type != ITEM_TABLE) {
  78. return p_item->subitems.front()->get();
  79. } else if (p_item->type == ITEM_FRAME) {
  80. return nullptr;
  81. } else if (p_item->E->next()) {
  82. return p_item->E->next()->get();
  83. } else {
  84. // Go up until something with a next is found.
  85. while (p_item->parent && p_item->type != ITEM_FRAME && !p_item->E->next()) {
  86. p_item = p_item->parent;
  87. }
  88. if (p_item->type != ITEM_FRAME) {
  89. return p_item->E->next()->get();
  90. } else {
  91. return nullptr;
  92. }
  93. }
  94. }
  95. }
  96. RichTextLabel::Item *RichTextLabel::_get_prev_item(Item *p_item, bool p_free) const {
  97. if (!p_item) {
  98. return nullptr;
  99. }
  100. if (p_free) {
  101. if (!p_item->parent) {
  102. return nullptr;
  103. } else if (p_item->E->prev()) {
  104. p_item = p_item->E->prev()->get();
  105. while (p_item->subitems.size()) {
  106. p_item = p_item->subitems.back()->get();
  107. }
  108. return p_item;
  109. } else {
  110. if (p_item->parent) {
  111. return p_item->parent;
  112. } else {
  113. return nullptr;
  114. }
  115. }
  116. } else {
  117. if (p_item->type == ITEM_FRAME) {
  118. return nullptr;
  119. } else if (p_item->E->prev()) {
  120. p_item = p_item->E->prev()->get();
  121. while (p_item->subitems.size() && p_item->type != ITEM_TABLE) {
  122. p_item = p_item->subitems.back()->get();
  123. }
  124. return p_item;
  125. } else {
  126. if (p_item->parent && p_item->type != ITEM_FRAME) {
  127. return p_item->parent;
  128. } else {
  129. return nullptr;
  130. }
  131. }
  132. }
  133. }
  134. Rect2 RichTextLabel::_get_text_rect() {
  135. return Rect2(theme_cache.normal_style->get_offset(), get_size() - theme_cache.normal_style->get_minimum_size());
  136. }
  137. RichTextLabel::Item *RichTextLabel::_get_item_at_pos(RichTextLabel::Item *p_item_from, RichTextLabel::Item *p_item_to, int p_position) {
  138. int offset = 0;
  139. for (Item *it = p_item_from; it && it != p_item_to; it = _get_next_item(it)) {
  140. switch (it->type) {
  141. case ITEM_TEXT: {
  142. ItemText *t = static_cast<ItemText *>(it);
  143. offset += t->text.length();
  144. if (offset > p_position) {
  145. return it;
  146. }
  147. } break;
  148. case ITEM_NEWLINE: {
  149. offset += 1;
  150. if (offset == p_position) {
  151. return it;
  152. }
  153. } break;
  154. case ITEM_IMAGE: {
  155. offset += 1;
  156. if (offset > p_position) {
  157. return it;
  158. }
  159. } break;
  160. case ITEM_TABLE: {
  161. offset += 1;
  162. } break;
  163. default:
  164. break;
  165. }
  166. }
  167. return p_item_from;
  168. }
  169. String RichTextLabel::_roman(int p_num, bool p_capitalize) const {
  170. if (p_num > 3999) {
  171. return "ERR";
  172. };
  173. String s;
  174. if (p_capitalize) {
  175. const String roman_M[] = { "", "M", "MM", "MMM" };
  176. const String roman_C[] = { "", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM" };
  177. const String roman_X[] = { "", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC" };
  178. const String roman_I[] = { "", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX" };
  179. s = roman_M[p_num / 1000] + roman_C[(p_num % 1000) / 100] + roman_X[(p_num % 100) / 10] + roman_I[p_num % 10];
  180. } else {
  181. const String roman_M[] = { "", "m", "mm", "mmm" };
  182. const String roman_C[] = { "", "c", "cc", "ccc", "cd", "d", "dc", "dcc", "dccc", "cm" };
  183. const String roman_X[] = { "", "x", "xx", "xxx", "xl", "l", "lx", "lxx", "lxxx", "xc" };
  184. const String roman_I[] = { "", "i", "ii", "iii", "iv", "v", "vi", "vii", "viii", "ix" };
  185. s = roman_M[p_num / 1000] + roman_C[(p_num % 1000) / 100] + roman_X[(p_num % 100) / 10] + roman_I[p_num % 10];
  186. }
  187. return s;
  188. }
  189. String RichTextLabel::_letters(int p_num, bool p_capitalize) const {
  190. int64_t n = p_num;
  191. int chars = 0;
  192. do {
  193. n /= 24;
  194. chars++;
  195. } while (n);
  196. String s;
  197. s.resize(chars + 1);
  198. char32_t *c = s.ptrw();
  199. c[chars] = 0;
  200. n = p_num;
  201. do {
  202. int mod = Math::abs(n % 24);
  203. char a = (p_capitalize ? 'A' : 'a');
  204. c[--chars] = a + mod - 1;
  205. n /= 24;
  206. } while (n);
  207. return s;
  208. }
  209. String RichTextLabel::_get_prefix(Item *p_item, const Vector<int> &p_list_index, const Vector<ItemList *> &p_list_items) {
  210. String prefix;
  211. int segments = 0;
  212. for (int i = 0; i < p_list_index.size(); i++) {
  213. String segment;
  214. if (p_list_items[i]->list_type == LIST_DOTS) {
  215. if (segments == 0) {
  216. prefix = p_list_items[i]->bullet;
  217. }
  218. break;
  219. }
  220. prefix = "." + prefix;
  221. if (p_list_items[i]->list_type == LIST_NUMBERS) {
  222. segment = itos(p_list_index[i]);
  223. if (is_localizing_numeral_system()) {
  224. segment = TS->format_number(segment, _find_language(p_item));
  225. }
  226. segments++;
  227. } else if (p_list_items[i]->list_type == LIST_LETTERS) {
  228. segment = _letters(p_list_index[i], p_list_items[i]->capitalize);
  229. segments++;
  230. } else if (p_list_items[i]->list_type == LIST_ROMAN) {
  231. segment = _roman(p_list_index[i], p_list_items[i]->capitalize);
  232. segments++;
  233. }
  234. prefix = segment + prefix;
  235. }
  236. return prefix + " ";
  237. }
  238. void RichTextLabel::_update_line_font(ItemFrame *p_frame, int p_line, const Ref<Font> &p_base_font, int p_base_font_size) {
  239. ERR_FAIL_NULL(p_frame);
  240. ERR_FAIL_COND(p_line < 0 || p_line >= (int)p_frame->lines.size());
  241. Line &l = p_frame->lines[p_line];
  242. MutexLock lock(l.text_buf->get_mutex());
  243. // Prefix.
  244. Vector<int> list_index;
  245. Vector<int> list_count;
  246. Vector<ItemList *> list_items;
  247. _find_list(l.from, list_index, list_count, list_items);
  248. if (list_items.size() > 0) {
  249. if (list_index[0] == 1) {
  250. // List level start, shape all prefixes for this level and compute max. prefix width.
  251. list_items[0]->max_width = 0;
  252. int index = 0;
  253. for (int i = p_line; i < (int)p_frame->lines.size(); i++) {
  254. Line &list_l = p_frame->lines[i];
  255. if (_find_list_item(list_l.from) == list_items[0]) {
  256. index++;
  257. Ref<Font> font = theme_cache.normal_font;
  258. int font_size = theme_cache.normal_font_size;
  259. ItemFont *font_it = _find_font(list_l.from);
  260. if (font_it) {
  261. if (font_it->font.is_valid()) {
  262. font = font_it->font;
  263. }
  264. if (font_it->font_size > 0) {
  265. font_size = font_it->font_size;
  266. }
  267. }
  268. ItemFontSize *font_size_it = _find_font_size(list_l.from);
  269. if (font_size_it && font_size_it->font_size > 0) {
  270. font_size = font_size_it->font_size;
  271. }
  272. list_index.write[0] = index;
  273. String prefix = _get_prefix(list_l.from, list_index, list_items);
  274. list_l.text_prefix.instantiate();
  275. list_l.text_prefix->set_direction(_find_direction(list_l.from));
  276. list_l.text_prefix->add_string(prefix, font, font_size);
  277. list_items.write[0]->max_width = MAX(list_items[0]->max_width, list_l.text_prefix->get_size().x);
  278. }
  279. }
  280. }
  281. l.prefix_width = list_items[0]->max_width;
  282. }
  283. RID t = l.text_buf->get_rid();
  284. int spans = TS->shaped_get_span_count(t);
  285. for (int i = 0; i < spans; i++) {
  286. Item *it_span = items.get_or_null(TS->shaped_get_span_meta(t, i));
  287. ItemText *it = reinterpret_cast<ItemText *>(it_span);
  288. if (it) {
  289. Ref<Font> font = p_base_font;
  290. int font_size = p_base_font_size;
  291. ItemFont *font_it = _find_font(it);
  292. if (font_it) {
  293. if (font_it->font.is_valid()) {
  294. font = font_it->font;
  295. }
  296. if (font_it->font_size > 0) {
  297. font_size = font_it->font_size;
  298. }
  299. }
  300. ItemFontSize *font_size_it = _find_font_size(it);
  301. if (font_size_it && font_size_it->font_size > 0) {
  302. font_size = font_size_it->font_size;
  303. }
  304. TS->shaped_set_span_update_font(t, i, font->get_rids(), font_size, font->get_opentype_features());
  305. } else {
  306. TS->shaped_set_span_update_font(t, i, p_base_font->get_rids(), p_base_font_size, p_base_font->get_opentype_features());
  307. }
  308. }
  309. Item *it_to = (p_line + 1 < (int)p_frame->lines.size()) ? p_frame->lines[p_line + 1].from : nullptr;
  310. for (Item *it = l.from; it && it != it_to; it = _get_next_item(it)) {
  311. switch (it->type) {
  312. case ITEM_TABLE: {
  313. ItemTable *table = static_cast<ItemTable *>(it);
  314. for (Item *E : table->subitems) {
  315. ERR_CONTINUE(E->type != ITEM_FRAME); // Children should all be frames.
  316. ItemFrame *frame = static_cast<ItemFrame *>(E);
  317. for (int i = 0; i < (int)frame->lines.size(); i++) {
  318. _update_line_font(frame, i, p_base_font, p_base_font_size);
  319. }
  320. }
  321. } break;
  322. default:
  323. break;
  324. }
  325. }
  326. }
  327. 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) {
  328. ERR_FAIL_NULL_V(p_frame, p_h);
  329. ERR_FAIL_COND_V(p_line < 0 || p_line >= (int)p_frame->lines.size(), p_h);
  330. Line &l = p_frame->lines[p_line];
  331. MutexLock lock(l.text_buf->get_mutex());
  332. l.indent = _find_margin(l.from, p_base_font, p_base_font_size) + l.prefix_width;
  333. l.offset.x = l.indent;
  334. l.text_buf->set_width(p_width - l.offset.x);
  335. PackedFloat32Array tab_stops = _find_tab_stops(l.from);
  336. if (!tab_stops.is_empty()) {
  337. l.text_buf->tab_align(tab_stops);
  338. } else if (tab_size > 0) { // Align inline tabs.
  339. Vector<float> tabs;
  340. tabs.push_back(tab_size * p_base_font->get_char_size(' ', p_base_font_size).width);
  341. l.text_buf->tab_align(tabs);
  342. }
  343. Item *it_to = (p_line + 1 < (int)p_frame->lines.size()) ? p_frame->lines[p_line + 1].from : nullptr;
  344. for (Item *it = l.from; it && it != it_to; it = _get_next_item(it)) {
  345. switch (it->type) {
  346. case ITEM_IMAGE: {
  347. ItemImage *img = static_cast<ItemImage *>(it);
  348. Size2 img_size = img->size;
  349. if (img->size_in_percent) {
  350. img_size = _get_image_size(img->image, p_width * img->rq_size.width / 100.f, p_width * img->rq_size.height / 100.f, img->region);
  351. l.text_buf->resize_object(it->rid, img_size, img->inline_align);
  352. }
  353. } break;
  354. case ITEM_TABLE: {
  355. ItemTable *table = static_cast<ItemTable *>(it);
  356. int col_count = table->columns.size();
  357. for (int i = 0; i < col_count; i++) {
  358. table->columns[i].width = 0;
  359. }
  360. for (Item *E : table->subitems) {
  361. ERR_CONTINUE(E->type != ITEM_FRAME); // Children should all be frames.
  362. ItemFrame *frame = static_cast<ItemFrame *>(E);
  363. float prev_h = 0;
  364. for (int i = 0; i < (int)frame->lines.size(); i++) {
  365. MutexLock sub_lock(frame->lines[i].text_buf->get_mutex());
  366. int w = _find_margin(frame->lines[i].from, p_base_font, p_base_font_size) + 1;
  367. prev_h = _resize_line(frame, i, p_base_font, p_base_font_size, w, prev_h);
  368. }
  369. }
  370. const int available_width = p_width - theme_cache.table_h_separation * (col_count - 1);
  371. _set_table_size(table, available_width);
  372. int row_idx = (table->align_to_row < 0) ? table->rows_baseline.size() - 1 : table->align_to_row;
  373. if (table->rows_baseline.size() != 0 && row_idx < (int)table->rows_baseline.size()) {
  374. l.text_buf->resize_object(it->rid, Size2(table->total_width, table->total_height), table->inline_align, Math::round(table->rows_baseline[row_idx]));
  375. } else {
  376. l.text_buf->resize_object(it->rid, Size2(table->total_width, table->total_height), table->inline_align);
  377. }
  378. } break;
  379. default:
  380. break;
  381. }
  382. }
  383. l.offset.y = p_h;
  384. return _calculate_line_vertical_offset(l);
  385. }
  386. 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) {
  387. ERR_FAIL_NULL_V(p_frame, p_h);
  388. ERR_FAIL_COND_V(p_line < 0 || p_line >= (int)p_frame->lines.size(), p_h);
  389. Line &l = p_frame->lines[p_line];
  390. MutexLock lock(l.text_buf->get_mutex());
  391. BitField<TextServer::LineBreakFlag> autowrap_flags = TextServer::BREAK_MANDATORY;
  392. switch (autowrap_mode) {
  393. case TextServer::AUTOWRAP_WORD_SMART:
  394. autowrap_flags = TextServer::BREAK_WORD_BOUND | TextServer::BREAK_ADAPTIVE | TextServer::BREAK_MANDATORY;
  395. break;
  396. case TextServer::AUTOWRAP_WORD:
  397. autowrap_flags = TextServer::BREAK_WORD_BOUND | TextServer::BREAK_MANDATORY;
  398. break;
  399. case TextServer::AUTOWRAP_ARBITRARY:
  400. autowrap_flags = TextServer::BREAK_GRAPHEME_BOUND | TextServer::BREAK_MANDATORY;
  401. break;
  402. case TextServer::AUTOWRAP_OFF:
  403. break;
  404. }
  405. autowrap_flags = autowrap_flags | autowrap_flags_trim;
  406. // Clear cache.
  407. l.dc_item = nullptr;
  408. l.text_buf->clear();
  409. l.text_buf->set_break_flags(autowrap_flags);
  410. l.text_buf->set_justification_flags(_find_jst_flags(l.from));
  411. l.char_offset = *r_char_offset;
  412. l.char_count = 0;
  413. // List prefix.
  414. Vector<int> list_index;
  415. Vector<int> list_count;
  416. Vector<ItemList *> list_items;
  417. _find_list(l.from, list_index, list_count, list_items);
  418. if (list_items.size() > 0) {
  419. if (list_index[0] == 1) {
  420. // List level start, shape all prefixes for this level and compute max. prefix width.
  421. list_items[0]->max_width = 0;
  422. int index = 0;
  423. for (int i = p_line; i < (int)p_frame->lines.size(); i++) {
  424. Line &list_l = p_frame->lines[i];
  425. if (_find_list_item(list_l.from) == list_items[0]) {
  426. index++;
  427. Ref<Font> font = theme_cache.normal_font;
  428. int font_size = theme_cache.normal_font_size;
  429. ItemFont *font_it = _find_font(list_l.from);
  430. if (font_it) {
  431. if (font_it->font.is_valid()) {
  432. font = font_it->font;
  433. }
  434. if (font_it->font_size > 0) {
  435. font_size = font_it->font_size;
  436. }
  437. }
  438. ItemFontSize *font_size_it = _find_font_size(list_l.from);
  439. if (font_size_it && font_size_it->font_size > 0) {
  440. font_size = font_size_it->font_size;
  441. }
  442. list_index.write[0] = index;
  443. String prefix = _get_prefix(list_l.from, list_index, list_items);
  444. list_l.text_prefix.instantiate();
  445. list_l.text_prefix->set_direction(_find_direction(list_l.from));
  446. list_l.text_prefix->add_string(prefix, font, font_size);
  447. list_items.write[0]->max_width = MAX(list_items[0]->max_width, list_l.text_prefix->get_size().x);
  448. }
  449. }
  450. }
  451. l.prefix_width = list_items[0]->max_width;
  452. }
  453. // Add indent.
  454. l.indent = _find_margin(l.from, p_base_font, p_base_font_size) + l.prefix_width;
  455. l.offset.x = l.indent;
  456. l.text_buf->set_width(p_width - l.offset.x);
  457. l.text_buf->set_alignment(_find_alignment(l.from));
  458. l.text_buf->set_direction(_find_direction(l.from));
  459. PackedFloat32Array tab_stops = _find_tab_stops(l.from);
  460. if (!tab_stops.is_empty()) {
  461. l.text_buf->tab_align(tab_stops);
  462. } else if (tab_size > 0) { // Align inline tabs.
  463. Vector<float> tabs;
  464. tabs.push_back(tab_size * p_base_font->get_char_size(' ', p_base_font_size).width);
  465. l.text_buf->tab_align(tabs);
  466. }
  467. // Shape current paragraph.
  468. String txt;
  469. Item *it_to = (p_line + 1 < (int)p_frame->lines.size()) ? p_frame->lines[p_line + 1].from : nullptr;
  470. int remaining_characters = visible_characters - l.char_offset;
  471. for (Item *it = l.from; it && it != it_to; it = _get_next_item(it)) {
  472. if (visible_chars_behavior == TextServer::VC_CHARS_BEFORE_SHAPING && visible_characters >= 0 && remaining_characters <= 0) {
  473. break;
  474. }
  475. switch (it->type) {
  476. case ITEM_DROPCAP: {
  477. // Add dropcap.
  478. ItemDropcap *dc = static_cast<ItemDropcap *>(it);
  479. l.text_buf->set_dropcap(dc->text, dc->font, dc->font_size, dc->dropcap_margins);
  480. l.dc_item = dc;
  481. l.dc_color = dc->color;
  482. l.dc_ol_size = dc->ol_size;
  483. l.dc_ol_color = dc->ol_color;
  484. } break;
  485. case ITEM_NEWLINE: {
  486. Ref<Font> font = p_base_font;
  487. int font_size = p_base_font_size;
  488. ItemFont *font_it = _find_font(it);
  489. if (font_it) {
  490. if (font_it->font.is_valid()) {
  491. font = font_it->font;
  492. }
  493. if (font_it->font_size > 0) {
  494. font_size = font_it->font_size;
  495. }
  496. }
  497. ItemFontSize *font_size_it = _find_font_size(it);
  498. if (font_size_it && font_size_it->font_size > 0) {
  499. font_size = font_size_it->font_size;
  500. }
  501. l.text_buf->add_string(String::chr(0x200B), font, font_size, String(), it->rid);
  502. txt += "\n";
  503. l.char_count++;
  504. remaining_characters--;
  505. } break;
  506. case ITEM_TEXT: {
  507. ItemText *t = static_cast<ItemText *>(it);
  508. Ref<Font> font = p_base_font;
  509. int font_size = p_base_font_size;
  510. ItemFont *font_it = _find_font(it);
  511. if (font_it) {
  512. if (font_it->font.is_valid()) {
  513. font = font_it->font;
  514. }
  515. if (font_it->font_size > 0) {
  516. font_size = font_it->font_size;
  517. }
  518. }
  519. ItemFontSize *font_size_it = _find_font_size(it);
  520. if (font_size_it && font_size_it->font_size > 0) {
  521. font_size = font_size_it->font_size;
  522. }
  523. String lang = _find_language(it);
  524. String tx = t->text;
  525. if (visible_chars_behavior == TextServer::VC_CHARS_BEFORE_SHAPING && visible_characters >= 0 && remaining_characters >= 0) {
  526. tx = tx.substr(0, remaining_characters);
  527. }
  528. remaining_characters -= tx.length();
  529. l.text_buf->add_string(tx, font, font_size, lang, it->rid);
  530. txt += tx;
  531. l.char_count += tx.length();
  532. } break;
  533. case ITEM_IMAGE: {
  534. ItemImage *img = static_cast<ItemImage *>(it);
  535. Size2 img_size = img->size;
  536. if (img->size_in_percent) {
  537. img_size = _get_image_size(img->image, p_width * img->rq_size.width / 100.f, p_width * img->rq_size.height / 100.f, img->region);
  538. }
  539. l.text_buf->add_object(it->rid, img_size, img->inline_align, 1);
  540. txt += String::chr(0xfffc);
  541. l.char_count++;
  542. remaining_characters--;
  543. } break;
  544. case ITEM_TABLE: {
  545. ItemTable *table = static_cast<ItemTable *>(it);
  546. int col_count = table->columns.size();
  547. int t_char_count = 0;
  548. // Set minimums to zero.
  549. for (int i = 0; i < col_count; i++) {
  550. table->columns[i].min_width = 0;
  551. table->columns[i].max_width = 0;
  552. table->columns[i].width = 0;
  553. }
  554. // Compute minimum width for each cell.
  555. const int available_width = p_width - theme_cache.table_h_separation * (col_count - 1);
  556. int idx = 0;
  557. for (Item *E : table->subitems) {
  558. ERR_CONTINUE(E->type != ITEM_FRAME); // Children should all be frames.
  559. ItemFrame *frame = static_cast<ItemFrame *>(E);
  560. int column = idx % col_count;
  561. float prev_h = 0;
  562. for (int i = 0; i < (int)frame->lines.size(); i++) {
  563. MutexLock sub_lock(frame->lines[i].text_buf->get_mutex());
  564. int char_offset = l.char_offset + l.char_count;
  565. int w = _find_margin(frame->lines[i].from, p_base_font, p_base_font_size) + 1;
  566. prev_h = _shape_line(frame, i, p_base_font, p_base_font_size, w, prev_h, &char_offset);
  567. int cell_ch = (char_offset - (l.char_offset + l.char_count));
  568. l.char_count += cell_ch;
  569. t_char_count += cell_ch;
  570. remaining_characters -= cell_ch;
  571. table->columns[column].min_width = MAX(table->columns[column].min_width, frame->lines[i].indent + std::ceil(frame->lines[i].text_buf->get_size().x));
  572. table->columns[column].max_width = MAX(table->columns[column].max_width, frame->lines[i].indent + std::ceil(frame->lines[i].text_buf->get_non_wrapped_size().x));
  573. }
  574. idx++;
  575. }
  576. _set_table_size(table, available_width);
  577. int row_idx = (table->align_to_row < 0) ? table->rows_baseline.size() - 1 : table->align_to_row;
  578. if (table->rows_baseline.size() != 0 && row_idx < (int)table->rows_baseline.size()) {
  579. l.text_buf->add_object(it->rid, Size2(table->total_width, table->total_height), table->inline_align, t_char_count, Math::round(table->rows_baseline[row_idx]));
  580. } else {
  581. l.text_buf->add_object(it->rid, Size2(table->total_width, table->total_height), table->inline_align, t_char_count);
  582. }
  583. txt += String::chr(0xfffc).repeat(t_char_count);
  584. } break;
  585. default:
  586. break;
  587. }
  588. }
  589. // Apply BiDi override.
  590. l.text_buf->set_bidi_override(structured_text_parser(_find_stt(l.from), st_args, txt));
  591. *r_char_offset = l.char_offset + l.char_count;
  592. l.offset.y = p_h;
  593. return _calculate_line_vertical_offset(l);
  594. }
  595. void RichTextLabel::_set_table_size(ItemTable *p_table, int p_available_width) {
  596. int col_count = p_table->columns.size();
  597. // Compute available width and total ratio (for expanders).
  598. int total_ratio = 0;
  599. int remaining_width = p_available_width;
  600. p_table->total_width = theme_cache.table_h_separation;
  601. for (int i = 0; i < col_count; i++) {
  602. remaining_width -= p_table->columns[i].min_width;
  603. if (p_table->columns[i].max_width > p_table->columns[i].min_width) {
  604. p_table->columns[i].expand = true;
  605. }
  606. if (p_table->columns[i].expand) {
  607. total_ratio += p_table->columns[i].expand_ratio;
  608. }
  609. }
  610. // Assign actual widths.
  611. for (int i = 0; i < col_count; i++) {
  612. p_table->columns[i].width = p_table->columns[i].min_width;
  613. if (p_table->columns[i].expand && total_ratio > 0 && remaining_width > 0) {
  614. p_table->columns[i].width += p_table->columns[i].expand_ratio * remaining_width / total_ratio;
  615. }
  616. if (i != col_count - 1) {
  617. p_table->total_width += p_table->columns[i].width + theme_cache.table_h_separation;
  618. } else {
  619. p_table->total_width += p_table->columns[i].width;
  620. }
  621. p_table->columns[i].width_with_padding = p_table->columns[i].width;
  622. }
  623. // Resize to max_width if needed and distribute the remaining space.
  624. bool table_need_fit = true;
  625. while (table_need_fit) {
  626. table_need_fit = false;
  627. // Fit slim.
  628. for (int i = 0; i < col_count; i++) {
  629. if (!p_table->columns[i].expand || !p_table->columns[i].shrink) {
  630. continue;
  631. }
  632. int dif = p_table->columns[i].width - p_table->columns[i].max_width;
  633. if (dif > 0) {
  634. table_need_fit = true;
  635. p_table->columns[i].width = p_table->columns[i].max_width;
  636. p_table->total_width -= dif;
  637. total_ratio -= p_table->columns[i].expand_ratio;
  638. p_table->columns[i].width_with_padding = p_table->columns[i].width;
  639. }
  640. }
  641. // Grow.
  642. remaining_width = p_available_width - p_table->total_width;
  643. if (remaining_width > 0 && total_ratio > 0) {
  644. for (int i = 0; i < col_count; i++) {
  645. if (p_table->columns[i].expand) {
  646. int dif = p_table->columns[i].max_width - p_table->columns[i].width;
  647. if (dif > 0) {
  648. int slice = p_table->columns[i].expand_ratio * remaining_width / total_ratio;
  649. int incr = MIN(dif, slice);
  650. p_table->columns[i].width += incr;
  651. p_table->total_width += incr;
  652. p_table->columns[i].width_with_padding = p_table->columns[i].width;
  653. }
  654. }
  655. }
  656. }
  657. }
  658. // Update line width and get total height.
  659. int idx = 0;
  660. p_table->total_height = 0;
  661. p_table->rows.clear();
  662. p_table->rows_no_padding.clear();
  663. p_table->rows_baseline.clear();
  664. Vector2 offset = Vector2(theme_cache.table_h_separation * 0.5, theme_cache.table_v_separation * 0.5).floor();
  665. float row_height = 0.0;
  666. float row_top_padding = 0.0;
  667. float row_bottom_padding = 0.0;
  668. const List<Item *>::Element *prev = p_table->subitems.front();
  669. for (const List<Item *>::Element *E = prev; E; E = E->next()) {
  670. ERR_CONTINUE(E->get()->type != ITEM_FRAME); // Children should all be frames.
  671. ItemFrame *frame = static_cast<ItemFrame *>(E->get());
  672. int column = idx % col_count;
  673. offset.x += frame->padding.position.x;
  674. float yofs = 0.0;
  675. float prev_h = 0.0;
  676. float row_baseline = 0.0;
  677. for (int i = 0; i < (int)frame->lines.size(); i++) {
  678. MutexLock sub_lock(frame->lines[i].text_buf->get_mutex());
  679. frame->lines[i].text_buf->set_width(p_table->columns[column].width);
  680. p_table->columns[column].width = MAX(p_table->columns[column].width, std::ceil(frame->lines[i].text_buf->get_size().x));
  681. p_table->columns[column].width_with_padding = MAX(p_table->columns[column].width_with_padding, std::ceil(frame->lines[i].text_buf->get_size().x + frame->padding.position.x + frame->padding.size.x));
  682. frame->lines[i].offset.y = prev_h;
  683. float h = frame->lines[i].text_buf->get_size().y + (frame->lines[i].text_buf->get_line_count() - 1) * theme_cache.line_separation;
  684. if (i > 0) {
  685. h += theme_cache.line_separation;
  686. }
  687. if (frame->min_size_over.y > 0) {
  688. h = MAX(h, frame->min_size_over.y);
  689. }
  690. if (frame->max_size_over.y > 0) {
  691. h = MIN(h, frame->max_size_over.y);
  692. }
  693. yofs += h;
  694. 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;
  695. frame->lines[i].offset += offset;
  696. row_baseline = MAX(row_baseline, frame->lines[i].text_buf->get_line_ascent(frame->lines[i].text_buf->get_line_count() - 1));
  697. }
  698. row_top_padding = MAX(row_top_padding, frame->padding.position.y);
  699. row_bottom_padding = MAX(row_bottom_padding, frame->padding.size.y);
  700. offset.x += p_table->columns[column].width + theme_cache.table_h_separation + frame->padding.size.x;
  701. row_height = MAX(yofs, row_height);
  702. // Add row height after last column of the row or last cell of the table.
  703. if (column == col_count - 1 || E->next() == nullptr) {
  704. offset.x = Math::floor(theme_cache.table_h_separation * 0.5);
  705. float row_contents_height = row_height;
  706. row_height += row_top_padding + row_bottom_padding;
  707. row_height += theme_cache.table_v_separation;
  708. p_table->total_height += row_height;
  709. offset.y += row_height;
  710. p_table->rows.push_back(row_height);
  711. p_table->rows_no_padding.push_back(row_contents_height);
  712. p_table->rows_baseline.push_back(p_table->total_height - row_height + row_baseline + Math::floor(theme_cache.table_v_separation * 0.5));
  713. for (const List<Item *>::Element *F = prev; F; F = F->next()) {
  714. ItemFrame *in_frame = static_cast<ItemFrame *>(F->get());
  715. for (int i = 0; i < (int)in_frame->lines.size(); i++) {
  716. in_frame->lines[i].offset.y += row_top_padding;
  717. }
  718. if (in_frame == frame) {
  719. break;
  720. }
  721. }
  722. row_height = 0.0;
  723. row_top_padding = 0.0;
  724. row_bottom_padding = 0.0;
  725. prev = E->next();
  726. }
  727. idx++;
  728. }
  729. // Recalculate total width.
  730. p_table->total_width = 0;
  731. for (int i = 0; i < col_count; i++) {
  732. p_table->total_width += p_table->columns[i].width_with_padding + theme_cache.table_h_separation;
  733. }
  734. }
  735. int RichTextLabel::_draw_line(ItemFrame *p_frame, int p_line, const Vector2 &p_ofs, int p_width, float p_vsep, 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) {
  736. ERR_FAIL_NULL_V(p_frame, 0);
  737. ERR_FAIL_COND_V(p_line < 0 || p_line >= (int)p_frame->lines.size(), 0);
  738. Vector2 off;
  739. Line &l = p_frame->lines[p_line];
  740. MutexLock lock(l.text_buf->get_mutex());
  741. Item *it_from = l.from;
  742. if (it_from == nullptr) {
  743. return 0;
  744. }
  745. RID ci = get_canvas_item();
  746. bool rtl = (l.text_buf->get_direction() == TextServer::DIRECTION_RTL);
  747. bool lrtl = is_layout_rtl();
  748. bool trim_chars = (visible_characters >= 0) && (visible_chars_behavior == TextServer::VC_CHARS_AFTER_SHAPING);
  749. bool trim_glyphs_ltr = (visible_characters >= 0) && ((visible_chars_behavior == TextServer::VC_GLYPHS_LTR) || ((visible_chars_behavior == TextServer::VC_GLYPHS_AUTO) && !lrtl));
  750. bool trim_glyphs_rtl = (visible_characters >= 0) && ((visible_chars_behavior == TextServer::VC_GLYPHS_RTL) || ((visible_chars_behavior == TextServer::VC_GLYPHS_AUTO) && lrtl));
  751. int total_glyphs = (trim_glyphs_ltr || trim_glyphs_rtl) ? get_total_glyph_count() : 0;
  752. int visible_glyphs = total_glyphs * visible_ratio;
  753. // Draw dropcap.
  754. int dc_lines = l.text_buf->get_dropcap_lines();
  755. float h_off = l.text_buf->get_dropcap_size().x;
  756. bool skip_dc = (trim_chars && l.char_offset > visible_characters) || (trim_glyphs_ltr && (r_processed_glyphs >= visible_glyphs)) || (trim_glyphs_rtl && (r_processed_glyphs < total_glyphs - visible_glyphs));
  757. if (!skip_dc) {
  758. if (l.dc_ol_size > 0) {
  759. l.text_buf->draw_dropcap_outline(ci, p_ofs + ((rtl) ? Vector2() : Vector2(l.offset.x, 0)), l.dc_ol_size, l.dc_ol_color);
  760. }
  761. l.text_buf->draw_dropcap(ci, p_ofs + ((rtl) ? Vector2() : Vector2(l.offset.x, 0)), l.dc_color);
  762. }
  763. int line_count = 0;
  764. bool has_visible_chars = false;
  765. // Bottom margin for text clipping.
  766. float v_limit = theme_cache.normal_style->get_margin(SIDE_BOTTOM);
  767. Size2 ctrl_size = get_size();
  768. // Draw text.
  769. for (int line = 0; line < l.text_buf->get_line_count(); line++) {
  770. if (line > 0) {
  771. off.y += (theme_cache.line_separation + p_vsep);
  772. }
  773. if (p_ofs.y + off.y >= ctrl_size.height - v_limit) {
  774. break;
  775. }
  776. double l_height = l.text_buf->get_line_ascent(line) + l.text_buf->get_line_descent(line);
  777. if (p_ofs.y + off.y + l_height <= 0) {
  778. off.y += l_height;
  779. continue;
  780. }
  781. float width = l.text_buf->get_width();
  782. float length = l.text_buf->get_line_size(line).x;
  783. // Draw line.
  784. if (rtl) {
  785. off.x = p_width - l.offset.x - width;
  786. if (!lrtl && p_frame == main) { // Skip Scrollbar.
  787. off.x -= scroll_w;
  788. }
  789. } else {
  790. off.x = l.offset.x;
  791. if (lrtl && p_frame == main) { // Skip Scrollbar.
  792. off.x += scroll_w;
  793. }
  794. }
  795. // Draw text.
  796. switch (l.text_buf->get_alignment()) {
  797. case HORIZONTAL_ALIGNMENT_FILL:
  798. case HORIZONTAL_ALIGNMENT_LEFT: {
  799. if (rtl) {
  800. off.x += width - length;
  801. }
  802. } break;
  803. case HORIZONTAL_ALIGNMENT_CENTER: {
  804. off.x += Math::floor((width - length) / 2.0);
  805. } break;
  806. case HORIZONTAL_ALIGNMENT_RIGHT: {
  807. if (!rtl) {
  808. off.x += width - length;
  809. }
  810. } break;
  811. }
  812. bool skip_prefix = (visible_chars_behavior == TextServer::VC_CHARS_BEFORE_SHAPING && l.char_offset == visible_characters) || (trim_chars && l.char_offset > visible_characters) || (trim_glyphs_ltr && (r_processed_glyphs >= visible_glyphs)) || (trim_glyphs_rtl && (r_processed_glyphs < total_glyphs - visible_glyphs));
  813. if (l.text_prefix.is_valid() && line == 0 && !skip_prefix) {
  814. Color font_color = _find_color(l.from, p_base_color);
  815. int outline_size = _find_outline_size(l.from, p_outline_size);
  816. Color font_outline_color = _find_outline_color(l.from, p_outline_color);
  817. Color font_shadow_color = p_font_shadow_color * Color(1, 1, 1, font_color.a);
  818. if (rtl) {
  819. if (p_shadow_outline_size > 0 && font_shadow_color.a != 0.0) {
  820. l.text_prefix->draw_outline(ci, p_ofs + Vector2(off.x + length, 0) + p_shadow_ofs, p_shadow_outline_size, font_shadow_color);
  821. }
  822. if (outline_size > 0 && font_outline_color.a != 0.0) {
  823. l.text_prefix->draw_outline(ci, p_ofs + Vector2(off.x + length, 0), outline_size, font_outline_color);
  824. }
  825. l.text_prefix->draw(ci, p_ofs + Vector2(off.x + length, 0), font_color);
  826. } else {
  827. if (p_shadow_outline_size > 0 && font_shadow_color.a != 0.0) {
  828. l.text_prefix->draw_outline(ci, p_ofs + Vector2(off.x - l.text_prefix->get_size().x, 0) + p_shadow_ofs, p_shadow_outline_size, font_shadow_color);
  829. }
  830. if (outline_size > 0 && font_outline_color.a != 0.0) {
  831. l.text_prefix->draw_outline(ci, p_ofs + Vector2(off.x - l.text_prefix->get_size().x, 0), outline_size, font_outline_color);
  832. }
  833. l.text_prefix->draw(ci, p_ofs + Vector2(off.x - l.text_prefix->get_size().x, 0), font_color);
  834. }
  835. }
  836. if (line <= dc_lines) {
  837. if (rtl) {
  838. off.x -= h_off;
  839. } else {
  840. off.x += h_off;
  841. }
  842. }
  843. RID rid = l.text_buf->get_line_rid(line);
  844. double l_ascent = TS->shaped_text_get_ascent(rid);
  845. Size2 l_size = TS->shaped_text_get_size(rid);
  846. double upos = TS->shaped_text_get_underline_position(rid);
  847. double uth = TS->shaped_text_get_underline_thickness(rid);
  848. off.y += l_ascent;
  849. const Glyph *glyphs = TS->shaped_text_get_glyphs(rid);
  850. int gl_size = TS->shaped_text_get_glyph_count(rid);
  851. Vector2i chr_range = TS->shaped_text_get_range(rid);
  852. int sel_start = -1;
  853. int sel_end = -1;
  854. if (selection.active && (selection.from_frame->lines[selection.from_line].char_offset + selection.from_char) <= (l.char_offset + chr_range.y) && (selection.to_frame->lines[selection.to_line].char_offset + selection.to_char) >= (l.char_offset + chr_range.x)) {
  855. sel_start = MAX(chr_range.x, (selection.from_frame->lines[selection.from_line].char_offset + selection.from_char) - l.char_offset);
  856. sel_end = MIN(chr_range.y, (selection.to_frame->lines[selection.to_line].char_offset + selection.to_char) - l.char_offset);
  857. }
  858. int processed_glyphs_step = 0;
  859. for (int step = DRAW_STEP_BACKGROUND; step < DRAW_STEP_MAX; step++) {
  860. if (step == DRAW_STEP_TEXT) {
  861. // Draw inlined objects.
  862. Array objects = TS->shaped_text_get_objects(rid);
  863. for (int i = 0; i < objects.size(); i++) {
  864. Item *it = items.get_or_null(objects[i]);
  865. if (it != nullptr) {
  866. Vector2i obj_range = TS->shaped_text_get_object_range(rid, objects[i]);
  867. if (trim_chars && l.char_offset + obj_range.y > visible_characters) {
  868. continue;
  869. }
  870. if (trim_glyphs_ltr || trim_glyphs_rtl) {
  871. int obj_glyph = r_processed_glyphs + TS->shaped_text_get_object_glyph(rid, objects[i]);
  872. if ((trim_glyphs_ltr && (obj_glyph >= visible_glyphs)) || (trim_glyphs_rtl && (obj_glyph < total_glyphs - visible_glyphs))) {
  873. continue;
  874. }
  875. }
  876. Rect2 rect = TS->shaped_text_get_object_rect(rid, objects[i]);
  877. switch (it->type) {
  878. case ITEM_IMAGE: {
  879. ItemImage *img = static_cast<ItemImage *>(it);
  880. if (img->pad) {
  881. Size2 pad_size = rect.size.min(img->image->get_size());
  882. Vector2 pad_off = (rect.size - pad_size) / 2;
  883. img->image->draw_rect(ci, Rect2(p_ofs + rect.position + off + pad_off, pad_size), false, img->color);
  884. } else {
  885. img->image->draw_rect(ci, Rect2(p_ofs + rect.position + off, rect.size), false, img->color);
  886. }
  887. } break;
  888. case ITEM_TABLE: {
  889. ItemTable *table = static_cast<ItemTable *>(it);
  890. Color odd_row_bg = theme_cache.table_odd_row_bg;
  891. Color even_row_bg = theme_cache.table_even_row_bg;
  892. Color border = theme_cache.table_border;
  893. float h_separation = theme_cache.table_h_separation;
  894. float v_separation = theme_cache.table_v_separation;
  895. int col_count = table->columns.size();
  896. int row_count = table->rows.size();
  897. int idx = 0;
  898. for (Item *E : table->subitems) {
  899. ItemFrame *frame = static_cast<ItemFrame *>(E);
  900. int col = idx % col_count;
  901. int row = idx / col_count;
  902. if (frame->lines.size() != 0 && row < row_count) {
  903. Vector2 coff = frame->lines[0].offset;
  904. coff.x -= frame->lines[0].indent;
  905. if (rtl) {
  906. coff.x = rect.size.width - table->columns[col].width - coff.x;
  907. }
  908. if (row % 2 == 0) {
  909. Color c = frame->odd_row_bg != Color(0, 0, 0, 0) ? frame->odd_row_bg : odd_row_bg;
  910. if (c.a > 0.0) {
  911. draw_rect(Rect2(p_ofs + rect.position + off + coff - frame->padding.position - Vector2(h_separation * 0.5, v_separation * 0.5).floor(), Size2(table->columns[col].width + h_separation + frame->padding.position.x + frame->padding.size.x, table->rows_no_padding[row] + frame->padding.position.y + frame->padding.size.y)), c, true);
  912. }
  913. } else {
  914. Color c = frame->even_row_bg != Color(0, 0, 0, 0) ? frame->even_row_bg : even_row_bg;
  915. if (c.a > 0.0) {
  916. draw_rect(Rect2(p_ofs + rect.position + off + coff - frame->padding.position - Vector2(h_separation * 0.5, v_separation * 0.5).floor(), Size2(table->columns[col].width + h_separation + frame->padding.position.x + frame->padding.size.x, table->rows_no_padding[row] + frame->padding.position.y + frame->padding.size.y)), c, true);
  917. }
  918. }
  919. Color bc = frame->border != Color(0, 0, 0, 0) ? frame->border : border;
  920. if (bc.a > 0.0) {
  921. draw_rect(Rect2(p_ofs + rect.position + off + coff - frame->padding.position - Vector2(h_separation * 0.5, v_separation * 0.5).floor(), Size2(table->columns[col].width + h_separation + frame->padding.position.x + frame->padding.size.x, table->rows_no_padding[row] + frame->padding.position.y + frame->padding.size.y)), bc, false);
  922. }
  923. }
  924. for (int j = 0; j < (int)frame->lines.size(); j++) {
  925. _draw_line(frame, j, p_ofs + rect.position + off + Vector2(0, frame->lines[j].offset.y), rect.size.x, 0, p_base_color, p_outline_size, p_outline_color, p_font_shadow_color, p_shadow_outline_size, p_shadow_ofs, r_processed_glyphs);
  926. }
  927. idx++;
  928. }
  929. } break;
  930. default:
  931. break;
  932. }
  933. }
  934. }
  935. }
  936. Vector2 off_step = off;
  937. processed_glyphs_step = r_processed_glyphs;
  938. Vector2 ul_start;
  939. bool ul_started = false;
  940. Color ul_color_prev;
  941. Color ul_color;
  942. Vector2 dot_ul_start;
  943. bool dot_ul_started = false;
  944. Color dot_ul_color_prev;
  945. Color dot_ul_color;
  946. Vector2 st_start;
  947. bool st_started = false;
  948. Color st_color_prev;
  949. Color st_color;
  950. float box_start = 0.0;
  951. Color last_color = Color(0, 0, 0, 0);
  952. Item *it = it_from;
  953. int span = -1;
  954. for (int i = 0; i < gl_size; i++) {
  955. bool selected = selection.active && (sel_start != -1) && (glyphs[i].start >= sel_start) && (glyphs[i].end <= sel_end);
  956. if (glyphs[i].span_index != span) {
  957. span = glyphs[i].span_index;
  958. if (span >= 0) {
  959. if ((glyphs[i].flags & TextServer::GRAPHEME_IS_EMBEDDED_OBJECT) == TextServer::GRAPHEME_IS_EMBEDDED_OBJECT) {
  960. Item *new_it = items.get_or_null(TS->shaped_get_span_embedded_object(rid, span));
  961. if (new_it) {
  962. it = new_it;
  963. }
  964. } else {
  965. Item *new_it = items.get_or_null(TS->shaped_get_span_meta(rid, span));
  966. if (new_it) {
  967. it = new_it;
  968. }
  969. }
  970. }
  971. }
  972. Color font_color = (step == DRAW_STEP_SHADOW_OUTLINE || step == DRAW_STEP_SHADOW || step == DRAW_STEP_OUTLINE || step == DRAW_STEP_TEXT) ? _find_color(it, p_base_color) : Color();
  973. int outline_size = (step == DRAW_STEP_OUTLINE) ? _find_outline_size(it, p_outline_size) : 0;
  974. Color font_outline_color = (step == DRAW_STEP_OUTLINE) ? _find_outline_color(it, p_outline_color) : Color();
  975. Color font_shadow_color = p_font_shadow_color;
  976. bool txt_visible = (font_color.a != 0);
  977. if (step == DRAW_STEP_OUTLINE && (outline_size <= 0 || font_outline_color.a == 0)) {
  978. continue;
  979. } else if (step == DRAW_STEP_SHADOW_OUTLINE && (font_shadow_color.a == 0 || p_shadow_outline_size <= 0)) {
  980. continue;
  981. } else if (step == DRAW_STEP_SHADOW && (font_shadow_color.a == 0)) {
  982. continue;
  983. } else if (step == DRAW_STEP_TEXT) {
  984. bool has_ul = _find_underline(it);
  985. if (!has_ul && underline_meta) {
  986. ItemMeta *meta = nullptr;
  987. if (_find_meta(it, nullptr, &meta) && meta) {
  988. switch (meta->underline) {
  989. case META_UNDERLINE_ALWAYS: {
  990. has_ul = true;
  991. } break;
  992. case META_UNDERLINE_NEVER: {
  993. has_ul = false;
  994. } break;
  995. case META_UNDERLINE_ON_HOVER: {
  996. has_ul = (meta == meta_hovering);
  997. } break;
  998. }
  999. }
  1000. }
  1001. if (has_ul) {
  1002. if (ul_started && font_color != ul_color_prev) {
  1003. float y_off = upos;
  1004. float underline_width = MAX(1.0, uth * theme_cache.base_scale);
  1005. draw_line(ul_start + Vector2(0, y_off), p_ofs + Vector2(off_step.x, off_step.y + y_off), ul_color, underline_width);
  1006. ul_start = p_ofs + Vector2(off_step.x, off_step.y);
  1007. ul_color_prev = font_color;
  1008. ul_color = font_color;
  1009. ul_color.a *= 0.5;
  1010. } else if (!ul_started) {
  1011. ul_started = true;
  1012. ul_start = p_ofs + Vector2(off_step.x, off_step.y);
  1013. ul_color_prev = font_color;
  1014. ul_color = font_color;
  1015. ul_color.a *= 0.5;
  1016. }
  1017. } else if (ul_started) {
  1018. ul_started = false;
  1019. float y_off = upos;
  1020. float underline_width = MAX(1.0, uth * theme_cache.base_scale);
  1021. draw_line(ul_start + Vector2(0, y_off), p_ofs + Vector2(off_step.x, off_step.y + y_off), ul_color, underline_width);
  1022. }
  1023. if (_find_hint(it, nullptr) && underline_hint) {
  1024. if (dot_ul_started && font_color != dot_ul_color_prev) {
  1025. float y_off = upos;
  1026. float underline_width = MAX(1.0, uth * theme_cache.base_scale);
  1027. draw_dashed_line(dot_ul_start + Vector2(0, y_off), p_ofs + Vector2(off_step.x, off_step.y + y_off), dot_ul_color, underline_width, MAX(2.0, underline_width * 2));
  1028. dot_ul_start = p_ofs + Vector2(off_step.x, off_step.y);
  1029. dot_ul_color_prev = font_color;
  1030. dot_ul_color = font_color;
  1031. dot_ul_color.a *= 0.5;
  1032. } else if (!dot_ul_started) {
  1033. dot_ul_started = true;
  1034. dot_ul_start = p_ofs + Vector2(off_step.x, off_step.y);
  1035. dot_ul_color_prev = font_color;
  1036. dot_ul_color = font_color;
  1037. dot_ul_color.a *= 0.5;
  1038. }
  1039. } else if (dot_ul_started) {
  1040. dot_ul_started = false;
  1041. float y_off = upos;
  1042. float underline_width = MAX(1.0, uth * theme_cache.base_scale);
  1043. draw_dashed_line(dot_ul_start + Vector2(0, y_off), p_ofs + Vector2(off_step.x, off_step.y + y_off), dot_ul_color, underline_width, MAX(2.0, underline_width * 2));
  1044. }
  1045. if (_find_strikethrough(it)) {
  1046. if (st_started && font_color != st_color_prev) {
  1047. float y_off = -l_ascent + l_size.y / 2;
  1048. float underline_width = MAX(1.0, uth * theme_cache.base_scale);
  1049. draw_line(st_start + Vector2(0, y_off), p_ofs + Vector2(off_step.x, off_step.y + y_off), st_color, underline_width);
  1050. st_start = p_ofs + Vector2(off_step.x, off_step.y);
  1051. st_color_prev = font_color;
  1052. st_color = font_color;
  1053. st_color.a *= 0.5;
  1054. } else if (!st_started) {
  1055. st_started = true;
  1056. st_start = p_ofs + Vector2(off_step.x, off_step.y);
  1057. st_color_prev = font_color;
  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 = -l_ascent + l_size.y / 2;
  1064. float underline_width = MAX(1.0, uth * theme_cache.base_scale);
  1065. draw_line(st_start + Vector2(0, y_off), p_ofs + Vector2(off_step.x, off_step.y + y_off), st_color, underline_width);
  1066. }
  1067. }
  1068. if (step == DRAW_STEP_SHADOW_OUTLINE || step == DRAW_STEP_SHADOW || step == DRAW_STEP_OUTLINE || step == DRAW_STEP_TEXT) {
  1069. ItemFade *fade = nullptr;
  1070. Item *fade_item = it;
  1071. while (fade_item) {
  1072. if (fade_item->type == ITEM_FADE) {
  1073. fade = static_cast<ItemFade *>(fade_item);
  1074. break;
  1075. }
  1076. fade_item = fade_item->parent;
  1077. }
  1078. Vector<ItemFX *> fx_stack;
  1079. _fetch_item_fx_stack(it, fx_stack);
  1080. bool custom_fx_ok = true;
  1081. Point2 fx_offset = Vector2(glyphs[i].x_off, glyphs[i].y_off);
  1082. RID frid = glyphs[i].font_rid;
  1083. uint32_t gl = glyphs[i].index;
  1084. uint16_t gl_fl = glyphs[i].flags;
  1085. uint8_t gl_cn = glyphs[i].count;
  1086. bool cprev_cluster = false;
  1087. bool cprev_conn = false;
  1088. if (gl_cn == 0) { // Parts of the same grapheme cluster, always connected.
  1089. cprev_cluster = true;
  1090. }
  1091. if (gl_fl & TextServer::GRAPHEME_IS_RTL) { // Check if previous grapheme cluster is connected.
  1092. if (i > 0 && (glyphs[i - 1].flags & TextServer::GRAPHEME_IS_CONNECTED)) {
  1093. cprev_conn = true;
  1094. }
  1095. } else {
  1096. if (glyphs[i].flags & TextServer::GRAPHEME_IS_CONNECTED) {
  1097. cprev_conn = true;
  1098. }
  1099. }
  1100. //Apply fx.
  1101. if (fade) {
  1102. float faded_visibility = 1.0f;
  1103. if (l.char_offset + glyphs[i].start >= fade->char_ofs + fade->starting_index) {
  1104. faded_visibility -= (float)((l.char_offset + glyphs[i].start) - (fade->char_ofs + fade->starting_index)) / (float)fade->length;
  1105. faded_visibility = faded_visibility < 0.0f ? 0.0f : faded_visibility;
  1106. }
  1107. font_color.a = faded_visibility;
  1108. }
  1109. Transform2D char_xform;
  1110. char_xform.set_origin(p_ofs + off_step);
  1111. for (int j = 0; j < fx_stack.size(); j++) {
  1112. ItemFX *item_fx = fx_stack[j];
  1113. bool cn = cprev_cluster || (cprev_conn && item_fx->connected);
  1114. if (item_fx->type == ITEM_CUSTOMFX && custom_fx_ok) {
  1115. ItemCustomFX *item_custom = static_cast<ItemCustomFX *>(item_fx);
  1116. Ref<CharFXTransform> charfx = item_custom->char_fx_transform;
  1117. Ref<RichTextEffect> custom_effect = item_custom->custom_effect;
  1118. if (custom_effect.is_valid()) {
  1119. charfx->elapsed_time = item_custom->elapsed_time;
  1120. charfx->range = Vector2i(l.char_offset + glyphs[i].start, l.char_offset + glyphs[i].end);
  1121. charfx->relative_index = l.char_offset + glyphs[i].start - item_fx->char_ofs;
  1122. charfx->visibility = txt_visible;
  1123. charfx->outline = (step == DRAW_STEP_SHADOW_OUTLINE) || (step == DRAW_STEP_SHADOW) || (step == DRAW_STEP_OUTLINE);
  1124. charfx->font = frid;
  1125. charfx->glyph_index = gl;
  1126. charfx->glyph_flags = gl_fl;
  1127. charfx->glyph_count = gl_cn;
  1128. charfx->offset = fx_offset;
  1129. charfx->color = font_color;
  1130. charfx->transform = char_xform;
  1131. bool effect_status = custom_effect->_process_effect_impl(charfx);
  1132. custom_fx_ok = effect_status;
  1133. char_xform = charfx->transform;
  1134. fx_offset = charfx->offset;
  1135. font_color = charfx->color;
  1136. gl = charfx->glyph_index;
  1137. txt_visible &= charfx->visibility;
  1138. }
  1139. } else if (item_fx->type == ITEM_SHAKE) {
  1140. ItemShake *item_shake = static_cast<ItemShake *>(item_fx);
  1141. if (!cn) {
  1142. uint64_t char_current_rand = item_shake->offset_random(glyphs[i].start);
  1143. uint64_t char_previous_rand = item_shake->offset_previous_random(glyphs[i].start);
  1144. uint64_t max_rand = 2147483647;
  1145. double current_offset = Math::remap(char_current_rand % max_rand, 0, max_rand, 0.0f, 2.f * (float)Math::PI);
  1146. double previous_offset = Math::remap(char_previous_rand % max_rand, 0, max_rand, 0.0f, 2.f * (float)Math::PI);
  1147. double n_time = (double)(item_shake->elapsed_time / (0.5f / item_shake->rate));
  1148. n_time = (n_time > 1.0) ? 1.0 : n_time;
  1149. 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;
  1150. }
  1151. fx_offset += item_shake->prev_off;
  1152. } else if (item_fx->type == ITEM_WAVE) {
  1153. ItemWave *item_wave = static_cast<ItemWave *>(item_fx);
  1154. if (!cn) {
  1155. double value = Math::sin(item_wave->frequency * item_wave->elapsed_time + ((p_ofs.x + off_step.x) / 50)) * (item_wave->amplitude / 10.0f);
  1156. item_wave->prev_off = Point2(0, 1) * value;
  1157. }
  1158. fx_offset += item_wave->prev_off;
  1159. } else if (item_fx->type == ITEM_TORNADO) {
  1160. ItemTornado *item_tornado = static_cast<ItemTornado *>(item_fx);
  1161. if (!cn) {
  1162. double torn_x = Math::sin(item_tornado->frequency * item_tornado->elapsed_time + ((p_ofs.x + off_step.x) / 50)) * (item_tornado->radius);
  1163. double torn_y = Math::cos(item_tornado->frequency * item_tornado->elapsed_time + ((p_ofs.x + off_step.x) / 50)) * (item_tornado->radius);
  1164. item_tornado->prev_off = Point2(torn_x, torn_y);
  1165. }
  1166. fx_offset += item_tornado->prev_off;
  1167. } else if (item_fx->type == ITEM_RAINBOW) {
  1168. ItemRainbow *item_rainbow = static_cast<ItemRainbow *>(item_fx);
  1169. font_color = font_color.from_ok_hsv(MAX(item_rainbow->frequency, 0) * Math::abs(item_rainbow->elapsed_time * item_rainbow->speed + ((p_ofs.x + off_step.x) / 50)), item_rainbow->saturation, item_rainbow->value, font_color.a);
  1170. } else if (item_fx->type == ITEM_PULSE) {
  1171. ItemPulse *item_pulse = static_cast<ItemPulse *>(item_fx);
  1172. const float sined_time = (Math::ease(Math::pingpong(item_pulse->elapsed_time, 1.0 / item_pulse->frequency) * item_pulse->frequency, item_pulse->ease));
  1173. font_color = font_color.lerp(font_color * item_pulse->color, sined_time);
  1174. }
  1175. }
  1176. if (is_inside_tree() && get_viewport()->is_snap_2d_transforms_to_pixel_enabled()) {
  1177. fx_offset = (fx_offset + Point2(0.5, 0.5)).floor();
  1178. }
  1179. Vector2 char_off = char_xform.get_origin();
  1180. Transform2D char_reverse_xform;
  1181. if (step == DRAW_STEP_TEXT) {
  1182. if (selected && use_selected_font_color) {
  1183. font_color = theme_cache.font_selected_color;
  1184. }
  1185. char_reverse_xform.set_origin(-char_off);
  1186. Transform2D char_final_xform = char_xform * char_reverse_xform;
  1187. draw_set_transform_matrix(char_final_xform);
  1188. } else if (step == DRAW_STEP_SHADOW_OUTLINE || step == DRAW_STEP_SHADOW) {
  1189. font_color = font_shadow_color * Color(1, 1, 1, font_color.a);
  1190. char_reverse_xform.set_origin(-char_off - p_shadow_ofs);
  1191. Transform2D char_final_xform = char_xform * char_reverse_xform;
  1192. char_final_xform.columns[2] += p_shadow_ofs;
  1193. draw_set_transform_matrix(char_final_xform);
  1194. } else if (step == DRAW_STEP_OUTLINE) {
  1195. font_color = font_outline_color * Color(1, 1, 1, font_color.a);
  1196. char_reverse_xform.set_origin(-char_off);
  1197. Transform2D char_final_xform = char_xform * char_reverse_xform;
  1198. draw_set_transform_matrix(char_final_xform);
  1199. }
  1200. // Draw glyphs.
  1201. for (int j = 0; j < glyphs[i].repeat; j++) {
  1202. bool skip = (trim_chars && l.char_offset + glyphs[i].end > visible_characters) || (trim_glyphs_ltr && (processed_glyphs_step >= visible_glyphs)) || (trim_glyphs_rtl && (processed_glyphs_step < total_glyphs - visible_glyphs));
  1203. if (!skip) {
  1204. if (txt_visible) {
  1205. has_visible_chars = true;
  1206. if (step == DRAW_STEP_TEXT) {
  1207. if (frid != RID()) {
  1208. TS->font_draw_glyph(frid, ci, glyphs[i].font_size, fx_offset + char_off, gl, font_color);
  1209. } else if (((glyphs[i].flags & TextServer::GRAPHEME_IS_VIRTUAL) != TextServer::GRAPHEME_IS_VIRTUAL) && ((glyphs[i].flags & TextServer::GRAPHEME_IS_EMBEDDED_OBJECT) != TextServer::GRAPHEME_IS_EMBEDDED_OBJECT)) {
  1210. TS->draw_hex_code_box(ci, glyphs[i].font_size, fx_offset + char_off, gl, font_color);
  1211. }
  1212. } else if (step == DRAW_STEP_SHADOW_OUTLINE && frid != RID()) {
  1213. TS->font_draw_glyph_outline(frid, ci, glyphs[i].font_size, p_shadow_outline_size, fx_offset + char_off + p_shadow_ofs, gl, font_color);
  1214. } else if (step == DRAW_STEP_SHADOW && frid != RID()) {
  1215. TS->font_draw_glyph(frid, ci, glyphs[i].font_size, fx_offset + char_off + p_shadow_ofs, gl, font_color);
  1216. } else if (step == DRAW_STEP_OUTLINE && frid != RID()) {
  1217. TS->font_draw_glyph_outline(frid, ci, glyphs[i].font_size, outline_size, fx_offset + char_off, gl, font_color);
  1218. }
  1219. }
  1220. }
  1221. processed_glyphs_step++;
  1222. if (step == DRAW_STEP_TEXT && skip) {
  1223. // Finish underline/overline/strikethrough is previous glyph is skipped.
  1224. if (ul_started) {
  1225. ul_started = false;
  1226. float y_off = upos;
  1227. float underline_width = MAX(1.0, uth * theme_cache.base_scale);
  1228. draw_line(ul_start + Vector2(0, y_off), p_ofs + Vector2(off_step.x, off_step.y + y_off), ul_color, underline_width);
  1229. }
  1230. if (dot_ul_started) {
  1231. dot_ul_started = false;
  1232. float y_off = upos;
  1233. float underline_width = MAX(1.0, uth * theme_cache.base_scale);
  1234. draw_dashed_line(dot_ul_start + Vector2(0, y_off), p_ofs + Vector2(off_step.x, off_step.y + y_off), dot_ul_color, underline_width, MAX(2.0, underline_width * 2));
  1235. }
  1236. if (st_started) {
  1237. st_started = false;
  1238. float y_off = -l_ascent + l_size.y / 2;
  1239. float underline_width = MAX(1.0, uth * theme_cache.base_scale);
  1240. draw_line(st_start + Vector2(0, y_off), p_ofs + Vector2(off_step.x, off_step.y + y_off), st_color, underline_width);
  1241. }
  1242. }
  1243. off_step.x += glyphs[i].advance;
  1244. }
  1245. draw_set_transform_matrix(Transform2D());
  1246. }
  1247. // Draw boxes.
  1248. if (step == DRAW_STEP_BACKGROUND || step == DRAW_STEP_FOREGROUND) {
  1249. for (int j = 0; j < glyphs[i].repeat; j++) {
  1250. bool skip = (trim_chars && l.char_offset + glyphs[i].end > visible_characters) || (trim_glyphs_ltr && (processed_glyphs_step >= visible_glyphs)) || (trim_glyphs_rtl && (processed_glyphs_step < total_glyphs - visible_glyphs));
  1251. if (!skip) {
  1252. Color color;
  1253. if (step == DRAW_STEP_BACKGROUND) {
  1254. color = _find_bgcolor(it);
  1255. } else if (step == DRAW_STEP_FOREGROUND) {
  1256. color = _find_fgcolor(it);
  1257. }
  1258. if (color != last_color) {
  1259. if (last_color.a > 0.0) {
  1260. Vector2 rect_off = p_ofs + Vector2(box_start - theme_cache.text_highlight_h_padding, off_step.y - l_ascent - theme_cache.text_highlight_v_padding);
  1261. Vector2 rect_size = Vector2(off_step.x - box_start + 2 * theme_cache.text_highlight_h_padding, l_size.y + 2 * theme_cache.text_highlight_v_padding);
  1262. RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(rect_off, rect_size), last_color);
  1263. }
  1264. if (color.a > 0.0) {
  1265. box_start = off_step.x;
  1266. }
  1267. }
  1268. last_color = color;
  1269. } else {
  1270. // Finish box is previous glyph is skipped.
  1271. if (last_color.a > 0.0) {
  1272. Vector2 rect_off = p_ofs + Vector2(box_start - theme_cache.text_highlight_h_padding, off_step.y - l_ascent - theme_cache.text_highlight_v_padding);
  1273. Vector2 rect_size = Vector2(off_step.x - box_start + 2 * theme_cache.text_highlight_h_padding, l_size.y + 2 * theme_cache.text_highlight_v_padding);
  1274. RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(rect_off, rect_size), last_color);
  1275. }
  1276. last_color = Color(0, 0, 0, 0);
  1277. }
  1278. processed_glyphs_step++;
  1279. off_step.x += glyphs[i].advance;
  1280. }
  1281. }
  1282. }
  1283. // Finish lines and boxes.
  1284. if (step == DRAW_STEP_BACKGROUND || step == DRAW_STEP_FOREGROUND) {
  1285. if (last_color.a > 0.0) {
  1286. Vector2 rect_off = p_ofs + Vector2(box_start - theme_cache.text_highlight_h_padding, off_step.y - l_ascent - theme_cache.text_highlight_v_padding);
  1287. Vector2 rect_size = Vector2(off_step.x - box_start + 2 * theme_cache.text_highlight_h_padding, l_size.y + 2 * theme_cache.text_highlight_v_padding);
  1288. RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(rect_off, rect_size), last_color);
  1289. }
  1290. }
  1291. if (step == DRAW_STEP_BACKGROUND) {
  1292. if (sel_start != -1) {
  1293. Color selection_bg = theme_cache.selection_color;
  1294. Vector<Vector2> sel = TS->shaped_text_get_selection(rid, sel_start, sel_end);
  1295. for (int i = 0; i < sel.size(); i++) {
  1296. Rect2 rect = Rect2(sel[i].x + p_ofs.x + off.x, p_ofs.y + off.y - l_ascent, sel[i].y - sel[i].x, l_size.y); // Note: use "off" not "off_step", selection is relative to the line start.
  1297. RenderingServer::get_singleton()->canvas_item_add_rect(ci, rect, selection_bg);
  1298. }
  1299. }
  1300. }
  1301. if (step == DRAW_STEP_TEXT) {
  1302. if (ul_started) {
  1303. ul_started = false;
  1304. float y_off = upos;
  1305. float underline_width = MAX(1.0, uth * theme_cache.base_scale);
  1306. draw_line(ul_start + Vector2(0, y_off), p_ofs + Vector2(off_step.x, off_step.y + y_off), ul_color, underline_width);
  1307. }
  1308. if (dot_ul_started) {
  1309. dot_ul_started = false;
  1310. float y_off = upos;
  1311. float underline_width = MAX(1.0, uth * theme_cache.base_scale);
  1312. draw_dashed_line(dot_ul_start + Vector2(0, y_off), p_ofs + Vector2(off_step.x, off_step.y + y_off), dot_ul_color, underline_width, MAX(2.0, underline_width * 2));
  1313. }
  1314. if (st_started) {
  1315. st_started = false;
  1316. float y_off = -l_ascent + l_size.y / 2;
  1317. float underline_width = MAX(1.0, uth * theme_cache.base_scale);
  1318. draw_line(st_start + Vector2(0, y_off), p_ofs + Vector2(off_step.x, off_step.y + y_off), st_color, underline_width);
  1319. }
  1320. }
  1321. }
  1322. r_processed_glyphs = processed_glyphs_step;
  1323. off.y += TS->shaped_text_get_descent(rid);
  1324. if (has_visible_chars) {
  1325. line_count++;
  1326. has_visible_chars = false;
  1327. }
  1328. }
  1329. return line_count;
  1330. }
  1331. 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) {
  1332. if (r_click_item) {
  1333. *r_click_item = nullptr;
  1334. }
  1335. if (r_click_char != nullptr) {
  1336. *r_click_char = 0;
  1337. }
  1338. if (r_outside != nullptr) {
  1339. *r_outside = true;
  1340. }
  1341. Size2 size = get_size();
  1342. Rect2 text_rect = _get_text_rect();
  1343. int vofs = vscroll->get_value();
  1344. // Search for the first line.
  1345. int to_line = main->first_invalid_line.load();
  1346. int from_line = _find_first_line(0, to_line, vofs);
  1347. int total_height = INT32_MAX;
  1348. if (to_line && vertical_alignment != VERTICAL_ALIGNMENT_TOP) {
  1349. MutexLock lock(main->lines[to_line - 1].text_buf->get_mutex());
  1350. if (theme_cache.line_separation < 0) {
  1351. // Do not apply to the last line to avoid cutting text.
  1352. 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;
  1353. } else {
  1354. 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;
  1355. }
  1356. }
  1357. float vbegin = 0, vsep = 0;
  1358. if (text_rect.size.y > total_height) {
  1359. switch (vertical_alignment) {
  1360. case VERTICAL_ALIGNMENT_TOP: {
  1361. // Nothing.
  1362. } break;
  1363. case VERTICAL_ALIGNMENT_CENTER: {
  1364. vbegin = (text_rect.size.y - total_height) / 2;
  1365. } break;
  1366. case VERTICAL_ALIGNMENT_BOTTOM: {
  1367. vbegin = text_rect.size.y - total_height;
  1368. } break;
  1369. case VERTICAL_ALIGNMENT_FILL: {
  1370. int lines = 0;
  1371. for (int l = from_line; l < to_line; l++) {
  1372. MutexLock lock(main->lines[l].text_buf->get_mutex());
  1373. lines += main->lines[l].text_buf->get_line_count();
  1374. }
  1375. if (lines > 1) {
  1376. vsep = (text_rect.size.y - total_height) / (lines - 1);
  1377. }
  1378. } break;
  1379. }
  1380. }
  1381. Point2 ofs = text_rect.get_position() + Vector2(0, vbegin + main->lines[from_line].offset.y - vofs);
  1382. while (ofs.y < size.height && from_line < to_line) {
  1383. MutexLock lock(main->lines[from_line].text_buf->get_mutex());
  1384. _find_click_in_line(p_frame, from_line, ofs, text_rect.size.x, vsep, p_click, r_click_frame, r_click_line, r_click_item, r_click_char, false, p_meta);
  1385. ofs.y += main->lines[from_line].text_buf->get_size().y + main->lines[from_line].text_buf->get_line_count() * (theme_cache.line_separation + vsep);
  1386. if (((r_click_item != nullptr) && ((*r_click_item) != nullptr)) || ((r_click_frame != nullptr) && ((*r_click_frame) != nullptr))) {
  1387. if (r_outside != nullptr) {
  1388. *r_outside = false;
  1389. }
  1390. return;
  1391. }
  1392. from_line++;
  1393. }
  1394. }
  1395. float RichTextLabel::_find_click_in_line(ItemFrame *p_frame, int p_line, const Vector2 &p_ofs, int p_width, float p_vsep, 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) {
  1396. Vector2 off;
  1397. bool line_clicked = false;
  1398. float text_rect_begin = 0.0;
  1399. int char_pos = -1;
  1400. bool char_clicked = false;
  1401. Line &l = p_frame->lines[p_line];
  1402. MutexLock lock(l.text_buf->get_mutex());
  1403. bool rtl = (l.text_buf->get_direction() == TextServer::DIRECTION_RTL);
  1404. bool lrtl = is_layout_rtl();
  1405. // Table hit test results.
  1406. bool table_hit = false;
  1407. Vector2i table_range;
  1408. float table_offy = 0.f;
  1409. ItemFrame *table_click_frame = nullptr;
  1410. int table_click_line = -1;
  1411. Item *table_click_item = nullptr;
  1412. int table_click_char = -1;
  1413. for (int line = 0; line < l.text_buf->get_line_count(); line++) {
  1414. RID rid = l.text_buf->get_line_rid(line);
  1415. float width = l.text_buf->get_width();
  1416. float length = TS->shaped_text_get_width(rid);
  1417. if (rtl) {
  1418. off.x = p_width - l.offset.x - width;
  1419. if (!lrtl && p_frame == main) { // Skip Scrollbar.
  1420. off.x -= scroll_w;
  1421. }
  1422. } else {
  1423. off.x = l.offset.x;
  1424. if (lrtl && p_frame == main) { // Skip Scrollbar.
  1425. off.x += scroll_w;
  1426. }
  1427. }
  1428. switch (l.text_buf->get_alignment()) {
  1429. case HORIZONTAL_ALIGNMENT_FILL:
  1430. case HORIZONTAL_ALIGNMENT_LEFT: {
  1431. if (rtl) {
  1432. off.x += width - length;
  1433. }
  1434. } break;
  1435. case HORIZONTAL_ALIGNMENT_CENTER: {
  1436. off.x += Math::floor((width - length) / 2.0);
  1437. } break;
  1438. case HORIZONTAL_ALIGNMENT_RIGHT: {
  1439. if (!rtl) {
  1440. off.x += width - length;
  1441. }
  1442. } break;
  1443. }
  1444. // Adjust for dropcap.
  1445. int dc_lines = l.text_buf->get_dropcap_lines();
  1446. float h_off = l.text_buf->get_dropcap_size().x;
  1447. if (line <= dc_lines) {
  1448. if (rtl) {
  1449. off.x -= h_off;
  1450. } else {
  1451. off.x += h_off;
  1452. }
  1453. }
  1454. off.y += TS->shaped_text_get_ascent(rid);
  1455. Array objects = TS->shaped_text_get_objects(rid);
  1456. for (int i = 0; i < objects.size(); i++) {
  1457. Item *it = items.get_or_null(objects[i]);
  1458. if (it != nullptr) {
  1459. Rect2 rect = TS->shaped_text_get_object_rect(rid, objects[i]);
  1460. rect.position += p_ofs + off;
  1461. if (p_click.y >= rect.position.y && p_click.y <= rect.position.y + rect.size.y) {
  1462. switch (it->type) {
  1463. case ITEM_TABLE: {
  1464. ItemTable *table = static_cast<ItemTable *>(it);
  1465. int idx = 0;
  1466. int col_count = table->columns.size();
  1467. int row_count = table->rows.size();
  1468. for (Item *E : table->subitems) {
  1469. ItemFrame *frame = static_cast<ItemFrame *>(E);
  1470. int col = idx % col_count;
  1471. int row = idx / col_count;
  1472. if (frame->lines.size() != 0 && row < row_count) {
  1473. Vector2 coff = frame->lines[0].offset;
  1474. if (rtl) {
  1475. coff.x = rect.size.width - table->columns[col].width - coff.x;
  1476. }
  1477. Rect2 crect = Rect2(rect.position + coff - frame->padding.position - Vector2(theme_cache.table_h_separation * 0.5, theme_cache.table_h_separation * 0.5).floor(), Size2(table->columns[col].width + theme_cache.table_h_separation, table->rows[row] + theme_cache.table_v_separation) + frame->padding.position + frame->padding.size);
  1478. if (col == col_count - 1) {
  1479. if (rtl) {
  1480. crect.size.x = crect.position.x + crect.size.x;
  1481. crect.position.x = 0;
  1482. } else {
  1483. crect.size.x = get_size().x;
  1484. }
  1485. }
  1486. if (crect.has_point(p_click)) {
  1487. for (int j = 0; j < (int)frame->lines.size(); j++) {
  1488. _find_click_in_line(frame, j, rect.position + Vector2(frame->padding.position.x, frame->lines[j].offset.y), rect.size.x, 0, p_click, &table_click_frame, &table_click_line, &table_click_item, &table_click_char, true, p_meta);
  1489. if (table_click_frame && table_click_item) {
  1490. // Save cell detected cell hit data.
  1491. table_range = Vector2i(INT32_MAX, 0);
  1492. for (Item *F : table->subitems) {
  1493. ItemFrame *sub_frame = static_cast<ItemFrame *>(F);
  1494. for (int k = 0; k < (int)sub_frame->lines.size(); k++) {
  1495. table_range.x = MIN(table_range.x, sub_frame->lines[k].char_offset);
  1496. table_range.y = MAX(table_range.y, sub_frame->lines[k].char_offset + sub_frame->lines[k].char_count);
  1497. }
  1498. }
  1499. table_offy = off.y;
  1500. table_hit = true;
  1501. }
  1502. }
  1503. }
  1504. }
  1505. idx++;
  1506. }
  1507. } break;
  1508. default:
  1509. break;
  1510. }
  1511. }
  1512. }
  1513. }
  1514. 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);
  1515. if (p_table) {
  1516. rect.size.y += theme_cache.table_v_separation;
  1517. }
  1518. if (p_click.y >= rect.position.y && p_click.y <= rect.position.y + rect.size.y) {
  1519. if (!p_meta) {
  1520. char_pos = rtl ? TS->shaped_text_get_range(rid).y : TS->shaped_text_get_range(rid).x;
  1521. }
  1522. if ((!rtl && p_click.x >= rect.position.x) || (rtl && p_click.x <= rect.position.x + rect.size.x)) {
  1523. if (p_meta) {
  1524. int64_t glyph_idx = TS->shaped_text_hit_test_grapheme(rid, p_click.x - rect.position.x);
  1525. if (glyph_idx >= 0) {
  1526. float baseline_y = rect.position.y + TS->shaped_text_get_ascent(rid);
  1527. const Glyph *glyphs = TS->shaped_text_get_glyphs(rid);
  1528. if (glyphs[glyph_idx].flags & TextServer::GRAPHEME_IS_EMBEDDED_OBJECT) {
  1529. // Emebedded object.
  1530. for (int i = 0; i < objects.size(); i++) {
  1531. if (TS->shaped_text_get_object_glyph(rid, objects[i]) == glyph_idx) {
  1532. Rect2 obj_rect = TS->shaped_text_get_object_rect(rid, objects[i]);
  1533. obj_rect.position.y += baseline_y;
  1534. if (p_click.y >= obj_rect.position.y && p_click.y <= obj_rect.position.y + obj_rect.size.y) {
  1535. char_pos = glyphs[glyph_idx].start;
  1536. char_clicked = true;
  1537. }
  1538. break;
  1539. }
  1540. }
  1541. } else if (glyphs[glyph_idx].font_rid != RID()) {
  1542. // Normal glyph.
  1543. float fa = TS->font_get_ascent(glyphs[glyph_idx].font_rid, glyphs[glyph_idx].font_size);
  1544. float fd = TS->font_get_descent(glyphs[glyph_idx].font_rid, glyphs[glyph_idx].font_size);
  1545. if (p_click.y >= baseline_y - fa && p_click.y <= baseline_y + fd) {
  1546. char_pos = glyphs[glyph_idx].start;
  1547. char_clicked = true;
  1548. }
  1549. } else if (!(glyphs[glyph_idx].flags & TextServer::GRAPHEME_IS_VIRTUAL)) {
  1550. // Hex code box.
  1551. Vector2 gl_size = TS->get_hex_code_box_size(glyphs[glyph_idx].font_size, glyphs[glyph_idx].index);
  1552. if (p_click.y >= baseline_y - gl_size.y * 0.85 && p_click.y <= baseline_y + gl_size.y * 0.15) {
  1553. char_pos = glyphs[glyph_idx].start;
  1554. char_clicked = true;
  1555. }
  1556. }
  1557. }
  1558. } else {
  1559. char_pos = TS->shaped_text_hit_test_position(rid, p_click.x - rect.position.x);
  1560. char_pos = TS->shaped_text_closest_character_pos(rid, char_pos);
  1561. char_clicked = true;
  1562. }
  1563. }
  1564. line_clicked = true;
  1565. text_rect_begin = rtl ? rect.position.x + rect.size.x : rect.position.x;
  1566. }
  1567. // If table hit was detected, and line hit is in the table bounds use table hit.
  1568. 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_clicked)) {
  1569. if (r_click_frame != nullptr) {
  1570. *r_click_frame = table_click_frame;
  1571. }
  1572. if (r_click_line != nullptr) {
  1573. *r_click_line = table_click_line;
  1574. }
  1575. if (r_click_item != nullptr) {
  1576. *r_click_item = table_click_item;
  1577. }
  1578. if (r_click_char != nullptr) {
  1579. *r_click_char = table_click_char;
  1580. }
  1581. return table_offy;
  1582. }
  1583. off.y += TS->shaped_text_get_descent(rid) + theme_cache.line_separation + p_vsep;
  1584. }
  1585. // Text line hit.
  1586. if (line_clicked) {
  1587. // Find item.
  1588. if (r_click_item != nullptr) {
  1589. Item *it = p_frame->lines[p_line].from;
  1590. Item *it_to = (p_line + 1 < (int)p_frame->lines.size()) ? p_frame->lines[p_line + 1].from : nullptr;
  1591. if (char_pos >= 0) {
  1592. *r_click_item = _get_item_at_pos(it, it_to, char_pos);
  1593. } else {
  1594. int stop = text_rect_begin;
  1595. *r_click_item = _find_indentable(it);
  1596. while (*r_click_item) {
  1597. Ref<Font> font = theme_cache.normal_font;
  1598. int font_size = theme_cache.normal_font_size;
  1599. ItemFont *font_it = _find_font(*r_click_item);
  1600. if (font_it) {
  1601. if (font_it->font.is_valid()) {
  1602. font = font_it->font;
  1603. }
  1604. if (font_it->font_size > 0) {
  1605. font_size = font_it->font_size;
  1606. }
  1607. }
  1608. ItemFontSize *font_size_it = _find_font_size(*r_click_item);
  1609. if (font_size_it && font_size_it->font_size > 0) {
  1610. font_size = font_size_it->font_size;
  1611. }
  1612. if (rtl) {
  1613. stop += tab_size * font->get_char_size(' ', font_size).width;
  1614. if (stop > p_click.x) {
  1615. break;
  1616. }
  1617. } else {
  1618. stop -= tab_size * font->get_char_size(' ', font_size).width;
  1619. if (stop < p_click.x) {
  1620. break;
  1621. }
  1622. }
  1623. *r_click_item = _find_indentable((*r_click_item)->parent);
  1624. }
  1625. }
  1626. }
  1627. if (r_click_frame != nullptr) {
  1628. *r_click_frame = p_frame;
  1629. }
  1630. if (r_click_line != nullptr) {
  1631. *r_click_line = p_line;
  1632. }
  1633. if (r_click_char != nullptr) {
  1634. *r_click_char = char_pos;
  1635. }
  1636. }
  1637. return off.y;
  1638. }
  1639. void RichTextLabel::_scroll_changed(double) {
  1640. if (updating_scroll) {
  1641. return;
  1642. }
  1643. if (scroll_follow && vscroll->get_value() > (vscroll->get_max() - vscroll->get_page() - 1)) {
  1644. scroll_following = true;
  1645. } else {
  1646. scroll_following = false;
  1647. }
  1648. scroll_updated = true;
  1649. queue_redraw();
  1650. }
  1651. void RichTextLabel::_update_fx(RichTextLabel::ItemFrame *p_frame, double p_delta_time) {
  1652. Item *it = p_frame;
  1653. while (it) {
  1654. ItemFX *ifx = nullptr;
  1655. if (it->type == ITEM_CUSTOMFX || it->type == ITEM_SHAKE || it->type == ITEM_WAVE || it->type == ITEM_TORNADO || it->type == ITEM_RAINBOW || it->type == ITEM_PULSE) {
  1656. ifx = static_cast<ItemFX *>(it);
  1657. }
  1658. if (!ifx) {
  1659. it = _get_next_item(it, true);
  1660. continue;
  1661. }
  1662. ifx->elapsed_time += p_delta_time;
  1663. ItemShake *shake = nullptr;
  1664. if (it->type == ITEM_SHAKE) {
  1665. shake = static_cast<ItemShake *>(it);
  1666. }
  1667. if (shake) {
  1668. bool cycle = (shake->elapsed_time > (1.0f / shake->rate));
  1669. if (cycle) {
  1670. shake->elapsed_time -= (1.0f / shake->rate);
  1671. shake->reroll_random();
  1672. }
  1673. }
  1674. it = _get_next_item(it, true);
  1675. }
  1676. }
  1677. int RichTextLabel::_find_first_line(int p_from, int p_to, int p_vofs) const {
  1678. int l = p_from;
  1679. int r = p_to;
  1680. while (l < r) {
  1681. int m = Math::floor(double(l + r) / 2.0);
  1682. MutexLock lock(main->lines[m].text_buf->get_mutex());
  1683. int ofs = _calculate_line_vertical_offset(main->lines[m]);
  1684. if (ofs < p_vofs) {
  1685. l = m + 1;
  1686. } else {
  1687. r = m;
  1688. }
  1689. }
  1690. return MIN(l, (int)main->lines.size() - 1);
  1691. }
  1692. _FORCE_INLINE_ float RichTextLabel::_calculate_line_vertical_offset(const RichTextLabel::Line &line) const {
  1693. return line.get_height(theme_cache.line_separation);
  1694. }
  1695. void RichTextLabel::_update_theme_item_cache() {
  1696. Control::_update_theme_item_cache();
  1697. theme_cache.base_scale = get_theme_default_base_scale();
  1698. use_selected_font_color = theme_cache.font_selected_color != Color(0, 0, 0, 0);
  1699. }
  1700. PackedStringArray RichTextLabel::get_accessibility_configuration_warnings() const {
  1701. PackedStringArray warnings = Control::get_accessibility_configuration_warnings();
  1702. Item *it = main;
  1703. while (it) {
  1704. if (it->type == ITEM_IMAGE) {
  1705. ItemImage *img = static_cast<ItemImage *>(it);
  1706. if (img && img->alt_text.strip_edges().is_empty()) {
  1707. warnings.push_back(RTR("Image alternative text must not be empty."));
  1708. }
  1709. }
  1710. it = _get_next_item(it, true);
  1711. }
  1712. return warnings;
  1713. }
  1714. void RichTextLabel::_accessibility_update_line(RID p_id, ItemFrame *p_frame, int p_line, const Vector2 &p_ofs, int p_width, float p_vsep) {
  1715. ERR_FAIL_NULL(p_frame);
  1716. ERR_FAIL_COND(p_line < 0 || p_line >= (int)p_frame->lines.size());
  1717. Line &l = p_frame->lines[p_line];
  1718. if (l.accessibility_line_element.is_valid()) {
  1719. return;
  1720. }
  1721. l.accessibility_line_element = DisplayServer::get_singleton()->accessibility_create_sub_element(p_id, DisplayServer::AccessibilityRole::ROLE_CONTAINER);
  1722. MutexLock lock(l.text_buf->get_mutex());
  1723. const RID &line_ae = l.accessibility_line_element;
  1724. Rect2 ae_rect = Rect2(p_ofs, Size2(p_width, l.text_buf->get_size().y + l.text_buf->get_line_count() * theme_cache.line_separation));
  1725. DisplayServer::get_singleton()->accessibility_update_set_bounds(line_ae, ae_rect);
  1726. ac_element_bounds_cache[line_ae] = ae_rect;
  1727. Item *it_from = l.from;
  1728. if (it_from == nullptr) {
  1729. return;
  1730. }
  1731. bool rtl = (l.text_buf->get_direction() == TextServer::DIRECTION_RTL);
  1732. bool lrtl = is_layout_rtl();
  1733. // Process dropcap.
  1734. int dc_lines = l.text_buf->get_dropcap_lines();
  1735. float h_off = l.text_buf->get_dropcap_size().x;
  1736. // Process text.
  1737. const RID &para_rid = l.text_buf->get_rid();
  1738. String l_text = TS->shaped_get_text(para_rid).remove_char(0xfffc).strip_edges();
  1739. if (l.dc_item) {
  1740. ItemDropcap *dc = static_cast<ItemDropcap *>(l.dc_item);
  1741. l_text = dc->text + l_text;
  1742. }
  1743. if (!l_text.is_empty()) {
  1744. Vector2 off;
  1745. if (rtl) {
  1746. off.x = p_width - l.offset.x - l.text_buf->get_width();
  1747. if (!lrtl && p_frame == main) { // Skip Scrollbar.
  1748. off.x -= scroll_w;
  1749. }
  1750. } else {
  1751. off.x = l.offset.x;
  1752. if (lrtl && p_frame == main) { // Skip Scrollbar.
  1753. off.x += scroll_w;
  1754. }
  1755. }
  1756. l.accessibility_text_element = DisplayServer::get_singleton()->accessibility_create_sub_element(line_ae, DisplayServer::AccessibilityRole::ROLE_STATIC_TEXT);
  1757. DisplayServer::get_singleton()->accessibility_update_set_value(l.accessibility_text_element, l_text);
  1758. ae_rect = Rect2(p_ofs + off, l.text_buf->get_size());
  1759. DisplayServer::get_singleton()->accessibility_update_set_bounds(l.accessibility_text_element, ae_rect);
  1760. ac_element_bounds_cache[l.accessibility_text_element] = ae_rect;
  1761. DisplayServer::get_singleton()->accessibility_update_add_action(l.accessibility_text_element, DisplayServer::AccessibilityAction::ACTION_FOCUS, callable_mp(this, &RichTextLabel::_accessibility_focus_item).bind((uint64_t)l.from, true, true));
  1762. DisplayServer::get_singleton()->accessibility_update_add_action(l.accessibility_text_element, DisplayServer::AccessibilityAction::ACTION_BLUR, callable_mp(this, &RichTextLabel::_accessibility_focus_item).bind((uint64_t)l.from, true, false));
  1763. DisplayServer::get_singleton()->accessibility_update_add_action(l.accessibility_text_element, DisplayServer::AccessibilityAction::ACTION_SCROLL_INTO_VIEW, callable_mp(this, &RichTextLabel::_accessibility_scroll_to_item).bind((uint64_t)l.from));
  1764. }
  1765. Vector2 off;
  1766. for (int line = 0; line < l.text_buf->get_line_count(); line++) {
  1767. if (line > 0) {
  1768. off.y += (theme_cache.line_separation + p_vsep);
  1769. }
  1770. const Size2 line_size = l.text_buf->get_line_size(line);
  1771. float width = l.text_buf->get_width();
  1772. float length = line_size.x;
  1773. // Process line.
  1774. if (rtl) {
  1775. off.x = p_width - l.offset.x - width;
  1776. if (!lrtl && p_frame == main) { // Skip Scrollbar.
  1777. off.x -= scroll_w;
  1778. }
  1779. } else {
  1780. off.x = l.offset.x;
  1781. if (lrtl && p_frame == main) { // Skip Scrollbar.
  1782. off.x += scroll_w;
  1783. }
  1784. }
  1785. // Process text.
  1786. switch (l.text_buf->get_alignment()) {
  1787. case HORIZONTAL_ALIGNMENT_FILL:
  1788. case HORIZONTAL_ALIGNMENT_LEFT: {
  1789. if (rtl) {
  1790. off.x += width - length;
  1791. }
  1792. } break;
  1793. case HORIZONTAL_ALIGNMENT_CENTER: {
  1794. off.x += Math::floor((width - length) / 2.0);
  1795. } break;
  1796. case HORIZONTAL_ALIGNMENT_RIGHT: {
  1797. if (!rtl) {
  1798. off.x += width - length;
  1799. }
  1800. } break;
  1801. }
  1802. if (line <= dc_lines) {
  1803. if (rtl) {
  1804. off.x -= h_off;
  1805. } else {
  1806. off.x += h_off;
  1807. }
  1808. }
  1809. const RID &rid = l.text_buf->get_line_rid(line);
  1810. Array objects = TS->shaped_text_get_objects(rid);
  1811. for (int i = 0; i < objects.size(); i++) {
  1812. Item *it = reinterpret_cast<Item *>((uint64_t)objects[i]);
  1813. if (it != nullptr) {
  1814. Rect2 rect = TS->shaped_text_get_object_rect(rid, objects[i]);
  1815. switch (it->type) {
  1816. case ITEM_IMAGE: {
  1817. ItemImage *img = static_cast<ItemImage *>(it);
  1818. RID img_ae = DisplayServer::get_singleton()->accessibility_create_sub_element(line_ae, DisplayServer::AccessibilityRole::ROLE_IMAGE);
  1819. DisplayServer::get_singleton()->accessibility_update_set_name(img_ae, img->alt_text);
  1820. if (img->pad) {
  1821. Size2 pad_size = rect.size.min(img->image->get_size());
  1822. Vector2 pad_off = (rect.size - pad_size) / 2;
  1823. ae_rect = Rect2(p_ofs + rect.position + off + pad_off, pad_size);
  1824. } else {
  1825. ae_rect = Rect2(p_ofs + rect.position + off, rect.size);
  1826. }
  1827. DisplayServer::get_singleton()->accessibility_update_set_bounds(img_ae, ae_rect);
  1828. ac_element_bounds_cache[img_ae] = ae_rect;
  1829. DisplayServer::get_singleton()->accessibility_update_add_action(img_ae, DisplayServer::AccessibilityAction::ACTION_FOCUS, callable_mp(this, &RichTextLabel::_accessibility_focus_item).bind((uint64_t)it, false, true));
  1830. DisplayServer::get_singleton()->accessibility_update_add_action(img_ae, DisplayServer::AccessibilityAction::ACTION_BLUR, callable_mp(this, &RichTextLabel::_accessibility_focus_item).bind((uint64_t)it, false, false));
  1831. DisplayServer::get_singleton()->accessibility_update_add_action(img_ae, DisplayServer::AccessibilityAction::ACTION_SCROLL_INTO_VIEW, callable_mp(this, &RichTextLabel::_accessibility_scroll_to_item).bind((uint64_t)it));
  1832. it->accessibility_item_element = img_ae;
  1833. } break;
  1834. case ITEM_TABLE: {
  1835. ItemTable *table = static_cast<ItemTable *>(it);
  1836. float h_separation = theme_cache.table_h_separation;
  1837. float v_separation = theme_cache.table_v_separation;
  1838. RID table_ae = DisplayServer::get_singleton()->accessibility_create_sub_element(line_ae, DisplayServer::AccessibilityRole::ROLE_TABLE);
  1839. int col_count = table->columns.size();
  1840. int row_count = table->rows.size();
  1841. DisplayServer::get_singleton()->accessibility_update_set_name(table_ae, table->name);
  1842. DisplayServer::get_singleton()->accessibility_update_set_role(table_ae, DisplayServer::AccessibilityRole::ROLE_TABLE);
  1843. DisplayServer::get_singleton()->accessibility_update_set_table_column_count(table_ae, col_count);
  1844. DisplayServer::get_singleton()->accessibility_update_set_table_row_count(table_ae, row_count);
  1845. ae_rect = Rect2(p_ofs + rect.position + off + Vector2(0, TS->shaped_text_get_ascent(rid)), rect.size);
  1846. DisplayServer::get_singleton()->accessibility_update_set_bounds(table_ae, ae_rect);
  1847. ac_element_bounds_cache[table_ae] = ae_rect;
  1848. DisplayServer::get_singleton()->accessibility_update_add_action(table_ae, DisplayServer::AccessibilityAction::ACTION_FOCUS, callable_mp(this, &RichTextLabel::_accessibility_focus_item).bind((uint64_t)it, false, true));
  1849. DisplayServer::get_singleton()->accessibility_update_add_action(table_ae, DisplayServer::AccessibilityAction::ACTION_BLUR, callable_mp(this, &RichTextLabel::_accessibility_focus_item).bind((uint64_t)it, false, false));
  1850. DisplayServer::get_singleton()->accessibility_update_add_action(table_ae, DisplayServer::AccessibilityAction::ACTION_SCROLL_INTO_VIEW, callable_mp(this, &RichTextLabel::_accessibility_scroll_to_item).bind((uint64_t)it));
  1851. Vector<RID> row_aes;
  1852. Vector2 row_off = Vector2(0, TS->shaped_text_get_ascent(rid));
  1853. for (int j = 0; j < row_count; j++) {
  1854. RID row_ae = DisplayServer::get_singleton()->accessibility_create_sub_element(table_ae, DisplayServer::AccessibilityRole::ROLE_ROW);
  1855. DisplayServer::get_singleton()->accessibility_update_set_table_row_index(row_ae, j);
  1856. ae_rect = Rect2(p_ofs + rect.position + off + row_off, Size2(rect.size.x, table->rows[j]));
  1857. DisplayServer::get_singleton()->accessibility_update_set_bounds(row_ae, ae_rect);
  1858. ac_element_bounds_cache[row_ae] = ae_rect;
  1859. row_off.y += table->rows[j];
  1860. row_aes.push_back(row_ae);
  1861. }
  1862. int idx = 0;
  1863. for (Item *E : table->subitems) {
  1864. ItemFrame *frame = static_cast<ItemFrame *>(E);
  1865. int col = idx % col_count;
  1866. int row = idx / col_count;
  1867. for (int j = 0; j < (int)frame->lines.size(); j++) {
  1868. RID cell_ae = DisplayServer::get_singleton()->accessibility_create_sub_element(row_aes[row], DisplayServer::AccessibilityRole::ROLE_CELL);
  1869. if (frame->lines.size() != 0 && row < row_count) {
  1870. Vector2 coff = frame->lines[0].offset;
  1871. coff.x -= frame->lines[0].indent;
  1872. if (rtl) {
  1873. coff.x = rect.size.width - table->columns[col].width - coff.x;
  1874. }
  1875. ae_rect = Rect2(p_ofs + rect.position + off + coff - frame->padding.position - Vector2(h_separation * 0.5, v_separation * 0.5).floor(), Size2(table->columns[col].width + h_separation + frame->padding.position.x + frame->padding.size.x, table->rows[row]));
  1876. DisplayServer::get_singleton()->accessibility_update_set_bounds(cell_ae, ae_rect);
  1877. ac_element_bounds_cache[cell_ae] = ae_rect;
  1878. }
  1879. DisplayServer::get_singleton()->accessibility_update_set_table_cell_position(cell_ae, row, col);
  1880. _accessibility_update_line(cell_ae, frame, j, p_ofs + rect.position + off + Vector2(0, frame->lines[j].offset.y), rect.size.x, p_vsep);
  1881. }
  1882. idx++;
  1883. }
  1884. it->accessibility_item_element = table_ae;
  1885. } break;
  1886. default:
  1887. break;
  1888. }
  1889. }
  1890. }
  1891. off.y += TS->shaped_text_get_descent(rid) + TS->shaped_text_get_ascent(rid);
  1892. }
  1893. }
  1894. void RichTextLabel::_accessibility_action_menu(const Variant &p_data) {
  1895. if (context_menu_enabled) {
  1896. _update_context_menu();
  1897. menu->set_position(get_screen_position());
  1898. menu->reset_size();
  1899. menu->popup();
  1900. menu->grab_focus();
  1901. }
  1902. }
  1903. void RichTextLabel::_accessibility_scroll_down(const Variant &p_data) {
  1904. vscroll->set_value(vscroll->get_value() + vscroll->get_page() / 4);
  1905. }
  1906. void RichTextLabel::_accessibility_scroll_up(const Variant &p_data) {
  1907. vscroll->set_value(vscroll->get_value() - vscroll->get_page() / 4);
  1908. }
  1909. void RichTextLabel::_accessibility_scroll_set(const Variant &p_data) {
  1910. const Point2 &pos = p_data;
  1911. vscroll->set_value(pos.y);
  1912. }
  1913. void RichTextLabel::_accessibility_focus_item(const Variant &p_data, uint64_t p_item, bool p_line, bool p_foucs) {
  1914. Item *it = reinterpret_cast<Item *>(p_item);
  1915. if (p_foucs) {
  1916. ItemFrame *f = nullptr;
  1917. _find_frame(it, &f, nullptr);
  1918. if (f && it) {
  1919. keyboard_focus_frame = f;
  1920. keyboard_focus_line = it->line;
  1921. keyboard_focus_item = it;
  1922. keyboard_focus_on_text = p_line;
  1923. }
  1924. } else {
  1925. keyboard_focus_frame = nullptr;
  1926. keyboard_focus_line = 0;
  1927. keyboard_focus_item = nullptr;
  1928. keyboard_focus_on_text = true;
  1929. }
  1930. }
  1931. void RichTextLabel::_accessibility_scroll_to_item(const Variant &p_data, uint64_t p_item) {
  1932. Item *it = reinterpret_cast<Item *>(p_item);
  1933. ItemFrame *f = nullptr;
  1934. _find_frame(it, &f, nullptr);
  1935. if (f && it) {
  1936. vscroll->set_value(f->lines[it->line].offset.y);
  1937. }
  1938. }
  1939. void RichTextLabel::_invalidate_accessibility() {
  1940. if (accessibility_scroll_element.is_null()) {
  1941. return;
  1942. }
  1943. Item *it = main;
  1944. while (it) {
  1945. if (it->type == ITEM_FRAME) {
  1946. ItemFrame *fr = static_cast<ItemFrame *>(it);
  1947. for (size_t i = 0; i < fr->lines.size(); i++) {
  1948. if (fr->lines[i].accessibility_line_element.is_valid()) {
  1949. DisplayServer::get_singleton()->accessibility_free_element(fr->lines[i].accessibility_line_element);
  1950. }
  1951. fr->lines[i].accessibility_line_element = RID();
  1952. fr->lines[i].accessibility_text_element = RID();
  1953. }
  1954. }
  1955. it->accessibility_item_element = RID();
  1956. it = _get_next_item(it, true);
  1957. }
  1958. }
  1959. RID RichTextLabel::get_focused_accessibility_element() const {
  1960. if (keyboard_focus_frame && keyboard_focus_item) {
  1961. if (keyboard_focus_on_text) {
  1962. return keyboard_focus_frame->lines[keyboard_focus_line].accessibility_text_element;
  1963. } else {
  1964. if (keyboard_focus_item->accessibility_item_element.is_valid()) {
  1965. return keyboard_focus_item->accessibility_item_element;
  1966. }
  1967. }
  1968. } else {
  1969. if (!main->lines.is_empty()) {
  1970. return main->lines[0].accessibility_text_element;
  1971. }
  1972. }
  1973. return get_accessibility_element();
  1974. }
  1975. void RichTextLabel::_notification(int p_what) {
  1976. switch (p_what) {
  1977. case NOTIFICATION_ACCESSIBILITY_INVALIDATE: {
  1978. accessibility_scroll_element = RID();
  1979. Item *it = main;
  1980. while (it) {
  1981. if (it->type == ITEM_FRAME) {
  1982. ItemFrame *fr = static_cast<ItemFrame *>(it);
  1983. for (size_t i = 0; i < fr->lines.size(); i++) {
  1984. fr->lines[i].accessibility_line_element = RID();
  1985. fr->lines[i].accessibility_text_element = RID();
  1986. }
  1987. }
  1988. it->accessibility_item_element = RID();
  1989. it = _get_next_item(it, true);
  1990. }
  1991. } break;
  1992. case NOTIFICATION_ACCESSIBILITY_UPDATE: {
  1993. RID ae = get_accessibility_element();
  1994. ERR_FAIL_COND(ae.is_null());
  1995. DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_CONTAINER);
  1996. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SHOW_CONTEXT_MENU, callable_mp(this, &RichTextLabel::_accessibility_action_menu));
  1997. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SCROLL_DOWN, callable_mp(this, &RichTextLabel::_accessibility_scroll_down));
  1998. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SCROLL_UP, callable_mp(this, &RichTextLabel::_accessibility_scroll_up));
  1999. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SET_SCROLL_OFFSET, callable_mp(this, &RichTextLabel::_accessibility_scroll_set));
  2000. if (_validate_line_caches()) {
  2001. DisplayServer::get_singleton()->accessibility_update_set_flag(ae, DisplayServer::AccessibilityFlags::FLAG_BUSY, false);
  2002. } else {
  2003. DisplayServer::get_singleton()->accessibility_update_set_flag(ae, DisplayServer::AccessibilityFlags::FLAG_BUSY, true);
  2004. return; // Do not update internal elements if threaded procesisng is not done.
  2005. }
  2006. if (accessibility_scroll_element.is_null()) {
  2007. accessibility_scroll_element = DisplayServer::get_singleton()->accessibility_create_sub_element(ae, DisplayServer::AccessibilityRole::ROLE_CONTAINER);
  2008. }
  2009. Rect2 text_rect = _get_text_rect();
  2010. Transform2D scroll_xform;
  2011. scroll_xform.set_origin(Vector2i(0, -vscroll->get_value()));
  2012. DisplayServer::get_singleton()->accessibility_update_set_transform(accessibility_scroll_element, scroll_xform);
  2013. DisplayServer::get_singleton()->accessibility_update_set_bounds(accessibility_scroll_element, text_rect);
  2014. MutexLock data_lock(data_mutex);
  2015. int to_line = main->first_invalid_line.load();
  2016. int from_line = 0;
  2017. int total_height = INT32_MAX;
  2018. if (to_line && vertical_alignment != VERTICAL_ALIGNMENT_TOP) {
  2019. MutexLock lock(main->lines[to_line - 1].text_buf->get_mutex());
  2020. if (theme_cache.line_separation < 0) {
  2021. // Do not apply to the last line to avoid cutting text.
  2022. 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;
  2023. } else {
  2024. 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;
  2025. }
  2026. }
  2027. float vbegin = 0, vsep = 0;
  2028. if (text_rect.size.y > total_height) {
  2029. switch (vertical_alignment) {
  2030. case VERTICAL_ALIGNMENT_TOP: {
  2031. // Nothing.
  2032. } break;
  2033. case VERTICAL_ALIGNMENT_CENTER: {
  2034. vbegin = (text_rect.size.y - total_height) / 2;
  2035. } break;
  2036. case VERTICAL_ALIGNMENT_BOTTOM: {
  2037. vbegin = text_rect.size.y - total_height;
  2038. } break;
  2039. case VERTICAL_ALIGNMENT_FILL: {
  2040. int lines = 0;
  2041. for (int l = from_line; l < to_line; l++) {
  2042. MutexLock lock(main->lines[l].text_buf->get_mutex());
  2043. lines += main->lines[l].text_buf->get_line_count();
  2044. }
  2045. if (lines > 1) {
  2046. vsep = (text_rect.size.y - total_height) / (lines - 1);
  2047. }
  2048. } break;
  2049. }
  2050. }
  2051. ac_element_bounds_cache.clear();
  2052. Point2 ofs = text_rect.get_position() + Vector2(0, vbegin + main->lines[from_line].offset.y);
  2053. while (from_line < to_line) {
  2054. MutexLock lock(main->lines[from_line].text_buf->get_mutex());
  2055. _accessibility_update_line(accessibility_scroll_element, main, from_line, ofs, text_rect.size.x, vsep);
  2056. ofs.y += main->lines[from_line].text_buf->get_size().y + main->lines[from_line].text_buf->get_line_count() * (theme_cache.line_separation + vsep);
  2057. from_line++;
  2058. }
  2059. } break;
  2060. case NOTIFICATION_MOUSE_EXIT: {
  2061. if (meta_hovering) {
  2062. meta_hovering = nullptr;
  2063. emit_signal(SNAME("meta_hover_ended"), current_meta);
  2064. current_meta = false;
  2065. queue_redraw();
  2066. }
  2067. } break;
  2068. case NOTIFICATION_RESIZED: {
  2069. _stop_thread();
  2070. main->first_resized_line.store(0); // Invalidate all lines.
  2071. _invalidate_accessibility();
  2072. queue_accessibility_update();
  2073. queue_redraw();
  2074. } break;
  2075. case NOTIFICATION_THEME_CHANGED: {
  2076. _stop_thread();
  2077. main->first_invalid_font_line.store(0); // Invalidate all lines.
  2078. _invalidate_accessibility();
  2079. queue_accessibility_update();
  2080. queue_redraw();
  2081. } break;
  2082. case NOTIFICATION_ENTER_TREE: {
  2083. _stop_thread();
  2084. if (!text.is_empty()) {
  2085. set_text(text);
  2086. }
  2087. main->first_invalid_line.store(0); // Invalidate all lines.
  2088. _invalidate_accessibility();
  2089. queue_accessibility_update();
  2090. queue_redraw();
  2091. } break;
  2092. case NOTIFICATION_PREDELETE:
  2093. case NOTIFICATION_EXIT_TREE: {
  2094. _stop_thread();
  2095. accessibility_scroll_element = RID();
  2096. Item *it = main;
  2097. while (it) {
  2098. if (it->type == ITEM_FRAME) {
  2099. ItemFrame *fr = static_cast<ItemFrame *>(it);
  2100. for (size_t i = 0; i < fr->lines.size(); i++) {
  2101. fr->lines[i].accessibility_line_element = RID();
  2102. fr->lines[i].accessibility_text_element = RID();
  2103. }
  2104. }
  2105. it = _get_next_item(it, true);
  2106. }
  2107. } break;
  2108. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED:
  2109. case NOTIFICATION_TRANSLATION_CHANGED: {
  2110. if (!stack_externally_modified) {
  2111. _apply_translation();
  2112. }
  2113. queue_redraw();
  2114. } break;
  2115. case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
  2116. if (is_visible_in_tree()) {
  2117. queue_redraw();
  2118. }
  2119. } break;
  2120. case NOTIFICATION_DRAW: {
  2121. RID ci = get_canvas_item();
  2122. Size2 size = get_size();
  2123. draw_style_box(theme_cache.normal_style, Rect2(Point2(), size));
  2124. if (has_focus()) {
  2125. RenderingServer::get_singleton()->canvas_item_add_clip_ignore(ci, true);
  2126. draw_style_box(theme_cache.focus_style, Rect2(Point2(), size));
  2127. RenderingServer::get_singleton()->canvas_item_add_clip_ignore(ci, false);
  2128. }
  2129. // Start text shaping.
  2130. if (_validate_line_caches()) {
  2131. set_physics_process_internal(false); // Disable auto refresh, if text is fully processed.
  2132. } else {
  2133. // Draw loading progress bar.
  2134. if ((progress_delay > 0) && (OS::get_singleton()->get_ticks_msec() - loading_started >= (uint64_t)progress_delay)) {
  2135. 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);
  2136. 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);
  2137. draw_style_box(theme_cache.progress_bg_style, Rect2(p_pos, p_size));
  2138. bool right_to_left = is_layout_rtl();
  2139. double r = loaded.load();
  2140. int mp = theme_cache.progress_fg_style->get_minimum_size().width;
  2141. int p = std::round(r * (p_size.width - mp));
  2142. if (right_to_left) {
  2143. int p_remaining = std::round((1.0 - r) * (p_size.width - mp));
  2144. 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)));
  2145. } else {
  2146. 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)));
  2147. }
  2148. }
  2149. }
  2150. // Draw main text.
  2151. Rect2 text_rect = _get_text_rect();
  2152. float vofs = vscroll->get_value();
  2153. // Search for the first line.
  2154. int to_line = main->first_invalid_line.load();
  2155. int from_line = _find_first_line(0, to_line, vofs);
  2156. // Bottom margin for text clipping.
  2157. float v_limit = theme_cache.normal_style->get_margin(SIDE_BOTTOM);
  2158. int total_height = INT32_MAX;
  2159. if (to_line && vertical_alignment != VERTICAL_ALIGNMENT_TOP) {
  2160. MutexLock lock(main->lines[to_line - 1].text_buf->get_mutex());
  2161. if (theme_cache.line_separation < 0) {
  2162. // Do not apply to the last line to avoid cutting text.
  2163. 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;
  2164. } else {
  2165. 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;
  2166. }
  2167. }
  2168. float vbegin = 0, vsep = 0;
  2169. if (text_rect.size.y > total_height) {
  2170. switch (vertical_alignment) {
  2171. case VERTICAL_ALIGNMENT_TOP: {
  2172. // Nothing.
  2173. } break;
  2174. case VERTICAL_ALIGNMENT_CENTER: {
  2175. vbegin = (text_rect.size.y - total_height) / 2;
  2176. } break;
  2177. case VERTICAL_ALIGNMENT_BOTTOM: {
  2178. vbegin = text_rect.size.y - total_height;
  2179. } break;
  2180. case VERTICAL_ALIGNMENT_FILL: {
  2181. int lines = 0;
  2182. for (int l = from_line; l < to_line; l++) {
  2183. MutexLock lock(main->lines[l].text_buf->get_mutex());
  2184. lines += main->lines[l].text_buf->get_line_count();
  2185. }
  2186. if (lines > 1) {
  2187. vsep = (text_rect.size.y - total_height) / (lines - 1);
  2188. }
  2189. } break;
  2190. }
  2191. }
  2192. Point2 shadow_ofs(theme_cache.shadow_offset_x, theme_cache.shadow_offset_y);
  2193. visible_paragraph_count = 0;
  2194. visible_line_count = 0;
  2195. // New cache draw.
  2196. Point2 ofs = text_rect.get_position() + Vector2(0, vbegin + main->lines[from_line].offset.y - vofs);
  2197. int processed_glyphs = 0;
  2198. while (ofs.y < size.height - v_limit && from_line < to_line) {
  2199. MutexLock lock(main->lines[from_line].text_buf->get_mutex());
  2200. visible_paragraph_count++;
  2201. visible_line_count += _draw_line(main, from_line, ofs, text_rect.size.x, vsep, 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);
  2202. ofs.y += main->lines[from_line].text_buf->get_size().y + main->lines[from_line].text_buf->get_line_count() * (theme_cache.line_separation + vsep);
  2203. from_line++;
  2204. }
  2205. if (has_focus() && get_tree()->is_accessibility_enabled()) {
  2206. RID ae;
  2207. if (keyboard_focus_frame && keyboard_focus_item) {
  2208. if (keyboard_focus_on_text) {
  2209. ae = keyboard_focus_frame->lines[keyboard_focus_line].accessibility_text_element;
  2210. } else {
  2211. if (keyboard_focus_item->accessibility_item_element.is_valid()) {
  2212. ae = keyboard_focus_item->accessibility_item_element;
  2213. }
  2214. }
  2215. } else {
  2216. if (!main->lines.is_empty()) {
  2217. ae = main->lines[0].accessibility_text_element;
  2218. }
  2219. }
  2220. if (ac_element_bounds_cache.has(ae)) {
  2221. draw_style_box(theme_cache.focus_style, ac_element_bounds_cache[ae]);
  2222. }
  2223. }
  2224. } break;
  2225. case NOTIFICATION_INTERNAL_PROCESS: {
  2226. if (is_visible_in_tree()) {
  2227. if (!is_finished()) {
  2228. return;
  2229. }
  2230. double dt = get_process_delta_time();
  2231. _update_fx(main, dt);
  2232. queue_redraw();
  2233. }
  2234. } break;
  2235. case NOTIFICATION_FOCUS_EXIT: {
  2236. if (deselect_on_focus_loss_enabled) {
  2237. deselect();
  2238. }
  2239. } break;
  2240. case NOTIFICATION_DRAG_END: {
  2241. selection.drag_attempt = false;
  2242. } break;
  2243. }
  2244. }
  2245. Control::CursorShape RichTextLabel::get_cursor_shape(const Point2 &p_pos) const {
  2246. if (selection.click_item) {
  2247. return CURSOR_IBEAM;
  2248. }
  2249. Item *item = nullptr;
  2250. bool outside = true;
  2251. const_cast<RichTextLabel *>(this)->_find_click(main, p_pos, nullptr, nullptr, &item, nullptr, &outside, true);
  2252. if (item && !outside && const_cast<RichTextLabel *>(this)->_find_meta(item, nullptr)) {
  2253. return CURSOR_POINTING_HAND;
  2254. }
  2255. return get_default_cursor_shape();
  2256. }
  2257. void RichTextLabel::gui_input(const Ref<InputEvent> &p_event) {
  2258. ERR_FAIL_COND(p_event.is_null());
  2259. Ref<InputEventMouseButton> b = p_event;
  2260. if (b.is_valid()) {
  2261. if (b->get_button_index() == MouseButton::LEFT) {
  2262. if (b->is_pressed() && !b->is_double_click()) {
  2263. scroll_updated = false;
  2264. ItemFrame *c_frame = nullptr;
  2265. int c_line = 0;
  2266. Item *c_item = nullptr;
  2267. int c_index = 0;
  2268. bool outside;
  2269. selection.double_click = false;
  2270. selection.drag_attempt = false;
  2271. _find_click(main, b->get_position(), &c_frame, &c_line, &c_item, &c_index, &outside, false);
  2272. if (c_item != nullptr) {
  2273. if (selection.enabled) {
  2274. selection.click_frame = c_frame;
  2275. selection.click_item = c_item;
  2276. selection.click_line = c_line;
  2277. selection.click_char = c_index;
  2278. // Erase previous selection.
  2279. if (selection.active) {
  2280. if (drag_and_drop_selection_enabled && _is_click_inside_selection()) {
  2281. selection.drag_attempt = true;
  2282. selection.click_item = nullptr;
  2283. } else {
  2284. selection.from_frame = nullptr;
  2285. selection.from_line = 0;
  2286. selection.from_item = nullptr;
  2287. selection.from_char = 0;
  2288. selection.to_frame = nullptr;
  2289. selection.to_line = 0;
  2290. selection.to_item = nullptr;
  2291. selection.to_char = 0;
  2292. deselect();
  2293. }
  2294. }
  2295. }
  2296. }
  2297. } else if (b->is_pressed() && b->is_double_click() && selection.enabled) {
  2298. //double_click: select word
  2299. ItemFrame *c_frame = nullptr;
  2300. int c_line = 0;
  2301. Item *c_item = nullptr;
  2302. int c_index = 0;
  2303. bool outside;
  2304. selection.drag_attempt = false;
  2305. _find_click(main, b->get_position(), &c_frame, &c_line, &c_item, &c_index, &outside, false);
  2306. if (c_frame) {
  2307. const Line &l = c_frame->lines[c_line];
  2308. MutexLock lock(l.text_buf->get_mutex());
  2309. PackedInt32Array words = TS->shaped_text_get_word_breaks(l.text_buf->get_rid());
  2310. for (int i = 0; i < words.size(); i = i + 2) {
  2311. if (c_index >= words[i] && c_index < words[i + 1]) {
  2312. selection.from_frame = c_frame;
  2313. selection.from_line = c_line;
  2314. selection.from_item = c_item;
  2315. selection.from_char = words[i];
  2316. selection.to_frame = c_frame;
  2317. selection.to_line = c_line;
  2318. selection.to_item = c_item;
  2319. selection.to_char = words[i + 1];
  2320. selection.active = true;
  2321. if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_CLIPBOARD_PRIMARY)) {
  2322. DisplayServer::get_singleton()->clipboard_set_primary(get_selected_text());
  2323. }
  2324. queue_accessibility_update();
  2325. queue_redraw();
  2326. break;
  2327. }
  2328. }
  2329. selection.click_frame = c_frame;
  2330. selection.click_item = c_item;
  2331. selection.click_line = c_line;
  2332. selection.click_char = c_index;
  2333. selection.double_click = true;
  2334. }
  2335. } else if (!b->is_pressed()) {
  2336. if (selection.enabled && DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_CLIPBOARD_PRIMARY)) {
  2337. DisplayServer::get_singleton()->clipboard_set_primary(get_selected_text());
  2338. }
  2339. selection.click_item = nullptr;
  2340. if (selection.drag_attempt) {
  2341. selection.drag_attempt = false;
  2342. if (_is_click_inside_selection()) {
  2343. selection.from_frame = nullptr;
  2344. selection.from_line = 0;
  2345. selection.from_item = nullptr;
  2346. selection.from_char = 0;
  2347. selection.to_frame = nullptr;
  2348. selection.to_line = 0;
  2349. selection.to_item = nullptr;
  2350. selection.to_char = 0;
  2351. deselect();
  2352. }
  2353. }
  2354. if (!b->is_double_click() && !scroll_updated && !selection.active) {
  2355. Item *c_item = nullptr;
  2356. bool outside = true;
  2357. _find_click(main, b->get_position(), nullptr, nullptr, &c_item, nullptr, &outside, true);
  2358. if (c_item) {
  2359. Variant meta;
  2360. if (!outside && _find_meta(c_item, &meta)) {
  2361. //meta clicked
  2362. emit_signal(SNAME("meta_clicked"), meta);
  2363. }
  2364. }
  2365. }
  2366. }
  2367. }
  2368. bool scroll_value_modified = false;
  2369. double prev_scroll = vscroll->get_value();
  2370. if (b->get_button_index() == MouseButton::WHEEL_UP) {
  2371. if (scroll_active) {
  2372. vscroll->scroll(-vscroll->get_page() * b->get_factor() * 0.5 / 8);
  2373. scroll_value_modified = true;
  2374. }
  2375. }
  2376. if (b->get_button_index() == MouseButton::WHEEL_DOWN) {
  2377. if (scroll_active) {
  2378. vscroll->scroll(vscroll->get_page() * b->get_factor() * 0.5 / 8);
  2379. scroll_value_modified = true;
  2380. }
  2381. }
  2382. if (scroll_value_modified && vscroll->get_value() != prev_scroll) {
  2383. accept_event();
  2384. return;
  2385. }
  2386. if (b->get_button_index() == MouseButton::RIGHT && context_menu_enabled) {
  2387. _update_context_menu();
  2388. menu->set_position(get_screen_transform().xform(b->get_position()));
  2389. menu->reset_size();
  2390. menu->popup();
  2391. grab_focus();
  2392. }
  2393. }
  2394. Ref<InputEventPanGesture> pan_gesture = p_event;
  2395. if (pan_gesture.is_valid()) {
  2396. if (scroll_active) {
  2397. vscroll->scroll(vscroll->get_page() * pan_gesture->get_delta().y * 0.5 / 8);
  2398. queue_accessibility_update();
  2399. }
  2400. return;
  2401. }
  2402. Ref<InputEventKey> k = p_event;
  2403. if (k.is_valid()) {
  2404. if (k->is_pressed()) {
  2405. bool handled = false;
  2406. if (k->is_action("ui_page_up", true) && vscroll->is_visible_in_tree()) {
  2407. vscroll->scroll(-vscroll->get_page());
  2408. queue_accessibility_update();
  2409. handled = true;
  2410. }
  2411. if (k->is_action("ui_page_down", true) && vscroll->is_visible_in_tree()) {
  2412. vscroll->scroll(vscroll->get_page());
  2413. queue_accessibility_update();
  2414. handled = true;
  2415. }
  2416. if (k->is_action("ui_up", true) && vscroll->is_visible_in_tree()) {
  2417. vscroll->scroll(-theme_cache.normal_font->get_height(theme_cache.normal_font_size));
  2418. queue_accessibility_update();
  2419. handled = true;
  2420. }
  2421. if (k->is_action("ui_down", true) && vscroll->is_visible_in_tree()) {
  2422. vscroll->scroll(theme_cache.normal_font->get_height(theme_cache.normal_font_size));
  2423. queue_accessibility_update();
  2424. handled = true;
  2425. }
  2426. if (k->is_action("ui_home", true) && vscroll->is_visible_in_tree()) {
  2427. vscroll->scroll_to(0);
  2428. queue_accessibility_update();
  2429. handled = true;
  2430. }
  2431. if (k->is_action("ui_end", true) && vscroll->is_visible_in_tree()) {
  2432. vscroll->scroll_to(vscroll->get_max());
  2433. queue_accessibility_update();
  2434. handled = true;
  2435. }
  2436. if (get_tree()->is_accessibility_enabled()) {
  2437. if (k->is_action("ui_left", true)) {
  2438. if (keyboard_focus_frame != nullptr) {
  2439. if (!keyboard_focus_on_text && keyboard_focus_line < (int)keyboard_focus_frame->lines.size() && keyboard_focus_frame->lines[keyboard_focus_line].from == keyboard_focus_item) {
  2440. keyboard_focus_on_text = true;
  2441. } else {
  2442. Item *it = keyboard_focus_item;
  2443. while (it) {
  2444. it = _get_prev_item(it, true);
  2445. if (it) {
  2446. ItemFrame *f = nullptr;
  2447. _find_frame(it, &f, nullptr);
  2448. if (it->type == ITEM_IMAGE || it->type == ITEM_TABLE) {
  2449. keyboard_focus_frame = f;
  2450. keyboard_focus_line = it->line;
  2451. keyboard_focus_item = it;
  2452. keyboard_focus_on_text = false;
  2453. break;
  2454. }
  2455. if (f && !f->lines.is_empty()) {
  2456. if (f->lines[it->line].from == it) {
  2457. keyboard_focus_frame = f;
  2458. keyboard_focus_line = it->line;
  2459. keyboard_focus_item = it;
  2460. keyboard_focus_on_text = true;
  2461. break;
  2462. }
  2463. }
  2464. }
  2465. }
  2466. }
  2467. }
  2468. queue_accessibility_update();
  2469. queue_redraw();
  2470. handled = true;
  2471. }
  2472. if (k->is_action("ui_right", true)) {
  2473. if (keyboard_focus_frame == nullptr) {
  2474. keyboard_focus_frame = main;
  2475. keyboard_focus_line = 0;
  2476. keyboard_focus_item = main->lines.is_empty() ? nullptr : main->lines[0].from;
  2477. keyboard_focus_on_text = true;
  2478. } else {
  2479. if (keyboard_focus_on_text && keyboard_focus_item && (keyboard_focus_item->type == ITEM_IMAGE || keyboard_focus_item->type == ITEM_TABLE)) {
  2480. keyboard_focus_on_text = false;
  2481. } else {
  2482. Item *it = keyboard_focus_item;
  2483. while (it) {
  2484. it = _get_next_item(it, true);
  2485. if (it) {
  2486. ItemFrame *f = nullptr;
  2487. _find_frame(it, &f, nullptr);
  2488. if (f && !f->lines.is_empty()) {
  2489. if (f->lines[it->line].from == it) {
  2490. keyboard_focus_frame = f;
  2491. keyboard_focus_line = it->line;
  2492. keyboard_focus_item = it;
  2493. keyboard_focus_on_text = true;
  2494. break;
  2495. }
  2496. }
  2497. if (it->type == ITEM_IMAGE || it->type == ITEM_TABLE) {
  2498. keyboard_focus_frame = f;
  2499. keyboard_focus_line = it->line;
  2500. keyboard_focus_item = it;
  2501. keyboard_focus_on_text = false;
  2502. break;
  2503. }
  2504. }
  2505. }
  2506. }
  2507. }
  2508. queue_accessibility_update();
  2509. queue_redraw();
  2510. handled = true;
  2511. }
  2512. }
  2513. if (is_shortcut_keys_enabled()) {
  2514. if (k->is_action("ui_text_select_all", true)) {
  2515. select_all();
  2516. handled = true;
  2517. }
  2518. if (k->is_action("ui_copy", true)) {
  2519. const String txt = get_selected_text();
  2520. if (!txt.is_empty()) {
  2521. DisplayServer::get_singleton()->clipboard_set(txt);
  2522. }
  2523. handled = true;
  2524. }
  2525. }
  2526. if (k->is_action("ui_menu", true)) {
  2527. if (context_menu_enabled) {
  2528. _update_context_menu();
  2529. menu->set_position(get_screen_position());
  2530. menu->reset_size();
  2531. menu->popup();
  2532. menu->grab_focus();
  2533. }
  2534. handled = true;
  2535. }
  2536. if (handled) {
  2537. accept_event();
  2538. }
  2539. }
  2540. }
  2541. Ref<InputEventMouseMotion> m = p_event;
  2542. if (m.is_valid()) {
  2543. ItemFrame *c_frame = nullptr;
  2544. int c_line = 0;
  2545. Item *c_item = nullptr;
  2546. int c_index = 0;
  2547. bool outside;
  2548. _find_click(main, m->get_position(), &c_frame, &c_line, &c_item, &c_index, &outside, false);
  2549. if (selection.click_item && c_item) {
  2550. selection.from_frame = selection.click_frame;
  2551. selection.from_line = selection.click_line;
  2552. selection.from_item = selection.click_item;
  2553. selection.from_char = selection.click_char;
  2554. selection.to_frame = c_frame;
  2555. selection.to_line = c_line;
  2556. selection.to_item = c_item;
  2557. selection.to_char = c_index;
  2558. bool swap = false;
  2559. if (selection.click_frame && c_frame) {
  2560. const Line &l1 = c_frame->lines[c_line];
  2561. const Line &l2 = selection.click_frame->lines[selection.click_line];
  2562. if (l1.char_offset + c_index < l2.char_offset + selection.click_char) {
  2563. swap = true;
  2564. } else if (l1.char_offset + c_index == l2.char_offset + selection.click_char && !selection.double_click) {
  2565. deselect();
  2566. return;
  2567. }
  2568. }
  2569. if (swap) {
  2570. SWAP(selection.from_frame, selection.to_frame);
  2571. SWAP(selection.from_line, selection.to_line);
  2572. SWAP(selection.from_item, selection.to_item);
  2573. SWAP(selection.from_char, selection.to_char);
  2574. }
  2575. if (selection.double_click && c_frame) {
  2576. // Expand the selection to word edges.
  2577. Line *l = &selection.from_frame->lines[selection.from_line];
  2578. MutexLock lock(l->text_buf->get_mutex());
  2579. PackedInt32Array words = TS->shaped_text_get_word_breaks(l->text_buf->get_rid());
  2580. for (int i = 0; i < words.size(); i = i + 2) {
  2581. if (selection.from_char > words[i] && selection.from_char < words[i + 1]) {
  2582. selection.from_char = words[i];
  2583. break;
  2584. }
  2585. }
  2586. l = &selection.to_frame->lines[selection.to_line];
  2587. lock = MutexLock(l->text_buf->get_mutex());
  2588. words = TS->shaped_text_get_word_breaks(l->text_buf->get_rid());
  2589. for (int i = 0; i < words.size(); i = i + 2) {
  2590. if (selection.to_char > words[i] && selection.to_char < words[i + 1]) {
  2591. selection.to_char = words[i + 1];
  2592. break;
  2593. }
  2594. }
  2595. }
  2596. selection.active = true;
  2597. queue_accessibility_update();
  2598. queue_redraw();
  2599. }
  2600. _find_click(main, m->get_position(), nullptr, nullptr, &c_item, nullptr, &outside, true);
  2601. Variant meta;
  2602. ItemMeta *item_meta;
  2603. ItemMeta *prev_meta = meta_hovering;
  2604. if (c_item && !outside && _find_meta(c_item, &meta, &item_meta)) {
  2605. if (meta_hovering != item_meta) {
  2606. if (meta_hovering) {
  2607. emit_signal(SNAME("meta_hover_ended"), current_meta);
  2608. }
  2609. meta_hovering = item_meta;
  2610. current_meta = meta;
  2611. emit_signal(SNAME("meta_hover_started"), meta);
  2612. if ((item_meta && item_meta->underline == META_UNDERLINE_ON_HOVER) || (prev_meta && prev_meta->underline == META_UNDERLINE_ON_HOVER)) {
  2613. queue_redraw();
  2614. }
  2615. }
  2616. } else if (meta_hovering) {
  2617. meta_hovering = nullptr;
  2618. emit_signal(SNAME("meta_hover_ended"), current_meta);
  2619. current_meta = false;
  2620. if (prev_meta->underline == META_UNDERLINE_ON_HOVER) {
  2621. queue_redraw();
  2622. }
  2623. }
  2624. }
  2625. }
  2626. String RichTextLabel::get_tooltip(const Point2 &p_pos) const {
  2627. Item *c_item = nullptr;
  2628. bool outside;
  2629. const_cast<RichTextLabel *>(this)->_find_click(main, p_pos, nullptr, nullptr, &c_item, nullptr, &outside, true);
  2630. String description;
  2631. if (c_item && !outside) {
  2632. ItemMeta *meta = nullptr;
  2633. if (const_cast<RichTextLabel *>(this)->_find_hint(c_item, &description)) {
  2634. return description;
  2635. } else if (c_item->type == ITEM_IMAGE && !static_cast<ItemImage *>(c_item)->tooltip.is_empty()) {
  2636. return static_cast<ItemImage *>(c_item)->tooltip;
  2637. } else if (const_cast<RichTextLabel *>(this)->_find_meta(c_item, nullptr, &meta) && meta && !meta->tooltip.is_empty()) {
  2638. return meta->tooltip;
  2639. }
  2640. }
  2641. return Control::get_tooltip(p_pos);
  2642. }
  2643. void RichTextLabel::_find_frame(Item *p_item, ItemFrame **r_frame, int *r_line) {
  2644. if (r_frame != nullptr) {
  2645. *r_frame = nullptr;
  2646. }
  2647. if (r_line != nullptr) {
  2648. *r_line = 0;
  2649. }
  2650. Item *item = p_item;
  2651. while (item) {
  2652. if (item->parent != nullptr && item->parent->type == ITEM_FRAME) {
  2653. if (r_frame != nullptr) {
  2654. *r_frame = static_cast<ItemFrame *>(item->parent);
  2655. }
  2656. if (r_line != nullptr) {
  2657. *r_line = item->line;
  2658. }
  2659. return;
  2660. }
  2661. item = item->parent;
  2662. }
  2663. }
  2664. RichTextLabel::Item *RichTextLabel::_find_indentable(Item *p_item) {
  2665. Item *indentable = p_item;
  2666. while (indentable) {
  2667. if (indentable->type == ITEM_INDENT || indentable->type == ITEM_LIST) {
  2668. return indentable;
  2669. }
  2670. indentable = indentable->parent;
  2671. }
  2672. return indentable;
  2673. }
  2674. RichTextLabel::ItemFont *RichTextLabel::_find_font(Item *p_item) {
  2675. Item *fontitem = p_item;
  2676. while (fontitem) {
  2677. if (fontitem->type == ITEM_FONT) {
  2678. ItemFont *fi = static_cast<ItemFont *>(fontitem);
  2679. switch (fi->def_font) {
  2680. case RTL_NORMAL_FONT: {
  2681. if (fi->variation) {
  2682. Ref<FontVariation> fc = fi->font;
  2683. if (fc.is_valid()) {
  2684. fc->set_base_font(theme_cache.normal_font);
  2685. }
  2686. } else {
  2687. fi->font = theme_cache.normal_font;
  2688. }
  2689. if (fi->def_size) {
  2690. fi->font_size = theme_cache.normal_font_size;
  2691. }
  2692. } break;
  2693. case RTL_BOLD_FONT: {
  2694. if (fi->variation) {
  2695. Ref<FontVariation> fc = fi->font;
  2696. if (fc.is_valid()) {
  2697. fc->set_base_font(theme_cache.bold_font);
  2698. }
  2699. } else {
  2700. fi->font = theme_cache.bold_font;
  2701. }
  2702. if (fi->def_size) {
  2703. fi->font_size = theme_cache.bold_font_size;
  2704. }
  2705. } break;
  2706. case RTL_ITALICS_FONT: {
  2707. if (fi->variation) {
  2708. Ref<FontVariation> fc = fi->font;
  2709. if (fc.is_valid()) {
  2710. fc->set_base_font(theme_cache.italics_font);
  2711. }
  2712. } else {
  2713. fi->font = theme_cache.italics_font;
  2714. }
  2715. if (fi->def_size) {
  2716. fi->font_size = theme_cache.italics_font_size;
  2717. }
  2718. } break;
  2719. case RTL_BOLD_ITALICS_FONT: {
  2720. if (fi->variation) {
  2721. Ref<FontVariation> fc = fi->font;
  2722. if (fc.is_valid()) {
  2723. fc->set_base_font(theme_cache.bold_italics_font);
  2724. }
  2725. } else {
  2726. fi->font = theme_cache.bold_italics_font;
  2727. }
  2728. if (fi->def_size) {
  2729. fi->font_size = theme_cache.bold_italics_font_size;
  2730. }
  2731. } break;
  2732. case RTL_MONO_FONT: {
  2733. if (fi->variation) {
  2734. Ref<FontVariation> fc = fi->font;
  2735. if (fc.is_valid()) {
  2736. fc->set_base_font(theme_cache.mono_font);
  2737. }
  2738. } else {
  2739. fi->font = theme_cache.mono_font;
  2740. }
  2741. if (fi->def_size) {
  2742. fi->font_size = theme_cache.mono_font_size;
  2743. }
  2744. } break;
  2745. default: {
  2746. } break;
  2747. }
  2748. return fi;
  2749. }
  2750. fontitem = fontitem->parent;
  2751. }
  2752. return nullptr;
  2753. }
  2754. RichTextLabel::ItemFontSize *RichTextLabel::_find_font_size(Item *p_item) {
  2755. Item *sizeitem = p_item;
  2756. while (sizeitem) {
  2757. if (sizeitem->type == ITEM_FONT_SIZE) {
  2758. ItemFontSize *fi = static_cast<ItemFontSize *>(sizeitem);
  2759. return fi;
  2760. }
  2761. sizeitem = sizeitem->parent;
  2762. }
  2763. return nullptr;
  2764. }
  2765. int RichTextLabel::_find_outline_size(Item *p_item, int p_default) {
  2766. Item *sizeitem = p_item;
  2767. while (sizeitem) {
  2768. if (sizeitem->type == ITEM_OUTLINE_SIZE) {
  2769. ItemOutlineSize *fi = static_cast<ItemOutlineSize *>(sizeitem);
  2770. return fi->outline_size;
  2771. }
  2772. sizeitem = sizeitem->parent;
  2773. }
  2774. return p_default;
  2775. }
  2776. RichTextLabel::ItemDropcap *RichTextLabel::_find_dc_item(Item *p_item) {
  2777. Item *item = p_item;
  2778. while (item) {
  2779. if (item->type == ITEM_DROPCAP) {
  2780. return static_cast<ItemDropcap *>(item);
  2781. }
  2782. item = item->parent;
  2783. }
  2784. return nullptr;
  2785. }
  2786. RichTextLabel::ItemList *RichTextLabel::_find_list_item(Item *p_item) {
  2787. Item *item = p_item;
  2788. while (item) {
  2789. if (item->type == ITEM_LIST) {
  2790. return static_cast<ItemList *>(item);
  2791. }
  2792. item = item->parent;
  2793. }
  2794. return nullptr;
  2795. }
  2796. int RichTextLabel::_find_list(Item *p_item, Vector<int> &r_index, Vector<int> &r_count, Vector<ItemList *> &r_list) {
  2797. Item *item = p_item;
  2798. Item *prev_item = p_item;
  2799. int level = 0;
  2800. while (item) {
  2801. if (item->type == ITEM_LIST) {
  2802. ItemList *list = static_cast<ItemList *>(item);
  2803. ItemFrame *frame = nullptr;
  2804. int line = -1;
  2805. _find_frame(list, &frame, &line);
  2806. int index = 1;
  2807. int count = 1;
  2808. if (frame != nullptr) {
  2809. for (int i = list->line + 1; i < (int)frame->lines.size(); i++) {
  2810. if (_find_list_item(frame->lines[i].from) == list) {
  2811. if (i <= prev_item->line) {
  2812. index++;
  2813. }
  2814. count++;
  2815. }
  2816. }
  2817. }
  2818. r_index.push_back(index);
  2819. r_count.push_back(count);
  2820. r_list.push_back(list);
  2821. prev_item = item;
  2822. }
  2823. level++;
  2824. item = item->parent;
  2825. }
  2826. return level;
  2827. }
  2828. int RichTextLabel::_find_margin(Item *p_item, const Ref<Font> &p_base_font, int p_base_font_size) {
  2829. Item *item = p_item;
  2830. float margin = 0.0;
  2831. while (item) {
  2832. if (item->type == ITEM_FRAME) {
  2833. break;
  2834. }
  2835. if (item->type == ITEM_INDENT) {
  2836. Ref<Font> font = p_base_font;
  2837. int font_size = p_base_font_size;
  2838. ItemFont *font_it = _find_font(item);
  2839. if (font_it) {
  2840. if (font_it->font.is_valid()) {
  2841. font = font_it->font;
  2842. }
  2843. if (font_it->font_size > 0) {
  2844. font_size = font_it->font_size;
  2845. }
  2846. }
  2847. ItemFontSize *font_size_it = _find_font_size(item);
  2848. if (font_size_it && font_size_it->font_size > 0) {
  2849. font_size = font_size_it->font_size;
  2850. }
  2851. margin += tab_size * font->get_char_size(' ', font_size).width;
  2852. } else if (item->type == ITEM_LIST) {
  2853. Ref<Font> font = p_base_font;
  2854. int font_size = p_base_font_size;
  2855. ItemFont *font_it = _find_font(item);
  2856. if (font_it) {
  2857. if (font_it->font.is_valid()) {
  2858. font = font_it->font;
  2859. }
  2860. if (font_it->font_size > 0) {
  2861. font_size = font_it->font_size;
  2862. }
  2863. }
  2864. ItemFontSize *font_size_it = _find_font_size(item);
  2865. if (font_size_it && font_size_it->font_size > 0) {
  2866. font_size = font_size_it->font_size;
  2867. }
  2868. margin += tab_size * font->get_char_size(' ', font_size).width;
  2869. }
  2870. item = item->parent;
  2871. }
  2872. return margin;
  2873. }
  2874. BitField<TextServer::JustificationFlag> RichTextLabel::_find_jst_flags(Item *p_item) {
  2875. Item *item = p_item;
  2876. while (item) {
  2877. if (item->type == ITEM_PARAGRAPH) {
  2878. ItemParagraph *p = static_cast<ItemParagraph *>(item);
  2879. return p->jst_flags;
  2880. }
  2881. item = item->parent;
  2882. }
  2883. return default_jst_flags;
  2884. }
  2885. PackedFloat32Array RichTextLabel::_find_tab_stops(Item *p_item) {
  2886. Item *item = p_item;
  2887. while (item) {
  2888. if (item->type == ITEM_PARAGRAPH) {
  2889. ItemParagraph *p = static_cast<ItemParagraph *>(item);
  2890. return p->tab_stops;
  2891. }
  2892. item = item->parent;
  2893. }
  2894. return default_tab_stops;
  2895. }
  2896. HorizontalAlignment RichTextLabel::_find_alignment(Item *p_item) {
  2897. Item *item = p_item;
  2898. while (item) {
  2899. if (item->type == ITEM_PARAGRAPH) {
  2900. ItemParagraph *p = static_cast<ItemParagraph *>(item);
  2901. return p->alignment;
  2902. }
  2903. item = item->parent;
  2904. }
  2905. return default_alignment;
  2906. }
  2907. TextServer::Direction RichTextLabel::_find_direction(Item *p_item) {
  2908. Item *item = p_item;
  2909. while (item) {
  2910. if (item->type == ITEM_PARAGRAPH) {
  2911. ItemParagraph *p = static_cast<ItemParagraph *>(item);
  2912. if (p->direction != Control::TEXT_DIRECTION_INHERITED) {
  2913. return (TextServer::Direction)p->direction;
  2914. }
  2915. }
  2916. item = item->parent;
  2917. }
  2918. if (text_direction == Control::TEXT_DIRECTION_INHERITED) {
  2919. return is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR;
  2920. } else {
  2921. return (TextServer::Direction)text_direction;
  2922. }
  2923. }
  2924. TextServer::StructuredTextParser RichTextLabel::_find_stt(Item *p_item) {
  2925. Item *item = p_item;
  2926. while (item) {
  2927. if (item->type == ITEM_PARAGRAPH) {
  2928. ItemParagraph *p = static_cast<ItemParagraph *>(item);
  2929. return p->st_parser;
  2930. }
  2931. item = item->parent;
  2932. }
  2933. return st_parser;
  2934. }
  2935. String RichTextLabel::_find_language(Item *p_item) {
  2936. Item *item = p_item;
  2937. while (item) {
  2938. if (item->type == ITEM_LANGUAGE) {
  2939. ItemLanguage *p = static_cast<ItemLanguage *>(item);
  2940. return p->language;
  2941. } else if (item->type == ITEM_PARAGRAPH) {
  2942. ItemParagraph *p = static_cast<ItemParagraph *>(item);
  2943. return p->language;
  2944. }
  2945. item = item->parent;
  2946. }
  2947. return language;
  2948. }
  2949. Color RichTextLabel::_find_color(Item *p_item, const Color &p_default_color) {
  2950. Item *item = p_item;
  2951. while (item) {
  2952. if (item->type == ITEM_COLOR) {
  2953. ItemColor *color = static_cast<ItemColor *>(item);
  2954. return color->color;
  2955. }
  2956. item = item->parent;
  2957. }
  2958. return p_default_color;
  2959. }
  2960. Color RichTextLabel::_find_outline_color(Item *p_item, const Color &p_default_color) {
  2961. Item *item = p_item;
  2962. while (item) {
  2963. if (item->type == ITEM_OUTLINE_COLOR) {
  2964. ItemOutlineColor *color = static_cast<ItemOutlineColor *>(item);
  2965. return color->color;
  2966. }
  2967. item = item->parent;
  2968. }
  2969. return p_default_color;
  2970. }
  2971. bool RichTextLabel::_find_underline(Item *p_item) {
  2972. Item *item = p_item;
  2973. while (item) {
  2974. if (item->type == ITEM_UNDERLINE) {
  2975. return true;
  2976. }
  2977. item = item->parent;
  2978. }
  2979. return false;
  2980. }
  2981. bool RichTextLabel::_find_strikethrough(Item *p_item) {
  2982. Item *item = p_item;
  2983. while (item) {
  2984. if (item->type == ITEM_STRIKETHROUGH) {
  2985. return true;
  2986. }
  2987. item = item->parent;
  2988. }
  2989. return false;
  2990. }
  2991. void RichTextLabel::_fetch_item_fx_stack(Item *p_item, Vector<ItemFX *> &r_stack) {
  2992. Item *item = p_item;
  2993. while (item) {
  2994. if (item->type == ITEM_CUSTOMFX || item->type == ITEM_SHAKE || item->type == ITEM_WAVE || item->type == ITEM_TORNADO || item->type == ITEM_RAINBOW || item->type == ITEM_PULSE) {
  2995. r_stack.push_back(static_cast<ItemFX *>(item));
  2996. }
  2997. item = item->parent;
  2998. }
  2999. }
  3000. void RichTextLabel::_normalize_subtags(Vector<String> &subtags) {
  3001. for (String &subtag : subtags) {
  3002. subtag = subtag.unquote();
  3003. }
  3004. }
  3005. bool RichTextLabel::_find_meta(Item *p_item, Variant *r_meta, ItemMeta **r_item) {
  3006. Item *item = p_item;
  3007. while (item) {
  3008. if (item->type == ITEM_META) {
  3009. ItemMeta *meta = static_cast<ItemMeta *>(item);
  3010. if (r_meta) {
  3011. *r_meta = meta->meta;
  3012. }
  3013. if (r_item) {
  3014. *r_item = meta;
  3015. }
  3016. return true;
  3017. }
  3018. item = item->parent;
  3019. }
  3020. return false;
  3021. }
  3022. bool RichTextLabel::_find_hint(Item *p_item, String *r_description) {
  3023. Item *item = p_item;
  3024. while (item) {
  3025. if (item->type == ITEM_HINT) {
  3026. ItemHint *hint = static_cast<ItemHint *>(item);
  3027. if (r_description) {
  3028. *r_description = hint->description;
  3029. }
  3030. return true;
  3031. }
  3032. item = item->parent;
  3033. }
  3034. return false;
  3035. }
  3036. Color RichTextLabel::_find_bgcolor(Item *p_item) {
  3037. Item *item = p_item;
  3038. while (item) {
  3039. if (item->type == ITEM_BGCOLOR) {
  3040. ItemBGColor *color = static_cast<ItemBGColor *>(item);
  3041. return color->color;
  3042. }
  3043. item = item->parent;
  3044. }
  3045. return Color(0, 0, 0, 0);
  3046. }
  3047. Color RichTextLabel::_find_fgcolor(Item *p_item) {
  3048. Item *item = p_item;
  3049. while (item) {
  3050. if (item->type == ITEM_FGCOLOR) {
  3051. ItemFGColor *color = static_cast<ItemFGColor *>(item);
  3052. return color->color;
  3053. }
  3054. item = item->parent;
  3055. }
  3056. return Color(0, 0, 0, 0);
  3057. }
  3058. bool RichTextLabel::_find_layout_subitem(Item *from, Item *to) {
  3059. if (from && from != to) {
  3060. if (from->type != ITEM_FONT && from->type != ITEM_COLOR && from->type != ITEM_UNDERLINE && from->type != ITEM_STRIKETHROUGH) {
  3061. return true;
  3062. }
  3063. for (Item *E : from->subitems) {
  3064. bool layout = _find_layout_subitem(E, to);
  3065. if (layout) {
  3066. return true;
  3067. }
  3068. }
  3069. }
  3070. return false;
  3071. }
  3072. void RichTextLabel::_thread_function(void *p_userdata) {
  3073. set_current_thread_safe_for_nodes(true);
  3074. _process_line_caches();
  3075. callable_mp(this, &RichTextLabel::_thread_end).call_deferred();
  3076. }
  3077. void RichTextLabel::_thread_end() {
  3078. set_physics_process_internal(false);
  3079. if (!scroll_visible) {
  3080. vscroll->hide();
  3081. }
  3082. if (is_visible_in_tree()) {
  3083. queue_accessibility_update();
  3084. queue_redraw();
  3085. }
  3086. }
  3087. void RichTextLabel::_stop_thread() {
  3088. if (threaded) {
  3089. stop_thread.store(true);
  3090. wait_until_finished();
  3091. }
  3092. }
  3093. int RichTextLabel::get_pending_paragraphs() const {
  3094. int to_line = main->first_invalid_line.load();
  3095. int lines = main->lines.size();
  3096. return lines - to_line;
  3097. }
  3098. bool RichTextLabel::is_finished() const {
  3099. const_cast<RichTextLabel *>(this)->_validate_line_caches();
  3100. if (updating.load()) {
  3101. return false;
  3102. }
  3103. 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());
  3104. }
  3105. bool RichTextLabel::is_updating() const {
  3106. return updating.load() || validating.load();
  3107. }
  3108. void RichTextLabel::wait_until_finished() {
  3109. if (task != WorkerThreadPool::INVALID_TASK_ID) {
  3110. WorkerThreadPool::get_singleton()->wait_for_task_completion(task);
  3111. task = WorkerThreadPool::INVALID_TASK_ID;
  3112. }
  3113. }
  3114. void RichTextLabel::set_threaded(bool p_threaded) {
  3115. if (threaded != p_threaded) {
  3116. _stop_thread();
  3117. threaded = p_threaded;
  3118. queue_redraw();
  3119. }
  3120. }
  3121. bool RichTextLabel::is_threaded() const {
  3122. return threaded;
  3123. }
  3124. void RichTextLabel::set_progress_bar_delay(int p_delay_ms) {
  3125. progress_delay = p_delay_ms;
  3126. }
  3127. int RichTextLabel::get_progress_bar_delay() const {
  3128. return progress_delay;
  3129. }
  3130. _FORCE_INLINE_ float RichTextLabel::_update_scroll_exceeds(float p_total_height, float p_ctrl_height, float p_width, int p_idx, float p_old_scroll, float p_text_rect_height) {
  3131. updating_scroll = true;
  3132. float total_height = p_total_height;
  3133. bool exceeds = p_total_height > p_ctrl_height && scroll_active;
  3134. if (exceeds != scroll_visible) {
  3135. if (exceeds) {
  3136. scroll_visible = true;
  3137. scroll_w = vscroll->get_combined_minimum_size().width;
  3138. vscroll->show();
  3139. vscroll->set_anchor_and_offset(SIDE_LEFT, ANCHOR_END, -scroll_w);
  3140. } else {
  3141. scroll_visible = false;
  3142. scroll_w = 0;
  3143. }
  3144. main->first_resized_line.store(0);
  3145. total_height = 0;
  3146. for (int j = 0; j <= p_idx; j++) {
  3147. total_height = _resize_line(main, j, theme_cache.normal_font, theme_cache.normal_font_size, p_width - scroll_w, total_height);
  3148. main->first_resized_line.store(j);
  3149. }
  3150. }
  3151. vscroll->set_max(total_height);
  3152. vscroll->set_page(p_text_rect_height);
  3153. if (scroll_follow && scroll_following) {
  3154. vscroll->set_value(total_height);
  3155. } else {
  3156. vscroll->set_value(p_old_scroll);
  3157. }
  3158. updating_scroll = false;
  3159. return total_height;
  3160. }
  3161. bool RichTextLabel::_validate_line_caches() {
  3162. if (updating.load()) {
  3163. return false;
  3164. }
  3165. validating.store(true);
  3166. if (main->first_invalid_line.load() == (int)main->lines.size()) {
  3167. MutexLock data_lock(data_mutex);
  3168. Rect2 text_rect = _get_text_rect();
  3169. float ctrl_height = get_size().height;
  3170. // Update fonts.
  3171. float old_scroll = vscroll->get_value();
  3172. if (main->first_invalid_font_line.load() != (int)main->lines.size()) {
  3173. for (int i = main->first_invalid_font_line.load(); i < (int)main->lines.size(); i++) {
  3174. _update_line_font(main, i, theme_cache.normal_font, theme_cache.normal_font_size);
  3175. }
  3176. main->first_resized_line.store(main->first_invalid_font_line.load());
  3177. main->first_invalid_font_line.store(main->lines.size());
  3178. }
  3179. if (main->first_resized_line.load() == (int)main->lines.size()) {
  3180. vscroll->set_value(old_scroll);
  3181. validating.store(false);
  3182. if (!scroll_visible) {
  3183. vscroll->hide();
  3184. }
  3185. queue_accessibility_update();
  3186. return true;
  3187. }
  3188. // Resize lines without reshaping.
  3189. int fi = main->first_resized_line.load();
  3190. float total_height = (fi == 0) ? 0 : _calculate_line_vertical_offset(main->lines[fi - 1]);
  3191. for (int i = fi; i < (int)main->lines.size(); i++) {
  3192. total_height = _resize_line(main, i, theme_cache.normal_font, theme_cache.normal_font_size, text_rect.get_size().width - scroll_w, total_height);
  3193. total_height = _update_scroll_exceeds(total_height, ctrl_height, text_rect.get_size().width, i, old_scroll, text_rect.size.height);
  3194. main->first_resized_line.store(i);
  3195. }
  3196. main->first_resized_line.store(main->lines.size());
  3197. if (fit_content) {
  3198. update_minimum_size();
  3199. }
  3200. validating.store(false);
  3201. if (!scroll_visible) {
  3202. vscroll->hide();
  3203. }
  3204. queue_accessibility_update();
  3205. return true;
  3206. }
  3207. validating.store(false);
  3208. stop_thread.store(false);
  3209. if (threaded) {
  3210. updating.store(true);
  3211. loaded.store(true);
  3212. task = WorkerThreadPool::get_singleton()->add_template_task(this, &RichTextLabel::_thread_function, nullptr, true, vformat("RichTextLabelShape:%x", (int64_t)get_instance_id()));
  3213. set_physics_process_internal(true);
  3214. loading_started = OS::get_singleton()->get_ticks_msec();
  3215. queue_accessibility_update();
  3216. return false;
  3217. } else {
  3218. updating.store(true);
  3219. _process_line_caches();
  3220. if (!scroll_visible) {
  3221. vscroll->hide();
  3222. }
  3223. queue_accessibility_update();
  3224. queue_redraw();
  3225. return true;
  3226. }
  3227. }
  3228. void RichTextLabel::_process_line_caches() {
  3229. // Shape invalid lines.
  3230. if (!is_inside_tree()) {
  3231. updating.store(false);
  3232. return;
  3233. }
  3234. MutexLock data_lock(data_mutex);
  3235. Rect2 text_rect = _get_text_rect();
  3236. float ctrl_height = get_size().height;
  3237. int fi = main->first_invalid_line.load();
  3238. int total_chars = main->lines[fi].char_offset;
  3239. float old_scroll = vscroll->get_value();
  3240. float total_height = 0;
  3241. if (fi != 0) {
  3242. int sr = MIN(main->first_invalid_font_line.load(), main->first_resized_line.load());
  3243. // Update fonts.
  3244. for (int i = main->first_invalid_font_line.load(); i < fi; i++) {
  3245. _update_line_font(main, i, theme_cache.normal_font, theme_cache.normal_font_size);
  3246. main->first_invalid_font_line.store(i);
  3247. if (stop_thread.load()) {
  3248. updating.store(false);
  3249. return;
  3250. }
  3251. }
  3252. // Resize lines without reshaping.
  3253. if (sr != 0) {
  3254. total_height = _calculate_line_vertical_offset(main->lines[sr - 1]);
  3255. }
  3256. for (int i = sr; i < fi; i++) {
  3257. total_height = _resize_line(main, i, theme_cache.normal_font, theme_cache.normal_font_size, text_rect.get_size().width - scroll_w, total_height);
  3258. total_height = _update_scroll_exceeds(total_height, ctrl_height, text_rect.get_size().width, i, old_scroll, text_rect.size.height);
  3259. main->first_resized_line.store(i);
  3260. if (stop_thread.load()) {
  3261. updating.store(false);
  3262. return;
  3263. }
  3264. }
  3265. }
  3266. total_height = (fi == 0) ? 0 : _calculate_line_vertical_offset(main->lines[fi - 1]);
  3267. for (int i = fi; i < (int)main->lines.size(); i++) {
  3268. 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);
  3269. total_height = _update_scroll_exceeds(total_height, ctrl_height, text_rect.get_size().width, i, old_scroll, text_rect.size.height);
  3270. main->first_invalid_line.store(i);
  3271. main->first_resized_line.store(i);
  3272. main->first_invalid_font_line.store(i);
  3273. if (stop_thread.load()) {
  3274. updating.store(false);
  3275. return;
  3276. }
  3277. loaded.store(double(i) / double(main->lines.size()));
  3278. }
  3279. main->first_invalid_line.store(main->lines.size());
  3280. main->first_resized_line.store(main->lines.size());
  3281. main->first_invalid_font_line.store(main->lines.size());
  3282. updating.store(false);
  3283. if (fit_content) {
  3284. update_minimum_size();
  3285. }
  3286. emit_signal(SceneStringName(finished));
  3287. }
  3288. void RichTextLabel::_invalidate_current_line(ItemFrame *p_frame) {
  3289. if ((int)p_frame->lines.size() - 1 <= p_frame->first_invalid_line) {
  3290. p_frame->first_invalid_line = (int)p_frame->lines.size() - 1;
  3291. queue_accessibility_update();
  3292. }
  3293. }
  3294. void RichTextLabel::_texture_changed(RID p_item) {
  3295. Item *it = items.get_or_null(p_item);
  3296. if (it && it->type == ITEM_IMAGE) {
  3297. ItemImage *img = reinterpret_cast<ItemImage *>(it);
  3298. Size2 new_size = _get_image_size(img->image, img->rq_size.width, img->rq_size.height, img->region);
  3299. if (img->size != new_size) {
  3300. main->first_invalid_line.store(0);
  3301. img->size = new_size;
  3302. }
  3303. }
  3304. queue_redraw();
  3305. }
  3306. void RichTextLabel::add_text(const String &p_text) {
  3307. _stop_thread();
  3308. MutexLock data_lock(data_mutex);
  3309. if (current->type == ITEM_TABLE) {
  3310. return; //can't add anything here
  3311. }
  3312. int pos = 0;
  3313. String t = p_text.replace("\r\n", "\n");
  3314. while (pos < t.length()) {
  3315. int end = t.find_char('\n', pos);
  3316. String line;
  3317. bool eol = false;
  3318. if (end == -1) {
  3319. end = t.length();
  3320. } else {
  3321. eol = true;
  3322. }
  3323. if (pos == 0 && end == t.length()) {
  3324. line = t;
  3325. } else {
  3326. line = t.substr(pos, end - pos);
  3327. }
  3328. if (line.length() > 0) {
  3329. if (current->subitems.size() && current->subitems.back()->get()->type == ITEM_TEXT) {
  3330. //append text condition!
  3331. ItemText *ti = static_cast<ItemText *>(current->subitems.back()->get());
  3332. ti->text += line;
  3333. _invalidate_current_line(main);
  3334. } else {
  3335. //append item condition
  3336. ItemText *item = memnew(ItemText);
  3337. item->owner = get_instance_id();
  3338. item->rid = items.make_rid(item);
  3339. item->text = line;
  3340. _add_item(item, false);
  3341. }
  3342. }
  3343. if (eol) {
  3344. ItemNewline *item = memnew(ItemNewline);
  3345. item->owner = get_instance_id();
  3346. item->rid = items.make_rid(item);
  3347. item->line = current_frame->lines.size();
  3348. _add_item(item, false);
  3349. current_frame->lines.resize(current_frame->lines.size() + 1);
  3350. if (item->type != ITEM_NEWLINE) {
  3351. current_frame->lines[current_frame->lines.size() - 1].from = item;
  3352. }
  3353. _invalidate_current_line(current_frame);
  3354. }
  3355. pos = end + 1;
  3356. }
  3357. queue_redraw();
  3358. }
  3359. void RichTextLabel::_add_item(Item *p_item, bool p_enter, bool p_ensure_newline) {
  3360. if (!internal_stack_editing) {
  3361. stack_externally_modified = true;
  3362. }
  3363. if (p_enter && !parsing_bbcode.load() && !tag_stack.is_empty()) {
  3364. tag_stack.push_back(U"?");
  3365. }
  3366. p_item->parent = current;
  3367. p_item->E = current->subitems.push_back(p_item);
  3368. p_item->index = current_idx++;
  3369. p_item->char_ofs = current_char_ofs;
  3370. if (p_item->type == ITEM_TEXT) {
  3371. ItemText *t = static_cast<ItemText *>(p_item);
  3372. current_char_ofs += t->text.length();
  3373. } else if (p_item->type == ITEM_IMAGE) {
  3374. current_char_ofs++;
  3375. } else if (p_item->type == ITEM_NEWLINE) {
  3376. current_char_ofs++;
  3377. }
  3378. if (p_enter) {
  3379. current = p_item;
  3380. }
  3381. if (p_ensure_newline) {
  3382. Item *from = current_frame->lines[current_frame->lines.size() - 1].from;
  3383. // only create a new line for Item types that generate content/layout, ignore those that represent formatting/styling
  3384. if (_find_layout_subitem(from, p_item)) {
  3385. _invalidate_current_line(current_frame);
  3386. current_frame->lines.resize(current_frame->lines.size() + 1);
  3387. }
  3388. }
  3389. if (current_frame->lines[current_frame->lines.size() - 1].from == nullptr) {
  3390. current_frame->lines[current_frame->lines.size() - 1].from = p_item;
  3391. }
  3392. p_item->line = current_frame->lines.size() - 1;
  3393. _invalidate_current_line(current_frame);
  3394. if (fit_content) {
  3395. update_minimum_size();
  3396. }
  3397. queue_accessibility_update();
  3398. queue_redraw();
  3399. }
  3400. Size2 RichTextLabel::_get_image_size(const Ref<Texture2D> &p_image, int p_width, int p_height, const Rect2 &p_region) {
  3401. Size2 ret;
  3402. if (p_width > 0) {
  3403. // custom width
  3404. ret.width = p_width;
  3405. if (p_height > 0) {
  3406. // custom height
  3407. ret.height = p_height;
  3408. } else {
  3409. // calculate height to keep aspect ratio
  3410. if (p_region.has_area()) {
  3411. ret.height = p_region.get_size().height * p_width / p_region.get_size().width;
  3412. } else {
  3413. ret.height = p_image->get_height() * p_width / p_image->get_width();
  3414. }
  3415. }
  3416. } else {
  3417. if (p_height > 0) {
  3418. // custom height
  3419. ret.height = p_height;
  3420. // calculate width to keep aspect ratio
  3421. if (p_region.has_area()) {
  3422. ret.width = p_region.get_size().width * p_height / p_region.get_size().height;
  3423. } else {
  3424. ret.width = p_image->get_width() * p_height / p_image->get_height();
  3425. }
  3426. } else {
  3427. if (p_region.has_area()) {
  3428. // if the image has a region, keep the region size
  3429. ret = p_region.get_size();
  3430. } else {
  3431. // keep original width and height
  3432. ret = p_image->get_size();
  3433. }
  3434. }
  3435. }
  3436. return ret;
  3437. }
  3438. void RichTextLabel::add_image(const Ref<Texture2D> &p_image, int p_width, int p_height, const Color &p_color, InlineAlignment p_alignment, const Rect2 &p_region, const Variant &p_key, bool p_pad, const String &p_tooltip, bool p_size_in_percent, const String &p_alt_text) {
  3439. _stop_thread();
  3440. MutexLock data_lock(data_mutex);
  3441. if (current->type == ITEM_TABLE) {
  3442. return;
  3443. }
  3444. ERR_FAIL_COND(p_image.is_null());
  3445. ERR_FAIL_COND(p_image->get_width() == 0);
  3446. ERR_FAIL_COND(p_image->get_height() == 0);
  3447. ERR_FAIL_COND(p_width < 0);
  3448. ERR_FAIL_COND(p_height < 0);
  3449. ItemImage *item = memnew(ItemImage);
  3450. item->owner = get_instance_id();
  3451. item->rid = items.make_rid(item);
  3452. if (p_region.has_area()) {
  3453. Ref<AtlasTexture> atlas_tex = memnew(AtlasTexture);
  3454. atlas_tex->set_atlas(p_image);
  3455. atlas_tex->set_region(p_region);
  3456. item->image = atlas_tex;
  3457. } else {
  3458. item->image = p_image;
  3459. }
  3460. item->color = p_color;
  3461. item->inline_align = p_alignment;
  3462. item->rq_size = Size2(p_width, p_height);
  3463. item->region = p_region;
  3464. item->size = _get_image_size(p_image, p_width, p_height, p_region);
  3465. item->size_in_percent = p_size_in_percent;
  3466. item->pad = p_pad;
  3467. item->key = p_key;
  3468. item->tooltip = p_tooltip;
  3469. item->alt_text = p_alt_text;
  3470. item->image->connect_changed(callable_mp(this, &RichTextLabel::_texture_changed).bind(item->rid), CONNECT_REFERENCE_COUNTED);
  3471. _add_item(item, false);
  3472. update_configuration_warnings();
  3473. }
  3474. void RichTextLabel::update_image(const Variant &p_key, BitField<ImageUpdateMask> p_mask, const Ref<Texture2D> &p_image, int p_width, int p_height, const Color &p_color, InlineAlignment p_alignment, const Rect2 &p_region, bool p_pad, const String &p_tooltip, bool p_size_in_percent) {
  3475. _stop_thread();
  3476. MutexLock data_lock(data_mutex);
  3477. if (p_mask & UPDATE_TEXTURE) {
  3478. ERR_FAIL_COND(p_image.is_null());
  3479. ERR_FAIL_COND(p_image->get_width() == 0);
  3480. ERR_FAIL_COND(p_image->get_height() == 0);
  3481. }
  3482. ERR_FAIL_COND(p_width < 0);
  3483. ERR_FAIL_COND(p_height < 0);
  3484. bool reshape = false;
  3485. Item *it = main;
  3486. while (it) {
  3487. if (it->type == ITEM_IMAGE) {
  3488. ItemImage *it_img = static_cast<ItemImage *>(it);
  3489. if (it_img->key == p_key) {
  3490. ItemImage *item = it_img;
  3491. if (p_mask & UPDATE_REGION) {
  3492. item->region = p_region;
  3493. if (!(p_mask & UPDATE_TEXTURE)) {
  3494. // Update existing atlas texture region, if texture is not updated.
  3495. Ref<AtlasTexture> atlas_tex = item->image;
  3496. if (atlas_tex.is_valid()) {
  3497. atlas_tex->set_region(item->region);
  3498. }
  3499. }
  3500. }
  3501. if (p_mask & UPDATE_TEXTURE) {
  3502. if (item->image.is_valid()) {
  3503. item->image->disconnect_changed(callable_mp(this, &RichTextLabel::_texture_changed));
  3504. }
  3505. if (item->region.has_area()) {
  3506. Ref<AtlasTexture> atlas_tex = memnew(AtlasTexture);
  3507. atlas_tex->set_atlas(p_image);
  3508. atlas_tex->set_region(item->region);
  3509. item->image = atlas_tex;
  3510. } else {
  3511. item->image = p_image;
  3512. }
  3513. item->image->connect_changed(callable_mp(this, &RichTextLabel::_texture_changed).bind(item->rid), CONNECT_REFERENCE_COUNTED);
  3514. }
  3515. if (p_mask & UPDATE_COLOR) {
  3516. item->color = p_color;
  3517. }
  3518. if (p_mask & UPDATE_TOOLTIP) {
  3519. item->tooltip = p_tooltip;
  3520. }
  3521. if (p_mask & UPDATE_PAD) {
  3522. item->pad = p_pad;
  3523. }
  3524. if (p_mask & UPDATE_ALIGNMENT) {
  3525. if (item->inline_align != p_alignment) {
  3526. reshape = true;
  3527. item->inline_align = p_alignment;
  3528. }
  3529. }
  3530. if (p_mask & UPDATE_WIDTH_IN_PERCENT) {
  3531. if (item->size_in_percent != p_size_in_percent) {
  3532. reshape = true;
  3533. item->size_in_percent = p_size_in_percent;
  3534. }
  3535. }
  3536. if (p_mask & UPDATE_SIZE) {
  3537. if (p_width > 0) {
  3538. item->rq_size.width = p_width;
  3539. }
  3540. if (p_height > 0) {
  3541. item->rq_size.height = p_height;
  3542. }
  3543. }
  3544. if ((p_mask & UPDATE_SIZE) || (p_mask & UPDATE_REGION) || (p_mask & UPDATE_TEXTURE)) {
  3545. ERR_FAIL_COND(item->image.is_null());
  3546. ERR_FAIL_COND(item->image->get_width() == 0);
  3547. ERR_FAIL_COND(item->image->get_height() == 0);
  3548. Size2 new_size = _get_image_size(item->image, item->rq_size.width, item->rq_size.height, item->region);
  3549. if (item->size != new_size) {
  3550. reshape = true;
  3551. item->size = new_size;
  3552. }
  3553. }
  3554. }
  3555. }
  3556. it = _get_next_item(it, true);
  3557. }
  3558. if (reshape) {
  3559. main->first_invalid_line.store(0);
  3560. }
  3561. queue_redraw();
  3562. }
  3563. void RichTextLabel::add_newline() {
  3564. _stop_thread();
  3565. MutexLock data_lock(data_mutex);
  3566. if (current->type == ITEM_TABLE) {
  3567. return;
  3568. }
  3569. ItemNewline *item = memnew(ItemNewline);
  3570. item->owner = get_instance_id();
  3571. item->rid = items.make_rid(item);
  3572. item->line = current_frame->lines.size();
  3573. _add_item(item, false);
  3574. current_frame->lines.resize(current_frame->lines.size() + 1);
  3575. _invalidate_current_line(current_frame);
  3576. queue_redraw();
  3577. }
  3578. void RichTextLabel::_remove_frame(HashSet<Item *> &r_erase_list, ItemFrame *p_frame, int p_line, bool p_erase, int p_char_offset, int p_line_offset) {
  3579. Line &l = p_frame->lines[p_line];
  3580. Item *it_to = (p_line + 1 < (int)p_frame->lines.size()) ? p_frame->lines[p_line + 1].from : nullptr;
  3581. if (!p_erase) {
  3582. l.char_offset -= p_char_offset;
  3583. }
  3584. for (Item *it = l.from; it && it != it_to;) {
  3585. Item *next_it = _get_next_item(it);
  3586. it->line -= p_line_offset;
  3587. if (!p_erase) {
  3588. while (r_erase_list.has(it->parent)) {
  3589. it->E->erase();
  3590. it->parent = it->parent->parent;
  3591. it->E = it->parent->subitems.push_back(it);
  3592. }
  3593. }
  3594. if (it->type == ITEM_TABLE) {
  3595. ItemTable *table = static_cast<ItemTable *>(it);
  3596. for (List<Item *>::Element *sub_it = table->subitems.front(); sub_it; sub_it = sub_it->next()) {
  3597. ERR_CONTINUE(sub_it->get()->type != ITEM_FRAME); // Children should all be frames.
  3598. ItemFrame *frame = static_cast<ItemFrame *>(sub_it->get());
  3599. for (int i = 0; i < (int)frame->lines.size(); i++) {
  3600. _remove_frame(r_erase_list, frame, i, p_erase, p_char_offset, 0);
  3601. }
  3602. if (p_erase) {
  3603. r_erase_list.insert(frame);
  3604. } else {
  3605. frame->char_ofs -= p_char_offset;
  3606. }
  3607. }
  3608. }
  3609. if (p_erase) {
  3610. r_erase_list.insert(it);
  3611. } else {
  3612. it->char_ofs -= p_char_offset;
  3613. }
  3614. it = next_it;
  3615. }
  3616. }
  3617. bool RichTextLabel::remove_paragraph(int p_paragraph, bool p_no_invalidate) {
  3618. _stop_thread();
  3619. MutexLock data_lock(data_mutex);
  3620. if (p_paragraph >= (int)main->lines.size() || p_paragraph < 0) {
  3621. return false;
  3622. }
  3623. stack_externally_modified = true;
  3624. if (main->lines.size() == 1) {
  3625. // Clear all.
  3626. main->_clear_children();
  3627. current = main;
  3628. current_frame = main;
  3629. main->lines.clear();
  3630. main->lines.resize(1);
  3631. current_char_ofs = 0;
  3632. } else {
  3633. HashSet<Item *> erase_list;
  3634. Line &l = main->lines[p_paragraph];
  3635. int off = l.char_count;
  3636. for (int i = p_paragraph; i < (int)main->lines.size(); i++) {
  3637. if (i == p_paragraph) {
  3638. _remove_frame(erase_list, main, i, true, off, 0);
  3639. } else {
  3640. _remove_frame(erase_list, main, i, false, off, 1);
  3641. }
  3642. }
  3643. for (HashSet<Item *>::Iterator E = erase_list.begin(); E; ++E) {
  3644. Item *it = *E;
  3645. if (current_frame == it) {
  3646. current_frame = main;
  3647. }
  3648. if (current == it) {
  3649. current = main;
  3650. }
  3651. if (!erase_list.has(it->parent)) {
  3652. it->E->erase();
  3653. }
  3654. items.free(it->rid);
  3655. it->subitems.clear();
  3656. memdelete(it);
  3657. }
  3658. main->lines.remove_at(p_paragraph);
  3659. current_char_ofs -= off;
  3660. }
  3661. selection.click_frame = nullptr;
  3662. selection.click_item = nullptr;
  3663. selection.active = false;
  3664. if (is_processing_internal()) {
  3665. bool process_enabled = false;
  3666. Item *it = main;
  3667. while (it) {
  3668. Vector<ItemFX *> fx_stack;
  3669. _fetch_item_fx_stack(it, fx_stack);
  3670. if (fx_stack.size()) {
  3671. process_enabled = true;
  3672. break;
  3673. }
  3674. it = _get_next_item(it, true);
  3675. }
  3676. set_process_internal(process_enabled);
  3677. }
  3678. if (p_no_invalidate) {
  3679. // Do not invalidate cache, only update vertical offsets of the paragraphs after deleted one and scrollbar.
  3680. int to_line = main->first_invalid_line.load() - 1;
  3681. float total_height = (p_paragraph == 0) ? 0 : _calculate_line_vertical_offset(main->lines[p_paragraph - 1]);
  3682. for (int i = p_paragraph; i < to_line; i++) {
  3683. MutexLock lock(main->lines[to_line - 1].text_buf->get_mutex());
  3684. main->lines[i].offset.y = total_height;
  3685. total_height = _calculate_line_vertical_offset(main->lines[i]);
  3686. }
  3687. updating_scroll = true;
  3688. vscroll->set_max(total_height);
  3689. updating_scroll = false;
  3690. main->first_invalid_line.store(MAX(main->first_invalid_line.load() - 1, 0));
  3691. main->first_resized_line.store(MAX(main->first_resized_line.load() - 1, 0));
  3692. main->first_invalid_font_line.store(MAX(main->first_invalid_font_line.load() - 1, 0));
  3693. } else {
  3694. // Invalidate cache after the deleted paragraph.
  3695. main->first_invalid_line.store(MIN(main->first_invalid_line.load(), p_paragraph));
  3696. main->first_resized_line.store(MIN(main->first_resized_line.load(), p_paragraph));
  3697. main->first_invalid_font_line.store(MIN(main->first_invalid_font_line.load(), p_paragraph));
  3698. }
  3699. queue_redraw();
  3700. return true;
  3701. }
  3702. bool RichTextLabel::invalidate_paragraph(int p_paragraph) {
  3703. _stop_thread();
  3704. MutexLock data_lock(data_mutex);
  3705. if (p_paragraph >= (int)main->lines.size() || p_paragraph < 0) {
  3706. return false;
  3707. }
  3708. // Invalidate cache.
  3709. main->first_invalid_line.store(MIN(main->first_invalid_line.load(), p_paragraph));
  3710. main->first_resized_line.store(MIN(main->first_resized_line.load(), p_paragraph));
  3711. main->first_invalid_font_line.store(MIN(main->first_invalid_font_line.load(), p_paragraph));
  3712. _invalidate_accessibility();
  3713. if (is_inside_tree()) {
  3714. queue_accessibility_update();
  3715. }
  3716. queue_redraw();
  3717. update_configuration_warnings();
  3718. return true;
  3719. }
  3720. 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) {
  3721. _stop_thread();
  3722. MutexLock data_lock(data_mutex);
  3723. ERR_FAIL_COND(current->type == ITEM_TABLE);
  3724. ERR_FAIL_COND(p_string.is_empty());
  3725. ERR_FAIL_COND(p_font.is_null());
  3726. ERR_FAIL_COND(p_size <= 0);
  3727. ItemDropcap *item = memnew(ItemDropcap);
  3728. item->owner = get_instance_id();
  3729. item->rid = items.make_rid(item);
  3730. item->text = p_string.replace("\r\n", "\n");
  3731. item->font = p_font;
  3732. item->font_size = p_size;
  3733. item->color = p_color;
  3734. item->ol_size = p_ol_size;
  3735. item->ol_color = p_ol_color;
  3736. item->dropcap_margins = p_dropcap_margins;
  3737. p_font->connect_changed(callable_mp(this, &RichTextLabel::_invalidate_fonts), CONNECT_REFERENCE_COUNTED);
  3738. _add_item(item, false);
  3739. }
  3740. void RichTextLabel::_push_def_font_var(DefaultFont p_def_font, const Ref<Font> &p_font, int p_size) {
  3741. _stop_thread();
  3742. MutexLock data_lock(data_mutex);
  3743. ERR_FAIL_COND(current->type == ITEM_TABLE);
  3744. ItemFont *item = memnew(ItemFont);
  3745. item->owner = get_instance_id();
  3746. item->rid = items.make_rid(item);
  3747. item->def_font = p_def_font;
  3748. item->variation = true;
  3749. item->font = p_font;
  3750. item->font_size = p_size;
  3751. item->def_size = (p_size <= 0);
  3752. p_font->connect_changed(callable_mp(this, &RichTextLabel::_invalidate_fonts), CONNECT_REFERENCE_COUNTED);
  3753. _add_item(item, true);
  3754. }
  3755. void RichTextLabel::_push_def_font(DefaultFont p_def_font) {
  3756. _stop_thread();
  3757. MutexLock data_lock(data_mutex);
  3758. ERR_FAIL_COND(current->type == ITEM_TABLE);
  3759. ItemFont *item = memnew(ItemFont);
  3760. item->owner = get_instance_id();
  3761. item->rid = items.make_rid(item);
  3762. item->def_font = p_def_font;
  3763. item->def_size = true;
  3764. _add_item(item, true);
  3765. }
  3766. void RichTextLabel::push_font(const Ref<Font> &p_font, int p_size) {
  3767. _stop_thread();
  3768. MutexLock data_lock(data_mutex);
  3769. ERR_FAIL_COND(current->type == ITEM_TABLE);
  3770. ERR_FAIL_COND(p_font.is_null());
  3771. ItemFont *item = memnew(ItemFont);
  3772. item->owner = get_instance_id();
  3773. item->rid = items.make_rid(item);
  3774. item->font = p_font;
  3775. item->font_size = p_size;
  3776. p_font->connect_changed(callable_mp(this, &RichTextLabel::_invalidate_fonts), CONNECT_REFERENCE_COUNTED);
  3777. _add_item(item, true);
  3778. }
  3779. void RichTextLabel::_invalidate_fonts() {
  3780. _stop_thread();
  3781. main->first_invalid_font_line.store(0); // Invalidate all lines.
  3782. _invalidate_accessibility();
  3783. queue_accessibility_update();
  3784. queue_redraw();
  3785. }
  3786. void RichTextLabel::push_normal() {
  3787. ERR_FAIL_COND(theme_cache.normal_font.is_null());
  3788. _push_def_font(RTL_NORMAL_FONT);
  3789. }
  3790. void RichTextLabel::push_bold() {
  3791. ERR_FAIL_COND(theme_cache.bold_font.is_null());
  3792. ItemFont *item_font = _find_font(current);
  3793. _push_def_font((item_font && item_font->def_font == RTL_ITALICS_FONT) ? RTL_BOLD_ITALICS_FONT : RTL_BOLD_FONT);
  3794. }
  3795. void RichTextLabel::push_bold_italics() {
  3796. ERR_FAIL_COND(theme_cache.bold_italics_font.is_null());
  3797. _push_def_font(RTL_BOLD_ITALICS_FONT);
  3798. }
  3799. void RichTextLabel::push_italics() {
  3800. ERR_FAIL_COND(theme_cache.italics_font.is_null());
  3801. ItemFont *item_font = _find_font(current);
  3802. _push_def_font((item_font && item_font->def_font == RTL_BOLD_FONT) ? RTL_BOLD_ITALICS_FONT : RTL_ITALICS_FONT);
  3803. }
  3804. void RichTextLabel::push_mono() {
  3805. ERR_FAIL_COND(theme_cache.mono_font.is_null());
  3806. _push_def_font(RTL_MONO_FONT);
  3807. }
  3808. void RichTextLabel::push_font_size(int p_font_size) {
  3809. _stop_thread();
  3810. MutexLock data_lock(data_mutex);
  3811. ERR_FAIL_COND(current->type == ITEM_TABLE);
  3812. ItemFontSize *item = memnew(ItemFontSize);
  3813. item->owner = get_instance_id();
  3814. item->rid = items.make_rid(item);
  3815. item->font_size = p_font_size;
  3816. _add_item(item, true);
  3817. }
  3818. void RichTextLabel::push_outline_size(int p_ol_size) {
  3819. _stop_thread();
  3820. MutexLock data_lock(data_mutex);
  3821. ERR_FAIL_COND(current->type == ITEM_TABLE);
  3822. ItemOutlineSize *item = memnew(ItemOutlineSize);
  3823. item->owner = get_instance_id();
  3824. item->rid = items.make_rid(item);
  3825. item->outline_size = p_ol_size;
  3826. _add_item(item, true);
  3827. }
  3828. void RichTextLabel::push_color(const Color &p_color) {
  3829. _stop_thread();
  3830. MutexLock data_lock(data_mutex);
  3831. ERR_FAIL_COND(current->type == ITEM_TABLE);
  3832. ItemColor *item = memnew(ItemColor);
  3833. item->owner = get_instance_id();
  3834. item->rid = items.make_rid(item);
  3835. item->color = p_color;
  3836. _add_item(item, true);
  3837. }
  3838. void RichTextLabel::push_outline_color(const Color &p_color) {
  3839. _stop_thread();
  3840. MutexLock data_lock(data_mutex);
  3841. ERR_FAIL_COND(current->type == ITEM_TABLE);
  3842. ItemOutlineColor *item = memnew(ItemOutlineColor);
  3843. item->owner = get_instance_id();
  3844. item->rid = items.make_rid(item);
  3845. item->color = p_color;
  3846. _add_item(item, true);
  3847. }
  3848. void RichTextLabel::push_underline() {
  3849. _stop_thread();
  3850. MutexLock data_lock(data_mutex);
  3851. ERR_FAIL_COND(current->type == ITEM_TABLE);
  3852. ItemUnderline *item = memnew(ItemUnderline);
  3853. item->owner = get_instance_id();
  3854. item->rid = items.make_rid(item);
  3855. _add_item(item, true);
  3856. }
  3857. void RichTextLabel::push_strikethrough() {
  3858. _stop_thread();
  3859. MutexLock data_lock(data_mutex);
  3860. ERR_FAIL_COND(current->type == ITEM_TABLE);
  3861. ItemStrikethrough *item = memnew(ItemStrikethrough);
  3862. item->owner = get_instance_id();
  3863. item->rid = items.make_rid(item);
  3864. _add_item(item, true);
  3865. }
  3866. void RichTextLabel::push_paragraph(HorizontalAlignment p_alignment, Control::TextDirection p_direction, const String &p_language, TextServer::StructuredTextParser p_st_parser, BitField<TextServer::JustificationFlag> p_jst_flags, const PackedFloat32Array &p_tab_stops) {
  3867. _stop_thread();
  3868. MutexLock data_lock(data_mutex);
  3869. ERR_FAIL_COND(current->type == ITEM_TABLE);
  3870. ItemParagraph *item = memnew(ItemParagraph);
  3871. item->owner = get_instance_id();
  3872. item->rid = items.make_rid(item);
  3873. item->alignment = p_alignment;
  3874. item->direction = p_direction;
  3875. item->language = p_language;
  3876. item->st_parser = p_st_parser;
  3877. item->jst_flags = p_jst_flags;
  3878. item->tab_stops = p_tab_stops;
  3879. _add_item(item, true, true);
  3880. }
  3881. void RichTextLabel::push_indent(int p_level) {
  3882. _stop_thread();
  3883. MutexLock data_lock(data_mutex);
  3884. ERR_FAIL_COND(current->type == ITEM_TABLE);
  3885. ERR_FAIL_COND(p_level < 0);
  3886. ItemIndent *item = memnew(ItemIndent);
  3887. item->owner = get_instance_id();
  3888. item->rid = items.make_rid(item);
  3889. item->level = p_level;
  3890. _add_item(item, true, true);
  3891. }
  3892. void RichTextLabel::push_list(int p_level, ListType p_list, bool p_capitalize, const String &p_bullet) {
  3893. _stop_thread();
  3894. MutexLock data_lock(data_mutex);
  3895. ERR_FAIL_COND(current->type == ITEM_TABLE);
  3896. ERR_FAIL_COND(p_level < 0);
  3897. ItemList *item = memnew(ItemList);
  3898. item->owner = get_instance_id();
  3899. item->rid = items.make_rid(item);
  3900. item->list_type = p_list;
  3901. item->level = p_level;
  3902. item->capitalize = p_capitalize;
  3903. item->bullet = p_bullet;
  3904. _add_item(item, true, true);
  3905. }
  3906. void RichTextLabel::push_meta(const Variant &p_meta, MetaUnderline p_underline_mode, const String &p_tooltip) {
  3907. _stop_thread();
  3908. MutexLock data_lock(data_mutex);
  3909. ERR_FAIL_COND(current->type == ITEM_TABLE);
  3910. ItemMeta *item = memnew(ItemMeta);
  3911. item->owner = get_instance_id();
  3912. item->rid = items.make_rid(item);
  3913. item->meta = p_meta;
  3914. item->underline = p_underline_mode;
  3915. item->tooltip = p_tooltip;
  3916. _add_item(item, true);
  3917. }
  3918. void RichTextLabel::push_language(const String &p_language) {
  3919. _stop_thread();
  3920. MutexLock data_lock(data_mutex);
  3921. ERR_FAIL_COND(current->type == ITEM_TABLE);
  3922. ItemLanguage *item = memnew(ItemLanguage);
  3923. item->owner = get_instance_id();
  3924. item->rid = items.make_rid(item);
  3925. item->language = p_language;
  3926. _add_item(item, true);
  3927. }
  3928. void RichTextLabel::push_hint(const String &p_string) {
  3929. _stop_thread();
  3930. MutexLock data_lock(data_mutex);
  3931. ERR_FAIL_COND(current->type == ITEM_TABLE);
  3932. ItemHint *item = memnew(ItemHint);
  3933. item->owner = get_instance_id();
  3934. item->rid = items.make_rid(item);
  3935. item->description = p_string;
  3936. _add_item(item, true);
  3937. }
  3938. void RichTextLabel::push_table(int p_columns, InlineAlignment p_alignment, int p_align_to_row, const String &p_alt_text) {
  3939. _stop_thread();
  3940. MutexLock data_lock(data_mutex);
  3941. ERR_FAIL_COND(current->type == ITEM_TABLE);
  3942. ERR_FAIL_COND(p_columns < 1);
  3943. ItemTable *item = memnew(ItemTable);
  3944. item->owner = get_instance_id();
  3945. item->rid = items.make_rid(item);
  3946. item->name = p_alt_text;
  3947. item->columns.resize(p_columns);
  3948. item->total_width = 0;
  3949. item->inline_align = p_alignment;
  3950. item->align_to_row = p_align_to_row;
  3951. for (int i = 0; i < (int)item->columns.size(); i++) {
  3952. item->columns[i].expand = false;
  3953. item->columns[i].shrink = true;
  3954. item->columns[i].expand_ratio = 1;
  3955. }
  3956. _add_item(item, true, false);
  3957. }
  3958. void RichTextLabel::push_fade(int p_start_index, int p_length) {
  3959. _stop_thread();
  3960. MutexLock data_lock(data_mutex);
  3961. ERR_FAIL_COND(current->type == ITEM_TABLE);
  3962. ItemFade *item = memnew(ItemFade);
  3963. item->owner = get_instance_id();
  3964. item->rid = items.make_rid(item);
  3965. item->starting_index = p_start_index;
  3966. item->length = p_length;
  3967. _add_item(item, true);
  3968. }
  3969. void RichTextLabel::push_shake(int p_strength = 10, float p_rate = 24.0f, bool p_connected = true) {
  3970. _stop_thread();
  3971. MutexLock data_lock(data_mutex);
  3972. ERR_FAIL_COND(current->type == ITEM_TABLE);
  3973. ItemShake *item = memnew(ItemShake);
  3974. item->owner = get_instance_id();
  3975. item->rid = items.make_rid(item);
  3976. item->strength = p_strength;
  3977. item->rate = p_rate;
  3978. item->connected = p_connected;
  3979. _add_item(item, true);
  3980. }
  3981. void RichTextLabel::push_wave(float p_frequency = 1.0f, float p_amplitude = 10.0f, bool p_connected = true) {
  3982. _stop_thread();
  3983. MutexLock data_lock(data_mutex);
  3984. ERR_FAIL_COND(current->type == ITEM_TABLE);
  3985. ItemWave *item = memnew(ItemWave);
  3986. item->owner = get_instance_id();
  3987. item->rid = items.make_rid(item);
  3988. item->frequency = p_frequency;
  3989. item->amplitude = p_amplitude;
  3990. item->connected = p_connected;
  3991. _add_item(item, true);
  3992. }
  3993. void RichTextLabel::push_tornado(float p_frequency = 1.0f, float p_radius = 10.0f, bool p_connected = true) {
  3994. _stop_thread();
  3995. MutexLock data_lock(data_mutex);
  3996. ERR_FAIL_COND(current->type == ITEM_TABLE);
  3997. ItemTornado *item = memnew(ItemTornado);
  3998. item->owner = get_instance_id();
  3999. item->rid = items.make_rid(item);
  4000. item->frequency = p_frequency;
  4001. item->radius = p_radius;
  4002. item->connected = p_connected;
  4003. _add_item(item, true);
  4004. }
  4005. void RichTextLabel::push_rainbow(float p_saturation, float p_value, float p_frequency, float p_speed) {
  4006. _stop_thread();
  4007. MutexLock data_lock(data_mutex);
  4008. ERR_FAIL_COND(current->type == ITEM_TABLE);
  4009. ItemRainbow *item = memnew(ItemRainbow);
  4010. item->owner = get_instance_id();
  4011. item->rid = items.make_rid(item);
  4012. item->speed = p_speed;
  4013. item->frequency = p_frequency;
  4014. item->saturation = p_saturation;
  4015. item->value = p_value;
  4016. _add_item(item, true);
  4017. }
  4018. void RichTextLabel::push_pulse(const Color &p_color, float p_frequency, float p_ease) {
  4019. _stop_thread();
  4020. MutexLock data_lock(data_mutex);
  4021. ItemPulse *item = memnew(ItemPulse);
  4022. item->owner = get_instance_id();
  4023. item->rid = items.make_rid(item);
  4024. item->color = p_color;
  4025. item->frequency = p_frequency;
  4026. item->ease = p_ease;
  4027. _add_item(item, true);
  4028. }
  4029. void RichTextLabel::push_bgcolor(const Color &p_color) {
  4030. _stop_thread();
  4031. MutexLock data_lock(data_mutex);
  4032. ERR_FAIL_COND(current->type == ITEM_TABLE);
  4033. ItemBGColor *item = memnew(ItemBGColor);
  4034. item->owner = get_instance_id();
  4035. item->rid = items.make_rid(item);
  4036. item->color = p_color;
  4037. _add_item(item, true);
  4038. }
  4039. void RichTextLabel::push_fgcolor(const Color &p_color) {
  4040. _stop_thread();
  4041. MutexLock data_lock(data_mutex);
  4042. ERR_FAIL_COND(current->type == ITEM_TABLE);
  4043. ItemFGColor *item = memnew(ItemFGColor);
  4044. item->owner = get_instance_id();
  4045. item->rid = items.make_rid(item);
  4046. item->color = p_color;
  4047. _add_item(item, true);
  4048. }
  4049. void RichTextLabel::push_customfx(Ref<RichTextEffect> p_custom_effect, Dictionary p_environment) {
  4050. _stop_thread();
  4051. MutexLock data_lock(data_mutex);
  4052. ERR_FAIL_COND(current->type == ITEM_TABLE);
  4053. ItemCustomFX *item = memnew(ItemCustomFX);
  4054. item->owner = get_instance_id();
  4055. item->rid = items.make_rid(item);
  4056. item->custom_effect = p_custom_effect;
  4057. item->char_fx_transform->environment = p_environment;
  4058. _add_item(item, true);
  4059. set_process_internal(true);
  4060. }
  4061. void RichTextLabel::push_context() {
  4062. _stop_thread();
  4063. MutexLock data_lock(data_mutex);
  4064. ERR_FAIL_COND(current->type == ITEM_TABLE);
  4065. ItemContext *item = memnew(ItemContext);
  4066. item->owner = get_instance_id();
  4067. item->rid = items.make_rid(item);
  4068. _add_item(item, true);
  4069. }
  4070. void RichTextLabel::set_table_column_expand(int p_column, bool p_expand, int p_ratio, bool p_shrink) {
  4071. _stop_thread();
  4072. MutexLock data_lock(data_mutex);
  4073. ERR_FAIL_COND(current->type != ITEM_TABLE);
  4074. ItemTable *table = static_cast<ItemTable *>(current);
  4075. ERR_FAIL_INDEX(p_column, (int)table->columns.size());
  4076. table->columns[p_column].expand = p_expand;
  4077. table->columns[p_column].shrink = p_shrink;
  4078. table->columns[p_column].expand_ratio = p_ratio;
  4079. }
  4080. void RichTextLabel::set_table_column_name(int p_column, const String &p_name) {
  4081. _stop_thread();
  4082. MutexLock data_lock(data_mutex);
  4083. ERR_FAIL_COND(current->type != ITEM_TABLE);
  4084. ItemTable *table = static_cast<ItemTable *>(current);
  4085. ERR_FAIL_INDEX(p_column, (int)table->columns.size());
  4086. table->columns[p_column].name = p_name;
  4087. }
  4088. void RichTextLabel::set_cell_row_background_color(const Color &p_odd_row_bg, const Color &p_even_row_bg) {
  4089. _stop_thread();
  4090. MutexLock data_lock(data_mutex);
  4091. ERR_FAIL_COND(current->type != ITEM_FRAME);
  4092. ItemFrame *cell = static_cast<ItemFrame *>(current);
  4093. ERR_FAIL_COND(!cell->cell);
  4094. cell->odd_row_bg = p_odd_row_bg;
  4095. cell->even_row_bg = p_even_row_bg;
  4096. }
  4097. void RichTextLabel::set_cell_border_color(const Color &p_color) {
  4098. _stop_thread();
  4099. MutexLock data_lock(data_mutex);
  4100. ERR_FAIL_COND(current->type != ITEM_FRAME);
  4101. ItemFrame *cell = static_cast<ItemFrame *>(current);
  4102. ERR_FAIL_COND(!cell->cell);
  4103. cell->border = p_color;
  4104. }
  4105. void RichTextLabel::set_cell_size_override(const Size2 &p_min_size, const Size2 &p_max_size) {
  4106. _stop_thread();
  4107. MutexLock data_lock(data_mutex);
  4108. ERR_FAIL_COND(current->type != ITEM_FRAME);
  4109. ItemFrame *cell = static_cast<ItemFrame *>(current);
  4110. ERR_FAIL_COND(!cell->cell);
  4111. cell->min_size_over = p_min_size;
  4112. cell->max_size_over = p_max_size;
  4113. }
  4114. void RichTextLabel::set_cell_padding(const Rect2 &p_padding) {
  4115. _stop_thread();
  4116. MutexLock data_lock(data_mutex);
  4117. ERR_FAIL_COND(current->type != ITEM_FRAME);
  4118. ItemFrame *cell = static_cast<ItemFrame *>(current);
  4119. ERR_FAIL_COND(!cell->cell);
  4120. cell->padding = p_padding;
  4121. }
  4122. void RichTextLabel::push_cell() {
  4123. _stop_thread();
  4124. MutexLock data_lock(data_mutex);
  4125. ERR_FAIL_COND(current->type != ITEM_TABLE);
  4126. ItemFrame *item = memnew(ItemFrame);
  4127. item->owner = get_instance_id();
  4128. item->rid = items.make_rid(item);
  4129. item->parent_frame = current_frame;
  4130. _add_item(item, true);
  4131. current_frame = item;
  4132. item->cell = true;
  4133. item->lines.resize(1);
  4134. item->lines[0].from = nullptr;
  4135. item->first_invalid_line.store(0); // parent frame last line ???
  4136. queue_accessibility_update();
  4137. }
  4138. int RichTextLabel::get_current_table_column() const {
  4139. ERR_FAIL_COND_V(current->type != ITEM_TABLE, -1);
  4140. ItemTable *table = static_cast<ItemTable *>(current);
  4141. return table->subitems.size() % table->columns.size();
  4142. }
  4143. void RichTextLabel::pop() {
  4144. _stop_thread();
  4145. MutexLock data_lock(data_mutex);
  4146. ERR_FAIL_NULL(current->parent);
  4147. if (current->type == ITEM_FRAME) {
  4148. current_frame = static_cast<ItemFrame *>(current)->parent_frame;
  4149. }
  4150. current = current->parent;
  4151. if (!parsing_bbcode.load() && !tag_stack.is_empty()) {
  4152. tag_stack.pop_back();
  4153. }
  4154. }
  4155. void RichTextLabel::pop_context() {
  4156. _stop_thread();
  4157. MutexLock data_lock(data_mutex);
  4158. ERR_FAIL_NULL(current->parent);
  4159. while (current->parent && current != main) {
  4160. if (current->type == ITEM_FRAME) {
  4161. current_frame = static_cast<ItemFrame *>(current)->parent_frame;
  4162. } else if (current->type == ITEM_CONTEXT) {
  4163. if (!parsing_bbcode.load() && !tag_stack.is_empty()) {
  4164. tag_stack.pop_back();
  4165. }
  4166. current = current->parent;
  4167. return;
  4168. }
  4169. if (!parsing_bbcode.load() && !tag_stack.is_empty()) {
  4170. tag_stack.pop_back();
  4171. }
  4172. current = current->parent;
  4173. }
  4174. }
  4175. void RichTextLabel::pop_all() {
  4176. _stop_thread();
  4177. MutexLock data_lock(data_mutex);
  4178. current = main;
  4179. current_frame = main;
  4180. }
  4181. void RichTextLabel::clear() {
  4182. _stop_thread();
  4183. set_process_internal(false);
  4184. MutexLock data_lock(data_mutex);
  4185. stack_externally_modified = false;
  4186. tag_stack.clear();
  4187. main->_clear_children();
  4188. current = main;
  4189. current_frame = main;
  4190. main->lines.clear();
  4191. main->lines.resize(1);
  4192. main->first_invalid_line.store(0);
  4193. _invalidate_accessibility();
  4194. keyboard_focus_frame = nullptr;
  4195. keyboard_focus_line = 0;
  4196. keyboard_focus_item = nullptr;
  4197. selection.click_frame = nullptr;
  4198. selection.click_item = nullptr;
  4199. deselect();
  4200. current_idx = 1;
  4201. current_char_ofs = 0;
  4202. if (scroll_follow) {
  4203. scroll_following = true;
  4204. }
  4205. if (fit_content) {
  4206. update_minimum_size();
  4207. }
  4208. queue_accessibility_update();
  4209. update_configuration_warnings();
  4210. }
  4211. void RichTextLabel::set_tab_size(int p_spaces) {
  4212. if (tab_size == p_spaces) {
  4213. return;
  4214. }
  4215. _stop_thread();
  4216. tab_size = p_spaces;
  4217. main->first_resized_line.store(0);
  4218. _invalidate_accessibility();
  4219. queue_accessibility_update();
  4220. queue_redraw();
  4221. }
  4222. int RichTextLabel::get_tab_size() const {
  4223. return tab_size;
  4224. }
  4225. void RichTextLabel::set_fit_content(bool p_enabled) {
  4226. if (p_enabled == fit_content) {
  4227. return;
  4228. }
  4229. fit_content = p_enabled;
  4230. update_minimum_size();
  4231. }
  4232. bool RichTextLabel::is_fit_content_enabled() const {
  4233. return fit_content;
  4234. }
  4235. void RichTextLabel::set_meta_underline(bool p_underline) {
  4236. if (underline_meta == p_underline) {
  4237. return;
  4238. }
  4239. underline_meta = p_underline;
  4240. queue_redraw();
  4241. }
  4242. bool RichTextLabel::is_meta_underlined() const {
  4243. return underline_meta;
  4244. }
  4245. void RichTextLabel::set_hint_underline(bool p_underline) {
  4246. underline_hint = p_underline;
  4247. queue_redraw();
  4248. }
  4249. bool RichTextLabel::is_hint_underlined() const {
  4250. return underline_hint;
  4251. }
  4252. void RichTextLabel::set_offset(int p_pixel) {
  4253. vscroll->set_value(p_pixel);
  4254. queue_accessibility_update();
  4255. }
  4256. void RichTextLabel::set_scroll_active(bool p_active) {
  4257. if (scroll_active == p_active) {
  4258. return;
  4259. }
  4260. scroll_active = p_active;
  4261. vscroll->set_drag_node_enabled(p_active);
  4262. queue_redraw();
  4263. }
  4264. bool RichTextLabel::is_scroll_active() const {
  4265. return scroll_active;
  4266. }
  4267. void RichTextLabel::set_scroll_follow(bool p_follow) {
  4268. scroll_follow = p_follow;
  4269. if (!vscroll->is_visible_in_tree() || vscroll->get_value() > (vscroll->get_max() - vscroll->get_page() - 1)) {
  4270. scroll_following = true;
  4271. }
  4272. }
  4273. bool RichTextLabel::is_scroll_following() const {
  4274. return scroll_follow;
  4275. }
  4276. void RichTextLabel::parse_bbcode(const String &p_bbcode) {
  4277. clear();
  4278. append_text(p_bbcode);
  4279. }
  4280. String RichTextLabel::_get_tag_value(const String &p_tag) {
  4281. return p_tag.substr(p_tag.find_char('=') + 1);
  4282. }
  4283. int RichTextLabel::_find_unquoted(const String &p_src, char32_t p_chr, int p_from) {
  4284. if (p_from < 0) {
  4285. return -1;
  4286. }
  4287. const int len = p_src.length();
  4288. if (len == 0) {
  4289. return -1;
  4290. }
  4291. const char32_t *src = p_src.get_data();
  4292. bool in_single_quote = false;
  4293. bool in_double_quote = false;
  4294. for (int i = p_from; i < len; i++) {
  4295. if (in_double_quote) {
  4296. if (src[i] == '"') {
  4297. in_double_quote = false;
  4298. }
  4299. } else if (in_single_quote) {
  4300. if (src[i] == '\'') {
  4301. in_single_quote = false;
  4302. }
  4303. } else {
  4304. if (src[i] == '"') {
  4305. in_double_quote = true;
  4306. } else if (src[i] == '\'') {
  4307. in_single_quote = true;
  4308. } else if (src[i] == p_chr) {
  4309. return i;
  4310. }
  4311. }
  4312. }
  4313. return -1;
  4314. }
  4315. Vector<String> RichTextLabel::_split_unquoted(const String &p_src, char32_t p_splitter) {
  4316. Vector<String> ret;
  4317. if (p_src.is_empty()) {
  4318. return ret;
  4319. }
  4320. int from = 0;
  4321. int len = p_src.length();
  4322. while (true) {
  4323. int end = _find_unquoted(p_src, p_splitter, from);
  4324. if (end < 0) {
  4325. end = len;
  4326. }
  4327. if (end > from) {
  4328. ret.push_back(p_src.substr(from, end - from));
  4329. }
  4330. if (end == len) {
  4331. break;
  4332. }
  4333. from = end + 1;
  4334. }
  4335. return ret;
  4336. }
  4337. void RichTextLabel::append_text(const String &p_bbcode) {
  4338. _stop_thread();
  4339. MutexLock data_lock(data_mutex);
  4340. parsing_bbcode.store(true);
  4341. int pos = 0;
  4342. bool in_bold = false;
  4343. bool in_italics = false;
  4344. bool after_list_open_tag = false;
  4345. bool after_list_close_tag = false;
  4346. String bbcode = p_bbcode.replace("\r\n", "\n");
  4347. while (pos <= bbcode.length()) {
  4348. int brk_pos = bbcode.find_char('[', pos);
  4349. if (brk_pos < 0) {
  4350. brk_pos = bbcode.length();
  4351. }
  4352. String txt = brk_pos > pos ? bbcode.substr(pos, brk_pos - pos) : "";
  4353. // Trim the first newline character, it may be added later as needed.
  4354. if (after_list_close_tag || after_list_open_tag) {
  4355. txt = txt.trim_prefix("\n");
  4356. }
  4357. if (brk_pos == bbcode.length()) {
  4358. // For tags that are not properly closed.
  4359. if (txt.is_empty() && after_list_open_tag) {
  4360. txt = "\n";
  4361. }
  4362. if (!txt.is_empty()) {
  4363. add_text(txt);
  4364. }
  4365. break; //nothing else to add
  4366. }
  4367. int brk_end = _find_unquoted(bbcode, ']', brk_pos + 1);
  4368. if (brk_end == -1) {
  4369. //no close, add the rest
  4370. txt += bbcode.substr(brk_pos);
  4371. add_text(txt);
  4372. break;
  4373. }
  4374. String tag = bbcode.substr(brk_pos + 1, brk_end - brk_pos - 1);
  4375. Vector<String> split_tag_block = _split_unquoted(tag, ' ');
  4376. // Find optional parameters.
  4377. String bbcode_name;
  4378. typedef HashMap<String, String> OptionMap;
  4379. OptionMap bbcode_options;
  4380. if (!split_tag_block.is_empty()) {
  4381. bbcode_name = split_tag_block[0];
  4382. for (int i = 1; i < split_tag_block.size(); i++) {
  4383. const String &expr = split_tag_block[i];
  4384. int value_pos = expr.find_char('=');
  4385. if (value_pos > -1) {
  4386. bbcode_options[expr.substr(0, value_pos)] = expr.substr(value_pos + 1).unquote();
  4387. }
  4388. }
  4389. } else {
  4390. bbcode_name = tag;
  4391. }
  4392. // Find main parameter.
  4393. String bbcode_value;
  4394. int main_value_pos = bbcode_name.find_char('=');
  4395. if (main_value_pos > -1) {
  4396. bbcode_value = bbcode_name.substr(main_value_pos + 1);
  4397. bbcode_name = bbcode_name.substr(0, main_value_pos);
  4398. }
  4399. if (tag.begins_with("/") && tag_stack.size()) {
  4400. bool tag_ok = tag_stack.size() && tag_stack.front()->get() == tag.substr(1);
  4401. if (tag_stack.front()->get() == "b") {
  4402. in_bold = false;
  4403. }
  4404. if (tag_stack.front()->get() == "i") {
  4405. in_italics = false;
  4406. }
  4407. if ((tag_stack.front()->get() == "indent") || (tag_stack.front()->get() == "ol") || (tag_stack.front()->get() == "ul")) {
  4408. current_frame->indent_level--;
  4409. }
  4410. if (!tag_ok) {
  4411. txt += "[" + tag;
  4412. add_text(txt);
  4413. after_list_open_tag = false;
  4414. after_list_close_tag = false;
  4415. pos = brk_end;
  4416. continue;
  4417. }
  4418. if (txt.is_empty() && after_list_open_tag) {
  4419. txt = "\n"; // Make empty list have at least one item.
  4420. }
  4421. after_list_open_tag = false;
  4422. if (tag == "/ol" || tag == "/ul") {
  4423. if (!txt.is_empty()) {
  4424. // Make sure text ends with a newline character, that is, the last item
  4425. // will wrap at the end of block.
  4426. if (!txt.ends_with("\n")) {
  4427. txt += "\n";
  4428. }
  4429. } else if (!after_list_close_tag) {
  4430. txt = "\n"; // Make the innermost list item wrap at the end of lists.
  4431. }
  4432. after_list_close_tag = true;
  4433. } else {
  4434. after_list_close_tag = false;
  4435. }
  4436. if (!txt.is_empty()) {
  4437. add_text(txt);
  4438. }
  4439. tag_stack.pop_front();
  4440. pos = brk_end + 1;
  4441. if (tag != "/img" && tag != "/dropcap") {
  4442. pop();
  4443. }
  4444. continue;
  4445. }
  4446. if (tag == "ol" || tag.begins_with("ol ") || tag == "ul" || tag.begins_with("ul ")) {
  4447. if (txt.is_empty() && after_list_open_tag) {
  4448. txt = "\n"; // Make each list have at least one item at the beginning.
  4449. }
  4450. after_list_open_tag = true;
  4451. } else {
  4452. after_list_open_tag = false;
  4453. }
  4454. if (!txt.is_empty()) {
  4455. add_text(txt);
  4456. }
  4457. after_list_close_tag = false;
  4458. if (tag == "b") {
  4459. //use bold font
  4460. in_bold = true;
  4461. if (in_italics) {
  4462. _push_def_font(RTL_BOLD_ITALICS_FONT);
  4463. } else {
  4464. _push_def_font(RTL_BOLD_FONT);
  4465. }
  4466. pos = brk_end + 1;
  4467. tag_stack.push_front(tag);
  4468. } else if (tag == "i") {
  4469. //use italics font
  4470. in_italics = true;
  4471. if (in_bold) {
  4472. _push_def_font(RTL_BOLD_ITALICS_FONT);
  4473. } else {
  4474. _push_def_font(RTL_ITALICS_FONT);
  4475. }
  4476. pos = brk_end + 1;
  4477. tag_stack.push_front(tag);
  4478. } else if (tag == "code") {
  4479. //use monospace font
  4480. _push_def_font(RTL_MONO_FONT);
  4481. pos = brk_end + 1;
  4482. tag_stack.push_front(tag);
  4483. } else if (tag.begins_with("table=")) {
  4484. Vector<String> subtag = _split_unquoted(_get_tag_value(tag), U',');
  4485. _normalize_subtags(subtag);
  4486. int columns = (subtag.is_empty()) ? 1 : subtag[0].to_int();
  4487. if (columns < 1) {
  4488. columns = 1;
  4489. }
  4490. int alignment = INLINE_ALIGNMENT_TOP;
  4491. if (subtag.size() > 2) {
  4492. if (subtag[1] == "top" || subtag[1] == "t") {
  4493. alignment = INLINE_ALIGNMENT_TOP_TO;
  4494. } else if (subtag[1] == "center" || subtag[1] == "c") {
  4495. alignment = INLINE_ALIGNMENT_CENTER_TO;
  4496. } else if (subtag[1] == "baseline" || subtag[1] == "l") {
  4497. alignment = INLINE_ALIGNMENT_BASELINE_TO;
  4498. } else if (subtag[1] == "bottom" || subtag[1] == "b") {
  4499. alignment = INLINE_ALIGNMENT_BOTTOM_TO;
  4500. }
  4501. if (subtag[2] == "top" || subtag[2] == "t") {
  4502. alignment |= INLINE_ALIGNMENT_TO_TOP;
  4503. } else if (subtag[2] == "center" || subtag[2] == "c") {
  4504. alignment |= INLINE_ALIGNMENT_TO_CENTER;
  4505. } else if (subtag[2] == "baseline" || subtag[2] == "l") {
  4506. alignment |= INLINE_ALIGNMENT_TO_BASELINE;
  4507. } else if (subtag[2] == "bottom" || subtag[2] == "b") {
  4508. alignment |= INLINE_ALIGNMENT_TO_BOTTOM;
  4509. }
  4510. } else if (subtag.size() > 1) {
  4511. if (subtag[1] == "top" || subtag[1] == "t") {
  4512. alignment = INLINE_ALIGNMENT_TOP;
  4513. } else if (subtag[1] == "center" || subtag[1] == "c") {
  4514. alignment = INLINE_ALIGNMENT_CENTER;
  4515. } else if (subtag[1] == "bottom" || subtag[1] == "b") {
  4516. alignment = INLINE_ALIGNMENT_BOTTOM;
  4517. }
  4518. }
  4519. int row = -1;
  4520. if (subtag.size() > 3) {
  4521. row = subtag[3].to_int();
  4522. }
  4523. OptionMap::Iterator alt_text_option = bbcode_options.find("name");
  4524. String alt_text;
  4525. if (alt_text_option) {
  4526. alt_text = alt_text_option->value;
  4527. }
  4528. push_table(columns, (InlineAlignment)alignment, row, alt_text);
  4529. pos = brk_end + 1;
  4530. tag_stack.push_front("table");
  4531. } else if (tag == "cell") {
  4532. push_cell();
  4533. pos = brk_end + 1;
  4534. tag_stack.push_front(tag);
  4535. } else if (tag.begins_with("cell=")) {
  4536. int ratio = _get_tag_value(tag).to_int();
  4537. if (ratio < 1) {
  4538. ratio = 1;
  4539. }
  4540. set_table_column_expand(get_current_table_column(), true, ratio);
  4541. push_cell();
  4542. pos = brk_end + 1;
  4543. tag_stack.push_front("cell");
  4544. } else if (tag.begins_with("cell ")) {
  4545. bool shrink = true;
  4546. OptionMap::Iterator shrink_option = bbcode_options.find("shrink");
  4547. if (shrink_option) {
  4548. shrink = (shrink_option->value == "true");
  4549. }
  4550. OptionMap::Iterator expand_option = bbcode_options.find("expand");
  4551. if (expand_option) {
  4552. int ratio = expand_option->value.to_int();
  4553. if (ratio < 1) {
  4554. ratio = 1;
  4555. }
  4556. set_table_column_expand(get_current_table_column(), true, ratio, shrink);
  4557. }
  4558. push_cell();
  4559. const Color fallback_color = Color(0, 0, 0, 0);
  4560. OptionMap::Iterator border_option = bbcode_options.find("border");
  4561. if (border_option) {
  4562. Color color = Color::from_string(border_option->value, fallback_color);
  4563. set_cell_border_color(color);
  4564. }
  4565. OptionMap::Iterator bg_option = bbcode_options.find("bg");
  4566. if (bg_option) {
  4567. Vector<String> subtag_b = _split_unquoted(bg_option->value, U',');
  4568. _normalize_subtags(subtag_b);
  4569. if (subtag_b.size() == 2) {
  4570. Color color1 = Color::from_string(subtag_b[0], fallback_color);
  4571. Color color2 = Color::from_string(subtag_b[1], fallback_color);
  4572. set_cell_row_background_color(color1, color2);
  4573. }
  4574. if (subtag_b.size() == 1) {
  4575. Color color1 = Color::from_string(bg_option->value, fallback_color);
  4576. set_cell_row_background_color(color1, color1);
  4577. }
  4578. }
  4579. OptionMap::Iterator padding_option = bbcode_options.find("padding");
  4580. if (padding_option) {
  4581. Vector<String> subtag_b = _split_unquoted(padding_option->value, U',');
  4582. _normalize_subtags(subtag_b);
  4583. if (subtag_b.size() == 4) {
  4584. set_cell_padding(Rect2(subtag_b[0].to_float(), subtag_b[1].to_float(), subtag_b[2].to_float(), subtag_b[3].to_float()));
  4585. }
  4586. }
  4587. pos = brk_end + 1;
  4588. tag_stack.push_front("cell");
  4589. } else if (tag == "u") {
  4590. //use underline
  4591. push_underline();
  4592. pos = brk_end + 1;
  4593. tag_stack.push_front(tag);
  4594. } else if (tag == "s") {
  4595. //use strikethrough
  4596. push_strikethrough();
  4597. pos = brk_end + 1;
  4598. tag_stack.push_front(tag);
  4599. } else if (tag.begins_with("char=")) {
  4600. int32_t char_code = _get_tag_value(tag).hex_to_int();
  4601. add_text(String::chr(char_code));
  4602. pos = brk_end + 1;
  4603. } else if (tag == "lb") {
  4604. add_text("[");
  4605. pos = brk_end + 1;
  4606. } else if (tag == "rb") {
  4607. add_text("]");
  4608. pos = brk_end + 1;
  4609. } else if (tag == "lrm") {
  4610. add_text(String::chr(0x200E));
  4611. pos = brk_end + 1;
  4612. } else if (tag == "rlm") {
  4613. add_text(String::chr(0x200F));
  4614. pos = brk_end + 1;
  4615. } else if (tag == "lre") {
  4616. add_text(String::chr(0x202A));
  4617. pos = brk_end + 1;
  4618. } else if (tag == "rle") {
  4619. add_text(String::chr(0x202B));
  4620. pos = brk_end + 1;
  4621. } else if (tag == "lro") {
  4622. add_text(String::chr(0x202D));
  4623. pos = brk_end + 1;
  4624. } else if (tag == "rlo") {
  4625. add_text(String::chr(0x202E));
  4626. pos = brk_end + 1;
  4627. } else if (tag == "pdf") {
  4628. add_text(String::chr(0x202C));
  4629. pos = brk_end + 1;
  4630. } else if (tag == "alm") {
  4631. add_text(String::chr(0x061c));
  4632. pos = brk_end + 1;
  4633. } else if (tag == "lri") {
  4634. add_text(String::chr(0x2066));
  4635. pos = brk_end + 1;
  4636. } else if (tag == "rli") {
  4637. add_text(String::chr(0x2027));
  4638. pos = brk_end + 1;
  4639. } else if (tag == "fsi") {
  4640. add_text(String::chr(0x2068));
  4641. pos = brk_end + 1;
  4642. } else if (tag == "pdi") {
  4643. add_text(String::chr(0x2069));
  4644. pos = brk_end + 1;
  4645. } else if (tag == "zwj") {
  4646. add_text(String::chr(0x200D));
  4647. pos = brk_end + 1;
  4648. } else if (tag == "zwnj") {
  4649. add_text(String::chr(0x200C));
  4650. pos = brk_end + 1;
  4651. } else if (tag == "wj") {
  4652. add_text(String::chr(0x2060));
  4653. pos = brk_end + 1;
  4654. } else if (tag == "shy") {
  4655. add_text(String::chr(0x00AD));
  4656. pos = brk_end + 1;
  4657. } else if (tag == "center") {
  4658. push_paragraph(HORIZONTAL_ALIGNMENT_CENTER, text_direction, language, st_parser, default_jst_flags, default_tab_stops);
  4659. pos = brk_end + 1;
  4660. tag_stack.push_front(tag);
  4661. } else if (tag == "fill") {
  4662. push_paragraph(HORIZONTAL_ALIGNMENT_FILL, text_direction, language, st_parser, default_jst_flags, default_tab_stops);
  4663. pos = brk_end + 1;
  4664. tag_stack.push_front(tag);
  4665. } else if (tag == "left") {
  4666. push_paragraph(HORIZONTAL_ALIGNMENT_LEFT, text_direction, language, st_parser, default_jst_flags, default_tab_stops);
  4667. pos = brk_end + 1;
  4668. tag_stack.push_front(tag);
  4669. } else if (tag == "right") {
  4670. push_paragraph(HORIZONTAL_ALIGNMENT_RIGHT, text_direction, language, st_parser, default_jst_flags, default_tab_stops);
  4671. pos = brk_end + 1;
  4672. tag_stack.push_front(tag);
  4673. } else if (tag == "ul") {
  4674. current_frame->indent_level++;
  4675. push_list(current_frame->indent_level, LIST_DOTS, false);
  4676. pos = brk_end + 1;
  4677. tag_stack.push_front(tag);
  4678. } else if (tag.begins_with("ul bullet=")) {
  4679. String bullet = _get_tag_value(tag);
  4680. current_frame->indent_level++;
  4681. push_list(current_frame->indent_level, LIST_DOTS, false, bullet);
  4682. pos = brk_end + 1;
  4683. tag_stack.push_front("ul");
  4684. } else if ((tag == "ol") || (tag == "ol type=1")) {
  4685. current_frame->indent_level++;
  4686. push_list(current_frame->indent_level, LIST_NUMBERS, false);
  4687. pos = brk_end + 1;
  4688. tag_stack.push_front("ol");
  4689. } else if (tag == "ol type=a") {
  4690. current_frame->indent_level++;
  4691. push_list(current_frame->indent_level, LIST_LETTERS, false);
  4692. pos = brk_end + 1;
  4693. tag_stack.push_front("ol");
  4694. } else if (tag == "ol type=A") {
  4695. current_frame->indent_level++;
  4696. push_list(current_frame->indent_level, LIST_LETTERS, true);
  4697. pos = brk_end + 1;
  4698. tag_stack.push_front("ol");
  4699. } else if (tag == "ol type=i") {
  4700. current_frame->indent_level++;
  4701. push_list(current_frame->indent_level, LIST_ROMAN, false);
  4702. pos = brk_end + 1;
  4703. tag_stack.push_front("ol");
  4704. } else if (tag == "ol type=I") {
  4705. current_frame->indent_level++;
  4706. push_list(current_frame->indent_level, LIST_ROMAN, true);
  4707. pos = brk_end + 1;
  4708. tag_stack.push_front("ol");
  4709. } else if (tag == "indent") {
  4710. current_frame->indent_level++;
  4711. push_indent(current_frame->indent_level);
  4712. pos = brk_end + 1;
  4713. tag_stack.push_front(tag);
  4714. } else if (tag.begins_with("lang=")) {
  4715. String lang = _get_tag_value(tag).unquote();
  4716. push_language(lang);
  4717. pos = brk_end + 1;
  4718. tag_stack.push_front("lang");
  4719. } else if (tag == "br") {
  4720. add_text("\r");
  4721. pos = brk_end + 1;
  4722. } else if (tag == "p") {
  4723. push_paragraph(HORIZONTAL_ALIGNMENT_LEFT);
  4724. pos = brk_end + 1;
  4725. tag_stack.push_front("p");
  4726. } else if (tag.begins_with("p ")) {
  4727. HorizontalAlignment alignment = HORIZONTAL_ALIGNMENT_LEFT;
  4728. Control::TextDirection dir = Control::TEXT_DIRECTION_INHERITED;
  4729. String lang = language;
  4730. PackedFloat32Array tab_stops = default_tab_stops;
  4731. TextServer::StructuredTextParser st_parser_type = TextServer::STRUCTURED_TEXT_DEFAULT;
  4732. BitField<TextServer::JustificationFlag> jst_flags = default_jst_flags;
  4733. OptionMap::Iterator justification_flags_option = bbcode_options.find("justification_flags");
  4734. if (!justification_flags_option) {
  4735. justification_flags_option = bbcode_options.find("jst");
  4736. }
  4737. if (justification_flags_option) {
  4738. Vector<String> subtag_b = _split_unquoted(justification_flags_option->value, U',');
  4739. jst_flags = 0; // Clear flags.
  4740. for (const String &E : subtag_b) {
  4741. if (E == "kashida" || E == "k") {
  4742. jst_flags.set_flag(TextServer::JUSTIFICATION_KASHIDA);
  4743. } else if (E == "word" || E == "w") {
  4744. jst_flags.set_flag(TextServer::JUSTIFICATION_WORD_BOUND);
  4745. } else if (E == "trim" || E == "tr") {
  4746. jst_flags.set_flag(TextServer::JUSTIFICATION_TRIM_EDGE_SPACES);
  4747. } else if (E == "after_last_tab" || E == "lt") {
  4748. jst_flags.set_flag(TextServer::JUSTIFICATION_AFTER_LAST_TAB);
  4749. } else if (E == "skip_last" || E == "sl") {
  4750. jst_flags.set_flag(TextServer::JUSTIFICATION_SKIP_LAST_LINE);
  4751. } else if (E == "skip_last_with_chars" || E == "sv") {
  4752. jst_flags.set_flag(TextServer::JUSTIFICATION_SKIP_LAST_LINE_WITH_VISIBLE_CHARS);
  4753. } else if (E == "do_not_skip_single" || E == "ns") {
  4754. jst_flags.set_flag(TextServer::JUSTIFICATION_DO_NOT_SKIP_SINGLE_LINE);
  4755. }
  4756. }
  4757. }
  4758. OptionMap::Iterator tab_stops_option = bbcode_options.find("tab_stops");
  4759. if (tab_stops_option) {
  4760. Vector<String> splitters;
  4761. splitters.push_back(",");
  4762. splitters.push_back(";");
  4763. tab_stops = tab_stops_option->value.split_floats_mk(splitters);
  4764. }
  4765. OptionMap::Iterator align_option = bbcode_options.find("align");
  4766. if (align_option) {
  4767. if (align_option->value == "l" || align_option->value == "left") {
  4768. alignment = HORIZONTAL_ALIGNMENT_LEFT;
  4769. } else if (align_option->value == "c" || align_option->value == "center") {
  4770. alignment = HORIZONTAL_ALIGNMENT_CENTER;
  4771. } else if (align_option->value == "r" || align_option->value == "right") {
  4772. alignment = HORIZONTAL_ALIGNMENT_RIGHT;
  4773. } else if (align_option->value == "f" || align_option->value == "fill") {
  4774. alignment = HORIZONTAL_ALIGNMENT_FILL;
  4775. }
  4776. }
  4777. OptionMap::Iterator direction_option = bbcode_options.find("direction");
  4778. if (!direction_option) {
  4779. direction_option = bbcode_options.find("dir");
  4780. }
  4781. if (direction_option) {
  4782. if (direction_option->value == "a" || direction_option->value == "auto") {
  4783. dir = Control::TEXT_DIRECTION_AUTO;
  4784. } else if (direction_option->value == "l" || direction_option->value == "ltr") {
  4785. dir = Control::TEXT_DIRECTION_LTR;
  4786. } else if (direction_option->value == "r" || direction_option->value == "rtl") {
  4787. dir = Control::TEXT_DIRECTION_RTL;
  4788. }
  4789. }
  4790. OptionMap::Iterator language_option = bbcode_options.find("language");
  4791. if (!language_option) {
  4792. language_option = bbcode_options.find("lang");
  4793. }
  4794. if (language_option) {
  4795. lang = language_option->value;
  4796. }
  4797. OptionMap::Iterator bidi_override_option = bbcode_options.find("bidi_override");
  4798. if (!bidi_override_option) {
  4799. bidi_override_option = bbcode_options.find("st");
  4800. }
  4801. if (bidi_override_option) {
  4802. if (bidi_override_option->value == "d" || bidi_override_option->value == "default") {
  4803. st_parser_type = TextServer::STRUCTURED_TEXT_DEFAULT;
  4804. } else if (bidi_override_option->value == "u" || bidi_override_option->value == "uri") {
  4805. st_parser_type = TextServer::STRUCTURED_TEXT_URI;
  4806. } else if (bidi_override_option->value == "f" || bidi_override_option->value == "file") {
  4807. st_parser_type = TextServer::STRUCTURED_TEXT_FILE;
  4808. } else if (bidi_override_option->value == "e" || bidi_override_option->value == "email") {
  4809. st_parser_type = TextServer::STRUCTURED_TEXT_EMAIL;
  4810. } else if (bidi_override_option->value == "l" || bidi_override_option->value == "list") {
  4811. st_parser_type = TextServer::STRUCTURED_TEXT_LIST;
  4812. } else if (bidi_override_option->value == "n" || bidi_override_option->value == "gdscript") {
  4813. st_parser_type = TextServer::STRUCTURED_TEXT_GDSCRIPT;
  4814. } else if (bidi_override_option->value == "c" || bidi_override_option->value == "custom") {
  4815. st_parser_type = TextServer::STRUCTURED_TEXT_CUSTOM;
  4816. }
  4817. }
  4818. push_paragraph(alignment, dir, lang, st_parser_type, jst_flags, tab_stops);
  4819. pos = brk_end + 1;
  4820. tag_stack.push_front("p");
  4821. } else if (tag == "url") {
  4822. int end = bbcode.find_char('[', brk_end);
  4823. if (end == -1) {
  4824. end = bbcode.length();
  4825. }
  4826. String url = bbcode.substr(brk_end + 1, end - brk_end - 1).unquote();
  4827. push_meta(url, META_UNDERLINE_ALWAYS);
  4828. pos = brk_end + 1;
  4829. tag_stack.push_front(tag);
  4830. } else if (tag.begins_with("url ")) {
  4831. String url;
  4832. MetaUnderline underline = META_UNDERLINE_ALWAYS;
  4833. String tooltip;
  4834. OptionMap::Iterator underline_option = bbcode_options.find("underline");
  4835. if (underline_option) {
  4836. if (underline_option->value == "never") {
  4837. underline = META_UNDERLINE_NEVER;
  4838. } else if (underline_option->value == "always") {
  4839. underline = META_UNDERLINE_ALWAYS;
  4840. } else if (underline_option->value == "hover") {
  4841. underline = META_UNDERLINE_ON_HOVER;
  4842. }
  4843. }
  4844. OptionMap::Iterator tooltip_option = bbcode_options.find("tooltip");
  4845. if (tooltip_option) {
  4846. tooltip = tooltip_option->value;
  4847. }
  4848. OptionMap::Iterator href_option = bbcode_options.find("href");
  4849. if (href_option) {
  4850. url = href_option->value;
  4851. }
  4852. push_meta(url, underline, tooltip);
  4853. pos = brk_end + 1;
  4854. tag_stack.push_front("url");
  4855. } else if (tag.begins_with("url=")) {
  4856. String url = _get_tag_value(tag).unquote();
  4857. push_meta(url, META_UNDERLINE_ALWAYS);
  4858. pos = brk_end + 1;
  4859. tag_stack.push_front("url");
  4860. } else if (tag.begins_with("hint=")) {
  4861. String description = _get_tag_value(tag).unquote();
  4862. push_hint(description);
  4863. pos = brk_end + 1;
  4864. tag_stack.push_front("hint");
  4865. } else if (tag.begins_with("dropcap")) {
  4866. int fs = theme_cache.normal_font_size * 3;
  4867. Ref<Font> f = theme_cache.normal_font;
  4868. Color color = theme_cache.default_color;
  4869. Color outline_color = theme_cache.font_outline_color;
  4870. int outline_size = theme_cache.outline_size;
  4871. Rect2 dropcap_margins;
  4872. OptionMap::Iterator font_option = bbcode_options.find("font");
  4873. if (!font_option) {
  4874. font_option = bbcode_options.find("f");
  4875. }
  4876. if (font_option) {
  4877. const String &fnt = font_option->value;
  4878. Ref<Font> font = ResourceLoader::load(fnt, "Font");
  4879. if (font.is_valid()) {
  4880. f = font;
  4881. }
  4882. }
  4883. OptionMap::Iterator font_size_option = bbcode_options.find("font_size");
  4884. if (font_size_option) {
  4885. fs = font_size_option->value.to_int();
  4886. }
  4887. OptionMap::Iterator margins_option = bbcode_options.find("margins");
  4888. if (margins_option) {
  4889. Vector<String> subtag_b = _split_unquoted(margins_option->value, U',');
  4890. _normalize_subtags(subtag_b);
  4891. if (subtag_b.size() == 4) {
  4892. dropcap_margins.position.x = subtag_b[0].to_float();
  4893. dropcap_margins.position.y = subtag_b[1].to_float();
  4894. dropcap_margins.size.x = subtag_b[2].to_float();
  4895. dropcap_margins.size.y = subtag_b[3].to_float();
  4896. }
  4897. }
  4898. OptionMap::Iterator outline_size_option = bbcode_options.find("outline_size");
  4899. if (outline_size_option) {
  4900. outline_size = outline_size_option->value.to_int();
  4901. }
  4902. OptionMap::Iterator color_option = bbcode_options.find("color");
  4903. if (color_option) {
  4904. color = Color::from_string(color_option->value, color);
  4905. }
  4906. OptionMap::Iterator outline_color_option = bbcode_options.find("outline_color");
  4907. if (outline_color_option) {
  4908. outline_color = Color::from_string(outline_color_option->value, outline_color);
  4909. }
  4910. int end = bbcode.find_char('[', brk_end);
  4911. if (end == -1) {
  4912. end = bbcode.length();
  4913. }
  4914. String dc_txt = bbcode.substr(brk_end + 1, end - brk_end - 1);
  4915. push_dropcap(dc_txt, f, fs, dropcap_margins, color, outline_size, outline_color);
  4916. pos = end;
  4917. tag_stack.push_front(bbcode_name);
  4918. } else if (tag.begins_with("img")) {
  4919. int alignment = INLINE_ALIGNMENT_CENTER;
  4920. if (tag.begins_with("img=")) {
  4921. Vector<String> subtag = _split_unquoted(_get_tag_value(tag), U',');
  4922. _normalize_subtags(subtag);
  4923. if (subtag.size() > 1) {
  4924. if (subtag[0] == "top" || subtag[0] == "t") {
  4925. alignment = INLINE_ALIGNMENT_TOP_TO;
  4926. } else if (subtag[0] == "center" || subtag[0] == "c") {
  4927. alignment = INLINE_ALIGNMENT_CENTER_TO;
  4928. } else if (subtag[0] == "bottom" || subtag[0] == "b") {
  4929. alignment = INLINE_ALIGNMENT_BOTTOM_TO;
  4930. }
  4931. if (subtag[1] == "top" || subtag[1] == "t") {
  4932. alignment |= INLINE_ALIGNMENT_TO_TOP;
  4933. } else if (subtag[1] == "center" || subtag[1] == "c") {
  4934. alignment |= INLINE_ALIGNMENT_TO_CENTER;
  4935. } else if (subtag[1] == "baseline" || subtag[1] == "l") {
  4936. alignment |= INLINE_ALIGNMENT_TO_BASELINE;
  4937. } else if (subtag[1] == "bottom" || subtag[1] == "b") {
  4938. alignment |= INLINE_ALIGNMENT_TO_BOTTOM;
  4939. }
  4940. } else if (subtag.size() > 0) {
  4941. if (subtag[0] == "top" || subtag[0] == "t") {
  4942. alignment = INLINE_ALIGNMENT_TOP;
  4943. } else if (subtag[0] == "center" || subtag[0] == "c") {
  4944. alignment = INLINE_ALIGNMENT_CENTER;
  4945. } else if (subtag[0] == "bottom" || subtag[0] == "b") {
  4946. alignment = INLINE_ALIGNMENT_BOTTOM;
  4947. }
  4948. }
  4949. }
  4950. int end = bbcode.find_char('[', brk_end);
  4951. if (end == -1) {
  4952. end = bbcode.length();
  4953. }
  4954. String image = bbcode.substr(brk_end + 1, end - brk_end - 1);
  4955. String alt_text;
  4956. Ref<Texture2D> texture = ResourceLoader::load(image, "Texture2D");
  4957. if (texture.is_valid()) {
  4958. Rect2 region;
  4959. OptionMap::Iterator region_option = bbcode_options.find("region");
  4960. if (region_option) {
  4961. Vector<String> region_values = _split_unquoted(region_option->value, U',');
  4962. if (region_values.size() == 4) {
  4963. region.position.x = region_values[0].to_float();
  4964. region.position.y = region_values[1].to_float();
  4965. region.size.x = region_values[2].to_float();
  4966. region.size.y = region_values[3].to_float();
  4967. }
  4968. }
  4969. Color color = Color(1.0, 1.0, 1.0);
  4970. OptionMap::Iterator color_option = bbcode_options.find("color");
  4971. if (color_option) {
  4972. color = Color::from_string(color_option->value, color);
  4973. }
  4974. OptionMap::Iterator alt_text_option = bbcode_options.find("alt");
  4975. if (alt_text_option) {
  4976. alt_text = alt_text_option->value;
  4977. }
  4978. int width = 0;
  4979. int height = 0;
  4980. bool pad = false;
  4981. String tooltip;
  4982. bool size_in_percent = false;
  4983. if (!bbcode_value.is_empty()) {
  4984. int sep = bbcode_value.find_char('x');
  4985. if (sep == -1) {
  4986. width = bbcode_value.to_int();
  4987. } else {
  4988. width = bbcode_value.substr(0, sep).to_int();
  4989. height = bbcode_value.substr(sep + 1).to_int();
  4990. }
  4991. } else {
  4992. OptionMap::Iterator align_option = bbcode_options.find("align");
  4993. if (align_option) {
  4994. Vector<String> subtag = _split_unquoted(align_option->value, U',');
  4995. _normalize_subtags(subtag);
  4996. if (subtag.size() > 1) {
  4997. if (subtag[0] == "top" || subtag[0] == "t") {
  4998. alignment = INLINE_ALIGNMENT_TOP_TO;
  4999. } else if (subtag[0] == "center" || subtag[0] == "c") {
  5000. alignment = INLINE_ALIGNMENT_CENTER_TO;
  5001. } else if (subtag[0] == "bottom" || subtag[0] == "b") {
  5002. alignment = INLINE_ALIGNMENT_BOTTOM_TO;
  5003. }
  5004. if (subtag[1] == "top" || subtag[1] == "t") {
  5005. alignment |= INLINE_ALIGNMENT_TO_TOP;
  5006. } else if (subtag[1] == "center" || subtag[1] == "c") {
  5007. alignment |= INLINE_ALIGNMENT_TO_CENTER;
  5008. } else if (subtag[1] == "baseline" || subtag[1] == "l") {
  5009. alignment |= INLINE_ALIGNMENT_TO_BASELINE;
  5010. } else if (subtag[1] == "bottom" || subtag[1] == "b") {
  5011. alignment |= INLINE_ALIGNMENT_TO_BOTTOM;
  5012. }
  5013. } else if (subtag.size() > 0) {
  5014. if (subtag[0] == "top" || subtag[0] == "t") {
  5015. alignment = INLINE_ALIGNMENT_TOP;
  5016. } else if (subtag[0] == "center" || subtag[0] == "c") {
  5017. alignment = INLINE_ALIGNMENT_CENTER;
  5018. } else if (subtag[0] == "bottom" || subtag[0] == "b") {
  5019. alignment = INLINE_ALIGNMENT_BOTTOM;
  5020. }
  5021. }
  5022. }
  5023. OptionMap::Iterator width_option = bbcode_options.find("width");
  5024. if (width_option) {
  5025. width = width_option->value.to_int();
  5026. if (width_option->value.ends_with("%")) {
  5027. size_in_percent = true;
  5028. }
  5029. }
  5030. OptionMap::Iterator height_option = bbcode_options.find("height");
  5031. if (height_option) {
  5032. height = height_option->value.to_int();
  5033. if (height_option->value.ends_with("%")) {
  5034. size_in_percent = true;
  5035. }
  5036. }
  5037. OptionMap::Iterator tooltip_option = bbcode_options.find("tooltip");
  5038. if (tooltip_option) {
  5039. tooltip = tooltip_option->value;
  5040. }
  5041. OptionMap::Iterator pad_option = bbcode_options.find("pad");
  5042. if (pad_option) {
  5043. pad = (pad_option->value == "true");
  5044. }
  5045. }
  5046. add_image(texture, width, height, color, (InlineAlignment)alignment, region, Variant(), pad, tooltip, size_in_percent, alt_text);
  5047. }
  5048. pos = end;
  5049. tag_stack.push_front(bbcode_name);
  5050. } else if (tag.begins_with("color=")) {
  5051. String color_str = _get_tag_value(tag).unquote();
  5052. Color color = Color::from_string(color_str, theme_cache.default_color);
  5053. push_color(color);
  5054. pos = brk_end + 1;
  5055. tag_stack.push_front("color");
  5056. } else if (tag.begins_with("outline_color=")) {
  5057. String color_str = _get_tag_value(tag).unquote();
  5058. Color color = Color::from_string(color_str, theme_cache.default_color);
  5059. push_outline_color(color);
  5060. pos = brk_end + 1;
  5061. tag_stack.push_front("outline_color");
  5062. } else if (tag.begins_with("font_size=")) {
  5063. int fnt_size = _get_tag_value(tag).to_int();
  5064. push_font_size(fnt_size);
  5065. pos = brk_end + 1;
  5066. tag_stack.push_front("font_size");
  5067. } else if (tag.begins_with("opentype_features=") || tag.begins_with("otf=")) {
  5068. int value_pos = tag.find_char('=');
  5069. String fnt_ftr = tag.substr(value_pos + 1);
  5070. Vector<String> subtag = fnt_ftr.split(",");
  5071. _normalize_subtags(subtag);
  5072. if (subtag.size() > 0) {
  5073. Ref<Font> font = theme_cache.normal_font;
  5074. DefaultFont def_font = RTL_NORMAL_FONT;
  5075. ItemFont *font_it = _find_font(current);
  5076. if (font_it) {
  5077. if (font_it->font.is_valid()) {
  5078. font = font_it->font;
  5079. def_font = font_it->def_font;
  5080. }
  5081. }
  5082. Dictionary features;
  5083. for (int i = 0; i < subtag.size(); i++) {
  5084. Vector<String> subtag_a = subtag[i].split("=");
  5085. _normalize_subtags(subtag_a);
  5086. if (subtag_a.size() == 2) {
  5087. features[TS->name_to_tag(subtag_a[0])] = subtag_a[1].to_int();
  5088. } else if (subtag_a.size() == 1) {
  5089. features[TS->name_to_tag(subtag_a[0])] = 1;
  5090. }
  5091. }
  5092. Ref<FontVariation> fc;
  5093. fc.instantiate();
  5094. fc->set_base_font(font);
  5095. fc->set_opentype_features(features);
  5096. if (def_font != RTL_CUSTOM_FONT) {
  5097. _push_def_font_var(def_font, fc);
  5098. } else {
  5099. push_font(fc);
  5100. }
  5101. }
  5102. pos = brk_end + 1;
  5103. tag_stack.push_front(tag.substr(0, value_pos));
  5104. } else if (tag.begins_with("font=")) {
  5105. String fnt = _get_tag_value(tag).unquote();
  5106. Ref<Font> fc = ResourceLoader::load(fnt, "Font");
  5107. if (fc.is_valid()) {
  5108. push_font(fc);
  5109. }
  5110. pos = brk_end + 1;
  5111. tag_stack.push_front("font");
  5112. } else if (tag.begins_with("font ")) {
  5113. Ref<Font> font = theme_cache.normal_font;
  5114. DefaultFont def_font = RTL_NORMAL_FONT;
  5115. int fnt_size = -1;
  5116. ItemFont *font_it = _find_font(current);
  5117. if (font_it) {
  5118. if (font_it->font.is_valid()) {
  5119. font = font_it->font;
  5120. def_font = font_it->def_font;
  5121. }
  5122. }
  5123. Ref<FontVariation> fc;
  5124. fc.instantiate();
  5125. OptionMap::Iterator name_option = bbcode_options.find("name");
  5126. if (!name_option) {
  5127. name_option = bbcode_options.find("n");
  5128. }
  5129. if (name_option) {
  5130. const String &fnt = name_option->value;
  5131. Ref<Font> font_data = ResourceLoader::load(fnt, "Font");
  5132. if (font_data.is_valid()) {
  5133. font = font_data;
  5134. def_font = RTL_CUSTOM_FONT;
  5135. }
  5136. }
  5137. OptionMap::Iterator size_option = bbcode_options.find("size");
  5138. if (!size_option) {
  5139. size_option = bbcode_options.find("s");
  5140. }
  5141. if (size_option) {
  5142. fnt_size = size_option->value.to_int();
  5143. }
  5144. OptionMap::Iterator glyph_spacing_option = bbcode_options.find("glyph_spacing");
  5145. if (!glyph_spacing_option) {
  5146. glyph_spacing_option = bbcode_options.find("gl");
  5147. }
  5148. if (glyph_spacing_option) {
  5149. int spacing = glyph_spacing_option->value.to_int();
  5150. fc->set_spacing(TextServer::SPACING_GLYPH, spacing);
  5151. }
  5152. OptionMap::Iterator space_spacing_option = bbcode_options.find("space_spacing");
  5153. if (!space_spacing_option) {
  5154. space_spacing_option = bbcode_options.find("sp");
  5155. }
  5156. if (space_spacing_option) {
  5157. int spacing = space_spacing_option->value.to_int();
  5158. fc->set_spacing(TextServer::SPACING_SPACE, spacing);
  5159. }
  5160. OptionMap::Iterator top_spacing_option = bbcode_options.find("top_spacing");
  5161. if (!top_spacing_option) {
  5162. top_spacing_option = bbcode_options.find("top");
  5163. }
  5164. if (top_spacing_option) {
  5165. int spacing = top_spacing_option->value.to_int();
  5166. fc->set_spacing(TextServer::SPACING_TOP, spacing);
  5167. }
  5168. OptionMap::Iterator bottom_spacing_option = bbcode_options.find("bottom_spacing");
  5169. if (!bottom_spacing_option) {
  5170. bottom_spacing_option = bbcode_options.find("bt");
  5171. }
  5172. if (bottom_spacing_option) {
  5173. int spacing = bottom_spacing_option->value.to_int();
  5174. fc->set_spacing(TextServer::SPACING_BOTTOM, spacing);
  5175. }
  5176. OptionMap::Iterator embolden_option = bbcode_options.find("embolden");
  5177. if (!embolden_option) {
  5178. embolden_option = bbcode_options.find("emb");
  5179. }
  5180. if (embolden_option) {
  5181. float emb = embolden_option->value.to_float();
  5182. fc->set_variation_embolden(emb);
  5183. }
  5184. OptionMap::Iterator face_index_option = bbcode_options.find("face_index");
  5185. if (!face_index_option) {
  5186. face_index_option = bbcode_options.find("fi");
  5187. }
  5188. if (face_index_option) {
  5189. int fi = face_index_option->value.to_int();
  5190. fc->set_variation_face_index(fi);
  5191. }
  5192. OptionMap::Iterator slant_option = bbcode_options.find("slant");
  5193. if (!slant_option) {
  5194. slant_option = bbcode_options.find("sln");
  5195. }
  5196. if (slant_option) {
  5197. float slant = slant_option->value.to_float();
  5198. fc->set_variation_transform(Transform2D(1.0, slant, 0.0, 1.0, 0.0, 0.0));
  5199. }
  5200. OptionMap::Iterator opentype_variation_option = bbcode_options.find("opentype_variation");
  5201. if (!opentype_variation_option) {
  5202. opentype_variation_option = bbcode_options.find("otv");
  5203. }
  5204. if (opentype_variation_option) {
  5205. Dictionary variations;
  5206. if (!opentype_variation_option->value.is_empty()) {
  5207. Vector<String> variation_tags = opentype_variation_option->value.split(",");
  5208. for (int j = 0; j < variation_tags.size(); j++) {
  5209. Vector<String> subtag_b = variation_tags[j].split("=");
  5210. _normalize_subtags(subtag_b);
  5211. if (subtag_b.size() == 2) {
  5212. variations[TS->name_to_tag(subtag_b[0])] = subtag_b[1].to_float();
  5213. }
  5214. }
  5215. fc->set_variation_opentype(variations);
  5216. }
  5217. }
  5218. OptionMap::Iterator opentype_features_option = bbcode_options.find("opentype_features");
  5219. if (!opentype_features_option) {
  5220. opentype_features_option = bbcode_options.find("otf");
  5221. }
  5222. if (opentype_features_option) {
  5223. Dictionary features;
  5224. if (!opentype_features_option->value.is_empty()) {
  5225. Vector<String> feature_tags = opentype_features_option->value.split(",");
  5226. for (int j = 0; j < feature_tags.size(); j++) {
  5227. Vector<String> subtag_b = feature_tags[j].split("=");
  5228. _normalize_subtags(subtag_b);
  5229. if (subtag_b.size() == 2) {
  5230. features[TS->name_to_tag(subtag_b[0])] = subtag_b[1].to_float();
  5231. } else if (subtag_b.size() == 1) {
  5232. features[TS->name_to_tag(subtag_b[0])] = 1;
  5233. }
  5234. }
  5235. fc->set_opentype_features(features);
  5236. }
  5237. }
  5238. fc->set_base_font(font);
  5239. if (def_font != RTL_CUSTOM_FONT) {
  5240. _push_def_font_var(def_font, fc, fnt_size);
  5241. } else {
  5242. push_font(fc, fnt_size);
  5243. }
  5244. pos = brk_end + 1;
  5245. tag_stack.push_front("font");
  5246. } else if (tag.begins_with("outline_size=")) {
  5247. int fnt_size = _get_tag_value(tag).to_int();
  5248. if (fnt_size > 0) {
  5249. push_outline_size(fnt_size);
  5250. }
  5251. pos = brk_end + 1;
  5252. tag_stack.push_front("outline_size");
  5253. } else if (bbcode_name == "fade") {
  5254. int start_index = 0;
  5255. OptionMap::Iterator start_option = bbcode_options.find("start");
  5256. if (start_option) {
  5257. start_index = start_option->value.to_int();
  5258. }
  5259. int length = 10;
  5260. OptionMap::Iterator length_option = bbcode_options.find("length");
  5261. if (length_option) {
  5262. length = length_option->value.to_int();
  5263. }
  5264. push_fade(start_index, length);
  5265. pos = brk_end + 1;
  5266. tag_stack.push_front("fade");
  5267. } else if (bbcode_name == "shake") {
  5268. int strength = 5;
  5269. OptionMap::Iterator strength_option = bbcode_options.find("level");
  5270. if (strength_option) {
  5271. strength = strength_option->value.to_int();
  5272. }
  5273. float rate = 20.0f;
  5274. OptionMap::Iterator rate_option = bbcode_options.find("rate");
  5275. if (rate_option) {
  5276. rate = rate_option->value.to_float();
  5277. }
  5278. bool connected = true;
  5279. OptionMap::Iterator connected_option = bbcode_options.find("connected");
  5280. if (connected_option) {
  5281. connected = connected_option->value.to_int();
  5282. }
  5283. push_shake(strength, rate, connected);
  5284. pos = brk_end + 1;
  5285. tag_stack.push_front("shake");
  5286. set_process_internal(true);
  5287. } else if (bbcode_name == "wave") {
  5288. float amplitude = 20.0f;
  5289. OptionMap::Iterator amplitude_option = bbcode_options.find("amp");
  5290. if (amplitude_option) {
  5291. amplitude = amplitude_option->value.to_float();
  5292. }
  5293. float period = 5.0f;
  5294. OptionMap::Iterator period_option = bbcode_options.find("freq");
  5295. if (period_option) {
  5296. period = period_option->value.to_float();
  5297. }
  5298. bool connected = true;
  5299. OptionMap::Iterator connected_option = bbcode_options.find("connected");
  5300. if (connected_option) {
  5301. connected = connected_option->value.to_int();
  5302. }
  5303. push_wave(period, amplitude, connected);
  5304. pos = brk_end + 1;
  5305. tag_stack.push_front("wave");
  5306. set_process_internal(true);
  5307. } else if (bbcode_name == "tornado") {
  5308. float radius = 10.0f;
  5309. OptionMap::Iterator radius_option = bbcode_options.find("radius");
  5310. if (radius_option) {
  5311. radius = radius_option->value.to_float();
  5312. }
  5313. float frequency = 1.0f;
  5314. OptionMap::Iterator frequency_option = bbcode_options.find("freq");
  5315. if (frequency_option) {
  5316. frequency = frequency_option->value.to_float();
  5317. }
  5318. bool connected = true;
  5319. OptionMap::Iterator connected_option = bbcode_options.find("connected");
  5320. if (connected_option) {
  5321. connected = connected_option->value.to_int();
  5322. }
  5323. push_tornado(frequency, radius, connected);
  5324. pos = brk_end + 1;
  5325. tag_stack.push_front("tornado");
  5326. set_process_internal(true);
  5327. } else if (bbcode_name == "rainbow") {
  5328. float saturation = 0.8f;
  5329. OptionMap::Iterator saturation_option = bbcode_options.find("sat");
  5330. if (saturation_option) {
  5331. saturation = saturation_option->value.to_float();
  5332. }
  5333. float value = 0.8f;
  5334. OptionMap::Iterator value_option = bbcode_options.find("val");
  5335. if (value_option) {
  5336. value = value_option->value.to_float();
  5337. }
  5338. float frequency = 1.0f;
  5339. OptionMap::Iterator frequency_option = bbcode_options.find("freq");
  5340. if (frequency_option) {
  5341. frequency = frequency_option->value.to_float();
  5342. }
  5343. float speed = 1.0f;
  5344. OptionMap::Iterator speed_option = bbcode_options.find("speed");
  5345. if (speed_option) {
  5346. speed = speed_option->value.to_float();
  5347. }
  5348. push_rainbow(saturation, value, frequency, speed);
  5349. pos = brk_end + 1;
  5350. tag_stack.push_front("rainbow");
  5351. set_process_internal(true);
  5352. } else if (bbcode_name == "pulse") {
  5353. Color color = Color(1, 1, 1, 0.25);
  5354. OptionMap::Iterator color_option = bbcode_options.find("color");
  5355. if (color_option) {
  5356. color = Color::from_string(color_option->value, color);
  5357. }
  5358. float frequency = 1.0;
  5359. OptionMap::Iterator freq_option = bbcode_options.find("freq");
  5360. if (freq_option) {
  5361. frequency = freq_option->value.to_float();
  5362. }
  5363. float ease = -2.0;
  5364. OptionMap::Iterator ease_option = bbcode_options.find("ease");
  5365. if (ease_option) {
  5366. ease = ease_option->value.to_float();
  5367. }
  5368. push_pulse(color, frequency, ease);
  5369. pos = brk_end + 1;
  5370. tag_stack.push_front("pulse");
  5371. set_process_internal(true);
  5372. } else if (tag.begins_with("bgcolor=")) {
  5373. String color_str = _get_tag_value(tag).unquote();
  5374. Color color = Color::from_string(color_str, theme_cache.default_color);
  5375. push_bgcolor(color);
  5376. pos = brk_end + 1;
  5377. tag_stack.push_front("bgcolor");
  5378. } else if (tag.begins_with("fgcolor=")) {
  5379. String color_str = _get_tag_value(tag).unquote();
  5380. Color color = Color::from_string(color_str, theme_cache.default_color);
  5381. push_fgcolor(color);
  5382. pos = brk_end + 1;
  5383. tag_stack.push_front("fgcolor");
  5384. } else {
  5385. Vector<String> &expr = split_tag_block;
  5386. if (expr.is_empty()) {
  5387. add_text("[");
  5388. pos = brk_pos + 1;
  5389. } else {
  5390. String identifier = expr[0];
  5391. expr.remove_at(0);
  5392. Dictionary properties = parse_expressions_for_values(expr);
  5393. Ref<RichTextEffect> effect = _get_custom_effect_by_code(identifier);
  5394. if (effect.is_valid()) {
  5395. push_customfx(effect, properties);
  5396. pos = brk_end + 1;
  5397. tag_stack.push_front(identifier);
  5398. } else {
  5399. add_text("["); //ignore
  5400. pos = brk_pos + 1;
  5401. }
  5402. }
  5403. }
  5404. }
  5405. parsing_bbcode.store(false);
  5406. }
  5407. void RichTextLabel::scroll_to_selection() {
  5408. float line_offset = get_selection_line_offset();
  5409. if (line_offset != -1.0) {
  5410. vscroll->set_value(line_offset);
  5411. queue_accessibility_update();
  5412. }
  5413. }
  5414. void RichTextLabel::scroll_to_paragraph(int p_paragraph) {
  5415. _validate_line_caches();
  5416. if (p_paragraph <= 0) {
  5417. vscroll->set_value(0);
  5418. } else if (p_paragraph >= main->first_invalid_line.load()) {
  5419. vscroll->set_value(vscroll->get_max());
  5420. } else {
  5421. vscroll->set_value(main->lines[p_paragraph].offset.y);
  5422. }
  5423. queue_accessibility_update();
  5424. }
  5425. int RichTextLabel::get_paragraph_count() const {
  5426. return main->lines.size();
  5427. }
  5428. int RichTextLabel::get_visible_paragraph_count() const {
  5429. if (!is_visible()) {
  5430. return 0;
  5431. }
  5432. const_cast<RichTextLabel *>(this)->_validate_line_caches();
  5433. return visible_paragraph_count;
  5434. }
  5435. void RichTextLabel::scroll_to_line(int p_line) {
  5436. if (p_line <= 0) {
  5437. vscroll->set_value(0);
  5438. queue_accessibility_update();
  5439. return;
  5440. }
  5441. _validate_line_caches();
  5442. int line_count = 0;
  5443. int to_line = main->first_invalid_line.load();
  5444. for (int i = 0; i < to_line; i++) {
  5445. MutexLock lock(main->lines[i].text_buf->get_mutex());
  5446. if ((line_count <= p_line) && (line_count + main->lines[i].text_buf->get_line_count() >= p_line)) {
  5447. float line_offset = 0.f;
  5448. for (int j = 0; j < p_line - line_count; j++) {
  5449. line_offset += main->lines[i].text_buf->get_line_ascent(j) + main->lines[i].text_buf->get_line_descent(j) + theme_cache.line_separation;
  5450. }
  5451. vscroll->set_value(main->lines[i].offset.y + line_offset);
  5452. queue_accessibility_update();
  5453. return;
  5454. }
  5455. line_count += main->lines[i].text_buf->get_line_count();
  5456. }
  5457. vscroll->set_value(vscroll->get_max());
  5458. queue_accessibility_update();
  5459. }
  5460. float RichTextLabel::get_line_offset(int p_line) {
  5461. _validate_line_caches();
  5462. int line_count = 0;
  5463. int to_line = main->first_invalid_line.load();
  5464. for (int i = 0; i < to_line; i++) {
  5465. MutexLock lock(main->lines[i].text_buf->get_mutex());
  5466. if ((line_count <= p_line) && (p_line <= line_count + main->lines[i].text_buf->get_line_count())) {
  5467. float line_offset = 0.f;
  5468. for (int j = 0; j < p_line - line_count; j++) {
  5469. line_offset += main->lines[i].text_buf->get_line_ascent(j) + main->lines[i].text_buf->get_line_descent(j) + theme_cache.line_separation;
  5470. }
  5471. return main->lines[i].offset.y + line_offset;
  5472. }
  5473. line_count += main->lines[i].text_buf->get_line_count();
  5474. }
  5475. return 0;
  5476. }
  5477. float RichTextLabel::get_paragraph_offset(int p_paragraph) {
  5478. _validate_line_caches();
  5479. int to_line = main->first_invalid_line.load();
  5480. if (0 <= p_paragraph && p_paragraph < to_line) {
  5481. return main->lines[p_paragraph].offset.y;
  5482. }
  5483. return 0;
  5484. }
  5485. int RichTextLabel::get_line_count() const {
  5486. const_cast<RichTextLabel *>(this)->_validate_line_caches();
  5487. int line_count = 0;
  5488. int to_line = main->first_invalid_line.load();
  5489. for (int i = 0; i < to_line; i++) {
  5490. MutexLock lock(main->lines[i].text_buf->get_mutex());
  5491. line_count += main->lines[i].text_buf->get_line_count();
  5492. }
  5493. return line_count;
  5494. }
  5495. Vector2i RichTextLabel::get_line_range(int p_line) {
  5496. const_cast<RichTextLabel *>(this)->_validate_line_caches();
  5497. int line_count = 0;
  5498. int to_line = main->first_invalid_line.load();
  5499. for (int i = 0; i < to_line; i++) {
  5500. MutexLock lock(main->lines[i].text_buf->get_mutex());
  5501. int lc = main->lines[i].text_buf->get_line_count();
  5502. if (p_line < line_count + lc) {
  5503. Vector2i char_offset = Vector2i(main->lines[i].char_offset, main->lines[i].char_offset);
  5504. Vector2i line_range = main->lines[i].text_buf->get_line_range(p_line - line_count);
  5505. return char_offset + line_range;
  5506. }
  5507. line_count += lc;
  5508. }
  5509. return Vector2i();
  5510. }
  5511. int RichTextLabel::get_visible_line_count() const {
  5512. if (!is_visible()) {
  5513. return 0;
  5514. }
  5515. const_cast<RichTextLabel *>(this)->_validate_line_caches();
  5516. return visible_line_count;
  5517. }
  5518. void RichTextLabel::set_selection_enabled(bool p_enabled) {
  5519. if (selection.enabled == p_enabled) {
  5520. return;
  5521. }
  5522. selection.enabled = p_enabled;
  5523. if (!p_enabled) {
  5524. if (selection.active) {
  5525. deselect();
  5526. }
  5527. set_focus_mode(FOCUS_ACCESSIBILITY);
  5528. } else {
  5529. set_focus_mode(FOCUS_ALL);
  5530. }
  5531. queue_accessibility_update();
  5532. }
  5533. void RichTextLabel::set_deselect_on_focus_loss_enabled(const bool p_enabled) {
  5534. if (deselect_on_focus_loss_enabled == p_enabled) {
  5535. return;
  5536. }
  5537. deselect_on_focus_loss_enabled = p_enabled;
  5538. if (p_enabled && selection.active && !has_focus()) {
  5539. deselect();
  5540. }
  5541. }
  5542. Variant RichTextLabel::get_drag_data(const Point2 &p_point) {
  5543. Variant ret = Control::get_drag_data(p_point);
  5544. if (ret != Variant()) {
  5545. return ret;
  5546. }
  5547. if (selection.drag_attempt && selection.enabled) {
  5548. String t = get_selected_text();
  5549. Label *l = memnew(Label);
  5550. l->set_text(t);
  5551. l->set_focus_mode(FOCUS_ACCESSIBILITY);
  5552. l->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED); // Text is already translated.
  5553. set_drag_preview(l);
  5554. return t;
  5555. }
  5556. return Variant();
  5557. }
  5558. bool RichTextLabel::_is_click_inside_selection() const {
  5559. if (selection.active && selection.enabled && selection.click_frame && selection.from_frame && selection.to_frame) {
  5560. const Line &l_click = selection.click_frame->lines[selection.click_line];
  5561. const Line &l_from = selection.from_frame->lines[selection.from_line];
  5562. const Line &l_to = selection.to_frame->lines[selection.to_line];
  5563. 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);
  5564. } else {
  5565. return false;
  5566. }
  5567. }
  5568. bool RichTextLabel::_search_table(ItemTable *p_table, List<Item *>::Element *p_from, const String &p_string, bool p_reverse_search) {
  5569. List<Item *>::Element *E = p_from;
  5570. while (E != nullptr) {
  5571. ERR_CONTINUE(E->get()->type != ITEM_FRAME); // Children should all be frames.
  5572. ItemFrame *frame = static_cast<ItemFrame *>(E->get());
  5573. if (p_reverse_search) {
  5574. for (int i = (int)frame->lines.size() - 1; i >= 0; i--) {
  5575. if (_search_line(frame, i, p_string, -1, p_reverse_search)) {
  5576. return true;
  5577. }
  5578. }
  5579. } else {
  5580. for (int i = 0; i < (int)frame->lines.size(); i++) {
  5581. if (_search_line(frame, i, p_string, 0, p_reverse_search)) {
  5582. return true;
  5583. }
  5584. }
  5585. }
  5586. E = p_reverse_search ? E->prev() : E->next();
  5587. }
  5588. return false;
  5589. }
  5590. bool RichTextLabel::_search_line(ItemFrame *p_frame, int p_line, const String &p_string, int p_char_idx, bool p_reverse_search) {
  5591. ERR_FAIL_NULL_V(p_frame, false);
  5592. ERR_FAIL_COND_V(p_line < 0 || p_line >= (int)p_frame->lines.size(), false);
  5593. Line &l = p_frame->lines[p_line];
  5594. String txt;
  5595. Item *it_to = (p_line + 1 < (int)p_frame->lines.size()) ? p_frame->lines[p_line + 1].from : nullptr;
  5596. for (Item *it = l.from; it && it != it_to; it = _get_next_item(it)) {
  5597. switch (it->type) {
  5598. case ITEM_NEWLINE: {
  5599. txt += "\n";
  5600. } break;
  5601. case ITEM_TEXT: {
  5602. ItemText *t = static_cast<ItemText *>(it);
  5603. txt += t->text;
  5604. } break;
  5605. case ITEM_IMAGE: {
  5606. txt += " ";
  5607. } break;
  5608. case ITEM_TABLE: {
  5609. ItemTable *table = static_cast<ItemTable *>(it);
  5610. List<Item *>::Element *E = p_reverse_search ? table->subitems.back() : table->subitems.front();
  5611. if (_search_table(table, E, p_string, p_reverse_search)) {
  5612. return true;
  5613. }
  5614. } break;
  5615. default:
  5616. break;
  5617. }
  5618. }
  5619. int sp = -1;
  5620. if (p_reverse_search) {
  5621. sp = txt.rfindn(p_string, p_char_idx);
  5622. } else {
  5623. sp = txt.findn(p_string, p_char_idx);
  5624. }
  5625. if (sp != -1) {
  5626. selection.from_frame = p_frame;
  5627. selection.from_line = p_line;
  5628. selection.from_item = _get_item_at_pos(l.from, it_to, sp);
  5629. selection.from_char = sp;
  5630. selection.to_frame = p_frame;
  5631. selection.to_line = p_line;
  5632. selection.to_item = _get_item_at_pos(l.from, it_to, sp + p_string.length());
  5633. selection.to_char = sp + p_string.length();
  5634. selection.active = true;
  5635. queue_accessibility_update();
  5636. return true;
  5637. }
  5638. return false;
  5639. }
  5640. bool RichTextLabel::search(const String &p_string, bool p_from_selection, bool p_search_previous) {
  5641. ERR_FAIL_COND_V(!selection.enabled, false);
  5642. if (p_string.is_empty()) {
  5643. selection.active = false;
  5644. queue_accessibility_update();
  5645. return false;
  5646. }
  5647. int char_idx = p_search_previous ? -1 : 0;
  5648. int current_line = 0;
  5649. int to_line = main->first_invalid_line.load();
  5650. int ending_line = to_line - 1;
  5651. if (p_from_selection && selection.active) {
  5652. // First check to see if other results exist in current line
  5653. char_idx = p_search_previous ? selection.from_char - 1 : selection.to_char;
  5654. if (!(p_search_previous && char_idx < 0) &&
  5655. _search_line(selection.from_frame, selection.from_line, p_string, char_idx, p_search_previous)) {
  5656. scroll_to_selection();
  5657. queue_redraw();
  5658. return true;
  5659. }
  5660. char_idx = p_search_previous ? -1 : 0;
  5661. // Next, check to see if the current search result is in a table
  5662. if (selection.from_frame->parent != nullptr && selection.from_frame->parent->type == ITEM_TABLE) {
  5663. // Find last search result in table
  5664. ItemTable *parent_table = static_cast<ItemTable *>(selection.from_frame->parent);
  5665. List<Item *>::Element *parent_element = p_search_previous ? parent_table->subitems.back() : parent_table->subitems.front();
  5666. while (parent_element->get() != selection.from_frame) {
  5667. parent_element = p_search_previous ? parent_element->prev() : parent_element->next();
  5668. ERR_FAIL_NULL_V(parent_element, false);
  5669. }
  5670. // Search remainder of table
  5671. if (!(p_search_previous && parent_element == parent_table->subitems.front()) &&
  5672. parent_element != parent_table->subitems.back()) {
  5673. parent_element = p_search_previous ? parent_element->prev() : parent_element->next(); // Don't want to search current item
  5674. ERR_FAIL_NULL_V(parent_element, false);
  5675. // Search for next element
  5676. if (_search_table(parent_table, parent_element, p_string, p_search_previous)) {
  5677. scroll_to_selection();
  5678. queue_redraw();
  5679. return true;
  5680. }
  5681. }
  5682. }
  5683. ending_line = selection.from_frame->line + selection.from_line;
  5684. current_line = p_search_previous ? ending_line - 1 : ending_line + 1;
  5685. } else if (p_search_previous) {
  5686. current_line = ending_line;
  5687. ending_line = 0;
  5688. }
  5689. // Search remainder of the file
  5690. while (current_line != ending_line) {
  5691. // Wrap around
  5692. if (current_line < 0) {
  5693. current_line = to_line - 1;
  5694. } else if (current_line >= to_line) {
  5695. current_line = 0;
  5696. }
  5697. if (_search_line(main, current_line, p_string, char_idx, p_search_previous)) {
  5698. scroll_to_selection();
  5699. queue_redraw();
  5700. return true;
  5701. }
  5702. if (current_line != ending_line) {
  5703. p_search_previous ? current_line-- : current_line++;
  5704. }
  5705. }
  5706. if (p_from_selection && selection.active) {
  5707. // Check contents of selection
  5708. return _search_line(main, current_line, p_string, char_idx, p_search_previous);
  5709. } else {
  5710. return false;
  5711. }
  5712. }
  5713. String RichTextLabel::_get_line_text(ItemFrame *p_frame, int p_line, Selection p_selection) const {
  5714. String txt;
  5715. ERR_FAIL_NULL_V(p_frame, txt);
  5716. ERR_FAIL_COND_V(p_line < 0 || p_line >= (int)p_frame->lines.size(), txt);
  5717. Line &l = p_frame->lines[p_line];
  5718. Item *it_to = (p_line + 1 < (int)p_frame->lines.size()) ? p_frame->lines[p_line + 1].from : nullptr;
  5719. int end_idx = 0;
  5720. if (it_to != nullptr) {
  5721. end_idx = it_to->index;
  5722. } else {
  5723. for (Item *it = l.from; it; it = _get_next_item(it)) {
  5724. end_idx = it->index + 1;
  5725. }
  5726. }
  5727. for (Item *it = l.from; it && it != it_to; it = _get_next_item(it)) {
  5728. if (it->type == ITEM_TABLE) {
  5729. ItemTable *table = static_cast<ItemTable *>(it);
  5730. for (Item *E : table->subitems) {
  5731. ERR_CONTINUE(E->type != ITEM_FRAME); // Children should all be frames.
  5732. ItemFrame *frame = static_cast<ItemFrame *>(E);
  5733. for (int i = 0; i < (int)frame->lines.size(); i++) {
  5734. txt += _get_line_text(frame, i, p_selection);
  5735. }
  5736. }
  5737. }
  5738. if ((p_selection.to_item != nullptr) && (p_selection.to_item->index < l.from->index)) {
  5739. continue;
  5740. }
  5741. if ((p_selection.from_item != nullptr) && (p_selection.from_item->index >= end_idx)) {
  5742. continue;
  5743. }
  5744. if (it->type == ITEM_DROPCAP) {
  5745. const ItemDropcap *dc = static_cast<ItemDropcap *>(it);
  5746. txt += dc->text;
  5747. } else if (it->type == ITEM_TEXT) {
  5748. const ItemText *t = static_cast<ItemText *>(it);
  5749. txt += t->text;
  5750. } else if (it->type == ITEM_NEWLINE) {
  5751. txt += "\n";
  5752. } else if (it->type == ITEM_IMAGE) {
  5753. txt += " ";
  5754. }
  5755. }
  5756. 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)) {
  5757. txt = txt.substr(0, p_selection.to_char);
  5758. }
  5759. 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)) {
  5760. txt = txt.substr(p_selection.from_char);
  5761. }
  5762. return txt;
  5763. }
  5764. void RichTextLabel::set_context_menu_enabled(bool p_enabled) {
  5765. context_menu_enabled = p_enabled;
  5766. }
  5767. bool RichTextLabel::is_context_menu_enabled() const {
  5768. return context_menu_enabled;
  5769. }
  5770. void RichTextLabel::set_shortcut_keys_enabled(bool p_enabled) {
  5771. shortcut_keys_enabled = p_enabled;
  5772. }
  5773. bool RichTextLabel::is_shortcut_keys_enabled() const {
  5774. return shortcut_keys_enabled;
  5775. }
  5776. // Context menu.
  5777. PopupMenu *RichTextLabel::get_menu() const {
  5778. if (!menu) {
  5779. const_cast<RichTextLabel *>(this)->_generate_context_menu();
  5780. }
  5781. return menu;
  5782. }
  5783. bool RichTextLabel::is_menu_visible() const {
  5784. return menu && menu->is_visible();
  5785. }
  5786. String RichTextLabel::get_selected_text() const {
  5787. if (!selection.active || !selection.enabled) {
  5788. return "";
  5789. }
  5790. String txt;
  5791. int to_line = main->first_invalid_line.load();
  5792. for (int i = 0; i < to_line; i++) {
  5793. txt += _get_line_text(main, i, selection);
  5794. }
  5795. if (selection_modifier.is_valid()) {
  5796. txt = selection_modifier.call(txt);
  5797. }
  5798. return txt;
  5799. }
  5800. void RichTextLabel::deselect() {
  5801. selection.active = false;
  5802. queue_accessibility_update();
  5803. queue_redraw();
  5804. }
  5805. void RichTextLabel::select_all() {
  5806. _validate_line_caches();
  5807. if (!selection.enabled) {
  5808. return;
  5809. }
  5810. Item *it = main;
  5811. Item *from_item = nullptr;
  5812. Item *to_item = nullptr;
  5813. while (it) {
  5814. if (it->type != ITEM_FRAME) {
  5815. if (!from_item) {
  5816. from_item = it;
  5817. }
  5818. to_item = it;
  5819. }
  5820. it = _get_next_item(it, true);
  5821. }
  5822. if (!from_item) {
  5823. return;
  5824. }
  5825. ItemFrame *from_frame = nullptr;
  5826. int from_line = 0;
  5827. _find_frame(from_item, &from_frame, &from_line);
  5828. if (!from_frame) {
  5829. return;
  5830. }
  5831. ItemFrame *to_frame = nullptr;
  5832. int to_line = 0;
  5833. _find_frame(to_item, &to_frame, &to_line);
  5834. if (!to_frame) {
  5835. return;
  5836. }
  5837. selection.from_line = from_line;
  5838. selection.from_frame = from_frame;
  5839. selection.from_char = 0;
  5840. selection.from_item = from_item;
  5841. selection.to_line = to_line;
  5842. selection.to_frame = to_frame;
  5843. selection.to_char = to_frame->lines[to_line].char_count;
  5844. selection.to_item = to_item;
  5845. selection.active = true;
  5846. queue_accessibility_update();
  5847. queue_redraw();
  5848. }
  5849. bool RichTextLabel::is_selection_enabled() const {
  5850. return selection.enabled;
  5851. }
  5852. bool RichTextLabel::is_deselect_on_focus_loss_enabled() const {
  5853. return deselect_on_focus_loss_enabled;
  5854. }
  5855. void RichTextLabel::set_drag_and_drop_selection_enabled(const bool p_enabled) {
  5856. drag_and_drop_selection_enabled = p_enabled;
  5857. }
  5858. bool RichTextLabel::is_drag_and_drop_selection_enabled() const {
  5859. return drag_and_drop_selection_enabled;
  5860. }
  5861. int RichTextLabel::get_selection_from() const {
  5862. if (!selection.active || !selection.enabled) {
  5863. return -1;
  5864. }
  5865. return selection.from_frame->lines[selection.from_line].char_offset + selection.from_char;
  5866. }
  5867. int RichTextLabel::get_selection_to() const {
  5868. if (!selection.active || !selection.enabled) {
  5869. return -1;
  5870. }
  5871. return selection.to_frame->lines[selection.to_line].char_offset + selection.to_char - 1;
  5872. }
  5873. float RichTextLabel::get_selection_line_offset() const {
  5874. if (selection.active && selection.from_frame && selection.from_line >= 0 && selection.from_line < (int)selection.from_frame->lines.size()) {
  5875. // Selected frame paragraph offset.
  5876. float line_offset = selection.from_frame->lines[selection.from_line].offset.y;
  5877. // Add wrapped line offset.
  5878. for (int i = 0; i < selection.from_frame->lines[selection.from_line].text_buf->get_line_count(); i++) {
  5879. Vector2i range = selection.from_frame->lines[selection.from_line].text_buf->get_line_range(i);
  5880. if (range.x <= selection.from_char && range.y >= selection.from_char) {
  5881. break;
  5882. }
  5883. line_offset += selection.from_frame->lines[selection.from_line].text_buf->get_line_ascent(i) + selection.from_frame->lines[selection.from_line].text_buf->get_line_descent(i) + theme_cache.line_separation;
  5884. }
  5885. // Add nested frame (e.g. table cell) offset.
  5886. ItemFrame *it = selection.from_frame;
  5887. while (it->parent_frame != nullptr) {
  5888. line_offset += it->parent_frame->lines[it->line].offset.y;
  5889. it = it->parent_frame;
  5890. }
  5891. return line_offset;
  5892. }
  5893. return -1.0;
  5894. }
  5895. void RichTextLabel::set_text(const String &p_bbcode) {
  5896. // Allow clearing the tag stack.
  5897. if (!p_bbcode.is_empty() && text == p_bbcode) {
  5898. return;
  5899. }
  5900. stack_externally_modified = false;
  5901. text = p_bbcode;
  5902. if (text.is_empty()) {
  5903. clear();
  5904. } else {
  5905. _apply_translation();
  5906. }
  5907. }
  5908. void RichTextLabel::_apply_translation() {
  5909. if (text.is_empty()) {
  5910. return;
  5911. }
  5912. internal_stack_editing = true;
  5913. String xl_text = atr(text);
  5914. if (use_bbcode) {
  5915. parse_bbcode(xl_text);
  5916. } else { // Raw text.
  5917. clear();
  5918. add_text(xl_text);
  5919. }
  5920. internal_stack_editing = false;
  5921. }
  5922. String RichTextLabel::get_text() const {
  5923. return text;
  5924. }
  5925. void RichTextLabel::set_use_bbcode(bool p_enable) {
  5926. if (use_bbcode == p_enable) {
  5927. return;
  5928. }
  5929. use_bbcode = p_enable;
  5930. notify_property_list_changed();
  5931. if (!stack_externally_modified) {
  5932. _apply_translation();
  5933. }
  5934. }
  5935. bool RichTextLabel::is_using_bbcode() const {
  5936. return use_bbcode;
  5937. }
  5938. String RichTextLabel::get_parsed_text() const {
  5939. String txt;
  5940. Item *it = main;
  5941. while (it) {
  5942. if (it->type == ITEM_DROPCAP) {
  5943. ItemDropcap *dc = static_cast<ItemDropcap *>(it);
  5944. txt += dc->text;
  5945. } else if (it->type == ITEM_TEXT) {
  5946. ItemText *t = static_cast<ItemText *>(it);
  5947. txt += t->text;
  5948. } else if (it->type == ITEM_NEWLINE) {
  5949. txt += "\n";
  5950. } else if (it->type == ITEM_IMAGE) {
  5951. txt += " ";
  5952. } else if (it->type == ITEM_INDENT || it->type == ITEM_LIST) {
  5953. txt += "\t";
  5954. }
  5955. it = _get_next_item(it, true);
  5956. }
  5957. return txt;
  5958. }
  5959. void RichTextLabel::set_text_direction(Control::TextDirection p_text_direction) {
  5960. ERR_FAIL_COND((int)p_text_direction < -1 || (int)p_text_direction > 3);
  5961. _stop_thread();
  5962. if (text_direction != p_text_direction) {
  5963. text_direction = p_text_direction;
  5964. if (!stack_externally_modified) {
  5965. _apply_translation();
  5966. } else {
  5967. main->first_invalid_line.store(0); // Invalidate all lines.
  5968. _invalidate_accessibility();
  5969. _validate_line_caches();
  5970. }
  5971. queue_redraw();
  5972. }
  5973. }
  5974. Control::TextDirection RichTextLabel::get_text_direction() const {
  5975. return text_direction;
  5976. }
  5977. void RichTextLabel::set_horizontal_alignment(HorizontalAlignment p_alignment) {
  5978. ERR_FAIL_INDEX((int)p_alignment, 4);
  5979. _stop_thread();
  5980. if (default_alignment != p_alignment) {
  5981. default_alignment = p_alignment;
  5982. if (!stack_externally_modified) {
  5983. _apply_translation();
  5984. } else {
  5985. main->first_invalid_line.store(0); // Invalidate all lines.
  5986. _validate_line_caches();
  5987. }
  5988. queue_redraw();
  5989. }
  5990. }
  5991. HorizontalAlignment RichTextLabel::get_horizontal_alignment() const {
  5992. return default_alignment;
  5993. }
  5994. void RichTextLabel::set_vertical_alignment(VerticalAlignment p_alignment) {
  5995. ERR_FAIL_INDEX((int)p_alignment, 4);
  5996. if (vertical_alignment == p_alignment) {
  5997. return;
  5998. }
  5999. vertical_alignment = p_alignment;
  6000. queue_redraw();
  6001. }
  6002. VerticalAlignment RichTextLabel::get_vertical_alignment() const {
  6003. return vertical_alignment;
  6004. }
  6005. void RichTextLabel::set_justification_flags(BitField<TextServer::JustificationFlag> p_flags) {
  6006. _stop_thread();
  6007. if (default_jst_flags != p_flags) {
  6008. default_jst_flags = p_flags;
  6009. if (!stack_externally_modified) {
  6010. _apply_translation();
  6011. } else {
  6012. main->first_invalid_line.store(0); // Invalidate all lines.
  6013. _validate_line_caches();
  6014. }
  6015. queue_redraw();
  6016. }
  6017. }
  6018. BitField<TextServer::JustificationFlag> RichTextLabel::get_justification_flags() const {
  6019. return default_jst_flags;
  6020. }
  6021. void RichTextLabel::set_tab_stops(const PackedFloat32Array &p_tab_stops) {
  6022. _stop_thread();
  6023. if (default_tab_stops != p_tab_stops) {
  6024. default_tab_stops = p_tab_stops;
  6025. if (!stack_externally_modified) {
  6026. _apply_translation();
  6027. } else {
  6028. main->first_invalid_line.store(0); // Invalidate all lines.
  6029. _validate_line_caches();
  6030. }
  6031. queue_redraw();
  6032. }
  6033. }
  6034. PackedFloat32Array RichTextLabel::get_tab_stops() const {
  6035. return default_tab_stops;
  6036. }
  6037. void RichTextLabel::set_structured_text_bidi_override(TextServer::StructuredTextParser p_parser) {
  6038. if (st_parser != p_parser) {
  6039. _stop_thread();
  6040. st_parser = p_parser;
  6041. if (!stack_externally_modified) {
  6042. _apply_translation();
  6043. } else {
  6044. main->first_invalid_line.store(0); // Invalidate all lines.
  6045. _invalidate_accessibility();
  6046. _validate_line_caches();
  6047. }
  6048. queue_redraw();
  6049. }
  6050. }
  6051. TextServer::StructuredTextParser RichTextLabel::get_structured_text_bidi_override() const {
  6052. return st_parser;
  6053. }
  6054. void RichTextLabel::set_structured_text_bidi_override_options(Array p_args) {
  6055. if (st_args != p_args) {
  6056. _stop_thread();
  6057. st_args = p_args;
  6058. main->first_invalid_line.store(0); // Invalidate all lines.
  6059. _invalidate_accessibility();
  6060. _validate_line_caches();
  6061. queue_redraw();
  6062. }
  6063. }
  6064. Array RichTextLabel::get_structured_text_bidi_override_options() const {
  6065. return st_args;
  6066. }
  6067. void RichTextLabel::set_language(const String &p_language) {
  6068. if (language != p_language) {
  6069. _stop_thread();
  6070. language = p_language;
  6071. if (!stack_externally_modified) {
  6072. _apply_translation();
  6073. } else {
  6074. main->first_invalid_line.store(0); // Invalidate all lines.
  6075. _invalidate_accessibility();
  6076. _validate_line_caches();
  6077. }
  6078. queue_redraw();
  6079. }
  6080. }
  6081. String RichTextLabel::get_language() const {
  6082. return language;
  6083. }
  6084. void RichTextLabel::set_autowrap_mode(TextServer::AutowrapMode p_mode) {
  6085. if (autowrap_mode != p_mode) {
  6086. _stop_thread();
  6087. autowrap_mode = p_mode;
  6088. main->first_invalid_line = 0; // Invalidate all lines.
  6089. _invalidate_accessibility();
  6090. _validate_line_caches();
  6091. queue_redraw();
  6092. }
  6093. }
  6094. TextServer::AutowrapMode RichTextLabel::get_autowrap_mode() const {
  6095. return autowrap_mode;
  6096. }
  6097. void RichTextLabel::set_autowrap_trim_flags(BitField<TextServer::LineBreakFlag> p_flags) {
  6098. if (autowrap_flags_trim != (p_flags & TextServer::BREAK_TRIM_MASK)) {
  6099. _stop_thread();
  6100. autowrap_flags_trim = p_flags & TextServer::BREAK_TRIM_MASK;
  6101. main->first_invalid_line = 0; // Invalidate all lines.
  6102. _validate_line_caches();
  6103. queue_redraw();
  6104. }
  6105. }
  6106. BitField<TextServer::LineBreakFlag> RichTextLabel::get_autowrap_trim_flags() const {
  6107. return autowrap_flags_trim;
  6108. }
  6109. void RichTextLabel::set_visible_ratio(float p_ratio) {
  6110. if (visible_ratio != p_ratio) {
  6111. _stop_thread();
  6112. if (p_ratio >= 1.0) {
  6113. visible_characters = -1;
  6114. visible_ratio = 1.0;
  6115. } else if (p_ratio < 0.0) {
  6116. visible_characters = 0;
  6117. visible_ratio = 0.0;
  6118. } else {
  6119. visible_characters = get_total_character_count() * p_ratio;
  6120. visible_ratio = p_ratio;
  6121. }
  6122. if (visible_chars_behavior == TextServer::VC_CHARS_BEFORE_SHAPING) {
  6123. main->first_invalid_line.store(0); // Invalidate all lines..
  6124. _invalidate_accessibility();
  6125. _validate_line_caches();
  6126. }
  6127. queue_redraw();
  6128. }
  6129. }
  6130. float RichTextLabel::get_visible_ratio() const {
  6131. return visible_ratio;
  6132. }
  6133. void RichTextLabel::set_effects(Array p_effects) {
  6134. custom_effects = p_effects;
  6135. reload_effects();
  6136. }
  6137. Array RichTextLabel::get_effects() {
  6138. return custom_effects;
  6139. }
  6140. void RichTextLabel::install_effect(const Variant effect) {
  6141. Ref<RichTextEffect> rteffect;
  6142. rteffect = effect;
  6143. ERR_FAIL_COND_MSG(rteffect.is_null(), "Invalid RichTextEffect resource.");
  6144. custom_effects.push_back(effect);
  6145. reload_effects();
  6146. }
  6147. void RichTextLabel::reload_effects() {
  6148. if (!stack_externally_modified && use_bbcode) {
  6149. internal_stack_editing = true;
  6150. parse_bbcode(atr(text));
  6151. internal_stack_editing = false;
  6152. }
  6153. }
  6154. int RichTextLabel::get_content_height() const {
  6155. const_cast<RichTextLabel *>(this)->_validate_line_caches();
  6156. int total_height = 0;
  6157. int to_line = main->first_invalid_line.load();
  6158. if (to_line) {
  6159. MutexLock lock(main->lines[to_line - 1].text_buf->get_mutex());
  6160. if (theme_cache.line_separation < 0) {
  6161. // Do not apply to the last line to avoid cutting text.
  6162. 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;
  6163. } else {
  6164. 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;
  6165. }
  6166. }
  6167. return total_height;
  6168. }
  6169. int RichTextLabel::get_content_width() const {
  6170. const_cast<RichTextLabel *>(this)->_validate_line_caches();
  6171. int total_width = 0;
  6172. int to_line = main->first_invalid_line.load();
  6173. for (int i = 0; i < to_line; i++) {
  6174. MutexLock lock(main->lines[i].text_buf->get_mutex());
  6175. total_width = MAX(total_width, main->lines[i].offset.x + main->lines[i].text_buf->get_size().x);
  6176. }
  6177. return total_width;
  6178. }
  6179. int RichTextLabel::get_line_height(int p_line) const {
  6180. const_cast<RichTextLabel *>(this)->_validate_line_caches();
  6181. int line_count = 0;
  6182. int to_line = main->first_invalid_line.load();
  6183. for (int i = 0; i < to_line; i++) {
  6184. MutexLock lock(main->lines[i].text_buf->get_mutex());
  6185. int lc = main->lines[i].text_buf->get_line_count();
  6186. if (p_line < line_count + lc) {
  6187. const Ref<TextParagraph> text_buf = main->lines[i].text_buf;
  6188. return text_buf->get_line_ascent(p_line - line_count) + text_buf->get_line_descent(p_line - line_count) + theme_cache.line_separation;
  6189. }
  6190. line_count += lc;
  6191. }
  6192. return 0;
  6193. }
  6194. int RichTextLabel::get_line_width(int p_line) const {
  6195. const_cast<RichTextLabel *>(this)->_validate_line_caches();
  6196. int line_count = 0;
  6197. int to_line = main->first_invalid_line.load();
  6198. for (int i = 0; i < to_line; i++) {
  6199. MutexLock lock(main->lines[i].text_buf->get_mutex());
  6200. int lc = main->lines[i].text_buf->get_line_count();
  6201. if (p_line < line_count + lc) {
  6202. return main->lines[i].text_buf->get_line_width(p_line - line_count);
  6203. }
  6204. line_count += lc;
  6205. }
  6206. return 0;
  6207. }
  6208. #ifndef DISABLE_DEPRECATED
  6209. // People will be very angry, if their texts get erased, because of #39148. (3.x -> 4.0)
  6210. // Although some people may not used bbcode_text, so we only overwrite, if bbcode_text is not empty.
  6211. bool RichTextLabel::_set(const StringName &p_name, const Variant &p_value) {
  6212. if (p_name == "bbcode_text" && !((String)p_value).is_empty()) {
  6213. set_text(p_value);
  6214. return true;
  6215. }
  6216. return false;
  6217. }
  6218. #endif
  6219. void RichTextLabel::_bind_methods() {
  6220. ClassDB::bind_method(D_METHOD("get_parsed_text"), &RichTextLabel::get_parsed_text);
  6221. ClassDB::bind_method(D_METHOD("add_text", "text"), &RichTextLabel::add_text);
  6222. ClassDB::bind_method(D_METHOD("set_text", "text"), &RichTextLabel::set_text);
  6223. ClassDB::bind_method(D_METHOD("add_image", "image", "width", "height", "color", "inline_align", "region", "key", "pad", "tooltip", "size_in_percent", "alt_text"), &RichTextLabel::add_image, DEFVAL(0), DEFVAL(0), DEFVAL(Color(1.0, 1.0, 1.0)), DEFVAL(INLINE_ALIGNMENT_CENTER), DEFVAL(Rect2()), DEFVAL(Variant()), DEFVAL(false), DEFVAL(String()), DEFVAL(false), DEFVAL(String()));
  6224. ClassDB::bind_method(D_METHOD("update_image", "key", "mask", "image", "width", "height", "color", "inline_align", "region", "pad", "tooltip", "size_in_percent"), &RichTextLabel::update_image, DEFVAL(0), DEFVAL(0), DEFVAL(Color(1.0, 1.0, 1.0)), DEFVAL(INLINE_ALIGNMENT_CENTER), DEFVAL(Rect2()), DEFVAL(false), DEFVAL(String()), DEFVAL(false));
  6225. ClassDB::bind_method(D_METHOD("newline"), &RichTextLabel::add_newline);
  6226. ClassDB::bind_method(D_METHOD("remove_paragraph", "paragraph", "no_invalidate"), &RichTextLabel::remove_paragraph, DEFVAL(false));
  6227. ClassDB::bind_method(D_METHOD("invalidate_paragraph", "paragraph"), &RichTextLabel::invalidate_paragraph);
  6228. ClassDB::bind_method(D_METHOD("push_font", "font", "font_size"), &RichTextLabel::push_font, DEFVAL(0));
  6229. ClassDB::bind_method(D_METHOD("push_font_size", "font_size"), &RichTextLabel::push_font_size);
  6230. ClassDB::bind_method(D_METHOD("push_normal"), &RichTextLabel::push_normal);
  6231. ClassDB::bind_method(D_METHOD("push_bold"), &RichTextLabel::push_bold);
  6232. ClassDB::bind_method(D_METHOD("push_bold_italics"), &RichTextLabel::push_bold_italics);
  6233. ClassDB::bind_method(D_METHOD("push_italics"), &RichTextLabel::push_italics);
  6234. ClassDB::bind_method(D_METHOD("push_mono"), &RichTextLabel::push_mono);
  6235. ClassDB::bind_method(D_METHOD("push_color", "color"), &RichTextLabel::push_color);
  6236. ClassDB::bind_method(D_METHOD("push_outline_size", "outline_size"), &RichTextLabel::push_outline_size);
  6237. ClassDB::bind_method(D_METHOD("push_outline_color", "color"), &RichTextLabel::push_outline_color);
  6238. ClassDB::bind_method(D_METHOD("push_paragraph", "alignment", "base_direction", "language", "st_parser", "justification_flags", "tab_stops"), &RichTextLabel::push_paragraph, DEFVAL(TextServer::DIRECTION_AUTO), DEFVAL(""), DEFVAL(TextServer::STRUCTURED_TEXT_DEFAULT), DEFVAL(TextServer::JUSTIFICATION_WORD_BOUND | TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_SKIP_LAST_LINE | TextServer::JUSTIFICATION_DO_NOT_SKIP_SINGLE_LINE), DEFVAL(PackedFloat32Array()));
  6239. ClassDB::bind_method(D_METHOD("push_indent", "level"), &RichTextLabel::push_indent);
  6240. ClassDB::bind_method(D_METHOD("push_list", "level", "type", "capitalize", "bullet"), &RichTextLabel::push_list, DEFVAL(String::utf8("•")));
  6241. ClassDB::bind_method(D_METHOD("push_meta", "data", "underline_mode", "tooltip"), &RichTextLabel::push_meta, DEFVAL(META_UNDERLINE_ALWAYS), DEFVAL(String()));
  6242. ClassDB::bind_method(D_METHOD("push_hint", "description"), &RichTextLabel::push_hint);
  6243. ClassDB::bind_method(D_METHOD("push_language", "language"), &RichTextLabel::push_language);
  6244. ClassDB::bind_method(D_METHOD("push_underline"), &RichTextLabel::push_underline);
  6245. ClassDB::bind_method(D_METHOD("push_strikethrough"), &RichTextLabel::push_strikethrough);
  6246. ClassDB::bind_method(D_METHOD("push_table", "columns", "inline_align", "align_to_row", "name"), &RichTextLabel::push_table, DEFVAL(INLINE_ALIGNMENT_TOP), DEFVAL(-1), DEFVAL(String()));
  6247. 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)));
  6248. ClassDB::bind_method(D_METHOD("set_table_column_expand", "column", "expand", "ratio", "shrink"), &RichTextLabel::set_table_column_expand, DEFVAL(1), DEFVAL(true));
  6249. ClassDB::bind_method(D_METHOD("set_table_column_name", "column", "name"), &RichTextLabel::set_table_column_name);
  6250. ClassDB::bind_method(D_METHOD("set_cell_row_background_color", "odd_row_bg", "even_row_bg"), &RichTextLabel::set_cell_row_background_color);
  6251. ClassDB::bind_method(D_METHOD("set_cell_border_color", "color"), &RichTextLabel::set_cell_border_color);
  6252. ClassDB::bind_method(D_METHOD("set_cell_size_override", "min_size", "max_size"), &RichTextLabel::set_cell_size_override);
  6253. ClassDB::bind_method(D_METHOD("set_cell_padding", "padding"), &RichTextLabel::set_cell_padding);
  6254. ClassDB::bind_method(D_METHOD("push_cell"), &RichTextLabel::push_cell);
  6255. ClassDB::bind_method(D_METHOD("push_fgcolor", "fgcolor"), &RichTextLabel::push_fgcolor);
  6256. ClassDB::bind_method(D_METHOD("push_bgcolor", "bgcolor"), &RichTextLabel::push_bgcolor);
  6257. ClassDB::bind_method(D_METHOD("push_customfx", "effect", "env"), &RichTextLabel::push_customfx);
  6258. ClassDB::bind_method(D_METHOD("push_context"), &RichTextLabel::push_context);
  6259. ClassDB::bind_method(D_METHOD("pop_context"), &RichTextLabel::pop_context);
  6260. ClassDB::bind_method(D_METHOD("pop"), &RichTextLabel::pop);
  6261. ClassDB::bind_method(D_METHOD("pop_all"), &RichTextLabel::pop_all);
  6262. ClassDB::bind_method(D_METHOD("clear"), &RichTextLabel::clear);
  6263. ClassDB::bind_method(D_METHOD("set_structured_text_bidi_override", "parser"), &RichTextLabel::set_structured_text_bidi_override);
  6264. ClassDB::bind_method(D_METHOD("get_structured_text_bidi_override"), &RichTextLabel::get_structured_text_bidi_override);
  6265. ClassDB::bind_method(D_METHOD("set_structured_text_bidi_override_options", "args"), &RichTextLabel::set_structured_text_bidi_override_options);
  6266. ClassDB::bind_method(D_METHOD("get_structured_text_bidi_override_options"), &RichTextLabel::get_structured_text_bidi_override_options);
  6267. ClassDB::bind_method(D_METHOD("set_text_direction", "direction"), &RichTextLabel::set_text_direction);
  6268. ClassDB::bind_method(D_METHOD("get_text_direction"), &RichTextLabel::get_text_direction);
  6269. ClassDB::bind_method(D_METHOD("set_language", "language"), &RichTextLabel::set_language);
  6270. ClassDB::bind_method(D_METHOD("get_language"), &RichTextLabel::get_language);
  6271. ClassDB::bind_method(D_METHOD("set_horizontal_alignment", "alignment"), &RichTextLabel::set_horizontal_alignment);
  6272. ClassDB::bind_method(D_METHOD("get_horizontal_alignment"), &RichTextLabel::get_horizontal_alignment);
  6273. ClassDB::bind_method(D_METHOD("set_vertical_alignment", "alignment"), &RichTextLabel::set_vertical_alignment);
  6274. ClassDB::bind_method(D_METHOD("get_vertical_alignment"), &RichTextLabel::get_vertical_alignment);
  6275. ClassDB::bind_method(D_METHOD("set_justification_flags", "justification_flags"), &RichTextLabel::set_justification_flags);
  6276. ClassDB::bind_method(D_METHOD("get_justification_flags"), &RichTextLabel::get_justification_flags);
  6277. ClassDB::bind_method(D_METHOD("set_tab_stops", "tab_stops"), &RichTextLabel::set_tab_stops);
  6278. ClassDB::bind_method(D_METHOD("get_tab_stops"), &RichTextLabel::get_tab_stops);
  6279. ClassDB::bind_method(D_METHOD("set_autowrap_mode", "autowrap_mode"), &RichTextLabel::set_autowrap_mode);
  6280. ClassDB::bind_method(D_METHOD("get_autowrap_mode"), &RichTextLabel::get_autowrap_mode);
  6281. ClassDB::bind_method(D_METHOD("set_autowrap_trim_flags", "autowrap_trim_flags"), &RichTextLabel::set_autowrap_trim_flags);
  6282. ClassDB::bind_method(D_METHOD("get_autowrap_trim_flags"), &RichTextLabel::get_autowrap_trim_flags);
  6283. ClassDB::bind_method(D_METHOD("set_meta_underline", "enable"), &RichTextLabel::set_meta_underline);
  6284. ClassDB::bind_method(D_METHOD("is_meta_underlined"), &RichTextLabel::is_meta_underlined);
  6285. ClassDB::bind_method(D_METHOD("set_hint_underline", "enable"), &RichTextLabel::set_hint_underline);
  6286. ClassDB::bind_method(D_METHOD("is_hint_underlined"), &RichTextLabel::is_hint_underlined);
  6287. ClassDB::bind_method(D_METHOD("set_scroll_active", "active"), &RichTextLabel::set_scroll_active);
  6288. ClassDB::bind_method(D_METHOD("is_scroll_active"), &RichTextLabel::is_scroll_active);
  6289. ClassDB::bind_method(D_METHOD("set_scroll_follow", "follow"), &RichTextLabel::set_scroll_follow);
  6290. ClassDB::bind_method(D_METHOD("is_scroll_following"), &RichTextLabel::is_scroll_following);
  6291. ClassDB::bind_method(D_METHOD("get_v_scroll_bar"), &RichTextLabel::get_v_scroll_bar);
  6292. ClassDB::bind_method(D_METHOD("scroll_to_line", "line"), &RichTextLabel::scroll_to_line);
  6293. ClassDB::bind_method(D_METHOD("scroll_to_paragraph", "paragraph"), &RichTextLabel::scroll_to_paragraph);
  6294. ClassDB::bind_method(D_METHOD("scroll_to_selection"), &RichTextLabel::scroll_to_selection);
  6295. ClassDB::bind_method(D_METHOD("set_tab_size", "spaces"), &RichTextLabel::set_tab_size);
  6296. ClassDB::bind_method(D_METHOD("get_tab_size"), &RichTextLabel::get_tab_size);
  6297. ClassDB::bind_method(D_METHOD("set_fit_content", "enabled"), &RichTextLabel::set_fit_content);
  6298. ClassDB::bind_method(D_METHOD("is_fit_content_enabled"), &RichTextLabel::is_fit_content_enabled);
  6299. ClassDB::bind_method(D_METHOD("set_selection_enabled", "enabled"), &RichTextLabel::set_selection_enabled);
  6300. ClassDB::bind_method(D_METHOD("is_selection_enabled"), &RichTextLabel::is_selection_enabled);
  6301. ClassDB::bind_method(D_METHOD("set_context_menu_enabled", "enabled"), &RichTextLabel::set_context_menu_enabled);
  6302. ClassDB::bind_method(D_METHOD("is_context_menu_enabled"), &RichTextLabel::is_context_menu_enabled);
  6303. ClassDB::bind_method(D_METHOD("set_shortcut_keys_enabled", "enabled"), &RichTextLabel::set_shortcut_keys_enabled);
  6304. ClassDB::bind_method(D_METHOD("is_shortcut_keys_enabled"), &RichTextLabel::is_shortcut_keys_enabled);
  6305. ClassDB::bind_method(D_METHOD("set_deselect_on_focus_loss_enabled", "enable"), &RichTextLabel::set_deselect_on_focus_loss_enabled);
  6306. ClassDB::bind_method(D_METHOD("is_deselect_on_focus_loss_enabled"), &RichTextLabel::is_deselect_on_focus_loss_enabled);
  6307. ClassDB::bind_method(D_METHOD("set_drag_and_drop_selection_enabled", "enable"), &RichTextLabel::set_drag_and_drop_selection_enabled);
  6308. ClassDB::bind_method(D_METHOD("is_drag_and_drop_selection_enabled"), &RichTextLabel::is_drag_and_drop_selection_enabled);
  6309. ClassDB::bind_method(D_METHOD("get_selection_from"), &RichTextLabel::get_selection_from);
  6310. ClassDB::bind_method(D_METHOD("get_selection_to"), &RichTextLabel::get_selection_to);
  6311. ClassDB::bind_method(D_METHOD("get_selection_line_offset"), &RichTextLabel::get_selection_line_offset);
  6312. ClassDB::bind_method(D_METHOD("select_all"), &RichTextLabel::select_all);
  6313. ClassDB::bind_method(D_METHOD("get_selected_text"), &RichTextLabel::get_selected_text);
  6314. ClassDB::bind_method(D_METHOD("deselect"), &RichTextLabel::deselect);
  6315. ClassDB::bind_method(D_METHOD("parse_bbcode", "bbcode"), &RichTextLabel::parse_bbcode);
  6316. ClassDB::bind_method(D_METHOD("append_text", "bbcode"), &RichTextLabel::append_text);
  6317. ClassDB::bind_method(D_METHOD("get_text"), &RichTextLabel::get_text);
  6318. #ifndef DISABLE_DEPRECATED
  6319. ClassDB::bind_method(D_METHOD("is_ready"), &RichTextLabel::is_finished);
  6320. #endif // DISABLE_DEPRECATED
  6321. ClassDB::bind_method(D_METHOD("is_finished"), &RichTextLabel::is_finished);
  6322. ClassDB::bind_method(D_METHOD("set_threaded", "threaded"), &RichTextLabel::set_threaded);
  6323. ClassDB::bind_method(D_METHOD("is_threaded"), &RichTextLabel::is_threaded);
  6324. ClassDB::bind_method(D_METHOD("set_progress_bar_delay", "delay_ms"), &RichTextLabel::set_progress_bar_delay);
  6325. ClassDB::bind_method(D_METHOD("get_progress_bar_delay"), &RichTextLabel::get_progress_bar_delay);
  6326. ClassDB::bind_method(D_METHOD("set_visible_characters", "amount"), &RichTextLabel::set_visible_characters);
  6327. ClassDB::bind_method(D_METHOD("get_visible_characters"), &RichTextLabel::get_visible_characters);
  6328. ClassDB::bind_method(D_METHOD("get_visible_characters_behavior"), &RichTextLabel::get_visible_characters_behavior);
  6329. ClassDB::bind_method(D_METHOD("set_visible_characters_behavior", "behavior"), &RichTextLabel::set_visible_characters_behavior);
  6330. ClassDB::bind_method(D_METHOD("set_visible_ratio", "ratio"), &RichTextLabel::set_visible_ratio);
  6331. ClassDB::bind_method(D_METHOD("get_visible_ratio"), &RichTextLabel::get_visible_ratio);
  6332. ClassDB::bind_method(D_METHOD("get_character_line", "character"), &RichTextLabel::get_character_line);
  6333. ClassDB::bind_method(D_METHOD("get_character_paragraph", "character"), &RichTextLabel::get_character_paragraph);
  6334. ClassDB::bind_method(D_METHOD("get_total_character_count"), &RichTextLabel::get_total_character_count);
  6335. ClassDB::bind_method(D_METHOD("set_use_bbcode", "enable"), &RichTextLabel::set_use_bbcode);
  6336. ClassDB::bind_method(D_METHOD("is_using_bbcode"), &RichTextLabel::is_using_bbcode);
  6337. ClassDB::bind_method(D_METHOD("get_line_count"), &RichTextLabel::get_line_count);
  6338. ClassDB::bind_method(D_METHOD("get_line_range", "line"), &RichTextLabel::get_line_range);
  6339. ClassDB::bind_method(D_METHOD("get_visible_line_count"), &RichTextLabel::get_visible_line_count);
  6340. ClassDB::bind_method(D_METHOD("get_paragraph_count"), &RichTextLabel::get_paragraph_count);
  6341. ClassDB::bind_method(D_METHOD("get_visible_paragraph_count"), &RichTextLabel::get_visible_paragraph_count);
  6342. ClassDB::bind_method(D_METHOD("get_content_height"), &RichTextLabel::get_content_height);
  6343. ClassDB::bind_method(D_METHOD("get_content_width"), &RichTextLabel::get_content_width);
  6344. ClassDB::bind_method(D_METHOD("get_line_height", "line"), &RichTextLabel::get_line_height);
  6345. ClassDB::bind_method(D_METHOD("get_line_width", "line"), &RichTextLabel::get_line_width);
  6346. ClassDB::bind_method(D_METHOD("get_line_offset", "line"), &RichTextLabel::get_line_offset);
  6347. ClassDB::bind_method(D_METHOD("get_paragraph_offset", "paragraph"), &RichTextLabel::get_paragraph_offset);
  6348. ClassDB::bind_method(D_METHOD("parse_expressions_for_values", "expressions"), &RichTextLabel::parse_expressions_for_values);
  6349. ClassDB::bind_method(D_METHOD("set_effects", "effects"), &RichTextLabel::set_effects);
  6350. ClassDB::bind_method(D_METHOD("get_effects"), &RichTextLabel::get_effects);
  6351. ClassDB::bind_method(D_METHOD("install_effect", "effect"), &RichTextLabel::install_effect);
  6352. ClassDB::bind_method(D_METHOD("reload_effects"), &RichTextLabel::reload_effects);
  6353. ClassDB::bind_method(D_METHOD("get_menu"), &RichTextLabel::get_menu);
  6354. ClassDB::bind_method(D_METHOD("is_menu_visible"), &RichTextLabel::is_menu_visible);
  6355. ClassDB::bind_method(D_METHOD("menu_option", "option"), &RichTextLabel::menu_option);
  6356. // Note: set "bbcode_enabled" first, to avoid unnecessary "text" resets.
  6357. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "bbcode_enabled"), "set_use_bbcode", "is_using_bbcode");
  6358. ADD_PROPERTY(PropertyInfo(Variant::STRING, "text", PROPERTY_HINT_MULTILINE_TEXT), "set_text", "get_text");
  6359. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "fit_content"), "set_fit_content", "is_fit_content_enabled");
  6360. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_active"), "set_scroll_active", "is_scroll_active");
  6361. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_following"), "set_scroll_follow", "is_scroll_following");
  6362. ADD_PROPERTY(PropertyInfo(Variant::INT, "autowrap_mode", PROPERTY_HINT_ENUM, "Off,Arbitrary,Word,Word (Smart)"), "set_autowrap_mode", "get_autowrap_mode");
  6363. ADD_PROPERTY(PropertyInfo(Variant::INT, "autowrap_trim_flags", PROPERTY_HINT_FLAGS, vformat("Trim Spaces After Break:%d,Trim Spaces Before Break:%d", TextServer::BREAK_TRIM_START_EDGE_SPACES, TextServer::BREAK_TRIM_END_EDGE_SPACES)), "set_autowrap_trim_flags", "get_autowrap_trim_flags");
  6364. ADD_PROPERTY(PropertyInfo(Variant::INT, "tab_size", PROPERTY_HINT_RANGE, "0,24,1"), "set_tab_size", "get_tab_size");
  6365. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "context_menu_enabled"), "set_context_menu_enabled", "is_context_menu_enabled");
  6366. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shortcut_keys_enabled"), "set_shortcut_keys_enabled", "is_shortcut_keys_enabled");
  6367. ADD_PROPERTY(PropertyInfo(Variant::INT, "horizontal_alignment", PROPERTY_HINT_ENUM, "Left,Center,Right,Fill"), "set_horizontal_alignment", "get_horizontal_alignment");
  6368. ADD_PROPERTY(PropertyInfo(Variant::INT, "vertical_alignment", PROPERTY_HINT_ENUM, "Top,Center,Bottom,Fill"), "set_vertical_alignment", "get_vertical_alignment");
  6369. ADD_PROPERTY(PropertyInfo(Variant::INT, "justification_flags", PROPERTY_HINT_FLAGS, "Kashida Justification:1,Word Justification:2,Justify Only After Last Tab:8,Skip Last Line:32,Skip Last Line With Visible Characters:64,Do Not Skip Single Line:128"), "set_justification_flags", "get_justification_flags");
  6370. ADD_PROPERTY(PropertyInfo(Variant::PACKED_FLOAT32_ARRAY, "tab_stops"), "set_tab_stops", "get_tab_stops");
  6371. ADD_GROUP("Markup", "");
  6372. 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");
  6373. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "meta_underlined"), "set_meta_underline", "is_meta_underlined");
  6374. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hint_underlined"), "set_hint_underline", "is_hint_underlined");
  6375. ADD_GROUP("Threading", "");
  6376. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "threaded"), "set_threaded", "is_threaded");
  6377. ADD_PROPERTY(PropertyInfo(Variant::INT, "progress_bar_delay", PROPERTY_HINT_NONE, "suffix:ms"), "set_progress_bar_delay", "get_progress_bar_delay");
  6378. ADD_GROUP("Text Selection", "");
  6379. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "selection_enabled"), "set_selection_enabled", "is_selection_enabled");
  6380. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "deselect_on_focus_loss_enabled"), "set_deselect_on_focus_loss_enabled", "is_deselect_on_focus_loss_enabled");
  6381. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "drag_and_drop_selection_enabled"), "set_drag_and_drop_selection_enabled", "is_drag_and_drop_selection_enabled");
  6382. ADD_GROUP("Displayed Text", "");
  6383. // Note: "visible_characters" and "visible_ratio" should be set after "text" to be correctly applied.
  6384. ADD_PROPERTY(PropertyInfo(Variant::INT, "visible_characters", PROPERTY_HINT_RANGE, "-1,128000,1"), "set_visible_characters", "get_visible_characters");
  6385. 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");
  6386. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "visible_ratio", PROPERTY_HINT_RANGE, "0,1,0.001"), "set_visible_ratio", "get_visible_ratio");
  6387. ADD_GROUP("BiDi", "");
  6388. ADD_PROPERTY(PropertyInfo(Variant::INT, "text_direction", PROPERTY_HINT_ENUM, "Auto,Left-to-Right,Right-to-Left,Inherited"), "set_text_direction", "get_text_direction");
  6389. ADD_PROPERTY(PropertyInfo(Variant::STRING, "language", PROPERTY_HINT_LOCALE_ID, ""), "set_language", "get_language");
  6390. 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");
  6391. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "structured_text_bidi_override_options"), "set_structured_text_bidi_override_options", "get_structured_text_bidi_override_options");
  6392. ADD_SIGNAL(MethodInfo("meta_clicked", PropertyInfo(Variant::NIL, "meta", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NIL_IS_VARIANT)));
  6393. ADD_SIGNAL(MethodInfo("meta_hover_started", PropertyInfo(Variant::NIL, "meta", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NIL_IS_VARIANT)));
  6394. ADD_SIGNAL(MethodInfo("meta_hover_ended", PropertyInfo(Variant::NIL, "meta", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NIL_IS_VARIANT)));
  6395. ADD_SIGNAL(MethodInfo("finished"));
  6396. BIND_ENUM_CONSTANT(LIST_NUMBERS);
  6397. BIND_ENUM_CONSTANT(LIST_LETTERS);
  6398. BIND_ENUM_CONSTANT(LIST_ROMAN);
  6399. BIND_ENUM_CONSTANT(LIST_DOTS);
  6400. BIND_ENUM_CONSTANT(MENU_COPY);
  6401. BIND_ENUM_CONSTANT(MENU_SELECT_ALL);
  6402. BIND_ENUM_CONSTANT(MENU_MAX);
  6403. BIND_ENUM_CONSTANT(META_UNDERLINE_NEVER);
  6404. BIND_ENUM_CONSTANT(META_UNDERLINE_ALWAYS);
  6405. BIND_ENUM_CONSTANT(META_UNDERLINE_ON_HOVER);
  6406. BIND_BITFIELD_FLAG(UPDATE_TEXTURE);
  6407. BIND_BITFIELD_FLAG(UPDATE_SIZE);
  6408. BIND_BITFIELD_FLAG(UPDATE_COLOR);
  6409. BIND_BITFIELD_FLAG(UPDATE_ALIGNMENT);
  6410. BIND_BITFIELD_FLAG(UPDATE_REGION);
  6411. BIND_BITFIELD_FLAG(UPDATE_PAD);
  6412. BIND_BITFIELD_FLAG(UPDATE_TOOLTIP);
  6413. BIND_BITFIELD_FLAG(UPDATE_WIDTH_IN_PERCENT);
  6414. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, RichTextLabel, normal_style, "normal");
  6415. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, RichTextLabel, focus_style, "focus");
  6416. BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_STYLEBOX, RichTextLabel, progress_bg_style, "background", "ProgressBar");
  6417. BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_STYLEBOX, RichTextLabel, progress_fg_style, "fill", "ProgressBar");
  6418. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, RichTextLabel, line_separation);
  6419. BIND_THEME_ITEM(Theme::DATA_TYPE_FONT, RichTextLabel, normal_font);
  6420. BIND_THEME_ITEM(Theme::DATA_TYPE_FONT_SIZE, RichTextLabel, normal_font_size);
  6421. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, RichTextLabel, default_color);
  6422. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, RichTextLabel, font_selected_color);
  6423. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, RichTextLabel, selection_color);
  6424. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, RichTextLabel, font_outline_color);
  6425. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, RichTextLabel, font_shadow_color);
  6426. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, RichTextLabel, shadow_outline_size);
  6427. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, RichTextLabel, shadow_offset_x);
  6428. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, RichTextLabel, shadow_offset_y);
  6429. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, RichTextLabel, outline_size);
  6430. BIND_THEME_ITEM(Theme::DATA_TYPE_FONT, RichTextLabel, bold_font);
  6431. BIND_THEME_ITEM(Theme::DATA_TYPE_FONT_SIZE, RichTextLabel, bold_font_size);
  6432. BIND_THEME_ITEM(Theme::DATA_TYPE_FONT, RichTextLabel, bold_italics_font);
  6433. BIND_THEME_ITEM(Theme::DATA_TYPE_FONT_SIZE, RichTextLabel, bold_italics_font_size);
  6434. BIND_THEME_ITEM(Theme::DATA_TYPE_FONT, RichTextLabel, italics_font);
  6435. BIND_THEME_ITEM(Theme::DATA_TYPE_FONT_SIZE, RichTextLabel, italics_font_size);
  6436. BIND_THEME_ITEM(Theme::DATA_TYPE_FONT, RichTextLabel, mono_font);
  6437. BIND_THEME_ITEM(Theme::DATA_TYPE_FONT_SIZE, RichTextLabel, mono_font_size);
  6438. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, RichTextLabel, text_highlight_h_padding);
  6439. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, RichTextLabel, text_highlight_v_padding);
  6440. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, RichTextLabel, table_h_separation);
  6441. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, RichTextLabel, table_v_separation);
  6442. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, RichTextLabel, table_odd_row_bg);
  6443. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, RichTextLabel, table_even_row_bg);
  6444. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, RichTextLabel, table_border);
  6445. ADD_CLASS_DEPENDENCY("PopupMenu");
  6446. }
  6447. TextServer::VisibleCharactersBehavior RichTextLabel::get_visible_characters_behavior() const {
  6448. return visible_chars_behavior;
  6449. }
  6450. void RichTextLabel::set_visible_characters_behavior(TextServer::VisibleCharactersBehavior p_behavior) {
  6451. if (visible_chars_behavior != p_behavior) {
  6452. _stop_thread();
  6453. visible_chars_behavior = p_behavior;
  6454. main->first_invalid_line.store(0); // Invalidate all lines.
  6455. _invalidate_accessibility();
  6456. _validate_line_caches();
  6457. queue_redraw();
  6458. }
  6459. }
  6460. void RichTextLabel::set_visible_characters(int p_visible) {
  6461. if (visible_characters != p_visible) {
  6462. _stop_thread();
  6463. visible_characters = p_visible;
  6464. if (p_visible == -1) {
  6465. visible_ratio = 1;
  6466. } else {
  6467. int total_char_count = get_total_character_count();
  6468. if (total_char_count > 0) {
  6469. visible_ratio = (float)p_visible / (float)total_char_count;
  6470. }
  6471. }
  6472. if (visible_chars_behavior == TextServer::VC_CHARS_BEFORE_SHAPING) {
  6473. main->first_invalid_line.store(0); // Invalidate all lines.
  6474. _invalidate_accessibility();
  6475. _validate_line_caches();
  6476. }
  6477. queue_redraw();
  6478. }
  6479. }
  6480. int RichTextLabel::get_visible_characters() const {
  6481. return visible_characters;
  6482. }
  6483. int RichTextLabel::get_character_line(int p_char) {
  6484. _validate_line_caches();
  6485. int line_count = 0;
  6486. int to_line = main->first_invalid_line.load();
  6487. for (int i = 0; i < to_line; i++) {
  6488. MutexLock lock(main->lines[i].text_buf->get_mutex());
  6489. int char_offset = main->lines[i].char_offset;
  6490. int char_count = main->lines[i].char_count;
  6491. if (char_offset <= p_char && p_char < char_offset + char_count) {
  6492. int lc = main->lines[i].text_buf->get_line_count();
  6493. for (int j = 0; j < lc; j++) {
  6494. Vector2i range = main->lines[i].text_buf->get_line_range(j);
  6495. if (char_offset + range.x <= p_char && p_char < char_offset + range.y) {
  6496. break;
  6497. }
  6498. if (char_offset + range.x > p_char && line_count > 0) {
  6499. line_count--; // Character is not rendered and is between the lines (e.g., edge space).
  6500. break;
  6501. }
  6502. if (j != lc - 1) {
  6503. line_count++;
  6504. }
  6505. }
  6506. return line_count;
  6507. } else {
  6508. line_count += main->lines[i].text_buf->get_line_count();
  6509. }
  6510. }
  6511. return -1;
  6512. }
  6513. int RichTextLabel::get_character_paragraph(int p_char) {
  6514. _validate_line_caches();
  6515. int to_line = main->first_invalid_line.load();
  6516. for (int i = 0; i < to_line; i++) {
  6517. int char_offset = main->lines[i].char_offset;
  6518. if (char_offset <= p_char && p_char < char_offset + main->lines[i].char_count) {
  6519. return i;
  6520. }
  6521. }
  6522. return -1;
  6523. }
  6524. int RichTextLabel::get_total_character_count() const {
  6525. // Note: Do not use line buffer "char_count", it includes only visible characters.
  6526. int tc = 0;
  6527. Item *it = main;
  6528. while (it) {
  6529. if (it->type == ITEM_TEXT) {
  6530. ItemText *t = static_cast<ItemText *>(it);
  6531. tc += t->text.length();
  6532. } else if (it->type == ITEM_NEWLINE) {
  6533. tc++;
  6534. } else if (it->type == ITEM_IMAGE) {
  6535. tc++;
  6536. }
  6537. it = _get_next_item(it, true);
  6538. }
  6539. return tc;
  6540. }
  6541. int RichTextLabel::get_total_glyph_count() const {
  6542. const_cast<RichTextLabel *>(this)->_validate_line_caches();
  6543. int tg = 0;
  6544. Item *it = main;
  6545. while (it) {
  6546. if (it->type == ITEM_FRAME) {
  6547. ItemFrame *f = static_cast<ItemFrame *>(it);
  6548. for (int i = 0; i < (int)f->lines.size(); i++) {
  6549. MutexLock lock(f->lines[i].text_buf->get_mutex());
  6550. tg += TS->shaped_text_get_glyph_count(f->lines[i].text_buf->get_rid());
  6551. }
  6552. }
  6553. it = _get_next_item(it, true);
  6554. }
  6555. return tg;
  6556. }
  6557. Size2 RichTextLabel::get_minimum_size() const {
  6558. Size2 sb_min_size = theme_cache.normal_style->get_minimum_size();
  6559. Size2 min_size;
  6560. if (fit_content) {
  6561. min_size.x = get_content_width();
  6562. min_size.y = get_content_height();
  6563. }
  6564. return sb_min_size +
  6565. ((autowrap_mode != TextServer::AUTOWRAP_OFF) ? Size2(1, min_size.height) : min_size);
  6566. }
  6567. // Context menu.
  6568. void RichTextLabel::_generate_context_menu() {
  6569. menu = memnew(PopupMenu);
  6570. add_child(menu, false, INTERNAL_MODE_FRONT);
  6571. menu->connect(SceneStringName(id_pressed), callable_mp(this, &RichTextLabel::menu_option));
  6572. menu->add_item(ETR("Copy"), MENU_COPY);
  6573. menu->add_item(ETR("Select All"), MENU_SELECT_ALL);
  6574. }
  6575. void RichTextLabel::_update_context_menu() {
  6576. if (!menu) {
  6577. _generate_context_menu();
  6578. }
  6579. int idx = -1;
  6580. #define MENU_ITEM_ACTION_DISABLED(m_menu, m_id, m_action, m_disabled) \
  6581. idx = m_menu->get_item_index(m_id); \
  6582. if (idx >= 0) { \
  6583. m_menu->set_item_accelerator(idx, shortcut_keys_enabled ? _get_menu_action_accelerator(m_action) : Key::NONE); \
  6584. m_menu->set_item_disabled(idx, m_disabled); \
  6585. }
  6586. MENU_ITEM_ACTION_DISABLED(menu, MENU_COPY, "ui_copy", !selection.enabled)
  6587. MENU_ITEM_ACTION_DISABLED(menu, MENU_SELECT_ALL, "ui_text_select_all", !selection.enabled)
  6588. #undef MENU_ITEM_ACTION_DISABLED
  6589. }
  6590. Key RichTextLabel::_get_menu_action_accelerator(const String &p_action) {
  6591. const List<Ref<InputEvent>> *events = InputMap::get_singleton()->action_get_events(p_action);
  6592. if (!events) {
  6593. return Key::NONE;
  6594. }
  6595. // Use first event in the list for the accelerator.
  6596. const List<Ref<InputEvent>>::Element *first_event = events->front();
  6597. if (!first_event) {
  6598. return Key::NONE;
  6599. }
  6600. const Ref<InputEventKey> event = first_event->get();
  6601. if (event.is_null()) {
  6602. return Key::NONE;
  6603. }
  6604. // Use physical keycode if non-zero
  6605. if (event->get_physical_keycode() != Key::NONE) {
  6606. return event->get_physical_keycode_with_modifiers();
  6607. } else {
  6608. return event->get_keycode_with_modifiers();
  6609. }
  6610. }
  6611. void RichTextLabel::menu_option(int p_option) {
  6612. switch (p_option) {
  6613. case MENU_COPY: {
  6614. String txt = get_selected_text();
  6615. if (txt.is_empty()) {
  6616. txt = get_parsed_text();
  6617. }
  6618. if (!txt.is_empty()) {
  6619. DisplayServer::get_singleton()->clipboard_set(txt);
  6620. }
  6621. } break;
  6622. case MENU_SELECT_ALL: {
  6623. select_all();
  6624. } break;
  6625. }
  6626. }
  6627. Ref<RichTextEffect> RichTextLabel::_get_custom_effect_by_code(String p_bbcode_identifier) {
  6628. for (int i = 0; i < custom_effects.size(); i++) {
  6629. Ref<RichTextEffect> effect = custom_effects[i];
  6630. if (effect.is_null()) {
  6631. continue;
  6632. }
  6633. if (effect->get_bbcode() == p_bbcode_identifier) {
  6634. return effect;
  6635. }
  6636. }
  6637. return Ref<RichTextEffect>();
  6638. }
  6639. Dictionary RichTextLabel::parse_expressions_for_values(Vector<String> p_expressions) {
  6640. Dictionary d;
  6641. for (int i = 0; i < p_expressions.size(); i++) {
  6642. Array a;
  6643. Vector<String> parts = p_expressions[i].split("=", true);
  6644. const String &key = parts[0];
  6645. if (parts.size() != 2) {
  6646. return d;
  6647. }
  6648. Vector<String> values = parts[1].split(",", false);
  6649. #ifdef MODULE_REGEX_ENABLED
  6650. RegEx color = RegEx();
  6651. color.compile("^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$");
  6652. RegEx nodepath = RegEx();
  6653. nodepath.compile("^\\$");
  6654. RegEx boolean = RegEx();
  6655. boolean.compile("^(true|false)$");
  6656. RegEx numerical = RegEx();
  6657. numerical.compile("^[-+]?\\d+$");
  6658. RegEx decimal = RegEx();
  6659. decimal.compile("^[+-]?\\d*(\\.\\d*)?([eE][+-]?\\d+)?$");
  6660. for (int j = 0; j < values.size(); j++) {
  6661. if (color.search(values[j]).is_valid()) {
  6662. a.append(Color::html(values[j]));
  6663. } else if (nodepath.search(values[j]).is_valid()) {
  6664. if (values[j].begins_with("$")) {
  6665. String v = values[j].substr(1);
  6666. a.append(NodePath(v));
  6667. }
  6668. } else if (boolean.search(values[j]).is_valid()) {
  6669. if (values[j] == "true") {
  6670. a.append(true);
  6671. } else if (values[j] == "false") {
  6672. a.append(false);
  6673. }
  6674. } else if (numerical.search(values[j]).is_valid()) {
  6675. a.append(values[j].to_int());
  6676. } else if (decimal.search(values[j]).is_valid()) {
  6677. a.append(values[j].to_float());
  6678. } else {
  6679. a.append(values[j]);
  6680. }
  6681. }
  6682. #endif
  6683. if (values.size() > 1) {
  6684. d[key] = a;
  6685. } else if (values.size() == 1) {
  6686. d[key] = a[0];
  6687. }
  6688. }
  6689. return d;
  6690. }
  6691. RichTextLabel::RichTextLabel(const String &p_text) {
  6692. main = memnew(ItemFrame);
  6693. main->owner = get_instance_id();
  6694. main->rid = items.make_rid(main);
  6695. main->index = 0;
  6696. current = main;
  6697. main->lines.resize(1);
  6698. main->lines[0].from = main;
  6699. main->first_invalid_line.store(0);
  6700. main->first_resized_line.store(0);
  6701. main->first_invalid_font_line.store(0);
  6702. current_frame = main;
  6703. vscroll = memnew(VScrollBar);
  6704. add_child(vscroll, false, INTERNAL_MODE_FRONT);
  6705. vscroll->set_drag_node(String(".."));
  6706. vscroll->set_step(1);
  6707. vscroll->set_anchor_and_offset(SIDE_TOP, ANCHOR_BEGIN, 0);
  6708. vscroll->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, 0);
  6709. vscroll->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_END, 0);
  6710. vscroll->connect(SceneStringName(value_changed), callable_mp(this, &RichTextLabel::_scroll_changed));
  6711. vscroll->set_step(1);
  6712. vscroll->hide();
  6713. set_focus_mode(FOCUS_ACCESSIBILITY);
  6714. set_text(p_text);
  6715. updating.store(false);
  6716. validating.store(false);
  6717. stop_thread.store(false);
  6718. parsing_bbcode.store(false);
  6719. set_clip_contents(true);
  6720. }
  6721. RichTextLabel::~RichTextLabel() {
  6722. _stop_thread();
  6723. items.free(main->rid);
  6724. memdelete(main);
  6725. }