rich_text_label.cpp 209 KB

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