rich_text_label.cpp 187 KB

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