rich_text_label.cpp 228 KB

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