rich_text_label.cpp 268 KB

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