tcpdf.php 886 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794
  1. <?php
  2. //============================================================+
  3. // File name : tcpdf.php
  4. // Version : 6.8.0
  5. // Begin : 2002-08-03
  6. // Last Update : 2024-12-23
  7. // Author : Nicola Asuni - Tecnick.com LTD - www.tecnick.com - info@tecnick.com
  8. // License : GNU-LGPL v3 (http://www.gnu.org/copyleft/lesser.html)
  9. // -------------------------------------------------------------------
  10. // Copyright (C) 2002-2024 Nicola Asuni - Tecnick.com LTD
  11. //
  12. // This file is part of TCPDF software library.
  13. //
  14. // TCPDF is free software: you can redistribute it and/or modify it
  15. // under the terms of the GNU Lesser General Public License as
  16. // published by the Free Software Foundation, either version 3 of the
  17. // License, or (at your option) any later version.
  18. //
  19. // TCPDF is distributed in the hope that it will be useful, but
  20. // WITHOUT ANY WARRANTY; without even the implied warranty of
  21. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  22. // See the GNU Lesser General Public License for more details.
  23. //
  24. // You should have received a copy of the License
  25. // along with TCPDF. If not, see
  26. // <http://www.tecnick.com/pagefiles/tcpdf/LICENSE.TXT>.
  27. //
  28. // See LICENSE.TXT file for more information.
  29. // -------------------------------------------------------------------
  30. //
  31. // Description :
  32. // This is a PHP class for generating PDF documents without requiring external extensions.
  33. //
  34. // NOTE:
  35. // This class was originally derived in 2002 from the Public
  36. // Domain FPDF class by Olivier Plathey (http://www.fpdf.org),
  37. // but now is almost entirely rewritten and contains thousands of
  38. // new lines of code and hundreds new features.
  39. //
  40. // Main features:
  41. // * no external libraries are required for the basic functions;
  42. // * all standard page formats, custom page formats, custom margins and units of measure;
  43. // * UTF-8 Unicode and Right-To-Left languages;
  44. // * TrueTypeUnicode, TrueType, Type1 and CID-0 fonts;
  45. // * font subsetting;
  46. // * methods to publish some XHTML + CSS code, Javascript and Forms;
  47. // * images, graphic (geometric figures) and transformation methods;
  48. // * supports JPEG, PNG and SVG images natively, all images supported by GD (GD, GD2, GD2PART, GIF, JPEG, PNG, BMP, XBM, XPM) and all images supported via ImageMagick (http://www.imagemagick.org/www/formats.html)
  49. // * 1D and 2D barcodes: CODE 39, ANSI MH10.8M-1983, USD-3, 3 of 9, CODE 93, USS-93, Standard 2 of 5, Interleaved 2 of 5, CODE 128 A/B/C, 2 and 5 Digits UPC-Based Extension, EAN 8, EAN 13, UPC-A, UPC-E, MSI, POSTNET, PLANET, RMS4CC (Royal Mail 4-state Customer Code), CBC (Customer Bar Code), KIX (Klant index - Customer index), Intelligent Mail Barcode, Onecode, USPS-B-3200, CODABAR, CODE 11, PHARMACODE, PHARMACODE TWO-TRACKS, Datamatrix, QR-Code, PDF417;
  50. // * JPEG and PNG ICC profiles, Grayscale, RGB, CMYK, Spot Colors and Transparencies;
  51. // * automatic page header and footer management;
  52. // * document encryption up to 256 bit and digital signature certifications;
  53. // * transactions to UNDO commands;
  54. // * PDF annotations, including links, text and file attachments;
  55. // * text rendering modes (fill, stroke and clipping);
  56. // * multiple columns mode;
  57. // * no-write page regions;
  58. // * bookmarks, named destinations and table of content;
  59. // * text hyphenation;
  60. // * text stretching and spacing (tracking);
  61. // * automatic page break, line break and text alignments including justification;
  62. // * automatic page numbering and page groups;
  63. // * move and delete pages;
  64. // * page compression (requires php-zlib extension);
  65. // * XOBject Templates;
  66. // * Layers and object visibility.
  67. // * PDF/A-1b support
  68. //============================================================+
  69. /**
  70. * @file
  71. * This is a PHP class for generating PDF documents without requiring external extensions.<br>
  72. * TCPDF project (http://www.tcpdf.org) was originally derived in 2002 from the Public Domain FPDF class by Olivier Plathey (http://www.fpdf.org), but now is almost entirely rewritten.<br>
  73. * <h3>TCPDF main features are:</h3>
  74. * <ul>
  75. * <li>no external libraries are required for the basic functions;</li>
  76. * <li>all standard page formats, custom page formats, custom margins and units of measure;</li>
  77. * <li>UTF-8 Unicode and Right-To-Left languages;</li>
  78. * <li>TrueTypeUnicode, TrueType, Type1 and CID-0 fonts;</li>
  79. * <li>font subsetting;</li>
  80. * <li>methods to publish some XHTML + CSS code, Javascript and Forms;</li>
  81. * <li>images, graphic (geometric figures) and transformation methods;
  82. * <li>supports JPEG, PNG and SVG images natively, all images supported by GD (GD, GD2, GD2PART, GIF, JPEG, PNG, BMP, XBM, XPM) and all images supported via ImageMagick (http://www.imagemagick.org/www/formats.html)</li>
  83. * <li>1D and 2D barcodes: CODE 39, ANSI MH10.8M-1983, USD-3, 3 of 9, CODE 93, USS-93, Standard 2 of 5, Interleaved 2 of 5, CODE 128 A/B/C, 2 and 5 Digits UPC-Based Extension, EAN 8, EAN 13, UPC-A, UPC-E, MSI, POSTNET, PLANET, RMS4CC (Royal Mail 4-state Customer Code), CBC (Customer Bar Code), KIX (Klant index - Customer index), Intelligent Mail Barcode, Onecode, USPS-B-3200, CODABAR, CODE 11, PHARMACODE, PHARMACODE TWO-TRACKS, Datamatrix, QR-Code, PDF417;</li>
  84. * <li>JPEG and PNG ICC profiles, Grayscale, RGB, CMYK, Spot Colors and Transparencies;</li>
  85. * <li>automatic page header and footer management;</li>
  86. * <li>document encryption up to 256 bit and digital signature certifications;</li>
  87. * <li>transactions to UNDO commands;</li>
  88. * <li>PDF annotations, including links, text and file attachments;</li>
  89. * <li>text rendering modes (fill, stroke and clipping);</li>
  90. * <li>multiple columns mode;</li>
  91. * <li>no-write page regions;</li>
  92. * <li>bookmarks, named destinations and table of content;</li>
  93. * <li>text hyphenation;</li>
  94. * <li>text stretching and spacing (tracking);</li>
  95. * <li>automatic page break, line break and text alignments including justification;</li>
  96. * <li>automatic page numbering and page groups;</li>
  97. * <li>move and delete pages;</li>
  98. * <li>page compression (requires php-zlib extension);</li>
  99. * <li>XOBject Templates;</li>
  100. * <li>Layers and object visibility;</li>
  101. * <li>PDF/A-1b support.</li>
  102. * </ul>
  103. * Tools to encode your unicode fonts are on fonts/utils directory.</p>
  104. * @package com.tecnick.tcpdf
  105. * @author Nicola Asuni
  106. * @version 6.8.0
  107. */
  108. // TCPDF configuration
  109. require_once(dirname(__FILE__).'/tcpdf_autoconfig.php');
  110. // TCPDF static font methods and data
  111. require_once(dirname(__FILE__).'/include/tcpdf_font_data.php');
  112. // TCPDF static font methods and data
  113. require_once(dirname(__FILE__).'/include/tcpdf_fonts.php');
  114. // TCPDF static color methods and data
  115. require_once(dirname(__FILE__).'/include/tcpdf_colors.php');
  116. // TCPDF static image methods and data
  117. require_once(dirname(__FILE__).'/include/tcpdf_images.php');
  118. // TCPDF static methods and data
  119. require_once(dirname(__FILE__).'/include/tcpdf_static.php');
  120. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  121. /**
  122. * @class TCPDF
  123. * PHP class for generating PDF documents without requiring external extensions.
  124. * TCPDF project (http://www.tcpdf.org) has been originally derived in 2002 from the Public Domain FPDF class by Olivier Plathey (http://www.fpdf.org), but now is almost entirely rewritten.<br>
  125. * @package com.tecnick.tcpdf
  126. * @brief PHP class for generating PDF documents without requiring external extensions.
  127. * @version 6.8.0
  128. * @author Nicola Asuni - info@tecnick.com
  129. * @IgnoreAnnotation("protected")
  130. * @IgnoreAnnotation("public")
  131. * @IgnoreAnnotation("pre")
  132. */
  133. class TCPDF {
  134. // Protected properties
  135. /**
  136. * Current page number.
  137. * @protected
  138. */
  139. protected $page;
  140. /**
  141. * Current object number.
  142. * @protected
  143. */
  144. protected $n;
  145. /**
  146. * Array of object offsets.
  147. * @protected
  148. */
  149. protected $offsets = array();
  150. /**
  151. * Array of object IDs for each page.
  152. * @protected
  153. */
  154. protected $pageobjects = array();
  155. /**
  156. * Buffer holding in-memory PDF.
  157. * @protected
  158. */
  159. protected $buffer;
  160. /**
  161. * Array containing pages.
  162. * @protected
  163. */
  164. protected $pages = array();
  165. /**
  166. * Current document state.
  167. * @protected
  168. */
  169. protected $state;
  170. /**
  171. * Compression flag.
  172. * @protected
  173. */
  174. protected $compress;
  175. /**
  176. * Current page orientation (P = Portrait, L = Landscape).
  177. * @protected
  178. */
  179. protected $CurOrientation;
  180. /**
  181. * Page dimensions.
  182. * @protected
  183. */
  184. protected $pagedim = array();
  185. /**
  186. * Scale factor (number of points in user unit).
  187. * @protected
  188. */
  189. protected $k;
  190. /**
  191. * Width of page format in points.
  192. * @protected
  193. */
  194. protected $fwPt;
  195. /**
  196. * Height of page format in points.
  197. * @protected
  198. */
  199. protected $fhPt;
  200. /**
  201. * Current width of page in points.
  202. * @protected
  203. */
  204. protected $wPt;
  205. /**
  206. * Current height of page in points.
  207. * @protected
  208. */
  209. protected $hPt;
  210. /**
  211. * Current width of page in user unit.
  212. * @protected
  213. */
  214. protected $w;
  215. /**
  216. * Current height of page in user unit.
  217. * @protected
  218. */
  219. protected $h;
  220. /**
  221. * Left margin.
  222. * @protected
  223. */
  224. protected $lMargin;
  225. /**
  226. * Right margin.
  227. * @protected
  228. */
  229. protected $rMargin;
  230. /**
  231. * Cell left margin (used by regions).
  232. * @protected
  233. */
  234. protected $clMargin;
  235. /**
  236. * Cell right margin (used by regions).
  237. * @protected
  238. */
  239. protected $crMargin;
  240. /**
  241. * Top margin.
  242. * @protected
  243. */
  244. protected $tMargin;
  245. /**
  246. * Page break margin.
  247. * @protected
  248. */
  249. protected $bMargin;
  250. /**
  251. * Array of cell internal paddings ('T' => top, 'R' => right, 'B' => bottom, 'L' => left).
  252. * @since 5.9.000 (2010-10-03)
  253. * @protected
  254. */
  255. protected $cell_padding = array('T' => 0, 'R' => 0, 'B' => 0, 'L' => 0);
  256. /**
  257. * Array of cell margins ('T' => top, 'R' => right, 'B' => bottom, 'L' => left).
  258. * @since 5.9.000 (2010-10-04)
  259. * @protected
  260. */
  261. protected $cell_margin = array('T' => 0, 'R' => 0, 'B' => 0, 'L' => 0);
  262. /**
  263. * Current horizontal position in user unit for cell positioning.
  264. * @protected
  265. */
  266. protected $x;
  267. /**
  268. * Current vertical position in user unit for cell positioning.
  269. * @protected
  270. */
  271. protected $y;
  272. /**
  273. * Height of last cell printed.
  274. * @protected
  275. */
  276. protected $lasth;
  277. /**
  278. * Line width in user unit.
  279. * @protected
  280. */
  281. protected $LineWidth;
  282. /**
  283. * Array of standard font names.
  284. * @protected
  285. */
  286. protected $CoreFonts;
  287. /**
  288. * Array of used fonts.
  289. * @protected
  290. */
  291. protected $fonts = array();
  292. /**
  293. * Array of font files.
  294. * @protected
  295. */
  296. protected $FontFiles = array();
  297. /**
  298. * Array of encoding differences.
  299. * @protected
  300. */
  301. protected $diffs = array();
  302. /**
  303. * Array of used images.
  304. * @protected
  305. */
  306. protected $images = array();
  307. /**
  308. * Depth of the svg tag, to keep track if the svg tag is a subtag or the root tag.
  309. * @protected
  310. */
  311. protected $svg_tag_depth = 0;
  312. /**
  313. * Array of Annotations in pages.
  314. * @protected
  315. */
  316. protected $PageAnnots = array();
  317. /**
  318. * Array of internal links.
  319. * @protected
  320. */
  321. protected $links = array();
  322. /**
  323. * Current font family.
  324. * @protected
  325. */
  326. protected $FontFamily;
  327. /**
  328. * Current font style.
  329. * @protected
  330. */
  331. protected $FontStyle;
  332. /**
  333. * Current font ascent (distance between font top and baseline).
  334. * @protected
  335. * @since 2.8.000 (2007-03-29)
  336. */
  337. protected $FontAscent;
  338. /**
  339. * Current font descent (distance between font bottom and baseline).
  340. * @protected
  341. * @since 2.8.000 (2007-03-29)
  342. */
  343. protected $FontDescent;
  344. /**
  345. * Underlining flag.
  346. * @protected
  347. */
  348. protected $underline;
  349. /**
  350. * Overlining flag.
  351. * @protected
  352. */
  353. protected $overline;
  354. /**
  355. * Current font info.
  356. * @protected
  357. */
  358. protected $CurrentFont;
  359. /**
  360. * Current font size in points.
  361. * @protected
  362. */
  363. protected $FontSizePt;
  364. /**
  365. * Current font size in user unit.
  366. * @protected
  367. */
  368. protected $FontSize;
  369. /**
  370. * Commands for drawing color.
  371. * @protected
  372. */
  373. protected $DrawColor;
  374. /**
  375. * Commands for filling color.
  376. * @protected
  377. */
  378. protected $FillColor;
  379. /**
  380. * Commands for text color.
  381. * @protected
  382. */
  383. protected $TextColor;
  384. /**
  385. * Indicates whether fill and text colors are different.
  386. * @protected
  387. */
  388. protected $ColorFlag;
  389. /**
  390. * Automatic page breaking.
  391. * @protected
  392. */
  393. protected $AutoPageBreak;
  394. /**
  395. * Threshold used to trigger page breaks.
  396. * @protected
  397. */
  398. protected $PageBreakTrigger;
  399. /**
  400. * Flag set when processing page header.
  401. * @protected
  402. */
  403. protected $InHeader = false;
  404. /**
  405. * Flag set when processing page footer.
  406. * @protected
  407. */
  408. protected $InFooter = false;
  409. /**
  410. * Zoom display mode.
  411. * @protected
  412. */
  413. protected $ZoomMode;
  414. /**
  415. * Layout display mode.
  416. * @protected
  417. */
  418. protected $LayoutMode;
  419. /**
  420. * If true set the document information dictionary in Unicode.
  421. * @protected
  422. */
  423. protected $docinfounicode = true;
  424. /**
  425. * Document title.
  426. * @protected
  427. */
  428. protected $title = '';
  429. /**
  430. * Document subject.
  431. * @protected
  432. */
  433. protected $subject = '';
  434. /**
  435. * Document author.
  436. * @protected
  437. */
  438. protected $author = '';
  439. /**
  440. * Document keywords.
  441. * @protected
  442. */
  443. protected $keywords = '';
  444. /**
  445. * Document creator.
  446. * @protected
  447. */
  448. protected $creator = '';
  449. /**
  450. * Starting page number.
  451. * @protected
  452. */
  453. protected $starting_page_number = 1;
  454. /**
  455. * The right-bottom (or left-bottom for RTL) corner X coordinate of last inserted image.
  456. * @since 2002-07-31
  457. * @author Nicola Asuni
  458. * @protected
  459. */
  460. protected $img_rb_x;
  461. /**
  462. * The right-bottom corner Y coordinate of last inserted image.
  463. * @since 2002-07-31
  464. * @author Nicola Asuni
  465. * @protected
  466. */
  467. protected $img_rb_y;
  468. /**
  469. * Adjusting factor to convert pixels to user units.
  470. * @since 2004-06-14
  471. * @author Nicola Asuni
  472. * @protected
  473. */
  474. protected $imgscale = 1;
  475. /**
  476. * Boolean flag set to true when the input text is unicode (require unicode fonts).
  477. * @since 2005-01-02
  478. * @author Nicola Asuni
  479. * @protected
  480. */
  481. protected $isunicode = false;
  482. /**
  483. * PDF version.
  484. * @since 1.5.3
  485. * @protected
  486. */
  487. protected $PDFVersion = '1.7';
  488. /**
  489. * ID of the stored default header template (-1 = not set).
  490. * @protected
  491. */
  492. protected $header_xobjid = false;
  493. /**
  494. * If true reset the Header Xobject template at each page
  495. * @protected
  496. */
  497. protected $header_xobj_autoreset = false;
  498. /**
  499. * Minimum distance between header and top page margin.
  500. * @protected
  501. * @var float
  502. */
  503. protected $header_margin;
  504. /**
  505. * Minimum distance between footer and bottom page margin.
  506. * @protected
  507. * @var float
  508. */
  509. protected $footer_margin;
  510. /**
  511. * Original left margin value.
  512. * @protected
  513. * @since 1.53.0.TC013
  514. */
  515. protected $original_lMargin;
  516. /**
  517. * Original right margin value.
  518. * @protected
  519. * @since 1.53.0.TC013
  520. */
  521. protected $original_rMargin;
  522. /**
  523. * Default font used on page header.
  524. * @protected
  525. * @var array<int,string|float|null>
  526. * @phpstan-var array{0: string, 1: string, 2: float|null}
  527. */
  528. protected $header_font;
  529. /**
  530. * Default font used on page footer.
  531. * @protected
  532. * @var array<int,string|float|null>
  533. * @phpstan-var array{0: string, 1: string, 2: float|null}
  534. */
  535. protected $footer_font;
  536. /**
  537. * Language templates.
  538. * @protected
  539. */
  540. protected $l;
  541. /**
  542. * Barcode to print on page footer (only if set).
  543. * @protected
  544. */
  545. protected $barcode = false;
  546. /**
  547. * Boolean flag to print/hide page header.
  548. * @protected
  549. */
  550. protected $print_header = true;
  551. /**
  552. * Boolean flag to print/hide page footer.
  553. * @protected
  554. */
  555. protected $print_footer = true;
  556. /**
  557. * Header image logo.
  558. * @protected
  559. */
  560. protected $header_logo = '';
  561. /**
  562. * Width of header image logo in user units.
  563. * @protected
  564. */
  565. protected $header_logo_width = 30;
  566. /**
  567. * Title to be printed on default page header.
  568. * @protected
  569. */
  570. protected $header_title = '';
  571. /**
  572. * String to print on page header after title.
  573. * @protected
  574. */
  575. protected $header_string = '';
  576. /**
  577. * Color for header text (RGB array).
  578. * @since 5.9.174 (2012-07-25)
  579. * @protected
  580. * @var int[]
  581. * @phpstan-var array{0: int, 1: int, 2: int}
  582. */
  583. protected $header_text_color = array(0,0,0);
  584. /**
  585. * Color for header line (RGB array).
  586. * @since 5.9.174 (2012-07-25)
  587. * @protected
  588. * @var int[]
  589. * @phpstan-var array{0: int, 1: int, 2: int}
  590. */
  591. protected $header_line_color = array(0,0,0);
  592. /**
  593. * Color for footer text (RGB array).
  594. * @since 5.9.174 (2012-07-25)
  595. * @protected
  596. * @var int[]
  597. * @phpstan-var array{0: int, 1: int, 2: int}
  598. */
  599. protected $footer_text_color = array(0,0,0);
  600. /**
  601. * Color for footer line (RGB array).
  602. * @since 5.9.174 (2012-07-25)
  603. * @protected
  604. * @var int[]
  605. * @phpstan-var array{0: int, 1: int, 2: int}
  606. */
  607. protected $footer_line_color = array(0,0,0);
  608. /**
  609. * Text shadow data array.
  610. * @since 5.9.174 (2012-07-25)
  611. * @protected
  612. */
  613. protected $txtshadow = array('enabled'=>false, 'depth_w'=>0, 'depth_h'=>0, 'color'=>false, 'opacity'=>1, 'blend_mode'=>'Normal');
  614. /**
  615. * Default number of columns for html table.
  616. * @protected
  617. */
  618. protected $default_table_columns = 4;
  619. // variables for html parser
  620. /**
  621. * HTML PARSER: array to store current link and rendering styles.
  622. * @protected
  623. */
  624. protected $HREF = array();
  625. /**
  626. * List of available fonts on filesystem.
  627. * @protected
  628. */
  629. protected $fontlist = array();
  630. /**
  631. * Current foreground color.
  632. * @protected
  633. */
  634. protected $fgcolor;
  635. /**
  636. * HTML PARSER: array of boolean values, true in case of ordered list (OL), false otherwise.
  637. * @protected
  638. */
  639. protected $listordered = array();
  640. /**
  641. * HTML PARSER: array count list items on nested lists.
  642. * @protected
  643. */
  644. protected $listcount = array();
  645. /**
  646. * HTML PARSER: current list nesting level.
  647. * @protected
  648. */
  649. protected $listnum = 0;
  650. /**
  651. * HTML PARSER: indent amount for lists.
  652. * @protected
  653. */
  654. protected $listindent = 0;
  655. /**
  656. * HTML PARSER: current list indententation level.
  657. * @protected
  658. */
  659. protected $listindentlevel = 0;
  660. /**
  661. * Current background color.
  662. * @protected
  663. */
  664. protected $bgcolor;
  665. /**
  666. * Temporary font size in points.
  667. * @protected
  668. */
  669. protected $tempfontsize = 10;
  670. /**
  671. * Spacer string for LI tags.
  672. * @protected
  673. */
  674. protected $lispacer = '';
  675. /**
  676. * Default encoding.
  677. * @protected
  678. * @since 1.53.0.TC010
  679. */
  680. protected $encoding = 'UTF-8';
  681. /**
  682. * Boolean flag to indicate if the document language is Right-To-Left.
  683. * @protected
  684. * @since 2.0.000
  685. */
  686. protected $rtl = false;
  687. /**
  688. * Boolean flag used to force RTL or LTR string direction.
  689. * @protected
  690. * @since 2.0.000
  691. */
  692. protected $tmprtl = false;
  693. // --- Variables used for document encryption:
  694. /**
  695. * IBoolean flag indicating whether document is protected.
  696. * @protected
  697. * @since 2.0.000 (2008-01-02)
  698. */
  699. protected $encrypted;
  700. /**
  701. * Array containing encryption settings.
  702. * @protected
  703. * @since 5.0.005 (2010-05-11)
  704. */
  705. protected $encryptdata = array();
  706. /**
  707. * Last RC4 key encrypted (cached for optimisation).
  708. * @protected
  709. * @since 2.0.000 (2008-01-02)
  710. */
  711. protected $last_enc_key;
  712. /**
  713. * Last RC4 computed key.
  714. * @protected
  715. * @since 2.0.000 (2008-01-02)
  716. */
  717. protected $last_enc_key_c;
  718. /**
  719. * File ID (used on document trailer).
  720. * @protected
  721. * @since 5.0.005 (2010-05-12)
  722. */
  723. protected $file_id;
  724. /**
  725. * Internal secret used to encrypt data.
  726. * @protected
  727. * @since 6.7.5 (2024-03-21)
  728. */
  729. protected $hash_key;
  730. // --- bookmark ---
  731. /**
  732. * Outlines for bookmark.
  733. * @protected
  734. * @since 2.1.002 (2008-02-12)
  735. */
  736. protected $outlines = array();
  737. /**
  738. * Outline root for bookmark.
  739. * @protected
  740. * @since 2.1.002 (2008-02-12)
  741. */
  742. protected $OutlineRoot;
  743. // --- javascript and form ---
  744. /**
  745. * Javascript code.
  746. * @protected
  747. * @since 2.1.002 (2008-02-12)
  748. */
  749. protected $javascript = '';
  750. /**
  751. * Javascript counter.
  752. * @protected
  753. * @since 2.1.002 (2008-02-12)
  754. */
  755. protected $n_js;
  756. /**
  757. * line through state
  758. * @protected
  759. * @since 2.8.000 (2008-03-19)
  760. */
  761. protected $linethrough;
  762. /**
  763. * Array with additional document-wide usage rights for the document.
  764. * @protected
  765. * @since 5.8.014 (2010-08-23)
  766. */
  767. protected $ur = array();
  768. /**
  769. * DPI (Dot Per Inch) Document Resolution (do not change).
  770. * @protected
  771. * @since 3.0.000 (2008-03-27)
  772. */
  773. protected $dpi = 72;
  774. /**
  775. * Array of page numbers were a new page group was started (the page numbers are the keys of the array).
  776. * @protected
  777. * @since 3.0.000 (2008-03-27)
  778. */
  779. protected $newpagegroup = array();
  780. /**
  781. * Array that contains the number of pages in each page group.
  782. * @protected
  783. * @since 3.0.000 (2008-03-27)
  784. */
  785. protected $pagegroups = array();
  786. /**
  787. * Current page group number.
  788. * @protected
  789. * @since 3.0.000 (2008-03-27)
  790. */
  791. protected $currpagegroup = 0;
  792. /**
  793. * Array of transparency objects and parameters.
  794. * @protected
  795. * @since 3.0.000 (2008-03-27)
  796. */
  797. protected $extgstates;
  798. /**
  799. * Set the default JPEG compression quality (1-100).
  800. * @protected
  801. * @since 3.0.000 (2008-03-27)
  802. */
  803. protected $jpeg_quality;
  804. /**
  805. * Default cell height ratio.
  806. * @protected
  807. * @since 3.0.014 (2008-05-23)
  808. * @var float
  809. */
  810. protected $cell_height_ratio = K_CELL_HEIGHT_RATIO;
  811. /**
  812. * PDF viewer preferences.
  813. * @protected
  814. * @since 3.1.000 (2008-06-09)
  815. */
  816. protected $viewer_preferences;
  817. /**
  818. * A name object specifying how the document should be displayed when opened.
  819. * @protected
  820. * @since 3.1.000 (2008-06-09)
  821. */
  822. protected $PageMode;
  823. /**
  824. * Array for storing gradient information.
  825. * @protected
  826. * @since 3.1.000 (2008-06-09)
  827. */
  828. protected $gradients = array();
  829. /**
  830. * Array used to store positions inside the pages buffer (keys are the page numbers).
  831. * @protected
  832. * @since 3.2.000 (2008-06-26)
  833. */
  834. protected $intmrk = array();
  835. /**
  836. * Array used to store positions inside the pages buffer (keys are the page numbers).
  837. * @protected
  838. * @since 5.7.000 (2010-08-03)
  839. */
  840. protected $bordermrk = array();
  841. /**
  842. * Array used to store page positions to track empty pages (keys are the page numbers).
  843. * @protected
  844. * @since 5.8.007 (2010-08-18)
  845. */
  846. protected $emptypagemrk = array();
  847. /**
  848. * Array used to store content positions inside the pages buffer (keys are the page numbers).
  849. * @protected
  850. * @since 4.6.021 (2009-07-20)
  851. */
  852. protected $cntmrk = array();
  853. /**
  854. * Array used to store footer positions of each page.
  855. * @protected
  856. * @since 3.2.000 (2008-07-01)
  857. */
  858. protected $footerpos = array();
  859. /**
  860. * Array used to store footer length of each page.
  861. * @protected
  862. * @since 4.0.014 (2008-07-29)
  863. */
  864. protected $footerlen = array();
  865. /**
  866. * Boolean flag to indicate if a new line is created.
  867. * @protected
  868. * @since 3.2.000 (2008-07-01)
  869. */
  870. protected $newline = true;
  871. /**
  872. * End position of the latest inserted line.
  873. * @protected
  874. * @since 3.2.000 (2008-07-01)
  875. */
  876. protected $endlinex = 0;
  877. /**
  878. * PDF string for width value of the last line.
  879. * @protected
  880. * @since 4.0.006 (2008-07-16)
  881. */
  882. protected $linestyleWidth = '';
  883. /**
  884. * PDF string for CAP value of the last line.
  885. * @protected
  886. * @since 4.0.006 (2008-07-16)
  887. */
  888. protected $linestyleCap = '0 J';
  889. /**
  890. * PDF string for join value of the last line.
  891. * @protected
  892. * @since 4.0.006 (2008-07-16)
  893. */
  894. protected $linestyleJoin = '0 j';
  895. /**
  896. * PDF string for dash value of the last line.
  897. * @protected
  898. * @since 4.0.006 (2008-07-16)
  899. */
  900. protected $linestyleDash = '[] 0 d';
  901. /**
  902. * Boolean flag to indicate if marked-content sequence is open.
  903. * @protected
  904. * @since 4.0.013 (2008-07-28)
  905. */
  906. protected $openMarkedContent = false;
  907. /**
  908. * Count the latest inserted vertical spaces on HTML.
  909. * @protected
  910. * @since 4.0.021 (2008-08-24)
  911. */
  912. protected $htmlvspace = 0;
  913. /**
  914. * Array of Spot colors.
  915. * @protected
  916. * @since 4.0.024 (2008-09-12)
  917. */
  918. protected $spot_colors = array();
  919. /**
  920. * Symbol used for HTML unordered list items.
  921. * @protected
  922. * @since 4.0.028 (2008-09-26)
  923. */
  924. protected $lisymbol = '';
  925. /**
  926. * String used to mark the beginning and end of EPS image blocks.
  927. * @protected
  928. * @since 4.1.000 (2008-10-18)
  929. */
  930. protected $epsmarker = 'x#!#EPS#!#x';
  931. /**
  932. * Array of transformation matrix.
  933. * @protected
  934. * @since 4.2.000 (2008-10-29)
  935. */
  936. protected $transfmatrix = array();
  937. /**
  938. * Current key for transformation matrix.
  939. * @protected
  940. * @since 4.8.005 (2009-09-17)
  941. */
  942. protected $transfmatrix_key = 0;
  943. /**
  944. * Booklet mode for double-sided pages.
  945. * @protected
  946. * @since 4.2.000 (2008-10-29)
  947. */
  948. protected $booklet = false;
  949. /**
  950. * Epsilon value used for float calculations.
  951. * @protected
  952. * @since 4.2.000 (2008-10-29)
  953. */
  954. protected $feps = 0.005;
  955. /**
  956. * Array used for custom vertical spaces for HTML tags.
  957. * @protected
  958. * @since 4.2.001 (2008-10-30)
  959. */
  960. protected $tagvspaces = array();
  961. /**
  962. * HTML PARSER: custom indent amount for lists. Negative value means disabled.
  963. * @protected
  964. * @since 4.2.007 (2008-11-12)
  965. */
  966. protected $customlistindent = -1;
  967. /**
  968. * Boolean flag to indicate if the border of the cell sides that cross the page should be removed.
  969. * @protected
  970. * @since 4.2.010 (2008-11-14)
  971. */
  972. protected $opencell = true;
  973. /**
  974. * Array of files to embedd.
  975. * @protected
  976. * @since 4.4.000 (2008-12-07)
  977. */
  978. protected $embeddedfiles = array();
  979. /**
  980. * Boolean flag to indicate if we are inside a PRE tag.
  981. * @protected
  982. * @since 4.4.001 (2008-12-08)
  983. */
  984. protected $premode = false;
  985. /**
  986. * Array used to store positions of graphics transformation blocks inside the page buffer.
  987. * keys are the page numbers
  988. * @protected
  989. * @since 4.4.002 (2008-12-09)
  990. */
  991. protected $transfmrk = array();
  992. /**
  993. * Default color for html links.
  994. * @protected
  995. * @since 4.4.003 (2008-12-09)
  996. */
  997. protected $htmlLinkColorArray = array(0, 0, 255);
  998. /**
  999. * Default font style to add to html links.
  1000. * @protected
  1001. * @since 4.4.003 (2008-12-09)
  1002. */
  1003. protected $htmlLinkFontStyle = 'U';
  1004. /**
  1005. * Counts the number of pages.
  1006. * @protected
  1007. * @since 4.5.000 (2008-12-31)
  1008. */
  1009. protected $numpages = 0;
  1010. /**
  1011. * Array containing page lengths in bytes.
  1012. * @protected
  1013. * @since 4.5.000 (2008-12-31)
  1014. */
  1015. protected $pagelen = array();
  1016. /**
  1017. * Counts the number of pages.
  1018. * @protected
  1019. * @since 4.5.000 (2008-12-31)
  1020. */
  1021. protected $numimages = 0;
  1022. /**
  1023. * Store the image keys.
  1024. * @protected
  1025. * @since 4.5.000 (2008-12-31)
  1026. */
  1027. protected $imagekeys = array();
  1028. /**
  1029. * Length of the buffer in bytes.
  1030. * @protected
  1031. * @since 4.5.000 (2008-12-31)
  1032. */
  1033. protected $bufferlen = 0;
  1034. /**
  1035. * Counts the number of fonts.
  1036. * @protected
  1037. * @since 4.5.000 (2009-01-02)
  1038. */
  1039. protected $numfonts = 0;
  1040. /**
  1041. * Store the font keys.
  1042. * @protected
  1043. * @since 4.5.000 (2009-01-02)
  1044. */
  1045. protected $fontkeys = array();
  1046. /**
  1047. * Store the font object IDs.
  1048. * @protected
  1049. * @since 4.8.001 (2009-09-09)
  1050. */
  1051. protected $font_obj_ids = array();
  1052. /**
  1053. * Store the fage status (true when opened, false when closed).
  1054. * @protected
  1055. * @since 4.5.000 (2009-01-02)
  1056. */
  1057. protected $pageopen = array();
  1058. /**
  1059. * Default monospace font.
  1060. * @protected
  1061. * @since 4.5.025 (2009-03-10)
  1062. */
  1063. protected $default_monospaced_font = 'courier';
  1064. /**
  1065. * Cloned copy of the current class object.
  1066. * @protected
  1067. * @since 4.5.029 (2009-03-19)
  1068. */
  1069. protected $objcopy;
  1070. /**
  1071. * Array used to store the lengths of cache files.
  1072. * @protected
  1073. * @since 4.5.029 (2009-03-19)
  1074. */
  1075. protected $cache_file_length = array();
  1076. /**
  1077. * Table header content to be repeated on each new page.
  1078. * @protected
  1079. * @since 4.5.030 (2009-03-20)
  1080. */
  1081. protected $thead = '';
  1082. /**
  1083. * Margins used for table header.
  1084. * @protected
  1085. * @since 4.5.030 (2009-03-20)
  1086. */
  1087. protected $theadMargins = array();
  1088. /**
  1089. * Boolean flag to enable document digital signature.
  1090. * @protected
  1091. * @since 4.6.005 (2009-04-24)
  1092. */
  1093. protected $sign = false;
  1094. /**
  1095. * Digital signature data.
  1096. * @protected
  1097. * @since 4.6.005 (2009-04-24)
  1098. */
  1099. protected $signature_data = array();
  1100. /**
  1101. * Digital signature max length.
  1102. * @protected
  1103. * @since 4.6.005 (2009-04-24)
  1104. */
  1105. protected $signature_max_length = 11742;
  1106. /**
  1107. * Data for digital signature appearance.
  1108. * @protected
  1109. * @since 5.3.011 (2010-06-16)
  1110. */
  1111. protected $signature_appearance = array('page' => 1, 'rect' => '0 0 0 0');
  1112. /**
  1113. * Array of empty digital signature appearances.
  1114. * @protected
  1115. * @since 5.9.101 (2011-07-06)
  1116. */
  1117. protected $empty_signature_appearance = array();
  1118. /**
  1119. * Boolean flag to enable document timestamping with TSA.
  1120. * @protected
  1121. * @since 6.0.085 (2014-06-19)
  1122. */
  1123. protected $tsa_timestamp = false;
  1124. /**
  1125. * Timestamping data.
  1126. * @protected
  1127. * @since 6.0.085 (2014-06-19)
  1128. */
  1129. protected $tsa_data = array();
  1130. /**
  1131. * Regular expression used to find blank characters (required for word-wrapping).
  1132. * @protected
  1133. * @since 4.6.006 (2009-04-28)
  1134. */
  1135. protected $re_spaces = '/[^\S\xa0]/';
  1136. /**
  1137. * Array of $re_spaces parts.
  1138. * @protected
  1139. * @since 5.5.011 (2010-07-09)
  1140. */
  1141. protected $re_space = array('p' => '[^\S\xa0]', 'm' => '');
  1142. /**
  1143. * Digital signature object ID.
  1144. * @protected
  1145. * @since 4.6.022 (2009-06-23)
  1146. */
  1147. protected $sig_obj_id = 0;
  1148. /**
  1149. * ID of page objects.
  1150. * @protected
  1151. * @since 4.7.000 (2009-08-29)
  1152. */
  1153. protected $page_obj_id = array();
  1154. /**
  1155. * List of form annotations IDs.
  1156. * @protected
  1157. * @since 4.8.000 (2009-09-07)
  1158. */
  1159. protected $form_obj_id = array();
  1160. /**
  1161. * Deafult Javascript field properties. Possible values are described on official Javascript for Acrobat API reference. Annotation options can be directly specified using the 'aopt' entry.
  1162. * @protected
  1163. * @since 4.8.000 (2009-09-07)
  1164. */
  1165. protected $default_form_prop = array('lineWidth'=>1, 'borderStyle'=>'solid', 'fillColor'=>array(255, 255, 255), 'strokeColor'=>array(128, 128, 128));
  1166. /**
  1167. * Javascript objects array.
  1168. * @protected
  1169. * @since 4.8.000 (2009-09-07)
  1170. */
  1171. protected $js_objects = array();
  1172. /**
  1173. * Current form action (used during XHTML rendering).
  1174. * @protected
  1175. * @since 4.8.000 (2009-09-07)
  1176. */
  1177. protected $form_action = '';
  1178. /**
  1179. * Current form encryption type (used during XHTML rendering).
  1180. * @protected
  1181. * @since 4.8.000 (2009-09-07)
  1182. */
  1183. protected $form_enctype = 'application/x-www-form-urlencoded';
  1184. /**
  1185. * Current method to submit forms.
  1186. * @protected
  1187. * @since 4.8.000 (2009-09-07)
  1188. */
  1189. protected $form_mode = 'post';
  1190. /**
  1191. * List of fonts used on form fields (fontname => fontkey).
  1192. * @protected
  1193. * @since 4.8.001 (2009-09-09)
  1194. */
  1195. protected $annotation_fonts = array();
  1196. /**
  1197. * List of radio buttons parent objects.
  1198. * @protected
  1199. * @since 4.8.001 (2009-09-09)
  1200. */
  1201. protected $radiobutton_groups = array();
  1202. /**
  1203. * List of radio group objects IDs.
  1204. * @protected
  1205. * @since 4.8.001 (2009-09-09)
  1206. */
  1207. protected $radio_groups = array();
  1208. /**
  1209. * Text indentation value (used for text-indent CSS attribute).
  1210. * @protected
  1211. * @since 4.8.006 (2009-09-23)
  1212. */
  1213. protected $textindent = 0;
  1214. /**
  1215. * Store page number when startTransaction() is called.
  1216. * @protected
  1217. * @since 4.8.006 (2009-09-23)
  1218. */
  1219. protected $start_transaction_page = 0;
  1220. /**
  1221. * Store Y position when startTransaction() is called.
  1222. * @protected
  1223. * @since 4.9.001 (2010-03-28)
  1224. */
  1225. protected $start_transaction_y = 0;
  1226. /**
  1227. * True when we are printing the thead section on a new page.
  1228. * @protected
  1229. * @since 4.8.027 (2010-01-25)
  1230. */
  1231. protected $inthead = false;
  1232. /**
  1233. * Array of column measures (width, space, starting Y position).
  1234. * @protected
  1235. * @since 4.9.001 (2010-03-28)
  1236. */
  1237. protected $columns = array();
  1238. /**
  1239. * Number of colums.
  1240. * @protected
  1241. * @since 4.9.001 (2010-03-28)
  1242. */
  1243. protected $num_columns = 1;
  1244. /**
  1245. * Current column number.
  1246. * @protected
  1247. * @since 4.9.001 (2010-03-28)
  1248. */
  1249. protected $current_column = 0;
  1250. /**
  1251. * Starting page for columns.
  1252. * @protected
  1253. * @since 4.9.001 (2010-03-28)
  1254. */
  1255. protected $column_start_page = 0;
  1256. /**
  1257. * Maximum page and column selected.
  1258. * @protected
  1259. * @since 5.8.000 (2010-08-11)
  1260. */
  1261. protected $maxselcol = array('page' => 0, 'column' => 0);
  1262. /**
  1263. * Array of: X difference between table cell x start and starting page margin, cellspacing, cellpadding.
  1264. * @protected
  1265. * @since 5.8.000 (2010-08-11)
  1266. */
  1267. protected $colxshift = array('x' => 0, 's' => array('H' => 0, 'V' => 0), 'p' => array('L' => 0, 'T' => 0, 'R' => 0, 'B' => 0));
  1268. /**
  1269. * Text rendering mode: 0 = Fill text; 1 = Stroke text; 2 = Fill, then stroke text; 3 = Neither fill nor stroke text (invisible); 4 = Fill text and add to path for clipping; 5 = Stroke text and add to path for clipping; 6 = Fill, then stroke text and add to path for clipping; 7 = Add text to path for clipping.
  1270. * @protected
  1271. * @since 4.9.008 (2010-04-03)
  1272. */
  1273. protected $textrendermode = 0;
  1274. /**
  1275. * Text stroke width in doc units.
  1276. * @protected
  1277. * @since 4.9.008 (2010-04-03)
  1278. */
  1279. protected $textstrokewidth = 0;
  1280. /**
  1281. * Current stroke color.
  1282. * @protected
  1283. * @since 4.9.008 (2010-04-03)
  1284. */
  1285. protected $strokecolor;
  1286. /**
  1287. * Default unit of measure for document.
  1288. * @protected
  1289. * @since 5.0.000 (2010-04-22)
  1290. */
  1291. protected $pdfunit = 'mm';
  1292. /**
  1293. * Boolean flag true when we are on TOC (Table Of Content) page.
  1294. * @protected
  1295. */
  1296. protected $tocpage = false;
  1297. /**
  1298. * Boolean flag: if true convert vector images (SVG, EPS) to raster image using GD or ImageMagick library.
  1299. * @protected
  1300. * @since 5.0.000 (2010-04-26)
  1301. */
  1302. protected $rasterize_vector_images = false;
  1303. /**
  1304. * Boolean flag: if true enables font subsetting by default.
  1305. * @protected
  1306. * @since 5.3.002 (2010-06-07)
  1307. */
  1308. protected $font_subsetting = true;
  1309. /**
  1310. * Array of default graphic settings.
  1311. * @protected
  1312. * @since 5.5.008 (2010-07-02)
  1313. */
  1314. protected $default_graphic_vars = array();
  1315. /**
  1316. * Array of XObjects.
  1317. * @protected
  1318. * @since 5.8.014 (2010-08-23)
  1319. */
  1320. protected $xobjects = array();
  1321. /**
  1322. * Boolean value true when we are inside an XObject.
  1323. * @protected
  1324. * @since 5.8.017 (2010-08-24)
  1325. */
  1326. protected $inxobj = false;
  1327. /**
  1328. * Current XObject ID.
  1329. * @protected
  1330. * @since 5.8.017 (2010-08-24)
  1331. */
  1332. protected $xobjid = '';
  1333. /**
  1334. * Percentage of character stretching.
  1335. * @protected
  1336. * @since 5.9.000 (2010-09-29)
  1337. */
  1338. protected $font_stretching = 100;
  1339. /**
  1340. * Increases or decreases the space between characters in a text by the specified amount (tracking).
  1341. * @protected
  1342. * @since 5.9.000 (2010-09-29)
  1343. */
  1344. protected $font_spacing = 0;
  1345. /**
  1346. * Array of no-write regions.
  1347. * ('page' => page number or empy for current page, 'xt' => X top, 'yt' => Y top, 'xb' => X bottom, 'yb' => Y bottom, 'side' => page side 'L' = left or 'R' = right)
  1348. * @protected
  1349. * @since 5.9.003 (2010-10-14)
  1350. */
  1351. protected $page_regions = array();
  1352. /**
  1353. * Boolean value true when page region check is active.
  1354. * @protected
  1355. */
  1356. protected $check_page_regions = true;
  1357. /**
  1358. * Array of PDF layers data.
  1359. * @protected
  1360. * @since 5.9.102 (2011-07-13)
  1361. */
  1362. protected $pdflayers = array();
  1363. /**
  1364. * A dictionary of names and corresponding destinations (Dests key on document Catalog).
  1365. * @protected
  1366. * @since 5.9.097 (2011-06-23)
  1367. */
  1368. protected $dests = array();
  1369. /**
  1370. * Object ID for Named Destinations
  1371. * @protected
  1372. * @since 5.9.097 (2011-06-23)
  1373. */
  1374. protected $n_dests;
  1375. /**
  1376. * Embedded Files Names
  1377. * @protected
  1378. * @since 5.9.204 (2013-01-23)
  1379. */
  1380. protected $efnames = array();
  1381. /**
  1382. * Directory used for the last SVG image.
  1383. * @protected
  1384. * @since 5.0.000 (2010-05-05)
  1385. */
  1386. protected $svgdir = '';
  1387. /**
  1388. * Deafult unit of measure for SVG.
  1389. * @protected
  1390. * @since 5.0.000 (2010-05-02)
  1391. */
  1392. protected $svgunit = 'px';
  1393. /**
  1394. * Array of SVG gradients.
  1395. * @protected
  1396. * @since 5.0.000 (2010-05-02)
  1397. */
  1398. protected $svggradients = array();
  1399. /**
  1400. * ID of last SVG gradient.
  1401. * @protected
  1402. * @since 5.0.000 (2010-05-02)
  1403. */
  1404. protected $svggradientid = 0;
  1405. /**
  1406. * Boolean value true when in SVG defs group.
  1407. * @protected
  1408. * @since 5.0.000 (2010-05-02)
  1409. */
  1410. protected $svgdefsmode = false;
  1411. /**
  1412. * Array of SVG defs.
  1413. * @protected
  1414. * @since 5.0.000 (2010-05-02)
  1415. */
  1416. protected $svgdefs = array();
  1417. /**
  1418. * Boolean value true when in SVG clipPath tag.
  1419. * @protected
  1420. * @since 5.0.000 (2010-04-26)
  1421. */
  1422. protected $svgclipmode = false;
  1423. /**
  1424. * Array of SVG clipPath commands.
  1425. * @protected
  1426. * @since 5.0.000 (2010-05-02)
  1427. */
  1428. protected $svgclippaths = array();
  1429. /**
  1430. * Array of SVG clipPath tranformation matrix.
  1431. * @protected
  1432. * @since 5.8.022 (2010-08-31)
  1433. */
  1434. protected $svgcliptm = array();
  1435. /**
  1436. * ID of last SVG clipPath.
  1437. * @protected
  1438. * @since 5.0.000 (2010-05-02)
  1439. */
  1440. protected $svgclipid = 0;
  1441. /**
  1442. * SVG text.
  1443. * @protected
  1444. * @since 5.0.000 (2010-05-02)
  1445. */
  1446. protected $svgtext = '';
  1447. /**
  1448. * SVG text properties.
  1449. * @protected
  1450. * @since 5.8.013 (2010-08-23)
  1451. */
  1452. protected $svgtextmode = array();
  1453. /**
  1454. * Array of SVG properties.
  1455. * @protected
  1456. * @since 5.0.000 (2010-05-02)
  1457. */
  1458. protected $svgstyles = array(array(
  1459. 'alignment-baseline' => 'auto',
  1460. 'baseline-shift' => 'baseline',
  1461. 'clip' => 'auto',
  1462. 'clip-path' => 'none',
  1463. 'clip-rule' => 'nonzero',
  1464. 'color' => 'black',
  1465. 'color-interpolation' => 'sRGB',
  1466. 'color-interpolation-filters' => 'linearRGB',
  1467. 'color-profile' => 'auto',
  1468. 'color-rendering' => 'auto',
  1469. 'cursor' => 'auto',
  1470. 'direction' => 'ltr',
  1471. 'display' => 'inline',
  1472. 'dominant-baseline' => 'auto',
  1473. 'enable-background' => 'accumulate',
  1474. 'fill' => 'black',
  1475. 'fill-opacity' => 1,
  1476. 'fill-rule' => 'nonzero',
  1477. 'filter' => 'none',
  1478. 'flood-color' => 'black',
  1479. 'flood-opacity' => 1,
  1480. 'font' => '',
  1481. 'font-family' => 'helvetica',
  1482. 'font-size' => 'medium',
  1483. 'font-size-adjust' => 'none',
  1484. 'font-stretch' => 'normal',
  1485. 'font-style' => 'normal',
  1486. 'font-variant' => 'normal',
  1487. 'font-weight' => 'normal',
  1488. 'glyph-orientation-horizontal' => '0deg',
  1489. 'glyph-orientation-vertical' => 'auto',
  1490. 'image-rendering' => 'auto',
  1491. 'kerning' => 'auto',
  1492. 'letter-spacing' => 'normal',
  1493. 'lighting-color' => 'white',
  1494. 'marker' => '',
  1495. 'marker-end' => 'none',
  1496. 'marker-mid' => 'none',
  1497. 'marker-start' => 'none',
  1498. 'mask' => 'none',
  1499. 'opacity' => 1,
  1500. 'overflow' => 'auto',
  1501. 'pointer-events' => 'visiblePainted',
  1502. 'shape-rendering' => 'auto',
  1503. 'stop-color' => 'black',
  1504. 'stop-opacity' => 1,
  1505. 'stroke' => 'none',
  1506. 'stroke-dasharray' => 'none',
  1507. 'stroke-dashoffset' => 0,
  1508. 'stroke-linecap' => 'butt',
  1509. 'stroke-linejoin' => 'miter',
  1510. 'stroke-miterlimit' => 4,
  1511. 'stroke-opacity' => 1,
  1512. 'stroke-width' => 1,
  1513. 'text-anchor' => 'start',
  1514. 'text-decoration' => 'none',
  1515. 'text-rendering' => 'auto',
  1516. 'unicode-bidi' => 'normal',
  1517. 'visibility' => 'visible',
  1518. 'word-spacing' => 'normal',
  1519. 'writing-mode' => 'lr-tb',
  1520. 'text-color' => 'black',
  1521. 'transfmatrix' => array(1, 0, 0, 1, 0, 0)
  1522. ));
  1523. /**
  1524. * If true force sRGB color profile for all document.
  1525. * @protected
  1526. * @since 5.9.121 (2011-09-28)
  1527. */
  1528. protected $force_srgb = false;
  1529. /**
  1530. * If true set the document to PDF/A mode.
  1531. * @protected
  1532. * @since 5.9.121 (2011-09-27)
  1533. */
  1534. protected $pdfa_mode = false;
  1535. /**
  1536. * version of PDF/A mode (1 - 3).
  1537. * @protected
  1538. * @since 6.2.26 (2019-03-12)
  1539. */
  1540. protected $pdfa_version = 1;
  1541. /**
  1542. * Document creation date-time
  1543. * @protected
  1544. * @since 5.9.152 (2012-03-22)
  1545. */
  1546. protected $doc_creation_timestamp;
  1547. /**
  1548. * Document modification date-time
  1549. * @protected
  1550. * @since 5.9.152 (2012-03-22)
  1551. */
  1552. protected $doc_modification_timestamp;
  1553. /**
  1554. * Custom XMP data.
  1555. * @protected
  1556. * @since 5.9.128 (2011-10-06)
  1557. */
  1558. protected $custom_xmp = '';
  1559. /**
  1560. * Custom XMP RDF data.
  1561. * @protected
  1562. * @since 6.3.0 (2019-09-19)
  1563. */
  1564. protected $custom_xmp_rdf = '';
  1565. /**
  1566. * Overprint mode array.
  1567. * (Check the "Entries in a Graphics State Parameter Dictionary" on PDF 32000-1:2008).
  1568. * @protected
  1569. * @since 5.9.152 (2012-03-23)
  1570. * @var array<string,bool|int>
  1571. */
  1572. protected $overprint = array('OP' => false, 'op' => false, 'OPM' => 0);
  1573. /**
  1574. * Alpha mode array.
  1575. * (Check the "Entries in a Graphics State Parameter Dictionary" on PDF 32000-1:2008).
  1576. * @protected
  1577. * @since 5.9.152 (2012-03-23)
  1578. */
  1579. protected $alpha = array('CA' => 1, 'ca' => 1, 'BM' => '/Normal', 'AIS' => false);
  1580. /**
  1581. * Define the page boundaries boxes to be set on document.
  1582. * @protected
  1583. * @since 5.9.152 (2012-03-23)
  1584. */
  1585. protected $page_boxes = array('MediaBox', 'CropBox', 'BleedBox', 'TrimBox', 'ArtBox');
  1586. /**
  1587. * If true print TCPDF meta link.
  1588. * @protected
  1589. * @since 5.9.152 (2012-03-23)
  1590. */
  1591. protected $tcpdflink = true;
  1592. /**
  1593. * Cache array for computed GD gamma values.
  1594. * @protected
  1595. * @since 5.9.1632 (2012-06-05)
  1596. */
  1597. protected $gdgammacache = array();
  1598. /**
  1599. * Cache array for file content
  1600. * @protected
  1601. * @var array
  1602. * @since 6.3.5 (2020-09-28)
  1603. */
  1604. protected $fileContentCache = array();
  1605. /**
  1606. * Whether to allow local file path in image html tags, when prefixed with file://
  1607. *
  1608. * @var bool
  1609. * @protected
  1610. * @since 6.4 (2020-07-23)
  1611. */
  1612. protected $allowLocalFiles = false;
  1613. //------------------------------------------------------------
  1614. // METHODS
  1615. //------------------------------------------------------------
  1616. /**
  1617. * This is the class constructor.
  1618. * It allows to set up the page format, the orientation and the measure unit used in all the methods (except for the font sizes).
  1619. *
  1620. * @param string $orientation page orientation. Possible values are (case insensitive):<ul><li>P or Portrait (default)</li><li>L or Landscape</li><li>'' (empty string) for automatic orientation</li></ul>
  1621. * @param string $unit User measure unit. Possible values are:<ul><li>pt: point</li><li>mm: millimeter (default)</li><li>cm: centimeter</li><li>in: inch</li></ul><br />A point equals 1/72 of inch, that is to say about 0.35 mm (an inch being 2.54 cm). This is a very common unit in typography; font sizes are expressed in that unit.
  1622. * @param mixed $format The format used for pages. It can be either: one of the string values specified at getPageSizeFromFormat() or an array of parameters specified at setPageFormat().
  1623. * @param boolean $unicode TRUE means that the input text is unicode (default = true)
  1624. * @param string $encoding Charset encoding (used only when converting back html entities); default is UTF-8.
  1625. * @param boolean $diskcache DEPRECATED FEATURE
  1626. * @param false|integer $pdfa If not false, set the document to PDF/A mode and the good version (1 or 3).
  1627. * @public
  1628. * @see getPageSizeFromFormat(), setPageFormat()
  1629. */
  1630. public function __construct($orientation='P', $unit='mm', $format='A4', $unicode=true, $encoding='UTF-8', $diskcache=false, $pdfa=false) {
  1631. // set file ID for trailer
  1632. $serformat = (is_array($format) ? json_encode($format) : $format);
  1633. $this->file_id = md5(TCPDF_STATIC::getRandomSeed('TCPDF'.$orientation.$unit.$serformat.$encoding));
  1634. $this->hash_key = hash_hmac('sha256', TCPDF_STATIC::getRandomSeed($this->file_id), TCPDF_STATIC::getRandomSeed('TCPDF'), false);
  1635. $this->font_obj_ids = array();
  1636. $this->page_obj_id = array();
  1637. $this->form_obj_id = array();
  1638. // set pdf/a mode
  1639. if ($pdfa != false) {
  1640. $this->pdfa_mode = true;
  1641. $this->pdfa_version = $pdfa; // 1 or 3
  1642. } else
  1643. $this->pdfa_mode = false;
  1644. $this->force_srgb = false;
  1645. // set language direction
  1646. $this->rtl = false;
  1647. $this->tmprtl = false;
  1648. // some checks
  1649. $this->_dochecks();
  1650. // initialization of properties
  1651. $this->isunicode = $unicode;
  1652. $this->page = 0;
  1653. $this->transfmrk[0] = array();
  1654. $this->pagedim = array();
  1655. $this->n = 2;
  1656. $this->buffer = '';
  1657. $this->pages = array();
  1658. $this->state = 0;
  1659. $this->fonts = array();
  1660. $this->FontFiles = array();
  1661. $this->diffs = array();
  1662. $this->images = array();
  1663. $this->links = array();
  1664. $this->gradients = array();
  1665. $this->InFooter = false;
  1666. $this->lasth = 0;
  1667. $this->FontFamily = defined('PDF_FONT_NAME_MAIN')?PDF_FONT_NAME_MAIN:'helvetica';
  1668. $this->FontStyle = '';
  1669. $this->FontSizePt = 12;
  1670. $this->underline = false;
  1671. $this->overline = false;
  1672. $this->linethrough = false;
  1673. $this->DrawColor = '0 G';
  1674. $this->FillColor = '0 g';
  1675. $this->TextColor = '0 g';
  1676. $this->ColorFlag = false;
  1677. $this->pdflayers = array();
  1678. // encryption values
  1679. $this->encrypted = false;
  1680. $this->last_enc_key = '';
  1681. // standard Unicode fonts
  1682. $this->CoreFonts = array(
  1683. 'courier'=>'Courier',
  1684. 'courierB'=>'Courier-Bold',
  1685. 'courierI'=>'Courier-Oblique',
  1686. 'courierBI'=>'Courier-BoldOblique',
  1687. 'helvetica'=>'Helvetica',
  1688. 'helveticaB'=>'Helvetica-Bold',
  1689. 'helveticaI'=>'Helvetica-Oblique',
  1690. 'helveticaBI'=>'Helvetica-BoldOblique',
  1691. 'times'=>'Times-Roman',
  1692. 'timesB'=>'Times-Bold',
  1693. 'timesI'=>'Times-Italic',
  1694. 'timesBI'=>'Times-BoldItalic',
  1695. 'symbol'=>'Symbol',
  1696. 'zapfdingbats'=>'ZapfDingbats'
  1697. );
  1698. // set scale factor
  1699. $this->setPageUnit($unit);
  1700. // set page format and orientation
  1701. $this->setPageFormat($format, $orientation);
  1702. // page margins (1 cm)
  1703. $margin = 28.35 / $this->k;
  1704. $this->setMargins($margin, $margin);
  1705. $this->clMargin = $this->lMargin;
  1706. $this->crMargin = $this->rMargin;
  1707. // internal cell padding
  1708. $cpadding = $margin / 10;
  1709. $this->setCellPaddings($cpadding, 0, $cpadding, 0);
  1710. // cell margins
  1711. $this->setCellMargins(0, 0, 0, 0);
  1712. // line width (0.2 mm)
  1713. $this->LineWidth = 0.57 / $this->k;
  1714. $this->linestyleWidth = sprintf('%F w', ($this->LineWidth * $this->k));
  1715. $this->linestyleCap = '0 J';
  1716. $this->linestyleJoin = '0 j';
  1717. $this->linestyleDash = '[] 0 d';
  1718. // automatic page break
  1719. $this->setAutoPageBreak(true, (2 * $margin));
  1720. // full width display mode
  1721. $this->setDisplayMode('fullwidth');
  1722. // compression
  1723. $this->setCompression();
  1724. // set default PDF version number
  1725. $this->setPDFVersion();
  1726. $this->tcpdflink = true;
  1727. $this->encoding = $encoding;
  1728. $this->HREF = array();
  1729. $this->getFontsList();
  1730. $this->fgcolor = array('R' => 0, 'G' => 0, 'B' => 0);
  1731. $this->strokecolor = array('R' => 0, 'G' => 0, 'B' => 0);
  1732. $this->bgcolor = array('R' => 255, 'G' => 255, 'B' => 255);
  1733. $this->extgstates = array();
  1734. $this->setTextShadow();
  1735. // signature
  1736. $this->sign = false;
  1737. $this->tsa_timestamp = false;
  1738. $this->tsa_data = array();
  1739. $this->signature_appearance = array('page' => 1, 'rect' => '0 0 0 0', 'name' => 'Signature');
  1740. $this->empty_signature_appearance = array();
  1741. // user's rights
  1742. $this->ur['enabled'] = false;
  1743. $this->ur['document'] = '/FullSave';
  1744. $this->ur['annots'] = '/Create/Delete/Modify/Copy/Import/Export';
  1745. $this->ur['form'] = '/Add/Delete/FillIn/Import/Export/SubmitStandalone/SpawnTemplate';
  1746. $this->ur['signature'] = '/Modify';
  1747. $this->ur['ef'] = '/Create/Delete/Modify/Import';
  1748. $this->ur['formex'] = '';
  1749. // set default JPEG quality
  1750. $this->jpeg_quality = 75;
  1751. // initialize some settings
  1752. TCPDF_FONTS::utf8Bidi(array(), '', false, $this->isunicode, $this->CurrentFont);
  1753. // set default font
  1754. $this->setFont($this->FontFamily, $this->FontStyle, $this->FontSizePt);
  1755. $this->setHeaderFont(array($this->FontFamily, $this->FontStyle, $this->FontSizePt));
  1756. $this->setFooterFont(array($this->FontFamily, $this->FontStyle, $this->FontSizePt));
  1757. // check if PCRE Unicode support is enabled
  1758. if ($this->isunicode AND (@preg_match('/\pL/u', 'a') == 1)) {
  1759. // PCRE unicode support is turned ON
  1760. // \s : any whitespace character
  1761. // \p{Z} : any separator
  1762. // \p{Lo} : Unicode letter or ideograph that does not have lowercase and uppercase variants. Is used to chunk chinese words.
  1763. // \xa0 : Unicode Character 'NO-BREAK SPACE' (U+00A0)
  1764. //$this->setSpacesRE('/(?!\xa0)[\s\p{Z}\p{Lo}]/u');
  1765. $this->setSpacesRE('/(?!\xa0)[\s\p{Z}]/u');
  1766. } else {
  1767. // PCRE unicode support is turned OFF
  1768. $this->setSpacesRE('/[^\S\xa0]/');
  1769. }
  1770. $this->default_form_prop = array('lineWidth'=>1, 'borderStyle'=>'solid', 'fillColor'=>array(255, 255, 255), 'strokeColor'=>array(128, 128, 128));
  1771. // set document creation and modification timestamp
  1772. $this->doc_creation_timestamp = time();
  1773. $this->doc_modification_timestamp = $this->doc_creation_timestamp;
  1774. // get default graphic vars
  1775. $this->default_graphic_vars = $this->getGraphicVars();
  1776. $this->header_xobj_autoreset = false;
  1777. $this->custom_xmp = '';
  1778. $this->custom_xmp_rdf = '';
  1779. }
  1780. /**
  1781. * Default destructor.
  1782. * @public
  1783. * @since 1.53.0.TC016
  1784. */
  1785. public function __destruct() {
  1786. // cleanup
  1787. $this->_destroy(true);
  1788. }
  1789. /**
  1790. * Set the units of measure for the document.
  1791. * @param string $unit User measure unit. Possible values are:<ul><li>pt: point</li><li>mm: millimeter (default)</li><li>cm: centimeter</li><li>in: inch</li></ul><br />A point equals 1/72 of inch, that is to say about 0.35 mm (an inch being 2.54 cm). This is a very common unit in typography; font sizes are expressed in that unit.
  1792. * @public
  1793. * @since 3.0.015 (2008-06-06)
  1794. */
  1795. public function setPageUnit($unit) {
  1796. $unit = strtolower($unit);
  1797. //Set scale factor
  1798. switch ($unit) {
  1799. // points
  1800. case 'px':
  1801. case 'pt': {
  1802. $this->k = 1;
  1803. break;
  1804. }
  1805. // millimeters
  1806. case 'mm': {
  1807. $this->k = $this->dpi / 25.4;
  1808. break;
  1809. }
  1810. // centimeters
  1811. case 'cm': {
  1812. $this->k = $this->dpi / 2.54;
  1813. break;
  1814. }
  1815. // inches
  1816. case 'in': {
  1817. $this->k = $this->dpi;
  1818. break;
  1819. }
  1820. // unsupported unit
  1821. default : {
  1822. $this->Error('Incorrect unit: '.$unit);
  1823. break;
  1824. }
  1825. }
  1826. $this->pdfunit = $unit;
  1827. if (isset($this->CurOrientation)) {
  1828. $this->setPageOrientation($this->CurOrientation);
  1829. }
  1830. }
  1831. /**
  1832. * Change the format of the current page
  1833. * @param mixed $format The format used for pages. It can be either: one of the string values specified at getPageSizeFromFormat() documentation or an array of two numbers (width, height) or an array containing the following measures and options:<ul>
  1834. * <li>['format'] = page format name (one of the above);</li>
  1835. * <li>['Rotate'] : The number of degrees by which the page shall be rotated clockwise when displayed or printed. The value shall be a multiple of 90.</li>
  1836. * <li>['PZ'] : The page's preferred zoom (magnification) factor.</li>
  1837. * <li>['MediaBox'] : the boundaries of the physical medium on which the page shall be displayed or printed:</li>
  1838. * <li>['MediaBox']['llx'] : lower-left x coordinate</li>
  1839. * <li>['MediaBox']['lly'] : lower-left y coordinate</li>
  1840. * <li>['MediaBox']['urx'] : upper-right x coordinate</li>
  1841. * <li>['MediaBox']['ury'] : upper-right y coordinate</li>
  1842. * <li>['CropBox'] : the visible region of default user space:</li>
  1843. * <li>['CropBox']['llx'] : lower-left x coordinate</li>
  1844. * <li>['CropBox']['lly'] : lower-left y coordinate</li>
  1845. * <li>['CropBox']['urx'] : upper-right x coordinate</li>
  1846. * <li>['CropBox']['ury'] : upper-right y coordinate</li>
  1847. * <li>['BleedBox'] : the region to which the contents of the page shall be clipped when output in a production environment:</li>
  1848. * <li>['BleedBox']['llx'] : lower-left x coordinate</li>
  1849. * <li>['BleedBox']['lly'] : lower-left y coordinate</li>
  1850. * <li>['BleedBox']['urx'] : upper-right x coordinate</li>
  1851. * <li>['BleedBox']['ury'] : upper-right y coordinate</li>
  1852. * <li>['TrimBox'] : the intended dimensions of the finished page after trimming:</li>
  1853. * <li>['TrimBox']['llx'] : lower-left x coordinate</li>
  1854. * <li>['TrimBox']['lly'] : lower-left y coordinate</li>
  1855. * <li>['TrimBox']['urx'] : upper-right x coordinate</li>
  1856. * <li>['TrimBox']['ury'] : upper-right y coordinate</li>
  1857. * <li>['ArtBox'] : the extent of the page's meaningful content:</li>
  1858. * <li>['ArtBox']['llx'] : lower-left x coordinate</li>
  1859. * <li>['ArtBox']['lly'] : lower-left y coordinate</li>
  1860. * <li>['ArtBox']['urx'] : upper-right x coordinate</li>
  1861. * <li>['ArtBox']['ury'] : upper-right y coordinate</li>
  1862. * <li>['BoxColorInfo'] :specify the colours and other visual characteristics that should be used in displaying guidelines on the screen for each of the possible page boundaries other than the MediaBox:</li>
  1863. * <li>['BoxColorInfo'][BOXTYPE]['C'] : an array of three numbers in the range 0-255, representing the components in the DeviceRGB colour space.</li>
  1864. * <li>['BoxColorInfo'][BOXTYPE]['W'] : the guideline width in default user units</li>
  1865. * <li>['BoxColorInfo'][BOXTYPE]['S'] : the guideline style: S = Solid; D = Dashed</li>
  1866. * <li>['BoxColorInfo'][BOXTYPE]['D'] : dash array defining a pattern of dashes and gaps to be used in drawing dashed guidelines</li>
  1867. * <li>['trans'] : the style and duration of the visual transition to use when moving from another page to the given page during a presentation</li>
  1868. * <li>['trans']['Dur'] : The page's display duration (also called its advance timing): the maximum length of time, in seconds, that the page shall be displayed during presentations before the viewer application shall automatically advance to the next page.</li>
  1869. * <li>['trans']['S'] : transition style : Split, Blinds, Box, Wipe, Dissolve, Glitter, R, Fly, Push, Cover, Uncover, Fade</li>
  1870. * <li>['trans']['D'] : The duration of the transition effect, in seconds.</li>
  1871. * <li>['trans']['Dm'] : (Split and Blinds transition styles only) The dimension in which the specified transition effect shall occur: H = Horizontal, V = Vertical. Default value: H.</li>
  1872. * <li>['trans']['M'] : (Split, Box and Fly transition styles only) The direction of motion for the specified transition effect: I = Inward from the edges of the page, O = Outward from the center of the pageDefault value: I.</li>
  1873. * <li>['trans']['Di'] : (Wipe, Glitter, Fly, Cover, Uncover and Push transition styles only) The direction in which the specified transition effect shall moves, expressed in degrees counterclockwise starting from a left-to-right direction. If the value is a number, it shall be one of: 0 = Left to right, 90 = Bottom to top (Wipe only), 180 = Right to left (Wipe only), 270 = Top to bottom, 315 = Top-left to bottom-right (Glitter only). If the value is a name, it shall be None, which is relevant only for the Fly transition when the value of SS is not 1.0. Default value: 0.</li>
  1874. * <li>['trans']['SS'] : (Fly transition style only) The starting or ending scale at which the changes shall be drawn. If M specifies an inward transition, the scale of the changes drawn shall progress from SS to 1.0 over the course of the transition. If M specifies an outward transition, the scale of the changes drawn shall progress from 1.0 to SS over the course of the transition. Default: 1.0.</li>
  1875. * <li>['trans']['B'] : (Fly transition style only) If true, the area that shall be flown in is rectangular and opaque. Default: false.</li>
  1876. * </ul>
  1877. * @param string $orientation page orientation. Possible values are (case insensitive):<ul>
  1878. * <li>P or Portrait (default)</li>
  1879. * <li>L or Landscape</li>
  1880. * <li>'' (empty string) for automatic orientation</li>
  1881. * </ul>
  1882. * @protected
  1883. * @since 3.0.015 (2008-06-06)
  1884. * @see getPageSizeFromFormat()
  1885. */
  1886. protected function setPageFormat($format, $orientation='P') {
  1887. if (!empty($format) AND isset($this->pagedim[$this->page])) {
  1888. // remove inherited values
  1889. unset($this->pagedim[$this->page]);
  1890. }
  1891. if (is_string($format)) {
  1892. // get page measures from format name
  1893. $pf = TCPDF_STATIC::getPageSizeFromFormat($format);
  1894. $this->fwPt = $pf[0];
  1895. $this->fhPt = $pf[1];
  1896. } else {
  1897. // the boundaries of the physical medium on which the page shall be displayed or printed
  1898. if (isset($format['MediaBox'])) {
  1899. $this->pagedim = TCPDF_STATIC::setPageBoxes($this->page, 'MediaBox', $format['MediaBox']['llx'], $format['MediaBox']['lly'], $format['MediaBox']['urx'], $format['MediaBox']['ury'], false, $this->k, $this->pagedim);
  1900. $this->fwPt = (($format['MediaBox']['urx'] - $format['MediaBox']['llx']) * $this->k);
  1901. $this->fhPt = (($format['MediaBox']['ury'] - $format['MediaBox']['lly']) * $this->k);
  1902. } else {
  1903. if (isset($format[0]) AND is_numeric($format[0]) AND isset($format[1]) AND is_numeric($format[1])) {
  1904. $pf = array(($format[0] * $this->k), ($format[1] * $this->k));
  1905. } else {
  1906. if (!isset($format['format'])) {
  1907. // default value
  1908. $format['format'] = 'A4';
  1909. }
  1910. $pf = TCPDF_STATIC::getPageSizeFromFormat($format['format']);
  1911. }
  1912. $this->fwPt = $pf[0];
  1913. $this->fhPt = $pf[1];
  1914. $this->pagedim = TCPDF_STATIC::setPageBoxes($this->page, 'MediaBox', 0, 0, $this->fwPt, $this->fhPt, true, $this->k, $this->pagedim);
  1915. }
  1916. // the visible region of default user space
  1917. if (isset($format['CropBox'])) {
  1918. $this->pagedim = TCPDF_STATIC::setPageBoxes($this->page, 'CropBox', $format['CropBox']['llx'], $format['CropBox']['lly'], $format['CropBox']['urx'], $format['CropBox']['ury'], false, $this->k, $this->pagedim);
  1919. }
  1920. // the region to which the contents of the page shall be clipped when output in a production environment
  1921. if (isset($format['BleedBox'])) {
  1922. $this->pagedim = TCPDF_STATIC::setPageBoxes($this->page, 'BleedBox', $format['BleedBox']['llx'], $format['BleedBox']['lly'], $format['BleedBox']['urx'], $format['BleedBox']['ury'], false, $this->k, $this->pagedim);
  1923. }
  1924. // the intended dimensions of the finished page after trimming
  1925. if (isset($format['TrimBox'])) {
  1926. $this->pagedim = TCPDF_STATIC::setPageBoxes($this->page, 'TrimBox', $format['TrimBox']['llx'], $format['TrimBox']['lly'], $format['TrimBox']['urx'], $format['TrimBox']['ury'], false, $this->k, $this->pagedim);
  1927. }
  1928. // the page's meaningful content (including potential white space)
  1929. if (isset($format['ArtBox'])) {
  1930. $this->pagedim = TCPDF_STATIC::setPageBoxes($this->page, 'ArtBox', $format['ArtBox']['llx'], $format['ArtBox']['lly'], $format['ArtBox']['urx'], $format['ArtBox']['ury'], false, $this->k, $this->pagedim);
  1931. }
  1932. // specify the colours and other visual characteristics that should be used in displaying guidelines on the screen for the various page boundaries
  1933. if (isset($format['BoxColorInfo'])) {
  1934. $this->pagedim[$this->page]['BoxColorInfo'] = $format['BoxColorInfo'];
  1935. }
  1936. if (isset($format['Rotate']) AND (($format['Rotate'] % 90) == 0)) {
  1937. // The number of degrees by which the page shall be rotated clockwise when displayed or printed. The value shall be a multiple of 90.
  1938. $this->pagedim[$this->page]['Rotate'] = intval($format['Rotate']);
  1939. }
  1940. if (isset($format['PZ'])) {
  1941. // The page's preferred zoom (magnification) factor
  1942. $this->pagedim[$this->page]['PZ'] = floatval($format['PZ']);
  1943. }
  1944. if (isset($format['trans'])) {
  1945. // The style and duration of the visual transition to use when moving from another page to the given page during a presentation
  1946. if (isset($format['trans']['Dur'])) {
  1947. // The page's display duration
  1948. $this->pagedim[$this->page]['trans']['Dur'] = floatval($format['trans']['Dur']);
  1949. }
  1950. $stansition_styles = array('Split', 'Blinds', 'Box', 'Wipe', 'Dissolve', 'Glitter', 'R', 'Fly', 'Push', 'Cover', 'Uncover', 'Fade');
  1951. if (isset($format['trans']['S']) AND in_array($format['trans']['S'], $stansition_styles)) {
  1952. // The transition style that shall be used when moving to this page from another during a presentation
  1953. $this->pagedim[$this->page]['trans']['S'] = $format['trans']['S'];
  1954. $valid_effect = array('Split', 'Blinds');
  1955. $valid_vals = array('H', 'V');
  1956. if (isset($format['trans']['Dm']) AND in_array($format['trans']['S'], $valid_effect) AND in_array($format['trans']['Dm'], $valid_vals)) {
  1957. $this->pagedim[$this->page]['trans']['Dm'] = $format['trans']['Dm'];
  1958. }
  1959. $valid_effect = array('Split', 'Box', 'Fly');
  1960. $valid_vals = array('I', 'O');
  1961. if (isset($format['trans']['M']) AND in_array($format['trans']['S'], $valid_effect) AND in_array($format['trans']['M'], $valid_vals)) {
  1962. $this->pagedim[$this->page]['trans']['M'] = $format['trans']['M'];
  1963. }
  1964. $valid_effect = array('Wipe', 'Glitter', 'Fly', 'Cover', 'Uncover', 'Push');
  1965. if (isset($format['trans']['Di']) AND in_array($format['trans']['S'], $valid_effect)) {
  1966. if (((($format['trans']['Di'] == 90) OR ($format['trans']['Di'] == 180)) AND ($format['trans']['S'] == 'Wipe'))
  1967. OR (($format['trans']['Di'] == 315) AND ($format['trans']['S'] == 'Glitter'))
  1968. OR (($format['trans']['Di'] == 0) OR ($format['trans']['Di'] == 270))) {
  1969. $this->pagedim[$this->page]['trans']['Di'] = intval($format['trans']['Di']);
  1970. }
  1971. }
  1972. if (isset($format['trans']['SS']) AND ($format['trans']['S'] == 'Fly')) {
  1973. $this->pagedim[$this->page]['trans']['SS'] = floatval($format['trans']['SS']);
  1974. }
  1975. if (isset($format['trans']['B']) AND ($format['trans']['B'] === true) AND ($format['trans']['S'] == 'Fly')) {
  1976. $this->pagedim[$this->page]['trans']['B'] = 'true';
  1977. }
  1978. } else {
  1979. $this->pagedim[$this->page]['trans']['S'] = 'R';
  1980. }
  1981. if (isset($format['trans']['D'])) {
  1982. // The duration of the transition effect, in seconds
  1983. $this->pagedim[$this->page]['trans']['D'] = floatval($format['trans']['D']);
  1984. } else {
  1985. $this->pagedim[$this->page]['trans']['D'] = 1;
  1986. }
  1987. }
  1988. }
  1989. $this->setPageOrientation($orientation);
  1990. }
  1991. /**
  1992. * Set page orientation.
  1993. * @param string $orientation page orientation. Possible values are (case insensitive):<ul><li>P or Portrait (default)</li><li>L or Landscape</li><li>'' (empty string) for automatic orientation</li></ul>
  1994. * @param boolean|null $autopagebreak Boolean indicating if auto-page-break mode should be on or off.
  1995. * @param float|null $bottommargin bottom margin of the page.
  1996. * @public
  1997. * @since 3.0.015 (2008-06-06)
  1998. */
  1999. public function setPageOrientation($orientation, $autopagebreak=null, $bottommargin=null) {
  2000. if (!isset($this->pagedim[$this->page]['MediaBox'])) {
  2001. // the boundaries of the physical medium on which the page shall be displayed or printed
  2002. $this->pagedim = TCPDF_STATIC::setPageBoxes($this->page, 'MediaBox', 0, 0, $this->fwPt, $this->fhPt, true, $this->k, $this->pagedim);
  2003. }
  2004. if (!isset($this->pagedim[$this->page]['CropBox'])) {
  2005. // the visible region of default user space
  2006. $this->pagedim = TCPDF_STATIC::setPageBoxes($this->page, 'CropBox', $this->pagedim[$this->page]['MediaBox']['llx'], $this->pagedim[$this->page]['MediaBox']['lly'], $this->pagedim[$this->page]['MediaBox']['urx'], $this->pagedim[$this->page]['MediaBox']['ury'], true, $this->k, $this->pagedim);
  2007. }
  2008. if (!isset($this->pagedim[$this->page]['BleedBox'])) {
  2009. // the region to which the contents of the page shall be clipped when output in a production environment
  2010. $this->pagedim = TCPDF_STATIC::setPageBoxes($this->page, 'BleedBox', $this->pagedim[$this->page]['CropBox']['llx'], $this->pagedim[$this->page]['CropBox']['lly'], $this->pagedim[$this->page]['CropBox']['urx'], $this->pagedim[$this->page]['CropBox']['ury'], true, $this->k, $this->pagedim);
  2011. }
  2012. if (!isset($this->pagedim[$this->page]['TrimBox'])) {
  2013. // the intended dimensions of the finished page after trimming
  2014. $this->pagedim = TCPDF_STATIC::setPageBoxes($this->page, 'TrimBox', $this->pagedim[$this->page]['CropBox']['llx'], $this->pagedim[$this->page]['CropBox']['lly'], $this->pagedim[$this->page]['CropBox']['urx'], $this->pagedim[$this->page]['CropBox']['ury'], true, $this->k, $this->pagedim);
  2015. }
  2016. if (!isset($this->pagedim[$this->page]['ArtBox'])) {
  2017. // the page's meaningful content (including potential white space)
  2018. $this->pagedim = TCPDF_STATIC::setPageBoxes($this->page, 'ArtBox', $this->pagedim[$this->page]['CropBox']['llx'], $this->pagedim[$this->page]['CropBox']['lly'], $this->pagedim[$this->page]['CropBox']['urx'], $this->pagedim[$this->page]['CropBox']['ury'], true, $this->k, $this->pagedim);
  2019. }
  2020. if (!isset($this->pagedim[$this->page]['Rotate'])) {
  2021. // The number of degrees by which the page shall be rotated clockwise when displayed or printed. The value shall be a multiple of 90.
  2022. $this->pagedim[$this->page]['Rotate'] = 0;
  2023. }
  2024. if (!isset($this->pagedim[$this->page]['PZ'])) {
  2025. // The page's preferred zoom (magnification) factor
  2026. $this->pagedim[$this->page]['PZ'] = 1;
  2027. }
  2028. if ($this->fwPt > $this->fhPt) {
  2029. // landscape
  2030. $default_orientation = 'L';
  2031. } else {
  2032. // portrait
  2033. $default_orientation = 'P';
  2034. }
  2035. $valid_orientations = array('P', 'L');
  2036. if (empty($orientation)) {
  2037. $orientation = $default_orientation;
  2038. } else {
  2039. $orientation = strtoupper($orientation[0]);
  2040. }
  2041. if (in_array($orientation, $valid_orientations) AND ($orientation != $default_orientation)) {
  2042. $this->CurOrientation = $orientation;
  2043. $this->wPt = $this->fhPt;
  2044. $this->hPt = $this->fwPt;
  2045. } else {
  2046. $this->CurOrientation = $default_orientation;
  2047. $this->wPt = $this->fwPt;
  2048. $this->hPt = $this->fhPt;
  2049. }
  2050. if ((abs($this->pagedim[$this->page]['MediaBox']['urx'] - $this->hPt) < $this->feps) AND (abs($this->pagedim[$this->page]['MediaBox']['ury'] - $this->wPt) < $this->feps)){
  2051. // swap X and Y coordinates (change page orientation)
  2052. $this->pagedim = TCPDF_STATIC::swapPageBoxCoordinates($this->page, $this->pagedim);
  2053. }
  2054. $this->w = ($this->wPt / $this->k);
  2055. $this->h = ($this->hPt / $this->k);
  2056. if (TCPDF_STATIC::empty_string($autopagebreak)) {
  2057. if (isset($this->AutoPageBreak)) {
  2058. $autopagebreak = $this->AutoPageBreak;
  2059. } else {
  2060. $autopagebreak = true;
  2061. }
  2062. }
  2063. if (TCPDF_STATIC::empty_string($bottommargin)) {
  2064. if (isset($this->bMargin)) {
  2065. $bottommargin = $this->bMargin;
  2066. } else {
  2067. // default value = 2 cm
  2068. $bottommargin = 2 * 28.35 / $this->k;
  2069. }
  2070. }
  2071. $this->setAutoPageBreak($autopagebreak, $bottommargin);
  2072. // store page dimensions
  2073. $this->pagedim[$this->page]['w'] = $this->wPt;
  2074. $this->pagedim[$this->page]['h'] = $this->hPt;
  2075. $this->pagedim[$this->page]['wk'] = $this->w;
  2076. $this->pagedim[$this->page]['hk'] = $this->h;
  2077. $this->pagedim[$this->page]['tm'] = $this->tMargin;
  2078. $this->pagedim[$this->page]['bm'] = $bottommargin;
  2079. $this->pagedim[$this->page]['lm'] = $this->lMargin;
  2080. $this->pagedim[$this->page]['rm'] = $this->rMargin;
  2081. $this->pagedim[$this->page]['pb'] = $autopagebreak;
  2082. $this->pagedim[$this->page]['or'] = $this->CurOrientation;
  2083. $this->pagedim[$this->page]['olm'] = $this->original_lMargin;
  2084. $this->pagedim[$this->page]['orm'] = $this->original_rMargin;
  2085. }
  2086. /**
  2087. * Set regular expression to detect withespaces or word separators.
  2088. * The pattern delimiter must be the forward-slash character "/".
  2089. * Some example patterns are:
  2090. * <pre>
  2091. * Non-Unicode or missing PCRE unicode support: "/[^\S\xa0]/"
  2092. * Unicode and PCRE unicode support: "/(?!\xa0)[\s\p{Z}]/u"
  2093. * Unicode and PCRE unicode support in Chinese mode: "/(?!\xa0)[\s\p{Z}\p{Lo}]/u"
  2094. * if PCRE unicode support is turned ON ("\P" is the negate class of "\p"):
  2095. * \s : any whitespace character
  2096. * \p{Z} : any separator
  2097. * \p{Lo} : Unicode letter or ideograph that does not have lowercase and uppercase variants. Is used to chunk chinese words.
  2098. * \xa0 : Unicode Character 'NO-BREAK SPACE' (U+00A0)
  2099. * </pre>
  2100. * @param string $re regular expression (leave empty for default).
  2101. * @public
  2102. * @since 4.6.016 (2009-06-15)
  2103. */
  2104. public function setSpacesRE($re='/[^\S\xa0]/') {
  2105. $this->re_spaces = $re;
  2106. $re_parts = explode('/', $re);
  2107. // get pattern parts
  2108. $this->re_space = array();
  2109. if (isset($re_parts[1]) AND !empty($re_parts[1])) {
  2110. $this->re_space['p'] = $re_parts[1];
  2111. } else {
  2112. $this->re_space['p'] = '[\s]';
  2113. }
  2114. // set pattern modifiers
  2115. if (isset($re_parts[2]) AND !empty($re_parts[2])) {
  2116. $this->re_space['m'] = $re_parts[2];
  2117. } else {
  2118. $this->re_space['m'] = '';
  2119. }
  2120. }
  2121. /**
  2122. * Enable or disable Right-To-Left language mode
  2123. * @param boolean $enable if true enable Right-To-Left language mode.
  2124. * @param boolean $resetx if true reset the X position on direction change.
  2125. * @public
  2126. * @since 2.0.000 (2008-01-03)
  2127. */
  2128. public function setRTL($enable, $resetx=true) {
  2129. $enable = $enable ? true : false;
  2130. $resetx = ($resetx AND ($enable != $this->rtl));
  2131. $this->rtl = $enable;
  2132. $this->tmprtl = false;
  2133. if ($resetx) {
  2134. $this->Ln(0);
  2135. }
  2136. }
  2137. /**
  2138. * Return the RTL status
  2139. * @return bool
  2140. * @public
  2141. * @since 4.0.012 (2008-07-24)
  2142. */
  2143. public function getRTL() {
  2144. return $this->rtl;
  2145. }
  2146. /**
  2147. * Force temporary RTL language direction
  2148. * @param false|string $mode can be false, 'L' for LTR or 'R' for RTL
  2149. * @public
  2150. * @since 2.1.000 (2008-01-09)
  2151. */
  2152. public function setTempRTL($mode) {
  2153. $newmode = false;
  2154. switch (strtoupper($mode)) {
  2155. case 'LTR':
  2156. case 'L': {
  2157. if ($this->rtl) {
  2158. $newmode = 'L';
  2159. }
  2160. break;
  2161. }
  2162. case 'RTL':
  2163. case 'R': {
  2164. if (!$this->rtl) {
  2165. $newmode = 'R';
  2166. }
  2167. break;
  2168. }
  2169. case false:
  2170. default: {
  2171. $newmode = false;
  2172. break;
  2173. }
  2174. }
  2175. $this->tmprtl = $newmode;
  2176. }
  2177. /**
  2178. * Return the current temporary RTL status
  2179. * @return bool
  2180. * @public
  2181. * @since 4.8.014 (2009-11-04)
  2182. */
  2183. public function isRTLTextDir() {
  2184. return ($this->rtl OR ($this->tmprtl == 'R'));
  2185. }
  2186. /**
  2187. * Set the last cell height.
  2188. * @param float $h cell height.
  2189. * @author Nicola Asuni
  2190. * @public
  2191. * @since 1.53.0.TC034
  2192. */
  2193. public function setLastH($h) {
  2194. $this->lasth = $h;
  2195. }
  2196. /**
  2197. * Return the cell height
  2198. * @param int $fontsize Font size in internal units
  2199. * @param boolean $padding If true add cell padding
  2200. * @public
  2201. * @return float
  2202. */
  2203. public function getCellHeight($fontsize, $padding=TRUE) {
  2204. $height = ($fontsize * $this->cell_height_ratio);
  2205. if ($padding && !empty($this->cell_padding)) {
  2206. $height += ($this->cell_padding['T'] + $this->cell_padding['B']);
  2207. }
  2208. return round($height, 6);
  2209. }
  2210. /**
  2211. * Reset the last cell height.
  2212. * @public
  2213. * @since 5.9.000 (2010-10-03)
  2214. */
  2215. public function resetLastH() {
  2216. $this->lasth = $this->getCellHeight($this->FontSize);
  2217. }
  2218. /**
  2219. * Get the last cell height.
  2220. * @return float last cell height
  2221. * @public
  2222. * @since 4.0.017 (2008-08-05)
  2223. */
  2224. public function getLastH() {
  2225. return $this->lasth;
  2226. }
  2227. /**
  2228. * Set the adjusting factor to convert pixels to user units.
  2229. * @param float $scale adjusting factor to convert pixels to user units.
  2230. * @author Nicola Asuni
  2231. * @public
  2232. * @since 1.5.2
  2233. */
  2234. public function setImageScale($scale) {
  2235. $this->imgscale = $scale;
  2236. }
  2237. /**
  2238. * Returns the adjusting factor to convert pixels to user units.
  2239. * @return float adjusting factor to convert pixels to user units.
  2240. * @author Nicola Asuni
  2241. * @public
  2242. * @since 1.5.2
  2243. */
  2244. public function getImageScale() {
  2245. return $this->imgscale;
  2246. }
  2247. /**
  2248. * Returns an array of page dimensions:
  2249. * <ul><li>$this->pagedim[$this->page]['w'] = page width in points</li><li>$this->pagedim[$this->page]['h'] = height in points</li><li>$this->pagedim[$this->page]['wk'] = page width in user units</li><li>$this->pagedim[$this->page]['hk'] = page height in user units</li><li>$this->pagedim[$this->page]['tm'] = top margin</li><li>$this->pagedim[$this->page]['bm'] = bottom margin</li><li>$this->pagedim[$this->page]['lm'] = left margin</li><li>$this->pagedim[$this->page]['rm'] = right margin</li><li>$this->pagedim[$this->page]['pb'] = auto page break</li><li>$this->pagedim[$this->page]['or'] = page orientation</li><li>$this->pagedim[$this->page]['olm'] = original left margin</li><li>$this->pagedim[$this->page]['orm'] = original right margin</li><li>$this->pagedim[$this->page]['Rotate'] = The number of degrees by which the page shall be rotated clockwise when displayed or printed. The value shall be a multiple of 90.</li><li>$this->pagedim[$this->page]['PZ'] = The page's preferred zoom (magnification) factor.</li><li>$this->pagedim[$this->page]['trans'] : the style and duration of the visual transition to use when moving from another page to the given page during a presentation<ul><li>$this->pagedim[$this->page]['trans']['Dur'] = The page's display duration (also called its advance timing): the maximum length of time, in seconds, that the page shall be displayed during presentations before the viewer application shall automatically advance to the next page.</li><li>$this->pagedim[$this->page]['trans']['S'] = transition style : Split, Blinds, Box, Wipe, Dissolve, Glitter, R, Fly, Push, Cover, Uncover, Fade</li><li>$this->pagedim[$this->page]['trans']['D'] = The duration of the transition effect, in seconds.</li><li>$this->pagedim[$this->page]['trans']['Dm'] = (Split and Blinds transition styles only) The dimension in which the specified transition effect shall occur: H = Horizontal, V = Vertical. Default value: H.</li><li>$this->pagedim[$this->page]['trans']['M'] = (Split, Box and Fly transition styles only) The direction of motion for the specified transition effect: I = Inward from the edges of the page, O = Outward from the center of the pageDefault value: I.</li><li>$this->pagedim[$this->page]['trans']['Di'] = (Wipe, Glitter, Fly, Cover, Uncover and Push transition styles only) The direction in which the specified transition effect shall moves, expressed in degrees counterclockwise starting from a left-to-right direction. If the value is a number, it shall be one of: 0 = Left to right, 90 = Bottom to top (Wipe only), 180 = Right to left (Wipe only), 270 = Top to bottom, 315 = Top-left to bottom-right (Glitter only). If the value is a name, it shall be None, which is relevant only for the Fly transition when the value of SS is not 1.0. Default value: 0.</li><li>$this->pagedim[$this->page]['trans']['SS'] = (Fly transition style only) The starting or ending scale at which the changes shall be drawn. If M specifies an inward transition, the scale of the changes drawn shall progress from SS to 1.0 over the course of the transition. If M specifies an outward transition, the scale of the changes drawn shall progress from 1.0 to SS over the course of the transition. Default: 1.0. </li><li>$this->pagedim[$this->page]['trans']['B'] = (Fly transition style only) If true, the area that shall be flown in is rectangular and opaque. Default: false.</li></ul></li><li>$this->pagedim[$this->page]['MediaBox'] : the boundaries of the physical medium on which the page shall be displayed or printed<ul><li>$this->pagedim[$this->page]['MediaBox']['llx'] = lower-left x coordinate in points</li><li>$this->pagedim[$this->page]['MediaBox']['lly'] = lower-left y coordinate in points</li><li>$this->pagedim[$this->page]['MediaBox']['urx'] = upper-right x coordinate in points</li><li>$this->pagedim[$this->page]['MediaBox']['ury'] = upper-right y coordinate in points</li></ul></li><li>$this->pagedim[$this->page]['CropBox'] : the visible region of default user space<ul><li>$this->pagedim[$this->page]['CropBox']['llx'] = lower-left x coordinate in points</li><li>$this->pagedim[$this->page]['CropBox']['lly'] = lower-left y coordinate in points</li><li>$this->pagedim[$this->page]['CropBox']['urx'] = upper-right x coordinate in points</li><li>$this->pagedim[$this->page]['CropBox']['ury'] = upper-right y coordinate in points</li></ul></li><li>$this->pagedim[$this->page]['BleedBox'] : the region to which the contents of the page shall be clipped when output in a production environment<ul><li>$this->pagedim[$this->page]['BleedBox']['llx'] = lower-left x coordinate in points</li><li>$this->pagedim[$this->page]['BleedBox']['lly'] = lower-left y coordinate in points</li><li>$this->pagedim[$this->page]['BleedBox']['urx'] = upper-right x coordinate in points</li><li>$this->pagedim[$this->page]['BleedBox']['ury'] = upper-right y coordinate in points</li></ul></li><li>$this->pagedim[$this->page]['TrimBox'] : the intended dimensions of the finished page after trimming<ul><li>$this->pagedim[$this->page]['TrimBox']['llx'] = lower-left x coordinate in points</li><li>$this->pagedim[$this->page]['TrimBox']['lly'] = lower-left y coordinate in points</li><li>$this->pagedim[$this->page]['TrimBox']['urx'] = upper-right x coordinate in points</li><li>$this->pagedim[$this->page]['TrimBox']['ury'] = upper-right y coordinate in points</li></ul></li><li>$this->pagedim[$this->page]['ArtBox'] : the extent of the page's meaningful content<ul><li>$this->pagedim[$this->page]['ArtBox']['llx'] = lower-left x coordinate in points</li><li>$this->pagedim[$this->page]['ArtBox']['lly'] = lower-left y coordinate in points</li><li>$this->pagedim[$this->page]['ArtBox']['urx'] = upper-right x coordinate in points</li><li>$this->pagedim[$this->page]['ArtBox']['ury'] = upper-right y coordinate in points</li></ul></li></ul>
  2250. * @param int|null $pagenum page number (empty = current page)
  2251. * @return array of page dimensions.
  2252. * @author Nicola Asuni
  2253. * @public
  2254. * @since 4.5.027 (2009-03-16)
  2255. */
  2256. public function getPageDimensions($pagenum=null) {
  2257. if (empty($pagenum)) {
  2258. $pagenum = $this->page;
  2259. }
  2260. return $this->pagedim[$pagenum];
  2261. }
  2262. /**
  2263. * Returns the page width in units.
  2264. * @param int|null $pagenum page number (empty = current page)
  2265. * @return int|float page width.
  2266. * @author Nicola Asuni
  2267. * @public
  2268. * @since 1.5.2
  2269. * @see getPageDimensions()
  2270. */
  2271. public function getPageWidth($pagenum=null) {
  2272. if (empty($pagenum)) {
  2273. return $this->w;
  2274. }
  2275. return $this->pagedim[$pagenum]['w'];
  2276. }
  2277. /**
  2278. * Returns the page height in units.
  2279. * @param int|null $pagenum page number (empty = current page)
  2280. * @return int|float page height.
  2281. * @author Nicola Asuni
  2282. * @public
  2283. * @since 1.5.2
  2284. * @see getPageDimensions()
  2285. */
  2286. public function getPageHeight($pagenum=null) {
  2287. if (empty($pagenum)) {
  2288. return $this->h;
  2289. }
  2290. return $this->pagedim[$pagenum]['h'];
  2291. }
  2292. /**
  2293. * Returns the page break margin.
  2294. * @param int|null $pagenum page number (empty = current page)
  2295. * @return int|float page break margin.
  2296. * @author Nicola Asuni
  2297. * @public
  2298. * @since 1.5.2
  2299. * @see getPageDimensions()
  2300. */
  2301. public function getBreakMargin($pagenum=null) {
  2302. if (empty($pagenum)) {
  2303. return $this->bMargin;
  2304. }
  2305. return $this->pagedim[$pagenum]['bm'];
  2306. }
  2307. /**
  2308. * Returns the scale factor (number of points in user unit).
  2309. * @return int scale factor.
  2310. * @author Nicola Asuni
  2311. * @public
  2312. * @since 1.5.2
  2313. */
  2314. public function getScaleFactor() {
  2315. return $this->k;
  2316. }
  2317. /**
  2318. * Defines the left, top and right margins.
  2319. * @param int|float $left Left margin.
  2320. * @param int|float $top Top margin.
  2321. * @param int|float|null $right Right margin. Default value is the left one.
  2322. * @param boolean $keepmargins if true overwrites the default page margins
  2323. * @public
  2324. * @since 1.0
  2325. * @see SetLeftMargin(), SetTopMargin(), SetRightMargin(), SetAutoPageBreak()
  2326. */
  2327. public function setMargins($left, $top, $right=null, $keepmargins=false) {
  2328. //Set left, top and right margins
  2329. $this->lMargin = $left;
  2330. $this->tMargin = $top;
  2331. if ($right == -1 OR $right === null) {
  2332. $right = $left;
  2333. }
  2334. $this->rMargin = $right;
  2335. if ($keepmargins) {
  2336. // overwrite original values
  2337. $this->original_lMargin = $this->lMargin;
  2338. $this->original_rMargin = $this->rMargin;
  2339. }
  2340. }
  2341. /**
  2342. * Defines the left margin. The method can be called before creating the first page. If the current abscissa gets out of page, it is brought back to the margin.
  2343. * @param int|float $margin The margin.
  2344. * @public
  2345. * @since 1.4
  2346. * @see SetTopMargin(), SetRightMargin(), SetAutoPageBreak(), SetMargins()
  2347. */
  2348. public function setLeftMargin($margin) {
  2349. //Set left margin
  2350. $this->lMargin = $margin;
  2351. if (($this->page > 0) AND ($this->x < $margin)) {
  2352. $this->x = $margin;
  2353. }
  2354. }
  2355. /**
  2356. * Defines the top margin. The method can be called before creating the first page.
  2357. * @param int|float $margin The margin.
  2358. * @public
  2359. * @since 1.5
  2360. * @see SetLeftMargin(), SetRightMargin(), SetAutoPageBreak(), SetMargins()
  2361. */
  2362. public function setTopMargin($margin) {
  2363. //Set top margin
  2364. $this->tMargin = $margin;
  2365. if (($this->page > 0) AND ($this->y < $margin)) {
  2366. $this->y = $margin;
  2367. }
  2368. }
  2369. /**
  2370. * Defines the right margin. The method can be called before creating the first page.
  2371. * @param int|float $margin The margin.
  2372. * @public
  2373. * @since 1.5
  2374. * @see SetLeftMargin(), SetTopMargin(), SetAutoPageBreak(), SetMargins()
  2375. */
  2376. public function setRightMargin($margin) {
  2377. $this->rMargin = $margin;
  2378. if (($this->page > 0) AND ($this->x > ($this->w - $margin))) {
  2379. $this->x = $this->w - $margin;
  2380. }
  2381. }
  2382. /**
  2383. * Set the same internal Cell padding for top, right, bottom, left-
  2384. * @param int|float $pad internal padding.
  2385. * @public
  2386. * @since 2.1.000 (2008-01-09)
  2387. * @see getCellPaddings(), setCellPaddings()
  2388. */
  2389. public function setCellPadding($pad) {
  2390. if ($pad >= 0) {
  2391. $this->cell_padding['L'] = $pad;
  2392. $this->cell_padding['T'] = $pad;
  2393. $this->cell_padding['R'] = $pad;
  2394. $this->cell_padding['B'] = $pad;
  2395. }
  2396. }
  2397. /**
  2398. * Set the internal Cell paddings.
  2399. * @param int|float|null $left left padding
  2400. * @param int|float|null $top top padding
  2401. * @param int|float|null $right right padding
  2402. * @param int|float|null $bottom bottom padding
  2403. * @public
  2404. * @since 5.9.000 (2010-10-03)
  2405. * @see getCellPaddings(), SetCellPadding()
  2406. */
  2407. public function setCellPaddings($left=null, $top=null, $right=null, $bottom=null) {
  2408. if (!TCPDF_STATIC::empty_string($left) AND ($left >= 0)) {
  2409. $this->cell_padding['L'] = $left;
  2410. }
  2411. if (!TCPDF_STATIC::empty_string($top) AND ($top >= 0)) {
  2412. $this->cell_padding['T'] = $top;
  2413. }
  2414. if (!TCPDF_STATIC::empty_string($right) AND ($right >= 0)) {
  2415. $this->cell_padding['R'] = $right;
  2416. }
  2417. if (!TCPDF_STATIC::empty_string($bottom) AND ($bottom >= 0)) {
  2418. $this->cell_padding['B'] = $bottom;
  2419. }
  2420. }
  2421. /**
  2422. * Get the internal Cell padding array.
  2423. * @return array of padding values
  2424. * @public
  2425. * @since 5.9.000 (2010-10-03)
  2426. * @see setCellPaddings(), SetCellPadding()
  2427. */
  2428. public function getCellPaddings() {
  2429. return $this->cell_padding;
  2430. }
  2431. /**
  2432. * Set the internal Cell margins.
  2433. * @param int|float|null $left left margin
  2434. * @param int|float|null $top top margin
  2435. * @param int|float|null $right right margin
  2436. * @param int|float|null $bottom bottom margin
  2437. * @public
  2438. * @since 5.9.000 (2010-10-03)
  2439. * @see getCellMargins()
  2440. */
  2441. public function setCellMargins($left=null, $top=null, $right=null, $bottom=null) {
  2442. if (!TCPDF_STATIC::empty_string($left) AND ($left >= 0)) {
  2443. $this->cell_margin['L'] = $left;
  2444. }
  2445. if (!TCPDF_STATIC::empty_string($top) AND ($top >= 0)) {
  2446. $this->cell_margin['T'] = $top;
  2447. }
  2448. if (!TCPDF_STATIC::empty_string($right) AND ($right >= 0)) {
  2449. $this->cell_margin['R'] = $right;
  2450. }
  2451. if (!TCPDF_STATIC::empty_string($bottom) AND ($bottom >= 0)) {
  2452. $this->cell_margin['B'] = $bottom;
  2453. }
  2454. }
  2455. /**
  2456. * Get the internal Cell margin array.
  2457. * @return array of margin values
  2458. * @public
  2459. * @since 5.9.000 (2010-10-03)
  2460. * @see setCellMargins()
  2461. */
  2462. public function getCellMargins() {
  2463. return $this->cell_margin;
  2464. }
  2465. /**
  2466. * Adjust the internal Cell padding array to take account of the line width.
  2467. * @param string|array|int|bool $brd Indicates if borders must be drawn around the cell. The value can be a number:<ul><li>0: no border (default)</li><li>1: frame</li></ul> or a string containing some or all of the following characters (in any order):<ul><li>L: left</li><li>T: top</li><li>R: right</li><li>B: bottom</li></ul> or an array of line styles for each border group - for example: array('LTRB' => array('width' => 2, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)))
  2468. * @return void|array array of adjustments
  2469. * @public
  2470. * @since 5.9.000 (2010-10-03)
  2471. */
  2472. protected function adjustCellPadding($brd=0) {
  2473. if (empty($brd)) {
  2474. return;
  2475. }
  2476. if (is_string($brd)) {
  2477. // convert string to array
  2478. $slen = strlen($brd);
  2479. $newbrd = array();
  2480. for ($i = 0; $i < $slen; ++$i) {
  2481. $newbrd[$brd[$i]] = true;
  2482. }
  2483. $brd = $newbrd;
  2484. } elseif (
  2485. ($brd === 1)
  2486. || ($brd === true)
  2487. || (is_numeric($brd) && ((int)$brd > 0))
  2488. ) {
  2489. $brd = array('LRTB' => true);
  2490. }
  2491. if (!is_array($brd)) {
  2492. return;
  2493. }
  2494. // store current cell padding
  2495. $cp = $this->cell_padding;
  2496. // select border mode
  2497. if (isset($brd['mode'])) {
  2498. $mode = $brd['mode'];
  2499. unset($brd['mode']);
  2500. } else {
  2501. $mode = 'normal';
  2502. }
  2503. // process borders
  2504. foreach ($brd as $border => $style) {
  2505. $line_width = $this->LineWidth;
  2506. if (is_array($style) && isset($style['width'])) {
  2507. // get border width
  2508. $line_width = $style['width'];
  2509. }
  2510. $adj = 0; // line width inside the cell
  2511. switch ($mode) {
  2512. case 'ext': {
  2513. $adj = 0;
  2514. break;
  2515. }
  2516. case 'int': {
  2517. $adj = $line_width;
  2518. break;
  2519. }
  2520. case 'normal':
  2521. default: {
  2522. $adj = ($line_width / 2);
  2523. break;
  2524. }
  2525. }
  2526. // correct internal cell padding if required to avoid overlap between text and lines
  2527. if (
  2528. is_numeric($this->cell_padding['T'])
  2529. && ($this->cell_padding['T'] < $adj)
  2530. && (strpos($border, 'T') !== false)
  2531. ) {
  2532. $this->cell_padding['T'] = $adj;
  2533. }
  2534. if (
  2535. is_numeric($this->cell_padding['R'])
  2536. && ($this->cell_padding['R'] < $adj)
  2537. && (strpos($border, 'R') !== false)
  2538. ) {
  2539. $this->cell_padding['R'] = $adj;
  2540. }
  2541. if (
  2542. is_numeric($this->cell_padding['B'])
  2543. && ($this->cell_padding['B'] < $adj)
  2544. && (strpos($border, 'B') !== false)
  2545. ) {
  2546. $this->cell_padding['B'] = $adj;
  2547. }
  2548. if (
  2549. is_numeric($this->cell_padding['L'])
  2550. && ($this->cell_padding['L'] < $adj)
  2551. && (strpos($border, 'L') !== false)
  2552. ) {
  2553. $this->cell_padding['L'] = $adj;
  2554. }
  2555. }
  2556. return array(
  2557. 'T' => ($this->cell_padding['T'] - $cp['T']),
  2558. 'R' => ($this->cell_padding['R'] - $cp['R']),
  2559. 'B' => ($this->cell_padding['B'] - $cp['B']),
  2560. 'L' => ($this->cell_padding['L'] - $cp['L']),
  2561. );
  2562. }
  2563. /**
  2564. * Enables or disables the automatic page breaking mode. When enabling, the second parameter is the distance from the bottom of the page that defines the triggering limit. By default, the mode is on and the margin is 2 cm.
  2565. * @param boolean $auto Boolean indicating if mode should be on or off.
  2566. * @param float $margin Distance from the bottom of the page.
  2567. * @public
  2568. * @since 1.0
  2569. * @see Cell(), MultiCell(), AcceptPageBreak()
  2570. */
  2571. public function setAutoPageBreak($auto, $margin=0) {
  2572. $this->AutoPageBreak = $auto ? true : false;
  2573. $this->bMargin = $margin;
  2574. $this->PageBreakTrigger = $this->h - $margin;
  2575. }
  2576. /**
  2577. * Return the auto-page-break mode (true or false).
  2578. * @return bool auto-page-break mode
  2579. * @public
  2580. * @since 5.9.088
  2581. */
  2582. public function getAutoPageBreak() {
  2583. return $this->AutoPageBreak;
  2584. }
  2585. /**
  2586. * Defines the way the document is to be displayed by the viewer.
  2587. * @param mixed $zoom The zoom to use. It can be one of the following string values or a number indicating the zooming factor to use. <ul><li>fullpage: displays the entire page on screen </li><li>fullwidth: uses maximum width of window</li><li>real: uses real size (equivalent to 100% zoom)</li><li>default: uses viewer default mode</li></ul>
  2588. * @param string $layout The page layout. Possible values are:<ul><li>SinglePage Display one page at a time</li><li>OneColumn Display the pages in one column</li><li>TwoColumnLeft Display the pages in two columns, with odd-numbered pages on the left</li><li>TwoColumnRight Display the pages in two columns, with odd-numbered pages on the right</li><li>TwoPageLeft (PDF 1.5) Display the pages two at a time, with odd-numbered pages on the left</li><li>TwoPageRight (PDF 1.5) Display the pages two at a time, with odd-numbered pages on the right</li></ul>
  2589. * @param string $mode A name object specifying how the document should be displayed when opened:<ul><li>UseNone Neither document outline nor thumbnail images visible</li><li>UseOutlines Document outline visible</li><li>UseThumbs Thumbnail images visible</li><li>FullScreen Full-screen mode, with no menu bar, window controls, or any other window visible</li><li>UseOC (PDF 1.5) Optional content group panel visible</li><li>UseAttachments (PDF 1.6) Attachments panel visible</li></ul>
  2590. * @public
  2591. * @since 1.2
  2592. */
  2593. public function setDisplayMode($zoom, $layout='SinglePage', $mode='UseNone') {
  2594. if (($zoom == 'fullpage') OR ($zoom == 'fullwidth') OR ($zoom == 'real') OR ($zoom == 'default') OR (!is_string($zoom))) {
  2595. $this->ZoomMode = $zoom;
  2596. } else {
  2597. $this->Error('Incorrect zoom display mode: '.$zoom);
  2598. }
  2599. $this->LayoutMode = TCPDF_STATIC::getPageLayoutMode($layout);
  2600. $this->PageMode = TCPDF_STATIC::getPageMode($mode);
  2601. }
  2602. /**
  2603. * Activates or deactivates page compression. When activated, the internal representation of each page is compressed, which leads to a compression ratio of about 2 for the resulting document. Compression is on by default.
  2604. * Note: the Zlib extension is required for this feature. If not present, compression will be turned off.
  2605. * @param boolean $compress Boolean indicating if compression must be enabled.
  2606. * @public
  2607. * @since 1.4
  2608. */
  2609. public function setCompression($compress=true) {
  2610. $this->compress = false;
  2611. if (function_exists('gzcompress')) {
  2612. if ($compress) {
  2613. if ( !$this->pdfa_mode) {
  2614. $this->compress = true;
  2615. }
  2616. }
  2617. }
  2618. }
  2619. /**
  2620. * Set flag to force sRGB_IEC61966-2.1 black scaled ICC color profile for the whole document.
  2621. * @param boolean $mode If true force sRGB output intent.
  2622. * @public
  2623. * @since 5.9.121 (2011-09-28)
  2624. */
  2625. public function setSRGBmode($mode=false) {
  2626. $this->force_srgb = $mode ? true : false;
  2627. }
  2628. /**
  2629. * Turn on/off Unicode mode for document information dictionary (meta tags).
  2630. * This has effect only when unicode mode is set to false.
  2631. * @param boolean $unicode if true set the meta information in Unicode
  2632. * @since 5.9.027 (2010-12-01)
  2633. * @public
  2634. */
  2635. public function setDocInfoUnicode($unicode=true) {
  2636. $this->docinfounicode = $unicode ? true : false;
  2637. }
  2638. /**
  2639. * Defines the title of the document.
  2640. * @param string $title The title.
  2641. * @public
  2642. * @since 1.2
  2643. * @see SetAuthor(), SetCreator(), SetKeywords(), SetSubject()
  2644. */
  2645. public function setTitle($title) {
  2646. $this->title = $title;
  2647. }
  2648. /**
  2649. * Defines the subject of the document.
  2650. * @param string $subject The subject.
  2651. * @public
  2652. * @since 1.2
  2653. * @see SetAuthor(), SetCreator(), SetKeywords(), SetTitle()
  2654. */
  2655. public function setSubject($subject) {
  2656. $this->subject = $subject;
  2657. }
  2658. /**
  2659. * Defines the author of the document.
  2660. * @param string $author The name of the author.
  2661. * @public
  2662. * @since 1.2
  2663. * @see SetCreator(), SetKeywords(), SetSubject(), SetTitle()
  2664. */
  2665. public function setAuthor($author) {
  2666. $this->author = $author;
  2667. }
  2668. /**
  2669. * Associates keywords with the document, generally in the form 'keyword1 keyword2 ...'.
  2670. * @param string $keywords The list of keywords.
  2671. * @public
  2672. * @since 1.2
  2673. * @see SetAuthor(), SetCreator(), SetSubject(), SetTitle()
  2674. */
  2675. public function setKeywords($keywords) {
  2676. $this->keywords = $keywords;
  2677. }
  2678. /**
  2679. * Defines the creator of the document. This is typically the name of the application that generates the PDF.
  2680. * @param string $creator The name of the creator.
  2681. * @public
  2682. * @since 1.2
  2683. * @see SetAuthor(), SetKeywords(), SetSubject(), SetTitle()
  2684. */
  2685. public function setCreator($creator) {
  2686. $this->creator = $creator;
  2687. }
  2688. /**
  2689. * Whether to allow local file path in image html tags, when prefixed with file://
  2690. *
  2691. * @param bool $allowLocalFiles true, when local files should be allowed. Otherwise false.
  2692. * @public
  2693. * @since 6.4
  2694. */
  2695. public function setAllowLocalFiles($allowLocalFiles) {
  2696. $this->allowLocalFiles = (bool) $allowLocalFiles;
  2697. }
  2698. /**
  2699. * Throw an exception or print an error message and die if the K_TCPDF_PARSER_THROW_EXCEPTION_ERROR constant is set to true.
  2700. * @param string $msg The error message
  2701. * @public
  2702. * @since 1.0
  2703. */
  2704. public function Error($msg) {
  2705. // unset all class variables
  2706. $this->_destroy(true);
  2707. $msg = htmlspecialchars($msg, ENT_QUOTES, 'UTF-8');
  2708. if (defined('K_TCPDF_THROW_EXCEPTION_ERROR') AND !K_TCPDF_THROW_EXCEPTION_ERROR) {
  2709. die('<strong>TCPDF ERROR: </strong>'.$msg);
  2710. } else {
  2711. throw new Exception('TCPDF ERROR: '.$msg);
  2712. }
  2713. }
  2714. /**
  2715. * This method begins the generation of the PDF document.
  2716. * It is not necessary to call it explicitly because AddPage() does it automatically.
  2717. * Note: no page is created by this method
  2718. * @public
  2719. * @since 1.0
  2720. * @see AddPage(), Close()
  2721. */
  2722. public function Open() {
  2723. $this->state = 1;
  2724. }
  2725. /**
  2726. * Terminates the PDF document.
  2727. * It is not necessary to call this method explicitly because Output() does it automatically.
  2728. * If the document contains no page, AddPage() is called to prevent from getting an invalid document.
  2729. * @public
  2730. * @since 1.0
  2731. * @see Open(), Output()
  2732. */
  2733. public function Close() {
  2734. if ($this->state == 3) {
  2735. return;
  2736. }
  2737. if ($this->page == 0) {
  2738. $this->AddPage();
  2739. }
  2740. $this->endLayer();
  2741. if ($this->tcpdflink) {
  2742. // save current graphic settings
  2743. $gvars = $this->getGraphicVars();
  2744. $this->setEqualColumns();
  2745. $this->lastpage(true);
  2746. $this->setAutoPageBreak(false);
  2747. $this->x = 0;
  2748. $this->y = $this->h - (1 / $this->k);
  2749. $this->lMargin = 0;
  2750. $this->_outSaveGraphicsState();
  2751. $font = defined('PDF_FONT_NAME_MAIN')?PDF_FONT_NAME_MAIN:'helvetica';
  2752. $this->setFont($font, '', 1);
  2753. $this->setTextRenderingMode(0, false, false);
  2754. $msg = "\x50\x6f\x77\x65\x72\x65\x64\x20\x62\x79\x20\x54\x43\x50\x44\x46\x20\x28\x77\x77\x77\x2e\x74\x63\x70\x64\x66\x2e\x6f\x72\x67\x29";
  2755. $lnk = "\x68\x74\x74\x70\x3a\x2f\x2f\x77\x77\x77\x2e\x74\x63\x70\x64\x66\x2e\x6f\x72\x67";
  2756. $this->Cell(0, 0, $msg, 0, 0, 'L', 0, $lnk, 0, false, 'D', 'B');
  2757. $this->_outRestoreGraphicsState();
  2758. // restore graphic settings
  2759. $this->setGraphicVars($gvars);
  2760. }
  2761. // close page
  2762. $this->endPage();
  2763. // close document
  2764. $this->_enddoc();
  2765. // unset all class variables (except critical ones)
  2766. $this->_destroy(false);
  2767. }
  2768. /**
  2769. * Move pointer at the specified document page and update page dimensions.
  2770. * @param int $pnum page number (1 ... numpages)
  2771. * @param boolean $resetmargins if true reset left, right, top margins and Y position.
  2772. * @public
  2773. * @since 2.1.000 (2008-01-07)
  2774. * @see getPage(), lastpage(), getNumPages()
  2775. */
  2776. public function setPage($pnum, $resetmargins=false) {
  2777. if (($pnum == $this->page) AND ($this->state == 2)) {
  2778. return;
  2779. }
  2780. if (($pnum > 0) AND ($pnum <= $this->numpages)) {
  2781. $this->state = 2;
  2782. // save current graphic settings
  2783. //$gvars = $this->getGraphicVars();
  2784. $oldpage = $this->page;
  2785. $this->page = $pnum;
  2786. $this->wPt = $this->pagedim[$this->page]['w'];
  2787. $this->hPt = $this->pagedim[$this->page]['h'];
  2788. $this->w = $this->pagedim[$this->page]['wk'];
  2789. $this->h = $this->pagedim[$this->page]['hk'];
  2790. $this->tMargin = $this->pagedim[$this->page]['tm'];
  2791. $this->bMargin = $this->pagedim[$this->page]['bm'];
  2792. $this->original_lMargin = $this->pagedim[$this->page]['olm'];
  2793. $this->original_rMargin = $this->pagedim[$this->page]['orm'];
  2794. $this->AutoPageBreak = $this->pagedim[$this->page]['pb'];
  2795. $this->CurOrientation = $this->pagedim[$this->page]['or'];
  2796. $this->setAutoPageBreak($this->AutoPageBreak, $this->bMargin);
  2797. // restore graphic settings
  2798. //$this->setGraphicVars($gvars);
  2799. if ($resetmargins) {
  2800. $this->lMargin = $this->pagedim[$this->page]['olm'];
  2801. $this->rMargin = $this->pagedim[$this->page]['orm'];
  2802. $this->setY($this->tMargin);
  2803. } else {
  2804. // account for booklet mode
  2805. if ($this->pagedim[$this->page]['olm'] != $this->pagedim[$oldpage]['olm']) {
  2806. $deltam = $this->pagedim[$this->page]['olm'] - $this->pagedim[$this->page]['orm'];
  2807. $this->lMargin += $deltam;
  2808. $this->rMargin -= $deltam;
  2809. }
  2810. }
  2811. } else {
  2812. $this->Error('Wrong page number on setPage() function: '.$pnum);
  2813. }
  2814. }
  2815. /**
  2816. * Reset pointer to the last document page.
  2817. * @param boolean $resetmargins if true reset left, right, top margins and Y position.
  2818. * @public
  2819. * @since 2.0.000 (2008-01-04)
  2820. * @see setPage(), getPage(), getNumPages()
  2821. */
  2822. public function lastPage($resetmargins=false) {
  2823. $this->setPage($this->getNumPages(), $resetmargins);
  2824. }
  2825. /**
  2826. * Get current document page number.
  2827. * @return int page number
  2828. * @public
  2829. * @since 2.1.000 (2008-01-07)
  2830. * @see setPage(), lastpage(), getNumPages()
  2831. */
  2832. public function getPage() {
  2833. return $this->page;
  2834. }
  2835. /**
  2836. * Get the total number of insered pages.
  2837. * @return int number of pages
  2838. * @public
  2839. * @since 2.1.000 (2008-01-07)
  2840. * @see setPage(), getPage(), lastpage()
  2841. */
  2842. public function getNumPages() {
  2843. return $this->numpages;
  2844. }
  2845. /**
  2846. * Adds a new TOC (Table Of Content) page to the document.
  2847. * @param string $orientation page orientation.
  2848. * @param mixed $format The format used for pages. It can be either: one of the string values specified at getPageSizeFromFormat() or an array of parameters specified at setPageFormat().
  2849. * @param boolean $keepmargins if true overwrites the default page margins with the current margins
  2850. * @public
  2851. * @since 5.0.001 (2010-05-06)
  2852. * @see AddPage(), startPage(), endPage(), endTOCPage()
  2853. */
  2854. public function addTOCPage($orientation='', $format='', $keepmargins=false) {
  2855. $this->AddPage($orientation, $format, $keepmargins, true);
  2856. }
  2857. /**
  2858. * Terminate the current TOC (Table Of Content) page
  2859. * @public
  2860. * @since 5.0.001 (2010-05-06)
  2861. * @see AddPage(), startPage(), endPage(), addTOCPage()
  2862. */
  2863. public function endTOCPage() {
  2864. $this->endPage(true);
  2865. }
  2866. /**
  2867. * Adds a new page to the document. If a page is already present, the Footer() method is called first to output the footer (if enabled). Then the page is added, the current position set to the top-left corner according to the left and top margins (or top-right if in RTL mode), and Header() is called to display the header (if enabled).
  2868. * The origin of the coordinate system is at the top-left corner (or top-right for RTL) and increasing ordinates go downwards.
  2869. * @param string $orientation page orientation. Possible values are (case insensitive):<ul><li>P or PORTRAIT (default)</li><li>L or LANDSCAPE</li></ul>
  2870. * @param mixed $format The format used for pages. It can be either: one of the string values specified at getPageSizeFromFormat() or an array of parameters specified at setPageFormat().
  2871. * @param boolean $keepmargins if true overwrites the default page margins with the current margins
  2872. * @param boolean $tocpage if true set the tocpage state to true (the added page will be used to display Table Of Content).
  2873. * @public
  2874. * @since 1.0
  2875. * @see startPage(), endPage(), addTOCPage(), endTOCPage(), getPageSizeFromFormat(), setPageFormat()
  2876. */
  2877. public function AddPage($orientation='', $format='', $keepmargins=false, $tocpage=false) {
  2878. if ($this->inxobj) {
  2879. // we are inside an XObject template
  2880. return;
  2881. }
  2882. if (!isset($this->original_lMargin) OR $keepmargins) {
  2883. $this->original_lMargin = $this->lMargin;
  2884. }
  2885. if (!isset($this->original_rMargin) OR $keepmargins) {
  2886. $this->original_rMargin = $this->rMargin;
  2887. }
  2888. // terminate previous page
  2889. $this->endPage();
  2890. // start new page
  2891. $this->startPage($orientation, $format, $tocpage);
  2892. }
  2893. /**
  2894. * Terminate the current page
  2895. * @param boolean $tocpage if true set the tocpage state to false (end the page used to display Table Of Content).
  2896. * @public
  2897. * @since 4.2.010 (2008-11-14)
  2898. * @see AddPage(), startPage(), addTOCPage(), endTOCPage()
  2899. */
  2900. public function endPage($tocpage=false) {
  2901. // check if page is already closed
  2902. if (($this->page == 0) OR ($this->numpages > $this->page) OR (!$this->pageopen[$this->page])) {
  2903. return;
  2904. }
  2905. // print page footer
  2906. $this->setFooter();
  2907. // close page
  2908. $this->_endpage();
  2909. // mark page as closed
  2910. $this->pageopen[$this->page] = false;
  2911. if ($tocpage) {
  2912. $this->tocpage = false;
  2913. }
  2914. }
  2915. /**
  2916. * Starts a new page to the document. The page must be closed using the endPage() function.
  2917. * The origin of the coordinate system is at the top-left corner and increasing ordinates go downwards.
  2918. * @param string $orientation page orientation. Possible values are (case insensitive):<ul><li>P or PORTRAIT (default)</li><li>L or LANDSCAPE</li></ul>
  2919. * @param mixed $format The format used for pages. It can be either: one of the string values specified at getPageSizeFromFormat() or an array of parameters specified at setPageFormat().
  2920. * @param boolean $tocpage if true the page is designated to contain the Table-Of-Content.
  2921. * @since 4.2.010 (2008-11-14)
  2922. * @see AddPage(), endPage(), addTOCPage(), endTOCPage(), getPageSizeFromFormat(), setPageFormat()
  2923. * @public
  2924. */
  2925. public function startPage($orientation='', $format='', $tocpage=false) {
  2926. if ($tocpage) {
  2927. $this->tocpage = true;
  2928. }
  2929. // move page numbers of documents to be attached
  2930. if ($this->tocpage) {
  2931. // move reference to unexistent pages (used for page attachments)
  2932. // adjust outlines
  2933. $tmpoutlines = $this->outlines;
  2934. foreach ($tmpoutlines as $key => $outline) {
  2935. if (!$outline['f'] AND ($outline['p'] > $this->numpages)) {
  2936. $this->outlines[$key]['p'] = ($outline['p'] + 1);
  2937. }
  2938. }
  2939. // adjust dests
  2940. $tmpdests = $this->dests;
  2941. foreach ($tmpdests as $key => $dest) {
  2942. if (!$dest['f'] AND ($dest['p'] > $this->numpages)) {
  2943. $this->dests[$key]['p'] = ($dest['p'] + 1);
  2944. }
  2945. }
  2946. // adjust links
  2947. $tmplinks = $this->links;
  2948. foreach ($tmplinks as $key => $link) {
  2949. if (!$link['f'] AND ($link['p'] > $this->numpages)) {
  2950. $this->links[$key]['p'] = ($link['p'] + 1);
  2951. }
  2952. }
  2953. }
  2954. if ($this->numpages > $this->page) {
  2955. // this page has been already added
  2956. $this->setPage($this->page + 1);
  2957. $this->setY($this->tMargin);
  2958. return;
  2959. }
  2960. // start a new page
  2961. if ($this->state == 0) {
  2962. $this->Open();
  2963. }
  2964. ++$this->numpages;
  2965. $this->swapMargins($this->booklet);
  2966. // save current graphic settings
  2967. $gvars = $this->getGraphicVars();
  2968. // start new page
  2969. $this->_beginpage($orientation, $format);
  2970. // mark page as open
  2971. $this->pageopen[$this->page] = true;
  2972. // restore graphic settings
  2973. $this->setGraphicVars($gvars);
  2974. // mark this point
  2975. $this->setPageMark();
  2976. // print page header
  2977. $this->setHeader();
  2978. // restore graphic settings
  2979. $this->setGraphicVars($gvars);
  2980. // mark this point
  2981. $this->setPageMark();
  2982. // print table header (if any)
  2983. $this->setTableHeader();
  2984. // set mark for empty page check
  2985. $this->emptypagemrk[$this->page]= $this->pagelen[$this->page];
  2986. }
  2987. /**
  2988. * Set start-writing mark on current page stream used to put borders and fills.
  2989. * Borders and fills are always created after content and inserted on the position marked by this method.
  2990. * This function must be called after calling Image() function for a background image.
  2991. * Background images must be always inserted before calling Multicell() or WriteHTMLCell() or WriteHTML() functions.
  2992. * @public
  2993. * @since 4.0.016 (2008-07-30)
  2994. */
  2995. public function setPageMark() {
  2996. $this->intmrk[$this->page] = $this->pagelen[$this->page];
  2997. $this->bordermrk[$this->page] = $this->intmrk[$this->page];
  2998. $this->setContentMark();
  2999. }
  3000. /**
  3001. * Set start-writing mark on selected page.
  3002. * Borders and fills are always created after content and inserted on the position marked by this method.
  3003. * @param int $page page number (default is the current page)
  3004. * @protected
  3005. * @since 4.6.021 (2009-07-20)
  3006. */
  3007. protected function setContentMark($page=0) {
  3008. if ($page <= 0) {
  3009. $page = $this->page;
  3010. }
  3011. if (isset($this->footerlen[$page])) {
  3012. $this->cntmrk[$page] = $this->pagelen[$page] - $this->footerlen[$page];
  3013. } else {
  3014. $this->cntmrk[$page] = $this->pagelen[$page];
  3015. }
  3016. }
  3017. /**
  3018. * Set header data.
  3019. * @param string $ln header image logo
  3020. * @param int $lw header image logo width in mm
  3021. * @param string $ht string to print as title on document header
  3022. * @param string $hs string to print on document header
  3023. * @param int[] $tc RGB array color for text.
  3024. * @param int[] $lc RGB array color for line.
  3025. * @public
  3026. */
  3027. public function setHeaderData($ln='', $lw=0, $ht='', $hs='', $tc=array(0,0,0), $lc=array(0,0,0)) {
  3028. $this->header_logo = $ln;
  3029. $this->header_logo_width = $lw;
  3030. $this->header_title = $ht;
  3031. $this->header_string = $hs;
  3032. $this->header_text_color = $tc;
  3033. $this->header_line_color = $lc;
  3034. }
  3035. /**
  3036. * Set footer data.
  3037. * @param int[] $tc RGB array color for text.
  3038. * @param int[] $lc RGB array color for line.
  3039. * @public
  3040. */
  3041. public function setFooterData($tc=array(0,0,0), $lc=array(0,0,0)) {
  3042. $this->footer_text_color = $tc;
  3043. $this->footer_line_color = $lc;
  3044. }
  3045. /**
  3046. * Returns header data:
  3047. * <ul><li>$ret['logo'] = logo image</li><li>$ret['logo_width'] = width of the image logo in user units</li><li>$ret['title'] = header title</li><li>$ret['string'] = header description string</li></ul>
  3048. * @return array<string,mixed>
  3049. * @public
  3050. * @since 4.0.012 (2008-07-24)
  3051. */
  3052. public function getHeaderData() {
  3053. $ret = array();
  3054. $ret['logo'] = $this->header_logo;
  3055. $ret['logo_width'] = $this->header_logo_width;
  3056. $ret['title'] = $this->header_title;
  3057. $ret['string'] = $this->header_string;
  3058. $ret['text_color'] = $this->header_text_color;
  3059. $ret['line_color'] = $this->header_line_color;
  3060. return $ret;
  3061. }
  3062. /**
  3063. * Set header margin.
  3064. * (minimum distance between header and top page margin)
  3065. * @param float $hm distance in user units
  3066. * @public
  3067. */
  3068. public function setHeaderMargin($hm=10) {
  3069. $this->header_margin = $hm;
  3070. }
  3071. /**
  3072. * Returns header margin in user units.
  3073. * @return float
  3074. * @since 4.0.012 (2008-07-24)
  3075. * @public
  3076. */
  3077. public function getHeaderMargin() {
  3078. return $this->header_margin;
  3079. }
  3080. /**
  3081. * Set footer margin.
  3082. * (minimum distance between footer and bottom page margin)
  3083. * @param float $fm distance in user units
  3084. * @public
  3085. */
  3086. public function setFooterMargin($fm=10) {
  3087. $this->footer_margin = $fm;
  3088. }
  3089. /**
  3090. * Returns footer margin in user units.
  3091. * @return float
  3092. * @since 4.0.012 (2008-07-24)
  3093. * @public
  3094. */
  3095. public function getFooterMargin() {
  3096. return $this->footer_margin;
  3097. }
  3098. /**
  3099. * Set a flag to print page header.
  3100. * @param boolean $val set to true to print the page header (default), false otherwise.
  3101. * @public
  3102. */
  3103. public function setPrintHeader($val=true) {
  3104. $this->print_header = $val ? true : false;
  3105. }
  3106. /**
  3107. * Set a flag to print page footer.
  3108. * @param boolean $val set to true to print the page footer (default), false otherwise.
  3109. * @public
  3110. */
  3111. public function setPrintFooter($val=true) {
  3112. $this->print_footer = $val ? true : false;
  3113. }
  3114. /**
  3115. * Return the right-bottom (or left-bottom for RTL) corner X coordinate of last inserted image
  3116. * @return float
  3117. * @public
  3118. */
  3119. public function getImageRBX() {
  3120. return $this->img_rb_x;
  3121. }
  3122. /**
  3123. * Return the right-bottom (or left-bottom for RTL) corner Y coordinate of last inserted image
  3124. * @return float
  3125. * @public
  3126. */
  3127. public function getImageRBY() {
  3128. return $this->img_rb_y;
  3129. }
  3130. /**
  3131. * Reset the xobject template used by Header() method.
  3132. * @public
  3133. */
  3134. public function resetHeaderTemplate() {
  3135. $this->header_xobjid = false;
  3136. }
  3137. /**
  3138. * Set a flag to automatically reset the xobject template used by Header() method at each page.
  3139. * @param boolean $val set to true to reset Header xobject template at each page, false otherwise.
  3140. * @public
  3141. */
  3142. public function setHeaderTemplateAutoreset($val=true) {
  3143. $this->header_xobj_autoreset = $val ? true : false;
  3144. }
  3145. /**
  3146. * This method is used to render the page header.
  3147. * It is automatically called by AddPage() and could be overwritten in your own inherited class.
  3148. * @public
  3149. */
  3150. public function Header() {
  3151. if ($this->header_xobjid === false) {
  3152. // start a new XObject Template
  3153. $this->header_xobjid = $this->startTemplate($this->w, $this->tMargin);
  3154. $headerfont = $this->getHeaderFont();
  3155. $headerdata = $this->getHeaderData();
  3156. $this->y = $this->header_margin;
  3157. if ($this->rtl) {
  3158. $this->x = $this->w - $this->original_rMargin;
  3159. } else {
  3160. $this->x = $this->original_lMargin;
  3161. }
  3162. if (($headerdata['logo']) AND ($headerdata['logo'] != K_BLANK_IMAGE)) {
  3163. $imgtype = TCPDF_IMAGES::getImageFileType(K_PATH_IMAGES.$headerdata['logo']);
  3164. if (($imgtype == 'eps') OR ($imgtype == 'ai')) {
  3165. $this->ImageEps(K_PATH_IMAGES.$headerdata['logo'], '', '', $headerdata['logo_width']);
  3166. } elseif ($imgtype == 'svg') {
  3167. $this->ImageSVG(K_PATH_IMAGES.$headerdata['logo'], '', '', $headerdata['logo_width']);
  3168. } else {
  3169. $this->Image(K_PATH_IMAGES.$headerdata['logo'], '', '', $headerdata['logo_width']);
  3170. }
  3171. $imgy = $this->getImageRBY();
  3172. } else {
  3173. $imgy = $this->y;
  3174. }
  3175. $cell_height = $this->getCellHeight($headerfont[2] / $this->k);
  3176. // set starting margin for text data cell
  3177. if ($this->getRTL()) {
  3178. $header_x = $this->original_rMargin + ($headerdata['logo_width'] * 1.1);
  3179. } else {
  3180. $header_x = $this->original_lMargin + ($headerdata['logo_width'] * 1.1);
  3181. }
  3182. $cw = $this->w - $this->original_lMargin - $this->original_rMargin - ($headerdata['logo_width'] * 1.1);
  3183. $this->setTextColorArray($this->header_text_color);
  3184. // header title
  3185. $this->setFont($headerfont[0], 'B', $headerfont[2] + 1);
  3186. $this->setX($header_x);
  3187. $this->Cell($cw, $cell_height, $headerdata['title'], 0, 1, '', 0, '', 0);
  3188. // header string
  3189. $this->setFont($headerfont[0], $headerfont[1], $headerfont[2]);
  3190. $this->setX($header_x);
  3191. $this->MultiCell($cw, $cell_height, $headerdata['string'], 0, '', 0, 1, '', '', true, 0, false, true, 0, 'T', false);
  3192. // print an ending header line
  3193. $this->setLineStyle(array('width' => 0.85 / $this->k, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => $headerdata['line_color']));
  3194. $this->setY((2.835 / $this->k) + max($imgy, $this->y));
  3195. if ($this->rtl) {
  3196. $this->setX($this->original_rMargin);
  3197. } else {
  3198. $this->setX($this->original_lMargin);
  3199. }
  3200. $this->Cell(($this->w - $this->original_lMargin - $this->original_rMargin), 0, '', 'T', 0, 'C');
  3201. $this->endTemplate();
  3202. }
  3203. // print header template
  3204. $x = 0;
  3205. $dx = 0;
  3206. if (!$this->header_xobj_autoreset AND $this->booklet AND (($this->page % 2) == 0)) {
  3207. // adjust margins for booklet mode
  3208. $dx = ($this->original_lMargin - $this->original_rMargin);
  3209. }
  3210. if ($this->rtl) {
  3211. $x = $this->w + $dx;
  3212. } else {
  3213. $x = 0 + $dx;
  3214. }
  3215. $this->printTemplate($this->header_xobjid, $x, 0, 0, 0, '', '', false);
  3216. if ($this->header_xobj_autoreset) {
  3217. // reset header xobject template at each page
  3218. $this->header_xobjid = false;
  3219. }
  3220. }
  3221. /**
  3222. * This method is used to render the page footer.
  3223. * It is automatically called by AddPage() and could be overwritten in your own inherited class.
  3224. * @public
  3225. */
  3226. public function Footer() {
  3227. $cur_y = $this->y;
  3228. $this->setTextColorArray($this->footer_text_color);
  3229. //set style for cell border
  3230. $line_width = (0.85 / $this->k);
  3231. $this->setLineStyle(array('width' => $line_width, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => $this->footer_line_color));
  3232. //print document barcode
  3233. $barcode = $this->getBarcode();
  3234. if (!empty($barcode)) {
  3235. $this->Ln($line_width);
  3236. $barcode_width = round(($this->w - $this->original_lMargin - $this->original_rMargin) / 3);
  3237. $style = array(
  3238. 'position' => $this->rtl?'R':'L',
  3239. 'align' => $this->rtl?'R':'L',
  3240. 'stretch' => false,
  3241. 'fitwidth' => true,
  3242. 'cellfitalign' => '',
  3243. 'border' => false,
  3244. 'padding' => 0,
  3245. 'fgcolor' => array(0,0,0),
  3246. 'bgcolor' => false,
  3247. 'text' => false
  3248. );
  3249. $this->write1DBarcode($barcode, 'C128', '', $cur_y + $line_width, '', (($this->footer_margin / 3) - $line_width), 0.3, $style, '');
  3250. }
  3251. $w_page = isset($this->l['w_page']) ? $this->l['w_page'].' ' : '';
  3252. if (empty($this->pagegroups)) {
  3253. $pagenumtxt = $w_page.$this->getAliasNumPage().' / '.$this->getAliasNbPages();
  3254. } else {
  3255. $pagenumtxt = $w_page.$this->getPageNumGroupAlias().' / '.$this->getPageGroupAlias();
  3256. }
  3257. $this->setY($cur_y);
  3258. //Print page number
  3259. if ($this->getRTL()) {
  3260. $this->setX($this->original_rMargin);
  3261. $this->Cell(0, 0, $pagenumtxt, 'T', 0, 'L');
  3262. } else {
  3263. $this->setX($this->original_lMargin);
  3264. $this->Cell(0, 0, $this->getAliasRightShift().$pagenumtxt, 'T', 0, 'R');
  3265. }
  3266. }
  3267. /**
  3268. * This method is used to render the page header.
  3269. * @protected
  3270. * @since 4.0.012 (2008-07-24)
  3271. */
  3272. protected function setHeader() {
  3273. if (!$this->print_header OR ($this->state != 2)) {
  3274. return;
  3275. }
  3276. $this->InHeader = true;
  3277. $this->setGraphicVars($this->default_graphic_vars);
  3278. $temp_thead = $this->thead;
  3279. $temp_theadMargins = $this->theadMargins;
  3280. $lasth = $this->lasth;
  3281. $newline = $this->newline;
  3282. $this->_outSaveGraphicsState();
  3283. $this->rMargin = $this->original_rMargin;
  3284. $this->lMargin = $this->original_lMargin;
  3285. $this->setCellPadding(0);
  3286. //set current position
  3287. if ($this->rtl) {
  3288. $this->setXY($this->original_rMargin, $this->header_margin);
  3289. } else {
  3290. $this->setXY($this->original_lMargin, $this->header_margin);
  3291. }
  3292. $this->setFont($this->header_font[0], $this->header_font[1], $this->header_font[2]);
  3293. $this->Header();
  3294. //restore position
  3295. if ($this->rtl) {
  3296. $this->setXY($this->original_rMargin, $this->tMargin);
  3297. } else {
  3298. $this->setXY($this->original_lMargin, $this->tMargin);
  3299. }
  3300. $this->_outRestoreGraphicsState();
  3301. $this->lasth = $lasth;
  3302. $this->thead = $temp_thead;
  3303. $this->theadMargins = $temp_theadMargins;
  3304. $this->newline = $newline;
  3305. $this->InHeader = false;
  3306. }
  3307. /**
  3308. * This method is used to render the page footer.
  3309. * @protected
  3310. * @since 4.0.012 (2008-07-24)
  3311. */
  3312. protected function setFooter() {
  3313. if ($this->state != 2) {
  3314. return;
  3315. }
  3316. $this->InFooter = true;
  3317. // save current graphic settings
  3318. $gvars = $this->getGraphicVars();
  3319. // mark this point
  3320. $this->footerpos[$this->page] = $this->pagelen[$this->page];
  3321. $this->_out("\n");
  3322. if ($this->print_footer) {
  3323. $this->setGraphicVars($this->default_graphic_vars);
  3324. $this->current_column = 0;
  3325. $this->num_columns = 1;
  3326. $temp_thead = $this->thead;
  3327. $temp_theadMargins = $this->theadMargins;
  3328. $lasth = $this->lasth;
  3329. $this->_outSaveGraphicsState();
  3330. $this->rMargin = $this->original_rMargin;
  3331. $this->lMargin = $this->original_lMargin;
  3332. $this->setCellPadding(0);
  3333. //set current position
  3334. $footer_y = $this->h - $this->footer_margin;
  3335. if ($this->rtl) {
  3336. $this->setXY($this->original_rMargin, $footer_y);
  3337. } else {
  3338. $this->setXY($this->original_lMargin, $footer_y);
  3339. }
  3340. $this->setFont($this->footer_font[0], $this->footer_font[1], $this->footer_font[2]);
  3341. $this->Footer();
  3342. //restore position
  3343. if ($this->rtl) {
  3344. $this->setXY($this->original_rMargin, $this->tMargin);
  3345. } else {
  3346. $this->setXY($this->original_lMargin, $this->tMargin);
  3347. }
  3348. $this->_outRestoreGraphicsState();
  3349. $this->lasth = $lasth;
  3350. $this->thead = $temp_thead;
  3351. $this->theadMargins = $temp_theadMargins;
  3352. }
  3353. // restore graphic settings
  3354. $this->setGraphicVars($gvars);
  3355. $this->current_column = $gvars['current_column'];
  3356. $this->num_columns = $gvars['num_columns'];
  3357. // calculate footer length
  3358. $this->footerlen[$this->page] = $this->pagelen[$this->page] - $this->footerpos[$this->page] + 1;
  3359. $this->InFooter = false;
  3360. }
  3361. /**
  3362. * Check if we are on the page body (excluding page header and footer).
  3363. * @return bool true if we are not in page header nor in page footer, false otherwise.
  3364. * @protected
  3365. * @since 5.9.091 (2011-06-15)
  3366. */
  3367. protected function inPageBody() {
  3368. return (($this->InHeader === false) AND ($this->InFooter === false));
  3369. }
  3370. /**
  3371. * This method is used to render the table header on new page (if any).
  3372. * @protected
  3373. * @since 4.5.030 (2009-03-25)
  3374. */
  3375. protected function setTableHeader() {
  3376. if ($this->num_columns > 1) {
  3377. // multi column mode
  3378. return;
  3379. }
  3380. if (isset($this->theadMargins['top'])) {
  3381. // restore the original top-margin
  3382. $this->tMargin = $this->theadMargins['top'];
  3383. $this->pagedim[$this->page]['tm'] = $this->tMargin;
  3384. $this->y = $this->tMargin;
  3385. }
  3386. if (!TCPDF_STATIC::empty_string($this->thead) AND (!$this->inthead)) {
  3387. // set margins
  3388. $prev_lMargin = $this->lMargin;
  3389. $prev_rMargin = $this->rMargin;
  3390. $prev_cell_padding = $this->cell_padding;
  3391. $this->lMargin = $this->theadMargins['lmargin'] + ($this->pagedim[$this->page]['olm'] - $this->pagedim[$this->theadMargins['page']]['olm']);
  3392. $this->rMargin = $this->theadMargins['rmargin'] + ($this->pagedim[$this->page]['orm'] - $this->pagedim[$this->theadMargins['page']]['orm']);
  3393. $this->cell_padding = $this->theadMargins['cell_padding'];
  3394. if ($this->rtl) {
  3395. $this->x = $this->w - $this->rMargin;
  3396. } else {
  3397. $this->x = $this->lMargin;
  3398. }
  3399. // account for special "cell" mode
  3400. if ($this->theadMargins['cell']) {
  3401. if ($this->rtl) {
  3402. $this->x -= $this->cell_padding['R'];
  3403. } else {
  3404. $this->x += $this->cell_padding['L'];
  3405. }
  3406. }
  3407. $gvars = $this->getGraphicVars();
  3408. if (!empty($this->theadMargins['gvars'])) {
  3409. // set the correct graphic style
  3410. $this->setGraphicVars($this->theadMargins['gvars']);
  3411. $this->rMargin = $gvars['rMargin'];
  3412. $this->lMargin = $gvars['lMargin'];
  3413. }
  3414. // print table header
  3415. $this->writeHTML($this->thead, false, false, false, false, '');
  3416. $this->setGraphicVars($gvars);
  3417. // set new top margin to skip the table headers
  3418. if (!isset($this->theadMargins['top'])) {
  3419. $this->theadMargins['top'] = $this->tMargin;
  3420. }
  3421. // store end of header position
  3422. if (!isset($this->columns[0]['th'])) {
  3423. $this->columns[0]['th'] = array();
  3424. }
  3425. $this->columns[0]['th']['\''.$this->page.'\''] = $this->y;
  3426. $this->tMargin = $this->y;
  3427. $this->pagedim[$this->page]['tm'] = $this->tMargin;
  3428. $this->lasth = 0;
  3429. $this->lMargin = $prev_lMargin;
  3430. $this->rMargin = $prev_rMargin;
  3431. $this->cell_padding = $prev_cell_padding;
  3432. }
  3433. }
  3434. /**
  3435. * Returns the current page number.
  3436. * @return int page number
  3437. * @public
  3438. * @since 1.0
  3439. * @see getAliasNbPages()
  3440. */
  3441. public function PageNo() {
  3442. return $this->page;
  3443. }
  3444. /**
  3445. * Returns the array of spot colors.
  3446. * @return array Spot colors array.
  3447. * @public
  3448. * @since 6.0.038 (2013-09-30)
  3449. */
  3450. public function getAllSpotColors() {
  3451. return $this->spot_colors;
  3452. }
  3453. /**
  3454. * Defines a new spot color.
  3455. * It can be expressed in RGB components or gray scale.
  3456. * The method can be called before the first page is created and the value is retained from page to page.
  3457. * @param string $name Full name of the spot color.
  3458. * @param float $c Cyan color for CMYK. Value between 0 and 100.
  3459. * @param float $m Magenta color for CMYK. Value between 0 and 100.
  3460. * @param float $y Yellow color for CMYK. Value between 0 and 100.
  3461. * @param float $k Key (Black) color for CMYK. Value between 0 and 100.
  3462. * @public
  3463. * @since 4.0.024 (2008-09-12)
  3464. * @see SetDrawSpotColor(), SetFillSpotColor(), SetTextSpotColor()
  3465. */
  3466. public function AddSpotColor($name, $c, $m, $y, $k) {
  3467. if (!isset($this->spot_colors[$name])) {
  3468. $i = (1 + count($this->spot_colors));
  3469. $this->spot_colors[$name] = array('C' => $c, 'M' => $m, 'Y' => $y, 'K' => $k, 'name' => $name, 'i' => $i);
  3470. }
  3471. }
  3472. /**
  3473. * Set the spot color for the specified type ('draw', 'fill', 'text').
  3474. * @param string $type Type of object affected by this color: ('draw', 'fill', 'text').
  3475. * @param string $name Name of the spot color.
  3476. * @param float $tint Intensity of the color (from 0 to 100 ; 100 = full intensity by default).
  3477. * @return string PDF color command.
  3478. * @public
  3479. * @since 5.9.125 (2011-10-03)
  3480. */
  3481. public function setSpotColor($type, $name, $tint=100) {
  3482. $spotcolor = TCPDF_COLORS::getSpotColor($name, $this->spot_colors);
  3483. if ($spotcolor === false) {
  3484. $this->Error('Undefined spot color: '.$name.', you must add it using the AddSpotColor() method.');
  3485. }
  3486. $tint = (max(0, min(100, $tint)) / 100);
  3487. $pdfcolor = sprintf('/CS%d ', $this->spot_colors[$name]['i']);
  3488. switch ($type) {
  3489. case 'draw': {
  3490. $pdfcolor .= sprintf('CS %F SCN', $tint);
  3491. $this->DrawColor = $pdfcolor;
  3492. $this->strokecolor = $spotcolor;
  3493. break;
  3494. }
  3495. case 'fill': {
  3496. $pdfcolor .= sprintf('cs %F scn', $tint);
  3497. $this->FillColor = $pdfcolor;
  3498. $this->bgcolor = $spotcolor;
  3499. break;
  3500. }
  3501. case 'text': {
  3502. $pdfcolor .= sprintf('cs %F scn', $tint);
  3503. $this->TextColor = $pdfcolor;
  3504. $this->fgcolor = $spotcolor;
  3505. break;
  3506. }
  3507. }
  3508. $this->ColorFlag = ($this->FillColor != $this->TextColor);
  3509. if ($this->state == 2) {
  3510. $this->_out($pdfcolor);
  3511. }
  3512. if ($this->inxobj) {
  3513. // we are inside an XObject template
  3514. $this->xobjects[$this->xobjid]['spot_colors'][$name] = $this->spot_colors[$name];
  3515. }
  3516. return $pdfcolor;
  3517. }
  3518. /**
  3519. * Defines the spot color used for all drawing operations (lines, rectangles and cell borders).
  3520. * @param string $name Name of the spot color.
  3521. * @param float $tint Intensity of the color (from 0 to 100 ; 100 = full intensity by default).
  3522. * @public
  3523. * @since 4.0.024 (2008-09-12)
  3524. * @see AddSpotColor(), SetFillSpotColor(), SetTextSpotColor()
  3525. */
  3526. public function setDrawSpotColor($name, $tint=100) {
  3527. $this->setSpotColor('draw', $name, $tint);
  3528. }
  3529. /**
  3530. * Defines the spot color used for all filling operations (filled rectangles and cell backgrounds).
  3531. * @param string $name Name of the spot color.
  3532. * @param float $tint Intensity of the color (from 0 to 100 ; 100 = full intensity by default).
  3533. * @public
  3534. * @since 4.0.024 (2008-09-12)
  3535. * @see AddSpotColor(), SetDrawSpotColor(), SetTextSpotColor()
  3536. */
  3537. public function setFillSpotColor($name, $tint=100) {
  3538. $this->setSpotColor('fill', $name, $tint);
  3539. }
  3540. /**
  3541. * Defines the spot color used for text.
  3542. * @param string $name Name of the spot color.
  3543. * @param int $tint Intensity of the color (from 0 to 100 ; 100 = full intensity by default).
  3544. * @public
  3545. * @since 4.0.024 (2008-09-12)
  3546. * @see AddSpotColor(), SetDrawSpotColor(), SetFillSpotColor()
  3547. */
  3548. public function setTextSpotColor($name, $tint=100) {
  3549. $this->setSpotColor('text', $name, $tint);
  3550. }
  3551. /**
  3552. * Set the color array for the specified type ('draw', 'fill', 'text').
  3553. * It can be expressed in RGB, CMYK or GRAY SCALE components.
  3554. * The method can be called before the first page is created and the value is retained from page to page.
  3555. * @param string $type Type of object affected by this color: ('draw', 'fill', 'text').
  3556. * @param array $color Array of colors (1=gray, 3=RGB, 4=CMYK or 5=spotcolor=CMYK+name values).
  3557. * @param boolean $ret If true do not send the PDF command.
  3558. * @return string The PDF command or empty string.
  3559. * @public
  3560. * @since 3.1.000 (2008-06-11)
  3561. */
  3562. public function setColorArray($type, $color, $ret=false) {
  3563. if (is_array($color)) {
  3564. $color = array_values($color);
  3565. // component: grey, RGB red or CMYK cyan
  3566. $c = isset($color[0]) ? $color[0] : -1;
  3567. // component: RGB green or CMYK magenta
  3568. $m = isset($color[1]) ? $color[1] : -1;
  3569. // component: RGB blue or CMYK yellow
  3570. $y = isset($color[2]) ? $color[2] : -1;
  3571. // component: CMYK black
  3572. $k = isset($color[3]) ? $color[3] : -1;
  3573. // color name
  3574. $name = isset($color[4]) ? $color[4] : '';
  3575. if ($c >= 0) {
  3576. return $this->setColor($type, $c, $m, $y, $k, $ret, $name);
  3577. }
  3578. }
  3579. return '';
  3580. }
  3581. /**
  3582. * Defines the color used for all drawing operations (lines, rectangles and cell borders).
  3583. * It can be expressed in RGB, CMYK or GRAY SCALE components.
  3584. * The method can be called before the first page is created and the value is retained from page to page.
  3585. * @param array $color Array of colors (1, 3 or 4 values).
  3586. * @param boolean $ret If true do not send the PDF command.
  3587. * @return string the PDF command
  3588. * @public
  3589. * @since 3.1.000 (2008-06-11)
  3590. * @see SetDrawColor()
  3591. */
  3592. public function setDrawColorArray($color, $ret=false) {
  3593. return $this->setColorArray('draw', $color, $ret);
  3594. }
  3595. /**
  3596. * Defines the color used for all filling operations (filled rectangles and cell backgrounds).
  3597. * It can be expressed in RGB, CMYK or GRAY SCALE components.
  3598. * The method can be called before the first page is created and the value is retained from page to page.
  3599. * @param array $color Array of colors (1, 3 or 4 values).
  3600. * @param boolean $ret If true do not send the PDF command.
  3601. * @public
  3602. * @since 3.1.000 (2008-6-11)
  3603. * @see SetFillColor()
  3604. */
  3605. public function setFillColorArray($color, $ret=false) {
  3606. return $this->setColorArray('fill', $color, $ret);
  3607. }
  3608. /**
  3609. * Defines the color used for text. It can be expressed in RGB components or gray scale.
  3610. * The method can be called before the first page is created and the value is retained from page to page.
  3611. * @param array $color Array of colors (1, 3 or 4 values).
  3612. * @param boolean $ret If true do not send the PDF command.
  3613. * @public
  3614. * @since 3.1.000 (2008-6-11)
  3615. * @see SetFillColor()
  3616. */
  3617. public function setTextColorArray($color, $ret=false) {
  3618. return $this->setColorArray('text', $color, $ret);
  3619. }
  3620. /**
  3621. * Defines the color used by the specified type ('draw', 'fill', 'text').
  3622. * @param string $type Type of object affected by this color: ('draw', 'fill', 'text').
  3623. * @param float $col1 GRAY level for single color, or Red color for RGB (0-255), or CYAN color for CMYK (0-100).
  3624. * @param float $col2 GREEN color for RGB (0-255), or MAGENTA color for CMYK (0-100).
  3625. * @param float $col3 BLUE color for RGB (0-255), or YELLOW color for CMYK (0-100).
  3626. * @param float $col4 KEY (BLACK) color for CMYK (0-100).
  3627. * @param boolean $ret If true do not send the command.
  3628. * @param string $name spot color name (if any)
  3629. * @return string The PDF command or empty string.
  3630. * @public
  3631. * @since 5.9.125 (2011-10-03)
  3632. */
  3633. public function setColor($type, $col1=0, $col2=-1, $col3=-1, $col4=-1, $ret=false, $name='') {
  3634. // set default values
  3635. if (!is_numeric($col1)) {
  3636. $col1 = 0;
  3637. }
  3638. if (!is_numeric($col2)) {
  3639. $col2 = -1;
  3640. }
  3641. if (!is_numeric($col3)) {
  3642. $col3 = -1;
  3643. }
  3644. if (!is_numeric($col4)) {
  3645. $col4 = -1;
  3646. }
  3647. // set color by case
  3648. $suffix = '';
  3649. if (($col2 == -1) AND ($col3 == -1) AND ($col4 == -1)) {
  3650. // Grey scale
  3651. $col1 = max(0, min(255, $col1));
  3652. $intcolor = array('G' => $col1);
  3653. $pdfcolor = sprintf('%F ', ($col1 / 255));
  3654. $suffix = 'g';
  3655. } elseif ($col4 == -1) {
  3656. // RGB
  3657. $col1 = max(0, min(255, $col1));
  3658. $col2 = max(0, min(255, $col2));
  3659. $col3 = max(0, min(255, $col3));
  3660. $intcolor = array('R' => $col1, 'G' => $col2, 'B' => $col3);
  3661. $pdfcolor = sprintf('%F %F %F ', ($col1 / 255), ($col2 / 255), ($col3 / 255));
  3662. $suffix = 'rg';
  3663. } else {
  3664. $col1 = max(0, min(100, $col1));
  3665. $col2 = max(0, min(100, $col2));
  3666. $col3 = max(0, min(100, $col3));
  3667. $col4 = max(0, min(100, $col4));
  3668. if (empty($name)) {
  3669. // CMYK
  3670. $intcolor = array('C' => $col1, 'M' => $col2, 'Y' => $col3, 'K' => $col4);
  3671. $pdfcolor = sprintf('%F %F %F %F ', ($col1 / 100), ($col2 / 100), ($col3 / 100), ($col4 / 100));
  3672. $suffix = 'k';
  3673. } else {
  3674. // SPOT COLOR
  3675. $intcolor = array('C' => $col1, 'M' => $col2, 'Y' => $col3, 'K' => $col4, 'name' => $name);
  3676. $this->AddSpotColor($name, $col1, $col2, $col3, $col4);
  3677. $pdfcolor = $this->setSpotColor($type, $name, 100);
  3678. }
  3679. }
  3680. switch ($type) {
  3681. case 'draw': {
  3682. $pdfcolor .= strtoupper($suffix);
  3683. $this->DrawColor = $pdfcolor;
  3684. $this->strokecolor = $intcolor;
  3685. break;
  3686. }
  3687. case 'fill': {
  3688. $pdfcolor .= $suffix;
  3689. $this->FillColor = $pdfcolor;
  3690. $this->bgcolor = $intcolor;
  3691. break;
  3692. }
  3693. case 'text': {
  3694. $pdfcolor .= $suffix;
  3695. $this->TextColor = $pdfcolor;
  3696. $this->fgcolor = $intcolor;
  3697. break;
  3698. }
  3699. }
  3700. $this->ColorFlag = ($this->FillColor != $this->TextColor);
  3701. if (($type != 'text') AND ($this->state == 2) AND $type !== 0) {
  3702. if (!$ret) {
  3703. $this->_out($pdfcolor);
  3704. }
  3705. return $pdfcolor;
  3706. }
  3707. return '';
  3708. }
  3709. /**
  3710. * Defines the color used for all drawing operations (lines, rectangles and cell borders). It can be expressed in RGB components or gray scale. The method can be called before the first page is created and the value is retained from page to page.
  3711. * @param float $col1 GRAY level for single color, or Red color for RGB (0-255), or CYAN color for CMYK (0-100).
  3712. * @param float $col2 GREEN color for RGB (0-255), or MAGENTA color for CMYK (0-100).
  3713. * @param float $col3 BLUE color for RGB (0-255), or YELLOW color for CMYK (0-100).
  3714. * @param float $col4 KEY (BLACK) color for CMYK (0-100).
  3715. * @param boolean $ret If true do not send the command.
  3716. * @param string $name spot color name (if any)
  3717. * @return string the PDF command
  3718. * @public
  3719. * @since 1.3
  3720. * @see SetDrawColorArray(), SetFillColor(), SetTextColor(), Line(), Rect(), Cell(), MultiCell()
  3721. */
  3722. public function setDrawColor($col1=0, $col2=-1, $col3=-1, $col4=-1, $ret=false, $name='') {
  3723. return $this->setColor('draw', $col1, $col2, $col3, $col4, $ret, $name);
  3724. }
  3725. /**
  3726. * Defines the color used for all filling operations (filled rectangles and cell backgrounds). It can be expressed in RGB components or gray scale. The method can be called before the first page is created and the value is retained from page to page.
  3727. * @param float $col1 GRAY level for single color, or Red color for RGB (0-255), or CYAN color for CMYK (0-100).
  3728. * @param float $col2 GREEN color for RGB (0-255), or MAGENTA color for CMYK (0-100).
  3729. * @param float $col3 BLUE color for RGB (0-255), or YELLOW color for CMYK (0-100).
  3730. * @param float $col4 KEY (BLACK) color for CMYK (0-100).
  3731. * @param boolean $ret If true do not send the command.
  3732. * @param string $name Spot color name (if any).
  3733. * @return string The PDF command.
  3734. * @public
  3735. * @since 1.3
  3736. * @see SetFillColorArray(), SetDrawColor(), SetTextColor(), Rect(), Cell(), MultiCell()
  3737. */
  3738. public function setFillColor($col1=0, $col2=-1, $col3=-1, $col4=-1, $ret=false, $name='') {
  3739. return $this->setColor('fill', $col1, $col2, $col3, $col4, $ret, $name);
  3740. }
  3741. /**
  3742. * Defines the color used for text. It can be expressed in RGB components or gray scale. The method can be called before the first page is created and the value is retained from page to page.
  3743. * @param float $col1 GRAY level for single color, or Red color for RGB (0-255), or CYAN color for CMYK (0-100).
  3744. * @param float $col2 GREEN color for RGB (0-255), or MAGENTA color for CMYK (0-100).
  3745. * @param float $col3 BLUE color for RGB (0-255), or YELLOW color for CMYK (0-100).
  3746. * @param float $col4 KEY (BLACK) color for CMYK (0-100).
  3747. * @param boolean $ret If true do not send the command.
  3748. * @param string $name Spot color name (if any).
  3749. * @return string Empty string.
  3750. * @public
  3751. * @since 1.3
  3752. * @see SetTextColorArray(), SetDrawColor(), SetFillColor(), Text(), Cell(), MultiCell()
  3753. */
  3754. public function setTextColor($col1=0, $col2=-1, $col3=-1, $col4=-1, $ret=false, $name='') {
  3755. return $this->setColor('text', $col1, $col2, $col3, $col4, $ret, $name);
  3756. }
  3757. /**
  3758. * Returns the length of a string in user unit. A font must be selected.<br>
  3759. * @param string $s The string whose length is to be computed
  3760. * @param string $fontname Family font. It can be either a name defined by AddFont() or one of the standard families. It is also possible to pass an empty string, in that case, the current family is retained.
  3761. * @param string $fontstyle Font style. Possible values are (case insensitive):<ul><li>empty string: regular</li><li>B: bold</li><li>I: italic</li><li>U: underline</li><li>D: line-through</li><li>O: overline</li></ul> or any combination. The default value is regular.
  3762. * @param float $fontsize Font size in points. The default value is the current size.
  3763. * @param boolean $getarray if true returns an array of characters widths, if false returns the total length.
  3764. * @return float[]|float total string length or array of characted widths
  3765. * @phpstan-return ($getarray is true ? float[] : float) total string length or array of characted widths
  3766. * @author Nicola Asuni
  3767. * @public
  3768. * @since 1.2
  3769. */
  3770. public function GetStringWidth($s, $fontname='', $fontstyle='', $fontsize=0, $getarray=false) {
  3771. return $this->GetArrStringWidth(TCPDF_FONTS::utf8Bidi(TCPDF_FONTS::UTF8StringToArray($s, $this->isunicode, $this->CurrentFont), $s, $this->tmprtl, $this->isunicode, $this->CurrentFont), $fontname, $fontstyle, $fontsize, $getarray);
  3772. }
  3773. /**
  3774. * Returns the string length of an array of chars in user unit or an array of characters widths. A font must be selected.<br>
  3775. * @param array $sa The array of chars whose total length is to be computed
  3776. * @param string $fontname Family font. It can be either a name defined by AddFont() or one of the standard families. It is also possible to pass an empty string, in that case, the current family is retained.
  3777. * @param string $fontstyle Font style. Possible values are (case insensitive):<ul><li>empty string: regular</li><li>B: bold</li><li>I: italic</li><li>U: underline</li><li>D: line through</li><li>O: overline</li></ul> or any combination. The default value is regular.
  3778. * @param float $fontsize Font size in points. The default value is the current size.
  3779. * @param boolean $getarray if true returns an array of characters widths, if false returns the total length.
  3780. * @return float[]|float total string length or array of characted widths
  3781. * @phpstan-return ($getarray is true ? float[] : float) total string length or array of characted widths
  3782. * @author Nicola Asuni
  3783. * @public
  3784. * @since 2.4.000 (2008-03-06)
  3785. */
  3786. public function GetArrStringWidth($sa, $fontname='', $fontstyle='', $fontsize=0, $getarray=false) {
  3787. // store current values
  3788. if (!TCPDF_STATIC::empty_string($fontname)) {
  3789. $prev_FontFamily = $this->FontFamily;
  3790. $prev_FontStyle = $this->FontStyle;
  3791. $prev_FontSizePt = $this->FontSizePt;
  3792. $this->setFont($fontname, $fontstyle, $fontsize, '', 'default', false);
  3793. }
  3794. // convert UTF-8 array to Latin1 if required
  3795. if ($this->isunicode AND (!$this->isUnicodeFont())) {
  3796. $sa = TCPDF_FONTS::UTF8ArrToLatin1Arr($sa);
  3797. }
  3798. $w = 0; // total width
  3799. $wa = array(); // array of characters widths
  3800. foreach ($sa as $ck => $char) {
  3801. // character width
  3802. $cw = $this->GetCharWidth($char, isset($sa[($ck + 1)]));
  3803. $wa[] = $cw;
  3804. $w += $cw;
  3805. }
  3806. // restore previous values
  3807. if (!TCPDF_STATIC::empty_string($fontname)) {
  3808. $this->setFont($prev_FontFamily, $prev_FontStyle, $prev_FontSizePt, '', 'default', false);
  3809. }
  3810. if ($getarray) {
  3811. return $wa;
  3812. }
  3813. return $w;
  3814. }
  3815. /**
  3816. * Returns the length of the char in user unit for the current font considering current stretching and spacing (tracking).
  3817. * @param int $char The char code whose length is to be returned
  3818. * @param boolean $notlast If false ignore the font-spacing.
  3819. * @return float char width
  3820. * @author Nicola Asuni
  3821. * @public
  3822. * @since 2.4.000 (2008-03-06)
  3823. */
  3824. public function GetCharWidth($char, $notlast=true) {
  3825. // get raw width
  3826. $chw = $this->getRawCharWidth($char);
  3827. if (($this->font_spacing < 0) OR (($this->font_spacing > 0) AND $notlast)) {
  3828. // increase/decrease font spacing
  3829. $chw += $this->font_spacing;
  3830. }
  3831. if ($this->font_stretching != 100) {
  3832. // fixed stretching mode
  3833. $chw *= ($this->font_stretching / 100);
  3834. }
  3835. return $chw;
  3836. }
  3837. /**
  3838. * Returns the length of the char in user unit for the current font.
  3839. * @param int $char The char code whose length is to be returned
  3840. * @return float char width
  3841. * @author Nicola Asuni
  3842. * @public
  3843. * @since 5.9.000 (2010-09-28)
  3844. */
  3845. public function getRawCharWidth($char) {
  3846. if ($char == 173) {
  3847. // SHY character will not be printed
  3848. return (0);
  3849. }
  3850. if (isset($this->CurrentFont['cw'][intval($char)])) {
  3851. $w = $this->CurrentFont['cw'][intval($char)];
  3852. } elseif (isset($this->CurrentFont['dw'])) {
  3853. // default width
  3854. $w = $this->CurrentFont['dw'];
  3855. } elseif (isset($this->CurrentFont['cw'][32])) {
  3856. // default width
  3857. $w = $this->CurrentFont['cw'][32];
  3858. } else {
  3859. $w = 600;
  3860. }
  3861. return $this->getAbsFontMeasure($w);
  3862. }
  3863. /**
  3864. * Returns the numbero of characters in a string.
  3865. * @param string $s The input string.
  3866. * @return int number of characters
  3867. * @public
  3868. * @since 2.0.0001 (2008-01-07)
  3869. */
  3870. public function GetNumChars($s) {
  3871. if ($this->isUnicodeFont()) {
  3872. return count(TCPDF_FONTS::UTF8StringToArray($s, $this->isunicode, $this->CurrentFont));
  3873. }
  3874. return strlen($s);
  3875. }
  3876. /**
  3877. * Fill the list of available fonts ($this->fontlist).
  3878. * @protected
  3879. * @since 4.0.013 (2008-07-28)
  3880. */
  3881. protected function getFontsList() {
  3882. if (($fontsdir = opendir(TCPDF_FONTS::_getfontpath())) !== false) {
  3883. while (($file = readdir($fontsdir)) !== false) {
  3884. if (substr($file, -4) == '.php') {
  3885. array_push($this->fontlist, strtolower(basename($file, '.php')));
  3886. }
  3887. }
  3888. closedir($fontsdir);
  3889. }
  3890. }
  3891. /**
  3892. * Imports a TrueType, Type1, core, or CID0 font and makes it available.
  3893. * It is necessary to generate a font definition file first (read /fonts/utils/README.TXT).
  3894. * The definition file (and the font file itself when embedding) must be present either in the current directory or in the one indicated by K_PATH_FONTS if the constant is defined. If it could not be found, the error "Could not include font definition file" is generated.
  3895. * @param string $family Font family. The name can be chosen arbitrarily. If it is a standard family name, it will override the corresponding font.
  3896. * @param string $style Font style. Possible values are (case insensitive):<ul><li>empty string: regular (default)</li><li>B: bold</li><li>I: italic</li><li>BI or IB: bold italic</li></ul>
  3897. * @param string $fontfile The font definition file. By default, the name is built from the family and style, in lower case with no spaces.
  3898. * @return array|false array containing the font data, or false in case of error.
  3899. * @param mixed $subset if true embedd only a subset of the font (stores only the information related to the used characters); if false embedd full font; if 'default' uses the default value set using setFontSubsetting(). This option is valid only for TrueTypeUnicode fonts. If you want to enable users to change the document, set this parameter to false. If you subset the font, the person who receives your PDF would need to have your same font in order to make changes to your PDF. The file size of the PDF would also be smaller because you are embedding only part of a font.
  3900. * @public
  3901. * @since 1.5
  3902. * @see SetFont(), setFontSubsetting()
  3903. */
  3904. public function AddFont($family, $style='', $fontfile='', $subset='default') {
  3905. if ($subset === 'default') {
  3906. $subset = $this->font_subsetting;
  3907. }
  3908. if ($this->pdfa_mode) {
  3909. $subset = false;
  3910. }
  3911. if (TCPDF_STATIC::empty_string($family)) {
  3912. if (!TCPDF_STATIC::empty_string($this->FontFamily)) {
  3913. $family = $this->FontFamily;
  3914. } else {
  3915. $this->Error('Empty font family');
  3916. }
  3917. }
  3918. // move embedded styles on $style
  3919. if (substr($family, -1) == 'I') {
  3920. $style .= 'I';
  3921. $family = substr($family, 0, -1);
  3922. }
  3923. if (substr($family, -1) == 'B') {
  3924. $style .= 'B';
  3925. $family = substr($family, 0, -1);
  3926. }
  3927. // normalize family name
  3928. $family = strtolower($family);
  3929. if ((!$this->isunicode) AND ($family == 'arial')) {
  3930. $family = 'helvetica';
  3931. }
  3932. if (($family == 'symbol') OR ($family == 'zapfdingbats')) {
  3933. $style = '';
  3934. }
  3935. if ($this->pdfa_mode AND (isset($this->CoreFonts[$family]))) {
  3936. // all fonts must be embedded
  3937. $family = 'pdfa'.$family;
  3938. }
  3939. $tempstyle = strtoupper($style === null ? '' : $style);
  3940. $style = '';
  3941. // underline
  3942. if (strpos($tempstyle, 'U') !== false) {
  3943. $this->underline = true;
  3944. } else {
  3945. $this->underline = false;
  3946. }
  3947. // line-through (deleted)
  3948. if (strpos($tempstyle, 'D') !== false) {
  3949. $this->linethrough = true;
  3950. } else {
  3951. $this->linethrough = false;
  3952. }
  3953. // overline
  3954. if (strpos($tempstyle, 'O') !== false) {
  3955. $this->overline = true;
  3956. } else {
  3957. $this->overline = false;
  3958. }
  3959. // bold
  3960. if (strpos($tempstyle, 'B') !== false) {
  3961. $style .= 'B';
  3962. }
  3963. // oblique
  3964. if (strpos($tempstyle, 'I') !== false) {
  3965. $style .= 'I';
  3966. }
  3967. $bistyle = $style;
  3968. $fontkey = $family.$style;
  3969. $font_style = $style.($this->underline ? 'U' : '').($this->linethrough ? 'D' : '').($this->overline ? 'O' : '');
  3970. $fontdata = array('fontkey' => $fontkey, 'family' => $family, 'style' => $font_style);
  3971. // check if the font has been already added
  3972. $fb = $this->getFontBuffer($fontkey);
  3973. if ($fb !== false) {
  3974. if ($this->inxobj) {
  3975. // we are inside an XObject template
  3976. $this->xobjects[$this->xobjid]['fonts'][$fontkey] = $fb['i'];
  3977. }
  3978. return $fontdata;
  3979. }
  3980. // get specified font directory (if any)
  3981. $fontdir = false;
  3982. if (!TCPDF_STATIC::empty_string($fontfile)) {
  3983. $fontdir = dirname($fontfile);
  3984. if (TCPDF_STATIC::empty_string($fontdir) OR ($fontdir == '.')) {
  3985. $fontdir = '';
  3986. } else {
  3987. $fontdir .= '/';
  3988. }
  3989. }
  3990. // true when the font style variation is missing
  3991. $missing_style = false;
  3992. // search and include font file
  3993. if (TCPDF_STATIC::empty_string($fontfile) OR (!@TCPDF_STATIC::file_exists($fontfile))) {
  3994. // build a standard filenames for specified font
  3995. $tmp_fontfile = str_replace(' ', '', $family).strtolower($style).'.php';
  3996. $fontfile = TCPDF_FONTS::getFontFullPath($tmp_fontfile, $fontdir);
  3997. if (TCPDF_STATIC::empty_string($fontfile)) {
  3998. $missing_style = true;
  3999. // try to remove the style part
  4000. $tmp_fontfile = str_replace(' ', '', $family).'.php';
  4001. $fontfile = TCPDF_FONTS::getFontFullPath($tmp_fontfile, $fontdir);
  4002. }
  4003. }
  4004. // include font file
  4005. if (!TCPDF_STATIC::empty_string($fontfile) AND (@TCPDF_STATIC::file_exists($fontfile))) {
  4006. $type=null;
  4007. $name=null;
  4008. $desc=null;
  4009. $up=-null;
  4010. $ut=null;
  4011. $cw=null;
  4012. $cbbox=null;
  4013. $dw=null;
  4014. $enc=null;
  4015. $cidinfo=null;
  4016. $file=null;
  4017. $ctg=null;
  4018. $diff=null;
  4019. $originalsize=null;
  4020. $size1=null;
  4021. $size2=null;
  4022. include($fontfile);
  4023. } else {
  4024. $this->Error('Could not include font definition file: '.$family.'');
  4025. }
  4026. // check font parameters
  4027. if ((!isset($type)) OR (!isset($cw))) {
  4028. $this->Error('The font definition file has a bad format: '.$fontfile.'');
  4029. }
  4030. // SET default parameters
  4031. if (!isset($file) OR TCPDF_STATIC::empty_string($file)) {
  4032. $file = '';
  4033. }
  4034. if (!isset($enc) OR TCPDF_STATIC::empty_string($enc)) {
  4035. $enc = '';
  4036. }
  4037. if (!isset($cidinfo) OR TCPDF_STATIC::empty_string($cidinfo)) {
  4038. $cidinfo = array('Registry'=>'Adobe', 'Ordering'=>'Identity', 'Supplement'=>0);
  4039. $cidinfo['uni2cid'] = array();
  4040. }
  4041. if (!isset($ctg) OR TCPDF_STATIC::empty_string($ctg)) {
  4042. $ctg = '';
  4043. }
  4044. if (!isset($desc) OR TCPDF_STATIC::empty_string($desc)) {
  4045. $desc = array();
  4046. }
  4047. if (!isset($up) OR TCPDF_STATIC::empty_string($up)) {
  4048. $up = -100;
  4049. }
  4050. if (!isset($ut) OR TCPDF_STATIC::empty_string($ut)) {
  4051. $ut = 50;
  4052. }
  4053. if (!isset($cw) OR TCPDF_STATIC::empty_string($cw)) {
  4054. $cw = array();
  4055. }
  4056. if (!isset($dw) OR TCPDF_STATIC::empty_string($dw)) {
  4057. // set default width
  4058. if (isset($desc['MissingWidth']) AND ($desc['MissingWidth'] > 0)) {
  4059. $dw = $desc['MissingWidth'];
  4060. } elseif (isset($cw[32])) {
  4061. $dw = $cw[32];
  4062. } else {
  4063. $dw = 600;
  4064. }
  4065. }
  4066. ++$this->numfonts;
  4067. if ($type == 'core') {
  4068. $name = $this->CoreFonts[$fontkey];
  4069. $subset = false;
  4070. } elseif (($type == 'TrueType') OR ($type == 'Type1')) {
  4071. $subset = false;
  4072. } elseif ($type == 'TrueTypeUnicode') {
  4073. $enc = 'Identity-H';
  4074. } elseif ($type == 'cidfont0') {
  4075. if ($this->pdfa_mode) {
  4076. $this->Error('All fonts must be embedded in PDF/A mode!');
  4077. }
  4078. } else {
  4079. $this->Error('Unknow font type: '.$type.'');
  4080. }
  4081. // set name if unset
  4082. if (empty($name)) {
  4083. $name = $fontkey;
  4084. }
  4085. // create artificial font style variations if missing (only works with non-embedded fonts)
  4086. if (($type != 'core') AND $missing_style) {
  4087. // style variations
  4088. $styles = array('' => '', 'B' => ',Bold', 'I' => ',Italic', 'BI' => ',BoldItalic');
  4089. $name .= $styles[$bistyle];
  4090. // artificial bold
  4091. if (strpos($bistyle, 'B') !== false) {
  4092. if (isset($desc['StemV'])) {
  4093. // from normal to bold
  4094. $desc['StemV'] = round($desc['StemV'] * 1.75);
  4095. } else {
  4096. // bold
  4097. $desc['StemV'] = 123;
  4098. }
  4099. }
  4100. // artificial italic
  4101. if (strpos($bistyle, 'I') !== false) {
  4102. if (isset($desc['ItalicAngle'])) {
  4103. $desc['ItalicAngle'] -= 11;
  4104. } else {
  4105. $desc['ItalicAngle'] = -11;
  4106. }
  4107. if (isset($desc['Flags'])) {
  4108. $desc['Flags'] |= 64; //bit 7
  4109. } else {
  4110. $desc['Flags'] = 64;
  4111. }
  4112. }
  4113. }
  4114. // check if the array of characters bounding boxes is defined
  4115. if (!isset($cbbox)) {
  4116. $cbbox = array();
  4117. }
  4118. // initialize subsetchars
  4119. $subsetchars = array_fill(0, 255, true);
  4120. $this->setFontBuffer($fontkey, array('fontkey' => $fontkey, 'i' => $this->numfonts, 'type' => $type, 'name' => $name, 'desc' => $desc, 'up' => $up, 'ut' => $ut, 'cw' => $cw, 'cbbox' => $cbbox, 'dw' => $dw, 'enc' => $enc, 'cidinfo' => $cidinfo, 'file' => $file, 'ctg' => $ctg, 'subset' => $subset, 'subsetchars' => $subsetchars));
  4121. if ($this->inxobj) {
  4122. // we are inside an XObject template
  4123. $this->xobjects[$this->xobjid]['fonts'][$fontkey] = $this->numfonts;
  4124. }
  4125. if (!empty($diff)) {
  4126. //Search existing encodings
  4127. $d = 0;
  4128. $nb = count($this->diffs);
  4129. for ($i=1; $i <= $nb; ++$i) {
  4130. if ($this->diffs[$i] == $diff) {
  4131. $d = $i;
  4132. break;
  4133. }
  4134. }
  4135. if ($d == 0) {
  4136. $d = $nb + 1;
  4137. $this->diffs[$d] = $diff;
  4138. }
  4139. $this->setFontSubBuffer($fontkey, 'diff', $d);
  4140. }
  4141. if (!TCPDF_STATIC::empty_string($file)) {
  4142. if (!isset($this->FontFiles[$file])) {
  4143. if ((strcasecmp($type,'TrueType') == 0) OR (strcasecmp($type, 'TrueTypeUnicode') == 0)) {
  4144. $this->FontFiles[$file] = array('length1' => $originalsize, 'fontdir' => $fontdir, 'subset' => $subset, 'fontkeys' => array($fontkey));
  4145. } elseif ($type != 'core') {
  4146. $this->FontFiles[$file] = array('length1' => $size1, 'length2' => $size2, 'fontdir' => $fontdir, 'subset' => $subset, 'fontkeys' => array($fontkey));
  4147. }
  4148. } else {
  4149. // update fontkeys that are sharing this font file
  4150. $this->FontFiles[$file]['subset'] = ($this->FontFiles[$file]['subset'] AND $subset);
  4151. if (!in_array($fontkey, $this->FontFiles[$file]['fontkeys'])) {
  4152. $this->FontFiles[$file]['fontkeys'][] = $fontkey;
  4153. }
  4154. }
  4155. }
  4156. return $fontdata;
  4157. }
  4158. /**
  4159. * Sets the font used to print character strings.
  4160. * The font can be either a standard one or a font added via the AddFont() method. Standard fonts use Windows encoding cp1252 (Western Europe).
  4161. * The method can be called before the first page is created and the font is retained from page to page.
  4162. * If you just wish to change the current font size, it is simpler to call SetFontSize().
  4163. * Note: for the standard fonts, the font metric files must be accessible. There are three possibilities for this:<ul><li>They are in the current directory (the one where the running script lies)</li><li>They are in one of the directories defined by the include_path parameter</li><li>They are in the directory defined by the K_PATH_FONTS constant</li></ul><br />
  4164. * @param string $family Family font. It can be either a name defined by AddFont() or one of the standard Type1 families (case insensitive):<ul><li>times (Times-Roman)</li><li>timesb (Times-Bold)</li><li>timesi (Times-Italic)</li><li>timesbi (Times-BoldItalic)</li><li>helvetica (Helvetica)</li><li>helveticab (Helvetica-Bold)</li><li>helveticai (Helvetica-Oblique)</li><li>helveticabi (Helvetica-BoldOblique)</li><li>courier (Courier)</li><li>courierb (Courier-Bold)</li><li>courieri (Courier-Oblique)</li><li>courierbi (Courier-BoldOblique)</li><li>symbol (Symbol)</li><li>zapfdingbats (ZapfDingbats)</li></ul> It is also possible to pass an empty string. In that case, the current family is retained.
  4165. * @param string $style Font style. Possible values are (case insensitive):<ul><li>empty string: regular</li><li>B: bold</li><li>I: italic</li><li>U: underline</li><li>D: line through</li><li>O: overline</li></ul> or any combination. The default value is regular. Bold and italic styles do not apply to Symbol and ZapfDingbats basic fonts or other fonts when not defined.
  4166. * @param float|null $size Font size in points. The default value is the current size. If no size has been specified since the beginning of the document, the value taken is 12
  4167. * @param string $fontfile The font definition file. By default, the name is built from the family and style, in lower case with no spaces.
  4168. * @param mixed $subset if true embedd only a subset of the font (stores only the information related to the used characters); if false embedd full font; if 'default' uses the default value set using setFontSubsetting(). This option is valid only for TrueTypeUnicode fonts. If you want to enable users to change the document, set this parameter to false. If you subset the font, the person who receives your PDF would need to have your same font in order to make changes to your PDF. The file size of the PDF would also be smaller because you are embedding only part of a font.
  4169. * @param boolean $out if true output the font size command, otherwise only set the font properties.
  4170. * @author Nicola Asuni
  4171. * @public
  4172. * @since 1.0
  4173. * @see AddFont(), SetFontSize()
  4174. */
  4175. public function setFont($family, $style='', $size=null, $fontfile='', $subset='default', $out=true) {
  4176. //Select a font; size given in points
  4177. if ($size === null) {
  4178. $size = $this->FontSizePt;
  4179. }
  4180. if ($size < 0) {
  4181. $size = 0;
  4182. }
  4183. // try to add font (if not already added)
  4184. $fontdata = $this->AddFont($family, $style, $fontfile, $subset);
  4185. $this->FontFamily = $fontdata['family'];
  4186. $this->FontStyle = $fontdata['style'];
  4187. if (isset($this->CurrentFont['fontkey']) AND isset($this->CurrentFont['subsetchars'])) {
  4188. // save subset chars of the previous font
  4189. $this->setFontSubBuffer($this->CurrentFont['fontkey'], 'subsetchars', $this->CurrentFont['subsetchars']);
  4190. }
  4191. $this->CurrentFont = $this->getFontBuffer($fontdata['fontkey']);
  4192. $this->setFontSize($size, $out);
  4193. }
  4194. /**
  4195. * Defines the size of the current font.
  4196. * @param float $size The font size in points.
  4197. * @param boolean $out if true output the font size command, otherwise only set the font properties.
  4198. * @public
  4199. * @since 1.0
  4200. * @see SetFont()
  4201. */
  4202. public function setFontSize($size, $out=true) {
  4203. $size = (float)$size;
  4204. // font size in points
  4205. $this->FontSizePt = $size;
  4206. // font size in user units
  4207. $this->FontSize = $size / $this->k;
  4208. // calculate some font metrics
  4209. if (isset($this->CurrentFont['desc']['FontBBox'])) {
  4210. $bbox = explode(' ', substr($this->CurrentFont['desc']['FontBBox'], 1, -1));
  4211. $font_height = ((intval($bbox[3]) - intval($bbox[1])) * $size / 1000);
  4212. } else {
  4213. $font_height = $size * 1.219;
  4214. }
  4215. if (isset($this->CurrentFont['desc']['Ascent']) AND ($this->CurrentFont['desc']['Ascent'] > 0)) {
  4216. $font_ascent = ($this->CurrentFont['desc']['Ascent'] * $size / 1000);
  4217. }
  4218. if (isset($this->CurrentFont['desc']['Descent']) AND ($this->CurrentFont['desc']['Descent'] <= 0)) {
  4219. $font_descent = (- $this->CurrentFont['desc']['Descent'] * $size / 1000);
  4220. }
  4221. if (!isset($font_ascent) AND !isset($font_descent)) {
  4222. // core font
  4223. $font_ascent = 0.76 * $font_height;
  4224. $font_descent = $font_height - $font_ascent;
  4225. } elseif (!isset($font_descent)) {
  4226. $font_descent = $font_height - $font_ascent;
  4227. } elseif (!isset($font_ascent)) {
  4228. $font_ascent = $font_height - $font_descent;
  4229. }
  4230. $this->FontAscent = ($font_ascent / $this->k);
  4231. $this->FontDescent = ($font_descent / $this->k);
  4232. if ($out AND ($this->page > 0) AND (isset($this->CurrentFont['i'])) AND ($this->state == 2)) {
  4233. $this->_out(sprintf('BT /F%d %F Tf ET', $this->CurrentFont['i'], $this->FontSizePt));
  4234. }
  4235. }
  4236. /**
  4237. * Returns the bounding box of the current font in user units.
  4238. * @return array
  4239. * @public
  4240. * @since 5.9.152 (2012-03-23)
  4241. */
  4242. public function getFontBBox() {
  4243. $fbbox = array();
  4244. if (isset($this->CurrentFont['desc']['FontBBox'])) {
  4245. $tmpbbox = explode(' ', substr($this->CurrentFont['desc']['FontBBox'], 1, -1));
  4246. $fbbox = array_map(array($this,'getAbsFontMeasure'), $tmpbbox);
  4247. } else {
  4248. // Find max width
  4249. if (isset($this->CurrentFont['desc']['MaxWidth'])) {
  4250. $maxw = $this->getAbsFontMeasure(intval($this->CurrentFont['desc']['MaxWidth']));
  4251. } else {
  4252. $maxw = 0;
  4253. if (isset($this->CurrentFont['desc']['MissingWidth'])) {
  4254. $maxw = max($maxw, $this->CurrentFont['desc']['MissingWidth']);
  4255. }
  4256. if (isset($this->CurrentFont['desc']['AvgWidth'])) {
  4257. $maxw = max($maxw, $this->CurrentFont['desc']['AvgWidth']);
  4258. }
  4259. if (isset($this->CurrentFont['dw'])) {
  4260. $maxw = max($maxw, $this->CurrentFont['dw']);
  4261. }
  4262. foreach ($this->CurrentFont['cw'] as $char => $w) {
  4263. $maxw = max($maxw, $w);
  4264. }
  4265. if ($maxw == 0) {
  4266. $maxw = 600;
  4267. }
  4268. $maxw = $this->getAbsFontMeasure($maxw);
  4269. }
  4270. $fbbox = array(0, (0 - $this->FontDescent), $maxw, $this->FontAscent);
  4271. }
  4272. return $fbbox;
  4273. }
  4274. /**
  4275. * Convert a relative font measure into absolute value.
  4276. * @param int $s Font measure.
  4277. * @return float Absolute measure.
  4278. * @since 5.9.186 (2012-09-13)
  4279. */
  4280. public function getAbsFontMeasure($s) {
  4281. return ($s * $this->FontSize / 1000);
  4282. }
  4283. /**
  4284. * Returns the glyph bounding box of the specified character in the current font in user units.
  4285. * @param int $char Input character code.
  4286. * @return false|array array(xMin, yMin, xMax, yMax) or FALSE if not defined.
  4287. * @since 5.9.186 (2012-09-13)
  4288. */
  4289. public function getCharBBox($char) {
  4290. $c = intval($char);
  4291. if (isset($this->CurrentFont['cw'][$c])) {
  4292. // glyph is defined ... use zero width & height for glyphs without outlines
  4293. $result = array(0,0,0,0);
  4294. if (isset($this->CurrentFont['cbbox'][$c])) {
  4295. $result = $this->CurrentFont['cbbox'][$c];
  4296. }
  4297. return array_map(array($this,'getAbsFontMeasure'), $result);
  4298. }
  4299. return false;
  4300. }
  4301. /**
  4302. * Return the font descent value
  4303. * @param string $font font name
  4304. * @param string $style font style
  4305. * @param float $size The size (in points)
  4306. * @return int font descent
  4307. * @public
  4308. * @author Nicola Asuni
  4309. * @since 4.9.003 (2010-03-30)
  4310. */
  4311. public function getFontDescent($font, $style='', $size=0) {
  4312. $fontdata = $this->AddFont($font, $style);
  4313. $fontinfo = $this->getFontBuffer($fontdata['fontkey']);
  4314. if (isset($fontinfo['desc']['Descent']) AND ($fontinfo['desc']['Descent'] <= 0)) {
  4315. $descent = (- $fontinfo['desc']['Descent'] * $size / 1000);
  4316. } else {
  4317. $descent = (1.219 * 0.24 * $size);
  4318. }
  4319. return ($descent / $this->k);
  4320. }
  4321. /**
  4322. * Return the font ascent value.
  4323. * @param string $font font name
  4324. * @param string $style font style
  4325. * @param float $size The size (in points)
  4326. * @return int font ascent
  4327. * @public
  4328. * @author Nicola Asuni
  4329. * @since 4.9.003 (2010-03-30)
  4330. */
  4331. public function getFontAscent($font, $style='', $size=0) {
  4332. $fontdata = $this->AddFont($font, $style);
  4333. $fontinfo = $this->getFontBuffer($fontdata['fontkey']);
  4334. if (isset($fontinfo['desc']['Ascent']) AND ($fontinfo['desc']['Ascent'] > 0)) {
  4335. $ascent = ($fontinfo['desc']['Ascent'] * $size / 1000);
  4336. } else {
  4337. $ascent = 1.219 * 0.76 * $size;
  4338. }
  4339. return ($ascent / $this->k);
  4340. }
  4341. /**
  4342. * Return true in the character is present in the specified font.
  4343. * @param mixed $char Character to check (integer value or string)
  4344. * @param string $font Font name (family name).
  4345. * @param string $style Font style.
  4346. * @return bool true if the char is defined, false otherwise.
  4347. * @public
  4348. * @since 5.9.153 (2012-03-28)
  4349. */
  4350. public function isCharDefined($char, $font='', $style='') {
  4351. if (is_string($char)) {
  4352. // get character code
  4353. $char = TCPDF_FONTS::UTF8StringToArray($char, $this->isunicode, $this->CurrentFont);
  4354. $char = $char[0];
  4355. }
  4356. if (TCPDF_STATIC::empty_string($font)) {
  4357. if (TCPDF_STATIC::empty_string($style)) {
  4358. return (isset($this->CurrentFont['cw'][intval($char)]));
  4359. }
  4360. $font = $this->FontFamily;
  4361. }
  4362. $fontdata = $this->AddFont($font, $style);
  4363. $fontinfo = $this->getFontBuffer($fontdata['fontkey']);
  4364. return (isset($fontinfo['cw'][intval($char)]));
  4365. }
  4366. /**
  4367. * Replace missing font characters on selected font with specified substitutions.
  4368. * @param string $text Text to process.
  4369. * @param string $font Font name (family name).
  4370. * @param string $style Font style.
  4371. * @param array $subs Array of possible character substitutions. The key is the character to check (integer value) and the value is a single intege value or an array of possible substitutes.
  4372. * @return string Processed text.
  4373. * @public
  4374. * @since 5.9.153 (2012-03-28)
  4375. */
  4376. public function replaceMissingChars($text, $font='', $style='', $subs=array()) {
  4377. if (empty($subs)) {
  4378. return $text;
  4379. }
  4380. if (TCPDF_STATIC::empty_string($font)) {
  4381. $font = $this->FontFamily;
  4382. }
  4383. $fontdata = $this->AddFont($font, $style);
  4384. $fontinfo = $this->getFontBuffer($fontdata['fontkey']);
  4385. $uniarr = TCPDF_FONTS::UTF8StringToArray($text, $this->isunicode, $this->CurrentFont);
  4386. foreach ($uniarr as $k => $chr) {
  4387. if (!isset($fontinfo['cw'][$chr])) {
  4388. // this character is missing on the selected font
  4389. if (isset($subs[$chr])) {
  4390. // we have available substitutions
  4391. if (is_array($subs[$chr])) {
  4392. foreach($subs[$chr] as $s) {
  4393. if (isset($fontinfo['cw'][$s])) {
  4394. $uniarr[$k] = $s;
  4395. break;
  4396. }
  4397. }
  4398. } elseif (isset($fontinfo['cw'][$subs[$chr]])) {
  4399. $uniarr[$k] = $subs[$chr];
  4400. }
  4401. }
  4402. }
  4403. }
  4404. return TCPDF_FONTS::UniArrSubString(TCPDF_FONTS::UTF8ArrayToUniArray($uniarr, $this->isunicode));
  4405. }
  4406. /**
  4407. * Defines the default monospaced font.
  4408. * @param string $font Font name.
  4409. * @public
  4410. * @since 4.5.025
  4411. */
  4412. public function setDefaultMonospacedFont($font) {
  4413. $this->default_monospaced_font = $font;
  4414. }
  4415. /**
  4416. * Creates a new internal link and returns its identifier. An internal link is a clickable area which directs to another place within the document.<br />
  4417. * The identifier can then be passed to Cell(), Write(), Image() or Link(). The destination is defined with SetLink().
  4418. * @public
  4419. * @since 1.5
  4420. * @see Cell(), Write(), Image(), Link(), SetLink()
  4421. */
  4422. public function AddLink() {
  4423. // create a new internal link
  4424. $n = count($this->links) + 1;
  4425. $this->links[$n] = array('p' => 0, 'y' => 0, 'f' => false);
  4426. return $n;
  4427. }
  4428. /**
  4429. * Defines the page and position a link points to.
  4430. * @param int $link The link identifier returned by AddLink()
  4431. * @param float $y Ordinate of target position; -1 indicates the current position. The default value is 0 (top of page)
  4432. * @param int|string $page Number of target page; -1 indicates the current page (default value). If you prefix a page number with the * character, then this page will not be changed when adding/deleting/moving pages.
  4433. * @public
  4434. * @since 1.5
  4435. * @see AddLink()
  4436. */
  4437. public function setLink($link, $y=0, $page=-1) {
  4438. $fixed = false;
  4439. if (!empty($page) AND (substr($page, 0, 1) == '*')) {
  4440. $page = intval(substr($page, 1));
  4441. // this page number will not be changed when moving/add/deleting pages
  4442. $fixed = true;
  4443. }
  4444. if ($page < 0) {
  4445. $page = $this->page;
  4446. }
  4447. if ($y == -1) {
  4448. $y = $this->y;
  4449. }
  4450. $this->links[$link] = array('p' => $page, 'y' => $y, 'f' => $fixed);
  4451. }
  4452. /**
  4453. * Puts a link on a rectangular area of the page.
  4454. * Text or image links are generally put via Cell(), Write() or Image(), but this method can be useful for instance to define a clickable area inside an image.
  4455. * @param float $x Abscissa of the upper-left corner of the rectangle
  4456. * @param float $y Ordinate of the upper-left corner of the rectangle
  4457. * @param float $w Width of the rectangle
  4458. * @param float $h Height of the rectangle
  4459. * @param mixed $link URL or identifier returned by AddLink()
  4460. * @param int $spaces number of spaces on the text to link
  4461. * @public
  4462. * @since 1.5
  4463. * @see AddLink(), Annotation(), Cell(), Write(), Image()
  4464. */
  4465. public function Link($x, $y, $w, $h, $link, $spaces=0) {
  4466. $this->Annotation($x, $y, $w, $h, $link, array('Subtype'=>'Link'), $spaces);
  4467. }
  4468. /**
  4469. * Puts a markup annotation on a rectangular area of the page.
  4470. * !!!!THE ANNOTATION SUPPORT IS NOT YET FULLY IMPLEMENTED !!!!
  4471. * @param float $x Abscissa of the upper-left corner of the rectangle
  4472. * @param float $y Ordinate of the upper-left corner of the rectangle
  4473. * @param float $w Width of the rectangle
  4474. * @param float $h Height of the rectangle
  4475. * @param string $text annotation text or alternate content
  4476. * @param array $opt array of options (see section 8.4 of PDF reference 1.7).
  4477. * @param int $spaces number of spaces on the text to link
  4478. * @public
  4479. * @since 4.0.018 (2008-08-06)
  4480. */
  4481. public function Annotation($x, $y, $w, $h, $text, $opt=array('Subtype'=>'Text'), $spaces=0) {
  4482. if ($this->inxobj) {
  4483. // store parameters for later use on template
  4484. $this->xobjects[$this->xobjid]['annotations'][] = array('x' => $x, 'y' => $y, 'w' => $w, 'h' => $h, 'text' => $text, 'opt' => $opt, 'spaces' => $spaces);
  4485. return;
  4486. }
  4487. if ($x === '') {
  4488. $x = $this->x;
  4489. }
  4490. if ($y === '') {
  4491. $y = $this->y;
  4492. }
  4493. // check page for no-write regions and adapt page margins if necessary
  4494. list($x, $y) = $this->checkPageRegions($h, $x, $y);
  4495. // recalculate coordinates to account for graphic transformations
  4496. if (isset($this->transfmatrix) AND !empty($this->transfmatrix)) {
  4497. for ($i=$this->transfmatrix_key; $i > 0; --$i) {
  4498. $maxid = count($this->transfmatrix[$i]) - 1;
  4499. for ($j=$maxid; $j >= 0; --$j) {
  4500. $ctm = $this->transfmatrix[$i][$j];
  4501. if (isset($ctm['a'])) {
  4502. $x = $x * $this->k;
  4503. $y = ($this->h - $y) * $this->k;
  4504. $w = $w * $this->k;
  4505. $h = $h * $this->k;
  4506. // top left
  4507. $xt = $x;
  4508. $yt = $y;
  4509. $x1 = ($ctm['a'] * $xt) + ($ctm['c'] * $yt) + $ctm['e'];
  4510. $y1 = ($ctm['b'] * $xt) + ($ctm['d'] * $yt) + $ctm['f'];
  4511. // top right
  4512. $xt = $x + $w;
  4513. $yt = $y;
  4514. $x2 = ($ctm['a'] * $xt) + ($ctm['c'] * $yt) + $ctm['e'];
  4515. $y2 = ($ctm['b'] * $xt) + ($ctm['d'] * $yt) + $ctm['f'];
  4516. // bottom left
  4517. $xt = $x;
  4518. $yt = $y - $h;
  4519. $x3 = ($ctm['a'] * $xt) + ($ctm['c'] * $yt) + $ctm['e'];
  4520. $y3 = ($ctm['b'] * $xt) + ($ctm['d'] * $yt) + $ctm['f'];
  4521. // bottom right
  4522. $xt = $x + $w;
  4523. $yt = $y - $h;
  4524. $x4 = ($ctm['a'] * $xt) + ($ctm['c'] * $yt) + $ctm['e'];
  4525. $y4 = ($ctm['b'] * $xt) + ($ctm['d'] * $yt) + $ctm['f'];
  4526. // new coordinates (rectangle area)
  4527. $x = min($x1, $x2, $x3, $x4);
  4528. $y = max($y1, $y2, $y3, $y4);
  4529. $w = (max($x1, $x2, $x3, $x4) - $x) / $this->k;
  4530. $h = ($y - min($y1, $y2, $y3, $y4)) / $this->k;
  4531. $x = $x / $this->k;
  4532. $y = $this->h - ($y / $this->k);
  4533. }
  4534. }
  4535. }
  4536. }
  4537. if ($this->page <= 0) {
  4538. $page = 1;
  4539. } else {
  4540. $page = $this->page;
  4541. }
  4542. if (!isset($this->PageAnnots[$page])) {
  4543. $this->PageAnnots[$page] = array();
  4544. }
  4545. $this->PageAnnots[$page][] = array('n' => ++$this->n, 'x' => $x, 'y' => $y, 'w' => $w, 'h' => $h, 'txt' => $text, 'opt' => $opt, 'numspaces' => $spaces);
  4546. if (!$this->pdfa_mode || ($this->pdfa_mode && $this->pdfa_version == 3)) {
  4547. if ((($opt['Subtype'] == 'FileAttachment') OR ($opt['Subtype'] == 'Sound')) AND (!TCPDF_STATIC::empty_string($opt['FS']))
  4548. AND (@TCPDF_STATIC::file_exists($opt['FS']) OR TCPDF_STATIC::isValidURL($opt['FS']))
  4549. AND (!isset($this->embeddedfiles[basename($opt['FS'])]))) {
  4550. $this->embeddedfiles[basename($opt['FS'])] = array('f' => ++$this->n, 'n' => ++$this->n, 'file' => $opt['FS']);
  4551. }
  4552. }
  4553. // Add widgets annotation's icons
  4554. if (isset($opt['mk']['i']) AND @TCPDF_STATIC::file_exists($opt['mk']['i'])) {
  4555. $this->Image($opt['mk']['i'], '', '', 10, 10, '', '', '', false, 300, '', false, false, 0, false, true);
  4556. }
  4557. if (isset($opt['mk']['ri']) AND @TCPDF_STATIC::file_exists($opt['mk']['ri'])) {
  4558. $this->Image($opt['mk']['ri'], '', '', 0, 0, '', '', '', false, 300, '', false, false, 0, false, true);
  4559. }
  4560. if (isset($opt['mk']['ix']) AND @TCPDF_STATIC::file_exists($opt['mk']['ix'])) {
  4561. $this->Image($opt['mk']['ix'], '', '', 0, 0, '', '', '', false, 300, '', false, false, 0, false, true);
  4562. }
  4563. }
  4564. /**
  4565. * Embedd the attached files.
  4566. * @since 4.4.000 (2008-12-07)
  4567. * @protected
  4568. * @see Annotation()
  4569. */
  4570. protected function _putEmbeddedFiles() {
  4571. if ($this->pdfa_mode && $this->pdfa_version != 3) {
  4572. // embedded files are not allowed in PDF/A mode version 1 and 2
  4573. return;
  4574. }
  4575. reset($this->embeddedfiles);
  4576. foreach ($this->embeddedfiles as $filename => $filedata) {
  4577. $data = $this->getCachedFileContents($filedata['file']);
  4578. if ($data !== FALSE) {
  4579. $rawsize = strlen($data);
  4580. if ($rawsize > 0) {
  4581. // update name tree
  4582. $this->efnames[$filename] = $filedata['f'].' 0 R';
  4583. // embedded file specification object
  4584. $out = $this->_getobj($filedata['f'])."\n";
  4585. $out .= '<</Type /Filespec /F '.$this->_datastring($filename, $filedata['f']);
  4586. $out .= ' /UF '.$this->_datastring($filename, $filedata['f']);
  4587. $out .= ' /AFRelationship /Source';
  4588. $out .= ' /EF <</F '.$filedata['n'].' 0 R>> >>';
  4589. $out .= "\n".'endobj';
  4590. $this->_out($out);
  4591. // embedded file object
  4592. $filter = '';
  4593. if ($this->compress) {
  4594. $data = gzcompress($data);
  4595. $filter = ' /Filter /FlateDecode';
  4596. }
  4597. if ($this->pdfa_version == 3) {
  4598. $filter = ' /Subtype /text#2Fxml';
  4599. }
  4600. $stream = $this->_getrawstream($data, $filedata['n']);
  4601. $out = $this->_getobj($filedata['n'])."\n";
  4602. $out .= '<< /Type /EmbeddedFile'.$filter.' /Length '.strlen($stream).' /Params <</Size '.$rawsize.'>> >>';
  4603. $out .= ' stream'."\n".$stream."\n".'endstream';
  4604. $out .= "\n".'endobj';
  4605. $this->_out($out);
  4606. }
  4607. }
  4608. }
  4609. }
  4610. /**
  4611. * Prints a text cell at the specified position.
  4612. * This method allows to place a string precisely on the page.
  4613. * @param float $x Abscissa of the cell origin
  4614. * @param float $y Ordinate of the cell origin
  4615. * @param string $txt String to print
  4616. * @param int $fstroke outline size in user units (0 = disable)
  4617. * @param boolean $fclip if true activate clipping mode (you must call StartTransform() before this function and StopTransform() to stop the clipping tranformation).
  4618. * @param boolean $ffill if true fills the text
  4619. * @param mixed $border Indicates if borders must be drawn around the cell. The value can be a number:<ul><li>0: no border (default)</li><li>1: frame</li></ul> or a string containing some or all of the following characters (in any order):<ul><li>L: left</li><li>T: top</li><li>R: right</li><li>B: bottom</li></ul> or an array of line styles for each border group - for example: array('LTRB' => array('width' => 2, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)))
  4620. * @param int $ln Indicates where the current position should go after the call. Possible values are:<ul><li>0: to the right (or left for RTL languages)</li><li>1: to the beginning of the next line</li><li>2: below</li></ul>Putting 1 is equivalent to putting 0 and calling Ln() just after. Default value: 0.
  4621. * @param string $align Allows to center or align the text. Possible values are:<ul><li>L or empty string: left align (default value)</li><li>C: center</li><li>R: right align</li><li>J: justify</li></ul>
  4622. * @param boolean $fill Indicates if the cell background must be painted (true) or transparent (false).
  4623. * @param mixed $link URL or identifier returned by AddLink().
  4624. * @param int $stretch font stretch mode: <ul><li>0 = disabled</li><li>1 = horizontal scaling only if text is larger than cell width</li><li>2 = forced horizontal scaling to fit cell width</li><li>3 = character spacing only if text is larger than cell width</li><li>4 = forced character spacing to fit cell width</li></ul> General font stretching and scaling values will be preserved when possible.
  4625. * @param boolean $ignore_min_height if true ignore automatic minimum height value.
  4626. * @param string $calign cell vertical alignment relative to the specified Y value. Possible values are:<ul><li>T : cell top</li><li>A : font top</li><li>L : font baseline</li><li>D : font bottom</li><li>B : cell bottom</li></ul>
  4627. * @param string $valign text vertical alignment inside the cell. Possible values are:<ul><li>T : top</li><li>C : center</li><li>B : bottom</li></ul>
  4628. * @param boolean $rtloff if true uses the page top-left corner as origin of axis for $x and $y initial position.
  4629. * @public
  4630. * @since 1.0
  4631. * @see Cell(), Write(), MultiCell(), WriteHTML(), WriteHTMLCell()
  4632. */
  4633. public function Text($x, $y, $txt, $fstroke=0, $fclip=false, $ffill=true, $border=0, $ln=0, $align='', $fill=false, $link='', $stretch=0, $ignore_min_height=false, $calign='T', $valign='M', $rtloff=false) {
  4634. $textrendermode = $this->textrendermode;
  4635. $textstrokewidth = $this->textstrokewidth;
  4636. $this->setTextRenderingMode($fstroke, $ffill, $fclip);
  4637. $this->setXY($x, $y, $rtloff);
  4638. $this->Cell(0, 0, $txt, $border, $ln, $align, $fill, $link, $stretch, $ignore_min_height, $calign, $valign);
  4639. // restore previous rendering mode
  4640. $this->textrendermode = $textrendermode;
  4641. $this->textstrokewidth = $textstrokewidth;
  4642. }
  4643. /**
  4644. * Whenever a page break condition is met, the method is called, and the break is issued or not depending on the returned value.
  4645. * The default implementation returns a value according to the mode selected by SetAutoPageBreak().<br />
  4646. * This method is called automatically and should not be called directly by the application.
  4647. * @return bool
  4648. * @public
  4649. * @since 1.4
  4650. * @see SetAutoPageBreak()
  4651. */
  4652. public function AcceptPageBreak() {
  4653. if ($this->num_columns > 1) {
  4654. // multi column mode
  4655. if ($this->current_column < ($this->num_columns - 1)) {
  4656. // go to next column
  4657. $this->selectColumn($this->current_column + 1);
  4658. } elseif ($this->AutoPageBreak) {
  4659. // add a new page
  4660. $this->AddPage();
  4661. // set first column
  4662. $this->selectColumn(0);
  4663. }
  4664. // avoid page breaking from checkPageBreak()
  4665. return false;
  4666. }
  4667. return $this->AutoPageBreak;
  4668. }
  4669. /**
  4670. * Add page if needed.
  4671. * @param float $h Cell height. Default value: 0.
  4672. * @param float|null $y starting y position, leave empty for current position.
  4673. * @param bool $addpage if true add a page, otherwise only return the true/false state
  4674. * @return bool true in case of page break, false otherwise.
  4675. * @since 3.2.000 (2008-07-01)
  4676. * @protected
  4677. */
  4678. protected function checkPageBreak($h=0, $y=null, $addpage=true) {
  4679. if (TCPDF_STATIC::empty_string($y)) {
  4680. $y = $this->y;
  4681. }
  4682. $current_page = $this->page;
  4683. if ((($y + $h) > $this->PageBreakTrigger) AND ($this->inPageBody()) AND ($this->AcceptPageBreak())) {
  4684. if ($addpage) {
  4685. //Automatic page break
  4686. $x = $this->x;
  4687. $this->AddPage($this->CurOrientation);
  4688. $this->y = $this->tMargin;
  4689. $oldpage = $this->page - 1;
  4690. if ($this->rtl) {
  4691. if ($this->pagedim[$this->page]['orm'] != $this->pagedim[$oldpage]['orm']) {
  4692. $this->x = $x - ($this->pagedim[$this->page]['orm'] - $this->pagedim[$oldpage]['orm']);
  4693. } else {
  4694. $this->x = $x;
  4695. }
  4696. } else {
  4697. if ($this->pagedim[$this->page]['olm'] != $this->pagedim[$oldpage]['olm']) {
  4698. $this->x = $x + ($this->pagedim[$this->page]['olm'] - $this->pagedim[$oldpage]['olm']);
  4699. } else {
  4700. $this->x = $x;
  4701. }
  4702. }
  4703. }
  4704. return true;
  4705. }
  4706. if ($current_page != $this->page) {
  4707. // account for columns mode
  4708. return true;
  4709. }
  4710. return false;
  4711. }
  4712. /**
  4713. * Prints a cell (rectangular area) with optional borders, background color and character string. The upper-left corner of the cell corresponds to the current position. The text can be aligned or centered. After the call, the current position moves to the right or to the next line. It is possible to put a link on the text.<br />
  4714. * If automatic page breaking is enabled and the cell goes beyond the limit, a page break is done before outputting.
  4715. * @param float $w Cell width. If 0, the cell extends up to the right margin.
  4716. * @param float $h Cell height. Default value: 0.
  4717. * @param string $txt String to print. Default value: empty string.
  4718. * @param mixed $border Indicates if borders must be drawn around the cell. The value can be a number:<ul><li>0: no border (default)</li><li>1: frame</li></ul> or a string containing some or all of the following characters (in any order):<ul><li>L: left</li><li>T: top</li><li>R: right</li><li>B: bottom</li></ul> or an array of line styles for each border group - for example: array('LTRB' => array('width' => 2, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)))
  4719. * @param int $ln Indicates where the current position should go after the call. Possible values are:<ul><li>0: to the right (or left for RTL languages)</li><li>1: to the beginning of the next line</li><li>2: below</li></ul> Putting 1 is equivalent to putting 0 and calling Ln() just after. Default value: 0.
  4720. * @param string $align Allows to center or align the text. Possible values are:<ul><li>L or empty string: left align (default value)</li><li>C: center</li><li>R: right align</li><li>J: justify</li></ul>
  4721. * @param boolean $fill Indicates if the cell background must be painted (true) or transparent (false).
  4722. * @param mixed $link URL or identifier returned by AddLink().
  4723. * @param int $stretch font stretch mode: <ul><li>0 = disabled</li><li>1 = horizontal scaling only if text is larger than cell width</li><li>2 = forced horizontal scaling to fit cell width</li><li>3 = character spacing only if text is larger than cell width</li><li>4 = forced character spacing to fit cell width</li></ul> General font stretching and scaling values will be preserved when possible.
  4724. * @param boolean $ignore_min_height if true ignore automatic minimum height value.
  4725. * @param string $calign cell vertical alignment relative to the specified Y value. Possible values are:<ul><li>T : cell top</li><li>C : center</li><li>B : cell bottom</li><li>A : font top</li><li>L : font baseline</li><li>D : font bottom</li></ul>
  4726. * @param string $valign text vertical alignment inside the cell. Possible values are:<ul><li>T : top</li><li>C : center</li><li>B : bottom</li></ul>
  4727. * @public
  4728. * @since 1.0
  4729. * @see SetFont(), SetDrawColor(), SetFillColor(), SetTextColor(), SetLineWidth(), AddLink(), Ln(), MultiCell(), Write(), SetAutoPageBreak()
  4730. */
  4731. public function Cell($w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=false, $link='', $stretch=0, $ignore_min_height=false, $calign='T', $valign='M') {
  4732. $prev_cell_margin = $this->cell_margin;
  4733. $prev_cell_padding = $this->cell_padding;
  4734. $this->adjustCellPadding($border);
  4735. if (!$ignore_min_height) {
  4736. $min_cell_height = $this->getCellHeight($this->FontSize);
  4737. if ($h < $min_cell_height) {
  4738. $h = $min_cell_height;
  4739. }
  4740. }
  4741. $this->checkPageBreak($h + $this->cell_margin['T'] + $this->cell_margin['B']);
  4742. // apply text shadow if enabled
  4743. if ($this->txtshadow['enabled']) {
  4744. // save data
  4745. $x = $this->x;
  4746. $y = $this->y;
  4747. $bc = $this->bgcolor;
  4748. $fc = $this->fgcolor;
  4749. $sc = $this->strokecolor;
  4750. $alpha = $this->alpha;
  4751. // print shadow
  4752. $this->x += $this->txtshadow['depth_w'];
  4753. $this->y += $this->txtshadow['depth_h'];
  4754. $this->setFillColorArray($this->txtshadow['color']);
  4755. $this->setTextColorArray($this->txtshadow['color']);
  4756. $this->setDrawColorArray($this->txtshadow['color']);
  4757. if ($this->txtshadow['opacity'] != $alpha['CA']) {
  4758. $this->setAlpha($this->txtshadow['opacity'], $this->txtshadow['blend_mode']);
  4759. }
  4760. if ($this->state == 2) {
  4761. $this->_out($this->getCellCode($w, $h, $txt, $border, $ln, $align, $fill, $link, $stretch, true, $calign, $valign));
  4762. }
  4763. //restore data
  4764. $this->x = $x;
  4765. $this->y = $y;
  4766. $this->setFillColorArray($bc);
  4767. $this->setTextColorArray($fc);
  4768. $this->setDrawColorArray($sc);
  4769. if ($this->txtshadow['opacity'] != $alpha['CA']) {
  4770. $this->setAlpha($alpha['CA'], $alpha['BM'], $alpha['ca'], $alpha['AIS']);
  4771. }
  4772. }
  4773. if ($this->state == 2) {
  4774. $this->_out($this->getCellCode($w, $h, $txt, $border, $ln, $align, $fill, $link, $stretch, true, $calign, $valign));
  4775. }
  4776. $this->cell_padding = $prev_cell_padding;
  4777. $this->cell_margin = $prev_cell_margin;
  4778. }
  4779. /**
  4780. * Returns the PDF string code to print a cell (rectangular area) with optional borders, background color and character string. The upper-left corner of the cell corresponds to the current position. The text can be aligned or centered. After the call, the current position moves to the right or to the next line. It is possible to put a link on the text.<br />
  4781. * If automatic page breaking is enabled and the cell goes beyond the limit, a page break is done before outputting.
  4782. * @param float $w Cell width. If 0, the cell extends up to the right margin.
  4783. * @param float $h Cell height. Default value: 0.
  4784. * @param string $txt String to print. Default value: empty string.
  4785. * @param mixed $border Indicates if borders must be drawn around the cell. The value can be a number:<ul><li>0: no border (default)</li><li>1: frame</li></ul> or a string containing some or all of the following characters (in any order):<ul><li>L: left</li><li>T: top</li><li>R: right</li><li>B: bottom</li></ul> or an array of line styles for each border group - for example: array('LTRB' => array('width' => 2, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)))
  4786. * @param int $ln Indicates where the current position should go after the call. Possible values are:<ul><li>0: to the right (or left for RTL languages)</li><li>1: to the beginning of the next line</li><li>2: below</li></ul>Putting 1 is equivalent to putting 0 and calling Ln() just after. Default value: 0.
  4787. * @param string $align Allows to center or align the text. Possible values are:<ul><li>L or empty string: left align (default value)</li><li>C: center</li><li>R: right align</li><li>J: justify</li></ul>
  4788. * @param boolean $fill Indicates if the cell background must be painted (true) or transparent (false).
  4789. * @param mixed $link URL or identifier returned by AddLink().
  4790. * @param int $stretch font stretch mode: <ul><li>0 = disabled</li><li>1 = horizontal scaling only if text is larger than cell width</li><li>2 = forced horizontal scaling to fit cell width</li><li>3 = character spacing only if text is larger than cell width</li><li>4 = forced character spacing to fit cell width</li></ul> General font stretching and scaling values will be preserved when possible.
  4791. * @param boolean $ignore_min_height if true ignore automatic minimum height value.
  4792. * @param string $calign cell vertical alignment relative to the specified Y value. Possible values are:<ul><li>T : cell top</li><li>C : center</li><li>B : cell bottom</li><li>A : font top</li><li>L : font baseline</li><li>D : font bottom</li></ul>
  4793. * @param string $valign text vertical alignment inside the cell. Possible values are:<ul><li>T : top</li><li>M : middle</li><li>B : bottom</li></ul>
  4794. * @return string containing cell code
  4795. * @protected
  4796. * @since 1.0
  4797. * @see Cell()
  4798. */
  4799. protected function getCellCode($w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=false, $link='', $stretch=0, $ignore_min_height=false, $calign='T', $valign='M') {
  4800. // replace 'NO-BREAK SPACE' (U+00A0) character with a simple space
  4801. $txt = is_null($txt) ? '' : $txt;
  4802. $txt = str_replace(TCPDF_FONTS::unichr(160, $this->isunicode), ' ', $txt);
  4803. $prev_cell_margin = $this->cell_margin;
  4804. $prev_cell_padding = $this->cell_padding;
  4805. $txt = TCPDF_STATIC::removeSHY($txt, $this->isunicode);
  4806. $rs = ''; //string to be returned
  4807. $this->adjustCellPadding($border);
  4808. if (!$ignore_min_height) {
  4809. $min_cell_height = $this->getCellHeight($this->FontSize);
  4810. if ($h < $min_cell_height) {
  4811. $h = $min_cell_height;
  4812. }
  4813. }
  4814. $k = $this->k;
  4815. // check page for no-write regions and adapt page margins if necessary
  4816. list($this->x, $this->y) = $this->checkPageRegions($h, $this->x, $this->y);
  4817. if ($this->rtl) {
  4818. $x = $this->x - $this->cell_margin['R'];
  4819. } else {
  4820. $x = $this->x + $this->cell_margin['L'];
  4821. }
  4822. $y = $this->y + $this->cell_margin['T'];
  4823. $prev_font_stretching = $this->font_stretching;
  4824. $prev_font_spacing = $this->font_spacing;
  4825. // cell vertical alignment
  4826. switch ($calign) {
  4827. case 'A': {
  4828. // font top
  4829. switch ($valign) {
  4830. case 'T': {
  4831. // top
  4832. $y -= $this->cell_padding['T'];
  4833. break;
  4834. }
  4835. case 'B': {
  4836. // bottom
  4837. $y -= ($h - $this->cell_padding['B'] - $this->FontAscent - $this->FontDescent);
  4838. break;
  4839. }
  4840. default:
  4841. case 'C':
  4842. case 'M': {
  4843. // center
  4844. $y -= (($h - $this->FontAscent - $this->FontDescent) / 2);
  4845. break;
  4846. }
  4847. }
  4848. break;
  4849. }
  4850. case 'L': {
  4851. // font baseline
  4852. switch ($valign) {
  4853. case 'T': {
  4854. // top
  4855. $y -= ($this->cell_padding['T'] + $this->FontAscent);
  4856. break;
  4857. }
  4858. case 'B': {
  4859. // bottom
  4860. $y -= ($h - $this->cell_padding['B'] - $this->FontDescent);
  4861. break;
  4862. }
  4863. default:
  4864. case 'C':
  4865. case 'M': {
  4866. // center
  4867. $y -= (($h + $this->FontAscent - $this->FontDescent) / 2);
  4868. break;
  4869. }
  4870. }
  4871. break;
  4872. }
  4873. case 'D': {
  4874. // font bottom
  4875. switch ($valign) {
  4876. case 'T': {
  4877. // top
  4878. $y -= ($this->cell_padding['T'] + $this->FontAscent + $this->FontDescent);
  4879. break;
  4880. }
  4881. case 'B': {
  4882. // bottom
  4883. $y -= ($h - $this->cell_padding['B']);
  4884. break;
  4885. }
  4886. default:
  4887. case 'C':
  4888. case 'M': {
  4889. // center
  4890. $y -= (($h + $this->FontAscent + $this->FontDescent) / 2);
  4891. break;
  4892. }
  4893. }
  4894. break;
  4895. }
  4896. case 'B': {
  4897. // cell bottom
  4898. $y -= $h;
  4899. break;
  4900. }
  4901. case 'C':
  4902. case 'M': {
  4903. // cell center
  4904. $y -= ($h / 2);
  4905. break;
  4906. }
  4907. default:
  4908. case 'T': {
  4909. // cell top
  4910. break;
  4911. }
  4912. }
  4913. // text vertical alignment
  4914. switch ($valign) {
  4915. case 'T': {
  4916. // top
  4917. $yt = $y + $this->cell_padding['T'];
  4918. break;
  4919. }
  4920. case 'B': {
  4921. // bottom
  4922. $yt = $y + $h - $this->cell_padding['B'] - $this->FontAscent - $this->FontDescent;
  4923. break;
  4924. }
  4925. default:
  4926. case 'C':
  4927. case 'M': {
  4928. // center
  4929. $yt = $y + (($h - $this->FontAscent - $this->FontDescent) / 2);
  4930. break;
  4931. }
  4932. }
  4933. $basefonty = $yt + $this->FontAscent;
  4934. if (TCPDF_STATIC::empty_string($w) OR ($w <= 0)) {
  4935. if ($this->rtl) {
  4936. $w = $x - $this->lMargin;
  4937. } else {
  4938. $w = $this->w - $this->rMargin - $x;
  4939. }
  4940. }
  4941. $s = '';
  4942. // fill and borders
  4943. if (is_string($border) AND (strlen($border) == 4)) {
  4944. // full border
  4945. $border = 1;
  4946. }
  4947. if ($fill OR ($border == 1)) {
  4948. if ($fill) {
  4949. $op = ($border == 1) ? 'B' : 'f';
  4950. } else {
  4951. $op = 'S';
  4952. }
  4953. if ($this->rtl) {
  4954. $xk = (($x - $w) * $k);
  4955. } else {
  4956. $xk = ($x * $k);
  4957. }
  4958. $s .= sprintf('%F %F %F %F re %s ', $xk, (($this->h - $y) * $k), ($w * $k), (-$h * $k), $op);
  4959. }
  4960. // draw borders
  4961. $s .= $this->getCellBorder($x, $y, $w, $h, $border);
  4962. if ($txt != '') {
  4963. $txt2 = $txt;
  4964. if ($this->isunicode) {
  4965. if (($this->CurrentFont['type'] == 'core') OR ($this->CurrentFont['type'] == 'TrueType') OR ($this->CurrentFont['type'] == 'Type1')) {
  4966. $txt2 = TCPDF_FONTS::UTF8ToLatin1($txt2, $this->isunicode, $this->CurrentFont);
  4967. } else {
  4968. $unicode = TCPDF_FONTS::UTF8StringToArray($txt, $this->isunicode, $this->CurrentFont); // array of UTF-8 unicode values
  4969. $unicode = TCPDF_FONTS::utf8Bidi($unicode, '', $this->tmprtl, $this->isunicode, $this->CurrentFont);
  4970. // replace thai chars (if any)
  4971. if (defined('K_THAI_TOPCHARS') AND (K_THAI_TOPCHARS == true)) {
  4972. // number of chars
  4973. $numchars = count($unicode);
  4974. // po pla, for far, for fan
  4975. $longtail = array(0x0e1b, 0x0e1d, 0x0e1f);
  4976. // do chada, to patak
  4977. $lowtail = array(0x0e0e, 0x0e0f);
  4978. // mai hun arkad, sara i, sara ii, sara ue, sara uee
  4979. $upvowel = array(0x0e31, 0x0e34, 0x0e35, 0x0e36, 0x0e37);
  4980. // mai ek, mai tho, mai tri, mai chattawa, karan
  4981. $tonemark = array(0x0e48, 0x0e49, 0x0e4a, 0x0e4b, 0x0e4c);
  4982. // sara u, sara uu, pinthu
  4983. $lowvowel = array(0x0e38, 0x0e39, 0x0e3a);
  4984. $output = array();
  4985. for ($i = 0; $i < $numchars; $i++) {
  4986. if (($unicode[$i] >= 0x0e00) && ($unicode[$i] <= 0x0e5b)) {
  4987. $ch0 = $unicode[$i];
  4988. $ch1 = ($i > 0) ? $unicode[($i - 1)] : 0;
  4989. $ch2 = ($i > 1) ? $unicode[($i - 2)] : 0;
  4990. $chn = ($i < ($numchars - 1)) ? $unicode[($i + 1)] : 0;
  4991. if (in_array($ch0, $tonemark)) {
  4992. if ($chn == 0x0e33) {
  4993. // sara um
  4994. if (in_array($ch1, $longtail)) {
  4995. // tonemark at upper left
  4996. $output[] = $this->replaceChar($ch0, (0xf713 + $ch0 - 0x0e48));
  4997. } else {
  4998. // tonemark at upper right (normal position)
  4999. $output[] = $ch0;
  5000. }
  5001. } elseif (in_array($ch1, $longtail) OR (in_array($ch2, $longtail) AND in_array($ch1, $lowvowel))) {
  5002. // tonemark at lower left
  5003. $output[] = $this->replaceChar($ch0, (0xf705 + $ch0 - 0x0e48));
  5004. } elseif (in_array($ch1, $upvowel)) {
  5005. if (in_array($ch2, $longtail)) {
  5006. // tonemark at upper left
  5007. $output[] = $this->replaceChar($ch0, (0xf713 + $ch0 - 0x0e48));
  5008. } else {
  5009. // tonemark at upper right (normal position)
  5010. $output[] = $ch0;
  5011. }
  5012. } else {
  5013. // tonemark at lower right
  5014. $output[] = $this->replaceChar($ch0, (0xf70a + $ch0 - 0x0e48));
  5015. }
  5016. } elseif (($ch0 == 0x0e33) AND (in_array($ch1, $longtail) OR (in_array($ch2, $longtail) AND in_array($ch1, $tonemark)))) {
  5017. // add lower left nikhahit and sara aa
  5018. if ($this->isCharDefined(0xf711) AND $this->isCharDefined(0x0e32)) {
  5019. $output[] = 0xf711;
  5020. $this->CurrentFont['subsetchars'][0xf711] = true;
  5021. $output[] = 0x0e32;
  5022. $this->CurrentFont['subsetchars'][0x0e32] = true;
  5023. } else {
  5024. $output[] = $ch0;
  5025. }
  5026. } elseif (in_array($ch1, $longtail)) {
  5027. if ($ch0 == 0x0e31) {
  5028. // lower left mai hun arkad
  5029. $output[] = $this->replaceChar($ch0, 0xf710);
  5030. } elseif (in_array($ch0, $upvowel)) {
  5031. // lower left
  5032. $output[] = $this->replaceChar($ch0, (0xf701 + $ch0 - 0x0e34));
  5033. } elseif ($ch0 == 0x0e47) {
  5034. // lower left mai tai koo
  5035. $output[] = $this->replaceChar($ch0, 0xf712);
  5036. } else {
  5037. // normal character
  5038. $output[] = $ch0;
  5039. }
  5040. } elseif (in_array($ch1, $lowtail) AND in_array($ch0, $lowvowel)) {
  5041. // lower vowel
  5042. $output[] = $this->replaceChar($ch0, (0xf718 + $ch0 - 0x0e38));
  5043. } elseif (($ch0 == 0x0e0d) AND in_array($chn, $lowvowel)) {
  5044. // yo ying without lower part
  5045. $output[] = $this->replaceChar($ch0, 0xf70f);
  5046. } elseif (($ch0 == 0x0e10) AND in_array($chn, $lowvowel)) {
  5047. // tho santan without lower part
  5048. $output[] = $this->replaceChar($ch0, 0xf700);
  5049. } else {
  5050. $output[] = $ch0;
  5051. }
  5052. } else {
  5053. // non-thai character
  5054. $output[] = $unicode[$i];
  5055. }
  5056. }
  5057. $unicode = $output;
  5058. // update font subsetchars
  5059. $this->setFontSubBuffer($this->CurrentFont['fontkey'], 'subsetchars', $this->CurrentFont['subsetchars']);
  5060. } // end of K_THAI_TOPCHARS
  5061. $txt2 = TCPDF_FONTS::arrUTF8ToUTF16BE($unicode, false);
  5062. }
  5063. }
  5064. $txt2 = TCPDF_STATIC::_escape($txt2);
  5065. // get current text width (considering general font stretching and spacing)
  5066. $txwidth = $this->GetStringWidth($txt);
  5067. $width = $txwidth;
  5068. // check for stretch mode
  5069. if ($stretch > 0) {
  5070. // calculate ratio between cell width and text width
  5071. if ($width <= 0) {
  5072. $ratio = 1;
  5073. } else {
  5074. $ratio = (($w - $this->cell_padding['L'] - $this->cell_padding['R']) / $width);
  5075. }
  5076. // check if stretching is required
  5077. if (($ratio < 1) OR (($ratio > 1) AND (($stretch % 2) == 0))) {
  5078. // the text will be stretched to fit cell width
  5079. if ($stretch > 2) {
  5080. // set new character spacing
  5081. $this->font_spacing += ($w - $this->cell_padding['L'] - $this->cell_padding['R'] - $width) / (max(($this->GetNumChars($txt) - 1), 1) * ($this->font_stretching / 100));
  5082. } else {
  5083. // set new horizontal stretching
  5084. $this->font_stretching *= $ratio;
  5085. }
  5086. // recalculate text width (the text fills the entire cell)
  5087. $width = $w - $this->cell_padding['L'] - $this->cell_padding['R'];
  5088. // reset alignment
  5089. $align = '';
  5090. }
  5091. }
  5092. if ($this->font_stretching != 100) {
  5093. // apply font stretching
  5094. $rs .= sprintf('BT %F Tz ET ', $this->font_stretching);
  5095. }
  5096. if ($this->font_spacing != 0) {
  5097. // increase/decrease font spacing
  5098. $rs .= sprintf('BT %F Tc ET ', ($this->font_spacing * $this->k));
  5099. }
  5100. if ($this->ColorFlag AND ($this->textrendermode < 4)) {
  5101. $s .= 'q '.$this->TextColor.' ';
  5102. }
  5103. // rendering mode
  5104. $s .= sprintf('BT %d Tr %F w ET ', $this->textrendermode, ($this->textstrokewidth * $this->k));
  5105. // count number of spaces
  5106. $ns = substr_count($txt, chr(32));
  5107. // Justification
  5108. $spacewidth = 0;
  5109. if (($align == 'J') AND ($ns > 0)) {
  5110. if ($this->isUnicodeFont()) {
  5111. // get string width without spaces
  5112. $width = $this->GetStringWidth(str_replace(' ', '', $txt));
  5113. // calculate average space width
  5114. $spacewidth = -1000 * ($w - $width - $this->cell_padding['L'] - $this->cell_padding['R']) / ($ns?$ns:1) / ($this->FontSize?$this->FontSize:1);
  5115. if ($this->font_stretching != 100) {
  5116. // word spacing is affected by stretching
  5117. $spacewidth /= ($this->font_stretching / 100);
  5118. }
  5119. // set word position to be used with TJ operator
  5120. $txt2 = str_replace(chr(0).chr(32), ') '.sprintf('%F', $spacewidth).' (', $txt2);
  5121. $unicode_justification = true;
  5122. } else {
  5123. // get string width
  5124. $width = $txwidth;
  5125. // new space width
  5126. $spacewidth = (($w - $width - $this->cell_padding['L'] - $this->cell_padding['R']) / ($ns?$ns:1)) * $this->k;
  5127. if ($this->font_stretching != 100) {
  5128. // word spacing (Tw) is affected by stretching
  5129. $spacewidth /= ($this->font_stretching / 100);
  5130. }
  5131. // set word spacing
  5132. $rs .= sprintf('BT %F Tw ET ', $spacewidth);
  5133. }
  5134. $width = $w - $this->cell_padding['L'] - $this->cell_padding['R'];
  5135. }
  5136. // replace carriage return characters
  5137. $txt2 = str_replace("\r", ' ', $txt2);
  5138. switch ($align) {
  5139. case 'C': {
  5140. $dx = ($w - $width) / 2;
  5141. break;
  5142. }
  5143. case 'R': {
  5144. if ($this->rtl) {
  5145. $dx = $this->cell_padding['R'];
  5146. } else {
  5147. $dx = $w - $width - $this->cell_padding['R'];
  5148. }
  5149. break;
  5150. }
  5151. case 'L': {
  5152. if ($this->rtl) {
  5153. $dx = $w - $width - $this->cell_padding['L'];
  5154. } else {
  5155. $dx = $this->cell_padding['L'];
  5156. }
  5157. break;
  5158. }
  5159. case 'J':
  5160. default: {
  5161. if ($this->rtl) {
  5162. $dx = $this->cell_padding['R'];
  5163. } else {
  5164. $dx = $this->cell_padding['L'];
  5165. }
  5166. break;
  5167. }
  5168. }
  5169. if ($this->rtl) {
  5170. $xdx = $x - $dx - $width;
  5171. } else {
  5172. $xdx = $x + $dx;
  5173. }
  5174. $xdk = $xdx * $k;
  5175. // print text
  5176. $s .= sprintf('BT %F %F Td [(%s)] TJ ET', $xdk, (($this->h - $basefonty) * $k), $txt2);
  5177. if (isset($uniblock)) { // @phpstan-ignore-line
  5178. // print overlapping characters as separate string
  5179. $xshift = 0; // horizontal shift
  5180. $ty = (($this->h - $basefonty + (0.2 * $this->FontSize)) * $k);
  5181. $spw = (($w - $txwidth - $this->cell_padding['L'] - $this->cell_padding['R']) / ($ns?$ns:1));
  5182. foreach ($uniblock as $uk => $uniarr) { // @phpstan-ignore-line
  5183. if (($uk % 2) == 0) {
  5184. // x space to skip
  5185. if ($spacewidth != 0) {
  5186. // justification shift
  5187. $xshift += (count(array_keys($uniarr, 32)) * $spw);
  5188. }
  5189. $xshift += $this->GetArrStringWidth($uniarr); // + shift justification
  5190. } else {
  5191. // character to print
  5192. $topchr = TCPDF_FONTS::arrUTF8ToUTF16BE($uniarr, false);
  5193. $topchr = TCPDF_STATIC::_escape($topchr);
  5194. $s .= sprintf(' BT %F %F Td [(%s)] TJ ET', ($xdk + ($xshift * $k)), $ty, $topchr);
  5195. }
  5196. }
  5197. }
  5198. if ($this->underline) {
  5199. $s .= ' '.$this->_dounderlinew($xdx, $basefonty, $width);
  5200. }
  5201. if ($this->linethrough) {
  5202. $s .= ' '.$this->_dolinethroughw($xdx, $basefonty, $width);
  5203. }
  5204. if ($this->overline) {
  5205. $s .= ' '.$this->_dooverlinew($xdx, $basefonty, $width);
  5206. }
  5207. if ($this->ColorFlag AND ($this->textrendermode < 4)) {
  5208. $s .= ' Q';
  5209. }
  5210. if ($link) {
  5211. $this->Link($xdx, $yt, $width, ($this->FontAscent + $this->FontDescent), $link, $ns);
  5212. }
  5213. }
  5214. // output cell
  5215. if ($s) {
  5216. // output cell
  5217. $rs .= $s;
  5218. if ($this->font_spacing != 0) {
  5219. // reset font spacing mode
  5220. $rs .= ' BT 0 Tc ET';
  5221. }
  5222. if ($this->font_stretching != 100) {
  5223. // reset font stretching mode
  5224. $rs .= ' BT 100 Tz ET';
  5225. }
  5226. }
  5227. // reset word spacing
  5228. if (!$this->isUnicodeFont() AND ($align == 'J')) {
  5229. $rs .= ' BT 0 Tw ET';
  5230. }
  5231. // reset stretching and spacing
  5232. $this->font_stretching = $prev_font_stretching;
  5233. $this->font_spacing = $prev_font_spacing;
  5234. $this->lasth = $h;
  5235. if ($ln > 0) {
  5236. //Go to the beginning of the next line
  5237. $this->y = $y + $h + $this->cell_margin['B'];
  5238. if ($ln == 1) {
  5239. if ($this->rtl) {
  5240. $this->x = $this->w - $this->rMargin;
  5241. } else {
  5242. $this->x = $this->lMargin;
  5243. }
  5244. }
  5245. } else {
  5246. // go left or right by case
  5247. if ($this->rtl) {
  5248. $this->x = $x - $w - $this->cell_margin['L'];
  5249. } else {
  5250. $this->x = $x + $w + $this->cell_margin['R'];
  5251. }
  5252. }
  5253. $gstyles = ''.$this->linestyleWidth.' '.$this->linestyleCap.' '.$this->linestyleJoin.' '.$this->linestyleDash.' '.$this->DrawColor.' '.$this->FillColor."\n";
  5254. $rs = $gstyles.$rs;
  5255. $this->cell_padding = $prev_cell_padding;
  5256. $this->cell_margin = $prev_cell_margin;
  5257. return $rs;
  5258. }
  5259. /**
  5260. * Replace a char if is defined on the current font.
  5261. * @param int $oldchar Integer code (unicode) of the character to replace.
  5262. * @param int $newchar Integer code (unicode) of the new character.
  5263. * @return int the replaced char or the old char in case the new char i not defined
  5264. * @protected
  5265. * @since 5.9.167 (2012-06-22)
  5266. */
  5267. protected function replaceChar($oldchar, $newchar) {
  5268. if ($this->isCharDefined($newchar)) {
  5269. // add the new char on the subset list
  5270. $this->CurrentFont['subsetchars'][$newchar] = true;
  5271. // return the new character
  5272. return $newchar;
  5273. }
  5274. // return the old char
  5275. return $oldchar;
  5276. }
  5277. /**
  5278. * Returns the code to draw the cell border
  5279. * @param float $x X coordinate.
  5280. * @param float $y Y coordinate.
  5281. * @param float $w Cell width.
  5282. * @param float $h Cell height.
  5283. * @param string|array|int $brd Indicates if borders must be drawn around the cell. The value can be a number:<ul><li>0: no border (default)</li><li>1: frame</li></ul> or a string containing some or all of the following characters (in any order):<ul><li>L: left</li><li>T: top</li><li>R: right</li><li>B: bottom</li></ul> or an array of line styles for each border group - for example: array('LTRB' => array('width' => 2, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)))
  5284. * @return string containing cell border code
  5285. * @protected
  5286. * @see SetLineStyle()
  5287. * @since 5.7.000 (2010-08-02)
  5288. */
  5289. protected function getCellBorder($x, $y, $w, $h, $brd) {
  5290. $s = ''; // string to be returned
  5291. if (empty($brd)) {
  5292. return $s;
  5293. }
  5294. if ($brd == 1) {
  5295. $brd = array('LRTB' => true);
  5296. }
  5297. // calculate coordinates for border
  5298. $k = $this->k;
  5299. if ($this->rtl) {
  5300. $xeL = ($x - $w) * $k;
  5301. $xeR = $x * $k;
  5302. } else {
  5303. $xeL = $x * $k;
  5304. $xeR = ($x + $w) * $k;
  5305. }
  5306. $yeL = (($this->h - ($y + $h)) * $k);
  5307. $yeT = (($this->h - $y) * $k);
  5308. $xeT = $xeL;
  5309. $xeB = $xeR;
  5310. $yeR = $yeT;
  5311. $yeB = $yeL;
  5312. if (is_string($brd)) {
  5313. // convert string to array
  5314. $slen = strlen($brd);
  5315. $newbrd = array();
  5316. for ($i = 0; $i < $slen; ++$i) {
  5317. $newbrd[$brd[$i]] = array('cap' => 'square', 'join' => 'miter');
  5318. }
  5319. $brd = $newbrd;
  5320. }
  5321. if (isset($brd['mode'])) {
  5322. $mode = $brd['mode'];
  5323. unset($brd['mode']);
  5324. } else {
  5325. $mode = 'normal';
  5326. }
  5327. foreach ($brd as $border => $style) {
  5328. if (is_array($style) AND !empty($style)) {
  5329. // apply border style
  5330. $prev_style = $this->linestyleWidth.' '.$this->linestyleCap.' '.$this->linestyleJoin.' '.$this->linestyleDash.' '.$this->DrawColor.' ';
  5331. $s .= $this->setLineStyle($style, true)."\n";
  5332. }
  5333. switch ($mode) {
  5334. case 'ext': {
  5335. $off = (($this->LineWidth / 2) * $k);
  5336. $xL = $xeL - $off;
  5337. $xR = $xeR + $off;
  5338. $yT = $yeT + $off;
  5339. $yL = $yeL - $off;
  5340. $xT = $xL;
  5341. $xB = $xR;
  5342. $yR = $yT;
  5343. $yB = $yL;
  5344. $w += $this->LineWidth;
  5345. $h += $this->LineWidth;
  5346. break;
  5347. }
  5348. case 'int': {
  5349. $off = ($this->LineWidth / 2) * $k;
  5350. $xL = $xeL + $off;
  5351. $xR = $xeR - $off;
  5352. $yT = $yeT - $off;
  5353. $yL = $yeL + $off;
  5354. $xT = $xL;
  5355. $xB = $xR;
  5356. $yR = $yT;
  5357. $yB = $yL;
  5358. $w -= $this->LineWidth;
  5359. $h -= $this->LineWidth;
  5360. break;
  5361. }
  5362. case 'normal':
  5363. default: {
  5364. $xL = $xeL;
  5365. $xT = $xeT;
  5366. $xB = $xeB;
  5367. $xR = $xeR;
  5368. $yL = $yeL;
  5369. $yT = $yeT;
  5370. $yB = $yeB;
  5371. $yR = $yeR;
  5372. break;
  5373. }
  5374. }
  5375. // draw borders by case
  5376. if (strlen($border) == 4) {
  5377. $s .= sprintf('%F %F %F %F re S ', $xT, $yT, ($w * $k), (-$h * $k));
  5378. } elseif (strlen($border) == 3) {
  5379. if (strpos($border,'B') === false) { // LTR
  5380. $s .= sprintf('%F %F m ', $xL, $yL);
  5381. $s .= sprintf('%F %F l ', $xT, $yT);
  5382. $s .= sprintf('%F %F l ', $xR, $yR);
  5383. $s .= sprintf('%F %F l ', $xB, $yB);
  5384. $s .= 'S ';
  5385. } elseif (strpos($border,'L') === false) { // TRB
  5386. $s .= sprintf('%F %F m ', $xT, $yT);
  5387. $s .= sprintf('%F %F l ', $xR, $yR);
  5388. $s .= sprintf('%F %F l ', $xB, $yB);
  5389. $s .= sprintf('%F %F l ', $xL, $yL);
  5390. $s .= 'S ';
  5391. } elseif (strpos($border,'T') === false) { // RBL
  5392. $s .= sprintf('%F %F m ', $xR, $yR);
  5393. $s .= sprintf('%F %F l ', $xB, $yB);
  5394. $s .= sprintf('%F %F l ', $xL, $yL);
  5395. $s .= sprintf('%F %F l ', $xT, $yT);
  5396. $s .= 'S ';
  5397. } elseif (strpos($border,'R') === false) { // BLT
  5398. $s .= sprintf('%F %F m ', $xB, $yB);
  5399. $s .= sprintf('%F %F l ', $xL, $yL);
  5400. $s .= sprintf('%F %F l ', $xT, $yT);
  5401. $s .= sprintf('%F %F l ', $xR, $yR);
  5402. $s .= 'S ';
  5403. }
  5404. } elseif (strlen($border) == 2) {
  5405. if ((strpos($border,'L') !== false) AND (strpos($border,'T') !== false)) { // LT
  5406. $s .= sprintf('%F %F m ', $xL, $yL);
  5407. $s .= sprintf('%F %F l ', $xT, $yT);
  5408. $s .= sprintf('%F %F l ', $xR, $yR);
  5409. $s .= 'S ';
  5410. } elseif ((strpos($border,'T') !== false) AND (strpos($border,'R') !== false)) { // TR
  5411. $s .= sprintf('%F %F m ', $xT, $yT);
  5412. $s .= sprintf('%F %F l ', $xR, $yR);
  5413. $s .= sprintf('%F %F l ', $xB, $yB);
  5414. $s .= 'S ';
  5415. } elseif ((strpos($border,'R') !== false) AND (strpos($border,'B') !== false)) { // RB
  5416. $s .= sprintf('%F %F m ', $xR, $yR);
  5417. $s .= sprintf('%F %F l ', $xB, $yB);
  5418. $s .= sprintf('%F %F l ', $xL, $yL);
  5419. $s .= 'S ';
  5420. } elseif ((strpos($border,'B') !== false) AND (strpos($border,'L') !== false)) { // BL
  5421. $s .= sprintf('%F %F m ', $xB, $yB);
  5422. $s .= sprintf('%F %F l ', $xL, $yL);
  5423. $s .= sprintf('%F %F l ', $xT, $yT);
  5424. $s .= 'S ';
  5425. } elseif ((strpos($border,'L') !== false) AND (strpos($border,'R') !== false)) { // LR
  5426. $s .= sprintf('%F %F m ', $xL, $yL);
  5427. $s .= sprintf('%F %F l ', $xT, $yT);
  5428. $s .= 'S ';
  5429. $s .= sprintf('%F %F m ', $xR, $yR);
  5430. $s .= sprintf('%F %F l ', $xB, $yB);
  5431. $s .= 'S ';
  5432. } elseif ((strpos($border,'T') !== false) AND (strpos($border,'B') !== false)) { // TB
  5433. $s .= sprintf('%F %F m ', $xT, $yT);
  5434. $s .= sprintf('%F %F l ', $xR, $yR);
  5435. $s .= 'S ';
  5436. $s .= sprintf('%F %F m ', $xB, $yB);
  5437. $s .= sprintf('%F %F l ', $xL, $yL);
  5438. $s .= 'S ';
  5439. }
  5440. } else { // strlen($border) == 1
  5441. if (strpos($border,'L') !== false) { // L
  5442. $s .= sprintf('%F %F m ', $xL, $yL);
  5443. $s .= sprintf('%F %F l ', $xT, $yT);
  5444. $s .= 'S ';
  5445. } elseif (strpos($border,'T') !== false) { // T
  5446. $s .= sprintf('%F %F m ', $xT, $yT);
  5447. $s .= sprintf('%F %F l ', $xR, $yR);
  5448. $s .= 'S ';
  5449. } elseif (strpos($border,'R') !== false) { // R
  5450. $s .= sprintf('%F %F m ', $xR, $yR);
  5451. $s .= sprintf('%F %F l ', $xB, $yB);
  5452. $s .= 'S ';
  5453. } elseif (strpos($border,'B') !== false) { // B
  5454. $s .= sprintf('%F %F m ', $xB, $yB);
  5455. $s .= sprintf('%F %F l ', $xL, $yL);
  5456. $s .= 'S ';
  5457. }
  5458. }
  5459. if (is_array($style) AND !empty($style)) {
  5460. // reset border style to previous value
  5461. $s .= "\n".$this->linestyleWidth.' '.$this->linestyleCap.' '.$this->linestyleJoin.' '.$this->linestyleDash.' '.$this->DrawColor."\n";
  5462. }
  5463. }
  5464. return $s;
  5465. }
  5466. /**
  5467. * This method allows printing text with line breaks.
  5468. * They can be automatic (as soon as the text reaches the right border of the cell) or explicit (via the \n character). As many cells as necessary are output, one below the other.<br />
  5469. * Text can be aligned, centered or justified. The cell block can be framed and the background painted.
  5470. * @param float $w Width of cells. If 0, they extend up to the right margin of the page.
  5471. * @param float $h Cell minimum height. The cell extends automatically if needed.
  5472. * @param string $txt String to print
  5473. * @param mixed $border Indicates if borders must be drawn around the cell. The value can be a number:<ul><li>0: no border (default)</li><li>1: frame</li></ul> or a string containing some or all of the following characters (in any order):<ul><li>L: left</li><li>T: top</li><li>R: right</li><li>B: bottom</li></ul> or an array of line styles for each border group - for example: array('LTRB' => array('width' => 2, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)))
  5474. * @param string $align Allows to center or align the text. Possible values are:<ul><li>L or empty string: left align</li><li>C: center</li><li>R: right align</li><li>J: justification (default value when $ishtml=false)</li></ul>
  5475. * @param boolean $fill Indicates if the cell background must be painted (true) or transparent (false).
  5476. * @param int $ln Indicates where the current position should go after the call. Possible values are:<ul><li>0: to the right</li><li>1: to the beginning of the next line [DEFAULT]</li><li>2: below</li></ul>
  5477. * @param float|null $x x position in user units
  5478. * @param float|null $y y position in user units
  5479. * @param boolean $reseth if true reset the last cell height (default true).
  5480. * @param int $stretch font stretch mode: <ul><li>0 = disabled</li><li>1 = horizontal scaling only if text is larger than cell width</li><li>2 = forced horizontal scaling to fit cell width</li><li>3 = character spacing only if text is larger than cell width</li><li>4 = forced character spacing to fit cell width</li></ul> General font stretching and scaling values will be preserved when possible.
  5481. * @param boolean $ishtml INTERNAL USE ONLY -- set to true if $txt is HTML content (default = false). Never set this parameter to true, use instead writeHTMLCell() or writeHTML() methods.
  5482. * @param boolean $autopadding if true, uses internal padding and automatically adjust it to account for line width.
  5483. * @param float $maxh maximum height. It should be >= $h and less then remaining space to the bottom of the page, or 0 for disable this feature. This feature works only when $ishtml=false.
  5484. * @param string $valign Vertical alignment of text (requires $maxh = $h > 0). Possible values are:<ul><li>T: TOP</li><li>M: middle</li><li>B: bottom</li></ul>. This feature works only when $ishtml=false and the cell must fit in a single page.
  5485. * @param boolean $fitcell if true attempt to fit all the text within the cell by reducing the font size (do not work in HTML mode). $maxh must be greater than 0 and equal to $h.
  5486. * @return int Return the number of cells or 1 for html mode.
  5487. * @public
  5488. * @since 1.3
  5489. * @see SetFont(), SetDrawColor(), SetFillColor(), SetTextColor(), SetLineWidth(), Cell(), Write(), SetAutoPageBreak()
  5490. */
  5491. public function MultiCell($w, $h, $txt, $border=0, $align='J', $fill=false, $ln=1, $x=null, $y=null, $reseth=true, $stretch=0, $ishtml=false, $autopadding=true, $maxh=0, $valign='T', $fitcell=false) {
  5492. $prev_cell_margin = $this->cell_margin;
  5493. $prev_cell_padding = $this->cell_padding;
  5494. // adjust internal padding
  5495. $this->adjustCellPadding($border);
  5496. $mc_padding = $this->cell_padding;
  5497. $mc_margin = $this->cell_margin;
  5498. $this->cell_padding['T'] = 0;
  5499. $this->cell_padding['B'] = 0;
  5500. $this->setCellMargins(0, 0, 0, 0);
  5501. if (TCPDF_STATIC::empty_string($this->lasth) OR $reseth) {
  5502. // reset row height
  5503. $this->resetLastH();
  5504. }
  5505. if (!TCPDF_STATIC::empty_string($y)) {
  5506. $this->setY($y); // set y in order to convert negative y values to positive ones
  5507. }
  5508. $y = $this->GetY();
  5509. $resth = 0;
  5510. if (($h > 0) AND $this->inPageBody() AND (($y + $h + $mc_margin['T'] + $mc_margin['B']) > $this->PageBreakTrigger)) {
  5511. // spit cell in more pages/columns
  5512. $newh = ($this->PageBreakTrigger - $y);
  5513. $resth = ($h - $newh); // cell to be printed on the next page/column
  5514. $h = $newh;
  5515. }
  5516. // get current page number
  5517. $startpage = $this->page;
  5518. // get current column
  5519. $startcolumn = $this->current_column;
  5520. if (!TCPDF_STATIC::empty_string($x)) {
  5521. $this->setX($x);
  5522. } else {
  5523. $x = $this->GetX();
  5524. }
  5525. // check page for no-write regions and adapt page margins if necessary
  5526. list($x, $y) = $this->checkPageRegions(0, $x, $y);
  5527. // apply margins
  5528. $oy = $y + $mc_margin['T'];
  5529. if ($this->rtl) {
  5530. $ox = ($this->w - $x - $mc_margin['R']);
  5531. } else {
  5532. $ox = ($x + $mc_margin['L']);
  5533. }
  5534. $this->x = $ox;
  5535. $this->y = $oy;
  5536. // set width
  5537. if (TCPDF_STATIC::empty_string($w) OR ($w <= 0)) {
  5538. if ($this->rtl) {
  5539. $w = ($this->x - $this->lMargin - $mc_margin['L']);
  5540. } else {
  5541. $w = ($this->w - $this->x - $this->rMargin - $mc_margin['R']);
  5542. }
  5543. }
  5544. // store original margin values
  5545. $lMargin = $this->lMargin;
  5546. $rMargin = $this->rMargin;
  5547. if ($this->rtl) {
  5548. $this->rMargin = ($this->w - $this->x);
  5549. $this->lMargin = ($this->x - $w);
  5550. } else {
  5551. $this->lMargin = ($this->x);
  5552. $this->rMargin = ($this->w - $this->x - $w);
  5553. }
  5554. $this->clMargin = $this->lMargin;
  5555. $this->crMargin = $this->rMargin;
  5556. if ($autopadding) {
  5557. // add top padding
  5558. $this->y += $mc_padding['T'];
  5559. }
  5560. if ($ishtml) { // ******* Write HTML text
  5561. $this->writeHTML($txt, true, false, $reseth, true, $align);
  5562. $nl = 1;
  5563. } else { // ******* Write simple text
  5564. $prev_FontSizePt = $this->FontSizePt;
  5565. if ($fitcell) {
  5566. // ajust height values
  5567. $tobottom = ($this->h - $this->y - $this->bMargin - $this->cell_padding['T'] - $this->cell_padding['B']);
  5568. $h = $maxh = max(min($h, $tobottom), min($maxh, $tobottom));
  5569. }
  5570. // vertical alignment
  5571. if ($maxh > 0) {
  5572. // get text height
  5573. $text_height = $this->getStringHeight($w, $txt, $reseth, $autopadding, $mc_padding, $border);
  5574. if ($fitcell AND ($text_height > $maxh) AND ($this->FontSizePt > 1)) {
  5575. // try to reduce font size to fit text on cell (use a quick search algorithm)
  5576. $fmin = 1;
  5577. $fmax = $this->FontSizePt;
  5578. $diff_epsilon = (1 / $this->k); // one point (min resolution)
  5579. $maxit = (2 * min(100, max(10, intval($fmax)))); // max number of iterations
  5580. while ($maxit >= 0) {
  5581. $fmid = (($fmax + $fmin) / 2);
  5582. $this->setFontSize($fmid, false);
  5583. $this->resetLastH();
  5584. $text_height = $this->getStringHeight($w, $txt, $reseth, $autopadding, $mc_padding, $border);
  5585. $diff = ($maxh - $text_height);
  5586. if ($diff >= 0) {
  5587. if ($diff <= $diff_epsilon) {
  5588. break;
  5589. }
  5590. $fmin = $fmid;
  5591. } else {
  5592. $fmax = $fmid;
  5593. }
  5594. --$maxit;
  5595. }
  5596. if ($maxit < 0) {
  5597. // premature exit, we get the minimum font value to fit the cell
  5598. $this->setFontSize($fmin);
  5599. $this->resetLastH();
  5600. $text_height = $this->getStringHeight($w, $txt, $reseth, $autopadding, $mc_padding, $border);
  5601. } else {
  5602. $this->setFontSize($fmid);
  5603. $this->resetLastH();
  5604. }
  5605. }
  5606. if ($text_height < $maxh) {
  5607. if ($valign == 'M') {
  5608. // text vertically centered
  5609. $this->y += (($maxh - $text_height) / 2);
  5610. } elseif ($valign == 'B') {
  5611. // text vertically aligned on bottom
  5612. $this->y += ($maxh - $text_height);
  5613. }
  5614. }
  5615. }
  5616. $nl = $this->Write($this->lasth, $txt, '', 0, $align, true, $stretch, false, true, $maxh, 0, $mc_margin);
  5617. if ($fitcell) {
  5618. // restore font size
  5619. $this->setFontSize($prev_FontSizePt);
  5620. }
  5621. }
  5622. if ($autopadding) {
  5623. // add bottom padding
  5624. $this->y += $mc_padding['B'];
  5625. }
  5626. // Get end-of-text Y position
  5627. $currentY = $this->y;
  5628. // get latest page number
  5629. $endpage = $this->page;
  5630. if ($resth > 0) {
  5631. $skip = ($endpage - $startpage);
  5632. $tmpresth = $resth;
  5633. while ($tmpresth > 0) {
  5634. if ($skip <= 0) {
  5635. // add a page (or trig AcceptPageBreak() for multicolumn mode)
  5636. $this->checkPageBreak($this->PageBreakTrigger + 1);
  5637. }
  5638. if ($this->num_columns > 1) {
  5639. $tmpresth -= ($this->h - $this->y - $this->bMargin);
  5640. } else {
  5641. $tmpresth -= ($this->h - $this->tMargin - $this->bMargin);
  5642. }
  5643. --$skip;
  5644. }
  5645. $currentY = $this->y;
  5646. $endpage = $this->page;
  5647. }
  5648. // get latest column
  5649. $endcolumn = $this->current_column;
  5650. if ($this->num_columns == 0) {
  5651. $this->num_columns = 1;
  5652. }
  5653. // disable page regions check
  5654. $check_page_regions = $this->check_page_regions;
  5655. $this->check_page_regions = false;
  5656. // get border modes
  5657. $border_start = TCPDF_STATIC::getBorderMode($border, $position='start', $this->opencell);
  5658. $border_end = TCPDF_STATIC::getBorderMode($border, $position='end', $this->opencell);
  5659. $border_middle = TCPDF_STATIC::getBorderMode($border, $position='middle', $this->opencell);
  5660. // design borders around HTML cells.
  5661. for ($page = $startpage; $page <= $endpage; ++$page) { // for each page
  5662. $ccode = '';
  5663. $this->setPage($page);
  5664. if ($this->num_columns < 2) {
  5665. // single-column mode
  5666. $this->setX($x);
  5667. $this->y = $this->tMargin;
  5668. }
  5669. // account for margin changes
  5670. if ($page > $startpage) {
  5671. if (($this->rtl) AND ($this->pagedim[$page]['orm'] != $this->pagedim[$startpage]['orm'])) {
  5672. $this->x -= ($this->pagedim[$page]['orm'] - $this->pagedim[$startpage]['orm']);
  5673. } elseif ((!$this->rtl) AND ($this->pagedim[$page]['olm'] != $this->pagedim[$startpage]['olm'])) {
  5674. $this->x += ($this->pagedim[$page]['olm'] - $this->pagedim[$startpage]['olm']);
  5675. }
  5676. }
  5677. if ($startpage == $endpage) {
  5678. // single page
  5679. for ($column = $startcolumn; $column <= $endcolumn; ++$column) { // for each column
  5680. if ($column != $this->current_column) {
  5681. $this->selectColumn($column);
  5682. }
  5683. if ($this->rtl) {
  5684. $this->x -= $mc_margin['R'];
  5685. } else {
  5686. $this->x += $mc_margin['L'];
  5687. }
  5688. if ($startcolumn == $endcolumn) { // single column
  5689. $cborder = $border;
  5690. $h = max($h, ($currentY - $oy));
  5691. $this->y = $oy;
  5692. } elseif ($column == $startcolumn) { // first column
  5693. $cborder = $border_start;
  5694. $this->y = $oy;
  5695. $h = $this->h - $this->y - $this->bMargin;
  5696. } elseif ($column == $endcolumn) { // end column
  5697. $cborder = $border_end;
  5698. $h = $currentY - $this->y;
  5699. if ($resth > $h) {
  5700. $h = $resth;
  5701. }
  5702. } else { // middle column
  5703. $cborder = $border_middle;
  5704. $h = $this->h - $this->y - $this->bMargin;
  5705. $resth -= $h;
  5706. }
  5707. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
  5708. } // end for each column
  5709. } elseif ($page == $startpage) { // first page
  5710. for ($column = $startcolumn; $column < $this->num_columns; ++$column) { // for each column
  5711. if ($column != $this->current_column) {
  5712. $this->selectColumn($column);
  5713. }
  5714. if ($this->rtl) {
  5715. $this->x -= $mc_margin['R'];
  5716. } else {
  5717. $this->x += $mc_margin['L'];
  5718. }
  5719. if ($column == $startcolumn) { // first column
  5720. $cborder = $border_start;
  5721. $this->y = $oy;
  5722. $h = $this->h - $this->y - $this->bMargin;
  5723. } else { // middle column
  5724. $cborder = $border_middle;
  5725. $h = $this->h - $this->y - $this->bMargin;
  5726. $resth -= $h;
  5727. }
  5728. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
  5729. } // end for each column
  5730. } elseif ($page == $endpage) { // last page
  5731. for ($column = 0; $column <= $endcolumn; ++$column) { // for each column
  5732. if ($column != $this->current_column) {
  5733. $this->selectColumn($column);
  5734. }
  5735. if ($this->rtl) {
  5736. $this->x -= $mc_margin['R'];
  5737. } else {
  5738. $this->x += $mc_margin['L'];
  5739. }
  5740. if ($column == $endcolumn) {
  5741. // end column
  5742. $cborder = $border_end;
  5743. $h = $currentY - $this->y;
  5744. if ($resth > $h) {
  5745. $h = $resth;
  5746. }
  5747. } else {
  5748. // middle column
  5749. $cborder = $border_middle;
  5750. $h = $this->h - $this->y - $this->bMargin;
  5751. $resth -= $h;
  5752. }
  5753. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
  5754. } // end for each column
  5755. } else { // middle page
  5756. for ($column = 0; $column < $this->num_columns; ++$column) { // for each column
  5757. $this->selectColumn($column);
  5758. if ($this->rtl) {
  5759. $this->x -= $mc_margin['R'];
  5760. } else {
  5761. $this->x += $mc_margin['L'];
  5762. }
  5763. $cborder = $border_middle;
  5764. $h = $this->h - $this->y - $this->bMargin;
  5765. $resth -= $h;
  5766. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
  5767. } // end for each column
  5768. }
  5769. if ($cborder OR $fill) {
  5770. $offsetlen = strlen($ccode);
  5771. // draw border and fill
  5772. if ($this->inxobj) {
  5773. // we are inside an XObject template
  5774. if (end($this->xobjects[$this->xobjid]['transfmrk']) !== false) {
  5775. $pagemarkkey = key($this->xobjects[$this->xobjid]['transfmrk']);
  5776. $pagemark = $this->xobjects[$this->xobjid]['transfmrk'][$pagemarkkey];
  5777. $this->xobjects[$this->xobjid]['transfmrk'][$pagemarkkey] += $offsetlen;
  5778. } else {
  5779. $pagemark = $this->xobjects[$this->xobjid]['intmrk'];
  5780. $this->xobjects[$this->xobjid]['intmrk'] += $offsetlen;
  5781. }
  5782. $pagebuff = $this->xobjects[$this->xobjid]['outdata'];
  5783. $pstart = substr($pagebuff, 0, $pagemark);
  5784. $pend = substr($pagebuff, $pagemark);
  5785. $this->xobjects[$this->xobjid]['outdata'] = $pstart.$ccode.$pend;
  5786. } else {
  5787. if (end($this->transfmrk[$this->page]) !== false) {
  5788. $pagemarkkey = key($this->transfmrk[$this->page]);
  5789. $pagemark = $this->transfmrk[$this->page][$pagemarkkey];
  5790. $this->transfmrk[$this->page][$pagemarkkey] += $offsetlen;
  5791. } elseif ($this->InFooter) {
  5792. $pagemark = $this->footerpos[$this->page];
  5793. $this->footerpos[$this->page] += $offsetlen;
  5794. } else {
  5795. $pagemark = $this->intmrk[$this->page];
  5796. $this->intmrk[$this->page] += $offsetlen;
  5797. }
  5798. $pagebuff = $this->getPageBuffer($this->page);
  5799. $pstart = substr($pagebuff, 0, $pagemark);
  5800. $pend = substr($pagebuff, $pagemark);
  5801. $this->setPageBuffer($this->page, $pstart.$ccode.$pend);
  5802. }
  5803. }
  5804. } // end for each page
  5805. // restore page regions check
  5806. $this->check_page_regions = $check_page_regions;
  5807. // Get end-of-cell Y position
  5808. $currentY = $this->GetY();
  5809. // restore previous values
  5810. if ($this->num_columns > 1) {
  5811. $this->selectColumn();
  5812. } else {
  5813. // restore original margins
  5814. $this->lMargin = $lMargin;
  5815. $this->rMargin = $rMargin;
  5816. if ($this->page > $startpage) {
  5817. // check for margin variations between pages (i.e. booklet mode)
  5818. $dl = ($this->pagedim[$this->page]['olm'] - $this->pagedim[$startpage]['olm']);
  5819. $dr = ($this->pagedim[$this->page]['orm'] - $this->pagedim[$startpage]['orm']);
  5820. if (($dl != 0) OR ($dr != 0)) {
  5821. $this->lMargin += $dl;
  5822. $this->rMargin += $dr;
  5823. }
  5824. }
  5825. }
  5826. if ($ln > 0) {
  5827. //Go to the beginning of the next line
  5828. $this->setY($currentY + $mc_margin['B']);
  5829. if ($ln == 2) {
  5830. $this->setX($x + $w + $mc_margin['L'] + $mc_margin['R']);
  5831. }
  5832. } else {
  5833. // go left or right by case
  5834. $this->setPage($startpage);
  5835. $this->y = $y;
  5836. $this->setX($x + $w + $mc_margin['L'] + $mc_margin['R']);
  5837. }
  5838. $this->setContentMark();
  5839. $this->cell_padding = $prev_cell_padding;
  5840. $this->cell_margin = $prev_cell_margin;
  5841. $this->clMargin = $this->lMargin;
  5842. $this->crMargin = $this->rMargin;
  5843. return $nl;
  5844. }
  5845. /**
  5846. * This method return the estimated number of lines for print a simple text string using Multicell() method.
  5847. * @param string $txt String for calculating his height
  5848. * @param float $w Width of cells. If 0, they extend up to the right margin of the page.
  5849. * @param boolean $reseth if true reset the last cell height (default false).
  5850. * @param boolean $autopadding if true, uses internal padding and automatically adjust it to account for line width (default true).
  5851. * @param array|null $cellpadding Internal cell padding, if empty uses default cell padding.
  5852. * @param mixed $border Indicates if borders must be drawn around the cell. The value can be a number:<ul><li>0: no border (default)</li><li>1: frame</li></ul> or a string containing some or all of the following characters (in any order):<ul><li>L: left</li><li>T: top</li><li>R: right</li><li>B: bottom</li></ul> or an array of line styles for each border group - for example: array('LTRB' => array('width' => 2, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)))
  5853. * @return float Return the minimal height needed for multicell method for printing the $txt param.
  5854. * @author Alexander Escalona Fern\E1ndez, Nicola Asuni
  5855. * @public
  5856. * @since 4.5.011
  5857. */
  5858. public function getNumLines($txt, $w=0, $reseth=false, $autopadding=true, $cellpadding=null, $border=0) {
  5859. if ($txt === NULL) {
  5860. return 0;
  5861. }
  5862. if ($txt === '') {
  5863. // empty string
  5864. return 1;
  5865. }
  5866. // adjust internal padding
  5867. $prev_cell_padding = $this->cell_padding;
  5868. $prev_lasth = $this->lasth;
  5869. if (is_array($cellpadding)) {
  5870. $this->cell_padding = $cellpadding;
  5871. }
  5872. $this->adjustCellPadding($border);
  5873. if (TCPDF_STATIC::empty_string($w) OR ($w <= 0)) {
  5874. if ($this->rtl) {
  5875. $w = $this->x - $this->lMargin;
  5876. } else {
  5877. $w = $this->w - $this->rMargin - $this->x;
  5878. }
  5879. }
  5880. $wmax = $w - $this->cell_padding['L'] - $this->cell_padding['R'];
  5881. if ($reseth) {
  5882. // reset row height
  5883. $this->resetLastH();
  5884. }
  5885. $lines = 1;
  5886. $sum = 0;
  5887. $chars = TCPDF_FONTS::utf8Bidi(TCPDF_FONTS::UTF8StringToArray($txt, $this->isunicode, $this->CurrentFont), $txt, $this->tmprtl, $this->isunicode, $this->CurrentFont);
  5888. $charsWidth = $this->GetArrStringWidth($chars, '', '', 0, true);
  5889. $length = count($chars);
  5890. $lastSeparator = -1;
  5891. for ($i = 0; $i < $length; ++$i) {
  5892. $c = $chars[$i];
  5893. $charWidth = $charsWidth[$i];
  5894. if (($c != 160)
  5895. AND (($c == 173)
  5896. OR preg_match($this->re_spaces, TCPDF_FONTS::unichr($c, $this->isunicode))
  5897. OR (($c == 45)
  5898. AND ($i > 0) AND ($i < ($length - 1))
  5899. AND @preg_match('/[\p{L}]/'.$this->re_space['m'], TCPDF_FONTS::unichr($chars[($i - 1)], $this->isunicode))
  5900. AND @preg_match('/[\p{L}]/'.$this->re_space['m'], TCPDF_FONTS::unichr($chars[($i + 1)], $this->isunicode))
  5901. )
  5902. )
  5903. ) {
  5904. $lastSeparator = $i;
  5905. }
  5906. if ((($sum + $charWidth) > $wmax) OR ($c == 10)) {
  5907. ++$lines;
  5908. if ($c == 10) {
  5909. $lastSeparator = -1;
  5910. $sum = 0;
  5911. } elseif ($lastSeparator != -1) {
  5912. $i = $lastSeparator;
  5913. $lastSeparator = -1;
  5914. $sum = 0;
  5915. } else {
  5916. $sum = $charWidth;
  5917. }
  5918. } else {
  5919. $sum += $charWidth;
  5920. }
  5921. }
  5922. if ($chars[($length - 1)] == 10) {
  5923. --$lines;
  5924. }
  5925. $this->cell_padding = $prev_cell_padding;
  5926. $this->lasth = $prev_lasth;
  5927. return $lines;
  5928. }
  5929. /**
  5930. * This method return the estimated height needed for printing a simple text string using the Multicell() method.
  5931. * Generally, if you want to know the exact height for a block of content you can use the following alternative technique:
  5932. * @pre
  5933. * // store current object
  5934. * $pdf->startTransaction();
  5935. * // store starting values
  5936. * $start_y = $pdf->GetY();
  5937. * $start_page = $pdf->getPage();
  5938. * // call your printing functions with your parameters
  5939. * // - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  5940. * $pdf->MultiCell($w=0, $h=0, $txt, $border=1, $align='L', $fill=false, $ln=1, $x=null, $y=null, $reseth=true, $stretch=0, $ishtml=false, $autopadding=true, $maxh=0);
  5941. * // - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  5942. * // get the new Y
  5943. * $end_y = $pdf->GetY();
  5944. * $end_page = $pdf->getPage();
  5945. * // calculate height
  5946. * $height = 0;
  5947. * if ($end_page == $start_page) {
  5948. * $height = $end_y - $start_y;
  5949. * } else {
  5950. * for ($page=$start_page; $page <= $end_page; ++$page) {
  5951. * $this->setPage($page);
  5952. * if ($page == $start_page) {
  5953. * // first page
  5954. * $height += $this->h - $start_y - $this->bMargin;
  5955. * } elseif ($page == $end_page) {
  5956. * // last page
  5957. * $height += $end_y - $this->tMargin;
  5958. * } else {
  5959. * $height += $this->h - $this->tMargin - $this->bMargin;
  5960. * }
  5961. * }
  5962. * }
  5963. * // restore previous object
  5964. * $pdf = $pdf->rollbackTransaction();
  5965. *
  5966. * @param float $w Width of cells. If 0, they extend up to the right margin of the page.
  5967. * @param string $txt String for calculating his height
  5968. * @param boolean $reseth if true reset the last cell height (default false).
  5969. * @param boolean $autopadding if true, uses internal padding and automatically adjust it to account for line width (default true).
  5970. * @param array|null $cellpadding Internal cell padding, if empty uses default cell padding.
  5971. * @param mixed $border Indicates if borders must be drawn around the cell. The value can be a number:<ul><li>0: no border (default)</li><li>1: frame</li></ul> or a string containing some or all of the following characters (in any order):<ul><li>L: left</li><li>T: top</li><li>R: right</li><li>B: bottom</li></ul> or an array of line styles for each border group - for example: array('LTRB' => array('width' => 2, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)))
  5972. * @return float Return the minimal height needed for multicell method for printing the $txt param.
  5973. * @author Nicola Asuni, Alexander Escalona Fern\E1ndez
  5974. * @public
  5975. */
  5976. public function getStringHeight($w, $txt, $reseth=false, $autopadding=true, $cellpadding=null, $border=0) {
  5977. // adjust internal padding
  5978. $prev_cell_padding = $this->cell_padding;
  5979. $prev_lasth = $this->lasth;
  5980. if (is_array($cellpadding)) {
  5981. $this->cell_padding = $cellpadding;
  5982. }
  5983. $this->adjustCellPadding($border);
  5984. $lines = $this->getNumLines($txt, $w, $reseth, $autopadding, $cellpadding, $border);
  5985. $height = $this->getCellHeight(($lines * $this->FontSize), $autopadding);
  5986. $this->cell_padding = $prev_cell_padding;
  5987. $this->lasth = $prev_lasth;
  5988. return $height;
  5989. }
  5990. /**
  5991. * This method prints text from the current position.<br />
  5992. * @param float $h Line height
  5993. * @param string $txt String to print
  5994. * @param mixed $link URL or identifier returned by AddLink()
  5995. * @param boolean $fill Indicates if the cell background must be painted (true) or transparent (false).
  5996. * @param string $align Allows to center or align the text. Possible values are:<ul><li>L or empty string: left align (default value)</li><li>C: center</li><li>R: right align</li><li>J: justify</li></ul>
  5997. * @param boolean $ln if true set cursor at the bottom of the line, otherwise set cursor at the top of the line.
  5998. * @param int $stretch font stretch mode: <ul><li>0 = disabled</li><li>1 = horizontal scaling only if text is larger than cell width</li><li>2 = forced horizontal scaling to fit cell width</li><li>3 = character spacing only if text is larger than cell width</li><li>4 = forced character spacing to fit cell width</li></ul> General font stretching and scaling values will be preserved when possible.
  5999. * @param boolean $firstline if true prints only the first line and return the remaining string.
  6000. * @param boolean $firstblock if true the string is the starting of a line.
  6001. * @param float $maxh maximum height. It should be >= $h and less then remaining space to the bottom of the page, or 0 for disable this feature.
  6002. * @param float $wadj first line width will be reduced by this amount (used in HTML mode).
  6003. * @param array|null $margin margin array of the parent container
  6004. * @return mixed Return the number of cells or the remaining string if $firstline = true.
  6005. * @public
  6006. * @since 1.5
  6007. */
  6008. public function Write($h, $txt, $link='', $fill=false, $align='', $ln=false, $stretch=0, $firstline=false, $firstblock=false, $maxh=0, $wadj=0, $margin=null) {
  6009. // check page for no-write regions and adapt page margins if necessary
  6010. list($this->x, $this->y) = $this->checkPageRegions($h, $this->x, $this->y);
  6011. if (strlen($txt) == 0) {
  6012. // fix empty text
  6013. $txt = ' ';
  6014. }
  6015. if (!is_array($margin)) {
  6016. // set default margins
  6017. $margin = $this->cell_margin;
  6018. }
  6019. // remove carriage returns
  6020. $s = str_replace("\r", '', $txt);
  6021. // check if string contains arabic text
  6022. if (preg_match(TCPDF_FONT_DATA::$uni_RE_PATTERN_ARABIC, $s)) {
  6023. $arabic = true;
  6024. } else {
  6025. $arabic = false;
  6026. }
  6027. // check if string contains RTL text
  6028. if ($arabic OR ($this->tmprtl == 'R') OR preg_match(TCPDF_FONT_DATA::$uni_RE_PATTERN_RTL, $s)) {
  6029. $rtlmode = true;
  6030. } else {
  6031. $rtlmode = false;
  6032. }
  6033. // get a char width
  6034. $chrwidth = $this->GetCharWidth(46); // dot character
  6035. // get array of unicode values
  6036. $chars = TCPDF_FONTS::UTF8StringToArray($s, $this->isunicode, $this->CurrentFont);
  6037. // calculate maximum width for a single character on string
  6038. $chrw = $this->GetArrStringWidth($chars, '', '', 0, true);
  6039. array_walk($chrw, array($this, 'getRawCharWidth'));
  6040. $maxchwidth = ((is_array($chrw) || $chrw instanceof Countable) && count($chrw) > 0) ? max($chrw) : 0;
  6041. // get array of chars
  6042. $uchars = TCPDF_FONTS::UTF8ArrayToUniArray($chars, $this->isunicode);
  6043. // get the number of characters
  6044. $nb = count($chars);
  6045. // replacement for SHY character (minus symbol)
  6046. $shy_replacement = 45;
  6047. $shy_replacement_char = TCPDF_FONTS::unichr($shy_replacement, $this->isunicode);
  6048. // widht for SHY replacement
  6049. $shy_replacement_width = $this->GetCharWidth($shy_replacement);
  6050. // page width
  6051. $pw = $w = $this->w - $this->lMargin - $this->rMargin;
  6052. // calculate remaining line width ($w)
  6053. if ($this->rtl) {
  6054. $w = $this->x - $this->lMargin;
  6055. } else {
  6056. $w = $this->w - $this->rMargin - $this->x;
  6057. }
  6058. // max column width
  6059. $wmax = ($w - $wadj);
  6060. if (!$firstline) {
  6061. $wmax -= ($this->cell_padding['L'] + $this->cell_padding['R']);
  6062. }
  6063. if ((!$firstline) AND (($chrwidth > $wmax) OR ($maxchwidth > $wmax))) {
  6064. // the maximum width character do not fit on column
  6065. return '';
  6066. }
  6067. // minimum row height
  6068. $row_height = max($h, $this->getCellHeight($this->FontSize));
  6069. // max Y
  6070. $maxy = $this->y + $maxh - max($row_height, $h);
  6071. $start_page = $this->page;
  6072. $i = 0; // character position
  6073. $j = 0; // current starting position
  6074. $sep = -1; // position of the last blank space
  6075. $prevsep = $sep; // previous separator
  6076. $shy = false; // true if the last blank is a soft hypen (SHY)
  6077. $prevshy = $shy; // previous shy mode
  6078. $l = 0; // current string length
  6079. $nl = 0; //number of lines
  6080. $linebreak = false;
  6081. $pc = 0; // previous character
  6082. // for each character
  6083. while ($i < $nb) {
  6084. if (($maxh > 0) AND ($this->y > $maxy) ) {
  6085. break;
  6086. }
  6087. //Get the current character
  6088. $c = $chars[$i];
  6089. if ($c == 10) { // 10 = "\n" = new line
  6090. //Explicit line break
  6091. if ($align == 'J') {
  6092. if ($this->rtl) {
  6093. $talign = 'R';
  6094. } else {
  6095. $talign = 'L';
  6096. }
  6097. } else {
  6098. $talign = $align;
  6099. }
  6100. $tmpstr = TCPDF_FONTS::UniArrSubString($uchars, $j, $i);
  6101. if ($firstline) {
  6102. $startx = $this->x;
  6103. $tmparr = array_slice($chars, $j, ($i - $j));
  6104. if ($rtlmode) {
  6105. $tmparr = TCPDF_FONTS::utf8Bidi($tmparr, $tmpstr, $this->tmprtl, $this->isunicode, $this->CurrentFont);
  6106. }
  6107. $linew = $this->GetArrStringWidth($tmparr);
  6108. unset($tmparr);
  6109. if ($this->rtl) {
  6110. $this->endlinex = $startx - $linew;
  6111. } else {
  6112. $this->endlinex = $startx + $linew;
  6113. }
  6114. $w = $linew;
  6115. $tmpcellpadding = $this->cell_padding;
  6116. if ($maxh == 0) {
  6117. $this->setCellPadding(0);
  6118. }
  6119. }
  6120. if ($firstblock AND $this->isRTLTextDir()) {
  6121. $tmpstr = $this->stringRightTrim($tmpstr);
  6122. }
  6123. // Skip newlines at the beginning of a page or column
  6124. if (!empty($tmpstr) OR ($this->y < ($this->PageBreakTrigger - $row_height))) {
  6125. $this->Cell($w, $h, $tmpstr, 0, 1, $talign, $fill, $link, $stretch);
  6126. }
  6127. unset($tmpstr);
  6128. if ($firstline) {
  6129. $this->cell_padding = $tmpcellpadding;
  6130. return (TCPDF_FONTS::UniArrSubString($uchars, $i));
  6131. }
  6132. ++$nl;
  6133. $j = $i + 1;
  6134. $l = 0;
  6135. $sep = -1;
  6136. $prevsep = $sep;
  6137. $shy = false;
  6138. // account for margin changes
  6139. if ((($this->y + $this->lasth) > $this->PageBreakTrigger) AND ($this->inPageBody())) {
  6140. if ($this->AcceptPageBreak())
  6141. {
  6142. if ($this->rtl) {
  6143. $this->x -= $margin['R'];
  6144. } else {
  6145. $this->x += $margin['L'];
  6146. }
  6147. $this->lMargin += $margin['L'];
  6148. $this->rMargin += $margin['R'];
  6149. }
  6150. }
  6151. $w = $this->getRemainingWidth();
  6152. $wmax = ($w - $this->cell_padding['L'] - $this->cell_padding['R']);
  6153. } else {
  6154. // 160 is the non-breaking space.
  6155. // 173 is SHY (Soft Hypen).
  6156. // \p{Z} or \p{Separator}: any kind of Unicode whitespace or invisible separator.
  6157. // \p{Lo} or \p{Other_Letter}: a Unicode letter or ideograph that does not have lowercase and uppercase variants.
  6158. // \p{Lo} is needed because Chinese characters are packed next to each other without spaces in between.
  6159. if (($c != 160)
  6160. AND (($c == 173)
  6161. OR preg_match($this->re_spaces, TCPDF_FONTS::unichr($c, $this->isunicode))
  6162. OR (($c == 45)
  6163. AND ($i < ($nb - 1))
  6164. AND @preg_match('/[\p{L}]/'.$this->re_space['m'], TCPDF_FONTS::unichr($pc, $this->isunicode))
  6165. AND @preg_match('/[\p{L}]/'.$this->re_space['m'], TCPDF_FONTS::unichr($chars[($i + 1)], $this->isunicode))
  6166. )
  6167. )
  6168. ) {
  6169. // update last blank space position
  6170. $prevsep = $sep;
  6171. $sep = $i;
  6172. // check if is a SHY
  6173. if (($c == 173) OR ($c == 45)) {
  6174. $prevshy = $shy;
  6175. $shy = true;
  6176. if ($pc == 45) {
  6177. $tmp_shy_replacement_width = 0;
  6178. $tmp_shy_replacement_char = '';
  6179. } else {
  6180. $tmp_shy_replacement_width = $shy_replacement_width;
  6181. $tmp_shy_replacement_char = $shy_replacement_char;
  6182. }
  6183. } else {
  6184. $shy = false;
  6185. }
  6186. }
  6187. // update string length
  6188. if ($this->isUnicodeFont() AND ($arabic)) {
  6189. // with bidirectional algorithm some chars may be changed affecting the line length
  6190. // *** very slow ***
  6191. $l = $this->GetArrStringWidth(TCPDF_FONTS::utf8Bidi(array_slice($chars, $j, ($i - $j)), '', $this->tmprtl, $this->isunicode, $this->CurrentFont));
  6192. } else {
  6193. $l += $this->GetCharWidth($c, ($i+1 < $nb));
  6194. }
  6195. if (($l > $wmax) OR (($c == 173) AND (($l + $tmp_shy_replacement_width) >= $wmax))) {
  6196. if (($c == 173) AND (($l + $tmp_shy_replacement_width) > $wmax)) {
  6197. $sep = $prevsep;
  6198. $shy = $prevshy;
  6199. }
  6200. // we have reached the end of column
  6201. if ($sep == -1) {
  6202. // check if the line was already started
  6203. if (($this->rtl AND ($this->x <= ($this->w - $this->rMargin - $this->cell_padding['R'] - $margin['R'] - $chrwidth)))
  6204. OR ((!$this->rtl) AND ($this->x >= ($this->lMargin + $this->cell_padding['L'] + $margin['L'] + $chrwidth)))) {
  6205. // print a void cell and go to next line
  6206. $this->Cell($w, $h, '', 0, 1);
  6207. $linebreak = true;
  6208. if ($firstline) {
  6209. return (TCPDF_FONTS::UniArrSubString($uchars, $j));
  6210. }
  6211. } else {
  6212. // truncate the word because do not fit on column
  6213. $tmpstr = TCPDF_FONTS::UniArrSubString($uchars, $j, $i);
  6214. if ($firstline) {
  6215. $startx = $this->x;
  6216. $tmparr = array_slice($chars, $j, ($i - $j));
  6217. if ($rtlmode) {
  6218. $tmparr = TCPDF_FONTS::utf8Bidi($tmparr, $tmpstr, $this->tmprtl, $this->isunicode, $this->CurrentFont);
  6219. }
  6220. $linew = $this->GetArrStringWidth($tmparr);
  6221. unset($tmparr);
  6222. if ($this->rtl) {
  6223. $this->endlinex = $startx - $linew;
  6224. } else {
  6225. $this->endlinex = $startx + $linew;
  6226. }
  6227. $w = $linew;
  6228. $tmpcellpadding = $this->cell_padding;
  6229. if ($maxh == 0) {
  6230. $this->setCellPadding(0);
  6231. }
  6232. }
  6233. if ($firstblock AND $this->isRTLTextDir()) {
  6234. $tmpstr = $this->stringRightTrim($tmpstr);
  6235. }
  6236. $this->Cell($w, $h, $tmpstr, 0, 1, $align, $fill, $link, $stretch);
  6237. unset($tmpstr);
  6238. if ($firstline) {
  6239. $this->cell_padding = $tmpcellpadding;
  6240. return (TCPDF_FONTS::UniArrSubString($uchars, $i));
  6241. }
  6242. $j = $i;
  6243. --$i;
  6244. }
  6245. } else {
  6246. // word wrapping
  6247. if ($this->rtl AND (!$firstblock) AND ($sep < $i)) {
  6248. $endspace = 1;
  6249. } else {
  6250. $endspace = 0;
  6251. }
  6252. // check the length of the next string
  6253. $strrest = TCPDF_FONTS::UniArrSubString($uchars, ($sep + $endspace));
  6254. $nextstr = TCPDF_STATIC::pregSplit('/'.$this->re_space['p'].'/', $this->re_space['m'], $this->stringTrim($strrest));
  6255. if (isset($nextstr[0]) AND ($this->GetStringWidth($nextstr[0]) > $pw)) {
  6256. // truncate the word because do not fit on a full page width
  6257. $tmpstr = TCPDF_FONTS::UniArrSubString($uchars, $j, $i);
  6258. if ($firstline) {
  6259. $startx = $this->x;
  6260. $tmparr = array_slice($chars, $j, ($i - $j));
  6261. if ($rtlmode) {
  6262. $tmparr = TCPDF_FONTS::utf8Bidi($tmparr, $tmpstr, $this->tmprtl, $this->isunicode, $this->CurrentFont);
  6263. }
  6264. $linew = $this->GetArrStringWidth($tmparr);
  6265. unset($tmparr);
  6266. if ($this->rtl) {
  6267. $this->endlinex = ($startx - $linew);
  6268. } else {
  6269. $this->endlinex = ($startx + $linew);
  6270. }
  6271. $w = $linew;
  6272. $tmpcellpadding = $this->cell_padding;
  6273. if ($maxh == 0) {
  6274. $this->setCellPadding(0);
  6275. }
  6276. }
  6277. if ($firstblock AND $this->isRTLTextDir()) {
  6278. $tmpstr = $this->stringRightTrim($tmpstr);
  6279. }
  6280. $this->Cell($w, $h, $tmpstr, 0, 1, $align, $fill, $link, $stretch);
  6281. unset($tmpstr);
  6282. if ($firstline) {
  6283. $this->cell_padding = $tmpcellpadding;
  6284. return (TCPDF_FONTS::UniArrSubString($uchars, $i));
  6285. }
  6286. $j = $i;
  6287. --$i;
  6288. } else {
  6289. // word wrapping
  6290. if ($shy) {
  6291. // add hypen (minus symbol) at the end of the line
  6292. $shy_width = $tmp_shy_replacement_width;
  6293. if ($this->rtl) {
  6294. $shy_char_left = $tmp_shy_replacement_char;
  6295. $shy_char_right = '';
  6296. } else {
  6297. $shy_char_left = '';
  6298. $shy_char_right = $tmp_shy_replacement_char;
  6299. }
  6300. } else {
  6301. $shy_width = 0;
  6302. $shy_char_left = '';
  6303. $shy_char_right = '';
  6304. }
  6305. $tmpstr = TCPDF_FONTS::UniArrSubString($uchars, $j, ($sep + $endspace));
  6306. if ($firstline) {
  6307. $startx = $this->x;
  6308. $tmparr = array_slice($chars, $j, (($sep + $endspace) - $j));
  6309. if ($rtlmode) {
  6310. $tmparr = TCPDF_FONTS::utf8Bidi($tmparr, $tmpstr, $this->tmprtl, $this->isunicode, $this->CurrentFont);
  6311. }
  6312. $linew = $this->GetArrStringWidth($tmparr);
  6313. unset($tmparr);
  6314. if ($this->rtl) {
  6315. $this->endlinex = $startx - $linew - $shy_width;
  6316. } else {
  6317. $this->endlinex = $startx + $linew + $shy_width;
  6318. }
  6319. $w = $linew;
  6320. $tmpcellpadding = $this->cell_padding;
  6321. if ($maxh == 0) {
  6322. $this->setCellPadding(0);
  6323. }
  6324. }
  6325. // print the line
  6326. if ($firstblock AND $this->isRTLTextDir()) {
  6327. $tmpstr = $this->stringRightTrim($tmpstr);
  6328. }
  6329. $this->Cell($w, $h, $shy_char_left.$tmpstr.$shy_char_right, 0, 1, $align, $fill, $link, $stretch);
  6330. unset($tmpstr);
  6331. if ($firstline) {
  6332. if ($chars[$sep] == 45) {
  6333. $endspace += 1;
  6334. }
  6335. // return the remaining text
  6336. $this->cell_padding = $tmpcellpadding;
  6337. return (TCPDF_FONTS::UniArrSubString($uchars, ($sep + $endspace)));
  6338. }
  6339. $i = $sep;
  6340. $sep = -1;
  6341. $shy = false;
  6342. $j = ($i + 1);
  6343. }
  6344. }
  6345. // account for margin changes
  6346. if ((($this->y + $this->lasth) > $this->PageBreakTrigger) AND ($this->inPageBody())) {
  6347. if ($this->AcceptPageBreak())
  6348. {
  6349. if ($this->rtl) {
  6350. $this->x -= $margin['R'];
  6351. } else {
  6352. $this->x += $margin['L'];
  6353. }
  6354. $this->lMargin += $margin['L'];
  6355. $this->rMargin += $margin['R'];
  6356. }
  6357. }
  6358. $w = $this->getRemainingWidth();
  6359. $wmax = $w - $this->cell_padding['L'] - $this->cell_padding['R'];
  6360. if ($linebreak) {
  6361. $linebreak = false;
  6362. } else {
  6363. ++$nl;
  6364. $l = 0;
  6365. }
  6366. }
  6367. }
  6368. // save last character
  6369. $pc = $c;
  6370. ++$i;
  6371. } // end while i < nb
  6372. // print last substring (if any)
  6373. if ($l > 0) {
  6374. switch ($align) {
  6375. case 'J':
  6376. case 'C': {
  6377. break;
  6378. }
  6379. case 'L': {
  6380. if (!$this->rtl) {
  6381. $w = $l;
  6382. }
  6383. break;
  6384. }
  6385. case 'R': {
  6386. if ($this->rtl) {
  6387. $w = $l;
  6388. }
  6389. break;
  6390. }
  6391. default: {
  6392. $w = $l;
  6393. break;
  6394. }
  6395. }
  6396. $tmpstr = TCPDF_FONTS::UniArrSubString($uchars, $j, $nb);
  6397. if ($firstline) {
  6398. $startx = $this->x;
  6399. $tmparr = array_slice($chars, $j, ($nb - $j));
  6400. if ($rtlmode) {
  6401. $tmparr = TCPDF_FONTS::utf8Bidi($tmparr, $tmpstr, $this->tmprtl, $this->isunicode, $this->CurrentFont);
  6402. }
  6403. $linew = $this->GetArrStringWidth($tmparr);
  6404. unset($tmparr);
  6405. if ($this->rtl) {
  6406. $this->endlinex = $startx - $linew;
  6407. } else {
  6408. $this->endlinex = $startx + $linew;
  6409. }
  6410. $w = $linew;
  6411. $tmpcellpadding = $this->cell_padding;
  6412. if ($maxh == 0) {
  6413. $this->setCellPadding(0);
  6414. }
  6415. }
  6416. if ($firstblock AND $this->isRTLTextDir()) {
  6417. $tmpstr = $this->stringRightTrim($tmpstr);
  6418. }
  6419. $this->Cell($w, $h, $tmpstr, 0, $ln, $align, $fill, $link, $stretch);
  6420. unset($tmpstr);
  6421. if ($firstline) {
  6422. $this->cell_padding = $tmpcellpadding;
  6423. return (TCPDF_FONTS::UniArrSubString($uchars, $nb));
  6424. }
  6425. ++$nl;
  6426. }
  6427. if ($firstline) {
  6428. return '';
  6429. }
  6430. return $nl;
  6431. }
  6432. /**
  6433. * Returns the remaining width between the current position and margins.
  6434. * @return float Return the remaining width
  6435. * @protected
  6436. */
  6437. protected function getRemainingWidth() {
  6438. list($this->x, $this->y) = $this->checkPageRegions(0, $this->x, $this->y);
  6439. if ($this->rtl) {
  6440. return ($this->x - $this->lMargin);
  6441. } else {
  6442. return ($this->w - $this->rMargin - $this->x);
  6443. }
  6444. }
  6445. /**
  6446. * Set the block dimensions accounting for page breaks and page/column fitting
  6447. * @param float $w width
  6448. * @param float $h height
  6449. * @param float $x X coordinate
  6450. * @param float $y Y coodiante
  6451. * @param boolean $fitonpage if true the block is resized to not exceed page dimensions.
  6452. * @return array array($w, $h, $x, $y)
  6453. * @protected
  6454. * @since 5.5.009 (2010-07-05)
  6455. */
  6456. protected function fitBlock($w, $h, $x, $y, $fitonpage=false) {
  6457. if ($w <= 0) {
  6458. // set maximum width
  6459. $w = ($this->w - $this->lMargin - $this->rMargin);
  6460. if ($w <= 0) {
  6461. $w = 1;
  6462. }
  6463. }
  6464. if ($h <= 0) {
  6465. // set maximum height
  6466. $h = ($this->PageBreakTrigger - $this->tMargin);
  6467. if ($h <= 0) {
  6468. $h = 1;
  6469. }
  6470. }
  6471. // resize the block to be vertically contained on a single page or single column
  6472. if ($fitonpage OR $this->AutoPageBreak) {
  6473. $ratio_wh = ($w / $h);
  6474. if ($h > ($this->PageBreakTrigger - $this->tMargin)) {
  6475. $h = $this->PageBreakTrigger - $this->tMargin;
  6476. $w = ($h * $ratio_wh);
  6477. }
  6478. // resize the block to be horizontally contained on a single page or single column
  6479. if ($fitonpage) {
  6480. $maxw = ($this->w - $this->lMargin - $this->rMargin);
  6481. if ($w > $maxw) {
  6482. $w = $maxw;
  6483. $h = ($w / $ratio_wh);
  6484. }
  6485. }
  6486. }
  6487. // Check whether we need a new page or new column first as this does not fit
  6488. $prev_x = $this->x;
  6489. $prev_y = $this->y;
  6490. if ($this->checkPageBreak($h, $y) OR ($this->y < $prev_y)) {
  6491. $y = $this->y;
  6492. if ($this->rtl) {
  6493. $x += ($prev_x - $this->x);
  6494. } else {
  6495. $x += ($this->x - $prev_x);
  6496. }
  6497. $this->newline = true;
  6498. }
  6499. // resize the block to be contained on the remaining available page or column space
  6500. if ($fitonpage) {
  6501. // fallback to avoid division by zero
  6502. $h = $h == 0 ? 1 : $h;
  6503. $ratio_wh = ($w / $h);
  6504. if (($y + $h) > $this->PageBreakTrigger) {
  6505. $h = $this->PageBreakTrigger - $y;
  6506. $w = ($h * $ratio_wh);
  6507. }
  6508. if ((!$this->rtl) AND (($x + $w) > ($this->w - $this->rMargin))) {
  6509. $w = $this->w - $this->rMargin - $x;
  6510. $h = ($w / $ratio_wh);
  6511. } elseif (($this->rtl) AND (($x - $w) < ($this->lMargin))) {
  6512. $w = $x - $this->lMargin;
  6513. $h = ($w / $ratio_wh);
  6514. }
  6515. }
  6516. return array($w, $h, $x, $y);
  6517. }
  6518. /**
  6519. * Puts an image in the page.
  6520. * The upper-left corner must be given.
  6521. * The dimensions can be specified in different ways:<ul>
  6522. * <li>explicit width and height (expressed in user unit)</li>
  6523. * <li>one explicit dimension, the other being calculated automatically in order to keep the original proportions</li>
  6524. * <li>no explicit dimension, in which case the image is put at 72 dpi</li></ul>
  6525. * Supported formats are JPEG and PNG images whitout GD library and all images supported by GD: GD, GD2, GD2PART, GIF, JPEG, PNG, BMP, XBM, XPM;
  6526. * The format can be specified explicitly or inferred from the file extension.<br />
  6527. * It is possible to put a link on the image.<br />
  6528. * Remark: if an image is used several times, only one copy will be embedded in the file.<br />
  6529. * @param string $file Name of the file containing the image or a '@' character followed by the image data string. To link an image without embedding it on the document, set an asterisk character before the URL (i.e.: '*http://www.example.com/image.jpg').
  6530. * @param float|null $x Abscissa of the upper-left corner (LTR) or upper-right corner (RTL).
  6531. * @param float|null $y Ordinate of the upper-left corner (LTR) or upper-right corner (RTL).
  6532. * @param float $w Width of the image in the page. If not specified or equal to zero, it is automatically calculated.
  6533. * @param float $h Height of the image in the page. If not specified or equal to zero, it is automatically calculated.
  6534. * @param string $type Image format. Possible values are (case insensitive): JPEG and PNG (whitout GD library) and all images supported by GD: GD, GD2, GD2PART, GIF, JPEG, PNG, BMP, XBM, XPM;. If not specified, the type is inferred from the file extension.
  6535. * @param mixed $link URL or identifier returned by AddLink().
  6536. * @param string $align Indicates the alignment of the pointer next to image insertion relative to image height. The value can be:<ul><li>T: top-right for LTR or top-left for RTL</li><li>M: middle-right for LTR or middle-left for RTL</li><li>B: bottom-right for LTR or bottom-left for RTL</li><li>N: next line</li></ul>
  6537. * @param mixed $resize If true resize (reduce) the image to fit $w and $h (requires GD or ImageMagick library); if false do not resize; if 2 force resize in all cases (upscaling and downscaling).
  6538. * @param int $dpi dot-per-inch resolution used on resize
  6539. * @param string $palign Allows to center or align the image on the current line. Possible values are:<ul><li>L : left align</li><li>C : center</li><li>R : right align</li><li>'' : empty string : left for LTR or right for RTL</li></ul>
  6540. * @param boolean $ismask true if this image is a mask, false otherwise
  6541. * @param mixed $imgmask image object returned by this function or false
  6542. * @param mixed $border Indicates if borders must be drawn around the cell. The value can be a number:<ul><li>0: no border (default)</li><li>1: frame</li></ul> or a string containing some or all of the following characters (in any order):<ul><li>L: left</li><li>T: top</li><li>R: right</li><li>B: bottom</li></ul> or an array of line styles for each border group - for example: array('LTRB' => array('width' => 2, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)))
  6543. * @param mixed $fitbox If not false scale image dimensions proportionally to fit within the ($w, $h) box. $fitbox can be true or a 2 characters string indicating the image alignment inside the box. The first character indicate the horizontal alignment (L = left, C = center, R = right) the second character indicate the vertical algnment (T = top, M = middle, B = bottom).
  6544. * @param boolean $hidden If true do not display the image.
  6545. * @param boolean $fitonpage If true the image is resized to not exceed page dimensions.
  6546. * @param boolean $alt If true the image will be added as alternative and not directly printed (the ID of the image will be returned).
  6547. * @param array $altimgs Array of alternate images IDs. Each alternative image must be an array with two values: an integer representing the image ID (the value returned by the Image method) and a boolean value to indicate if the image is the default for printing.
  6548. * @return mixed|false image information
  6549. * @public
  6550. * @since 1.1
  6551. */
  6552. public function Image($file, $x=null, $y=null, $w=0, $h=0, $type='', $link='', $align='', $resize=false, $dpi=300, $palign='', $ismask=false, $imgmask=false, $border=0, $fitbox=false, $hidden=false, $fitonpage=false, $alt=false, $altimgs=array()) {
  6553. if ($this->state != 2) {
  6554. return false;
  6555. }
  6556. if (TCPDF_STATIC::empty_string($x)) {
  6557. $x = $this->x;
  6558. }
  6559. if (TCPDF_STATIC::empty_string($y)) {
  6560. $y = $this->y;
  6561. }
  6562. // check page for no-write regions and adapt page margins if necessary
  6563. list($x, $y) = $this->checkPageRegions($h, $x, $y);
  6564. $exurl = ''; // external streams
  6565. $imsize = FALSE;
  6566. // Make sure the file variable is not empty or null because accessing $file[0] later
  6567. // results in error when running PHP 7.4
  6568. if (empty($file)) {
  6569. return false;
  6570. }
  6571. // check if we are passing an image as file or string
  6572. if ($file[0] === '@') {
  6573. // image from string
  6574. $imgdata = substr($file, 1);
  6575. } else { // image file
  6576. if ($file[0] === '*') {
  6577. // image as external stream
  6578. $file = substr($file, 1);
  6579. $exurl = $file;
  6580. }
  6581. // check if file exist and it is valid
  6582. if (!@$this->fileExists($file)) {
  6583. return false;
  6584. }
  6585. if (false !== $info = $this->getImageBuffer($file)) {
  6586. $imsize = array($info['w'], $info['h']);
  6587. } elseif (($imsize = @getimagesize($file)) === FALSE && strpos($file, '__tcpdf_'.$this->file_id.'_img') === FALSE){
  6588. $imgdata = $this->getCachedFileContents($file);
  6589. }
  6590. }
  6591. if (!empty($imgdata)) {
  6592. // copy image to cache
  6593. $original_file = $file;
  6594. $file = TCPDF_STATIC::getObjFilename('img', $this->file_id);
  6595. $fp = TCPDF_STATIC::fopenLocal($file, 'w');
  6596. if (!$fp) {
  6597. $this->Error('Unable to write file: '.$file);
  6598. }
  6599. fwrite($fp, $imgdata);
  6600. fclose($fp);
  6601. unset($imgdata);
  6602. $imsize = @getimagesize($file);
  6603. if ($imsize === FALSE) {
  6604. unlink($file);
  6605. $file = $original_file;
  6606. }
  6607. }
  6608. if ($imsize === FALSE) {
  6609. if (($w > 0) AND ($h > 0)) {
  6610. // get measures from specified data
  6611. $pw = $this->getHTMLUnitToUnits($w, 0, $this->pdfunit, true) * $this->imgscale * $this->k;
  6612. $ph = $this->getHTMLUnitToUnits($h, 0, $this->pdfunit, true) * $this->imgscale * $this->k;
  6613. $imsize = array($pw, $ph);
  6614. } else {
  6615. $this->Error('[Image] Unable to get the size of the image: '.$file);
  6616. }
  6617. }
  6618. // file hash
  6619. $filehash = md5($file);
  6620. // get original image width and height in pixels
  6621. list($pixw, $pixh) = $imsize;
  6622. // calculate image width and height on document
  6623. if (($w <= 0) AND ($h <= 0)) {
  6624. // convert image size to document unit
  6625. $w = $this->pixelsToUnits($pixw);
  6626. $h = $this->pixelsToUnits($pixh);
  6627. } elseif ($w <= 0) {
  6628. $w = $h * $pixw / $pixh;
  6629. } elseif ($h <= 0) {
  6630. $h = $w * $pixh / $pixw;
  6631. } elseif (($fitbox !== false) AND ($w > 0) AND ($h > 0)) {
  6632. if (strlen($fitbox) !== 2) {
  6633. // set default alignment
  6634. $fitbox = '--';
  6635. }
  6636. // scale image dimensions proportionally to fit within the ($w, $h) box
  6637. if ((($w * $pixh) / ($h * $pixw)) < 1) {
  6638. // store current height
  6639. $oldh = $h;
  6640. // calculate new height
  6641. $h = $w * $pixh / $pixw;
  6642. // height difference
  6643. $hdiff = ($oldh - $h);
  6644. // vertical alignment
  6645. switch (strtoupper($fitbox[1])) {
  6646. case 'T': {
  6647. break;
  6648. }
  6649. case 'M': {
  6650. $y += ($hdiff / 2);
  6651. break;
  6652. }
  6653. case 'B': {
  6654. $y += $hdiff;
  6655. break;
  6656. }
  6657. }
  6658. } else {
  6659. // store current width
  6660. $oldw = $w;
  6661. // calculate new width
  6662. $w = $h * $pixw / $pixh;
  6663. // width difference
  6664. $wdiff = ($oldw - $w);
  6665. // horizontal alignment
  6666. switch (strtoupper($fitbox[0])) {
  6667. case 'L': {
  6668. if ($this->rtl) {
  6669. $x -= $wdiff;
  6670. }
  6671. break;
  6672. }
  6673. case 'C': {
  6674. if ($this->rtl) {
  6675. $x -= ($wdiff / 2);
  6676. } else {
  6677. $x += ($wdiff / 2);
  6678. }
  6679. break;
  6680. }
  6681. case 'R': {
  6682. if (!$this->rtl) {
  6683. $x += $wdiff;
  6684. }
  6685. break;
  6686. }
  6687. }
  6688. }
  6689. }
  6690. // fit the image on available space
  6691. list($w, $h, $x, $y) = $this->fitBlock($w, $h, $x, $y, $fitonpage);
  6692. // calculate new minimum dimensions in pixels
  6693. $neww = round($w * $this->k * $dpi / $this->dpi);
  6694. $newh = round($h * $this->k * $dpi / $this->dpi);
  6695. // check if resize is necessary (resize is used only to reduce the image)
  6696. $newsize = ($neww * $newh);
  6697. $pixsize = ($pixw * $pixh);
  6698. if (intval($resize) == 2) {
  6699. $resize = true;
  6700. } elseif ($newsize >= $pixsize) {
  6701. $resize = false;
  6702. }
  6703. // check if image has been already added on document
  6704. $newimage = true;
  6705. if (in_array($file, $this->imagekeys)) {
  6706. $newimage = false;
  6707. // get existing image data
  6708. $info = $this->getImageBuffer($file);
  6709. if (strpos($file, '__tcpdf_'.$this->file_id.'_imgmask_') === FALSE) {
  6710. // check if the newer image is larger
  6711. $oldsize = ($info['w'] * $info['h']);
  6712. if ((($oldsize < $newsize) AND ($resize)) OR (($oldsize < $pixsize) AND (!$resize))) {
  6713. $newimage = true;
  6714. }
  6715. }
  6716. } elseif (($ismask === false) AND ($imgmask === false) AND (strpos($file, '__tcpdf_'.$this->file_id.'_imgmask_') === FALSE)) {
  6717. // create temp image file (without alpha channel)
  6718. $tempfile_plain = K_PATH_CACHE.'__tcpdf_'.$this->file_id.'_imgmask_plain_'.$filehash;
  6719. // create temp alpha file
  6720. $tempfile_alpha = K_PATH_CACHE.'__tcpdf_'.$this->file_id.'_imgmask_alpha_'.$filehash;
  6721. // check for cached images
  6722. if (in_array($tempfile_plain, $this->imagekeys)) {
  6723. // get existing image data
  6724. $info = $this->getImageBuffer($tempfile_plain);
  6725. // check if the newer image is larger
  6726. $oldsize = ($info['w'] * $info['h']);
  6727. if ((($oldsize < $newsize) AND ($resize)) OR (($oldsize < $pixsize) AND (!$resize))) {
  6728. $newimage = true;
  6729. } else {
  6730. $newimage = false;
  6731. // embed mask image
  6732. $imgmask = $this->Image($tempfile_alpha, $x, $y, $w, $h, 'PNG', '', '', $resize, $dpi, '', true, false);
  6733. // embed image, masked with previously embedded mask
  6734. return $this->Image($tempfile_plain, $x, $y, $w, $h, $type, $link, $align, $resize, $dpi, $palign, false, $imgmask);
  6735. }
  6736. }
  6737. }
  6738. if ($newimage) {
  6739. //First use of image, get info
  6740. $type = strtolower($type);
  6741. if ($type == '') {
  6742. $type = TCPDF_IMAGES::getImageFileType($file, $imsize);
  6743. } elseif ($type == 'jpg') {
  6744. $type = 'jpeg';
  6745. }
  6746. // Specific image handlers (defined on TCPDF_IMAGES CLASS)
  6747. $mtd = '_parse'.$type;
  6748. // GD image handler function
  6749. $gdfunction = 'imagecreatefrom'.$type;
  6750. $info = false;
  6751. if ((method_exists('TCPDF_IMAGES', $mtd)) AND (!($resize AND (function_exists($gdfunction) OR extension_loaded('imagick'))))) {
  6752. // TCPDF image functions
  6753. $info = TCPDF_IMAGES::$mtd($file);
  6754. if (($ismask === false) AND ($imgmask === false) AND (strpos($file, '__tcpdf_'.$this->file_id.'_imgmask_') === FALSE)
  6755. AND (($info === 'pngalpha') OR (isset($info['trns']) AND !empty($info['trns'])))) {
  6756. return $this->ImagePngAlpha($file, $x, $y, $pixw, $pixh, $w, $h, 'PNG', $link, $align, $resize, $dpi, $palign, $filehash);
  6757. }
  6758. }
  6759. if (($info === false) AND function_exists($gdfunction)) {
  6760. try {
  6761. // GD library
  6762. $img = $gdfunction($file);
  6763. if ($img !== false) {
  6764. if ($resize) {
  6765. $imgr = imagecreatetruecolor($neww, $newh);
  6766. if (($type == 'gif') OR ($type == 'png')) {
  6767. $imgr = TCPDF_IMAGES::setGDImageTransparency($imgr, $img);
  6768. }
  6769. imagecopyresampled($imgr, $img, 0, 0, 0, 0, $neww, $newh, $pixw, $pixh);
  6770. $img = $imgr;
  6771. }
  6772. if (($type == 'gif') OR ($type == 'png')) {
  6773. $info = TCPDF_IMAGES::_toPNG($img, TCPDF_STATIC::getObjFilename('img', $this->file_id));
  6774. } else {
  6775. $info = TCPDF_IMAGES::_toJPEG($img, $this->jpeg_quality, TCPDF_STATIC::getObjFilename('img', $this->file_id));
  6776. }
  6777. }
  6778. } catch(Exception $e) {
  6779. $info = false;
  6780. }
  6781. }
  6782. if (($info === false) AND extension_loaded('imagick')) {
  6783. try {
  6784. // ImageMagick library
  6785. $img = new Imagick();
  6786. if ($type == 'svg') {
  6787. if ($file[0] === '@') {
  6788. // image from string
  6789. $svgimg = substr($file, 1);
  6790. } else {
  6791. // get SVG file content
  6792. $svgimg = $this->getCachedFileContents($file);
  6793. }
  6794. if ($svgimg !== FALSE) {
  6795. // get width and height
  6796. $regs = array();
  6797. if (preg_match('/<svg([^\>]*)>/si', $svgimg, $regs)) {
  6798. $svgtag = $regs[1];
  6799. $tmp = array();
  6800. if (preg_match('/[\s]+width[\s]*=[\s]*"([^"]*)"/si', $svgtag, $tmp)) {
  6801. $ow = $this->getHTMLUnitToUnits($tmp[1], 1, $this->svgunit, false);
  6802. $owu = sprintf('%F', ($ow * $dpi / 72)).$this->pdfunit;
  6803. $svgtag = preg_replace('/[\s]+width[\s]*=[\s]*"[^"]*"/si', ' width="'.$owu.'"', $svgtag, 1);
  6804. } else {
  6805. $ow = $w;
  6806. }
  6807. $tmp = array();
  6808. if (preg_match('/[\s]+height[\s]*=[\s]*"([^"]*)"/si', $svgtag, $tmp)) {
  6809. $oh = $this->getHTMLUnitToUnits($tmp[1], 1, $this->svgunit, false);
  6810. $ohu = sprintf('%F', ($oh * $dpi / 72)).$this->pdfunit;
  6811. $svgtag = preg_replace('/[\s]+height[\s]*=[\s]*"[^"]*"/si', ' height="'.$ohu.'"', $svgtag, 1);
  6812. } else {
  6813. $oh = $h;
  6814. }
  6815. $tmp = array();
  6816. if (!preg_match('/[\s]+viewBox[\s]*=[\s]*"[\s]*([0-9\.]+)[\s]+([0-9\.]+)[\s]+([0-9\.]+)[\s]+([0-9\.]+)[\s]*"/si', $svgtag, $tmp)) {
  6817. $vbw = ($ow * $this->imgscale * $this->k);
  6818. $vbh = ($oh * $this->imgscale * $this->k);
  6819. $vbox = sprintf(' viewBox="0 0 %F %F" ', $vbw, $vbh);
  6820. $svgtag = $vbox.$svgtag;
  6821. }
  6822. $svgimg = preg_replace('/<svg([^\>]*)>/si', '<svg'.$svgtag.'>', $svgimg, 1);
  6823. }
  6824. $img->readImageBlob($svgimg);
  6825. }
  6826. } else {
  6827. $img->readImage($file);
  6828. }
  6829. if ($resize) {
  6830. $img->resizeImage($neww, $newh, 10, 1, false);
  6831. }
  6832. $img->setCompressionQuality($this->jpeg_quality);
  6833. $img->setImageFormat('jpeg');
  6834. $tempname = TCPDF_STATIC::getObjFilename('img', $this->file_id);
  6835. $img->writeImage($tempname);
  6836. $info = TCPDF_IMAGES::_parsejpeg($tempname);
  6837. unlink($tempname);
  6838. $img->destroy();
  6839. } catch(Exception $e) {
  6840. $info = false;
  6841. }
  6842. }
  6843. if ($info === false) {
  6844. // unable to process image
  6845. return false;
  6846. }
  6847. if ($ismask) {
  6848. // force grayscale
  6849. $info['cs'] = 'DeviceGray';
  6850. }
  6851. if ($imgmask !== false) {
  6852. $info['masked'] = $imgmask;
  6853. }
  6854. if (!empty($exurl)) {
  6855. $info['exurl'] = $exurl;
  6856. }
  6857. // array of alternative images
  6858. $info['altimgs'] = $altimgs;
  6859. // add image to document
  6860. $info['i'] = $this->setImageBuffer($file, $info);
  6861. }
  6862. // set alignment
  6863. $this->img_rb_x = $x + $w;
  6864. $this->img_rb_y = $y + $h;
  6865. // set alignment
  6866. if ($palign == 'L') {
  6867. $ximg = $this->lMargin;
  6868. } elseif ($palign == 'C') {
  6869. $ximg = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
  6870. } elseif ($palign == 'R') {
  6871. $ximg = $this->w - $this->rMargin - $w;
  6872. } else {
  6873. $ximg = $this->rtl ? $x - $w : $x;
  6874. }
  6875. if ($ismask OR $hidden) {
  6876. // image is not displayed
  6877. return $info['i'];
  6878. }
  6879. $xkimg = $ximg * $this->k;
  6880. if (!$alt) {
  6881. // only non-alternative immages will be set
  6882. $this->_out(sprintf('q %F 0 0 %F %F %F cm /I%u Do Q', ($w * $this->k), ($h * $this->k), $xkimg, (($this->h - ($y + $h)) * $this->k), $info['i']));
  6883. }
  6884. if (!empty($border)) {
  6885. $bx = $this->x;
  6886. $by = $this->y;
  6887. $this->x = $ximg;
  6888. if ($this->rtl) {
  6889. $this->x += $w;
  6890. }
  6891. $this->y = $y;
  6892. $this->Cell($w, $h, '', $border, 0, '', 0, '', 0, true);
  6893. $this->x = $bx;
  6894. $this->y = $by;
  6895. }
  6896. if ($link) {
  6897. $this->Link($ximg, $y, $w, $h, $link, 0);
  6898. }
  6899. // set pointer to align the next text/objects
  6900. switch($align) {
  6901. case 'T': {
  6902. $this->y = $y;
  6903. $this->x = $this->img_rb_x;
  6904. break;
  6905. }
  6906. case 'M': {
  6907. $this->y = $y + round($h/2);
  6908. $this->x = $this->img_rb_x;
  6909. break;
  6910. }
  6911. case 'B': {
  6912. $this->y = $this->img_rb_y;
  6913. $this->x = $this->img_rb_x;
  6914. break;
  6915. }
  6916. case 'N': {
  6917. $this->setY($this->img_rb_y);
  6918. break;
  6919. }
  6920. default:{
  6921. break;
  6922. }
  6923. }
  6924. $this->endlinex = $this->img_rb_x;
  6925. if ($this->inxobj) {
  6926. // we are inside an XObject template
  6927. $this->xobjects[$this->xobjid]['images'][] = $info['i'];
  6928. }
  6929. return $info['i'];
  6930. }
  6931. /**
  6932. * Extract info from a PNG image with alpha channel using the Imagick or GD library.
  6933. * @param string $file Name of the file containing the image.
  6934. * @param float $x Abscissa of the upper-left corner.
  6935. * @param float $y Ordinate of the upper-left corner.
  6936. * @param float $wpx Original width of the image in pixels.
  6937. * @param float $hpx original height of the image in pixels.
  6938. * @param float $w Width of the image in the page. If not specified or equal to zero, it is automatically calculated.
  6939. * @param float $h Height of the image in the page. If not specified or equal to zero, it is automatically calculated.
  6940. * @param string $type Image format. Possible values are (case insensitive): JPEG and PNG (whitout GD library) and all images supported by GD: GD, GD2, GD2PART, GIF, JPEG, PNG, BMP, XBM, XPM;. If not specified, the type is inferred from the file extension.
  6941. * @param mixed $link URL or identifier returned by AddLink().
  6942. * @param string $align Indicates the alignment of the pointer next to image insertion relative to image height. The value can be:<ul><li>T: top-right for LTR or top-left for RTL</li><li>M: middle-right for LTR or middle-left for RTL</li><li>B: bottom-right for LTR or bottom-left for RTL</li><li>N: next line</li></ul>
  6943. * @param boolean $resize If true resize (reduce) the image to fit $w and $h (requires GD library).
  6944. * @param int $dpi dot-per-inch resolution used on resize
  6945. * @param string $palign Allows to center or align the image on the current line. Possible values are:<ul><li>L : left align</li><li>C : center</li><li>R : right align</li><li>'' : empty string : left for LTR or right for RTL</li></ul>
  6946. * @param string $filehash File hash used to build unique file names.
  6947. * @author Nicola Asuni
  6948. * @protected
  6949. * @since 4.3.007 (2008-12-04)
  6950. * @see Image()
  6951. */
  6952. protected function ImagePngAlpha($file, $x, $y, $wpx, $hpx, $w, $h, $type, $link, $align, $resize, $dpi, $palign, $filehash='') {
  6953. // create temp images
  6954. if (empty($filehash)) {
  6955. $filehash = md5($file);
  6956. }
  6957. // create temp image file (without alpha channel)
  6958. $tempfile_plain = K_PATH_CACHE.'__tcpdf_'.$this->file_id.'_imgmask_plain_'.$filehash;
  6959. // create temp alpha file
  6960. $tempfile_alpha = K_PATH_CACHE.'__tcpdf_'.$this->file_id.'_imgmask_alpha_'.$filehash;
  6961. $parsed = false;
  6962. $parse_error = '';
  6963. // ImageMagick extension
  6964. if (($parsed === false) AND extension_loaded('imagick')) {
  6965. try {
  6966. // ImageMagick library
  6967. $img = new Imagick();
  6968. $img->readImage($file);
  6969. // clone image object
  6970. $imga = TCPDF_STATIC::objclone($img);
  6971. // extract alpha channel
  6972. if (method_exists($img, 'setImageAlphaChannel') AND defined('Imagick::ALPHACHANNEL_EXTRACT')) {
  6973. $img->setImageAlphaChannel(Imagick::ALPHACHANNEL_EXTRACT);
  6974. } else {
  6975. $img->separateImageChannel(8); // 8 = (imagick::CHANNEL_ALPHA | imagick::CHANNEL_OPACITY | imagick::CHANNEL_MATTE);
  6976. $img->negateImage(true);
  6977. }
  6978. $img->setImageFormat('png');
  6979. $img->writeImage($tempfile_alpha);
  6980. // remove alpha channel
  6981. if (method_exists($imga, 'setImageMatte')) {
  6982. $imga->setImageMatte(false);
  6983. } else {
  6984. $imga->separateImageChannel(39); // 39 = (imagick::CHANNEL_ALL & ~(imagick::CHANNEL_ALPHA | imagick::CHANNEL_OPACITY | imagick::CHANNEL_MATTE));
  6985. }
  6986. $imga->setImageFormat('png');
  6987. $imga->writeImage($tempfile_plain);
  6988. $parsed = true;
  6989. } catch (Exception $e) {
  6990. // Imagemagick fails, try with GD
  6991. $parse_error = 'Imagick library error: '.$e->getMessage();
  6992. }
  6993. }
  6994. // GD extension
  6995. if (($parsed === false) AND function_exists('imagecreatefrompng')) {
  6996. try {
  6997. // generate images
  6998. $img = imagecreatefrompng($file);
  6999. $imgalpha = imagecreate($wpx, $hpx);
  7000. // generate gray scale palette (0 -> 255)
  7001. for ($c = 0; $c < 256; ++$c) {
  7002. ImageColorAllocate($imgalpha, $c, $c, $c);
  7003. }
  7004. // extract alpha channel
  7005. for ($xpx = 0; $xpx < $wpx; ++$xpx) {
  7006. for ($ypx = 0; $ypx < $hpx; ++$ypx) {
  7007. $color = imagecolorat($img, $xpx, $ypx);
  7008. // get and correct gamma color
  7009. $alpha = $this->getGDgamma($img, $color);
  7010. imagesetpixel($imgalpha, (int) $xpx, (int) $ypx, (int) $alpha);
  7011. }
  7012. }
  7013. imagepng($imgalpha, $tempfile_alpha);
  7014. imagedestroy($imgalpha);
  7015. // extract image without alpha channel
  7016. $imgplain = imagecreatetruecolor($wpx, $hpx);
  7017. imagecopy($imgplain, $img, 0, 0, 0, 0, $wpx, $hpx);
  7018. imagepng($imgplain, $tempfile_plain);
  7019. imagedestroy($imgplain);
  7020. $parsed = true;
  7021. } catch (Exception $e) {
  7022. // GD fails
  7023. $parse_error = 'GD library error: '.$e->getMessage();
  7024. }
  7025. }
  7026. if ($parsed === false) {
  7027. if (empty($parse_error)) {
  7028. $this->Error('TCPDF requires the Imagick or GD extension to handle PNG images with alpha channel.');
  7029. } else {
  7030. $this->Error($parse_error);
  7031. }
  7032. }
  7033. // embed mask image
  7034. $imgmask = $this->Image($tempfile_alpha, $x, $y, $w, $h, 'PNG', '', '', $resize, $dpi, '', true, false);
  7035. // embed image, masked with previously embedded mask
  7036. $this->Image($tempfile_plain, $x, $y, $w, $h, $type, $link, $align, $resize, $dpi, $palign, false, $imgmask);
  7037. }
  7038. /**
  7039. * Get the GD-corrected PNG gamma value from alpha color
  7040. * @param resource $img GD image Resource ID.
  7041. * @param int $c alpha color
  7042. * @protected
  7043. * @since 4.3.007 (2008-12-04)
  7044. */
  7045. protected function getGDgamma($img, $c) {
  7046. if (!isset($this->gdgammacache['#'.$c])) {
  7047. $colors = imagecolorsforindex($img, $c);
  7048. // GD alpha is only 7 bit (0 -> 127)
  7049. $this->gdgammacache['#'.$c] = (int) (((127 - $colors['alpha']) / 127) * 255);
  7050. // correct gamma
  7051. $this->gdgammacache['#'.$c] = (int) (pow(($this->gdgammacache['#'.$c] / 255), 2.2) * 255);
  7052. // store the latest values on cache to improve performances
  7053. if (count($this->gdgammacache) > 8) {
  7054. // remove one element from the cache array
  7055. array_shift($this->gdgammacache);
  7056. }
  7057. }
  7058. return $this->gdgammacache['#'.$c];
  7059. }
  7060. /**
  7061. * Performs a line break.
  7062. * The current abscissa goes back to the left margin and the ordinate increases by the amount passed in parameter.
  7063. * @param float|null $h The height of the break. By default, the value equals the height of the last printed cell.
  7064. * @param boolean $cell if true add the current left (or right o for RTL) padding to the X coordinate
  7065. * @public
  7066. * @since 1.0
  7067. * @see Cell()
  7068. */
  7069. public function Ln($h=null, $cell=false) {
  7070. if (($this->num_columns > 1) AND ($this->y == $this->columns[$this->current_column]['y']) AND isset($this->columns[$this->current_column]['x']) AND ($this->x == $this->columns[$this->current_column]['x'])) {
  7071. // revove vertical space from the top of the column
  7072. return;
  7073. }
  7074. if ($cell) {
  7075. if ($this->rtl) {
  7076. $cellpadding = $this->cell_padding['R'];
  7077. } else {
  7078. $cellpadding = $this->cell_padding['L'];
  7079. }
  7080. } else {
  7081. $cellpadding = 0;
  7082. }
  7083. if ($this->rtl) {
  7084. $this->x = $this->w - $this->rMargin - $cellpadding;
  7085. } else {
  7086. $this->x = $this->lMargin + $cellpadding;
  7087. }
  7088. if (TCPDF_STATIC::empty_string($h)) {
  7089. $h = $this->lasth;
  7090. }
  7091. $this->y += $h;
  7092. $this->newline = true;
  7093. }
  7094. /**
  7095. * Returns the relative X value of current position.
  7096. * The value is relative to the left border for LTR languages and to the right border for RTL languages.
  7097. * @return float
  7098. * @public
  7099. * @since 1.2
  7100. * @see SetX(), GetY(), SetY()
  7101. */
  7102. public function GetX() {
  7103. //Get x position
  7104. if ($this->rtl) {
  7105. return ($this->w - $this->x);
  7106. } else {
  7107. return $this->x;
  7108. }
  7109. }
  7110. /**
  7111. * Returns the absolute X value of current position.
  7112. * @return float
  7113. * @public
  7114. * @since 1.2
  7115. * @see SetX(), GetY(), SetY()
  7116. */
  7117. public function GetAbsX() {
  7118. return $this->x;
  7119. }
  7120. /**
  7121. * Returns the ordinate of the current position.
  7122. * @return float
  7123. * @public
  7124. * @since 1.0
  7125. * @see SetY(), GetX(), SetX()
  7126. */
  7127. public function GetY() {
  7128. return $this->y;
  7129. }
  7130. /**
  7131. * Defines the abscissa of the current position.
  7132. * If the passed value is negative, it is relative to the right of the page (or left if language is RTL).
  7133. * @param float $x The value of the abscissa in user units.
  7134. * @param boolean $rtloff if true always uses the page top-left corner as origin of axis.
  7135. * @public
  7136. * @since 1.2
  7137. * @see GetX(), GetY(), SetY(), SetXY()
  7138. */
  7139. public function setX($x, $rtloff=false) {
  7140. $x = floatval($x);
  7141. if (!$rtloff AND $this->rtl) {
  7142. if ($x >= 0) {
  7143. $this->x = $this->w - $x;
  7144. } else {
  7145. $this->x = abs($x);
  7146. }
  7147. } else {
  7148. if ($x >= 0) {
  7149. $this->x = $x;
  7150. } else {
  7151. $this->x = $this->w + $x;
  7152. }
  7153. }
  7154. if ($this->x < 0) {
  7155. $this->x = 0;
  7156. }
  7157. if ($this->x > $this->w) {
  7158. $this->x = $this->w;
  7159. }
  7160. }
  7161. /**
  7162. * Moves the current abscissa back to the left margin and sets the ordinate.
  7163. * If the passed value is negative, it is relative to the bottom of the page.
  7164. * @param float $y The value of the ordinate in user units.
  7165. * @param bool $resetx if true (default) reset the X position.
  7166. * @param boolean $rtloff if true always uses the page top-left corner as origin of axis.
  7167. * @public
  7168. * @since 1.0
  7169. * @see GetX(), GetY(), SetY(), SetXY()
  7170. */
  7171. public function setY($y, $resetx=true, $rtloff=false) {
  7172. $y = floatval($y);
  7173. if ($resetx) {
  7174. //reset x
  7175. if (!$rtloff AND $this->rtl) {
  7176. $this->x = $this->w - $this->rMargin;
  7177. } else {
  7178. $this->x = $this->lMargin;
  7179. }
  7180. }
  7181. if ($y >= 0) {
  7182. $this->y = $y;
  7183. } else {
  7184. $this->y = $this->h + $y;
  7185. }
  7186. if ($this->y < 0) {
  7187. $this->y = 0;
  7188. }
  7189. if ($this->y > $this->h) {
  7190. $this->y = $this->h;
  7191. }
  7192. }
  7193. /**
  7194. * Defines the abscissa and ordinate of the current position.
  7195. * If the passed values are negative, they are relative respectively to the right and bottom of the page.
  7196. * @param float $x The value of the abscissa.
  7197. * @param float $y The value of the ordinate.
  7198. * @param boolean $rtloff if true always uses the page top-left corner as origin of axis.
  7199. * @public
  7200. * @since 1.2
  7201. * @see SetX(), SetY()
  7202. */
  7203. public function setXY($x, $y, $rtloff=false) {
  7204. $this->setY($y, false, $rtloff);
  7205. $this->setX($x, $rtloff);
  7206. }
  7207. /**
  7208. * Set the absolute X coordinate of the current pointer.
  7209. * @param float $x The value of the abscissa in user units.
  7210. * @public
  7211. * @since 5.9.186 (2012-09-13)
  7212. * @see setAbsX(), setAbsY(), SetAbsXY()
  7213. */
  7214. public function setAbsX($x) {
  7215. $this->x = floatval($x);
  7216. }
  7217. /**
  7218. * Set the absolute Y coordinate of the current pointer.
  7219. * @param float $y (float) The value of the ordinate in user units.
  7220. * @public
  7221. * @since 5.9.186 (2012-09-13)
  7222. * @see setAbsX(), setAbsY(), SetAbsXY()
  7223. */
  7224. public function setAbsY($y) {
  7225. $this->y = floatval($y);
  7226. }
  7227. /**
  7228. * Set the absolute X and Y coordinates of the current pointer.
  7229. * @param float $x The value of the abscissa in user units.
  7230. * @param float $y (float) The value of the ordinate in user units.
  7231. * @public
  7232. * @since 5.9.186 (2012-09-13)
  7233. * @see setAbsX(), setAbsY(), SetAbsXY()
  7234. */
  7235. public function setAbsXY($x, $y) {
  7236. $this->setAbsX($x);
  7237. $this->setAbsY($y);
  7238. }
  7239. /**
  7240. * Send the document to a given destination: string, local file or browser.
  7241. * In the last case, the plug-in may be used (if present) or a download ("Save as" dialog box) may be forced.<br />
  7242. * The method first calls Close() if necessary to terminate the document.
  7243. * @param string $name The name of the file when saved
  7244. * @param string $dest Destination where to send the document. It can take one of the following values:<ul><li>I: send the file inline to the browser (default). The plug-in is used if available. The name given by name is used when one selects the "Save as" option on the link generating the PDF.</li><li>D: send to the browser and force a file download with the name given by name.</li><li>F: save to a local server file with the name given by name.</li><li>S: return the document as a string (name is ignored).</li><li>FI: equivalent to F + I option</li><li>FD: equivalent to F + D option</li><li>E: return the document as base64 mime multi-part email attachment (RFC 2045)</li></ul>
  7245. * @return string
  7246. * @public
  7247. * @since 1.0
  7248. * @see Close()
  7249. */
  7250. public function Output($name='doc.pdf', $dest='I') {
  7251. //Output PDF to some destination
  7252. //Finish document if necessary
  7253. if ($this->state < 3) {
  7254. $this->Close();
  7255. }
  7256. //Normalize parameters
  7257. if (is_bool($dest)) {
  7258. $dest = $dest ? 'D' : 'F';
  7259. }
  7260. $dest = strtoupper($dest);
  7261. if ($this->sign) {
  7262. // *** apply digital signature to the document ***
  7263. // get the document content
  7264. $pdfdoc = $this->getBuffer();
  7265. // remove last newline
  7266. $pdfdoc = substr($pdfdoc, 0, -1);
  7267. // remove filler space
  7268. $byterange_string_len = strlen(TCPDF_STATIC::$byterange_string);
  7269. // define the ByteRange
  7270. $byte_range = array();
  7271. $byte_range[0] = 0;
  7272. $byte_range[1] = strpos($pdfdoc, TCPDF_STATIC::$byterange_string) + $byterange_string_len + 10;
  7273. $byte_range[2] = $byte_range[1] + $this->signature_max_length + 2;
  7274. $byte_range[3] = strlen($pdfdoc) - $byte_range[2];
  7275. $pdfdoc = substr($pdfdoc, 0, $byte_range[1]).substr($pdfdoc, $byte_range[2]);
  7276. // replace the ByteRange
  7277. $byterange = sprintf('/ByteRange[0 %u %u %u]', $byte_range[1], $byte_range[2], $byte_range[3]);
  7278. $byterange .= str_repeat(' ', ($byterange_string_len - strlen($byterange)));
  7279. $pdfdoc = str_replace(TCPDF_STATIC::$byterange_string, $byterange, $pdfdoc);
  7280. // write the document to a temporary folder
  7281. $tempdoc = TCPDF_STATIC::getObjFilename('doc', $this->file_id);
  7282. $f = TCPDF_STATIC::fopenLocal($tempdoc, 'wb');
  7283. if (!$f) {
  7284. $this->Error('Unable to create temporary file: '.$tempdoc);
  7285. }
  7286. $pdfdoc_length = strlen($pdfdoc);
  7287. fwrite($f, $pdfdoc, $pdfdoc_length);
  7288. fclose($f);
  7289. // get digital signature via openssl library
  7290. $tempsign = TCPDF_STATIC::getObjFilename('sig', $this->file_id);
  7291. if (empty($this->signature_data['extracerts'])) {
  7292. openssl_pkcs7_sign($tempdoc, $tempsign, $this->signature_data['signcert'], array($this->signature_data['privkey'], $this->signature_data['password']), array(), PKCS7_BINARY | PKCS7_DETACHED);
  7293. } else {
  7294. openssl_pkcs7_sign($tempdoc, $tempsign, $this->signature_data['signcert'], array($this->signature_data['privkey'], $this->signature_data['password']), array(), PKCS7_BINARY | PKCS7_DETACHED, $this->signature_data['extracerts']);
  7295. }
  7296. // read signature
  7297. $signature = file_get_contents($tempsign);
  7298. // extract signature
  7299. $signature = substr($signature, $pdfdoc_length);
  7300. $signature = substr($signature, (strpos($signature, "%%EOF\n\n------") + 13));
  7301. $tmparr = explode("\n\n", $signature);
  7302. $signature = $tmparr[1];
  7303. // decode signature
  7304. $signature = base64_decode(trim($signature));
  7305. // add TSA timestamp to signature
  7306. $signature = $this->applyTSA($signature);
  7307. // convert signature to hex
  7308. $signature = current(unpack('H*', $signature));
  7309. $signature = str_pad($signature, $this->signature_max_length, '0');
  7310. // Add signature to the document
  7311. $this->buffer = substr($pdfdoc, 0, $byte_range[1]).'<'.$signature.'>'.substr($pdfdoc, $byte_range[1]);
  7312. $this->bufferlen = strlen($this->buffer);
  7313. }
  7314. switch($dest) {
  7315. case 'I': {
  7316. // Send PDF to the standard output
  7317. if (ob_get_contents()) {
  7318. $this->Error('Some data has already been output, can\'t send PDF file');
  7319. }
  7320. if (php_sapi_name() != 'cli') {
  7321. // send output to a browser
  7322. header('Content-Type: application/pdf');
  7323. if (headers_sent()) {
  7324. $this->Error('Some data has already been output to browser, can\'t send PDF file');
  7325. }
  7326. header('Cache-Control: private, must-revalidate, post-check=0, pre-check=0, max-age=1');
  7327. //header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
  7328. header('Pragma: public');
  7329. header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  7330. header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  7331. header('Content-Disposition: inline; filename="' . rawurlencode(basename($name)) . '"; ' .
  7332. 'filename*=UTF-8\'\'' . rawurlencode(basename($name)));
  7333. TCPDF_STATIC::sendOutputData($this->getBuffer(), $this->bufferlen);
  7334. } else {
  7335. echo $this->getBuffer();
  7336. }
  7337. break;
  7338. }
  7339. case 'D': {
  7340. // download PDF as file
  7341. if (ob_get_contents()) {
  7342. $this->Error('Some data has already been output, can\'t send PDF file');
  7343. }
  7344. header('Content-Description: File Transfer');
  7345. if (headers_sent()) {
  7346. $this->Error('Some data has already been output to browser, can\'t send PDF file');
  7347. }
  7348. header('Cache-Control: private, must-revalidate, post-check=0, pre-check=0, max-age=1');
  7349. //header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
  7350. header('Pragma: public');
  7351. header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  7352. header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  7353. // force download dialog
  7354. if (strpos(php_sapi_name(), 'cgi') === false) {
  7355. header('Content-Type: application/force-download');
  7356. header('Content-Type: application/octet-stream', false);
  7357. header('Content-Type: application/download', false);
  7358. header('Content-Type: application/pdf', false);
  7359. } else {
  7360. header('Content-Type: application/pdf');
  7361. }
  7362. // use the Content-Disposition header to supply a recommended filename
  7363. header('Content-Disposition: attachment; filename="' . rawurlencode(basename($name)) . '"; ' .
  7364. 'filename*=UTF-8\'\'' . rawurlencode(basename($name)));
  7365. header('Content-Transfer-Encoding: binary');
  7366. TCPDF_STATIC::sendOutputData($this->getBuffer(), $this->bufferlen);
  7367. break;
  7368. }
  7369. case 'F':
  7370. case 'FI':
  7371. case 'FD': {
  7372. // save PDF to a local file
  7373. $f = TCPDF_STATIC::fopenLocal($name, 'wb');
  7374. if (!$f) {
  7375. $this->Error('Unable to create output file: '.$name);
  7376. }
  7377. fwrite($f, $this->getBuffer(), $this->bufferlen);
  7378. fclose($f);
  7379. if ($dest == 'FI') {
  7380. // send headers to browser
  7381. header('Content-Type: application/pdf');
  7382. header('Cache-Control: private, must-revalidate, post-check=0, pre-check=0, max-age=1');
  7383. //header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
  7384. header('Pragma: public');
  7385. header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  7386. header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  7387. header('Content-Disposition: inline; filename="'.basename($name).'"');
  7388. TCPDF_STATIC::sendOutputData(file_get_contents($name), filesize($name));
  7389. } elseif ($dest == 'FD') {
  7390. // send headers to browser
  7391. if (ob_get_contents()) {
  7392. $this->Error('Some data has already been output, can\'t send PDF file');
  7393. }
  7394. header('Content-Description: File Transfer');
  7395. if (headers_sent()) {
  7396. $this->Error('Some data has already been output to browser, can\'t send PDF file');
  7397. }
  7398. header('Cache-Control: private, must-revalidate, post-check=0, pre-check=0, max-age=1');
  7399. header('Pragma: public');
  7400. header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  7401. header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  7402. // force download dialog
  7403. if (strpos(php_sapi_name(), 'cgi') === false) {
  7404. header('Content-Type: application/force-download');
  7405. header('Content-Type: application/octet-stream', false);
  7406. header('Content-Type: application/download', false);
  7407. header('Content-Type: application/pdf', false);
  7408. } else {
  7409. header('Content-Type: application/pdf');
  7410. }
  7411. // use the Content-Disposition header to supply a recommended filename
  7412. header('Content-Disposition: attachment; filename="'.basename($name).'"');
  7413. header('Content-Transfer-Encoding: binary');
  7414. TCPDF_STATIC::sendOutputData(file_get_contents($name), filesize($name));
  7415. }
  7416. break;
  7417. }
  7418. case 'E': {
  7419. // return PDF as base64 mime multi-part email attachment (RFC 2045)
  7420. $retval = 'Content-Type: application/pdf;'."\r\n";
  7421. $retval .= ' name="'.$name.'"'."\r\n";
  7422. $retval .= 'Content-Transfer-Encoding: base64'."\r\n";
  7423. $retval .= 'Content-Disposition: attachment;'."\r\n";
  7424. $retval .= ' filename="'.$name.'"'."\r\n\r\n";
  7425. $retval .= chunk_split(base64_encode($this->getBuffer()), 76, "\r\n");
  7426. return $retval;
  7427. }
  7428. case 'S': {
  7429. // returns PDF as a string
  7430. return $this->getBuffer();
  7431. }
  7432. default: {
  7433. $this->Error('Incorrect output destination: '.$dest);
  7434. }
  7435. }
  7436. return '';
  7437. }
  7438. protected static $cleaned_ids = array();
  7439. /**
  7440. * Unset all class variables except the following critical variables.
  7441. * @param boolean $destroyall if true destroys all class variables, otherwise preserves critical variables.
  7442. * @param boolean $preserve_objcopy if true preserves the objcopy variable
  7443. * @public
  7444. * @since 4.5.016 (2009-02-24)
  7445. */
  7446. public function _destroy($destroyall=false, $preserve_objcopy=false) {
  7447. if (isset(self::$cleaned_ids[$this->file_id])) {
  7448. $destroyall = false;
  7449. }
  7450. if ($destroyall AND !$preserve_objcopy && isset($this->file_id)) {
  7451. self::$cleaned_ids[$this->file_id] = true;
  7452. // remove all temporary files
  7453. if ($handle = @opendir(K_PATH_CACHE)) {
  7454. while ( false !== ( $file_name = readdir( $handle ) ) ) {
  7455. if (strpos($file_name, '__tcpdf_'.$this->file_id.'_') === 0) {
  7456. unlink(K_PATH_CACHE.$file_name);
  7457. }
  7458. }
  7459. closedir($handle);
  7460. }
  7461. if (isset($this->imagekeys)) {
  7462. foreach($this->imagekeys as $file) {
  7463. if (strpos($file, K_PATH_CACHE) === 0 && TCPDF_STATIC::file_exists($file)) {
  7464. @unlink($file);
  7465. }
  7466. }
  7467. }
  7468. }
  7469. $preserve = array(
  7470. 'file_id',
  7471. 'state',
  7472. 'bufferlen',
  7473. 'buffer',
  7474. 'cached_files',
  7475. 'imagekeys',
  7476. 'sign',
  7477. 'signature_data',
  7478. 'signature_max_length',
  7479. 'byterange_string',
  7480. 'tsa_timestamp',
  7481. 'tsa_data'
  7482. );
  7483. foreach (array_keys(get_object_vars($this)) as $val) {
  7484. if ($destroyall OR !in_array($val, $preserve)) {
  7485. if ((!$preserve_objcopy OR ($val != 'objcopy')) AND ($val != 'file_id') AND isset($this->$val)) {
  7486. unset($this->$val);
  7487. }
  7488. }
  7489. }
  7490. }
  7491. /**
  7492. * Check for locale-related bug
  7493. * @protected
  7494. */
  7495. protected function _dochecks() {
  7496. //Check for locale-related bug
  7497. if (1.1 == 1) {
  7498. $this->Error('Don\'t alter the locale before including class file');
  7499. }
  7500. //Check for decimal separator
  7501. if (sprintf('%.1F', 1.0) != '1.0') {
  7502. setlocale(LC_NUMERIC, 'C');
  7503. }
  7504. }
  7505. /**
  7506. * Return an array containing variations for the basic page number alias.
  7507. * @param string $a Base alias.
  7508. * @return array of page number aliases
  7509. * @protected
  7510. */
  7511. protected function getInternalPageNumberAliases($a= '') {
  7512. $alias = array();
  7513. // build array of Unicode + ASCII variants (the order is important)
  7514. $alias = array('u' => array(), 'a' => array());
  7515. $u = '{'.$a.'}';
  7516. $alias['u'][] = TCPDF_STATIC::_escape($u);
  7517. if ($this->isunicode) {
  7518. $alias['u'][] = TCPDF_STATIC::_escape(TCPDF_FONTS::UTF8ToLatin1($u, $this->isunicode, $this->CurrentFont));
  7519. $alias['u'][] = TCPDF_STATIC::_escape(TCPDF_FONTS::utf8StrRev($u, false, $this->tmprtl, $this->isunicode, $this->CurrentFont));
  7520. $alias['a'][] = TCPDF_STATIC::_escape(TCPDF_FONTS::UTF8ToLatin1($a, $this->isunicode, $this->CurrentFont));
  7521. $alias['a'][] = TCPDF_STATIC::_escape(TCPDF_FONTS::utf8StrRev($a, false, $this->tmprtl, $this->isunicode, $this->CurrentFont));
  7522. }
  7523. $alias['a'][] = TCPDF_STATIC::_escape($a);
  7524. return $alias;
  7525. }
  7526. /**
  7527. * Return an array containing all internal page aliases.
  7528. * @return array of page number aliases
  7529. * @protected
  7530. */
  7531. protected function getAllInternalPageNumberAliases() {
  7532. $basic_alias = array(TCPDF_STATIC::$alias_tot_pages, TCPDF_STATIC::$alias_num_page, TCPDF_STATIC::$alias_group_tot_pages, TCPDF_STATIC::$alias_group_num_page, TCPDF_STATIC::$alias_right_shift);
  7533. $pnalias = array();
  7534. foreach($basic_alias as $k => $a) {
  7535. $pnalias[$k] = $this->getInternalPageNumberAliases($a);
  7536. }
  7537. return $pnalias;
  7538. }
  7539. /**
  7540. * Replace right shift page number aliases with spaces to correct right alignment.
  7541. * This works perfectly only when using monospaced fonts.
  7542. * @param string $page Page content.
  7543. * @param array $aliases Array of page aliases.
  7544. * @param int $diff initial difference to add.
  7545. * @return string replaced page content.
  7546. * @protected
  7547. */
  7548. protected function replaceRightShiftPageNumAliases($page, $aliases, $diff) {
  7549. foreach ($aliases as $type => $alias) {
  7550. foreach ($alias as $a) {
  7551. // find position of compensation factor
  7552. $startnum = (strpos($a, ':') + 1);
  7553. $a = substr($a, 0, $startnum);
  7554. if (($pos = strpos($page, $a)) !== false) {
  7555. // end of alias
  7556. $endnum = strpos($page, '}', $pos);
  7557. // string to be replaced
  7558. $aa = substr($page, $pos, ($endnum - $pos + 1));
  7559. // get compensation factor
  7560. $ratio = substr($page, ($pos + $startnum), ($endnum - $pos - $startnum));
  7561. $ratio = preg_replace('/[^0-9\.]/', '', $ratio);
  7562. $ratio = floatval($ratio);
  7563. if ($type == 'u') {
  7564. $chrdiff = floor(($diff + 12) * $ratio);
  7565. $shift = str_repeat(' ', $chrdiff);
  7566. $shift = TCPDF_FONTS::UTF8ToUTF16BE($shift, false, $this->isunicode, $this->CurrentFont);
  7567. } else {
  7568. $chrdiff = floor(($diff + 11) * $ratio);
  7569. $shift = str_repeat(' ', $chrdiff);
  7570. }
  7571. $page = str_replace($aa, $shift, $page);
  7572. }
  7573. }
  7574. }
  7575. return $page;
  7576. }
  7577. /**
  7578. * Set page boxes to be included on page descriptions.
  7579. * @param array $boxes Array of page boxes to set on document: ('MediaBox', 'CropBox', 'BleedBox', 'TrimBox', 'ArtBox').
  7580. * @protected
  7581. */
  7582. protected function setPageBoxTypes($boxes) {
  7583. $this->page_boxes = array();
  7584. foreach ($boxes as $box) {
  7585. if (in_array($box, TCPDF_STATIC::$pageboxes)) {
  7586. $this->page_boxes[] = $box;
  7587. }
  7588. }
  7589. }
  7590. /**
  7591. * Output pages (and replace page number aliases).
  7592. * @protected
  7593. */
  7594. protected function _putpages() {
  7595. $filter = ($this->compress) ? '/Filter /FlateDecode ' : '';
  7596. // get internal aliases for page numbers
  7597. $pnalias = $this->getAllInternalPageNumberAliases();
  7598. $num_pages = $this->numpages;
  7599. $ptpa = TCPDF_STATIC::formatPageNumber(($this->starting_page_number + $num_pages - 1));
  7600. $ptpu = TCPDF_FONTS::UTF8ToUTF16BE($ptpa, false, $this->isunicode, $this->CurrentFont);
  7601. $ptp_num_chars = $this->GetNumChars($ptpa);
  7602. $pagegroupnum = 0;
  7603. $groupnum = 0;
  7604. $ptgu = 1;
  7605. $ptga = 1;
  7606. $ptg_num_chars = 1;
  7607. for ($n = 1; $n <= $num_pages; ++$n) {
  7608. // get current page
  7609. $temppage = $this->getPageBuffer($n);
  7610. $pagelen = strlen($temppage);
  7611. // set replacements for total pages number
  7612. $pnpa = TCPDF_STATIC::formatPageNumber(($this->starting_page_number + $n - 1));
  7613. $pnpu = TCPDF_FONTS::UTF8ToUTF16BE($pnpa, false, $this->isunicode, $this->CurrentFont);
  7614. $pnp_num_chars = $this->GetNumChars($pnpa);
  7615. $pdiff = 0; // difference used for right shift alignment of page numbers
  7616. $gdiff = 0; // difference used for right shift alignment of page group numbers
  7617. if (!empty($this->pagegroups)) {
  7618. if (isset($this->newpagegroup[$n])) {
  7619. $pagegroupnum = 0;
  7620. ++$groupnum;
  7621. $ptga = TCPDF_STATIC::formatPageNumber($this->pagegroups[$groupnum]);
  7622. $ptgu = TCPDF_FONTS::UTF8ToUTF16BE($ptga, false, $this->isunicode, $this->CurrentFont);
  7623. $ptg_num_chars = $this->GetNumChars($ptga);
  7624. }
  7625. ++$pagegroupnum;
  7626. $pnga = TCPDF_STATIC::formatPageNumber($pagegroupnum);
  7627. $pngu = TCPDF_FONTS::UTF8ToUTF16BE($pnga, false, $this->isunicode, $this->CurrentFont);
  7628. $png_num_chars = $this->GetNumChars($pnga);
  7629. // replace page numbers
  7630. $replace = array();
  7631. $replace[] = array($ptgu, $ptg_num_chars, 9, $pnalias[2]['u']);
  7632. $replace[] = array($ptga, $ptg_num_chars, 7, $pnalias[2]['a']);
  7633. $replace[] = array($pngu, $png_num_chars, 9, $pnalias[3]['u']);
  7634. $replace[] = array($pnga, $png_num_chars, 7, $pnalias[3]['a']);
  7635. list($temppage, $gdiff) = TCPDF_STATIC::replacePageNumAliases($temppage, $replace, $gdiff);
  7636. }
  7637. // replace page numbers
  7638. $replace = array();
  7639. $replace[] = array($ptpu, $ptp_num_chars, 9, $pnalias[0]['u']);
  7640. $replace[] = array($ptpa, $ptp_num_chars, 7, $pnalias[0]['a']);
  7641. $replace[] = array($pnpu, $pnp_num_chars, 9, $pnalias[1]['u']);
  7642. $replace[] = array($pnpa, $pnp_num_chars, 7, $pnalias[1]['a']);
  7643. list($temppage, $pdiff) = TCPDF_STATIC::replacePageNumAliases($temppage, $replace, $pdiff);
  7644. // replace right shift alias
  7645. $temppage = $this->replaceRightShiftPageNumAliases($temppage, $pnalias[4], max($pdiff, $gdiff));
  7646. // replace EPS marker
  7647. $temppage = str_replace($this->epsmarker, '', $temppage);
  7648. //Page
  7649. $this->page_obj_id[$n] = $this->_newobj();
  7650. $out = '<<';
  7651. $out .= ' /Type /Page';
  7652. $out .= ' /Parent 1 0 R';
  7653. if (empty($this->signature_data['approval']) OR ($this->signature_data['approval'] != 'A')) {
  7654. $out .= ' /LastModified '.$this->_datestring(0, $this->doc_modification_timestamp);
  7655. }
  7656. $out .= ' /Resources 2 0 R';
  7657. foreach ($this->page_boxes as $box) {
  7658. $out .= ' /'.$box;
  7659. $out .= sprintf(' [%F %F %F %F]', $this->pagedim[$n][$box]['llx'], $this->pagedim[$n][$box]['lly'], $this->pagedim[$n][$box]['urx'], $this->pagedim[$n][$box]['ury']);
  7660. }
  7661. if (isset($this->pagedim[$n]['BoxColorInfo']) AND !empty($this->pagedim[$n]['BoxColorInfo'])) {
  7662. $out .= ' /BoxColorInfo <<';
  7663. foreach ($this->page_boxes as $box) {
  7664. if (isset($this->pagedim[$n]['BoxColorInfo'][$box])) {
  7665. $out .= ' /'.$box.' <<';
  7666. if (isset($this->pagedim[$n]['BoxColorInfo'][$box]['C'])) {
  7667. $color = $this->pagedim[$n]['BoxColorInfo'][$box]['C'];
  7668. $out .= ' /C [';
  7669. $out .= sprintf(' %F %F %F', ($color[0] / 255), ($color[1] / 255), ($color[2] / 255));
  7670. $out .= ' ]';
  7671. }
  7672. if (isset($this->pagedim[$n]['BoxColorInfo'][$box]['W'])) {
  7673. $out .= ' /W '.($this->pagedim[$n]['BoxColorInfo'][$box]['W'] * $this->k);
  7674. }
  7675. if (isset($this->pagedim[$n]['BoxColorInfo'][$box]['S'])) {
  7676. $out .= ' /S /'.$this->pagedim[$n]['BoxColorInfo'][$box]['S'];
  7677. }
  7678. if (isset($this->pagedim[$n]['BoxColorInfo'][$box]['D'])) {
  7679. $dashes = $this->pagedim[$n]['BoxColorInfo'][$box]['D'];
  7680. $out .= ' /D [';
  7681. foreach ($dashes as $dash) {
  7682. $out .= sprintf(' %F', ($dash * $this->k));
  7683. }
  7684. $out .= ' ]';
  7685. }
  7686. $out .= ' >>';
  7687. }
  7688. }
  7689. $out .= ' >>';
  7690. }
  7691. $out .= ' /Contents '.($this->n + 1).' 0 R';
  7692. $out .= ' /Rotate '.$this->pagedim[$n]['Rotate'];
  7693. if (!$this->pdfa_mode || $this->pdfa_version >= 2) {
  7694. $out .= ' /Group << /Type /Group /S /Transparency /CS /DeviceRGB >>';
  7695. }
  7696. if (isset($this->pagedim[$n]['trans']) AND !empty($this->pagedim[$n]['trans'])) {
  7697. // page transitions
  7698. if (isset($this->pagedim[$n]['trans']['Dur'])) {
  7699. $out .= ' /Dur '.$this->pagedim[$n]['trans']['Dur'];
  7700. }
  7701. $out .= ' /Trans <<';
  7702. $out .= ' /Type /Trans';
  7703. if (isset($this->pagedim[$n]['trans']['S'])) {
  7704. $out .= ' /S /'.$this->pagedim[$n]['trans']['S'];
  7705. }
  7706. if (isset($this->pagedim[$n]['trans']['D'])) {
  7707. $out .= ' /D '.$this->pagedim[$n]['trans']['D'];
  7708. }
  7709. if (isset($this->pagedim[$n]['trans']['Dm'])) {
  7710. $out .= ' /Dm /'.$this->pagedim[$n]['trans']['Dm'];
  7711. }
  7712. if (isset($this->pagedim[$n]['trans']['M'])) {
  7713. $out .= ' /M /'.$this->pagedim[$n]['trans']['M'];
  7714. }
  7715. if (isset($this->pagedim[$n]['trans']['Di'])) {
  7716. $out .= ' /Di '.$this->pagedim[$n]['trans']['Di'];
  7717. }
  7718. if (isset($this->pagedim[$n]['trans']['SS'])) {
  7719. $out .= ' /SS '.$this->pagedim[$n]['trans']['SS'];
  7720. }
  7721. if (isset($this->pagedim[$n]['trans']['B'])) {
  7722. $out .= ' /B '.$this->pagedim[$n]['trans']['B'];
  7723. }
  7724. $out .= ' >>';
  7725. }
  7726. $out .= $this->_getannotsrefs($n);
  7727. $out .= ' /PZ '.$this->pagedim[$n]['PZ'];
  7728. $out .= ' >>';
  7729. $out .= "\n".'endobj';
  7730. $this->_out($out);
  7731. //Page content
  7732. $p = ($this->compress) ? gzcompress($temppage) : $temppage;
  7733. $this->_newobj();
  7734. $p = $this->_getrawstream($p);
  7735. $this->_out('<<'.$filter.'/Length '.strlen($p).'>> stream'."\n".$p."\n".'endstream'."\n".'endobj');
  7736. }
  7737. //Pages root
  7738. $out = $this->_getobj(1)."\n";
  7739. $out .= '<< /Type /Pages /Kids [';
  7740. foreach($this->page_obj_id as $page_obj) {
  7741. $out .= ' '.$page_obj.' 0 R';
  7742. }
  7743. $out .= ' ] /Count '.$num_pages.' >>';
  7744. $out .= "\n".'endobj';
  7745. $this->_out($out);
  7746. }
  7747. /**
  7748. * Get references to page annotations.
  7749. * @param int $n page number
  7750. * @return string
  7751. * @protected
  7752. * @author Nicola Asuni
  7753. * @since 5.0.010 (2010-05-17)
  7754. */
  7755. protected function _getannotsrefs($n) {
  7756. if (!(isset($this->PageAnnots[$n]) OR count($this->empty_signature_appearance)>0 OR ($this->sign AND isset($this->signature_data['cert_type'])))) {
  7757. return '';
  7758. }
  7759. $out = ' /Annots [';
  7760. if (isset($this->PageAnnots[$n])) {
  7761. foreach ($this->PageAnnots[$n] as $key => $val) {
  7762. if (!in_array($val['n'], $this->radio_groups)) {
  7763. $out .= ' '.$val['n'].' 0 R';
  7764. }
  7765. }
  7766. // add radiobutton groups
  7767. if (isset($this->radiobutton_groups[$n])) {
  7768. foreach ($this->radiobutton_groups[$n] as $key => $data) {
  7769. if (isset($data['n'])) {
  7770. $out .= ' '.$data['n'].' 0 R';
  7771. }
  7772. }
  7773. }
  7774. }
  7775. if ($this->sign AND ($n == $this->signature_appearance['page']) AND isset($this->signature_data['cert_type'])) {
  7776. // set reference for signature object
  7777. $out .= ' '.$this->sig_obj_id.' 0 R';
  7778. }
  7779. if (!empty($this->empty_signature_appearance)) {
  7780. foreach ($this->empty_signature_appearance as $esa) {
  7781. if ($esa['page'] == $n) {
  7782. // set reference for empty signature objects
  7783. $out .= ' '.$esa['objid'].' 0 R';
  7784. }
  7785. }
  7786. }
  7787. $out .= ' ]';
  7788. return $out;
  7789. }
  7790. /**
  7791. * Output annotations objects for all pages.
  7792. * !!! THIS METHOD IS NOT YET COMPLETED !!!
  7793. * See section 12.5 of PDF 32000_2008 reference.
  7794. * @protected
  7795. * @author Nicola Asuni
  7796. * @since 4.0.018 (2008-08-06)
  7797. */
  7798. protected function _putannotsobjs() {
  7799. // reset object counter
  7800. for ($n=1; $n <= $this->numpages; ++$n) {
  7801. if (isset($this->PageAnnots[$n])) {
  7802. // set page annotations
  7803. foreach ($this->PageAnnots[$n] as $key => $pl) {
  7804. $annot_obj_id = $this->PageAnnots[$n][$key]['n'];
  7805. // create annotation object for grouping radiobuttons
  7806. if (isset($this->radiobutton_groups[$n][$pl['txt']]) AND is_array($this->radiobutton_groups[$n][$pl['txt']])) {
  7807. $radio_button_obj_id = $this->radiobutton_groups[$n][$pl['txt']]['n'];
  7808. $annots = '<<';
  7809. $annots .= ' /Type /Annot';
  7810. $annots .= ' /Subtype /Widget';
  7811. $annots .= ' /Rect [0 0 0 0]';
  7812. if ($this->radiobutton_groups[$n][$pl['txt']]['#readonly#']) {
  7813. // read only
  7814. $annots .= ' /F 68';
  7815. $annots .= ' /Ff 49153';
  7816. } else {
  7817. $annots .= ' /F 4'; // default print for PDF/A
  7818. $annots .= ' /Ff 49152';
  7819. }
  7820. $annots .= ' /T '.$this->_datastring($pl['txt'], $radio_button_obj_id);
  7821. if (isset($pl['opt']['tu']) AND is_string($pl['opt']['tu'])) {
  7822. $annots .= ' /TU '.$this->_datastring($pl['opt']['tu'], $radio_button_obj_id);
  7823. }
  7824. $annots .= ' /FT /Btn';
  7825. $annots .= ' /Kids [';
  7826. $defval = '';
  7827. foreach ($this->radiobutton_groups[$n][$pl['txt']] as $key => $data) {
  7828. if (isset($data['kid'])) {
  7829. $annots .= ' '.$data['kid'].' 0 R';
  7830. if ($data['def'] !== 'Off') {
  7831. $defval = $data['def'];
  7832. }
  7833. }
  7834. }
  7835. $annots .= ' ]';
  7836. if (!empty($defval)) {
  7837. $annots .= ' /V /'.$defval;
  7838. }
  7839. $annots .= ' >>';
  7840. $this->_out($this->_getobj($radio_button_obj_id)."\n".$annots."\n".'endobj');
  7841. $this->form_obj_id[] = $radio_button_obj_id;
  7842. // store object id to be used on Parent entry of Kids
  7843. $this->radiobutton_groups[$n][$pl['txt']] = $radio_button_obj_id;
  7844. }
  7845. $formfield = false;
  7846. $pl['opt'] = array_change_key_case($pl['opt'], CASE_LOWER);
  7847. $a = $pl['x'] * $this->k;
  7848. $b = $this->pagedim[$n]['h'] - (($pl['y'] + $pl['h']) * $this->k);
  7849. $c = $pl['w'] * $this->k;
  7850. $d = $pl['h'] * $this->k;
  7851. $rect = sprintf('%F %F %F %F', $a, $b, $a+$c, $b+$d);
  7852. // create new annotation object
  7853. $annots = '<</Type /Annot';
  7854. $annots .= ' /Subtype /'.$pl['opt']['subtype'];
  7855. $annots .= ' /Rect ['.$rect.']';
  7856. $ft = array('Btn', 'Tx', 'Ch', 'Sig');
  7857. if (isset($pl['opt']['ft']) AND in_array($pl['opt']['ft'], $ft)) {
  7858. $annots .= ' /FT /'.$pl['opt']['ft'];
  7859. $formfield = true;
  7860. }
  7861. if ($pl['opt']['subtype'] !== 'Link') {
  7862. $annots .= ' /Contents '.$this->_textstring($pl['txt'], $annot_obj_id);
  7863. }
  7864. $annots .= ' /P '.$this->page_obj_id[$n].' 0 R';
  7865. $annots .= ' /NM '.$this->_datastring(sprintf('%04u-%04u', $n, $key), $annot_obj_id);
  7866. $annots .= ' /M '.$this->_datestring($annot_obj_id, $this->doc_modification_timestamp);
  7867. if (isset($pl['opt']['f'])) {
  7868. $fval = 0;
  7869. if (is_array($pl['opt']['f'])) {
  7870. foreach ($pl['opt']['f'] as $f) {
  7871. switch (strtolower($f)) {
  7872. case 'invisible': {
  7873. $fval += 1 << 0;
  7874. break;
  7875. }
  7876. case 'hidden': {
  7877. $fval += 1 << 1;
  7878. break;
  7879. }
  7880. case 'print': {
  7881. $fval += 1 << 2;
  7882. break;
  7883. }
  7884. case 'nozoom': {
  7885. $fval += 1 << 3;
  7886. break;
  7887. }
  7888. case 'norotate': {
  7889. $fval += 1 << 4;
  7890. break;
  7891. }
  7892. case 'noview': {
  7893. $fval += 1 << 5;
  7894. break;
  7895. }
  7896. case 'readonly': {
  7897. $fval += 1 << 6;
  7898. break;
  7899. }
  7900. case 'locked': {
  7901. $fval += 1 << 8;
  7902. break;
  7903. }
  7904. case 'togglenoview': {
  7905. $fval += 1 << 9;
  7906. break;
  7907. }
  7908. case 'lockedcontents': {
  7909. $fval += 1 << 10;
  7910. break;
  7911. }
  7912. default: {
  7913. break;
  7914. }
  7915. }
  7916. }
  7917. } else {
  7918. $fval = intval($pl['opt']['f']);
  7919. }
  7920. } else {
  7921. $fval = 4;
  7922. }
  7923. if ($this->pdfa_mode) {
  7924. // force print flag for PDF/A mode
  7925. $fval |= 4;
  7926. }
  7927. $annots .= ' /F '.intval($fval);
  7928. if (isset($pl['opt']['as']) AND is_string($pl['opt']['as'])) {
  7929. $annots .= ' /AS /'.$pl['opt']['as'];
  7930. }
  7931. if (isset($pl['opt']['ap'])) {
  7932. // appearance stream
  7933. $annots .= ' /AP <<';
  7934. if (is_array($pl['opt']['ap'])) {
  7935. foreach ($pl['opt']['ap'] as $apmode => $apdef) {
  7936. // $apmode can be: n = normal; r = rollover; d = down;
  7937. $annots .= ' /'.strtoupper($apmode);
  7938. if (is_array($apdef)) {
  7939. $annots .= ' <<';
  7940. foreach ($apdef as $apstate => $stream) {
  7941. // reference to XObject that define the appearance for this mode-state
  7942. $apsobjid = $this->_putAPXObject($c, $d, $stream);
  7943. $annots .= ' /'.$apstate.' '.$apsobjid.' 0 R';
  7944. }
  7945. $annots .= ' >>';
  7946. } else {
  7947. // reference to XObject that define the appearance for this mode
  7948. $apsobjid = $this->_putAPXObject($c, $d, $apdef);
  7949. $annots .= ' '.$apsobjid.' 0 R';
  7950. }
  7951. }
  7952. } else {
  7953. $annots .= $pl['opt']['ap'];
  7954. }
  7955. $annots .= ' >>';
  7956. }
  7957. if (isset($pl['opt']['bs']) AND (is_array($pl['opt']['bs']))) {
  7958. $annots .= ' /BS <<';
  7959. $annots .= ' /Type /Border';
  7960. if (isset($pl['opt']['bs']['w'])) {
  7961. $annots .= ' /W '.intval($pl['opt']['bs']['w']);
  7962. }
  7963. $bstyles = array('S', 'D', 'B', 'I', 'U');
  7964. if (isset($pl['opt']['bs']['s']) AND in_array($pl['opt']['bs']['s'], $bstyles)) {
  7965. $annots .= ' /S /'.$pl['opt']['bs']['s'];
  7966. }
  7967. if (isset($pl['opt']['bs']['d']) AND (is_array($pl['opt']['bs']['d']))) {
  7968. $annots .= ' /D [';
  7969. foreach ($pl['opt']['bs']['d'] as $cord) {
  7970. $annots .= ' '.intval($cord);
  7971. }
  7972. $annots .= ']';
  7973. }
  7974. $annots .= ' >>';
  7975. } else {
  7976. $annots .= ' /Border [';
  7977. if (isset($pl['opt']['border']) AND (count($pl['opt']['border']) >= 3)) {
  7978. $annots .= intval($pl['opt']['border'][0]).' ';
  7979. $annots .= intval($pl['opt']['border'][1]).' ';
  7980. $annots .= intval($pl['opt']['border'][2]);
  7981. if (isset($pl['opt']['border'][3]) AND is_array($pl['opt']['border'][3])) {
  7982. $annots .= ' [';
  7983. foreach ($pl['opt']['border'][3] as $dash) {
  7984. $annots .= intval($dash).' ';
  7985. }
  7986. $annots .= ']';
  7987. }
  7988. } else {
  7989. $annots .= '0 0 0';
  7990. }
  7991. $annots .= ']';
  7992. }
  7993. if (isset($pl['opt']['be']) AND (is_array($pl['opt']['be']))) {
  7994. $annots .= ' /BE <<';
  7995. $bstyles = array('S', 'C');
  7996. if (isset($pl['opt']['be']['s']) AND in_array($pl['opt']['be']['s'], $bstyles)) {
  7997. $annots .= ' /S /'.$pl['opt']['bs']['s'];
  7998. } else {
  7999. $annots .= ' /S /S';
  8000. }
  8001. if (isset($pl['opt']['be']['i']) AND ($pl['opt']['be']['i'] >= 0) AND ($pl['opt']['be']['i'] <= 2)) {
  8002. $annots .= ' /I '.sprintf(' %F', $pl['opt']['be']['i']);
  8003. }
  8004. $annots .= '>>';
  8005. }
  8006. if (isset($pl['opt']['c']) AND (is_array($pl['opt']['c'])) AND !empty($pl['opt']['c'])) {
  8007. $annots .= ' /C '.TCPDF_COLORS::getColorStringFromArray($pl['opt']['c']);
  8008. }
  8009. //$annots .= ' /StructParent ';
  8010. //$annots .= ' /OC ';
  8011. $markups = array('text', 'freetext', 'line', 'square', 'circle', 'polygon', 'polyline', 'highlight', 'underline', 'squiggly', 'strikeout', 'stamp', 'caret', 'ink', 'fileattachment', 'sound');
  8012. if (in_array(strtolower($pl['opt']['subtype']), $markups)) {
  8013. // this is a markup type
  8014. if (isset($pl['opt']['t']) AND is_string($pl['opt']['t'])) {
  8015. $annots .= ' /T '.$this->_textstring($pl['opt']['t'], $annot_obj_id);
  8016. }
  8017. //$annots .= ' /Popup ';
  8018. if (isset($pl['opt']['ca'])) {
  8019. $annots .= ' /CA '.sprintf('%F', floatval($pl['opt']['ca']));
  8020. }
  8021. if (isset($pl['opt']['rc'])) {
  8022. $annots .= ' /RC '.$this->_textstring($pl['opt']['rc'], $annot_obj_id);
  8023. }
  8024. $annots .= ' /CreationDate '.$this->_datestring($annot_obj_id, $this->doc_creation_timestamp);
  8025. //$annots .= ' /IRT ';
  8026. if (isset($pl['opt']['subj'])) {
  8027. $annots .= ' /Subj '.$this->_textstring($pl['opt']['subj'], $annot_obj_id);
  8028. }
  8029. //$annots .= ' /RT ';
  8030. //$annots .= ' /IT ';
  8031. //$annots .= ' /ExData ';
  8032. }
  8033. $lineendings = array('Square', 'Circle', 'Diamond', 'OpenArrow', 'ClosedArrow', 'None', 'Butt', 'ROpenArrow', 'RClosedArrow', 'Slash');
  8034. // Annotation types
  8035. switch (strtolower($pl['opt']['subtype'])) {
  8036. case 'text': {
  8037. if (isset($pl['opt']['open'])) {
  8038. $annots .= ' /Open '. (strtolower($pl['opt']['open']) == 'true' ? 'true' : 'false');
  8039. }
  8040. $iconsapp = array('Comment', 'Help', 'Insert', 'Key', 'NewParagraph', 'Note', 'Paragraph');
  8041. if (isset($pl['opt']['name']) AND in_array($pl['opt']['name'], $iconsapp)) {
  8042. $annots .= ' /Name /'.$pl['opt']['name'];
  8043. } else {
  8044. $annots .= ' /Name /Note';
  8045. }
  8046. $hasStateModel = isset($pl['opt']['statemodel']);
  8047. $hasState = isset($pl['opt']['state']);
  8048. $statemodels = array('Marked', 'Review');
  8049. if (!$hasStateModel && !$hasState) {
  8050. break;
  8051. }
  8052. if ($hasStateModel AND in_array($pl['opt']['statemodel'], $statemodels)) {
  8053. $annots .= ' /StateModel /'.$pl['opt']['statemodel'];
  8054. } else {
  8055. $pl['opt']['statemodel'] = 'Marked';
  8056. $annots .= ' /StateModel /'.$pl['opt']['statemodel'];
  8057. }
  8058. if ($pl['opt']['statemodel'] == 'Marked') {
  8059. $states = array('Accepted', 'Unmarked');
  8060. } else {
  8061. $states = array('Accepted', 'Rejected', 'Cancelled', 'Completed', 'None');
  8062. }
  8063. if ($hasState AND in_array($pl['opt']['state'], $states)) {
  8064. $annots .= ' /State /'.$pl['opt']['state'];
  8065. } else {
  8066. if ($pl['opt']['statemodel'] == 'Marked') {
  8067. $annots .= ' /State /Unmarked';
  8068. } else {
  8069. $annots .= ' /State /None';
  8070. }
  8071. }
  8072. break;
  8073. }
  8074. case 'link': {
  8075. if (is_string($pl['txt']) && !empty($pl['txt'])) {
  8076. if ($pl['txt'][0] == '#') {
  8077. // internal destination
  8078. $annots .= ' /A <</S /GoTo /D /'.TCPDF_STATIC::encodeNameObject(substr($pl['txt'], 1)).'>>';
  8079. } elseif ($pl['txt'][0] == '%') {
  8080. // embedded PDF file
  8081. $filename = basename(substr($pl['txt'], 1));
  8082. $annots .= ' /A << /S /GoToE /D [0 /Fit] /NewWindow true /T << /R /C /P '.($n - 1).' /A '.$this->embeddedfiles[$filename]['a'].' >> >>';
  8083. } elseif ($pl['txt'][0] == '*') {
  8084. // embedded generic file
  8085. $filename = basename(substr($pl['txt'], 1));
  8086. $jsa = 'var D=event.target.doc;var MyData=D.dataObjects;for (var i in MyData) if (MyData[i].path=="'.$filename.'") D.exportDataObject( { cName : MyData[i].name, nLaunch : 2});';
  8087. $annots .= ' /A << /S /JavaScript /JS '.$this->_textstring($jsa, $annot_obj_id).'>>';
  8088. } else {
  8089. $parsedUrl = parse_url($pl['txt']);
  8090. if (empty($parsedUrl['scheme']) AND (!empty($parsedUrl['path']) && strtolower(substr($parsedUrl['path'], -4)) == '.pdf')) {
  8091. // relative link to a PDF file
  8092. $dest = '[0 /Fit]'; // default page 0
  8093. if (!empty($parsedUrl['fragment'])) {
  8094. // check for named destination
  8095. $tmp = explode('=', $parsedUrl['fragment']);
  8096. $dest = '('.((count($tmp) == 2) ? $tmp[1] : $tmp[0]).')';
  8097. }
  8098. $annots .= ' /A <</S /GoToR /D '.$dest.' /F '.$this->_datastring($this->unhtmlentities($parsedUrl['path']), $annot_obj_id).' /NewWindow true>>';
  8099. } else {
  8100. // external URI link
  8101. $annots .= ' /A <</S /URI /URI '.$this->_datastring($this->unhtmlentities($pl['txt']), $annot_obj_id).'>>';
  8102. }
  8103. }
  8104. } elseif (isset($this->links[$pl['txt']])) {
  8105. // internal link ID
  8106. $l = $this->links[$pl['txt']];
  8107. if (isset($this->page_obj_id[($l['p'])])) {
  8108. $annots .= sprintf(' /Dest [%u 0 R /XYZ 0 %F null]', $this->page_obj_id[($l['p'])], ($this->pagedim[$l['p']]['h'] - ($l['y'] * $this->k)));
  8109. }
  8110. }
  8111. $hmodes = array('N', 'I', 'O', 'P');
  8112. if (isset($pl['opt']['h']) AND in_array($pl['opt']['h'], $hmodes)) {
  8113. $annots .= ' /H /'.$pl['opt']['h'];
  8114. } else {
  8115. $annots .= ' /H /I';
  8116. }
  8117. //$annots .= ' /PA ';
  8118. //$annots .= ' /Quadpoints ';
  8119. break;
  8120. }
  8121. case 'freetext': {
  8122. if (isset($pl['opt']['da']) AND !empty($pl['opt']['da'])) {
  8123. $annots .= ' /DA '.$this->_datastring($pl['opt']['da']);
  8124. }
  8125. if (isset($pl['opt']['q']) AND ($pl['opt']['q'] >= 0) AND ($pl['opt']['q'] <= 2)) {
  8126. $annots .= ' /Q '.intval($pl['opt']['q']);
  8127. }
  8128. if (isset($pl['opt']['rc'])) {
  8129. $annots .= ' /RC '.$this->_textstring($pl['opt']['rc'], $annot_obj_id);
  8130. }
  8131. if (isset($pl['opt']['ds'])) {
  8132. $annots .= ' /DS '.$this->_textstring($pl['opt']['ds'], $annot_obj_id);
  8133. }
  8134. if (isset($pl['opt']['cl']) AND is_array($pl['opt']['cl'])) {
  8135. $annots .= ' /CL [';
  8136. foreach ($pl['opt']['cl'] as $cl) {
  8137. $annots .= sprintf('%F ', $cl * $this->k);
  8138. }
  8139. $annots .= ']';
  8140. }
  8141. $tfit = array('FreeText', 'FreeTextCallout', 'FreeTextTypeWriter');
  8142. if (isset($pl['opt']['it']) AND in_array($pl['opt']['it'], $tfit)) {
  8143. $annots .= ' /IT /'.$pl['opt']['it'];
  8144. }
  8145. if (isset($pl['opt']['rd']) AND is_array($pl['opt']['rd'])) {
  8146. $l = $pl['opt']['rd'][0] * $this->k;
  8147. $r = $pl['opt']['rd'][1] * $this->k;
  8148. $t = $pl['opt']['rd'][2] * $this->k;
  8149. $b = $pl['opt']['rd'][3] * $this->k;
  8150. $annots .= ' /RD ['.sprintf('%F %F %F %F', $l, $r, $t, $b).']';
  8151. }
  8152. if (isset($pl['opt']['le']) AND in_array($pl['opt']['le'], $lineendings)) {
  8153. $annots .= ' /LE /'.$pl['opt']['le'];
  8154. }
  8155. break;
  8156. }
  8157. case 'line': {
  8158. break;
  8159. }
  8160. case 'square': {
  8161. break;
  8162. }
  8163. case 'circle': {
  8164. break;
  8165. }
  8166. case 'polygon': {
  8167. break;
  8168. }
  8169. case 'polyline': {
  8170. break;
  8171. }
  8172. case 'highlight': {
  8173. break;
  8174. }
  8175. case 'underline': {
  8176. break;
  8177. }
  8178. case 'squiggly': {
  8179. break;
  8180. }
  8181. case 'strikeout': {
  8182. break;
  8183. }
  8184. case 'stamp': {
  8185. break;
  8186. }
  8187. case 'caret': {
  8188. break;
  8189. }
  8190. case 'ink': {
  8191. break;
  8192. }
  8193. case 'popup': {
  8194. break;
  8195. }
  8196. case 'fileattachment': {
  8197. if ($this->pdfa_mode && $this->pdfa_version != 3) {
  8198. // embedded files are not allowed in PDF/A mode version 1 and 2
  8199. break;
  8200. }
  8201. if (!isset($pl['opt']['fs'])) {
  8202. break;
  8203. }
  8204. $filename = basename($pl['opt']['fs']);
  8205. if (isset($this->embeddedfiles[$filename]['f'])) {
  8206. $annots .= ' /FS '.$this->embeddedfiles[$filename]['f'].' 0 R';
  8207. $iconsapp = array('Graph', 'Paperclip', 'PushPin', 'Tag');
  8208. if (isset($pl['opt']['name']) AND in_array($pl['opt']['name'], $iconsapp)) {
  8209. $annots .= ' /Name /'.$pl['opt']['name'];
  8210. } else {
  8211. $annots .= ' /Name /PushPin';
  8212. }
  8213. // index (zero-based) of the annotation in the Annots array of this page
  8214. $this->embeddedfiles[$filename]['a'] = $key;
  8215. }
  8216. break;
  8217. }
  8218. case 'sound': {
  8219. if (!isset($pl['opt']['fs'])) {
  8220. break;
  8221. }
  8222. $filename = basename($pl['opt']['fs']);
  8223. if (isset($this->embeddedfiles[$filename]['f'])) {
  8224. // ... TO BE COMPLETED ...
  8225. // /R /C /B /E /CO /CP
  8226. $annots .= ' /Sound '.$this->embeddedfiles[$filename]['f'].' 0 R';
  8227. $iconsapp = array('Speaker', 'Mic');
  8228. if (isset($pl['opt']['name']) AND in_array($pl['opt']['name'], $iconsapp)) {
  8229. $annots .= ' /Name /'.$pl['opt']['name'];
  8230. } else {
  8231. $annots .= ' /Name /Speaker';
  8232. }
  8233. }
  8234. break;
  8235. }
  8236. case 'movie': {
  8237. break;
  8238. }
  8239. case 'widget': {
  8240. $hmode = array('N', 'I', 'O', 'P', 'T');
  8241. if (isset($pl['opt']['h']) AND in_array($pl['opt']['h'], $hmode)) {
  8242. $annots .= ' /H /'.$pl['opt']['h'];
  8243. }
  8244. if (isset($pl['opt']['mk']) AND (is_array($pl['opt']['mk'])) AND !empty($pl['opt']['mk'])) {
  8245. $annots .= ' /MK <<';
  8246. if (isset($pl['opt']['mk']['r'])) {
  8247. $annots .= ' /R '.$pl['opt']['mk']['r'];
  8248. }
  8249. if (isset($pl['opt']['mk']['bc']) AND (is_array($pl['opt']['mk']['bc']))) {
  8250. $annots .= ' /BC '.TCPDF_COLORS::getColorStringFromArray($pl['opt']['mk']['bc']);
  8251. }
  8252. if (isset($pl['opt']['mk']['bg']) AND (is_array($pl['opt']['mk']['bg']))) {
  8253. $annots .= ' /BG '.TCPDF_COLORS::getColorStringFromArray($pl['opt']['mk']['bg']);
  8254. }
  8255. if (isset($pl['opt']['mk']['ca'])) {
  8256. $annots .= ' /CA '.$pl['opt']['mk']['ca'];
  8257. }
  8258. if (isset($pl['opt']['mk']['rc'])) {
  8259. $annots .= ' /RC '.$pl['opt']['mk']['rc'];
  8260. }
  8261. if (isset($pl['opt']['mk']['ac'])) {
  8262. $annots .= ' /AC '.$pl['opt']['mk']['ac'];
  8263. }
  8264. if (isset($pl['opt']['mk']['i'])) {
  8265. $info = $this->getImageBuffer($pl['opt']['mk']['i']);
  8266. if ($info !== false) {
  8267. $annots .= ' /I '.$info['n'].' 0 R';
  8268. }
  8269. }
  8270. if (isset($pl['opt']['mk']['ri'])) {
  8271. $info = $this->getImageBuffer($pl['opt']['mk']['ri']);
  8272. if ($info !== false) {
  8273. $annots .= ' /RI '.$info['n'].' 0 R';
  8274. }
  8275. }
  8276. if (isset($pl['opt']['mk']['ix'])) {
  8277. $info = $this->getImageBuffer($pl['opt']['mk']['ix']);
  8278. if ($info !== false) {
  8279. $annots .= ' /IX '.$info['n'].' 0 R';
  8280. }
  8281. }
  8282. if (isset($pl['opt']['mk']['if']) AND (is_array($pl['opt']['mk']['if'])) AND !empty($pl['opt']['mk']['if'])) {
  8283. $annots .= ' /IF <<';
  8284. $if_sw = array('A', 'B', 'S', 'N');
  8285. if (isset($pl['opt']['mk']['if']['sw']) AND in_array($pl['opt']['mk']['if']['sw'], $if_sw)) {
  8286. $annots .= ' /SW /'.$pl['opt']['mk']['if']['sw'];
  8287. }
  8288. $if_s = array('A', 'P');
  8289. if (isset($pl['opt']['mk']['if']['s']) AND in_array($pl['opt']['mk']['if']['s'], $if_s)) {
  8290. $annots .= ' /S /'.$pl['opt']['mk']['if']['s'];
  8291. }
  8292. if (isset($pl['opt']['mk']['if']['a']) AND (is_array($pl['opt']['mk']['if']['a'])) AND !empty($pl['opt']['mk']['if']['a'])) {
  8293. $annots .= sprintf(' /A [%F %F]', $pl['opt']['mk']['if']['a'][0], $pl['opt']['mk']['if']['a'][1]);
  8294. }
  8295. if (isset($pl['opt']['mk']['if']['fb']) AND ($pl['opt']['mk']['if']['fb'])) {
  8296. $annots .= ' /FB true';
  8297. }
  8298. $annots .= '>>';
  8299. }
  8300. if (isset($pl['opt']['mk']['tp']) AND ($pl['opt']['mk']['tp'] >= 0) AND ($pl['opt']['mk']['tp'] <= 6)) {
  8301. $annots .= ' /TP '.intval($pl['opt']['mk']['tp']);
  8302. }
  8303. $annots .= '>>';
  8304. } // end MK
  8305. // --- Entries for field dictionaries ---
  8306. if (isset($this->radiobutton_groups[$n][$pl['txt']])) {
  8307. // set parent
  8308. $annots .= ' /Parent '.$this->radiobutton_groups[$n][$pl['txt']].' 0 R';
  8309. }
  8310. if (isset($pl['opt']['t']) AND is_string($pl['opt']['t'])) {
  8311. $annots .= ' /T '.$this->_datastring($pl['opt']['t'], $annot_obj_id);
  8312. }
  8313. if (isset($pl['opt']['tu']) AND is_string($pl['opt']['tu'])) {
  8314. $annots .= ' /TU '.$this->_datastring($pl['opt']['tu'], $annot_obj_id);
  8315. }
  8316. if (isset($pl['opt']['tm']) AND is_string($pl['opt']['tm'])) {
  8317. $annots .= ' /TM '.$this->_datastring($pl['opt']['tm'], $annot_obj_id);
  8318. }
  8319. if (isset($pl['opt']['ff'])) {
  8320. if (is_array($pl['opt']['ff'])) {
  8321. // array of bit settings
  8322. $flag = 0;
  8323. foreach($pl['opt']['ff'] as $val) {
  8324. $flag += 1 << ($val - 1);
  8325. }
  8326. } else {
  8327. $flag = intval($pl['opt']['ff']);
  8328. }
  8329. $annots .= ' /Ff '.$flag;
  8330. }
  8331. if (isset($pl['opt']['maxlen'])) {
  8332. $annots .= ' /MaxLen '.intval($pl['opt']['maxlen']);
  8333. }
  8334. if (isset($pl['opt']['v'])) {
  8335. $annots .= ' /V';
  8336. if (is_array($pl['opt']['v'])) {
  8337. foreach ($pl['opt']['v'] AS $optval) {
  8338. if (is_float($optval)) {
  8339. $optval = sprintf('%F', $optval);
  8340. }
  8341. $annots .= ' '.$optval;
  8342. }
  8343. } else {
  8344. $annots .= ' '.$this->_textstring($pl['opt']['v'], $annot_obj_id);
  8345. }
  8346. }
  8347. if (isset($pl['opt']['dv'])) {
  8348. $annots .= ' /DV';
  8349. if (is_array($pl['opt']['dv'])) {
  8350. foreach ($pl['opt']['dv'] AS $optval) {
  8351. if (is_float($optval)) {
  8352. $optval = sprintf('%F', $optval);
  8353. }
  8354. $annots .= ' '.$optval;
  8355. }
  8356. } else {
  8357. $annots .= ' '.$this->_textstring($pl['opt']['dv'], $annot_obj_id);
  8358. }
  8359. }
  8360. if (isset($pl['opt']['rv'])) {
  8361. $annots .= ' /RV';
  8362. if (is_array($pl['opt']['rv'])) {
  8363. foreach ($pl['opt']['rv'] AS $optval) {
  8364. if (is_float($optval)) {
  8365. $optval = sprintf('%F', $optval);
  8366. }
  8367. $annots .= ' '.$optval;
  8368. }
  8369. } else {
  8370. $annots .= ' '.$this->_textstring($pl['opt']['rv'], $annot_obj_id);
  8371. }
  8372. }
  8373. if (isset($pl['opt']['a']) AND !empty($pl['opt']['a'])) {
  8374. $annots .= ' /A << '.$pl['opt']['a'].' >>';
  8375. }
  8376. if (isset($pl['opt']['aa']) AND !empty($pl['opt']['aa'])) {
  8377. $annots .= ' /AA << '.$pl['opt']['aa'].' >>';
  8378. }
  8379. if (isset($pl['opt']['da']) AND !empty($pl['opt']['da'])) {
  8380. $annots .= ' /DA '.$this->_datastring($pl['opt']['da']);
  8381. }
  8382. if (isset($pl['opt']['q']) AND ($pl['opt']['q'] >= 0) AND ($pl['opt']['q'] <= 2)) {
  8383. $annots .= ' /Q '.intval($pl['opt']['q']);
  8384. }
  8385. if (isset($pl['opt']['opt']) AND (is_array($pl['opt']['opt'])) AND !empty($pl['opt']['opt'])) {
  8386. $annots .= ' /Opt [';
  8387. foreach($pl['opt']['opt'] AS $copt) {
  8388. if (is_array($copt)) {
  8389. $annots .= ' ['.$this->_textstring($copt[0], $annot_obj_id).' '.$this->_textstring($copt[1], $annot_obj_id).']';
  8390. } else {
  8391. $annots .= ' '.$this->_textstring($copt, $annot_obj_id);
  8392. }
  8393. }
  8394. $annots .= ']';
  8395. }
  8396. if (isset($pl['opt']['ti'])) {
  8397. $annots .= ' /TI '.intval($pl['opt']['ti']);
  8398. }
  8399. if (isset($pl['opt']['i']) AND (is_array($pl['opt']['i'])) AND !empty($pl['opt']['i'])) {
  8400. $annots .= ' /I [';
  8401. foreach($pl['opt']['i'] AS $copt) {
  8402. $annots .= intval($copt).' ';
  8403. }
  8404. $annots .= ']';
  8405. }
  8406. break;
  8407. }
  8408. case 'screen': {
  8409. break;
  8410. }
  8411. case 'printermark': {
  8412. break;
  8413. }
  8414. case 'trapnet': {
  8415. break;
  8416. }
  8417. case 'watermark': {
  8418. break;
  8419. }
  8420. case '3d': {
  8421. break;
  8422. }
  8423. default: {
  8424. break;
  8425. }
  8426. }
  8427. $annots .= '>>';
  8428. // create new annotation object
  8429. $this->_out($this->_getobj($annot_obj_id)."\n".$annots."\n".'endobj');
  8430. if ($formfield AND !isset($this->radiobutton_groups[$n][$pl['txt']])) {
  8431. // store reference of form object
  8432. $this->form_obj_id[] = $annot_obj_id;
  8433. }
  8434. }
  8435. }
  8436. } // end for each page
  8437. }
  8438. /**
  8439. * Put appearance streams XObject used to define annotation's appearance states.
  8440. * @param int $w annotation width
  8441. * @param int $h annotation height
  8442. * @param string $stream appearance stream
  8443. * @return int object ID
  8444. * @protected
  8445. * @since 4.8.001 (2009-09-09)
  8446. */
  8447. protected function _putAPXObject($w=0, $h=0, $stream='') {
  8448. $stream = trim($stream);
  8449. $out = $this->_getobj()."\n";
  8450. $this->xobjects['AX'.$this->n] = array('n' => $this->n);
  8451. $out .= '<<';
  8452. $out .= ' /Type /XObject';
  8453. $out .= ' /Subtype /Form';
  8454. $out .= ' /FormType 1';
  8455. if ($this->compress) {
  8456. $stream = gzcompress($stream);
  8457. $out .= ' /Filter /FlateDecode';
  8458. }
  8459. $rect = sprintf('%F %F', $w, $h);
  8460. $out .= ' /BBox [0 0 '.$rect.']';
  8461. $out .= ' /Matrix [1 0 0 1 0 0]';
  8462. $out .= ' /Resources 2 0 R';
  8463. $stream = $this->_getrawstream($stream);
  8464. $out .= ' /Length '.strlen($stream);
  8465. $out .= ' >>';
  8466. $out .= ' stream'."\n".$stream."\n".'endstream';
  8467. $out .= "\n".'endobj';
  8468. $this->_out($out);
  8469. return $this->n;
  8470. }
  8471. /**
  8472. * Output fonts.
  8473. * @author Nicola Asuni
  8474. * @protected
  8475. */
  8476. protected function _putfonts() {
  8477. $nf = $this->n;
  8478. foreach ($this->diffs as $diff) {
  8479. //Encodings
  8480. $this->_newobj();
  8481. $this->_out('<< /Type /Encoding /BaseEncoding /WinAnsiEncoding /Differences ['.$diff.'] >>'."\n".'endobj');
  8482. }
  8483. foreach ($this->FontFiles as $file => $info) {
  8484. // search and get font file to embedd
  8485. $fontfile = TCPDF_FONTS::getFontFullPath($file, $info['fontdir']);
  8486. if (!TCPDF_STATIC::empty_string($fontfile)) {
  8487. $font = file_get_contents($fontfile);
  8488. $compressed = (substr($file, -2) == '.z');
  8489. if ((!$compressed) AND (isset($info['length2']))) {
  8490. $header = (ord($font[0]) == 128);
  8491. if ($header) {
  8492. // strip first binary header
  8493. $font = substr($font, 6);
  8494. }
  8495. if ($header AND (ord($font[$info['length1']]) == 128)) {
  8496. // strip second binary header
  8497. $font = substr($font, 0, $info['length1']).substr($font, ($info['length1'] + 6));
  8498. }
  8499. } elseif ($info['subset'] AND ((!$compressed) OR ($compressed AND function_exists('gzcompress')))) {
  8500. if ($compressed) {
  8501. // uncompress font
  8502. $font = gzuncompress($font);
  8503. }
  8504. // merge subset characters
  8505. $subsetchars = array(); // used chars
  8506. foreach ($info['fontkeys'] as $fontkey) {
  8507. $fontinfo = $this->getFontBuffer($fontkey);
  8508. $subsetchars += $fontinfo['subsetchars'];
  8509. }
  8510. // rebuild a font subset
  8511. $font = TCPDF_FONTS::_getTrueTypeFontSubset($font, $subsetchars);
  8512. // calculate new font length
  8513. $info['length1'] = strlen($font);
  8514. if ($compressed) {
  8515. // recompress font
  8516. $font = gzcompress($font);
  8517. }
  8518. }
  8519. $this->_newobj();
  8520. $this->FontFiles[$file]['n'] = $this->n;
  8521. $stream = $this->_getrawstream($font);
  8522. $out = '<< /Length '.strlen($stream);
  8523. if ($compressed) {
  8524. $out .= ' /Filter /FlateDecode';
  8525. }
  8526. $out .= ' /Length1 '.$info['length1'];
  8527. if (isset($info['length2'])) {
  8528. $out .= ' /Length2 '.$info['length2'].' /Length3 0';
  8529. }
  8530. $out .= ' >>';
  8531. $out .= ' stream'."\n".$stream."\n".'endstream';
  8532. $out .= "\n".'endobj';
  8533. $this->_out($out);
  8534. }
  8535. }
  8536. foreach ($this->fontkeys as $k) {
  8537. //Font objects
  8538. $font = $this->getFontBuffer($k);
  8539. $type = $font['type'];
  8540. $name = $font['name'];
  8541. if ($type == 'core') {
  8542. // standard core font
  8543. $out = $this->_getobj($this->font_obj_ids[$k])."\n";
  8544. $out .= '<</Type /Font';
  8545. $out .= ' /Subtype /Type1';
  8546. $out .= ' /BaseFont /'.$name;
  8547. $out .= ' /Name /F'.$font['i'];
  8548. if ((strtolower($name) != 'symbol') AND (strtolower($name) != 'zapfdingbats')) {
  8549. $out .= ' /Encoding /WinAnsiEncoding';
  8550. }
  8551. if ($k == 'helvetica') {
  8552. // add default font for annotations
  8553. $this->annotation_fonts[$k] = $font['i'];
  8554. }
  8555. $out .= ' >>';
  8556. $out .= "\n".'endobj';
  8557. $this->_out($out);
  8558. } elseif (($type == 'Type1') OR ($type == 'TrueType')) {
  8559. // additional Type1 or TrueType font
  8560. $out = $this->_getobj($this->font_obj_ids[$k])."\n";
  8561. $out .= '<</Type /Font';
  8562. $out .= ' /Subtype /'.$type;
  8563. $out .= ' /BaseFont /'.$name;
  8564. $out .= ' /Name /F'.$font['i'];
  8565. $out .= ' /FirstChar 32 /LastChar 255';
  8566. $out .= ' /Widths '.($this->n + 1).' 0 R';
  8567. $out .= ' /FontDescriptor '.($this->n + 2).' 0 R';
  8568. if ($font['enc']) {
  8569. if (isset($font['diff'])) {
  8570. $out .= ' /Encoding '.($nf + $font['diff']).' 0 R';
  8571. } else {
  8572. $out .= ' /Encoding /WinAnsiEncoding';
  8573. }
  8574. }
  8575. $out .= ' >>';
  8576. $out .= "\n".'endobj';
  8577. $this->_out($out);
  8578. // Widths
  8579. $this->_newobj();
  8580. $s = '[';
  8581. for ($i = 32; $i < 256; ++$i) {
  8582. if (isset($font['cw'][$i])) {
  8583. $s .= $font['cw'][$i].' ';
  8584. } else {
  8585. $s .= $font['dw'].' ';
  8586. }
  8587. }
  8588. $s .= ']';
  8589. $s .= "\n".'endobj';
  8590. $this->_out($s);
  8591. //Descriptor
  8592. $this->_newobj();
  8593. $s = '<</Type /FontDescriptor /FontName /'.$name;
  8594. foreach ($font['desc'] as $fdk => $fdv) {
  8595. if (is_float($fdv)) {
  8596. $fdv = sprintf('%F', $fdv);
  8597. }
  8598. $s .= ' /'.$fdk.' '.$fdv.'';
  8599. }
  8600. if (!TCPDF_STATIC::empty_string($font['file'])) {
  8601. $s .= ' /FontFile'.($type == 'Type1' ? '' : '2').' '.$this->FontFiles[$font['file']]['n'].' 0 R';
  8602. }
  8603. $s .= '>>';
  8604. $s .= "\n".'endobj';
  8605. $this->_out($s);
  8606. } else {
  8607. // additional types
  8608. $mtd = '_put'.strtolower($type);
  8609. if (!method_exists($this, $mtd)) {
  8610. $this->Error('Unsupported font type: '.$type);
  8611. }
  8612. $this->$mtd($font);
  8613. }
  8614. }
  8615. }
  8616. /**
  8617. * Adds unicode fonts.<br>
  8618. * Based on PDF Reference 1.3 (section 5)
  8619. * @param array $font font data
  8620. * @protected
  8621. * @author Nicola Asuni
  8622. * @since 1.52.0.TC005 (2005-01-05)
  8623. */
  8624. protected function _puttruetypeunicode($font) {
  8625. $fontname = '';
  8626. if ($font['subset']) {
  8627. // change name for font subsetting
  8628. $subtag = sprintf('%06u', $font['i']);
  8629. $subtag = strtr($subtag, '0123456789', 'ABCDEFGHIJ');
  8630. $fontname .= $subtag.'+';
  8631. }
  8632. $fontname .= $font['name'];
  8633. // Type0 Font
  8634. // A composite font composed of other fonts, organized hierarchically
  8635. $out = $this->_getobj($this->font_obj_ids[$font['fontkey']])."\n";
  8636. $out .= '<< /Type /Font';
  8637. $out .= ' /Subtype /Type0';
  8638. $out .= ' /BaseFont /'.$fontname;
  8639. $out .= ' /Name /F'.$font['i'];
  8640. $out .= ' /Encoding /'.$font['enc'];
  8641. $out .= ' /ToUnicode '.($this->n + 1).' 0 R';
  8642. $out .= ' /DescendantFonts ['.($this->n + 2).' 0 R]';
  8643. $out .= ' >>';
  8644. $out .= "\n".'endobj';
  8645. $this->_out($out);
  8646. // ToUnicode map for Identity-H
  8647. $stream = TCPDF_FONT_DATA::$uni_identity_h;
  8648. // ToUnicode Object
  8649. $this->_newobj();
  8650. $stream = ($this->compress) ? gzcompress($stream) : $stream;
  8651. $filter = ($this->compress) ? '/Filter /FlateDecode ' : '';
  8652. $stream = $this->_getrawstream($stream);
  8653. $this->_out('<<'.$filter.'/Length '.strlen($stream).'>> stream'."\n".$stream."\n".'endstream'."\n".'endobj');
  8654. // CIDFontType2
  8655. // A CIDFont whose glyph descriptions are based on TrueType font technology
  8656. $oid = $this->_newobj();
  8657. $out = '<< /Type /Font';
  8658. $out .= ' /Subtype /CIDFontType2';
  8659. $out .= ' /BaseFont /'.$fontname;
  8660. // A dictionary containing entries that define the character collection of the CIDFont.
  8661. $cidinfo = '/Registry '.$this->_datastring($font['cidinfo']['Registry'], $oid);
  8662. $cidinfo .= ' /Ordering '.$this->_datastring($font['cidinfo']['Ordering'], $oid);
  8663. $cidinfo .= ' /Supplement '.$font['cidinfo']['Supplement'];
  8664. $out .= ' /CIDSystemInfo << '.$cidinfo.' >>';
  8665. $out .= ' /FontDescriptor '.($this->n + 1).' 0 R';
  8666. $out .= ' /DW '.$font['dw']; // default width
  8667. $out .= "\n".TCPDF_FONTS::_putfontwidths($font, 0);
  8668. if (isset($font['ctg']) AND (!TCPDF_STATIC::empty_string($font['ctg']))) {
  8669. $out .= "\n".'/CIDToGIDMap '.($this->n + 2).' 0 R';
  8670. }
  8671. $out .= ' >>';
  8672. $out .= "\n".'endobj';
  8673. $this->_out($out);
  8674. // Font descriptor
  8675. // A font descriptor describing the CIDFont default metrics other than its glyph widths
  8676. $this->_newobj();
  8677. $out = '<< /Type /FontDescriptor';
  8678. $out .= ' /FontName /'.$fontname;
  8679. foreach ($font['desc'] as $key => $value) {
  8680. if (is_float($value)) {
  8681. $value = sprintf('%F', $value);
  8682. }
  8683. $out .= ' /'.$key.' '.$value;
  8684. }
  8685. $fontdir = false;
  8686. if (!TCPDF_STATIC::empty_string($font['file'])) {
  8687. // A stream containing a TrueType font
  8688. $out .= ' /FontFile2 '.$this->FontFiles[$font['file']]['n'].' 0 R';
  8689. $fontdir = $this->FontFiles[$font['file']]['fontdir'];
  8690. }
  8691. $out .= ' >>';
  8692. $out .= "\n".'endobj';
  8693. $this->_out($out);
  8694. if (isset($font['ctg']) AND (!TCPDF_STATIC::empty_string($font['ctg']))) {
  8695. $this->_newobj();
  8696. // Embed CIDToGIDMap
  8697. // A specification of the mapping from CIDs to glyph indices
  8698. // search and get CTG font file to embedd
  8699. $ctgfile = strtolower($font['ctg']);
  8700. // search and get ctg font file to embedd
  8701. $fontfile = TCPDF_FONTS::getFontFullPath($ctgfile, $fontdir);
  8702. if (TCPDF_STATIC::empty_string($fontfile)) {
  8703. $this->Error('Font file not found: '.$ctgfile);
  8704. }
  8705. $stream = $this->_getrawstream(file_get_contents($fontfile));
  8706. $out = '<< /Length '.strlen($stream).'';
  8707. if (substr($fontfile, -2) == '.z') { // check file extension
  8708. // Decompresses data encoded using the public-domain
  8709. // zlib/deflate compression method, reproducing the
  8710. // original text or binary data
  8711. $out .= ' /Filter /FlateDecode';
  8712. }
  8713. $out .= ' >>';
  8714. $out .= ' stream'."\n".$stream."\n".'endstream';
  8715. $out .= "\n".'endobj';
  8716. $this->_out($out);
  8717. }
  8718. }
  8719. /**
  8720. * Output CID-0 fonts.
  8721. * A Type 0 CIDFont contains glyph descriptions based on the Adobe Type 1 font format
  8722. * @param array $font font data
  8723. * @protected
  8724. * @author Andrew Whitehead, Nicola Asuni, Yukihiro Nakadaira
  8725. * @since 3.2.000 (2008-06-23)
  8726. */
  8727. protected function _putcidfont0($font) {
  8728. $cidoffset = 0;
  8729. if (!isset($font['cw'][1])) {
  8730. $cidoffset = 31;
  8731. }
  8732. if (isset($font['cidinfo']['uni2cid'])) {
  8733. // convert unicode to cid.
  8734. $uni2cid = $font['cidinfo']['uni2cid'];
  8735. $cw = array();
  8736. foreach ($font['cw'] as $uni => $width) {
  8737. if (isset($uni2cid[$uni])) {
  8738. $cw[($uni2cid[$uni] + $cidoffset)] = $width;
  8739. } elseif ($uni < 256) {
  8740. $cw[$uni] = $width;
  8741. } // else unknown character
  8742. }
  8743. $font = array_merge($font, array('cw' => $cw));
  8744. }
  8745. $name = $font['name'];
  8746. $enc = $font['enc'];
  8747. if ($enc) {
  8748. $longname = $name.'-'.$enc;
  8749. } else {
  8750. $longname = $name;
  8751. }
  8752. $out = $this->_getobj($this->font_obj_ids[$font['fontkey']])."\n";
  8753. $out .= '<</Type /Font';
  8754. $out .= ' /Subtype /Type0';
  8755. $out .= ' /BaseFont /'.$longname;
  8756. $out .= ' /Name /F'.$font['i'];
  8757. if ($enc) {
  8758. $out .= ' /Encoding /'.$enc;
  8759. }
  8760. $out .= ' /DescendantFonts ['.($this->n + 1).' 0 R]';
  8761. $out .= ' >>';
  8762. $out .= "\n".'endobj';
  8763. $this->_out($out);
  8764. $oid = $this->_newobj();
  8765. $out = '<</Type /Font';
  8766. $out .= ' /Subtype /CIDFontType0';
  8767. $out .= ' /BaseFont /'.$name;
  8768. $cidinfo = '/Registry '.$this->_datastring($font['cidinfo']['Registry'], $oid);
  8769. $cidinfo .= ' /Ordering '.$this->_datastring($font['cidinfo']['Ordering'], $oid);
  8770. $cidinfo .= ' /Supplement '.$font['cidinfo']['Supplement'];
  8771. $out .= ' /CIDSystemInfo <<'.$cidinfo.'>>';
  8772. $out .= ' /FontDescriptor '.($this->n + 1).' 0 R';
  8773. $out .= ' /DW '.$font['dw'];
  8774. $out .= "\n".TCPDF_FONTS::_putfontwidths($font, $cidoffset);
  8775. $out .= ' >>';
  8776. $out .= "\n".'endobj';
  8777. $this->_out($out);
  8778. $this->_newobj();
  8779. $s = '<</Type /FontDescriptor /FontName /'.$name;
  8780. foreach ($font['desc'] as $k => $v) {
  8781. if ($k != 'Style') {
  8782. if (is_float($v)) {
  8783. $v = sprintf('%F', $v);
  8784. }
  8785. $s .= ' /'.$k.' '.$v.'';
  8786. }
  8787. }
  8788. $s .= '>>';
  8789. $s .= "\n".'endobj';
  8790. $this->_out($s);
  8791. }
  8792. /**
  8793. * Output images.
  8794. * @protected
  8795. */
  8796. protected function _putimages() {
  8797. $filter = ($this->compress) ? '/Filter /FlateDecode ' : '';
  8798. foreach ($this->imagekeys as $file) {
  8799. $info = $this->getImageBuffer($file);
  8800. // set object for alternate images array
  8801. $altoid = null;
  8802. if ((!$this->pdfa_mode) AND isset($info['altimgs']) AND !empty($info['altimgs'])) {
  8803. $altoid = $this->_newobj();
  8804. $out = '[';
  8805. foreach ($info['altimgs'] as $altimage) {
  8806. if (isset($this->xobjects['I'.$altimage[0]]['n'])) {
  8807. $out .= ' << /Image '.$this->xobjects['I'.$altimage[0]]['n'].' 0 R';
  8808. $out .= ' /DefaultForPrinting';
  8809. if ($altimage[1] === true) {
  8810. $out .= ' true';
  8811. } else {
  8812. $out .= ' false';
  8813. }
  8814. $out .= ' >>';
  8815. }
  8816. }
  8817. $out .= ' ]';
  8818. $out .= "\n".'endobj';
  8819. $this->_out($out);
  8820. }
  8821. // set image object
  8822. $oid = $this->_newobj();
  8823. $this->xobjects['I'.$info['i']] = array('n' => $oid);
  8824. $this->setImageSubBuffer($file, 'n', $this->n);
  8825. $out = '<</Type /XObject';
  8826. $out .= ' /Subtype /Image';
  8827. $out .= ' /Width '.$info['w'];
  8828. $out .= ' /Height '.$info['h'];
  8829. if (array_key_exists('masked', $info)) {
  8830. $out .= ' /SMask '.($this->n - 1).' 0 R';
  8831. }
  8832. // set color space
  8833. $icc = false;
  8834. if (isset($info['icc']) AND ($info['icc'] !== false)) {
  8835. // ICC Colour Space
  8836. $icc = true;
  8837. $out .= ' /ColorSpace [/ICCBased '.($this->n + 1).' 0 R]';
  8838. } elseif ($info['cs'] == 'Indexed') {
  8839. // Indexed Colour Space
  8840. $out .= ' /ColorSpace [/Indexed /DeviceRGB '.((strlen($info['pal']) / 3) - 1).' '.($this->n + 1).' 0 R]';
  8841. } else {
  8842. // Device Colour Space
  8843. $out .= ' /ColorSpace /'.$info['cs'];
  8844. }
  8845. if ($info['cs'] == 'DeviceCMYK') {
  8846. $out .= ' /Decode [1 0 1 0 1 0 1 0]';
  8847. }
  8848. $out .= ' /BitsPerComponent '.$info['bpc'];
  8849. if ($altoid > 0) {
  8850. // reference to alternate images dictionary
  8851. $out .= ' /Alternates '.$altoid.' 0 R';
  8852. }
  8853. if (isset($info['exurl']) AND !empty($info['exurl'])) {
  8854. // external stream
  8855. $out .= ' /Length 0';
  8856. $out .= ' /F << /FS /URL /F '.$this->_datastring($info['exurl'], $oid).' >>';
  8857. if (isset($info['f'])) {
  8858. $out .= ' /FFilter /'.$info['f'];
  8859. }
  8860. $out .= ' >>';
  8861. $out .= ' stream'."\n".'endstream';
  8862. } else {
  8863. if (isset($info['f'])) {
  8864. $out .= ' /Filter /'.$info['f'];
  8865. }
  8866. if (isset($info['parms'])) {
  8867. $out .= ' '.$info['parms'];
  8868. }
  8869. if (isset($info['trns']) AND is_array($info['trns'])) {
  8870. $trns = '';
  8871. $count_info = count($info['trns']);
  8872. if ($info['cs'] == 'Indexed') {
  8873. $maxval =(pow(2, $info['bpc']) - 1);
  8874. for ($i = 0; $i < $count_info; ++$i) {
  8875. if (($info['trns'][$i] != 0) AND ($info['trns'][$i] != $maxval)) {
  8876. // this is not a binary type mask @TODO: create a SMask
  8877. $trns = '';
  8878. break;
  8879. } elseif (empty($trns) AND ($info['trns'][$i] == 0)) {
  8880. // store the first fully transparent value
  8881. $trns .= $i.' '.$i.' ';
  8882. }
  8883. }
  8884. } else {
  8885. // grayscale or RGB
  8886. for ($i = 0; $i < $count_info; ++$i) {
  8887. if ($info['trns'][$i] == 0) {
  8888. $trns .= $info['trns'][$i].' '.$info['trns'][$i].' ';
  8889. }
  8890. }
  8891. }
  8892. // Colour Key Masking
  8893. if (!empty($trns)) {
  8894. $out .= ' /Mask ['.$trns.']';
  8895. }
  8896. }
  8897. $stream = $this->_getrawstream($info['data']);
  8898. $out .= ' /Length '.strlen($stream).' >>';
  8899. $out .= ' stream'."\n".$stream."\n".'endstream';
  8900. }
  8901. $out .= "\n".'endobj';
  8902. $this->_out($out);
  8903. if ($icc) {
  8904. // ICC colour profile
  8905. $this->_newobj();
  8906. $icc = ($this->compress) ? gzcompress($info['icc']) : $info['icc'];
  8907. $icc = $this->_getrawstream($icc);
  8908. $this->_out('<</N '.$info['ch'].' /Alternate /'.$info['cs'].' '.$filter.'/Length '.strlen($icc).'>> stream'."\n".$icc."\n".'endstream'."\n".'endobj');
  8909. } elseif ($info['cs'] == 'Indexed') {
  8910. // colour palette
  8911. $this->_newobj();
  8912. $pal = ($this->compress) ? gzcompress($info['pal']) : $info['pal'];
  8913. $pal = $this->_getrawstream($pal);
  8914. $this->_out('<<'.$filter.'/Length '.strlen($pal).'>> stream'."\n".$pal."\n".'endstream'."\n".'endobj');
  8915. }
  8916. }
  8917. }
  8918. /**
  8919. * Output Form XObjects Templates.
  8920. * @author Nicola Asuni
  8921. * @since 5.8.017 (2010-08-24)
  8922. * @protected
  8923. * @see startTemplate(), endTemplate(), printTemplate()
  8924. */
  8925. protected function _putxobjects() {
  8926. foreach ($this->xobjects as $key => $data) {
  8927. if (isset($data['outdata'])) {
  8928. $stream = str_replace($this->epsmarker, '', trim($data['outdata']));
  8929. $out = $this->_getobj($data['n'])."\n";
  8930. $out .= '<<';
  8931. $out .= ' /Type /XObject';
  8932. $out .= ' /Subtype /Form';
  8933. $out .= ' /FormType 1';
  8934. if ($this->compress) {
  8935. $stream = gzcompress($stream);
  8936. $out .= ' /Filter /FlateDecode';
  8937. }
  8938. $out .= sprintf(' /BBox [%F %F %F %F]', ($data['x'] * $this->k), (-$data['y'] * $this->k), (($data['w'] + $data['x']) * $this->k), (($data['h'] - $data['y']) * $this->k));
  8939. $out .= ' /Matrix [1 0 0 1 0 0]';
  8940. $out .= ' /Resources <<';
  8941. $out .= ' /ProcSet [/PDF /Text /ImageB /ImageC /ImageI]';
  8942. if (!$this->pdfa_mode || $this->pdfa_version >= 2) {
  8943. // transparency
  8944. if (isset($data['extgstates']) AND !empty($data['extgstates'])) {
  8945. $out .= ' /ExtGState <<';
  8946. foreach ($data['extgstates'] as $k => $extgstate) {
  8947. if (isset($this->extgstates[$k]['name'])) {
  8948. $out .= ' /'.$this->extgstates[$k]['name'];
  8949. } else {
  8950. $out .= ' /GS'.$k;
  8951. }
  8952. $out .= ' '.$this->extgstates[$k]['n'].' 0 R';
  8953. }
  8954. $out .= ' >>';
  8955. }
  8956. if (isset($data['gradients']) AND !empty($data['gradients'])) {
  8957. $gp = '';
  8958. $gs = '';
  8959. foreach ($data['gradients'] as $id => $grad) {
  8960. // gradient patterns
  8961. $gp .= ' /p'.$id.' '.$this->gradients[$id]['pattern'].' 0 R';
  8962. // gradient shadings
  8963. $gs .= ' /Sh'.$id.' '.$this->gradients[$id]['id'].' 0 R';
  8964. }
  8965. $out .= ' /Pattern <<'.$gp.' >>';
  8966. $out .= ' /Shading <<'.$gs.' >>';
  8967. }
  8968. }
  8969. // spot colors
  8970. if (isset($data['spot_colors']) AND !empty($data['spot_colors'])) {
  8971. $out .= ' /ColorSpace <<';
  8972. foreach ($data['spot_colors'] as $name => $color) {
  8973. $out .= ' /CS'.$color['i'].' '.$this->spot_colors[$name]['n'].' 0 R';
  8974. }
  8975. $out .= ' >>';
  8976. }
  8977. // fonts
  8978. if (!empty($data['fonts'])) {
  8979. $out .= ' /Font <<';
  8980. foreach ($data['fonts'] as $fontkey => $fontid) {
  8981. $out .= ' /F'.$fontid.' '.$this->font_obj_ids[$fontkey].' 0 R';
  8982. }
  8983. $out .= ' >>';
  8984. }
  8985. // images or nested xobjects
  8986. if (!empty($data['images']) OR !empty($data['xobjects'])) {
  8987. $out .= ' /XObject <<';
  8988. foreach ($data['images'] as $imgid) {
  8989. $out .= ' /I'.$imgid.' '.$this->xobjects['I'.$imgid]['n'].' 0 R';
  8990. }
  8991. foreach ($data['xobjects'] as $sub_id => $sub_objid) {
  8992. $out .= ' /'.$sub_id.' '.$sub_objid['n'].' 0 R';
  8993. }
  8994. $out .= ' >>';
  8995. }
  8996. $out .= ' >>'; //end resources
  8997. if (isset($data['group']) AND ($data['group'] !== false)) {
  8998. // set transparency group
  8999. $out .= ' /Group << /Type /Group /S /Transparency';
  9000. if (is_array($data['group'])) {
  9001. if (isset($data['group']['CS']) AND !empty($data['group']['CS'])) {
  9002. $out .= ' /CS /'.$data['group']['CS'];
  9003. }
  9004. if (isset($data['group']['I'])) {
  9005. $out .= ' /I /'.($data['group']['I']===true?'true':'false');
  9006. }
  9007. if (isset($data['group']['K'])) {
  9008. $out .= ' /K /'.($data['group']['K']===true?'true':'false');
  9009. }
  9010. }
  9011. $out .= ' >>';
  9012. }
  9013. $stream = $this->_getrawstream($stream, $data['n']);
  9014. $out .= ' /Length '.strlen($stream);
  9015. $out .= ' >>';
  9016. $out .= ' stream'."\n".$stream."\n".'endstream';
  9017. $out .= "\n".'endobj';
  9018. $this->_out($out);
  9019. }
  9020. }
  9021. }
  9022. /**
  9023. * Output Spot Colors Resources.
  9024. * @protected
  9025. * @since 4.0.024 (2008-09-12)
  9026. */
  9027. protected function _putspotcolors() {
  9028. foreach ($this->spot_colors as $name => $color) {
  9029. $this->_newobj();
  9030. $this->spot_colors[$name]['n'] = $this->n;
  9031. $out = '[/Separation /'.str_replace(' ', '#20', $name);
  9032. $out .= ' /DeviceCMYK <<';
  9033. $out .= ' /Range [0 1 0 1 0 1 0 1] /C0 [0 0 0 0]';
  9034. $out .= ' '.sprintf('/C1 [%F %F %F %F] ', ($color['C'] / 100), ($color['M'] / 100), ($color['Y'] / 100), ($color['K'] / 100));
  9035. $out .= ' /FunctionType 2 /Domain [0 1] /N 1>>]';
  9036. $out .= "\n".'endobj';
  9037. $this->_out($out);
  9038. }
  9039. }
  9040. /**
  9041. * Return XObjects Dictionary.
  9042. * @return string XObjects dictionary
  9043. * @protected
  9044. * @since 5.8.014 (2010-08-23)
  9045. */
  9046. protected function _getxobjectdict() {
  9047. $out = '';
  9048. foreach ($this->xobjects as $id => $objid) {
  9049. $out .= ' /'.$id.' '.$objid['n'].' 0 R';
  9050. }
  9051. return $out;
  9052. }
  9053. /**
  9054. * Output Resources Dictionary.
  9055. * @protected
  9056. */
  9057. protected function _putresourcedict() {
  9058. $out = $this->_getobj(2)."\n";
  9059. $out .= '<< /ProcSet [/PDF /Text /ImageB /ImageC /ImageI]';
  9060. $out .= ' /Font <<';
  9061. foreach ($this->fontkeys as $fontkey) {
  9062. $font = $this->getFontBuffer($fontkey);
  9063. $out .= ' /F'.$font['i'].' '.$font['n'].' 0 R';
  9064. }
  9065. $out .= ' >>';
  9066. $out .= ' /XObject <<';
  9067. $out .= $this->_getxobjectdict();
  9068. $out .= ' >>';
  9069. // layers
  9070. if (!empty($this->pdflayers)) {
  9071. $out .= ' /Properties <<';
  9072. foreach ($this->pdflayers as $layer) {
  9073. $out .= ' /'.$layer['layer'].' '.$layer['objid'].' 0 R';
  9074. }
  9075. $out .= ' >>';
  9076. }
  9077. if (!$this->pdfa_mode || $this->pdfa_version >= 2) {
  9078. // transparency
  9079. if (isset($this->extgstates) AND !empty($this->extgstates)) {
  9080. $out .= ' /ExtGState <<';
  9081. foreach ($this->extgstates as $k => $extgstate) {
  9082. if (isset($extgstate['name'])) {
  9083. $out .= ' /'.$extgstate['name'];
  9084. } else {
  9085. $out .= ' /GS'.$k;
  9086. }
  9087. $out .= ' '.$extgstate['n'].' 0 R';
  9088. }
  9089. $out .= ' >>';
  9090. }
  9091. if (isset($this->gradients) AND !empty($this->gradients)) {
  9092. $gp = '';
  9093. $gs = '';
  9094. foreach ($this->gradients as $id => $grad) {
  9095. // gradient patterns
  9096. $gp .= ' /p'.$id.' '.$grad['pattern'].' 0 R';
  9097. // gradient shadings
  9098. $gs .= ' /Sh'.$id.' '.$grad['id'].' 0 R';
  9099. }
  9100. $out .= ' /Pattern <<'.$gp.' >>';
  9101. $out .= ' /Shading <<'.$gs.' >>';
  9102. }
  9103. }
  9104. // spot colors
  9105. if (isset($this->spot_colors) AND !empty($this->spot_colors)) {
  9106. $out .= ' /ColorSpace <<';
  9107. foreach ($this->spot_colors as $color) {
  9108. $out .= ' /CS'.$color['i'].' '.$color['n'].' 0 R';
  9109. }
  9110. $out .= ' >>';
  9111. }
  9112. $out .= ' >>';
  9113. $out .= "\n".'endobj';
  9114. $this->_out($out);
  9115. }
  9116. /**
  9117. * Output Resources.
  9118. * @protected
  9119. */
  9120. protected function _putresources() {
  9121. $this->_putextgstates();
  9122. $this->_putocg();
  9123. $this->_putfonts();
  9124. $this->_putimages();
  9125. $this->_putspotcolors();
  9126. $this->_putshaders();
  9127. $this->_putxobjects();
  9128. $this->_putresourcedict();
  9129. $this->_putdests();
  9130. $this->_putEmbeddedFiles();
  9131. $this->_putannotsobjs();
  9132. $this->_putjavascript();
  9133. $this->_putbookmarks();
  9134. $this->_putencryption();
  9135. }
  9136. /**
  9137. * Adds some Metadata information (Document Information Dictionary)
  9138. * (see Chapter 14.3.3 Document Information Dictionary of PDF32000_2008.pdf Reference)
  9139. * @return int object id
  9140. * @protected
  9141. */
  9142. protected function _putinfo() {
  9143. $oid = $this->_newobj();
  9144. $out = '<<';
  9145. // store current isunicode value
  9146. $prev_isunicode = $this->isunicode;
  9147. if ($this->docinfounicode) {
  9148. $this->isunicode = true;
  9149. }
  9150. if (!TCPDF_STATIC::empty_string($this->title)) {
  9151. // The document's title.
  9152. $out .= ' /Title '.$this->_textstring($this->title, $oid);
  9153. }
  9154. if (!TCPDF_STATIC::empty_string($this->author)) {
  9155. // The name of the person who created the document.
  9156. $out .= ' /Author '.$this->_textstring($this->author, $oid);
  9157. }
  9158. if (!TCPDF_STATIC::empty_string($this->subject)) {
  9159. // The subject of the document.
  9160. $out .= ' /Subject '.$this->_textstring($this->subject, $oid);
  9161. }
  9162. if (!TCPDF_STATIC::empty_string($this->keywords)) {
  9163. // Keywords associated with the document.
  9164. $out .= ' /Keywords '.$this->_textstring($this->keywords, $oid);
  9165. }
  9166. if (!TCPDF_STATIC::empty_string($this->creator)) {
  9167. // If the document was converted to PDF from another format, the name of the conforming product that created the original document from which it was converted.
  9168. $out .= ' /Creator '.$this->_textstring($this->creator, $oid);
  9169. }
  9170. // restore previous isunicode value
  9171. $this->isunicode = $prev_isunicode;
  9172. // default producer
  9173. $out .= ' /Producer '.$this->_textstring(TCPDF_STATIC::getTCPDFProducer(), $oid);
  9174. // The date and time the document was created, in human-readable form
  9175. $out .= ' /CreationDate '.$this->_datestring(0, $this->doc_creation_timestamp);
  9176. // The date and time the document was most recently modified, in human-readable form
  9177. $out .= ' /ModDate '.$this->_datestring(0, $this->doc_modification_timestamp);
  9178. // A name object indicating whether the document has been modified to include trapping information
  9179. $out .= ' /Trapped /False';
  9180. $out .= ' >>';
  9181. $out .= "\n".'endobj';
  9182. $this->_out($out);
  9183. return $oid;
  9184. }
  9185. /**
  9186. * Set additional XMP data to be added on the default XMP data just before the end of "x:xmpmeta" tag.
  9187. * IMPORTANT: This data is added as-is without controls, so you have to validate your data before using this method!
  9188. * @param string $xmp Custom XMP data.
  9189. * @since 5.9.128 (2011-10-06)
  9190. * @public
  9191. */
  9192. public function setExtraXMP($xmp) {
  9193. $this->custom_xmp = $xmp;
  9194. }
  9195. /**
  9196. * Set additional XMP data to be added on the default XMP data just before the end of "rdf:RDF" tag.
  9197. * IMPORTANT: This data is added as-is without controls, so you have to validate your data before using this method!
  9198. * @param string $xmp Custom XMP RDF data.
  9199. * @since 6.3.0 (2019-09-19)
  9200. * @public
  9201. */
  9202. public function setExtraXMPRDF($xmp) {
  9203. $this->custom_xmp_rdf = $xmp;
  9204. }
  9205. /**
  9206. * Put XMP data object and return ID.
  9207. * @return int The object ID.
  9208. * @since 5.9.121 (2011-09-28)
  9209. * @protected
  9210. */
  9211. protected function _putXMP() {
  9212. $oid = $this->_newobj();
  9213. // store current isunicode value
  9214. $prev_isunicode = $this->isunicode;
  9215. $this->isunicode = true;
  9216. $prev_encrypted = $this->encrypted;
  9217. $this->encrypted = false;
  9218. // set XMP data
  9219. $xmp = '<?xpacket begin="'.TCPDF_FONTS::unichr(0xfeff, $this->isunicode).'" id="W5M0MpCehiHzreSzNTczkc9d"?>'."\n";
  9220. $xmp .= '<x:xmpmeta xmlns:x="adobe:ns:meta/" x:xmptk="Adobe XMP Core 4.2.1-c043 52.372728, 2009/01/18-15:08:04">'."\n";
  9221. $xmp .= "\t".'<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">'."\n";
  9222. $xmp .= "\t\t".'<rdf:Description rdf:about="" xmlns:dc="http://purl.org/dc/elements/1.1/">'."\n";
  9223. $xmp .= "\t\t\t".'<dc:format>application/pdf</dc:format>'."\n";
  9224. $xmp .= "\t\t\t".'<dc:title>'."\n";
  9225. $xmp .= "\t\t\t\t".'<rdf:Alt>'."\n";
  9226. $xmp .= "\t\t\t\t\t".'<rdf:li xml:lang="x-default">'.TCPDF_STATIC::_escapeXML($this->title).'</rdf:li>'."\n";
  9227. $xmp .= "\t\t\t\t".'</rdf:Alt>'."\n";
  9228. $xmp .= "\t\t\t".'</dc:title>'."\n";
  9229. $xmp .= "\t\t\t".'<dc:creator>'."\n";
  9230. $xmp .= "\t\t\t\t".'<rdf:Seq>'."\n";
  9231. $xmp .= "\t\t\t\t\t".'<rdf:li>'.TCPDF_STATIC::_escapeXML($this->author).'</rdf:li>'."\n";
  9232. $xmp .= "\t\t\t\t".'</rdf:Seq>'."\n";
  9233. $xmp .= "\t\t\t".'</dc:creator>'."\n";
  9234. $xmp .= "\t\t\t".'<dc:description>'."\n";
  9235. $xmp .= "\t\t\t\t".'<rdf:Alt>'."\n";
  9236. $xmp .= "\t\t\t\t\t".'<rdf:li xml:lang="x-default">'.TCPDF_STATIC::_escapeXML($this->subject).'</rdf:li>'."\n";
  9237. $xmp .= "\t\t\t\t".'</rdf:Alt>'."\n";
  9238. $xmp .= "\t\t\t".'</dc:description>'."\n";
  9239. $xmp .= "\t\t\t".'<dc:subject>'."\n";
  9240. $xmp .= "\t\t\t\t".'<rdf:Bag>'."\n";
  9241. $xmp .= "\t\t\t\t\t".'<rdf:li>'.TCPDF_STATIC::_escapeXML($this->keywords).'</rdf:li>'."\n";
  9242. $xmp .= "\t\t\t\t".'</rdf:Bag>'."\n";
  9243. $xmp .= "\t\t\t".'</dc:subject>'."\n";
  9244. $xmp .= "\t\t".'</rdf:Description>'."\n";
  9245. // convert doc creation date format
  9246. $dcdate = TCPDF_STATIC::getFormattedDate($this->doc_creation_timestamp);
  9247. $doccreationdate = substr($dcdate, 0, 4).'-'.substr($dcdate, 4, 2).'-'.substr($dcdate, 6, 2);
  9248. $doccreationdate .= 'T'.substr($dcdate, 8, 2).':'.substr($dcdate, 10, 2).':'.substr($dcdate, 12, 2);
  9249. $doccreationdate .= substr($dcdate, 14, 3).':'.substr($dcdate, 18, 2);
  9250. $doccreationdate = TCPDF_STATIC::_escapeXML($doccreationdate);
  9251. // convert doc modification date format
  9252. $dmdate = TCPDF_STATIC::getFormattedDate($this->doc_modification_timestamp);
  9253. $docmoddate = substr($dmdate, 0, 4).'-'.substr($dmdate, 4, 2).'-'.substr($dmdate, 6, 2);
  9254. $docmoddate .= 'T'.substr($dmdate, 8, 2).':'.substr($dmdate, 10, 2).':'.substr($dmdate, 12, 2);
  9255. $docmoddate .= substr($dmdate, 14, 3).':'.substr($dmdate, 18, 2);
  9256. $docmoddate = TCPDF_STATIC::_escapeXML($docmoddate);
  9257. $xmp .= "\t\t".'<rdf:Description rdf:about="" xmlns:xmp="http://ns.adobe.com/xap/1.0/">'."\n";
  9258. $xmp .= "\t\t\t".'<xmp:CreateDate>'.$doccreationdate.'</xmp:CreateDate>'."\n";
  9259. $xmp .= "\t\t\t".'<xmp:CreatorTool>'.$this->creator.'</xmp:CreatorTool>'."\n";
  9260. $xmp .= "\t\t\t".'<xmp:ModifyDate>'.$docmoddate.'</xmp:ModifyDate>'."\n";
  9261. $xmp .= "\t\t\t".'<xmp:MetadataDate>'.$doccreationdate.'</xmp:MetadataDate>'."\n";
  9262. $xmp .= "\t\t".'</rdf:Description>'."\n";
  9263. $xmp .= "\t\t".'<rdf:Description rdf:about="" xmlns:pdf="http://ns.adobe.com/pdf/1.3/">'."\n";
  9264. $xmp .= "\t\t\t".'<pdf:Keywords>'.TCPDF_STATIC::_escapeXML($this->keywords).'</pdf:Keywords>'."\n";
  9265. $xmp .= "\t\t\t".'<pdf:Producer>'.TCPDF_STATIC::_escapeXML(TCPDF_STATIC::getTCPDFProducer()).'</pdf:Producer>'."\n";
  9266. $xmp .= "\t\t".'</rdf:Description>'."\n";
  9267. $xmp .= "\t\t".'<rdf:Description rdf:about="" xmlns:xmpMM="http://ns.adobe.com/xap/1.0/mm/">'."\n";
  9268. $uuid = 'uuid:'.substr($this->file_id, 0, 8).'-'.substr($this->file_id, 8, 4).'-'.substr($this->file_id, 12, 4).'-'.substr($this->file_id, 16, 4).'-'.substr($this->file_id, 20, 12);
  9269. $xmp .= "\t\t\t".'<xmpMM:DocumentID>'.$uuid.'</xmpMM:DocumentID>'."\n";
  9270. $xmp .= "\t\t\t".'<xmpMM:InstanceID>'.$uuid.'</xmpMM:InstanceID>'."\n";
  9271. $xmp .= "\t\t".'</rdf:Description>'."\n";
  9272. if ($this->pdfa_mode) {
  9273. $xmp .= "\t\t".'<rdf:Description rdf:about="" xmlns:pdfaid="http://www.aiim.org/pdfa/ns/id/">'."\n";
  9274. $xmp .= "\t\t\t".'<pdfaid:part>'.$this->pdfa_version.'</pdfaid:part>'."\n";
  9275. $xmp .= "\t\t\t".'<pdfaid:conformance>B</pdfaid:conformance>'."\n";
  9276. $xmp .= "\t\t".'</rdf:Description>'."\n";
  9277. }
  9278. // XMP extension schemas
  9279. $xmp .= "\t\t".'<rdf:Description rdf:about="" xmlns:pdfaExtension="http://www.aiim.org/pdfa/ns/extension/" xmlns:pdfaSchema="http://www.aiim.org/pdfa/ns/schema#" xmlns:pdfaProperty="http://www.aiim.org/pdfa/ns/property#">'."\n";
  9280. $xmp .= "\t\t\t".'<pdfaExtension:schemas>'."\n";
  9281. $xmp .= "\t\t\t\t".'<rdf:Bag>'."\n";
  9282. $xmp .= "\t\t\t\t\t".'<rdf:li rdf:parseType="Resource">'."\n";
  9283. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:namespaceURI>http://ns.adobe.com/pdf/1.3/</pdfaSchema:namespaceURI>'."\n";
  9284. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:prefix>pdf</pdfaSchema:prefix>'."\n";
  9285. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:schema>Adobe PDF Schema</pdfaSchema:schema>'."\n";
  9286. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:property>'."\n";
  9287. $xmp .= "\t\t\t\t\t\t\t".'<rdf:Seq>'."\n";
  9288. $xmp .= "\t\t\t\t\t\t\t\t".'<rdf:li rdf:parseType="Resource">'."\n";
  9289. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:category>internal</pdfaProperty:category>'."\n";
  9290. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:description>Adobe PDF Schema</pdfaProperty:description>'."\n";
  9291. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:name>InstanceID</pdfaProperty:name>'."\n";
  9292. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:valueType>URI</pdfaProperty:valueType>'."\n";
  9293. $xmp .= "\t\t\t\t\t\t\t\t".'</rdf:li>'."\n";
  9294. $xmp .= "\t\t\t\t\t\t\t".'</rdf:Seq>'."\n";
  9295. $xmp .= "\t\t\t\t\t\t".'</pdfaSchema:property>'."\n";
  9296. $xmp .= "\t\t\t\t\t".'</rdf:li>'."\n";
  9297. $xmp .= "\t\t\t\t\t".'<rdf:li rdf:parseType="Resource">'."\n";
  9298. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:namespaceURI>http://ns.adobe.com/xap/1.0/mm/</pdfaSchema:namespaceURI>'."\n";
  9299. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:prefix>xmpMM</pdfaSchema:prefix>'."\n";
  9300. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:schema>XMP Media Management Schema</pdfaSchema:schema>'."\n";
  9301. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:property>'."\n";
  9302. $xmp .= "\t\t\t\t\t\t\t".'<rdf:Seq>'."\n";
  9303. $xmp .= "\t\t\t\t\t\t\t\t".'<rdf:li rdf:parseType="Resource">'."\n";
  9304. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:category>internal</pdfaProperty:category>'."\n";
  9305. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:description>UUID based identifier for specific incarnation of a document</pdfaProperty:description>'."\n";
  9306. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:name>InstanceID</pdfaProperty:name>'."\n";
  9307. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:valueType>URI</pdfaProperty:valueType>'."\n";
  9308. $xmp .= "\t\t\t\t\t\t\t\t".'</rdf:li>'."\n";
  9309. $xmp .= "\t\t\t\t\t\t\t".'</rdf:Seq>'."\n";
  9310. $xmp .= "\t\t\t\t\t\t".'</pdfaSchema:property>'."\n";
  9311. $xmp .= "\t\t\t\t\t".'</rdf:li>'."\n";
  9312. $xmp .= "\t\t\t\t\t".'<rdf:li rdf:parseType="Resource">'."\n";
  9313. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:namespaceURI>http://www.aiim.org/pdfa/ns/id/</pdfaSchema:namespaceURI>'."\n";
  9314. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:prefix>pdfaid</pdfaSchema:prefix>'."\n";
  9315. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:schema>PDF/A ID Schema</pdfaSchema:schema>'."\n";
  9316. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:property>'."\n";
  9317. $xmp .= "\t\t\t\t\t\t\t".'<rdf:Seq>'."\n";
  9318. $xmp .= "\t\t\t\t\t\t\t\t".'<rdf:li rdf:parseType="Resource">'."\n";
  9319. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:category>internal</pdfaProperty:category>'."\n";
  9320. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:description>Part of PDF/A standard</pdfaProperty:description>'."\n";
  9321. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:name>part</pdfaProperty:name>'."\n";
  9322. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:valueType>Integer</pdfaProperty:valueType>'."\n";
  9323. $xmp .= "\t\t\t\t\t\t\t\t".'</rdf:li>'."\n";
  9324. $xmp .= "\t\t\t\t\t\t\t\t".'<rdf:li rdf:parseType="Resource">'."\n";
  9325. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:category>internal</pdfaProperty:category>'."\n";
  9326. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:description>Amendment of PDF/A standard</pdfaProperty:description>'."\n";
  9327. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:name>amd</pdfaProperty:name>'."\n";
  9328. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:valueType>Text</pdfaProperty:valueType>'."\n";
  9329. $xmp .= "\t\t\t\t\t\t\t\t".'</rdf:li>'."\n";
  9330. $xmp .= "\t\t\t\t\t\t\t\t".'<rdf:li rdf:parseType="Resource">'."\n";
  9331. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:category>internal</pdfaProperty:category>'."\n";
  9332. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:description>Conformance level of PDF/A standard</pdfaProperty:description>'."\n";
  9333. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:name>conformance</pdfaProperty:name>'."\n";
  9334. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:valueType>Text</pdfaProperty:valueType>'."\n";
  9335. $xmp .= "\t\t\t\t\t\t\t\t".'</rdf:li>'."\n";
  9336. $xmp .= "\t\t\t\t\t\t\t".'</rdf:Seq>'."\n";
  9337. $xmp .= "\t\t\t\t\t\t".'</pdfaSchema:property>'."\n";
  9338. $xmp .= "\t\t\t\t\t".'</rdf:li>'."\n";
  9339. $xmp .= "\t\t\t\t".'</rdf:Bag>'."\n";
  9340. $xmp .= "\t\t\t".'</pdfaExtension:schemas>'."\n";
  9341. $xmp .= "\t\t".'</rdf:Description>'."\n";
  9342. $xmp .= $this->custom_xmp_rdf;
  9343. $xmp .= "\t".'</rdf:RDF>'."\n";
  9344. $xmp .= $this->custom_xmp;
  9345. $xmp .= '</x:xmpmeta>'."\n";
  9346. $xmp .= '<?xpacket end="w"?>';
  9347. $out = '<< /Type /Metadata /Subtype /XML /Length '.strlen($xmp).' >> stream'."\n".$xmp."\n".'endstream'."\n".'endobj';
  9348. // restore previous isunicode value
  9349. $this->isunicode = $prev_isunicode;
  9350. $this->encrypted = $prev_encrypted;
  9351. $this->_out($out);
  9352. return $oid;
  9353. }
  9354. /**
  9355. * Output Catalog.
  9356. * @return int object id
  9357. * @protected
  9358. */
  9359. protected function _putcatalog() {
  9360. // put XMP
  9361. $xmpobj = $this->_putXMP();
  9362. // if required, add standard sRGB ICC colour profile
  9363. if ($this->pdfa_mode OR $this->force_srgb) {
  9364. $iccobj = $this->_newobj();
  9365. $icc = file_get_contents(dirname(__FILE__).'/include/sRGB.icc');
  9366. $filter = '';
  9367. if ($this->compress) {
  9368. $filter = ' /Filter /FlateDecode';
  9369. $icc = gzcompress($icc);
  9370. }
  9371. $icc = $this->_getrawstream($icc);
  9372. $this->_out('<</N 3 '.$filter.'/Length '.strlen($icc).'>> stream'."\n".$icc."\n".'endstream'."\n".'endobj');
  9373. }
  9374. // start catalog
  9375. $oid = $this->_newobj();
  9376. $out = '<< /Type /Catalog';
  9377. $out .= ' /Version /'.$this->PDFVersion;
  9378. //$out .= ' /Extensions <<>>';
  9379. $out .= ' /Pages 1 0 R';
  9380. //$out .= ' /PageLabels ' //...;
  9381. $out .= ' /Names <<';
  9382. if ((!$this->pdfa_mode) AND !empty($this->n_js)) {
  9383. $out .= ' /JavaScript '.$this->n_js;
  9384. }
  9385. if (!empty($this->efnames)) {
  9386. $out .= ' /EmbeddedFiles <</Names [';
  9387. foreach ($this->efnames AS $fn => $fref) {
  9388. $out .= ' '.$this->_datastring($fn).' '.$fref;
  9389. }
  9390. $out .= ' ]>>';
  9391. }
  9392. $out .= ' >>';
  9393. if (!empty($this->dests)) {
  9394. $out .= ' /Dests '.($this->n_dests).' 0 R';
  9395. }
  9396. $out .= $this->_putviewerpreferences();
  9397. if (isset($this->LayoutMode) AND (!TCPDF_STATIC::empty_string($this->LayoutMode))) {
  9398. $out .= ' /PageLayout /'.$this->LayoutMode;
  9399. }
  9400. if (isset($this->PageMode) AND (!TCPDF_STATIC::empty_string($this->PageMode))) {
  9401. $out .= ' /PageMode /'.$this->PageMode;
  9402. }
  9403. if (count($this->outlines) > 0) {
  9404. $out .= ' /Outlines '.$this->OutlineRoot.' 0 R';
  9405. $out .= ' /PageMode /UseOutlines';
  9406. }
  9407. //$out .= ' /Threads []';
  9408. if ($this->ZoomMode == 'fullpage') {
  9409. $out .= ' /OpenAction ['.$this->page_obj_id[1].' 0 R /Fit]';
  9410. } elseif ($this->ZoomMode == 'fullwidth') {
  9411. $out .= ' /OpenAction ['.$this->page_obj_id[1].' 0 R /FitH null]';
  9412. } elseif ($this->ZoomMode == 'real') {
  9413. $out .= ' /OpenAction ['.$this->page_obj_id[1].' 0 R /XYZ null null 1]';
  9414. } elseif (!is_string($this->ZoomMode)) {
  9415. $out .= sprintf(' /OpenAction ['.$this->page_obj_id[1].' 0 R /XYZ null null %F]', ($this->ZoomMode / 100));
  9416. }
  9417. //$out .= ' /AA <<>>';
  9418. //$out .= ' /URI <<>>';
  9419. $out .= ' /Metadata '.$xmpobj.' 0 R';
  9420. //$out .= ' /StructTreeRoot <<>>';
  9421. //$out .= ' /MarkInfo <<>>';
  9422. if (isset($this->l['a_meta_language'])) {
  9423. $out .= ' /Lang '.$this->_textstring($this->l['a_meta_language'], $oid);
  9424. }
  9425. //$out .= ' /SpiderInfo <<>>';
  9426. // set OutputIntent to sRGB IEC61966-2.1 if required
  9427. if ($this->pdfa_mode OR $this->force_srgb) {
  9428. $out .= ' /OutputIntents [<<';
  9429. $out .= ' /Type /OutputIntent';
  9430. $out .= ' /S /GTS_PDFA1';
  9431. $out .= ' /OutputCondition '.$this->_textstring('sRGB IEC61966-2.1', $oid);
  9432. $out .= ' /OutputConditionIdentifier '.$this->_textstring('sRGB IEC61966-2.1', $oid);
  9433. $out .= ' /RegistryName '.$this->_textstring('http://www.color.org', $oid);
  9434. $out .= ' /Info '.$this->_textstring('sRGB IEC61966-2.1', $oid);
  9435. $out .= ' /DestOutputProfile '.$iccobj.' 0 R';
  9436. $out .= ' >>]';
  9437. }
  9438. //$out .= ' /PieceInfo <<>>';
  9439. if (!empty($this->pdflayers)) {
  9440. $lyrobjs = '';
  9441. $lyrobjs_off = '';
  9442. $lyrobjs_lock = '';
  9443. foreach ($this->pdflayers as $layer) {
  9444. $layer_obj_ref = ' '.$layer['objid'].' 0 R';
  9445. $lyrobjs .= $layer_obj_ref;
  9446. if ($layer['view'] === false) {
  9447. $lyrobjs_off .= $layer_obj_ref;
  9448. }
  9449. if ($layer['lock']) {
  9450. $lyrobjs_lock .= $layer_obj_ref;
  9451. }
  9452. }
  9453. $out .= ' /OCProperties << /OCGs ['.$lyrobjs.']';
  9454. $out .= ' /D <<';
  9455. $out .= ' /Name '.$this->_textstring('Layers', $oid);
  9456. $out .= ' /Creator '.$this->_textstring('TCPDF', $oid);
  9457. $out .= ' /BaseState /ON';
  9458. $out .= ' /OFF ['.$lyrobjs_off.']';
  9459. $out .= ' /Locked ['.$lyrobjs_lock.']';
  9460. $out .= ' /Intent /View';
  9461. $out .= ' /AS [';
  9462. $out .= ' << /Event /Print /OCGs ['.$lyrobjs.'] /Category [/Print] >>';
  9463. $out .= ' << /Event /View /OCGs ['.$lyrobjs.'] /Category [/View] >>';
  9464. $out .= ' ]';
  9465. $out .= ' /Order ['.$lyrobjs.']';
  9466. $out .= ' /ListMode /AllPages';
  9467. //$out .= ' /RBGroups ['..']';
  9468. //$out .= ' /Locked ['..']';
  9469. $out .= ' >>';
  9470. $out .= ' >>';
  9471. }
  9472. // AcroForm
  9473. if (!empty($this->form_obj_id)
  9474. OR ($this->sign AND isset($this->signature_data['cert_type']))
  9475. OR !empty($this->empty_signature_appearance)) {
  9476. $out .= ' /AcroForm <<';
  9477. $objrefs = '';
  9478. if ($this->sign AND isset($this->signature_data['cert_type'])) {
  9479. // set reference for signature object
  9480. $objrefs .= $this->sig_obj_id.' 0 R';
  9481. }
  9482. if (!empty($this->empty_signature_appearance)) {
  9483. foreach ($this->empty_signature_appearance as $esa) {
  9484. // set reference for empty signature objects
  9485. $objrefs .= ' '.$esa['objid'].' 0 R';
  9486. }
  9487. }
  9488. if (!empty($this->form_obj_id)) {
  9489. foreach($this->form_obj_id as $objid) {
  9490. $objrefs .= ' '.$objid.' 0 R';
  9491. }
  9492. }
  9493. $out .= ' /Fields ['.$objrefs.']';
  9494. // It's better to turn off this value and set the appearance stream for each annotation (/AP) to avoid conflicts with signature fields.
  9495. if (empty($this->signature_data['approval']) OR ($this->signature_data['approval'] != 'A')) {
  9496. $out .= ' /NeedAppearances false';
  9497. }
  9498. if ($this->sign AND isset($this->signature_data['cert_type'])) {
  9499. if ($this->signature_data['cert_type'] > 0) {
  9500. $out .= ' /SigFlags 3';
  9501. } else {
  9502. $out .= ' /SigFlags 1';
  9503. }
  9504. }
  9505. //$out .= ' /CO ';
  9506. if (isset($this->annotation_fonts) AND !empty($this->annotation_fonts)) {
  9507. $out .= ' /DR <<';
  9508. $out .= ' /Font <<';
  9509. foreach ($this->annotation_fonts as $fontkey => $fontid) {
  9510. $out .= ' /F'.$fontid.' '.$this->font_obj_ids[$fontkey].' 0 R';
  9511. }
  9512. $out .= ' >> >>';
  9513. }
  9514. $font = $this->getFontBuffer((($this->pdfa_mode) ? 'pdfa' : '') .'helvetica');
  9515. $out .= ' /DA ' . $this->_datastring('/F'.$font['i'].' 0 Tf 0 g');
  9516. $out .= ' /Q '.(($this->rtl)?'2':'0');
  9517. //$out .= ' /XFA ';
  9518. $out .= ' >>';
  9519. // signatures
  9520. if ($this->sign AND isset($this->signature_data['cert_type'])
  9521. AND (empty($this->signature_data['approval']) OR ($this->signature_data['approval'] != 'A'))) {
  9522. if ($this->signature_data['cert_type'] > 0) {
  9523. $out .= ' /Perms << /DocMDP '.($this->sig_obj_id + 1).' 0 R >>';
  9524. } else {
  9525. $out .= ' /Perms << /UR3 '.($this->sig_obj_id + 1).' 0 R >>';
  9526. }
  9527. }
  9528. }
  9529. //$out .= ' /Legal <<>>';
  9530. //$out .= ' /Requirements []';
  9531. //$out .= ' /Collection <<>>';
  9532. //$out .= ' /NeedsRendering true';
  9533. $out .= ' >>';
  9534. $out .= "\n".'endobj';
  9535. $this->_out($out);
  9536. return $oid;
  9537. }
  9538. /**
  9539. * Output viewer preferences.
  9540. * @return string for viewer preferences
  9541. * @author Nicola asuni
  9542. * @since 3.1.000 (2008-06-09)
  9543. * @protected
  9544. */
  9545. protected function _putviewerpreferences() {
  9546. $vp = $this->viewer_preferences;
  9547. $out = ' /ViewerPreferences <<';
  9548. if ($this->rtl) {
  9549. $out .= ' /Direction /R2L';
  9550. } else {
  9551. $out .= ' /Direction /L2R';
  9552. }
  9553. if (isset($vp['HideToolbar']) AND ($vp['HideToolbar'])) {
  9554. $out .= ' /HideToolbar true';
  9555. }
  9556. if (isset($vp['HideMenubar']) AND ($vp['HideMenubar'])) {
  9557. $out .= ' /HideMenubar true';
  9558. }
  9559. if (isset($vp['HideWindowUI']) AND ($vp['HideWindowUI'])) {
  9560. $out .= ' /HideWindowUI true';
  9561. }
  9562. if (isset($vp['FitWindow']) AND ($vp['FitWindow'])) {
  9563. $out .= ' /FitWindow true';
  9564. }
  9565. if (isset($vp['CenterWindow']) AND ($vp['CenterWindow'])) {
  9566. $out .= ' /CenterWindow true';
  9567. }
  9568. if (isset($vp['DisplayDocTitle']) AND ($vp['DisplayDocTitle'])) {
  9569. $out .= ' /DisplayDocTitle true';
  9570. }
  9571. if (isset($vp['NonFullScreenPageMode'])) {
  9572. $out .= ' /NonFullScreenPageMode /'.$vp['NonFullScreenPageMode'];
  9573. }
  9574. if (isset($vp['ViewArea'])) {
  9575. $out .= ' /ViewArea /'.$vp['ViewArea'];
  9576. }
  9577. if (isset($vp['ViewClip'])) {
  9578. $out .= ' /ViewClip /'.$vp['ViewClip'];
  9579. }
  9580. if (isset($vp['PrintArea'])) {
  9581. $out .= ' /PrintArea /'.$vp['PrintArea'];
  9582. }
  9583. if (isset($vp['PrintClip'])) {
  9584. $out .= ' /PrintClip /'.$vp['PrintClip'];
  9585. }
  9586. if (isset($vp['PrintScaling'])) {
  9587. $out .= ' /PrintScaling /'.$vp['PrintScaling'];
  9588. }
  9589. if (isset($vp['Duplex']) AND (!TCPDF_STATIC::empty_string($vp['Duplex']))) {
  9590. $out .= ' /Duplex /'.$vp['Duplex'];
  9591. }
  9592. if (isset($vp['PickTrayByPDFSize'])) {
  9593. if ($vp['PickTrayByPDFSize']) {
  9594. $out .= ' /PickTrayByPDFSize true';
  9595. } else {
  9596. $out .= ' /PickTrayByPDFSize false';
  9597. }
  9598. }
  9599. if (isset($vp['PrintPageRange'])) {
  9600. $PrintPageRangeNum = '';
  9601. foreach ($vp['PrintPageRange'] as $k => $v) {
  9602. $PrintPageRangeNum .= ' '.($v - 1).'';
  9603. }
  9604. $out .= ' /PrintPageRange ['.substr($PrintPageRangeNum,1).']';
  9605. }
  9606. if (isset($vp['NumCopies'])) {
  9607. $out .= ' /NumCopies '.intval($vp['NumCopies']);
  9608. }
  9609. $out .= ' >>';
  9610. return $out;
  9611. }
  9612. /**
  9613. * Output PDF File Header (7.5.2).
  9614. * @protected
  9615. */
  9616. protected function _putheader() {
  9617. $this->_out('%PDF-'.$this->PDFVersion);
  9618. $this->_out('%'.chr(0xe2).chr(0xe3).chr(0xcf).chr(0xd3));
  9619. }
  9620. /**
  9621. * Output end of document (EOF).
  9622. * @protected
  9623. */
  9624. protected function _enddoc() {
  9625. if (isset($this->CurrentFont['fontkey']) AND isset($this->CurrentFont['subsetchars'])) {
  9626. // save subset chars of the previous font
  9627. $this->setFontSubBuffer($this->CurrentFont['fontkey'], 'subsetchars', $this->CurrentFont['subsetchars']);
  9628. }
  9629. $this->state = 1;
  9630. $this->_putheader();
  9631. $this->_putpages();
  9632. $this->_putresources();
  9633. // empty signature fields
  9634. if (!empty($this->empty_signature_appearance)) {
  9635. foreach ($this->empty_signature_appearance as $key => $esa) {
  9636. // widget annotation for empty signature
  9637. $out = $this->_getobj($esa['objid'])."\n";
  9638. $out .= '<< /Type /Annot';
  9639. $out .= ' /Subtype /Widget';
  9640. $out .= ' /Rect ['.$esa['rect'].']';
  9641. $out .= ' /P '.$this->page_obj_id[($esa['page'])].' 0 R'; // link to signature appearance page
  9642. $out .= ' /F 4';
  9643. $out .= ' /FT /Sig';
  9644. $signame = $esa['name'].sprintf(' [%03d]', ($key + 1));
  9645. $out .= ' /T '.$this->_textstring($signame, $esa['objid']);
  9646. $out .= ' /Ff 0';
  9647. $out .= ' >>';
  9648. $out .= "\n".'endobj';
  9649. $this->_out($out);
  9650. }
  9651. }
  9652. // Signature
  9653. if ($this->sign AND isset($this->signature_data['cert_type'])) {
  9654. // widget annotation for signature
  9655. $out = $this->_getobj($this->sig_obj_id)."\n";
  9656. $out .= '<< /Type /Annot';
  9657. $out .= ' /Subtype /Widget';
  9658. $out .= ' /Rect ['.$this->signature_appearance['rect'].']';
  9659. $out .= ' /P '.$this->page_obj_id[($this->signature_appearance['page'])].' 0 R'; // link to signature appearance page
  9660. $out .= ' /F 4';
  9661. $out .= ' /FT /Sig';
  9662. $out .= ' /T '.$this->_textstring($this->signature_appearance['name'], $this->sig_obj_id);
  9663. $out .= ' /Ff 0';
  9664. $out .= ' /V '.($this->sig_obj_id + 1).' 0 R';
  9665. $out .= ' >>';
  9666. $out .= "\n".'endobj';
  9667. $this->_out($out);
  9668. // signature
  9669. $this->_putsignature();
  9670. }
  9671. // Info
  9672. $objid_info = $this->_putinfo();
  9673. // Catalog
  9674. $objid_catalog = $this->_putcatalog();
  9675. // Cross-ref
  9676. $o = $this->bufferlen;
  9677. // XREF section
  9678. $this->_out('xref');
  9679. $this->_out('0 '.($this->n + 1));
  9680. $this->_out('0000000000 65535 f ');
  9681. $freegen = ($this->n + 2);
  9682. for ($i=1; $i <= $this->n; ++$i) {
  9683. if (!isset($this->offsets[$i]) AND ($i > 1)) {
  9684. $this->_out(sprintf('0000000000 %05d f ', $freegen));
  9685. ++$freegen;
  9686. } else {
  9687. $this->_out(sprintf('%010d 00000 n ', $this->offsets[$i]));
  9688. }
  9689. }
  9690. // TRAILER
  9691. $out = 'trailer'."\n";
  9692. $out .= '<<';
  9693. $out .= ' /Size '.($this->n + 1);
  9694. $out .= ' /Root '.$objid_catalog.' 0 R';
  9695. $out .= ' /Info '.$objid_info.' 0 R';
  9696. if ($this->encrypted) {
  9697. $out .= ' /Encrypt '.$this->encryptdata['objid'].' 0 R';
  9698. }
  9699. $out .= ' /ID [ <'.$this->file_id.'> <'.$this->file_id.'> ]';
  9700. $out .= ' >>';
  9701. $this->_out($out);
  9702. $this->_out('startxref');
  9703. $this->_out($o);
  9704. $this->_out('%%EOF');
  9705. $this->state = 3; // end-of-doc
  9706. }
  9707. /**
  9708. * Initialize a new page.
  9709. * @param string $orientation page orientation. Possible values are (case insensitive):<ul><li>P or PORTRAIT (default)</li><li>L or LANDSCAPE</li></ul>
  9710. * @param mixed $format The format used for pages. It can be either: one of the string values specified at getPageSizeFromFormat() or an array of parameters specified at setPageFormat().
  9711. * @protected
  9712. * @see getPageSizeFromFormat(), setPageFormat()
  9713. */
  9714. protected function _beginpage($orientation='', $format='') {
  9715. ++$this->page;
  9716. $this->pageobjects[$this->page] = array();
  9717. $this->setPageBuffer($this->page, '');
  9718. // initialize array for graphics tranformation positions inside a page buffer
  9719. $this->transfmrk[$this->page] = array();
  9720. $this->state = 2;
  9721. if (TCPDF_STATIC::empty_string($orientation)) {
  9722. if (isset($this->CurOrientation)) {
  9723. $orientation = $this->CurOrientation;
  9724. } elseif ($this->fwPt > $this->fhPt) {
  9725. // landscape
  9726. $orientation = 'L';
  9727. } else {
  9728. // portrait
  9729. $orientation = 'P';
  9730. }
  9731. }
  9732. if (TCPDF_STATIC::empty_string($format)) {
  9733. $this->pagedim[$this->page] = $this->pagedim[($this->page - 1)];
  9734. $this->setPageOrientation($orientation);
  9735. } else {
  9736. $this->setPageFormat($format, $orientation);
  9737. }
  9738. if ($this->rtl) {
  9739. $this->x = $this->w - $this->rMargin;
  9740. } else {
  9741. $this->x = $this->lMargin;
  9742. }
  9743. $this->y = $this->tMargin;
  9744. if (isset($this->newpagegroup[$this->page])) {
  9745. // start a new group
  9746. $this->currpagegroup = $this->newpagegroup[$this->page];
  9747. $this->pagegroups[$this->currpagegroup] = 1;
  9748. } elseif (isset($this->currpagegroup) AND ($this->currpagegroup > 0)) {
  9749. ++$this->pagegroups[$this->currpagegroup];
  9750. }
  9751. }
  9752. /**
  9753. * Mark end of page.
  9754. * @protected
  9755. */
  9756. protected function _endpage() {
  9757. $this->setVisibility('all');
  9758. $this->state = 1;
  9759. }
  9760. /**
  9761. * Begin a new object and return the object number.
  9762. * @return int object number
  9763. * @protected
  9764. */
  9765. protected function _newobj() {
  9766. $this->_out($this->_getobj());
  9767. return $this->n;
  9768. }
  9769. /**
  9770. * Return the starting object string for the selected object ID.
  9771. * @param int|null $objid Object ID (leave empty to get a new ID).
  9772. * @return string the starting object string
  9773. * @protected
  9774. * @since 5.8.009 (2010-08-20)
  9775. */
  9776. protected function _getobj($objid=null) {
  9777. if (TCPDF_STATIC::empty_string($objid)) {
  9778. ++$this->n;
  9779. $objid = $this->n;
  9780. }
  9781. $this->offsets[$objid] = $this->bufferlen;
  9782. $this->pageobjects[$this->page][] = $objid;
  9783. return $objid.' 0 obj';
  9784. }
  9785. /**
  9786. * Underline text.
  9787. * @param int $x X coordinate
  9788. * @param int $y Y coordinate
  9789. * @param string $txt text to underline
  9790. * @protected
  9791. */
  9792. protected function _dounderline($x, $y, $txt) {
  9793. $w = $this->GetStringWidth($txt);
  9794. return $this->_dounderlinew($x, $y, $w);
  9795. }
  9796. /**
  9797. * Underline for rectangular text area.
  9798. * @param int $x X coordinate
  9799. * @param int $y Y coordinate
  9800. * @param int $w width to underline
  9801. * @protected
  9802. * @since 4.8.008 (2009-09-29)
  9803. */
  9804. protected function _dounderlinew($x, $y, $w) {
  9805. $linew = - $this->CurrentFont['ut'] / 1000 * $this->FontSizePt;
  9806. return sprintf('%F %F %F %F re f', $x * $this->k, ((($this->h - $y) * $this->k) + $linew), $w * $this->k, $linew);
  9807. }
  9808. /**
  9809. * Line through text.
  9810. * @param int $x X coordinate
  9811. * @param int $y Y coordinate
  9812. * @param string $txt text to linethrough
  9813. * @protected
  9814. */
  9815. protected function _dolinethrough($x, $y, $txt) {
  9816. $w = $this->GetStringWidth($txt);
  9817. return $this->_dolinethroughw($x, $y, $w);
  9818. }
  9819. /**
  9820. * Line through for rectangular text area.
  9821. * @param int $x X coordinate
  9822. * @param int $y Y coordinate
  9823. * @param int $w line length (width)
  9824. * @protected
  9825. * @since 4.9.008 (2009-09-29)
  9826. */
  9827. protected function _dolinethroughw($x, $y, $w) {
  9828. $linew = - $this->CurrentFont['ut'] / 1000 * $this->FontSizePt;
  9829. return sprintf('%F %F %F %F re f', $x * $this->k, ((($this->h - $y) * $this->k) + $linew + ($this->FontSizePt / 3)), $w * $this->k, $linew);
  9830. }
  9831. /**
  9832. * Overline text.
  9833. * @param int $x X coordinate
  9834. * @param int $y Y coordinate
  9835. * @param string $txt text to overline
  9836. * @protected
  9837. * @since 4.9.015 (2010-04-19)
  9838. */
  9839. protected function _dooverline($x, $y, $txt) {
  9840. $w = $this->GetStringWidth($txt);
  9841. return $this->_dooverlinew($x, $y, $w);
  9842. }
  9843. /**
  9844. * Overline for rectangular text area.
  9845. * @param int $x X coordinate
  9846. * @param int $y Y coordinate
  9847. * @param int $w width to overline
  9848. * @protected
  9849. * @since 4.9.015 (2010-04-19)
  9850. */
  9851. protected function _dooverlinew($x, $y, $w) {
  9852. $linew = - $this->CurrentFont['ut'] / 1000 * $this->FontSizePt;
  9853. return sprintf('%F %F %F %F re f', $x * $this->k, (($this->h - $y + $this->FontAscent) * $this->k) - $linew, $w * $this->k, $linew);
  9854. }
  9855. /**
  9856. * Format a data string for meta information
  9857. * @param string $s data string to escape.
  9858. * @param int $n object ID
  9859. * @return string escaped string.
  9860. * @protected
  9861. */
  9862. protected function _datastring($s, $n=0) {
  9863. if ($n == 0) {
  9864. $n = $this->n;
  9865. }
  9866. $s = $this->_encrypt_data($n, $s);
  9867. return '('. TCPDF_STATIC::_escape($s).')';
  9868. }
  9869. /**
  9870. * Set the document creation timestamp
  9871. * @param mixed $time Document creation timestamp in seconds or date-time string.
  9872. * @public
  9873. * @since 5.9.152 (2012-03-23)
  9874. */
  9875. public function setDocCreationTimestamp($time) {
  9876. if (is_string($time)) {
  9877. $time = TCPDF_STATIC::getTimestamp($time);
  9878. }
  9879. $this->doc_creation_timestamp = intval($time);
  9880. }
  9881. /**
  9882. * Set the document modification timestamp
  9883. * @param mixed $time Document modification timestamp in seconds or date-time string.
  9884. * @public
  9885. * @since 5.9.152 (2012-03-23)
  9886. */
  9887. public function setDocModificationTimestamp($time) {
  9888. if (is_string($time)) {
  9889. $time = TCPDF_STATIC::getTimestamp($time);
  9890. }
  9891. $this->doc_modification_timestamp = intval($time);
  9892. }
  9893. /**
  9894. * Returns document creation timestamp in seconds.
  9895. * @return int Creation timestamp in seconds.
  9896. * @public
  9897. * @since 5.9.152 (2012-03-23)
  9898. */
  9899. public function getDocCreationTimestamp() {
  9900. return $this->doc_creation_timestamp;
  9901. }
  9902. /**
  9903. * Returns document modification timestamp in seconds.
  9904. * @return int Modfication timestamp in seconds.
  9905. * @public
  9906. * @since 5.9.152 (2012-03-23)
  9907. */
  9908. public function getDocModificationTimestamp() {
  9909. return $this->doc_modification_timestamp;
  9910. }
  9911. /**
  9912. * Returns a formatted date for meta information
  9913. * @param int $n Object ID.
  9914. * @param int $timestamp Timestamp to convert.
  9915. * @return string escaped date string.
  9916. * @protected
  9917. * @since 4.6.028 (2009-08-25)
  9918. */
  9919. protected function _datestring($n=0, $timestamp=0) {
  9920. if ((empty($timestamp)) OR ($timestamp < 0)) {
  9921. $timestamp = $this->doc_creation_timestamp;
  9922. }
  9923. return $this->_datastring('D:'.TCPDF_STATIC::getFormattedDate($timestamp), $n);
  9924. }
  9925. /**
  9926. * Format a text string for meta information
  9927. * @param string $s string to escape.
  9928. * @param int $n object ID
  9929. * @return string escaped string.
  9930. * @protected
  9931. */
  9932. protected function _textstring($s, $n=0) {
  9933. if ($this->isunicode) {
  9934. //Convert string to UTF-16BE
  9935. $s = TCPDF_FONTS::UTF8ToUTF16BE($s, true, $this->isunicode, $this->CurrentFont);
  9936. }
  9937. return $this->_datastring($s, $n);
  9938. }
  9939. /**
  9940. * get raw output stream.
  9941. * @param string $s string to output.
  9942. * @param int $n object reference for encryption mode
  9943. * @protected
  9944. * @author Nicola Asuni
  9945. * @since 5.5.000 (2010-06-22)
  9946. */
  9947. protected function _getrawstream($s, $n=0) {
  9948. if ($n <= 0) {
  9949. // default to current object
  9950. $n = $this->n;
  9951. }
  9952. return $this->_encrypt_data($n, $s);
  9953. }
  9954. /**
  9955. * Output a string to the document.
  9956. * @param string $s string to output.
  9957. * @protected
  9958. */
  9959. protected function _out($s) {
  9960. if ($this->state == 2) {
  9961. if ($this->inxobj) {
  9962. // we are inside an XObject template
  9963. $this->xobjects[$this->xobjid]['outdata'] .= $s."\n";
  9964. } elseif ((!$this->InFooter) AND isset($this->footerlen[$this->page]) AND ($this->footerlen[$this->page] > 0)) {
  9965. // puts data before page footer
  9966. $pagebuff = $this->getPageBuffer($this->page);
  9967. $page = substr($pagebuff, 0, -$this->footerlen[$this->page]);
  9968. $footer = substr($pagebuff, -$this->footerlen[$this->page]);
  9969. $this->setPageBuffer($this->page, $page.$s."\n".$footer);
  9970. // update footer position
  9971. $this->footerpos[$this->page] += strlen($s."\n");
  9972. } else {
  9973. // set page data
  9974. $this->setPageBuffer($this->page, $s."\n", true);
  9975. }
  9976. } elseif ($this->state > 0) {
  9977. // set general data
  9978. $this->setBuffer($s."\n");
  9979. }
  9980. }
  9981. /**
  9982. * Set header font.
  9983. * @param array<int,string|float|null> $font Array describing the basic font parameters: (family, style, size).
  9984. * @phpstan-param array{0: string, 1: string, 2: float|null} $font
  9985. * @public
  9986. * @since 1.1
  9987. */
  9988. public function setHeaderFont($font) {
  9989. $this->header_font = $font;
  9990. }
  9991. /**
  9992. * Get header font.
  9993. * @return array<int,string|float|null> Array describing the basic font parameters: (family, style, size).
  9994. * @phpstan-return array{0: string, 1: string, 2: float|null}
  9995. * @public
  9996. * @since 4.0.012 (2008-07-24)
  9997. */
  9998. public function getHeaderFont() {
  9999. return $this->header_font;
  10000. }
  10001. /**
  10002. * Set footer font.
  10003. * @param array<int,string|float|null> $font Array describing the basic font parameters: (family, style, size).
  10004. * @phpstan-param array{0: string, 1: string, 2: float|null} $font
  10005. * @public
  10006. * @since 1.1
  10007. */
  10008. public function setFooterFont($font) {
  10009. $this->footer_font = $font;
  10010. }
  10011. /**
  10012. * Get Footer font.
  10013. * @return array<int,string|float|null> Array describing the basic font parameters: (family, style, size).
  10014. * @phpstan-return array{0: string, 1: string, 2: float|null} $font
  10015. * @public
  10016. * @since 4.0.012 (2008-07-24)
  10017. */
  10018. public function getFooterFont() {
  10019. return $this->footer_font;
  10020. }
  10021. /**
  10022. * Set language array.
  10023. * @param array $language
  10024. * @public
  10025. * @since 1.1
  10026. */
  10027. public function setLanguageArray($language) {
  10028. $this->l = $language;
  10029. if (isset($this->l['a_meta_dir'])) {
  10030. $this->rtl = $this->l['a_meta_dir']=='rtl' ? true : false;
  10031. } else {
  10032. $this->rtl = false;
  10033. }
  10034. }
  10035. /**
  10036. * Returns the PDF data.
  10037. * @public
  10038. */
  10039. public function getPDFData() {
  10040. if ($this->state < 3) {
  10041. $this->Close();
  10042. }
  10043. return $this->buffer;
  10044. }
  10045. /**
  10046. * Output anchor link.
  10047. * @param string $url link URL or internal link (i.e.: &lt;a href="#23,4.5"&gt;link to page 23 at 4.5 Y position&lt;/a&gt;)
  10048. * @param string $name link name
  10049. * @param boolean $fill Indicates if the cell background must be painted (true) or transparent (false).
  10050. * @param boolean $firstline if true prints only the first line and return the remaining string.
  10051. * @param array|null $color array of RGB text color
  10052. * @param string $style font style (U, D, B, I)
  10053. * @param boolean $firstblock if true the string is the starting of a line.
  10054. * @return int the number of cells used or the remaining text if $firstline = true;
  10055. * @public
  10056. */
  10057. public function addHtmlLink($url, $name, $fill=false, $firstline=false, $color=null, $style=-1, $firstblock=false) {
  10058. if (isset($url[1]) AND ($url[0] == '#') AND is_numeric($url[1])) {
  10059. // convert url to internal link
  10060. $lnkdata = explode(',', $url);
  10061. if (isset($lnkdata[0]) ) {
  10062. $page = substr($lnkdata[0], 1);
  10063. if (isset($lnkdata[1]) AND (strlen($lnkdata[1]) > 0)) {
  10064. $lnky = floatval($lnkdata[1]);
  10065. } else {
  10066. $lnky = 0;
  10067. }
  10068. $url = $this->AddLink();
  10069. $this->setLink($url, $lnky, $page);
  10070. }
  10071. }
  10072. // store current settings
  10073. $prevcolor = $this->fgcolor;
  10074. $prevstyle = $this->FontStyle;
  10075. if (empty($color)) {
  10076. $this->setTextColorArray($this->htmlLinkColorArray);
  10077. } else {
  10078. $this->setTextColorArray($color);
  10079. }
  10080. if ($style == -1) {
  10081. $this->setFont('', $this->FontStyle.$this->htmlLinkFontStyle);
  10082. } else {
  10083. $this->setFont('', $this->FontStyle.$style);
  10084. }
  10085. $ret = $this->Write($this->lasth, $name, $url, $fill, '', false, 0, $firstline, $firstblock, 0);
  10086. // restore settings
  10087. $this->setFont('', $prevstyle);
  10088. $this->setTextColorArray($prevcolor);
  10089. return $ret;
  10090. }
  10091. /**
  10092. * Converts pixels to User's Units.
  10093. * @param int $px pixels
  10094. * @return float value in user's unit
  10095. * @public
  10096. * @see setImageScale(), getImageScale()
  10097. */
  10098. public function pixelsToUnits($px) {
  10099. return ($px / ($this->imgscale * $this->k));
  10100. }
  10101. /**
  10102. * Reverse function for htmlentities.
  10103. * Convert entities in UTF-8.
  10104. * @param string $text_to_convert Text to convert.
  10105. * @return string converted text string
  10106. * @public
  10107. */
  10108. public function unhtmlentities($text_to_convert) {
  10109. return @html_entity_decode($text_to_convert, ENT_QUOTES, $this->encoding);
  10110. }
  10111. // ENCRYPTION METHODS ----------------------------------
  10112. /**
  10113. * Compute encryption key depending on object number where the encrypted data is stored.
  10114. * This is used for all strings and streams without crypt filter specifier.
  10115. * @param int $n object number
  10116. * @return int object key
  10117. * @protected
  10118. * @author Nicola Asuni
  10119. * @since 2.0.000 (2008-01-02)
  10120. */
  10121. protected function _objectkey($n) {
  10122. $objkey = $this->encryptdata['key'].pack('VXxx', $n);
  10123. if ($this->encryptdata['mode'] == 2) { // AES-128
  10124. // AES padding
  10125. $objkey .= "\x73\x41\x6C\x54"; // sAlT
  10126. }
  10127. $objkey = substr(TCPDF_STATIC::_md5_16($objkey), 0, (($this->encryptdata['Length'] / 8) + 5));
  10128. $objkey = substr($objkey, 0, 16);
  10129. return $objkey;
  10130. }
  10131. /**
  10132. * Encrypt the input string.
  10133. * @param int $n object number
  10134. * @param string $s data string to encrypt
  10135. * @return string encrypted string
  10136. * @protected
  10137. * @author Nicola Asuni
  10138. * @since 5.0.005 (2010-05-11)
  10139. */
  10140. protected function _encrypt_data($n, $s) {
  10141. if (!$this->encrypted) {
  10142. return $s;
  10143. }
  10144. switch ($this->encryptdata['mode']) {
  10145. case 0: // RC4-40
  10146. case 1: { // RC4-128
  10147. $s = TCPDF_STATIC::_RC4($this->_objectkey($n), $s, $this->last_enc_key, $this->last_enc_key_c);
  10148. break;
  10149. }
  10150. case 2: { // AES-128
  10151. $s = TCPDF_STATIC::_AES($this->_objectkey($n), $s);
  10152. break;
  10153. }
  10154. case 3: { // AES-256
  10155. $s = TCPDF_STATIC::_AES($this->encryptdata['key'], $s);
  10156. break;
  10157. }
  10158. }
  10159. return $s;
  10160. }
  10161. /**
  10162. * Put encryption on PDF document.
  10163. * @protected
  10164. * @author Nicola Asuni
  10165. * @since 2.0.000 (2008-01-02)
  10166. */
  10167. protected function _putencryption() {
  10168. if (!$this->encrypted) {
  10169. return;
  10170. }
  10171. $this->encryptdata['objid'] = $this->_newobj();
  10172. $out = '<<';
  10173. if (!isset($this->encryptdata['Filter']) OR empty($this->encryptdata['Filter'])) {
  10174. $this->encryptdata['Filter'] = 'Standard';
  10175. }
  10176. $out .= ' /Filter /'.$this->encryptdata['Filter'];
  10177. if (isset($this->encryptdata['SubFilter']) AND !empty($this->encryptdata['SubFilter'])) {
  10178. $out .= ' /SubFilter /'.$this->encryptdata['SubFilter'];
  10179. }
  10180. if (!isset($this->encryptdata['V']) OR empty($this->encryptdata['V'])) {
  10181. $this->encryptdata['V'] = 1;
  10182. }
  10183. // V is a code specifying the algorithm to be used in encrypting and decrypting the document
  10184. $out .= ' /V '.$this->encryptdata['V'];
  10185. if (isset($this->encryptdata['Length']) AND !empty($this->encryptdata['Length'])) {
  10186. // The length of the encryption key, in bits. The value shall be a multiple of 8, in the range 40 to 256
  10187. $out .= ' /Length '.$this->encryptdata['Length'];
  10188. } else {
  10189. $out .= ' /Length 40';
  10190. }
  10191. if ($this->encryptdata['V'] >= 4) {
  10192. if (!isset($this->encryptdata['StmF']) OR empty($this->encryptdata['StmF'])) {
  10193. $this->encryptdata['StmF'] = 'Identity';
  10194. }
  10195. if (!isset($this->encryptdata['StrF']) OR empty($this->encryptdata['StrF'])) {
  10196. // The name of the crypt filter that shall be used when decrypting all strings in the document.
  10197. $this->encryptdata['StrF'] = 'Identity';
  10198. }
  10199. // A dictionary whose keys shall be crypt filter names and whose values shall be the corresponding crypt filter dictionaries.
  10200. if (isset($this->encryptdata['CF']) AND !empty($this->encryptdata['CF'])) {
  10201. $out .= ' /CF <<';
  10202. $out .= ' /'.$this->encryptdata['StmF'].' <<';
  10203. $out .= ' /Type /CryptFilter';
  10204. if (isset($this->encryptdata['CF']['CFM']) AND !empty($this->encryptdata['CF']['CFM'])) {
  10205. // The method used
  10206. $out .= ' /CFM /'.$this->encryptdata['CF']['CFM'];
  10207. if ($this->encryptdata['pubkey']) {
  10208. $out .= ' /Recipients [';
  10209. foreach ($this->encryptdata['Recipients'] as $rec) {
  10210. $out .= ' <'.$rec.'>';
  10211. }
  10212. $out .= ' ]';
  10213. if (isset($this->encryptdata['CF']['EncryptMetadata']) AND (!$this->encryptdata['CF']['EncryptMetadata'])) {
  10214. $out .= ' /EncryptMetadata false';
  10215. } else {
  10216. $out .= ' /EncryptMetadata true';
  10217. }
  10218. }
  10219. } else {
  10220. $out .= ' /CFM /None';
  10221. }
  10222. if (isset($this->encryptdata['CF']['AuthEvent']) AND !empty($this->encryptdata['CF']['AuthEvent'])) {
  10223. // The event to be used to trigger the authorization that is required to access encryption keys used by this filter.
  10224. $out .= ' /AuthEvent /'.$this->encryptdata['CF']['AuthEvent'];
  10225. } else {
  10226. $out .= ' /AuthEvent /DocOpen';
  10227. }
  10228. if (isset($this->encryptdata['CF']['Length']) AND !empty($this->encryptdata['CF']['Length'])) {
  10229. // The bit length of the encryption key.
  10230. $out .= ' /Length '.$this->encryptdata['CF']['Length'];
  10231. }
  10232. $out .= ' >> >>';
  10233. }
  10234. // The name of the crypt filter that shall be used by default when decrypting streams.
  10235. $out .= ' /StmF /'.$this->encryptdata['StmF'];
  10236. // The name of the crypt filter that shall be used when decrypting all strings in the document.
  10237. $out .= ' /StrF /'.$this->encryptdata['StrF'];
  10238. if (isset($this->encryptdata['EFF']) AND !empty($this->encryptdata['EFF'])) {
  10239. // The name of the crypt filter that shall be used when encrypting embedded file streams that do not have their own crypt filter specifier.
  10240. $out .= ' /EFF /'.$this->encryptdata[''];
  10241. }
  10242. }
  10243. // Additional encryption dictionary entries for the standard security handler
  10244. if ($this->encryptdata['pubkey']) {
  10245. if (($this->encryptdata['V'] < 4) AND isset($this->encryptdata['Recipients']) AND !empty($this->encryptdata['Recipients'])) {
  10246. $out .= ' /Recipients [';
  10247. foreach ($this->encryptdata['Recipients'] as $rec) {
  10248. $out .= ' <'.$rec.'>';
  10249. }
  10250. $out .= ' ]';
  10251. }
  10252. } else {
  10253. $out .= ' /R';
  10254. if ($this->encryptdata['V'] == 5) { // AES-256
  10255. $out .= ' 5';
  10256. $out .= ' /OE ('.TCPDF_STATIC::_escape($this->encryptdata['OE']).')';
  10257. $out .= ' /UE ('.TCPDF_STATIC::_escape($this->encryptdata['UE']).')';
  10258. $out .= ' /Perms ('.TCPDF_STATIC::_escape($this->encryptdata['perms']).')';
  10259. } elseif ($this->encryptdata['V'] == 4) { // AES-128
  10260. $out .= ' 4';
  10261. } elseif ($this->encryptdata['V'] < 2) { // RC-40
  10262. $out .= ' 2';
  10263. } else { // RC-128
  10264. $out .= ' 3';
  10265. }
  10266. $out .= ' /O ('.TCPDF_STATIC::_escape($this->encryptdata['O']).')';
  10267. $out .= ' /U ('.TCPDF_STATIC::_escape($this->encryptdata['U']).')';
  10268. $out .= ' /P '.$this->encryptdata['P'];
  10269. if (isset($this->encryptdata['EncryptMetadata']) AND (!$this->encryptdata['EncryptMetadata'])) {
  10270. $out .= ' /EncryptMetadata false';
  10271. } else {
  10272. $out .= ' /EncryptMetadata true';
  10273. }
  10274. }
  10275. $out .= ' >>';
  10276. $out .= "\n".'endobj';
  10277. $this->_out($out);
  10278. }
  10279. /**
  10280. * Compute U value (used for encryption)
  10281. * @return string U value
  10282. * @protected
  10283. * @since 2.0.000 (2008-01-02)
  10284. * @author Nicola Asuni
  10285. */
  10286. protected function _Uvalue() {
  10287. if ($this->encryptdata['mode'] == 0) { // RC4-40
  10288. return TCPDF_STATIC::_RC4($this->encryptdata['key'], TCPDF_STATIC::$enc_padding, $this->last_enc_key, $this->last_enc_key_c);
  10289. } elseif ($this->encryptdata['mode'] < 3) { // RC4-128, AES-128
  10290. $tmp = TCPDF_STATIC::_md5_16(TCPDF_STATIC::$enc_padding.$this->encryptdata['fileid']);
  10291. $enc = TCPDF_STATIC::_RC4($this->encryptdata['key'], $tmp, $this->last_enc_key, $this->last_enc_key_c);
  10292. $len = strlen($tmp);
  10293. for ($i = 1; $i <= 19; ++$i) {
  10294. $ek = '';
  10295. for ($j = 0; $j < $len; ++$j) {
  10296. $ek .= chr(ord($this->encryptdata['key'][$j]) ^ $i);
  10297. }
  10298. $enc = TCPDF_STATIC::_RC4($ek, $enc, $this->last_enc_key, $this->last_enc_key_c);
  10299. }
  10300. $enc .= str_repeat("\x00", 16);
  10301. return substr($enc, 0, 32);
  10302. } elseif ($this->encryptdata['mode'] == 3) { // AES-256
  10303. $seed = TCPDF_STATIC::_md5_16(TCPDF_STATIC::getRandomSeed());
  10304. // User Validation Salt
  10305. $this->encryptdata['UVS'] = substr($seed, 0, 8);
  10306. // User Key Salt
  10307. $this->encryptdata['UKS'] = substr($seed, 8, 16);
  10308. return hash('sha256', $this->encryptdata['user_password'].$this->encryptdata['UVS'], true).$this->encryptdata['UVS'].$this->encryptdata['UKS'];
  10309. }
  10310. }
  10311. /**
  10312. * Compute UE value (used for encryption)
  10313. * @return string UE value
  10314. * @protected
  10315. * @since 5.9.006 (2010-10-19)
  10316. * @author Nicola Asuni
  10317. */
  10318. protected function _UEvalue() {
  10319. $hashkey = hash('sha256', $this->encryptdata['user_password'].$this->encryptdata['UKS'], true);
  10320. return TCPDF_STATIC::_AESnopad($hashkey, $this->encryptdata['key']);
  10321. }
  10322. /**
  10323. * Compute O value (used for encryption)
  10324. * @return string O value
  10325. * @protected
  10326. * @since 2.0.000 (2008-01-02)
  10327. * @author Nicola Asuni
  10328. */
  10329. protected function _Ovalue() {
  10330. if ($this->encryptdata['mode'] < 3) { // RC4-40, RC4-128, AES-128
  10331. $tmp = TCPDF_STATIC::_md5_16($this->encryptdata['owner_password']);
  10332. if ($this->encryptdata['mode'] > 0) {
  10333. for ($i = 0; $i < 50; ++$i) {
  10334. $tmp = TCPDF_STATIC::_md5_16($tmp);
  10335. }
  10336. }
  10337. $owner_key = substr($tmp, 0, ($this->encryptdata['Length'] / 8));
  10338. $enc = TCPDF_STATIC::_RC4($owner_key, $this->encryptdata['user_password'], $this->last_enc_key, $this->last_enc_key_c);
  10339. if ($this->encryptdata['mode'] > 0) {
  10340. $len = strlen($owner_key);
  10341. for ($i = 1; $i <= 19; ++$i) {
  10342. $ek = '';
  10343. for ($j = 0; $j < $len; ++$j) {
  10344. $ek .= chr(ord($owner_key[$j]) ^ $i);
  10345. }
  10346. $enc = TCPDF_STATIC::_RC4($ek, $enc, $this->last_enc_key, $this->last_enc_key_c);
  10347. }
  10348. }
  10349. return $enc;
  10350. } elseif ($this->encryptdata['mode'] == 3) { // AES-256
  10351. $seed = TCPDF_STATIC::_md5_16(TCPDF_STATIC::getRandomSeed());
  10352. // Owner Validation Salt
  10353. $this->encryptdata['OVS'] = substr($seed, 0, 8);
  10354. // Owner Key Salt
  10355. $this->encryptdata['OKS'] = substr($seed, 8, 16);
  10356. return hash('sha256', $this->encryptdata['owner_password'].$this->encryptdata['OVS'].$this->encryptdata['U'], true).$this->encryptdata['OVS'].$this->encryptdata['OKS'];
  10357. }
  10358. }
  10359. /**
  10360. * Compute OE value (used for encryption)
  10361. * @return string OE value
  10362. * @protected
  10363. * @since 5.9.006 (2010-10-19)
  10364. * @author Nicola Asuni
  10365. */
  10366. protected function _OEvalue() {
  10367. $hashkey = hash('sha256', $this->encryptdata['owner_password'].$this->encryptdata['OKS'].$this->encryptdata['U'], true);
  10368. return TCPDF_STATIC::_AESnopad($hashkey, $this->encryptdata['key']);
  10369. }
  10370. /**
  10371. * Convert password for AES-256 encryption mode
  10372. * @param string $password password
  10373. * @return string password
  10374. * @protected
  10375. * @since 5.9.006 (2010-10-19)
  10376. * @author Nicola Asuni
  10377. */
  10378. protected function _fixAES256Password($password) {
  10379. $psw = ''; // password to be returned
  10380. $psw_array = TCPDF_FONTS::utf8Bidi(TCPDF_FONTS::UTF8StringToArray($password, $this->isunicode, $this->CurrentFont), $password, $this->rtl, $this->isunicode, $this->CurrentFont);
  10381. foreach ($psw_array as $c) {
  10382. $psw .= TCPDF_FONTS::unichr($c, $this->isunicode);
  10383. }
  10384. return substr($psw, 0, 127);
  10385. }
  10386. /**
  10387. * Compute encryption key
  10388. * @protected
  10389. * @since 2.0.000 (2008-01-02)
  10390. * @author Nicola Asuni
  10391. */
  10392. protected function _generateencryptionkey() {
  10393. $keybytelen = ($this->encryptdata['Length'] / 8);
  10394. if (!$this->encryptdata['pubkey']) { // standard mode
  10395. if ($this->encryptdata['mode'] == 3) { // AES-256
  10396. // generate 256 bit random key
  10397. $this->encryptdata['key'] = substr(hash('sha256', TCPDF_STATIC::getRandomSeed(), true), 0, $keybytelen);
  10398. // truncate passwords
  10399. $this->encryptdata['user_password'] = $this->_fixAES256Password($this->encryptdata['user_password']);
  10400. $this->encryptdata['owner_password'] = $this->_fixAES256Password($this->encryptdata['owner_password']);
  10401. // Compute U value
  10402. $this->encryptdata['U'] = $this->_Uvalue();
  10403. // Compute UE value
  10404. $this->encryptdata['UE'] = $this->_UEvalue();
  10405. // Compute O value
  10406. $this->encryptdata['O'] = $this->_Ovalue();
  10407. // Compute OE value
  10408. $this->encryptdata['OE'] = $this->_OEvalue();
  10409. // Compute P value
  10410. $this->encryptdata['P'] = $this->encryptdata['protection'];
  10411. // Computing the encryption dictionary's Perms (permissions) value
  10412. $perms = TCPDF_STATIC::getEncPermissionsString($this->encryptdata['protection']); // bytes 0-3
  10413. $perms .= chr(255).chr(255).chr(255).chr(255); // bytes 4-7
  10414. if (isset($this->encryptdata['CF']['EncryptMetadata']) AND (!$this->encryptdata['CF']['EncryptMetadata'])) { // byte 8
  10415. $perms .= 'F';
  10416. } else {
  10417. $perms .= 'T';
  10418. }
  10419. $perms .= 'adb'; // bytes 9-11
  10420. $perms .= 'nick'; // bytes 12-15
  10421. $this->encryptdata['perms'] = TCPDF_STATIC::_AESnopad($this->encryptdata['key'], $perms);
  10422. } else { // RC4-40, RC4-128, AES-128
  10423. // Pad passwords
  10424. $this->encryptdata['user_password'] = substr($this->encryptdata['user_password'].TCPDF_STATIC::$enc_padding, 0, 32);
  10425. $this->encryptdata['owner_password'] = substr($this->encryptdata['owner_password'].TCPDF_STATIC::$enc_padding, 0, 32);
  10426. // Compute O value
  10427. $this->encryptdata['O'] = $this->_Ovalue();
  10428. // get default permissions (reverse byte order)
  10429. $permissions = TCPDF_STATIC::getEncPermissionsString($this->encryptdata['protection']);
  10430. // Compute encryption key
  10431. $tmp = TCPDF_STATIC::_md5_16($this->encryptdata['user_password'].$this->encryptdata['O'].$permissions.$this->encryptdata['fileid']);
  10432. if ($this->encryptdata['mode'] > 0) {
  10433. for ($i = 0; $i < 50; ++$i) {
  10434. $tmp = TCPDF_STATIC::_md5_16(substr($tmp, 0, $keybytelen));
  10435. }
  10436. }
  10437. $this->encryptdata['key'] = substr($tmp, 0, $keybytelen);
  10438. // Compute U value
  10439. $this->encryptdata['U'] = $this->_Uvalue();
  10440. // Compute P value
  10441. $this->encryptdata['P'] = $this->encryptdata['protection'];
  10442. }
  10443. } else { // Public-Key mode
  10444. // random 20-byte seed
  10445. $seed = sha1(TCPDF_STATIC::getRandomSeed(), true);
  10446. $recipient_bytes = '';
  10447. foreach ($this->encryptdata['pubkeys'] as $pubkey) {
  10448. // for each public certificate
  10449. if (isset($pubkey['p'])) {
  10450. $pkprotection = TCPDF_STATIC::getUserPermissionCode($pubkey['p'], $this->encryptdata['mode']);
  10451. } else {
  10452. $pkprotection = $this->encryptdata['protection'];
  10453. }
  10454. // get default permissions (reverse byte order)
  10455. $pkpermissions = TCPDF_STATIC::getEncPermissionsString($pkprotection);
  10456. // envelope data
  10457. $envelope = $seed.$pkpermissions;
  10458. // write the envelope data to a temporary file
  10459. $tempkeyfile = TCPDF_STATIC::getObjFilename('key', $this->file_id);
  10460. $f = TCPDF_STATIC::fopenLocal($tempkeyfile, 'wb');
  10461. if (!$f) {
  10462. $this->Error('Unable to create temporary key file: '.$tempkeyfile);
  10463. }
  10464. $envelope_length = strlen($envelope);
  10465. fwrite($f, $envelope, $envelope_length);
  10466. fclose($f);
  10467. $tempencfile = TCPDF_STATIC::getObjFilename('enc', $this->file_id);
  10468. if (!openssl_pkcs7_encrypt($tempkeyfile, $tempencfile, $pubkey['c'], array(), PKCS7_BINARY | PKCS7_DETACHED)) {
  10469. $this->Error('Unable to encrypt the file: '.$tempkeyfile);
  10470. }
  10471. // read encryption signature
  10472. $signature = file_get_contents($tempencfile, false, null, $envelope_length);
  10473. // extract signature
  10474. $signature = substr($signature, strpos($signature, 'Content-Disposition'));
  10475. $tmparr = explode("\n\n", $signature);
  10476. $signature = trim($tmparr[1]);
  10477. unset($tmparr);
  10478. // decode signature
  10479. $signature = base64_decode($signature);
  10480. // convert signature to hex
  10481. $hexsignature = current(unpack('H*', $signature));
  10482. // store signature on recipients array
  10483. $this->encryptdata['Recipients'][] = $hexsignature;
  10484. // The bytes of each item in the Recipients array of PKCS#7 objects in the order in which they appear in the array
  10485. $recipient_bytes .= $signature;
  10486. }
  10487. // calculate encryption key
  10488. if ($this->encryptdata['mode'] == 3) { // AES-256
  10489. $this->encryptdata['key'] = substr(hash('sha256', $seed.$recipient_bytes, true), 0, $keybytelen);
  10490. } else { // RC4-40, RC4-128, AES-128
  10491. $this->encryptdata['key'] = substr(sha1($seed.$recipient_bytes, true), 0, $keybytelen);
  10492. }
  10493. }
  10494. }
  10495. /**
  10496. * Set document protection
  10497. * Remark: the protection against modification is for people who have the full Acrobat product.
  10498. * If you don't set any password, the document will open as usual. If you set a user password, the PDF viewer will ask for it before displaying the document. The master password, if different from the user one, can be used to get full access.
  10499. * Note: protecting a document requires to encrypt it, which increases the processing time a lot. This can cause a PHP time-out in some cases, especially if the document contains images or fonts.
  10500. * @param array $permissions the set of permissions (specify the ones you want to block):<ul><li>print : Print the document;</li><li>modify : Modify the contents of the document by operations other than those controlled by 'fill-forms', 'extract' and 'assemble';</li><li>copy : Copy or otherwise extract text and graphics from the document;</li><li>annot-forms : Add or modify text annotations, fill in interactive form fields, and, if 'modify' is also set, create or modify interactive form fields (including signature fields);</li><li>fill-forms : Fill in existing interactive form fields (including signature fields), even if 'annot-forms' is not specified;</li><li>extract : Extract text and graphics (in support of accessibility to users with disabilities or for other purposes);</li><li>assemble : Assemble the document (insert, rotate, or delete pages and create bookmarks or thumbnail images), even if 'modify' is not set;</li><li>print-high : Print the document to a representation from which a faithful digital copy of the PDF content could be generated. When this is not set, printing is limited to a low-level representation of the appearance, possibly of degraded quality.</li><li>owner : (inverted logic - only for public-key) when set permits change of encryption and enables all other permissions.</li></ul>
  10501. * @param string $user_pass user password. Empty by default.
  10502. * @param string|null $owner_pass owner password. If not specified, a random value is used.
  10503. * @param int $mode encryption strength: 0 = RC4 40 bit; 1 = RC4 128 bit; 2 = AES 128 bit; 3 = AES 256 bit.
  10504. * @param array|null $pubkeys array of recipients containing public-key certificates ('c') and permissions ('p'). For example: array(array('c' => 'file://../examples/data/cert/tcpdf.crt', 'p' => array('print')))
  10505. * @public
  10506. * @since 2.0.000 (2008-01-02)
  10507. * @author Nicola Asuni
  10508. */
  10509. public function setProtection($permissions=array('print', 'modify', 'copy', 'annot-forms', 'fill-forms', 'extract', 'assemble', 'print-high'), $user_pass='', $owner_pass=null, $mode=0, $pubkeys=null) {
  10510. if ($this->pdfa_mode) {
  10511. // encryption is not allowed in PDF/A mode
  10512. return;
  10513. }
  10514. $this->encryptdata['protection'] = TCPDF_STATIC::getUserPermissionCode($permissions, $mode);
  10515. if (($pubkeys !== null) AND (is_array($pubkeys))) {
  10516. // public-key mode
  10517. $this->encryptdata['pubkeys'] = $pubkeys;
  10518. if ($mode == 0) {
  10519. // public-Key Security requires at least 128 bit
  10520. $mode = 1;
  10521. }
  10522. if (!function_exists('openssl_pkcs7_encrypt')) {
  10523. $this->Error('Public-Key Security requires openssl library.');
  10524. }
  10525. // Set Public-Key filter (available are: Entrust.PPKEF, Adobe.PPKLite, Adobe.PubSec)
  10526. $this->encryptdata['pubkey'] = true;
  10527. $this->encryptdata['Filter'] = 'Adobe.PubSec';
  10528. $this->encryptdata['StmF'] = 'DefaultCryptFilter';
  10529. $this->encryptdata['StrF'] = 'DefaultCryptFilter';
  10530. } else {
  10531. // standard mode (password mode)
  10532. $this->encryptdata['pubkey'] = false;
  10533. $this->encryptdata['Filter'] = 'Standard';
  10534. $this->encryptdata['StmF'] = 'StdCF';
  10535. $this->encryptdata['StrF'] = 'StdCF';
  10536. }
  10537. if ($mode > 1) { // AES
  10538. if (!extension_loaded('openssl') && !extension_loaded('mcrypt')) {
  10539. $this->Error('AES encryption requires openssl or mcrypt extension (http://www.php.net/manual/en/mcrypt.requirements.php).');
  10540. }
  10541. if (extension_loaded('openssl') && !in_array('aes-256-cbc', openssl_get_cipher_methods())) {
  10542. $this->Error('AES encryption requires openssl/aes-256-cbc cypher.');
  10543. }
  10544. if (extension_loaded('mcrypt') && mcrypt_get_cipher_name(MCRYPT_RIJNDAEL_128) === false) {
  10545. $this->Error('AES encryption requires MCRYPT_RIJNDAEL_128 cypher.');
  10546. }
  10547. if (($mode == 3) AND !function_exists('hash')) {
  10548. // the Hash extension requires no external libraries and is enabled by default as of PHP 5.1.2.
  10549. $this->Error('AES 256 encryption requires HASH Message Digest Framework (http://www.php.net/manual/en/book.hash.php).');
  10550. }
  10551. }
  10552. if ($owner_pass === null) {
  10553. $owner_pass = md5(TCPDF_STATIC::getRandomSeed());
  10554. }
  10555. $this->encryptdata['user_password'] = $user_pass;
  10556. $this->encryptdata['owner_password'] = $owner_pass;
  10557. $this->encryptdata['mode'] = $mode;
  10558. switch ($mode) {
  10559. case 0: { // RC4 40 bit
  10560. $this->encryptdata['V'] = 1;
  10561. $this->encryptdata['Length'] = 40;
  10562. $this->encryptdata['CF']['CFM'] = 'V2';
  10563. break;
  10564. }
  10565. case 1: { // RC4 128 bit
  10566. $this->encryptdata['V'] = 2;
  10567. $this->encryptdata['Length'] = 128;
  10568. $this->encryptdata['CF']['CFM'] = 'V2';
  10569. if ($this->encryptdata['pubkey']) {
  10570. $this->encryptdata['SubFilter'] = 'adbe.pkcs7.s4';
  10571. $this->encryptdata['Recipients'] = array();
  10572. }
  10573. break;
  10574. }
  10575. case 2: { // AES 128 bit
  10576. $this->encryptdata['V'] = 4;
  10577. $this->encryptdata['Length'] = 128;
  10578. $this->encryptdata['CF']['CFM'] = 'AESV2';
  10579. $this->encryptdata['CF']['Length'] = 16;
  10580. if ($this->encryptdata['pubkey']) {
  10581. $this->encryptdata['SubFilter'] = 'adbe.pkcs7.s5';
  10582. $this->encryptdata['Recipients'] = array();
  10583. }
  10584. break;
  10585. }
  10586. case 3: { // AES 256 bit
  10587. $this->encryptdata['V'] = 5;
  10588. $this->encryptdata['Length'] = 256;
  10589. $this->encryptdata['CF']['CFM'] = 'AESV3';
  10590. $this->encryptdata['CF']['Length'] = 32;
  10591. if ($this->encryptdata['pubkey']) {
  10592. $this->encryptdata['SubFilter'] = 'adbe.pkcs7.s5';
  10593. $this->encryptdata['Recipients'] = array();
  10594. }
  10595. break;
  10596. }
  10597. }
  10598. $this->encrypted = true;
  10599. $this->encryptdata['fileid'] = TCPDF_STATIC::convertHexStringToString($this->file_id);
  10600. $this->_generateencryptionkey();
  10601. }
  10602. // END OF ENCRYPTION FUNCTIONS -------------------------
  10603. // START TRANSFORMATIONS SECTION -----------------------
  10604. /**
  10605. * Starts a 2D tranformation saving current graphic state.
  10606. * This function must be called before scaling, mirroring, translation, rotation and skewing.
  10607. * Use StartTransform() before, and StopTransform() after the transformations to restore the normal behavior.
  10608. * @public
  10609. * @since 2.1.000 (2008-01-07)
  10610. * @see StartTransform(), StopTransform()
  10611. */
  10612. public function StartTransform() {
  10613. if ($this->state != 2) {
  10614. return;
  10615. }
  10616. $this->_outSaveGraphicsState();
  10617. if ($this->inxobj) {
  10618. // we are inside an XObject template
  10619. $this->xobjects[$this->xobjid]['transfmrk'][] = strlen($this->xobjects[$this->xobjid]['outdata']);
  10620. } else {
  10621. $this->transfmrk[$this->page][] = $this->pagelen[$this->page];
  10622. }
  10623. ++$this->transfmatrix_key;
  10624. $this->transfmatrix[$this->transfmatrix_key] = array();
  10625. }
  10626. /**
  10627. * Stops a 2D tranformation restoring previous graphic state.
  10628. * This function must be called after scaling, mirroring, translation, rotation and skewing.
  10629. * Use StartTransform() before, and StopTransform() after the transformations to restore the normal behavior.
  10630. * @public
  10631. * @since 2.1.000 (2008-01-07)
  10632. * @see StartTransform(), StopTransform()
  10633. */
  10634. public function StopTransform() {
  10635. if ($this->state != 2) {
  10636. return;
  10637. }
  10638. $this->_outRestoreGraphicsState();
  10639. if (isset($this->transfmatrix[$this->transfmatrix_key])) {
  10640. array_pop($this->transfmatrix[$this->transfmatrix_key]);
  10641. --$this->transfmatrix_key;
  10642. }
  10643. if ($this->inxobj) {
  10644. // we are inside an XObject template
  10645. array_pop($this->xobjects[$this->xobjid]['transfmrk']);
  10646. } else {
  10647. array_pop($this->transfmrk[$this->page]);
  10648. }
  10649. }
  10650. /**
  10651. * Horizontal Scaling.
  10652. * @param float $s_x scaling factor for width as percent. 0 is not allowed.
  10653. * @param int $x abscissa of the scaling center. Default is current x position
  10654. * @param int $y ordinate of the scaling center. Default is current y position
  10655. * @public
  10656. * @since 2.1.000 (2008-01-07)
  10657. * @see StartTransform(), StopTransform()
  10658. */
  10659. public function ScaleX($s_x, $x='', $y='') {
  10660. $this->Scale($s_x, 100, $x, $y);
  10661. }
  10662. /**
  10663. * Vertical Scaling.
  10664. * @param float $s_y scaling factor for height as percent. 0 is not allowed.
  10665. * @param int $x abscissa of the scaling center. Default is current x position
  10666. * @param int $y ordinate of the scaling center. Default is current y position
  10667. * @public
  10668. * @since 2.1.000 (2008-01-07)
  10669. * @see StartTransform(), StopTransform()
  10670. */
  10671. public function ScaleY($s_y, $x='', $y='') {
  10672. $this->Scale(100, $s_y, $x, $y);
  10673. }
  10674. /**
  10675. * Vertical and horizontal proportional Scaling.
  10676. * @param float $s scaling factor for width and height as percent. 0 is not allowed.
  10677. * @param int $x abscissa of the scaling center. Default is current x position
  10678. * @param int $y ordinate of the scaling center. Default is current y position
  10679. * @public
  10680. * @since 2.1.000 (2008-01-07)
  10681. * @see StartTransform(), StopTransform()
  10682. */
  10683. public function ScaleXY($s, $x='', $y='') {
  10684. $this->Scale($s, $s, $x, $y);
  10685. }
  10686. /**
  10687. * Vertical and horizontal non-proportional Scaling.
  10688. * @param float $s_x scaling factor for width as percent. 0 is not allowed.
  10689. * @param float $s_y scaling factor for height as percent. 0 is not allowed.
  10690. * @param float|null $x abscissa of the scaling center. Default is current x position
  10691. * @param float|null $y ordinate of the scaling center. Default is current y position
  10692. * @public
  10693. * @since 2.1.000 (2008-01-07)
  10694. * @see StartTransform(), StopTransform()
  10695. */
  10696. public function Scale($s_x, $s_y, $x=null, $y=null) {
  10697. if (TCPDF_STATIC::empty_string($x)) {
  10698. $x = $this->x;
  10699. }
  10700. if (TCPDF_STATIC::empty_string($y)) {
  10701. $y = $this->y;
  10702. }
  10703. if (($s_x == 0) OR ($s_y == 0)) {
  10704. $this->Error('Please do not use values equal to zero for scaling');
  10705. }
  10706. $y = ($this->h - $y) * $this->k;
  10707. $x *= $this->k;
  10708. //calculate elements of transformation matrix
  10709. $s_x /= 100;
  10710. $s_y /= 100;
  10711. $tm = array();
  10712. $tm[0] = $s_x;
  10713. $tm[1] = 0;
  10714. $tm[2] = 0;
  10715. $tm[3] = $s_y;
  10716. $tm[4] = $x * (1 - $s_x);
  10717. $tm[5] = $y * (1 - $s_y);
  10718. //scale the coordinate system
  10719. $this->Transform($tm);
  10720. }
  10721. /**
  10722. * Horizontal Mirroring.
  10723. * @param float|null $x abscissa of the point. Default is current x position
  10724. * @public
  10725. * @since 2.1.000 (2008-01-07)
  10726. * @see StartTransform(), StopTransform()
  10727. */
  10728. public function MirrorH($x=null) {
  10729. $this->Scale(-100, 100, $x);
  10730. }
  10731. /**
  10732. * Verical Mirroring.
  10733. * @param float|null $y ordinate of the point. Default is current y position
  10734. * @public
  10735. * @since 2.1.000 (2008-01-07)
  10736. * @see StartTransform(), StopTransform()
  10737. */
  10738. public function MirrorV($y=null) {
  10739. $this->Scale(100, -100, null, $y);
  10740. }
  10741. /**
  10742. * Point reflection mirroring.
  10743. * @param float|null $x abscissa of the point. Default is current x position
  10744. * @param float|null $y ordinate of the point. Default is current y position
  10745. * @public
  10746. * @since 2.1.000 (2008-01-07)
  10747. * @see StartTransform(), StopTransform()
  10748. */
  10749. public function MirrorP($x=null,$y=null) {
  10750. $this->Scale(-100, -100, $x, $y);
  10751. }
  10752. /**
  10753. * Reflection against a straight line through point (x, y) with the gradient angle (angle).
  10754. * @param float $angle gradient angle of the straight line. Default is 0 (horizontal line).
  10755. * @param float|null $x abscissa of the point. Default is current x position
  10756. * @param float|null $y ordinate of the point. Default is current y position
  10757. * @public
  10758. * @since 2.1.000 (2008-01-07)
  10759. * @see StartTransform(), StopTransform()
  10760. */
  10761. public function MirrorL($angle=0, $x=null,$y=null) {
  10762. $this->Scale(-100, 100, $x, $y);
  10763. $this->Rotate(-2*($angle-90), $x, $y);
  10764. }
  10765. /**
  10766. * Translate graphic object horizontally.
  10767. * @param int $t_x movement to the right (or left for RTL)
  10768. * @public
  10769. * @since 2.1.000 (2008-01-07)
  10770. * @see StartTransform(), StopTransform()
  10771. */
  10772. public function TranslateX($t_x) {
  10773. $this->Translate($t_x, 0);
  10774. }
  10775. /**
  10776. * Translate graphic object vertically.
  10777. * @param int $t_y movement to the bottom
  10778. * @public
  10779. * @since 2.1.000 (2008-01-07)
  10780. * @see StartTransform(), StopTransform()
  10781. */
  10782. public function TranslateY($t_y) {
  10783. $this->Translate(0, $t_y);
  10784. }
  10785. /**
  10786. * Translate graphic object horizontally and vertically.
  10787. * @param int $t_x movement to the right
  10788. * @param int $t_y movement to the bottom
  10789. * @public
  10790. * @since 2.1.000 (2008-01-07)
  10791. * @see StartTransform(), StopTransform()
  10792. */
  10793. public function Translate($t_x, $t_y) {
  10794. //calculate elements of transformation matrix
  10795. $tm = array();
  10796. $tm[0] = 1;
  10797. $tm[1] = 0;
  10798. $tm[2] = 0;
  10799. $tm[3] = 1;
  10800. $tm[4] = $t_x * $this->k;
  10801. $tm[5] = -$t_y * $this->k;
  10802. //translate the coordinate system
  10803. $this->Transform($tm);
  10804. }
  10805. /**
  10806. * Rotate object.
  10807. * @param float $angle angle in degrees for counter-clockwise rotation
  10808. * @param float|null $x abscissa of the rotation center. Default is current x position
  10809. * @param float|null $y ordinate of the rotation center. Default is current y position
  10810. * @public
  10811. * @since 2.1.000 (2008-01-07)
  10812. * @see StartTransform(), StopTransform()
  10813. */
  10814. public function Rotate($angle, $x=null, $y=null) {
  10815. if (TCPDF_STATIC::empty_string($x)) {
  10816. $x = $this->x;
  10817. }
  10818. if (TCPDF_STATIC::empty_string($y)) {
  10819. $y = $this->y;
  10820. }
  10821. $y = ($this->h - $y) * $this->k;
  10822. $x *= $this->k;
  10823. //calculate elements of transformation matrix
  10824. $tm = array();
  10825. $tm[0] = cos(deg2rad($angle));
  10826. $tm[1] = sin(deg2rad($angle));
  10827. $tm[2] = -$tm[1];
  10828. $tm[3] = $tm[0];
  10829. $tm[4] = $x + ($tm[1] * $y) - ($tm[0] * $x);
  10830. $tm[5] = $y - ($tm[0] * $y) - ($tm[1] * $x);
  10831. //rotate the coordinate system around ($x,$y)
  10832. $this->Transform($tm);
  10833. }
  10834. /**
  10835. * Skew horizontally.
  10836. * @param float $angle_x angle in degrees between -90 (skew to the left) and 90 (skew to the right)
  10837. * @param float|null $x abscissa of the skewing center. default is current x position
  10838. * @param float|null $y ordinate of the skewing center. default is current y position
  10839. * @public
  10840. * @since 2.1.000 (2008-01-07)
  10841. * @see StartTransform(), StopTransform()
  10842. */
  10843. public function SkewX($angle_x, $x=null, $y=null) {
  10844. $this->Skew($angle_x, 0, $x, $y);
  10845. }
  10846. /**
  10847. * Skew vertically.
  10848. * @param float $angle_y angle in degrees between -90 (skew to the bottom) and 90 (skew to the top)
  10849. * @param float|null $x abscissa of the skewing center. default is current x position
  10850. * @param float|null $y ordinate of the skewing center. default is current y position
  10851. * @public
  10852. * @since 2.1.000 (2008-01-07)
  10853. * @see StartTransform(), StopTransform()
  10854. */
  10855. public function SkewY($angle_y, $x=null, $y=null) {
  10856. $this->Skew(0, $angle_y, $x, $y);
  10857. }
  10858. /**
  10859. * Skew.
  10860. * @param float $angle_x angle in degrees between -90 (skew to the left) and 90 (skew to the right)
  10861. * @param float $angle_y angle in degrees between -90 (skew to the bottom) and 90 (skew to the top)
  10862. * @param float|null $x abscissa of the skewing center. default is current x position
  10863. * @param float|null $y ordinate of the skewing center. default is current y position
  10864. * @public
  10865. * @since 2.1.000 (2008-01-07)
  10866. * @see StartTransform(), StopTransform()
  10867. */
  10868. public function Skew($angle_x, $angle_y, $x=null, $y=null) {
  10869. if (TCPDF_STATIC::empty_string($x)) {
  10870. $x = $this->x;
  10871. }
  10872. if (TCPDF_STATIC::empty_string($y)) {
  10873. $y = $this->y;
  10874. }
  10875. if (($angle_x <= -90) OR ($angle_x >= 90) OR ($angle_y <= -90) OR ($angle_y >= 90)) {
  10876. $this->Error('Please use values between -90 and +90 degrees for Skewing.');
  10877. }
  10878. $x *= $this->k;
  10879. $y = ($this->h - $y) * $this->k;
  10880. //calculate elements of transformation matrix
  10881. $tm = array();
  10882. $tm[0] = 1;
  10883. $tm[1] = tan(deg2rad($angle_y));
  10884. $tm[2] = tan(deg2rad($angle_x));
  10885. $tm[3] = 1;
  10886. $tm[4] = -$tm[2] * $y;
  10887. $tm[5] = -$tm[1] * $x;
  10888. //skew the coordinate system
  10889. $this->Transform($tm);
  10890. }
  10891. /**
  10892. * Apply graphic transformations.
  10893. * @param array $tm transformation matrix
  10894. * @protected
  10895. * @since 2.1.000 (2008-01-07)
  10896. * @see StartTransform(), StopTransform()
  10897. */
  10898. protected function Transform($tm) {
  10899. if ($this->state != 2) {
  10900. return;
  10901. }
  10902. $this->_out(sprintf('%F %F %F %F %F %F cm', $tm[0], $tm[1], $tm[2], $tm[3], $tm[4], $tm[5]));
  10903. // add tranformation matrix
  10904. $this->transfmatrix[$this->transfmatrix_key][] = array('a' => $tm[0], 'b' => $tm[1], 'c' => $tm[2], 'd' => $tm[3], 'e' => $tm[4], 'f' => $tm[5]);
  10905. // update transformation mark
  10906. if ($this->inxobj) {
  10907. // we are inside an XObject template
  10908. if (end($this->xobjects[$this->xobjid]['transfmrk']) !== false) {
  10909. $key = key($this->xobjects[$this->xobjid]['transfmrk']);
  10910. $this->xobjects[$this->xobjid]['transfmrk'][$key] = strlen($this->xobjects[$this->xobjid]['outdata']);
  10911. }
  10912. } elseif (end($this->transfmrk[$this->page]) !== false) {
  10913. $key = key($this->transfmrk[$this->page]);
  10914. $this->transfmrk[$this->page][$key] = $this->pagelen[$this->page];
  10915. }
  10916. }
  10917. // END TRANSFORMATIONS SECTION -------------------------
  10918. // START GRAPHIC FUNCTIONS SECTION ---------------------
  10919. // The following section is based on the code provided by David Hernandez Sanz
  10920. /**
  10921. * Defines the line width. By default, the value equals 0.2 mm. The method can be called before the first page is created and the value is retained from page to page.
  10922. * @param float $width The width.
  10923. * @public
  10924. * @since 1.0
  10925. * @see Line(), Rect(), Cell(), MultiCell()
  10926. */
  10927. public function setLineWidth($width) {
  10928. //Set line width
  10929. $this->LineWidth = $width;
  10930. $this->linestyleWidth = sprintf('%F w', ($width * $this->k));
  10931. if ($this->state == 2) {
  10932. $this->_out($this->linestyleWidth);
  10933. }
  10934. }
  10935. /**
  10936. * Returns the current the line width.
  10937. * @return int Line width
  10938. * @public
  10939. * @since 2.1.000 (2008-01-07)
  10940. * @see Line(), SetLineWidth()
  10941. */
  10942. public function GetLineWidth() {
  10943. return $this->LineWidth;
  10944. }
  10945. /**
  10946. * Set line style.
  10947. * @param array $style Line style. Array with keys among the following:
  10948. * <ul>
  10949. * <li>width (float): Width of the line in user units.</li>
  10950. * <li>cap (string): Type of cap to put on the line. Possible values are:
  10951. * butt, round, square. The difference between "square" and "butt" is that
  10952. * "square" projects a flat end past the end of the line.</li>
  10953. * <li>join (string): Type of join. Possible values are: miter, round,
  10954. * bevel.</li>
  10955. * <li>dash (mixed): Dash pattern. Is 0 (without dash) or string with
  10956. * series of length values, which are the lengths of the on and off dashes.
  10957. * For example: "2" represents 2 on, 2 off, 2 on, 2 off, ...; "2,1" is 2 on,
  10958. * 1 off, 2 on, 1 off, ...</li>
  10959. * <li>phase (integer): Modifier on the dash pattern which is used to shift
  10960. * the point at which the pattern starts.</li>
  10961. * <li>color (array): Draw color. Format: array(GREY) or array(R,G,B) or array(C,M,Y,K) or array(C,M,Y,K,SpotColorName).</li>
  10962. * </ul>
  10963. * @param boolean $ret if true do not send the command.
  10964. * @return string the PDF command
  10965. * @public
  10966. * @since 2.1.000 (2008-01-08)
  10967. */
  10968. public function setLineStyle($style, $ret=false) {
  10969. $s = ''; // string to be returned
  10970. if (!is_array($style)) {
  10971. return $s;
  10972. }
  10973. if (isset($style['width'])) {
  10974. $this->LineWidth = $style['width'];
  10975. $this->linestyleWidth = sprintf('%F w', ($style['width'] * $this->k));
  10976. $s .= $this->linestyleWidth.' ';
  10977. }
  10978. if (isset($style['cap'])) {
  10979. $ca = array('butt' => 0, 'round'=> 1, 'square' => 2);
  10980. if (isset($ca[$style['cap']])) {
  10981. $this->linestyleCap = $ca[$style['cap']].' J';
  10982. $s .= $this->linestyleCap.' ';
  10983. }
  10984. }
  10985. if (isset($style['join'])) {
  10986. $ja = array('miter' => 0, 'round' => 1, 'bevel' => 2);
  10987. if (isset($ja[$style['join']])) {
  10988. $this->linestyleJoin = $ja[$style['join']].' j';
  10989. $s .= $this->linestyleJoin.' ';
  10990. }
  10991. }
  10992. if (isset($style['dash'])) {
  10993. $dash_string = '';
  10994. if ($style['dash']) {
  10995. if (preg_match('/^.+,/', $style['dash']) > 0) {
  10996. $tab = explode(',', $style['dash']);
  10997. } else {
  10998. $tab = array($style['dash']);
  10999. }
  11000. $dash_string = '';
  11001. foreach ($tab as $i => $v) {
  11002. if ($i) {
  11003. $dash_string .= ' ';
  11004. }
  11005. $dash_string .= sprintf('%F', $v);
  11006. }
  11007. }
  11008. if (!isset($style['phase']) OR !$style['dash']) {
  11009. $style['phase'] = 0;
  11010. }
  11011. $this->linestyleDash = sprintf('[%s] %F d', $dash_string, $style['phase']);
  11012. $s .= $this->linestyleDash.' ';
  11013. }
  11014. if (isset($style['color'])) {
  11015. $s .= $this->setDrawColorArray($style['color'], true).' ';
  11016. }
  11017. if (!$ret AND ($this->state == 2)) {
  11018. $this->_out($s);
  11019. }
  11020. return $s;
  11021. }
  11022. /**
  11023. * Begin a new subpath by moving the current point to coordinates (x, y), omitting any connecting line segment.
  11024. * @param float $x Abscissa of point.
  11025. * @param float $y Ordinate of point.
  11026. * @protected
  11027. * @since 2.1.000 (2008-01-08)
  11028. */
  11029. protected function _outPoint($x, $y) {
  11030. if ($this->state == 2) {
  11031. $this->_out(sprintf('%F %F m', ($x * $this->k), (($this->h - $y) * $this->k)));
  11032. }
  11033. }
  11034. /**
  11035. * Append a straight line segment from the current point to the point (x, y).
  11036. * The new current point shall be (x, y).
  11037. * @param float $x Abscissa of end point.
  11038. * @param float $y Ordinate of end point.
  11039. * @protected
  11040. * @since 2.1.000 (2008-01-08)
  11041. */
  11042. protected function _outLine($x, $y) {
  11043. if ($this->state == 2) {
  11044. $this->_out(sprintf('%F %F l', ($x * $this->k), (($this->h - $y) * $this->k)));
  11045. }
  11046. }
  11047. /**
  11048. * Append a rectangle to the current path as a complete subpath, with lower-left corner (x, y) and dimensions widthand height in user space.
  11049. * @param float $x Abscissa of upper-left corner.
  11050. * @param float $y Ordinate of upper-left corner.
  11051. * @param float $w Width.
  11052. * @param float $h Height.
  11053. * @param string $op options
  11054. * @protected
  11055. * @since 2.1.000 (2008-01-08)
  11056. */
  11057. protected function _outRect($x, $y, $w, $h, $op) {
  11058. if ($this->state == 2) {
  11059. $this->_out(sprintf('%F %F %F %F re %s', ($x * $this->k), (($this->h - $y) * $this->k), ($w * $this->k), (-$h * $this->k), $op));
  11060. }
  11061. }
  11062. /**
  11063. * Append a cubic Bezier curve to the current path. The curve shall extend from the current point to the point (x3, y3), using (x1, y1) and (x2, y2) as the Bezier control points.
  11064. * The new current point shall be (x3, y3).
  11065. * @param float $x1 Abscissa of control point 1.
  11066. * @param float $y1 Ordinate of control point 1.
  11067. * @param float $x2 Abscissa of control point 2.
  11068. * @param float $y2 Ordinate of control point 2.
  11069. * @param float $x3 Abscissa of end point.
  11070. * @param float $y3 Ordinate of end point.
  11071. * @protected
  11072. * @since 2.1.000 (2008-01-08)
  11073. */
  11074. protected function _outCurve($x1, $y1, $x2, $y2, $x3, $y3) {
  11075. if ($this->state == 2) {
  11076. $this->_out(sprintf('%F %F %F %F %F %F c', ($x1 * $this->k), (($this->h - $y1) * $this->k), ($x2 * $this->k), (($this->h - $y2) * $this->k), ($x3 * $this->k), (($this->h - $y3) * $this->k)));
  11077. }
  11078. }
  11079. /**
  11080. * Append a cubic Bezier curve to the current path. The curve shall extend from the current point to the point (x3, y3), using the current point and (x2, y2) as the Bezier control points.
  11081. * The new current point shall be (x3, y3).
  11082. * @param float $x2 Abscissa of control point 2.
  11083. * @param float $y2 Ordinate of control point 2.
  11084. * @param float $x3 Abscissa of end point.
  11085. * @param float $y3 Ordinate of end point.
  11086. * @protected
  11087. * @since 4.9.019 (2010-04-26)
  11088. */
  11089. protected function _outCurveV($x2, $y2, $x3, $y3) {
  11090. if ($this->state == 2) {
  11091. $this->_out(sprintf('%F %F %F %F v', ($x2 * $this->k), (($this->h - $y2) * $this->k), ($x3 * $this->k), (($this->h - $y3) * $this->k)));
  11092. }
  11093. }
  11094. /**
  11095. * Append a cubic Bezier curve to the current path. The curve shall extend from the current point to the point (x3, y3), using (x1, y1) and (x3, y3) as the Bezier control points.
  11096. * The new current point shall be (x3, y3).
  11097. * @param float $x1 Abscissa of control point 1.
  11098. * @param float $y1 Ordinate of control point 1.
  11099. * @param float $x3 Abscissa of end point.
  11100. * @param float $y3 Ordinate of end point.
  11101. * @protected
  11102. * @since 2.1.000 (2008-01-08)
  11103. */
  11104. protected function _outCurveY($x1, $y1, $x3, $y3) {
  11105. if ($this->state == 2) {
  11106. $this->_out(sprintf('%F %F %F %F y', ($x1 * $this->k), (($this->h - $y1) * $this->k), ($x3 * $this->k), (($this->h - $y3) * $this->k)));
  11107. }
  11108. }
  11109. /**
  11110. * Draws a line between two points.
  11111. * @param float $x1 Abscissa of first point.
  11112. * @param float $y1 Ordinate of first point.
  11113. * @param float $x2 Abscissa of second point.
  11114. * @param float $y2 Ordinate of second point.
  11115. * @param array $style Line style. Array like for SetLineStyle(). Default value: default line style (empty array).
  11116. * @public
  11117. * @since 1.0
  11118. * @see SetLineWidth(), SetDrawColor(), SetLineStyle()
  11119. */
  11120. public function Line($x1, $y1, $x2, $y2, $style=array()) {
  11121. if ($this->state != 2) {
  11122. return;
  11123. }
  11124. if (is_array($style)) {
  11125. $this->setLineStyle($style);
  11126. }
  11127. $this->_outPoint($x1, $y1);
  11128. $this->_outLine($x2, $y2);
  11129. $this->_out('S');
  11130. }
  11131. /**
  11132. * Draws a rectangle.
  11133. * @param float $x Abscissa of upper-left corner.
  11134. * @param float $y Ordinate of upper-left corner.
  11135. * @param float $w Width.
  11136. * @param float $h Height.
  11137. * @param string $style Style of rendering. See the getPathPaintOperator() function for more information.
  11138. * @param array $border_style Border style of rectangle. Array with keys among the following:
  11139. * <ul>
  11140. * <li>all: Line style of all borders. Array like for SetLineStyle().</li>
  11141. * <li>L, T, R, B or combinations: Line style of left, top, right or bottom border. Array like for SetLineStyle().</li>
  11142. * </ul>
  11143. * If a key is not present or is null, the correspondent border is not drawn. Default value: default line style (empty array).
  11144. * @param array $fill_color Fill color. Format: array(GREY) or array(R,G,B) or array(C,M,Y,K) or array(C,M,Y,K,SpotColorName). Default value: default color (empty array).
  11145. * @public
  11146. * @since 1.0
  11147. * @see SetLineStyle()
  11148. */
  11149. public function Rect($x, $y, $w, $h, $style='', $border_style=array(), $fill_color=array()) {
  11150. if ($this->state != 2) {
  11151. return;
  11152. }
  11153. if (empty($style)) {
  11154. $style = 'S';
  11155. }
  11156. if (!(strpos($style, 'F') === false) AND !empty($fill_color)) {
  11157. // set background color
  11158. $this->setFillColorArray($fill_color);
  11159. }
  11160. if (!empty($border_style)) {
  11161. if (isset($border_style['all']) AND !empty($border_style['all'])) {
  11162. //set global style for border
  11163. $this->setLineStyle($border_style['all']);
  11164. $border_style = array();
  11165. } else {
  11166. // remove stroke operator from style
  11167. $opnostroke = array('S' => '', 'D' => '', 's' => '', 'd' => '', 'B' => 'F', 'FD' => 'F', 'DF' => 'F', 'B*' => 'F*', 'F*D' => 'F*', 'DF*' => 'F*', 'b' => 'f', 'fd' => 'f', 'df' => 'f', 'b*' => 'f*', 'f*d' => 'f*', 'df*' => 'f*' );
  11168. if (isset($opnostroke[$style])) {
  11169. $style = $opnostroke[$style];
  11170. }
  11171. }
  11172. }
  11173. if (!empty($style)) {
  11174. $op = TCPDF_STATIC::getPathPaintOperator($style);
  11175. $this->_outRect($x, $y, $w, $h, $op);
  11176. }
  11177. if (!empty($border_style)) {
  11178. $border_style2 = array();
  11179. foreach ($border_style as $line => $value) {
  11180. $length = strlen($line);
  11181. for ($i = 0; $i < $length; ++$i) {
  11182. $border_style2[$line[$i]] = $value;
  11183. }
  11184. }
  11185. $border_style = $border_style2;
  11186. if (isset($border_style['L']) AND $border_style['L']) {
  11187. $this->Line($x, $y, $x, $y + $h, $border_style['L']);
  11188. }
  11189. if (isset($border_style['T']) AND $border_style['T']) {
  11190. $this->Line($x, $y, $x + $w, $y, $border_style['T']);
  11191. }
  11192. if (isset($border_style['R']) AND $border_style['R']) {
  11193. $this->Line($x + $w, $y, $x + $w, $y + $h, $border_style['R']);
  11194. }
  11195. if (isset($border_style['B']) AND $border_style['B']) {
  11196. $this->Line($x, $y + $h, $x + $w, $y + $h, $border_style['B']);
  11197. }
  11198. }
  11199. }
  11200. /**
  11201. * Draws a Bezier curve.
  11202. * The Bezier curve is a tangent to the line between the control points at
  11203. * either end of the curve.
  11204. * @param float $x0 Abscissa of start point.
  11205. * @param float $y0 Ordinate of start point.
  11206. * @param float $x1 Abscissa of control point 1.
  11207. * @param float $y1 Ordinate of control point 1.
  11208. * @param float $x2 Abscissa of control point 2.
  11209. * @param float $y2 Ordinate of control point 2.
  11210. * @param float $x3 Abscissa of end point.
  11211. * @param float $y3 Ordinate of end point.
  11212. * @param string $style Style of rendering. See the getPathPaintOperator() function for more information.
  11213. * @param array $line_style Line style of curve. Array like for SetLineStyle(). Default value: default line style (empty array).
  11214. * @param array $fill_color Fill color. Format: array(GREY) or array(R,G,B) or array(C,M,Y,K) or array(C,M,Y,K,SpotColorName). Default value: default color (empty array).
  11215. * @public
  11216. * @see SetLineStyle()
  11217. * @since 2.1.000 (2008-01-08)
  11218. */
  11219. public function Curve($x0, $y0, $x1, $y1, $x2, $y2, $x3, $y3, $style='', $line_style=array(), $fill_color=array()) {
  11220. if ($this->state != 2) {
  11221. return;
  11222. }
  11223. if (!(false === strpos($style, 'F')) AND is_array($fill_color)) {
  11224. $this->setFillColorArray($fill_color);
  11225. }
  11226. $op = TCPDF_STATIC::getPathPaintOperator($style);
  11227. if ($line_style) {
  11228. $this->setLineStyle($line_style);
  11229. }
  11230. $this->_outPoint($x0, $y0);
  11231. $this->_outCurve($x1, $y1, $x2, $y2, $x3, $y3);
  11232. $this->_out($op);
  11233. }
  11234. /**
  11235. * Draws a poly-Bezier curve.
  11236. * Each Bezier curve segment is a tangent to the line between the control points at
  11237. * either end of the curve.
  11238. * @param float $x0 Abscissa of start point.
  11239. * @param float $y0 Ordinate of start point.
  11240. * @param float[] $segments An array of bezier descriptions. Format: array(x1, y1, x2, y2, x3, y3).
  11241. * @param string $style Style of rendering. See the getPathPaintOperator() function for more information.
  11242. * @param array $line_style Line style of curve. Array like for SetLineStyle(). Default value: default line style (empty array).
  11243. * @param array $fill_color Fill color. Format: array(GREY) or array(R,G,B) or array(C,M,Y,K) or array(C,M,Y,K,SpotColorName). Default value: default color (empty array).
  11244. * @public
  11245. * @see SetLineStyle()
  11246. * @since 3.0008 (2008-05-12)
  11247. */
  11248. public function Polycurve($x0, $y0, $segments, $style='', $line_style=array(), $fill_color=array()) {
  11249. if ($this->state != 2) {
  11250. return;
  11251. }
  11252. if (!(false === strpos($style, 'F')) AND is_array($fill_color)) {
  11253. $this->setFillColorArray($fill_color);
  11254. }
  11255. $op = TCPDF_STATIC::getPathPaintOperator($style);
  11256. if ($op == 'f') {
  11257. $line_style = array();
  11258. }
  11259. if ($line_style) {
  11260. $this->setLineStyle($line_style);
  11261. }
  11262. $this->_outPoint($x0, $y0);
  11263. foreach ($segments as $segment) {
  11264. list($x1, $y1, $x2, $y2, $x3, $y3) = $segment;
  11265. $this->_outCurve($x1, $y1, $x2, $y2, $x3, $y3);
  11266. }
  11267. $this->_out($op);
  11268. }
  11269. /**
  11270. * Draws an ellipse.
  11271. * An ellipse is formed from n Bezier curves.
  11272. * @param float $x0 Abscissa of center point.
  11273. * @param float $y0 Ordinate of center point.
  11274. * @param float $rx Horizontal radius.
  11275. * @param float $ry Vertical radius (if ry = 0 then is a circle, see Circle()). Default value: 0.
  11276. * @param float $angle Angle oriented (anti-clockwise). Default value: 0.
  11277. * @param float $astart Angle start of draw line. Default value: 0.
  11278. * @param float $afinish Angle finish of draw line. Default value: 360.
  11279. * @param string $style Style of rendering. See the getPathPaintOperator() function for more information.
  11280. * @param array $line_style Line style of ellipse. Array like for SetLineStyle(). Default value: default line style (empty array).
  11281. * @param array $fill_color Fill color. Format: array(GREY) or array(R,G,B) or array(C,M,Y,K) or array(C,M,Y,K,SpotColorName). Default value: default color (empty array).
  11282. * @param integer $nc Number of curves used to draw a 90 degrees portion of ellipse.
  11283. * @author Nicola Asuni
  11284. * @public
  11285. * @since 2.1.000 (2008-01-08)
  11286. */
  11287. public function Ellipse($x0, $y0, $rx, $ry=0, $angle=0, $astart=0, $afinish=360, $style='', $line_style=array(), $fill_color=array(), $nc=2) {
  11288. if ($this->state != 2) {
  11289. return;
  11290. }
  11291. if (TCPDF_STATIC::empty_string($ry) OR ($ry == 0)) {
  11292. $ry = $rx;
  11293. }
  11294. if (!(false === strpos($style, 'F')) AND is_array($fill_color)) {
  11295. $this->setFillColorArray($fill_color);
  11296. }
  11297. $op = TCPDF_STATIC::getPathPaintOperator($style);
  11298. if ($op == 'f') {
  11299. $line_style = array();
  11300. }
  11301. if ($line_style) {
  11302. $this->setLineStyle($line_style);
  11303. }
  11304. $this->_outellipticalarc($x0, $y0, $rx, $ry, $angle, $astart, $afinish, false, $nc, true, true, false);
  11305. $this->_out($op);
  11306. }
  11307. /**
  11308. * Append an elliptical arc to the current path.
  11309. * An ellipse is formed from n Bezier curves.
  11310. * @param float $xc Abscissa of center point.
  11311. * @param float $yc Ordinate of center point.
  11312. * @param float $rx Horizontal radius.
  11313. * @param float $ry Vertical radius (if ry = 0 then is a circle, see Circle()). Default value: 0.
  11314. * @param float $xang Angle between the X-axis and the major axis of the ellipse. Default value: 0.
  11315. * @param float $angs Angle start of draw line. Default value: 0.
  11316. * @param float $angf Angle finish of draw line. Default value: 360.
  11317. * @param boolean $pie if true do not mark the border point (used to draw pie sectors).
  11318. * @param integer $nc Number of curves used to draw a 90 degrees portion of ellipse.
  11319. * @param boolean $startpoint if true output a starting point.
  11320. * @param boolean $ccw if true draws in counter-clockwise.
  11321. * @param boolean $svg if true the angles are in svg mode (already calculated).
  11322. * @return array bounding box coordinates (x min, y min, x max, y max)
  11323. * @author Nicola Asuni
  11324. * @protected
  11325. * @since 4.9.019 (2010-04-26)
  11326. */
  11327. protected function _outellipticalarc($xc, $yc, $rx, $ry, $xang=0, $angs=0, $angf=360, $pie=false, $nc=2, $startpoint=true, $ccw=true, $svg=false) {
  11328. if (($rx <= 0) OR ($ry < 0)) {
  11329. return;
  11330. }
  11331. $k = $this->k;
  11332. if ($nc < 2) {
  11333. $nc = 2;
  11334. }
  11335. $xmin = 2147483647;
  11336. $ymin = 2147483647;
  11337. $xmax = 0;
  11338. $ymax = 0;
  11339. if ($pie) {
  11340. // center of the arc
  11341. $this->_outPoint($xc, $yc);
  11342. }
  11343. $xang = deg2rad((float) $xang);
  11344. $angs = deg2rad((float) $angs);
  11345. $angf = deg2rad((float) $angf);
  11346. if ($svg) {
  11347. $as = $angs;
  11348. $af = $angf;
  11349. } else {
  11350. $as = atan2((sin($angs) / $ry), (cos($angs) / $rx));
  11351. $af = atan2((sin($angf) / $ry), (cos($angf) / $rx));
  11352. }
  11353. if ($as < 0) {
  11354. $as += (2 * M_PI);
  11355. }
  11356. if ($af < 0) {
  11357. $af += (2 * M_PI);
  11358. }
  11359. if ($ccw AND ($as > $af)) {
  11360. // reverse rotation
  11361. $as -= (2 * M_PI);
  11362. } elseif (!$ccw AND ($as < $af)) {
  11363. // reverse rotation
  11364. $af -= (2 * M_PI);
  11365. }
  11366. $total_angle = ($af - $as);
  11367. if ($nc < 2) {
  11368. $nc = 2;
  11369. }
  11370. // total arcs to draw
  11371. $nc *= (2 * abs($total_angle) / M_PI);
  11372. $nc = round($nc) + 1;
  11373. // angle of each arc
  11374. $arcang = ($total_angle / $nc);
  11375. // center point in PDF coordinates
  11376. $x0 = $xc;
  11377. $y0 = ($this->h - $yc);
  11378. // starting angle
  11379. $ang = $as;
  11380. $alpha = sin($arcang) * ((sqrt(4 + (3 * pow(tan(($arcang) / 2), 2))) - 1) / 3);
  11381. $cos_xang = cos($xang);
  11382. $sin_xang = sin($xang);
  11383. $cos_ang = cos($ang);
  11384. $sin_ang = sin($ang);
  11385. // first arc point
  11386. $px1 = $x0 + ($rx * $cos_xang * $cos_ang) - ($ry * $sin_xang * $sin_ang);
  11387. $py1 = $y0 + ($rx * $sin_xang * $cos_ang) + ($ry * $cos_xang * $sin_ang);
  11388. // first Bezier control point
  11389. $qx1 = ($alpha * ((-$rx * $cos_xang * $sin_ang) - ($ry * $sin_xang * $cos_ang)));
  11390. $qy1 = ($alpha * ((-$rx * $sin_xang * $sin_ang) + ($ry * $cos_xang * $cos_ang)));
  11391. if ($pie) {
  11392. // line from center to arc starting point
  11393. $this->_outLine($px1, $this->h - $py1);
  11394. } elseif ($startpoint) {
  11395. // arc starting point
  11396. $this->_outPoint($px1, $this->h - $py1);
  11397. }
  11398. // draw arcs
  11399. for ($i = 1; $i <= $nc; ++$i) {
  11400. // starting angle
  11401. $ang = $as + ($i * $arcang);
  11402. if ($i == $nc) {
  11403. $ang = $af;
  11404. }
  11405. $cos_ang = cos($ang);
  11406. $sin_ang = sin($ang);
  11407. // second arc point
  11408. $px2 = $x0 + ($rx * $cos_xang * $cos_ang) - ($ry * $sin_xang * $sin_ang);
  11409. $py2 = $y0 + ($rx * $sin_xang * $cos_ang) + ($ry * $cos_xang * $sin_ang);
  11410. // second Bezier control point
  11411. $qx2 = ($alpha * ((-$rx * $cos_xang * $sin_ang) - ($ry * $sin_xang * $cos_ang)));
  11412. $qy2 = ($alpha * ((-$rx * $sin_xang * $sin_ang) + ($ry * $cos_xang * $cos_ang)));
  11413. // draw arc
  11414. $cx1 = ($px1 + $qx1);
  11415. $cy1 = ($this->h - ($py1 + $qy1));
  11416. $cx2 = ($px2 - $qx2);
  11417. $cy2 = ($this->h - ($py2 - $qy2));
  11418. $cx3 = $px2;
  11419. $cy3 = ($this->h - $py2);
  11420. $this->_outCurve($cx1, $cy1, $cx2, $cy2, $cx3, $cy3);
  11421. // get bounding box coordinates
  11422. $xmin = min($xmin, $cx1, $cx2, $cx3);
  11423. $ymin = min($ymin, $cy1, $cy2, $cy3);
  11424. $xmax = max($xmax, $cx1, $cx2, $cx3);
  11425. $ymax = max($ymax, $cy1, $cy2, $cy3);
  11426. // move to next point
  11427. $px1 = $px2;
  11428. $py1 = $py2;
  11429. $qx1 = $qx2;
  11430. $qy1 = $qy2;
  11431. }
  11432. if ($pie) {
  11433. $this->_outLine($xc, $yc);
  11434. // get bounding box coordinates
  11435. $xmin = min($xmin, $xc);
  11436. $ymin = min($ymin, $yc);
  11437. $xmax = max($xmax, $xc);
  11438. $ymax = max($ymax, $yc);
  11439. }
  11440. return array($xmin, $ymin, $xmax, $ymax);
  11441. }
  11442. /**
  11443. * Draws a circle.
  11444. * A circle is formed from n Bezier curves.
  11445. * @param float $x0 Abscissa of center point.
  11446. * @param float $y0 Ordinate of center point.
  11447. * @param float $r Radius.
  11448. * @param float $angstr Angle start of draw line. Default value: 0.
  11449. * @param float $angend Angle finish of draw line. Default value: 360.
  11450. * @param string $style Style of rendering. See the getPathPaintOperator() function for more information.
  11451. * @param array $line_style Line style of circle. Array like for SetLineStyle(). Default value: default line style (empty array).
  11452. * @param array $fill_color Fill color. Format: array(red, green, blue). Default value: default color (empty array).
  11453. * @param integer $nc Number of curves used to draw a 90 degrees portion of circle.
  11454. * @public
  11455. * @since 2.1.000 (2008-01-08)
  11456. */
  11457. public function Circle($x0, $y0, $r, $angstr=0, $angend=360, $style='', $line_style=array(), $fill_color=array(), $nc=2) {
  11458. $this->Ellipse($x0, $y0, $r, $r, 0, $angstr, $angend, $style, $line_style, $fill_color, $nc);
  11459. }
  11460. /**
  11461. * Draws a polygonal line
  11462. * @param array $p Points 0 to ($np - 1). Array with values (x0, y0, x1, y1,..., x(np-1), y(np - 1))
  11463. * @param string $style Style of rendering. See the getPathPaintOperator() function for more information.
  11464. * @param array $line_style Line style of polygon. Array with keys among the following:
  11465. * <ul>
  11466. * <li>all: Line style of all lines. Array like for SetLineStyle().</li>
  11467. * <li>0 to ($np - 1): Line style of each line. Array like for SetLineStyle().</li>
  11468. * </ul>
  11469. * If a key is not present or is null, not draws the line. Default value is default line style (empty array).
  11470. * @param array $fill_color Fill color. Format: array(GREY) or array(R,G,B) or array(C,M,Y,K) or array(C,M,Y,K,SpotColorName). Default value: default color (empty array).
  11471. * @since 4.8.003 (2009-09-15)
  11472. * @public
  11473. */
  11474. public function PolyLine($p, $style='', $line_style=array(), $fill_color=array()) {
  11475. $this->Polygon($p, $style, $line_style, $fill_color, false);
  11476. }
  11477. /**
  11478. * Draws a polygon.
  11479. * @param array $p Points 0 to ($np - 1). Array with values (x0, y0, x1, y1,..., x(np-1), y(np - 1))
  11480. * @param string $style Style of rendering. See the getPathPaintOperator() function for more information.
  11481. * @param array $line_style Line style of polygon. Array with keys among the following:
  11482. * <ul>
  11483. * <li>all: Line style of all lines. Array like for SetLineStyle().</li>
  11484. * <li>0 to ($np - 1): Line style of each line. Array like for SetLineStyle().</li>
  11485. * </ul>
  11486. * If a key is not present or is null, not draws the line. Default value is default line style (empty array).
  11487. * @param array $fill_color Fill color. Format: array(GREY) or array(R,G,B) or array(C,M,Y,K) or array(C,M,Y,K,SpotColorName). Default value: default color (empty array).
  11488. * @param boolean $closed if true the polygon is closes, otherwise will remain open
  11489. * @public
  11490. * @since 2.1.000 (2008-01-08)
  11491. */
  11492. public function Polygon($p, $style='', $line_style=array(), $fill_color=array(), $closed=true) {
  11493. if ($this->state != 2) {
  11494. return;
  11495. }
  11496. $nc = count($p); // number of coordinates
  11497. $np = $nc / 2; // number of points
  11498. if ($closed) {
  11499. // close polygon by adding the first 2 points at the end (one line)
  11500. for ($i = 0; $i < 4; ++$i) {
  11501. $p[$nc + $i] = $p[$i];
  11502. }
  11503. // copy style for the last added line
  11504. if (isset($line_style[0])) {
  11505. $line_style[$np] = $line_style[0];
  11506. }
  11507. $nc += 4;
  11508. }
  11509. if (!(false === strpos($style, 'F')) AND is_array($fill_color)) {
  11510. $this->setFillColorArray($fill_color);
  11511. }
  11512. $op = TCPDF_STATIC::getPathPaintOperator($style);
  11513. if ($op == 'f') {
  11514. $line_style = array();
  11515. }
  11516. $draw = true;
  11517. if ($line_style) {
  11518. if (isset($line_style['all'])) {
  11519. $this->setLineStyle($line_style['all']);
  11520. } else {
  11521. $draw = false;
  11522. if ($op == 'B') {
  11523. // draw fill
  11524. $op = 'f';
  11525. $this->_outPoint($p[0], $p[1]);
  11526. for ($i = 2; $i < $nc; $i = $i + 2) {
  11527. $this->_outLine($p[$i], $p[$i + 1]);
  11528. }
  11529. $this->_out($op);
  11530. }
  11531. // draw outline
  11532. $this->_outPoint($p[0], $p[1]);
  11533. for ($i = 2; $i < $nc; $i = $i + 2) {
  11534. $line_num = ($i / 2) - 1;
  11535. if (isset($line_style[$line_num])) {
  11536. if ($line_style[$line_num] != 0) {
  11537. if (is_array($line_style[$line_num])) {
  11538. $this->_out('S');
  11539. $this->setLineStyle($line_style[$line_num]);
  11540. $this->_outPoint($p[$i - 2], $p[$i - 1]);
  11541. $this->_outLine($p[$i], $p[$i + 1]);
  11542. $this->_out('S');
  11543. $this->_outPoint($p[$i], $p[$i + 1]);
  11544. } else {
  11545. $this->_outLine($p[$i], $p[$i + 1]);
  11546. }
  11547. }
  11548. } else {
  11549. $this->_outLine($p[$i], $p[$i + 1]);
  11550. }
  11551. }
  11552. $this->_out($op);
  11553. }
  11554. }
  11555. if ($draw) {
  11556. $this->_outPoint($p[0], $p[1]);
  11557. for ($i = 2; $i < $nc; $i = $i + 2) {
  11558. $this->_outLine($p[$i], $p[$i + 1]);
  11559. }
  11560. $this->_out($op);
  11561. }
  11562. }
  11563. /**
  11564. * Draws a regular polygon.
  11565. * @param float $x0 Abscissa of center point.
  11566. * @param float $y0 Ordinate of center point.
  11567. * @param float $r Radius of inscribed circle.
  11568. * @param integer $ns Number of sides.
  11569. * @param float $angle Angle oriented (anti-clockwise). Default value: 0.
  11570. * @param boolean $draw_circle Draw inscribed circle or not. Default value: false.
  11571. * @param string $style Style of rendering. See the getPathPaintOperator() function for more information.
  11572. * @param array $line_style Line style of polygon sides. Array with keys among the following:
  11573. * <ul>
  11574. * <li>all: Line style of all sides. Array like for SetLineStyle().</li>
  11575. * <li>0 to ($ns - 1): Line style of each side. Array like for SetLineStyle().</li>
  11576. * </ul>
  11577. * If a key is not present or is null, not draws the side. Default value is default line style (empty array).
  11578. * @param array $fill_color Fill color. Format: array(red, green, blue). Default value: default color (empty array).
  11579. * @param string $circle_style Style of rendering of inscribed circle (if draws). Possible values are:
  11580. * <ul>
  11581. * <li>D or empty string: Draw (default).</li>
  11582. * <li>F: Fill.</li>
  11583. * <li>DF or FD: Draw and fill.</li>
  11584. * <li>CNZ: Clipping mode (using the even-odd rule to determine which regions lie inside the clipping path).</li>
  11585. * <li>CEO: Clipping mode (using the nonzero winding number rule to determine which regions lie inside the clipping path).</li>
  11586. * </ul>
  11587. * @param array $circle_outLine_style Line style of inscribed circle (if draws). Array like for SetLineStyle(). Default value: default line style (empty array).
  11588. * @param array $circle_fill_color Fill color of inscribed circle (if draws). Format: array(red, green, blue). Default value: default color (empty array).
  11589. * @public
  11590. * @since 2.1.000 (2008-01-08)
  11591. */
  11592. public function RegularPolygon($x0, $y0, $r, $ns, $angle=0, $draw_circle=false, $style='', $line_style=array(), $fill_color=array(), $circle_style='', $circle_outLine_style=array(), $circle_fill_color=array()) {
  11593. if (3 > $ns) {
  11594. $ns = 3;
  11595. }
  11596. if ($draw_circle) {
  11597. $this->Circle($x0, $y0, $r, 0, 360, $circle_style, $circle_outLine_style, $circle_fill_color);
  11598. }
  11599. $p = array();
  11600. for ($i = 0; $i < $ns; ++$i) {
  11601. $a = $angle + ($i * 360 / $ns);
  11602. $a_rad = deg2rad((float) $a);
  11603. $p[] = $x0 + ($r * sin($a_rad));
  11604. $p[] = $y0 + ($r * cos($a_rad));
  11605. }
  11606. $this->Polygon($p, $style, $line_style, $fill_color);
  11607. }
  11608. /**
  11609. * Draws a star polygon
  11610. * @param float $x0 Abscissa of center point.
  11611. * @param float $y0 Ordinate of center point.
  11612. * @param float $r Radius of inscribed circle.
  11613. * @param integer $nv Number of vertices.
  11614. * @param integer $ng Number of gap (if ($ng % $nv = 1) then is a regular polygon).
  11615. * @param float $angle Angle oriented (anti-clockwise). Default value: 0.
  11616. * @param boolean $draw_circle Draw inscribed circle or not. Default value is false.
  11617. * @param string $style Style of rendering. See the getPathPaintOperator() function for more information.
  11618. * @param array $line_style Line style of polygon sides. Array with keys among the following:
  11619. * <ul>
  11620. * <li>all: Line style of all sides. Array like for
  11621. * SetLineStyle().</li>
  11622. * <li>0 to (n - 1): Line style of each side. Array like for SetLineStyle().</li>
  11623. * </ul>
  11624. * If a key is not present or is null, not draws the side. Default value is default line style (empty array).
  11625. * @param array $fill_color Fill color. Format: array(red, green, blue). Default value: default color (empty array).
  11626. * @param string $circle_style Style of rendering of inscribed circle (if draws). Possible values are:
  11627. * <ul>
  11628. * <li>D or empty string: Draw (default).</li>
  11629. * <li>F: Fill.</li>
  11630. * <li>DF or FD: Draw and fill.</li>
  11631. * <li>CNZ: Clipping mode (using the even-odd rule to determine which regions lie inside the clipping path).</li>
  11632. * <li>CEO: Clipping mode (using the nonzero winding number rule to determine which regions lie inside the clipping path).</li>
  11633. * </ul>
  11634. * @param array $circle_outLine_style Line style of inscribed circle (if draws). Array like for SetLineStyle(). Default value: default line style (empty array).
  11635. * @param array $circle_fill_color Fill color of inscribed circle (if draws). Format: array(red, green, blue). Default value: default color (empty array).
  11636. * @public
  11637. * @since 2.1.000 (2008-01-08)
  11638. */
  11639. public function StarPolygon($x0, $y0, $r, $nv, $ng, $angle=0, $draw_circle=false, $style='', $line_style=array(), $fill_color=array(), $circle_style='', $circle_outLine_style=array(), $circle_fill_color=array()) {
  11640. if ($nv < 2) {
  11641. $nv = 2;
  11642. }
  11643. if ($draw_circle) {
  11644. $this->Circle($x0, $y0, $r, 0, 360, $circle_style, $circle_outLine_style, $circle_fill_color);
  11645. }
  11646. $p2 = array();
  11647. $visited = array();
  11648. for ($i = 0; $i < $nv; ++$i) {
  11649. $a = $angle + ($i * 360 / $nv);
  11650. $a_rad = deg2rad((float) $a);
  11651. $p2[] = $x0 + ($r * sin($a_rad));
  11652. $p2[] = $y0 + ($r * cos($a_rad));
  11653. $visited[] = false;
  11654. }
  11655. $p = array();
  11656. $i = 0;
  11657. do {
  11658. $p[] = $p2[$i * 2];
  11659. $p[] = $p2[($i * 2) + 1];
  11660. $visited[$i] = true;
  11661. $i += $ng;
  11662. $i %= $nv;
  11663. } while (!$visited[$i]);
  11664. $this->Polygon($p, $style, $line_style, $fill_color);
  11665. }
  11666. /**
  11667. * Draws a rounded rectangle.
  11668. * @param float $x Abscissa of upper-left corner.
  11669. * @param float $y Ordinate of upper-left corner.
  11670. * @param float $w Width.
  11671. * @param float $h Height.
  11672. * @param float $r the radius of the circle used to round off the corners of the rectangle.
  11673. * @param string $round_corner Draws rounded corner or not. String with a 0 (not rounded i-corner) or 1 (rounded i-corner) in i-position. Positions are, in order and begin to 0: top right, bottom right, bottom left and top left. Default value: all rounded corner ("1111").
  11674. * @param string $style Style of rendering. See the getPathPaintOperator() function for more information.
  11675. * @param array $border_style Border style of rectangle. Array like for SetLineStyle(). Default value: default line style (empty array).
  11676. * @param array $fill_color Fill color. Format: array(GREY) or array(R,G,B) or array(C,M,Y,K) or array(C,M,Y,K,SpotColorName). Default value: default color (empty array).
  11677. * @public
  11678. * @since 2.1.000 (2008-01-08)
  11679. */
  11680. public function RoundedRect($x, $y, $w, $h, $r, $round_corner='1111', $style='', $border_style=array(), $fill_color=array()) {
  11681. $this->RoundedRectXY($x, $y, $w, $h, $r, $r, $round_corner, $style, $border_style, $fill_color);
  11682. }
  11683. /**
  11684. * Draws a rounded rectangle.
  11685. * @param float $x Abscissa of upper-left corner.
  11686. * @param float $y Ordinate of upper-left corner.
  11687. * @param float $w Width.
  11688. * @param float $h Height.
  11689. * @param float $rx the x-axis radius of the ellipse used to round off the corners of the rectangle.
  11690. * @param float $ry the y-axis radius of the ellipse used to round off the corners of the rectangle.
  11691. * @param string $round_corner Draws rounded corner or not. String with a 0 (not rounded i-corner) or 1 (rounded i-corner) in i-position. Positions are, in order and begin to 0: top right, bottom right, bottom left and top left. Default value: all rounded corner ("1111").
  11692. * @param string $style Style of rendering. See the getPathPaintOperator() function for more information.
  11693. * @param array $border_style Border style of rectangle. Array like for SetLineStyle(). Default value: default line style (empty array).
  11694. * @param array $fill_color Fill color. Format: array(GREY) or array(R,G,B) or array(C,M,Y,K) or array(C,M,Y,K,SpotColorName). Default value: default color (empty array).
  11695. * @public
  11696. * @since 4.9.019 (2010-04-22)
  11697. */
  11698. public function RoundedRectXY($x, $y, $w, $h, $rx, $ry, $round_corner='1111', $style='', $border_style=array(), $fill_color=array()) {
  11699. if ($this->state != 2) {
  11700. return;
  11701. }
  11702. if (($round_corner == '0000') OR (($rx == $ry) AND ($rx == 0))) {
  11703. // Not rounded
  11704. $this->Rect($x, $y, $w, $h, $style, $border_style, $fill_color);
  11705. return;
  11706. }
  11707. // Rounded
  11708. if (!(false === strpos($style, 'F')) AND is_array($fill_color)) {
  11709. $this->setFillColorArray($fill_color);
  11710. }
  11711. $op = TCPDF_STATIC::getPathPaintOperator($style);
  11712. if ($op == 'f') {
  11713. $border_style = array();
  11714. }
  11715. if ($border_style) {
  11716. $this->setLineStyle($border_style);
  11717. }
  11718. $MyArc = 4 / 3 * (sqrt(2) - 1);
  11719. $this->_outPoint($x + $rx, $y);
  11720. $xc = $x + $w - $rx;
  11721. $yc = $y + $ry;
  11722. $this->_outLine($xc, $y);
  11723. if ($round_corner[0]) {
  11724. $this->_outCurve($xc + ($rx * $MyArc), $yc - $ry, $xc + $rx, $yc - ($ry * $MyArc), $xc + $rx, $yc);
  11725. } else {
  11726. $this->_outLine($x + $w, $y);
  11727. }
  11728. $xc = $x + $w - $rx;
  11729. $yc = $y + $h - $ry;
  11730. $this->_outLine($x + $w, $yc);
  11731. if ($round_corner[1]) {
  11732. $this->_outCurve($xc + $rx, $yc + ($ry * $MyArc), $xc + ($rx * $MyArc), $yc + $ry, $xc, $yc + $ry);
  11733. } else {
  11734. $this->_outLine($x + $w, $y + $h);
  11735. }
  11736. $xc = $x + $rx;
  11737. $yc = $y + $h - $ry;
  11738. $this->_outLine($xc, $y + $h);
  11739. if ($round_corner[2]) {
  11740. $this->_outCurve($xc - ($rx * $MyArc), $yc + $ry, $xc - $rx, $yc + ($ry * $MyArc), $xc - $rx, $yc);
  11741. } else {
  11742. $this->_outLine($x, $y + $h);
  11743. }
  11744. $xc = $x + $rx;
  11745. $yc = $y + $ry;
  11746. $this->_outLine($x, $yc);
  11747. if ($round_corner[3]) {
  11748. $this->_outCurve($xc - $rx, $yc - ($ry * $MyArc), $xc - ($rx * $MyArc), $yc - $ry, $xc, $yc - $ry);
  11749. } else {
  11750. $this->_outLine($x, $y);
  11751. $this->_outLine($x + $rx, $y);
  11752. }
  11753. $this->_out($op);
  11754. }
  11755. /**
  11756. * Draws a grahic arrow.
  11757. * @param float $x0 Abscissa of first point.
  11758. * @param float $y0 Ordinate of first point.
  11759. * @param float $x1 Abscissa of second point.
  11760. * @param float $y1 Ordinate of second point.
  11761. * @param int $head_style (0 = draw only arrowhead arms, 1 = draw closed arrowhead, but no fill, 2 = closed and filled arrowhead, 3 = filled arrowhead)
  11762. * @param float $arm_size length of arrowhead arms
  11763. * @param int $arm_angle angle between an arm and the shaft
  11764. * @author Piotr Galecki, Nicola Asuni, Andy Meier
  11765. * @since 4.6.018 (2009-07-10)
  11766. */
  11767. public function Arrow($x0, $y0, $x1, $y1, $head_style=0, $arm_size=5, $arm_angle=15) {
  11768. // getting arrow direction angle
  11769. // 0 deg angle is when both arms go along X axis. angle grows clockwise.
  11770. $dir_angle = atan2(($y0 - $y1), ($x0 - $x1));
  11771. if ($dir_angle < 0) {
  11772. $dir_angle += (2 * M_PI);
  11773. }
  11774. $arm_angle = deg2rad($arm_angle);
  11775. $sx1 = $x1;
  11776. $sy1 = $y1;
  11777. if ($head_style > 0) {
  11778. // calculate the stopping point for the arrow shaft
  11779. $sx1 = $x1 + (($arm_size - $this->LineWidth) * cos($dir_angle));
  11780. $sy1 = $y1 + (($arm_size - $this->LineWidth) * sin($dir_angle));
  11781. }
  11782. // main arrow line / shaft
  11783. $this->Line($x0, $y0, $sx1, $sy1);
  11784. // left arrowhead arm tip
  11785. $x2L = $x1 + ($arm_size * cos($dir_angle + $arm_angle));
  11786. $y2L = $y1 + ($arm_size * sin($dir_angle + $arm_angle));
  11787. // right arrowhead arm tip
  11788. $x2R = $x1 + ($arm_size * cos($dir_angle - $arm_angle));
  11789. $y2R = $y1 + ($arm_size * sin($dir_angle - $arm_angle));
  11790. $mode = 'D';
  11791. $style = array();
  11792. switch ($head_style) {
  11793. case 0: {
  11794. // draw only arrowhead arms
  11795. $mode = 'D';
  11796. $style = array(1, 1, 0);
  11797. break;
  11798. }
  11799. case 1: {
  11800. // draw closed arrowhead, but no fill
  11801. $mode = 'D';
  11802. break;
  11803. }
  11804. case 2: {
  11805. // closed and filled arrowhead
  11806. $mode = 'DF';
  11807. break;
  11808. }
  11809. case 3: {
  11810. // filled arrowhead
  11811. $mode = 'F';
  11812. break;
  11813. }
  11814. }
  11815. $this->Polygon(array($x2L, $y2L, $x1, $y1, $x2R, $y2R), $mode, $style, array());
  11816. }
  11817. // END GRAPHIC FUNCTIONS SECTION -----------------------
  11818. /**
  11819. * Add a Named Destination.
  11820. * NOTE: destination names are unique, so only last entry will be saved.
  11821. * @param string $name Destination name.
  11822. * @param float $y Y position in user units of the destiantion on the selected page (default = -1 = current position; 0 = page start;).
  11823. * @param int|string $page Target page number (leave empty for current page). If you prefix a page number with the * character, then this page will not be changed when adding/deleting/moving pages.
  11824. * @param float $x X position in user units of the destiantion on the selected page (default = -1 = current position;).
  11825. * @return string|false Stripped named destination identifier or false in case of error.
  11826. * @public
  11827. * @author Christian Deligant, Nicola Asuni
  11828. * @since 5.9.097 (2011-06-23)
  11829. */
  11830. public function setDestination($name, $y=-1, $page='', $x=-1) {
  11831. // remove unsupported characters
  11832. $name = TCPDF_STATIC::encodeNameObject($name);
  11833. if (TCPDF_STATIC::empty_string($name)) {
  11834. return false;
  11835. }
  11836. if ($y == -1) {
  11837. $y = $this->GetY();
  11838. } elseif ($y < 0) {
  11839. $y = 0;
  11840. } elseif ($y > $this->h) {
  11841. $y = $this->h;
  11842. }
  11843. if ($x == -1) {
  11844. $x = $this->GetX();
  11845. } elseif ($x < 0) {
  11846. $x = 0;
  11847. } elseif ($x > $this->w) {
  11848. $x = $this->w;
  11849. }
  11850. $fixed = false;
  11851. if (!empty($page) AND (substr($page, 0, 1) == '*')) {
  11852. $page = intval(substr($page, 1));
  11853. // this page number will not be changed when moving/add/deleting pages
  11854. $fixed = true;
  11855. }
  11856. if (empty($page)) {
  11857. $page = $this->PageNo();
  11858. if (empty($page)) {
  11859. return;
  11860. }
  11861. }
  11862. $this->dests[$name] = array('x' => $x, 'y' => $y, 'p' => $page, 'f' => $fixed);
  11863. return $name;
  11864. }
  11865. /**
  11866. * Return the Named Destination array.
  11867. * @return array Named Destination array.
  11868. * @public
  11869. * @author Nicola Asuni
  11870. * @since 5.9.097 (2011-06-23)
  11871. */
  11872. public function getDestination() {
  11873. return $this->dests;
  11874. }
  11875. /**
  11876. * Insert Named Destinations.
  11877. * @protected
  11878. * @author Johannes G\FCntert, Nicola Asuni
  11879. * @since 5.9.098 (2011-06-23)
  11880. */
  11881. protected function _putdests() {
  11882. if (empty($this->dests)) {
  11883. return;
  11884. }
  11885. $this->n_dests = $this->_newobj();
  11886. $out = ' <<';
  11887. foreach($this->dests as $name => $o) {
  11888. $out .= ' /'.$name.' '.sprintf('[%u 0 R /XYZ %F %F null]', $this->page_obj_id[($o['p'])], ($o['x'] * $this->k), ($this->pagedim[$o['p']]['h'] - ($o['y'] * $this->k)));
  11889. }
  11890. $out .= ' >>';
  11891. $out .= "\n".'endobj';
  11892. $this->_out($out);
  11893. }
  11894. /**
  11895. * Adds a bookmark - alias for Bookmark().
  11896. * @param string $txt Bookmark description.
  11897. * @param int $level Bookmark level (minimum value is 0).
  11898. * @param float $y Y position in user units of the bookmark on the selected page (default = -1 = current position; 0 = page start;).
  11899. * @param int|string $page Target page number (leave empty for current page). If you prefix a page number with the * character, then this page will not be changed when adding/deleting/moving pages.
  11900. * @param string $style Font style: B = Bold, I = Italic, BI = Bold + Italic.
  11901. * @param array $color RGB color array (values from 0 to 255).
  11902. * @param float $x X position in user units of the bookmark on the selected page (default = -1 = current position;).
  11903. * @param mixed $link URL, or numerical link ID, or named destination (# character followed by the destination name), or embedded file (* character followed by the file name).
  11904. * @public
  11905. */
  11906. public function setBookmark($txt, $level=0, $y=-1, $page='', $style='', $color=array(0,0,0), $x=-1, $link='') {
  11907. $this->Bookmark($txt, $level, $y, $page, $style, $color, $x, $link);
  11908. }
  11909. /**
  11910. * Adds a bookmark.
  11911. * @param string $txt Bookmark description.
  11912. * @param int $level Bookmark level (minimum value is 0).
  11913. * @param float $y Y position in user units of the bookmark on the selected page (default = -1 = current position; 0 = page start;).
  11914. * @param int|string $page Target page number (leave empty for current page). If you prefix a page number with the * character, then this page will not be changed when adding/deleting/moving pages.
  11915. * @param string $style Font style: B = Bold, I = Italic, BI = Bold + Italic.
  11916. * @param array $color RGB color array (values from 0 to 255).
  11917. * @param float $x X position in user units of the bookmark on the selected page (default = -1 = current position;).
  11918. * @param mixed $link URL, or numerical link ID, or named destination (# character followed by the destination name), or embedded file (* character followed by the file name).
  11919. * @public
  11920. * @since 2.1.002 (2008-02-12)
  11921. */
  11922. public function Bookmark($txt, $level=0, $y=-1, $page='', $style='', $color=array(0,0,0), $x=-1, $link='') {
  11923. if ($level < 0) {
  11924. $level = 0;
  11925. }
  11926. if (isset($this->outlines[0])) {
  11927. $lastoutline = end($this->outlines);
  11928. $maxlevel = $lastoutline['l'] + 1;
  11929. } else {
  11930. $maxlevel = 0;
  11931. }
  11932. if ($level > $maxlevel) {
  11933. $level = $maxlevel;
  11934. }
  11935. if ($y == -1) {
  11936. $y = $this->GetY();
  11937. } elseif ($y < 0) {
  11938. $y = 0;
  11939. } elseif ($y > $this->h) {
  11940. $y = $this->h;
  11941. }
  11942. if ($x == -1) {
  11943. $x = $this->GetX();
  11944. } elseif ($x < 0) {
  11945. $x = 0;
  11946. } elseif ($x > $this->w) {
  11947. $x = $this->w;
  11948. }
  11949. $fixed = false;
  11950. $pageAsString = (string) $page;
  11951. if ($pageAsString && $pageAsString[0] == '*') {
  11952. $page = intval(substr($page, 1));
  11953. // this page number will not be changed when moving/add/deleting pages
  11954. $fixed = true;
  11955. }
  11956. if (empty($page)) {
  11957. $page = $this->PageNo();
  11958. if (empty($page)) {
  11959. return;
  11960. }
  11961. }
  11962. $this->outlines[] = array('t' => $txt, 'l' => $level, 'x' => $x, 'y' => $y, 'p' => $page, 'f' => $fixed, 's' => strtoupper($style), 'c' => $color, 'u' => $link);
  11963. }
  11964. /**
  11965. * Sort bookmarks for page and key.
  11966. * @protected
  11967. * @since 5.9.119 (2011-09-19)
  11968. */
  11969. protected function sortBookmarks() {
  11970. // get sorting columns
  11971. $outline_p = array();
  11972. $outline_y = array();
  11973. foreach ($this->outlines as $key => $row) {
  11974. $outline_p[$key] = $row['p'];
  11975. $outline_k[$key] = $key;
  11976. }
  11977. // sort outlines by page and original position
  11978. array_multisort($outline_p, SORT_NUMERIC, SORT_ASC, $outline_k, SORT_NUMERIC, SORT_ASC, $this->outlines);
  11979. }
  11980. /**
  11981. * Create a bookmark PDF string.
  11982. * @protected
  11983. * @author Olivier Plathey, Nicola Asuni
  11984. * @since 2.1.002 (2008-02-12)
  11985. */
  11986. protected function _putbookmarks() {
  11987. $nb = count($this->outlines);
  11988. if ($nb == 0) {
  11989. return;
  11990. }
  11991. // sort bookmarks
  11992. $this->sortBookmarks();
  11993. $lru = array();
  11994. $level = 0;
  11995. foreach ($this->outlines as $i => $o) {
  11996. if ($o['l'] > 0) {
  11997. $parent = $lru[($o['l'] - 1)];
  11998. //Set parent and last pointers
  11999. $this->outlines[$i]['parent'] = $parent;
  12000. $this->outlines[$parent]['last'] = $i;
  12001. if ($o['l'] > $level) {
  12002. //Level increasing: set first pointer
  12003. $this->outlines[$parent]['first'] = $i;
  12004. }
  12005. } else {
  12006. $this->outlines[$i]['parent'] = $nb;
  12007. }
  12008. if (($o['l'] <= $level) AND ($i > 0)) {
  12009. //Set prev and next pointers
  12010. $prev = $lru[$o['l']];
  12011. $this->outlines[$prev]['next'] = $i;
  12012. $this->outlines[$i]['prev'] = $prev;
  12013. }
  12014. $lru[$o['l']] = $i;
  12015. $level = $o['l'];
  12016. }
  12017. //Outline items
  12018. $n = $this->n + 1;
  12019. $nltags = '/<br[\s]?\/>|<\/(blockquote|dd|dl|div|dt|h1|h2|h3|h4|h5|h6|hr|li|ol|p|pre|ul|tcpdf|table|tr|td)>/si';
  12020. foreach ($this->outlines as $i => $o) {
  12021. $oid = $this->_newobj();
  12022. // covert HTML title to string
  12023. $title = preg_replace($nltags, "\n", $o['t']);
  12024. $title = preg_replace("/[\r]+/si", '', $title);
  12025. $title = preg_replace("/[\n]+/si", "\n", $title);
  12026. $title = strip_tags($title);
  12027. $title = $this->stringTrim($title);
  12028. $out = '<</Title '.$this->_textstring($title, $oid);
  12029. $out .= ' /Parent '.($n + $o['parent']).' 0 R';
  12030. if (isset($o['prev'])) {
  12031. $out .= ' /Prev '.($n + $o['prev']).' 0 R';
  12032. }
  12033. if (isset($o['next'])) {
  12034. $out .= ' /Next '.($n + $o['next']).' 0 R';
  12035. }
  12036. if (isset($o['first'])) {
  12037. $out .= ' /First '.($n + $o['first']).' 0 R';
  12038. }
  12039. if (isset($o['last'])) {
  12040. $out .= ' /Last '.($n + $o['last']).' 0 R';
  12041. }
  12042. if (isset($o['u']) AND !empty($o['u'])) {
  12043. // link
  12044. if (is_string($o['u'])) {
  12045. if ($o['u'][0] == '#') {
  12046. // internal destination
  12047. $out .= ' /Dest /'.TCPDF_STATIC::encodeNameObject(substr($o['u'], 1));
  12048. } elseif ($o['u'][0] == '%') {
  12049. // embedded PDF file
  12050. $filename = basename(substr($o['u'], 1));
  12051. $out .= ' /A <</S /GoToE /D [0 /Fit] /NewWindow true /T << /R /C /P '.($o['p'] - 1).' /A '.$this->embeddedfiles[$filename]['a'].' >> >>';
  12052. } elseif ($o['u'][0] == '*') {
  12053. // embedded generic file
  12054. $filename = basename(substr($o['u'], 1));
  12055. $jsa = 'var D=event.target.doc;var MyData=D.dataObjects;for (var i in MyData) if (MyData[i].path=="'.$filename.'") D.exportDataObject( { cName : MyData[i].name, nLaunch : 2});';
  12056. $out .= ' /A <</S /JavaScript /JS '.$this->_textstring($jsa, $oid).'>>';
  12057. } else {
  12058. // external URI link
  12059. $out .= ' /A <</S /URI /URI '.$this->_datastring($this->unhtmlentities($o['u']), $oid).'>>';
  12060. }
  12061. } elseif (isset($this->links[$o['u']])) {
  12062. // internal link ID
  12063. $l = $this->links[$o['u']];
  12064. if (isset($this->page_obj_id[($l['p'])])) {
  12065. $out .= sprintf(' /Dest [%u 0 R /XYZ 0 %F null]', $this->page_obj_id[($l['p'])], ($this->pagedim[$l['p']]['h'] - ($l['y'] * $this->k)));
  12066. }
  12067. }
  12068. } elseif (isset($this->page_obj_id[($o['p'])])) {
  12069. // link to a page
  12070. $out .= ' '.sprintf('/Dest [%u 0 R /XYZ %F %F null]', $this->page_obj_id[($o['p'])], ($o['x'] * $this->k), ($this->pagedim[$o['p']]['h'] - ($o['y'] * $this->k)));
  12071. }
  12072. // set font style
  12073. $style = 0;
  12074. if (!empty($o['s'])) {
  12075. // bold
  12076. if (strpos($o['s'], 'B') !== false) {
  12077. $style |= 2;
  12078. }
  12079. // oblique
  12080. if (strpos($o['s'], 'I') !== false) {
  12081. $style |= 1;
  12082. }
  12083. }
  12084. $out .= sprintf(' /F %d', $style);
  12085. // set bookmark color
  12086. if (isset($o['c']) AND is_array($o['c']) AND (count($o['c']) == 3)) {
  12087. $color = array_values($o['c']);
  12088. $out .= sprintf(' /C [%F %F %F]', ($color[0] / 255), ($color[1] / 255), ($color[2] / 255));
  12089. } else {
  12090. // black
  12091. $out .= ' /C [0.0 0.0 0.0]';
  12092. }
  12093. $out .= ' /Count 0'; // normally closed item
  12094. $out .= ' >>';
  12095. $out .= "\n".'endobj';
  12096. $this->_out($out);
  12097. }
  12098. //Outline root
  12099. $this->OutlineRoot = $this->_newobj();
  12100. $this->_out('<< /Type /Outlines /First '.$n.' 0 R /Last '.($n + $lru[0]).' 0 R >>'."\n".'endobj');
  12101. }
  12102. // --- JAVASCRIPT ------------------------------------------------------
  12103. /**
  12104. * Adds a javascript
  12105. * @param string $script Javascript code
  12106. * @public
  12107. * @author Johannes G\FCntert, Nicola Asuni
  12108. * @since 2.1.002 (2008-02-12)
  12109. */
  12110. public function IncludeJS($script) {
  12111. $this->javascript .= $script;
  12112. }
  12113. /**
  12114. * Adds a javascript object and return object ID
  12115. * @param string $script Javascript code
  12116. * @param boolean $onload if true executes this object when opening the document
  12117. * @return int internal object ID
  12118. * @public
  12119. * @author Nicola Asuni
  12120. * @since 4.8.000 (2009-09-07)
  12121. */
  12122. public function addJavascriptObject($script, $onload=false) {
  12123. if ($this->pdfa_mode) {
  12124. // javascript is not allowed in PDF/A mode
  12125. return false;
  12126. }
  12127. ++$this->n;
  12128. $this->js_objects[$this->n] = array('n' => $this->n, 'js' => $script, 'onload' => $onload);
  12129. return $this->n;
  12130. }
  12131. /**
  12132. * Create a javascript PDF string.
  12133. * @protected
  12134. * @author Johannes G\FCntert, Nicola Asuni
  12135. * @since 2.1.002 (2008-02-12)
  12136. */
  12137. protected function _putjavascript() {
  12138. if ($this->pdfa_mode OR (empty($this->javascript) AND empty($this->js_objects))) {
  12139. return;
  12140. }
  12141. if (strpos($this->javascript, 'this.addField') > 0) {
  12142. if (!$this->ur['enabled']) {
  12143. //$this->setUserRights();
  12144. }
  12145. // the following two lines are used to avoid form fields duplication after saving
  12146. // The addField method only works when releasing user rights (UR3)
  12147. $jsa = sprintf("ftcpdfdocsaved=this.addField('%s','%s',%d,[%F,%F,%F,%F]);", 'tcpdfdocsaved', 'text', 0, 0, 1, 0, 1);
  12148. $jsb = "getField('tcpdfdocsaved').value='saved';";
  12149. $this->javascript = $jsa."\n".$this->javascript."\n".$jsb;
  12150. }
  12151. // name tree for javascript
  12152. $this->n_js = '<< /Names [';
  12153. if (!empty($this->javascript)) {
  12154. $this->n_js .= ' (EmbeddedJS) '.($this->n + 1).' 0 R';
  12155. }
  12156. if (!empty($this->js_objects)) {
  12157. foreach ($this->js_objects as $key => $val) {
  12158. if ($val['onload']) {
  12159. $this->n_js .= ' (JS'.$key.') '.$key.' 0 R';
  12160. }
  12161. }
  12162. }
  12163. $this->n_js .= ' ] >>';
  12164. // default Javascript object
  12165. if (!empty($this->javascript)) {
  12166. $obj_id = $this->_newobj();
  12167. $out = '<< /S /JavaScript';
  12168. $out .= ' /JS '.$this->_textstring($this->javascript, $obj_id);
  12169. $out .= ' >>';
  12170. $out .= "\n".'endobj';
  12171. $this->_out($out);
  12172. }
  12173. // additional Javascript objects
  12174. if (!empty($this->js_objects)) {
  12175. foreach ($this->js_objects as $key => $val) {
  12176. $out = $this->_getobj($key)."\n".' << /S /JavaScript /JS '.$this->_textstring($val['js'], $key).' >>'."\n".'endobj';
  12177. $this->_out($out);
  12178. }
  12179. }
  12180. }
  12181. /**
  12182. * Adds a javascript form field.
  12183. * @param string $type field type
  12184. * @param string $name field name
  12185. * @param int $x horizontal position
  12186. * @param int $y vertical position
  12187. * @param int $w width
  12188. * @param int $h height
  12189. * @param array $prop javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  12190. * @protected
  12191. * @author Denis Van Nuffelen, Nicola Asuni
  12192. * @since 2.1.002 (2008-02-12)
  12193. */
  12194. protected function _addfield($type, $name, $x, $y, $w, $h, $prop) {
  12195. if ($this->rtl) {
  12196. $x = $x - $w;
  12197. }
  12198. // the followind avoid fields duplication after saving the document
  12199. $this->javascript .= "if (getField('tcpdfdocsaved').value != 'saved') {";
  12200. $k = $this->k;
  12201. $this->javascript .= sprintf("f".$name."=this.addField('%s','%s',%u,[%F,%F,%F,%F]);", $name, $type, $this->PageNo()-1, $x*$k, ($this->h-$y)*$k+1, ($x+$w)*$k, ($this->h-$y-$h)*$k+1)."\n";
  12202. $this->javascript .= 'f'.$name.'.textSize='.$this->FontSizePt.";\n";
  12203. foreach($prop as $key => $val) {
  12204. if (strcmp(substr($key, -5), 'Color') == 0) {
  12205. $val = TCPDF_COLORS::_JScolor($val);
  12206. } else {
  12207. $val = "'".$val."'";
  12208. }
  12209. $this->javascript .= 'f'.$name.'.'.$key.'='.$val.";\n";
  12210. }
  12211. if ($this->rtl) {
  12212. $this->x -= $w;
  12213. } else {
  12214. $this->x += $w;
  12215. }
  12216. $this->javascript .= '}';
  12217. }
  12218. // --- FORM FIELDS -----------------------------------------------------
  12219. /**
  12220. * Set default properties for form fields.
  12221. * @param array $prop javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  12222. * @public
  12223. * @author Nicola Asuni
  12224. * @since 4.8.000 (2009-09-06)
  12225. */
  12226. public function setFormDefaultProp($prop=array()) {
  12227. $this->default_form_prop = $prop;
  12228. }
  12229. /**
  12230. * Return the default properties for form fields.
  12231. * @return array $prop javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  12232. * @public
  12233. * @author Nicola Asuni
  12234. * @since 4.8.000 (2009-09-06)
  12235. */
  12236. public function getFormDefaultProp() {
  12237. return $this->default_form_prop;
  12238. }
  12239. /**
  12240. * Creates a text field
  12241. * @param string $name field name
  12242. * @param float $w Width of the rectangle
  12243. * @param float $h Height of the rectangle
  12244. * @param array $prop javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  12245. * @param array $opt annotation parameters. Possible values are described on official PDF32000_2008 reference.
  12246. * @param float|null $x Abscissa of the upper-left corner of the rectangle
  12247. * @param float|null $y Ordinate of the upper-left corner of the rectangle
  12248. * @param boolean $js if true put the field using JavaScript (requires Acrobat Writer to be rendered).
  12249. * @public
  12250. * @author Nicola Asuni
  12251. * @since 4.8.000 (2009-09-07)
  12252. */
  12253. public function TextField($name, $w, $h, $prop=array(), $opt=array(), $x=null, $y=null, $js=false) {
  12254. if (TCPDF_STATIC::empty_string($x)) {
  12255. $x = $this->x;
  12256. }
  12257. if (TCPDF_STATIC::empty_string($y)) {
  12258. $y = $this->y;
  12259. }
  12260. // check page for no-write regions and adapt page margins if necessary
  12261. list($x, $y) = $this->checkPageRegions($h, $x, $y);
  12262. if ($js) {
  12263. $this->_addfield('text', $name, $x, $y, $w, $h, $prop);
  12264. return;
  12265. }
  12266. // get default style
  12267. $prop = array_merge($this->getFormDefaultProp(), $prop);
  12268. // get annotation data
  12269. $popt = TCPDF_STATIC::getAnnotOptFromJSProp($prop, $this->spot_colors, $this->rtl);
  12270. // set default appearance stream
  12271. $this->annotation_fonts[$this->CurrentFont['fontkey']] = $this->CurrentFont['i'];
  12272. $fontstyle = sprintf('/F%d %F Tf %s', $this->CurrentFont['i'], $this->FontSizePt, $this->TextColor);
  12273. $popt['da'] = $fontstyle;
  12274. // build appearance stream
  12275. $popt['ap'] = array();
  12276. $popt['ap']['n'] = '/Tx BMC q '.$fontstyle.' ';
  12277. $text = '';
  12278. if (isset($prop['value']) AND !empty($prop['value'])) {
  12279. $text = $prop['value'];
  12280. } elseif (isset($opt['v']) AND !empty($opt['v'])) {
  12281. $text = $opt['v'];
  12282. }
  12283. $tmpid = $this->startTemplate($w, $h, false);
  12284. $align = '';
  12285. if (isset($popt['q'])) {
  12286. switch ($popt['q']) {
  12287. case 0: {
  12288. $align = 'L';
  12289. break;
  12290. }
  12291. case 1: {
  12292. $align = 'C';
  12293. break;
  12294. }
  12295. case 2: {
  12296. $align = 'R';
  12297. break;
  12298. }
  12299. default: {
  12300. $align = '';
  12301. break;
  12302. }
  12303. }
  12304. }
  12305. $this->MultiCell($w, $h, $text, 0, $align, false, 0, 0, 0, true, 0, false, true, 0, 'T', false);
  12306. $this->endTemplate();
  12307. --$this->n;
  12308. $popt['ap']['n'] .= $this->xobjects[$tmpid]['outdata'];
  12309. unset($this->xobjects[$tmpid]);
  12310. $popt['ap']['n'] .= 'Q EMC';
  12311. // merge options
  12312. $opt = array_merge($popt, $opt);
  12313. // remove some conflicting options
  12314. unset($opt['bs']);
  12315. // set remaining annotation data
  12316. $opt['Subtype'] = 'Widget';
  12317. $opt['ft'] = 'Tx';
  12318. $opt['t'] = $name;
  12319. // Additional annotation's parameters (check _putannotsobj() method):
  12320. //$opt['f']
  12321. //$opt['as']
  12322. //$opt['bs']
  12323. //$opt['be']
  12324. //$opt['c']
  12325. //$opt['border']
  12326. //$opt['h']
  12327. //$opt['mk'];
  12328. //$opt['mk']['r']
  12329. //$opt['mk']['bc'];
  12330. //$opt['mk']['bg'];
  12331. unset($opt['mk']['ca']);
  12332. unset($opt['mk']['rc']);
  12333. unset($opt['mk']['ac']);
  12334. unset($opt['mk']['i']);
  12335. unset($opt['mk']['ri']);
  12336. unset($opt['mk']['ix']);
  12337. unset($opt['mk']['if']);
  12338. //$opt['mk']['if']['sw'];
  12339. //$opt['mk']['if']['s'];
  12340. //$opt['mk']['if']['a'];
  12341. //$opt['mk']['if']['fb'];
  12342. unset($opt['mk']['tp']);
  12343. //$opt['tu']
  12344. //$opt['tm']
  12345. //$opt['ff']
  12346. //$opt['v']
  12347. //$opt['dv']
  12348. //$opt['a']
  12349. //$opt['aa']
  12350. //$opt['q']
  12351. $this->Annotation($x, $y, $w, $h, $name, $opt, 0);
  12352. if ($this->rtl) {
  12353. $this->x -= $w;
  12354. } else {
  12355. $this->x += $w;
  12356. }
  12357. }
  12358. /**
  12359. * Creates a RadioButton field.
  12360. * @param string $name Field name.
  12361. * @param int $w Width of the radio button.
  12362. * @param array $prop Javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  12363. * @param array $opt Annotation parameters. Possible values are described on official PDF32000_2008 reference.
  12364. * @param string $onvalue Value to be returned if selected.
  12365. * @param boolean $checked Define the initial state.
  12366. * @param float|null $x Abscissa of the upper-left corner of the rectangle
  12367. * @param float|null $y Ordinate of the upper-left corner of the rectangle
  12368. * @param boolean $js If true put the field using JavaScript (requires Acrobat Writer to be rendered).
  12369. * @public
  12370. * @author Nicola Asuni
  12371. * @since 4.8.000 (2009-09-07)
  12372. */
  12373. public function RadioButton($name, $w, $prop=array(), $opt=array(), $onvalue='On', $checked=false, $x=null, $y=null, $js=false) {
  12374. if (TCPDF_STATIC::empty_string($x)) {
  12375. $x = $this->x;
  12376. }
  12377. if (TCPDF_STATIC::empty_string($y)) {
  12378. $y = $this->y;
  12379. }
  12380. // check page for no-write regions and adapt page margins if necessary
  12381. list($x, $y) = $this->checkPageRegions($w, $x, $y);
  12382. if ($js) {
  12383. $this->_addfield('radiobutton', $name, $x, $y, $w, $w, $prop);
  12384. return;
  12385. }
  12386. if (TCPDF_STATIC::empty_string($onvalue)) {
  12387. $onvalue = 'On';
  12388. }
  12389. if ($checked) {
  12390. $defval = $onvalue;
  12391. } else {
  12392. $defval = 'Off';
  12393. }
  12394. // set font
  12395. $font = 'zapfdingbats';
  12396. if ($this->pdfa_mode) {
  12397. // all fonts must be embedded
  12398. $font = 'pdfa'.$font;
  12399. }
  12400. $this->AddFont($font);
  12401. $tmpfont = $this->getFontBuffer($font);
  12402. // set data for parent group
  12403. if (!isset($this->radiobutton_groups[$this->page])) {
  12404. $this->radiobutton_groups[$this->page] = array();
  12405. }
  12406. if (!isset($this->radiobutton_groups[$this->page][$name])) {
  12407. $this->radiobutton_groups[$this->page][$name] = array();
  12408. ++$this->n;
  12409. $this->radiobutton_groups[$this->page][$name]['n'] = $this->n;
  12410. $this->radio_groups[] = $this->n;
  12411. }
  12412. $kid = ($this->n + 1);
  12413. // save object ID to be added on Kids entry on parent object
  12414. $this->radiobutton_groups[$this->page][$name][] = array('kid' => $kid, 'def' => $defval);
  12415. // get default style
  12416. $prop = array_merge($this->getFormDefaultProp(), $prop);
  12417. $prop['NoToggleToOff'] = 'true';
  12418. $prop['Radio'] = 'true';
  12419. $prop['borderStyle'] = 'inset';
  12420. // get annotation data
  12421. $popt = TCPDF_STATIC::getAnnotOptFromJSProp($prop, $this->spot_colors, $this->rtl);
  12422. // set additional default options
  12423. $this->annotation_fonts[$tmpfont['fontkey']] = $tmpfont['i'];
  12424. $fontstyle = sprintf('/F%d %F Tf %s', $tmpfont['i'], $this->FontSizePt, $this->TextColor);
  12425. $popt['da'] = $fontstyle;
  12426. // build appearance stream
  12427. $popt['ap'] = array();
  12428. $popt['ap']['n'] = array();
  12429. $fx = ((($w - $this->getAbsFontMeasure($tmpfont['cw'][108])) / 2) * $this->k);
  12430. $fy = (($w - ((($tmpfont['desc']['Ascent'] - $tmpfont['desc']['Descent']) * $this->FontSizePt / 1000) / $this->k)) * $this->k);
  12431. $popt['ap']['n'][$onvalue] = sprintf('q %s BT /F%d %F Tf %F %F Td ('.chr(108).') Tj ET Q', $this->TextColor, $tmpfont['i'], $this->FontSizePt, $fx, $fy);
  12432. $popt['ap']['n']['Off'] = sprintf('q %s BT /F%d %F Tf %F %F Td ('.chr(109).') Tj ET Q', $this->TextColor, $tmpfont['i'], $this->FontSizePt, $fx, $fy);
  12433. if (!isset($popt['mk'])) {
  12434. $popt['mk'] = array();
  12435. }
  12436. $popt['mk']['ca'] = '(l)';
  12437. // merge options
  12438. $opt = array_merge($popt, $opt);
  12439. // set remaining annotation data
  12440. $opt['Subtype'] = 'Widget';
  12441. $opt['ft'] = 'Btn';
  12442. if ($checked) {
  12443. $opt['v'] = array('/'.$onvalue);
  12444. $opt['as'] = $onvalue;
  12445. } else {
  12446. $opt['as'] = 'Off';
  12447. }
  12448. // store readonly flag
  12449. if (!isset($this->radiobutton_groups[$this->page][$name]['#readonly#'])) {
  12450. $this->radiobutton_groups[$this->page][$name]['#readonly#'] = false;
  12451. }
  12452. $this->radiobutton_groups[$this->page][$name]['#readonly#'] |= ($opt['f'] & 64);
  12453. $this->Annotation($x, $y, $w, $w, $name, $opt, 0);
  12454. if ($this->rtl) {
  12455. $this->x -= $w;
  12456. } else {
  12457. $this->x += $w;
  12458. }
  12459. }
  12460. /**
  12461. * Creates a List-box field
  12462. * @param string $name field name
  12463. * @param int $w width
  12464. * @param int $h height
  12465. * @param array $values array containing the list of values.
  12466. * @param array $prop javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  12467. * @param array $opt annotation parameters. Possible values are described on official PDF32000_2008 reference.
  12468. * @param float|null $x Abscissa of the upper-left corner of the rectangle
  12469. * @param float|null $y Ordinate of the upper-left corner of the rectangle
  12470. * @param boolean $js if true put the field using JavaScript (requires Acrobat Writer to be rendered).
  12471. * @public
  12472. * @author Nicola Asuni
  12473. * @since 4.8.000 (2009-09-07)
  12474. */
  12475. public function ListBox($name, $w, $h, $values, $prop=array(), $opt=array(), $x=null, $y=null, $js=false) {
  12476. if (TCPDF_STATIC::empty_string($x)) {
  12477. $x = $this->x;
  12478. }
  12479. if (TCPDF_STATIC::empty_string($y)) {
  12480. $y = $this->y;
  12481. }
  12482. // check page for no-write regions and adapt page margins if necessary
  12483. list($x, $y) = $this->checkPageRegions($h, $x, $y);
  12484. if ($js) {
  12485. $this->_addfield('listbox', $name, $x, $y, $w, $h, $prop);
  12486. $s = '';
  12487. foreach ($values as $value) {
  12488. if (is_array($value)) {
  12489. $s .= ',[\''.addslashes($value[1]).'\',\''.addslashes($value[0]).'\']';
  12490. } else {
  12491. $s .= ',[\''.addslashes($value).'\',\''.addslashes($value).'\']';
  12492. }
  12493. }
  12494. $this->javascript .= 'f'.$name.'.setItems('.substr($s, 1).');'."\n";
  12495. return;
  12496. }
  12497. // get default style
  12498. $prop = array_merge($this->getFormDefaultProp(), $prop);
  12499. // get annotation data
  12500. $popt = TCPDF_STATIC::getAnnotOptFromJSProp($prop, $this->spot_colors, $this->rtl);
  12501. // set additional default values
  12502. $this->annotation_fonts[$this->CurrentFont['fontkey']] = $this->CurrentFont['i'];
  12503. $fontstyle = sprintf('/F%d %F Tf %s', $this->CurrentFont['i'], $this->FontSizePt, $this->TextColor);
  12504. $popt['da'] = $fontstyle;
  12505. // build appearance stream
  12506. $popt['ap'] = array();
  12507. $popt['ap']['n'] = '/Tx BMC q '.$fontstyle.' ';
  12508. $text = '';
  12509. foreach($values as $item) {
  12510. if (is_array($item)) {
  12511. $text .= $item[1]."\n";
  12512. } else {
  12513. $text .= $item."\n";
  12514. }
  12515. }
  12516. $tmpid = $this->startTemplate($w, $h, false);
  12517. $this->MultiCell($w, $h, $text, 0, '', false, 0, 0, 0, true, 0, false, true, 0, 'T', false);
  12518. $this->endTemplate();
  12519. --$this->n;
  12520. $popt['ap']['n'] .= $this->xobjects[$tmpid]['outdata'];
  12521. unset($this->xobjects[$tmpid]);
  12522. $popt['ap']['n'] .= 'Q EMC';
  12523. // merge options
  12524. $opt = array_merge($popt, $opt);
  12525. // set remaining annotation data
  12526. $opt['Subtype'] = 'Widget';
  12527. $opt['ft'] = 'Ch';
  12528. $opt['t'] = $name;
  12529. $opt['opt'] = $values;
  12530. unset($opt['mk']['ca']);
  12531. unset($opt['mk']['rc']);
  12532. unset($opt['mk']['ac']);
  12533. unset($opt['mk']['i']);
  12534. unset($opt['mk']['ri']);
  12535. unset($opt['mk']['ix']);
  12536. unset($opt['mk']['if']);
  12537. unset($opt['mk']['tp']);
  12538. $this->Annotation($x, $y, $w, $h, $name, $opt, 0);
  12539. if ($this->rtl) {
  12540. $this->x -= $w;
  12541. } else {
  12542. $this->x += $w;
  12543. }
  12544. }
  12545. /**
  12546. * Creates a Combo-box field
  12547. * @param string $name field name
  12548. * @param int $w width
  12549. * @param int $h height
  12550. * @param array $values array containing the list of values.
  12551. * @param array $prop javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  12552. * @param array $opt annotation parameters. Possible values are described on official PDF32000_2008 reference.
  12553. * @param float|null $x Abscissa of the upper-left corner of the rectangle
  12554. * @param float|null $y Ordinate of the upper-left corner of the rectangle
  12555. * @param boolean $js if true put the field using JavaScript (requires Acrobat Writer to be rendered).
  12556. * @public
  12557. * @author Nicola Asuni
  12558. * @since 4.8.000 (2009-09-07)
  12559. */
  12560. public function ComboBox($name, $w, $h, $values, $prop=array(), $opt=array(), $x=null, $y=null, $js=false) {
  12561. if (TCPDF_STATIC::empty_string($x)) {
  12562. $x = $this->x;
  12563. }
  12564. if (TCPDF_STATIC::empty_string($y)) {
  12565. $y = $this->y;
  12566. }
  12567. // check page for no-write regions and adapt page margins if necessary
  12568. list($x, $y) = $this->checkPageRegions($h, $x, $y);
  12569. if ($js) {
  12570. $this->_addfield('combobox', $name, $x, $y, $w, $h, $prop);
  12571. $s = '';
  12572. foreach ($values as $value) {
  12573. if (is_array($value)) {
  12574. $s .= ',[\''.addslashes($value[1]).'\',\''.addslashes($value[0]).'\']';
  12575. } else {
  12576. $s .= ',[\''.addslashes($value).'\',\''.addslashes($value).'\']';
  12577. }
  12578. }
  12579. $this->javascript .= 'f'.$name.'.setItems('.substr($s, 1).');'."\n";
  12580. return;
  12581. }
  12582. // get default style
  12583. $prop = array_merge($this->getFormDefaultProp(), $prop);
  12584. $prop['Combo'] = true;
  12585. // get annotation data
  12586. $popt = TCPDF_STATIC::getAnnotOptFromJSProp($prop, $this->spot_colors, $this->rtl);
  12587. // set additional default options
  12588. $this->annotation_fonts[$this->CurrentFont['fontkey']] = $this->CurrentFont['i'];
  12589. $fontstyle = sprintf('/F%d %F Tf %s', $this->CurrentFont['i'], $this->FontSizePt, $this->TextColor);
  12590. $popt['da'] = $fontstyle;
  12591. // build appearance stream
  12592. $popt['ap'] = array();
  12593. $popt['ap']['n'] = '/Tx BMC q '.$fontstyle.' ';
  12594. $text = '';
  12595. foreach($values as $item) {
  12596. if (is_array($item)) {
  12597. $text .= $item[1]."\n";
  12598. } else {
  12599. $text .= $item."\n";
  12600. }
  12601. }
  12602. $tmpid = $this->startTemplate($w, $h, false);
  12603. $this->MultiCell($w, $h, $text, 0, '', false, 0, 0, 0, true, 0, false, true, 0, 'T', false);
  12604. $this->endTemplate();
  12605. --$this->n;
  12606. $popt['ap']['n'] .= $this->xobjects[$tmpid]['outdata'];
  12607. unset($this->xobjects[$tmpid]);
  12608. $popt['ap']['n'] .= 'Q EMC';
  12609. // merge options
  12610. $opt = array_merge($popt, $opt);
  12611. // set remaining annotation data
  12612. $opt['Subtype'] = 'Widget';
  12613. $opt['ft'] = 'Ch';
  12614. $opt['t'] = $name;
  12615. $opt['opt'] = $values;
  12616. unset($opt['mk']['ca']);
  12617. unset($opt['mk']['rc']);
  12618. unset($opt['mk']['ac']);
  12619. unset($opt['mk']['i']);
  12620. unset($opt['mk']['ri']);
  12621. unset($opt['mk']['ix']);
  12622. unset($opt['mk']['if']);
  12623. unset($opt['mk']['tp']);
  12624. $this->Annotation($x, $y, $w, $h, $name, $opt, 0);
  12625. if ($this->rtl) {
  12626. $this->x -= $w;
  12627. } else {
  12628. $this->x += $w;
  12629. }
  12630. }
  12631. /**
  12632. * Creates a CheckBox field
  12633. * @param string $name field name
  12634. * @param int $w width
  12635. * @param boolean $checked define the initial state.
  12636. * @param array $prop javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  12637. * @param array $opt annotation parameters. Possible values are described on official PDF32000_2008 reference.
  12638. * @param string $onvalue value to be returned if selected.
  12639. * @param float|null $x Abscissa of the upper-left corner of the rectangle
  12640. * @param float|null $y Ordinate of the upper-left corner of the rectangle
  12641. * @param boolean $js if true put the field using JavaScript (requires Acrobat Writer to be rendered).
  12642. * @public
  12643. * @author Nicola Asuni
  12644. * @since 4.8.000 (2009-09-07)
  12645. */
  12646. public function CheckBox($name, $w, $checked=false, $prop=array(), $opt=array(), $onvalue='Yes', $x=null, $y=null, $js=false) {
  12647. if (TCPDF_STATIC::empty_string($x)) {
  12648. $x = $this->x;
  12649. }
  12650. if (TCPDF_STATIC::empty_string($y)) {
  12651. $y = $this->y;
  12652. }
  12653. // check page for no-write regions and adapt page margins if necessary
  12654. list($x, $y) = $this->checkPageRegions($w, $x, $y);
  12655. if ($js) {
  12656. $this->_addfield('checkbox', $name, $x, $y, $w, $w, $prop);
  12657. return;
  12658. }
  12659. if (!isset($prop['value'])) {
  12660. $prop['value'] = array('Yes');
  12661. }
  12662. // get default style
  12663. $prop = array_merge($this->getFormDefaultProp(), $prop);
  12664. $prop['borderStyle'] = 'inset';
  12665. // get annotation data
  12666. $popt = TCPDF_STATIC::getAnnotOptFromJSProp($prop, $this->spot_colors, $this->rtl);
  12667. // set additional default options
  12668. $font = 'zapfdingbats';
  12669. if ($this->pdfa_mode) {
  12670. // all fonts must be embedded
  12671. $font = 'pdfa'.$font;
  12672. }
  12673. $this->AddFont($font);
  12674. $tmpfont = $this->getFontBuffer($font);
  12675. $this->annotation_fonts[$tmpfont['fontkey']] = $tmpfont['i'];
  12676. $fontstyle = sprintf('/F%d %F Tf %s', $tmpfont['i'], $this->FontSizePt, $this->TextColor);
  12677. $popt['da'] = $fontstyle;
  12678. // build appearance stream
  12679. $popt['ap'] = array();
  12680. $popt['ap']['n'] = array();
  12681. $fx = ((($w - $this->getAbsFontMeasure($tmpfont['cw'][110])) / 2) * $this->k);
  12682. $fy = (($w - ((($tmpfont['desc']['Ascent'] - $tmpfont['desc']['Descent']) * $this->FontSizePt / 1000) / $this->k)) * $this->k);
  12683. $popt['ap']['n']['Yes'] = sprintf('q %s BT /F%d %F Tf %F %F Td ('.chr(110).') Tj ET Q', $this->TextColor, $tmpfont['i'], $this->FontSizePt, $fx, $fy);
  12684. $popt['ap']['n']['Off'] = sprintf('q %s BT /F%d %F Tf %F %F Td ('.chr(111).') Tj ET Q', $this->TextColor, $tmpfont['i'], $this->FontSizePt, $fx, $fy);
  12685. // merge options
  12686. $opt = array_merge($popt, $opt);
  12687. // set remaining annotation data
  12688. $opt['Subtype'] = 'Widget';
  12689. $opt['ft'] = 'Btn';
  12690. $opt['t'] = $name;
  12691. if (TCPDF_STATIC::empty_string($onvalue)) {
  12692. $onvalue = 'Yes';
  12693. }
  12694. $opt['opt'] = array($onvalue);
  12695. if ($checked) {
  12696. $opt['v'] = array('/Yes');
  12697. $opt['as'] = 'Yes';
  12698. } else {
  12699. $opt['v'] = array('/Off');
  12700. $opt['as'] = 'Off';
  12701. }
  12702. $this->Annotation($x, $y, $w, $w, $name, $opt, 0);
  12703. if ($this->rtl) {
  12704. $this->x -= $w;
  12705. } else {
  12706. $this->x += $w;
  12707. }
  12708. }
  12709. /**
  12710. * Creates a button field
  12711. * @param string $name field name
  12712. * @param int $w width
  12713. * @param int $h height
  12714. * @param string $caption caption.
  12715. * @param mixed $action action triggered by pressing the button. Use a string to specify a javascript action. Use an array to specify a form action options as on section 12.7.5 of PDF32000_2008.
  12716. * @param array $prop javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  12717. * @param array $opt annotation parameters. Possible values are described on official PDF32000_2008 reference.
  12718. * @param float|null $x Abscissa of the upper-left corner of the rectangle
  12719. * @param float|null $y Ordinate of the upper-left corner of the rectangle
  12720. * @param boolean $js if true put the field using JavaScript (requires Acrobat Writer to be rendered).
  12721. * @public
  12722. * @author Nicola Asuni
  12723. * @since 4.8.000 (2009-09-07)
  12724. */
  12725. public function Button($name, $w, $h, $caption, $action, $prop=array(), $opt=array(), $x=null, $y=null, $js=false) {
  12726. if (TCPDF_STATIC::empty_string($x)) {
  12727. $x = $this->x;
  12728. }
  12729. if (TCPDF_STATIC::empty_string($y)) {
  12730. $y = $this->y;
  12731. }
  12732. // check page for no-write regions and adapt page margins if necessary
  12733. list($x, $y) = $this->checkPageRegions($h, $x, $y);
  12734. if ($js) {
  12735. $this->_addfield('button', $name, $this->x, $this->y, $w, $h, $prop);
  12736. $this->javascript .= 'f'.$name.".buttonSetCaption('".addslashes($caption)."');\n";
  12737. $this->javascript .= 'f'.$name.".setAction('MouseUp','".addslashes($action)."');\n";
  12738. $this->javascript .= 'f'.$name.".highlight='push';\n";
  12739. $this->javascript .= 'f'.$name.".print=false;\n";
  12740. return;
  12741. }
  12742. // get default style
  12743. $prop = array_merge($this->getFormDefaultProp(), $prop);
  12744. $prop['Pushbutton'] = 'true';
  12745. $prop['highlight'] = 'push';
  12746. $prop['display'] = 'display.noPrint';
  12747. // get annotation data
  12748. $popt = TCPDF_STATIC::getAnnotOptFromJSProp($prop, $this->spot_colors, $this->rtl);
  12749. $this->annotation_fonts[$this->CurrentFont['fontkey']] = $this->CurrentFont['i'];
  12750. $fontstyle = sprintf('/F%d %F Tf %s', $this->CurrentFont['i'], $this->FontSizePt, $this->TextColor);
  12751. $popt['da'] = $fontstyle;
  12752. // build appearance stream
  12753. $popt['ap'] = array();
  12754. $popt['ap']['n'] = '/Tx BMC q '.$fontstyle.' ';
  12755. $tmpid = $this->startTemplate($w, $h, false);
  12756. $bw = (2 / $this->k); // border width
  12757. $border = array(
  12758. 'L' => array('width' => $bw, 'cap' => 'square', 'join' => 'miter', 'dash' => 0, 'color' => array(231)),
  12759. 'R' => array('width' => $bw, 'cap' => 'square', 'join' => 'miter', 'dash' => 0, 'color' => array(51)),
  12760. 'T' => array('width' => $bw, 'cap' => 'square', 'join' => 'miter', 'dash' => 0, 'color' => array(231)),
  12761. 'B' => array('width' => $bw, 'cap' => 'square', 'join' => 'miter', 'dash' => 0, 'color' => array(51)));
  12762. $this->setFillColor(204);
  12763. $this->Cell($w, $h, $caption, $border, 0, 'C', true, '', 1, false, 'T', 'M');
  12764. $this->endTemplate();
  12765. --$this->n;
  12766. $popt['ap']['n'] .= $this->xobjects[$tmpid]['outdata'];
  12767. unset($this->xobjects[$tmpid]);
  12768. $popt['ap']['n'] .= 'Q EMC';
  12769. // set additional default options
  12770. if (!isset($popt['mk'])) {
  12771. $popt['mk'] = array();
  12772. }
  12773. $ann_obj_id = ($this->n + 1);
  12774. if (!empty($action) AND !is_array($action)) {
  12775. $ann_obj_id = ($this->n + 2);
  12776. }
  12777. $popt['mk']['ca'] = $this->_textstring($caption, $ann_obj_id);
  12778. $popt['mk']['rc'] = $this->_textstring($caption, $ann_obj_id);
  12779. $popt['mk']['ac'] = $this->_textstring($caption, $ann_obj_id);
  12780. // merge options
  12781. $opt = array_merge($popt, $opt);
  12782. // set remaining annotation data
  12783. $opt['Subtype'] = 'Widget';
  12784. $opt['ft'] = 'Btn';
  12785. $opt['t'] = $caption;
  12786. $opt['v'] = $name;
  12787. if (!empty($action)) {
  12788. if (is_array($action)) {
  12789. // form action options as on section 12.7.5 of PDF32000_2008.
  12790. $opt['aa'] = '/D <<';
  12791. $bmode = array('SubmitForm', 'ResetForm', 'ImportData');
  12792. foreach ($action AS $key => $val) {
  12793. if (($key == 'S') AND in_array($val, $bmode)) {
  12794. $opt['aa'] .= ' /S /'.$val;
  12795. } elseif (($key == 'F') AND (!empty($val))) {
  12796. $opt['aa'] .= ' /F '.$this->_datastring($val, $ann_obj_id);
  12797. } elseif (($key == 'Fields') AND is_array($val) AND !empty($val)) {
  12798. $opt['aa'] .= ' /Fields [';
  12799. foreach ($val AS $field) {
  12800. $opt['aa'] .= ' '.$this->_textstring($field, $ann_obj_id);
  12801. }
  12802. $opt['aa'] .= ']';
  12803. } elseif (($key == 'Flags')) {
  12804. $ff = 0;
  12805. if (is_array($val)) {
  12806. foreach ($val AS $flag) {
  12807. switch ($flag) {
  12808. case 'Include/Exclude': {
  12809. $ff += 1 << 0;
  12810. break;
  12811. }
  12812. case 'IncludeNoValueFields': {
  12813. $ff += 1 << 1;
  12814. break;
  12815. }
  12816. case 'ExportFormat': {
  12817. $ff += 1 << 2;
  12818. break;
  12819. }
  12820. case 'GetMethod': {
  12821. $ff += 1 << 3;
  12822. break;
  12823. }
  12824. case 'SubmitCoordinates': {
  12825. $ff += 1 << 4;
  12826. break;
  12827. }
  12828. case 'XFDF': {
  12829. $ff += 1 << 5;
  12830. break;
  12831. }
  12832. case 'IncludeAppendSaves': {
  12833. $ff += 1 << 6;
  12834. break;
  12835. }
  12836. case 'IncludeAnnotations': {
  12837. $ff += 1 << 7;
  12838. break;
  12839. }
  12840. case 'SubmitPDF': {
  12841. $ff += 1 << 8;
  12842. break;
  12843. }
  12844. case 'CanonicalFormat': {
  12845. $ff += 1 << 9;
  12846. break;
  12847. }
  12848. case 'ExclNonUserAnnots': {
  12849. $ff += 1 << 10;
  12850. break;
  12851. }
  12852. case 'ExclFKey': {
  12853. $ff += 1 << 11;
  12854. break;
  12855. }
  12856. case 'EmbedForm': {
  12857. $ff += 1 << 13;
  12858. break;
  12859. }
  12860. }
  12861. }
  12862. } else {
  12863. $ff = intval($val);
  12864. }
  12865. $opt['aa'] .= ' /Flags '.$ff;
  12866. }
  12867. }
  12868. $opt['aa'] .= ' >>';
  12869. } else {
  12870. // Javascript action or raw action command
  12871. $js_obj_id = $this->addJavascriptObject($action);
  12872. $opt['aa'] = '/D '.$js_obj_id.' 0 R';
  12873. }
  12874. }
  12875. $this->Annotation($x, $y, $w, $h, $name, $opt, 0);
  12876. if ($this->rtl) {
  12877. $this->x -= $w;
  12878. } else {
  12879. $this->x += $w;
  12880. }
  12881. }
  12882. // --- END FORMS FIELDS ------------------------------------------------
  12883. /**
  12884. * Add certification signature (DocMDP or UR3)
  12885. * You can set only one signature type
  12886. * @protected
  12887. * @author Nicola Asuni
  12888. * @since 4.6.008 (2009-05-07)
  12889. */
  12890. protected function _putsignature() {
  12891. if ((!$this->sign) OR (!isset($this->signature_data['cert_type']))) {
  12892. return;
  12893. }
  12894. $sigobjid = ($this->sig_obj_id + 1);
  12895. $out = $this->_getobj($sigobjid)."\n";
  12896. $out .= '<< /Type /Sig';
  12897. $out .= ' /Filter /Adobe.PPKLite';
  12898. $out .= ' /SubFilter /adbe.pkcs7.detached';
  12899. $out .= ' '.TCPDF_STATIC::$byterange_string;
  12900. $out .= ' /Contents<'.str_repeat('0', $this->signature_max_length).'>';
  12901. if (empty($this->signature_data['approval']) OR ($this->signature_data['approval'] != 'A')) {
  12902. $out .= ' /Reference ['; // array of signature reference dictionaries
  12903. $out .= ' << /Type /SigRef';
  12904. if ($this->signature_data['cert_type'] > 0) {
  12905. $out .= ' /TransformMethod /DocMDP';
  12906. $out .= ' /TransformParams <<';
  12907. $out .= ' /Type /TransformParams';
  12908. $out .= ' /P '.$this->signature_data['cert_type'];
  12909. $out .= ' /V /1.2';
  12910. } else {
  12911. $out .= ' /TransformMethod /UR3';
  12912. $out .= ' /TransformParams <<';
  12913. $out .= ' /Type /TransformParams';
  12914. $out .= ' /V /2.2';
  12915. if (!TCPDF_STATIC::empty_string($this->ur['document'])) {
  12916. $out .= ' /Document['.$this->ur['document'].']';
  12917. }
  12918. if (!TCPDF_STATIC::empty_string($this->ur['form'])) {
  12919. $out .= ' /Form['.$this->ur['form'].']';
  12920. }
  12921. if (!TCPDF_STATIC::empty_string($this->ur['signature'])) {
  12922. $out .= ' /Signature['.$this->ur['signature'].']';
  12923. }
  12924. if (!TCPDF_STATIC::empty_string($this->ur['annots'])) {
  12925. $out .= ' /Annots['.$this->ur['annots'].']';
  12926. }
  12927. if (!TCPDF_STATIC::empty_string($this->ur['ef'])) {
  12928. $out .= ' /EF['.$this->ur['ef'].']';
  12929. }
  12930. if (!TCPDF_STATIC::empty_string($this->ur['formex'])) {
  12931. $out .= ' /FormEX['.$this->ur['formex'].']';
  12932. }
  12933. }
  12934. $out .= ' >>'; // close TransformParams
  12935. // optional digest data (values must be calculated and replaced later)
  12936. //$out .= ' /Data ********** 0 R';
  12937. //$out .= ' /DigestMethod/MD5';
  12938. //$out .= ' /DigestLocation[********** 34]';
  12939. //$out .= ' /DigestValue<********************************>';
  12940. $out .= ' >>';
  12941. $out .= ' ]'; // end of reference
  12942. }
  12943. if (isset($this->signature_data['info']['Name']) AND !TCPDF_STATIC::empty_string($this->signature_data['info']['Name'])) {
  12944. $out .= ' /Name '.$this->_textstring($this->signature_data['info']['Name'], $sigobjid);
  12945. }
  12946. if (isset($this->signature_data['info']['Location']) AND !TCPDF_STATIC::empty_string($this->signature_data['info']['Location'])) {
  12947. $out .= ' /Location '.$this->_textstring($this->signature_data['info']['Location'], $sigobjid);
  12948. }
  12949. if (isset($this->signature_data['info']['Reason']) AND !TCPDF_STATIC::empty_string($this->signature_data['info']['Reason'])) {
  12950. $out .= ' /Reason '.$this->_textstring($this->signature_data['info']['Reason'], $sigobjid);
  12951. }
  12952. if (isset($this->signature_data['info']['ContactInfo']) AND !TCPDF_STATIC::empty_string($this->signature_data['info']['ContactInfo'])) {
  12953. $out .= ' /ContactInfo '.$this->_textstring($this->signature_data['info']['ContactInfo'], $sigobjid);
  12954. }
  12955. $out .= ' /M '.$this->_datestring($sigobjid, $this->doc_modification_timestamp);
  12956. $out .= ' >>';
  12957. $out .= "\n".'endobj';
  12958. $this->_out($out);
  12959. }
  12960. /**
  12961. * Set User's Rights for PDF Reader
  12962. * WARNING: This is experimental and currently do not work.
  12963. * Check the PDF Reference 8.7.1 Transform Methods,
  12964. * Table 8.105 Entries in the UR transform parameters dictionary
  12965. * @param boolean $enable if true enable user's rights on PDF reader
  12966. * @param string $document Names specifying additional document-wide usage rights for the document. The only defined value is "/FullSave", which permits a user to save the document along with modified form and/or annotation data.
  12967. * @param string $annots Names specifying additional annotation-related usage rights for the document. Valid names in PDF 1.5 and later are /Create/Delete/Modify/Copy/Import/Export, which permit the user to perform the named operation on annotations.
  12968. * @param string $form Names specifying additional form-field-related usage rights for the document. Valid names are: /Add/Delete/FillIn/Import/Export/SubmitStandalone/SpawnTemplate
  12969. * @param string $signature Names specifying additional signature-related usage rights for the document. The only defined value is /Modify, which permits a user to apply a digital signature to an existing signature form field or clear a signed signature form field.
  12970. * @param string $ef Names specifying additional usage rights for named embedded files in the document. Valid names are /Create/Delete/Modify/Import, which permit the user to perform the named operation on named embedded files
  12971. Names specifying additional embedded-files-related usage rights for the document.
  12972. * @param string $formex Names specifying additional form-field-related usage rights. The only valid name is BarcodePlaintext, which permits text form field data to be encoded as a plaintext two-dimensional barcode.
  12973. * @public
  12974. * @author Nicola Asuni
  12975. * @since 2.9.000 (2008-03-26)
  12976. */
  12977. public function setUserRights(
  12978. $enable=true,
  12979. $document='/FullSave',
  12980. $annots='/Create/Delete/Modify/Copy/Import/Export',
  12981. $form='/Add/Delete/FillIn/Import/Export/SubmitStandalone/SpawnTemplate',
  12982. $signature='/Modify',
  12983. $ef='/Create/Delete/Modify/Import',
  12984. $formex='') {
  12985. $this->ur['enabled'] = $enable;
  12986. $this->ur['document'] = $document;
  12987. $this->ur['annots'] = $annots;
  12988. $this->ur['form'] = $form;
  12989. $this->ur['signature'] = $signature;
  12990. $this->ur['ef'] = $ef;
  12991. $this->ur['formex'] = $formex;
  12992. if (!$this->sign) {
  12993. $this->setSignature('', '', '', '', 0, array());
  12994. }
  12995. }
  12996. /**
  12997. * Enable document signature (requires the OpenSSL Library).
  12998. * The digital signature improve document authenticity and integrity and allows o enable extra features on Acrobat Reader.
  12999. * To create self-signed signature: openssl req -x509 -nodes -days 365000 -newkey rsa:1024 -keyout tcpdf.crt -out tcpdf.crt
  13000. * To export crt to p12: openssl pkcs12 -export -in tcpdf.crt -out tcpdf.p12
  13001. * To convert pfx certificate to pem: openssl pkcs12 -in tcpdf.pfx -out tcpdf.crt -nodes
  13002. * @param mixed $signing_cert signing certificate (string or filename prefixed with 'file://')
  13003. * @param mixed $private_key private key (string or filename prefixed with 'file://')
  13004. * @param string $private_key_password password
  13005. * @param string $extracerts specifies the name of a file containing a bunch of extra certificates to include in the signature which can for example be used to help the recipient to verify the certificate that you used.
  13006. * @param int $cert_type The access permissions granted for this document. Valid values shall be: 1 = No changes to the document shall be permitted; any change to the document shall invalidate the signature; 2 = Permitted changes shall be filling in forms, instantiating page templates, and signing; other changes shall invalidate the signature; 3 = Permitted changes shall be the same as for 2, as well as annotation creation, deletion, and modification; other changes shall invalidate the signature.
  13007. * @param array $info array of option information: Name, Location, Reason, ContactInfo.
  13008. * @param string $approval Enable approval signature eg. for PDF incremental update
  13009. * @public
  13010. * @author Nicola Asuni
  13011. * @since 4.6.005 (2009-04-24)
  13012. */
  13013. public function setSignature($signing_cert='', $private_key='', $private_key_password='', $extracerts='', $cert_type=2, $info=array(), $approval='') {
  13014. // to create self-signed signature: openssl req -x509 -nodes -days 365000 -newkey rsa:1024 -keyout tcpdf.crt -out tcpdf.crt
  13015. // to export crt to p12: openssl pkcs12 -export -in tcpdf.crt -out tcpdf.p12
  13016. // to convert pfx certificate to pem: openssl
  13017. // OpenSSL> pkcs12 -in <cert.pfx> -out <cert.crt> -nodes
  13018. $this->sign = true;
  13019. ++$this->n;
  13020. $this->sig_obj_id = $this->n; // signature widget
  13021. ++$this->n; // signature object ($this->sig_obj_id + 1)
  13022. $this->signature_data = array();
  13023. if (strlen($signing_cert) == 0) {
  13024. $this->Error('Please provide a certificate file and password!');
  13025. }
  13026. if (strlen($private_key) == 0) {
  13027. $private_key = $signing_cert;
  13028. }
  13029. $this->signature_data['signcert'] = $signing_cert;
  13030. $this->signature_data['privkey'] = $private_key;
  13031. $this->signature_data['password'] = $private_key_password;
  13032. $this->signature_data['extracerts'] = $extracerts;
  13033. $this->signature_data['cert_type'] = $cert_type;
  13034. $this->signature_data['info'] = $info;
  13035. $this->signature_data['approval'] = $approval;
  13036. }
  13037. /**
  13038. * Set the digital signature appearance (a cliccable rectangle area to get signature properties)
  13039. * @param float $x Abscissa of the upper-left corner.
  13040. * @param float $y Ordinate of the upper-left corner.
  13041. * @param float $w Width of the signature area.
  13042. * @param float $h Height of the signature area.
  13043. * @param int $page option page number (if < 0 the current page is used).
  13044. * @param string $name Name of the signature.
  13045. * @public
  13046. * @author Nicola Asuni
  13047. * @since 5.3.011 (2010-06-17)
  13048. */
  13049. public function setSignatureAppearance($x=0, $y=0, $w=0, $h=0, $page=-1, $name='') {
  13050. $this->signature_appearance = $this->getSignatureAppearanceArray($x, $y, $w, $h, $page, $name);
  13051. }
  13052. /**
  13053. * Add an empty digital signature appearance (a cliccable rectangle area to get signature properties)
  13054. * @param float $x Abscissa of the upper-left corner.
  13055. * @param float $y Ordinate of the upper-left corner.
  13056. * @param float $w Width of the signature area.
  13057. * @param float $h Height of the signature area.
  13058. * @param int $page option page number (if < 0 the current page is used).
  13059. * @param string $name Name of the signature.
  13060. * @public
  13061. * @author Nicola Asuni
  13062. * @since 5.9.101 (2011-07-06)
  13063. */
  13064. public function addEmptySignatureAppearance($x=0, $y=0, $w=0, $h=0, $page=-1, $name='') {
  13065. ++$this->n;
  13066. $this->empty_signature_appearance[] = array('objid' => $this->n) + $this->getSignatureAppearanceArray($x, $y, $w, $h, $page, $name);
  13067. }
  13068. /**
  13069. * Get the array that defines the signature appearance (page and rectangle coordinates).
  13070. * @param float $x Abscissa of the upper-left corner.
  13071. * @param float $y Ordinate of the upper-left corner.
  13072. * @param float $w Width of the signature area.
  13073. * @param float $h Height of the signature area.
  13074. * @param int $page option page number (if < 0 the current page is used).
  13075. * @param string $name Name of the signature.
  13076. * @return array Array defining page and rectangle coordinates of signature appearance.
  13077. * @protected
  13078. * @author Nicola Asuni
  13079. * @since 5.9.101 (2011-07-06)
  13080. */
  13081. protected function getSignatureAppearanceArray($x=0, $y=0, $w=0, $h=0, $page=-1, $name='') {
  13082. $sigapp = array();
  13083. if (($page < 1) OR ($page > $this->numpages)) {
  13084. $sigapp['page'] = $this->page;
  13085. } else {
  13086. $sigapp['page'] = intval($page);
  13087. }
  13088. if (empty($name)) {
  13089. $sigapp['name'] = 'Signature';
  13090. } else {
  13091. $sigapp['name'] = $name;
  13092. }
  13093. $a = $x * $this->k;
  13094. $b = $this->pagedim[($sigapp['page'])]['h'] - (($y + $h) * $this->k);
  13095. $c = $w * $this->k;
  13096. $d = $h * $this->k;
  13097. $sigapp['rect'] = sprintf('%F %F %F %F', $a, $b, ($a + $c), ($b + $d));
  13098. return $sigapp;
  13099. }
  13100. /**
  13101. * Enable document timestamping (requires the OpenSSL Library).
  13102. * The trusted timestamping improve document security that means that no one should be able to change the document once it has been recorded.
  13103. * Use with digital signature only!
  13104. * @param string $tsa_host Time Stamping Authority (TSA) server (prefixed with 'https://')
  13105. * @param string $tsa_username Specifies the username for TSA authorization (optional) OR specifies the TSA authorization PEM file (see: example_66.php, optional)
  13106. * @param string $tsa_password Specifies the password for TSA authorization (optional)
  13107. * @param string $tsa_cert Specifies the location of TSA certificate for authorization (optional for cURL)
  13108. * @public
  13109. * @author Richard Stockinger
  13110. * @since 6.0.090 (2014-06-16)
  13111. */
  13112. public function setTimeStamp($tsa_host='', $tsa_username='', $tsa_password='', $tsa_cert='') {
  13113. $this->tsa_data = array();
  13114. if (!function_exists('curl_init')) {
  13115. $this->Error('Please enable cURL PHP extension!');
  13116. }
  13117. if (strlen($tsa_host) == 0) {
  13118. $this->Error('Please specify the host of Time Stamping Authority (TSA)!');
  13119. }
  13120. $this->tsa_data['tsa_host'] = $tsa_host;
  13121. if (is_file($tsa_username)) {
  13122. $this->tsa_data['tsa_auth'] = $tsa_username;
  13123. } else {
  13124. $this->tsa_data['tsa_username'] = $tsa_username;
  13125. }
  13126. $this->tsa_data['tsa_password'] = $tsa_password;
  13127. $this->tsa_data['tsa_cert'] = $tsa_cert;
  13128. $this->tsa_timestamp = true;
  13129. }
  13130. /**
  13131. * NOT YET IMPLEMENTED
  13132. * Request TSA for a timestamp
  13133. * @param string $signature Digital signature as binary string
  13134. * @return string Timestamped digital signature
  13135. * @protected
  13136. * @author Richard Stockinger
  13137. * @since 6.0.090 (2014-06-16)
  13138. */
  13139. protected function applyTSA($signature) {
  13140. if (!$this->tsa_timestamp) {
  13141. return $signature;
  13142. }
  13143. //@TODO: implement this feature
  13144. return $signature;
  13145. }
  13146. /**
  13147. * Create a new page group.
  13148. * NOTE: call this function before calling AddPage()
  13149. * @param int|null $page starting group page (leave empty for next page).
  13150. * @public
  13151. * @since 3.0.000 (2008-03-27)
  13152. */
  13153. public function startPageGroup($page=null) {
  13154. if (empty($page)) {
  13155. $page = $this->page + 1;
  13156. }
  13157. $this->newpagegroup[$page] = sizeof($this->newpagegroup) + 1;
  13158. }
  13159. /**
  13160. * Set the starting page number.
  13161. * @param int $num Starting page number.
  13162. * @since 5.9.093 (2011-06-16)
  13163. * @public
  13164. */
  13165. public function setStartingPageNumber($num=1) {
  13166. $this->starting_page_number = max(0, intval($num));
  13167. }
  13168. /**
  13169. * Returns the string alias used right align page numbers.
  13170. * If the current font is unicode type, the returned string wil contain an additional open curly brace.
  13171. * @return string
  13172. * @since 5.9.099 (2011-06-27)
  13173. * @public
  13174. */
  13175. public function getAliasRightShift() {
  13176. // calculate aproximatively the ratio between widths of aliases and replacements.
  13177. $ref = '{'.TCPDF_STATIC::$alias_right_shift.'}{'.TCPDF_STATIC::$alias_tot_pages.'}{'.TCPDF_STATIC::$alias_num_page.'}';
  13178. $rep = str_repeat(' ', $this->GetNumChars($ref));
  13179. $wrep = $this->GetStringWidth($rep);
  13180. if ($wrep > 0) {
  13181. $wdiff = max(1, ($this->GetStringWidth($ref) / $wrep));
  13182. } else {
  13183. $wdiff = 1;
  13184. }
  13185. $sdiff = sprintf('%F', $wdiff);
  13186. $alias = TCPDF_STATIC::$alias_right_shift.$sdiff.'}';
  13187. if ($this->isUnicodeFont()) {
  13188. $alias = '{'.$alias;
  13189. }
  13190. return $alias;
  13191. }
  13192. /**
  13193. * Returns the string alias used for the total number of pages.
  13194. * If the current font is unicode type, the returned string is surrounded by additional curly braces.
  13195. * This alias will be replaced by the total number of pages in the document.
  13196. * @return string
  13197. * @since 4.0.018 (2008-08-08)
  13198. * @public
  13199. */
  13200. public function getAliasNbPages() {
  13201. if ($this->isUnicodeFont()) {
  13202. return '{'.TCPDF_STATIC::$alias_tot_pages.'}';
  13203. }
  13204. return TCPDF_STATIC::$alias_tot_pages;
  13205. }
  13206. /**
  13207. * Returns the string alias used for the page number.
  13208. * If the current font is unicode type, the returned string is surrounded by additional curly braces.
  13209. * This alias will be replaced by the page number.
  13210. * @return string
  13211. * @since 4.5.000 (2009-01-02)
  13212. * @public
  13213. */
  13214. public function getAliasNumPage() {
  13215. if ($this->isUnicodeFont()) {
  13216. return '{'.TCPDF_STATIC::$alias_num_page.'}';
  13217. }
  13218. return TCPDF_STATIC::$alias_num_page;
  13219. }
  13220. /**
  13221. * Return the alias for the total number of pages in the current page group.
  13222. * If the current font is unicode type, the returned string is surrounded by additional curly braces.
  13223. * This alias will be replaced by the total number of pages in this group.
  13224. * @return string alias of the current page group
  13225. * @public
  13226. * @since 3.0.000 (2008-03-27)
  13227. */
  13228. public function getPageGroupAlias() {
  13229. if ($this->isUnicodeFont()) {
  13230. return '{'.TCPDF_STATIC::$alias_group_tot_pages.'}';
  13231. }
  13232. return TCPDF_STATIC::$alias_group_tot_pages;
  13233. }
  13234. /**
  13235. * Return the alias for the page number on the current page group.
  13236. * If the current font is unicode type, the returned string is surrounded by additional curly braces.
  13237. * This alias will be replaced by the page number (relative to the belonging group).
  13238. * @return string alias of the current page group
  13239. * @public
  13240. * @since 4.5.000 (2009-01-02)
  13241. */
  13242. public function getPageNumGroupAlias() {
  13243. if ($this->isUnicodeFont()) {
  13244. return '{'.TCPDF_STATIC::$alias_group_num_page.'}';
  13245. }
  13246. return TCPDF_STATIC::$alias_group_num_page;
  13247. }
  13248. /**
  13249. * Return the current page in the group.
  13250. * @return int current page in the group
  13251. * @public
  13252. * @since 3.0.000 (2008-03-27)
  13253. */
  13254. public function getGroupPageNo() {
  13255. return $this->pagegroups[$this->currpagegroup];
  13256. }
  13257. /**
  13258. * Returns the current group page number formatted as a string.
  13259. * @public
  13260. * @since 4.3.003 (2008-11-18)
  13261. * @see PaneNo(), formatPageNumber()
  13262. */
  13263. public function getGroupPageNoFormatted() {
  13264. return TCPDF_STATIC::formatPageNumber($this->getGroupPageNo());
  13265. }
  13266. /**
  13267. * Returns the current page number formatted as a string.
  13268. * @public
  13269. * @since 4.2.005 (2008-11-06)
  13270. * @see PaneNo(), formatPageNumber()
  13271. */
  13272. public function PageNoFormatted() {
  13273. return TCPDF_STATIC::formatPageNumber($this->PageNo());
  13274. }
  13275. /**
  13276. * Put pdf layers.
  13277. * @protected
  13278. * @since 3.0.000 (2008-03-27)
  13279. */
  13280. protected function _putocg() {
  13281. if (empty($this->pdflayers)) {
  13282. return;
  13283. }
  13284. foreach ($this->pdflayers as $key => $layer) {
  13285. $this->pdflayers[$key]['objid'] = $this->_newobj();
  13286. $out = '<< /Type /OCG';
  13287. $out .= ' /Name '.$this->_textstring($layer['name'], $this->pdflayers[$key]['objid']);
  13288. $out .= ' /Usage <<';
  13289. if (isset($layer['print']) AND ($layer['print'] !== NULL)) {
  13290. $out .= ' /Print <</PrintState /'.($layer['print']?'ON':'OFF').'>>';
  13291. }
  13292. $out .= ' /View <</ViewState /'.($layer['view']?'ON':'OFF').'>>';
  13293. $out .= ' >> >>';
  13294. $out .= "\n".'endobj';
  13295. $this->_out($out);
  13296. }
  13297. }
  13298. /**
  13299. * Start a new pdf layer.
  13300. * @param string $name Layer name (only a-z letters and numbers). Leave empty for automatic name.
  13301. * @param boolean|null $print Set to TRUE to print this layer, FALSE to not print and NULL to not set this option
  13302. * @param boolean $view Set to true to view this layer.
  13303. * @param boolean $lock If true lock the layer
  13304. * @public
  13305. * @since 5.9.102 (2011-07-13)
  13306. */
  13307. public function startLayer($name='', $print=true, $view=true, $lock=true) {
  13308. if ($this->state != 2) {
  13309. return;
  13310. }
  13311. $layer = sprintf('LYR%03d', (count($this->pdflayers) + 1));
  13312. if (empty($name)) {
  13313. $name = $layer;
  13314. } else {
  13315. $name = preg_replace('/[^a-zA-Z0-9_\-]/', '', $name);
  13316. }
  13317. $this->pdflayers[] = array('layer' => $layer, 'name' => $name, 'print' => $print, 'view' => $view, 'lock' => $lock);
  13318. $this->openMarkedContent = true;
  13319. $this->_out('/OC /'.$layer.' BDC');
  13320. }
  13321. /**
  13322. * End the current PDF layer.
  13323. * @public
  13324. * @since 5.9.102 (2011-07-13)
  13325. */
  13326. public function endLayer() {
  13327. if ($this->state != 2) {
  13328. return;
  13329. }
  13330. if ($this->openMarkedContent) {
  13331. // close existing open marked-content layer
  13332. $this->_out('EMC');
  13333. $this->openMarkedContent = false;
  13334. }
  13335. }
  13336. /**
  13337. * Set the visibility of the successive elements.
  13338. * This can be useful, for instance, to put a background
  13339. * image or color that will show on screen but won't print.
  13340. * @param string $v visibility mode. Legal values are: all, print, screen or view.
  13341. * @public
  13342. * @since 3.0.000 (2008-03-27)
  13343. */
  13344. public function setVisibility($v) {
  13345. if ($this->state != 2) {
  13346. return;
  13347. }
  13348. $this->endLayer();
  13349. switch($v) {
  13350. case 'print': {
  13351. $this->startLayer('Print', true, false);
  13352. break;
  13353. }
  13354. case 'view':
  13355. case 'screen': {
  13356. $this->startLayer('View', false, true);
  13357. break;
  13358. }
  13359. case 'all': {
  13360. $this->_out('');
  13361. break;
  13362. }
  13363. default: {
  13364. $this->Error('Incorrect visibility: '.$v);
  13365. break;
  13366. }
  13367. }
  13368. }
  13369. /**
  13370. * Add transparency parameters to the current extgstate
  13371. * @param array $parms parameters
  13372. * @return int|void the number of extgstates
  13373. * @protected
  13374. * @since 3.0.000 (2008-03-27)
  13375. */
  13376. protected function addExtGState($parms) {
  13377. if (($this->pdfa_mode && $this->pdfa_version < 2) || ($this->state != 2)) {
  13378. // transparency is not allowed in PDF/A-1 mode
  13379. return;
  13380. }
  13381. // check if this ExtGState already exist
  13382. foreach ($this->extgstates as $i => $ext) {
  13383. if ($ext['parms'] == $parms) {
  13384. if ($this->inxobj) {
  13385. // we are inside an XObject template
  13386. $this->xobjects[$this->xobjid]['extgstates'][$i] = $ext;
  13387. }
  13388. // return reference to existing ExtGState
  13389. return $i;
  13390. }
  13391. }
  13392. $n = (count($this->extgstates) + 1);
  13393. $this->extgstates[$n] = array('parms' => $parms);
  13394. if ($this->inxobj) {
  13395. // we are inside an XObject template
  13396. $this->xobjects[$this->xobjid]['extgstates'][$n] = $this->extgstates[$n];
  13397. }
  13398. return $n;
  13399. }
  13400. /**
  13401. * Add an extgstate
  13402. * @param int $gs extgstate
  13403. * @protected
  13404. * @since 3.0.000 (2008-03-27)
  13405. */
  13406. protected function setExtGState($gs) {
  13407. if (($this->pdfa_mode && $this->pdfa_version < 2) OR ($this->state != 2)) {
  13408. // transparency is not allowed in PDF/A-1 mode
  13409. return;
  13410. }
  13411. $this->_out(sprintf('/GS%d gs', $gs));
  13412. }
  13413. /**
  13414. * Put extgstates for object transparency
  13415. * @protected
  13416. * @since 3.0.000 (2008-03-27)
  13417. */
  13418. protected function _putextgstates() {
  13419. foreach ($this->extgstates as $i => $ext) {
  13420. $this->extgstates[$i]['n'] = $this->_newobj();
  13421. $out = '<< /Type /ExtGState';
  13422. foreach ($ext['parms'] as $k => $v) {
  13423. if (is_float($v)) {
  13424. $v = sprintf('%F', $v);
  13425. } elseif ($v === true) {
  13426. $v = 'true';
  13427. } elseif ($v === false) {
  13428. $v = 'false';
  13429. }
  13430. $out .= ' /'.$k.' '.$v;
  13431. }
  13432. $out .= ' >>';
  13433. $out .= "\n".'endobj';
  13434. $this->_out($out);
  13435. }
  13436. }
  13437. /**
  13438. * Set overprint mode for stroking (OP) and non-stroking (op) painting operations.
  13439. * (Check the "Entries in a Graphics State Parameter Dictionary" on PDF 32000-1:2008).
  13440. * @param boolean $stroking If true apply overprint for stroking operations.
  13441. * @param boolean|null $nonstroking If true apply overprint for painting operations other than stroking.
  13442. * @param integer $mode Overprint mode: (0 = each source colour component value replaces the value previously painted for the corresponding device colorant; 1 = a tint value of 0.0 for a source colour component shall leave the corresponding component of the previously painted colour unchanged).
  13443. * @public
  13444. * @since 5.9.152 (2012-03-23)
  13445. */
  13446. public function setOverprint($stroking=true, $nonstroking=null, $mode=0) {
  13447. if ($this->state != 2) {
  13448. return;
  13449. }
  13450. $stroking = $stroking ? true : false;
  13451. if (TCPDF_STATIC::empty_string($nonstroking)) {
  13452. // default value if not set
  13453. $nonstroking = $stroking;
  13454. } else {
  13455. $nonstroking = $nonstroking ? true : false;
  13456. }
  13457. if (($mode != 0) AND ($mode != 1)) {
  13458. $mode = 0;
  13459. }
  13460. $this->overprint = array('OP' => $stroking, 'op' => $nonstroking, 'OPM' => $mode);
  13461. $gs = $this->addExtGState($this->overprint);
  13462. $this->setExtGState($gs);
  13463. }
  13464. /**
  13465. * Get the overprint mode array (OP, op, OPM).
  13466. * (Check the "Entries in a Graphics State Parameter Dictionary" on PDF 32000-1:2008).
  13467. * @return array<string,bool|int>
  13468. * @public
  13469. * @since 5.9.152 (2012-03-23)
  13470. */
  13471. public function getOverprint() {
  13472. return $this->overprint;
  13473. }
  13474. /**
  13475. * Set alpha for stroking (CA) and non-stroking (ca) operations.
  13476. * @param float $stroking Alpha value for stroking operations: real value from 0 (transparent) to 1 (opaque).
  13477. * @param string $bm blend mode, one of the following: Normal, Multiply, Screen, Overlay, Darken, Lighten, ColorDodge, ColorBurn, HardLight, SoftLight, Difference, Exclusion, Hue, Saturation, Color, Luminosity
  13478. * @param float|null $nonstroking Alpha value for non-stroking operations: real value from 0 (transparent) to 1 (opaque).
  13479. * @param boolean $ais
  13480. * @public
  13481. * @since 3.0.000 (2008-03-27)
  13482. */
  13483. public function setAlpha($stroking=1, $bm='Normal', $nonstroking=null, $ais=false) {
  13484. if ($this->pdfa_mode && $this->pdfa_version < 2) {
  13485. // transparency is not allowed in PDF/A-1 mode
  13486. return;
  13487. }
  13488. $stroking = floatval($stroking);
  13489. if (TCPDF_STATIC::empty_string($nonstroking)) {
  13490. // default value if not set
  13491. $nonstroking = $stroking;
  13492. } else {
  13493. $nonstroking = floatval($nonstroking);
  13494. }
  13495. if ($bm[0] == '/') {
  13496. // remove trailing slash
  13497. $bm = substr($bm, 1);
  13498. }
  13499. if (!in_array($bm, array('Normal', 'Multiply', 'Screen', 'Overlay', 'Darken', 'Lighten', 'ColorDodge', 'ColorBurn', 'HardLight', 'SoftLight', 'Difference', 'Exclusion', 'Hue', 'Saturation', 'Color', 'Luminosity'))) {
  13500. $bm = 'Normal';
  13501. }
  13502. $ais = $ais ? true : false;
  13503. $this->alpha = array('CA' => $stroking, 'ca' => $nonstroking, 'BM' => '/'.$bm, 'AIS' => $ais);
  13504. $gs = $this->addExtGState($this->alpha);
  13505. $this->setExtGState($gs);
  13506. }
  13507. /**
  13508. * Get the alpha mode array (CA, ca, BM, AIS).
  13509. * (Check the "Entries in a Graphics State Parameter Dictionary" on PDF 32000-1:2008).
  13510. * @return array<string,bool|string>
  13511. * @public
  13512. * @since 5.9.152 (2012-03-23)
  13513. */
  13514. public function getAlpha() {
  13515. return $this->alpha;
  13516. }
  13517. /**
  13518. * Set the default JPEG compression quality (1-100)
  13519. * @param int $quality JPEG quality, integer between 1 and 100
  13520. * @public
  13521. * @since 3.0.000 (2008-03-27)
  13522. */
  13523. public function setJPEGQuality($quality) {
  13524. if (($quality < 1) OR ($quality > 100)) {
  13525. $quality = 75;
  13526. }
  13527. $this->jpeg_quality = intval($quality);
  13528. }
  13529. /**
  13530. * Set the default number of columns in a row for HTML tables.
  13531. * @param int $cols number of columns
  13532. * @public
  13533. * @since 3.0.014 (2008-06-04)
  13534. */
  13535. public function setDefaultTableColumns($cols=4) {
  13536. $this->default_table_columns = intval($cols);
  13537. }
  13538. /**
  13539. * Set the height of the cell (line height) respect the font height.
  13540. * @param float $h cell proportion respect font height (typical value = 1.25).
  13541. * @public
  13542. * @since 3.0.014 (2008-06-04)
  13543. */
  13544. public function setCellHeightRatio($h) {
  13545. $this->cell_height_ratio = $h;
  13546. }
  13547. /**
  13548. * return the height of cell repect font height.
  13549. * @public
  13550. * @return float
  13551. * @since 4.0.012 (2008-07-24)
  13552. */
  13553. public function getCellHeightRatio() {
  13554. return $this->cell_height_ratio;
  13555. }
  13556. /**
  13557. * Set the PDF version (check PDF reference for valid values).
  13558. * @param string $version PDF document version.
  13559. * @public
  13560. * @since 3.1.000 (2008-06-09)
  13561. */
  13562. public function setPDFVersion($version='1.7') {
  13563. if ($this->pdfa_mode && $this->pdfa_version == 1 ) {
  13564. // PDF/A-1 mode
  13565. $this->PDFVersion = '1.4';
  13566. } elseif ($this->pdfa_mode && $this->pdfa_version >= 2 ) {
  13567. // PDF/A-2 mode
  13568. $this->PDFVersion = '1.7';
  13569. } else {
  13570. $this->PDFVersion = $version;
  13571. }
  13572. }
  13573. /**
  13574. * Set the viewer preferences dictionary controlling the way the document is to be presented on the screen or in print.
  13575. * (see Section 8.1 of PDF reference, "Viewer Preferences").
  13576. * <ul><li>HideToolbar boolean (Optional) A flag specifying whether to hide the viewer application's tool bars when the document is active. Default value: false.</li><li>HideMenubar boolean (Optional) A flag specifying whether to hide the viewer application's menu bar when the document is active. Default value: false.</li><li>HideWindowUI boolean (Optional) A flag specifying whether to hide user interface elements in the document's window (such as scroll bars and navigation controls), leaving only the document's contents displayed. Default value: false.</li><li>FitWindow boolean (Optional) A flag specifying whether to resize the document's window to fit the size of the first displayed page. Default value: false.</li><li>CenterWindow boolean (Optional) A flag specifying whether to position the document's window in the center of the screen. Default value: false.</li><li>DisplayDocTitle boolean (Optional; PDF 1.4) A flag specifying whether the window's title bar should display the document title taken from the Title entry of the document information dictionary (see Section 10.2.1, "Document Information Dictionary"). If false, the title bar should instead display the name of the PDF file containing the document. Default value: false.</li><li>NonFullScreenPageMode name (Optional) The document's page mode, specifying how to display the document on exiting full-screen mode:<ul><li>UseNone Neither document outline nor thumbnail images visible</li><li>UseOutlines Document outline visible</li><li>UseThumbs Thumbnail images visible</li><li>UseOC Optional content group panel visible</li></ul>This entry is meaningful only if the value of the PageMode entry in the catalog dictionary (see Section 3.6.1, "Document Catalog") is FullScreen; it is ignored otherwise. Default value: UseNone.</li><li>ViewArea name (Optional; PDF 1.4) The name of the page boundary representing the area of a page to be displayed when viewing the document on the screen. Valid values are (see Section 10.10.1, "Page Boundaries").:<ul><li>MediaBox</li><li>CropBox (default)</li><li>BleedBox</li><li>TrimBox</li><li>ArtBox</li></ul></li><li>ViewClip name (Optional; PDF 1.4) The name of the page boundary to which the contents of a page are to be clipped when viewing the document on the screen. Valid values are (see Section 10.10.1, "Page Boundaries").:<ul><li>MediaBox</li><li>CropBox (default)</li><li>BleedBox</li><li>TrimBox</li><li>ArtBox</li></ul></li><li>PrintArea name (Optional; PDF 1.4) The name of the page boundary representing the area of a page to be rendered when printing the document. Valid values are (see Section 10.10.1, "Page Boundaries").:<ul><li>MediaBox</li><li>CropBox (default)</li><li>BleedBox</li><li>TrimBox</li><li>ArtBox</li></ul></li><li>PrintClip name (Optional; PDF 1.4) The name of the page boundary to which the contents of a page are to be clipped when printing the document. Valid values are (see Section 10.10.1, "Page Boundaries").:<ul><li>MediaBox</li><li>CropBox (default)</li><li>BleedBox</li><li>TrimBox</li><li>ArtBox</li></ul></li><li>PrintScaling name (Optional; PDF 1.6) The page scaling option to be selected when a print dialog is displayed for this document. Valid values are: <ul><li>None, which indicates that the print dialog should reflect no page scaling</li><li>AppDefault (default), which indicates that applications should use the current print scaling</li></ul></li><li>Duplex name (Optional; PDF 1.7) The paper handling option to use when printing the file from the print dialog. The following values are valid:<ul><li>Simplex - Print single-sided</li><li>DuplexFlipShortEdge - Duplex and flip on the short edge of the sheet</li><li>DuplexFlipLongEdge - Duplex and flip on the long edge of the sheet</li></ul>Default value: none</li><li>PickTrayByPDFSize boolean (Optional; PDF 1.7) A flag specifying whether the PDF page size is used to select the input paper tray. This setting influences only the preset values used to populate the print dialog presented by a PDF viewer application. If PickTrayByPDFSize is true, the check box in the print dialog associated with input paper tray is checked. Note: This setting has no effect on Mac OS systems, which do not provide the ability to pick the input tray by size.</li><li>PrintPageRange array (Optional; PDF 1.7) The page numbers used to initialize the print dialog box when the file is printed. The first page of the PDF file is denoted by 1. Each pair consists of the first and last pages in the sub-range. An odd number of integers causes this entry to be ignored. Negative numbers cause the entire array to be ignored. Default value: as defined by PDF viewer application</li><li>NumCopies integer (Optional; PDF 1.7) The number of copies to be printed when the print dialog is opened for this file. Supported values are the integers 2 through 5. Values outside this range are ignored. Default value: as defined by PDF viewer application, but typically 1</li></ul>
  13577. * @param array $preferences array of options.
  13578. * @author Nicola Asuni
  13579. * @public
  13580. * @since 3.1.000 (2008-06-09)
  13581. */
  13582. public function setViewerPreferences($preferences) {
  13583. $this->viewer_preferences = $preferences;
  13584. }
  13585. /**
  13586. * Paints color transition registration bars
  13587. * @param float $x abscissa of the top left corner of the rectangle.
  13588. * @param float $y ordinate of the top left corner of the rectangle.
  13589. * @param float $w width of the rectangle.
  13590. * @param float $h height of the rectangle.
  13591. * @param boolean $transition if true prints tcolor transitions to white.
  13592. * @param boolean $vertical if true prints bar vertically.
  13593. * @param string $colors colors to print separated by comma. Valid values are: A,W,R,G,B,C,M,Y,K,RGB,CMYK,ALL,ALLSPOT,<SPOT_COLOR_NAME>. Where: A = grayscale black, W = grayscale white, R = RGB red, G RGB green, B RGB blue, C = CMYK cyan, M = CMYK magenta, Y = CMYK yellow, K = CMYK key/black, RGB = RGB registration color, CMYK = CMYK registration color, ALL = Spot registration color, ALLSPOT = print all defined spot colors, <SPOT_COLOR_NAME> = name of the spot color to print.
  13594. * @author Nicola Asuni
  13595. * @since 4.9.000 (2010-03-26)
  13596. * @public
  13597. */
  13598. public function colorRegistrationBar($x, $y, $w, $h, $transition=true, $vertical=false, $colors='A,R,G,B,C,M,Y,K') {
  13599. if (strpos($colors, 'ALLSPOT') !== false) {
  13600. // expand spot colors
  13601. $spot_colors = '';
  13602. foreach ($this->spot_colors as $spot_color_name => $v) {
  13603. $spot_colors .= ','.$spot_color_name;
  13604. }
  13605. if (!empty($spot_colors)) {
  13606. $spot_colors = substr($spot_colors, 1);
  13607. $colors = str_replace('ALLSPOT', $spot_colors, $colors);
  13608. } else {
  13609. $colors = str_replace('ALLSPOT', 'NONE', $colors);
  13610. }
  13611. }
  13612. $bars = explode(',', $colors);
  13613. $numbars = count($bars); // number of bars to print
  13614. if ($numbars <= 0) {
  13615. return;
  13616. }
  13617. // set bar measures
  13618. if ($vertical) {
  13619. $coords = array(0, 0, 0, 1);
  13620. $wb = $w / $numbars; // bar width
  13621. $hb = $h; // bar height
  13622. $xd = $wb; // delta x
  13623. $yd = 0; // delta y
  13624. } else {
  13625. $coords = array(1, 0, 0, 0);
  13626. $wb = $w; // bar width
  13627. $hb = $h / $numbars; // bar height
  13628. $xd = 0; // delta x
  13629. $yd = $hb; // delta y
  13630. }
  13631. $xb = $x;
  13632. $yb = $y;
  13633. foreach ($bars as $col) {
  13634. switch ($col) {
  13635. // set transition colors
  13636. case 'A': { // BLACK (GRAYSCALE)
  13637. $col_a = array(255);
  13638. $col_b = array(0);
  13639. break;
  13640. }
  13641. case 'W': { // WHITE (GRAYSCALE)
  13642. $col_a = array(0);
  13643. $col_b = array(255);
  13644. break;
  13645. }
  13646. case 'R': { // RED (RGB)
  13647. $col_a = array(255,255,255);
  13648. $col_b = array(255,0,0);
  13649. break;
  13650. }
  13651. case 'G': { // GREEN (RGB)
  13652. $col_a = array(255,255,255);
  13653. $col_b = array(0,255,0);
  13654. break;
  13655. }
  13656. case 'B': { // BLUE (RGB)
  13657. $col_a = array(255,255,255);
  13658. $col_b = array(0,0,255);
  13659. break;
  13660. }
  13661. case 'C': { // CYAN (CMYK)
  13662. $col_a = array(0,0,0,0);
  13663. $col_b = array(100,0,0,0);
  13664. break;
  13665. }
  13666. case 'M': { // MAGENTA (CMYK)
  13667. $col_a = array(0,0,0,0);
  13668. $col_b = array(0,100,0,0);
  13669. break;
  13670. }
  13671. case 'Y': { // YELLOW (CMYK)
  13672. $col_a = array(0,0,0,0);
  13673. $col_b = array(0,0,100,0);
  13674. break;
  13675. }
  13676. case 'K': { // KEY - BLACK (CMYK)
  13677. $col_a = array(0,0,0,0);
  13678. $col_b = array(0,0,0,100);
  13679. break;
  13680. }
  13681. case 'RGB': { // BLACK REGISTRATION (RGB)
  13682. $col_a = array(255,255,255);
  13683. $col_b = array(0,0,0);
  13684. break;
  13685. }
  13686. case 'CMYK': { // BLACK REGISTRATION (CMYK)
  13687. $col_a = array(0,0,0,0);
  13688. $col_b = array(100,100,100,100);
  13689. break;
  13690. }
  13691. case 'ALL': { // SPOT COLOR REGISTRATION
  13692. $col_a = array(0,0,0,0,'None');
  13693. $col_b = array(100,100,100,100,'All');
  13694. break;
  13695. }
  13696. case 'NONE': { // SKIP THIS COLOR
  13697. $col_a = array(0,0,0,0,'None');
  13698. $col_b = array(0,0,0,0,'None');
  13699. break;
  13700. }
  13701. default: { // SPECIFIC SPOT COLOR NAME
  13702. $col_a = array(0,0,0,0,'None');
  13703. $col_b = TCPDF_COLORS::getSpotColor($col, $this->spot_colors);
  13704. if ($col_b === false) {
  13705. // in case of error defaults to the registration color
  13706. $col_b = array(100,100,100,100,'All');
  13707. }
  13708. break;
  13709. }
  13710. }
  13711. if ($col != 'NONE') {
  13712. if ($transition) {
  13713. // color gradient
  13714. $this->LinearGradient($xb, $yb, $wb, $hb, $col_a, $col_b, $coords);
  13715. } else {
  13716. $this->setFillColorArray($col_b);
  13717. // colored rectangle
  13718. $this->Rect($xb, $yb, $wb, $hb, 'F', array());
  13719. }
  13720. $xb += $xd;
  13721. $yb += $yd;
  13722. }
  13723. }
  13724. }
  13725. /**
  13726. * Paints crop marks.
  13727. * @param float $x abscissa of the crop mark center.
  13728. * @param float $y ordinate of the crop mark center.
  13729. * @param float $w width of the crop mark.
  13730. * @param float $h height of the crop mark.
  13731. * @param string $type type of crop mark, one symbol per type separated by comma: T = TOP, F = BOTTOM, L = LEFT, R = RIGHT, TL = A = TOP-LEFT, TR = B = TOP-RIGHT, BL = C = BOTTOM-LEFT, BR = D = BOTTOM-RIGHT.
  13732. * @param array $color crop mark color (default spot registration color).
  13733. * @author Nicola Asuni
  13734. * @since 4.9.000 (2010-03-26)
  13735. * @public
  13736. */
  13737. public function cropMark($x, $y, $w, $h, $type='T,R,B,L', $color=array(100,100,100,100,'All')) {
  13738. $this->setLineStyle(array('width' => (0.5 / $this->k), 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => $color));
  13739. $type = strtoupper($type);
  13740. $type = preg_replace('/[^A-Z\-\,]*/', '', $type);
  13741. // split type in single components
  13742. $type = str_replace('-', ',', $type);
  13743. $type = str_replace('TL', 'T,L', $type);
  13744. $type = str_replace('TR', 'T,R', $type);
  13745. $type = str_replace('BL', 'F,L', $type);
  13746. $type = str_replace('BR', 'F,R', $type);
  13747. $type = str_replace('A', 'T,L', $type);
  13748. $type = str_replace('B', 'T,R', $type);
  13749. $type = str_replace('T,RO', 'BO', $type);
  13750. $type = str_replace('C', 'F,L', $type);
  13751. $type = str_replace('D', 'F,R', $type);
  13752. $crops = explode(',', strtoupper($type));
  13753. // remove duplicates
  13754. $crops = array_unique($crops);
  13755. $dw = ($w / 4); // horizontal space to leave before the intersection point
  13756. $dh = ($h / 4); // vertical space to leave before the intersection point
  13757. foreach ($crops as $crop) {
  13758. switch ($crop) {
  13759. case 'T':
  13760. case 'TOP': {
  13761. $x1 = $x;
  13762. $y1 = ($y - $h);
  13763. $x2 = $x;
  13764. $y2 = ($y - $dh);
  13765. break;
  13766. }
  13767. case 'F':
  13768. case 'BOTTOM': {
  13769. $x1 = $x;
  13770. $y1 = ($y + $dh);
  13771. $x2 = $x;
  13772. $y2 = ($y + $h);
  13773. break;
  13774. }
  13775. case 'L':
  13776. case 'LEFT': {
  13777. $x1 = ($x - $w);
  13778. $y1 = $y;
  13779. $x2 = ($x - $dw);
  13780. $y2 = $y;
  13781. break;
  13782. }
  13783. case 'R':
  13784. case 'RIGHT': {
  13785. $x1 = ($x + $dw);
  13786. $y1 = $y;
  13787. $x2 = ($x + $w);
  13788. $y2 = $y;
  13789. break;
  13790. }
  13791. }
  13792. $this->Line($x1, $y1, $x2, $y2);
  13793. }
  13794. }
  13795. /**
  13796. * Paints a registration mark
  13797. * @param float $x abscissa of the registration mark center.
  13798. * @param float $y ordinate of the registration mark center.
  13799. * @param float $r radius of the crop mark.
  13800. * @param boolean $double if true print two concentric crop marks.
  13801. * @param array $cola crop mark color (default spot registration color 'All').
  13802. * @param array $colb second crop mark color (default spot registration color 'None').
  13803. * @author Nicola Asuni
  13804. * @since 4.9.000 (2010-03-26)
  13805. * @public
  13806. */
  13807. public function registrationMark($x, $y, $r, $double=false, $cola=array(100,100,100,100,'All'), $colb=array(0,0,0,0,'None')) {
  13808. $line_style = array('width' => max((0.5 / $this->k),($r / 30)), 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => $cola);
  13809. $this->setFillColorArray($cola);
  13810. $this->PieSector($x, $y, $r, 90, 180, 'F');
  13811. $this->PieSector($x, $y, $r, 270, 360, 'F');
  13812. $this->Circle($x, $y, $r, 0, 360, 'C', $line_style, array(), 8);
  13813. if ($double) {
  13814. $ri = $r * 0.5;
  13815. $this->setFillColorArray($colb);
  13816. $this->PieSector($x, $y, $ri, 90, 180, 'F');
  13817. $this->PieSector($x, $y, $ri, 270, 360, 'F');
  13818. $this->setFillColorArray($cola);
  13819. $this->PieSector($x, $y, $ri, 0, 90, 'F');
  13820. $this->PieSector($x, $y, $ri, 180, 270, 'F');
  13821. $this->Circle($x, $y, $ri, 0, 360, 'C', $line_style, array(), 8);
  13822. }
  13823. }
  13824. /**
  13825. * Paints a CMYK registration mark
  13826. * @param float $x abscissa of the registration mark center.
  13827. * @param float $y ordinate of the registration mark center.
  13828. * @param float $r radius of the crop mark.
  13829. * @author Nicola Asuni
  13830. * @since 6.0.038 (2013-09-30)
  13831. * @public
  13832. */
  13833. public function registrationMarkCMYK($x, $y, $r) {
  13834. // line width
  13835. $lw = max((0.5 / $this->k),($r / 8));
  13836. // internal radius
  13837. $ri = ($r * 0.6);
  13838. // external radius
  13839. $re = ($r * 1.3);
  13840. // Cyan
  13841. $this->setFillColorArray(array(100,0,0,0));
  13842. $this->PieSector($x, $y, $ri, 270, 360, 'F');
  13843. // Magenta
  13844. $this->setFillColorArray(array(0,100,0,0));
  13845. $this->PieSector($x, $y, $ri, 0, 90, 'F');
  13846. // Yellow
  13847. $this->setFillColorArray(array(0,0,100,0));
  13848. $this->PieSector($x, $y, $ri, 90, 180, 'F');
  13849. // Key - black
  13850. $this->setFillColorArray(array(0,0,0,100));
  13851. $this->PieSector($x, $y, $ri, 180, 270, 'F');
  13852. // registration color
  13853. $line_style = array('width' => $lw, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(100,100,100,100,'All'));
  13854. $this->setFillColorArray(array(100,100,100,100,'All'));
  13855. // external circle
  13856. $this->Circle($x, $y, $r, 0, 360, 'C', $line_style, array(), 8);
  13857. // cross lines
  13858. $this->Line($x, ($y - $re), $x, ($y - $ri));
  13859. $this->Line($x, ($y + $ri), $x, ($y + $re));
  13860. $this->Line(($x - $re), $y, ($x - $ri), $y);
  13861. $this->Line(($x + $ri), $y, ($x + $re), $y);
  13862. }
  13863. /**
  13864. * Paints a linear colour gradient.
  13865. * @param float $x abscissa of the top left corner of the rectangle.
  13866. * @param float $y ordinate of the top left corner of the rectangle.
  13867. * @param float $w width of the rectangle.
  13868. * @param float $h height of the rectangle.
  13869. * @param array $col1 first color (Grayscale, RGB or CMYK components).
  13870. * @param array $col2 second color (Grayscale, RGB or CMYK components).
  13871. * @param array $coords array of the form (x1, y1, x2, y2) which defines the gradient vector (see linear_gradient_coords.jpg). The default value is from left to right (x1=0, y1=0, x2=1, y2=0).
  13872. * @author Andreas W\FCrmser, Nicola Asuni
  13873. * @since 3.1.000 (2008-06-09)
  13874. * @public
  13875. */
  13876. public function LinearGradient($x, $y, $w, $h, $col1=array(), $col2=array(), $coords=array(0,0,1,0)) {
  13877. $this->Clip($x, $y, $w, $h);
  13878. $this->Gradient(2, $coords, array(array('color' => $col1, 'offset' => 0, 'exponent' => 1), array('color' => $col2, 'offset' => 1, 'exponent' => 1)), array(), false);
  13879. }
  13880. /**
  13881. * Paints a radial colour gradient.
  13882. * @param float $x abscissa of the top left corner of the rectangle.
  13883. * @param float $y ordinate of the top left corner of the rectangle.
  13884. * @param float $w width of the rectangle.
  13885. * @param float $h height of the rectangle.
  13886. * @param array $col1 first color (Grayscale, RGB or CMYK components).
  13887. * @param array $col2 second color (Grayscale, RGB or CMYK components).
  13888. * @param array $coords array of the form (fx, fy, cx, cy, r) where (fx, fy) is the starting point of the gradient with color1, (cx, cy) is the center of the circle with color2, and r is the radius of the circle (see radial_gradient_coords.jpg). (fx, fy) should be inside the circle, otherwise some areas will not be defined.
  13889. * @author Andreas W\FCrmser, Nicola Asuni
  13890. * @since 3.1.000 (2008-06-09)
  13891. * @public
  13892. */
  13893. public function RadialGradient($x, $y, $w, $h, $col1=array(), $col2=array(), $coords=array(0.5,0.5,0.5,0.5,1)) {
  13894. $this->Clip($x, $y, $w, $h);
  13895. $this->Gradient(3, $coords, array(array('color' => $col1, 'offset' => 0, 'exponent' => 1), array('color' => $col2, 'offset' => 1, 'exponent' => 1)), array(), false);
  13896. }
  13897. /**
  13898. * Paints a coons patch mesh.
  13899. * @param float $x abscissa of the top left corner of the rectangle.
  13900. * @param float $y ordinate of the top left corner of the rectangle.
  13901. * @param float $w width of the rectangle.
  13902. * @param float $h height of the rectangle.
  13903. * @param array $col1 first color (lower left corner) (RGB components).
  13904. * @param array $col2 second color (lower right corner) (RGB components).
  13905. * @param array $col3 third color (upper right corner) (RGB components).
  13906. * @param array $col4 fourth color (upper left corner) (RGB components).
  13907. * @param array $coords <ul><li>for one patch mesh: array(float x1, float y1, .... float x12, float y12): 12 pairs of coordinates (normally from 0 to 1) which specify the Bezier control points that define the patch. First pair is the lower left edge point, next is its right control point (control point 2). Then the other points are defined in the order: control point 1, edge point, control point 2 going counter-clockwise around the patch. Last (x12, y12) is the first edge point's left control point (control point 1).</li><li>for two or more patch meshes: array[number of patches]: arrays with the following keys for each patch: f: where to put that patch (0 = first patch, 1, 2, 3 = right, top and left of precedent patch - I didn't figure this out completely - just try and error ;-) points: 12 pairs of coordinates of the Bezier control points as above for the first patch, 8 pairs of coordinates for the following patches, ignoring the coordinates already defined by the precedent patch (I also didn't figure out the order of these - also: try and see what's happening) colors: must be 4 colors for the first patch, 2 colors for the following patches</li></ul>
  13908. * @param array $coords_min minimum value used by the coordinates. If a coordinate's value is smaller than this it will be cut to coords_min. default: 0
  13909. * @param array $coords_max maximum value used by the coordinates. If a coordinate's value is greater than this it will be cut to coords_max. default: 1
  13910. * @param boolean $antialias A flag indicating whether to filter the shading function to prevent aliasing artifacts.
  13911. * @author Andreas W\FCrmser, Nicola Asuni
  13912. * @since 3.1.000 (2008-06-09)
  13913. * @public
  13914. */
  13915. public function CoonsPatchMesh($x, $y, $w, $h, $col1=array(), $col2=array(), $col3=array(), $col4=array(), $coords=array(0.00,0.0,0.33,0.00,0.67,0.00,1.00,0.00,1.00,0.33,1.00,0.67,1.00,1.00,0.67,1.00,0.33,1.00,0.00,1.00,0.00,0.67,0.00,0.33), $coords_min=0, $coords_max=1, $antialias=false) {
  13916. if (($this->pdfa_mode && $this->pdfa_version < 2) OR ($this->state != 2)) {
  13917. return;
  13918. }
  13919. $this->Clip($x, $y, $w, $h);
  13920. $n = count($this->gradients) + 1;
  13921. $this->gradients[$n] = array();
  13922. $this->gradients[$n]['type'] = 6; //coons patch mesh
  13923. $this->gradients[$n]['coords'] = array();
  13924. $this->gradients[$n]['antialias'] = $antialias;
  13925. $this->gradients[$n]['colors'] = array();
  13926. $this->gradients[$n]['transparency'] = false;
  13927. //check the coords array if it is the simple array or the multi patch array
  13928. if (!isset($coords[0]['f'])) {
  13929. //simple array -> convert to multi patch array
  13930. if (!isset($col1[1])) {
  13931. $col1[1] = $col1[2] = $col1[0];
  13932. }
  13933. if (!isset($col2[1])) {
  13934. $col2[1] = $col2[2] = $col2[0];
  13935. }
  13936. if (!isset($col3[1])) {
  13937. $col3[1] = $col3[2] = $col3[0];
  13938. }
  13939. if (!isset($col4[1])) {
  13940. $col4[1] = $col4[2] = $col4[0];
  13941. }
  13942. $patch_array[0]['f'] = 0;
  13943. $patch_array[0]['points'] = $coords;
  13944. $patch_array[0]['colors'][0]['r'] = $col1[0];
  13945. $patch_array[0]['colors'][0]['g'] = $col1[1];
  13946. $patch_array[0]['colors'][0]['b'] = $col1[2];
  13947. $patch_array[0]['colors'][1]['r'] = $col2[0];
  13948. $patch_array[0]['colors'][1]['g'] = $col2[1];
  13949. $patch_array[0]['colors'][1]['b'] = $col2[2];
  13950. $patch_array[0]['colors'][2]['r'] = $col3[0];
  13951. $patch_array[0]['colors'][2]['g'] = $col3[1];
  13952. $patch_array[0]['colors'][2]['b'] = $col3[2];
  13953. $patch_array[0]['colors'][3]['r'] = $col4[0];
  13954. $patch_array[0]['colors'][3]['g'] = $col4[1];
  13955. $patch_array[0]['colors'][3]['b'] = $col4[2];
  13956. } else {
  13957. //multi patch array
  13958. $patch_array = $coords;
  13959. }
  13960. $bpcd = 65535; //16 bits per coordinate
  13961. //build the data stream
  13962. $this->gradients[$n]['stream'] = '';
  13963. $count_patch = count($patch_array);
  13964. for ($i=0; $i < $count_patch; ++$i) {
  13965. $this->gradients[$n]['stream'] .= chr($patch_array[$i]['f']); //start with the edge flag as 8 bit
  13966. $count_points = count($patch_array[$i]['points']);
  13967. for ($j=0; $j < $count_points; ++$j) {
  13968. //each point as 16 bit
  13969. $patch_array[$i]['points'][$j] = (($patch_array[$i]['points'][$j] - $coords_min) / ($coords_max - $coords_min)) * $bpcd;
  13970. if ($patch_array[$i]['points'][$j] < 0) {
  13971. $patch_array[$i]['points'][$j] = 0;
  13972. }
  13973. if ($patch_array[$i]['points'][$j] > $bpcd) {
  13974. $patch_array[$i]['points'][$j] = $bpcd;
  13975. }
  13976. $this->gradients[$n]['stream'] .= chr((int) floor($patch_array[$i]['points'][$j] / 256));
  13977. $this->gradients[$n]['stream'] .= chr((int) floor(intval($patch_array[$i]['points'][$j]) % 256));
  13978. }
  13979. $count_cols = count($patch_array[$i]['colors']);
  13980. for ($j=0; $j < $count_cols; ++$j) {
  13981. //each color component as 8 bit
  13982. $this->gradients[$n]['stream'] .= chr($patch_array[$i]['colors'][$j]['r']);
  13983. $this->gradients[$n]['stream'] .= chr($patch_array[$i]['colors'][$j]['g']);
  13984. $this->gradients[$n]['stream'] .= chr($patch_array[$i]['colors'][$j]['b']);
  13985. }
  13986. }
  13987. //paint the gradient
  13988. $this->_out('/Sh'.$n.' sh');
  13989. //restore previous Graphic State
  13990. $this->_outRestoreGraphicsState();
  13991. if ($this->inxobj) {
  13992. // we are inside an XObject template
  13993. $this->xobjects[$this->xobjid]['gradients'][$n] = $this->gradients[$n];
  13994. }
  13995. }
  13996. /**
  13997. * Set a rectangular clipping area.
  13998. * @param float $x abscissa of the top left corner of the rectangle (or top right corner for RTL mode).
  13999. * @param float $y ordinate of the top left corner of the rectangle.
  14000. * @param float $w width of the rectangle.
  14001. * @param float $h height of the rectangle.
  14002. * @author Andreas W\FCrmser, Nicola Asuni
  14003. * @since 3.1.000 (2008-06-09)
  14004. * @protected
  14005. */
  14006. protected function Clip($x, $y, $w, $h) {
  14007. if ($this->state != 2) {
  14008. return;
  14009. }
  14010. if ($this->rtl) {
  14011. $x = $this->w - $x - $w;
  14012. }
  14013. //save current Graphic State
  14014. $s = 'q';
  14015. //set clipping area
  14016. $s .= sprintf(' %F %F %F %F re W n', $x*$this->k, ($this->h-$y)*$this->k, $w*$this->k, -$h*$this->k);
  14017. //set up transformation matrix for gradient
  14018. $s .= sprintf(' %F 0 0 %F %F %F cm', $w*$this->k, $h*$this->k, $x*$this->k, ($this->h-($y+$h))*$this->k);
  14019. $this->_out($s);
  14020. }
  14021. /**
  14022. * Output gradient.
  14023. * @param int $type type of gradient (1 Function-based shading; 2 Axial shading; 3 Radial shading; 4 Free-form Gouraud-shaded triangle mesh; 5 Lattice-form Gouraud-shaded triangle mesh; 6 Coons patch mesh; 7 Tensor-product patch mesh). (Not all types are currently supported)
  14024. * @param array $coords array of coordinates.
  14025. * @param array $stops array gradient color components: color = array of GRAY, RGB or CMYK color components; offset = (0 to 1) represents a location along the gradient vector; exponent = exponent of the exponential interpolation function (default = 1).
  14026. * @param array $background An array of colour components appropriate to the colour space, specifying a single background colour value.
  14027. * @param boolean $antialias A flag indicating whether to filter the shading function to prevent aliasing artifacts.
  14028. * @author Nicola Asuni
  14029. * @since 3.1.000 (2008-06-09)
  14030. * @public
  14031. */
  14032. public function Gradient($type, $coords, $stops, $background=array(), $antialias=false) {
  14033. if (($this->pdfa_mode && $this->pdfa_version < 2) OR ($this->state != 2)) {
  14034. return;
  14035. }
  14036. $n = count($this->gradients) + 1;
  14037. $this->gradients[$n] = array();
  14038. $this->gradients[$n]['type'] = $type;
  14039. $this->gradients[$n]['coords'] = $coords;
  14040. $this->gradients[$n]['antialias'] = $antialias;
  14041. $this->gradients[$n]['colors'] = array();
  14042. $this->gradients[$n]['transparency'] = false;
  14043. // color space
  14044. $numcolspace = count($stops[0]['color']);
  14045. $bcolor = array_values($background);
  14046. switch($numcolspace) {
  14047. case 5: // SPOT
  14048. case 4: { // CMYK
  14049. $this->gradients[$n]['colspace'] = 'DeviceCMYK';
  14050. if (!empty($background)) {
  14051. $this->gradients[$n]['background'] = sprintf('%F %F %F %F', $bcolor[0]/100, $bcolor[1]/100, $bcolor[2]/100, $bcolor[3]/100);
  14052. }
  14053. break;
  14054. }
  14055. case 3: { // RGB
  14056. $this->gradients[$n]['colspace'] = 'DeviceRGB';
  14057. if (!empty($background)) {
  14058. $this->gradients[$n]['background'] = sprintf('%F %F %F', $bcolor[0]/255, $bcolor[1]/255, $bcolor[2]/255);
  14059. }
  14060. break;
  14061. }
  14062. case 1: { // GRAY SCALE
  14063. $this->gradients[$n]['colspace'] = 'DeviceGray';
  14064. if (!empty($background)) {
  14065. $this->gradients[$n]['background'] = sprintf('%F', $bcolor[0]/255);
  14066. }
  14067. break;
  14068. }
  14069. }
  14070. $num_stops = count($stops);
  14071. $last_stop_id = $num_stops - 1;
  14072. foreach ($stops as $key => $stop) {
  14073. $this->gradients[$n]['colors'][$key] = array();
  14074. // offset represents a location along the gradient vector
  14075. if (isset($stop['offset'])) {
  14076. $this->gradients[$n]['colors'][$key]['offset'] = $stop['offset'];
  14077. } else {
  14078. if ($key == 0) {
  14079. $this->gradients[$n]['colors'][$key]['offset'] = 0;
  14080. } elseif ($key == $last_stop_id) {
  14081. $this->gradients[$n]['colors'][$key]['offset'] = 1;
  14082. } else {
  14083. $offsetstep = (1 - $this->gradients[$n]['colors'][($key - 1)]['offset']) / ($num_stops - $key);
  14084. $this->gradients[$n]['colors'][$key]['offset'] = $this->gradients[$n]['colors'][($key - 1)]['offset'] + $offsetstep;
  14085. }
  14086. }
  14087. if (isset($stop['opacity'])) {
  14088. $this->gradients[$n]['colors'][$key]['opacity'] = $stop['opacity'];
  14089. if ((!($this->pdfa_mode && $this->pdfa_version < 2)) AND ($stop['opacity'] < 1)) {
  14090. $this->gradients[$n]['transparency'] = true;
  14091. }
  14092. } else {
  14093. $this->gradients[$n]['colors'][$key]['opacity'] = 1;
  14094. }
  14095. // exponent for the exponential interpolation function
  14096. if (isset($stop['exponent'])) {
  14097. $this->gradients[$n]['colors'][$key]['exponent'] = $stop['exponent'];
  14098. } else {
  14099. $this->gradients[$n]['colors'][$key]['exponent'] = 1;
  14100. }
  14101. // set colors
  14102. $color = array_values($stop['color']);
  14103. switch($numcolspace) {
  14104. case 5: // SPOT
  14105. case 4: { // CMYK
  14106. $this->gradients[$n]['colors'][$key]['color'] = sprintf('%F %F %F %F', $color[0]/100, $color[1]/100, $color[2]/100, $color[3]/100);
  14107. break;
  14108. }
  14109. case 3: { // RGB
  14110. $this->gradients[$n]['colors'][$key]['color'] = sprintf('%F %F %F', $color[0]/255, $color[1]/255, $color[2]/255);
  14111. break;
  14112. }
  14113. case 1: { // GRAY SCALE
  14114. $this->gradients[$n]['colors'][$key]['color'] = sprintf('%F', $color[0]/255);
  14115. break;
  14116. }
  14117. }
  14118. }
  14119. if ($this->gradients[$n]['transparency']) {
  14120. // paint luminosity gradient
  14121. $this->_out('/TGS'.$n.' gs');
  14122. }
  14123. //paint the gradient
  14124. $this->_out('/Sh'.$n.' sh');
  14125. //restore previous Graphic State
  14126. $this->_outRestoreGraphicsState();
  14127. if ($this->inxobj) {
  14128. // we are inside an XObject template
  14129. $this->xobjects[$this->xobjid]['gradients'][$n] = $this->gradients[$n];
  14130. }
  14131. }
  14132. /**
  14133. * Output gradient shaders.
  14134. * @author Nicola Asuni
  14135. * @since 3.1.000 (2008-06-09)
  14136. * @protected
  14137. */
  14138. function _putshaders() {
  14139. if ($this->pdfa_mode && $this->pdfa_version < 2) {
  14140. return;
  14141. }
  14142. $idt = count($this->gradients); //index for transparency gradients
  14143. foreach ($this->gradients as $id => $grad) {
  14144. if (($grad['type'] == 2) OR ($grad['type'] == 3)) {
  14145. $fc = $this->_newobj();
  14146. $out = '<<';
  14147. $out .= ' /FunctionType 3';
  14148. $out .= ' /Domain [0 1]';
  14149. $functions = '';
  14150. $bounds = '';
  14151. $encode = '';
  14152. $i = 1;
  14153. $num_cols = count($grad['colors']);
  14154. $lastcols = $num_cols - 1;
  14155. for ($i = 1; $i < $num_cols; ++$i) {
  14156. $functions .= ($fc + $i).' 0 R ';
  14157. if ($i < $lastcols) {
  14158. $bounds .= sprintf('%F ', $grad['colors'][$i]['offset']);
  14159. }
  14160. $encode .= '0 1 ';
  14161. }
  14162. $out .= ' /Functions ['.trim($functions).']';
  14163. $out .= ' /Bounds ['.trim($bounds).']';
  14164. $out .= ' /Encode ['.trim($encode).']';
  14165. $out .= ' >>';
  14166. $out .= "\n".'endobj';
  14167. $this->_out($out);
  14168. for ($i = 1; $i < $num_cols; ++$i) {
  14169. $this->_newobj();
  14170. $out = '<<';
  14171. $out .= ' /FunctionType 2';
  14172. $out .= ' /Domain [0 1]';
  14173. $out .= ' /C0 ['.$grad['colors'][($i - 1)]['color'].']';
  14174. $out .= ' /C1 ['.$grad['colors'][$i]['color'].']';
  14175. $out .= ' /N '.$grad['colors'][$i]['exponent'];
  14176. $out .= ' >>';
  14177. $out .= "\n".'endobj';
  14178. $this->_out($out);
  14179. }
  14180. // set transparency functions
  14181. if ($grad['transparency']) {
  14182. $ft = $this->_newobj();
  14183. $out = '<<';
  14184. $out .= ' /FunctionType 3';
  14185. $out .= ' /Domain [0 1]';
  14186. $functions = '';
  14187. $i = 1;
  14188. $num_cols = count($grad['colors']);
  14189. for ($i = 1; $i < $num_cols; ++$i) {
  14190. $functions .= ($ft + $i).' 0 R ';
  14191. }
  14192. $out .= ' /Functions ['.trim($functions).']';
  14193. $out .= ' /Bounds ['.trim($bounds).']';
  14194. $out .= ' /Encode ['.trim($encode).']';
  14195. $out .= ' >>';
  14196. $out .= "\n".'endobj';
  14197. $this->_out($out);
  14198. for ($i = 1; $i < $num_cols; ++$i) {
  14199. $this->_newobj();
  14200. $out = '<<';
  14201. $out .= ' /FunctionType 2';
  14202. $out .= ' /Domain [0 1]';
  14203. $out .= ' /C0 ['.$grad['colors'][($i - 1)]['opacity'].']';
  14204. $out .= ' /C1 ['.$grad['colors'][$i]['opacity'].']';
  14205. $out .= ' /N '.$grad['colors'][$i]['exponent'];
  14206. $out .= ' >>';
  14207. $out .= "\n".'endobj';
  14208. $this->_out($out);
  14209. }
  14210. }
  14211. }
  14212. // set shading object
  14213. $this->_newobj();
  14214. $out = '<< /ShadingType '.$grad['type'];
  14215. if (isset($grad['colspace'])) {
  14216. $out .= ' /ColorSpace /'.$grad['colspace'];
  14217. } else {
  14218. $out .= ' /ColorSpace /DeviceRGB';
  14219. }
  14220. if (isset($grad['background']) AND !empty($grad['background'])) {
  14221. $out .= ' /Background ['.$grad['background'].']';
  14222. }
  14223. if (isset($grad['antialias']) AND ($grad['antialias'] === true)) {
  14224. $out .= ' /AntiAlias true';
  14225. }
  14226. if ($grad['type'] == 2) {
  14227. $out .= ' '.sprintf('/Coords [%F %F %F %F]', $grad['coords'][0], $grad['coords'][1], $grad['coords'][2], $grad['coords'][3]);
  14228. $out .= ' /Domain [0 1]';
  14229. $out .= ' /Function '.$fc.' 0 R';
  14230. $out .= ' /Extend [true true]';
  14231. $out .= ' >>';
  14232. } elseif ($grad['type'] == 3) {
  14233. //x0, y0, r0, x1, y1, r1
  14234. //at this this time radius of inner circle is 0
  14235. $out .= ' '.sprintf('/Coords [%F %F 0 %F %F %F]', $grad['coords'][0], $grad['coords'][1], $grad['coords'][2], $grad['coords'][3], $grad['coords'][4]);
  14236. $out .= ' /Domain [0 1]';
  14237. $out .= ' /Function '.$fc.' 0 R';
  14238. $out .= ' /Extend [true true]';
  14239. $out .= ' >>';
  14240. } elseif ($grad['type'] == 6) {
  14241. $out .= ' /BitsPerCoordinate 16';
  14242. $out .= ' /BitsPerComponent 8';
  14243. $out .= ' /Decode[0 1 0 1 0 1 0 1 0 1]';
  14244. $out .= ' /BitsPerFlag 8';
  14245. $stream = $this->_getrawstream($grad['stream']);
  14246. $out .= ' /Length '.strlen($stream);
  14247. $out .= ' >>';
  14248. $out .= ' stream'."\n".$stream."\n".'endstream';
  14249. }
  14250. $out .= "\n".'endobj';
  14251. $this->_out($out);
  14252. if ($grad['transparency']) {
  14253. $shading_transparency = preg_replace('/\/ColorSpace \/[^\s]+/si', '/ColorSpace /DeviceGray', $out);
  14254. $shading_transparency = preg_replace('/\/Function [0-9]+ /si', '/Function '.$ft.' ', $shading_transparency);
  14255. }
  14256. $this->gradients[$id]['id'] = $this->n;
  14257. // set pattern object
  14258. $this->_newobj();
  14259. $out = '<< /Type /Pattern /PatternType 2';
  14260. $out .= ' /Shading '.$this->gradients[$id]['id'].' 0 R';
  14261. $out .= ' >>';
  14262. $out .= "\n".'endobj';
  14263. $this->_out($out);
  14264. $this->gradients[$id]['pattern'] = $this->n;
  14265. // set shading and pattern for transparency mask
  14266. if ($grad['transparency']) {
  14267. // luminosity pattern
  14268. $idgs = $id + $idt;
  14269. $this->_newobj();
  14270. $this->_out($shading_transparency);
  14271. $this->gradients[$idgs]['id'] = $this->n;
  14272. $this->_newobj();
  14273. $out = '<< /Type /Pattern /PatternType 2';
  14274. $out .= ' /Shading '.$this->gradients[$idgs]['id'].' 0 R';
  14275. $out .= ' >>';
  14276. $out .= "\n".'endobj';
  14277. $this->_out($out);
  14278. $this->gradients[$idgs]['pattern'] = $this->n;
  14279. // luminosity XObject
  14280. $oid = $this->_newobj();
  14281. $this->xobjects['LX'.$oid] = array('n' => $oid);
  14282. $filter = '';
  14283. $stream = 'q /a0 gs /Pattern cs /p'.$idgs.' scn 0 0 '.$this->wPt.' '.$this->hPt.' re f Q';
  14284. if ($this->compress) {
  14285. $filter = ' /Filter /FlateDecode';
  14286. $stream = gzcompress($stream);
  14287. }
  14288. $stream = $this->_getrawstream($stream);
  14289. $out = '<< /Type /XObject /Subtype /Form /FormType 1'.$filter;
  14290. $out .= ' /Length '.strlen($stream);
  14291. $rect = sprintf('%F %F', $this->wPt, $this->hPt);
  14292. $out .= ' /BBox [0 0 '.$rect.']';
  14293. $out .= ' /Group << /Type /Group /S /Transparency /CS /DeviceGray >>';
  14294. $out .= ' /Resources <<';
  14295. $out .= ' /ExtGState << /a0 << /ca 1 /CA 1 >> >>';
  14296. $out .= ' /Pattern << /p'.$idgs.' '.$this->gradients[$idgs]['pattern'].' 0 R >>';
  14297. $out .= ' >>';
  14298. $out .= ' >> ';
  14299. $out .= ' stream'."\n".$stream."\n".'endstream';
  14300. $out .= "\n".'endobj';
  14301. $this->_out($out);
  14302. // SMask
  14303. $this->_newobj();
  14304. $out = '<< /Type /Mask /S /Luminosity /G '.($this->n - 1).' 0 R >>'."\n".'endobj';
  14305. $this->_out($out);
  14306. // ExtGState
  14307. $this->_newobj();
  14308. $out = '<< /Type /ExtGState /SMask '.($this->n - 1).' 0 R /AIS false >>'."\n".'endobj';
  14309. $this->_out($out);
  14310. $this->extgstates[] = array('n' => $this->n, 'name' => 'TGS'.$id);
  14311. }
  14312. }
  14313. }
  14314. /**
  14315. * Draw the sector of a circle.
  14316. * It can be used for instance to render pie charts.
  14317. * @param float $xc abscissa of the center.
  14318. * @param float $yc ordinate of the center.
  14319. * @param float $r radius.
  14320. * @param float $a start angle (in degrees).
  14321. * @param float $b end angle (in degrees).
  14322. * @param string $style Style of rendering. See the getPathPaintOperator() function for more information.
  14323. * @param float $cw indicates whether to go clockwise (default: true).
  14324. * @param float $o origin of angles (0 for 3 o'clock, 90 for noon, 180 for 9 o'clock, 270 for 6 o'clock). Default: 90.
  14325. * @author Maxime Delorme, Nicola Asuni
  14326. * @since 3.1.000 (2008-06-09)
  14327. * @public
  14328. */
  14329. public function PieSector($xc, $yc, $r, $a, $b, $style='FD', $cw=true, $o=90) {
  14330. $this->PieSectorXY($xc, $yc, $r, $r, $a, $b, $style, $cw, $o);
  14331. }
  14332. /**
  14333. * Draw the sector of an ellipse.
  14334. * It can be used for instance to render pie charts.
  14335. * @param float $xc abscissa of the center.
  14336. * @param float $yc ordinate of the center.
  14337. * @param float $rx the x-axis radius.
  14338. * @param float $ry the y-axis radius.
  14339. * @param float $a start angle (in degrees).
  14340. * @param float $b end angle (in degrees).
  14341. * @param string $style Style of rendering. See the getPathPaintOperator() function for more information.
  14342. * @param float $cw indicates whether to go clockwise.
  14343. * @param float $o origin of angles (0 for 3 o'clock, 90 for noon, 180 for 9 o'clock, 270 for 6 o'clock).
  14344. * @param integer $nc Number of curves used to draw a 90 degrees portion of arc.
  14345. * @author Maxime Delorme, Nicola Asuni
  14346. * @since 3.1.000 (2008-06-09)
  14347. * @public
  14348. */
  14349. public function PieSectorXY($xc, $yc, $rx, $ry, $a, $b, $style='FD', $cw=false, $o=0, $nc=2) {
  14350. if ($this->state != 2) {
  14351. return;
  14352. }
  14353. if ($this->rtl) {
  14354. $xc = ($this->w - $xc);
  14355. }
  14356. $op = TCPDF_STATIC::getPathPaintOperator($style);
  14357. if ($op == 'f') {
  14358. $line_style = array();
  14359. }
  14360. if ($cw) {
  14361. $d = $b;
  14362. $b = (360 - $a + $o);
  14363. $a = (360 - $d + $o);
  14364. } else {
  14365. $b += $o;
  14366. $a += $o;
  14367. }
  14368. $this->_outellipticalarc($xc, $yc, $rx, $ry, 0, $a, $b, true, $nc);
  14369. $this->_out($op);
  14370. }
  14371. /**
  14372. * Embed vector-based Adobe Illustrator (AI) or AI-compatible EPS files.
  14373. * NOTE: EPS is not yet fully implemented, use the setRasterizeVectorImages() method to enable/disable rasterization of vector images using ImageMagick library.
  14374. * Only vector drawing is supported, not text or bitmap.
  14375. * Although the script was successfully tested with various AI format versions, best results are probably achieved with files that were exported in the AI3 format (tested with Illustrator CS2, Freehand MX and Photoshop CS2).
  14376. * @param string $file Name of the file containing the image or a '@' character followed by the EPS/AI data string.
  14377. * @param float|null $x Abscissa of the upper-left corner.
  14378. * @param float|null $y Ordinate of the upper-left corner.
  14379. * @param float $w Width of the image in the page. If not specified or equal to zero, it is automatically calculated.
  14380. * @param float $h Height of the image in the page. If not specified or equal to zero, it is automatically calculated.
  14381. * @param mixed $link URL or identifier returned by AddLink().
  14382. * @param boolean $useBoundingBox specifies whether to position the bounding box (true) or the complete canvas (false) at location (x,y). Default value is true.
  14383. * @param string $align Indicates the alignment of the pointer next to image insertion relative to image height. The value can be:<ul><li>T: top-right for LTR or top-left for RTL</li><li>M: middle-right for LTR or middle-left for RTL</li><li>B: bottom-right for LTR or bottom-left for RTL</li><li>N: next line</li></ul>
  14384. * @param string $palign Allows to center or align the image on the current line. Possible values are:<ul><li>L : left align</li><li>C : center</li><li>R : right align</li><li>'' : empty string : left for LTR or right for RTL</li></ul>
  14385. * @param mixed $border Indicates if borders must be drawn around the cell. The value can be a number:<ul><li>0: no border (default)</li><li>1: frame</li></ul> or a string containing some or all of the following characters (in any order):<ul><li>L: left</li><li>T: top</li><li>R: right</li><li>B: bottom</li></ul> or an array of line styles for each border group - for example: array('LTRB' => array('width' => 2, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)))
  14386. * @param boolean $fitonpage if true the image is resized to not exceed page dimensions.
  14387. * @param boolean $fixoutvals if true remove values outside the bounding box.
  14388. * @author Valentin Schmidt, Nicola Asuni
  14389. * @since 3.1.000 (2008-06-09)
  14390. * @public
  14391. */
  14392. public function ImageEps($file, $x=null, $y=null, $w=0, $h=0, $link='', $useBoundingBox=true, $align='', $palign='', $border=0, $fitonpage=false, $fixoutvals=false) {
  14393. if ($this->state != 2) {
  14394. return;
  14395. }
  14396. if ($this->rasterize_vector_images AND ($w > 0) AND ($h > 0)) {
  14397. // convert EPS to raster image using GD or ImageMagick libraries
  14398. return $this->Image($file, $x, $y, $w, $h, 'EPS', $link, $align, true, 300, $palign, false, false, $border, false, false, $fitonpage);
  14399. }
  14400. if (TCPDF_STATIC::empty_string($x)) {
  14401. $x = $this->x;
  14402. }
  14403. if (TCPDF_STATIC::empty_string($y)) {
  14404. $y = $this->y;
  14405. }
  14406. // check page for no-write regions and adapt page margins if necessary
  14407. list($x, $y) = $this->checkPageRegions($h, $x, $y);
  14408. $k = $this->k;
  14409. if ($file[0] === '@') { // image from string
  14410. $data = substr($file, 1);
  14411. } else { // EPS/AI file
  14412. $data = $this->getCachedFileContents($file);
  14413. }
  14414. if ($data === FALSE) {
  14415. $this->Error('EPS file not found: '.$file);
  14416. }
  14417. $regs = array();
  14418. // EPS/AI compatibility check (only checks files created by Adobe Illustrator!)
  14419. preg_match("/%%Creator:([^\r\n]+)/", $data, $regs); # find Creator
  14420. if (count($regs) > 1) {
  14421. $version_str = trim($regs[1]); # e.g. "Adobe Illustrator(R) 8.0"
  14422. if (strpos($version_str, 'Adobe Illustrator') !== false) {
  14423. $versexp = explode(' ', $version_str);
  14424. $version = (float)array_pop($versexp);
  14425. if ($version >= 9) {
  14426. $this->Error('This version of Adobe Illustrator file is not supported: '.$file);
  14427. }
  14428. }
  14429. }
  14430. // strip binary bytes in front of PS-header
  14431. $start = strpos($data, '%!PS-Adobe');
  14432. if ($start > 0) {
  14433. $data = substr($data, $start);
  14434. }
  14435. // find BoundingBox params
  14436. preg_match("/%%BoundingBox:([^\r\n]+)/", $data, $regs);
  14437. if (count($regs) > 1) {
  14438. list($x1, $y1, $x2, $y2) = explode(' ', trim($regs[1]));
  14439. } else {
  14440. $this->Error('No BoundingBox found in EPS/AI file: '.$file);
  14441. }
  14442. $start = strpos($data, '%%EndSetup');
  14443. if ($start === false) {
  14444. $start = strpos($data, '%%EndProlog');
  14445. }
  14446. if ($start === false) {
  14447. $start = strpos($data, '%%BoundingBox');
  14448. }
  14449. $data = substr($data, $start);
  14450. $end = strpos($data, '%%PageTrailer');
  14451. if ($end===false) {
  14452. $end = strpos($data, 'showpage');
  14453. }
  14454. if ($end) {
  14455. $data = substr($data, 0, $end);
  14456. }
  14457. // calculate image width and height on document
  14458. if (($w <= 0) AND ($h <= 0)) {
  14459. $w = ($x2 - $x1) / $k;
  14460. $h = ($y2 - $y1) / $k;
  14461. } elseif ($w <= 0) {
  14462. $w = ($x2-$x1) / $k * ($h / (($y2 - $y1) / $k));
  14463. } elseif ($h <= 0) {
  14464. $h = ($y2 - $y1) / $k * ($w / (($x2 - $x1) / $k));
  14465. }
  14466. // fit the image on available space
  14467. list($w, $h, $x, $y) = $this->fitBlock($w, $h, $x, $y, $fitonpage);
  14468. if ($this->rasterize_vector_images) {
  14469. // convert EPS to raster image using GD or ImageMagick libraries
  14470. return $this->Image($file, $x, $y, $w, $h, 'EPS', $link, $align, true, 300, $palign, false, false, $border, false, false, $fitonpage);
  14471. }
  14472. // set scaling factors
  14473. $scale_x = $w / (($x2 - $x1) / $k);
  14474. $scale_y = $h / (($y2 - $y1) / $k);
  14475. // set alignment
  14476. $this->img_rb_y = $y + $h;
  14477. // set alignment
  14478. if ($this->rtl) {
  14479. if ($palign == 'L') {
  14480. $ximg = $this->lMargin;
  14481. } elseif ($palign == 'C') {
  14482. $ximg = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
  14483. } elseif ($palign == 'R') {
  14484. $ximg = $this->w - $this->rMargin - $w;
  14485. } else {
  14486. $ximg = $x - $w;
  14487. }
  14488. $this->img_rb_x = $ximg;
  14489. } else {
  14490. if ($palign == 'L') {
  14491. $ximg = $this->lMargin;
  14492. } elseif ($palign == 'C') {
  14493. $ximg = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
  14494. } elseif ($palign == 'R') {
  14495. $ximg = $this->w - $this->rMargin - $w;
  14496. } else {
  14497. $ximg = $x;
  14498. }
  14499. $this->img_rb_x = $ximg + $w;
  14500. }
  14501. if ($useBoundingBox) {
  14502. $dx = $ximg * $k - $x1;
  14503. $dy = $y * $k - $y1;
  14504. } else {
  14505. $dx = $ximg * $k;
  14506. $dy = $y * $k;
  14507. }
  14508. // save the current graphic state
  14509. $this->_out('q'.$this->epsmarker);
  14510. // translate
  14511. $this->_out(sprintf('%F %F %F %F %F %F cm', 1, 0, 0, 1, $dx, $dy + ($this->hPt - (2 * $y * $k) - ($y2 - $y1))));
  14512. // scale
  14513. $this->_out(sprintf('%F %F %F %F %F %F cm', $scale_x, 0, 0, $scale_y, $x1 * (1 - $scale_x), $y2 * (1 - $scale_y)));
  14514. // handle pc/unix/mac line endings
  14515. $lines = preg_split('/[\r\n]+/si', $data, -1, PREG_SPLIT_NO_EMPTY);
  14516. $u=0;
  14517. $cnt = count($lines);
  14518. for ($i=0; $i < $cnt; ++$i) {
  14519. $line = $lines[$i];
  14520. if (($line == '') OR ($line[0] == '%')) {
  14521. continue;
  14522. }
  14523. $len = strlen($line);
  14524. // check for spot color names
  14525. $color_name = '';
  14526. if (strcasecmp('x', substr(trim($line), -1)) == 0) {
  14527. if (preg_match('/\([^\)]*\)/', $line, $matches) > 0) {
  14528. // extract spot color name
  14529. $color_name = $matches[0];
  14530. // remove color name from string
  14531. $line = str_replace(' '.$color_name, '', $line);
  14532. // remove pharentesis from color name
  14533. $color_name = substr($color_name, 1, -1);
  14534. }
  14535. }
  14536. $chunks = explode(' ', $line);
  14537. $cmd = trim(array_pop($chunks));
  14538. // RGB
  14539. if (($cmd == 'Xa') OR ($cmd == 'XA')) {
  14540. $b = array_pop($chunks);
  14541. $g = array_pop($chunks);
  14542. $r = array_pop($chunks);
  14543. $this->_out(''.$r.' '.$g.' '.$b.' '.($cmd=='Xa'?'rg':'RG')); //substr($line, 0, -2).'rg' -> in EPS (AI8): c m y k r g b rg!
  14544. continue;
  14545. }
  14546. $skip = false;
  14547. if ($fixoutvals) {
  14548. // check for values outside the bounding box
  14549. switch ($cmd) {
  14550. case 'm':
  14551. case 'l':
  14552. case 'L': {
  14553. // skip values outside bounding box
  14554. foreach ($chunks as $key => $val) {
  14555. if ((($key % 2) == 0) AND (($val < $x1) OR ($val > $x2))) {
  14556. $skip = true;
  14557. } elseif ((($key % 2) != 0) AND (($val < $y1) OR ($val > $y2))) {
  14558. $skip = true;
  14559. }
  14560. }
  14561. }
  14562. }
  14563. }
  14564. switch ($cmd) {
  14565. case 'm':
  14566. case 'l':
  14567. case 'v':
  14568. case 'y':
  14569. case 'c':
  14570. case 'k':
  14571. case 'K':
  14572. case 'g':
  14573. case 'G':
  14574. case 's':
  14575. case 'S':
  14576. case 'J':
  14577. case 'j':
  14578. case 'w':
  14579. case 'M':
  14580. case 'd':
  14581. case 'n': {
  14582. if ($skip) {
  14583. break;
  14584. }
  14585. $this->_out($line);
  14586. break;
  14587. }
  14588. case 'x': {// custom fill color
  14589. if (empty($color_name)) {
  14590. // CMYK color
  14591. list($col_c, $col_m, $col_y, $col_k) = $chunks;
  14592. $this->_out(''.$col_c.' '.$col_m.' '.$col_y.' '.$col_k.' k');
  14593. } else {
  14594. // Spot Color (CMYK + tint)
  14595. list($col_c, $col_m, $col_y, $col_k, $col_t) = $chunks;
  14596. $this->AddSpotColor($color_name, ($col_c * 100), ($col_m * 100), ($col_y * 100), ($col_k * 100));
  14597. $color_cmd = sprintf('/CS%d cs %F scn', $this->spot_colors[$color_name]['i'], (1 - $col_t));
  14598. $this->_out($color_cmd);
  14599. }
  14600. break;
  14601. }
  14602. case 'X': { // custom stroke color
  14603. if (empty($color_name)) {
  14604. // CMYK color
  14605. list($col_c, $col_m, $col_y, $col_k) = $chunks;
  14606. $this->_out(''.$col_c.' '.$col_m.' '.$col_y.' '.$col_k.' K');
  14607. } else {
  14608. // Spot Color (CMYK + tint)
  14609. list($col_c, $col_m, $col_y, $col_k, $col_t) = $chunks;
  14610. $this->AddSpotColor($color_name, ($col_c * 100), ($col_m * 100), ($col_y * 100), ($col_k * 100));
  14611. $color_cmd = sprintf('/CS%d CS %F SCN', $this->spot_colors[$color_name]['i'], (1 - $col_t));
  14612. $this->_out($color_cmd);
  14613. }
  14614. break;
  14615. }
  14616. case 'Y':
  14617. case 'N':
  14618. case 'V':
  14619. case 'L':
  14620. case 'C': {
  14621. if ($skip) {
  14622. break;
  14623. }
  14624. $line[($len - 1)] = strtolower($cmd);
  14625. $this->_out($line);
  14626. break;
  14627. }
  14628. case 'b':
  14629. case 'B': {
  14630. $this->_out($cmd . '*');
  14631. break;
  14632. }
  14633. case 'f':
  14634. case 'F': {
  14635. if ($u > 0) {
  14636. $isU = false;
  14637. $max = min(($i + 5), $cnt);
  14638. for ($j = ($i + 1); $j < $max; ++$j) {
  14639. $isU = ($isU OR (($lines[$j] == 'U') OR ($lines[$j] == '*U')));
  14640. }
  14641. if ($isU) {
  14642. $this->_out('f*');
  14643. }
  14644. } else {
  14645. $this->_out('f*');
  14646. }
  14647. break;
  14648. }
  14649. case '*u': {
  14650. ++$u;
  14651. break;
  14652. }
  14653. case '*U': {
  14654. --$u;
  14655. break;
  14656. }
  14657. }
  14658. }
  14659. // restore previous graphic state
  14660. $this->_out($this->epsmarker.'Q');
  14661. if (!empty($border)) {
  14662. $bx = $this->x;
  14663. $by = $this->y;
  14664. $this->x = $ximg;
  14665. if ($this->rtl) {
  14666. $this->x += $w;
  14667. }
  14668. $this->y = $y;
  14669. $this->Cell($w, $h, '', $border, 0, '', 0, '', 0, true);
  14670. $this->x = $bx;
  14671. $this->y = $by;
  14672. }
  14673. if ($link) {
  14674. $this->Link($ximg, $y, $w, $h, $link, 0);
  14675. }
  14676. // set pointer to align the next text/objects
  14677. switch($align) {
  14678. case 'T':{
  14679. $this->y = $y;
  14680. $this->x = $this->img_rb_x;
  14681. break;
  14682. }
  14683. case 'M':{
  14684. $this->y = $y + round($h/2);
  14685. $this->x = $this->img_rb_x;
  14686. break;
  14687. }
  14688. case 'B':{
  14689. $this->y = $this->img_rb_y;
  14690. $this->x = $this->img_rb_x;
  14691. break;
  14692. }
  14693. case 'N':{
  14694. $this->setY($this->img_rb_y);
  14695. break;
  14696. }
  14697. default:{
  14698. break;
  14699. }
  14700. }
  14701. $this->endlinex = $this->img_rb_x;
  14702. }
  14703. /**
  14704. * Set document barcode.
  14705. * @param string $bc barcode
  14706. * @public
  14707. */
  14708. public function setBarcode($bc='') {
  14709. $this->barcode = $bc;
  14710. }
  14711. /**
  14712. * Get current barcode.
  14713. * @return string
  14714. * @public
  14715. * @since 4.0.012 (2008-07-24)
  14716. */
  14717. public function getBarcode() {
  14718. return $this->barcode;
  14719. }
  14720. /**
  14721. * Print a Linear Barcode.
  14722. * @param string $code code to print
  14723. * @param string $type type of barcode (see tcpdf_barcodes_1d.php for supported formats).
  14724. * @param float|null $x x position in user units (null = current x position)
  14725. * @param float|null $y y position in user units (null = current y position)
  14726. * @param float|null $w width in user units (null = remaining page width)
  14727. * @param float|null $h height in user units (null = remaining page height)
  14728. * @param float|null $xres width of the smallest bar in user units (null = default value = 0.4mm)
  14729. * @param array $style array of options:<ul>
  14730. * <li>boolean $style['border'] if true prints a border</li>
  14731. * <li>int $style['padding'] padding to leave around the barcode in user units (set to 'auto' for automatic padding)</li>
  14732. * <li>int $style['hpadding'] horizontal padding in user units (set to 'auto' for automatic padding)</li>
  14733. * <li>int $style['vpadding'] vertical padding in user units (set to 'auto' for automatic padding)</li>
  14734. * <li>array $style['fgcolor'] color array for bars and text</li>
  14735. * <li>mixed $style['bgcolor'] color array for background (set to false for transparent)</li>
  14736. * <li>boolean $style['text'] if true prints text below the barcode</li>
  14737. * <li>string $style['label'] override default label</li>
  14738. * <li>string $style['font'] font name for text</li><li>int $style['fontsize'] font size for text</li>
  14739. * <li>int $style['stretchtext']: 0 = disabled; 1 = horizontal scaling only if necessary; 2 = forced horizontal scaling; 3 = character spacing only if necessary; 4 = forced character spacing.</li>
  14740. * <li>string $style['position'] horizontal position of the containing barcode cell on the page: L = left margin; C = center; R = right margin.</li>
  14741. * <li>string $style['align'] horizontal position of the barcode on the containing rectangle: L = left; C = center; R = right.</li>
  14742. * <li>string $style['stretch'] if true stretch the barcode to best fit the available width, otherwise uses $xres resolution for a single bar.</li>
  14743. * <li>string $style['fitwidth'] if true reduce the width to fit the barcode width + padding. When this option is enabled the 'stretch' option is automatically disabled.</li>
  14744. * <li>string $style['cellfitalign'] this option works only when 'fitwidth' is true and 'position' is unset or empty. Set the horizontal position of the containing barcode cell inside the specified rectangle: L = left; C = center; R = right.</li></ul>
  14745. * @param string $align Indicates the alignment of the pointer next to barcode insertion relative to barcode height. The value can be:<ul><li>T: top-right for LTR or top-left for RTL</li><li>M: middle-right for LTR or middle-left for RTL</li><li>B: bottom-right for LTR or bottom-left for RTL</li><li>N: next line</li></ul>
  14746. * @author Nicola Asuni
  14747. * @since 3.1.000 (2008-06-09)
  14748. * @public
  14749. */
  14750. public function write1DBarcode($code, $type, $x=null, $y=null, $w=null, $h=null, $xres=null, $style=array(), $align='') {
  14751. if (TCPDF_STATIC::empty_string(trim($code))) {
  14752. return;
  14753. }
  14754. require_once(dirname(__FILE__).'/tcpdf_barcodes_1d.php');
  14755. // save current graphic settings
  14756. $gvars = $this->getGraphicVars();
  14757. // create new barcode object
  14758. $barcodeobj = new TCPDFBarcode($code, $type);
  14759. $arrcode = $barcodeobj->getBarcodeArray();
  14760. if (empty($arrcode) OR ($arrcode['maxw'] <= 0)) {
  14761. $this->Error('Error in 1D barcode string');
  14762. }
  14763. if ($arrcode['maxh'] <= 0) {
  14764. $arrcode['maxh'] = 1;
  14765. }
  14766. // set default values
  14767. if (!isset($style['position'])) {
  14768. $style['position'] = '';
  14769. } elseif ($style['position'] == 'S') {
  14770. // keep this for backward compatibility
  14771. $style['position'] = '';
  14772. $style['stretch'] = true;
  14773. }
  14774. if (!isset($style['fitwidth'])) {
  14775. if (!isset($style['stretch'])) {
  14776. $style['fitwidth'] = true;
  14777. } else {
  14778. $style['fitwidth'] = false;
  14779. }
  14780. }
  14781. if ($style['fitwidth']) {
  14782. // disable stretch
  14783. $style['stretch'] = false;
  14784. }
  14785. if (!isset($style['stretch'])) {
  14786. if (($w === '') OR ($w <= 0)) {
  14787. $style['stretch'] = false;
  14788. } else {
  14789. $style['stretch'] = true;
  14790. }
  14791. }
  14792. if (!isset($style['fgcolor'])) {
  14793. $style['fgcolor'] = array(0,0,0); // default black
  14794. }
  14795. if (!isset($style['bgcolor'])) {
  14796. $style['bgcolor'] = false; // default transparent
  14797. }
  14798. if (!isset($style['border'])) {
  14799. $style['border'] = false;
  14800. }
  14801. $fontsize = 0;
  14802. if (!isset($style['text'])) {
  14803. $style['text'] = false;
  14804. }
  14805. if ($style['text'] AND isset($style['font'])) {
  14806. if (isset($style['fontsize'])) {
  14807. $fontsize = $style['fontsize'];
  14808. }
  14809. $this->setFont($style['font'], '', $fontsize);
  14810. }
  14811. if (!isset($style['stretchtext'])) {
  14812. $style['stretchtext'] = 4;
  14813. }
  14814. if (TCPDF_STATIC::empty_string($x)) {
  14815. $x = $this->x;
  14816. }
  14817. if (TCPDF_STATIC::empty_string($y)) {
  14818. $y = $this->y;
  14819. }
  14820. // check page for no-write regions and adapt page margins if necessary
  14821. list($x, $y) = $this->checkPageRegions($h, $x, $y);
  14822. if (TCPDF_STATIC::empty_string($w) OR ($w <= 0)) {
  14823. if ($this->rtl) {
  14824. $w = $x - $this->lMargin;
  14825. } else {
  14826. $w = $this->w - $this->rMargin - $x;
  14827. }
  14828. }
  14829. // padding
  14830. if (!isset($style['padding'])) {
  14831. $padding = 0;
  14832. } elseif ($style['padding'] === 'auto') {
  14833. $padding = 10 * ($w / ($arrcode['maxw'] + 20));
  14834. } else {
  14835. $padding = floatval($style['padding']);
  14836. }
  14837. // horizontal padding
  14838. if (!isset($style['hpadding'])) {
  14839. $hpadding = $padding;
  14840. } elseif ($style['hpadding'] === 'auto') {
  14841. $hpadding = 10 * ($w / ($arrcode['maxw'] + 20));
  14842. } else {
  14843. $hpadding = floatval($style['hpadding']);
  14844. }
  14845. // vertical padding
  14846. if (!isset($style['vpadding'])) {
  14847. $vpadding = $padding;
  14848. } elseif ($style['vpadding'] === 'auto') {
  14849. $vpadding = ($hpadding / 2);
  14850. } else {
  14851. $vpadding = floatval($style['vpadding']);
  14852. }
  14853. // calculate xres (single bar width)
  14854. $max_xres = ($w - (2 * $hpadding)) / $arrcode['maxw'];
  14855. if ($style['stretch']) {
  14856. $xres = $max_xres;
  14857. } else {
  14858. if (TCPDF_STATIC::empty_string($xres)) {
  14859. $xres = (0.141 * $this->k); // default bar width = 0.4 mm
  14860. }
  14861. if ($xres > $max_xres) {
  14862. // correct xres to fit on $w
  14863. $xres = $max_xres;
  14864. }
  14865. if ((isset($style['padding']) AND ($style['padding'] === 'auto'))
  14866. OR (isset($style['hpadding']) AND ($style['hpadding'] === 'auto'))) {
  14867. $hpadding = 10 * $xres;
  14868. if (isset($style['vpadding']) AND ($style['vpadding'] === 'auto')) {
  14869. $vpadding = ($hpadding / 2);
  14870. }
  14871. }
  14872. }
  14873. if ($style['fitwidth']) {
  14874. $wold = $w;
  14875. $w = (($arrcode['maxw'] * $xres) + (2 * $hpadding));
  14876. if (isset($style['cellfitalign'])) {
  14877. switch ($style['cellfitalign']) {
  14878. case 'L': {
  14879. if ($this->rtl) {
  14880. $x -= ($wold - $w);
  14881. }
  14882. break;
  14883. }
  14884. case 'R': {
  14885. if (!$this->rtl) {
  14886. $x += ($wold - $w);
  14887. }
  14888. break;
  14889. }
  14890. case 'C': {
  14891. if ($this->rtl) {
  14892. $x -= (($wold - $w) / 2);
  14893. } else {
  14894. $x += (($wold - $w) / 2);
  14895. }
  14896. break;
  14897. }
  14898. default : {
  14899. break;
  14900. }
  14901. }
  14902. }
  14903. }
  14904. $text_height = $this->getCellHeight($fontsize / $this->k);
  14905. // height
  14906. if (TCPDF_STATIC::empty_string($h) OR ($h <= 0)) {
  14907. // set default height
  14908. $h = (($arrcode['maxw'] * $xres) / 3) + (2 * $vpadding) + $text_height;
  14909. }
  14910. $barh = $h - $text_height - (2 * $vpadding);
  14911. if ($barh <=0) {
  14912. // try to reduce font or padding to fit barcode on available height
  14913. if ($text_height > $h) {
  14914. $fontsize = (($h * $this->k) / (4 * $this->cell_height_ratio));
  14915. $text_height = $this->getCellHeight($fontsize / $this->k);
  14916. $this->setFont($style['font'], '', $fontsize);
  14917. }
  14918. if ($vpadding > 0) {
  14919. $vpadding = (($h - $text_height) / 4);
  14920. }
  14921. $barh = $h - $text_height - (2 * $vpadding);
  14922. }
  14923. // fit the barcode on available space
  14924. list($w, $h, $x, $y) = $this->fitBlock($w, $h, $x, $y, false);
  14925. // set alignment
  14926. $this->img_rb_y = $y + $h;
  14927. // set alignment
  14928. if ($this->rtl) {
  14929. if ($style['position'] == 'L') {
  14930. $xpos = $this->lMargin;
  14931. } elseif ($style['position'] == 'C') {
  14932. $xpos = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
  14933. } elseif ($style['position'] == 'R') {
  14934. $xpos = $this->w - $this->rMargin - $w;
  14935. } else {
  14936. $xpos = $x - $w;
  14937. }
  14938. $this->img_rb_x = $xpos;
  14939. } else {
  14940. if ($style['position'] == 'L') {
  14941. $xpos = $this->lMargin;
  14942. } elseif ($style['position'] == 'C') {
  14943. $xpos = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
  14944. } elseif ($style['position'] == 'R') {
  14945. $xpos = $this->w - $this->rMargin - $w;
  14946. } else {
  14947. $xpos = $x;
  14948. }
  14949. $this->img_rb_x = $xpos + $w;
  14950. }
  14951. $xpos_rect = $xpos;
  14952. if (!isset($style['align'])) {
  14953. $style['align'] = 'C';
  14954. }
  14955. switch ($style['align']) {
  14956. case 'L': {
  14957. $xpos = $xpos_rect + $hpadding;
  14958. break;
  14959. }
  14960. case 'R': {
  14961. $xpos = $xpos_rect + ($w - ($arrcode['maxw'] * $xres)) - $hpadding;
  14962. break;
  14963. }
  14964. case 'C':
  14965. default : {
  14966. $xpos = $xpos_rect + (($w - ($arrcode['maxw'] * $xres)) / 2);
  14967. break;
  14968. }
  14969. }
  14970. $xpos_text = $xpos;
  14971. // barcode is always printed in LTR direction
  14972. $tempRTL = $this->rtl;
  14973. $this->rtl = false;
  14974. // print background color
  14975. if ($style['bgcolor']) {
  14976. $this->Rect($xpos_rect, $y, $w, $h, $style['border'] ? 'DF' : 'F', '', $style['bgcolor']);
  14977. } elseif ($style['border']) {
  14978. $this->Rect($xpos_rect, $y, $w, $h, 'D');
  14979. }
  14980. // set foreground color
  14981. $this->setDrawColorArray($style['fgcolor']);
  14982. $this->setTextColorArray($style['fgcolor']);
  14983. // print bars
  14984. foreach ($arrcode['bcode'] as $k => $v) {
  14985. $bw = ($v['w'] * $xres);
  14986. if ($v['t']) {
  14987. // draw a vertical bar
  14988. $ypos = $y + $vpadding + ($v['p'] * $barh / $arrcode['maxh']);
  14989. $this->Rect($xpos, $ypos, $bw, ($v['h'] * $barh / $arrcode['maxh']), 'F', array(), $style['fgcolor']);
  14990. }
  14991. $xpos += $bw;
  14992. }
  14993. // print text
  14994. if ($style['text']) {
  14995. if (isset($style['label']) AND !TCPDF_STATIC::empty_string($style['label'])) {
  14996. $label = $style['label'];
  14997. } else {
  14998. $label = $code;
  14999. }
  15000. $txtwidth = ($arrcode['maxw'] * $xres);
  15001. if ($this->GetStringWidth($label) > $txtwidth) {
  15002. $style['stretchtext'] = 2;
  15003. }
  15004. // print text
  15005. $this->x = $xpos_text;
  15006. $this->y = $y + $vpadding + $barh;
  15007. $cellpadding = $this->cell_padding;
  15008. $this->setCellPadding(0);
  15009. $this->Cell($txtwidth, 0, $label, 0, 0, 'C', false, '', $style['stretchtext'], false, 'T', 'T');
  15010. $this->cell_padding = $cellpadding;
  15011. }
  15012. // restore original direction
  15013. $this->rtl = $tempRTL;
  15014. // restore previous settings
  15015. $this->setGraphicVars($gvars);
  15016. // set pointer to align the next text/objects
  15017. switch($align) {
  15018. case 'T':{
  15019. $this->y = $y;
  15020. $this->x = $this->img_rb_x;
  15021. break;
  15022. }
  15023. case 'M':{
  15024. $this->y = $y + round($h / 2);
  15025. $this->x = $this->img_rb_x;
  15026. break;
  15027. }
  15028. case 'B':{
  15029. $this->y = $this->img_rb_y;
  15030. $this->x = $this->img_rb_x;
  15031. break;
  15032. }
  15033. case 'N':{
  15034. $this->setY($this->img_rb_y);
  15035. break;
  15036. }
  15037. default:{
  15038. break;
  15039. }
  15040. }
  15041. $this->endlinex = $this->img_rb_x;
  15042. }
  15043. /**
  15044. * Print 2D Barcode.
  15045. * @param string $code code to print
  15046. * @param string $type type of barcode (see tcpdf_barcodes_2d.php for supported formats).
  15047. * @param float|null $x x position in user units
  15048. * @param float|null $y y position in user units
  15049. * @param float|null $w width in user units
  15050. * @param float|null $h height in user units
  15051. * @param array $style array of options:<ul>
  15052. * <li>boolean $style['border'] if true prints a border around the barcode</li>
  15053. * <li>int $style['padding'] padding to leave around the barcode in barcode units (set to 'auto' for automatic padding)</li>
  15054. * <li>int $style['hpadding'] horizontal padding in barcode units (set to 'auto' for automatic padding)</li>
  15055. * <li>int $style['vpadding'] vertical padding in barcode units (set to 'auto' for automatic padding)</li>
  15056. * <li>int $style['module_width'] width of a single module in points</li>
  15057. * <li>int $style['module_height'] height of a single module in points</li>
  15058. * <li>array $style['fgcolor'] color array for bars and text</li>
  15059. * <li>mixed $style['bgcolor'] color array for background or false for transparent</li>
  15060. * <li>string $style['position'] barcode position on the page: L = left margin; C = center; R = right margin; S = stretch</li>
  15061. * @param string $align Indicates the alignment of the pointer next to barcode insertion relative to barcode height. The value can be:<ul><li>T: top-right for LTR or top-left for RTL</li><li>M: middle-right for LTR or middle-left for RTL</li><li>B: bottom-right for LTR or bottom-left for RTL</li><li>N: next line</li></ul>
  15062. * @param boolean $distort if true distort the barcode to fit width and height, otherwise preserve aspect ratio
  15063. * @author Nicola Asuni
  15064. * @since 4.5.037 (2009-04-07)
  15065. * @public
  15066. */
  15067. public function write2DBarcode($code, $type, $x=null, $y=null, $w=null, $h=null, $style=array(), $align='', $distort=false) {
  15068. if (TCPDF_STATIC::empty_string(trim($code))) {
  15069. return;
  15070. }
  15071. require_once(dirname(__FILE__).'/tcpdf_barcodes_2d.php');
  15072. // save current graphic settings
  15073. $gvars = $this->getGraphicVars();
  15074. // create new barcode object
  15075. $barcodeobj = new TCPDF2DBarcode($code, $type);
  15076. $arrcode = $barcodeobj->getBarcodeArray();
  15077. if (empty($arrcode) OR !isset($arrcode['num_rows']) OR ($arrcode['num_rows'] == 0) OR !isset($arrcode['num_cols']) OR ($arrcode['num_cols'] == 0)) {
  15078. $this->Error('Error in 2D barcode string');
  15079. }
  15080. // set default values
  15081. if (!isset($style['position'])) {
  15082. $style['position'] = '';
  15083. }
  15084. if (!isset($style['fgcolor'])) {
  15085. $style['fgcolor'] = array(0,0,0); // default black
  15086. }
  15087. if (!isset($style['bgcolor'])) {
  15088. $style['bgcolor'] = false; // default transparent
  15089. }
  15090. if (!isset($style['border'])) {
  15091. $style['border'] = false;
  15092. }
  15093. // padding
  15094. if (!isset($style['padding'])) {
  15095. $style['padding'] = 0;
  15096. } elseif ($style['padding'] === 'auto') {
  15097. $style['padding'] = 4;
  15098. }
  15099. if (!isset($style['hpadding'])) {
  15100. $style['hpadding'] = $style['padding'];
  15101. } elseif ($style['hpadding'] === 'auto') {
  15102. $style['hpadding'] = 4;
  15103. }
  15104. if (!isset($style['vpadding'])) {
  15105. $style['vpadding'] = $style['padding'];
  15106. } elseif ($style['vpadding'] === 'auto') {
  15107. $style['vpadding'] = 4;
  15108. }
  15109. $hpad = (2 * $style['hpadding']);
  15110. $vpad = (2 * $style['vpadding']);
  15111. // cell (module) dimension
  15112. if (!isset($style['module_width'])) {
  15113. $style['module_width'] = 1; // width of a single module in points
  15114. }
  15115. if (!isset($style['module_height'])) {
  15116. $style['module_height'] = 1; // height of a single module in points
  15117. }
  15118. if (TCPDF_STATIC::empty_string($x)) {
  15119. $x = $this->x;
  15120. }
  15121. if (TCPDF_STATIC::empty_string($y)) {
  15122. $y = $this->y;
  15123. }
  15124. // check page for no-write regions and adapt page margins if necessary
  15125. list($x, $y) = $this->checkPageRegions($h, $x, $y);
  15126. // number of barcode columns and rows
  15127. $rows = $arrcode['num_rows'];
  15128. $cols = $arrcode['num_cols'];
  15129. if (($rows <= 0) || ($cols <= 0)){
  15130. $this->Error('Error in 2D barcode string');
  15131. }
  15132. // module width and height
  15133. $mw = $style['module_width'];
  15134. $mh = $style['module_height'];
  15135. if (($mw <= 0) OR ($mh <= 0)) {
  15136. $this->Error('Error in 2D barcode string');
  15137. }
  15138. // get max dimensions
  15139. if ($this->rtl) {
  15140. $maxw = $x - $this->lMargin;
  15141. } else {
  15142. $maxw = $this->w - $this->rMargin - $x;
  15143. }
  15144. $maxh = ($this->h - $this->tMargin - $this->bMargin);
  15145. $ratioHW = ((($rows * $mh) + $hpad) / (($cols * $mw) + $vpad));
  15146. $ratioWH = ((($cols * $mw) + $vpad) / (($rows * $mh) + $hpad));
  15147. if (!$distort) {
  15148. if (($maxw * $ratioHW) > $maxh) {
  15149. $maxw = $maxh * $ratioWH;
  15150. }
  15151. if (($maxh * $ratioWH) > $maxw) {
  15152. $maxh = $maxw * $ratioHW;
  15153. }
  15154. }
  15155. // set maximum dimensions
  15156. if ($w > $maxw) {
  15157. $w = $maxw;
  15158. }
  15159. if ($h > $maxh) {
  15160. $h = $maxh;
  15161. }
  15162. // set dimensions
  15163. if ((TCPDF_STATIC::empty_string($w) OR ($w <= 0)) AND (TCPDF_STATIC::empty_string($h) OR ($h <= 0))) {
  15164. $w = ($cols + $hpad) * ($mw / $this->k);
  15165. $h = ($rows + $vpad) * ($mh / $this->k);
  15166. } elseif (($w === '') OR ($w <= 0)) {
  15167. $w = $h * $ratioWH;
  15168. } elseif (($h === '') OR ($h <= 0)) {
  15169. $h = $w * $ratioHW;
  15170. }
  15171. // barcode size (excluding padding)
  15172. $bw = ($w * $cols) / ($cols + $hpad);
  15173. $bh = ($h * $rows) / ($rows + $vpad);
  15174. // dimension of single barcode cell unit
  15175. $cw = $bw / $cols;
  15176. $ch = $bh / $rows;
  15177. if (!$distort) {
  15178. if (($cw / $ch) > ($mw / $mh)) {
  15179. // correct horizontal distortion
  15180. $cw = $ch * $mw / $mh;
  15181. $bw = $cw * $cols;
  15182. $style['hpadding'] = ($w - $bw) / (2 * $cw);
  15183. } else {
  15184. // correct vertical distortion
  15185. $ch = $cw * $mh / $mw;
  15186. $bh = $ch * $rows;
  15187. $style['vpadding'] = ($h - $bh) / (2 * $ch);
  15188. }
  15189. }
  15190. // fit the barcode on available space
  15191. list($w, $h, $x, $y) = $this->fitBlock($w, $h, $x, $y, false);
  15192. // set alignment
  15193. $this->img_rb_y = $y + $h;
  15194. // set alignment
  15195. if ($this->rtl) {
  15196. if ($style['position'] == 'L') {
  15197. $xpos = $this->lMargin;
  15198. } elseif ($style['position'] == 'C') {
  15199. $xpos = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
  15200. } elseif ($style['position'] == 'R') {
  15201. $xpos = $this->w - $this->rMargin - $w;
  15202. } else {
  15203. $xpos = $x - $w;
  15204. }
  15205. $this->img_rb_x = $xpos;
  15206. } else {
  15207. if ($style['position'] == 'L') {
  15208. $xpos = $this->lMargin;
  15209. } elseif ($style['position'] == 'C') {
  15210. $xpos = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
  15211. } elseif ($style['position'] == 'R') {
  15212. $xpos = $this->w - $this->rMargin - $w;
  15213. } else {
  15214. $xpos = $x;
  15215. }
  15216. $this->img_rb_x = $xpos + $w;
  15217. }
  15218. $xstart = $xpos + ($style['hpadding'] * $cw);
  15219. $ystart = $y + ($style['vpadding'] * $ch);
  15220. // barcode is always printed in LTR direction
  15221. $tempRTL = $this->rtl;
  15222. $this->rtl = false;
  15223. // print background color
  15224. if ($style['bgcolor']) {
  15225. $this->Rect($xpos, $y, $w, $h, $style['border'] ? 'DF' : 'F', '', $style['bgcolor']);
  15226. } elseif ($style['border']) {
  15227. $this->Rect($xpos, $y, $w, $h, 'D');
  15228. }
  15229. // set foreground color
  15230. $this->setDrawColorArray($style['fgcolor']);
  15231. // print barcode cells
  15232. // for each row
  15233. for ($r = 0; $r < $rows; ++$r) {
  15234. $xr = $xstart;
  15235. // for each column
  15236. for ($c = 0; $c < $cols; ++$c) {
  15237. if ($arrcode['bcode'][$r][$c] == 1) {
  15238. // draw a single barcode cell
  15239. $this->Rect($xr, $ystart, $cw, $ch, 'F', array(), $style['fgcolor']);
  15240. }
  15241. $xr += $cw;
  15242. }
  15243. $ystart += $ch;
  15244. }
  15245. // restore original direction
  15246. $this->rtl = $tempRTL;
  15247. // restore previous settings
  15248. $this->setGraphicVars($gvars);
  15249. // set pointer to align the next text/objects
  15250. switch($align) {
  15251. case 'T':{
  15252. $this->y = $y;
  15253. $this->x = $this->img_rb_x;
  15254. break;
  15255. }
  15256. case 'M':{
  15257. $this->y = $y + round($h/2);
  15258. $this->x = $this->img_rb_x;
  15259. break;
  15260. }
  15261. case 'B':{
  15262. $this->y = $this->img_rb_y;
  15263. $this->x = $this->img_rb_x;
  15264. break;
  15265. }
  15266. case 'N':{
  15267. $this->setY($this->img_rb_y);
  15268. break;
  15269. }
  15270. default:{
  15271. break;
  15272. }
  15273. }
  15274. $this->endlinex = $this->img_rb_x;
  15275. }
  15276. /**
  15277. * Returns an array containing current margins:
  15278. * <ul>
  15279. <li>$ret['left'] = left margin</li>
  15280. <li>$ret['right'] = right margin</li>
  15281. <li>$ret['top'] = top margin</li>
  15282. <li>$ret['bottom'] = bottom margin</li>
  15283. <li>$ret['header'] = header margin</li>
  15284. <li>$ret['footer'] = footer margin</li>
  15285. <li>$ret['cell'] = cell padding array</li>
  15286. <li>$ret['padding_left'] = cell left padding</li>
  15287. <li>$ret['padding_top'] = cell top padding</li>
  15288. <li>$ret['padding_right'] = cell right padding</li>
  15289. <li>$ret['padding_bottom'] = cell bottom padding</li>
  15290. * </ul>
  15291. * @return array containing all margins measures
  15292. * @public
  15293. * @since 3.2.000 (2008-06-23)
  15294. */
  15295. public function getMargins() {
  15296. $ret = array(
  15297. 'left' => $this->lMargin,
  15298. 'right' => $this->rMargin,
  15299. 'top' => $this->tMargin,
  15300. 'bottom' => $this->bMargin,
  15301. 'header' => $this->header_margin,
  15302. 'footer' => $this->footer_margin,
  15303. 'cell' => $this->cell_padding,
  15304. 'padding_left' => $this->cell_padding['L'],
  15305. 'padding_top' => $this->cell_padding['T'],
  15306. 'padding_right' => $this->cell_padding['R'],
  15307. 'padding_bottom' => $this->cell_padding['B']
  15308. );
  15309. return $ret;
  15310. }
  15311. /**
  15312. * Returns an array containing original margins:
  15313. * <ul>
  15314. <li>$ret['left'] = left margin</li>
  15315. <li>$ret['right'] = right margin</li>
  15316. * </ul>
  15317. * @return array containing all margins measures
  15318. * @public
  15319. * @since 4.0.012 (2008-07-24)
  15320. */
  15321. public function getOriginalMargins() {
  15322. $ret = array(
  15323. 'left' => $this->original_lMargin,
  15324. 'right' => $this->original_rMargin
  15325. );
  15326. return $ret;
  15327. }
  15328. /**
  15329. * Returns the current font size.
  15330. * @return float current font size
  15331. * @public
  15332. * @since 3.2.000 (2008-06-23)
  15333. */
  15334. public function getFontSize() {
  15335. return $this->FontSize;
  15336. }
  15337. /**
  15338. * Returns the current font size in points unit.
  15339. * @return int current font size in points unit
  15340. * @public
  15341. * @since 3.2.000 (2008-06-23)
  15342. */
  15343. public function getFontSizePt() {
  15344. return $this->FontSizePt;
  15345. }
  15346. /**
  15347. * Returns the current font family name.
  15348. * @return string current font family name
  15349. * @public
  15350. * @since 4.3.008 (2008-12-05)
  15351. */
  15352. public function getFontFamily() {
  15353. return $this->FontFamily;
  15354. }
  15355. /**
  15356. * Returns the current font style.
  15357. * @return string current font style
  15358. * @public
  15359. * @since 4.3.008 (2008-12-05)
  15360. */
  15361. public function getFontStyle() {
  15362. return $this->FontStyle;
  15363. }
  15364. /**
  15365. * Cleanup HTML code (requires HTML Tidy library).
  15366. * @param string $html htmlcode to fix
  15367. * @param string $default_css CSS commands to add
  15368. * @param array|null $tagvs parameters for setHtmlVSpace method
  15369. * @param array|null $tidy_options options for tidy_parse_string function
  15370. * @return string XHTML code cleaned up
  15371. * @author Nicola Asuni
  15372. * @public
  15373. * @since 5.9.017 (2010-11-16)
  15374. * @see setHtmlVSpace()
  15375. */
  15376. public function fixHTMLCode($html, $default_css='', $tagvs=null, $tidy_options=null) {
  15377. return TCPDF_STATIC::fixHTMLCode($html, $default_css, $tagvs, $tidy_options, $this->tagvspaces);
  15378. }
  15379. /**
  15380. * Returns the border width from CSS property
  15381. * @param string $width border width
  15382. * @return int with in user units
  15383. * @protected
  15384. * @since 5.7.000 (2010-08-02)
  15385. */
  15386. protected function getCSSBorderWidth($width) {
  15387. if ($width == 'thin') {
  15388. $width = (2 / $this->k);
  15389. } elseif ($width == 'medium') {
  15390. $width = (4 / $this->k);
  15391. } elseif ($width == 'thick') {
  15392. $width = (6 / $this->k);
  15393. } else {
  15394. $width = $this->getHTMLUnitToUnits($width, 1, 'px', false);
  15395. }
  15396. return $width;
  15397. }
  15398. /**
  15399. * Returns the border dash style from CSS property
  15400. * @param string $style border style to convert
  15401. * @return int sash style (return -1 in case of none or hidden border)
  15402. * @protected
  15403. * @since 5.7.000 (2010-08-02)
  15404. */
  15405. protected function getCSSBorderDashStyle($style) {
  15406. switch (strtolower($style)) {
  15407. case 'none':
  15408. case 'hidden': {
  15409. $dash = -1;
  15410. break;
  15411. }
  15412. case 'dotted': {
  15413. $dash = 1;
  15414. break;
  15415. }
  15416. case 'dashed': {
  15417. $dash = 3;
  15418. break;
  15419. }
  15420. case 'double':
  15421. case 'groove':
  15422. case 'ridge':
  15423. case 'inset':
  15424. case 'outset':
  15425. case 'solid':
  15426. default: {
  15427. $dash = 0;
  15428. break;
  15429. }
  15430. }
  15431. return $dash;
  15432. }
  15433. /**
  15434. * Returns the border style array from CSS border properties
  15435. * @param string $cssborder border properties
  15436. * @return array containing border properties
  15437. * @protected
  15438. * @since 5.7.000 (2010-08-02)
  15439. */
  15440. protected function getCSSBorderStyle($cssborder) {
  15441. $bprop = preg_split('/[\s]+/', trim($cssborder));
  15442. $count = count($bprop);
  15443. if ($count > 0 && $bprop[$count - 1] === '!important') {
  15444. unset($bprop[$count - 1]);
  15445. --$count;
  15446. }
  15447. $border = array(); // value to be returned
  15448. switch ($count) {
  15449. case 2: {
  15450. $width = 'medium';
  15451. $style = $bprop[0];
  15452. $color = $bprop[1];
  15453. break;
  15454. }
  15455. case 1: {
  15456. $width = 'medium';
  15457. $style = $bprop[0];
  15458. $color = 'black';
  15459. break;
  15460. }
  15461. case 0: {
  15462. $width = 'medium';
  15463. $style = 'solid';
  15464. $color = 'black';
  15465. break;
  15466. }
  15467. default: {
  15468. $width = $bprop[0];
  15469. $style = $bprop[1];
  15470. $color = $bprop[2];
  15471. break;
  15472. }
  15473. }
  15474. if ($style == 'none') {
  15475. return array();
  15476. }
  15477. $border['cap'] = 'square';
  15478. $border['join'] = 'miter';
  15479. $border['dash'] = $this->getCSSBorderDashStyle($style);
  15480. if ($border['dash'] < 0) {
  15481. return array();
  15482. }
  15483. $border['width'] = $this->getCSSBorderWidth($width);
  15484. $border['color'] = TCPDF_COLORS::convertHTMLColorToDec($color, $this->spot_colors);
  15485. return $border;
  15486. }
  15487. /**
  15488. * Get the internal Cell padding from CSS attribute.
  15489. * @param string $csspadding padding properties
  15490. * @param float $width width of the containing element
  15491. * @return array of cell paddings
  15492. * @public
  15493. * @since 5.9.000 (2010-10-04)
  15494. */
  15495. public function getCSSPadding($csspadding, $width=0) {
  15496. $padding = preg_split('/[\s]+/', trim($csspadding));
  15497. $cell_padding = array(); // value to be returned
  15498. switch (count($padding)) {
  15499. case 4: {
  15500. $cell_padding['T'] = $padding[0];
  15501. $cell_padding['R'] = $padding[1];
  15502. $cell_padding['B'] = $padding[2];
  15503. $cell_padding['L'] = $padding[3];
  15504. break;
  15505. }
  15506. case 3: {
  15507. $cell_padding['T'] = $padding[0];
  15508. $cell_padding['R'] = $padding[1];
  15509. $cell_padding['B'] = $padding[2];
  15510. $cell_padding['L'] = $padding[1];
  15511. break;
  15512. }
  15513. case 2: {
  15514. $cell_padding['T'] = $padding[0];
  15515. $cell_padding['R'] = $padding[1];
  15516. $cell_padding['B'] = $padding[0];
  15517. $cell_padding['L'] = $padding[1];
  15518. break;
  15519. }
  15520. case 1: {
  15521. $cell_padding['T'] = $padding[0];
  15522. $cell_padding['R'] = $padding[0];
  15523. $cell_padding['B'] = $padding[0];
  15524. $cell_padding['L'] = $padding[0];
  15525. break;
  15526. }
  15527. default: {
  15528. return $this->cell_padding;
  15529. }
  15530. }
  15531. if ($width == 0) {
  15532. $width = $this->w - $this->lMargin - $this->rMargin;
  15533. }
  15534. $cell_padding['T'] = $this->getHTMLUnitToUnits($cell_padding['T'], $width, 'px', false);
  15535. $cell_padding['R'] = $this->getHTMLUnitToUnits($cell_padding['R'], $width, 'px', false);
  15536. $cell_padding['B'] = $this->getHTMLUnitToUnits($cell_padding['B'], $width, 'px', false);
  15537. $cell_padding['L'] = $this->getHTMLUnitToUnits($cell_padding['L'], $width, 'px', false);
  15538. return $cell_padding;
  15539. }
  15540. /**
  15541. * Get the internal Cell margin from CSS attribute.
  15542. * @param string $cssmargin margin properties
  15543. * @param float $width width of the containing element
  15544. * @return array of cell margins
  15545. * @public
  15546. * @since 5.9.000 (2010-10-04)
  15547. */
  15548. public function getCSSMargin($cssmargin, $width=0) {
  15549. $margin = preg_split('/[\s]+/', trim($cssmargin));
  15550. $cell_margin = array(); // value to be returned
  15551. switch (count($margin)) {
  15552. case 4: {
  15553. $cell_margin['T'] = $margin[0];
  15554. $cell_margin['R'] = $margin[1];
  15555. $cell_margin['B'] = $margin[2];
  15556. $cell_margin['L'] = $margin[3];
  15557. break;
  15558. }
  15559. case 3: {
  15560. $cell_margin['T'] = $margin[0];
  15561. $cell_margin['R'] = $margin[1];
  15562. $cell_margin['B'] = $margin[2];
  15563. $cell_margin['L'] = $margin[1];
  15564. break;
  15565. }
  15566. case 2: {
  15567. $cell_margin['T'] = $margin[0];
  15568. $cell_margin['R'] = $margin[1];
  15569. $cell_margin['B'] = $margin[0];
  15570. $cell_margin['L'] = $margin[1];
  15571. break;
  15572. }
  15573. case 1: {
  15574. $cell_margin['T'] = $margin[0];
  15575. $cell_margin['R'] = $margin[0];
  15576. $cell_margin['B'] = $margin[0];
  15577. $cell_margin['L'] = $margin[0];
  15578. break;
  15579. }
  15580. default: {
  15581. return $this->cell_margin;
  15582. }
  15583. }
  15584. if ($width == 0) {
  15585. $width = $this->w - $this->lMargin - $this->rMargin;
  15586. }
  15587. $cell_margin['T'] = $this->getHTMLUnitToUnits(str_replace('auto', '0', $cell_margin['T']), $width, 'px', false);
  15588. $cell_margin['R'] = $this->getHTMLUnitToUnits(str_replace('auto', '0', $cell_margin['R']), $width, 'px', false);
  15589. $cell_margin['B'] = $this->getHTMLUnitToUnits(str_replace('auto', '0', $cell_margin['B']), $width, 'px', false);
  15590. $cell_margin['L'] = $this->getHTMLUnitToUnits(str_replace('auto', '0', $cell_margin['L']), $width, 'px', false);
  15591. return $cell_margin;
  15592. }
  15593. /**
  15594. * Get the border-spacing from CSS attribute.
  15595. * @param string $cssbspace border-spacing CSS properties
  15596. * @param float $width width of the containing element
  15597. * @return array of border spacings
  15598. * @public
  15599. * @since 5.9.010 (2010-10-27)
  15600. */
  15601. public function getCSSBorderMargin($cssbspace, $width=0) {
  15602. $space = preg_split('/[\s]+/', trim($cssbspace));
  15603. $border_spacing = array(); // value to be returned
  15604. switch (count($space)) {
  15605. case 2: {
  15606. $border_spacing['H'] = $space[0];
  15607. $border_spacing['V'] = $space[1];
  15608. break;
  15609. }
  15610. case 1: {
  15611. $border_spacing['H'] = $space[0];
  15612. $border_spacing['V'] = $space[0];
  15613. break;
  15614. }
  15615. default: {
  15616. return array('H' => 0, 'V' => 0);
  15617. }
  15618. }
  15619. if ($width == 0) {
  15620. $width = $this->w - $this->lMargin - $this->rMargin;
  15621. }
  15622. $border_spacing['H'] = $this->getHTMLUnitToUnits($border_spacing['H'], $width, 'px', false);
  15623. $border_spacing['V'] = $this->getHTMLUnitToUnits($border_spacing['V'], $width, 'px', false);
  15624. return $border_spacing;
  15625. }
  15626. /**
  15627. * Returns the letter-spacing value from CSS value
  15628. * @param string $spacing letter-spacing value
  15629. * @param float $parent font spacing (tracking) value of the parent element
  15630. * @return float quantity to increases or decreases the space between characters in a text.
  15631. * @protected
  15632. * @since 5.9.000 (2010-10-02)
  15633. */
  15634. protected function getCSSFontSpacing($spacing, $parent=0) {
  15635. $val = 0; // value to be returned
  15636. $spacing = trim($spacing);
  15637. switch ($spacing) {
  15638. case 'normal': {
  15639. $val = 0;
  15640. break;
  15641. }
  15642. case 'inherit': {
  15643. if ($parent == 'normal') {
  15644. $val = 0;
  15645. } else {
  15646. $val = $parent;
  15647. }
  15648. break;
  15649. }
  15650. default: {
  15651. $val = $this->getHTMLUnitToUnits($spacing, 0, 'px', false);
  15652. }
  15653. }
  15654. return $val;
  15655. }
  15656. /**
  15657. * Returns the percentage of font stretching from CSS value
  15658. * @param string $stretch stretch mode
  15659. * @param float $parent stretch value of the parent element
  15660. * @return float font stretching percentage
  15661. * @protected
  15662. * @since 5.9.000 (2010-10-02)
  15663. */
  15664. protected function getCSSFontStretching($stretch, $parent=100) {
  15665. $val = 100; // value to be returned
  15666. $stretch = trim($stretch);
  15667. switch ($stretch) {
  15668. case 'ultra-condensed': {
  15669. $val = 40;
  15670. break;
  15671. }
  15672. case 'extra-condensed': {
  15673. $val = 55;
  15674. break;
  15675. }
  15676. case 'condensed': {
  15677. $val = 70;
  15678. break;
  15679. }
  15680. case 'semi-condensed': {
  15681. $val = 85;
  15682. break;
  15683. }
  15684. case 'normal': {
  15685. $val = 100;
  15686. break;
  15687. }
  15688. case 'semi-expanded': {
  15689. $val = 115;
  15690. break;
  15691. }
  15692. case 'expanded': {
  15693. $val = 130;
  15694. break;
  15695. }
  15696. case 'extra-expanded': {
  15697. $val = 145;
  15698. break;
  15699. }
  15700. case 'ultra-expanded': {
  15701. $val = 160;
  15702. break;
  15703. }
  15704. case 'wider': {
  15705. $val = ($parent + 10);
  15706. break;
  15707. }
  15708. case 'narrower': {
  15709. $val = ($parent - 10);
  15710. break;
  15711. }
  15712. case 'inherit': {
  15713. if ($parent == 'normal') {
  15714. $val = 100;
  15715. } else {
  15716. $val = $parent;
  15717. }
  15718. break;
  15719. }
  15720. default: {
  15721. $val = $this->getHTMLUnitToUnits($stretch, 100, '%', false);
  15722. }
  15723. }
  15724. return $val;
  15725. }
  15726. /**
  15727. * Convert HTML string containing font size value to points
  15728. * @param string $val String containing font size value and unit.
  15729. * @param float $refsize Reference font size in points.
  15730. * @param float $parent_size Parent font size in points.
  15731. * @param string $defaultunit Default unit (can be one of the following: %, em, ex, px, in, mm, pc, pt).
  15732. * @return float value in points
  15733. * @public
  15734. */
  15735. public function getHTMLFontUnits($val, $refsize=12, $parent_size=12, $defaultunit='pt') {
  15736. $refsize = TCPDF_FONTS::getFontRefSize($refsize);
  15737. $parent_size = TCPDF_FONTS::getFontRefSize($parent_size, $refsize);
  15738. switch ($val) {
  15739. case 'xx-small': {
  15740. $size = ($refsize - 4);
  15741. break;
  15742. }
  15743. case 'x-small': {
  15744. $size = ($refsize - 3);
  15745. break;
  15746. }
  15747. case 'small': {
  15748. $size = ($refsize - 2);
  15749. break;
  15750. }
  15751. case 'medium': {
  15752. $size = $refsize;
  15753. break;
  15754. }
  15755. case 'large': {
  15756. $size = ($refsize + 2);
  15757. break;
  15758. }
  15759. case 'x-large': {
  15760. $size = ($refsize + 4);
  15761. break;
  15762. }
  15763. case 'xx-large': {
  15764. $size = ($refsize + 6);
  15765. break;
  15766. }
  15767. case 'smaller': {
  15768. $size = ($parent_size - 3);
  15769. break;
  15770. }
  15771. case 'larger': {
  15772. $size = ($parent_size + 3);
  15773. break;
  15774. }
  15775. default: {
  15776. $parentSize = $this->getHTMLUnitToUnits($parent_size, $refsize, $defaultunit, true);
  15777. $size = $this->getHTMLUnitToUnits($val, $parent_size, $defaultunit, true);
  15778. }
  15779. }
  15780. return $size;
  15781. }
  15782. /**
  15783. * Returns the HTML DOM array.
  15784. * @param string $html html code
  15785. * @return array
  15786. * @protected
  15787. * @since 3.2.000 (2008-06-20)
  15788. */
  15789. protected function getHtmlDomArray($html) {
  15790. // set inheritable properties fot the first void element
  15791. // possible inheritable properties are: azimuth, border-collapse, border-spacing, caption-side, color, cursor, direction, empty-cells, font, font-family, font-stretch, font-size, font-size-adjust, font-style, font-variant, font-weight, letter-spacing, line-height, list-style, list-style-image, list-style-position, list-style-type, orphans, page, page-break-inside, quotes, speak, speak-header, text-align, text-indent, text-transform, volume, white-space, widows, word-spacing
  15792. $dom = array(
  15793. array(
  15794. 'tag' => false,
  15795. 'block' => false,
  15796. 'value' => '',
  15797. 'parent' => 0,
  15798. 'hide' => false,
  15799. 'fontname' => $this->FontFamily,
  15800. 'fontstyle' => $this->FontStyle,
  15801. 'fontsize' => $this->FontSizePt,
  15802. 'font-stretch' => $this->font_stretching,
  15803. 'letter-spacing' => $this->font_spacing,
  15804. 'stroke' => $this->textstrokewidth,
  15805. 'fill' => (($this->textrendermode % 2) == 0),
  15806. 'clip' => ($this->textrendermode > 3),
  15807. 'line-height' => $this->cell_height_ratio,
  15808. 'bgcolor' => false,
  15809. 'fgcolor' => $this->fgcolor, // color
  15810. 'strokecolor' => $this->strokecolor,
  15811. 'align' => '',
  15812. 'listtype' => '',
  15813. 'text-indent' => 0,
  15814. 'text-transform' => '',
  15815. 'border' => array(),
  15816. 'dir' => $this->rtl?'rtl':'ltr',
  15817. 'width' => 0,
  15818. 'height' => 0,
  15819. 'x' => 0,
  15820. 'y' => 0,
  15821. 'w' => 0,
  15822. 'h' => 0,
  15823. 'l' => 0,
  15824. 't' => 0,
  15825. 'r' => 0,
  15826. 'b' => 0,
  15827. 'padding' => array('T' => 0, 'R' => 0, 'B' => 0, 'L' => 0),
  15828. 'margin' => array('T' => 0, 'R' => 0, 'B' => 0, 'L' => 0),
  15829. 'border-spacing' => array('H' => 0, 'V' => 0),
  15830. 'border-collapse' => 'separate',
  15831. )
  15832. );
  15833. if($html === '' || $html === null) {
  15834. return $dom;
  15835. }
  15836. // array of CSS styles ( selector => properties).
  15837. $css = array();
  15838. // get CSS array defined at previous call
  15839. $matches = array();
  15840. if (preg_match_all('/<cssarray>([^\<]*?)<\/cssarray>/is', $html, $matches) > 0) {
  15841. if (isset($matches[1][0])) {
  15842. $css = array_merge($css, json_decode($this->unhtmlentities($matches[1][0]), true));
  15843. }
  15844. $html = preg_replace('/<cssarray>(.*?)<\/cssarray>/is', '', $html);
  15845. }
  15846. // extract external CSS files
  15847. $matches = array();
  15848. if (preg_match_all('/<link([^\>]*?)>/is', $html, $matches) > 0) {
  15849. foreach ($matches[1] as $key => $link) {
  15850. $type = array();
  15851. if (preg_match('/type[\s]*=[\s]*"text\/css"/', $link, $type)) {
  15852. $type = array();
  15853. preg_match('/media[\s]*=[\s]*"([^"]*)"/', $link, $type);
  15854. // get 'all' and 'print' media, other media types are discarded
  15855. // (all, braille, embossed, handheld, print, projection, screen, speech, tty, tv)
  15856. if (empty($type) OR (isset($type[1]) AND (($type[1] == 'all') OR ($type[1] == 'print')))) {
  15857. $type = array();
  15858. if (preg_match('/href[\s]*=[\s]*"([^"]*)"/', $link, $type) > 0) {
  15859. // read CSS data file
  15860. $cssdata = $this->getCachedFileContents(trim($type[1]));
  15861. if (($cssdata !== FALSE) AND (strlen($cssdata) > 0)) {
  15862. $css = array_merge($css, TCPDF_STATIC::extractCSSproperties($cssdata));
  15863. }
  15864. }
  15865. }
  15866. }
  15867. }
  15868. }
  15869. // extract style tags
  15870. $matches = array();
  15871. if (preg_match_all('/<style([^\>]*?)>([^\<]*?)<\/style>/is', $html, $matches) > 0) {
  15872. foreach ($matches[1] as $key => $media) {
  15873. $type = array();
  15874. preg_match('/media[\s]*=[\s]*"([^"]*)"/', $media, $type);
  15875. // get 'all' and 'print' media, other media types are discarded
  15876. // (all, braille, embossed, handheld, print, projection, screen, speech, tty, tv)
  15877. if (empty($type) OR (isset($type[1]) AND (($type[1] == 'all') OR ($type[1] == 'print')))) {
  15878. $cssdata = $matches[2][$key];
  15879. $css = array_merge($css, TCPDF_STATIC::extractCSSproperties($cssdata));
  15880. }
  15881. }
  15882. }
  15883. // create a special tag to contain the CSS array (used for table content)
  15884. $csstagarray = '<cssarray>'.htmlentities(json_encode($css)).'</cssarray>';
  15885. // remove head and style blocks
  15886. $html = preg_replace('/<head([^\>]*?)>(.*?)<\/head>/is', '', $html);
  15887. $html = preg_replace('/<style([^\>]*?)>([^\<]*?)<\/style>/is', '', $html);
  15888. // define block tags
  15889. $blocktags = array('blockquote','br','dd','dl','div','dt','h1','h2','h3','h4','h5','h6','hr','li','ol','p','pre','ul','tcpdf','table','tr','td');
  15890. // define self-closing tags
  15891. $selfclosingtags = array('area','base','basefont','br','hr','input','img','link','meta');
  15892. // remove all unsupported tags (the line below lists all supported tags)
  15893. $html = strip_tags($html, '<marker/><a><b><blockquote><body><br><br/><dd><del><div><dl><dt><em><font><form><h1><h2><h3><h4><h5><h6><hr><hr/><i><img><input><label><li><ol><option><p><pre><s><select><small><span><strike><strong><sub><sup><table><tablehead><tcpdf><td><textarea><th><thead><tr><tt><u><ul>');
  15894. //replace some blank characters
  15895. $html = preg_replace('/<pre/', '<xre', $html); // preserve pre tag
  15896. $html = preg_replace('/<(table|tr|td|th|tcpdf|blockquote|dd|div|dl|dt|form|h1|h2|h3|h4|h5|h6|br|hr|li|ol|ul|p)([^\>]*)>[\n\r\t]+/', '<\\1\\2>', $html);
  15897. $html = preg_replace('@(\r\n|\r)@', "\n", $html);
  15898. $repTable = array("\t" => ' ', "\0" => ' ', "\x0B" => ' ', "\\" => "\\\\");
  15899. $html = strtr($html, $repTable);
  15900. $offset = 0;
  15901. while (($offset < strlen($html)) AND ($pos = strpos($html, '</pre>', $offset)) !== false) {
  15902. $html_a = substr($html, 0, $offset);
  15903. $html_b = substr($html, $offset, ($pos - $offset + 6));
  15904. while (preg_match("'<xre([^\>]*)>(.*?)\n(.*?)</pre>'si", $html_b)) {
  15905. // preserve newlines on <pre> tag
  15906. $html_b = preg_replace("'<xre([^\>]*)>(.*?)\n(.*?)</pre>'si", "<xre\\1>\\2<br />\\3</pre>", $html_b);
  15907. }
  15908. while (preg_match("'<xre([^\>]*)>(.*?)".$this->re_space['p']."(.*?)</pre>'".$this->re_space['m'], $html_b)) {
  15909. // preserve spaces on <pre> tag
  15910. $html_b = preg_replace("'<xre([^\>]*)>(.*?)".$this->re_space['p']."(.*?)</pre>'".$this->re_space['m'], "<xre\\1>\\2&nbsp;\\3</pre>", $html_b);
  15911. }
  15912. $html = $html_a.$html_b.substr($html, $pos + 6);
  15913. $offset = strlen($html_a.$html_b);
  15914. }
  15915. $offset = 0;
  15916. while (($offset < strlen($html)) AND ($pos = strpos($html, '</textarea>', $offset)) !== false) {
  15917. $html_a = substr($html, 0, $offset);
  15918. $html_b = substr($html, $offset, ($pos - $offset + 11));
  15919. while (preg_match("'<textarea([^\>]*)>(.*?)\n(.*?)</textarea>'si", $html_b)) {
  15920. // preserve newlines on <textarea> tag
  15921. $html_b = preg_replace("'<textarea([^\>]*)>(.*?)\n(.*?)</textarea>'si", "<textarea\\1>\\2<TBR>\\3</textarea>", $html_b);
  15922. $html_b = preg_replace("'<textarea([^\>]*)>(.*?)[\"](.*?)</textarea>'si", "<textarea\\1>\\2''\\3</textarea>", $html_b);
  15923. }
  15924. $html = $html_a.$html_b.substr($html, $pos + 11);
  15925. $offset = strlen($html_a.$html_b);
  15926. }
  15927. $html = preg_replace('/([\s]*)<option/si', '<option', $html);
  15928. $html = preg_replace('/<\/option>([\s]*)/si', '</option>', $html);
  15929. $offset = 0;
  15930. while (($offset < strlen($html)) AND ($pos = strpos($html, '</option>', $offset)) !== false) {
  15931. $html_a = substr($html, 0, $offset);
  15932. $html_b = substr($html, $offset, ($pos - $offset + 9));
  15933. while (preg_match("'<option([^\>]*)>(.*?)</option>'si", $html_b)) {
  15934. $html_b = preg_replace("'<option([\s]+)value=\"([^\"]*)\"([^\>]*)>(.*?)</option>'si", "\\2#!TaB!#\\4#!NwL!#", $html_b);
  15935. $html_b = preg_replace("'<option([^\>]*)>(.*?)</option>'si", "\\2#!NwL!#", $html_b);
  15936. }
  15937. $html = $html_a.$html_b.substr($html, $pos + 9);
  15938. $offset = strlen($html_a.$html_b);
  15939. }
  15940. if (preg_match("'</select'si", $html)) {
  15941. $html = preg_replace("'<select([^\>]*)>'si", "<select\\1 opt=\"", $html);
  15942. $html = preg_replace("'#!NwL!#</select>'si", "\" />", $html);
  15943. }
  15944. $html = str_replace("\n", ' ', $html);
  15945. // restore textarea newlines
  15946. $html = str_replace('<TBR>', "\n", $html);
  15947. // remove extra spaces from code
  15948. $html = preg_replace('/[\s]+<\/(table|tr|ul|ol|dl)>/', '</\\1>', $html);
  15949. $html = preg_replace('/'.$this->re_space['p'].'+<\/(td|th|li|dt|dd)>/'.$this->re_space['m'], '</\\1>', $html);
  15950. $html = preg_replace('/[\s]+<(tr|td|th|li|dt|dd)/', '<\\1', $html);
  15951. $html = preg_replace('/'.$this->re_space['p'].'+<(ul|ol|dl|br)/'.$this->re_space['m'], '<\\1', $html);
  15952. $html = preg_replace('/<\/(table|tr|td|th|blockquote|dd|dt|dl|div|dt|h1|h2|h3|h4|h5|h6|hr|li|ol|ul|p)>[\s]+</', '</\\1><', $html);
  15953. $html = preg_replace('/<\/(td|th)>/', '<marker style="font-size:0"/></\\1>', $html);
  15954. $html = preg_replace('/<\/table>([\s]*)<marker style="font-size:0"\/>/', '</table>', $html);
  15955. $html = preg_replace('/'.$this->re_space['p'].'+<img/'.$this->re_space['m'], chr(32).'<img', $html);
  15956. $html = preg_replace('/<img([^\>]*)>[\s]+([^\<])/xi', '<img\\1>&nbsp;\\2', $html);
  15957. $html = preg_replace('/<img([^\>]*)>/xi', '<img\\1><span><marker style="font-size:0"/></span>', $html);
  15958. $html = preg_replace('/<xre/', '<pre', $html); // restore pre tag
  15959. $html = preg_replace('/<textarea([^\>]*)>([^\<]*)<\/textarea>/xi', '<textarea\\1 value="\\2" />', $html);
  15960. $html = preg_replace('/<li([^\>]*)><\/li>/', '<li\\1>&nbsp;</li>', $html);
  15961. $html = preg_replace('/<li([^\>]*)>'.$this->re_space['p'].'*<img/'.$this->re_space['m'], '<li\\1><font size="1">&nbsp;</font><img', $html);
  15962. $html = preg_replace('/<([^\>\/]*)>[\s]/', '<\\1>&nbsp;', $html); // preserve some spaces
  15963. $html = preg_replace('/[\s]<\/([^\>]*)>/', '&nbsp;</\\1>', $html); // preserve some spaces
  15964. $html = preg_replace('/<su([bp])/', '<zws/><su\\1', $html); // fix sub/sup alignment
  15965. $html = preg_replace('/<\/su([bp])>/', '</su\\1><zws/>', $html); // fix sub/sup alignment
  15966. $html = preg_replace('/'.$this->re_space['p'].'+/'.$this->re_space['m'], chr(32), $html); // replace multiple spaces with a single space
  15967. // trim string
  15968. $html = $this->stringTrim($html);
  15969. // fix br tag after li
  15970. $html = preg_replace('/<li><br([^\>]*)>/', '<li> <br\\1>', $html);
  15971. // fix first image tag alignment
  15972. $html = preg_replace('/^<img/', '<span style="font-size:0"><br /></span> <img', $html, 1);
  15973. // pattern for generic tag
  15974. $tagpattern = '/(<[^>]+>)/';
  15975. // explodes the string
  15976. $a = preg_split($tagpattern, $html, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
  15977. // count elements
  15978. $maxel = count($a);
  15979. $elkey = 0;
  15980. $thead = false; // true when we are inside the THEAD tag
  15981. $key = 1;
  15982. $level = array();
  15983. array_push($level, 0); // root
  15984. while ($elkey < $maxel) {
  15985. $dom[$key] = array();
  15986. $element = $a[$elkey];
  15987. $dom[$key]['elkey'] = $elkey;
  15988. if (preg_match($tagpattern, $element)) {
  15989. // html tag
  15990. $element = substr($element, 1, -1);
  15991. // get tag name
  15992. preg_match('/[\/]?([a-zA-Z0-9]*)/', $element, $tag);
  15993. $tagname = strtolower($tag[1]);
  15994. // check if we are inside a table header
  15995. if ($tagname == 'thead') {
  15996. if ($element[0] == '/') {
  15997. $thead = false;
  15998. } else {
  15999. $thead = true;
  16000. }
  16001. ++$elkey;
  16002. continue;
  16003. }
  16004. $dom[$key]['tag'] = true;
  16005. $dom[$key]['value'] = $tagname;
  16006. if (in_array($dom[$key]['value'], $blocktags)) {
  16007. $dom[$key]['block'] = true;
  16008. } else {
  16009. $dom[$key]['block'] = false;
  16010. }
  16011. if ($element[0] == '/') {
  16012. // *** closing html tag
  16013. $dom[$key]['opening'] = false;
  16014. $dom[$key]['parent'] = end($level);
  16015. array_pop($level);
  16016. $dom[$key]['hide'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['hide'];
  16017. $dom[$key]['fontname'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['fontname'];
  16018. $dom[$key]['fontstyle'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['fontstyle'];
  16019. $dom[$key]['fontsize'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['fontsize'];
  16020. $dom[$key]['font-stretch'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['font-stretch'];
  16021. $dom[$key]['letter-spacing'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['letter-spacing'];
  16022. $dom[$key]['stroke'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['stroke'];
  16023. $dom[$key]['fill'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['fill'];
  16024. $dom[$key]['clip'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['clip'];
  16025. $dom[$key]['line-height'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['line-height'];
  16026. $dom[$key]['bgcolor'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['bgcolor'];
  16027. $dom[$key]['fgcolor'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['fgcolor'];
  16028. $dom[$key]['strokecolor'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['strokecolor'];
  16029. $dom[$key]['align'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['align'];
  16030. $dom[$key]['text-transform'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['text-transform'];
  16031. $dom[$key]['dir'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['dir'];
  16032. if (isset($dom[($dom[($dom[$key]['parent'])]['parent'])]['listtype'])) {
  16033. $dom[$key]['listtype'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['listtype'];
  16034. }
  16035. // set the number of columns in table tag
  16036. if (($dom[$key]['value'] == 'tr') AND (!isset($dom[($dom[($dom[$key]['parent'])]['parent'])]['cols']))) {
  16037. $dom[($dom[($dom[$key]['parent'])]['parent'])]['cols'] = $dom[($dom[$key]['parent'])]['cols'];
  16038. }
  16039. if (($dom[$key]['value'] == 'td') OR ($dom[$key]['value'] == 'th')) {
  16040. $dom[($dom[$key]['parent'])]['content'] = $csstagarray;
  16041. for ($i = ($dom[$key]['parent'] + 1); $i < $key; ++$i) {
  16042. $dom[($dom[$key]['parent'])]['content'] .= stripslashes($a[$dom[$i]['elkey']]);
  16043. }
  16044. $key = $i;
  16045. // mark nested tables
  16046. $dom[($dom[$key]['parent'])]['content'] = str_replace('<table', '<table nested="true"', $dom[($dom[$key]['parent'])]['content']);
  16047. // remove thead sections from nested tables
  16048. $dom[($dom[$key]['parent'])]['content'] = str_replace('<thead>', '', $dom[($dom[$key]['parent'])]['content']);
  16049. $dom[($dom[$key]['parent'])]['content'] = str_replace('</thead>', '', $dom[($dom[$key]['parent'])]['content']);
  16050. }
  16051. // store header rows on a new table
  16052. if (
  16053. ($dom[$key]['value'] === 'tr')
  16054. && !empty($dom[($dom[$key]['parent'])]['thead'])
  16055. && ($dom[($dom[$key]['parent'])]['thead'] === true)
  16056. ) {
  16057. if (TCPDF_STATIC::empty_string($dom[($dom[($dom[$key]['parent'])]['parent'])]['thead'])) {
  16058. $dom[($dom[($dom[$key]['parent'])]['parent'])]['thead'] = $csstagarray.$a[$dom[($dom[($dom[$key]['parent'])]['parent'])]['elkey']];
  16059. }
  16060. for ($i = $dom[$key]['parent']; $i <= $key; ++$i) {
  16061. $dom[($dom[($dom[$key]['parent'])]['parent'])]['thead'] .= $a[$dom[$i]['elkey']];
  16062. }
  16063. if (!isset($dom[($dom[$key]['parent'])]['attribute'])) {
  16064. $dom[($dom[$key]['parent'])]['attribute'] = array();
  16065. }
  16066. // header elements must be always contained in a single page
  16067. $dom[($dom[$key]['parent'])]['attribute']['nobr'] = 'true';
  16068. }
  16069. if (($dom[$key]['value'] == 'table') AND (!TCPDF_STATIC::empty_string($dom[($dom[$key]['parent'])]['thead']))) {
  16070. // remove the nobr attributes from the table header
  16071. $dom[($dom[$key]['parent'])]['thead'] = str_replace(' nobr="true"', '', $dom[($dom[$key]['parent'])]['thead']);
  16072. $dom[($dom[$key]['parent'])]['thead'] .= '</tablehead>';
  16073. }
  16074. } else {
  16075. // *** opening or self-closing html tag
  16076. $dom[$key]['opening'] = true;
  16077. $dom[$key]['parent'] = end($level);
  16078. if ((substr($element, -1, 1) == '/') OR (in_array($dom[$key]['value'], $selfclosingtags))) {
  16079. // self-closing tag
  16080. $dom[$key]['self'] = true;
  16081. } else {
  16082. // opening tag
  16083. array_push($level, $key);
  16084. $dom[$key]['self'] = false;
  16085. }
  16086. // copy some values from parent
  16087. $parentkey = 0;
  16088. if ($key > 0) {
  16089. $parentkey = $dom[$key]['parent'];
  16090. $dom[$key]['hide'] = $dom[$parentkey]['hide'];
  16091. $dom[$key]['fontname'] = $dom[$parentkey]['fontname'];
  16092. $dom[$key]['fontstyle'] = $dom[$parentkey]['fontstyle'];
  16093. $dom[$key]['fontsize'] = $dom[$parentkey]['fontsize'];
  16094. $dom[$key]['font-stretch'] = $dom[$parentkey]['font-stretch'];
  16095. $dom[$key]['letter-spacing'] = $dom[$parentkey]['letter-spacing'];
  16096. $dom[$key]['stroke'] = $dom[$parentkey]['stroke'];
  16097. $dom[$key]['fill'] = $dom[$parentkey]['fill'];
  16098. $dom[$key]['clip'] = $dom[$parentkey]['clip'];
  16099. $dom[$key]['line-height'] = $dom[$parentkey]['line-height'];
  16100. $dom[$key]['bgcolor'] = $dom[$parentkey]['bgcolor'];
  16101. $dom[$key]['fgcolor'] = $dom[$parentkey]['fgcolor'];
  16102. $dom[$key]['strokecolor'] = $dom[$parentkey]['strokecolor'];
  16103. $dom[$key]['align'] = $dom[$parentkey]['align'];
  16104. $dom[$key]['listtype'] = $dom[$parentkey]['listtype'];
  16105. $dom[$key]['text-indent'] = $dom[$parentkey]['text-indent'];
  16106. $dom[$key]['text-transform'] = $dom[$parentkey]['text-transform'];
  16107. $dom[$key]['border'] = array();
  16108. $dom[$key]['dir'] = $dom[$parentkey]['dir'];
  16109. }
  16110. // get attributes
  16111. preg_match_all('/([^=\s]*)[\s]*=[\s]*"([^"]*)"/', $element, $attr_array, PREG_PATTERN_ORDER);
  16112. $dom[$key]['attribute'] = array(); // reset attribute array
  16113. foreach($attr_array[1] as $id => $name) {
  16114. $dom[$key]['attribute'][strtolower($name)] = $attr_array[2][$id];
  16115. }
  16116. if (!empty($css)) {
  16117. // merge CSS style to current style
  16118. list($dom[$key]['csssel'], $dom[$key]['cssdata']) = TCPDF_STATIC::getCSSdataArray($dom, $key, $css);
  16119. $dom[$key]['attribute']['style'] = TCPDF_STATIC::getTagStyleFromCSSarray($dom[$key]['cssdata']);
  16120. }
  16121. // split style attributes
  16122. if (isset($dom[$key]['attribute']['style']) AND !empty($dom[$key]['attribute']['style'])) {
  16123. // get style attributes
  16124. preg_match_all('/([^;:\s]*):([^;]*)/', $dom[$key]['attribute']['style'], $style_array, PREG_PATTERN_ORDER);
  16125. $dom[$key]['style'] = array(); // reset style attribute array
  16126. foreach($style_array[1] as $id => $name) {
  16127. // in case of duplicate attribute the last replace the previous
  16128. $dom[$key]['style'][strtolower($name)] = trim($style_array[2][$id]);
  16129. }
  16130. // --- get some style attributes ---
  16131. // text direction
  16132. if (isset($dom[$key]['style']['direction'])) {
  16133. $dom[$key]['dir'] = $dom[$key]['style']['direction'];
  16134. }
  16135. // display
  16136. if (isset($dom[$key]['style']['display'])) {
  16137. $dom[$key]['hide'] = (trim(strtolower($dom[$key]['style']['display'])) == 'none');
  16138. }
  16139. // font family
  16140. if (isset($dom[$key]['style']['font-family'])) {
  16141. $dom[$key]['fontname'] = $this->getFontFamilyName($dom[$key]['style']['font-family']);
  16142. }
  16143. // list-style-type
  16144. if (isset($dom[$key]['style']['list-style-type'])) {
  16145. $dom[$key]['listtype'] = trim(strtolower($dom[$key]['style']['list-style-type']));
  16146. if ($dom[$key]['listtype'] == 'inherit') {
  16147. $dom[$key]['listtype'] = $dom[$parentkey]['listtype'];
  16148. }
  16149. }
  16150. // text-indent
  16151. if (isset($dom[$key]['style']['text-indent'])) {
  16152. $dom[$key]['text-indent'] = $this->getHTMLUnitToUnits($dom[$key]['style']['text-indent']);
  16153. if ($dom[$key]['text-indent'] == 'inherit') {
  16154. $dom[$key]['text-indent'] = $dom[$parentkey]['text-indent'];
  16155. }
  16156. }
  16157. // text-transform
  16158. if (isset($dom[$key]['style']['text-transform'])) {
  16159. $dom[$key]['text-transform'] = $dom[$key]['style']['text-transform'];
  16160. }
  16161. // font size
  16162. if (isset($dom[$key]['style']['font-size'])) {
  16163. $fsize = trim($dom[$key]['style']['font-size']);
  16164. $dom[$key]['fontsize'] = $this->getHTMLFontUnits($fsize, $dom[0]['fontsize'], $dom[$parentkey]['fontsize'], 'pt');
  16165. }
  16166. // font-stretch
  16167. if (isset($dom[$key]['style']['font-stretch'])) {
  16168. $dom[$key]['font-stretch'] = $this->getCSSFontStretching($dom[$key]['style']['font-stretch'], $dom[$parentkey]['font-stretch']);
  16169. }
  16170. // letter-spacing
  16171. if (isset($dom[$key]['style']['letter-spacing'])) {
  16172. $dom[$key]['letter-spacing'] = $this->getCSSFontSpacing($dom[$key]['style']['letter-spacing'], $dom[$parentkey]['letter-spacing']);
  16173. }
  16174. // line-height (internally is the cell height ratio)
  16175. if (isset($dom[$key]['style']['line-height'])) {
  16176. $lineheight = trim($dom[$key]['style']['line-height']);
  16177. switch ($lineheight) {
  16178. // A normal line height. This is default
  16179. case 'normal': {
  16180. $dom[$key]['line-height'] = $dom[0]['line-height'];
  16181. break;
  16182. }
  16183. case 'inherit': {
  16184. $dom[$key]['line-height'] = $dom[$parentkey]['line-height'];
  16185. }
  16186. default: {
  16187. if (is_numeric($lineheight)) {
  16188. // convert to percentage of font height
  16189. $lineheight = ($lineheight * 100).'%';
  16190. }
  16191. $dom[$key]['line-height'] = $this->getHTMLUnitToUnits($lineheight, 1, '%', true);
  16192. if (substr($lineheight, -1) !== '%') {
  16193. if ($dom[$key]['fontsize'] <= 0) {
  16194. $dom[$key]['line-height'] = 1;
  16195. } else {
  16196. $dom[$key]['line-height'] = (($dom[$key]['line-height'] - $this->cell_padding['T'] - $this->cell_padding['B']) / $dom[$key]['fontsize']);
  16197. }
  16198. }
  16199. }
  16200. }
  16201. }
  16202. // font style
  16203. if (isset($dom[$key]['style']['font-weight'])) {
  16204. if (strtolower($dom[$key]['style']['font-weight'][0]) == 'n') {
  16205. if (strpos($dom[$key]['fontstyle'], 'B') !== false) {
  16206. $dom[$key]['fontstyle'] = str_replace('B', '', $dom[$key]['fontstyle']);
  16207. }
  16208. } elseif (strtolower($dom[$key]['style']['font-weight'][0]) == 'b') {
  16209. $dom[$key]['fontstyle'] .= 'B';
  16210. }
  16211. }
  16212. if (isset($dom[$key]['style']['font-style']) AND (strtolower($dom[$key]['style']['font-style'][0]) == 'i')) {
  16213. $dom[$key]['fontstyle'] .= 'I';
  16214. }
  16215. // font color
  16216. if (isset($dom[$key]['style']['color']) AND (!TCPDF_STATIC::empty_string($dom[$key]['style']['color']))) {
  16217. $dom[$key]['fgcolor'] = TCPDF_COLORS::convertHTMLColorToDec($dom[$key]['style']['color'], $this->spot_colors);
  16218. } elseif ($dom[$key]['value'] == 'a') {
  16219. $dom[$key]['fgcolor'] = $this->htmlLinkColorArray;
  16220. }
  16221. // background color
  16222. if (isset($dom[$key]['style']['background-color']) AND (!TCPDF_STATIC::empty_string($dom[$key]['style']['background-color']))) {
  16223. $dom[$key]['bgcolor'] = TCPDF_COLORS::convertHTMLColorToDec($dom[$key]['style']['background-color'], $this->spot_colors);
  16224. }
  16225. // text-decoration
  16226. if (isset($dom[$key]['style']['text-decoration'])) {
  16227. $decors = explode(' ', strtolower($dom[$key]['style']['text-decoration']));
  16228. foreach ($decors as $dec) {
  16229. $dec = trim($dec);
  16230. if (!TCPDF_STATIC::empty_string($dec)) {
  16231. if ($dec[0] == 'u') {
  16232. // underline
  16233. $dom[$key]['fontstyle'] .= 'U';
  16234. } elseif ($dec[0] == 'l') {
  16235. // line-through
  16236. $dom[$key]['fontstyle'] .= 'D';
  16237. } elseif ($dec[0] == 'o') {
  16238. // overline
  16239. $dom[$key]['fontstyle'] .= 'O';
  16240. }
  16241. }
  16242. }
  16243. } elseif ($dom[$key]['value'] == 'a') {
  16244. $dom[$key]['fontstyle'] = $this->htmlLinkFontStyle;
  16245. }
  16246. // check for width attribute
  16247. if (isset($dom[$key]['style']['width'])) {
  16248. $dom[$key]['width'] = $dom[$key]['style']['width'];
  16249. }
  16250. // check for height attribute
  16251. if (isset($dom[$key]['style']['height'])) {
  16252. $dom[$key]['height'] = $dom[$key]['style']['height'];
  16253. }
  16254. // check for text alignment
  16255. if (isset($dom[$key]['style']['text-align'])) {
  16256. $dom[$key]['align'] = strtoupper($dom[$key]['style']['text-align'][0]);
  16257. }
  16258. // check for CSS border properties
  16259. if (isset($dom[$key]['style']['border'])) {
  16260. $borderstyle = $this->getCSSBorderStyle($dom[$key]['style']['border']);
  16261. if (!empty($borderstyle)) {
  16262. $dom[$key]['border']['LTRB'] = $borderstyle;
  16263. }
  16264. }
  16265. if (isset($dom[$key]['style']['border-color'])) {
  16266. $brd_colors = preg_split('/[\s]+/', trim($dom[$key]['style']['border-color']));
  16267. if (isset($brd_colors[3])) {
  16268. $dom[$key]['border']['L']['color'] = TCPDF_COLORS::convertHTMLColorToDec($brd_colors[3], $this->spot_colors);
  16269. }
  16270. if (isset($brd_colors[1])) {
  16271. $dom[$key]['border']['R']['color'] = TCPDF_COLORS::convertHTMLColorToDec($brd_colors[1], $this->spot_colors);
  16272. }
  16273. if (isset($brd_colors[0])) {
  16274. $dom[$key]['border']['T']['color'] = TCPDF_COLORS::convertHTMLColorToDec($brd_colors[0], $this->spot_colors);
  16275. }
  16276. if (isset($brd_colors[2])) {
  16277. $dom[$key]['border']['B']['color'] = TCPDF_COLORS::convertHTMLColorToDec($brd_colors[2], $this->spot_colors);
  16278. }
  16279. }
  16280. if (isset($dom[$key]['style']['border-width'])) {
  16281. $brd_widths = preg_split('/[\s]+/', trim($dom[$key]['style']['border-width']));
  16282. if (isset($brd_widths[3])) {
  16283. $dom[$key]['border']['L']['width'] = $this->getCSSBorderWidth($brd_widths[3]);
  16284. }
  16285. if (isset($brd_widths[1])) {
  16286. $dom[$key]['border']['R']['width'] = $this->getCSSBorderWidth($brd_widths[1]);
  16287. }
  16288. if (isset($brd_widths[0])) {
  16289. $dom[$key]['border']['T']['width'] = $this->getCSSBorderWidth($brd_widths[0]);
  16290. }
  16291. if (isset($brd_widths[2])) {
  16292. $dom[$key]['border']['B']['width'] = $this->getCSSBorderWidth($brd_widths[2]);
  16293. }
  16294. }
  16295. if (isset($dom[$key]['style']['border-style'])) {
  16296. $brd_styles = preg_split('/[\s]+/', trim($dom[$key]['style']['border-style']));
  16297. if (isset($brd_styles[3]) AND ($brd_styles[3]!='none')) {
  16298. $dom[$key]['border']['L']['cap'] = 'square';
  16299. $dom[$key]['border']['L']['join'] = 'miter';
  16300. $dom[$key]['border']['L']['dash'] = $this->getCSSBorderDashStyle($brd_styles[3]);
  16301. if ($dom[$key]['border']['L']['dash'] < 0) {
  16302. $dom[$key]['border']['L'] = array();
  16303. }
  16304. }
  16305. if (isset($brd_styles[1])) {
  16306. $dom[$key]['border']['R']['cap'] = 'square';
  16307. $dom[$key]['border']['R']['join'] = 'miter';
  16308. $dom[$key]['border']['R']['dash'] = $this->getCSSBorderDashStyle($brd_styles[1]);
  16309. if ($dom[$key]['border']['R']['dash'] < 0) {
  16310. $dom[$key]['border']['R'] = array();
  16311. }
  16312. }
  16313. if (isset($brd_styles[0])) {
  16314. $dom[$key]['border']['T']['cap'] = 'square';
  16315. $dom[$key]['border']['T']['join'] = 'miter';
  16316. $dom[$key]['border']['T']['dash'] = $this->getCSSBorderDashStyle($brd_styles[0]);
  16317. if ($dom[$key]['border']['T']['dash'] < 0) {
  16318. $dom[$key]['border']['T'] = array();
  16319. }
  16320. }
  16321. if (isset($brd_styles[2])) {
  16322. $dom[$key]['border']['B']['cap'] = 'square';
  16323. $dom[$key]['border']['B']['join'] = 'miter';
  16324. $dom[$key]['border']['B']['dash'] = $this->getCSSBorderDashStyle($brd_styles[2]);
  16325. if ($dom[$key]['border']['B']['dash'] < 0) {
  16326. $dom[$key]['border']['B'] = array();
  16327. }
  16328. }
  16329. }
  16330. $cellside = array('L' => 'left', 'R' => 'right', 'T' => 'top', 'B' => 'bottom');
  16331. foreach ($cellside as $bsk => $bsv) {
  16332. if (isset($dom[$key]['style']['border-'.$bsv])) {
  16333. $borderstyle = $this->getCSSBorderStyle($dom[$key]['style']['border-'.$bsv]);
  16334. if (!empty($borderstyle)) {
  16335. $dom[$key]['border'][$bsk] = $borderstyle;
  16336. }
  16337. }
  16338. if (isset($dom[$key]['style']['border-'.$bsv.'-color'])) {
  16339. $dom[$key]['border'][$bsk]['color'] = TCPDF_COLORS::convertHTMLColorToDec($dom[$key]['style']['border-'.$bsv.'-color'], $this->spot_colors);
  16340. }
  16341. if (isset($dom[$key]['style']['border-'.$bsv.'-width'])) {
  16342. $dom[$key]['border'][$bsk]['width'] = $this->getCSSBorderWidth($dom[$key]['style']['border-'.$bsv.'-width']);
  16343. }
  16344. if (isset($dom[$key]['style']['border-'.$bsv.'-style'])) {
  16345. $dom[$key]['border'][$bsk]['dash'] = $this->getCSSBorderDashStyle($dom[$key]['style']['border-'.$bsv.'-style']);
  16346. if ($dom[$key]['border'][$bsk]['dash'] < 0) {
  16347. $dom[$key]['border'][$bsk] = array();
  16348. }
  16349. }
  16350. }
  16351. // check for CSS padding properties
  16352. if (isset($dom[$key]['style']['padding'])) {
  16353. $dom[$key]['padding'] = $this->getCSSPadding($dom[$key]['style']['padding']);
  16354. } else {
  16355. $dom[$key]['padding'] = $this->cell_padding;
  16356. }
  16357. foreach ($cellside as $psk => $psv) {
  16358. if (isset($dom[$key]['style']['padding-'.$psv])) {
  16359. $dom[$key]['padding'][$psk] = $this->getHTMLUnitToUnits($dom[$key]['style']['padding-'.$psv], 0, 'px', false);
  16360. }
  16361. }
  16362. // check for CSS margin properties
  16363. if (isset($dom[$key]['style']['margin'])) {
  16364. $dom[$key]['margin'] = $this->getCSSMargin($dom[$key]['style']['margin']);
  16365. } else {
  16366. $dom[$key]['margin'] = $this->cell_margin;
  16367. }
  16368. foreach ($cellside as $psk => $psv) {
  16369. if (isset($dom[$key]['style']['margin-'.$psv])) {
  16370. $dom[$key]['margin'][$psk] = $this->getHTMLUnitToUnits(str_replace('auto', '0', $dom[$key]['style']['margin-'.$psv]), 0, 'px', false);
  16371. }
  16372. }
  16373. // check for CSS border-spacing properties
  16374. if (isset($dom[$key]['style']['border-spacing'])) {
  16375. $dom[$key]['border-spacing'] = $this->getCSSBorderMargin($dom[$key]['style']['border-spacing']);
  16376. }
  16377. // page-break-inside
  16378. if (isset($dom[$key]['style']['page-break-inside']) AND ($dom[$key]['style']['page-break-inside'] == 'avoid')) {
  16379. $dom[$key]['attribute']['nobr'] = 'true';
  16380. }
  16381. // page-break-before
  16382. if (isset($dom[$key]['style']['page-break-before'])) {
  16383. if ($dom[$key]['style']['page-break-before'] == 'always') {
  16384. $dom[$key]['attribute']['pagebreak'] = 'true';
  16385. } elseif ($dom[$key]['style']['page-break-before'] == 'left') {
  16386. $dom[$key]['attribute']['pagebreak'] = 'left';
  16387. } elseif ($dom[$key]['style']['page-break-before'] == 'right') {
  16388. $dom[$key]['attribute']['pagebreak'] = 'right';
  16389. }
  16390. }
  16391. // page-break-after
  16392. if (isset($dom[$key]['style']['page-break-after'])) {
  16393. if ($dom[$key]['style']['page-break-after'] == 'always') {
  16394. $dom[$key]['attribute']['pagebreakafter'] = 'true';
  16395. } elseif ($dom[$key]['style']['page-break-after'] == 'left') {
  16396. $dom[$key]['attribute']['pagebreakafter'] = 'left';
  16397. } elseif ($dom[$key]['style']['page-break-after'] == 'right') {
  16398. $dom[$key]['attribute']['pagebreakafter'] = 'right';
  16399. }
  16400. }
  16401. }
  16402. if (isset($dom[$key]['attribute']['display'])) {
  16403. $dom[$key]['hide'] = (trim(strtolower($dom[$key]['attribute']['display'])) == 'none');
  16404. }
  16405. if (isset($dom[$key]['attribute']['border']) AND ($dom[$key]['attribute']['border'] != 0)) {
  16406. $borderstyle = $this->getCSSBorderStyle($dom[$key]['attribute']['border'].' solid black');
  16407. if (!empty($borderstyle)) {
  16408. $dom[$key]['border']['LTRB'] = $borderstyle;
  16409. }
  16410. }
  16411. // check for font tag
  16412. if ($dom[$key]['value'] == 'font') {
  16413. // font family
  16414. if (isset($dom[$key]['attribute']['face'])) {
  16415. $dom[$key]['fontname'] = $this->getFontFamilyName($dom[$key]['attribute']['face']);
  16416. }
  16417. // font size
  16418. if (isset($dom[$key]['attribute']['size'])) {
  16419. if ($key > 0) {
  16420. if ($dom[$key]['attribute']['size'][0] == '+') {
  16421. $dom[$key]['fontsize'] = $dom[($dom[$key]['parent'])]['fontsize'] + intval(substr($dom[$key]['attribute']['size'], 1));
  16422. } elseif ($dom[$key]['attribute']['size'][0] == '-') {
  16423. $dom[$key]['fontsize'] = $dom[($dom[$key]['parent'])]['fontsize'] - intval(substr($dom[$key]['attribute']['size'], 1));
  16424. } else {
  16425. $dom[$key]['fontsize'] = intval($dom[$key]['attribute']['size']);
  16426. }
  16427. } else {
  16428. $dom[$key]['fontsize'] = intval($dom[$key]['attribute']['size']);
  16429. }
  16430. }
  16431. }
  16432. // force natural alignment for lists
  16433. if ((($dom[$key]['value'] == 'ul') OR ($dom[$key]['value'] == 'ol') OR ($dom[$key]['value'] == 'dl'))
  16434. AND (!isset($dom[$key]['align']) OR TCPDF_STATIC::empty_string($dom[$key]['align']) OR ($dom[$key]['align'] != 'J'))) {
  16435. if ($this->rtl) {
  16436. $dom[$key]['align'] = 'R';
  16437. } else {
  16438. $dom[$key]['align'] = 'L';
  16439. }
  16440. }
  16441. if (($dom[$key]['value'] == 'small') OR ($dom[$key]['value'] == 'sup') OR ($dom[$key]['value'] == 'sub')) {
  16442. if (!isset($dom[$key]['attribute']['size']) AND !isset($dom[$key]['style']['font-size'])) {
  16443. $dom[$key]['fontsize'] = $dom[$key]['fontsize'] * K_SMALL_RATIO;
  16444. }
  16445. }
  16446. if (($dom[$key]['value'] == 'strong') OR ($dom[$key]['value'] == 'b')) {
  16447. $dom[$key]['fontstyle'] .= 'B';
  16448. }
  16449. if (($dom[$key]['value'] == 'em') OR ($dom[$key]['value'] == 'i')) {
  16450. $dom[$key]['fontstyle'] .= 'I';
  16451. }
  16452. if ($dom[$key]['value'] == 'u') {
  16453. $dom[$key]['fontstyle'] .= 'U';
  16454. }
  16455. if (($dom[$key]['value'] == 'del') OR ($dom[$key]['value'] == 's') OR ($dom[$key]['value'] == 'strike')) {
  16456. $dom[$key]['fontstyle'] .= 'D';
  16457. }
  16458. if (!isset($dom[$key]['style']['text-decoration']) AND ($dom[$key]['value'] == 'a')) {
  16459. $dom[$key]['fontstyle'] = $this->htmlLinkFontStyle;
  16460. }
  16461. if (($dom[$key]['value'] == 'pre') OR ($dom[$key]['value'] == 'tt')) {
  16462. $dom[$key]['fontname'] = $this->default_monospaced_font;
  16463. }
  16464. if (!empty($dom[$key]['value']) AND ($dom[$key]['value'][0] == 'h') AND (intval($dom[$key]['value'][1]) > 0) AND (intval($dom[$key]['value'][1]) < 7)) {
  16465. // headings h1, h2, h3, h4, h5, h6
  16466. if (!isset($dom[$key]['attribute']['size']) AND !isset($dom[$key]['style']['font-size'])) {
  16467. $headsize = (4 - intval($dom[$key]['value'][1])) * 2;
  16468. $dom[$key]['fontsize'] = $dom[0]['fontsize'] + $headsize;
  16469. }
  16470. if (!isset($dom[$key]['style']['font-weight'])) {
  16471. $dom[$key]['fontstyle'] .= 'B';
  16472. }
  16473. }
  16474. if (($dom[$key]['value'] == 'table')) {
  16475. $dom[$key]['rows'] = 0; // number of rows
  16476. $dom[$key]['trids'] = array(); // IDs of TR elements
  16477. $dom[$key]['thead'] = ''; // table header rows
  16478. }
  16479. if (($dom[$key]['value'] == 'tr')) {
  16480. $dom[$key]['cols'] = 0;
  16481. if ($thead) {
  16482. $dom[$key]['thead'] = true;
  16483. // rows on thead block are printed as a separate table
  16484. } else {
  16485. $dom[$key]['thead'] = false;
  16486. $parent = $dom[$key]['parent'];
  16487. if (!isset($dom[$parent]['rows'])) {
  16488. $dom[$parent]['rows'] = 0;
  16489. }
  16490. // store the number of rows on table element
  16491. ++$dom[$parent]['rows'];
  16492. if (!isset($dom[$parent]['trids'])) {
  16493. $dom[$parent]['trids'] = array();
  16494. }
  16495. // store the TR elements IDs on table element
  16496. array_push($dom[$parent]['trids'], $key);
  16497. }
  16498. }
  16499. if (($dom[$key]['value'] == 'th') OR ($dom[$key]['value'] == 'td')) {
  16500. if (isset($dom[$key]['attribute']['colspan'])) {
  16501. $colspan = intval($dom[$key]['attribute']['colspan']);
  16502. } else {
  16503. $colspan = 1;
  16504. }
  16505. $dom[$key]['attribute']['colspan'] = $colspan;
  16506. $dom[($dom[$key]['parent'])]['cols'] += $colspan;
  16507. }
  16508. // text direction
  16509. if (isset($dom[$key]['attribute']['dir'])) {
  16510. $dom[$key]['dir'] = $dom[$key]['attribute']['dir'];
  16511. }
  16512. // set foreground color attribute
  16513. if (isset($dom[$key]['attribute']['color']) AND (!TCPDF_STATIC::empty_string($dom[$key]['attribute']['color']))) {
  16514. $dom[$key]['fgcolor'] = TCPDF_COLORS::convertHTMLColorToDec($dom[$key]['attribute']['color'], $this->spot_colors);
  16515. } elseif (!isset($dom[$key]['style']['color']) AND ($dom[$key]['value'] == 'a')) {
  16516. $dom[$key]['fgcolor'] = $this->htmlLinkColorArray;
  16517. }
  16518. // set background color attribute
  16519. if (isset($dom[$key]['attribute']['bgcolor']) AND (!TCPDF_STATIC::empty_string($dom[$key]['attribute']['bgcolor']))) {
  16520. $dom[$key]['bgcolor'] = TCPDF_COLORS::convertHTMLColorToDec($dom[$key]['attribute']['bgcolor'], $this->spot_colors);
  16521. }
  16522. // set stroke color attribute
  16523. if (isset($dom[$key]['attribute']['strokecolor']) AND (!TCPDF_STATIC::empty_string($dom[$key]['attribute']['strokecolor']))) {
  16524. $dom[$key]['strokecolor'] = TCPDF_COLORS::convertHTMLColorToDec($dom[$key]['attribute']['strokecolor'], $this->spot_colors);
  16525. }
  16526. // check for width attribute
  16527. if (isset($dom[$key]['attribute']['width'])) {
  16528. $dom[$key]['width'] = $dom[$key]['attribute']['width'];
  16529. }
  16530. // check for height attribute
  16531. if (isset($dom[$key]['attribute']['height'])) {
  16532. $dom[$key]['height'] = $dom[$key]['attribute']['height'];
  16533. }
  16534. // check for text alignment
  16535. if (isset($dom[$key]['attribute']['align']) AND (!TCPDF_STATIC::empty_string($dom[$key]['attribute']['align'])) AND ($dom[$key]['value'] !== 'img')) {
  16536. $dom[$key]['align'] = strtoupper($dom[$key]['attribute']['align'][0]);
  16537. }
  16538. // check for text rendering mode (the following attributes do not exist in HTML)
  16539. if (isset($dom[$key]['attribute']['stroke'])) {
  16540. // font stroke width
  16541. $dom[$key]['stroke'] = $this->getHTMLUnitToUnits($dom[$key]['attribute']['stroke'], $dom[$key]['fontsize'], 'pt', true);
  16542. }
  16543. if (isset($dom[$key]['attribute']['fill'])) {
  16544. // font fill
  16545. if ($dom[$key]['attribute']['fill'] == 'true') {
  16546. $dom[$key]['fill'] = true;
  16547. } else {
  16548. $dom[$key]['fill'] = false;
  16549. }
  16550. }
  16551. if (isset($dom[$key]['attribute']['clip'])) {
  16552. // clipping mode
  16553. if ($dom[$key]['attribute']['clip'] == 'true') {
  16554. $dom[$key]['clip'] = true;
  16555. } else {
  16556. $dom[$key]['clip'] = false;
  16557. }
  16558. }
  16559. } // end opening tag
  16560. } else {
  16561. // text
  16562. $dom[$key]['tag'] = false;
  16563. $dom[$key]['block'] = false;
  16564. $dom[$key]['parent'] = end($level);
  16565. $dom[$key]['dir'] = $dom[$dom[$key]['parent']]['dir'];
  16566. if (!empty($dom[$dom[$key]['parent']]['text-transform'])) {
  16567. // text-transform for unicode requires mb_convert_case (Multibyte String Functions)
  16568. if (function_exists('mb_convert_case')) {
  16569. $ttm = array('capitalize' => MB_CASE_TITLE, 'uppercase' => MB_CASE_UPPER, 'lowercase' => MB_CASE_LOWER);
  16570. if (isset($ttm[$dom[$dom[$key]['parent']]['text-transform']])) {
  16571. $element = mb_convert_case($element, $ttm[$dom[$dom[$key]['parent']]['text-transform']], $this->encoding);
  16572. }
  16573. } elseif (!$this->isunicode) {
  16574. switch ($dom[$dom[$key]['parent']]['text-transform']) {
  16575. case 'capitalize': {
  16576. $element = ucwords(strtolower($element));
  16577. break;
  16578. }
  16579. case 'uppercase': {
  16580. $element = strtoupper($element);
  16581. break;
  16582. }
  16583. case 'lowercase': {
  16584. $element = strtolower($element);
  16585. break;
  16586. }
  16587. }
  16588. }
  16589. $element = preg_replace("/&NBSP;/i", "&nbsp;", $element);
  16590. }
  16591. $dom[$key]['value'] = stripslashes($this->unhtmlentities($element));
  16592. }
  16593. ++$elkey;
  16594. ++$key;
  16595. }
  16596. return $dom;
  16597. }
  16598. /**
  16599. * Returns the string used to find spaces
  16600. * @return string
  16601. * @protected
  16602. * @author Nicola Asuni
  16603. * @since 4.8.024 (2010-01-15)
  16604. */
  16605. protected function getSpaceString() {
  16606. $spacestr = chr(32);
  16607. if ($this->isUnicodeFont()) {
  16608. $spacestr = chr(0).chr(32);
  16609. }
  16610. return $spacestr;
  16611. }
  16612. /**
  16613. * Calculates the hash value of the given data.
  16614. *
  16615. * @param string $data The data to be hashed.
  16616. * @return string The hashed value of the data.
  16617. */
  16618. protected function hashTCPDFtag($data) {
  16619. return hash_hmac('sha256', $data, $this->hash_key, false);
  16620. }
  16621. /**
  16622. * Serialize data to be used with TCPDF tag in HTML code.
  16623. * @param string $method TCPDF method name
  16624. * @param array $params Method parameters
  16625. * @return string Serialized data
  16626. * @public static
  16627. */
  16628. public function serializeTCPDFtag($method, $params=array()) {
  16629. $data = array('m' => $method, 'p' => $params);
  16630. $encoded = urlencode(json_encode($data));
  16631. $hash = $this->hashTCPDFtag($encoded);
  16632. return strlen($hash).'+'.$hash.'+'.$encoded;
  16633. }
  16634. /**
  16635. * Unserialize data to be used with TCPDF tag in HTML code.
  16636. * @param string $data serialized data
  16637. * @return array containing unserialized data
  16638. * @protected static
  16639. */
  16640. protected function unserializeTCPDFtag($data) {
  16641. $hpos = strpos($data, '+');
  16642. $hlen = intval(substr($data, 0, $hpos));
  16643. $hash = substr($data, $hpos + 1, $hlen);
  16644. $encoded = substr($data, $hpos + 2 + $hlen);
  16645. if (!hash_equals( $this->hashTCPDFtag($encoded), $hash)) {
  16646. $this->Error('Invalid parameters');
  16647. }
  16648. return json_decode(urldecode($encoded), true);
  16649. }
  16650. /**
  16651. * Check if a TCPDF tag is allowed
  16652. * @param string $method TCPDF method name
  16653. * @return boolean
  16654. * @protected
  16655. */
  16656. protected function allowedTCPDFtag($method) {
  16657. if (defined('K_ALLOWED_TCPDF_TAGS')) {
  16658. return (strpos(K_ALLOWED_TCPDF_TAGS, '|'.$method.'|') !== false);
  16659. }
  16660. return false;
  16661. }
  16662. /**
  16663. * Prints a cell (rectangular area) with optional borders, background color and html text string.
  16664. * The upper-left corner of the cell corresponds to the current position. After the call, the current position moves to the right or to the next line.<br />
  16665. * If automatic page breaking is enabled and the cell goes beyond the limit, a page break is done before outputting.
  16666. * IMPORTANT: The HTML must be well formatted - try to clean-up it using an application like HTML-Tidy before submitting.
  16667. * Supported tags are: a, b, blockquote, br, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, img, li, ol, p, pre, small, span, strong, sub, sup, table, tcpdf, td, th, thead, tr, tt, u, ul
  16668. * NOTE: all the HTML attributes must be enclosed in double-quote.
  16669. * @param float $w Cell width. If 0, the cell extends up to the right margin.
  16670. * @param float $h Cell minimum height. The cell extends automatically if needed.
  16671. * @param float|null $x upper-left corner X coordinate
  16672. * @param float|null $y upper-left corner Y coordinate
  16673. * @param string $html html text to print. Default value: empty string.
  16674. * @param mixed $border Indicates if borders must be drawn around the cell. The value can be a number:<ul><li>0: no border (default)</li><li>1: frame</li></ul> or a string containing some or all of the following characters (in any order):<ul><li>L: left</li><li>T: top</li><li>R: right</li><li>B: bottom</li></ul> or an array of line styles for each border group - for example: array('LTRB' => array('width' => 2, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)))
  16675. * @param int $ln Indicates where the current position should go after the call. Possible values are:<ul><li>0: to the right (or left for RTL language)</li><li>1: to the beginning of the next line</li><li>2: below</li></ul> Putting 1 is equivalent to putting 0 and calling Ln() just after. Default value: 0.
  16676. * @param boolean $fill Indicates if the cell background must be painted (true) or transparent (false).
  16677. * @param boolean $reseth if true reset the last cell height (default true).
  16678. * @param string $align Allows to center or align the text. Possible values are:<ul><li>L : left align</li><li>C : center</li><li>R : right align</li><li>'' : empty string : left for LTR or right for RTL</li></ul>
  16679. * @param boolean $autopadding if true, uses internal padding and automatically adjust it to account for line width.
  16680. * @see Multicell(), writeHTML()
  16681. * @public
  16682. */
  16683. public function writeHTMLCell($w, $h, $x, $y, $html='', $border=0, $ln=0, $fill=false, $reseth=true, $align='', $autopadding=true) {
  16684. return $this->MultiCell($w, $h, $html, $border, $align, $fill, $ln, $x, $y, $reseth, 0, true, $autopadding, 0, 'T', false);
  16685. }
  16686. /**
  16687. * Allows to preserve some HTML formatting (limited support).<br />
  16688. * IMPORTANT: The HTML must be well formatted - try to clean-up it using an application like HTML-Tidy before submitting.
  16689. * Supported tags are: a, b, blockquote, br, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, img, li, ol, p, pre, small, span, strong, sub, sup, table, tcpdf, td, th, thead, tr, tt, u, ul
  16690. * NOTE: all the HTML attributes must be enclosed in double-quote.
  16691. * @param string $html text to display
  16692. * @param boolean $ln if true add a new line after text (default = true)
  16693. * @param boolean $fill Indicates if the background must be painted (true) or transparent (false).
  16694. * @param boolean $reseth if true reset the last cell height (default false).
  16695. * @param boolean $cell if true add the current left (or right for RTL) padding to each Write (default false).
  16696. * @param string $align Allows to center or align the text. Possible values are:<ul><li>L : left align</li><li>C : center</li><li>R : right align</li><li>'' : empty string : left for LTR or right for RTL</li></ul>
  16697. * @public
  16698. */
  16699. public function writeHTML($html, $ln=true, $fill=false, $reseth=false, $cell=false, $align='') {
  16700. $gvars = $this->getGraphicVars();
  16701. // store current values
  16702. $prev_cell_margin = $this->cell_margin;
  16703. $prev_cell_padding = $this->cell_padding;
  16704. $prevPage = $this->page;
  16705. $prevlMargin = $this->lMargin;
  16706. $prevrMargin = $this->rMargin;
  16707. $curfontname = $this->FontFamily;
  16708. $curfontstyle = $this->FontStyle;
  16709. $curfontsize = $this->FontSizePt;
  16710. $curfontascent = $this->getFontAscent($curfontname, $curfontstyle, $curfontsize);
  16711. $curfontdescent = $this->getFontDescent($curfontname, $curfontstyle, $curfontsize);
  16712. $curfontstretcing = $this->font_stretching;
  16713. $curfonttracking = $this->font_spacing;
  16714. $this->newline = true;
  16715. $newline = true;
  16716. $startlinepage = $this->page;
  16717. $minstartliney = $this->y;
  16718. $maxbottomliney = 0;
  16719. $startlinex = $this->x;
  16720. $startliney = $this->y;
  16721. $yshift = 0;
  16722. $loop = 0;
  16723. $curpos = 0;
  16724. $this_method_vars = array();
  16725. $undo = false;
  16726. $fontaligned = false;
  16727. $reverse_dir = false; // true when the text direction is reversed
  16728. $this->premode = false;
  16729. if ($this->inxobj) {
  16730. // we are inside an XObject template
  16731. $pask = count($this->xobjects[$this->xobjid]['annotations']);
  16732. } elseif (isset($this->PageAnnots[$this->page])) {
  16733. $pask = count($this->PageAnnots[$this->page]);
  16734. } else {
  16735. $pask = 0;
  16736. }
  16737. if ($this->inxobj) {
  16738. // we are inside an XObject template
  16739. $startlinepos = strlen($this->xobjects[$this->xobjid]['outdata']);
  16740. } elseif (!$this->InFooter) {
  16741. if (isset($this->footerlen[$this->page])) {
  16742. $this->footerpos[$this->page] = $this->pagelen[$this->page] - $this->footerlen[$this->page];
  16743. } else {
  16744. $this->footerpos[$this->page] = $this->pagelen[$this->page];
  16745. }
  16746. $startlinepos = $this->footerpos[$this->page];
  16747. } else {
  16748. // we are inside the footer
  16749. $startlinepos = $this->pagelen[$this->page];
  16750. }
  16751. $lalign = $align;
  16752. $plalign = $align;
  16753. if ($this->rtl) {
  16754. $w = $this->x - $this->lMargin;
  16755. } else {
  16756. $w = $this->w - $this->rMargin - $this->x;
  16757. }
  16758. $w -= ($this->cell_padding['L'] + $this->cell_padding['R']);
  16759. if ($cell) {
  16760. if ($this->rtl) {
  16761. $this->x -= $this->cell_padding['R'];
  16762. $this->lMargin += $this->cell_padding['L'];
  16763. } else {
  16764. $this->x += $this->cell_padding['L'];
  16765. $this->rMargin += $this->cell_padding['R'];
  16766. }
  16767. }
  16768. if ($this->customlistindent >= 0) {
  16769. $this->listindent = $this->customlistindent;
  16770. } else {
  16771. $this->listindent = $this->GetStringWidth('000000');
  16772. }
  16773. $this->listindentlevel = 0;
  16774. // save previous states
  16775. $prev_cell_height_ratio = $this->cell_height_ratio;
  16776. $prev_listnum = $this->listnum;
  16777. $prev_listordered = $this->listordered;
  16778. $prev_listcount = $this->listcount;
  16779. $prev_lispacer = $this->lispacer;
  16780. $this->listnum = 0;
  16781. $this->listordered = array();
  16782. $this->listcount = array();
  16783. $this->lispacer = '';
  16784. if ((TCPDF_STATIC::empty_string($this->lasth)) OR ($reseth)) {
  16785. // reset row height
  16786. $this->resetLastH();
  16787. }
  16788. $dom = $this->getHtmlDomArray($html);
  16789. $maxel = count($dom);
  16790. $key = 0;
  16791. while ($key < $maxel) {
  16792. if ($dom[$key]['tag'] AND $dom[$key]['opening'] AND $dom[$key]['hide']) {
  16793. // store the node key
  16794. $hidden_node_key = $key;
  16795. if ($dom[$key]['self']) {
  16796. // skip just this self-closing tag
  16797. ++$key;
  16798. } else {
  16799. // skip this and all children tags
  16800. while (($key < $maxel) AND (!$dom[$key]['tag'] OR $dom[$key]['opening'] OR ($dom[$key]['parent'] != $hidden_node_key))) {
  16801. // skip hidden objects
  16802. ++$key;
  16803. }
  16804. ++$key;
  16805. }
  16806. }
  16807. if ($key == $maxel) break;
  16808. if ($dom[$key]['tag'] AND isset($dom[$key]['attribute']['pagebreak'])) {
  16809. // check for pagebreak
  16810. if (($dom[$key]['attribute']['pagebreak'] == 'true') OR ($dom[$key]['attribute']['pagebreak'] == 'left') OR ($dom[$key]['attribute']['pagebreak'] == 'right')) {
  16811. // add a page (or trig AcceptPageBreak() for multicolumn mode)
  16812. $this->checkPageBreak($this->PageBreakTrigger + 1);
  16813. $this->htmlvspace = ($this->PageBreakTrigger + 1);
  16814. }
  16815. if ((($dom[$key]['attribute']['pagebreak'] == 'left') AND (((!$this->rtl) AND (($this->page % 2) == 0)) OR (($this->rtl) AND (($this->page % 2) != 0))))
  16816. OR (($dom[$key]['attribute']['pagebreak'] == 'right') AND (((!$this->rtl) AND (($this->page % 2) != 0)) OR (($this->rtl) AND (($this->page % 2) == 0))))) {
  16817. // add a page (or trig AcceptPageBreak() for multicolumn mode)
  16818. $this->checkPageBreak($this->PageBreakTrigger + 1);
  16819. $this->htmlvspace = ($this->PageBreakTrigger + 1);
  16820. }
  16821. }
  16822. if ($dom[$key]['tag'] AND $dom[$key]['opening'] AND isset($dom[$key]['attribute']['nobr']) AND ($dom[$key]['attribute']['nobr'] == 'true')) {
  16823. if (isset($dom[($dom[$key]['parent'])]['attribute']['nobr']) AND ($dom[($dom[$key]['parent'])]['attribute']['nobr'] == 'true')) {
  16824. $dom[$key]['attribute']['nobr'] = false;
  16825. } else {
  16826. // store current object
  16827. $this->startTransaction();
  16828. // save this method vars
  16829. $this_method_vars['html'] = $html;
  16830. $this_method_vars['ln'] = $ln;
  16831. $this_method_vars['fill'] = $fill;
  16832. $this_method_vars['reseth'] = $reseth;
  16833. $this_method_vars['cell'] = $cell;
  16834. $this_method_vars['align'] = $align;
  16835. $this_method_vars['gvars'] = $gvars;
  16836. $this_method_vars['prevPage'] = $prevPage;
  16837. $this_method_vars['prev_cell_margin'] = $prev_cell_margin;
  16838. $this_method_vars['prev_cell_padding'] = $prev_cell_padding;
  16839. $this_method_vars['prevlMargin'] = $prevlMargin;
  16840. $this_method_vars['prevrMargin'] = $prevrMargin;
  16841. $this_method_vars['curfontname'] = $curfontname;
  16842. $this_method_vars['curfontstyle'] = $curfontstyle;
  16843. $this_method_vars['curfontsize'] = $curfontsize;
  16844. $this_method_vars['curfontascent'] = $curfontascent;
  16845. $this_method_vars['curfontdescent'] = $curfontdescent;
  16846. $this_method_vars['curfontstretcing'] = $curfontstretcing;
  16847. $this_method_vars['curfonttracking'] = $curfonttracking;
  16848. $this_method_vars['minstartliney'] = $minstartliney;
  16849. $this_method_vars['maxbottomliney'] = $maxbottomliney;
  16850. $this_method_vars['yshift'] = $yshift;
  16851. $this_method_vars['startlinepage'] = $startlinepage;
  16852. $this_method_vars['startlinepos'] = $startlinepos;
  16853. $this_method_vars['startlinex'] = $startlinex;
  16854. $this_method_vars['startliney'] = $startliney;
  16855. $this_method_vars['newline'] = $newline;
  16856. $this_method_vars['loop'] = $loop;
  16857. $this_method_vars['curpos'] = $curpos;
  16858. $this_method_vars['pask'] = $pask;
  16859. $this_method_vars['lalign'] = $lalign;
  16860. $this_method_vars['plalign'] = $plalign;
  16861. $this_method_vars['w'] = $w;
  16862. $this_method_vars['prev_cell_height_ratio'] = $prev_cell_height_ratio;
  16863. $this_method_vars['prev_listnum'] = $prev_listnum;
  16864. $this_method_vars['prev_listordered'] = $prev_listordered;
  16865. $this_method_vars['prev_listcount'] = $prev_listcount;
  16866. $this_method_vars['prev_lispacer'] = $prev_lispacer;
  16867. $this_method_vars['fontaligned'] = $fontaligned;
  16868. $this_method_vars['key'] = $key;
  16869. $this_method_vars['dom'] = $dom;
  16870. }
  16871. }
  16872. // print THEAD block
  16873. if (($dom[$key]['value'] == 'tr') AND isset($dom[$key]['thead']) AND $dom[$key]['thead']) {
  16874. if (isset($dom[$key]['parent']) AND isset($dom[$dom[$key]['parent']]['thead']) AND !TCPDF_STATIC::empty_string($dom[$dom[$key]['parent']]['thead'])) {
  16875. $this->inthead = true;
  16876. // print table header (thead)
  16877. $this->writeHTML($this->thead, false, false, false, false, '');
  16878. // check if we are on a new page or on a new column
  16879. if (($this->y < $this->start_transaction_y) OR ($this->checkPageBreak($this->lasth, '', false))) {
  16880. // we are on a new page or on a new column and the total object height is less than the available vertical space.
  16881. // restore previous object
  16882. $this->rollbackTransaction(true);
  16883. // restore previous values
  16884. foreach ($this_method_vars as $vkey => $vval) {
  16885. $$vkey = $vval;
  16886. }
  16887. // disable table header
  16888. $tmp_thead = $this->thead;
  16889. $this->thead = '';
  16890. // add a page (or trig AcceptPageBreak() for multicolumn mode)
  16891. $pre_y = $this->y;
  16892. if ((!$this->checkPageBreak($this->PageBreakTrigger + 1)) AND ($this->y < $pre_y)) {
  16893. // fix for multicolumn mode
  16894. $startliney = $this->y;
  16895. }
  16896. $this->start_transaction_page = $this->page;
  16897. $this->start_transaction_y = $this->y;
  16898. // restore table header
  16899. $this->thead = $tmp_thead;
  16900. // fix table border properties
  16901. if (isset($dom[$dom[$key]['parent']]['attribute']['cellspacing'])) {
  16902. $tmp_cellspacing = $this->getHTMLUnitToUnits($dom[$dom[$key]['parent']]['attribute']['cellspacing'], 1, 'px');
  16903. } elseif (isset($dom[$dom[$key]['parent']]['border-spacing'])) {
  16904. $tmp_cellspacing = $dom[$dom[$key]['parent']]['border-spacing']['V'];
  16905. } else {
  16906. $tmp_cellspacing = 0;
  16907. }
  16908. $dom[$dom[$key]['parent']]['borderposition']['page'] = $this->page;
  16909. $dom[$dom[$key]['parent']]['borderposition']['column'] = $this->current_column;
  16910. $dom[$dom[$key]['parent']]['borderposition']['y'] = $this->y + $tmp_cellspacing;
  16911. $xoffset = ($this->x - $dom[$dom[$key]['parent']]['borderposition']['x']);
  16912. $dom[$dom[$key]['parent']]['borderposition']['x'] += $xoffset;
  16913. $dom[$dom[$key]['parent']]['borderposition']['xmax'] += $xoffset;
  16914. // print table header (thead)
  16915. $this->writeHTML($this->thead, false, false, false, false, '');
  16916. }
  16917. }
  16918. // move $key index forward to skip THEAD block
  16919. while ( ($key < $maxel) AND (!(
  16920. ($dom[$key]['tag'] AND $dom[$key]['opening'] AND ($dom[$key]['value'] == 'tr') AND (!isset($dom[$key]['thead']) OR !$dom[$key]['thead']))
  16921. OR ($dom[$key]['tag'] AND (!$dom[$key]['opening']) AND ($dom[$key]['value'] == 'table'))) )) {
  16922. ++$key;
  16923. }
  16924. }
  16925. if ($dom[$key]['tag'] OR ($key == 0)) {
  16926. if ((($dom[$key]['value'] == 'table') OR ($dom[$key]['value'] == 'tr')) AND (isset($dom[$key]['align']))) {
  16927. $dom[$key]['align'] = ($this->rtl) ? 'R' : 'L';
  16928. }
  16929. // vertically align image in line
  16930. if ((!$this->newline) AND ($dom[$key]['value'] == 'img') AND (isset($dom[$key]['height'])) AND ($dom[$key]['height'] > 0)) {
  16931. // get image height
  16932. $imgh = $this->getHTMLUnitToUnits($dom[$key]['height'], ($dom[$key]['fontsize'] / $this->k), 'px');
  16933. $autolinebreak = false;
  16934. if (!empty($dom[$key]['width'])) {
  16935. $imgw = $this->getHTMLUnitToUnits($dom[$key]['width'], ($dom[$key]['fontsize'] / $this->k), 'px', false);
  16936. if (($imgw <= ($this->w - $this->lMargin - $this->rMargin - $this->cell_padding['L'] - $this->cell_padding['R']))
  16937. AND ((($this->rtl) AND (($this->x - $imgw) < ($this->lMargin + $this->cell_padding['L'])))
  16938. OR ((!$this->rtl) AND (($this->x + $imgw) > ($this->w - $this->rMargin - $this->cell_padding['R']))))) {
  16939. // add automatic line break
  16940. $autolinebreak = true;
  16941. $this->Ln('', $cell);
  16942. if ((!$dom[($key-1)]['tag']) AND ($dom[($key-1)]['value'] == ' ')) {
  16943. // go back to evaluate this line break
  16944. --$key;
  16945. }
  16946. }
  16947. }
  16948. if (!$autolinebreak) {
  16949. if ($this->inPageBody()) {
  16950. $pre_y = $this->y;
  16951. // check for page break
  16952. if ((!$this->checkPageBreak($imgh)) AND ($this->y < $pre_y)) {
  16953. // fix for multicolumn mode
  16954. $startliney = $this->y;
  16955. }
  16956. }
  16957. if ($this->page > $startlinepage) {
  16958. // fix line splitted over two pages
  16959. if (isset($this->footerlen[$startlinepage])) {
  16960. $curpos = $this->pagelen[$startlinepage] - $this->footerlen[$startlinepage];
  16961. }
  16962. // line to be moved one page forward
  16963. $pagebuff = $this->getPageBuffer($startlinepage);
  16964. $linebeg = substr($pagebuff, $startlinepos, ($curpos - $startlinepos));
  16965. $tstart = substr($pagebuff, 0, $startlinepos);
  16966. $tend = substr($this->getPageBuffer($startlinepage), $curpos);
  16967. // remove line from previous page
  16968. $this->setPageBuffer($startlinepage, $tstart.''.$tend);
  16969. $pagebuff = $this->getPageBuffer($this->page);
  16970. $tstart = substr($pagebuff, 0, $this->cntmrk[$this->page]);
  16971. $tend = substr($pagebuff, $this->cntmrk[$this->page]);
  16972. // add line start to current page
  16973. $yshift = ($minstartliney - $this->y);
  16974. if ($fontaligned) {
  16975. $yshift += ($curfontsize / $this->k);
  16976. }
  16977. $try = sprintf('1 0 0 1 0 %F cm', ($yshift * $this->k));
  16978. $this->setPageBuffer($this->page, $tstart."\nq\n".$try."\n".$linebeg."\nQ\n".$tend);
  16979. // shift the annotations and links
  16980. if (isset($this->PageAnnots[$this->page])) {
  16981. $next_pask = count($this->PageAnnots[$this->page]);
  16982. } else {
  16983. $next_pask = 0;
  16984. }
  16985. if (isset($this->PageAnnots[$startlinepage])) {
  16986. foreach ($this->PageAnnots[$startlinepage] as $pak => $pac) {
  16987. if ($pak >= $pask) {
  16988. $this->PageAnnots[$this->page][] = $pac;
  16989. unset($this->PageAnnots[$startlinepage][$pak]);
  16990. $npak = count($this->PageAnnots[$this->page]) - 1;
  16991. $this->PageAnnots[$this->page][$npak]['y'] -= $yshift;
  16992. }
  16993. }
  16994. }
  16995. $pask = $next_pask;
  16996. $startlinepos = $this->cntmrk[$this->page];
  16997. $startlinepage = $this->page;
  16998. $startliney = $this->y;
  16999. $this->newline = false;
  17000. }
  17001. $this->y += ($this->getCellHeight($curfontsize / $this->k) - ($curfontdescent * $this->cell_height_ratio) - $imgh);
  17002. $minstartliney = min($this->y, $minstartliney);
  17003. $maxbottomliney = ($startliney + $this->getCellHeight($curfontsize / $this->k));
  17004. }
  17005. } elseif (isset($dom[$key]['fontname']) OR isset($dom[$key]['fontstyle']) OR isset($dom[$key]['fontsize']) OR isset($dom[$key]['line-height'])) {
  17006. // account for different font size
  17007. $pfontname = $curfontname;
  17008. $pfontstyle = $curfontstyle;
  17009. $pfontsize = $curfontsize;
  17010. $fontname = (isset($dom[$key]['fontname']) ? $dom[$key]['fontname'] : $curfontname);
  17011. $fontstyle = (isset($dom[$key]['fontstyle']) ? $dom[$key]['fontstyle'] : $curfontstyle);
  17012. $fontsize = (isset($dom[$key]['fontsize']) ? $dom[$key]['fontsize'] : $curfontsize);
  17013. $fontascent = $this->getFontAscent($fontname, $fontstyle, $fontsize);
  17014. $fontdescent = $this->getFontDescent($fontname, $fontstyle, $fontsize);
  17015. if (($fontname != $curfontname) OR ($fontstyle != $curfontstyle) OR ($fontsize != $curfontsize)
  17016. OR ($this->cell_height_ratio != $dom[$key]['line-height'])
  17017. OR ($dom[$key]['tag'] AND $dom[$key]['opening'] AND ($dom[$key]['value'] == 'li')) ) {
  17018. if (($key < ($maxel - 1)) AND (
  17019. ($dom[$key]['tag'] AND $dom[$key]['opening'] AND ($dom[$key]['value'] == 'li'))
  17020. OR ($this->cell_height_ratio != $dom[$key]['line-height'])
  17021. OR (!$this->newline AND is_numeric($fontsize) AND is_numeric($curfontsize)
  17022. AND ($fontsize >= 0) AND ($curfontsize >= 0)
  17023. AND (($fontsize != $curfontsize) OR ($fontstyle != $curfontstyle) OR ($fontname != $curfontname)))
  17024. )) {
  17025. if ($this->page > $startlinepage) {
  17026. // fix lines splitted over two pages
  17027. if (isset($this->footerlen[$startlinepage])) {
  17028. $curpos = $this->pagelen[$startlinepage] - $this->footerlen[$startlinepage];
  17029. }
  17030. // line to be moved one page forward
  17031. $pagebuff = $this->getPageBuffer($startlinepage);
  17032. $linebeg = substr($pagebuff, $startlinepos, ($curpos - $startlinepos));
  17033. $tstart = substr($pagebuff, 0, $startlinepos);
  17034. $tend = substr($this->getPageBuffer($startlinepage), $curpos);
  17035. // remove line start from previous page
  17036. $this->setPageBuffer($startlinepage, $tstart.''.$tend);
  17037. $pagebuff = $this->getPageBuffer($this->page);
  17038. $tstart = substr($pagebuff, 0, $this->cntmrk[$this->page]);
  17039. $tend = substr($pagebuff, $this->cntmrk[$this->page]);
  17040. // add line start to current page
  17041. $yshift = ($minstartliney - $this->y);
  17042. $try = sprintf('1 0 0 1 0 %F cm', ($yshift * $this->k));
  17043. $this->setPageBuffer($this->page, $tstart."\nq\n".$try."\n".$linebeg."\nQ\n".$tend);
  17044. // shift the annotations and links
  17045. if (isset($this->PageAnnots[$this->page])) {
  17046. $next_pask = count($this->PageAnnots[$this->page]);
  17047. } else {
  17048. $next_pask = 0;
  17049. }
  17050. if (isset($this->PageAnnots[$startlinepage])) {
  17051. foreach ($this->PageAnnots[$startlinepage] as $pak => $pac) {
  17052. if ($pak >= $pask) {
  17053. $this->PageAnnots[$this->page][] = $pac;
  17054. unset($this->PageAnnots[$startlinepage][$pak]);
  17055. $npak = count($this->PageAnnots[$this->page]) - 1;
  17056. $this->PageAnnots[$this->page][$npak]['y'] -= $yshift;
  17057. }
  17058. }
  17059. }
  17060. $pask = $next_pask;
  17061. $startlinepos = $this->cntmrk[$this->page];
  17062. $startlinepage = $this->page;
  17063. $startliney = $this->y;
  17064. }
  17065. if (!isset($dom[$key]['line-height'])) {
  17066. $dom[$key]['line-height'] = $this->cell_height_ratio;
  17067. }
  17068. if (!$dom[$key]['block']) {
  17069. if (!(isset($dom[($key + 1)]) AND $dom[($key + 1)]['tag'] AND (!$dom[($key + 1)]['opening']) AND ($dom[($key + 1)]['value'] != 'li') AND $dom[$key]['tag'] AND (!$dom[$key]['opening']))) {
  17070. $this->y += (((($curfontsize * $this->cell_height_ratio) - ($fontsize * $dom[$key]['line-height'])) / $this->k) + $curfontascent - $fontascent - $curfontdescent + $fontdescent) / 2;
  17071. }
  17072. if (($dom[$key]['value'] != 'sup') AND ($dom[$key]['value'] != 'sub')) {
  17073. $current_line_align_data = array($key, $minstartliney, $maxbottomliney);
  17074. if (isset($line_align_data) AND (($line_align_data[0] == ($key - 1)) OR (($line_align_data[0] == ($key - 2)) AND (isset($dom[($key - 1)])) AND (preg_match('/^([\s]+)$/', $dom[($key - 1)]['value']) > 0)))) {
  17075. $minstartliney = min($this->y, $line_align_data[1]);
  17076. $maxbottomliney = max(($this->y + $this->getCellHeight($fontsize / $this->k)), $line_align_data[2]);
  17077. } else {
  17078. $minstartliney = min($this->y, $minstartliney);
  17079. $maxbottomliney = max(($this->y + $this->getCellHeight($fontsize / $this->k)), $maxbottomliney);
  17080. }
  17081. $line_align_data = $current_line_align_data;
  17082. }
  17083. }
  17084. $this->cell_height_ratio = $dom[$key]['line-height'];
  17085. $fontaligned = true;
  17086. }
  17087. $this->setFont($fontname, $fontstyle, $fontsize);
  17088. // reset row height
  17089. $this->resetLastH();
  17090. $curfontname = $fontname;
  17091. $curfontstyle = $fontstyle;
  17092. $curfontsize = $fontsize;
  17093. $curfontascent = $fontascent;
  17094. $curfontdescent = $fontdescent;
  17095. }
  17096. }
  17097. // set text rendering mode
  17098. $textstroke = isset($dom[$key]['stroke']) ? $dom[$key]['stroke'] : $this->textstrokewidth;
  17099. $textfill = isset($dom[$key]['fill']) ? $dom[$key]['fill'] : (($this->textrendermode % 2) == 0);
  17100. $textclip = isset($dom[$key]['clip']) ? $dom[$key]['clip'] : ($this->textrendermode > 3);
  17101. $this->setTextRenderingMode($textstroke, $textfill, $textclip);
  17102. if (isset($dom[$key]['font-stretch']) AND ($dom[$key]['font-stretch'] !== false)) {
  17103. $this->setFontStretching($dom[$key]['font-stretch']);
  17104. }
  17105. if (isset($dom[$key]['letter-spacing']) AND ($dom[$key]['letter-spacing'] !== false)) {
  17106. $this->setFontSpacing($dom[$key]['letter-spacing']);
  17107. }
  17108. if (($plalign == 'J') AND $dom[$key]['block']) {
  17109. $plalign = '';
  17110. }
  17111. // get current position on page buffer
  17112. $curpos = $this->pagelen[$startlinepage];
  17113. if (isset($dom[$key]['bgcolor']) AND ($dom[$key]['bgcolor'] !== false)) {
  17114. $this->setFillColorArray($dom[$key]['bgcolor']);
  17115. $wfill = true;
  17116. } else {
  17117. $wfill = $fill | false;
  17118. }
  17119. if (isset($dom[$key]['fgcolor']) AND ($dom[$key]['fgcolor'] !== false)) {
  17120. $this->setTextColorArray($dom[$key]['fgcolor']);
  17121. }
  17122. if (isset($dom[$key]['strokecolor']) AND ($dom[$key]['strokecolor'] !== false)) {
  17123. $this->setDrawColorArray($dom[$key]['strokecolor']);
  17124. }
  17125. if (isset($dom[$key]['align'])) {
  17126. $lalign = $dom[$key]['align'];
  17127. }
  17128. if (TCPDF_STATIC::empty_string($lalign)) {
  17129. $lalign = $align;
  17130. }
  17131. }
  17132. // align lines
  17133. if ($this->newline AND (strlen($dom[$key]['value']) > 0) AND ($dom[$key]['value'] != 'td') AND ($dom[$key]['value'] != 'th')) {
  17134. $newline = true;
  17135. $fontaligned = false;
  17136. // we are at the beginning of a new line
  17137. if (isset($startlinex)) {
  17138. $yshift = ($minstartliney - $startliney);
  17139. if (($yshift > 0) OR ($this->page > $startlinepage)) {
  17140. $yshift = 0;
  17141. }
  17142. $t_x = 0;
  17143. // the last line must be shifted to be aligned as requested
  17144. $linew = abs($this->endlinex - $startlinex);
  17145. if ($this->inxobj) {
  17146. // we are inside an XObject template
  17147. $pstart = substr($this->xobjects[$this->xobjid]['outdata'], 0, $startlinepos);
  17148. if (isset($opentagpos)) {
  17149. $midpos = $opentagpos;
  17150. } else {
  17151. $midpos = 0;
  17152. }
  17153. if ($midpos > 0) {
  17154. $pmid = substr($this->xobjects[$this->xobjid]['outdata'], $startlinepos, ($midpos - $startlinepos));
  17155. $pend = substr($this->xobjects[$this->xobjid]['outdata'], $midpos);
  17156. } else {
  17157. $pmid = substr($this->xobjects[$this->xobjid]['outdata'], $startlinepos);
  17158. $pend = '';
  17159. }
  17160. } else {
  17161. $pstart = substr($this->getPageBuffer($startlinepage), 0, $startlinepos);
  17162. if (isset($opentagpos) AND isset($this->footerlen[$startlinepage]) AND (!$this->InFooter)) {
  17163. $this->footerpos[$startlinepage] = $this->pagelen[$startlinepage] - $this->footerlen[$startlinepage];
  17164. $midpos = min($opentagpos, $this->footerpos[$startlinepage]);
  17165. } elseif (isset($opentagpos)) {
  17166. $midpos = $opentagpos;
  17167. } elseif (isset($this->footerlen[$startlinepage]) AND (!$this->InFooter)) {
  17168. $this->footerpos[$startlinepage] = $this->pagelen[$startlinepage] - $this->footerlen[$startlinepage];
  17169. $midpos = $this->footerpos[$startlinepage];
  17170. } else {
  17171. $midpos = 0;
  17172. }
  17173. if ($midpos > 0) {
  17174. $pmid = substr($this->getPageBuffer($startlinepage), $startlinepos, ($midpos - $startlinepos));
  17175. $pend = substr($this->getPageBuffer($startlinepage), $midpos);
  17176. } else {
  17177. $pmid = substr($this->getPageBuffer($startlinepage), $startlinepos);
  17178. $pend = '';
  17179. }
  17180. }
  17181. if ((((($plalign == 'C') OR ($plalign == 'J') OR (($plalign == 'R') AND (!$this->rtl)) OR (($plalign == 'L') AND ($this->rtl)))))) {
  17182. // calculate shifting amount
  17183. $tw = $w;
  17184. if (($plalign == 'J') AND $this->isRTLTextDir() AND ($this->num_columns > 1)) {
  17185. $tw += $this->cell_padding['R'];
  17186. }
  17187. if ($this->lMargin != $prevlMargin) {
  17188. $tw += ($prevlMargin - $this->lMargin);
  17189. }
  17190. if ($this->rMargin != $prevrMargin) {
  17191. $tw += ($prevrMargin - $this->rMargin);
  17192. }
  17193. $one_space_width = $this->GetStringWidth(chr(32));
  17194. $no = 0; // number of spaces on a line contained on a single block
  17195. if ($this->isRTLTextDir()) { // RTL
  17196. // remove left space if exist
  17197. $pos1 = TCPDF_STATIC::revstrpos($pmid, '[(');
  17198. if ($pos1 > 0) {
  17199. $pos1 = intval($pos1);
  17200. if ($this->isUnicodeFont()) {
  17201. $pos2 = intval(TCPDF_STATIC::revstrpos($pmid, '[('.chr(0).chr(32)));
  17202. $spacelen = 2;
  17203. } else {
  17204. $pos2 = intval(TCPDF_STATIC::revstrpos($pmid, '[('.chr(32)));
  17205. $spacelen = 1;
  17206. }
  17207. if ($pos1 == $pos2) {
  17208. $pmid = substr($pmid, 0, ($pos1 + 2)).substr($pmid, ($pos1 + 2 + $spacelen));
  17209. if (substr($pmid, $pos1, 4) == '[()]') {
  17210. $linew -= $one_space_width;
  17211. } elseif ($pos1 == strpos($pmid, '[(')) {
  17212. $no = 1;
  17213. }
  17214. }
  17215. }
  17216. } else { // LTR
  17217. // remove right space if exist
  17218. $pos1 = TCPDF_STATIC::revstrpos($pmid, ')]');
  17219. if ($pos1 > 0) {
  17220. $pos1 = intval($pos1);
  17221. if ($this->isUnicodeFont()) {
  17222. $pos2 = intval(TCPDF_STATIC::revstrpos($pmid, chr(0).chr(32).')]')) + 2;
  17223. $spacelen = 2;
  17224. } else {
  17225. $pos2 = intval(TCPDF_STATIC::revstrpos($pmid, chr(32).')]')) + 1;
  17226. $spacelen = 1;
  17227. }
  17228. if ($pos1 == $pos2) {
  17229. $pmid = substr($pmid, 0, ($pos1 - $spacelen)).substr($pmid, $pos1);
  17230. $linew -= $one_space_width;
  17231. }
  17232. }
  17233. }
  17234. $mdiff = ($tw - $linew);
  17235. if ($plalign == 'C') {
  17236. if ($this->rtl) {
  17237. $t_x = -($mdiff / 2);
  17238. } else {
  17239. $t_x = ($mdiff / 2);
  17240. }
  17241. } elseif ($plalign == 'R') {
  17242. // right alignment on LTR document
  17243. $t_x = $mdiff;
  17244. } elseif ($plalign == 'L') {
  17245. // left alignment on RTL document
  17246. $t_x = -$mdiff;
  17247. } elseif (($plalign == 'J') AND ($plalign == $lalign)) {
  17248. // Justification
  17249. if ($this->isRTLTextDir()) {
  17250. // align text on the left
  17251. $t_x = -$mdiff;
  17252. }
  17253. $ns = 0; // number of spaces
  17254. $pmidtemp = $pmid;
  17255. // escape special characters
  17256. $pmidtemp = preg_replace('/[\\\][\(]/x', '\\#!#OP#!#', $pmidtemp);
  17257. $pmidtemp = preg_replace('/[\\\][\)]/x', '\\#!#CP#!#', $pmidtemp);
  17258. // search spaces
  17259. if (preg_match_all('/\[\(([^\)]*)\)\]/x', $pmidtemp, $lnstring, PREG_PATTERN_ORDER)) {
  17260. $spacestr = $this->getSpaceString();
  17261. $maxkk = count($lnstring[1]) - 1;
  17262. for ($kk=0; $kk <= $maxkk; ++$kk) {
  17263. // restore special characters
  17264. $lnstring[1][$kk] = str_replace('#!#OP#!#', '(', $lnstring[1][$kk]);
  17265. $lnstring[1][$kk] = str_replace('#!#CP#!#', ')', $lnstring[1][$kk]);
  17266. // store number of spaces on the strings
  17267. $lnstring[2][$kk] = substr_count($lnstring[1][$kk], $spacestr);
  17268. // count total spaces on line
  17269. $ns += $lnstring[2][$kk];
  17270. $lnstring[3][$kk] = $ns;
  17271. }
  17272. if ($ns == 0) {
  17273. $ns = 1;
  17274. }
  17275. // calculate additional space to add to each existing space
  17276. $spacewidth = ($mdiff / ($ns - $no)) * $this->k;
  17277. if ($this->FontSize <= 0) {
  17278. $this->FontSize = 1;
  17279. }
  17280. $spacewidthu = -1000 * ($mdiff + (($ns + $no) * $one_space_width)) / $ns / $this->FontSize;
  17281. if ($this->font_spacing != 0) {
  17282. // fixed spacing mode
  17283. $osw = -1000 * $this->font_spacing / $this->FontSize;
  17284. $spacewidthu += $osw;
  17285. }
  17286. $nsmax = $ns;
  17287. $ns = 0;
  17288. reset($lnstring);
  17289. $offset = 0;
  17290. $strcount = 0;
  17291. $prev_epsposbeg = 0;
  17292. $textpos = 0;
  17293. if ($this->isRTLTextDir()) {
  17294. $textpos = $this->wPt;
  17295. }
  17296. while (preg_match('/([0-9\.\+\-]*)[\s](Td|cm|m|l|c|re)[\s]/x', $pmid, $strpiece, PREG_OFFSET_CAPTURE, $offset) == 1) {
  17297. // check if we are inside a string section '[( ... )]'
  17298. $stroffset = strpos($pmid, '[(', $offset);
  17299. if (($stroffset !== false) AND ($stroffset <= $strpiece[2][1])) {
  17300. // set offset to the end of string section
  17301. $offset = strpos($pmid, ')]', $stroffset);
  17302. while (($offset !== false) AND ($pmid[($offset - 1)] == '\\')) {
  17303. $offset = strpos($pmid, ')]', ($offset + 1));
  17304. }
  17305. if ($offset === false) {
  17306. $this->Error('HTML Justification: malformed PDF code.');
  17307. }
  17308. continue;
  17309. }
  17310. if ($this->isRTLTextDir()) {
  17311. $spacew = ($spacewidth * ($nsmax - $ns));
  17312. } else {
  17313. $spacew = ($spacewidth * $ns);
  17314. }
  17315. $offset = $strpiece[2][1] + strlen($strpiece[2][0]);
  17316. $epsposend = strpos($pmid, $this->epsmarker.'Q', $offset);
  17317. if ($epsposend !== null) {
  17318. $epsposend += strlen($this->epsmarker.'Q');
  17319. $epsposbeg = strpos($pmid, 'q'.$this->epsmarker, $offset);
  17320. if ($epsposbeg === null) {
  17321. $epsposbeg = strpos($pmid, 'q'.$this->epsmarker, ($prev_epsposbeg - 6));
  17322. $prev_epsposbeg = $epsposbeg;
  17323. }
  17324. if (($epsposbeg > 0) AND ($epsposend > 0) AND ($offset > $epsposbeg) AND ($offset < $epsposend)) {
  17325. // shift EPS images
  17326. $trx = sprintf('1 0 0 1 %F 0 cm', $spacew);
  17327. $pmid_b = substr($pmid, 0, $epsposbeg);
  17328. $pmid_m = substr($pmid, $epsposbeg, ($epsposend - $epsposbeg));
  17329. $pmid_e = substr($pmid, $epsposend);
  17330. $pmid = $pmid_b."\nq\n".$trx."\n".$pmid_m."\nQ\n".$pmid_e;
  17331. $offset = $epsposend;
  17332. continue;
  17333. }
  17334. }
  17335. $currentxpos = 0;
  17336. // shift blocks of code
  17337. switch ($strpiece[2][0]) {
  17338. case 'Td':
  17339. case 'cm':
  17340. case 'm':
  17341. case 'l': {
  17342. // get current X position
  17343. preg_match('/([0-9\.\+\-]*)[\s]('.$strpiece[1][0].')[\s]('.$strpiece[2][0].')([\s]*)/x', $pmid, $xmatches);
  17344. if (!isset($xmatches[1])) {
  17345. break;
  17346. }
  17347. $currentxpos = $xmatches[1];
  17348. $textpos = $currentxpos;
  17349. if (($strcount <= $maxkk) AND ($strpiece[2][0] == 'Td')) {
  17350. $ns = $lnstring[3][$strcount];
  17351. if ($this->isRTLTextDir()) {
  17352. $spacew = ($spacewidth * ($nsmax - $ns));
  17353. }
  17354. ++$strcount;
  17355. }
  17356. // justify block
  17357. if (preg_match('/([0-9\.\+\-]*)[\s]('.$strpiece[1][0].')[\s]('.$strpiece[2][0].')([\s]*)/x', $pmid, $pmatch) == 1) {
  17358. $newpmid = sprintf('%F',(floatval($pmatch[1]) + $spacew)).' '.$pmatch[2].' x*#!#*x'.$pmatch[3].$pmatch[4];
  17359. $pmid = str_replace($pmatch[0], $newpmid, $pmid);
  17360. unset($pmatch, $newpmid);
  17361. }
  17362. break;
  17363. }
  17364. case 're': {
  17365. // justify block
  17366. if (!TCPDF_STATIC::empty_string($this->lispacer)) {
  17367. $this->lispacer = '';
  17368. break;
  17369. }
  17370. preg_match('/([0-9\.\+\-]*)[\s]([0-9\.\+\-]*)[\s]([0-9\.\+\-]*)[\s]('.$strpiece[1][0].')[\s](re)([\s]*)/x', $pmid, $xmatches);
  17371. if (!isset($xmatches[1])) {
  17372. break;
  17373. }
  17374. $currentxpos = $xmatches[1];
  17375. $x_diff = 0;
  17376. $w_diff = 0;
  17377. if ($this->isRTLTextDir()) { // RTL
  17378. if ($currentxpos < $textpos) {
  17379. $x_diff = ($spacewidth * ($nsmax - $lnstring[3][$strcount]));
  17380. $w_diff = ($spacewidth * $lnstring[2][$strcount]);
  17381. } else {
  17382. if ($strcount > 0) {
  17383. $x_diff = ($spacewidth * ($nsmax - $lnstring[3][($strcount - 1)]));
  17384. $w_diff = ($spacewidth * $lnstring[2][($strcount - 1)]);
  17385. }
  17386. }
  17387. } else { // LTR
  17388. if ($currentxpos > $textpos) {
  17389. if ($strcount > 0) {
  17390. $x_diff = ($spacewidth * $lnstring[3][($strcount - 1)]);
  17391. }
  17392. $w_diff = ($spacewidth * $lnstring[2][$strcount]);
  17393. } else {
  17394. if ($strcount > 1) {
  17395. $x_diff = ($spacewidth * $lnstring[3][($strcount - 2)]);
  17396. }
  17397. if ($strcount > 0) {
  17398. $w_diff = ($spacewidth * $lnstring[2][($strcount - 1)]);
  17399. }
  17400. }
  17401. }
  17402. if (preg_match('/('.$xmatches[1].')[\s]('.$xmatches[2].')[\s]('.$xmatches[3].')[\s]('.$strpiece[1][0].')[\s](re)([\s]*)/x', $pmid, $pmatch) == 1) {
  17403. $newx = sprintf('%F',(floatval($pmatch[1]) + $x_diff));
  17404. $neww = sprintf('%F',(floatval($pmatch[3]) + $w_diff));
  17405. $newpmid = $newx.' '.$pmatch[2].' '.$neww.' '.$pmatch[4].' x*#!#*x'.$pmatch[5].$pmatch[6];
  17406. $pmid = str_replace($pmatch[0], $newpmid, $pmid);
  17407. unset($pmatch, $newpmid, $newx, $neww);
  17408. }
  17409. break;
  17410. }
  17411. case 'c': {
  17412. // get current X position
  17413. preg_match('/([0-9\.\+\-]*)[\s]([0-9\.\+\-]*)[\s]([0-9\.\+\-]*)[\s]([0-9\.\+\-]*)[\s]([0-9\.\+\-]*)[\s]('.$strpiece[1][0].')[\s](c)([\s]*)/x', $pmid, $xmatches);
  17414. if (!isset($xmatches[1])) {
  17415. break;
  17416. }
  17417. $currentxpos = $xmatches[1];
  17418. // justify block
  17419. if (preg_match('/('.$xmatches[1].')[\s]('.$xmatches[2].')[\s]('.$xmatches[3].')[\s]('.$xmatches[4].')[\s]('.$xmatches[5].')[\s]('.$strpiece[1][0].')[\s](c)([\s]*)/x', $pmid, $pmatch) == 1) {
  17420. $newx1 = sprintf('%F',(floatval($pmatch[1]) + $spacew));
  17421. $newx2 = sprintf('%F',(floatval($pmatch[3]) + $spacew));
  17422. $newx3 = sprintf('%F',(floatval($pmatch[5]) + $spacew));
  17423. $newpmid = $newx1.' '.$pmatch[2].' '.$newx2.' '.$pmatch[4].' '.$newx3.' '.$pmatch[6].' x*#!#*x'.$pmatch[7].$pmatch[8];
  17424. $pmid = str_replace($pmatch[0], $newpmid, $pmid);
  17425. unset($pmatch, $newpmid, $newx1, $newx2, $newx3);
  17426. }
  17427. break;
  17428. }
  17429. }
  17430. // shift the annotations and links
  17431. $cxpos = ($currentxpos / $this->k);
  17432. $lmpos = ($this->lMargin + $this->cell_padding['L'] + $this->feps);
  17433. if ($this->inxobj) {
  17434. // we are inside an XObject template
  17435. foreach ($this->xobjects[$this->xobjid]['annotations'] as $pak => $pac) {
  17436. if (($pac['y'] >= $minstartliney) AND (($pac['x'] * $this->k) >= ($currentxpos - $this->feps)) AND (($pac['x'] * $this->k) <= ($currentxpos + $this->feps))) {
  17437. if ($cxpos > $lmpos) {
  17438. $this->xobjects[$this->xobjid]['annotations'][$pak]['x'] += ($spacew / $this->k);
  17439. $this->xobjects[$this->xobjid]['annotations'][$pak]['w'] += (($spacewidth * $pac['numspaces']) / $this->k);
  17440. } else {
  17441. $this->xobjects[$this->xobjid]['annotations'][$pak]['w'] += (($spacewidth * $pac['numspaces']) / $this->k);
  17442. }
  17443. break;
  17444. }
  17445. }
  17446. } elseif (isset($this->PageAnnots[$this->page])) {
  17447. foreach ($this->PageAnnots[$this->page] as $pak => $pac) {
  17448. if (($pac['y'] >= $minstartliney) AND (($pac['x'] * $this->k) >= ($currentxpos - $this->feps)) AND (($pac['x'] * $this->k) <= ($currentxpos + $this->feps))) {
  17449. if ($cxpos > $lmpos) {
  17450. $this->PageAnnots[$this->page][$pak]['x'] += ($spacew / $this->k);
  17451. $this->PageAnnots[$this->page][$pak]['w'] += (($spacewidth * $pac['numspaces']) / $this->k);
  17452. } else {
  17453. $this->PageAnnots[$this->page][$pak]['w'] += (($spacewidth * $pac['numspaces']) / $this->k);
  17454. }
  17455. break;
  17456. }
  17457. }
  17458. }
  17459. } // end of while
  17460. // remove markers
  17461. $pmid = str_replace('x*#!#*x', '', $pmid);
  17462. if ($this->isUnicodeFont()) {
  17463. // multibyte characters
  17464. $spacew = $spacewidthu;
  17465. if ($this->font_stretching != 100) {
  17466. // word spacing is affected by stretching
  17467. $spacew /= ($this->font_stretching / 100);
  17468. }
  17469. // escape special characters
  17470. $pos = 0;
  17471. $pmid = preg_replace('/[\\\][\(]/x', '\\#!#OP#!#', $pmid);
  17472. $pmid = preg_replace('/[\\\][\)]/x', '\\#!#CP#!#', $pmid);
  17473. if (preg_match_all('/\[\(([^\)]*)\)\]/x', $pmid, $pamatch) > 0) {
  17474. foreach($pamatch[0] as $pk => $pmatch) {
  17475. $replace = $pamatch[1][$pk];
  17476. $replace = str_replace('#!#OP#!#', '(', $replace);
  17477. $replace = str_replace('#!#CP#!#', ')', $replace);
  17478. $newpmid = '[('.str_replace(chr(0).chr(32), ') '.sprintf('%F', $spacew).' (', $replace).')]';
  17479. $pos = strpos($pmid, $pmatch, $pos);
  17480. if ($pos !== FALSE) {
  17481. $pmid = substr_replace($pmid, $newpmid, $pos, strlen($pmatch));
  17482. }
  17483. ++$pos;
  17484. }
  17485. unset($pamatch);
  17486. }
  17487. if ($this->inxobj) {
  17488. // we are inside an XObject template
  17489. $this->xobjects[$this->xobjid]['outdata'] = $pstart."\n".$pmid."\n".$pend;
  17490. } else {
  17491. $this->setPageBuffer($startlinepage, $pstart."\n".$pmid."\n".$pend);
  17492. }
  17493. $endlinepos = strlen($pstart."\n".$pmid."\n");
  17494. } else {
  17495. // non-unicode (single-byte characters)
  17496. if ($this->font_stretching != 100) {
  17497. // word spacing (Tw) is affected by stretching
  17498. $spacewidth /= ($this->font_stretching / 100);
  17499. }
  17500. $rs = sprintf('%F Tw', $spacewidth);
  17501. $pmid = preg_replace("/\[\(/x", $rs.' [(', $pmid);
  17502. if ($this->inxobj) {
  17503. // we are inside an XObject template
  17504. $this->xobjects[$this->xobjid]['outdata'] = $pstart."\n".$pmid."\nBT 0 Tw ET\n".$pend;
  17505. } else {
  17506. $this->setPageBuffer($startlinepage, $pstart."\n".$pmid."\nBT 0 Tw ET\n".$pend);
  17507. }
  17508. $endlinepos = strlen($pstart."\n".$pmid."\nBT 0 Tw ET\n");
  17509. }
  17510. }
  17511. } // end of J
  17512. } // end if $startlinex
  17513. if (($t_x != 0) OR ($yshift < 0)) {
  17514. // shift the line
  17515. $trx = sprintf('1 0 0 1 %F %F cm', ($t_x * $this->k), ($yshift * $this->k));
  17516. $pstart .= "\nq\n".$trx."\n".$pmid."\nQ\n";
  17517. $endlinepos = strlen($pstart);
  17518. if ($this->inxobj) {
  17519. // we are inside an XObject template
  17520. $this->xobjects[$this->xobjid]['outdata'] = $pstart.$pend;
  17521. foreach ($this->xobjects[$this->xobjid]['annotations'] as $pak => $pac) {
  17522. if ($pak >= $pask) {
  17523. $this->xobjects[$this->xobjid]['annotations'][$pak]['x'] += $t_x;
  17524. $this->xobjects[$this->xobjid]['annotations'][$pak]['y'] -= $yshift;
  17525. }
  17526. }
  17527. } else {
  17528. $this->setPageBuffer($startlinepage, $pstart.$pend);
  17529. // shift the annotations and links
  17530. if (isset($this->PageAnnots[$this->page])) {
  17531. foreach ($this->PageAnnots[$this->page] as $pak => $pac) {
  17532. if ($pak >= $pask) {
  17533. $this->PageAnnots[$this->page][$pak]['x'] += $t_x;
  17534. $this->PageAnnots[$this->page][$pak]['y'] -= $yshift;
  17535. }
  17536. }
  17537. }
  17538. }
  17539. $this->y -= $yshift;
  17540. }
  17541. }
  17542. $pbrk = $this->checkPageBreak($this->lasth);
  17543. $this->newline = false;
  17544. $startlinex = $this->x;
  17545. $startliney = $this->y;
  17546. if ($dom[$dom[$key]['parent']]['value'] == 'sup') {
  17547. $startliney -= ((0.3 * $this->FontSizePt) / $this->k);
  17548. } elseif ($dom[$dom[$key]['parent']]['value'] == 'sub') {
  17549. $startliney -= (($this->FontSizePt / 0.7) / $this->k);
  17550. } else {
  17551. $minstartliney = $startliney;
  17552. $maxbottomliney = ($this->y + $this->getCellHeight($fontsize / $this->k));
  17553. }
  17554. $startlinepage = $this->page;
  17555. if (isset($endlinepos) AND (!$pbrk)) {
  17556. $startlinepos = $endlinepos;
  17557. } else {
  17558. if ($this->inxobj) {
  17559. // we are inside an XObject template
  17560. $startlinepos = strlen($this->xobjects[$this->xobjid]['outdata']);
  17561. } elseif (!$this->InFooter) {
  17562. if (isset($this->footerlen[$this->page])) {
  17563. $this->footerpos[$this->page] = $this->pagelen[$this->page] - $this->footerlen[$this->page];
  17564. } else {
  17565. $this->footerpos[$this->page] = $this->pagelen[$this->page];
  17566. }
  17567. $startlinepos = $this->footerpos[$this->page];
  17568. } else {
  17569. $startlinepos = $this->pagelen[$this->page];
  17570. }
  17571. }
  17572. unset($endlinepos);
  17573. $plalign = $lalign;
  17574. if (isset($this->PageAnnots[$this->page])) {
  17575. $pask = count($this->PageAnnots[$this->page]);
  17576. } else {
  17577. $pask = 0;
  17578. }
  17579. if (!($dom[$key]['tag'] AND !$dom[$key]['opening'] AND ($dom[$key]['value'] == 'table')
  17580. AND (isset($this->emptypagemrk[$this->page]))
  17581. AND ($this->emptypagemrk[$this->page] == $this->pagelen[$this->page]))) {
  17582. $this->setFont($fontname, $fontstyle, $fontsize);
  17583. if ($wfill) {
  17584. $this->setFillColorArray($this->bgcolor);
  17585. }
  17586. }
  17587. } // end newline
  17588. if (isset($opentagpos)) {
  17589. unset($opentagpos);
  17590. }
  17591. if ($dom[$key]['tag']) {
  17592. if ($dom[$key]['opening']) {
  17593. // get text indentation (if any)
  17594. if (isset($dom[$key]['text-indent']) AND $dom[$key]['block']) {
  17595. $this->textindent = $dom[$key]['text-indent'];
  17596. $this->newline = true;
  17597. }
  17598. // table
  17599. if (($dom[$key]['value'] == 'table') AND isset($dom[$key]['cols']) AND ($dom[$key]['cols'] > 0)) {
  17600. // available page width
  17601. if ($this->rtl) {
  17602. $wtmp = $this->x - $this->lMargin;
  17603. } else {
  17604. $wtmp = $this->w - $this->rMargin - $this->x;
  17605. }
  17606. // get cell spacing
  17607. if (isset($dom[$key]['attribute']['cellspacing'])) {
  17608. $clsp = $this->getHTMLUnitToUnits($dom[$key]['attribute']['cellspacing'], 1, 'px');
  17609. $cellspacing = array('H' => $clsp, 'V' => $clsp);
  17610. } elseif (isset($dom[$key]['border-spacing'])) {
  17611. $cellspacing = $dom[$key]['border-spacing'];
  17612. } else {
  17613. $cellspacing = array('H' => 0, 'V' => 0);
  17614. }
  17615. // table width
  17616. if (isset($dom[$key]['width'])) {
  17617. $table_width = $this->getHTMLUnitToUnits($dom[$key]['width'], $wtmp, 'px');
  17618. } else {
  17619. $table_width = $wtmp;
  17620. }
  17621. $table_width -= (2 * $cellspacing['H']);
  17622. if (!$this->inthead) {
  17623. $this->y += $cellspacing['V'];
  17624. }
  17625. if ($this->rtl) {
  17626. $cellspacingx = -$cellspacing['H'];
  17627. } else {
  17628. $cellspacingx = $cellspacing['H'];
  17629. }
  17630. // total table width without cellspaces
  17631. $table_columns_width = ($table_width - ($cellspacing['H'] * ($dom[$key]['cols'] - 1)));
  17632. // minimum column width
  17633. $table_min_column_width = ($table_columns_width / $dom[$key]['cols']);
  17634. // array of custom column widths
  17635. $table_colwidths = array_fill(0, $dom[$key]['cols'], $table_min_column_width);
  17636. }
  17637. // table row
  17638. if ($dom[$key]['value'] == 'tr') {
  17639. // reset column counter
  17640. $colid = 0;
  17641. }
  17642. // table cell
  17643. if (($dom[$key]['value'] == 'td') OR ($dom[$key]['value'] == 'th')) {
  17644. $trid = $dom[$key]['parent'];
  17645. $table_el = $dom[$trid]['parent'];
  17646. if (!isset($dom[$table_el]['cols'])) {
  17647. $dom[$table_el]['cols'] = $dom[$trid]['cols'];
  17648. }
  17649. // store border info
  17650. $tdborder = 0;
  17651. if (isset($dom[$key]['border']) AND !empty($dom[$key]['border'])) {
  17652. $tdborder = $dom[$key]['border'];
  17653. }
  17654. $colspan = intval($dom[$key]['attribute']['colspan']);
  17655. if ($colspan <= 0) {
  17656. $colspan = 1;
  17657. }
  17658. $old_cell_padding = $this->cell_padding;
  17659. if (isset($dom[($dom[$trid]['parent'])]['attribute']['cellpadding'])) {
  17660. $crclpd = $this->getHTMLUnitToUnits($dom[($dom[$trid]['parent'])]['attribute']['cellpadding'], 1, 'px');
  17661. $current_cell_padding = array('L' => $crclpd, 'T' => $crclpd, 'R' => $crclpd, 'B' => $crclpd);
  17662. } elseif (isset($dom[($dom[$trid]['parent'])]['padding'])) {
  17663. $current_cell_padding = $dom[($dom[$trid]['parent'])]['padding'];
  17664. } else {
  17665. $current_cell_padding = array('L' => 0, 'T' => 0, 'R' => 0, 'B' => 0);
  17666. }
  17667. $this->cell_padding = $current_cell_padding;
  17668. if (isset($dom[$key]['height'])) {
  17669. // minimum cell height
  17670. $cellh = $this->getHTMLUnitToUnits($dom[$key]['height'], 0, 'px');
  17671. } else {
  17672. $cellh = 0;
  17673. }
  17674. if (isset($dom[$key]['content'])) {
  17675. $cell_content = $dom[$key]['content'];
  17676. } else {
  17677. $cell_content = '&nbsp;';
  17678. }
  17679. $tagtype = $dom[$key]['value'];
  17680. $parentid = $key;
  17681. while (($key < $maxel) AND (!(($dom[$key]['tag']) AND (!$dom[$key]['opening']) AND ($dom[$key]['value'] == $tagtype) AND ($dom[$key]['parent'] == $parentid)))) {
  17682. // move $key index forward
  17683. ++$key;
  17684. }
  17685. if (!isset($dom[$trid]['startpage'])) {
  17686. $dom[$trid]['startpage'] = $this->page;
  17687. } else {
  17688. $this->setPage($dom[$trid]['startpage']);
  17689. }
  17690. if (!isset($dom[$trid]['startcolumn'])) {
  17691. $dom[$trid]['startcolumn'] = $this->current_column;
  17692. } elseif ($this->current_column != $dom[$trid]['startcolumn']) {
  17693. $tmpx = $this->x;
  17694. $this->selectColumn($dom[$trid]['startcolumn']);
  17695. $this->x = $tmpx;
  17696. }
  17697. if (!isset($dom[$trid]['starty'])) {
  17698. $dom[$trid]['starty'] = $this->y;
  17699. } else {
  17700. $this->y = $dom[$trid]['starty'];
  17701. }
  17702. if (!isset($dom[$trid]['startx'])) {
  17703. $dom[$trid]['startx'] = $this->x;
  17704. $this->x += $cellspacingx;
  17705. } else {
  17706. $this->x += ($cellspacingx / 2);
  17707. }
  17708. if (isset($dom[$parentid]['attribute']['rowspan'])) {
  17709. $rowspan = intval($dom[$parentid]['attribute']['rowspan']);
  17710. } else {
  17711. $rowspan = 1;
  17712. }
  17713. // skip row-spanned cells started on the previous rows
  17714. if (isset($dom[$table_el]['rowspans'])) {
  17715. $rsk = 0;
  17716. $rskmax = count($dom[$table_el]['rowspans']);
  17717. while ($rsk < $rskmax) {
  17718. $trwsp = $dom[$table_el]['rowspans'][$rsk];
  17719. $rsstartx = $trwsp['startx'];
  17720. $rsendx = $trwsp['endx'];
  17721. // account for margin changes
  17722. if ($trwsp['startpage'] < $this->page) {
  17723. if (($this->rtl) AND ($this->pagedim[$this->page]['orm'] != $this->pagedim[$trwsp['startpage']]['orm'])) {
  17724. $dl = ($this->pagedim[$this->page]['orm'] - $this->pagedim[$trwsp['startpage']]['orm']);
  17725. $rsstartx -= $dl;
  17726. $rsendx -= $dl;
  17727. } elseif ((!$this->rtl) AND ($this->pagedim[$this->page]['olm'] != $this->pagedim[$trwsp['startpage']]['olm'])) {
  17728. $dl = ($this->pagedim[$this->page]['olm'] - $this->pagedim[$trwsp['startpage']]['olm']);
  17729. $rsstartx += $dl;
  17730. $rsendx += $dl;
  17731. }
  17732. }
  17733. if (($trwsp['rowspan'] > 0)
  17734. AND ($rsstartx > ($this->x - $cellspacing['H'] - $current_cell_padding['L'] - $this->feps))
  17735. AND ($rsstartx < ($this->x + $cellspacing['H'] + $current_cell_padding['R'] + $this->feps))
  17736. AND (($trwsp['starty'] < ($this->y - $this->feps)) OR ($trwsp['startpage'] < $this->page) OR ($trwsp['startcolumn'] < $this->current_column))) {
  17737. // set the starting X position of the current cell
  17738. $this->x = $rsendx + $cellspacingx;
  17739. // increment column indicator
  17740. $colid += $trwsp['colspan'];
  17741. if (($trwsp['rowspan'] == 1)
  17742. AND (isset($dom[$trid]['endy']))
  17743. AND (isset($dom[$trid]['endpage']))
  17744. AND (isset($dom[$trid]['endcolumn']))
  17745. AND ($trwsp['endpage'] == $dom[$trid]['endpage'])
  17746. AND ($trwsp['endcolumn'] == $dom[$trid]['endcolumn'])) {
  17747. // set ending Y position for row
  17748. $dom[$table_el]['rowspans'][$rsk]['endy'] = max($dom[$trid]['endy'], $trwsp['endy']);
  17749. $dom[$trid]['endy'] = $dom[$table_el]['rowspans'][$rsk]['endy'];
  17750. }
  17751. $rsk = 0;
  17752. } else {
  17753. ++$rsk;
  17754. }
  17755. }
  17756. }
  17757. if (isset($dom[$parentid]['width'])) {
  17758. // user specified width
  17759. $cellw = $this->getHTMLUnitToUnits($dom[$parentid]['width'], $table_columns_width, 'px');
  17760. $tmpcw = ($cellw / $colspan);
  17761. for ($i = 0; $i < $colspan; ++$i) {
  17762. $table_colwidths[($colid + $i)] = $tmpcw;
  17763. }
  17764. } else {
  17765. // inherit column width
  17766. $cellw = 0;
  17767. for ($i = 0; $i < $colspan; ++$i) {
  17768. $cellw += (isset($table_colwidths[($colid + $i)]) ? $table_colwidths[($colid + $i)] : 0);
  17769. }
  17770. }
  17771. $cellw += (($colspan - 1) * $cellspacing['H']);
  17772. // increment column indicator
  17773. $colid += $colspan;
  17774. // add rowspan information to table element
  17775. if ($rowspan > 1) {
  17776. $trsid = array_push($dom[$table_el]['rowspans'], array('trid' => $trid, 'rowspan' => $rowspan, 'mrowspan' => $rowspan, 'colspan' => $colspan, 'startpage' => $this->page, 'startcolumn' => $this->current_column, 'startx' => $this->x, 'starty' => $this->y));
  17777. }
  17778. $cellid = array_push($dom[$trid]['cellpos'], array('startx' => $this->x));
  17779. if ($rowspan > 1) {
  17780. $dom[$trid]['cellpos'][($cellid - 1)]['rowspanid'] = ($trsid - 1);
  17781. }
  17782. // push background colors
  17783. if (isset($dom[$parentid]['bgcolor']) AND ($dom[$parentid]['bgcolor'] !== false)) {
  17784. $dom[$trid]['cellpos'][($cellid - 1)]['bgcolor'] = $dom[$parentid]['bgcolor'];
  17785. }
  17786. // store border info
  17787. if (!empty($tdborder)) {
  17788. $dom[$trid]['cellpos'][($cellid - 1)]['border'] = $tdborder;
  17789. }
  17790. $prevLastH = $this->lasth;
  17791. // store some info for multicolumn mode
  17792. if ($this->rtl) {
  17793. $this->colxshift['x'] = $this->w - $this->x - $this->rMargin;
  17794. } else {
  17795. $this->colxshift['x'] = $this->x - $this->lMargin;
  17796. }
  17797. $this->colxshift['s'] = $cellspacing;
  17798. $this->colxshift['p'] = $current_cell_padding;
  17799. // ****** write the cell content ******
  17800. $this->MultiCell($cellw, $cellh, $cell_content, false, $lalign, false, 2, '', '', true, 0, true, true, 0, 'T', false);
  17801. // restore some values
  17802. $this->colxshift = array('x' => 0, 's' => array('H' => 0, 'V' => 0), 'p' => array('L' => 0, 'T' => 0, 'R' => 0, 'B' => 0));
  17803. $this->lasth = $prevLastH;
  17804. $this->cell_padding = $old_cell_padding;
  17805. $dom[$trid]['cellpos'][($cellid - 1)]['endx'] = $this->x;
  17806. // update the end of row position
  17807. if ($rowspan <= 1) {
  17808. if (isset($dom[$trid]['endy'])) {
  17809. if (($this->page == $dom[$trid]['endpage']) AND ($this->current_column == $dom[$trid]['endcolumn'])) {
  17810. $dom[$trid]['endy'] = max($this->y, $dom[$trid]['endy']);
  17811. } elseif (($this->page > $dom[$trid]['endpage']) OR ($this->current_column > $dom[$trid]['endcolumn'])) {
  17812. $dom[$trid]['endy'] = $this->y;
  17813. }
  17814. } else {
  17815. $dom[$trid]['endy'] = $this->y;
  17816. }
  17817. if (isset($dom[$trid]['endpage'])) {
  17818. $dom[$trid]['endpage'] = max($this->page, $dom[$trid]['endpage']);
  17819. } else {
  17820. $dom[$trid]['endpage'] = $this->page;
  17821. }
  17822. if (isset($dom[$trid]['endcolumn'])) {
  17823. $dom[$trid]['endcolumn'] = max($this->current_column, $dom[$trid]['endcolumn']);
  17824. } else {
  17825. $dom[$trid]['endcolumn'] = $this->current_column;
  17826. }
  17827. } else {
  17828. // account for row-spanned cells
  17829. $dom[$table_el]['rowspans'][($trsid - 1)]['endx'] = $this->x;
  17830. $dom[$table_el]['rowspans'][($trsid - 1)]['endy'] = $this->y;
  17831. $dom[$table_el]['rowspans'][($trsid - 1)]['endpage'] = $this->page;
  17832. $dom[$table_el]['rowspans'][($trsid - 1)]['endcolumn'] = $this->current_column;
  17833. }
  17834. if (isset($dom[$table_el]['rowspans'])) {
  17835. // update endy and endpage on rowspanned cells
  17836. foreach ($dom[$table_el]['rowspans'] as $k => $trwsp) {
  17837. if ($trwsp['rowspan'] > 0) {
  17838. if (isset($dom[$trid]['endpage'])) {
  17839. if (($trwsp['endpage'] == $dom[$trid]['endpage']) AND ($trwsp['endcolumn'] == $dom[$trid]['endcolumn'])) {
  17840. $dom[$table_el]['rowspans'][$k]['endy'] = max($dom[$trid]['endy'], $trwsp['endy']);
  17841. } elseif (($trwsp['endpage'] < $dom[$trid]['endpage']) OR ($trwsp['endcolumn'] < $dom[$trid]['endcolumn'])) {
  17842. $dom[$table_el]['rowspans'][$k]['endy'] = $dom[$trid]['endy'];
  17843. $dom[$table_el]['rowspans'][$k]['endpage'] = $dom[$trid]['endpage'];
  17844. $dom[$table_el]['rowspans'][$k]['endcolumn'] = $dom[$trid]['endcolumn'];
  17845. } else {
  17846. $dom[$trid]['endy'] = $this->pagedim[$dom[$trid]['endpage']]['hk'] - $this->pagedim[$dom[$trid]['endpage']]['bm'];
  17847. }
  17848. }
  17849. }
  17850. }
  17851. }
  17852. $this->x += ($cellspacingx / 2);
  17853. } else {
  17854. // opening tag (or self-closing tag)
  17855. if (!isset($opentagpos)) {
  17856. if ($this->inxobj) {
  17857. // we are inside an XObject template
  17858. $opentagpos = strlen($this->xobjects[$this->xobjid]['outdata']);
  17859. } elseif (!$this->InFooter) {
  17860. if (isset($this->footerlen[$this->page])) {
  17861. $this->footerpos[$this->page] = $this->pagelen[$this->page] - $this->footerlen[$this->page];
  17862. } else {
  17863. $this->footerpos[$this->page] = $this->pagelen[$this->page];
  17864. }
  17865. $opentagpos = $this->footerpos[$this->page];
  17866. }
  17867. }
  17868. $dom = $this->openHTMLTagHandler($dom, $key, $cell);
  17869. }
  17870. } else { // closing tag
  17871. $prev_numpages = $this->numpages;
  17872. $old_bordermrk = $this->bordermrk[$this->page];
  17873. $dom = $this->closeHTMLTagHandler($dom, $key, $cell, $maxbottomliney);
  17874. if ($this->bordermrk[$this->page] > $old_bordermrk) {
  17875. $startlinepos += ($this->bordermrk[$this->page] - $old_bordermrk);
  17876. }
  17877. if ($prev_numpages > $this->numpages) {
  17878. $startlinepage = $this->page;
  17879. }
  17880. }
  17881. } elseif (strlen($dom[$key]['value']) > 0) {
  17882. // print list-item
  17883. if (!TCPDF_STATIC::empty_string($this->lispacer) AND ($this->lispacer != '^')) {
  17884. $this->setFont($pfontname, $pfontstyle, $pfontsize);
  17885. $this->resetLastH();
  17886. $minstartliney = $this->y;
  17887. $maxbottomliney = ($startliney + $this->getCellHeight($this->FontSize));
  17888. if (is_numeric($pfontsize) AND ($pfontsize > 0)) {
  17889. $this->putHtmlListBullet($this->listnum, $this->lispacer, $pfontsize);
  17890. }
  17891. $this->setFont($curfontname, $curfontstyle, $curfontsize);
  17892. $this->resetLastH();
  17893. if (is_numeric($pfontsize) AND ($pfontsize > 0) AND is_numeric($curfontsize) AND ($curfontsize > 0) AND ($pfontsize != $curfontsize)) {
  17894. $pfontascent = $this->getFontAscent($pfontname, $pfontstyle, $pfontsize);
  17895. $pfontdescent = $this->getFontDescent($pfontname, $pfontstyle, $pfontsize);
  17896. $this->y += ($this->getCellHeight(($pfontsize - $curfontsize) / $this->k) + $pfontascent - $curfontascent - $pfontdescent + $curfontdescent) / 2;
  17897. $minstartliney = min($this->y, $minstartliney);
  17898. $maxbottomliney = max(($this->y + $this->getCellHeight($pfontsize / $this->k)), $maxbottomliney);
  17899. }
  17900. }
  17901. // text
  17902. $this->htmlvspace = 0;
  17903. $isRTLString = preg_match(TCPDF_FONT_DATA::$uni_RE_PATTERN_RTL, $dom[$key]['value']) || preg_match(TCPDF_FONT_DATA::$uni_RE_PATTERN_ARABIC, $dom[$key]['value']);
  17904. if ((!$this->premode) AND $this->isRTLTextDir() AND !$isRTLString) {
  17905. // reverse spaces order
  17906. $lsp = ''; // left spaces
  17907. $rsp = ''; // right spaces
  17908. if (preg_match('/^('.$this->re_space['p'].'+)/'.$this->re_space['m'], $dom[$key]['value'], $matches)) {
  17909. $lsp = $matches[1];
  17910. }
  17911. if (preg_match('/('.$this->re_space['p'].'+)$/'.$this->re_space['m'], $dom[$key]['value'], $matches)) {
  17912. $rsp = $matches[1];
  17913. }
  17914. $dom[$key]['value'] = $rsp.$this->stringTrim($dom[$key]['value']).$lsp;
  17915. }
  17916. if ($newline) {
  17917. if (!$this->premode) {
  17918. $prelen = strlen($dom[$key]['value']);
  17919. if ($this->isRTLTextDir() AND !$isRTLString) {
  17920. // right trim except non-breaking space
  17921. $dom[$key]['value'] = $this->stringRightTrim($dom[$key]['value']);
  17922. } else {
  17923. // left trim except non-breaking space
  17924. $dom[$key]['value'] = $this->stringLeftTrim($dom[$key]['value']);
  17925. }
  17926. $postlen = strlen($dom[$key]['value']);
  17927. if (($postlen == 0) AND ($prelen > 0)) {
  17928. $dom[$key]['trimmed_space'] = true;
  17929. }
  17930. }
  17931. $newline = false;
  17932. $firstblock = true;
  17933. } else {
  17934. $firstblock = false;
  17935. // replace empty multiple spaces string with a single space
  17936. $dom[$key]['value'] = preg_replace('/^'.$this->re_space['p'].'+$/'.$this->re_space['m'], chr(32), $dom[$key]['value']);
  17937. }
  17938. $strrest = '';
  17939. if ($this->rtl) {
  17940. $this->x -= $this->textindent;
  17941. } else {
  17942. $this->x += $this->textindent;
  17943. }
  17944. if (!isset($dom[$key]['trimmed_space']) OR !$dom[$key]['trimmed_space']) {
  17945. $strlinelen = $this->GetStringWidth($dom[$key]['value']);
  17946. if (!empty($this->HREF) AND (isset($this->HREF['url']))) {
  17947. // HTML <a> Link
  17948. $hrefcolor = '';
  17949. if (isset($dom[($dom[$key]['parent'])]['fgcolor']) AND ($dom[($dom[$key]['parent'])]['fgcolor'] !== false)) {
  17950. $hrefcolor = $dom[($dom[$key]['parent'])]['fgcolor'];
  17951. }
  17952. $hrefstyle = -1;
  17953. if (isset($dom[($dom[$key]['parent'])]['fontstyle']) AND ($dom[($dom[$key]['parent'])]['fontstyle'] !== false)) {
  17954. $hrefstyle = $dom[($dom[$key]['parent'])]['fontstyle'];
  17955. }
  17956. $strrest = $this->addHtmlLink($this->HREF['url'], $dom[$key]['value'], $wfill, true, $hrefcolor, $hrefstyle, true);
  17957. } else {
  17958. $wadj = 0; // space to leave for block continuity
  17959. if ($this->rtl) {
  17960. $cwa = ($this->x - $this->lMargin);
  17961. } else {
  17962. $cwa = ($this->w - $this->rMargin - $this->x);
  17963. }
  17964. if (($strlinelen < $cwa) AND (isset($dom[($key + 1)])) AND ($dom[($key + 1)]['tag']) AND (!$dom[($key + 1)]['block'])) {
  17965. // check the next text blocks for continuity
  17966. $nkey = ($key + 1);
  17967. $write_block = true;
  17968. $same_textdir = true;
  17969. $tmp_fontname = $this->FontFamily;
  17970. $tmp_fontstyle = $this->FontStyle;
  17971. $tmp_fontsize = $this->FontSizePt;
  17972. while ($write_block AND isset($dom[$nkey])) {
  17973. if ($dom[$nkey]['tag']) {
  17974. if ($dom[$nkey]['block']) {
  17975. // end of block
  17976. $write_block = false;
  17977. }
  17978. $tmp_fontname = isset($dom[$nkey]['fontname']) ? $dom[$nkey]['fontname'] : $this->FontFamily;
  17979. $tmp_fontstyle = isset($dom[$nkey]['fontstyle']) ? $dom[$nkey]['fontstyle'] : $this->FontStyle;
  17980. $tmp_fontsize = isset($dom[$nkey]['fontsize']) ? $dom[$nkey]['fontsize'] : $this->FontSizePt;
  17981. $same_textdir = ($dom[$nkey]['dir'] == $dom[$key]['dir']);
  17982. } else {
  17983. $nextstr = TCPDF_STATIC::pregSplit('/'.$this->re_space['p'].'+/', $this->re_space['m'], $dom[$nkey]['value']);
  17984. if (isset($nextstr[0]) AND $same_textdir) {
  17985. $wadj += $this->GetStringWidth($nextstr[0], $tmp_fontname, $tmp_fontstyle, $tmp_fontsize);
  17986. if (isset($nextstr[1])) {
  17987. $write_block = false;
  17988. }
  17989. }
  17990. }
  17991. ++$nkey;
  17992. }
  17993. }
  17994. if (($wadj > 0) AND (($strlinelen + $wadj) >= $cwa)) {
  17995. $wadj = 0;
  17996. $nextstr = TCPDF_STATIC::pregSplit('/'.$this->re_space['p'].'/', $this->re_space['m'], $dom[$key]['value']);
  17997. $numblks = count($nextstr);
  17998. if ($numblks > 1) {
  17999. // try to split on blank spaces
  18000. $wadj = ($cwa - $strlinelen + $this->GetStringWidth($nextstr[($numblks - 1)]));
  18001. } else {
  18002. // set the entire block on new line
  18003. $wadj = $this->GetStringWidth($nextstr[0]);
  18004. }
  18005. }
  18006. // check for reversed text direction
  18007. if (($wadj > 0) AND (($this->rtl AND ($this->tmprtl === 'L')) OR (!$this->rtl AND ($this->tmprtl === 'R')))) {
  18008. // LTR text on RTL direction or RTL text on LTR direction
  18009. $reverse_dir = true;
  18010. $this->rtl = !$this->rtl;
  18011. $revshift = ($strlinelen + $wadj + 0.000001); // add little quantity for rounding problems
  18012. if ($this->rtl) {
  18013. $this->x += $revshift;
  18014. } else {
  18015. $this->x -= $revshift;
  18016. }
  18017. $xws = $this->x;
  18018. }
  18019. // ****** write only until the end of the line and get the rest ******
  18020. $strrest = $this->Write($this->lasth, $dom[$key]['value'], '', $wfill, '', false, 0, true, $firstblock, 0, $wadj);
  18021. // restore default direction
  18022. if ($reverse_dir AND ($wadj == 0)) {
  18023. $this->x = $xws; // @phpstan-ignore-line
  18024. $this->rtl = !$this->rtl;
  18025. $reverse_dir = false;
  18026. }
  18027. }
  18028. }
  18029. $this->textindent = 0;
  18030. if (strlen($strrest) > 0) {
  18031. // store the remaining string on the previous $key position
  18032. $this->newline = true;
  18033. if ($strrest == $dom[$key]['value']) {
  18034. // used to avoid infinite loop
  18035. ++$loop;
  18036. } else {
  18037. $loop = 0;
  18038. }
  18039. $dom[$key]['value'] = $strrest;
  18040. if ($cell) {
  18041. if ($this->rtl) {
  18042. $this->x -= $this->cell_padding['R'];
  18043. } else {
  18044. $this->x += $this->cell_padding['L'];
  18045. }
  18046. }
  18047. if ($loop < 3) {
  18048. --$key;
  18049. }
  18050. } else {
  18051. $loop = 0;
  18052. // add the positive font spacing of the last character (if any)
  18053. if ($this->font_spacing > 0) {
  18054. if ($this->rtl) {
  18055. $this->x -= $this->font_spacing;
  18056. } else {
  18057. $this->x += $this->font_spacing;
  18058. }
  18059. }
  18060. }
  18061. }
  18062. ++$key;
  18063. if (isset($dom[$key]['tag']) AND $dom[$key]['tag'] AND (!isset($dom[$key]['opening']) OR !$dom[$key]['opening']) AND isset($dom[($dom[$key]['parent'])]['attribute']['nobr']) AND ($dom[($dom[$key]['parent'])]['attribute']['nobr'] == 'true')) {
  18064. // check if we are on a new page or on a new column
  18065. if ((!$undo) AND (($this->y < $this->start_transaction_y) OR (($dom[$key]['value'] == 'tr') AND ($dom[($dom[$key]['parent'])]['endy'] < $this->start_transaction_y)))) {
  18066. // we are on a new page or on a new column and the total object height is less than the available vertical space.
  18067. // restore previous object
  18068. $this->rollbackTransaction(true);
  18069. // restore previous values
  18070. foreach ($this_method_vars as $vkey => $vval) {
  18071. $$vkey = $vval;
  18072. }
  18073. if (!empty($dom[$key]['thead'])) {
  18074. $this->inthead = true;
  18075. }
  18076. // add a page (or trig AcceptPageBreak() for multicolumn mode)
  18077. $pre_y = $this->y;
  18078. if ((!$this->checkPageBreak($this->PageBreakTrigger + 1)) AND ($this->y < $pre_y)) {
  18079. $startliney = $this->y;
  18080. }
  18081. $undo = true; // avoid infinite loop
  18082. } else {
  18083. $undo = false;
  18084. }
  18085. }
  18086. } // end for each $key
  18087. // align the last line
  18088. if (isset($startlinex)) {
  18089. $yshift = ($minstartliney - $startliney);
  18090. if (($yshift > 0) OR ($this->page > $startlinepage)) {
  18091. $yshift = 0;
  18092. }
  18093. $t_x = 0;
  18094. // the last line must be shifted to be aligned as requested
  18095. $linew = abs($this->endlinex - $startlinex);
  18096. if ($this->inxobj) {
  18097. // we are inside an XObject template
  18098. $pstart = substr($this->xobjects[$this->xobjid]['outdata'], 0, $startlinepos);
  18099. if (isset($opentagpos)) {
  18100. $midpos = $opentagpos;
  18101. } else {
  18102. $midpos = 0;
  18103. }
  18104. if ($midpos > 0) {
  18105. $pmid = substr($this->xobjects[$this->xobjid]['outdata'], $startlinepos, ($midpos - $startlinepos));
  18106. $pend = substr($this->xobjects[$this->xobjid]['outdata'], $midpos);
  18107. } else {
  18108. $pmid = substr($this->xobjects[$this->xobjid]['outdata'], $startlinepos);
  18109. $pend = '';
  18110. }
  18111. } else {
  18112. $pstart = substr($this->getPageBuffer($startlinepage), 0, $startlinepos);
  18113. if (isset($opentagpos) AND isset($this->footerlen[$startlinepage]) AND (!$this->InFooter)) {
  18114. $this->footerpos[$startlinepage] = $this->pagelen[$startlinepage] - $this->footerlen[$startlinepage];
  18115. $midpos = min($opentagpos, $this->footerpos[$startlinepage]);
  18116. } elseif (isset($opentagpos)) {
  18117. $midpos = $opentagpos;
  18118. } elseif (isset($this->footerlen[$startlinepage]) AND (!$this->InFooter)) {
  18119. $this->footerpos[$startlinepage] = $this->pagelen[$startlinepage] - $this->footerlen[$startlinepage];
  18120. $midpos = $this->footerpos[$startlinepage];
  18121. } else {
  18122. $midpos = 0;
  18123. }
  18124. if ($midpos > 0) {
  18125. $pmid = substr($this->getPageBuffer($startlinepage), $startlinepos, ($midpos - $startlinepos));
  18126. $pend = substr($this->getPageBuffer($startlinepage), $midpos);
  18127. } else {
  18128. $pmid = substr($this->getPageBuffer($startlinepage), $startlinepos);
  18129. $pend = '';
  18130. }
  18131. }
  18132. if ((((($plalign == 'C') OR (($plalign == 'R') AND (!$this->rtl)) OR (($plalign == 'L') AND ($this->rtl)))))) {
  18133. // calculate shifting amount
  18134. $tw = $w;
  18135. if ($this->lMargin != $prevlMargin) {
  18136. $tw += ($prevlMargin - $this->lMargin);
  18137. }
  18138. if ($this->rMargin != $prevrMargin) {
  18139. $tw += ($prevrMargin - $this->rMargin);
  18140. }
  18141. $one_space_width = $this->GetStringWidth(chr(32));
  18142. $no = 0; // number of spaces on a line contained on a single block
  18143. if ($this->isRTLTextDir()) { // RTL
  18144. // remove left space if exist
  18145. $pos1 = TCPDF_STATIC::revstrpos($pmid, '[(');
  18146. if ($pos1 > 0) {
  18147. $pos1 = intval($pos1);
  18148. if ($this->isUnicodeFont()) {
  18149. $pos2 = intval(TCPDF_STATIC::revstrpos($pmid, '[('.chr(0).chr(32)));
  18150. $spacelen = 2;
  18151. } else {
  18152. $pos2 = intval(TCPDF_STATIC::revstrpos($pmid, '[('.chr(32)));
  18153. $spacelen = 1;
  18154. }
  18155. if ($pos1 == $pos2) {
  18156. $pmid = substr($pmid, 0, ($pos1 + 2)).substr($pmid, ($pos1 + 2 + $spacelen));
  18157. if (substr($pmid, $pos1, 4) == '[()]') {
  18158. $linew -= $one_space_width;
  18159. } elseif ($pos1 == strpos($pmid, '[(')) {
  18160. $no = 1;
  18161. }
  18162. }
  18163. }
  18164. } else { // LTR
  18165. // remove right space if exist
  18166. $pos1 = TCPDF_STATIC::revstrpos($pmid, ')]');
  18167. if ($pos1 > 0) {
  18168. $pos1 = intval($pos1);
  18169. if ($this->isUnicodeFont()) {
  18170. $pos2 = intval(TCPDF_STATIC::revstrpos($pmid, chr(0).chr(32).')]')) + 2;
  18171. $spacelen = 2;
  18172. } else {
  18173. $pos2 = intval(TCPDF_STATIC::revstrpos($pmid, chr(32).')]')) + 1;
  18174. $spacelen = 1;
  18175. }
  18176. if ($pos1 == $pos2) {
  18177. $pmid = substr($pmid, 0, ($pos1 - $spacelen)).substr($pmid, $pos1);
  18178. $linew -= $one_space_width;
  18179. }
  18180. }
  18181. }
  18182. $mdiff = ($tw - $linew);
  18183. if ($plalign == 'C') {
  18184. if ($this->rtl) {
  18185. $t_x = -($mdiff / 2);
  18186. } else {
  18187. $t_x = ($mdiff / 2);
  18188. }
  18189. } elseif ($plalign == 'R') {
  18190. // right alignment on LTR document
  18191. $t_x = $mdiff;
  18192. } elseif ($plalign == 'L') {
  18193. // left alignment on RTL document
  18194. $t_x = -$mdiff;
  18195. }
  18196. } // end if startlinex
  18197. if (($t_x != 0) OR ($yshift < 0)) {
  18198. // shift the line
  18199. $trx = sprintf('1 0 0 1 %F %F cm', ($t_x * $this->k), ($yshift * $this->k));
  18200. $pstart .= "\nq\n".$trx."\n".$pmid."\nQ\n";
  18201. $endlinepos = strlen($pstart);
  18202. if ($this->inxobj) {
  18203. // we are inside an XObject template
  18204. $this->xobjects[$this->xobjid]['outdata'] = $pstart.$pend;
  18205. foreach ($this->xobjects[$this->xobjid]['annotations'] as $pak => $pac) {
  18206. if ($pak >= $pask) {
  18207. $this->xobjects[$this->xobjid]['annotations'][$pak]['x'] += $t_x;
  18208. $this->xobjects[$this->xobjid]['annotations'][$pak]['y'] -= $yshift;
  18209. }
  18210. }
  18211. } else {
  18212. $this->setPageBuffer($startlinepage, $pstart.$pend);
  18213. // shift the annotations and links
  18214. if (isset($this->PageAnnots[$this->page])) {
  18215. foreach ($this->PageAnnots[$this->page] as $pak => $pac) {
  18216. if ($pak >= $pask) {
  18217. $this->PageAnnots[$this->page][$pak]['x'] += $t_x;
  18218. $this->PageAnnots[$this->page][$pak]['y'] -= $yshift;
  18219. }
  18220. }
  18221. }
  18222. }
  18223. $this->y -= $yshift;
  18224. $yshift = 0;
  18225. }
  18226. }
  18227. // restore previous values
  18228. $this->setGraphicVars($gvars);
  18229. if ($this->num_columns > 1) {
  18230. $this->selectColumn();
  18231. } elseif ($this->page > $prevPage) {
  18232. $this->lMargin = $this->pagedim[$this->page]['olm'];
  18233. $this->rMargin = $this->pagedim[$this->page]['orm'];
  18234. }
  18235. // restore previous list state
  18236. $this->cell_height_ratio = $prev_cell_height_ratio;
  18237. $this->listnum = $prev_listnum;
  18238. $this->listordered = $prev_listordered;
  18239. $this->listcount = $prev_listcount;
  18240. $this->lispacer = $prev_lispacer;
  18241. if ($ln AND (!($cell AND ($dom[$key-1]['value'] == 'table')))) {
  18242. $this->Ln($this->lasth);
  18243. if (($this->y < $maxbottomliney) AND ($startlinepage == $this->page)) {
  18244. $this->y = $maxbottomliney;
  18245. }
  18246. }
  18247. unset($dom);
  18248. }
  18249. /**
  18250. * Process opening tags.
  18251. * @param array $dom html dom array
  18252. * @param int $key current element id
  18253. * @param boolean $cell if true add the default left (or right if RTL) padding to each new line (default false).
  18254. * @return array $dom
  18255. * @protected
  18256. */
  18257. protected function openHTMLTagHandler($dom, $key, $cell) {
  18258. $tag = $dom[$key];
  18259. $parent = $dom[($dom[$key]['parent'])];
  18260. $firsttag = ($key == 1);
  18261. // check for text direction attribute
  18262. if (isset($tag['dir'])) {
  18263. $this->setTempRTL($tag['dir']);
  18264. } else {
  18265. $this->tmprtl = false;
  18266. }
  18267. if ($tag['block']) {
  18268. $hbz = 0; // distance from y to line bottom
  18269. $hb = 0; // vertical space between block tags
  18270. // calculate vertical space for block tags
  18271. if (isset($this->tagvspaces[$tag['value']][0]['h']) && !empty($this->tagvspaces[$tag['value']][0]['h']) && ($this->tagvspaces[$tag['value']][0]['h'] >= 0)) {
  18272. $cur_h = $this->tagvspaces[$tag['value']][0]['h'];
  18273. } elseif (isset($tag['fontsize'])) {
  18274. $cur_h = $this->getCellHeight($tag['fontsize'] / $this->k);
  18275. } else {
  18276. $cur_h = $this->getCellHeight($this->FontSize);
  18277. }
  18278. if (isset($this->tagvspaces[$tag['value']][0]['n'])) {
  18279. $on = $this->tagvspaces[$tag['value']][0]['n'];
  18280. } elseif (preg_match('/[h][0-9]/', $tag['value']) > 0) {
  18281. $on = 0.6;
  18282. } else {
  18283. $on = 1;
  18284. }
  18285. if ((!isset($this->tagvspaces[$tag['value']])) AND (in_array($tag['value'], array('div', 'dt', 'dd', 'li', 'br', 'hr')))) {
  18286. $hb = 0;
  18287. } else {
  18288. $hb = ($on * $cur_h);
  18289. }
  18290. if (($this->htmlvspace <= 0) AND ($on > 0)) {
  18291. if (isset($parent['fontsize'])) {
  18292. $hbz = (($parent['fontsize'] / $this->k) * $this->cell_height_ratio);
  18293. } else {
  18294. $hbz = $this->getCellHeight($this->FontSize);
  18295. }
  18296. }
  18297. if (isset($dom[($key - 1)]) AND ($dom[($key - 1)]['value'] == 'table')) {
  18298. // fix vertical space after table
  18299. $hbz = 0;
  18300. }
  18301. // closing vertical space
  18302. $hbc = 0;
  18303. if (isset($this->tagvspaces[$tag['value']][1]['h']) && !empty($this->tagvspaces[$tag['value']][1]['h']) && ($this->tagvspaces[$tag['value']][1]['h'] >= 0)) {
  18304. $pre_h = $this->tagvspaces[$tag['value']][1]['h'];
  18305. } elseif (isset($parent['fontsize'])) {
  18306. $pre_h = $this->getCellHeight($parent['fontsize'] / $this->k);
  18307. } else {
  18308. $pre_h = $this->getCellHeight($this->FontSize);
  18309. }
  18310. if (isset($this->tagvspaces[$tag['value']][1]['n'])) {
  18311. $cn = $this->tagvspaces[$tag['value']][1]['n'];
  18312. } elseif (preg_match('/[h][0-9]/', $tag['value']) > 0) {
  18313. $cn = 0.6;
  18314. } else {
  18315. $cn = 1;
  18316. }
  18317. if (isset($this->tagvspaces[$tag['value']][1])) {
  18318. $hbc = ($cn * $pre_h);
  18319. }
  18320. }
  18321. // Opening tag
  18322. switch($tag['value']) {
  18323. case 'table': {
  18324. $cp = 0;
  18325. $cs = 0;
  18326. $dom[$key]['rowspans'] = array();
  18327. if (!isset($dom[$key]['attribute']['nested']) OR ($dom[$key]['attribute']['nested'] != 'true')) {
  18328. $this->htmlvspace = 0;
  18329. // set table header
  18330. if (!TCPDF_STATIC::empty_string($dom[$key]['thead'])) {
  18331. // set table header
  18332. $this->thead = $dom[$key]['thead'];
  18333. if (!isset($this->theadMargins) OR (empty($this->theadMargins))) {
  18334. $this->theadMargins = array();
  18335. $this->theadMargins['cell_padding'] = $this->cell_padding;
  18336. $this->theadMargins['lmargin'] = $this->lMargin;
  18337. $this->theadMargins['rmargin'] = $this->rMargin;
  18338. $this->theadMargins['page'] = $this->page;
  18339. $this->theadMargins['cell'] = $cell;
  18340. $this->theadMargins['gvars'] = $this->getGraphicVars();
  18341. }
  18342. }
  18343. }
  18344. // store current margins and page
  18345. $dom[$key]['old_cell_padding'] = $this->cell_padding;
  18346. if (isset($tag['attribute']['cellpadding'])) {
  18347. $pad = $this->getHTMLUnitToUnits($tag['attribute']['cellpadding'], 1, 'px');
  18348. $this->setCellPadding($pad);
  18349. } elseif (isset($tag['padding'])) {
  18350. $this->cell_padding = $tag['padding'];
  18351. }
  18352. if (isset($tag['attribute']['cellspacing'])) {
  18353. $cs = $this->getHTMLUnitToUnits($tag['attribute']['cellspacing'], 1, 'px');
  18354. } elseif (isset($tag['border-spacing'])) {
  18355. $cs = $tag['border-spacing']['V'];
  18356. }
  18357. $prev_y = $this->y;
  18358. if ($this->checkPageBreak(((2 * $cp) + (2 * $cs) + $this->lasth), '', false) OR ($this->y < $prev_y)) {
  18359. $this->inthead = true;
  18360. // add a page (or trig AcceptPageBreak() for multicolumn mode)
  18361. $this->checkPageBreak($this->PageBreakTrigger + 1);
  18362. }
  18363. break;
  18364. }
  18365. case 'tr': {
  18366. // array of columns positions
  18367. $dom[$key]['cellpos'] = array();
  18368. break;
  18369. }
  18370. case 'hr': {
  18371. if ((isset($tag['height'])) AND ($tag['height'] != '')) {
  18372. $hrHeight = $this->getHTMLUnitToUnits($tag['height'], 1, 'px');
  18373. } else {
  18374. $hrHeight = $this->GetLineWidth();
  18375. }
  18376. $this->addHTMLVertSpace($hbz, max($hb, ($hrHeight / 2)), $cell, $firsttag);
  18377. $x = $this->GetX();
  18378. $y = $this->GetY();
  18379. $wtmp = $this->w - $this->lMargin - $this->rMargin;
  18380. if ($cell) {
  18381. $wtmp -= ($this->cell_padding['L'] + $this->cell_padding['R']);
  18382. }
  18383. if ((isset($tag['width'])) AND ($tag['width'] != '')) {
  18384. $hrWidth = $this->getHTMLUnitToUnits($tag['width'], $wtmp, 'px');
  18385. } else {
  18386. $hrWidth = $wtmp;
  18387. }
  18388. $prevlinewidth = $this->GetLineWidth();
  18389. $this->setLineWidth($hrHeight);
  18390. $lineStyle = array();
  18391. if (isset($tag['fgcolor'])) {
  18392. $lineStyle['color'] = $tag['fgcolor'];
  18393. }
  18394. if (isset($tag['fgcolor'])) {
  18395. $lineStyle['color'] = $tag['fgcolor'];
  18396. }
  18397. if (isset($tag['style']['cap'])) {
  18398. $lineStyle['cap'] = $tag['style']['cap'];
  18399. }
  18400. if (isset($tag['style']['join'])) {
  18401. $lineStyle['join'] = $tag['style']['join'];
  18402. }
  18403. if (isset($tag['style']['dash'])) {
  18404. $lineStyle['dash'] = $tag['style']['dash'];
  18405. }
  18406. if (isset($tag['style']['phase'])) {
  18407. $lineStyle['phase'] = $tag['style']['phase'];
  18408. }
  18409. $lineStyle = array_filter($lineStyle);
  18410. $this->Line($x, $y, $x + $hrWidth, $y, $lineStyle);
  18411. $this->setLineWidth($prevlinewidth);
  18412. $this->addHTMLVertSpace(max($hbc, ($hrHeight / 2)), 0, $cell, !isset($dom[($key + 1)]));
  18413. break;
  18414. }
  18415. case 'a': {
  18416. if (array_key_exists('href', $tag['attribute'])) {
  18417. $this->HREF['url'] = $tag['attribute']['href'];
  18418. }
  18419. break;
  18420. }
  18421. case 'img': {
  18422. if (empty($tag['attribute']['src'])) {
  18423. break;
  18424. }
  18425. $imgsrc = $tag['attribute']['src'];
  18426. if ($imgsrc[0] === '@') {
  18427. // data stream
  18428. $imgsrc = '@'.base64_decode(substr($imgsrc, 1));
  18429. $type = preg_match('/<svg\s+[^>]*[^>]*>.*<\/svg>/is', $imgsrc) ? 'svg' : '';
  18430. } else if (preg_match('@^data:image/([^;]*);base64,(.*)@', $imgsrc, $reg)) {
  18431. $imgsrc = '@'.base64_decode($reg[2]);
  18432. $type = $reg[1];
  18433. } elseif (strpos($imgsrc, '../') !== false) {
  18434. // accessing parent folders is not allowed
  18435. break;
  18436. } elseif ( $this->allowLocalFiles && substr($imgsrc, 0, 7) === 'file://') {
  18437. // get image type from a local file path
  18438. $imgsrc = substr($imgsrc, 7);
  18439. $type = TCPDF_IMAGES::getImageFileType($imgsrc);
  18440. } else {
  18441. if (($imgsrc[0] === '/') AND !empty($_SERVER['DOCUMENT_ROOT']) AND ($_SERVER['DOCUMENT_ROOT'] != '/')) {
  18442. // fix image path
  18443. $findroot = strpos($imgsrc, $_SERVER['DOCUMENT_ROOT']);
  18444. if (($findroot === false) OR ($findroot > 1)) {
  18445. if (substr($_SERVER['DOCUMENT_ROOT'], -1) == '/') {
  18446. $imgsrc = substr($_SERVER['DOCUMENT_ROOT'], 0, -1).$imgsrc;
  18447. } else {
  18448. $imgsrc = $_SERVER['DOCUMENT_ROOT'].$imgsrc;
  18449. }
  18450. }
  18451. $imgsrc = urldecode($imgsrc);
  18452. $testscrtype = @parse_url($imgsrc);
  18453. if (empty($testscrtype['query'])) {
  18454. // convert URL to server path
  18455. $imgsrc = str_replace(K_PATH_URL, K_PATH_MAIN, $imgsrc);
  18456. } elseif (preg_match('|^https?://|', $imgsrc) !== 1) {
  18457. // convert URL to server path
  18458. $imgsrc = str_replace(K_PATH_MAIN, K_PATH_URL, $imgsrc);
  18459. }
  18460. }
  18461. // get image type
  18462. $type = TCPDF_IMAGES::getImageFileType($imgsrc);
  18463. }
  18464. if (!isset($tag['width'])) {
  18465. $tag['width'] = 0;
  18466. }
  18467. if (!isset($tag['height'])) {
  18468. $tag['height'] = 0;
  18469. }
  18470. //if (!isset($tag['attribute']['align'])) {
  18471. // the only alignment supported is "bottom"
  18472. // further development is required for other modes.
  18473. $tag['attribute']['align'] = 'bottom';
  18474. //}
  18475. switch($tag['attribute']['align']) {
  18476. case 'top': {
  18477. $align = 'T';
  18478. break;
  18479. }
  18480. case 'middle': {
  18481. $align = 'M';
  18482. break;
  18483. }
  18484. case 'bottom': {
  18485. $align = 'B';
  18486. break;
  18487. }
  18488. default: {
  18489. $align = 'B';
  18490. break;
  18491. }
  18492. }
  18493. $prevy = $this->y;
  18494. $xpos = $this->x;
  18495. $imglink = '';
  18496. if (isset($this->HREF['url']) AND !TCPDF_STATIC::empty_string($this->HREF['url'])) {
  18497. $imglink = $this->HREF['url'];
  18498. if ($imglink[0] == '#' AND is_numeric($imglink[1])) {
  18499. // convert url to internal link
  18500. $lnkdata = explode(',', $imglink);
  18501. if (isset($lnkdata[0])) {
  18502. $page = intval(substr($lnkdata[0], 1));
  18503. if (empty($page) OR ($page <= 0)) {
  18504. $page = $this->page;
  18505. }
  18506. if (isset($lnkdata[1]) AND (strlen($lnkdata[1]) > 0)) {
  18507. $lnky = floatval($lnkdata[1]);
  18508. } else {
  18509. $lnky = 0;
  18510. }
  18511. $imglink = $this->AddLink();
  18512. $this->setLink($imglink, $lnky, $page);
  18513. }
  18514. }
  18515. }
  18516. $border = 0;
  18517. if (isset($tag['border']) AND !empty($tag['border'])) {
  18518. // currently only support 1 (frame) or a combination of 'LTRB'
  18519. $border = $tag['border'];
  18520. }
  18521. $iw = '';
  18522. if (isset($tag['width'])) {
  18523. $iw = $this->getHTMLUnitToUnits($tag['width'], ($tag['fontsize'] / $this->k), 'px', false);
  18524. }
  18525. $ih = '';
  18526. if (isset($tag['height'])) {
  18527. $ih = $this->getHTMLUnitToUnits($tag['height'], ($tag['fontsize'] / $this->k), 'px', false);
  18528. }
  18529. if (($type == 'eps') OR ($type == 'ai')) {
  18530. $this->ImageEps($imgsrc, $xpos, $this->y, $iw, $ih, $imglink, true, $align, '', $border, true);
  18531. } elseif ($type == 'svg') {
  18532. $this->ImageSVG($imgsrc, $xpos, $this->y, $iw, $ih, $imglink, $align, '', $border, true);
  18533. } else {
  18534. $this->Image($imgsrc, $xpos, $this->y, $iw, $ih, '', $imglink, $align, false, 300, '', false, false, $border, false, false, true);
  18535. }
  18536. switch($align) {
  18537. case 'T': {
  18538. $this->y = $prevy;
  18539. break;
  18540. }
  18541. case 'M': {
  18542. $this->y = (($this->img_rb_y + $prevy - ($this->getCellHeight($tag['fontsize'] / $this->k))) / 2);
  18543. break;
  18544. }
  18545. case 'B': {
  18546. $this->y = $this->img_rb_y - ($this->getCellHeight($tag['fontsize'] / $this->k) - ($this->getFontDescent($tag['fontname'], $tag['fontstyle'], $tag['fontsize']) * $this->cell_height_ratio));
  18547. break;
  18548. }
  18549. }
  18550. break;
  18551. }
  18552. case 'dl': {
  18553. ++$this->listnum;
  18554. if ($this->listnum == 1) {
  18555. $this->addHTMLVertSpace($hbz, $hb, $cell, $firsttag);
  18556. } else {
  18557. $this->addHTMLVertSpace(0, 0, $cell, $firsttag);
  18558. }
  18559. break;
  18560. }
  18561. case 'dt': {
  18562. $this->addHTMLVertSpace($hbz, $hb, $cell, $firsttag);
  18563. break;
  18564. }
  18565. case 'dd': {
  18566. if ($this->rtl) {
  18567. $this->rMargin += $this->listindent;
  18568. } else {
  18569. $this->lMargin += $this->listindent;
  18570. }
  18571. ++$this->listindentlevel;
  18572. $this->addHTMLVertSpace($hbz, $hb, $cell, $firsttag);
  18573. break;
  18574. }
  18575. case 'ul':
  18576. case 'ol': {
  18577. ++$this->listnum;
  18578. if ($tag['value'] == 'ol') {
  18579. $this->listordered[$this->listnum] = true;
  18580. } else {
  18581. $this->listordered[$this->listnum] = false;
  18582. }
  18583. if (isset($tag['attribute']['start'])) {
  18584. $this->listcount[$this->listnum] = intval($tag['attribute']['start']) - 1;
  18585. } else {
  18586. $this->listcount[$this->listnum] = 0;
  18587. }
  18588. if ($this->rtl) {
  18589. $this->rMargin += $this->listindent;
  18590. $this->x -= $this->listindent;
  18591. } else {
  18592. $this->lMargin += $this->listindent;
  18593. $this->x += $this->listindent;
  18594. }
  18595. ++$this->listindentlevel;
  18596. if ($this->listnum == 1) {
  18597. if ($key > 1) {
  18598. $this->addHTMLVertSpace($hbz, $hb, $cell, $firsttag);
  18599. }
  18600. } else {
  18601. $this->addHTMLVertSpace(0, 0, $cell, $firsttag);
  18602. }
  18603. break;
  18604. }
  18605. case 'li': {
  18606. if ($key > 2) {
  18607. $this->addHTMLVertSpace($hbz, $hb, $cell, $firsttag);
  18608. }
  18609. if ($this->listordered[$this->listnum]) {
  18610. // ordered item
  18611. if (isset($parent['attribute']['type']) AND !TCPDF_STATIC::empty_string($parent['attribute']['type'])) {
  18612. $this->lispacer = $parent['attribute']['type'];
  18613. } elseif (isset($parent['listtype']) AND !TCPDF_STATIC::empty_string($parent['listtype'])) {
  18614. $this->lispacer = $parent['listtype'];
  18615. } elseif (isset($this->lisymbol) AND !TCPDF_STATIC::empty_string($this->lisymbol)) {
  18616. $this->lispacer = $this->lisymbol;
  18617. } else {
  18618. $this->lispacer = '#';
  18619. }
  18620. ++$this->listcount[$this->listnum];
  18621. if (isset($tag['attribute']['value'])) {
  18622. $this->listcount[$this->listnum] = intval($tag['attribute']['value']);
  18623. }
  18624. } else {
  18625. // unordered item
  18626. if (isset($parent['attribute']['type']) AND !TCPDF_STATIC::empty_string($parent['attribute']['type'])) {
  18627. $this->lispacer = $parent['attribute']['type'];
  18628. } elseif (isset($parent['listtype']) AND !TCPDF_STATIC::empty_string($parent['listtype'])) {
  18629. $this->lispacer = $parent['listtype'];
  18630. } elseif (isset($this->lisymbol) AND !TCPDF_STATIC::empty_string($this->lisymbol)) {
  18631. $this->lispacer = $this->lisymbol;
  18632. } else {
  18633. $this->lispacer = '!';
  18634. }
  18635. }
  18636. break;
  18637. }
  18638. case 'blockquote': {
  18639. if ($this->rtl) {
  18640. $this->rMargin += $this->listindent;
  18641. } else {
  18642. $this->lMargin += $this->listindent;
  18643. }
  18644. ++$this->listindentlevel;
  18645. $this->addHTMLVertSpace($hbz, $hb, $cell, $firsttag);
  18646. break;
  18647. }
  18648. case 'br': {
  18649. $this->addHTMLVertSpace($hbz, $hb, $cell, $firsttag);
  18650. break;
  18651. }
  18652. case 'div': {
  18653. $this->addHTMLVertSpace($hbz, $hb, $cell, $firsttag);
  18654. break;
  18655. }
  18656. case 'p': {
  18657. $this->addHTMLVertSpace($hbz, $hb, $cell, $firsttag);
  18658. break;
  18659. }
  18660. case 'pre': {
  18661. $this->addHTMLVertSpace($hbz, $hb, $cell, $firsttag);
  18662. $this->premode = true;
  18663. break;
  18664. }
  18665. case 'sup': {
  18666. $this->setXY($this->GetX(), $this->GetY() - ((0.7 * $this->FontSizePt) / $this->k));
  18667. break;
  18668. }
  18669. case 'sub': {
  18670. $this->setXY($this->GetX(), $this->GetY() + ((0.3 * $this->FontSizePt) / $this->k));
  18671. break;
  18672. }
  18673. case 'h1':
  18674. case 'h2':
  18675. case 'h3':
  18676. case 'h4':
  18677. case 'h5':
  18678. case 'h6': {
  18679. $this->addHTMLVertSpace($hbz, $hb, $cell, $firsttag);
  18680. break;
  18681. }
  18682. // Form fields (since 4.8.000 - 2009-09-07)
  18683. case 'form': {
  18684. if (isset($tag['attribute']['action'])) {
  18685. $this->form_action = $tag['attribute']['action'];
  18686. } else {
  18687. $this->Error('Please explicitly set action attribute path!');
  18688. }
  18689. if (isset($tag['attribute']['enctype'])) {
  18690. $this->form_enctype = $tag['attribute']['enctype'];
  18691. } else {
  18692. $this->form_enctype = 'application/x-www-form-urlencoded';
  18693. }
  18694. if (isset($tag['attribute']['method'])) {
  18695. $this->form_mode = $tag['attribute']['method'];
  18696. } else {
  18697. $this->form_mode = 'post';
  18698. }
  18699. break;
  18700. }
  18701. case 'input': {
  18702. if (isset($tag['attribute']['name']) AND !TCPDF_STATIC::empty_string($tag['attribute']['name'])) {
  18703. $name = $tag['attribute']['name'];
  18704. } else {
  18705. break;
  18706. }
  18707. $prop = array();
  18708. $opt = array();
  18709. if (isset($tag['attribute']['readonly']) AND !TCPDF_STATIC::empty_string($tag['attribute']['readonly'])) {
  18710. $prop['readonly'] = true;
  18711. }
  18712. if (isset($tag['attribute']['value']) AND !TCPDF_STATIC::empty_string($tag['attribute']['value'])) {
  18713. $value = $tag['attribute']['value'];
  18714. }
  18715. if (isset($tag['attribute']['maxlength']) AND !TCPDF_STATIC::empty_string($tag['attribute']['maxlength'])) {
  18716. $opt['maxlen'] = intval($tag['attribute']['maxlength']);
  18717. }
  18718. $h = $this->getCellHeight($this->FontSize);
  18719. if (isset($tag['attribute']['size']) AND !TCPDF_STATIC::empty_string($tag['attribute']['size'])) {
  18720. $w = intval($tag['attribute']['size']) * $this->GetStringWidth(chr(32)) * 2;
  18721. } else {
  18722. $w = $h;
  18723. }
  18724. if (isset($tag['attribute']['checked']) AND (($tag['attribute']['checked'] == 'checked') OR ($tag['attribute']['checked'] == 'true'))) {
  18725. $checked = true;
  18726. } else {
  18727. $checked = false;
  18728. }
  18729. if (isset($tag['align'])) {
  18730. switch ($tag['align']) {
  18731. case 'C': {
  18732. $opt['q'] = 1;
  18733. break;
  18734. }
  18735. case 'R': {
  18736. $opt['q'] = 2;
  18737. break;
  18738. }
  18739. case 'L':
  18740. default: {
  18741. break;
  18742. }
  18743. }
  18744. }
  18745. switch ($tag['attribute']['type']) {
  18746. case 'text': {
  18747. if (isset($value)) {
  18748. $opt['v'] = $value;
  18749. }
  18750. $this->TextField($name, $w, $h, $prop, $opt, '', '', false);
  18751. break;
  18752. }
  18753. case 'password': {
  18754. if (isset($value)) {
  18755. $opt['v'] = $value;
  18756. }
  18757. $prop['password'] = 'true';
  18758. $this->TextField($name, $w, $h, $prop, $opt, '', '', false);
  18759. break;
  18760. }
  18761. case 'checkbox': {
  18762. if (!isset($value)) {
  18763. break;
  18764. }
  18765. $this->CheckBox($name, $w, $checked, $prop, $opt, $value, '', '', false);
  18766. break;
  18767. }
  18768. case 'radio': {
  18769. if (!isset($value)) {
  18770. break;
  18771. }
  18772. $this->RadioButton($name, $w, $prop, $opt, $value, $checked, '', '', false);
  18773. break;
  18774. }
  18775. case 'submit': {
  18776. if (!isset($value)) {
  18777. $value = 'submit';
  18778. }
  18779. $w = $this->GetStringWidth($value) * 1.5;
  18780. $h *= 1.6;
  18781. $prop = array('lineWidth'=>1, 'borderStyle'=>'beveled', 'fillColor'=>array(196, 196, 196), 'strokeColor'=>array(255, 255, 255));
  18782. $action = array();
  18783. $action['S'] = 'SubmitForm';
  18784. $action['F'] = $this->form_action;
  18785. if ($this->form_enctype != 'FDF') {
  18786. $action['Flags'] = array('ExportFormat');
  18787. }
  18788. if ($this->form_mode == 'get') {
  18789. $action['Flags'] = array('GetMethod');
  18790. }
  18791. $this->Button($name, $w, $h, $value, $action, $prop, $opt, '', '', false);
  18792. break;
  18793. }
  18794. case 'reset': {
  18795. if (!isset($value)) {
  18796. $value = 'reset';
  18797. }
  18798. $w = $this->GetStringWidth($value) * 1.5;
  18799. $h *= 1.6;
  18800. $prop = array('lineWidth'=>1, 'borderStyle'=>'beveled', 'fillColor'=>array(196, 196, 196), 'strokeColor'=>array(255, 255, 255));
  18801. $this->Button($name, $w, $h, $value, array('S'=>'ResetForm'), $prop, $opt, '', '', false);
  18802. break;
  18803. }
  18804. case 'file': {
  18805. $prop['fileSelect'] = 'true';
  18806. $this->TextField($name, $w, $h, $prop, $opt, '', '', false);
  18807. if (!isset($value)) {
  18808. $value = '*';
  18809. }
  18810. $w = $this->GetStringWidth($value) * 2;
  18811. $h *= 1.2;
  18812. $prop = array('lineWidth'=>1, 'borderStyle'=>'beveled', 'fillColor'=>array(196, 196, 196), 'strokeColor'=>array(255, 255, 255));
  18813. $jsaction = 'var f=this.getField(\''.$name.'\'); f.browseForFileToSubmit();';
  18814. $this->Button('FB_'.$name, $w, $h, $value, $jsaction, $prop, $opt, '', '', false);
  18815. break;
  18816. }
  18817. case 'hidden': {
  18818. if (isset($value)) {
  18819. $opt['v'] = $value;
  18820. }
  18821. $opt['f'] = array('invisible', 'hidden');
  18822. $this->TextField($name, 0, 0, $prop, $opt, '', '', false);
  18823. break;
  18824. }
  18825. case 'image': {
  18826. // THIS TYPE MUST BE FIXED
  18827. if (isset($tag['attribute']['src']) AND !TCPDF_STATIC::empty_string($tag['attribute']['src'])) {
  18828. $img = $tag['attribute']['src'];
  18829. } else {
  18830. break;
  18831. }
  18832. $value = 'img';
  18833. //$opt['mk'] = array('i'=>$img, 'tp'=>1, 'if'=>array('sw'=>'A', 's'=>'A', 'fb'=>false));
  18834. if (isset($tag['attribute']['onclick']) AND !empty($tag['attribute']['onclick'])) {
  18835. $jsaction = $tag['attribute']['onclick'];
  18836. } else {
  18837. $jsaction = '';
  18838. }
  18839. $this->Button($name, $w, $h, $value, $jsaction, $prop, $opt, '', '', false);
  18840. break;
  18841. }
  18842. case 'button': {
  18843. if (!isset($value)) {
  18844. $value = ' ';
  18845. }
  18846. $w = $this->GetStringWidth($value) * 1.5;
  18847. $h *= 1.6;
  18848. $prop = array('lineWidth'=>1, 'borderStyle'=>'beveled', 'fillColor'=>array(196, 196, 196), 'strokeColor'=>array(255, 255, 255));
  18849. if (isset($tag['attribute']['onclick']) AND !empty($tag['attribute']['onclick'])) {
  18850. $jsaction = $tag['attribute']['onclick'];
  18851. } else {
  18852. $jsaction = '';
  18853. }
  18854. $this->Button($name, $w, $h, $value, $jsaction, $prop, $opt, '', '', false);
  18855. break;
  18856. }
  18857. }
  18858. break;
  18859. }
  18860. case 'textarea': {
  18861. $prop = array();
  18862. $opt = array();
  18863. if (isset($tag['attribute']['readonly']) AND !TCPDF_STATIC::empty_string($tag['attribute']['readonly'])) {
  18864. $prop['readonly'] = true;
  18865. }
  18866. if (isset($tag['attribute']['name']) AND !TCPDF_STATIC::empty_string($tag['attribute']['name'])) {
  18867. $name = $tag['attribute']['name'];
  18868. } else {
  18869. break;
  18870. }
  18871. if (isset($tag['attribute']['value']) AND !TCPDF_STATIC::empty_string($tag['attribute']['value'])) {
  18872. $opt['v'] = $tag['attribute']['value'];
  18873. }
  18874. if (isset($tag['attribute']['cols']) AND !TCPDF_STATIC::empty_string($tag['attribute']['cols'])) {
  18875. $w = intval($tag['attribute']['cols']) * $this->GetStringWidth(chr(32)) * 2;
  18876. } else {
  18877. $w = 40;
  18878. }
  18879. if (isset($tag['attribute']['rows']) AND !TCPDF_STATIC::empty_string($tag['attribute']['rows'])) {
  18880. $h = intval($tag['attribute']['rows']) * $this->getCellHeight($this->FontSize);
  18881. } else {
  18882. $h = 10;
  18883. }
  18884. $prop['multiline'] = 'true';
  18885. $this->TextField($name, $w, $h, $prop, $opt, '', '', false);
  18886. break;
  18887. }
  18888. case 'select': {
  18889. $h = $this->getCellHeight($this->FontSize);
  18890. if (isset($tag['attribute']['size']) AND !TCPDF_STATIC::empty_string($tag['attribute']['size'])) {
  18891. $h *= ($tag['attribute']['size'] + 1);
  18892. }
  18893. $prop = array();
  18894. $opt = array();
  18895. if (isset($tag['attribute']['name']) AND !TCPDF_STATIC::empty_string($tag['attribute']['name'])) {
  18896. $name = $tag['attribute']['name'];
  18897. } else {
  18898. break;
  18899. }
  18900. $w = 0;
  18901. if (isset($tag['attribute']['opt']) AND !TCPDF_STATIC::empty_string($tag['attribute']['opt'])) {
  18902. $options = explode('#!NwL!#', $tag['attribute']['opt']);
  18903. $values = array();
  18904. foreach ($options as $val) {
  18905. if (strpos($val, '#!TaB!#') !== false) {
  18906. $opts = explode('#!TaB!#', $val);
  18907. $values[] = $opts;
  18908. $w = max($w, $this->GetStringWidth($opts[1]));
  18909. } else {
  18910. $values[] = $val;
  18911. $w = max($w, $this->GetStringWidth($val));
  18912. }
  18913. }
  18914. } else {
  18915. break;
  18916. }
  18917. $w *= 2;
  18918. if (isset($tag['attribute']['multiple']) AND ($tag['attribute']['multiple']='multiple')) {
  18919. $prop['multipleSelection'] = 'true';
  18920. $this->ListBox($name, $w, $h, $values, $prop, $opt, '', '', false);
  18921. } else {
  18922. $this->ComboBox($name, $w, $h, $values, $prop, $opt, '', '', false);
  18923. }
  18924. break;
  18925. }
  18926. case 'tcpdf': {
  18927. if (defined('K_TCPDF_CALLS_IN_HTML') AND (K_TCPDF_CALLS_IN_HTML === true)) {
  18928. // Special tag used to call TCPDF methods
  18929. // This tag is disabled by default by the K_TCPDF_CALLS_IN_HTML constant on TCPDF configuration file.
  18930. // Please use this feature only if you are in control of the HTML content and you are sure that it does not contain any harmful code.
  18931. if (!empty($tag['attribute']['data'])) {
  18932. $tcpdf_tag_data = $this->unserializeTCPDFtag($tag['attribute']['data']);
  18933. if ($this->allowedTCPDFtag($tcpdf_tag_data['m'])) {
  18934. call_user_func_array(array($this, $tcpdf_tag_data['m']), $tcpdf_tag_data['p']);
  18935. }
  18936. $this->newline = true;
  18937. }
  18938. }
  18939. break;
  18940. }
  18941. default: {
  18942. break;
  18943. }
  18944. }
  18945. // define tags that support borders and background colors
  18946. $bordertags = array('blockquote','br','dd','dl','div','dt','h1','h2','h3','h4','h5','h6','hr','li','ol','p','pre','ul','tcpdf','table');
  18947. if (in_array($tag['value'], $bordertags)) {
  18948. // set border
  18949. $dom[$key]['borderposition'] = $this->getBorderStartPosition();
  18950. }
  18951. if ($dom[$key]['self'] AND isset($dom[$key]['attribute']['pagebreakafter'])) {
  18952. $pba = $dom[$key]['attribute']['pagebreakafter'];
  18953. // check for pagebreak
  18954. if (($pba == 'true') OR ($pba == 'left') OR ($pba == 'right')) {
  18955. // add a page (or trig AcceptPageBreak() for multicolumn mode)
  18956. $this->checkPageBreak($this->PageBreakTrigger + 1);
  18957. }
  18958. if ((($pba == 'left') AND (((!$this->rtl) AND (($this->page % 2) == 0)) OR (($this->rtl) AND (($this->page % 2) != 0))))
  18959. OR (($pba == 'right') AND (((!$this->rtl) AND (($this->page % 2) != 0)) OR (($this->rtl) AND (($this->page % 2) == 0))))) {
  18960. // add a page (or trig AcceptPageBreak() for multicolumn mode)
  18961. $this->checkPageBreak($this->PageBreakTrigger + 1);
  18962. }
  18963. }
  18964. return $dom;
  18965. }
  18966. /**
  18967. * Process closing tags.
  18968. * @param array $dom html dom array
  18969. * @param int $key current element id
  18970. * @param boolean $cell if true add the default left (or right if RTL) padding to each new line (default false).
  18971. * @param int $maxbottomliney maximum y value of current line
  18972. * @return array $dom
  18973. * @protected
  18974. */
  18975. protected function closeHTMLTagHandler($dom, $key, $cell, $maxbottomliney=0) {
  18976. $tag = $dom[$key];
  18977. $parent = $dom[($dom[$key]['parent'])];
  18978. $lasttag = ((!isset($dom[($key + 1)])) OR ((!isset($dom[($key + 2)])) AND ($dom[($key + 1)]['value'] == 'marker')));
  18979. $in_table_head = false;
  18980. // maximum x position (used to draw borders)
  18981. if ($this->rtl) {
  18982. $xmax = $this->w;
  18983. } else {
  18984. $xmax = 0;
  18985. }
  18986. if ($tag['block']) {
  18987. $hbz = 0; // distance from y to line bottom
  18988. $hb = 0; // vertical space between block tags
  18989. // calculate vertical space for block tags
  18990. if (isset($this->tagvspaces[$tag['value']][1]['h']) && !empty($this->tagvspaces[$tag['value']][1]['h']) && ($this->tagvspaces[$tag['value']][1]['h'] >= 0)) {
  18991. $pre_h = $this->tagvspaces[$tag['value']][1]['h'];
  18992. } elseif (isset($parent['fontsize'])) {
  18993. $pre_h = $this->getCellHeight($parent['fontsize'] / $this->k);
  18994. } else {
  18995. $pre_h = $this->getCellHeight($this->FontSize);
  18996. }
  18997. if (isset($this->tagvspaces[$tag['value']][1]['n'])) {
  18998. $cn = $this->tagvspaces[$tag['value']][1]['n'];
  18999. } elseif (preg_match('/[h][0-9]/', $tag['value']) > 0) {
  19000. $cn = 0.6;
  19001. } else {
  19002. $cn = 1;
  19003. }
  19004. if ((!isset($this->tagvspaces[$tag['value']])) AND ($tag['value'] == 'div')) {
  19005. $hb = 0;
  19006. } else {
  19007. $hb = ($cn * $pre_h);
  19008. }
  19009. if ($maxbottomliney > $this->PageBreakTrigger) {
  19010. $hbz = $this->getCellHeight($this->FontSize);
  19011. } elseif ($this->y < $maxbottomliney) {
  19012. $hbz = ($maxbottomliney - $this->y);
  19013. }
  19014. }
  19015. // Closing tag
  19016. switch($tag['value']) {
  19017. case 'tr': {
  19018. $table_el = $dom[($dom[$key]['parent'])]['parent'];
  19019. if (!isset($parent['endy'])) {
  19020. $dom[($dom[$key]['parent'])]['endy'] = $this->y;
  19021. $parent['endy'] = $this->y;
  19022. }
  19023. if (!isset($parent['endpage'])) {
  19024. $dom[($dom[$key]['parent'])]['endpage'] = $this->page;
  19025. $parent['endpage'] = $this->page;
  19026. }
  19027. if (!isset($parent['endcolumn'])) {
  19028. $dom[($dom[$key]['parent'])]['endcolumn'] = $this->current_column;
  19029. $parent['endcolumn'] = $this->current_column;
  19030. }
  19031. // update row-spanned cells
  19032. if (isset($dom[$table_el]['rowspans'])) {
  19033. foreach ($dom[$table_el]['rowspans'] as $k => $trwsp) {
  19034. $dom[$table_el]['rowspans'][$k]['rowspan'] -= 1;
  19035. if ($dom[$table_el]['rowspans'][$k]['rowspan'] == 0) {
  19036. if (($dom[$table_el]['rowspans'][$k]['endpage'] == $parent['endpage']) AND ($dom[$table_el]['rowspans'][$k]['endcolumn'] == $parent['endcolumn'])) {
  19037. $dom[($dom[$key]['parent'])]['endy'] = max($dom[$table_el]['rowspans'][$k]['endy'], $parent['endy']);
  19038. } elseif (($dom[$table_el]['rowspans'][$k]['endpage'] > $parent['endpage']) OR ($dom[$table_el]['rowspans'][$k]['endcolumn'] > $parent['endcolumn'])) {
  19039. $dom[($dom[$key]['parent'])]['endy'] = $dom[$table_el]['rowspans'][$k]['endy'];
  19040. $dom[($dom[$key]['parent'])]['endpage'] = $dom[$table_el]['rowspans'][$k]['endpage'];
  19041. $dom[($dom[$key]['parent'])]['endcolumn'] = $dom[$table_el]['rowspans'][$k]['endcolumn'];
  19042. }
  19043. }
  19044. }
  19045. // report new endy and endpage to the rowspanned cells
  19046. foreach ($dom[$table_el]['rowspans'] as $k => $trwsp) {
  19047. if ($dom[$table_el]['rowspans'][$k]['rowspan'] == 0) {
  19048. $dom[$table_el]['rowspans'][$k]['endpage'] = max($dom[$table_el]['rowspans'][$k]['endpage'], $dom[($dom[$key]['parent'])]['endpage']);
  19049. $dom[($dom[$key]['parent'])]['endpage'] = $dom[$table_el]['rowspans'][$k]['endpage'];
  19050. $dom[$table_el]['rowspans'][$k]['endcolumn'] = max($dom[$table_el]['rowspans'][$k]['endcolumn'], $dom[($dom[$key]['parent'])]['endcolumn']);
  19051. $dom[($dom[$key]['parent'])]['endcolumn'] = $dom[$table_el]['rowspans'][$k]['endcolumn'];
  19052. $dom[$table_el]['rowspans'][$k]['endy'] = max($dom[$table_el]['rowspans'][$k]['endy'], $dom[($dom[$key]['parent'])]['endy']);
  19053. $dom[($dom[$key]['parent'])]['endy'] = $dom[$table_el]['rowspans'][$k]['endy'];
  19054. }
  19055. }
  19056. // update remaining rowspanned cells
  19057. foreach ($dom[$table_el]['rowspans'] as $k => $trwsp) {
  19058. if ($dom[$table_el]['rowspans'][$k]['rowspan'] == 0) {
  19059. $dom[$table_el]['rowspans'][$k]['endpage'] = $dom[($dom[$key]['parent'])]['endpage'];
  19060. $dom[$table_el]['rowspans'][$k]['endcolumn'] = $dom[($dom[$key]['parent'])]['endcolumn'];
  19061. $dom[$table_el]['rowspans'][$k]['endy'] = $dom[($dom[$key]['parent'])]['endy'];
  19062. }
  19063. }
  19064. }
  19065. $prev_page = $this->page;
  19066. $this->setPage($dom[($dom[$key]['parent'])]['endpage']);
  19067. if ($this->num_columns > 1) {
  19068. if (($prev_page < $this->page)
  19069. AND ((($this->current_column == 0) AND ($dom[($dom[$key]['parent'])]['endcolumn'] == ($this->num_columns - 1)))
  19070. OR ($this->current_column == $dom[($dom[$key]['parent'])]['endcolumn']))) {
  19071. // page jump
  19072. $this->selectColumn(0);
  19073. $dom[($dom[$key]['parent'])]['endcolumn'] = 0;
  19074. $dom[($dom[$key]['parent'])]['endy'] = $this->y;
  19075. } else {
  19076. $this->selectColumn($dom[($dom[$key]['parent'])]['endcolumn']);
  19077. $this->y = $dom[($dom[$key]['parent'])]['endy'];
  19078. }
  19079. } else {
  19080. $this->y = $dom[($dom[$key]['parent'])]['endy'];
  19081. }
  19082. if (isset($dom[$table_el]['attribute']['cellspacing'])) {
  19083. $this->y += $this->getHTMLUnitToUnits($dom[$table_el]['attribute']['cellspacing'], 1, 'px');
  19084. } elseif (isset($dom[$table_el]['border-spacing'])) {
  19085. $this->y += $dom[$table_el]['border-spacing']['V'];
  19086. }
  19087. $this->Ln(0, $cell);
  19088. if ($this->current_column == $parent['startcolumn']) {
  19089. $this->x = $parent['startx'];
  19090. }
  19091. // account for booklet mode
  19092. if ($this->page > $parent['startpage']) {
  19093. if (($this->rtl) AND ($this->pagedim[$this->page]['orm'] != $this->pagedim[$parent['startpage']]['orm'])) {
  19094. $this->x -= ($this->pagedim[$this->page]['orm'] - $this->pagedim[$parent['startpage']]['orm']);
  19095. } elseif ((!$this->rtl) AND ($this->pagedim[$this->page]['olm'] != $this->pagedim[$parent['startpage']]['olm'])) {
  19096. $this->x += ($this->pagedim[$this->page]['olm'] - $this->pagedim[$parent['startpage']]['olm']);
  19097. }
  19098. }
  19099. break;
  19100. }
  19101. case 'tablehead':
  19102. // closing tag used for the thead part
  19103. $in_table_head = true;
  19104. $this->inthead = false;
  19105. case 'table': {
  19106. $table_el = $parent;
  19107. // set default border
  19108. if (isset($table_el['attribute']['border']) AND ($table_el['attribute']['border'] > 0)) {
  19109. // set default border
  19110. $border = array('LTRB' => array('width' => $this->getCSSBorderWidth($table_el['attribute']['border']), 'cap'=>'square', 'join'=>'miter', 'dash'=> 0, 'color'=>array(0,0,0)));
  19111. } else {
  19112. $border = 0;
  19113. }
  19114. $default_border = $border;
  19115. // fix bottom line alignment of last line before page break
  19116. foreach ($dom[($dom[$key]['parent'])]['trids'] as $j => $trkey) {
  19117. // update row-spanned cells
  19118. if (isset($dom[($dom[$key]['parent'])]['rowspans'])) {
  19119. foreach ($dom[($dom[$key]['parent'])]['rowspans'] as $k => $trwsp) {
  19120. if (isset($prevtrkey) AND ($trwsp['trid'] == $prevtrkey) AND ($trwsp['mrowspan'] > 0)) {
  19121. $dom[($dom[$key]['parent'])]['rowspans'][$k]['trid'] = $trkey;
  19122. }
  19123. if ($dom[($dom[$key]['parent'])]['rowspans'][$k]['trid'] == $trkey) {
  19124. $dom[($dom[$key]['parent'])]['rowspans'][$k]['mrowspan'] -= 1;
  19125. }
  19126. }
  19127. }
  19128. if (isset($prevtrkey) AND ($dom[$trkey]['startpage'] > $dom[$prevtrkey]['endpage'])) {
  19129. $pgendy = $this->pagedim[$dom[$prevtrkey]['endpage']]['hk'] - $this->pagedim[$dom[$prevtrkey]['endpage']]['bm'];
  19130. $dom[$prevtrkey]['endy'] = $pgendy;
  19131. // update row-spanned cells
  19132. if (isset($dom[($dom[$key]['parent'])]['rowspans'])) {
  19133. foreach ($dom[($dom[$key]['parent'])]['rowspans'] as $k => $trwsp) {
  19134. if (($trwsp['trid'] == $prevtrkey) AND ($trwsp['mrowspan'] >= 0) AND ($trwsp['endpage'] == $dom[$prevtrkey]['endpage'])) {
  19135. $dom[($dom[$key]['parent'])]['rowspans'][$k]['endy'] = $pgendy;
  19136. $dom[($dom[$key]['parent'])]['rowspans'][$k]['mrowspan'] = -1;
  19137. }
  19138. }
  19139. }
  19140. }
  19141. $prevtrkey = $trkey;
  19142. $table_el = $dom[($dom[$key]['parent'])];
  19143. }
  19144. // for each row
  19145. if (!empty($table_el['trids'])) {
  19146. unset($xmax);
  19147. }
  19148. foreach ($table_el['trids'] as $j => $trkey) {
  19149. $parent = $dom[$trkey];
  19150. if (!isset($xmax)) {
  19151. $xmax = $parent['cellpos'][(count($parent['cellpos']) - 1)]['endx'];
  19152. }
  19153. // for each cell on the row
  19154. foreach ($parent['cellpos'] as $k => $cellpos) {
  19155. if (isset($cellpos['rowspanid']) AND ($cellpos['rowspanid'] >= 0)) {
  19156. $cellpos['startx'] = $table_el['rowspans'][($cellpos['rowspanid'])]['startx'];
  19157. $cellpos['endx'] = $table_el['rowspans'][($cellpos['rowspanid'])]['endx'];
  19158. $endy = $table_el['rowspans'][($cellpos['rowspanid'])]['endy'];
  19159. $startpage = $table_el['rowspans'][($cellpos['rowspanid'])]['startpage'];
  19160. $endpage = $table_el['rowspans'][($cellpos['rowspanid'])]['endpage'];
  19161. $startcolumn = $table_el['rowspans'][($cellpos['rowspanid'])]['startcolumn'];
  19162. $endcolumn = $table_el['rowspans'][($cellpos['rowspanid'])]['endcolumn'];
  19163. } else {
  19164. $endy = $parent['endy'];
  19165. $startpage = $parent['startpage'];
  19166. $endpage = $parent['endpage'];
  19167. $startcolumn = $parent['startcolumn'];
  19168. $endcolumn = $parent['endcolumn'];
  19169. }
  19170. if ($this->num_columns == 0) {
  19171. $this->num_columns = 1;
  19172. }
  19173. if (isset($cellpos['border'])) {
  19174. $border = $cellpos['border'];
  19175. }
  19176. if (isset($cellpos['bgcolor']) AND ($cellpos['bgcolor']) !== false) {
  19177. $this->setFillColorArray($cellpos['bgcolor']);
  19178. $fill = true;
  19179. } else {
  19180. $fill = false;
  19181. }
  19182. $x = $cellpos['startx'];
  19183. $y = $parent['starty'];
  19184. $starty = $y;
  19185. $w = abs($cellpos['endx'] - $cellpos['startx']);
  19186. // get border modes
  19187. $border_start = TCPDF_STATIC::getBorderMode($border, $position='start', $this->opencell);
  19188. $border_end = TCPDF_STATIC::getBorderMode($border, $position='end', $this->opencell);
  19189. $border_middle = TCPDF_STATIC::getBorderMode($border, $position='middle', $this->opencell);
  19190. // design borders around HTML cells.
  19191. for ($page = $startpage; $page <= $endpage; ++$page) { // for each page
  19192. $ccode = '';
  19193. $this->setPage($page);
  19194. if ($this->num_columns < 2) {
  19195. // single-column mode
  19196. $this->x = $x;
  19197. $this->y = $this->tMargin;
  19198. }
  19199. // account for margin changes
  19200. if ($page > $startpage) {
  19201. if (($this->rtl) AND ($this->pagedim[$page]['orm'] != $this->pagedim[$startpage]['orm'])) {
  19202. $this->x -= ($this->pagedim[$page]['orm'] - $this->pagedim[$startpage]['orm']);
  19203. } elseif ((!$this->rtl) AND ($this->pagedim[$page]['olm'] != $this->pagedim[$startpage]['olm'])) {
  19204. $this->x += ($this->pagedim[$page]['olm'] - $this->pagedim[$startpage]['olm']);
  19205. }
  19206. }
  19207. if ($startpage == $endpage) { // single page
  19208. $deltacol = 0;
  19209. $deltath = 0;
  19210. for ($column = $startcolumn; $column <= $endcolumn; ++$column) { // for each column
  19211. $this->selectColumn($column);
  19212. if ($startcolumn == $endcolumn) { // single column
  19213. $cborder = $border;
  19214. $h = $endy - $parent['starty'];
  19215. $this->y = $y;
  19216. $this->x = $x;
  19217. } elseif ($column == $startcolumn) { // first column
  19218. $cborder = $border_start;
  19219. $this->y = $starty;
  19220. $this->x = $x;
  19221. $h = $this->h - $this->y - $this->bMargin;
  19222. if ($this->rtl) {
  19223. $deltacol = $this->x + $this->rMargin - $this->w;
  19224. } else {
  19225. $deltacol = $this->x - $this->lMargin;
  19226. }
  19227. } elseif ($column == $endcolumn) { // end column
  19228. $cborder = $border_end;
  19229. if (isset($this->columns[$column]['th']['\''.$page.'\''])) {
  19230. $this->y = $this->columns[$column]['th']['\''.$page.'\''];
  19231. }
  19232. $this->x += $deltacol;
  19233. $h = $endy - $this->y;
  19234. } else { // middle column
  19235. $cborder = $border_middle;
  19236. if (isset($this->columns[$column]['th']['\''.$page.'\''])) {
  19237. $this->y = $this->columns[$column]['th']['\''.$page.'\''];
  19238. }
  19239. $this->x += $deltacol;
  19240. $h = $this->h - $this->y - $this->bMargin;
  19241. }
  19242. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
  19243. } // end for each column
  19244. } elseif ($page == $startpage) { // first page
  19245. $deltacol = 0;
  19246. $deltath = 0;
  19247. for ($column = $startcolumn; $column < $this->num_columns; ++$column) { // for each column
  19248. $this->selectColumn($column);
  19249. if ($column == $startcolumn) { // first column
  19250. $cborder = $border_start;
  19251. $this->y = $starty;
  19252. $this->x = $x;
  19253. $h = $this->h - $this->y - $this->bMargin;
  19254. if ($this->rtl) {
  19255. $deltacol = $this->x + $this->rMargin - $this->w;
  19256. } else {
  19257. $deltacol = $this->x - $this->lMargin;
  19258. }
  19259. } else { // middle column
  19260. $cborder = $border_middle;
  19261. if (isset($this->columns[$column]['th']['\''.$page.'\''])) {
  19262. $this->y = $this->columns[$column]['th']['\''.$page.'\''];
  19263. }
  19264. $this->x += $deltacol;
  19265. $h = $this->h - $this->y - $this->bMargin;
  19266. }
  19267. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
  19268. } // end for each column
  19269. } elseif ($page == $endpage) { // last page
  19270. $deltacol = 0;
  19271. $deltath = 0;
  19272. for ($column = 0; $column <= $endcolumn; ++$column) { // for each column
  19273. $this->selectColumn($column);
  19274. if ($column == $endcolumn) { // end column
  19275. $cborder = $border_end;
  19276. if (isset($this->columns[$column]['th']['\''.$page.'\''])) {
  19277. $this->y = $this->columns[$column]['th']['\''.$page.'\''];
  19278. }
  19279. $this->x += $deltacol;
  19280. $h = $endy - $this->y;
  19281. } else { // middle column
  19282. $cborder = $border_middle;
  19283. if (isset($this->columns[$column]['th']['\''.$page.'\''])) {
  19284. $this->y = $this->columns[$column]['th']['\''.$page.'\''];
  19285. }
  19286. $this->x += $deltacol;
  19287. $h = $this->h - $this->y - $this->bMargin;
  19288. }
  19289. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
  19290. } // end for each column
  19291. } else { // middle page
  19292. $deltacol = 0;
  19293. $deltath = 0;
  19294. for ($column = 0; $column < $this->num_columns; ++$column) { // for each column
  19295. $this->selectColumn($column);
  19296. $cborder = $border_middle;
  19297. if (isset($this->columns[$column]['th']['\''.$page.'\''])) {
  19298. $this->y = $this->columns[$column]['th']['\''.$page.'\''];
  19299. }
  19300. $this->x += $deltacol;
  19301. $h = $this->h - $this->y - $this->bMargin;
  19302. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
  19303. } // end for each column
  19304. }
  19305. if (!empty($cborder) OR !empty($fill)) {
  19306. $offsetlen = strlen($ccode);
  19307. // draw border and fill
  19308. if ($this->inxobj) {
  19309. // we are inside an XObject template
  19310. if (end($this->xobjects[$this->xobjid]['transfmrk']) !== false) {
  19311. $pagemarkkey = key($this->xobjects[$this->xobjid]['transfmrk']);
  19312. $pagemark = $this->xobjects[$this->xobjid]['transfmrk'][$pagemarkkey];
  19313. $this->xobjects[$this->xobjid]['transfmrk'][$pagemarkkey] += $offsetlen;
  19314. } else {
  19315. $pagemark = $this->xobjects[$this->xobjid]['intmrk'];
  19316. $this->xobjects[$this->xobjid]['intmrk'] += $offsetlen;
  19317. }
  19318. $pagebuff = $this->xobjects[$this->xobjid]['outdata'];
  19319. $pstart = substr($pagebuff, 0, $pagemark);
  19320. $pend = substr($pagebuff, $pagemark);
  19321. $this->xobjects[$this->xobjid]['outdata'] = $pstart.$ccode.$pend;
  19322. } else {
  19323. // draw border and fill
  19324. if (end($this->transfmrk[$this->page]) !== false) {
  19325. $pagemarkkey = key($this->transfmrk[$this->page]);
  19326. $pagemark = $this->transfmrk[$this->page][$pagemarkkey];
  19327. } elseif ($this->InFooter) {
  19328. $pagemark = $this->footerpos[$this->page];
  19329. } else {
  19330. $pagemark = $this->intmrk[$this->page];
  19331. }
  19332. $pagebuff = $this->getPageBuffer($this->page);
  19333. $pstart = substr($pagebuff, 0, $pagemark);
  19334. $pend = substr($pagebuff, $pagemark);
  19335. $this->setPageBuffer($this->page, $pstart.$ccode.$pend);
  19336. }
  19337. }
  19338. } // end for each page
  19339. // restore default border
  19340. $border = $default_border;
  19341. } // end for each cell on the row
  19342. if (isset($table_el['attribute']['cellspacing'])) {
  19343. $this->y += $this->getHTMLUnitToUnits($table_el['attribute']['cellspacing'], 1, 'px');
  19344. } elseif (isset($table_el['border-spacing'])) {
  19345. $this->y += $table_el['border-spacing']['V'];
  19346. }
  19347. $this->Ln(0, $cell);
  19348. $this->x = $parent['startx'];
  19349. if ($endpage > $startpage) {
  19350. if (($this->rtl) AND ($this->pagedim[$endpage]['orm'] != $this->pagedim[$startpage]['orm'])) {
  19351. $this->x += ($this->pagedim[$endpage]['orm'] - $this->pagedim[$startpage]['orm']);
  19352. } elseif ((!$this->rtl) AND ($this->pagedim[$endpage]['olm'] != $this->pagedim[$startpage]['olm'])) {
  19353. $this->x += ($this->pagedim[$endpage]['olm'] - $this->pagedim[$startpage]['olm']);
  19354. }
  19355. }
  19356. }
  19357. if (!$in_table_head) { // we are not inside a thead section
  19358. $this->cell_padding = isset($table_el['old_cell_padding']) ? $table_el['old_cell_padding'] : null;
  19359. // reset row height
  19360. $this->resetLastH();
  19361. if (($this->page == ($this->numpages - 1)) AND ($this->pageopen[$this->numpages])) {
  19362. $plendiff = ($this->pagelen[$this->numpages] - $this->emptypagemrk[$this->numpages]);
  19363. if (($plendiff > 0) AND ($plendiff < 60)) {
  19364. $pagediff = substr($this->getPageBuffer($this->numpages), $this->emptypagemrk[$this->numpages], $plendiff);
  19365. if (substr($pagediff, 0, 5) == 'BT /F') {
  19366. // the difference is only a font setting
  19367. $plendiff = 0;
  19368. }
  19369. }
  19370. if ($plendiff == 0) {
  19371. // remove last blank page
  19372. $this->deletePage($this->numpages);
  19373. }
  19374. }
  19375. if (isset($this->theadMargins['top'])) {
  19376. // restore top margin
  19377. $this->tMargin = $this->theadMargins['top'];
  19378. }
  19379. if (!isset($table_el['attribute']['nested']) OR ($table_el['attribute']['nested'] != 'true')) {
  19380. // reset main table header
  19381. $this->thead = '';
  19382. $this->theadMargins = array();
  19383. $this->pagedim[$this->page]['tm'] = $this->tMargin;
  19384. }
  19385. }
  19386. $parent = $table_el;
  19387. break;
  19388. }
  19389. case 'a': {
  19390. $this->HREF = array();
  19391. break;
  19392. }
  19393. case 'sup': {
  19394. $this->setXY($this->GetX(), $this->GetY() + ((0.7 * $parent['fontsize']) / $this->k));
  19395. break;
  19396. }
  19397. case 'sub': {
  19398. $this->setXY($this->GetX(), $this->GetY() - ((0.3 * $parent['fontsize']) / $this->k));
  19399. break;
  19400. }
  19401. case 'div': {
  19402. $this->addHTMLVertSpace($hbz, $hb, $cell, false, $lasttag);
  19403. break;
  19404. }
  19405. case 'blockquote': {
  19406. if ($this->rtl) {
  19407. $this->rMargin -= $this->listindent;
  19408. } else {
  19409. $this->lMargin -= $this->listindent;
  19410. }
  19411. --$this->listindentlevel;
  19412. $this->addHTMLVertSpace($hbz, $hb, $cell, false, $lasttag);
  19413. break;
  19414. }
  19415. case 'p': {
  19416. $this->addHTMLVertSpace($hbz, $hb, $cell, false, $lasttag);
  19417. break;
  19418. }
  19419. case 'pre': {
  19420. $this->addHTMLVertSpace($hbz, $hb, $cell, false, $lasttag);
  19421. $this->premode = false;
  19422. break;
  19423. }
  19424. case 'dl': {
  19425. --$this->listnum;
  19426. if ($this->listnum <= 0) {
  19427. $this->listnum = 0;
  19428. $this->addHTMLVertSpace($hbz, $hb, $cell, false, $lasttag);
  19429. } else {
  19430. $this->addHTMLVertSpace(0, 0, $cell, false, $lasttag);
  19431. }
  19432. $this->resetLastH();
  19433. break;
  19434. }
  19435. case 'dt': {
  19436. $this->lispacer = '';
  19437. $this->addHTMLVertSpace(0, 0, $cell, false, $lasttag);
  19438. break;
  19439. }
  19440. case 'dd': {
  19441. $this->lispacer = '';
  19442. if ($this->rtl) {
  19443. $this->rMargin -= $this->listindent;
  19444. } else {
  19445. $this->lMargin -= $this->listindent;
  19446. }
  19447. --$this->listindentlevel;
  19448. $this->addHTMLVertSpace(0, 0, $cell, false, $lasttag);
  19449. break;
  19450. }
  19451. case 'ul':
  19452. case 'ol': {
  19453. --$this->listnum;
  19454. $this->lispacer = '';
  19455. if ($this->rtl) {
  19456. $this->rMargin -= $this->listindent;
  19457. } else {
  19458. $this->lMargin -= $this->listindent;
  19459. }
  19460. --$this->listindentlevel;
  19461. if ($this->listnum <= 0) {
  19462. $this->listnum = 0;
  19463. $this->addHTMLVertSpace($hbz, $hb, $cell, false, $lasttag);
  19464. } else {
  19465. $this->addHTMLVertSpace(0, 0, $cell, false, $lasttag);
  19466. }
  19467. $this->resetLastH();
  19468. break;
  19469. }
  19470. case 'li': {
  19471. $this->lispacer = '';
  19472. $this->addHTMLVertSpace(0, 0, $cell, false, $lasttag);
  19473. break;
  19474. }
  19475. case 'h1':
  19476. case 'h2':
  19477. case 'h3':
  19478. case 'h4':
  19479. case 'h5':
  19480. case 'h6': {
  19481. $this->addHTMLVertSpace($hbz, $hb, $cell, false, $lasttag);
  19482. break;
  19483. }
  19484. // Form fields (since 4.8.000 - 2009-09-07)
  19485. case 'form': {
  19486. $this->form_action = '';
  19487. $this->form_enctype = 'application/x-www-form-urlencoded';
  19488. break;
  19489. }
  19490. default : {
  19491. break;
  19492. }
  19493. }
  19494. // draw border and background (if any)
  19495. $this->drawHTMLTagBorder($parent, $xmax);
  19496. if (isset($dom[($dom[$key]['parent'])]['attribute']['pagebreakafter'])) {
  19497. $pba = $dom[($dom[$key]['parent'])]['attribute']['pagebreakafter'];
  19498. // check for pagebreak
  19499. if (($pba == 'true') OR ($pba == 'left') OR ($pba == 'right')) {
  19500. // add a page (or trig AcceptPageBreak() for multicolumn mode)
  19501. $this->checkPageBreak($this->PageBreakTrigger + 1);
  19502. }
  19503. if ((($pba == 'left') AND (((!$this->rtl) AND (($this->page % 2) == 0)) OR (($this->rtl) AND (($this->page % 2) != 0))))
  19504. OR (($pba == 'right') AND (((!$this->rtl) AND (($this->page % 2) != 0)) OR (($this->rtl) AND (($this->page % 2) == 0))))) {
  19505. // add a page (or trig AcceptPageBreak() for multicolumn mode)
  19506. $this->checkPageBreak($this->PageBreakTrigger + 1);
  19507. }
  19508. }
  19509. $this->tmprtl = false;
  19510. return $dom;
  19511. }
  19512. /**
  19513. * Add vertical spaces if needed.
  19514. * @param string $hbz Distance between current y and line bottom.
  19515. * @param string $hb The height of the break.
  19516. * @param boolean $cell if true add the default left (or right if RTL) padding to each new line (default false).
  19517. * @param boolean $firsttag set to true when the tag is the first.
  19518. * @param boolean $lasttag set to true when the tag is the last.
  19519. * @protected
  19520. */
  19521. protected function addHTMLVertSpace($hbz=0, $hb=0, $cell=false, $firsttag=false, $lasttag=false) {
  19522. if ($firsttag) {
  19523. $this->Ln(0, $cell);
  19524. $this->htmlvspace = 0;
  19525. return;
  19526. }
  19527. if ($lasttag) {
  19528. $this->Ln($hbz, $cell);
  19529. $this->htmlvspace = 0;
  19530. return;
  19531. }
  19532. if ($hb < $this->htmlvspace) {
  19533. $hd = 0;
  19534. } else {
  19535. $hd = $hb - $this->htmlvspace;
  19536. $this->htmlvspace = $hb;
  19537. }
  19538. $this->Ln(($hbz + $hd), $cell);
  19539. }
  19540. /**
  19541. * Return the starting coordinates to draw an html border
  19542. * @return array containing top-left border coordinates
  19543. * @protected
  19544. * @since 5.7.000 (2010-08-03)
  19545. */
  19546. protected function getBorderStartPosition() {
  19547. if ($this->rtl) {
  19548. $xmax = $this->lMargin;
  19549. } else {
  19550. $xmax = $this->w - $this->rMargin;
  19551. }
  19552. return array('page' => $this->page, 'column' => $this->current_column, 'x' => $this->x, 'y' => $this->y, 'xmax' => $xmax);
  19553. }
  19554. /**
  19555. * Draw an HTML block border and fill
  19556. * @param array $tag array of tag properties.
  19557. * @param int $xmax end X coordinate for border.
  19558. * @protected
  19559. * @since 5.7.000 (2010-08-03)
  19560. */
  19561. protected function drawHTMLTagBorder($tag, $xmax) {
  19562. if (!isset($tag['borderposition'])) {
  19563. // nothing to draw
  19564. return;
  19565. }
  19566. $prev_x = $this->x;
  19567. $prev_y = $this->y;
  19568. $prev_lasth = $this->lasth;
  19569. $border = 0;
  19570. $fill = false;
  19571. $this->lasth = 0;
  19572. if (isset($tag['border']) AND !empty($tag['border'])) {
  19573. // get border style
  19574. $border = $tag['border'];
  19575. if (!TCPDF_STATIC::empty_string($this->thead) AND (!$this->inthead)) {
  19576. // border for table header
  19577. $border = TCPDF_STATIC::getBorderMode($border, $position='middle', $this->opencell);
  19578. }
  19579. }
  19580. if (isset($tag['bgcolor']) AND ($tag['bgcolor'] !== false)) {
  19581. // get background color
  19582. $old_bgcolor = $this->bgcolor;
  19583. $this->setFillColorArray($tag['bgcolor']);
  19584. $fill = true;
  19585. }
  19586. if (!$border AND !$fill) {
  19587. // nothing to draw
  19588. return;
  19589. }
  19590. if (isset($tag['attribute']['cellspacing'])) {
  19591. $clsp = $this->getHTMLUnitToUnits($tag['attribute']['cellspacing'], 1, 'px');
  19592. $cellspacing = array('H' => $clsp, 'V' => $clsp);
  19593. } elseif (isset($tag['border-spacing'])) {
  19594. $cellspacing = $tag['border-spacing'];
  19595. } else {
  19596. $cellspacing = array('H' => 0, 'V' => 0);
  19597. }
  19598. if (($tag['value'] != 'table') AND (is_array($border)) AND (!empty($border))) {
  19599. // draw the border externally respect the sqare edge.
  19600. $border['mode'] = 'ext';
  19601. }
  19602. if ($this->rtl) {
  19603. if ($xmax >= $tag['borderposition']['x']) {
  19604. $xmax = $tag['borderposition']['xmax'];
  19605. }
  19606. $w = ($tag['borderposition']['x'] - $xmax);
  19607. } else {
  19608. if ($xmax <= $tag['borderposition']['x']) {
  19609. $xmax = $tag['borderposition']['xmax'];
  19610. }
  19611. $w = ($xmax - $tag['borderposition']['x']);
  19612. }
  19613. if ($w <= 0) {
  19614. return;
  19615. }
  19616. $w += $cellspacing['H'];
  19617. $startpage = $tag['borderposition']['page'];
  19618. $startcolumn = $tag['borderposition']['column'];
  19619. $x = $tag['borderposition']['x'];
  19620. $y = $tag['borderposition']['y'];
  19621. $endpage = $this->page;
  19622. $starty = $tag['borderposition']['y'] - $cellspacing['V'];
  19623. $currentY = $this->y;
  19624. $this->x = $x;
  19625. // get latest column
  19626. $endcolumn = $this->current_column;
  19627. if ($this->num_columns == 0) {
  19628. $this->num_columns = 1;
  19629. }
  19630. // get border modes
  19631. $border_start = TCPDF_STATIC::getBorderMode($border, $position='start', $this->opencell);
  19632. $border_end = TCPDF_STATIC::getBorderMode($border, $position='end', $this->opencell);
  19633. $border_middle = TCPDF_STATIC::getBorderMode($border, $position='middle', $this->opencell);
  19634. // temporary disable page regions
  19635. $temp_page_regions = $this->page_regions;
  19636. $this->page_regions = array();
  19637. // design borders around HTML cells.
  19638. for ($page = $startpage; $page <= $endpage; ++$page) { // for each page
  19639. $ccode = '';
  19640. $this->setPage($page);
  19641. if ($this->num_columns < 2) {
  19642. // single-column mode
  19643. $this->x = $x;
  19644. $this->y = $this->tMargin;
  19645. }
  19646. // account for margin changes
  19647. if ($page > $startpage) {
  19648. if (($this->rtl) AND ($this->pagedim[$page]['orm'] != $this->pagedim[$startpage]['orm'])) {
  19649. $this->x -= ($this->pagedim[$page]['orm'] - $this->pagedim[$startpage]['orm']);
  19650. } elseif ((!$this->rtl) AND ($this->pagedim[$page]['olm'] != $this->pagedim[$startpage]['olm'])) {
  19651. $this->x += ($this->pagedim[$page]['olm'] - $this->pagedim[$startpage]['olm']);
  19652. }
  19653. }
  19654. if ($startpage == $endpage) {
  19655. // single page
  19656. for ($column = $startcolumn; $column <= $endcolumn; ++$column) { // for each column
  19657. $this->selectColumn($column);
  19658. if ($startcolumn == $endcolumn) { // single column
  19659. $cborder = $border;
  19660. $h = ($currentY - $y) + $cellspacing['V'];
  19661. $this->y = $starty;
  19662. } elseif ($column == $startcolumn) { // first column
  19663. $cborder = $border_start;
  19664. $this->y = $starty;
  19665. $h = $this->h - $this->y - $this->bMargin;
  19666. } elseif ($column == $endcolumn) { // end column
  19667. $cborder = $border_end;
  19668. $h = $currentY - $this->y;
  19669. } else { // middle column
  19670. $cborder = $border_middle;
  19671. $h = $this->h - $this->y - $this->bMargin;
  19672. }
  19673. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
  19674. } // end for each column
  19675. } elseif ($page == $startpage) { // first page
  19676. for ($column = $startcolumn; $column < $this->num_columns; ++$column) { // for each column
  19677. $this->selectColumn($column);
  19678. if ($column == $startcolumn) { // first column
  19679. $cborder = $border_start;
  19680. $this->y = $starty;
  19681. $h = $this->h - $this->y - $this->bMargin;
  19682. } else { // middle column
  19683. $cborder = $border_middle;
  19684. $h = $this->h - $this->y - $this->bMargin;
  19685. }
  19686. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
  19687. } // end for each column
  19688. } elseif ($page == $endpage) { // last page
  19689. for ($column = 0; $column <= $endcolumn; ++$column) { // for each column
  19690. $this->selectColumn($column);
  19691. if ($column == $endcolumn) {
  19692. // end column
  19693. $cborder = $border_end;
  19694. $h = $currentY - $this->y;
  19695. } else {
  19696. // middle column
  19697. $cborder = $border_middle;
  19698. $h = $this->h - $this->y - $this->bMargin;
  19699. }
  19700. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
  19701. } // end for each column
  19702. } else { // middle page
  19703. for ($column = 0; $column < $this->num_columns; ++$column) { // for each column
  19704. $this->selectColumn($column);
  19705. $cborder = $border_middle;
  19706. $h = $this->h - $this->y - $this->bMargin;
  19707. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
  19708. } // end for each column
  19709. }
  19710. if ($cborder OR $fill) {
  19711. $offsetlen = strlen($ccode);
  19712. // draw border and fill
  19713. if ($this->inxobj) {
  19714. // we are inside an XObject template
  19715. if (end($this->xobjects[$this->xobjid]['transfmrk']) !== false) {
  19716. $pagemarkkey = key($this->xobjects[$this->xobjid]['transfmrk']);
  19717. $pagemark = $this->xobjects[$this->xobjid]['transfmrk'][$pagemarkkey];
  19718. $this->xobjects[$this->xobjid]['transfmrk'][$pagemarkkey] += $offsetlen;
  19719. } else {
  19720. $pagemark = $this->xobjects[$this->xobjid]['intmrk'];
  19721. $this->xobjects[$this->xobjid]['intmrk'] += $offsetlen;
  19722. }
  19723. $pagebuff = $this->xobjects[$this->xobjid]['outdata'];
  19724. $pstart = substr($pagebuff, 0, $pagemark);
  19725. $pend = substr($pagebuff, $pagemark);
  19726. $this->xobjects[$this->xobjid]['outdata'] = $pstart.$ccode.$pend;
  19727. } else {
  19728. if (end($this->transfmrk[$this->page]) !== false) {
  19729. $pagemarkkey = key($this->transfmrk[$this->page]);
  19730. $pagemark = $this->transfmrk[$this->page][$pagemarkkey];
  19731. } elseif ($this->InFooter) {
  19732. $pagemark = $this->footerpos[$this->page];
  19733. } else {
  19734. $pagemark = $this->intmrk[$this->page];
  19735. }
  19736. $pagebuff = $this->getPageBuffer($this->page);
  19737. $pstart = substr($pagebuff, 0, $pagemark);
  19738. $pend = substr($pagebuff, $pagemark);
  19739. $this->setPageBuffer($this->page, $pstart.$ccode.$pend);
  19740. $this->bordermrk[$this->page] += $offsetlen;
  19741. $this->cntmrk[$this->page] += $offsetlen;
  19742. }
  19743. }
  19744. } // end for each page
  19745. // restore page regions
  19746. $this->page_regions = $temp_page_regions;
  19747. if (isset($old_bgcolor)) {
  19748. // restore background color
  19749. $this->setFillColorArray($old_bgcolor);
  19750. }
  19751. // restore pointer position
  19752. $this->x = $prev_x;
  19753. $this->y = $prev_y;
  19754. $this->lasth = $prev_lasth;
  19755. }
  19756. /**
  19757. * Set the default bullet to be used as LI bullet symbol
  19758. * @param string $symbol character or string to be used (legal values are: '' = automatic, '!' = auto bullet, '#' = auto numbering, 'disc', 'disc', 'circle', 'square', '1', 'decimal', 'decimal-leading-zero', 'i', 'lower-roman', 'I', 'upper-roman', 'a', 'lower-alpha', 'lower-latin', 'A', 'upper-alpha', 'upper-latin', 'lower-greek', 'img|type|width|height|image.ext')
  19759. * @public
  19760. * @since 4.0.028 (2008-09-26)
  19761. */
  19762. public function setLIsymbol($symbol='!') {
  19763. // check for custom image symbol
  19764. if (substr($symbol, 0, 4) == 'img|') {
  19765. $this->lisymbol = $symbol;
  19766. return;
  19767. }
  19768. $symbol = strtolower($symbol);
  19769. $valid_symbols = array('!', '#', 'disc', 'circle', 'square', '1', 'decimal', 'decimal-leading-zero', 'i', 'lower-roman', 'I', 'upper-roman', 'a', 'lower-alpha', 'lower-latin', 'A', 'upper-alpha', 'upper-latin', 'lower-greek');
  19770. if (in_array($symbol, $valid_symbols)) {
  19771. $this->lisymbol = $symbol;
  19772. } else {
  19773. $this->lisymbol = '';
  19774. }
  19775. }
  19776. /**
  19777. * Set the booklet mode for double-sided pages.
  19778. * @param boolean $booklet true set the booklet mode on, false otherwise.
  19779. * @param float $inner Inner page margin.
  19780. * @param float $outer Outer page margin.
  19781. * @public
  19782. * @since 4.2.000 (2008-10-29)
  19783. */
  19784. public function setBooklet($booklet=true, $inner=-1, $outer=-1) {
  19785. $this->booklet = $booklet;
  19786. if ($inner >= 0) {
  19787. $this->lMargin = $inner;
  19788. }
  19789. if ($outer >= 0) {
  19790. $this->rMargin = $outer;
  19791. }
  19792. }
  19793. /**
  19794. * Swap the left and right margins.
  19795. * @param boolean $reverse if true swap left and right margins.
  19796. * @protected
  19797. * @since 4.2.000 (2008-10-29)
  19798. */
  19799. protected function swapMargins($reverse=true) {
  19800. if ($reverse) {
  19801. // swap left and right margins
  19802. $mtemp = $this->original_lMargin;
  19803. $this->original_lMargin = $this->original_rMargin;
  19804. $this->original_rMargin = $mtemp;
  19805. $deltam = $this->original_lMargin - $this->original_rMargin;
  19806. $this->lMargin += $deltam;
  19807. $this->rMargin -= $deltam;
  19808. }
  19809. }
  19810. /**
  19811. * Set the vertical spaces for HTML tags.
  19812. * The array must have the following structure (example):
  19813. * $tagvs = array('h1' => array(0 => array('h' => '', 'n' => 2), 1 => array('h' => 1.3, 'n' => 1)));
  19814. * The first array level contains the tag names,
  19815. * the second level contains 0 for opening tags or 1 for closing tags,
  19816. * the third level contains the vertical space unit (h) and the number spaces to add (n).
  19817. * If the h parameter is not specified, default values are used.
  19818. * @param array $tagvs array of tags and relative vertical spaces.
  19819. * @public
  19820. * @since 4.2.001 (2008-10-30)
  19821. */
  19822. public function setHtmlVSpace($tagvs) {
  19823. $this->tagvspaces = $tagvs;
  19824. }
  19825. /**
  19826. * Set custom width for list indentation.
  19827. * @param float $width width of the indentation. Use negative value to disable it.
  19828. * @public
  19829. * @since 4.2.007 (2008-11-12)
  19830. */
  19831. public function setListIndentWidth($width) {
  19832. return $this->customlistindent = floatval($width);
  19833. }
  19834. /**
  19835. * Set the top/bottom cell sides to be open or closed when the cell cross the page.
  19836. * @param boolean $isopen if true keeps the top/bottom border open for the cell sides that cross the page.
  19837. * @public
  19838. * @since 4.2.010 (2008-11-14)
  19839. */
  19840. public function setOpenCell($isopen) {
  19841. $this->opencell = $isopen;
  19842. }
  19843. /**
  19844. * Set the color and font style for HTML links.
  19845. * @param array $color RGB array of colors
  19846. * @param string $fontstyle additional font styles to add
  19847. * @public
  19848. * @since 4.4.003 (2008-12-09)
  19849. */
  19850. public function setHtmlLinksStyle($color=array(0,0,255), $fontstyle='U') {
  19851. $this->htmlLinkColorArray = $color;
  19852. $this->htmlLinkFontStyle = $fontstyle;
  19853. }
  19854. /**
  19855. * Convert HTML string containing value and unit of measure to user's units or points.
  19856. * @param string $htmlval String containing values and unit.
  19857. * @param string $refsize Reference value in points.
  19858. * @param string $defaultunit Default unit (can be one of the following: %, em, ex, px, in, mm, pc, pt).
  19859. * @param boolean $points If true returns points, otherwise returns value in user's units.
  19860. * @return float value in user's unit or point if $points=true
  19861. * @public
  19862. * @since 4.4.004 (2008-12-10)
  19863. */
  19864. public function getHTMLUnitToUnits($htmlval, $refsize=1, $defaultunit='px', $points=false) {
  19865. $supportedunits = array('%', 'em', 'ex', 'px', 'in', 'cm', 'mm', 'pc', 'pt');
  19866. $retval = 0;
  19867. $value = 0;
  19868. $unit = 'px';
  19869. if ($points) {
  19870. $k = 1;
  19871. } else {
  19872. $k = $this->k;
  19873. }
  19874. if (in_array($defaultunit, $supportedunits)) {
  19875. $unit = $defaultunit;
  19876. }
  19877. if (is_numeric($htmlval)) {
  19878. $value = floatval($htmlval);
  19879. } elseif (preg_match('/([0-9\.\-\+]+)/', $htmlval, $mnum)) {
  19880. $value = floatval($mnum[1]);
  19881. if (preg_match('/([a-z%]+)/', $htmlval, $munit)) {
  19882. if (in_array($munit[1], $supportedunits)) {
  19883. $unit = $munit[1];
  19884. }
  19885. }
  19886. }
  19887. switch ($unit) {
  19888. // percentage
  19889. case '%': {
  19890. $retval = (($value * $refsize) / 100);
  19891. break;
  19892. }
  19893. // relative-size
  19894. case 'em': {
  19895. $retval = ($value * $refsize);
  19896. break;
  19897. }
  19898. // height of lower case 'x' (about half the font-size)
  19899. case 'ex': {
  19900. $retval = ($value * ($refsize / 2));
  19901. break;
  19902. }
  19903. // absolute-size
  19904. case 'in': {
  19905. $retval = (($value * $this->dpi) / $k);
  19906. break;
  19907. }
  19908. // centimeters
  19909. case 'cm': {
  19910. $retval = (($value / 2.54 * $this->dpi) / $k);
  19911. break;
  19912. }
  19913. // millimeters
  19914. case 'mm': {
  19915. $retval = (($value / 25.4 * $this->dpi) / $k);
  19916. break;
  19917. }
  19918. // one pica is 12 points
  19919. case 'pc': {
  19920. $retval = (($value * 12) / $k);
  19921. break;
  19922. }
  19923. // points
  19924. case 'pt': {
  19925. $retval = ($value / $k);
  19926. break;
  19927. }
  19928. // pixels
  19929. case 'px': {
  19930. $retval = $this->pixelsToUnits($value);
  19931. if ($points) {
  19932. $retval *= $this->k;
  19933. }
  19934. break;
  19935. }
  19936. }
  19937. return $retval;
  19938. }
  19939. /**
  19940. * Output an HTML list bullet or ordered item symbol
  19941. * @param int $listdepth list nesting level
  19942. * @param string $listtype type of list
  19943. * @param float $size current font size
  19944. * @protected
  19945. * @since 4.4.004 (2008-12-10)
  19946. */
  19947. protected function putHtmlListBullet($listdepth, $listtype='', $size=10) {
  19948. if ($this->state != 2) {
  19949. return;
  19950. }
  19951. $size /= $this->k;
  19952. $fill = '';
  19953. $bgcolor = $this->bgcolor;
  19954. $color = $this->fgcolor;
  19955. $strokecolor = $this->strokecolor;
  19956. $width = 0;
  19957. $textitem = '';
  19958. $tmpx = $this->x;
  19959. $lspace = $this->GetStringWidth(' ');
  19960. if ($listtype == '^') {
  19961. // special symbol used for avoid justification of rect bullet
  19962. $this->lispacer = '';
  19963. return;
  19964. } elseif ($listtype == '!') {
  19965. // set default list type for unordered list
  19966. $deftypes = array('disc', 'circle', 'square');
  19967. $listtype = $deftypes[($listdepth - 1) % 3];
  19968. } elseif ($listtype == '#') {
  19969. // set default list type for ordered list
  19970. $listtype = 'decimal';
  19971. } elseif (substr($listtype, 0, 4) == 'img|') {
  19972. // custom image type ('img|type|width|height|image.ext')
  19973. $img = explode('|', $listtype);
  19974. $listtype = 'img';
  19975. }
  19976. switch ($listtype) {
  19977. // unordered types
  19978. case 'none': {
  19979. break;
  19980. }
  19981. case 'disc': {
  19982. $r = $size / 6;
  19983. $lspace += (2 * $r);
  19984. if ($this->rtl) {
  19985. $this->x += $lspace;
  19986. } else {
  19987. $this->x -= $lspace;
  19988. }
  19989. $this->Circle(($this->x + $r), ($this->y + ($this->lasth / 2)), $r, 0, 360, 'F', array(), $color, 8);
  19990. break;
  19991. }
  19992. case 'circle': {
  19993. $r = $size / 6;
  19994. $lspace += (2 * $r);
  19995. if ($this->rtl) {
  19996. $this->x += $lspace;
  19997. } else {
  19998. $this->x -= $lspace;
  19999. }
  20000. $prev_line_style = $this->linestyleWidth.' '.$this->linestyleCap.' '.$this->linestyleJoin.' '.$this->linestyleDash.' '.$this->DrawColor;
  20001. $new_line_style = array('width' => ($r / 3), 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'phase' => 0, 'color'=>$color);
  20002. $this->Circle(($this->x + $r), ($this->y + ($this->lasth / 2)), ($r * (1 - (1/6))), 0, 360, 'D', $new_line_style, array(), 8);
  20003. $this->_out($prev_line_style); // restore line settings
  20004. break;
  20005. }
  20006. case 'square': {
  20007. $l = $size / 3;
  20008. $lspace += $l;
  20009. if ($this->rtl) {;
  20010. $this->x += $lspace;
  20011. } else {
  20012. $this->x -= $lspace;
  20013. }
  20014. $this->Rect($this->x, ($this->y + (($this->lasth - $l) / 2)), $l, $l, 'F', array(), $color);
  20015. break;
  20016. }
  20017. case 'img': {
  20018. // 1=>type, 2=>width, 3=>height, 4=>image.ext
  20019. $lspace += $img[2];
  20020. if ($this->rtl) {;
  20021. $this->x += $lspace;
  20022. } else {
  20023. $this->x -= $lspace;
  20024. }
  20025. $imgtype = strtolower($img[1]);
  20026. $prev_y = $this->y;
  20027. switch ($imgtype) {
  20028. case 'svg': {
  20029. $this->ImageSVG($img[4], $this->x, ($this->y + (($this->lasth - $img[3]) / 2)), $img[2], $img[3], '', 'T', '', 0, false);
  20030. break;
  20031. }
  20032. case 'ai':
  20033. case 'eps': {
  20034. $this->ImageEps($img[4], $this->x, ($this->y + (($this->lasth - $img[3]) / 2)), $img[2], $img[3], '', true, 'T', '', 0, false);
  20035. break;
  20036. }
  20037. default: {
  20038. $this->Image($img[4], $this->x, ($this->y + (($this->lasth - $img[3]) / 2)), $img[2], $img[3], $img[1], '', 'T', false, 300, '', false, false, 0, false, false, false);
  20039. break;
  20040. }
  20041. }
  20042. $this->y = $prev_y;
  20043. break;
  20044. }
  20045. // ordered types
  20046. // $this->listcount[$this->listnum];
  20047. // $textitem
  20048. case '1':
  20049. case 'decimal': {
  20050. $textitem = $this->listcount[$this->listnum];
  20051. break;
  20052. }
  20053. case 'decimal-leading-zero': {
  20054. $textitem = sprintf('%02d', $this->listcount[$this->listnum]);
  20055. break;
  20056. }
  20057. case 'i':
  20058. case 'lower-roman': {
  20059. $textitem = strtolower(TCPDF_STATIC::intToRoman($this->listcount[$this->listnum]));
  20060. break;
  20061. }
  20062. case 'I':
  20063. case 'upper-roman': {
  20064. $textitem = TCPDF_STATIC::intToRoman($this->listcount[$this->listnum]);
  20065. break;
  20066. }
  20067. case 'a':
  20068. case 'lower-alpha':
  20069. case 'lower-latin': {
  20070. $textitem = chr(97 + $this->listcount[$this->listnum] - 1);
  20071. break;
  20072. }
  20073. case 'A':
  20074. case 'upper-alpha':
  20075. case 'upper-latin': {
  20076. $textitem = chr(65 + $this->listcount[$this->listnum] - 1);
  20077. break;
  20078. }
  20079. case 'lower-greek': {
  20080. $textitem = TCPDF_FONTS::unichr((945 + $this->listcount[$this->listnum] - 1), $this->isunicode);
  20081. break;
  20082. }
  20083. /*
  20084. // Types to be implemented (special handling)
  20085. case 'hebrew': {
  20086. break;
  20087. }
  20088. case 'armenian': {
  20089. break;
  20090. }
  20091. case 'georgian': {
  20092. break;
  20093. }
  20094. case 'cjk-ideographic': {
  20095. break;
  20096. }
  20097. case 'hiragana': {
  20098. break;
  20099. }
  20100. case 'katakana': {
  20101. break;
  20102. }
  20103. case 'hiragana-iroha': {
  20104. break;
  20105. }
  20106. case 'katakana-iroha': {
  20107. break;
  20108. }
  20109. */
  20110. default: {
  20111. $textitem = $this->listcount[$this->listnum];
  20112. }
  20113. }
  20114. if (!TCPDF_STATIC::empty_string($textitem)) {
  20115. // Check whether we need a new page or new column
  20116. $prev_y = $this->y;
  20117. $h = $this->getCellHeight($this->FontSize);
  20118. if ($this->checkPageBreak($h) OR ($this->y < $prev_y)) {
  20119. $tmpx = $this->x;
  20120. }
  20121. // print ordered item
  20122. if ($this->rtl) {
  20123. $textitem = '.'.$textitem;
  20124. } else {
  20125. $textitem = $textitem.'.';
  20126. }
  20127. $lspace += $this->GetStringWidth($textitem);
  20128. if ($this->rtl) {
  20129. $this->x += $lspace;
  20130. } else {
  20131. $this->x -= $lspace;
  20132. }
  20133. $this->Write($this->lasth, $textitem, '', false, '', false, 0, false);
  20134. }
  20135. $this->x = $tmpx;
  20136. $this->lispacer = '^';
  20137. // restore colors
  20138. $this->setFillColorArray($bgcolor);
  20139. $this->setDrawColorArray($strokecolor);
  20140. $this->settextColorArray($color);
  20141. }
  20142. /**
  20143. * Returns current graphic variables as array.
  20144. * @return array of graphic variables
  20145. * @protected
  20146. * @since 4.2.010 (2008-11-14)
  20147. */
  20148. protected function getGraphicVars() {
  20149. $grapvars = array(
  20150. 'FontFamily' => $this->FontFamily,
  20151. 'FontStyle' => $this->FontStyle,
  20152. 'FontSizePt' => $this->FontSizePt,
  20153. 'rMargin' => $this->rMargin,
  20154. 'lMargin' => $this->lMargin,
  20155. 'cell_padding' => $this->cell_padding,
  20156. 'cell_margin' => $this->cell_margin,
  20157. 'LineWidth' => $this->LineWidth,
  20158. 'linestyleWidth' => $this->linestyleWidth,
  20159. 'linestyleCap' => $this->linestyleCap,
  20160. 'linestyleJoin' => $this->linestyleJoin,
  20161. 'linestyleDash' => $this->linestyleDash,
  20162. 'textrendermode' => $this->textrendermode,
  20163. 'textstrokewidth' => $this->textstrokewidth,
  20164. 'DrawColor' => $this->DrawColor,
  20165. 'FillColor' => $this->FillColor,
  20166. 'TextColor' => $this->TextColor,
  20167. 'ColorFlag' => $this->ColorFlag,
  20168. 'bgcolor' => $this->bgcolor,
  20169. 'fgcolor' => $this->fgcolor,
  20170. 'htmlvspace' => $this->htmlvspace,
  20171. 'listindent' => $this->listindent,
  20172. 'listindentlevel' => $this->listindentlevel,
  20173. 'listnum' => $this->listnum,
  20174. 'listordered' => $this->listordered,
  20175. 'listcount' => $this->listcount,
  20176. 'lispacer' => $this->lispacer,
  20177. 'cell_height_ratio' => $this->cell_height_ratio,
  20178. 'font_stretching' => $this->font_stretching,
  20179. 'font_spacing' => $this->font_spacing,
  20180. 'alpha' => $this->alpha,
  20181. // extended
  20182. 'lasth' => $this->lasth,
  20183. 'tMargin' => $this->tMargin,
  20184. 'bMargin' => $this->bMargin,
  20185. 'AutoPageBreak' => $this->AutoPageBreak,
  20186. 'PageBreakTrigger' => $this->PageBreakTrigger,
  20187. 'x' => $this->x,
  20188. 'y' => $this->y,
  20189. 'w' => $this->w,
  20190. 'h' => $this->h,
  20191. 'wPt' => $this->wPt,
  20192. 'hPt' => $this->hPt,
  20193. 'fwPt' => $this->fwPt,
  20194. 'fhPt' => $this->fhPt,
  20195. 'page' => $this->page,
  20196. 'current_column' => $this->current_column,
  20197. 'num_columns' => $this->num_columns
  20198. );
  20199. return $grapvars;
  20200. }
  20201. /**
  20202. * Set graphic variables.
  20203. * @param array $gvars array of graphic variablesto restore
  20204. * @param boolean $extended if true restore extended graphic variables
  20205. * @protected
  20206. * @since 4.2.010 (2008-11-14)
  20207. */
  20208. protected function setGraphicVars($gvars, $extended=false) {
  20209. if ($this->state != 2) {
  20210. return;
  20211. }
  20212. $this->FontFamily = $gvars['FontFamily'];
  20213. $this->FontStyle = $gvars['FontStyle'];
  20214. $this->FontSizePt = $gvars['FontSizePt'];
  20215. $this->rMargin = $gvars['rMargin'];
  20216. $this->lMargin = $gvars['lMargin'];
  20217. $this->cell_padding = $gvars['cell_padding'];
  20218. $this->cell_margin = $gvars['cell_margin'];
  20219. $this->LineWidth = $gvars['LineWidth'];
  20220. $this->linestyleWidth = $gvars['linestyleWidth'];
  20221. $this->linestyleCap = $gvars['linestyleCap'];
  20222. $this->linestyleJoin = $gvars['linestyleJoin'];
  20223. $this->linestyleDash = $gvars['linestyleDash'];
  20224. $this->textrendermode = $gvars['textrendermode'];
  20225. $this->textstrokewidth = $gvars['textstrokewidth'];
  20226. $this->DrawColor = $gvars['DrawColor'];
  20227. $this->FillColor = $gvars['FillColor'];
  20228. $this->TextColor = $gvars['TextColor'];
  20229. $this->ColorFlag = $gvars['ColorFlag'];
  20230. $this->bgcolor = $gvars['bgcolor'];
  20231. $this->fgcolor = $gvars['fgcolor'];
  20232. $this->htmlvspace = $gvars['htmlvspace'];
  20233. $this->listindent = $gvars['listindent'];
  20234. $this->listindentlevel = $gvars['listindentlevel'];
  20235. $this->listnum = $gvars['listnum'];
  20236. $this->listordered = $gvars['listordered'];
  20237. $this->listcount = $gvars['listcount'];
  20238. $this->lispacer = $gvars['lispacer'];
  20239. $this->cell_height_ratio = $gvars['cell_height_ratio'];
  20240. $this->font_stretching = $gvars['font_stretching'];
  20241. $this->font_spacing = $gvars['font_spacing'];
  20242. $this->alpha = $gvars['alpha'];
  20243. if ($extended) {
  20244. // restore extended values
  20245. $this->lasth = $gvars['lasth'];
  20246. $this->tMargin = $gvars['tMargin'];
  20247. $this->bMargin = $gvars['bMargin'];
  20248. $this->AutoPageBreak = $gvars['AutoPageBreak'];
  20249. $this->PageBreakTrigger = $gvars['PageBreakTrigger'];
  20250. $this->x = $gvars['x'];
  20251. $this->y = $gvars['y'];
  20252. $this->w = $gvars['w'];
  20253. $this->h = $gvars['h'];
  20254. $this->wPt = $gvars['wPt'];
  20255. $this->hPt = $gvars['hPt'];
  20256. $this->fwPt = $gvars['fwPt'];
  20257. $this->fhPt = $gvars['fhPt'];
  20258. $this->page = $gvars['page'];
  20259. $this->current_column = $gvars['current_column'];
  20260. $this->num_columns = $gvars['num_columns'];
  20261. }
  20262. $this->_out(''.$this->linestyleWidth.' '.$this->linestyleCap.' '.$this->linestyleJoin.' '.$this->linestyleDash.' '.$this->DrawColor.' '.$this->FillColor.'');
  20263. if (!TCPDF_STATIC::empty_string($this->FontFamily)) {
  20264. $this->setFont($this->FontFamily, $this->FontStyle, $this->FontSizePt);
  20265. }
  20266. }
  20267. /**
  20268. * Outputs the "save graphics state" operator 'q'
  20269. * @protected
  20270. */
  20271. protected function _outSaveGraphicsState() {
  20272. $this->_out('q');
  20273. }
  20274. /**
  20275. * Outputs the "restore graphics state" operator 'Q'
  20276. * @protected
  20277. */
  20278. protected function _outRestoreGraphicsState() {
  20279. $this->_out('Q');
  20280. }
  20281. /**
  20282. * Set buffer content (always append data).
  20283. * @param string $data data
  20284. * @protected
  20285. * @since 4.5.000 (2009-01-02)
  20286. */
  20287. protected function setBuffer($data) {
  20288. $this->bufferlen += strlen($data);
  20289. $this->buffer .= $data;
  20290. }
  20291. /**
  20292. * Replace the buffer content
  20293. * @param string $data data
  20294. * @protected
  20295. * @since 5.5.000 (2010-06-22)
  20296. */
  20297. protected function replaceBuffer($data) {
  20298. $this->bufferlen = strlen($data);
  20299. $this->buffer = $data;
  20300. }
  20301. /**
  20302. * Get buffer content.
  20303. * @return string buffer content
  20304. * @protected
  20305. * @since 4.5.000 (2009-01-02)
  20306. */
  20307. protected function getBuffer() {
  20308. return $this->buffer;
  20309. }
  20310. /**
  20311. * Set page buffer content.
  20312. * @param int $page page number
  20313. * @param string $data page data
  20314. * @param boolean $append if true append data, false replace.
  20315. * @protected
  20316. * @since 4.5.000 (2008-12-31)
  20317. */
  20318. protected function setPageBuffer($page, $data, $append=false) {
  20319. if ($append) {
  20320. $this->pages[$page] .= $data;
  20321. } else {
  20322. $this->pages[$page] = $data;
  20323. }
  20324. if ($append AND isset($this->pagelen[$page])) {
  20325. $this->pagelen[$page] += strlen($data);
  20326. } else {
  20327. $this->pagelen[$page] = strlen($data);
  20328. }
  20329. }
  20330. /**
  20331. * Get page buffer content.
  20332. * @param int $page page number
  20333. * @return string page buffer content or false in case of error
  20334. * @protected
  20335. * @since 4.5.000 (2008-12-31)
  20336. */
  20337. protected function getPageBuffer($page) {
  20338. if (isset($this->pages[$page])) {
  20339. return $this->pages[$page];
  20340. }
  20341. return false;
  20342. }
  20343. /**
  20344. * Set image buffer content.
  20345. * @param string $image image key
  20346. * @param array $data image data
  20347. * @return int image index number
  20348. * @protected
  20349. * @since 4.5.000 (2008-12-31)
  20350. */
  20351. protected function setImageBuffer($image, $data) {
  20352. if (($data['i'] = array_search($image, $this->imagekeys)) === FALSE) {
  20353. $this->imagekeys[$this->numimages] = $image;
  20354. $data['i'] = $this->numimages;
  20355. ++$this->numimages;
  20356. }
  20357. $this->images[$image] = $data;
  20358. return $data['i'];
  20359. }
  20360. /**
  20361. * Set image buffer content for a specified sub-key.
  20362. * @param string $image image key
  20363. * @param string $key image sub-key
  20364. * @param array $data image data
  20365. * @protected
  20366. * @since 4.5.000 (2008-12-31)
  20367. */
  20368. protected function setImageSubBuffer($image, $key, $data) {
  20369. if (!isset($this->images[$image])) {
  20370. $this->setImageBuffer($image, array());
  20371. }
  20372. $this->images[$image][$key] = $data;
  20373. }
  20374. /**
  20375. * Get image buffer content.
  20376. * @param string $image image key
  20377. * @return string|false image buffer content or false in case of error
  20378. * @protected
  20379. * @since 4.5.000 (2008-12-31)
  20380. */
  20381. protected function getImageBuffer($image) {
  20382. if (isset($this->images[$image])) {
  20383. return $this->images[$image];
  20384. }
  20385. return false;
  20386. }
  20387. /**
  20388. * Set font buffer content.
  20389. * @param string $font font key
  20390. * @param array $data font data
  20391. * @protected
  20392. * @since 4.5.000 (2009-01-02)
  20393. */
  20394. protected function setFontBuffer($font, $data) {
  20395. $this->fonts[$font] = $data;
  20396. if (!in_array($font, $this->fontkeys)) {
  20397. $this->fontkeys[] = $font;
  20398. // store object ID for current font
  20399. ++$this->n;
  20400. $this->font_obj_ids[$font] = $this->n;
  20401. $this->setFontSubBuffer($font, 'n', $this->n);
  20402. }
  20403. }
  20404. /**
  20405. * Set font buffer content.
  20406. * @param string $font font key
  20407. * @param string $key font sub-key
  20408. * @param mixed $data font data
  20409. * @protected
  20410. * @since 4.5.000 (2009-01-02)
  20411. */
  20412. protected function setFontSubBuffer($font, $key, $data) {
  20413. if (!isset($this->fonts[$font])) {
  20414. $this->setFontBuffer($font, array());
  20415. }
  20416. $this->fonts[$font][$key] = $data;
  20417. }
  20418. /**
  20419. * Get font buffer content.
  20420. * @param string $font font key
  20421. * @return string|false font buffer content or false in case of error
  20422. * @protected
  20423. * @since 4.5.000 (2009-01-02)
  20424. */
  20425. protected function getFontBuffer($font) {
  20426. if (isset($this->fonts[$font])) {
  20427. return $this->fonts[$font];
  20428. }
  20429. return false;
  20430. }
  20431. /**
  20432. * Move a page to a previous position.
  20433. * @param int $frompage number of the source page
  20434. * @param int $topage number of the destination page (must be less than $frompage)
  20435. * @return bool true in case of success, false in case of error.
  20436. * @public
  20437. * @since 4.5.000 (2009-01-02)
  20438. */
  20439. public function movePage($frompage, $topage) {
  20440. if (($frompage > $this->numpages) OR ($frompage <= $topage)) {
  20441. return false;
  20442. }
  20443. if ($frompage == $this->page) {
  20444. // close the page before moving it
  20445. $this->endPage();
  20446. }
  20447. // move all page-related states
  20448. $tmppage = $this->getPageBuffer($frompage);
  20449. $tmppagedim = $this->pagedim[$frompage];
  20450. $tmppagelen = $this->pagelen[$frompage];
  20451. $tmpintmrk = $this->intmrk[$frompage];
  20452. $tmpbordermrk = $this->bordermrk[$frompage];
  20453. $tmpcntmrk = $this->cntmrk[$frompage];
  20454. $tmppageobjects = $this->pageobjects[$frompage];
  20455. if (isset($this->footerpos[$frompage])) {
  20456. $tmpfooterpos = $this->footerpos[$frompage];
  20457. }
  20458. if (isset($this->footerlen[$frompage])) {
  20459. $tmpfooterlen = $this->footerlen[$frompage];
  20460. }
  20461. if (isset($this->transfmrk[$frompage])) {
  20462. $tmptransfmrk = $this->transfmrk[$frompage];
  20463. }
  20464. if (isset($this->PageAnnots[$frompage])) {
  20465. $tmpannots = $this->PageAnnots[$frompage];
  20466. }
  20467. if (isset($this->newpagegroup) AND !empty($this->newpagegroup)) {
  20468. for ($i = $frompage; $i > $topage; --$i) {
  20469. if (isset($this->newpagegroup[$i]) AND (($i + $this->pagegroups[$this->newpagegroup[$i]]) > $frompage)) {
  20470. --$this->pagegroups[$this->newpagegroup[$i]];
  20471. break;
  20472. }
  20473. }
  20474. for ($i = $topage; $i > 0; --$i) {
  20475. if (isset($this->newpagegroup[$i]) AND (($i + $this->pagegroups[$this->newpagegroup[$i]]) > $topage)) {
  20476. ++$this->pagegroups[$this->newpagegroup[$i]];
  20477. break;
  20478. }
  20479. }
  20480. }
  20481. for ($i = $frompage; $i > $topage; --$i) {
  20482. $j = $i - 1;
  20483. // shift pages down
  20484. $this->setPageBuffer($i, $this->getPageBuffer($j));
  20485. $this->pagedim[$i] = $this->pagedim[$j];
  20486. $this->pagelen[$i] = $this->pagelen[$j];
  20487. $this->intmrk[$i] = $this->intmrk[$j];
  20488. $this->bordermrk[$i] = $this->bordermrk[$j];
  20489. $this->cntmrk[$i] = $this->cntmrk[$j];
  20490. $this->pageobjects[$i] = $this->pageobjects[$j];
  20491. if (isset($this->footerpos[$j])) {
  20492. $this->footerpos[$i] = $this->footerpos[$j];
  20493. } elseif (isset($this->footerpos[$i])) {
  20494. unset($this->footerpos[$i]);
  20495. }
  20496. if (isset($this->footerlen[$j])) {
  20497. $this->footerlen[$i] = $this->footerlen[$j];
  20498. } elseif (isset($this->footerlen[$i])) {
  20499. unset($this->footerlen[$i]);
  20500. }
  20501. if (isset($this->transfmrk[$j])) {
  20502. $this->transfmrk[$i] = $this->transfmrk[$j];
  20503. } elseif (isset($this->transfmrk[$i])) {
  20504. unset($this->transfmrk[$i]);
  20505. }
  20506. if (isset($this->PageAnnots[$j])) {
  20507. $this->PageAnnots[$i] = $this->PageAnnots[$j];
  20508. } elseif (isset($this->PageAnnots[$i])) {
  20509. unset($this->PageAnnots[$i]);
  20510. }
  20511. if (isset($this->newpagegroup[$j])) {
  20512. $this->newpagegroup[$i] = $this->newpagegroup[$j];
  20513. unset($this->newpagegroup[$j]);
  20514. }
  20515. if ($this->currpagegroup == $j) {
  20516. $this->currpagegroup = $i;
  20517. }
  20518. }
  20519. $this->setPageBuffer($topage, $tmppage);
  20520. $this->pagedim[$topage] = $tmppagedim;
  20521. $this->pagelen[$topage] = $tmppagelen;
  20522. $this->intmrk[$topage] = $tmpintmrk;
  20523. $this->bordermrk[$topage] = $tmpbordermrk;
  20524. $this->cntmrk[$topage] = $tmpcntmrk;
  20525. $this->pageobjects[$topage] = $tmppageobjects;
  20526. if (isset($tmpfooterpos)) {
  20527. $this->footerpos[$topage] = $tmpfooterpos;
  20528. } elseif (isset($this->footerpos[$topage])) {
  20529. unset($this->footerpos[$topage]);
  20530. }
  20531. if (isset($tmpfooterlen)) {
  20532. $this->footerlen[$topage] = $tmpfooterlen;
  20533. } elseif (isset($this->footerlen[$topage])) {
  20534. unset($this->footerlen[$topage]);
  20535. }
  20536. if (isset($tmptransfmrk)) {
  20537. $this->transfmrk[$topage] = $tmptransfmrk;
  20538. } elseif (isset($this->transfmrk[$topage])) {
  20539. unset($this->transfmrk[$topage]);
  20540. }
  20541. if (isset($tmpannots)) {
  20542. $this->PageAnnots[$topage] = $tmpannots;
  20543. } elseif (isset($this->PageAnnots[$topage])) {
  20544. unset($this->PageAnnots[$topage]);
  20545. }
  20546. // adjust outlines
  20547. $tmpoutlines = $this->outlines;
  20548. foreach ($tmpoutlines as $key => $outline) {
  20549. if (!$outline['f']) {
  20550. if (($outline['p'] >= $topage) AND ($outline['p'] < $frompage)) {
  20551. $this->outlines[$key]['p'] = ($outline['p'] + 1);
  20552. } elseif ($outline['p'] == $frompage) {
  20553. $this->outlines[$key]['p'] = $topage;
  20554. }
  20555. }
  20556. }
  20557. // adjust dests
  20558. $tmpdests = $this->dests;
  20559. foreach ($tmpdests as $key => $dest) {
  20560. if (!$dest['f']) {
  20561. if (($dest['p'] >= $topage) AND ($dest['p'] < $frompage)) {
  20562. $this->dests[$key]['p'] = ($dest['p'] + 1);
  20563. } elseif ($dest['p'] == $frompage) {
  20564. $this->dests[$key]['p'] = $topage;
  20565. }
  20566. }
  20567. }
  20568. // adjust links
  20569. $tmplinks = $this->links;
  20570. foreach ($tmplinks as $key => $link) {
  20571. if (!$link['f']) {
  20572. if (($link['p'] >= $topage) AND ($link['p'] < $frompage)) {
  20573. $this->links[$key]['p'] = ($link['p'] + 1);
  20574. } elseif ($link['p'] == $frompage) {
  20575. $this->links[$key]['p'] = $topage;
  20576. }
  20577. }
  20578. }
  20579. // adjust javascript
  20580. $jfrompage = $frompage;
  20581. $jtopage = $topage;
  20582. if (preg_match_all('/this\.addField\(\'([^\']*)\',\'([^\']*)\',([0-9]+)/', $this->javascript, $pamatch) > 0) {
  20583. foreach($pamatch[0] as $pk => $pmatch) {
  20584. $pagenum = intval($pamatch[3][$pk]) + 1;
  20585. if (($pagenum >= $jtopage) AND ($pagenum < $jfrompage)) {
  20586. $newpage = ($pagenum + 1);
  20587. } elseif ($pagenum == $jfrompage) {
  20588. $newpage = $jtopage;
  20589. } else {
  20590. $newpage = $pagenum;
  20591. }
  20592. --$newpage;
  20593. $newjs = "this.addField(\'".$pamatch[1][$pk]."\',\'".$pamatch[2][$pk]."\',".$newpage;
  20594. $this->javascript = str_replace($pmatch, $newjs, $this->javascript);
  20595. }
  20596. unset($pamatch);
  20597. }
  20598. // return to last page
  20599. $this->lastPage(true);
  20600. return true;
  20601. }
  20602. /**
  20603. * Remove the specified page.
  20604. * @param int $page page to remove
  20605. * @return bool true in case of success, false in case of error.
  20606. * @public
  20607. * @since 4.6.004 (2009-04-23)
  20608. */
  20609. public function deletePage($page) {
  20610. if (($page < 1) OR ($page > $this->numpages)) {
  20611. return false;
  20612. }
  20613. // delete current page
  20614. unset($this->pages[$page]);
  20615. unset($this->pagedim[$page]);
  20616. unset($this->pagelen[$page]);
  20617. unset($this->intmrk[$page]);
  20618. unset($this->bordermrk[$page]);
  20619. unset($this->cntmrk[$page]);
  20620. foreach ($this->pageobjects[$page] as $oid) {
  20621. if (isset($this->offsets[$oid])){
  20622. unset($this->offsets[$oid]);
  20623. }
  20624. }
  20625. unset($this->pageobjects[$page]);
  20626. if (isset($this->footerpos[$page])) {
  20627. unset($this->footerpos[$page]);
  20628. }
  20629. if (isset($this->footerlen[$page])) {
  20630. unset($this->footerlen[$page]);
  20631. }
  20632. if (isset($this->transfmrk[$page])) {
  20633. unset($this->transfmrk[$page]);
  20634. }
  20635. if (isset($this->PageAnnots[$page])) {
  20636. unset($this->PageAnnots[$page]);
  20637. }
  20638. if (isset($this->newpagegroup) AND !empty($this->newpagegroup)) {
  20639. for ($i = $page; $i > 0; --$i) {
  20640. if (isset($this->newpagegroup[$i]) AND (($i + $this->pagegroups[$this->newpagegroup[$i]]) > $page)) {
  20641. --$this->pagegroups[$this->newpagegroup[$i]];
  20642. break;
  20643. }
  20644. }
  20645. }
  20646. if (isset($this->pageopen[$page])) {
  20647. unset($this->pageopen[$page]);
  20648. }
  20649. if ($page < $this->numpages) {
  20650. // update remaining pages
  20651. for ($i = $page; $i < $this->numpages; ++$i) {
  20652. $j = $i + 1;
  20653. // shift pages
  20654. $this->setPageBuffer($i, $this->getPageBuffer($j));
  20655. $this->pagedim[$i] = $this->pagedim[$j];
  20656. $this->pagelen[$i] = $this->pagelen[$j];
  20657. $this->intmrk[$i] = $this->intmrk[$j];
  20658. $this->bordermrk[$i] = $this->bordermrk[$j];
  20659. $this->cntmrk[$i] = $this->cntmrk[$j];
  20660. $this->pageobjects[$i] = $this->pageobjects[$j];
  20661. if (isset($this->footerpos[$j])) {
  20662. $this->footerpos[$i] = $this->footerpos[$j];
  20663. } elseif (isset($this->footerpos[$i])) {
  20664. unset($this->footerpos[$i]);
  20665. }
  20666. if (isset($this->footerlen[$j])) {
  20667. $this->footerlen[$i] = $this->footerlen[$j];
  20668. } elseif (isset($this->footerlen[$i])) {
  20669. unset($this->footerlen[$i]);
  20670. }
  20671. if (isset($this->transfmrk[$j])) {
  20672. $this->transfmrk[$i] = $this->transfmrk[$j];
  20673. } elseif (isset($this->transfmrk[$i])) {
  20674. unset($this->transfmrk[$i]);
  20675. }
  20676. if (isset($this->PageAnnots[$j])) {
  20677. $this->PageAnnots[$i] = $this->PageAnnots[$j];
  20678. } elseif (isset($this->PageAnnots[$i])) {
  20679. unset($this->PageAnnots[$i]);
  20680. }
  20681. if (isset($this->newpagegroup[$j])) {
  20682. $this->newpagegroup[$i] = $this->newpagegroup[$j];
  20683. unset($this->newpagegroup[$j]);
  20684. }
  20685. if ($this->currpagegroup == $j) {
  20686. $this->currpagegroup = $i;
  20687. }
  20688. if (isset($this->pageopen[$j])) {
  20689. $this->pageopen[$i] = $this->pageopen[$j];
  20690. } elseif (isset($this->pageopen[$i])) {
  20691. unset($this->pageopen[$i]);
  20692. }
  20693. }
  20694. // remove last page
  20695. unset($this->pages[$this->numpages]);
  20696. unset($this->pagedim[$this->numpages]);
  20697. unset($this->pagelen[$this->numpages]);
  20698. unset($this->intmrk[$this->numpages]);
  20699. unset($this->bordermrk[$this->numpages]);
  20700. unset($this->cntmrk[$this->numpages]);
  20701. foreach ($this->pageobjects[$this->numpages] as $oid) {
  20702. if (isset($this->offsets[$oid])){
  20703. unset($this->offsets[$oid]);
  20704. }
  20705. }
  20706. unset($this->pageobjects[$this->numpages]);
  20707. if (isset($this->footerpos[$this->numpages])) {
  20708. unset($this->footerpos[$this->numpages]);
  20709. }
  20710. if (isset($this->footerlen[$this->numpages])) {
  20711. unset($this->footerlen[$this->numpages]);
  20712. }
  20713. if (isset($this->transfmrk[$this->numpages])) {
  20714. unset($this->transfmrk[$this->numpages]);
  20715. }
  20716. if (isset($this->PageAnnots[$this->numpages])) {
  20717. unset($this->PageAnnots[$this->numpages]);
  20718. }
  20719. if (isset($this->newpagegroup[$this->numpages])) {
  20720. unset($this->newpagegroup[$this->numpages]);
  20721. }
  20722. if ($this->currpagegroup == $this->numpages) {
  20723. $this->currpagegroup = ($this->numpages - 1);
  20724. }
  20725. if (isset($this->pagegroups[$this->numpages])) {
  20726. unset($this->pagegroups[$this->numpages]);
  20727. }
  20728. if (isset($this->pageopen[$this->numpages])) {
  20729. unset($this->pageopen[$this->numpages]);
  20730. }
  20731. }
  20732. --$this->numpages;
  20733. $this->page = $this->numpages;
  20734. // adjust outlines
  20735. $tmpoutlines = $this->outlines;
  20736. foreach ($tmpoutlines as $key => $outline) {
  20737. if (!$outline['f']) {
  20738. if ($outline['p'] > $page) {
  20739. $this->outlines[$key]['p'] = $outline['p'] - 1;
  20740. } elseif ($outline['p'] == $page) {
  20741. unset($this->outlines[$key]);
  20742. }
  20743. }
  20744. }
  20745. // adjust dests
  20746. $tmpdests = $this->dests;
  20747. foreach ($tmpdests as $key => $dest) {
  20748. if (!$dest['f']) {
  20749. if ($dest['p'] > $page) {
  20750. $this->dests[$key]['p'] = $dest['p'] - 1;
  20751. } elseif ($dest['p'] == $page) {
  20752. unset($this->dests[$key]);
  20753. }
  20754. }
  20755. }
  20756. // adjust links
  20757. $tmplinks = $this->links;
  20758. foreach ($tmplinks as $key => $link) {
  20759. if (!$link['f']) {
  20760. if ($link['p'] > $page) {
  20761. $this->links[$key]['p'] = $link['p'] - 1;
  20762. } elseif ($link['p'] == $page) {
  20763. unset($this->links[$key]);
  20764. }
  20765. }
  20766. }
  20767. // adjust javascript
  20768. $jpage = $page;
  20769. if (preg_match_all('/this\.addField\(\'([^\']*)\',\'([^\']*)\',([0-9]+)/', $this->javascript, $pamatch) > 0) {
  20770. foreach($pamatch[0] as $pk => $pmatch) {
  20771. $pagenum = intval($pamatch[3][$pk]) + 1;
  20772. if ($pagenum >= $jpage) {
  20773. $newpage = ($pagenum - 1);
  20774. } elseif ($pagenum == $jpage) {
  20775. $newpage = 1;
  20776. } else {
  20777. $newpage = $pagenum;
  20778. }
  20779. --$newpage;
  20780. $newjs = "this.addField(\'".$pamatch[1][$pk]."\',\'".$pamatch[2][$pk]."\',".$newpage;
  20781. $this->javascript = str_replace($pmatch, $newjs, $this->javascript);
  20782. }
  20783. unset($pamatch);
  20784. }
  20785. // return to last page
  20786. if ($this->numpages > 0) {
  20787. $this->lastPage(true);
  20788. }
  20789. return true;
  20790. }
  20791. /**
  20792. * Clone the specified page to a new page.
  20793. * @param int $page number of page to copy (0 = current page)
  20794. * @return bool true in case of success, false in case of error.
  20795. * @public
  20796. * @since 4.9.015 (2010-04-20)
  20797. */
  20798. public function copyPage($page=0) {
  20799. if ($page == 0) {
  20800. // default value
  20801. $page = $this->page;
  20802. }
  20803. if (($page < 1) OR ($page > $this->numpages)) {
  20804. return false;
  20805. }
  20806. // close the last page
  20807. $this->endPage();
  20808. // copy all page-related states
  20809. ++$this->numpages;
  20810. $this->page = $this->numpages;
  20811. $this->setPageBuffer($this->page, $this->getPageBuffer($page));
  20812. $this->pagedim[$this->page] = $this->pagedim[$page];
  20813. $this->pagelen[$this->page] = $this->pagelen[$page];
  20814. $this->intmrk[$this->page] = $this->intmrk[$page];
  20815. $this->bordermrk[$this->page] = $this->bordermrk[$page];
  20816. $this->cntmrk[$this->page] = $this->cntmrk[$page];
  20817. $this->pageobjects[$this->page] = $this->pageobjects[$page];
  20818. $this->pageopen[$this->page] = false;
  20819. if (isset($this->footerpos[$page])) {
  20820. $this->footerpos[$this->page] = $this->footerpos[$page];
  20821. }
  20822. if (isset($this->footerlen[$page])) {
  20823. $this->footerlen[$this->page] = $this->footerlen[$page];
  20824. }
  20825. if (isset($this->transfmrk[$page])) {
  20826. $this->transfmrk[$this->page] = $this->transfmrk[$page];
  20827. }
  20828. if (isset($this->PageAnnots[$page])) {
  20829. $this->PageAnnots[$this->page] = $this->PageAnnots[$page];
  20830. }
  20831. if (isset($this->newpagegroup[$page])) {
  20832. // start a new group
  20833. $this->newpagegroup[$this->page] = sizeof($this->newpagegroup) + 1;
  20834. $this->currpagegroup = $this->newpagegroup[$this->page];
  20835. $this->pagegroups[$this->currpagegroup] = 1;
  20836. } elseif (isset($this->currpagegroup) AND ($this->currpagegroup > 0)) {
  20837. ++$this->pagegroups[$this->currpagegroup];
  20838. }
  20839. // copy outlines
  20840. $tmpoutlines = $this->outlines;
  20841. foreach ($tmpoutlines as $key => $outline) {
  20842. if ($outline['p'] == $page) {
  20843. $this->outlines[] = array('t' => $outline['t'], 'l' => $outline['l'], 'x' => $outline['x'], 'y' => $outline['y'], 'p' => $this->page, 'f' => $outline['f'], 's' => $outline['s'], 'c' => $outline['c']);
  20844. }
  20845. }
  20846. // copy links
  20847. $tmplinks = $this->links;
  20848. foreach ($tmplinks as $key => $link) {
  20849. if ($link['p'] == $page) {
  20850. $this->links[] = array('p' => $this->page, 'y' => $link['y'], 'f' => $link['f']);
  20851. }
  20852. }
  20853. // return to last page
  20854. $this->lastPage(true);
  20855. return true;
  20856. }
  20857. /**
  20858. * Output a Table of Content Index (TOC).
  20859. * This method must be called after all Bookmarks were set.
  20860. * Before calling this method you have to open the page using the addTOCPage() method.
  20861. * After calling this method you have to call endTOCPage() to close the TOC page.
  20862. * You can override this method to achieve different styles.
  20863. * @param int|null $page page number where this TOC should be inserted (leave empty for current page).
  20864. * @param string $numbersfont set the font for page numbers (please use monospaced font for better alignment).
  20865. * @param string $filler string used to fill the space between text and page number.
  20866. * @param string $toc_name name to use for TOC bookmark.
  20867. * @param string $style Font style for title: B = Bold, I = Italic, BI = Bold + Italic.
  20868. * @param array $color RGB color array for bookmark title (values from 0 to 255).
  20869. * @public
  20870. * @author Nicola Asuni
  20871. * @since 4.5.000 (2009-01-02)
  20872. * @see addTOCPage(), endTOCPage(), addHTMLTOC()
  20873. */
  20874. public function addTOC($page=null, $numbersfont='', $filler='.', $toc_name='TOC', $style='', $color=array(0,0,0)) {
  20875. $fontsize = $this->FontSizePt;
  20876. $fontfamily = $this->FontFamily;
  20877. $fontstyle = $this->FontStyle;
  20878. $w = $this->w - $this->lMargin - $this->rMargin;
  20879. $spacer = $this->GetStringWidth(chr(32)) * 4;
  20880. $lmargin = $this->lMargin;
  20881. $rmargin = $this->rMargin;
  20882. $x_start = $this->GetX();
  20883. $page_first = $this->page;
  20884. $current_page = $this->page;
  20885. $page_fill_start = false;
  20886. $page_fill_end = false;
  20887. $current_column = $this->current_column;
  20888. if (TCPDF_STATIC::empty_string($numbersfont)) {
  20889. $numbersfont = $this->default_monospaced_font;
  20890. }
  20891. if (TCPDF_STATIC::empty_string($filler)) {
  20892. $filler = ' ';
  20893. }
  20894. if (TCPDF_STATIC::empty_string($page)) {
  20895. $gap = ' ';
  20896. } else {
  20897. $gap = '';
  20898. if ($page < 1) {
  20899. $page = 1;
  20900. }
  20901. }
  20902. $this->setFont($numbersfont, $fontstyle, $fontsize);
  20903. $numwidth = $this->GetStringWidth('00000');
  20904. $maxpage = 0; //used for pages on attached documents
  20905. foreach ($this->outlines as $key => $outline) {
  20906. // check for extra pages (used for attachments)
  20907. if (($this->page > $page_first) AND ($outline['p'] >= $this->numpages)) {
  20908. $outline['p'] += ($this->page - $page_first);
  20909. }
  20910. if ($this->rtl) {
  20911. $aligntext = 'R';
  20912. $alignnum = 'L';
  20913. } else {
  20914. $aligntext = 'L';
  20915. $alignnum = 'R';
  20916. }
  20917. if ($outline['l'] == 0) {
  20918. $this->setFont($fontfamily, $outline['s'].'B', $fontsize);
  20919. } else {
  20920. $this->setFont($fontfamily, $outline['s'], $fontsize - $outline['l']);
  20921. }
  20922. $this->setTextColorArray($outline['c']);
  20923. // check for page break
  20924. $this->checkPageBreak(2 * $this->getCellHeight($this->FontSize));
  20925. // set margins and X position
  20926. if (($this->page == $current_page) AND ($this->current_column == $current_column)) {
  20927. $this->lMargin = $lmargin;
  20928. $this->rMargin = $rmargin;
  20929. } else {
  20930. if ($this->current_column != $current_column) {
  20931. if ($this->rtl) {
  20932. $x_start = $this->w - $this->columns[$this->current_column]['x'];
  20933. } else {
  20934. $x_start = $this->columns[$this->current_column]['x'];
  20935. }
  20936. }
  20937. $lmargin = $this->lMargin;
  20938. $rmargin = $this->rMargin;
  20939. $current_page = $this->page;
  20940. $current_column = $this->current_column;
  20941. }
  20942. $this->setX($x_start);
  20943. $indent = ($spacer * $outline['l']);
  20944. if ($this->rtl) {
  20945. $this->x -= $indent;
  20946. $this->rMargin = $this->w - $this->x;
  20947. } else {
  20948. $this->x += $indent;
  20949. $this->lMargin = $this->x;
  20950. }
  20951. $link = $this->AddLink();
  20952. $this->setLink($link, $outline['y'], $outline['p']);
  20953. // write the text
  20954. if ($this->rtl) {
  20955. $txt = ' '.$outline['t'];
  20956. } else {
  20957. $txt = $outline['t'].' ';
  20958. }
  20959. $this->Write(0, $txt, $link, false, $aligntext, false, 0, false, false, 0, $numwidth, '');
  20960. if ($this->rtl) {
  20961. $tw = $this->x - $this->lMargin;
  20962. } else {
  20963. $tw = $this->w - $this->rMargin - $this->x;
  20964. }
  20965. $this->setFont($numbersfont, $fontstyle, $fontsize);
  20966. if (TCPDF_STATIC::empty_string($page)) {
  20967. $pagenum = $outline['p'];
  20968. } else {
  20969. // placemark to be replaced with the correct number
  20970. $pagenum = '{#'.($outline['p']).'}';
  20971. if ($this->isUnicodeFont()) {
  20972. $pagenum = '{'.$pagenum.'}';
  20973. }
  20974. $maxpage = max($maxpage, $outline['p']);
  20975. }
  20976. $fw = ($tw - $this->GetStringWidth($pagenum.$filler));
  20977. $wfiller = $this->GetStringWidth($filler);
  20978. if ($wfiller > 0) {
  20979. $numfills = floor($fw / $wfiller);
  20980. } else {
  20981. $numfills = 0;
  20982. }
  20983. if ($numfills > 0) {
  20984. $rowfill = str_repeat($filler, $numfills);
  20985. } else {
  20986. $rowfill = '';
  20987. }
  20988. if ($this->rtl) {
  20989. $pagenum = $pagenum.$gap.$rowfill;
  20990. } else {
  20991. $pagenum = $rowfill.$gap.$pagenum;
  20992. }
  20993. // write the number
  20994. $this->Cell($tw, 0, $pagenum, 0, 1, $alignnum, 0, $link, 0);
  20995. }
  20996. $page_last = $this->getPage();
  20997. $numpages = ($page_last - $page_first + 1);
  20998. // account for booklet mode
  20999. if ($this->booklet) {
  21000. // check if a blank page is required before TOC
  21001. $page_fill_start = ((($page_first % 2) == 0) XOR (($page % 2) == 0));
  21002. $page_fill_end = (!((($numpages % 2) == 0) XOR ($page_fill_start)));
  21003. if ($page_fill_start) {
  21004. // add a page at the end (to be moved before TOC)
  21005. $this->addPage();
  21006. ++$page_last;
  21007. ++$numpages;
  21008. }
  21009. if ($page_fill_end) {
  21010. // add a page at the end
  21011. $this->addPage();
  21012. ++$page_last;
  21013. ++$numpages;
  21014. }
  21015. }
  21016. $maxpage = max($maxpage, $page_last);
  21017. if (!TCPDF_STATIC::empty_string($page)) {
  21018. for ($p = $page_first; $p <= $page_last; ++$p) {
  21019. // get page data
  21020. $temppage = $this->getPageBuffer($p);
  21021. for ($n = 1; $n <= $maxpage; ++$n) {
  21022. // update page numbers
  21023. $a = '{#'.$n.'}';
  21024. // get page number aliases
  21025. $pnalias = $this->getInternalPageNumberAliases($a);
  21026. // calculate replacement number
  21027. if (($n >= $page) AND ($n <= $this->numpages)) {
  21028. $np = $n + $numpages;
  21029. } else {
  21030. $np = $n;
  21031. }
  21032. $na = TCPDF_STATIC::formatTOCPageNumber(($this->starting_page_number + $np - 1));
  21033. $nu = TCPDF_FONTS::UTF8ToUTF16BE($na, false, $this->isunicode, $this->CurrentFont);
  21034. // replace aliases with numbers
  21035. foreach ($pnalias['u'] as $u) {
  21036. $sfill = str_repeat($filler, max(0, (strlen($u) - strlen($nu.' '))));
  21037. if ($this->rtl) {
  21038. $nr = $nu.TCPDF_FONTS::UTF8ToUTF16BE(' '.$sfill, false, $this->isunicode, $this->CurrentFont);
  21039. } else {
  21040. $nr = TCPDF_FONTS::UTF8ToUTF16BE($sfill.' ', false, $this->isunicode, $this->CurrentFont).$nu;
  21041. }
  21042. $temppage = str_replace($u, $nr, $temppage);
  21043. }
  21044. foreach ($pnalias['a'] as $a) {
  21045. $sfill = str_repeat($filler, max(0, (strlen($a) - strlen($na.' '))));
  21046. if ($this->rtl) {
  21047. $nr = $na.' '.$sfill;
  21048. } else {
  21049. $nr = $sfill.' '.$na;
  21050. }
  21051. $temppage = str_replace($a, $nr, $temppage);
  21052. }
  21053. }
  21054. // save changes
  21055. $this->setPageBuffer($p, $temppage);
  21056. }
  21057. // move pages
  21058. $this->Bookmark($toc_name, 0, 0, $page_first, $style, $color);
  21059. if ($page_fill_start) {
  21060. $this->movePage($page_last, $page_first);
  21061. }
  21062. for ($i = 0; $i < $numpages; ++$i) {
  21063. $this->movePage($page_last, $page);
  21064. }
  21065. }
  21066. }
  21067. /**
  21068. * Output a Table Of Content Index (TOC) using HTML templates.
  21069. * This method must be called after all Bookmarks were set.
  21070. * Before calling this method you have to open the page using the addTOCPage() method.
  21071. * After calling this method you have to call endTOCPage() to close the TOC page.
  21072. * @param int|null $page page number where this TOC should be inserted (leave empty for current page).
  21073. * @param string $toc_name name to use for TOC bookmark.
  21074. * @param array $templates array of html templates. Use: "#TOC_DESCRIPTION#" for bookmark title, "#TOC_PAGE_NUMBER#" for page number.
  21075. * @param boolean $correct_align if true correct the number alignment (numbers must be in monospaced font like courier and right aligned on LTR, or left aligned on RTL)
  21076. * @param string $style Font style for title: B = Bold, I = Italic, BI = Bold + Italic.
  21077. * @param array $color RGB color array for title (values from 0 to 255).
  21078. * @public
  21079. * @author Nicola Asuni
  21080. * @since 5.0.001 (2010-05-06)
  21081. * @see addTOCPage(), endTOCPage(), addTOC()
  21082. */
  21083. public function addHTMLTOC($page=null, $toc_name='TOC', $templates=array(), $correct_align=true, $style='', $color=array(0,0,0)) {
  21084. $filler = ' ';
  21085. $prev_htmlLinkColorArray = $this->htmlLinkColorArray;
  21086. $prev_htmlLinkFontStyle = $this->htmlLinkFontStyle;
  21087. // set new style for link
  21088. $this->htmlLinkColorArray = array();
  21089. $this->htmlLinkFontStyle = '';
  21090. $page_first = $this->getPage();
  21091. $page_fill_start = false;
  21092. $page_fill_end = false;
  21093. // get the font type used for numbers in each template
  21094. $current_font = $this->FontFamily;
  21095. foreach ($templates as $level => $html) {
  21096. $dom = $this->getHtmlDomArray($html);
  21097. foreach ($dom as $key => $value) {
  21098. if ($value['value'] == '#TOC_PAGE_NUMBER#') {
  21099. $this->setFont($dom[($key - 1)]['fontname']);
  21100. $templates['F'.$level] = $this->isUnicodeFont();
  21101. }
  21102. }
  21103. }
  21104. $this->setFont($current_font);
  21105. $maxpage = 0; //used for pages on attached documents
  21106. foreach ($this->outlines as $key => $outline) {
  21107. // get HTML template
  21108. $row = $templates[$outline['l']];
  21109. if (TCPDF_STATIC::empty_string($page)) {
  21110. $pagenum = $outline['p'];
  21111. } else {
  21112. // placemark to be replaced with the correct number
  21113. $pagenum = '{#'.($outline['p']).'}';
  21114. if (isset($templates['F'.$outline['l']]) && $templates['F'.$outline['l']]) {
  21115. $pagenum = '{'.$pagenum.'}';
  21116. }
  21117. $maxpage = max($maxpage, $outline['p']);
  21118. }
  21119. // replace templates with current values
  21120. $row = str_replace('#TOC_DESCRIPTION#', $outline['t'], $row);
  21121. $row = str_replace('#TOC_PAGE_NUMBER#', $pagenum, $row);
  21122. // add link to page
  21123. $row = '<a href="#'.$outline['p'].','.$outline['y'].'">'.$row.'</a>';
  21124. // write bookmark entry
  21125. $this->writeHTML($row, false, false, true, false, '');
  21126. }
  21127. // restore link styles
  21128. $this->htmlLinkColorArray = $prev_htmlLinkColorArray;
  21129. $this->htmlLinkFontStyle = $prev_htmlLinkFontStyle;
  21130. // move TOC page and replace numbers
  21131. $page_last = $this->getPage();
  21132. $numpages = ($page_last - $page_first + 1);
  21133. // account for booklet mode
  21134. if ($this->booklet) {
  21135. // check if a blank page is required before TOC
  21136. $page_fill_start = ((($page_first % 2) == 0) XOR (($page % 2) == 0));
  21137. $page_fill_end = (!((($numpages % 2) == 0) XOR ($page_fill_start)));
  21138. if ($page_fill_start) {
  21139. // add a page at the end (to be moved before TOC)
  21140. $this->addPage();
  21141. ++$page_last;
  21142. ++$numpages;
  21143. }
  21144. if ($page_fill_end) {
  21145. // add a page at the end
  21146. $this->addPage();
  21147. ++$page_last;
  21148. ++$numpages;
  21149. }
  21150. }
  21151. $maxpage = max($maxpage, $page_last);
  21152. if (!TCPDF_STATIC::empty_string($page)) {
  21153. for ($p = $page_first; $p <= $page_last; ++$p) {
  21154. // get page data
  21155. $temppage = $this->getPageBuffer($p);
  21156. for ($n = 1; $n <= $maxpage; ++$n) {
  21157. // update page numbers
  21158. $a = '{#'.$n.'}';
  21159. // get page number aliases
  21160. $pnalias = $this->getInternalPageNumberAliases($a);
  21161. // calculate replacement number
  21162. if ($n >= $page) {
  21163. $np = $n + $numpages;
  21164. } else {
  21165. $np = $n;
  21166. }
  21167. $na = TCPDF_STATIC::formatTOCPageNumber(($this->starting_page_number + $np - 1));
  21168. $nu = TCPDF_FONTS::UTF8ToUTF16BE($na, false, $this->isunicode, $this->CurrentFont);
  21169. // replace aliases with numbers
  21170. foreach ($pnalias['u'] as $u) {
  21171. if ($correct_align) {
  21172. $sfill = str_repeat($filler, (strlen($u) - strlen($nu.' ')));
  21173. if ($this->rtl) {
  21174. $nr = $nu.TCPDF_FONTS::UTF8ToUTF16BE(' '.$sfill, false, $this->isunicode, $this->CurrentFont);
  21175. } else {
  21176. $nr = TCPDF_FONTS::UTF8ToUTF16BE($sfill.' ', false, $this->isunicode, $this->CurrentFont).$nu;
  21177. }
  21178. } else {
  21179. $nr = $nu;
  21180. }
  21181. $temppage = str_replace($u, $nr, $temppage);
  21182. }
  21183. foreach ($pnalias['a'] as $a) {
  21184. if ($correct_align) {
  21185. $sfill = str_repeat($filler, (strlen($a) - strlen($na.' ')));
  21186. if ($this->rtl) {
  21187. $nr = $na.' '.$sfill;
  21188. } else {
  21189. $nr = $sfill.' '.$na;
  21190. }
  21191. } else {
  21192. $nr = $na;
  21193. }
  21194. $temppage = str_replace($a, $nr, $temppage);
  21195. }
  21196. }
  21197. // save changes
  21198. $this->setPageBuffer($p, $temppage);
  21199. }
  21200. // move pages
  21201. $this->Bookmark($toc_name, 0, 0, $page_first, $style, $color);
  21202. if ($page_fill_start) {
  21203. $this->movePage($page_last, $page_first);
  21204. }
  21205. for ($i = 0; $i < $numpages; ++$i) {
  21206. $this->movePage($page_last, $page);
  21207. }
  21208. }
  21209. }
  21210. /**
  21211. * Stores a copy of the current TCPDF object used for undo operation.
  21212. * @public
  21213. * @since 4.5.029 (2009-03-19)
  21214. */
  21215. public function startTransaction() {
  21216. if (isset($this->objcopy)) {
  21217. // remove previous copy
  21218. $this->commitTransaction();
  21219. }
  21220. // record current page number and Y position
  21221. $this->start_transaction_page = $this->page;
  21222. $this->start_transaction_y = $this->y;
  21223. // clone current object
  21224. $this->objcopy = TCPDF_STATIC::objclone($this);
  21225. }
  21226. /**
  21227. * Delete the copy of the current TCPDF object used for undo operation.
  21228. * @public
  21229. * @since 4.5.029 (2009-03-19)
  21230. */
  21231. public function commitTransaction() {
  21232. if (isset($this->objcopy)) {
  21233. $this->objcopy->_destroy(true, true);
  21234. /* The unique file_id should not be used during cleanup again */
  21235. $this->objcopy->file_id = NULL;
  21236. unset($this->objcopy);
  21237. }
  21238. }
  21239. /**
  21240. * This method allows to undo the latest transaction by returning the latest saved TCPDF object with startTransaction().
  21241. * @param boolean $self if true restores current class object to previous state without the need of reassignment via the returned value.
  21242. * @return TCPDF object.
  21243. * @public
  21244. * @since 4.5.029 (2009-03-19)
  21245. */
  21246. public function rollbackTransaction($self=false) {
  21247. if (!isset($this->objcopy)) {
  21248. return $this;
  21249. }
  21250. $file_id = $this->file_id;
  21251. $objcopy = $this->objcopy;
  21252. $this->_destroy(true, true);
  21253. if ($self) {
  21254. $objvars = get_object_vars($objcopy);
  21255. foreach ($objvars as $key => $value) {
  21256. $this->$key = $value;
  21257. }
  21258. $objcopy->_destroy(true, true);
  21259. unset($objcopy);
  21260. return $this;
  21261. }
  21262. $this->file_id = $file_id;
  21263. return $objcopy;
  21264. }
  21265. // --- MULTI COLUMNS METHODS -----------------------
  21266. /**
  21267. * Set multiple columns of the same size
  21268. * @param int $numcols number of columns (set to zero to disable columns mode)
  21269. * @param int $width column width
  21270. * @param int|null $y column starting Y position (leave empty for current Y position)
  21271. * @public
  21272. * @since 4.9.001 (2010-03-28)
  21273. */
  21274. public function setEqualColumns($numcols=0, $width=0, $y=null) {
  21275. $this->columns = array();
  21276. if ($numcols < 2) {
  21277. $numcols = 0;
  21278. $this->columns = array();
  21279. } else {
  21280. // maximum column width
  21281. $maxwidth = ($this->w - $this->original_lMargin - $this->original_rMargin) / $numcols;
  21282. if (($width == 0) OR ($width > $maxwidth)) {
  21283. $width = $maxwidth;
  21284. }
  21285. if (TCPDF_STATIC::empty_string($y)) {
  21286. $y = $this->y;
  21287. }
  21288. // space between columns
  21289. $space = (($this->w - $this->original_lMargin - $this->original_rMargin - ($numcols * $width)) / ($numcols - 1));
  21290. // fill the columns array (with, space, starting Y position)
  21291. for ($i = 0; $i < $numcols; ++$i) {
  21292. $this->columns[$i] = array('w' => $width, 's' => $space, 'y' => $y);
  21293. }
  21294. }
  21295. $this->num_columns = $numcols;
  21296. $this->current_column = 0;
  21297. $this->column_start_page = $this->page;
  21298. $this->selectColumn(0);
  21299. }
  21300. /**
  21301. * Remove columns and reset page margins.
  21302. * @public
  21303. * @since 5.9.072 (2011-04-26)
  21304. */
  21305. public function resetColumns() {
  21306. $this->lMargin = $this->original_lMargin;
  21307. $this->rMargin = $this->original_rMargin;
  21308. $this->setEqualColumns();
  21309. }
  21310. /**
  21311. * Set columns array.
  21312. * Each column is represented by an array of arrays with the following keys: (w = width, s = space between columns, y = column top position).
  21313. * @param array $columns
  21314. * @public
  21315. * @since 4.9.001 (2010-03-28)
  21316. */
  21317. public function setColumnsArray($columns) {
  21318. $this->columns = $columns;
  21319. $this->num_columns = count($columns);
  21320. $this->current_column = 0;
  21321. $this->column_start_page = $this->page;
  21322. $this->selectColumn(0);
  21323. }
  21324. /**
  21325. * Set position at a given column
  21326. * @param int|null $col column number (from 0 to getNumberOfColumns()-1); empty string = current column.
  21327. * @public
  21328. * @since 4.9.001 (2010-03-28)
  21329. */
  21330. public function selectColumn($col=null) {
  21331. if (TCPDF_STATIC::empty_string($col)) {
  21332. $col = $this->current_column;
  21333. } elseif ($col >= $this->num_columns) {
  21334. $col = 0;
  21335. }
  21336. $xshift = array('x' => 0, 's' => array('H' => 0, 'V' => 0), 'p' => array('L' => 0, 'T' => 0, 'R' => 0, 'B' => 0));
  21337. $enable_thead = false;
  21338. if ($this->num_columns > 1) {
  21339. if ($col != $this->current_column) {
  21340. // move Y pointer at the top of the column
  21341. if ($this->column_start_page == $this->page) {
  21342. $this->y = $this->columns[$col]['y'];
  21343. } else {
  21344. $this->y = $this->tMargin;
  21345. }
  21346. // Avoid to write table headers more than once
  21347. if (($this->page > $this->maxselcol['page']) OR (($this->page == $this->maxselcol['page']) AND ($col > $this->maxselcol['column']))) {
  21348. $enable_thead = true;
  21349. $this->maxselcol['page'] = $this->page;
  21350. $this->maxselcol['column'] = $col;
  21351. }
  21352. }
  21353. $xshift = $this->colxshift;
  21354. // set X position of the current column by case
  21355. $listindent = ($this->listindentlevel * $this->listindent);
  21356. // calculate column X position
  21357. $colpos = 0;
  21358. for ($i = 0; $i < $col; ++$i) {
  21359. $colpos += ($this->columns[$i]['w'] + $this->columns[$i]['s']);
  21360. }
  21361. if ($this->rtl) {
  21362. $x = $this->w - $this->original_rMargin - $colpos;
  21363. $this->rMargin = ($this->w - $x + $listindent);
  21364. $this->lMargin = ($x - $this->columns[$col]['w']);
  21365. $this->x = $x - $listindent;
  21366. } else {
  21367. $x = $this->original_lMargin + $colpos;
  21368. $this->lMargin = ($x + $listindent);
  21369. $this->rMargin = ($this->w - $x - $this->columns[$col]['w']);
  21370. $this->x = $x + $listindent;
  21371. }
  21372. $this->columns[$col]['x'] = $x;
  21373. }
  21374. $this->current_column = $col;
  21375. // fix for HTML mode
  21376. $this->newline = true;
  21377. // print HTML table header (if any)
  21378. if ((!TCPDF_STATIC::empty_string($this->thead)) AND (!$this->inthead)) {
  21379. if ($enable_thead) {
  21380. // print table header
  21381. $this->writeHTML($this->thead, false, false, false, false, '');
  21382. $this->y += $xshift['s']['V'];
  21383. // store end of header position
  21384. if (!isset($this->columns[$col]['th'])) {
  21385. $this->columns[$col]['th'] = array();
  21386. }
  21387. $this->columns[$col]['th']['\''.$this->page.'\''] = $this->y;
  21388. $this->lasth = 0;
  21389. } elseif (isset($this->columns[$col]['th']['\''.$this->page.'\''])) {
  21390. $this->y = $this->columns[$col]['th']['\''.$this->page.'\''];
  21391. }
  21392. }
  21393. // account for an html table cell over multiple columns
  21394. if ($this->rtl) {
  21395. $this->rMargin += $xshift['x'];
  21396. $this->x -= ($xshift['x'] + $xshift['p']['R']);
  21397. } else {
  21398. $this->lMargin += $xshift['x'];
  21399. $this->x += $xshift['x'] + $xshift['p']['L'];
  21400. }
  21401. }
  21402. /**
  21403. * Return the current column number
  21404. * @return int current column number
  21405. * @public
  21406. * @since 5.5.011 (2010-07-08)
  21407. */
  21408. public function getColumn() {
  21409. return $this->current_column;
  21410. }
  21411. /**
  21412. * Return the current number of columns.
  21413. * @return int number of columns
  21414. * @public
  21415. * @since 5.8.018 (2010-08-25)
  21416. */
  21417. public function getNumberOfColumns() {
  21418. return $this->num_columns;
  21419. }
  21420. /**
  21421. * Set Text rendering mode.
  21422. * @param int $stroke outline size in user units (0 = disable).
  21423. * @param boolean $fill if true fills the text (default).
  21424. * @param boolean $clip if true activate clipping mode
  21425. * @public
  21426. * @since 4.9.008 (2009-04-02)
  21427. */
  21428. public function setTextRenderingMode($stroke=0, $fill=true, $clip=false) {
  21429. // Ref.: PDF 32000-1:2008 - 9.3.6 Text Rendering Mode
  21430. // convert text rendering parameters
  21431. if ($stroke < 0 || !is_numeric($stroke)) {
  21432. $stroke = 0;
  21433. }
  21434. if ($fill === true) {
  21435. if ($stroke > 0) {
  21436. if ($clip === true) {
  21437. // Fill, then stroke text and add to path for clipping
  21438. $textrendermode = 6;
  21439. } else {
  21440. // Fill, then stroke text
  21441. $textrendermode = 2;
  21442. }
  21443. $textstrokewidth = $stroke;
  21444. } else {
  21445. if ($clip === true) {
  21446. // Fill text and add to path for clipping
  21447. $textrendermode = 4;
  21448. } else {
  21449. // Fill text
  21450. $textrendermode = 0;
  21451. }
  21452. }
  21453. } else {
  21454. if ($stroke > 0) {
  21455. if ($clip === true) {
  21456. // Stroke text and add to path for clipping
  21457. $textrendermode = 5;
  21458. } else {
  21459. // Stroke text
  21460. $textrendermode = 1;
  21461. }
  21462. $textstrokewidth = $stroke;
  21463. } else {
  21464. if ($clip === true) {
  21465. // Add text to path for clipping
  21466. $textrendermode = 7;
  21467. } else {
  21468. // Neither fill nor stroke text (invisible)
  21469. $textrendermode = 3;
  21470. }
  21471. }
  21472. }
  21473. $this->textrendermode = $textrendermode;
  21474. $this->textstrokewidth = $stroke;
  21475. }
  21476. /**
  21477. * Set parameters for drop shadow effect for text.
  21478. * @param array $params Array of parameters: enabled (boolean) set to true to enable shadow; depth_w (float) shadow width in user units; depth_h (float) shadow height in user units; color (array) shadow color or false to use the stroke color; opacity (float) Alpha value: real value from 0 (transparent) to 1 (opaque); blend_mode (string) blend mode, one of the following: Normal, Multiply, Screen, Overlay, Darken, Lighten, ColorDodge, ColorBurn, HardLight, SoftLight, Difference, Exclusion, Hue, Saturation, Color, Luminosity.
  21479. * @since 5.9.174 (2012-07-25)
  21480. * @public
  21481. */
  21482. public function setTextShadow($params=array('enabled'=>false, 'depth_w'=>0, 'depth_h'=>0, 'color'=>false, 'opacity'=>1, 'blend_mode'=>'Normal')) {
  21483. if (isset($params['enabled'])) {
  21484. $this->txtshadow['enabled'] = $params['enabled']?true:false;
  21485. } else {
  21486. $this->txtshadow['enabled'] = false;
  21487. }
  21488. if (isset($params['depth_w'])) {
  21489. $this->txtshadow['depth_w'] = floatval($params['depth_w']);
  21490. } else {
  21491. $this->txtshadow['depth_w'] = 0;
  21492. }
  21493. if (isset($params['depth_h'])) {
  21494. $this->txtshadow['depth_h'] = floatval($params['depth_h']);
  21495. } else {
  21496. $this->txtshadow['depth_h'] = 0;
  21497. }
  21498. if (isset($params['color']) AND ($params['color'] !== false) AND is_array($params['color'])) {
  21499. $this->txtshadow['color'] = $params['color'];
  21500. } else {
  21501. $this->txtshadow['color'] = $this->strokecolor;
  21502. }
  21503. if (isset($params['opacity'])) {
  21504. $this->txtshadow['opacity'] = min(1, max(0, floatval($params['opacity'])));
  21505. } else {
  21506. $this->txtshadow['opacity'] = 1;
  21507. }
  21508. if (isset($params['blend_mode']) AND in_array($params['blend_mode'], array('Normal', 'Multiply', 'Screen', 'Overlay', 'Darken', 'Lighten', 'ColorDodge', 'ColorBurn', 'HardLight', 'SoftLight', 'Difference', 'Exclusion', 'Hue', 'Saturation', 'Color', 'Luminosity'))) {
  21509. $this->txtshadow['blend_mode'] = $params['blend_mode'];
  21510. } else {
  21511. $this->txtshadow['blend_mode'] = 'Normal';
  21512. }
  21513. if ((($this->txtshadow['depth_w'] == 0) AND ($this->txtshadow['depth_h'] == 0)) OR ($this->txtshadow['opacity'] == 0)) {
  21514. $this->txtshadow['enabled'] = false;
  21515. }
  21516. }
  21517. /**
  21518. * Return the text shadow parameters array.
  21519. * @return array array of parameters.
  21520. * @since 5.9.174 (2012-07-25)
  21521. * @public
  21522. */
  21523. public function getTextShadow() {
  21524. return $this->txtshadow;
  21525. }
  21526. /**
  21527. * Returns an array of chars containing soft hyphens.
  21528. * @param array $word array of chars
  21529. * @param array $patterns Array of hypenation patterns.
  21530. * @param array $dictionary Array of words to be returned without applying the hyphenation algorithm.
  21531. * @param int $leftmin Minimum number of character to leave on the left of the word without applying the hyphens.
  21532. * @param int $rightmin Minimum number of character to leave on the right of the word without applying the hyphens.
  21533. * @param int $charmin Minimum word length to apply the hyphenation algorithm.
  21534. * @param int $charmax Maximum length of broken piece of word.
  21535. * @return array text with soft hyphens
  21536. * @author Nicola Asuni
  21537. * @since 4.9.012 (2010-04-12)
  21538. * @protected
  21539. */
  21540. protected function hyphenateWord($word, $patterns, $dictionary=array(), $leftmin=1, $rightmin=2, $charmin=1, $charmax=8) {
  21541. $hyphenword = array(); // hyphens positions
  21542. $numchars = count($word);
  21543. if ($numchars <= $charmin) {
  21544. return $word;
  21545. }
  21546. $word_string = TCPDF_FONTS::UTF8ArrSubString($word, '', '', $this->isunicode);
  21547. // some words will be returned as-is
  21548. $pattern = '/^([a-zA-Z0-9_\.\-]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/';
  21549. if (preg_match($pattern, $word_string) > 0) {
  21550. // email
  21551. return $word;
  21552. }
  21553. $pattern = '/(([a-zA-Z0-9\-]+\.)?)((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/';
  21554. if (preg_match($pattern, $word_string) > 0) {
  21555. // URL
  21556. return $word;
  21557. }
  21558. if (isset($dictionary[$word_string])) {
  21559. return TCPDF_FONTS::UTF8StringToArray($dictionary[$word_string], $this->isunicode, $this->CurrentFont);
  21560. }
  21561. // surround word with '_' characters
  21562. $tmpword = array_merge(array(46), $word, array(46));
  21563. $tmpnumchars = $numchars + 2;
  21564. $maxpos = $tmpnumchars - 1;
  21565. for ($pos = 0; $pos < $maxpos; ++$pos) {
  21566. $imax = min(($tmpnumchars - $pos), $charmax);
  21567. for ($i = 1; $i <= $imax; ++$i) {
  21568. $subword = strtolower(TCPDF_FONTS::UTF8ArrSubString($tmpword, $pos, ($pos + $i), $this->isunicode));
  21569. if (isset($patterns[$subword])) {
  21570. $pattern = TCPDF_FONTS::UTF8StringToArray($patterns[$subword], $this->isunicode, $this->CurrentFont);
  21571. $pattern_length = count($pattern);
  21572. $digits = 1;
  21573. for ($j = 0; $j < $pattern_length; ++$j) {
  21574. // check if $pattern[$j] is a number = hyphenation level (only numbers from 1 to 5 are valid)
  21575. if (($pattern[$j] >= 48) AND ($pattern[$j] <= 57)) {
  21576. if ($j == 0) {
  21577. $zero = $pos - 1;
  21578. } else {
  21579. $zero = $pos + $j - $digits;
  21580. }
  21581. // get hyphenation level
  21582. $level = ($pattern[$j] - 48);
  21583. // if two levels from two different patterns match at the same point, the higher one is selected.
  21584. if (!isset($hyphenword[$zero]) OR ($hyphenword[$zero] < $level)) {
  21585. $hyphenword[$zero] = $level;
  21586. }
  21587. ++$digits;
  21588. }
  21589. }
  21590. }
  21591. }
  21592. }
  21593. $inserted = 0;
  21594. $maxpos = $numchars - $rightmin;
  21595. for ($i = $leftmin; $i <= $maxpos; ++$i) {
  21596. // only odd levels indicate allowed hyphenation points
  21597. if (isset($hyphenword[$i]) AND (($hyphenword[$i] % 2) != 0)) {
  21598. // 173 = soft hyphen character
  21599. array_splice($word, $i + $inserted, 0, 173);
  21600. ++$inserted;
  21601. }
  21602. }
  21603. return $word;
  21604. }
  21605. /**
  21606. * Returns text with soft hyphens.
  21607. * @param string $text text to process
  21608. * @param mixed $patterns Array of hypenation patterns or a TEX file containing hypenation patterns. TEX patterns can be downloaded from http://www.ctan.org/tex-archive/language/hyph-utf8/tex/generic/hyph-utf8/patterns/
  21609. * @param array $dictionary Array of words to be returned without applying the hyphenation algorithm.
  21610. * @param int $leftmin Minimum number of character to leave on the left of the word without applying the hyphens.
  21611. * @param int $rightmin Minimum number of character to leave on the right of the word without applying the hyphens.
  21612. * @param int $charmin Minimum word length to apply the hyphenation algorithm.
  21613. * @param int $charmax Maximum length of broken piece of word.
  21614. * @return string text with soft hyphens
  21615. * @author Nicola Asuni
  21616. * @since 4.9.012 (2010-04-12)
  21617. * @public
  21618. */
  21619. public function hyphenateText($text, $patterns, $dictionary=array(), $leftmin=1, $rightmin=2, $charmin=1, $charmax=8) {
  21620. $text = $this->unhtmlentities($text);
  21621. $word = array(); // last word
  21622. $txtarr = array(); // text to be returned
  21623. $intag = false; // true if we are inside an HTML tag
  21624. $skip = false; // true to skip hyphenation
  21625. if (!is_array($patterns)) {
  21626. $patterns = TCPDF_STATIC::getHyphenPatternsFromTEX($patterns);
  21627. }
  21628. // get array of characters
  21629. $unichars = TCPDF_FONTS::UTF8StringToArray($text, $this->isunicode, $this->CurrentFont);
  21630. // for each char
  21631. foreach ($unichars as $char) {
  21632. if ((!$intag) AND (!$skip) AND TCPDF_FONT_DATA::$uni_type[$char] == 'L') {
  21633. // letter character
  21634. $word[] = $char;
  21635. } else {
  21636. // other type of character
  21637. if (!TCPDF_STATIC::empty_string($word)) {
  21638. // hypenate the word
  21639. $txtarr = array_merge($txtarr, $this->hyphenateWord($word, $patterns, $dictionary, $leftmin, $rightmin, $charmin, $charmax));
  21640. $word = array();
  21641. }
  21642. $txtarr[] = $char;
  21643. if (chr($char) == '<') {
  21644. // we are inside an HTML tag
  21645. $intag = true;
  21646. } elseif ($intag AND (chr($char) == '>')) {
  21647. // end of HTML tag
  21648. $intag = false;
  21649. // check for style tag
  21650. $expected = array(115, 116, 121, 108, 101); // = 'style'
  21651. $current = array_slice($txtarr, -6, 5); // last 5 chars
  21652. $compare = array_diff($expected, $current);
  21653. if (empty($compare)) {
  21654. // check if it is a closing tag
  21655. $expected = array(47); // = '/'
  21656. $current = array_slice($txtarr, -7, 1);
  21657. $compare = array_diff($expected, $current);
  21658. if (empty($compare)) {
  21659. // closing style tag
  21660. $skip = false;
  21661. } else {
  21662. // opening style tag
  21663. $skip = true;
  21664. }
  21665. }
  21666. }
  21667. }
  21668. }
  21669. if (!TCPDF_STATIC::empty_string($word)) {
  21670. // hypenate the word
  21671. $txtarr = array_merge($txtarr, $this->hyphenateWord($word, $patterns, $dictionary, $leftmin, $rightmin, $charmin, $charmax));
  21672. }
  21673. // convert char array to string and return
  21674. return TCPDF_FONTS::UTF8ArrSubString($txtarr, '', '', $this->isunicode);
  21675. }
  21676. /**
  21677. * Enable/disable rasterization of vector images using ImageMagick library.
  21678. * @param boolean $mode if true enable rasterization, false otherwise.
  21679. * @public
  21680. * @since 5.0.000 (2010-04-27)
  21681. */
  21682. public function setRasterizeVectorImages($mode) {
  21683. $this->rasterize_vector_images = $mode;
  21684. }
  21685. /**
  21686. * Enable or disable default option for font subsetting.
  21687. * @param boolean $enable if true enable font subsetting by default.
  21688. * @author Nicola Asuni
  21689. * @public
  21690. * @since 5.3.002 (2010-06-07)
  21691. */
  21692. public function setFontSubsetting($enable=true) {
  21693. if ($this->pdfa_mode) {
  21694. $this->font_subsetting = false;
  21695. } else {
  21696. $this->font_subsetting = $enable ? true : false;
  21697. }
  21698. }
  21699. /**
  21700. * Return the default option for font subsetting.
  21701. * @return bool default font subsetting state.
  21702. * @author Nicola Asuni
  21703. * @public
  21704. * @since 5.3.002 (2010-06-07)
  21705. */
  21706. public function getFontSubsetting() {
  21707. return $this->font_subsetting;
  21708. }
  21709. /**
  21710. * Left trim the input string
  21711. * @param string $str string to trim
  21712. * @param string $replace string that replace spaces.
  21713. * @return string left trimmed string
  21714. * @author Nicola Asuni
  21715. * @public
  21716. * @since 5.8.000 (2010-08-11)
  21717. */
  21718. public function stringLeftTrim($str, $replace='') {
  21719. return preg_replace('/^'.$this->re_space['p'].'+/'.$this->re_space['m'], $replace, $str);
  21720. }
  21721. /**
  21722. * Right trim the input string
  21723. * @param string $str string to trim
  21724. * @param string $replace string that replace spaces.
  21725. * @return string right trimmed string
  21726. * @author Nicola Asuni
  21727. * @public
  21728. * @since 5.8.000 (2010-08-11)
  21729. */
  21730. public function stringRightTrim($str, $replace='') {
  21731. return preg_replace('/'.$this->re_space['p'].'+$/'.$this->re_space['m'], $replace, $str);
  21732. }
  21733. /**
  21734. * Trim the input string
  21735. * @param string $str string to trim
  21736. * @param string $replace string that replace spaces.
  21737. * @return string trimmed string
  21738. * @author Nicola Asuni
  21739. * @public
  21740. * @since 5.8.000 (2010-08-11)
  21741. */
  21742. public function stringTrim($str, $replace='') {
  21743. $str = $this->stringLeftTrim($str, $replace);
  21744. $str = $this->stringRightTrim($str, $replace);
  21745. return $str;
  21746. }
  21747. /**
  21748. * Return true if the current font is unicode type.
  21749. * @return bool true for unicode font, false otherwise.
  21750. * @author Nicola Asuni
  21751. * @public
  21752. * @since 5.8.002 (2010-08-14)
  21753. */
  21754. public function isUnicodeFont() {
  21755. return (($this->CurrentFont['type'] == 'TrueTypeUnicode') OR ($this->CurrentFont['type'] == 'cidfont0'));
  21756. }
  21757. /**
  21758. * Return normalized font name
  21759. * @param string $fontfamily property string containing font family names
  21760. * @return string normalized font name
  21761. * @author Nicola Asuni
  21762. * @public
  21763. * @since 5.8.004 (2010-08-17)
  21764. */
  21765. public function getFontFamilyName($fontfamily) {
  21766. // remove spaces and symbols
  21767. $fontfamily = preg_replace('/[^a-z0-9_\,]/', '', strtolower($fontfamily));
  21768. // extract all font names
  21769. $fontslist = preg_split('/[,]/', $fontfamily);
  21770. // find first valid font name
  21771. foreach ($fontslist as $font) {
  21772. // replace font variations
  21773. $font = preg_replace('/regular$/', '', $font);
  21774. $font = preg_replace('/italic$/', 'I', $font);
  21775. $font = preg_replace('/oblique$/', 'I', $font);
  21776. $font = preg_replace('/bold([I]?)$/', 'B\\1', $font);
  21777. // replace common family names and core fonts
  21778. $pattern = array();
  21779. $replacement = array();
  21780. $pattern[] = '/^serif|^cursive|^fantasy|^timesnewroman/';
  21781. $replacement[] = 'times';
  21782. $pattern[] = '/^sansserif/';
  21783. $replacement[] = 'helvetica';
  21784. $pattern[] = '/^monospace/';
  21785. $replacement[] = 'courier';
  21786. $font = preg_replace($pattern, $replacement, $font);
  21787. if (in_array(strtolower($font), $this->fontlist) OR in_array($font, $this->fontkeys)) {
  21788. return $font;
  21789. }
  21790. }
  21791. // return current font as default
  21792. return $this->CurrentFont['fontkey'];
  21793. }
  21794. /**
  21795. * Start a new XObject Template.
  21796. * An XObject Template is a PDF block that is a self-contained description of any sequence of graphics objects (including path objects, text objects, and sampled images).
  21797. * An XObject Template may be painted multiple times, either on several pages or at several locations on the same page and produces the same results each time, subject only to the graphics state at the time it is invoked.
  21798. * Note: X,Y coordinates will be reset to 0,0.
  21799. * @param int $w Template width in user units (empty string or zero = page width less margins).
  21800. * @param int $h Template height in user units (empty string or zero = page height less margins).
  21801. * @param mixed $group Set transparency group. Can be a boolean value or an array specifying optional parameters: 'CS' (solour space name), 'I' (boolean flag to indicate isolated group) and 'K' (boolean flag to indicate knockout group).
  21802. * @return string|false the XObject Template ID in case of success or false in case of error.
  21803. * @author Nicola Asuni
  21804. * @public
  21805. * @since 5.8.017 (2010-08-24)
  21806. * @see endTemplate(), printTemplate()
  21807. */
  21808. public function startTemplate($w=0, $h=0, $group=false) {
  21809. if ($this->inxobj) {
  21810. // we are already inside an XObject template
  21811. return false;
  21812. }
  21813. $this->inxobj = true;
  21814. ++$this->n;
  21815. // XObject ID
  21816. $this->xobjid = 'XT'.$this->n;
  21817. // object ID
  21818. $this->xobjects[$this->xobjid] = array('n' => $this->n);
  21819. // store current graphic state
  21820. $this->xobjects[$this->xobjid]['gvars'] = $this->getGraphicVars();
  21821. // initialize data
  21822. $this->xobjects[$this->xobjid]['intmrk'] = 0;
  21823. $this->xobjects[$this->xobjid]['transfmrk'] = array();
  21824. $this->xobjects[$this->xobjid]['outdata'] = '';
  21825. $this->xobjects[$this->xobjid]['xobjects'] = array();
  21826. $this->xobjects[$this->xobjid]['images'] = array();
  21827. $this->xobjects[$this->xobjid]['fonts'] = array();
  21828. $this->xobjects[$this->xobjid]['annotations'] = array();
  21829. $this->xobjects[$this->xobjid]['extgstates'] = array();
  21830. $this->xobjects[$this->xobjid]['gradients'] = array();
  21831. $this->xobjects[$this->xobjid]['spot_colors'] = array();
  21832. // set new environment
  21833. $this->num_columns = 1;
  21834. $this->current_column = 0;
  21835. $this->setAutoPageBreak(false);
  21836. if (($w === '') OR ($w <= 0)) {
  21837. $w = $this->w - $this->lMargin - $this->rMargin;
  21838. }
  21839. if (($h === '') OR ($h <= 0)) {
  21840. $h = $this->h - $this->tMargin - $this->bMargin;
  21841. }
  21842. $this->xobjects[$this->xobjid]['x'] = 0;
  21843. $this->xobjects[$this->xobjid]['y'] = 0;
  21844. $this->xobjects[$this->xobjid]['w'] = $w;
  21845. $this->xobjects[$this->xobjid]['h'] = $h;
  21846. $this->w = $w;
  21847. $this->h = $h;
  21848. $this->wPt = $this->w * $this->k;
  21849. $this->hPt = $this->h * $this->k;
  21850. $this->fwPt = $this->wPt;
  21851. $this->fhPt = $this->hPt;
  21852. $this->x = 0;
  21853. $this->y = 0;
  21854. $this->lMargin = 0;
  21855. $this->rMargin = 0;
  21856. $this->tMargin = 0;
  21857. $this->bMargin = 0;
  21858. // set group mode
  21859. $this->xobjects[$this->xobjid]['group'] = $group;
  21860. return $this->xobjid;
  21861. }
  21862. /**
  21863. * End the current XObject Template started with startTemplate() and restore the previous graphic state.
  21864. * An XObject Template is a PDF block that is a self-contained description of any sequence of graphics objects (including path objects, text objects, and sampled images).
  21865. * An XObject Template may be painted multiple times, either on several pages or at several locations on the same page and produces the same results each time, subject only to the graphics state at the time it is invoked.
  21866. * @return string|false the XObject Template ID in case of success or false in case of error.
  21867. * @author Nicola Asuni
  21868. * @public
  21869. * @since 5.8.017 (2010-08-24)
  21870. * @see startTemplate(), printTemplate()
  21871. */
  21872. public function endTemplate() {
  21873. if (!$this->inxobj) {
  21874. // we are not inside a template
  21875. return false;
  21876. }
  21877. $this->inxobj = false;
  21878. // restore previous graphic state
  21879. $this->setGraphicVars($this->xobjects[$this->xobjid]['gvars'], true);
  21880. return $this->xobjid;
  21881. }
  21882. /**
  21883. * Print an XObject Template.
  21884. * You can print an XObject Template inside the currently opened Template.
  21885. * An XObject Template is a PDF block that is a self-contained description of any sequence of graphics objects (including path objects, text objects, and sampled images).
  21886. * An XObject Template may be painted multiple times, either on several pages or at several locations on the same page and produces the same results each time, subject only to the graphics state at the time it is invoked.
  21887. * @param string $id The ID of XObject Template to print.
  21888. * @param float|null $x X position in user units (empty string = current x position)
  21889. * @param float|null $y Y position in user units (empty string = current y position)
  21890. * @param float $w Width in user units (zero = remaining page width)
  21891. * @param float $h Height in user units (zero = remaining page height)
  21892. * @param string $align Indicates the alignment of the pointer next to template insertion relative to template height. The value can be:<ul><li>T: top-right for LTR or top-left for RTL</li><li>M: middle-right for LTR or middle-left for RTL</li><li>B: bottom-right for LTR or bottom-left for RTL</li><li>N: next line</li></ul>
  21893. * @param string $palign Allows to center or align the template on the current line. Possible values are:<ul><li>L : left align</li><li>C : center</li><li>R : right align</li><li>'' : empty string : left for LTR or right for RTL</li></ul>
  21894. * @param boolean $fitonpage If true the template is resized to not exceed page dimensions.
  21895. * @author Nicola Asuni
  21896. * @public
  21897. * @since 5.8.017 (2010-08-24)
  21898. * @see startTemplate(), endTemplate()
  21899. */
  21900. public function printTemplate($id, $x=null, $y=null, $w=0, $h=0, $align='', $palign='', $fitonpage=false) {
  21901. if ($this->state != 2) {
  21902. return;
  21903. }
  21904. if (!isset($this->xobjects[$id])) {
  21905. $this->Error('The XObject Template \''.$id.'\' doesn\'t exist!');
  21906. }
  21907. if ($this->inxobj) {
  21908. if ($id == $this->xobjid) {
  21909. // close current template
  21910. $this->endTemplate();
  21911. } else {
  21912. // use the template as resource for the template currently opened
  21913. $this->xobjects[$this->xobjid]['xobjects'][$id] = $this->xobjects[$id];
  21914. }
  21915. }
  21916. // set default values
  21917. if (TCPDF_STATIC::empty_string($x)) {
  21918. $x = $this->x;
  21919. }
  21920. if (TCPDF_STATIC::empty_string($y)) {
  21921. $y = $this->y;
  21922. }
  21923. // check page for no-write regions and adapt page margins if necessary
  21924. list($x, $y) = $this->checkPageRegions($h, $x, $y);
  21925. $ow = $this->xobjects[$id]['w'];
  21926. if ($ow <= 0) {
  21927. $ow = 1;
  21928. }
  21929. $oh = $this->xobjects[$id]['h'];
  21930. if ($oh <= 0) {
  21931. $oh = 1;
  21932. }
  21933. // calculate template width and height on document
  21934. if (($w <= 0) AND ($h <= 0)) {
  21935. $w = $ow;
  21936. $h = $oh;
  21937. } elseif ($w <= 0) {
  21938. $w = $h * $ow / $oh;
  21939. } elseif ($h <= 0) {
  21940. $h = $w * $oh / $ow;
  21941. }
  21942. // fit the template on available space
  21943. list($w, $h, $x, $y) = $this->fitBlock($w, $h, $x, $y, $fitonpage);
  21944. // set page alignment
  21945. $rb_y = $y + $h;
  21946. // set alignment
  21947. if ($this->rtl) {
  21948. if ($palign == 'L') {
  21949. $xt = $this->lMargin;
  21950. } elseif ($palign == 'C') {
  21951. $xt = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
  21952. } elseif ($palign == 'R') {
  21953. $xt = $this->w - $this->rMargin - $w;
  21954. } else {
  21955. $xt = $x - $w;
  21956. }
  21957. $rb_x = $xt;
  21958. } else {
  21959. if ($palign == 'L') {
  21960. $xt = $this->lMargin;
  21961. } elseif ($palign == 'C') {
  21962. $xt = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
  21963. } elseif ($palign == 'R') {
  21964. $xt = $this->w - $this->rMargin - $w;
  21965. } else {
  21966. $xt = $x;
  21967. }
  21968. $rb_x = $xt + $w;
  21969. }
  21970. // print XObject Template + Transformation matrix
  21971. $this->StartTransform();
  21972. // translate and scale
  21973. $sx = ($w / $ow);
  21974. $sy = ($h / $oh);
  21975. $tm = array();
  21976. $tm[0] = $sx;
  21977. $tm[1] = 0;
  21978. $tm[2] = 0;
  21979. $tm[3] = $sy;
  21980. $tm[4] = $xt * $this->k;
  21981. $tm[5] = ($this->h - $h - $y) * $this->k;
  21982. $this->Transform($tm);
  21983. // set object
  21984. $this->_out('/'.$id.' Do');
  21985. $this->StopTransform();
  21986. // add annotations
  21987. if (!empty($this->xobjects[$id]['annotations'])) {
  21988. foreach ($this->xobjects[$id]['annotations'] as $annot) {
  21989. // transform original coordinates
  21990. $coordlt = TCPDF_STATIC::getTransformationMatrixProduct($tm, array(1, 0, 0, 1, ($annot['x'] * $this->k), (-$annot['y'] * $this->k)));
  21991. $ax = ($coordlt[4] / $this->k);
  21992. $ay = ($this->h - $h - ($coordlt[5] / $this->k));
  21993. $coordrb = TCPDF_STATIC::getTransformationMatrixProduct($tm, array(1, 0, 0, 1, (($annot['x'] + $annot['w']) * $this->k), ((-$annot['y'] - $annot['h']) * $this->k)));
  21994. $aw = ($coordrb[4] / $this->k) - $ax;
  21995. $ah = ($this->h - $h - ($coordrb[5] / $this->k)) - $ay;
  21996. $this->Annotation($ax, $ay, $aw, $ah, $annot['text'], $annot['opt'], $annot['spaces']);
  21997. }
  21998. }
  21999. // set pointer to align the next text/objects
  22000. switch($align) {
  22001. case 'T': {
  22002. $this->y = $y;
  22003. $this->x = $rb_x;
  22004. break;
  22005. }
  22006. case 'M': {
  22007. $this->y = $y + round($h/2);
  22008. $this->x = $rb_x;
  22009. break;
  22010. }
  22011. case 'B': {
  22012. $this->y = $rb_y;
  22013. $this->x = $rb_x;
  22014. break;
  22015. }
  22016. case 'N': {
  22017. $this->setY($rb_y);
  22018. break;
  22019. }
  22020. default:{
  22021. break;
  22022. }
  22023. }
  22024. }
  22025. /**
  22026. * Set the percentage of character stretching.
  22027. * @param int $perc percentage of stretching (100 = no stretching)
  22028. * @author Nicola Asuni
  22029. * @public
  22030. * @since 5.9.000 (2010-09-29)
  22031. */
  22032. public function setFontStretching($perc=100) {
  22033. $this->font_stretching = $perc;
  22034. }
  22035. /**
  22036. * Get the percentage of character stretching.
  22037. * @return float stretching value
  22038. * @author Nicola Asuni
  22039. * @public
  22040. * @since 5.9.000 (2010-09-29)
  22041. */
  22042. public function getFontStretching() {
  22043. return $this->font_stretching;
  22044. }
  22045. /**
  22046. * Set the amount to increase or decrease the space between characters in a text.
  22047. * @param float $spacing amount to increase or decrease the space between characters in a text (0 = default spacing)
  22048. * @author Nicola Asuni
  22049. * @public
  22050. * @since 5.9.000 (2010-09-29)
  22051. */
  22052. public function setFontSpacing($spacing=0) {
  22053. $this->font_spacing = $spacing;
  22054. }
  22055. /**
  22056. * Get the amount to increase or decrease the space between characters in a text.
  22057. * @return int font spacing (tracking) value
  22058. * @author Nicola Asuni
  22059. * @public
  22060. * @since 5.9.000 (2010-09-29)
  22061. */
  22062. public function getFontSpacing() {
  22063. return $this->font_spacing;
  22064. }
  22065. /**
  22066. * Return an array of no-write page regions
  22067. * @return array of no-write page regions
  22068. * @author Nicola Asuni
  22069. * @public
  22070. * @since 5.9.003 (2010-10-13)
  22071. * @see setPageRegions(), addPageRegion()
  22072. */
  22073. public function getPageRegions() {
  22074. return $this->page_regions;
  22075. }
  22076. /**
  22077. * Set no-write regions on page.
  22078. * A no-write region is a portion of the page with a rectangular or trapezium shape that will not be covered when writing text or html code.
  22079. * A region is always aligned on the left or right side of the page ad is defined using a vertical segment.
  22080. * You can set multiple regions for the same page.
  22081. * @param array $regions array of no-write regions. For each region you can define an array as follow: ('page' => page number or empy for current page, 'xt' => X top, 'yt' => Y top, 'xb' => X bottom, 'yb' => Y bottom, 'side' => page side 'L' = left or 'R' = right). Omit this parameter to remove all regions.
  22082. * @author Nicola Asuni
  22083. * @public
  22084. * @since 5.9.003 (2010-10-13)
  22085. * @see addPageRegion(), getPageRegions()
  22086. */
  22087. public function setPageRegions($regions=array()) {
  22088. // empty current regions array
  22089. $this->page_regions = array();
  22090. // add regions
  22091. foreach ($regions as $data) {
  22092. $this->addPageRegion($data);
  22093. }
  22094. }
  22095. /**
  22096. * Add a single no-write region on selected page.
  22097. * A no-write region is a portion of the page with a rectangular or trapezium shape that will not be covered when writing text or html code.
  22098. * A region is always aligned on the left or right side of the page ad is defined using a vertical segment.
  22099. * You can set multiple regions for the same page.
  22100. * @param array $region array of a single no-write region array: ('page' => page number or empy for current page, 'xt' => X top, 'yt' => Y top, 'xb' => X bottom, 'yb' => Y bottom, 'side' => page side 'L' = left or 'R' = right).
  22101. * @author Nicola Asuni
  22102. * @public
  22103. * @since 5.9.003 (2010-10-13)
  22104. * @see setPageRegions(), getPageRegions()
  22105. */
  22106. public function addPageRegion($region) {
  22107. if (!isset($region['page']) OR empty($region['page'])) {
  22108. $region['page'] = $this->page;
  22109. }
  22110. if (isset($region['xt']) AND isset($region['xb']) AND ($region['xt'] > 0) AND ($region['xb'] > 0)
  22111. AND isset($region['yt']) AND isset($region['yb']) AND ($region['yt'] >= 0) AND ($region['yt'] < $region['yb'])
  22112. AND isset($region['side']) AND (($region['side'] == 'L') OR ($region['side'] == 'R'))) {
  22113. $this->page_regions[] = $region;
  22114. }
  22115. }
  22116. /**
  22117. * Remove a single no-write region.
  22118. * @param int $key region key
  22119. * @author Nicola Asuni
  22120. * @public
  22121. * @since 5.9.003 (2010-10-13)
  22122. * @see setPageRegions(), getPageRegions()
  22123. */
  22124. public function removePageRegion($key) {
  22125. if (isset($this->page_regions[$key])) {
  22126. unset($this->page_regions[$key]);
  22127. }
  22128. }
  22129. /**
  22130. * Check page for no-write regions and adapt current coordinates and page margins if necessary.
  22131. * A no-write region is a portion of the page with a rectangular or trapezium shape that will not be covered when writing text or html code.
  22132. * A region is always aligned on the left or right side of the page ad is defined using a vertical segment.
  22133. * @param float $h height of the text/image/object to print in user units
  22134. * @param float $x current X coordinate in user units
  22135. * @param float $y current Y coordinate in user units
  22136. * @return float[] array($x, $y)
  22137. * @author Nicola Asuni
  22138. * @protected
  22139. * @since 5.9.003 (2010-10-13)
  22140. */
  22141. protected function checkPageRegions($h, $x, $y) {
  22142. // set default values
  22143. if ($x === '') {
  22144. $x = $this->x;
  22145. }
  22146. if ($y === '') {
  22147. $y = $this->y;
  22148. }
  22149. if (!$this->check_page_regions OR empty($this->page_regions)) {
  22150. // no page regions defined
  22151. return array($x, $y);
  22152. }
  22153. if (empty($h)) {
  22154. $h = $this->getCellHeight($this->FontSize);
  22155. }
  22156. // check for page break
  22157. if ($this->checkPageBreak($h, $y)) {
  22158. // the content will be printed on a new page
  22159. $x = $this->x;
  22160. $y = $this->y;
  22161. }
  22162. if ($this->num_columns > 1) {
  22163. if ($this->rtl) {
  22164. $this->lMargin = ($this->columns[$this->current_column]['x'] - $this->columns[$this->current_column]['w']);
  22165. } else {
  22166. $this->rMargin = ($this->w - $this->columns[$this->current_column]['x'] - $this->columns[$this->current_column]['w']);
  22167. }
  22168. } else {
  22169. if ($this->rtl) {
  22170. $this->lMargin = max($this->clMargin, $this->original_lMargin);
  22171. } else {
  22172. $this->rMargin = max($this->crMargin, $this->original_rMargin);
  22173. }
  22174. }
  22175. // adjust coordinates and page margins
  22176. foreach ($this->page_regions as $regid => $regdata) {
  22177. if ($regdata['page'] == $this->page) {
  22178. // check region boundaries
  22179. if (($y > ($regdata['yt'] - $h)) AND ($y <= $regdata['yb'])) {
  22180. // Y is inside the region
  22181. $minv = ($regdata['xb'] - $regdata['xt']) / ($regdata['yb'] - $regdata['yt']); // inverse of angular coefficient
  22182. $yt = max($y, $regdata['yt']);
  22183. $yb = min(($yt + $h), $regdata['yb']);
  22184. $xt = (($yt - $regdata['yt']) * $minv) + $regdata['xt'];
  22185. $xb = (($yb - $regdata['yt']) * $minv) + $regdata['xt'];
  22186. if ($regdata['side'] == 'L') { // left side
  22187. $new_margin = max($xt, $xb);
  22188. if ($this->lMargin < $new_margin) {
  22189. if ($this->rtl) {
  22190. // adjust left page margin
  22191. $this->lMargin = max(0, $new_margin);
  22192. }
  22193. if ($x < $new_margin) {
  22194. // adjust x position
  22195. $x = $new_margin;
  22196. if ($new_margin > ($this->w - $this->rMargin)) {
  22197. // adjust y position
  22198. $y = $regdata['yb'] - $h;
  22199. }
  22200. }
  22201. }
  22202. } elseif ($regdata['side'] == 'R') { // right side
  22203. $new_margin = min($xt, $xb);
  22204. if (($this->w - $this->rMargin) > $new_margin) {
  22205. if (!$this->rtl) {
  22206. // adjust right page margin
  22207. $this->rMargin = max(0, ($this->w - $new_margin));
  22208. }
  22209. if ($x > $new_margin) {
  22210. // adjust x position
  22211. $x = $new_margin;
  22212. if ($new_margin > $this->lMargin) {
  22213. // adjust y position
  22214. $y = $regdata['yb'] - $h;
  22215. }
  22216. }
  22217. }
  22218. }
  22219. }
  22220. }
  22221. }
  22222. return array($x, $y);
  22223. }
  22224. // --- SVG METHODS ---------------------------------------------------------
  22225. /**
  22226. * Embedd a Scalable Vector Graphics (SVG) image.
  22227. * NOTE: SVG standard is not yet fully implemented, use the setRasterizeVectorImages() method to enable/disable rasterization of vector images using ImageMagick library.
  22228. * @param string $file Name of the SVG file or a '@' character followed by the SVG data string.
  22229. * @param float|null $x Abscissa of the upper-left corner.
  22230. * @param float|null $y Ordinate of the upper-left corner.
  22231. * @param float $w Width of the image in the page. If not specified or equal to zero, it is automatically calculated.
  22232. * @param float $h Height of the image in the page. If not specified or equal to zero, it is automatically calculated.
  22233. * @param mixed $link URL or identifier returned by AddLink().
  22234. * @param string $align Indicates the alignment of the pointer next to image insertion relative to image height. The value can be:<ul><li>T: top-right for LTR or top-left for RTL</li><li>M: middle-right for LTR or middle-left for RTL</li><li>B: bottom-right for LTR or bottom-left for RTL</li><li>N: next line</li></ul> If the alignment is an empty string, then the pointer will be restored on the starting SVG position.
  22235. * @param string $palign Allows to center or align the image on the current line. Possible values are:<ul><li>L : left align</li><li>C : center</li><li>R : right align</li><li>'' : empty string : left for LTR or right for RTL</li></ul>
  22236. * @param mixed $border Indicates if borders must be drawn around the cell. The value can be a number:<ul><li>0: no border (default)</li><li>1: frame</li></ul> or a string containing some or all of the following characters (in any order):<ul><li>L: left</li><li>T: top</li><li>R: right</li><li>B: bottom</li></ul> or an array of line styles for each border group - for example: array('LTRB' => array('width' => 2, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)))
  22237. * @param boolean $fitonpage if true the image is resized to not exceed page dimensions.
  22238. * @author Nicola Asuni
  22239. * @since 5.0.000 (2010-05-02)
  22240. * @public
  22241. */
  22242. public function ImageSVG($file, $x=null, $y=null, $w=0, $h=0, $link='', $align='', $palign='', $border=0, $fitonpage=false) {
  22243. if ($this->state != 2) {
  22244. return;
  22245. }
  22246. // reset SVG vars
  22247. $this->svggradients = array();
  22248. $this->svggradientid = 0;
  22249. $this->svgdefsmode = false;
  22250. $this->svgdefs = array();
  22251. $this->svgclipmode = false;
  22252. $this->svgclippaths = array();
  22253. $this->svgcliptm = array();
  22254. $this->svgclipid = 0;
  22255. $this->svgtext = '';
  22256. $this->svgtextmode = array();
  22257. if ($this->rasterize_vector_images AND ($w > 0) AND ($h > 0)) {
  22258. // convert SVG to raster image using GD or ImageMagick libraries
  22259. return $this->Image($file, $x, $y, $w, $h, 'SVG', $link, $align, true, 300, $palign, false, false, $border, false, false, false);
  22260. }
  22261. if ($file[0] === '@') { // image from string
  22262. $this->svgdir = '';
  22263. $svgdata = substr($file, 1);
  22264. } else { // SVG file
  22265. $this->svgdir = dirname($file);
  22266. $svgdata = $this->getCachedFileContents($file);
  22267. }
  22268. if ($svgdata === FALSE) {
  22269. $this->Error('SVG file not found: '.$file);
  22270. }
  22271. if (TCPDF_STATIC::empty_string($x)) {
  22272. $x = $this->x;
  22273. }
  22274. if (TCPDF_STATIC::empty_string($y)) {
  22275. $y = $this->y;
  22276. }
  22277. // check page for no-write regions and adapt page margins if necessary
  22278. list($x, $y) = $this->checkPageRegions($h, $x, $y);
  22279. $k = $this->k;
  22280. $ox = 0;
  22281. $oy = 0;
  22282. $ow = $w;
  22283. $oh = $h;
  22284. $aspect_ratio_align = 'xMidYMid';
  22285. $aspect_ratio_ms = 'meet';
  22286. $regs = array();
  22287. // get original image width and height
  22288. preg_match('/<svg([^\>]*)>/si', $svgdata, $regs);
  22289. if (isset($regs[1]) AND !empty($regs[1])) {
  22290. $tmp = array();
  22291. if (preg_match('/[\s]+x[\s]*=[\s]*"([^"]*)"/si', $regs[1], $tmp)) {
  22292. $ox = $this->getHTMLUnitToUnits($tmp[1], 0, $this->svgunit, false);
  22293. }
  22294. $tmp = array();
  22295. if (preg_match('/[\s]+y[\s]*=[\s]*"([^"]*)"/si', $regs[1], $tmp)) {
  22296. $oy = $this->getHTMLUnitToUnits($tmp[1], 0, $this->svgunit, false);
  22297. }
  22298. $tmp = array();
  22299. if (preg_match('/[\s]+width[\s]*=[\s]*"([^"]*)"/si', $regs[1], $tmp)) {
  22300. $ow = $this->getHTMLUnitToUnits($tmp[1], 1, $this->svgunit, false);
  22301. }
  22302. $tmp = array();
  22303. if (preg_match('/[\s]+height[\s]*=[\s]*"([^"]*)"/si', $regs[1], $tmp)) {
  22304. $oh = $this->getHTMLUnitToUnits($tmp[1], 1, $this->svgunit, false);
  22305. }
  22306. $tmp = array();
  22307. $view_box = array();
  22308. if (preg_match('/[\s]+viewBox[\s]*=[\s]*"[\s]*([0-9\.\-]+)[\s]+([0-9\.\-]+)[\s]+([0-9\.]+)[\s]+([0-9\.]+)[\s]*"/si', $regs[1], $tmp)) {
  22309. if (count($tmp) == 5) {
  22310. array_shift($tmp);
  22311. foreach ($tmp as $key => $val) {
  22312. $view_box[$key] = $this->getHTMLUnitToUnits($val, 0, $this->svgunit, false);
  22313. }
  22314. $ox = $view_box[0];
  22315. $oy = $view_box[1];
  22316. }
  22317. // get aspect ratio
  22318. $tmp = array();
  22319. if (preg_match('/[\s]+preserveAspectRatio[\s]*=[\s]*"([^"]*)"/si', $regs[1], $tmp)) {
  22320. $aspect_ratio = preg_split('/[\s]+/si', $tmp[1]);
  22321. switch (count($aspect_ratio)) {
  22322. case 3: {
  22323. $aspect_ratio_align = $aspect_ratio[1];
  22324. $aspect_ratio_ms = $aspect_ratio[2];
  22325. break;
  22326. }
  22327. case 2: {
  22328. $aspect_ratio_align = $aspect_ratio[0];
  22329. $aspect_ratio_ms = $aspect_ratio[1];
  22330. break;
  22331. }
  22332. case 1: {
  22333. $aspect_ratio_align = $aspect_ratio[0];
  22334. $aspect_ratio_ms = 'meet';
  22335. break;
  22336. }
  22337. }
  22338. }
  22339. }
  22340. }
  22341. if ($ow <= 0) {
  22342. $ow = 1;
  22343. }
  22344. if ($oh <= 0) {
  22345. $oh = 1;
  22346. }
  22347. // calculate image width and height on document
  22348. if (($w <= 0) AND ($h <= 0)) {
  22349. // convert image size to document unit
  22350. $w = $ow;
  22351. $h = $oh;
  22352. } elseif ($w <= 0) {
  22353. $w = $h * $ow / $oh;
  22354. } elseif ($h <= 0) {
  22355. $h = $w * $oh / $ow;
  22356. }
  22357. // fit the image on available space
  22358. list($w, $h, $x, $y) = $this->fitBlock($w, $h, $x, $y, $fitonpage);
  22359. if ($this->rasterize_vector_images) {
  22360. // convert SVG to raster image using GD or ImageMagick libraries
  22361. return $this->Image($file, $x, $y, $w, $h, 'SVG', $link, $align, true, 300, $palign, false, false, $border, false, false, false);
  22362. }
  22363. // set alignment
  22364. $this->img_rb_y = $y + $h;
  22365. // set alignment
  22366. if ($this->rtl) {
  22367. if ($palign == 'L') {
  22368. $ximg = $this->lMargin;
  22369. } elseif ($palign == 'C') {
  22370. $ximg = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
  22371. } elseif ($palign == 'R') {
  22372. $ximg = $this->w - $this->rMargin - $w;
  22373. } else {
  22374. $ximg = $x - $w;
  22375. }
  22376. $this->img_rb_x = $ximg;
  22377. } else {
  22378. if ($palign == 'L') {
  22379. $ximg = $this->lMargin;
  22380. } elseif ($palign == 'C') {
  22381. $ximg = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
  22382. } elseif ($palign == 'R') {
  22383. $ximg = $this->w - $this->rMargin - $w;
  22384. } else {
  22385. $ximg = $x;
  22386. }
  22387. $this->img_rb_x = $ximg + $w;
  22388. }
  22389. // store current graphic vars
  22390. $gvars = $this->getGraphicVars();
  22391. // store SVG position and scale factors
  22392. $svgoffset_x = ($ximg - $ox) * $this->k;
  22393. $svgoffset_y = -($y - $oy) * $this->k;
  22394. if (isset($view_box[2]) AND ($view_box[2] > 0) AND ($view_box[3] > 0)) {
  22395. $ow = $view_box[2];
  22396. $oh = $view_box[3];
  22397. } else {
  22398. if ($ow <= 0) {
  22399. $ow = $w;
  22400. }
  22401. if ($oh <= 0) {
  22402. $oh = $h;
  22403. }
  22404. }
  22405. $svgscale_x = $w / $ow;
  22406. $svgscale_y = $h / $oh;
  22407. // scaling and alignment
  22408. if ($aspect_ratio_align != 'none') {
  22409. // store current scaling values
  22410. $svgscale_old_x = $svgscale_x;
  22411. $svgscale_old_y = $svgscale_y;
  22412. // force uniform scaling
  22413. if ($aspect_ratio_ms == 'slice') {
  22414. // the entire viewport is covered by the viewBox
  22415. if ($svgscale_x > $svgscale_y) {
  22416. $svgscale_y = $svgscale_x;
  22417. } elseif ($svgscale_x < $svgscale_y) {
  22418. $svgscale_x = $svgscale_y;
  22419. }
  22420. } else { // meet
  22421. // the entire viewBox is visible within the viewport
  22422. if ($svgscale_x < $svgscale_y) {
  22423. $svgscale_y = $svgscale_x;
  22424. } elseif ($svgscale_x > $svgscale_y) {
  22425. $svgscale_x = $svgscale_y;
  22426. }
  22427. }
  22428. // correct X alignment
  22429. switch (substr($aspect_ratio_align, 1, 3)) {
  22430. case 'Min': {
  22431. // do nothing
  22432. break;
  22433. }
  22434. case 'Max': {
  22435. $svgoffset_x += (($w * $this->k) - ($ow * $this->k * $svgscale_x));
  22436. break;
  22437. }
  22438. default:
  22439. case 'Mid': {
  22440. $svgoffset_x += ((($w * $this->k) - ($ow * $this->k * $svgscale_x)) / 2);
  22441. break;
  22442. }
  22443. }
  22444. // correct Y alignment
  22445. switch (substr($aspect_ratio_align, 5)) {
  22446. case 'Min': {
  22447. // do nothing
  22448. break;
  22449. }
  22450. case 'Max': {
  22451. $svgoffset_y -= (($h * $this->k) - ($oh * $this->k * $svgscale_y));
  22452. break;
  22453. }
  22454. default:
  22455. case 'Mid': {
  22456. $svgoffset_y -= ((($h * $this->k) - ($oh * $this->k * $svgscale_y)) / 2);
  22457. break;
  22458. }
  22459. }
  22460. }
  22461. // store current page break mode
  22462. $page_break_mode = $this->AutoPageBreak;
  22463. $page_break_margin = $this->getBreakMargin();
  22464. $cell_padding = $this->cell_padding;
  22465. $this->setCellPadding(0);
  22466. $this->setAutoPageBreak(false);
  22467. // save the current graphic state
  22468. $this->_out('q'.$this->epsmarker);
  22469. // set initial clipping mask
  22470. $this->Rect($ximg, $y, $w, $h, 'CNZ', array(), array());
  22471. // scale and translate
  22472. $e = $ox * $this->k * (1 - $svgscale_x);
  22473. $f = ($this->h - $oy) * $this->k * (1 - $svgscale_y);
  22474. $this->_out(sprintf('%F %F %F %F %F %F cm', $svgscale_x, 0, 0, $svgscale_y, ($e + $svgoffset_x), ($f + $svgoffset_y)));
  22475. // creates a new XML parser to be used by the other XML functions
  22476. $parser = xml_parser_create('UTF-8');
  22477. // disable case-folding for this XML parser
  22478. xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
  22479. // sets the element handler functions for the XML parser
  22480. xml_set_element_handler($parser, [$this, 'startSVGElementHandler'], [$this, 'endSVGElementHandler']);
  22481. // sets the character data handler function for the XML parser
  22482. xml_set_character_data_handler($parser, [$this, 'segSVGContentHandler']);
  22483. // start parsing an XML document
  22484. if (!xml_parse($parser, $svgdata)) {
  22485. $error_message = sprintf('SVG Error: %s at line %d', xml_error_string(xml_get_error_code($parser)), xml_get_current_line_number($parser));
  22486. $this->Error($error_message);
  22487. }
  22488. // free this XML parser
  22489. xml_parser_free($parser);
  22490. // >= PHP 7.0.0 "explicitly unset the reference to parser to avoid memory leaks"
  22491. unset($parser);
  22492. // restore previous graphic state
  22493. $this->_out($this->epsmarker.'Q');
  22494. // restore graphic vars
  22495. $this->setGraphicVars($gvars);
  22496. $this->lasth = $gvars['lasth'];
  22497. if (!empty($border)) {
  22498. $bx = $this->x;
  22499. $by = $this->y;
  22500. $this->x = $ximg;
  22501. if ($this->rtl) {
  22502. $this->x += $w;
  22503. }
  22504. $this->y = $y;
  22505. $this->Cell($w, $h, '', $border, 0, '', 0, '', 0, true);
  22506. $this->x = $bx;
  22507. $this->y = $by;
  22508. }
  22509. if ($link) {
  22510. $this->Link($ximg, $y, $w, $h, $link, 0);
  22511. }
  22512. // set pointer to align the next text/objects
  22513. switch($align) {
  22514. case 'T':{
  22515. $this->y = $y;
  22516. $this->x = $this->img_rb_x;
  22517. break;
  22518. }
  22519. case 'M':{
  22520. $this->y = $y + round($h/2);
  22521. $this->x = $this->img_rb_x;
  22522. break;
  22523. }
  22524. case 'B':{
  22525. $this->y = $this->img_rb_y;
  22526. $this->x = $this->img_rb_x;
  22527. break;
  22528. }
  22529. case 'N':{
  22530. $this->setY($this->img_rb_y);
  22531. break;
  22532. }
  22533. default:{
  22534. // restore pointer to starting position
  22535. $this->x = $gvars['x'];
  22536. $this->y = $gvars['y'];
  22537. $this->page = $gvars['page'];
  22538. $this->current_column = $gvars['current_column'];
  22539. $this->tMargin = $gvars['tMargin'];
  22540. $this->bMargin = $gvars['bMargin'];
  22541. $this->w = $gvars['w'];
  22542. $this->h = $gvars['h'];
  22543. $this->wPt = $gvars['wPt'];
  22544. $this->hPt = $gvars['hPt'];
  22545. $this->fwPt = $gvars['fwPt'];
  22546. $this->fhPt = $gvars['fhPt'];
  22547. break;
  22548. }
  22549. }
  22550. $this->endlinex = $this->img_rb_x;
  22551. // restore page break
  22552. $this->setAutoPageBreak($page_break_mode, $page_break_margin);
  22553. $this->cell_padding = $cell_padding;
  22554. }
  22555. /**
  22556. * Convert SVG transformation matrix to PDF.
  22557. * @param array $tm original SVG transformation matrix
  22558. * @return array transformation matrix
  22559. * @protected
  22560. * @since 5.0.000 (2010-05-02)
  22561. */
  22562. protected function convertSVGtMatrix($tm) {
  22563. $a = $tm[0];
  22564. $b = -$tm[1];
  22565. $c = -$tm[2];
  22566. $d = $tm[3];
  22567. $e = $this->getHTMLUnitToUnits($tm[4], 1, $this->svgunit, false) * $this->k;
  22568. $f = -$this->getHTMLUnitToUnits($tm[5], 1, $this->svgunit, false) * $this->k;
  22569. $x = 0;
  22570. $y = $this->h * $this->k;
  22571. $e = ($x * (1 - $a)) - ($y * $c) + $e;
  22572. $f = ($y * (1 - $d)) - ($x * $b) + $f;
  22573. return array($a, $b, $c, $d, $e, $f);
  22574. }
  22575. /**
  22576. * Apply SVG graphic transformation matrix.
  22577. * @param array $tm original SVG transformation matrix
  22578. * @protected
  22579. * @since 5.0.000 (2010-05-02)
  22580. */
  22581. protected function SVGTransform($tm) {
  22582. $this->Transform($this->convertSVGtMatrix($tm));
  22583. }
  22584. /**
  22585. * Apply the requested SVG styles (*** TO BE COMPLETED ***)
  22586. * @param array $svgstyle array of SVG styles to apply
  22587. * @param array $prevsvgstyle array of previous SVG style
  22588. * @param int $x X origin of the bounding box
  22589. * @param int $y Y origin of the bounding box
  22590. * @param int $w width of the bounding box
  22591. * @param int $h height of the bounding box
  22592. * @param string $clip_function clip function
  22593. * @param array $clip_params array of parameters for clipping function
  22594. * @return string style
  22595. * @author Nicola Asuni
  22596. * @since 5.0.000 (2010-05-02)
  22597. * @protected
  22598. */
  22599. protected function setSVGStyles($svgstyle, $prevsvgstyle, $x=0, $y=0, $w=1, $h=1, $clip_function='', $clip_params=array()) {
  22600. if ($this->state != 2) {
  22601. return;
  22602. }
  22603. $objstyle = '';
  22604. $minlen = (0.01 / $this->k); // minimum acceptable length
  22605. if (!isset($svgstyle['opacity'])) {
  22606. return $objstyle;
  22607. }
  22608. // clip-path
  22609. $regs = array();
  22610. if (preg_match('/url\([\s]*\#([^\)]*)\)/si', $svgstyle['clip-path'], $regs)) {
  22611. $clip_path = $this->svgclippaths[$regs[1]];
  22612. foreach ($clip_path as $cp) {
  22613. $this->startSVGElementHandler('clip-path', $cp['name'], $cp['attribs'], $cp['tm']);
  22614. }
  22615. }
  22616. // opacity
  22617. if ($svgstyle['opacity'] != 1) {
  22618. $this->setAlpha($svgstyle['opacity'], 'Normal', $svgstyle['opacity'], false);
  22619. }
  22620. // color
  22621. $fill_color = TCPDF_COLORS::convertHTMLColorToDec($svgstyle['color'], $this->spot_colors);
  22622. $this->setFillColorArray($fill_color);
  22623. // text color
  22624. $text_color = TCPDF_COLORS::convertHTMLColorToDec($svgstyle['text-color'], $this->spot_colors);
  22625. $this->setTextColorArray($text_color);
  22626. // clip
  22627. if (preg_match('/rect\(([a-z0-9\-\.]*+)[\s]*+([a-z0-9\-\.]*+)[\s]*+([a-z0-9\-\.]*+)[\s]*+([a-z0-9\-\.]*+)\)/si', $svgstyle['clip'], $regs)) {
  22628. $top = (isset($regs[1])?$this->getHTMLUnitToUnits($regs[1], 0, $this->svgunit, false):0);
  22629. $right = (isset($regs[2])?$this->getHTMLUnitToUnits($regs[2], 0, $this->svgunit, false):0);
  22630. $bottom = (isset($regs[3])?$this->getHTMLUnitToUnits($regs[3], 0, $this->svgunit, false):0);
  22631. $left = (isset($regs[4])?$this->getHTMLUnitToUnits($regs[4], 0, $this->svgunit, false):0);
  22632. $cx = $x + $left;
  22633. $cy = $y + $top;
  22634. $cw = $w - $left - $right;
  22635. $ch = $h - $top - $bottom;
  22636. if ($svgstyle['clip-rule'] == 'evenodd') {
  22637. $clip_rule = 'CNZ';
  22638. } else {
  22639. $clip_rule = 'CEO';
  22640. }
  22641. $this->Rect($cx, $cy, $cw, $ch, $clip_rule, array(), array());
  22642. }
  22643. // fill
  22644. $regs = array();
  22645. if (preg_match('/url\([\s]*\#([^\)]*)\)/si', $svgstyle['fill'], $regs)) {
  22646. // gradient
  22647. $gradient = $this->svggradients[$regs[1]];
  22648. if (isset($gradient['xref'])) {
  22649. // reference to another gradient definition
  22650. $newgradient = $this->svggradients[$gradient['xref']];
  22651. $newgradient['coords'] = $gradient['coords'];
  22652. $newgradient['mode'] = $gradient['mode'];
  22653. $newgradient['type'] = $gradient['type'];
  22654. $newgradient['gradientUnits'] = $gradient['gradientUnits'];
  22655. if (isset($gradient['gradientTransform'])) {
  22656. $newgradient['gradientTransform'] = $gradient['gradientTransform'];
  22657. }
  22658. $gradient = $newgradient;
  22659. }
  22660. //save current Graphic State
  22661. $this->_outSaveGraphicsState();
  22662. //set clipping area
  22663. if (!empty($clip_function) AND method_exists($this, $clip_function)) {
  22664. $bbox = call_user_func_array(array($this, $clip_function), $clip_params);
  22665. if ((!isset($gradient['type']) OR ($gradient['type'] != 3)) AND is_array($bbox) AND (count($bbox) == 4)) {
  22666. list($x, $y, $w, $h) = $bbox;
  22667. }
  22668. }
  22669. if ($gradient['mode'] == 'measure') {
  22670. if (!isset($gradient['coords'][4])) {
  22671. $gradient['coords'][4] = 0.5;
  22672. }
  22673. if (isset($gradient['gradientTransform']) AND !empty($gradient['gradientTransform'])) {
  22674. $gtm = $gradient['gradientTransform'];
  22675. // apply transformation matrix
  22676. $xa = ($gtm[0] * $gradient['coords'][0]) + ($gtm[2] * $gradient['coords'][1]) + $gtm[4];
  22677. $ya = ($gtm[1] * $gradient['coords'][0]) + ($gtm[3] * $gradient['coords'][1]) + $gtm[5];
  22678. $xb = ($gtm[0] * $gradient['coords'][2]) + ($gtm[2] * $gradient['coords'][3]) + $gtm[4];
  22679. $yb = ($gtm[1] * $gradient['coords'][2]) + ($gtm[3] * $gradient['coords'][3]) + $gtm[5];
  22680. $r = sqrt(pow(($gtm[0] * $gradient['coords'][4]), 2) + pow(($gtm[1] * $gradient['coords'][4]), 2));
  22681. $gradient['coords'][0] = $xa;
  22682. $gradient['coords'][1] = $ya;
  22683. $gradient['coords'][2] = $xb;
  22684. $gradient['coords'][3] = $yb;
  22685. $gradient['coords'][4] = $r;
  22686. }
  22687. // convert SVG coordinates to user units
  22688. $gradient['coords'][0] = $this->getHTMLUnitToUnits($gradient['coords'][0], 0, $this->svgunit, false);
  22689. $gradient['coords'][1] = $this->getHTMLUnitToUnits($gradient['coords'][1], 0, $this->svgunit, false);
  22690. $gradient['coords'][2] = $this->getHTMLUnitToUnits($gradient['coords'][2], 0, $this->svgunit, false);
  22691. $gradient['coords'][3] = $this->getHTMLUnitToUnits($gradient['coords'][3], 0, $this->svgunit, false);
  22692. $gradient['coords'][4] = $this->getHTMLUnitToUnits($gradient['coords'][4], 0, $this->svgunit, false);
  22693. if ($w <= $minlen) {
  22694. $w = $minlen;
  22695. }
  22696. if ($h <= $minlen) {
  22697. $h = $minlen;
  22698. }
  22699. // shift units
  22700. if ($gradient['gradientUnits'] == 'objectBoundingBox') {
  22701. // convert to SVG coordinate system
  22702. $gradient['coords'][0] += $x;
  22703. $gradient['coords'][1] += $y;
  22704. $gradient['coords'][2] += $x;
  22705. $gradient['coords'][3] += $y;
  22706. }
  22707. // calculate percentages
  22708. $gradient['coords'][0] = (($gradient['coords'][0] - $x) / $w);
  22709. $gradient['coords'][1] = (($gradient['coords'][1] - $y) / $h);
  22710. $gradient['coords'][2] = (($gradient['coords'][2] - $x) / $w);
  22711. $gradient['coords'][3] = (($gradient['coords'][3] - $y) / $h);
  22712. $gradient['coords'][4] /= $w;
  22713. } elseif ($gradient['mode'] == 'percentage') {
  22714. foreach($gradient['coords'] as $key => $val) {
  22715. $gradient['coords'][$key] = (intval($val) / 100);
  22716. if ($val < 0) {
  22717. $gradient['coords'][$key] = 0;
  22718. } elseif ($val > 1) {
  22719. $gradient['coords'][$key] = 1;
  22720. }
  22721. }
  22722. }
  22723. if (($gradient['type'] == 2) AND ($gradient['coords'][0] == $gradient['coords'][2]) AND ($gradient['coords'][1] == $gradient['coords'][3])) {
  22724. // single color (no shading)
  22725. $gradient['coords'][0] = 1;
  22726. $gradient['coords'][1] = 0;
  22727. $gradient['coords'][2] = 0.999;
  22728. $gradient['coords'][3] = 0;
  22729. }
  22730. // swap Y coordinates
  22731. $tmp = $gradient['coords'][1];
  22732. $gradient['coords'][1] = $gradient['coords'][3];
  22733. $gradient['coords'][3] = $tmp;
  22734. // set transformation map for gradient
  22735. $cy = ($this->h - $y);
  22736. if ($gradient['type'] == 3) {
  22737. // circular gradient
  22738. $cy -= ($gradient['coords'][1] * ($w + $h));
  22739. $h = $w = max($w, $h);
  22740. } else {
  22741. $cy -= $h;
  22742. }
  22743. $this->_out(sprintf('%F 0 0 %F %F %F cm', ($w * $this->k), ($h * $this->k), ($x * $this->k), ($cy * $this->k)));
  22744. if ((is_array($gradient['stops']) || $gradient['stops'] instanceof Countable) && count($gradient['stops']) > 1) {
  22745. $this->Gradient($gradient['type'], $gradient['coords'], $gradient['stops']);
  22746. }
  22747. } elseif ($svgstyle['fill'] != 'none') {
  22748. $fill_color = TCPDF_COLORS::convertHTMLColorToDec($svgstyle['fill'], $this->spot_colors);
  22749. if ($svgstyle['fill-opacity'] != 1) {
  22750. $this->setAlpha($this->alpha['CA'], 'Normal', $svgstyle['fill-opacity'], false);
  22751. }
  22752. $this->setFillColorArray($fill_color);
  22753. if ($svgstyle['fill-rule'] == 'evenodd') {
  22754. $objstyle .= 'F*';
  22755. } else {
  22756. $objstyle .= 'F';
  22757. }
  22758. }
  22759. // stroke
  22760. if ($svgstyle['stroke'] != 'none') {
  22761. if ($svgstyle['stroke-opacity'] != 1) {
  22762. $this->setAlpha($svgstyle['stroke-opacity'], 'Normal', $this->alpha['ca'], false);
  22763. } elseif (preg_match('/rgba\(\d+%?,\s*\d+%?,\s*\d+%?,\s*(\d+(?:\.\d+)?)\)/i', $svgstyle['stroke'], $rgba_matches)) {
  22764. $this->setAlpha($rgba_matches[1], 'Normal', $this->alpha['ca'], false);
  22765. }
  22766. $stroke_style = array(
  22767. 'color' => TCPDF_COLORS::convertHTMLColorToDec($svgstyle['stroke'], $this->spot_colors),
  22768. 'width' => $this->getHTMLUnitToUnits($svgstyle['stroke-width'], 0, $this->svgunit, false),
  22769. 'cap' => $svgstyle['stroke-linecap'],
  22770. 'join' => $svgstyle['stroke-linejoin']
  22771. );
  22772. if (isset($svgstyle['stroke-dasharray']) AND !empty($svgstyle['stroke-dasharray']) AND ($svgstyle['stroke-dasharray'] != 'none')) {
  22773. $stroke_style['dash'] = $svgstyle['stroke-dasharray'];
  22774. }
  22775. $this->setLineStyle($stroke_style);
  22776. $objstyle .= 'D';
  22777. }
  22778. // font
  22779. $regs = array();
  22780. if (!empty($svgstyle['font'])) {
  22781. if (preg_match('/font-family[\s]*:[\s]*([^\;\"]*)/si', $svgstyle['font'], $regs)) {
  22782. $font_family = $this->getFontFamilyName($regs[1]);
  22783. } else {
  22784. $font_family = $this->getFontFamilyName($svgstyle['font-family']);
  22785. }
  22786. if (preg_match('/font-size[\s]*:[\s]*([^\s\;\"]*)/si', $svgstyle['font'], $regs)) {
  22787. $font_size = trim($regs[1]);
  22788. } else {
  22789. $font_size = $svgstyle['font-size'];
  22790. }
  22791. if (preg_match('/font-style[\s]*:[\s]*([^\s\;\"]*)/si', $svgstyle['font'], $regs)) {
  22792. $font_style = trim($regs[1]);
  22793. } else {
  22794. $font_style = $svgstyle['font-style'];
  22795. }
  22796. if (preg_match('/font-weight[\s]*:[\s]*([^\s\;\"]*)/si', $svgstyle['font'], $regs)) {
  22797. $font_weight = trim($regs[1]);
  22798. } else {
  22799. $font_weight = $svgstyle['font-weight'];
  22800. }
  22801. if (preg_match('/font-stretch[\s]*:[\s]*([^\s\;\"]*)/si', $svgstyle['font'], $regs)) {
  22802. $font_stretch = trim($regs[1]);
  22803. } else {
  22804. $font_stretch = $svgstyle['font-stretch'];
  22805. }
  22806. if (preg_match('/letter-spacing[\s]*:[\s]*([^\s\;\"]*)/si', $svgstyle['font'], $regs)) {
  22807. $font_spacing = trim($regs[1]);
  22808. } else {
  22809. $font_spacing = $svgstyle['letter-spacing'];
  22810. }
  22811. } else {
  22812. $font_family = $this->getFontFamilyName($svgstyle['font-family']);
  22813. $font_size = $svgstyle['font-size'];
  22814. $font_style = $svgstyle['font-style'];
  22815. $font_weight = $svgstyle['font-weight'];
  22816. $font_stretch = $svgstyle['font-stretch'];
  22817. $font_spacing = $svgstyle['letter-spacing'];
  22818. }
  22819. $font_size = $this->getHTMLFontUnits($font_size, $this->svgstyles[0]['font-size'], $prevsvgstyle['font-size'], $this->svgunit);
  22820. $font_stretch = $this->getCSSFontStretching($font_stretch, $svgstyle['font-stretch']);
  22821. $font_spacing = $this->getCSSFontSpacing($font_spacing, $svgstyle['letter-spacing']);
  22822. switch ($font_style) {
  22823. case 'italic': {
  22824. $font_style = 'I';
  22825. break;
  22826. }
  22827. case 'oblique': {
  22828. $font_style = 'I';
  22829. break;
  22830. }
  22831. default:
  22832. case 'normal': {
  22833. $font_style = '';
  22834. break;
  22835. }
  22836. }
  22837. switch ($font_weight) {
  22838. case 'bold':
  22839. case 'bolder': {
  22840. $font_style .= 'B';
  22841. break;
  22842. }
  22843. case 'normal': {
  22844. if ((substr($font_family, -1) == 'I') AND (substr($font_family, -2, 1) == 'B')) {
  22845. $font_family = substr($font_family, 0, -2).'I';
  22846. } elseif (substr($font_family, -1) == 'B') {
  22847. $font_family = substr($font_family, 0, -1);
  22848. }
  22849. break;
  22850. }
  22851. }
  22852. switch ($svgstyle['text-decoration']) {
  22853. case 'underline': {
  22854. $font_style .= 'U';
  22855. break;
  22856. }
  22857. case 'overline': {
  22858. $font_style .= 'O';
  22859. break;
  22860. }
  22861. case 'line-through': {
  22862. $font_style .= 'D';
  22863. break;
  22864. }
  22865. default:
  22866. case 'none': {
  22867. break;
  22868. }
  22869. }
  22870. $this->setFont($font_family, $font_style, $font_size);
  22871. $this->setFontStretching($font_stretch);
  22872. $this->setFontSpacing($font_spacing);
  22873. return $objstyle;
  22874. }
  22875. /**
  22876. * Draws an SVG path
  22877. * @param string $d attribute d of the path SVG element
  22878. * @param string $style Style of rendering. Possible values are:
  22879. * <ul>
  22880. * <li>D or empty string: Draw (default).</li>
  22881. * <li>F: Fill.</li>
  22882. * <li>F*: Fill using the even-odd rule to determine which regions lie inside the clipping path.</li>
  22883. * <li>DF or FD: Draw and fill.</li>
  22884. * <li>DF* or FD*: Draw and fill using the even-odd rule to determine which regions lie inside the clipping path.</li>
  22885. * <li>CNZ: Clipping mode (using the even-odd rule to determine which regions lie inside the clipping path).</li>
  22886. * <li>CEO: Clipping mode (using the nonzero winding number rule to determine which regions lie inside the clipping path).</li>
  22887. * </ul>
  22888. * @return array of container box measures (x, y, w, h)
  22889. * @author Nicola Asuni
  22890. * @since 5.0.000 (2010-05-02)
  22891. * @protected
  22892. */
  22893. protected function SVGPath($d, $style='') {
  22894. if ($this->state != 2) {
  22895. return;
  22896. }
  22897. // set fill/stroke style
  22898. $op = TCPDF_STATIC::getPathPaintOperator($style, '');
  22899. if (empty($op)) {
  22900. return;
  22901. }
  22902. $paths = array();
  22903. $d = preg_replace('/([0-9ACHLMQSTVZ])([\-\+])/si', '\\1 \\2', $d);
  22904. $d = preg_replace('/(\.[0-9]+)(\.)/s', '\\1 \\2', $d);
  22905. preg_match_all('/([ACHLMQSTVZ])[\s]*([^ACHLMQSTVZ\"]*)/si', $d, $paths, PREG_SET_ORDER);
  22906. $x = 0;
  22907. $y = 0;
  22908. $x1 = 0;
  22909. $y1 = 0;
  22910. $x2 = 0;
  22911. $y2 = 0;
  22912. $xmin = 2147483647;
  22913. $xmax = 0;
  22914. $ymin = 2147483647;
  22915. $ymax = 0;
  22916. $xinitial = 0;
  22917. $yinitial = 0;
  22918. $relcoord = false;
  22919. $minlen = (0.01 / $this->k); // minimum acceptable length (3 point)
  22920. $firstcmd = true; // used to print first point
  22921. // draw curve pieces
  22922. foreach ($paths as $key => $val) {
  22923. // get curve type
  22924. $cmd = trim($val[1]);
  22925. if (strtolower($cmd) == $cmd) {
  22926. // use relative coordinated instead of absolute
  22927. $relcoord = true;
  22928. $xoffset = $x;
  22929. $yoffset = $y;
  22930. } else {
  22931. $relcoord = false;
  22932. $xoffset = 0;
  22933. $yoffset = 0;
  22934. }
  22935. $params = array();
  22936. if (isset($val[2])) {
  22937. // get curve parameters
  22938. preg_match_all('/-?\d*\.?\d+/', trim($val[2]), $matches);
  22939. $rawparams = $matches[0];
  22940. $params = array();
  22941. foreach ($rawparams as $ck => $cp) {
  22942. $params[$ck] = $this->getHTMLUnitToUnits($cp, 0, $this->svgunit, false);
  22943. if (abs($params[$ck]) < $minlen) {
  22944. // approximate little values to zero
  22945. $params[$ck] = 0;
  22946. }
  22947. }
  22948. }
  22949. // store current origin point
  22950. $x0 = $x;
  22951. $y0 = $y;
  22952. switch (strtoupper($cmd)) {
  22953. case 'M': { // moveto
  22954. foreach ($params as $ck => $cp) {
  22955. if (($ck % 2) == 0) {
  22956. $x = $cp + $xoffset;
  22957. } else {
  22958. $y = $cp + $yoffset;
  22959. if ($firstcmd OR (abs($x0 - $x) >= $minlen) OR (abs($y0 - $y) >= $minlen)) {
  22960. if ($ck == 1) {
  22961. $this->_outPoint($x, $y);
  22962. $firstcmd = false;
  22963. $xinitial = $x;
  22964. $yinitial = $y;
  22965. } else {
  22966. $this->_outLine($x, $y);
  22967. }
  22968. $x0 = $x;
  22969. $y0 = $y;
  22970. }
  22971. $xmin = min($xmin, $x);
  22972. $ymin = min($ymin, $y);
  22973. $xmax = max($xmax, $x);
  22974. $ymax = max($ymax, $y);
  22975. if ($relcoord) {
  22976. $xoffset = $x;
  22977. $yoffset = $y;
  22978. }
  22979. }
  22980. }
  22981. break;
  22982. }
  22983. case 'L': { // lineto
  22984. foreach ($params as $ck => $cp) {
  22985. if (($ck % 2) == 0) {
  22986. $x = $cp + $xoffset;
  22987. } else {
  22988. $y = $cp + $yoffset;
  22989. if ((abs($x0 - $x) >= $minlen) OR (abs($y0 - $y) >= $minlen)) {
  22990. $this->_outLine($x, $y);
  22991. $x0 = $x;
  22992. $y0 = $y;
  22993. }
  22994. $xmin = min($xmin, $x);
  22995. $ymin = min($ymin, $y);
  22996. $xmax = max($xmax, $x);
  22997. $ymax = max($ymax, $y);
  22998. if ($relcoord) {
  22999. $xoffset = $x;
  23000. $yoffset = $y;
  23001. }
  23002. }
  23003. }
  23004. break;
  23005. }
  23006. case 'H': { // horizontal lineto
  23007. foreach ($params as $ck => $cp) {
  23008. $x = $cp + $xoffset;
  23009. if ((abs($x0 - $x) >= $minlen) OR (abs($y0 - $y) >= $minlen)) {
  23010. $this->_outLine($x, $y);
  23011. $x0 = $x;
  23012. $y0 = $y;
  23013. }
  23014. $xmin = min($xmin, $x);
  23015. $xmax = max($xmax, $x);
  23016. if ($relcoord) {
  23017. $xoffset = $x;
  23018. }
  23019. }
  23020. break;
  23021. }
  23022. case 'V': { // vertical lineto
  23023. foreach ($params as $ck => $cp) {
  23024. $y = $cp + $yoffset;
  23025. if ((abs($x0 - $x) >= $minlen) OR (abs($y0 - $y) >= $minlen)) {
  23026. $this->_outLine($x, $y);
  23027. $x0 = $x;
  23028. $y0 = $y;
  23029. }
  23030. $ymin = min($ymin, $y);
  23031. $ymax = max($ymax, $y);
  23032. if ($relcoord) {
  23033. $yoffset = $y;
  23034. }
  23035. }
  23036. break;
  23037. }
  23038. case 'C': { // curveto
  23039. foreach ($params as $ck => $cp) {
  23040. $params[$ck] = $cp;
  23041. if ((($ck + 1) % 6) == 0) {
  23042. $x1 = $params[($ck - 5)] + $xoffset;
  23043. $y1 = $params[($ck - 4)] + $yoffset;
  23044. $x2 = $params[($ck - 3)] + $xoffset;
  23045. $y2 = $params[($ck - 2)] + $yoffset;
  23046. $x = $params[($ck - 1)] + $xoffset;
  23047. $y = $params[($ck)] + $yoffset;
  23048. $this->_outCurve($x1, $y1, $x2, $y2, $x, $y);
  23049. $xmin = min($xmin, $x, $x1, $x2);
  23050. $ymin = min($ymin, $y, $y1, $y2);
  23051. $xmax = max($xmax, $x, $x1, $x2);
  23052. $ymax = max($ymax, $y, $y1, $y2);
  23053. if ($relcoord) {
  23054. $xoffset = $x;
  23055. $yoffset = $y;
  23056. }
  23057. }
  23058. }
  23059. break;
  23060. }
  23061. case 'S': { // shorthand/smooth curveto
  23062. foreach ($params as $ck => $cp) {
  23063. $params[$ck] = $cp;
  23064. if ((($ck + 1) % 4) == 0) {
  23065. if (($key > 0) AND ((strtoupper($paths[($key - 1)][1]) == 'C') OR (strtoupper($paths[($key - 1)][1]) == 'S'))) {
  23066. $x1 = (2 * $x) - $x2;
  23067. $y1 = (2 * $y) - $y2;
  23068. } else {
  23069. $x1 = $x;
  23070. $y1 = $y;
  23071. }
  23072. $x2 = $params[($ck - 3)] + $xoffset;
  23073. $y2 = $params[($ck - 2)] + $yoffset;
  23074. $x = $params[($ck - 1)] + $xoffset;
  23075. $y = $params[($ck)] + $yoffset;
  23076. $this->_outCurve($x1, $y1, $x2, $y2, $x, $y);
  23077. $xmin = min($xmin, $x, $x1, $x2);
  23078. $ymin = min($ymin, $y, $y1, $y2);
  23079. $xmax = max($xmax, $x, $x1, $x2);
  23080. $ymax = max($ymax, $y, $y1, $y2);
  23081. if ($relcoord) {
  23082. $xoffset = $x;
  23083. $yoffset = $y;
  23084. }
  23085. }
  23086. }
  23087. break;
  23088. }
  23089. case 'Q': { // quadratic Bezier curveto
  23090. foreach ($params as $ck => $cp) {
  23091. $params[$ck] = $cp;
  23092. if ((($ck + 1) % 4) == 0) {
  23093. // convert quadratic points to cubic points
  23094. $x1 = $params[($ck - 3)] + $xoffset;
  23095. $y1 = $params[($ck - 2)] + $yoffset;
  23096. $xa = ($x + (2 * $x1)) / 3;
  23097. $ya = ($y + (2 * $y1)) / 3;
  23098. $x = $params[($ck - 1)] + $xoffset;
  23099. $y = $params[($ck)] + $yoffset;
  23100. $xb = ($x + (2 * $x1)) / 3;
  23101. $yb = ($y + (2 * $y1)) / 3;
  23102. $this->_outCurve($xa, $ya, $xb, $yb, $x, $y);
  23103. $xmin = min($xmin, $x, $xa, $xb);
  23104. $ymin = min($ymin, $y, $ya, $yb);
  23105. $xmax = max($xmax, $x, $xa, $xb);
  23106. $ymax = max($ymax, $y, $ya, $yb);
  23107. if ($relcoord) {
  23108. $xoffset = $x;
  23109. $yoffset = $y;
  23110. }
  23111. }
  23112. }
  23113. break;
  23114. }
  23115. case 'T': { // shorthand/smooth quadratic Bezier curveto
  23116. foreach ($params as $ck => $cp) {
  23117. $params[$ck] = $cp;
  23118. if (($ck % 2) != 0) {
  23119. if (($key > 0) AND ((strtoupper($paths[($key - 1)][1]) == 'Q') OR (strtoupper($paths[($key - 1)][1]) == 'T'))) {
  23120. $x1 = (2 * $x) - $x1;
  23121. $y1 = (2 * $y) - $y1;
  23122. } else {
  23123. $x1 = $x;
  23124. $y1 = $y;
  23125. }
  23126. // convert quadratic points to cubic points
  23127. $xa = ($x + (2 * $x1)) / 3;
  23128. $ya = ($y + (2 * $y1)) / 3;
  23129. $x = $params[($ck - 1)] + $xoffset;
  23130. $y = $params[($ck)] + $yoffset;
  23131. $xb = ($x + (2 * $x1)) / 3;
  23132. $yb = ($y + (2 * $y1)) / 3;
  23133. $this->_outCurve($xa, $ya, $xb, $yb, $x, $y);
  23134. $xmin = min($xmin, $x, $xa, $xb);
  23135. $ymin = min($ymin, $y, $ya, $yb);
  23136. $xmax = max($xmax, $x, $xa, $xb);
  23137. $ymax = max($ymax, $y, $ya, $yb);
  23138. if ($relcoord) {
  23139. $xoffset = $x;
  23140. $yoffset = $y;
  23141. }
  23142. }
  23143. }
  23144. break;
  23145. }
  23146. case 'A': { // elliptical arc
  23147. foreach ($params as $ck => $cp) {
  23148. $params[$ck] = $cp;
  23149. if ((($ck + 1) % 7) == 0) {
  23150. $x0 = $x;
  23151. $y0 = $y;
  23152. $rx = max(abs($params[($ck - 6)]), .000000001);
  23153. $ry = max(abs($params[($ck - 5)]), .000000001);
  23154. $ang = -$rawparams[($ck - 4)];
  23155. $angle = deg2rad($ang);
  23156. $fa = $rawparams[($ck - 3)]; // large-arc-flag
  23157. $fs = $rawparams[($ck - 2)]; // sweep-flag
  23158. $x = $params[($ck - 1)] + $xoffset;
  23159. $y = $params[$ck] + $yoffset;
  23160. if ((abs($x0 - $x) < $minlen) AND (abs($y0 - $y) < $minlen)) {
  23161. // endpoints are almost identical
  23162. $xmin = min($xmin, $x);
  23163. $ymin = min($ymin, $y);
  23164. $xmax = max($xmax, $x);
  23165. $ymax = max($ymax, $y);
  23166. } else {
  23167. $cos_ang = cos($angle);
  23168. $sin_ang = sin($angle);
  23169. $a = (($x0 - $x) / 2);
  23170. $b = (($y0 - $y) / 2);
  23171. $xa = ($a * $cos_ang) - ($b * $sin_ang);
  23172. $ya = ($a * $sin_ang) + ($b * $cos_ang);
  23173. $rx2 = $rx * $rx;
  23174. $ry2 = $ry * $ry;
  23175. $xa2 = $xa * $xa;
  23176. $ya2 = $ya * $ya;
  23177. $delta = ($xa2 / $rx2) + ($ya2 / $ry2);
  23178. if ($delta > 1) {
  23179. $rx *= sqrt($delta);
  23180. $ry *= sqrt($delta);
  23181. $rx2 = $rx * $rx;
  23182. $ry2 = $ry * $ry;
  23183. }
  23184. $numerator = (($rx2 * $ry2) - ($rx2 * $ya2) - ($ry2 * $xa2));
  23185. if ($numerator < 0) {
  23186. $root = 0;
  23187. } else {
  23188. $root = sqrt($numerator / (($rx2 * $ya2) + ($ry2 * $xa2)));
  23189. }
  23190. if ($fa == $fs){
  23191. $root *= -1;
  23192. }
  23193. $cax = $root * (($rx * $ya) / $ry);
  23194. $cay = -$root * (($ry * $xa) / $rx);
  23195. // coordinates of ellipse center
  23196. $cx = ($cax * $cos_ang) - ($cay * $sin_ang) + (($x0 + $x) / 2);
  23197. $cy = ($cax * $sin_ang) + ($cay * $cos_ang) + (($y0 + $y) / 2);
  23198. // get angles
  23199. $angs = TCPDF_STATIC::getVectorsAngle(1, 0, (($xa - $cax) / $rx), (($cay - $ya) / $ry));
  23200. $dang = TCPDF_STATIC::getVectorsAngle((($xa - $cax) / $rx), (($ya - $cay) / $ry), ((-$xa - $cax) / $rx), ((-$ya - $cay) / $ry));
  23201. if (($fs == 0) AND ($dang > 0)) {
  23202. $dang -= (2 * M_PI);
  23203. } elseif (($fs == 1) AND ($dang < 0)) {
  23204. $dang += (2 * M_PI);
  23205. }
  23206. $angf = $angs - $dang;
  23207. if ((($fs == 0) AND ($angs > $angf)) OR (($fs == 1) AND ($angs < $angf))) {
  23208. // reverse angles
  23209. $tmp = $angs;
  23210. $angs = $angf;
  23211. $angf = $tmp;
  23212. }
  23213. $angs = round(rad2deg($angs), 6);
  23214. $angf = round(rad2deg($angf), 6);
  23215. // covent angles to positive values
  23216. if (($angs < 0) AND ($angf < 0)) {
  23217. $angs += 360;
  23218. $angf += 360;
  23219. }
  23220. $pie = false;
  23221. if (($key == 0) AND (isset($paths[($key + 1)][1])) AND (trim($paths[($key + 1)][1]) == 'z')) {
  23222. $pie = true;
  23223. }
  23224. list($axmin, $aymin, $axmax, $aymax) = $this->_outellipticalarc($cx, $cy, $rx, $ry, $ang, $angs, $angf, $pie, 2, false, ($fs == 0), true);
  23225. $xmin = min($xmin, $x, $axmin);
  23226. $ymin = min($ymin, $y, $aymin);
  23227. $xmax = max($xmax, $x, $axmax);
  23228. $ymax = max($ymax, $y, $aymax);
  23229. }
  23230. if ($relcoord) {
  23231. $xoffset = $x;
  23232. $yoffset = $y;
  23233. }
  23234. }
  23235. }
  23236. break;
  23237. }
  23238. case 'Z': {
  23239. $this->_out('h');
  23240. $x = $x0 = $xinitial;
  23241. $y = $y0 = $yinitial;
  23242. break;
  23243. }
  23244. }
  23245. $firstcmd = false;
  23246. } // end foreach
  23247. $this->_out($op);
  23248. return array($xmin, $ymin, ($xmax - $xmin), ($ymax - $ymin));
  23249. }
  23250. /**
  23251. * Return the tag name without the namespace
  23252. * @param string $name Tag name
  23253. * @protected
  23254. */
  23255. protected function removeTagNamespace($name) {
  23256. if(strpos($name, ':') !== false) {
  23257. $parts = explode(':', $name);
  23258. return $parts[(sizeof($parts) - 1)];
  23259. }
  23260. return $name;
  23261. }
  23262. /**
  23263. * Sets the opening SVG element handler function for the XML parser. (*** TO BE COMPLETED ***)
  23264. * @param resource|string $parser The first parameter, parser, is a reference to the XML parser calling the handler.
  23265. * @param string $name The second parameter, name, contains the name of the element for which this handler is called. If case-folding is in effect for this parser, the element name will be in uppercase letters.
  23266. * @param array $attribs The third parameter, attribs, contains an associative array with the element's attributes (if any). The keys of this array are the attribute names, the values are the attribute values. Attribute names are case-folded on the same criteria as element names. Attribute values are not case-folded. The original order of the attributes can be retrieved by walking through attribs the normal way, using each(). The first key in the array was the first attribute, and so on.
  23267. * @param array $ctm tranformation matrix for clipping mode (starting transformation matrix).
  23268. * @author Nicola Asuni
  23269. * @since 5.0.000 (2010-05-02)
  23270. * @protected
  23271. */
  23272. protected function startSVGElementHandler($parser, $name, $attribs, $ctm=array()) {
  23273. $name = $this->removeTagNamespace($name);
  23274. // check if we are in clip mode
  23275. if ($this->svgclipmode) {
  23276. $this->svgclippaths[$this->svgclipid][] = array('name' => $name, 'attribs' => $attribs, 'tm' => $this->svgcliptm[$this->svgclipid]);
  23277. return;
  23278. }
  23279. if ($this->svgdefsmode AND !in_array($name, array('clipPath', 'linearGradient', 'radialGradient', 'stop'))) {
  23280. if (isset($attribs['id'])) {
  23281. $attribs['child_elements'] = array();
  23282. $this->svgdefs[$attribs['id']] = array('name' => $name, 'attribs' => $attribs);
  23283. return;
  23284. }
  23285. if (end($this->svgdefs) !== FALSE) {
  23286. $last_svgdefs_id = key($this->svgdefs);
  23287. if (isset($this->svgdefs[$last_svgdefs_id]['attribs']['child_elements'])) {
  23288. $attribs['id'] = 'DF_'.(count($this->svgdefs[$last_svgdefs_id]['attribs']['child_elements']) + 1);
  23289. $this->svgdefs[$last_svgdefs_id]['attribs']['child_elements'][$attribs['id']] = array('name' => $name, 'attribs' => $attribs);
  23290. return;
  23291. }
  23292. }
  23293. return;
  23294. }
  23295. $clipping = false;
  23296. if ($parser == 'clip-path') {
  23297. // set clipping mode
  23298. $clipping = true;
  23299. }
  23300. // get styling properties
  23301. $prev_svgstyle = $this->svgstyles[max(0,(count($this->svgstyles) - 1))]; // previous style
  23302. $svgstyle = $this->svgstyles[0]; // set default style
  23303. if ($clipping AND !isset($attribs['fill']) AND (!isset($attribs['style']) OR (!preg_match('/[;\"\s]{1}fill[\s]*:[\s]*([^;\"]*)/si', $attribs['style'], $attrval)))) {
  23304. // default fill attribute for clipping
  23305. $attribs['fill'] = 'none';
  23306. }
  23307. if (isset($attribs['style']) AND !TCPDF_STATIC::empty_string($attribs['style']) AND ($attribs['style'][0] != ';')) {
  23308. // fix style for regular expression
  23309. $attribs['style'] = ';'.$attribs['style'];
  23310. }
  23311. foreach ($prev_svgstyle as $key => $val) {
  23312. if (in_array($key, TCPDF_IMAGES::$svginheritprop)) {
  23313. // inherit previous value
  23314. $svgstyle[$key] = $val;
  23315. }
  23316. if (isset($attribs[$key]) AND !TCPDF_STATIC::empty_string($attribs[$key])) {
  23317. // specific attribute settings
  23318. if ($attribs[$key] == 'inherit') {
  23319. $svgstyle[$key] = $val;
  23320. } else {
  23321. $svgstyle[$key] = $attribs[$key];
  23322. }
  23323. } elseif (isset($attribs['style']) AND !TCPDF_STATIC::empty_string($attribs['style'])) {
  23324. // CSS style syntax
  23325. $attrval = array();
  23326. if (preg_match('/[;\"\s]{1}'.$key.'[\s]*:[\s]*([^;\"]*)/si', $attribs['style'], $attrval) AND isset($attrval[1])) {
  23327. if ($attrval[1] == 'inherit') {
  23328. $svgstyle[$key] = $val;
  23329. } else {
  23330. $svgstyle[$key] = $attrval[1];
  23331. }
  23332. }
  23333. }
  23334. }
  23335. // transformation matrix
  23336. if (!empty($ctm)) {
  23337. $tm = $ctm;
  23338. } else {
  23339. $tm = array(1,0,0,1,0,0);
  23340. }
  23341. if (isset($attribs['transform']) AND !empty($attribs['transform'])) {
  23342. $tm = TCPDF_STATIC::getTransformationMatrixProduct($tm, TCPDF_STATIC::getSVGTransformMatrix($attribs['transform']));
  23343. }
  23344. $svgstyle['transfmatrix'] = $tm;
  23345. $invisible = false;
  23346. if (($svgstyle['visibility'] == 'hidden') OR ($svgstyle['visibility'] == 'collapse') OR ($svgstyle['display'] == 'none')) {
  23347. // the current graphics element is invisible (nothing is painted)
  23348. $invisible = true;
  23349. }
  23350. // process tag
  23351. switch($name) {
  23352. case 'defs': {
  23353. $this->svgdefsmode = true;
  23354. break;
  23355. }
  23356. // clipPath
  23357. case 'clipPath': {
  23358. if ($invisible) {
  23359. break;
  23360. }
  23361. $this->svgclipmode = true;
  23362. if (!isset($attribs['id'])) {
  23363. $attribs['id'] = 'CP_'.(count($this->svgcliptm) + 1);
  23364. }
  23365. $this->svgclipid = $attribs['id'];
  23366. $this->svgclippaths[$this->svgclipid] = array();
  23367. $this->svgcliptm[$this->svgclipid] = $tm;
  23368. break;
  23369. }
  23370. case 'svg': {
  23371. // start of SVG object
  23372. if(++$this->svg_tag_depth <= 1) {
  23373. break;
  23374. }
  23375. // inner SVG
  23376. array_push($this->svgstyles, $svgstyle);
  23377. $this->StartTransform();
  23378. $svgX = (isset($attribs['x'])?$attribs['x']:0);
  23379. $svgY = (isset($attribs['y'])?$attribs['y']:0);
  23380. $svgW = (isset($attribs['width'])?$attribs['width']:0);
  23381. $svgH = (isset($attribs['height'])?$attribs['height']:0);
  23382. // set x, y position using transform matrix
  23383. $tm = TCPDF_STATIC::getTransformationMatrixProduct($tm, array( 1, 0, 0, 1, $svgX, $svgY));
  23384. $this->SVGTransform($tm);
  23385. // set clipping for width and height
  23386. $x = 0;
  23387. $y = 0;
  23388. $w = (isset($attribs['width'])?$this->getHTMLUnitToUnits($attribs['width'], 0, $this->svgunit, false):$this->w);
  23389. $h = (isset($attribs['height'])?$this->getHTMLUnitToUnits($attribs['height'], 0, $this->svgunit, false):$this->h);
  23390. // draw clipping rect
  23391. $this->Rect($x, $y, $w, $h, 'CNZ', array(), array());
  23392. // parse viewbox, calculate extra transformation matrix
  23393. if (isset($attribs['viewBox'])) {
  23394. $tmp = array();
  23395. preg_match_all("/[0-9]+/", $attribs['viewBox'], $tmp);
  23396. $tmp = $tmp[0];
  23397. if (sizeof($tmp) == 4) {
  23398. $vx = $tmp[0];
  23399. $vy = $tmp[1];
  23400. $vw = $tmp[2];
  23401. $vh = $tmp[3];
  23402. // get aspect ratio
  23403. $tmp = array();
  23404. $aspectX = 'xMid';
  23405. $aspectY = 'YMid';
  23406. $fit = 'meet';
  23407. if (isset($attribs['preserveAspectRatio'])) {
  23408. if($attribs['preserveAspectRatio'] == 'none') {
  23409. $fit = 'none';
  23410. } else {
  23411. preg_match_all('/[a-zA-Z]+/', $attribs['preserveAspectRatio'], $tmp);
  23412. $tmp = $tmp[0];
  23413. if ((sizeof($tmp) == 2) AND (strlen($tmp[0]) == 8) AND (in_array($tmp[1], array('meet', 'slice', 'none')))) {
  23414. $aspectX = substr($tmp[0], 0, 4);
  23415. $aspectY = substr($tmp[0], 4, 4);
  23416. $fit = $tmp[1];
  23417. }
  23418. }
  23419. }
  23420. $wr = ($svgW / $vw);
  23421. $hr = ($svgH / $vh);
  23422. $ax = $ay = 0;
  23423. if ((($fit == 'meet') AND ($hr < $wr)) OR (($fit == 'slice') AND ($hr > $wr))) {
  23424. if ($aspectX == 'xMax') {
  23425. $ax = (($vw * ($wr / $hr)) - $vw);
  23426. }
  23427. if ($aspectX == 'xMid') {
  23428. $ax = ((($vw * ($wr / $hr)) - $vw) / 2);
  23429. }
  23430. $wr = $hr;
  23431. } elseif ((($fit == 'meet') AND ($hr > $wr)) OR (($fit == 'slice') AND ($hr < $wr))) {
  23432. if ($aspectY == 'YMax') {
  23433. $ay = (($vh * ($hr / $wr)) - $vh);
  23434. }
  23435. if ($aspectY == 'YMid') {
  23436. $ay = ((($vh * ($hr / $wr)) - $vh) / 2);
  23437. }
  23438. $hr = $wr;
  23439. }
  23440. $newtm = array($wr, 0, 0, $hr, (($wr * ($ax - $vx)) - $svgX), (($hr * ($ay - $vy)) - $svgY));
  23441. $tm = TCPDF_STATIC::getTransformationMatrixProduct($tm, $newtm);
  23442. $this->SVGTransform($tm);
  23443. }
  23444. }
  23445. $this->setSVGStyles($svgstyle, $prev_svgstyle);
  23446. break;
  23447. }
  23448. case 'g': {
  23449. // group together related graphics elements
  23450. array_push($this->svgstyles, $svgstyle);
  23451. $this->StartTransform();
  23452. $x = (isset($attribs['x'])?$attribs['x']:0);
  23453. $y = (isset($attribs['y'])?$attribs['y']:0);
  23454. $w = 1;//(isset($attribs['width'])?$attribs['width']:1);
  23455. $h = 1;//(isset($attribs['height'])?$attribs['height']:1);
  23456. $tm = TCPDF_STATIC::getTransformationMatrixProduct($tm, array($w, 0, 0, $h, $x, $y));
  23457. $this->SVGTransform($tm);
  23458. $this->setSVGStyles($svgstyle, $prev_svgstyle);
  23459. break;
  23460. }
  23461. case 'linearGradient': {
  23462. if ($this->pdfa_mode && $this->pdfa_version < 2) {
  23463. break;
  23464. }
  23465. if (!isset($attribs['id'])) {
  23466. $attribs['id'] = 'GR_'.(count($this->svggradients) + 1);
  23467. }
  23468. $this->svggradientid = $attribs['id'];
  23469. $this->svggradients[$this->svggradientid] = array();
  23470. $this->svggradients[$this->svggradientid]['type'] = 2;
  23471. $this->svggradients[$this->svggradientid]['stops'] = array();
  23472. if (isset($attribs['gradientUnits'])) {
  23473. $this->svggradients[$this->svggradientid]['gradientUnits'] = $attribs['gradientUnits'];
  23474. } else {
  23475. $this->svggradients[$this->svggradientid]['gradientUnits'] = 'objectBoundingBox';
  23476. }
  23477. //$attribs['spreadMethod']
  23478. if (((!isset($attribs['x1'])) AND (!isset($attribs['y1'])) AND (!isset($attribs['x2'])) AND (!isset($attribs['y2'])))
  23479. OR ((isset($attribs['x1']) AND (substr($attribs['x1'], -1) == '%'))
  23480. OR (isset($attribs['y1']) AND (substr($attribs['y1'], -1) == '%'))
  23481. OR (isset($attribs['x2']) AND (substr($attribs['x2'], -1) == '%'))
  23482. OR (isset($attribs['y2']) AND (substr($attribs['y2'], -1) == '%')))) {
  23483. $this->svggradients[$this->svggradientid]['mode'] = 'percentage';
  23484. } else {
  23485. $this->svggradients[$this->svggradientid]['mode'] = 'measure';
  23486. }
  23487. $x1 = (isset($attribs['x1'])?$attribs['x1']:'0');
  23488. $y1 = (isset($attribs['y1'])?$attribs['y1']:'0');
  23489. $x2 = (isset($attribs['x2'])?$attribs['x2']:'100');
  23490. $y2 = (isset($attribs['y2'])?$attribs['y2']:'0');
  23491. if (isset($attribs['gradientTransform'])) {
  23492. $this->svggradients[$this->svggradientid]['gradientTransform'] = TCPDF_STATIC::getSVGTransformMatrix($attribs['gradientTransform']);
  23493. }
  23494. $this->svggradients[$this->svggradientid]['coords'] = array($x1, $y1, $x2, $y2);
  23495. if (isset($attribs['xlink:href']) AND !empty($attribs['xlink:href'])) {
  23496. // gradient is defined on another place
  23497. $this->svggradients[$this->svggradientid]['xref'] = substr($attribs['xlink:href'], 1);
  23498. }
  23499. break;
  23500. }
  23501. case 'radialGradient': {
  23502. if ($this->pdfa_mode && $this->pdfa_version < 2) {
  23503. break;
  23504. }
  23505. if (!isset($attribs['id'])) {
  23506. $attribs['id'] = 'GR_'.(count($this->svggradients) + 1);
  23507. }
  23508. $this->svggradientid = $attribs['id'];
  23509. $this->svggradients[$this->svggradientid] = array();
  23510. $this->svggradients[$this->svggradientid]['type'] = 3;
  23511. $this->svggradients[$this->svggradientid]['stops'] = array();
  23512. if (isset($attribs['gradientUnits'])) {
  23513. $this->svggradients[$this->svggradientid]['gradientUnits'] = $attribs['gradientUnits'];
  23514. } else {
  23515. $this->svggradients[$this->svggradientid]['gradientUnits'] = 'objectBoundingBox';
  23516. }
  23517. //$attribs['spreadMethod']
  23518. if (((!isset($attribs['cx'])) AND (!isset($attribs['cy'])))
  23519. OR ((isset($attribs['cx']) AND (substr($attribs['cx'], -1) == '%'))
  23520. OR (isset($attribs['cy']) AND (substr($attribs['cy'], -1) == '%')))) {
  23521. $this->svggradients[$this->svggradientid]['mode'] = 'percentage';
  23522. } elseif (isset($attribs['r']) AND is_numeric($attribs['r']) AND ($attribs['r']) <= 1) {
  23523. $this->svggradients[$this->svggradientid]['mode'] = 'ratio';
  23524. } else {
  23525. $this->svggradients[$this->svggradientid]['mode'] = 'measure';
  23526. }
  23527. $cx = (isset($attribs['cx']) ? $attribs['cx'] : 0.5);
  23528. $cy = (isset($attribs['cy']) ? $attribs['cy'] : 0.5);
  23529. $fx = (isset($attribs['fx']) ? $attribs['fx'] : $cx);
  23530. $fy = (isset($attribs['fy']) ? $attribs['fy'] : $cy);
  23531. $r = (isset($attribs['r']) ? $attribs['r'] : 0.5);
  23532. if (isset($attribs['gradientTransform'])) {
  23533. $this->svggradients[$this->svggradientid]['gradientTransform'] = TCPDF_STATIC::getSVGTransformMatrix($attribs['gradientTransform']);
  23534. }
  23535. $this->svggradients[$this->svggradientid]['coords'] = array($cx, $cy, $fx, $fy, $r);
  23536. if (isset($attribs['xlink:href']) AND !empty($attribs['xlink:href'])) {
  23537. // gradient is defined on another place
  23538. $this->svggradients[$this->svggradientid]['xref'] = substr($attribs['xlink:href'], 1);
  23539. }
  23540. break;
  23541. }
  23542. case 'stop': {
  23543. // gradient stops
  23544. if (substr($attribs['offset'], -1) == '%') {
  23545. $offset = floatval(substr($attribs['offset'], 0, -1)) / 100;
  23546. } else {
  23547. $offset = floatval($attribs['offset']);
  23548. if ($offset > 1) {
  23549. $offset /= 100;
  23550. }
  23551. }
  23552. $stop_color = isset($svgstyle['stop-color'])?TCPDF_COLORS::convertHTMLColorToDec($svgstyle['stop-color'], $this->spot_colors):'black';
  23553. $opacity = isset($svgstyle['stop-opacity'])?$svgstyle['stop-opacity']:1;
  23554. $this->svggradients[$this->svggradientid]['stops'][] = array('offset' => $offset, 'color' => $stop_color, 'opacity' => $opacity);
  23555. break;
  23556. }
  23557. // paths
  23558. case 'path': {
  23559. if ($invisible) {
  23560. break;
  23561. }
  23562. if (isset($attribs['d'])) {
  23563. $d = trim($attribs['d']);
  23564. if (!empty($d)) {
  23565. $x = (isset($attribs['x'])?$attribs['x']:0);
  23566. $y = (isset($attribs['y'])?$attribs['y']:0);
  23567. $w = (isset($attribs['width'])?$attribs['width']:1);
  23568. $h = (isset($attribs['height'])?$attribs['height']:1);
  23569. $tm = TCPDF_STATIC::getTransformationMatrixProduct($tm, array($w, 0, 0, $h, $x, $y));
  23570. if ($clipping) {
  23571. $this->SVGTransform($tm);
  23572. $this->SVGPath($d, 'CNZ');
  23573. } else {
  23574. $this->StartTransform();
  23575. $this->SVGTransform($tm);
  23576. $obstyle = $this->setSVGStyles($svgstyle, $prev_svgstyle, $x, $y, $w, $h, 'SVGPath', array($d, 'CNZ'));
  23577. if (!empty($obstyle)) {
  23578. $this->SVGPath($d, $obstyle);
  23579. }
  23580. $this->StopTransform();
  23581. }
  23582. }
  23583. }
  23584. break;
  23585. }
  23586. // shapes
  23587. case 'rect': {
  23588. if ($invisible) {
  23589. break;
  23590. }
  23591. $x = (isset($attribs['x'])?$this->getHTMLUnitToUnits($attribs['x'], 0, $this->svgunit, false):0);
  23592. $y = (isset($attribs['y'])?$this->getHTMLUnitToUnits($attribs['y'], 0, $this->svgunit, false):0);
  23593. $w = (isset($attribs['width'])?$this->getHTMLUnitToUnits($attribs['width'], 0, $this->svgunit, false):0);
  23594. $h = (isset($attribs['height'])?$this->getHTMLUnitToUnits($attribs['height'], 0, $this->svgunit, false):0);
  23595. $rx = (isset($attribs['rx'])?$this->getHTMLUnitToUnits($attribs['rx'], 0, $this->svgunit, false):0);
  23596. $ry = (isset($attribs['ry'])?$this->getHTMLUnitToUnits($attribs['ry'], 0, $this->svgunit, false):$rx);
  23597. if ($clipping) {
  23598. $this->SVGTransform($tm);
  23599. $this->RoundedRectXY($x, $y, $w, $h, $rx, $ry, '1111', 'CNZ', array(), array());
  23600. } else {
  23601. $this->StartTransform();
  23602. $this->SVGTransform($tm);
  23603. $obstyle = $this->setSVGStyles($svgstyle, $prev_svgstyle, $x, $y, $w, $h, 'RoundedRectXY', array($x, $y, $w, $h, $rx, $ry, '1111', 'CNZ'));
  23604. if (!empty($obstyle)) {
  23605. $this->RoundedRectXY($x, $y, $w, $h, $rx, $ry, '1111', $obstyle, array(), array());
  23606. }
  23607. $this->StopTransform();
  23608. }
  23609. break;
  23610. }
  23611. case 'circle': {
  23612. if ($invisible) {
  23613. break;
  23614. }
  23615. $r = (isset($attribs['r']) ? $this->getHTMLUnitToUnits($attribs['r'], 0, $this->svgunit, false) : 0);
  23616. $cx = (isset($attribs['cx']) ? $this->getHTMLUnitToUnits($attribs['cx'], 0, $this->svgunit, false) : (isset($attribs['x']) ? $this->getHTMLUnitToUnits($attribs['x'], 0, $this->svgunit, false) : 0));
  23617. $cy = (isset($attribs['cy']) ? $this->getHTMLUnitToUnits($attribs['cy'], 0, $this->svgunit, false) : (isset($attribs['y']) ? $this->getHTMLUnitToUnits($attribs['y'], 0, $this->svgunit, false) : 0));
  23618. $x = ($cx - $r);
  23619. $y = ($cy - $r);
  23620. $w = (2 * $r);
  23621. $h = $w;
  23622. if ($clipping) {
  23623. $this->SVGTransform($tm);
  23624. $this->Circle($cx, $cy, $r, 0, 360, 'CNZ', array(), array(), 8);
  23625. } else {
  23626. $this->StartTransform();
  23627. $this->SVGTransform($tm);
  23628. $obstyle = $this->setSVGStyles($svgstyle, $prev_svgstyle, $x, $y, $w, $h, 'Circle', array($cx, $cy, $r, 0, 360, 'CNZ'));
  23629. if (!empty($obstyle)) {
  23630. $this->Circle($cx, $cy, $r, 0, 360, $obstyle, array(), array(), 8);
  23631. }
  23632. $this->StopTransform();
  23633. }
  23634. break;
  23635. }
  23636. case 'ellipse': {
  23637. if ($invisible) {
  23638. break;
  23639. }
  23640. $rx = (isset($attribs['rx']) ? $this->getHTMLUnitToUnits($attribs['rx'], 0, $this->svgunit, false) : 0);
  23641. $ry = (isset($attribs['ry']) ? $this->getHTMLUnitToUnits($attribs['ry'], 0, $this->svgunit, false) : 0);
  23642. $cx = (isset($attribs['cx']) ? $this->getHTMLUnitToUnits($attribs['cx'], 0, $this->svgunit, false) : (isset($attribs['x']) ? $this->getHTMLUnitToUnits($attribs['x'], 0, $this->svgunit, false) : 0));
  23643. $cy = (isset($attribs['cy']) ? $this->getHTMLUnitToUnits($attribs['cy'], 0, $this->svgunit, false) : (isset($attribs['y']) ? $this->getHTMLUnitToUnits($attribs['y'], 0, $this->svgunit, false) : 0));
  23644. $x = ($cx - $rx);
  23645. $y = ($cy - $ry);
  23646. $w = (2 * $rx);
  23647. $h = (2 * $ry);
  23648. if ($clipping) {
  23649. $this->SVGTransform($tm);
  23650. $this->Ellipse($cx, $cy, $rx, $ry, 0, 0, 360, 'CNZ', array(), array(), 8);
  23651. } else {
  23652. $this->StartTransform();
  23653. $this->SVGTransform($tm);
  23654. $obstyle = $this->setSVGStyles($svgstyle, $prev_svgstyle, $x, $y, $w, $h, 'Ellipse', array($cx, $cy, $rx, $ry, 0, 0, 360, 'CNZ'));
  23655. if (!empty($obstyle)) {
  23656. $this->Ellipse($cx, $cy, $rx, $ry, 0, 0, 360, $obstyle, array(), array(), 8);
  23657. }
  23658. $this->StopTransform();
  23659. }
  23660. break;
  23661. }
  23662. case 'line': {
  23663. if ($invisible) {
  23664. break;
  23665. }
  23666. $x1 = (isset($attribs['x1'])?$this->getHTMLUnitToUnits($attribs['x1'], 0, $this->svgunit, false):0);
  23667. $y1 = (isset($attribs['y1'])?$this->getHTMLUnitToUnits($attribs['y1'], 0, $this->svgunit, false):0);
  23668. $x2 = (isset($attribs['x2'])?$this->getHTMLUnitToUnits($attribs['x2'], 0, $this->svgunit, false):0);
  23669. $y2 = (isset($attribs['y2'])?$this->getHTMLUnitToUnits($attribs['y2'], 0, $this->svgunit, false):0);
  23670. $x = $x1;
  23671. $y = $y1;
  23672. $w = abs($x2 - $x1);
  23673. $h = abs($y2 - $y1);
  23674. if (!$clipping) {
  23675. $this->StartTransform();
  23676. $this->SVGTransform($tm);
  23677. $obstyle = $this->setSVGStyles($svgstyle, $prev_svgstyle, $x, $y, $w, $h, 'Line', array($x1, $y1, $x2, $y2));
  23678. $this->Line($x1, $y1, $x2, $y2);
  23679. $this->StopTransform();
  23680. }
  23681. break;
  23682. }
  23683. case 'polyline':
  23684. case 'polygon': {
  23685. if ($invisible) {
  23686. break;
  23687. }
  23688. $points = (isset($attribs['points'])?$attribs['points']:'0 0');
  23689. $points = trim($points);
  23690. // note that point may use a complex syntax not covered here
  23691. $points = preg_split('/[\,\s]+/si', $points);
  23692. if (count($points) < 4) {
  23693. break;
  23694. }
  23695. $p = array();
  23696. $xmin = 2147483647;
  23697. $xmax = 0;
  23698. $ymin = 2147483647;
  23699. $ymax = 0;
  23700. foreach ($points as $key => $val) {
  23701. $p[$key] = $this->getHTMLUnitToUnits($val, 0, $this->svgunit, false);
  23702. if (($key % 2) == 0) {
  23703. // X coordinate
  23704. $xmin = min($xmin, $p[$key]);
  23705. $xmax = max($xmax, $p[$key]);
  23706. } else {
  23707. // Y coordinate
  23708. $ymin = min($ymin, $p[$key]);
  23709. $ymax = max($ymax, $p[$key]);
  23710. }
  23711. }
  23712. $x = $xmin;
  23713. $y = $ymin;
  23714. $w = ($xmax - $xmin);
  23715. $h = ($ymax - $ymin);
  23716. if ($name == 'polyline') {
  23717. $this->StartTransform();
  23718. $this->SVGTransform($tm);
  23719. $obstyle = $this->setSVGStyles($svgstyle, $prev_svgstyle, $x, $y, $w, $h, 'PolyLine', array($p, 'CNZ'));
  23720. if (!empty($obstyle)) {
  23721. $this->PolyLine($p, $obstyle, array(), array());
  23722. }
  23723. $this->StopTransform();
  23724. } else { // polygon
  23725. if ($clipping) {
  23726. $this->SVGTransform($tm);
  23727. $this->Polygon($p, 'CNZ', array(), array(), true);
  23728. } else {
  23729. $this->StartTransform();
  23730. $this->SVGTransform($tm);
  23731. $obstyle = $this->setSVGStyles($svgstyle, $prev_svgstyle, $x, $y, $w, $h, 'Polygon', array($p, 'CNZ'));
  23732. if (!empty($obstyle)) {
  23733. $this->Polygon($p, $obstyle, array(), array(), true);
  23734. }
  23735. $this->StopTransform();
  23736. }
  23737. }
  23738. break;
  23739. }
  23740. // image
  23741. case 'image': {
  23742. if ($invisible) {
  23743. break;
  23744. }
  23745. if (!isset($attribs['xlink:href']) OR empty($attribs['xlink:href'])) {
  23746. break;
  23747. }
  23748. $x = (isset($attribs['x'])?$this->getHTMLUnitToUnits($attribs['x'], 0, $this->svgunit, false):0);
  23749. $y = (isset($attribs['y'])?$this->getHTMLUnitToUnits($attribs['y'], 0, $this->svgunit, false):0);
  23750. $w = (isset($attribs['width'])?$this->getHTMLUnitToUnits($attribs['width'], 0, $this->svgunit, false):0);
  23751. $h = (isset($attribs['height'])?$this->getHTMLUnitToUnits($attribs['height'], 0, $this->svgunit, false):0);
  23752. $img = $attribs['xlink:href'];
  23753. if (!$clipping) {
  23754. $this->StartTransform();
  23755. $this->SVGTransform($tm);
  23756. $obstyle = $this->setSVGStyles($svgstyle, $prev_svgstyle, $x, $y, $w, $h);
  23757. if (preg_match('/^data:image\/[^;]+;base64,/', $img, $m) > 0) {
  23758. // embedded image encoded as base64
  23759. $img = '@'.base64_decode(substr($img, strlen($m[0])));
  23760. } else {
  23761. // fix image path
  23762. if (!TCPDF_STATIC::empty_string($this->svgdir) AND (($img[0] == '.') OR (basename($img) == $img))) {
  23763. // replace relative path with full server path
  23764. $img = $this->svgdir.'/'.$img;
  23765. }
  23766. if (($img[0] == '/') AND !empty($_SERVER['DOCUMENT_ROOT']) AND ($_SERVER['DOCUMENT_ROOT'] != '/')) {
  23767. $findroot = strpos($img, $_SERVER['DOCUMENT_ROOT']);
  23768. if (($findroot === false) OR ($findroot > 1)) {
  23769. if (substr($_SERVER['DOCUMENT_ROOT'], -1) == '/') {
  23770. $img = substr($_SERVER['DOCUMENT_ROOT'], 0, -1).$img;
  23771. } else {
  23772. $img = $_SERVER['DOCUMENT_ROOT'].$img;
  23773. }
  23774. }
  23775. }
  23776. $img = urldecode($img);
  23777. $testscrtype = @parse_url($img);
  23778. if (empty($testscrtype['query'])) {
  23779. // convert URL to server path
  23780. $img = str_replace(K_PATH_URL, K_PATH_MAIN, $img);
  23781. } elseif (preg_match('|^https?://|', $img) !== 1) {
  23782. // convert server path to URL
  23783. $img = str_replace(K_PATH_MAIN, K_PATH_URL, $img);
  23784. }
  23785. }
  23786. // get image type
  23787. $imgtype = TCPDF_IMAGES::getImageFileType($img);
  23788. if (($imgtype == 'eps') OR ($imgtype == 'ai')) {
  23789. $this->ImageEps($img, $x, $y, $w, $h);
  23790. } elseif ($imgtype == 'svg') {
  23791. // store SVG vars
  23792. $svggradients = $this->svggradients;
  23793. $svggradientid = $this->svggradientid;
  23794. $svgdefsmode = $this->svgdefsmode;
  23795. $svgdefs = $this->svgdefs;
  23796. $svgclipmode = $this->svgclipmode;
  23797. $svgclippaths = $this->svgclippaths;
  23798. $svgcliptm = $this->svgcliptm;
  23799. $svgclipid = $this->svgclipid;
  23800. $svgtext = $this->svgtext;
  23801. $svgtextmode = $this->svgtextmode;
  23802. $this->ImageSVG($img, $x, $y, $w, $h);
  23803. // restore SVG vars
  23804. $this->svggradients = $svggradients;
  23805. $this->svggradientid = $svggradientid;
  23806. $this->svgdefsmode = $svgdefsmode;
  23807. $this->svgdefs = $svgdefs;
  23808. $this->svgclipmode = $svgclipmode;
  23809. $this->svgclippaths = $svgclippaths;
  23810. $this->svgcliptm = $svgcliptm;
  23811. $this->svgclipid = $svgclipid;
  23812. $this->svgtext = $svgtext;
  23813. $this->svgtextmode = $svgtextmode;
  23814. } else {
  23815. $this->Image($img, $x, $y, $w, $h);
  23816. }
  23817. $this->StopTransform();
  23818. }
  23819. break;
  23820. }
  23821. // text
  23822. case 'text':
  23823. case 'tspan': {
  23824. if (isset($this->svgtextmode['text-anchor']) AND !empty($this->svgtext)) {
  23825. // @TODO: unsupported feature
  23826. }
  23827. // only basic support - advanced features must be implemented
  23828. $this->svgtextmode['invisible'] = $invisible;
  23829. if ($invisible) {
  23830. break;
  23831. }
  23832. array_push($this->svgstyles, $svgstyle);
  23833. if (isset($attribs['x'])) {
  23834. $x = $this->getHTMLUnitToUnits($attribs['x'], 0, $this->svgunit, false);
  23835. } elseif ($name == 'tspan') {
  23836. $x = $this->x;
  23837. } else {
  23838. $x = 0;
  23839. }
  23840. if (isset($attribs['dx'])) {
  23841. $x += $this->getHTMLUnitToUnits($attribs['dx'], 0, $this->svgunit, false);
  23842. }
  23843. if (isset($attribs['y'])) {
  23844. $y = $this->getHTMLUnitToUnits($attribs['y'], 0, $this->svgunit, false);
  23845. } elseif ($name == 'tspan') {
  23846. $y = $this->y;
  23847. } else {
  23848. $y = 0;
  23849. }
  23850. if (isset($attribs['dy'])) {
  23851. $y += $this->getHTMLUnitToUnits($attribs['dy'], 0, $this->svgunit, false);
  23852. }
  23853. $svgstyle['text-color'] = $svgstyle['fill'];
  23854. $this->svgtext = '';
  23855. if (isset($svgstyle['text-anchor'])) {
  23856. $this->svgtextmode['text-anchor'] = $svgstyle['text-anchor'];
  23857. } else {
  23858. $this->svgtextmode['text-anchor'] = 'start';
  23859. }
  23860. if (isset($svgstyle['direction'])) {
  23861. if ($svgstyle['direction'] == 'rtl') {
  23862. $this->svgtextmode['rtl'] = true;
  23863. } else {
  23864. $this->svgtextmode['rtl'] = false;
  23865. }
  23866. } else {
  23867. $this->svgtextmode['rtl'] = false;
  23868. }
  23869. if (isset($svgstyle['stroke']) AND ($svgstyle['stroke'] != 'none') AND isset($svgstyle['stroke-width']) AND ($svgstyle['stroke-width'] > 0)) {
  23870. $this->svgtextmode['stroke'] = $this->getHTMLUnitToUnits($svgstyle['stroke-width'], 0, $this->svgunit, false);
  23871. } else {
  23872. $this->svgtextmode['stroke'] = false;
  23873. }
  23874. $this->StartTransform();
  23875. $this->SVGTransform($tm);
  23876. $obstyle = $this->setSVGStyles($svgstyle, $prev_svgstyle, $x, $y, 1, 1);
  23877. $this->x = $x;
  23878. $this->y = $y;
  23879. break;
  23880. }
  23881. // use
  23882. case 'use': {
  23883. if (isset($attribs['xlink:href']) AND !empty($attribs['xlink:href'])) {
  23884. $svgdefid = substr($attribs['xlink:href'], 1);
  23885. if (isset($this->svgdefs[$svgdefid])) {
  23886. $use = $this->svgdefs[$svgdefid];
  23887. if (isset($attribs['xlink:href'])) {
  23888. unset($attribs['xlink:href']);
  23889. }
  23890. if (isset($attribs['id'])) {
  23891. unset($attribs['id']);
  23892. }
  23893. if (isset($use['attribs']['x']) AND isset($attribs['x'])) {
  23894. $attribs['x'] += $use['attribs']['x'];
  23895. }
  23896. if (isset($use['attribs']['y']) AND isset($attribs['y'])) {
  23897. $attribs['y'] += $use['attribs']['y'];
  23898. }
  23899. if (empty($attribs['style'])) {
  23900. $attribs['style'] = '';
  23901. }
  23902. if (!empty($use['attribs']['style'])) {
  23903. // merge styles
  23904. $attribs['style'] = str_replace(';;',';',';'.$use['attribs']['style'].$attribs['style']);
  23905. }
  23906. $attribs = array_merge($use['attribs'], $attribs);
  23907. $this->startSVGElementHandler($parser, $use['name'], $attribs);
  23908. return;
  23909. }
  23910. }
  23911. break;
  23912. }
  23913. default: {
  23914. break;
  23915. }
  23916. } // end of switch
  23917. // process child elements
  23918. if (!empty($attribs['child_elements'])) {
  23919. $child_elements = $attribs['child_elements'];
  23920. unset($attribs['child_elements']);
  23921. foreach($child_elements as $child_element) {
  23922. if (empty($child_element['attribs']['closing_tag'])) {
  23923. $this->startSVGElementHandler('child-tag', $child_element['name'], $child_element['attribs']);
  23924. } else {
  23925. if (isset($child_element['attribs']['content'])) {
  23926. $this->svgtext = $child_element['attribs']['content'];
  23927. }
  23928. $this->endSVGElementHandler('child-tag', $child_element['name']);
  23929. }
  23930. }
  23931. }
  23932. }
  23933. /**
  23934. * Sets the closing SVG element handler function for the XML parser.
  23935. * @param resource|string $parser The first parameter, parser, is a reference to the XML parser calling the handler.
  23936. * @param string $name The second parameter, name, contains the name of the element for which this handler is called. If case-folding is in effect for this parser, the element name will be in uppercase letters.
  23937. * @author Nicola Asuni
  23938. * @since 5.0.000 (2010-05-02)
  23939. * @protected
  23940. */
  23941. protected function endSVGElementHandler($parser, $name) {
  23942. $name = $this->removeTagNamespace($name);
  23943. if ($this->svgdefsmode AND !in_array($name, array('defs', 'clipPath', 'linearGradient', 'radialGradient', 'stop'))) {;
  23944. if (end($this->svgdefs) !== FALSE) {
  23945. $last_svgdefs_id = key($this->svgdefs);
  23946. if (isset($this->svgdefs[$last_svgdefs_id]['attribs']['child_elements'])) {
  23947. foreach($this->svgdefs[$last_svgdefs_id]['attribs']['child_elements'] as $child_element) {
  23948. if (isset($child_element['attribs']['id']) AND ($child_element['name'] == $name)) {
  23949. $this->svgdefs[$last_svgdefs_id]['attribs']['child_elements'][$child_element['attribs']['id'].'_CLOSE'] = array('name' => $name, 'attribs' => array('closing_tag' => TRUE, 'content' => $this->svgtext));
  23950. return;
  23951. }
  23952. }
  23953. if ($this->svgdefs[$last_svgdefs_id]['name'] == $name) {
  23954. $this->svgdefs[$last_svgdefs_id]['attribs']['child_elements'][$last_svgdefs_id.'_CLOSE'] = array('name' => $name, 'attribs' => array('closing_tag' => TRUE, 'content' => $this->svgtext));
  23955. return;
  23956. }
  23957. }
  23958. }
  23959. return;
  23960. }
  23961. switch($name) {
  23962. case 'defs': {
  23963. $this->svgdefsmode = false;
  23964. break;
  23965. }
  23966. // clipPath
  23967. case 'clipPath': {
  23968. $this->svgclipmode = false;
  23969. break;
  23970. }
  23971. case 'svg': {
  23972. if (--$this->svg_tag_depth <= 0) {
  23973. break;
  23974. }
  23975. }
  23976. case 'g': {
  23977. // ungroup: remove last style from array
  23978. array_pop($this->svgstyles);
  23979. $this->StopTransform();
  23980. break;
  23981. }
  23982. case 'text':
  23983. case 'tspan': {
  23984. if ($this->svgtextmode['invisible']) {
  23985. // This implementation must be fixed to following the rule:
  23986. // If the 'visibility' property is set to hidden on a 'tspan', 'tref' or 'altGlyph' element, then the text is invisible but still takes up space in text layout calculations.
  23987. break;
  23988. }
  23989. // print text
  23990. $text = $this->svgtext;
  23991. //$text = $this->stringTrim($text);
  23992. $textlen = $this->GetStringWidth($text);
  23993. if ($this->svgtextmode['text-anchor'] != 'start') {
  23994. // check if string is RTL text
  23995. if ($this->svgtextmode['text-anchor'] == 'end') {
  23996. if ($this->svgtextmode['rtl']) {
  23997. $this->x += $textlen;
  23998. } else {
  23999. $this->x -= $textlen;
  24000. }
  24001. } elseif ($this->svgtextmode['text-anchor'] == 'middle') {
  24002. if ($this->svgtextmode['rtl']) {
  24003. $this->x += ($textlen / 2);
  24004. } else {
  24005. $this->x -= ($textlen / 2);
  24006. }
  24007. }
  24008. }
  24009. $textrendermode = $this->textrendermode;
  24010. $textstrokewidth = $this->textstrokewidth;
  24011. $this->setTextRenderingMode($this->svgtextmode['stroke'], true, false);
  24012. if ($name == 'text') {
  24013. // store current coordinates
  24014. $tmpx = $this->x;
  24015. $tmpy = $this->y;
  24016. }
  24017. // print the text
  24018. $this->Cell($textlen, 0, $text, 0, 0, '', false, '', 0, false, 'L', 'T');
  24019. if ($name == 'text') {
  24020. // restore coordinates
  24021. $this->x = $tmpx;
  24022. $this->y = $tmpy;
  24023. }
  24024. // restore previous rendering mode
  24025. $this->textrendermode = $textrendermode;
  24026. $this->textstrokewidth = $textstrokewidth;
  24027. $this->svgtext = '';
  24028. $this->StopTransform();
  24029. if (!$this->svgdefsmode) {
  24030. array_pop($this->svgstyles);
  24031. }
  24032. break;
  24033. }
  24034. default: {
  24035. break;
  24036. }
  24037. }
  24038. }
  24039. /**
  24040. * Sets the character data handler function for the XML parser.
  24041. * @param resource $parser The first parameter, parser, is a reference to the XML parser calling the handler.
  24042. * @param string $data The second parameter, data, contains the character data as a string.
  24043. * @author Nicola Asuni
  24044. * @since 5.0.000 (2010-05-02)
  24045. * @protected
  24046. */
  24047. protected function segSVGContentHandler($parser, $data) {
  24048. $this->svgtext .= $data;
  24049. }
  24050. // --- END SVG METHODS -----------------------------------------------------
  24051. /**
  24052. * Keeps files in memory, so it doesn't need to downloaded everytime in a loop
  24053. * @param string $file
  24054. * @return string
  24055. */
  24056. protected function getCachedFileContents($file)
  24057. {
  24058. if (!isset($this->fileContentCache[$file])) {
  24059. $this->fileContentCache[$file] = TCPDF_STATIC::fileGetContents($file);
  24060. }
  24061. return $this->fileContentCache[$file];
  24062. }
  24063. /**
  24064. * Avoid multiple calls to an external server to see if a file exists
  24065. * @param string $file
  24066. * @return bool
  24067. */
  24068. protected function fileExists($file)
  24069. {
  24070. if (isset($this->fileContentCache[$file]) || false !== $this->getImageBuffer($file)) {
  24071. return true;
  24072. }
  24073. return TCPDF_STATIC::file_exists($file);
  24074. }
  24075. } // END OF TCPDF CLASS
  24076. //============================================================+
  24077. // END OF FILE
  24078. //============================================================+