rich_text_label.cpp 276 KB

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