tcpdf.php 877 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487
  1. <?php
  2. //============================================================+
  3. // File name : tcpdf.php
  4. // Version : 6.2.13
  5. // Begin : 2002-08-03
  6. // Last Update : 2015-06-18
  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-2015 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 ImagMagick (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 ImagMagick (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.2.8
  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.2.8
  128. * @author Nicola Asuni - info@tecnick.com
  129. */
  130. class TCPDF {
  131. // Protected properties
  132. /**
  133. * Current page number.
  134. * @protected
  135. */
  136. protected $page;
  137. /**
  138. * Current object number.
  139. * @protected
  140. */
  141. protected $n;
  142. /**
  143. * Array of object offsets.
  144. * @protected
  145. */
  146. protected $offsets = array();
  147. /**
  148. * Array of object IDs for each page.
  149. * @protected
  150. */
  151. protected $pageobjects = array();
  152. /**
  153. * Buffer holding in-memory PDF.
  154. * @protected
  155. */
  156. protected $buffer;
  157. /**
  158. * Array containing pages.
  159. * @protected
  160. */
  161. protected $pages = array();
  162. /**
  163. * Current document state.
  164. * @protected
  165. */
  166. protected $state;
  167. /**
  168. * Compression flag.
  169. * @protected
  170. */
  171. protected $compress;
  172. /**
  173. * Current page orientation (P = Portrait, L = Landscape).
  174. * @protected
  175. */
  176. protected $CurOrientation;
  177. /**
  178. * Page dimensions.
  179. * @protected
  180. */
  181. protected $pagedim = array();
  182. /**
  183. * Scale factor (number of points in user unit).
  184. * @protected
  185. */
  186. protected $k;
  187. /**
  188. * Width of page format in points.
  189. * @protected
  190. */
  191. protected $fwPt;
  192. /**
  193. * Height of page format in points.
  194. * @protected
  195. */
  196. protected $fhPt;
  197. /**
  198. * Current width of page in points.
  199. * @protected
  200. */
  201. protected $wPt;
  202. /**
  203. * Current height of page in points.
  204. * @protected
  205. */
  206. protected $hPt;
  207. /**
  208. * Current width of page in user unit.
  209. * @protected
  210. */
  211. protected $w;
  212. /**
  213. * Current height of page in user unit.
  214. * @protected
  215. */
  216. protected $h;
  217. /**
  218. * Left margin.
  219. * @protected
  220. */
  221. protected $lMargin;
  222. /**
  223. * Right margin.
  224. * @protected
  225. */
  226. protected $rMargin;
  227. /**
  228. * Cell left margin (used by regions).
  229. * @protected
  230. */
  231. protected $clMargin;
  232. /**
  233. * Cell right margin (used by regions).
  234. * @protected
  235. */
  236. protected $crMargin;
  237. /**
  238. * Top margin.
  239. * @protected
  240. */
  241. protected $tMargin;
  242. /**
  243. * Page break margin.
  244. * @protected
  245. */
  246. protected $bMargin;
  247. /**
  248. * Array of cell internal paddings ('T' => top, 'R' => right, 'B' => bottom, 'L' => left).
  249. * @since 5.9.000 (2010-10-03)
  250. * @protected
  251. */
  252. protected $cell_padding = array('T' => 0, 'R' => 0, 'B' => 0, 'L' => 0);
  253. /**
  254. * Array of cell margins ('T' => top, 'R' => right, 'B' => bottom, 'L' => left).
  255. * @since 5.9.000 (2010-10-04)
  256. * @protected
  257. */
  258. protected $cell_margin = array('T' => 0, 'R' => 0, 'B' => 0, 'L' => 0);
  259. /**
  260. * Current horizontal position in user unit for cell positioning.
  261. * @protected
  262. */
  263. protected $x;
  264. /**
  265. * Current vertical position in user unit for cell positioning.
  266. * @protected
  267. */
  268. protected $y;
  269. /**
  270. * Height of last cell printed.
  271. * @protected
  272. */
  273. protected $lasth;
  274. /**
  275. * Line width in user unit.
  276. * @protected
  277. */
  278. protected $LineWidth;
  279. /**
  280. * Array of standard font names.
  281. * @protected
  282. */
  283. protected $CoreFonts;
  284. /**
  285. * Array of used fonts.
  286. * @protected
  287. */
  288. protected $fonts = array();
  289. /**
  290. * Array of font files.
  291. * @protected
  292. */
  293. protected $FontFiles = array();
  294. /**
  295. * Array of encoding differences.
  296. * @protected
  297. */
  298. protected $diffs = array();
  299. /**
  300. * Array of used images.
  301. * @protected
  302. */
  303. protected $images = array();
  304. /**
  305. * Depth of the svg tag, to keep track if the svg tag is a subtag or the root tag.
  306. * @protected
  307. */
  308. protected $svg_tag_depth = 0;
  309. /**
  310. * Array of Annotations in pages.
  311. * @protected
  312. */
  313. protected $PageAnnots = array();
  314. /**
  315. * Array of internal links.
  316. * @protected
  317. */
  318. protected $links = array();
  319. /**
  320. * Current font family.
  321. * @protected
  322. */
  323. protected $FontFamily;
  324. /**
  325. * Current font style.
  326. * @protected
  327. */
  328. protected $FontStyle;
  329. /**
  330. * Current font ascent (distance between font top and baseline).
  331. * @protected
  332. * @since 2.8.000 (2007-03-29)
  333. */
  334. protected $FontAscent;
  335. /**
  336. * Current font descent (distance between font bottom and baseline).
  337. * @protected
  338. * @since 2.8.000 (2007-03-29)
  339. */
  340. protected $FontDescent;
  341. /**
  342. * Underlining flag.
  343. * @protected
  344. */
  345. protected $underline;
  346. /**
  347. * Overlining flag.
  348. * @protected
  349. */
  350. protected $overline;
  351. /**
  352. * Current font info.
  353. * @protected
  354. */
  355. protected $CurrentFont;
  356. /**
  357. * Current font size in points.
  358. * @protected
  359. */
  360. protected $FontSizePt;
  361. /**
  362. * Current font size in user unit.
  363. * @protected
  364. */
  365. protected $FontSize;
  366. /**
  367. * Commands for drawing color.
  368. * @protected
  369. */
  370. protected $DrawColor;
  371. /**
  372. * Commands for filling color.
  373. * @protected
  374. */
  375. protected $FillColor;
  376. /**
  377. * Commands for text color.
  378. * @protected
  379. */
  380. protected $TextColor;
  381. /**
  382. * Indicates whether fill and text colors are different.
  383. * @protected
  384. */
  385. protected $ColorFlag;
  386. /**
  387. * Automatic page breaking.
  388. * @protected
  389. */
  390. protected $AutoPageBreak;
  391. /**
  392. * Threshold used to trigger page breaks.
  393. * @protected
  394. */
  395. protected $PageBreakTrigger;
  396. /**
  397. * Flag set when processing page header.
  398. * @protected
  399. */
  400. protected $InHeader = false;
  401. /**
  402. * Flag set when processing page footer.
  403. * @protected
  404. */
  405. protected $InFooter = false;
  406. /**
  407. * Zoom display mode.
  408. * @protected
  409. */
  410. protected $ZoomMode;
  411. /**
  412. * Layout display mode.
  413. * @protected
  414. */
  415. protected $LayoutMode;
  416. /**
  417. * If true set the document information dictionary in Unicode.
  418. * @protected
  419. */
  420. protected $docinfounicode = true;
  421. /**
  422. * Document title.
  423. * @protected
  424. */
  425. protected $title = '';
  426. /**
  427. * Document subject.
  428. * @protected
  429. */
  430. protected $subject = '';
  431. /**
  432. * Document author.
  433. * @protected
  434. */
  435. protected $author = '';
  436. /**
  437. * Document keywords.
  438. * @protected
  439. */
  440. protected $keywords = '';
  441. /**
  442. * Document creator.
  443. * @protected
  444. */
  445. protected $creator = '';
  446. /**
  447. * Starting page number.
  448. * @protected
  449. */
  450. protected $starting_page_number = 1;
  451. /**
  452. * The right-bottom (or left-bottom for RTL) corner X coordinate of last inserted image.
  453. * @since 2002-07-31
  454. * @author Nicola Asuni
  455. * @protected
  456. */
  457. protected $img_rb_x;
  458. /**
  459. * The right-bottom corner Y coordinate of last inserted image.
  460. * @since 2002-07-31
  461. * @author Nicola Asuni
  462. * @protected
  463. */
  464. protected $img_rb_y;
  465. /**
  466. * Adjusting factor to convert pixels to user units.
  467. * @since 2004-06-14
  468. * @author Nicola Asuni
  469. * @protected
  470. */
  471. protected $imgscale = 1;
  472. /**
  473. * Boolean flag set to true when the input text is unicode (require unicode fonts).
  474. * @since 2005-01-02
  475. * @author Nicola Asuni
  476. * @protected
  477. */
  478. protected $isunicode = false;
  479. /**
  480. * PDF version.
  481. * @since 1.5.3
  482. * @protected
  483. */
  484. protected $PDFVersion = '1.7';
  485. /**
  486. * ID of the stored default header template (-1 = not set).
  487. * @protected
  488. */
  489. protected $header_xobjid = false;
  490. /**
  491. * If true reset the Header Xobject template at each page
  492. * @protected
  493. */
  494. protected $header_xobj_autoreset = false;
  495. /**
  496. * Minimum distance between header and top page margin.
  497. * @protected
  498. */
  499. protected $header_margin;
  500. /**
  501. * Minimum distance between footer and bottom page margin.
  502. * @protected
  503. */
  504. protected $footer_margin;
  505. /**
  506. * Original left margin value.
  507. * @protected
  508. * @since 1.53.0.TC013
  509. */
  510. protected $original_lMargin;
  511. /**
  512. * Original right margin value.
  513. * @protected
  514. * @since 1.53.0.TC013
  515. */
  516. protected $original_rMargin;
  517. /**
  518. * Default font used on page header.
  519. * @protected
  520. */
  521. protected $header_font;
  522. /**
  523. * Default font used on page footer.
  524. * @protected
  525. */
  526. protected $footer_font;
  527. /**
  528. * Language templates.
  529. * @protected
  530. */
  531. protected $l;
  532. /**
  533. * Barcode to print on page footer (only if set).
  534. * @protected
  535. */
  536. protected $barcode = false;
  537. /**
  538. * Boolean flag to print/hide page header.
  539. * @protected
  540. */
  541. protected $print_header = true;
  542. /**
  543. * Boolean flag to print/hide page footer.
  544. * @protected
  545. */
  546. protected $print_footer = true;
  547. /**
  548. * Header image logo.
  549. * @protected
  550. */
  551. protected $header_logo = '';
  552. /**
  553. * Width of header image logo in user units.
  554. * @protected
  555. */
  556. protected $header_logo_width = 30;
  557. /**
  558. * Title to be printed on default page header.
  559. * @protected
  560. */
  561. protected $header_title = '';
  562. /**
  563. * String to pring on page header after title.
  564. * @protected
  565. */
  566. protected $header_string = '';
  567. /**
  568. * Color for header text (RGB array).
  569. * @since 5.9.174 (2012-07-25)
  570. * @protected
  571. */
  572. protected $header_text_color = array(0,0,0);
  573. /**
  574. * Color for header line (RGB array).
  575. * @since 5.9.174 (2012-07-25)
  576. * @protected
  577. */
  578. protected $header_line_color = array(0,0,0);
  579. /**
  580. * Color for footer text (RGB array).
  581. * @since 5.9.174 (2012-07-25)
  582. * @protected
  583. */
  584. protected $footer_text_color = array(0,0,0);
  585. /**
  586. * Color for footer line (RGB array).
  587. * @since 5.9.174 (2012-07-25)
  588. * @protected
  589. */
  590. protected $footer_line_color = array(0,0,0);
  591. /**
  592. * Text shadow data array.
  593. * @since 5.9.174 (2012-07-25)
  594. * @protected
  595. */
  596. protected $txtshadow = array('enabled'=>false, 'depth_w'=>0, 'depth_h'=>0, 'color'=>false, 'opacity'=>1, 'blend_mode'=>'Normal');
  597. /**
  598. * Default number of columns for html table.
  599. * @protected
  600. */
  601. protected $default_table_columns = 4;
  602. // variables for html parser
  603. /**
  604. * HTML PARSER: array to store current link and rendering styles.
  605. * @protected
  606. */
  607. protected $HREF = array();
  608. /**
  609. * List of available fonts on filesystem.
  610. * @protected
  611. */
  612. protected $fontlist = array();
  613. /**
  614. * Current foreground color.
  615. * @protected
  616. */
  617. protected $fgcolor;
  618. /**
  619. * HTML PARSER: array of boolean values, true in case of ordered list (OL), false otherwise.
  620. * @protected
  621. */
  622. protected $listordered = array();
  623. /**
  624. * HTML PARSER: array count list items on nested lists.
  625. * @protected
  626. */
  627. protected $listcount = array();
  628. /**
  629. * HTML PARSER: current list nesting level.
  630. * @protected
  631. */
  632. protected $listnum = 0;
  633. /**
  634. * HTML PARSER: indent amount for lists.
  635. * @protected
  636. */
  637. protected $listindent = 0;
  638. /**
  639. * HTML PARSER: current list indententation level.
  640. * @protected
  641. */
  642. protected $listindentlevel = 0;
  643. /**
  644. * Current background color.
  645. * @protected
  646. */
  647. protected $bgcolor;
  648. /**
  649. * Temporary font size in points.
  650. * @protected
  651. */
  652. protected $tempfontsize = 10;
  653. /**
  654. * Spacer string for LI tags.
  655. * @protected
  656. */
  657. protected $lispacer = '';
  658. /**
  659. * Default encoding.
  660. * @protected
  661. * @since 1.53.0.TC010
  662. */
  663. protected $encoding = 'UTF-8';
  664. /**
  665. * PHP internal encoding.
  666. * @protected
  667. * @since 1.53.0.TC016
  668. */
  669. protected $internal_encoding;
  670. /**
  671. * Boolean flag to indicate if the document language is Right-To-Left.
  672. * @protected
  673. * @since 2.0.000
  674. */
  675. protected $rtl = false;
  676. /**
  677. * Boolean flag used to force RTL or LTR string direction.
  678. * @protected
  679. * @since 2.0.000
  680. */
  681. protected $tmprtl = false;
  682. // --- Variables used for document encryption:
  683. /**
  684. * IBoolean flag indicating whether document is protected.
  685. * @protected
  686. * @since 2.0.000 (2008-01-02)
  687. */
  688. protected $encrypted;
  689. /**
  690. * Array containing encryption settings.
  691. * @protected
  692. * @since 5.0.005 (2010-05-11)
  693. */
  694. protected $encryptdata = array();
  695. /**
  696. * Last RC4 key encrypted (cached for optimisation).
  697. * @protected
  698. * @since 2.0.000 (2008-01-02)
  699. */
  700. protected $last_enc_key;
  701. /**
  702. * Last RC4 computed key.
  703. * @protected
  704. * @since 2.0.000 (2008-01-02)
  705. */
  706. protected $last_enc_key_c;
  707. /**
  708. * File ID (used on document trailer).
  709. * @protected
  710. * @since 5.0.005 (2010-05-12)
  711. */
  712. protected $file_id;
  713. // --- bookmark ---
  714. /**
  715. * Outlines for bookmark.
  716. * @protected
  717. * @since 2.1.002 (2008-02-12)
  718. */
  719. protected $outlines = array();
  720. /**
  721. * Outline root for bookmark.
  722. * @protected
  723. * @since 2.1.002 (2008-02-12)
  724. */
  725. protected $OutlineRoot;
  726. // --- javascript and form ---
  727. /**
  728. * Javascript code.
  729. * @protected
  730. * @since 2.1.002 (2008-02-12)
  731. */
  732. protected $javascript = '';
  733. /**
  734. * Javascript counter.
  735. * @protected
  736. * @since 2.1.002 (2008-02-12)
  737. */
  738. protected $n_js;
  739. /**
  740. * line through state
  741. * @protected
  742. * @since 2.8.000 (2008-03-19)
  743. */
  744. protected $linethrough;
  745. /**
  746. * Array with additional document-wide usage rights for the document.
  747. * @protected
  748. * @since 5.8.014 (2010-08-23)
  749. */
  750. protected $ur = array();
  751. /**
  752. * DPI (Dot Per Inch) Document Resolution (do not change).
  753. * @protected
  754. * @since 3.0.000 (2008-03-27)
  755. */
  756. protected $dpi = 72;
  757. /**
  758. * Array of page numbers were a new page group was started (the page numbers are the keys of the array).
  759. * @protected
  760. * @since 3.0.000 (2008-03-27)
  761. */
  762. protected $newpagegroup = array();
  763. /**
  764. * Array that contains the number of pages in each page group.
  765. * @protected
  766. * @since 3.0.000 (2008-03-27)
  767. */
  768. protected $pagegroups = array();
  769. /**
  770. * Current page group number.
  771. * @protected
  772. * @since 3.0.000 (2008-03-27)
  773. */
  774. protected $currpagegroup = 0;
  775. /**
  776. * Array of transparency objects and parameters.
  777. * @protected
  778. * @since 3.0.000 (2008-03-27)
  779. */
  780. protected $extgstates;
  781. /**
  782. * Set the default JPEG compression quality (1-100).
  783. * @protected
  784. * @since 3.0.000 (2008-03-27)
  785. */
  786. protected $jpeg_quality;
  787. /**
  788. * Default cell height ratio.
  789. * @protected
  790. * @since 3.0.014 (2008-05-23)
  791. */
  792. protected $cell_height_ratio = K_CELL_HEIGHT_RATIO;
  793. /**
  794. * PDF viewer preferences.
  795. * @protected
  796. * @since 3.1.000 (2008-06-09)
  797. */
  798. protected $viewer_preferences;
  799. /**
  800. * A name object specifying how the document should be displayed when opened.
  801. * @protected
  802. * @since 3.1.000 (2008-06-09)
  803. */
  804. protected $PageMode;
  805. /**
  806. * Array for storing gradient information.
  807. * @protected
  808. * @since 3.1.000 (2008-06-09)
  809. */
  810. protected $gradients = array();
  811. /**
  812. * Array used to store positions inside the pages buffer (keys are the page numbers).
  813. * @protected
  814. * @since 3.2.000 (2008-06-26)
  815. */
  816. protected $intmrk = array();
  817. /**
  818. * Array used to store positions inside the pages buffer (keys are the page numbers).
  819. * @protected
  820. * @since 5.7.000 (2010-08-03)
  821. */
  822. protected $bordermrk = array();
  823. /**
  824. * Array used to store page positions to track empty pages (keys are the page numbers).
  825. * @protected
  826. * @since 5.8.007 (2010-08-18)
  827. */
  828. protected $emptypagemrk = array();
  829. /**
  830. * Array used to store content positions inside the pages buffer (keys are the page numbers).
  831. * @protected
  832. * @since 4.6.021 (2009-07-20)
  833. */
  834. protected $cntmrk = array();
  835. /**
  836. * Array used to store footer positions of each page.
  837. * @protected
  838. * @since 3.2.000 (2008-07-01)
  839. */
  840. protected $footerpos = array();
  841. /**
  842. * Array used to store footer length of each page.
  843. * @protected
  844. * @since 4.0.014 (2008-07-29)
  845. */
  846. protected $footerlen = array();
  847. /**
  848. * Boolean flag to indicate if a new line is created.
  849. * @protected
  850. * @since 3.2.000 (2008-07-01)
  851. */
  852. protected $newline = true;
  853. /**
  854. * End position of the latest inserted line.
  855. * @protected
  856. * @since 3.2.000 (2008-07-01)
  857. */
  858. protected $endlinex = 0;
  859. /**
  860. * PDF string for width value of the last line.
  861. * @protected
  862. * @since 4.0.006 (2008-07-16)
  863. */
  864. protected $linestyleWidth = '';
  865. /**
  866. * PDF string for CAP value of the last line.
  867. * @protected
  868. * @since 4.0.006 (2008-07-16)
  869. */
  870. protected $linestyleCap = '0 J';
  871. /**
  872. * PDF string for join value of the last line.
  873. * @protected
  874. * @since 4.0.006 (2008-07-16)
  875. */
  876. protected $linestyleJoin = '0 j';
  877. /**
  878. * PDF string for dash value of the last line.
  879. * @protected
  880. * @since 4.0.006 (2008-07-16)
  881. */
  882. protected $linestyleDash = '[] 0 d';
  883. /**
  884. * Boolean flag to indicate if marked-content sequence is open.
  885. * @protected
  886. * @since 4.0.013 (2008-07-28)
  887. */
  888. protected $openMarkedContent = false;
  889. /**
  890. * Count the latest inserted vertical spaces on HTML.
  891. * @protected
  892. * @since 4.0.021 (2008-08-24)
  893. */
  894. protected $htmlvspace = 0;
  895. /**
  896. * Array of Spot colors.
  897. * @protected
  898. * @since 4.0.024 (2008-09-12)
  899. */
  900. protected $spot_colors = array();
  901. /**
  902. * Symbol used for HTML unordered list items.
  903. * @protected
  904. * @since 4.0.028 (2008-09-26)
  905. */
  906. protected $lisymbol = '';
  907. /**
  908. * String used to mark the beginning and end of EPS image blocks.
  909. * @protected
  910. * @since 4.1.000 (2008-10-18)
  911. */
  912. protected $epsmarker = 'x#!#EPS#!#x';
  913. /**
  914. * Array of transformation matrix.
  915. * @protected
  916. * @since 4.2.000 (2008-10-29)
  917. */
  918. protected $transfmatrix = array();
  919. /**
  920. * Current key for transformation matrix.
  921. * @protected
  922. * @since 4.8.005 (2009-09-17)
  923. */
  924. protected $transfmatrix_key = 0;
  925. /**
  926. * Booklet mode for double-sided pages.
  927. * @protected
  928. * @since 4.2.000 (2008-10-29)
  929. */
  930. protected $booklet = false;
  931. /**
  932. * Epsilon value used for float calculations.
  933. * @protected
  934. * @since 4.2.000 (2008-10-29)
  935. */
  936. protected $feps = 0.005;
  937. /**
  938. * Array used for custom vertical spaces for HTML tags.
  939. * @protected
  940. * @since 4.2.001 (2008-10-30)
  941. */
  942. protected $tagvspaces = array();
  943. /**
  944. * HTML PARSER: custom indent amount for lists. Negative value means disabled.
  945. * @protected
  946. * @since 4.2.007 (2008-11-12)
  947. */
  948. protected $customlistindent = -1;
  949. /**
  950. * Boolean flag to indicate if the border of the cell sides that cross the page should be removed.
  951. * @protected
  952. * @since 4.2.010 (2008-11-14)
  953. */
  954. protected $opencell = true;
  955. /**
  956. * Array of files to embedd.
  957. * @protected
  958. * @since 4.4.000 (2008-12-07)
  959. */
  960. protected $embeddedfiles = array();
  961. /**
  962. * Boolean flag to indicate if we are inside a PRE tag.
  963. * @protected
  964. * @since 4.4.001 (2008-12-08)
  965. */
  966. protected $premode = false;
  967. /**
  968. * Array used to store positions of graphics transformation blocks inside the page buffer.
  969. * keys are the page numbers
  970. * @protected
  971. * @since 4.4.002 (2008-12-09)
  972. */
  973. protected $transfmrk = array();
  974. /**
  975. * Default color for html links.
  976. * @protected
  977. * @since 4.4.003 (2008-12-09)
  978. */
  979. protected $htmlLinkColorArray = array(0, 0, 255);
  980. /**
  981. * Default font style to add to html links.
  982. * @protected
  983. * @since 4.4.003 (2008-12-09)
  984. */
  985. protected $htmlLinkFontStyle = 'U';
  986. /**
  987. * Counts the number of pages.
  988. * @protected
  989. * @since 4.5.000 (2008-12-31)
  990. */
  991. protected $numpages = 0;
  992. /**
  993. * Array containing page lengths in bytes.
  994. * @protected
  995. * @since 4.5.000 (2008-12-31)
  996. */
  997. protected $pagelen = array();
  998. /**
  999. * Counts the number of pages.
  1000. * @protected
  1001. * @since 4.5.000 (2008-12-31)
  1002. */
  1003. protected $numimages = 0;
  1004. /**
  1005. * Store the image keys.
  1006. * @protected
  1007. * @since 4.5.000 (2008-12-31)
  1008. */
  1009. protected $imagekeys = array();
  1010. /**
  1011. * Length of the buffer in bytes.
  1012. * @protected
  1013. * @since 4.5.000 (2008-12-31)
  1014. */
  1015. protected $bufferlen = 0;
  1016. /**
  1017. * Counts the number of fonts.
  1018. * @protected
  1019. * @since 4.5.000 (2009-01-02)
  1020. */
  1021. protected $numfonts = 0;
  1022. /**
  1023. * Store the font keys.
  1024. * @protected
  1025. * @since 4.5.000 (2009-01-02)
  1026. */
  1027. protected $fontkeys = array();
  1028. /**
  1029. * Store the font object IDs.
  1030. * @protected
  1031. * @since 4.8.001 (2009-09-09)
  1032. */
  1033. protected $font_obj_ids = array();
  1034. /**
  1035. * Store the fage status (true when opened, false when closed).
  1036. * @protected
  1037. * @since 4.5.000 (2009-01-02)
  1038. */
  1039. protected $pageopen = array();
  1040. /**
  1041. * Default monospace font.
  1042. * @protected
  1043. * @since 4.5.025 (2009-03-10)
  1044. */
  1045. protected $default_monospaced_font = 'courier';
  1046. /**
  1047. * Cloned copy of the current class object.
  1048. * @protected
  1049. * @since 4.5.029 (2009-03-19)
  1050. */
  1051. protected $objcopy;
  1052. /**
  1053. * Array used to store the lengths of cache files.
  1054. * @protected
  1055. * @since 4.5.029 (2009-03-19)
  1056. */
  1057. protected $cache_file_length = array();
  1058. /**
  1059. * Table header content to be repeated on each new page.
  1060. * @protected
  1061. * @since 4.5.030 (2009-03-20)
  1062. */
  1063. protected $thead = '';
  1064. /**
  1065. * Margins used for table header.
  1066. * @protected
  1067. * @since 4.5.030 (2009-03-20)
  1068. */
  1069. protected $theadMargins = array();
  1070. /**
  1071. * Boolean flag to enable document digital signature.
  1072. * @protected
  1073. * @since 4.6.005 (2009-04-24)
  1074. */
  1075. protected $sign = false;
  1076. /**
  1077. * Digital signature data.
  1078. * @protected
  1079. * @since 4.6.005 (2009-04-24)
  1080. */
  1081. protected $signature_data = array();
  1082. /**
  1083. * Digital signature max length.
  1084. * @protected
  1085. * @since 4.6.005 (2009-04-24)
  1086. */
  1087. protected $signature_max_length = 11742;
  1088. /**
  1089. * Data for digital signature appearance.
  1090. * @protected
  1091. * @since 5.3.011 (2010-06-16)
  1092. */
  1093. protected $signature_appearance = array('page' => 1, 'rect' => '0 0 0 0');
  1094. /**
  1095. * Array of empty digital signature appearances.
  1096. * @protected
  1097. * @since 5.9.101 (2011-07-06)
  1098. */
  1099. protected $empty_signature_appearance = array();
  1100. /**
  1101. * Boolean flag to enable document timestamping with TSA.
  1102. * @protected
  1103. * @since 6.0.085 (2014-06-19)
  1104. */
  1105. protected $tsa_timestamp = false;
  1106. /**
  1107. * Timestamping data.
  1108. * @protected
  1109. * @since 6.0.085 (2014-06-19)
  1110. */
  1111. protected $tsa_data = array();
  1112. /**
  1113. * Regular expression used to find blank characters (required for word-wrapping).
  1114. * @protected
  1115. * @since 4.6.006 (2009-04-28)
  1116. */
  1117. protected $re_spaces = '/[^\S\xa0]/';
  1118. /**
  1119. * Array of $re_spaces parts.
  1120. * @protected
  1121. * @since 5.5.011 (2010-07-09)
  1122. */
  1123. protected $re_space = array('p' => '[^\S\xa0]', 'm' => '');
  1124. /**
  1125. * Digital signature object ID.
  1126. * @protected
  1127. * @since 4.6.022 (2009-06-23)
  1128. */
  1129. protected $sig_obj_id = 0;
  1130. /**
  1131. * ID of page objects.
  1132. * @protected
  1133. * @since 4.7.000 (2009-08-29)
  1134. */
  1135. protected $page_obj_id = array();
  1136. /**
  1137. * List of form annotations IDs.
  1138. * @protected
  1139. * @since 4.8.000 (2009-09-07)
  1140. */
  1141. protected $form_obj_id = array();
  1142. /**
  1143. * 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.
  1144. * @protected
  1145. * @since 4.8.000 (2009-09-07)
  1146. */
  1147. protected $default_form_prop = array('lineWidth'=>1, 'borderStyle'=>'solid', 'fillColor'=>array(255, 255, 255), 'strokeColor'=>array(128, 128, 128));
  1148. /**
  1149. * Javascript objects array.
  1150. * @protected
  1151. * @since 4.8.000 (2009-09-07)
  1152. */
  1153. protected $js_objects = array();
  1154. /**
  1155. * Current form action (used during XHTML rendering).
  1156. * @protected
  1157. * @since 4.8.000 (2009-09-07)
  1158. */
  1159. protected $form_action = '';
  1160. /**
  1161. * Current form encryption type (used during XHTML rendering).
  1162. * @protected
  1163. * @since 4.8.000 (2009-09-07)
  1164. */
  1165. protected $form_enctype = 'application/x-www-form-urlencoded';
  1166. /**
  1167. * Current method to submit forms.
  1168. * @protected
  1169. * @since 4.8.000 (2009-09-07)
  1170. */
  1171. protected $form_mode = 'post';
  1172. /**
  1173. * List of fonts used on form fields (fontname => fontkey).
  1174. * @protected
  1175. * @since 4.8.001 (2009-09-09)
  1176. */
  1177. protected $annotation_fonts = array();
  1178. /**
  1179. * List of radio buttons parent objects.
  1180. * @protected
  1181. * @since 4.8.001 (2009-09-09)
  1182. */
  1183. protected $radiobutton_groups = array();
  1184. /**
  1185. * List of radio group objects IDs.
  1186. * @protected
  1187. * @since 4.8.001 (2009-09-09)
  1188. */
  1189. protected $radio_groups = array();
  1190. /**
  1191. * Text indentation value (used for text-indent CSS attribute).
  1192. * @protected
  1193. * @since 4.8.006 (2009-09-23)
  1194. */
  1195. protected $textindent = 0;
  1196. /**
  1197. * Store page number when startTransaction() is called.
  1198. * @protected
  1199. * @since 4.8.006 (2009-09-23)
  1200. */
  1201. protected $start_transaction_page = 0;
  1202. /**
  1203. * Store Y position when startTransaction() is called.
  1204. * @protected
  1205. * @since 4.9.001 (2010-03-28)
  1206. */
  1207. protected $start_transaction_y = 0;
  1208. /**
  1209. * True when we are printing the thead section on a new page.
  1210. * @protected
  1211. * @since 4.8.027 (2010-01-25)
  1212. */
  1213. protected $inthead = false;
  1214. /**
  1215. * Array of column measures (width, space, starting Y position).
  1216. * @protected
  1217. * @since 4.9.001 (2010-03-28)
  1218. */
  1219. protected $columns = array();
  1220. /**
  1221. * Number of colums.
  1222. * @protected
  1223. * @since 4.9.001 (2010-03-28)
  1224. */
  1225. protected $num_columns = 1;
  1226. /**
  1227. * Current column number.
  1228. * @protected
  1229. * @since 4.9.001 (2010-03-28)
  1230. */
  1231. protected $current_column = 0;
  1232. /**
  1233. * Starting page for columns.
  1234. * @protected
  1235. * @since 4.9.001 (2010-03-28)
  1236. */
  1237. protected $column_start_page = 0;
  1238. /**
  1239. * Maximum page and column selected.
  1240. * @protected
  1241. * @since 5.8.000 (2010-08-11)
  1242. */
  1243. protected $maxselcol = array('page' => 0, 'column' => 0);
  1244. /**
  1245. * Array of: X difference between table cell x start and starting page margin, cellspacing, cellpadding.
  1246. * @protected
  1247. * @since 5.8.000 (2010-08-11)
  1248. */
  1249. protected $colxshift = array('x' => 0, 's' => array('H' => 0, 'V' => 0), 'p' => array('L' => 0, 'T' => 0, 'R' => 0, 'B' => 0));
  1250. /**
  1251. * 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.
  1252. * @protected
  1253. * @since 4.9.008 (2010-04-03)
  1254. */
  1255. protected $textrendermode = 0;
  1256. /**
  1257. * Text stroke width in doc units.
  1258. * @protected
  1259. * @since 4.9.008 (2010-04-03)
  1260. */
  1261. protected $textstrokewidth = 0;
  1262. /**
  1263. * Current stroke color.
  1264. * @protected
  1265. * @since 4.9.008 (2010-04-03)
  1266. */
  1267. protected $strokecolor;
  1268. /**
  1269. * Default unit of measure for document.
  1270. * @protected
  1271. * @since 5.0.000 (2010-04-22)
  1272. */
  1273. protected $pdfunit = 'mm';
  1274. /**
  1275. * Boolean flag true when we are on TOC (Table Of Content) page.
  1276. * @protected
  1277. */
  1278. protected $tocpage = false;
  1279. /**
  1280. * Boolean flag: if true convert vector images (SVG, EPS) to raster image using GD or ImageMagick library.
  1281. * @protected
  1282. * @since 5.0.000 (2010-04-26)
  1283. */
  1284. protected $rasterize_vector_images = false;
  1285. /**
  1286. * Boolean flag: if true enables font subsetting by default.
  1287. * @protected
  1288. * @since 5.3.002 (2010-06-07)
  1289. */
  1290. protected $font_subsetting = true;
  1291. /**
  1292. * Array of default graphic settings.
  1293. * @protected
  1294. * @since 5.5.008 (2010-07-02)
  1295. */
  1296. protected $default_graphic_vars = array();
  1297. /**
  1298. * Array of XObjects.
  1299. * @protected
  1300. * @since 5.8.014 (2010-08-23)
  1301. */
  1302. protected $xobjects = array();
  1303. /**
  1304. * Boolean value true when we are inside an XObject.
  1305. * @protected
  1306. * @since 5.8.017 (2010-08-24)
  1307. */
  1308. protected $inxobj = false;
  1309. /**
  1310. * Current XObject ID.
  1311. * @protected
  1312. * @since 5.8.017 (2010-08-24)
  1313. */
  1314. protected $xobjid = '';
  1315. /**
  1316. * Percentage of character stretching.
  1317. * @protected
  1318. * @since 5.9.000 (2010-09-29)
  1319. */
  1320. protected $font_stretching = 100;
  1321. /**
  1322. * Increases or decreases the space between characters in a text by the specified amount (tracking).
  1323. * @protected
  1324. * @since 5.9.000 (2010-09-29)
  1325. */
  1326. protected $font_spacing = 0;
  1327. /**
  1328. * Array of no-write regions.
  1329. * ('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)
  1330. * @protected
  1331. * @since 5.9.003 (2010-10-14)
  1332. */
  1333. protected $page_regions = array();
  1334. /**
  1335. * Boolean value true when page region check is active.
  1336. * @protected
  1337. */
  1338. protected $check_page_regions = true;
  1339. /**
  1340. * Array of PDF layers data.
  1341. * @protected
  1342. * @since 5.9.102 (2011-07-13)
  1343. */
  1344. protected $pdflayers = array();
  1345. /**
  1346. * A dictionary of names and corresponding destinations (Dests key on document Catalog).
  1347. * @protected
  1348. * @since 5.9.097 (2011-06-23)
  1349. */
  1350. protected $dests = array();
  1351. /**
  1352. * Object ID for Named Destinations
  1353. * @protected
  1354. * @since 5.9.097 (2011-06-23)
  1355. */
  1356. protected $n_dests;
  1357. /**
  1358. * Embedded Files Names
  1359. * @protected
  1360. * @since 5.9.204 (2013-01-23)
  1361. */
  1362. protected $efnames = array();
  1363. /**
  1364. * Directory used for the last SVG image.
  1365. * @protected
  1366. * @since 5.0.000 (2010-05-05)
  1367. */
  1368. protected $svgdir = '';
  1369. /**
  1370. * Deafult unit of measure for SVG.
  1371. * @protected
  1372. * @since 5.0.000 (2010-05-02)
  1373. */
  1374. protected $svgunit = 'px';
  1375. /**
  1376. * Array of SVG gradients.
  1377. * @protected
  1378. * @since 5.0.000 (2010-05-02)
  1379. */
  1380. protected $svggradients = array();
  1381. /**
  1382. * ID of last SVG gradient.
  1383. * @protected
  1384. * @since 5.0.000 (2010-05-02)
  1385. */
  1386. protected $svggradientid = 0;
  1387. /**
  1388. * Boolean value true when in SVG defs group.
  1389. * @protected
  1390. * @since 5.0.000 (2010-05-02)
  1391. */
  1392. protected $svgdefsmode = false;
  1393. /**
  1394. * Array of SVG defs.
  1395. * @protected
  1396. * @since 5.0.000 (2010-05-02)
  1397. */
  1398. protected $svgdefs = array();
  1399. /**
  1400. * Boolean value true when in SVG clipPath tag.
  1401. * @protected
  1402. * @since 5.0.000 (2010-04-26)
  1403. */
  1404. protected $svgclipmode = false;
  1405. /**
  1406. * Array of SVG clipPath commands.
  1407. * @protected
  1408. * @since 5.0.000 (2010-05-02)
  1409. */
  1410. protected $svgclippaths = array();
  1411. /**
  1412. * Array of SVG clipPath tranformation matrix.
  1413. * @protected
  1414. * @since 5.8.022 (2010-08-31)
  1415. */
  1416. protected $svgcliptm = array();
  1417. /**
  1418. * ID of last SVG clipPath.
  1419. * @protected
  1420. * @since 5.0.000 (2010-05-02)
  1421. */
  1422. protected $svgclipid = 0;
  1423. /**
  1424. * SVG text.
  1425. * @protected
  1426. * @since 5.0.000 (2010-05-02)
  1427. */
  1428. protected $svgtext = '';
  1429. /**
  1430. * SVG text properties.
  1431. * @protected
  1432. * @since 5.8.013 (2010-08-23)
  1433. */
  1434. protected $svgtextmode = array();
  1435. /**
  1436. * Array of SVG properties.
  1437. * @protected
  1438. * @since 5.0.000 (2010-05-02)
  1439. */
  1440. protected $svgstyles = array(array(
  1441. 'alignment-baseline' => 'auto',
  1442. 'baseline-shift' => 'baseline',
  1443. 'clip' => 'auto',
  1444. 'clip-path' => 'none',
  1445. 'clip-rule' => 'nonzero',
  1446. 'color' => 'black',
  1447. 'color-interpolation' => 'sRGB',
  1448. 'color-interpolation-filters' => 'linearRGB',
  1449. 'color-profile' => 'auto',
  1450. 'color-rendering' => 'auto',
  1451. 'cursor' => 'auto',
  1452. 'direction' => 'ltr',
  1453. 'display' => 'inline',
  1454. 'dominant-baseline' => 'auto',
  1455. 'enable-background' => 'accumulate',
  1456. 'fill' => 'black',
  1457. 'fill-opacity' => 1,
  1458. 'fill-rule' => 'nonzero',
  1459. 'filter' => 'none',
  1460. 'flood-color' => 'black',
  1461. 'flood-opacity' => 1,
  1462. 'font' => '',
  1463. 'font-family' => 'helvetica',
  1464. 'font-size' => 'medium',
  1465. 'font-size-adjust' => 'none',
  1466. 'font-stretch' => 'normal',
  1467. 'font-style' => 'normal',
  1468. 'font-variant' => 'normal',
  1469. 'font-weight' => 'normal',
  1470. 'glyph-orientation-horizontal' => '0deg',
  1471. 'glyph-orientation-vertical' => 'auto',
  1472. 'image-rendering' => 'auto',
  1473. 'kerning' => 'auto',
  1474. 'letter-spacing' => 'normal',
  1475. 'lighting-color' => 'white',
  1476. 'marker' => '',
  1477. 'marker-end' => 'none',
  1478. 'marker-mid' => 'none',
  1479. 'marker-start' => 'none',
  1480. 'mask' => 'none',
  1481. 'opacity' => 1,
  1482. 'overflow' => 'auto',
  1483. 'pointer-events' => 'visiblePainted',
  1484. 'shape-rendering' => 'auto',
  1485. 'stop-color' => 'black',
  1486. 'stop-opacity' => 1,
  1487. 'stroke' => 'none',
  1488. 'stroke-dasharray' => 'none',
  1489. 'stroke-dashoffset' => 0,
  1490. 'stroke-linecap' => 'butt',
  1491. 'stroke-linejoin' => 'miter',
  1492. 'stroke-miterlimit' => 4,
  1493. 'stroke-opacity' => 1,
  1494. 'stroke-width' => 1,
  1495. 'text-anchor' => 'start',
  1496. 'text-decoration' => 'none',
  1497. 'text-rendering' => 'auto',
  1498. 'unicode-bidi' => 'normal',
  1499. 'visibility' => 'visible',
  1500. 'word-spacing' => 'normal',
  1501. 'writing-mode' => 'lr-tb',
  1502. 'text-color' => 'black',
  1503. 'transfmatrix' => array(1, 0, 0, 1, 0, 0)
  1504. ));
  1505. /**
  1506. * If true force sRGB color profile for all document.
  1507. * @protected
  1508. * @since 5.9.121 (2011-09-28)
  1509. */
  1510. protected $force_srgb = false;
  1511. /**
  1512. * If true set the document to PDF/A mode.
  1513. * @protected
  1514. * @since 5.9.121 (2011-09-27)
  1515. */
  1516. protected $pdfa_mode = false;
  1517. /**
  1518. * Document creation date-time
  1519. * @protected
  1520. * @since 5.9.152 (2012-03-22)
  1521. */
  1522. protected $doc_creation_timestamp;
  1523. /**
  1524. * Document modification date-time
  1525. * @protected
  1526. * @since 5.9.152 (2012-03-22)
  1527. */
  1528. protected $doc_modification_timestamp;
  1529. /**
  1530. * Custom XMP data.
  1531. * @protected
  1532. * @since 5.9.128 (2011-10-06)
  1533. */
  1534. protected $custom_xmp = '';
  1535. /**
  1536. * Overprint mode array.
  1537. * (Check the "Entries in a Graphics State Parameter Dictionary" on PDF 32000-1:2008).
  1538. * @protected
  1539. * @since 5.9.152 (2012-03-23)
  1540. */
  1541. protected $overprint = array('OP' => false, 'op' => false, 'OPM' => 0);
  1542. /**
  1543. * Alpha mode array.
  1544. * (Check the "Entries in a Graphics State Parameter Dictionary" on PDF 32000-1:2008).
  1545. * @protected
  1546. * @since 5.9.152 (2012-03-23)
  1547. */
  1548. protected $alpha = array('CA' => 1, 'ca' => 1, 'BM' => '/Normal', 'AIS' => false);
  1549. /**
  1550. * Define the page boundaries boxes to be set on document.
  1551. * @protected
  1552. * @since 5.9.152 (2012-03-23)
  1553. */
  1554. protected $page_boxes = array('MediaBox', 'CropBox', 'BleedBox', 'TrimBox', 'ArtBox');
  1555. /**
  1556. * If true print TCPDF meta link.
  1557. * @protected
  1558. * @since 5.9.152 (2012-03-23)
  1559. */
  1560. protected $tcpdflink = true;
  1561. /**
  1562. * Cache array for computed GD gamma values.
  1563. * @protected
  1564. * @since 5.9.1632 (2012-06-05)
  1565. */
  1566. protected $gdgammacache = array();
  1567. //------------------------------------------------------------
  1568. // METHODS
  1569. //------------------------------------------------------------
  1570. /**
  1571. * This is the class constructor.
  1572. * It allows to set up the page format, the orientation and the measure unit used in all the methods (except for the font sizes).
  1573. *
  1574. * IMPORTANT: Please note that this method sets the mb_internal_encoding to ASCII, so if you are using the mbstring module functions with TCPDF you need to correctly set/unset the mb_internal_encoding when needed.
  1575. *
  1576. * @param $orientation (string) 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>
  1577. * @param $unit (string) 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.
  1578. * @param $format (mixed) 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().
  1579. * @param $unicode (boolean) TRUE means that the input text is unicode (default = true)
  1580. * @param $encoding (string) Charset encoding (used only when converting back html entities); default is UTF-8.
  1581. * @param $diskcache (boolean) DEPRECATED FEATURE
  1582. * @param $pdfa (boolean) If TRUE set the document to PDF/A mode.
  1583. * @public
  1584. * @see getPageSizeFromFormat(), setPageFormat()
  1585. */
  1586. public function __construct($orientation='P', $unit='mm', $format='A4', $unicode=true, $encoding='UTF-8', $diskcache=false, $pdfa=false) {
  1587. /* Set internal character encoding to ASCII */
  1588. if (function_exists('mb_internal_encoding') AND mb_internal_encoding()) {
  1589. $this->internal_encoding = mb_internal_encoding();
  1590. mb_internal_encoding('ASCII');
  1591. }
  1592. // set file ID for trailer
  1593. $serformat = (is_array($format) ? json_encode($format) : $format);
  1594. $this->file_id = md5(TCPDF_STATIC::getRandomSeed('TCPDF'.$orientation.$unit.$serformat.$encoding));
  1595. $this->font_obj_ids = array();
  1596. $this->page_obj_id = array();
  1597. $this->form_obj_id = array();
  1598. // set pdf/a mode
  1599. $this->pdfa_mode = $pdfa;
  1600. $this->force_srgb = false;
  1601. // set language direction
  1602. $this->rtl = false;
  1603. $this->tmprtl = false;
  1604. // some checks
  1605. $this->_dochecks();
  1606. // initialization of properties
  1607. $this->isunicode = $unicode;
  1608. $this->page = 0;
  1609. $this->transfmrk[0] = array();
  1610. $this->pagedim = array();
  1611. $this->n = 2;
  1612. $this->buffer = '';
  1613. $this->pages = array();
  1614. $this->state = 0;
  1615. $this->fonts = array();
  1616. $this->FontFiles = array();
  1617. $this->diffs = array();
  1618. $this->images = array();
  1619. $this->links = array();
  1620. $this->gradients = array();
  1621. $this->InFooter = false;
  1622. $this->lasth = 0;
  1623. $this->FontFamily = defined('PDF_FONT_NAME_MAIN')?PDF_FONT_NAME_MAIN:'helvetica';
  1624. $this->FontStyle = '';
  1625. $this->FontSizePt = 12;
  1626. $this->underline = false;
  1627. $this->overline = false;
  1628. $this->linethrough = false;
  1629. $this->DrawColor = '0 G';
  1630. $this->FillColor = '0 g';
  1631. $this->TextColor = '0 g';
  1632. $this->ColorFlag = false;
  1633. $this->pdflayers = array();
  1634. // encryption values
  1635. $this->encrypted = false;
  1636. $this->last_enc_key = '';
  1637. // standard Unicode fonts
  1638. $this->CoreFonts = array(
  1639. 'courier'=>'Courier',
  1640. 'courierB'=>'Courier-Bold',
  1641. 'courierI'=>'Courier-Oblique',
  1642. 'courierBI'=>'Courier-BoldOblique',
  1643. 'helvetica'=>'Helvetica',
  1644. 'helveticaB'=>'Helvetica-Bold',
  1645. 'helveticaI'=>'Helvetica-Oblique',
  1646. 'helveticaBI'=>'Helvetica-BoldOblique',
  1647. 'times'=>'Times-Roman',
  1648. 'timesB'=>'Times-Bold',
  1649. 'timesI'=>'Times-Italic',
  1650. 'timesBI'=>'Times-BoldItalic',
  1651. 'symbol'=>'Symbol',
  1652. 'zapfdingbats'=>'ZapfDingbats'
  1653. );
  1654. // set scale factor
  1655. $this->setPageUnit($unit);
  1656. // set page format and orientation
  1657. $this->setPageFormat($format, $orientation);
  1658. // page margins (1 cm)
  1659. $margin = 28.35 / $this->k;
  1660. $this->SetMargins($margin, $margin);
  1661. $this->clMargin = $this->lMargin;
  1662. $this->crMargin = $this->rMargin;
  1663. // internal cell padding
  1664. $cpadding = $margin / 10;
  1665. $this->setCellPaddings($cpadding, 0, $cpadding, 0);
  1666. // cell margins
  1667. $this->setCellMargins(0, 0, 0, 0);
  1668. // line width (0.2 mm)
  1669. $this->LineWidth = 0.57 / $this->k;
  1670. $this->linestyleWidth = sprintf('%F w', ($this->LineWidth * $this->k));
  1671. $this->linestyleCap = '0 J';
  1672. $this->linestyleJoin = '0 j';
  1673. $this->linestyleDash = '[] 0 d';
  1674. // automatic page break
  1675. $this->SetAutoPageBreak(true, (2 * $margin));
  1676. // full width display mode
  1677. $this->SetDisplayMode('fullwidth');
  1678. // compression
  1679. $this->SetCompression();
  1680. // set default PDF version number
  1681. $this->setPDFVersion();
  1682. $this->tcpdflink = true;
  1683. $this->encoding = $encoding;
  1684. $this->HREF = array();
  1685. $this->getFontsList();
  1686. $this->fgcolor = array('R' => 0, 'G' => 0, 'B' => 0);
  1687. $this->strokecolor = array('R' => 0, 'G' => 0, 'B' => 0);
  1688. $this->bgcolor = array('R' => 255, 'G' => 255, 'B' => 255);
  1689. $this->extgstates = array();
  1690. $this->setTextShadow();
  1691. // signature
  1692. $this->sign = false;
  1693. $this->tsa_timestamp = false;
  1694. $this->tsa_data = array();
  1695. $this->signature_appearance = array('page' => 1, 'rect' => '0 0 0 0', 'name' => 'Signature');
  1696. $this->empty_signature_appearance = array();
  1697. // user's rights
  1698. $this->ur['enabled'] = false;
  1699. $this->ur['document'] = '/FullSave';
  1700. $this->ur['annots'] = '/Create/Delete/Modify/Copy/Import/Export';
  1701. $this->ur['form'] = '/Add/Delete/FillIn/Import/Export/SubmitStandalone/SpawnTemplate';
  1702. $this->ur['signature'] = '/Modify';
  1703. $this->ur['ef'] = '/Create/Delete/Modify/Import';
  1704. $this->ur['formex'] = '';
  1705. // set default JPEG quality
  1706. $this->jpeg_quality = 75;
  1707. // initialize some settings
  1708. TCPDF_FONTS::utf8Bidi(array(''), '', false, $this->isunicode, $this->CurrentFont);
  1709. // set default font
  1710. $this->SetFont($this->FontFamily, $this->FontStyle, $this->FontSizePt);
  1711. $this->setHeaderFont(array($this->FontFamily, $this->FontStyle, $this->FontSizePt));
  1712. $this->setFooterFont(array($this->FontFamily, $this->FontStyle, $this->FontSizePt));
  1713. // check if PCRE Unicode support is enabled
  1714. if ($this->isunicode AND (@preg_match('/\pL/u', 'a') == 1)) {
  1715. // PCRE unicode support is turned ON
  1716. // \s : any whitespace character
  1717. // \p{Z} : any separator
  1718. // \p{Lo} : Unicode letter or ideograph that does not have lowercase and uppercase variants. Is used to chunk chinese words.
  1719. // \xa0 : Unicode Character 'NO-BREAK SPACE' (U+00A0)
  1720. //$this->setSpacesRE('/(?!\xa0)[\s\p{Z}\p{Lo}]/u');
  1721. $this->setSpacesRE('/(?!\xa0)[\s\p{Z}]/u');
  1722. } else {
  1723. // PCRE unicode support is turned OFF
  1724. $this->setSpacesRE('/[^\S\xa0]/');
  1725. }
  1726. $this->default_form_prop = array('lineWidth'=>1, 'borderStyle'=>'solid', 'fillColor'=>array(255, 255, 255), 'strokeColor'=>array(128, 128, 128));
  1727. // set document creation and modification timestamp
  1728. $this->doc_creation_timestamp = time();
  1729. $this->doc_modification_timestamp = $this->doc_creation_timestamp;
  1730. // get default graphic vars
  1731. $this->default_graphic_vars = $this->getGraphicVars();
  1732. $this->header_xobj_autoreset = false;
  1733. $this->custom_xmp = '';
  1734. // Call cleanup method after script execution finishes or exit() is called.
  1735. // NOTE: This will not be executed if the process is killed with a SIGTERM or SIGKILL signal.
  1736. register_shutdown_function(array($this, '_destroy'), true);
  1737. }
  1738. /**
  1739. * Default destructor.
  1740. * @public
  1741. * @since 1.53.0.TC016
  1742. */
  1743. public function __destruct() {
  1744. // restore internal encoding
  1745. if (isset($this->internal_encoding) AND !empty($this->internal_encoding)) {
  1746. mb_internal_encoding($this->internal_encoding);
  1747. }
  1748. // cleanup
  1749. $this->_destroy(true);
  1750. }
  1751. /**
  1752. * Set the units of measure for the document.
  1753. * @param $unit (string) 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.
  1754. * @public
  1755. * @since 3.0.015 (2008-06-06)
  1756. */
  1757. public function setPageUnit($unit) {
  1758. $unit = strtolower($unit);
  1759. //Set scale factor
  1760. switch ($unit) {
  1761. // points
  1762. case 'px':
  1763. case 'pt': {
  1764. $this->k = 1;
  1765. break;
  1766. }
  1767. // millimeters
  1768. case 'mm': {
  1769. $this->k = $this->dpi / 25.4;
  1770. break;
  1771. }
  1772. // centimeters
  1773. case 'cm': {
  1774. $this->k = $this->dpi / 2.54;
  1775. break;
  1776. }
  1777. // inches
  1778. case 'in': {
  1779. $this->k = $this->dpi;
  1780. break;
  1781. }
  1782. // unsupported unit
  1783. default : {
  1784. $this->Error('Incorrect unit: '.$unit);
  1785. break;
  1786. }
  1787. }
  1788. $this->pdfunit = $unit;
  1789. if (isset($this->CurOrientation)) {
  1790. $this->setPageOrientation($this->CurOrientation);
  1791. }
  1792. }
  1793. /**
  1794. * Change the format of the current page
  1795. * @param $format (mixed) 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>
  1796. * <li>['format'] = page format name (one of the above);</li>
  1797. * <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>
  1798. * <li>['PZ'] : The page's preferred zoom (magnification) factor.</li>
  1799. * <li>['MediaBox'] : the boundaries of the physical medium on which the page shall be displayed or printed:</li>
  1800. * <li>['MediaBox']['llx'] : lower-left x coordinate</li>
  1801. * <li>['MediaBox']['lly'] : lower-left y coordinate</li>
  1802. * <li>['MediaBox']['urx'] : upper-right x coordinate</li>
  1803. * <li>['MediaBox']['ury'] : upper-right y coordinate</li>
  1804. * <li>['CropBox'] : the visible region of default user space:</li>
  1805. * <li>['CropBox']['llx'] : lower-left x coordinate</li>
  1806. * <li>['CropBox']['lly'] : lower-left y coordinate</li>
  1807. * <li>['CropBox']['urx'] : upper-right x coordinate</li>
  1808. * <li>['CropBox']['ury'] : upper-right y coordinate</li>
  1809. * <li>['BleedBox'] : the region to which the contents of the page shall be clipped when output in a production environment:</li>
  1810. * <li>['BleedBox']['llx'] : lower-left x coordinate</li>
  1811. * <li>['BleedBox']['lly'] : lower-left y coordinate</li>
  1812. * <li>['BleedBox']['urx'] : upper-right x coordinate</li>
  1813. * <li>['BleedBox']['ury'] : upper-right y coordinate</li>
  1814. * <li>['TrimBox'] : the intended dimensions of the finished page after trimming:</li>
  1815. * <li>['TrimBox']['llx'] : lower-left x coordinate</li>
  1816. * <li>['TrimBox']['lly'] : lower-left y coordinate</li>
  1817. * <li>['TrimBox']['urx'] : upper-right x coordinate</li>
  1818. * <li>['TrimBox']['ury'] : upper-right y coordinate</li>
  1819. * <li>['ArtBox'] : the extent of the page's meaningful content:</li>
  1820. * <li>['ArtBox']['llx'] : lower-left x coordinate</li>
  1821. * <li>['ArtBox']['lly'] : lower-left y coordinate</li>
  1822. * <li>['ArtBox']['urx'] : upper-right x coordinate</li>
  1823. * <li>['ArtBox']['ury'] : upper-right y coordinate</li>
  1824. * <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>
  1825. * <li>['BoxColorInfo'][BOXTYPE]['C'] : an array of three numbers in the range 0-255, representing the components in the DeviceRGB colour space.</li>
  1826. * <li>['BoxColorInfo'][BOXTYPE]['W'] : the guideline width in default user units</li>
  1827. * <li>['BoxColorInfo'][BOXTYPE]['S'] : the guideline style: S = Solid; D = Dashed</li>
  1828. * <li>['BoxColorInfo'][BOXTYPE]['D'] : dash array defining a pattern of dashes and gaps to be used in drawing dashed guidelines</li>
  1829. * <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>
  1830. * <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>
  1831. * <li>['trans']['S'] : transition style : Split, Blinds, Box, Wipe, Dissolve, Glitter, R, Fly, Push, Cover, Uncover, Fade</li>
  1832. * <li>['trans']['D'] : The duration of the transition effect, in seconds.</li>
  1833. * <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>
  1834. * <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>
  1835. * <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>
  1836. * <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>
  1837. * <li>['trans']['B'] : (Fly transition style only) If true, the area that shall be flown in is rectangular and opaque. Default: false.</li>
  1838. * </ul>
  1839. * @param $orientation (string) page orientation. Possible values are (case insensitive):<ul>
  1840. * <li>P or Portrait (default)</li>
  1841. * <li>L or Landscape</li>
  1842. * <li>'' (empty string) for automatic orientation</li>
  1843. * </ul>
  1844. * @protected
  1845. * @since 3.0.015 (2008-06-06)
  1846. * @see getPageSizeFromFormat()
  1847. */
  1848. protected function setPageFormat($format, $orientation='P') {
  1849. if (!empty($format) AND isset($this->pagedim[$this->page])) {
  1850. // remove inherited values
  1851. unset($this->pagedim[$this->page]);
  1852. }
  1853. if (is_string($format)) {
  1854. // get page measures from format name
  1855. $pf = TCPDF_STATIC::getPageSizeFromFormat($format);
  1856. $this->fwPt = $pf[0];
  1857. $this->fhPt = $pf[1];
  1858. } else {
  1859. // the boundaries of the physical medium on which the page shall be displayed or printed
  1860. if (isset($format['MediaBox'])) {
  1861. $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);
  1862. $this->fwPt = (($format['MediaBox']['urx'] - $format['MediaBox']['llx']) * $this->k);
  1863. $this->fhPt = (($format['MediaBox']['ury'] - $format['MediaBox']['lly']) * $this->k);
  1864. } else {
  1865. if (isset($format[0]) AND is_numeric($format[0]) AND isset($format[1]) AND is_numeric($format[1])) {
  1866. $pf = array(($format[0] * $this->k), ($format[1] * $this->k));
  1867. } else {
  1868. if (!isset($format['format'])) {
  1869. // default value
  1870. $format['format'] = 'A4';
  1871. }
  1872. $pf = TCPDF_STATIC::getPageSizeFromFormat($format['format']);
  1873. }
  1874. $this->fwPt = $pf[0];
  1875. $this->fhPt = $pf[1];
  1876. $this->pagedim = TCPDF_STATIC::setPageBoxes($this->page, 'MediaBox', 0, 0, $this->fwPt, $this->fhPt, true, $this->k, $this->pagedim);
  1877. }
  1878. // the visible region of default user space
  1879. if (isset($format['CropBox'])) {
  1880. $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);
  1881. }
  1882. // the region to which the contents of the page shall be clipped when output in a production environment
  1883. if (isset($format['BleedBox'])) {
  1884. $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);
  1885. }
  1886. // the intended dimensions of the finished page after trimming
  1887. if (isset($format['TrimBox'])) {
  1888. $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);
  1889. }
  1890. // the page's meaningful content (including potential white space)
  1891. if (isset($format['ArtBox'])) {
  1892. $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);
  1893. }
  1894. // specify the colours and other visual characteristics that should be used in displaying guidelines on the screen for the various page boundaries
  1895. if (isset($format['BoxColorInfo'])) {
  1896. $this->pagedim[$this->page]['BoxColorInfo'] = $format['BoxColorInfo'];
  1897. }
  1898. if (isset($format['Rotate']) AND (($format['Rotate'] % 90) == 0)) {
  1899. // The number of degrees by which the page shall be rotated clockwise when displayed or printed. The value shall be a multiple of 90.
  1900. $this->pagedim[$this->page]['Rotate'] = intval($format['Rotate']);
  1901. }
  1902. if (isset($format['PZ'])) {
  1903. // The page's preferred zoom (magnification) factor
  1904. $this->pagedim[$this->page]['PZ'] = floatval($format['PZ']);
  1905. }
  1906. if (isset($format['trans'])) {
  1907. // The style and duration of the visual transition to use when moving from another page to the given page during a presentation
  1908. if (isset($format['trans']['Dur'])) {
  1909. // The page's display duration
  1910. $this->pagedim[$this->page]['trans']['Dur'] = floatval($format['trans']['Dur']);
  1911. }
  1912. $stansition_styles = array('Split', 'Blinds', 'Box', 'Wipe', 'Dissolve', 'Glitter', 'R', 'Fly', 'Push', 'Cover', 'Uncover', 'Fade');
  1913. if (isset($format['trans']['S']) AND in_array($format['trans']['S'], $stansition_styles)) {
  1914. // The transition style that shall be used when moving to this page from another during a presentation
  1915. $this->pagedim[$this->page]['trans']['S'] = $format['trans']['S'];
  1916. $valid_effect = array('Split', 'Blinds');
  1917. $valid_vals = array('H', 'V');
  1918. if (isset($format['trans']['Dm']) AND in_array($format['trans']['S'], $valid_effect) AND in_array($format['trans']['Dm'], $valid_vals)) {
  1919. $this->pagedim[$this->page]['trans']['Dm'] = $format['trans']['Dm'];
  1920. }
  1921. $valid_effect = array('Split', 'Box', 'Fly');
  1922. $valid_vals = array('I', 'O');
  1923. if (isset($format['trans']['M']) AND in_array($format['trans']['S'], $valid_effect) AND in_array($format['trans']['M'], $valid_vals)) {
  1924. $this->pagedim[$this->page]['trans']['M'] = $format['trans']['M'];
  1925. }
  1926. $valid_effect = array('Wipe', 'Glitter', 'Fly', 'Cover', 'Uncover', 'Push');
  1927. if (isset($format['trans']['Di']) AND in_array($format['trans']['S'], $valid_effect)) {
  1928. if (((($format['trans']['Di'] == 90) OR ($format['trans']['Di'] == 180)) AND ($format['trans']['S'] == 'Wipe'))
  1929. OR (($format['trans']['Di'] == 315) AND ($format['trans']['S'] == 'Glitter'))
  1930. OR (($format['trans']['Di'] == 0) OR ($format['trans']['Di'] == 270))) {
  1931. $this->pagedim[$this->page]['trans']['Di'] = intval($format['trans']['Di']);
  1932. }
  1933. }
  1934. if (isset($format['trans']['SS']) AND ($format['trans']['S'] == 'Fly')) {
  1935. $this->pagedim[$this->page]['trans']['SS'] = floatval($format['trans']['SS']);
  1936. }
  1937. if (isset($format['trans']['B']) AND ($format['trans']['B'] === true) AND ($format['trans']['S'] == 'Fly')) {
  1938. $this->pagedim[$this->page]['trans']['B'] = 'true';
  1939. }
  1940. } else {
  1941. $this->pagedim[$this->page]['trans']['S'] = 'R';
  1942. }
  1943. if (isset($format['trans']['D'])) {
  1944. // The duration of the transition effect, in seconds
  1945. $this->pagedim[$this->page]['trans']['D'] = floatval($format['trans']['D']);
  1946. } else {
  1947. $this->pagedim[$this->page]['trans']['D'] = 1;
  1948. }
  1949. }
  1950. }
  1951. $this->setPageOrientation($orientation);
  1952. }
  1953. /**
  1954. * Set page orientation.
  1955. * @param $orientation (string) 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>
  1956. * @param $autopagebreak (boolean) Boolean indicating if auto-page-break mode should be on or off.
  1957. * @param $bottommargin (float) bottom margin of the page.
  1958. * @public
  1959. * @since 3.0.015 (2008-06-06)
  1960. */
  1961. public function setPageOrientation($orientation, $autopagebreak='', $bottommargin='') {
  1962. if (!isset($this->pagedim[$this->page]['MediaBox'])) {
  1963. // the boundaries of the physical medium on which the page shall be displayed or printed
  1964. $this->pagedim = TCPDF_STATIC::setPageBoxes($this->page, 'MediaBox', 0, 0, $this->fwPt, $this->fhPt, true, $this->k, $this->pagedim);
  1965. }
  1966. if (!isset($this->pagedim[$this->page]['CropBox'])) {
  1967. // the visible region of default user space
  1968. $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);
  1969. }
  1970. if (!isset($this->pagedim[$this->page]['BleedBox'])) {
  1971. // the region to which the contents of the page shall be clipped when output in a production environment
  1972. $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);
  1973. }
  1974. if (!isset($this->pagedim[$this->page]['TrimBox'])) {
  1975. // the intended dimensions of the finished page after trimming
  1976. $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);
  1977. }
  1978. if (!isset($this->pagedim[$this->page]['ArtBox'])) {
  1979. // the page's meaningful content (including potential white space)
  1980. $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);
  1981. }
  1982. if (!isset($this->pagedim[$this->page]['Rotate'])) {
  1983. // The number of degrees by which the page shall be rotated clockwise when displayed or printed. The value shall be a multiple of 90.
  1984. $this->pagedim[$this->page]['Rotate'] = 0;
  1985. }
  1986. if (!isset($this->pagedim[$this->page]['PZ'])) {
  1987. // The page's preferred zoom (magnification) factor
  1988. $this->pagedim[$this->page]['PZ'] = 1;
  1989. }
  1990. if ($this->fwPt > $this->fhPt) {
  1991. // landscape
  1992. $default_orientation = 'L';
  1993. } else {
  1994. // portrait
  1995. $default_orientation = 'P';
  1996. }
  1997. $valid_orientations = array('P', 'L');
  1998. if (empty($orientation)) {
  1999. $orientation = $default_orientation;
  2000. } else {
  2001. $orientation = strtoupper($orientation[0]);
  2002. }
  2003. if (in_array($orientation, $valid_orientations) AND ($orientation != $default_orientation)) {
  2004. $this->CurOrientation = $orientation;
  2005. $this->wPt = $this->fhPt;
  2006. $this->hPt = $this->fwPt;
  2007. } else {
  2008. $this->CurOrientation = $default_orientation;
  2009. $this->wPt = $this->fwPt;
  2010. $this->hPt = $this->fhPt;
  2011. }
  2012. if ((abs($this->pagedim[$this->page]['MediaBox']['urx'] - $this->hPt) < $this->feps) AND (abs($this->pagedim[$this->page]['MediaBox']['ury'] - $this->wPt) < $this->feps)){
  2013. // swap X and Y coordinates (change page orientation)
  2014. $this->pagedim = TCPDF_STATIC::swapPageBoxCoordinates($this->page, $this->pagedim);
  2015. }
  2016. $this->w = ($this->wPt / $this->k);
  2017. $this->h = ($this->hPt / $this->k);
  2018. if (TCPDF_STATIC::empty_string($autopagebreak)) {
  2019. if (isset($this->AutoPageBreak)) {
  2020. $autopagebreak = $this->AutoPageBreak;
  2021. } else {
  2022. $autopagebreak = true;
  2023. }
  2024. }
  2025. if (TCPDF_STATIC::empty_string($bottommargin)) {
  2026. if (isset($this->bMargin)) {
  2027. $bottommargin = $this->bMargin;
  2028. } else {
  2029. // default value = 2 cm
  2030. $bottommargin = 2 * 28.35 / $this->k;
  2031. }
  2032. }
  2033. $this->SetAutoPageBreak($autopagebreak, $bottommargin);
  2034. // store page dimensions
  2035. $this->pagedim[$this->page]['w'] = $this->wPt;
  2036. $this->pagedim[$this->page]['h'] = $this->hPt;
  2037. $this->pagedim[$this->page]['wk'] = $this->w;
  2038. $this->pagedim[$this->page]['hk'] = $this->h;
  2039. $this->pagedim[$this->page]['tm'] = $this->tMargin;
  2040. $this->pagedim[$this->page]['bm'] = $bottommargin;
  2041. $this->pagedim[$this->page]['lm'] = $this->lMargin;
  2042. $this->pagedim[$this->page]['rm'] = $this->rMargin;
  2043. $this->pagedim[$this->page]['pb'] = $autopagebreak;
  2044. $this->pagedim[$this->page]['or'] = $this->CurOrientation;
  2045. $this->pagedim[$this->page]['olm'] = $this->original_lMargin;
  2046. $this->pagedim[$this->page]['orm'] = $this->original_rMargin;
  2047. }
  2048. /**
  2049. * Set regular expression to detect withespaces or word separators.
  2050. * The pattern delimiter must be the forward-slash character "/".
  2051. * Some example patterns are:
  2052. * <pre>
  2053. * Non-Unicode or missing PCRE unicode support: "/[^\S\xa0]/"
  2054. * Unicode and PCRE unicode support: "/(?!\xa0)[\s\p{Z}]/u"
  2055. * Unicode and PCRE unicode support in Chinese mode: "/(?!\xa0)[\s\p{Z}\p{Lo}]/u"
  2056. * if PCRE unicode support is turned ON ("\P" is the negate class of "\p"):
  2057. * \s : any whitespace character
  2058. * \p{Z} : any separator
  2059. * \p{Lo} : Unicode letter or ideograph that does not have lowercase and uppercase variants. Is used to chunk chinese words.
  2060. * \xa0 : Unicode Character 'NO-BREAK SPACE' (U+00A0)
  2061. * </pre>
  2062. * @param $re (string) regular expression (leave empty for default).
  2063. * @public
  2064. * @since 4.6.016 (2009-06-15)
  2065. */
  2066. public function setSpacesRE($re='/[^\S\xa0]/') {
  2067. $this->re_spaces = $re;
  2068. $re_parts = explode('/', $re);
  2069. // get pattern parts
  2070. $this->re_space = array();
  2071. if (isset($re_parts[1]) AND !empty($re_parts[1])) {
  2072. $this->re_space['p'] = $re_parts[1];
  2073. } else {
  2074. $this->re_space['p'] = '[\s]';
  2075. }
  2076. // set pattern modifiers
  2077. if (isset($re_parts[2]) AND !empty($re_parts[2])) {
  2078. $this->re_space['m'] = $re_parts[2];
  2079. } else {
  2080. $this->re_space['m'] = '';
  2081. }
  2082. }
  2083. /**
  2084. * Enable or disable Right-To-Left language mode
  2085. * @param $enable (Boolean) if true enable Right-To-Left language mode.
  2086. * @param $resetx (Boolean) if true reset the X position on direction change.
  2087. * @public
  2088. * @since 2.0.000 (2008-01-03)
  2089. */
  2090. public function setRTL($enable, $resetx=true) {
  2091. $enable = $enable ? true : false;
  2092. $resetx = ($resetx AND ($enable != $this->rtl));
  2093. $this->rtl = $enable;
  2094. $this->tmprtl = false;
  2095. if ($resetx) {
  2096. $this->Ln(0);
  2097. }
  2098. }
  2099. /**
  2100. * Return the RTL status
  2101. * @return boolean
  2102. * @public
  2103. * @since 4.0.012 (2008-07-24)
  2104. */
  2105. public function getRTL() {
  2106. return $this->rtl;
  2107. }
  2108. /**
  2109. * Force temporary RTL language direction
  2110. * @param $mode (mixed) can be false, 'L' for LTR or 'R' for RTL
  2111. * @public
  2112. * @since 2.1.000 (2008-01-09)
  2113. */
  2114. public function setTempRTL($mode) {
  2115. $newmode = false;
  2116. switch (strtoupper($mode)) {
  2117. case 'LTR':
  2118. case 'L': {
  2119. if ($this->rtl) {
  2120. $newmode = 'L';
  2121. }
  2122. break;
  2123. }
  2124. case 'RTL':
  2125. case 'R': {
  2126. if (!$this->rtl) {
  2127. $newmode = 'R';
  2128. }
  2129. break;
  2130. }
  2131. case false:
  2132. default: {
  2133. $newmode = false;
  2134. break;
  2135. }
  2136. }
  2137. $this->tmprtl = $newmode;
  2138. }
  2139. /**
  2140. * Return the current temporary RTL status
  2141. * @return boolean
  2142. * @public
  2143. * @since 4.8.014 (2009-11-04)
  2144. */
  2145. public function isRTLTextDir() {
  2146. return ($this->rtl OR ($this->tmprtl == 'R'));
  2147. }
  2148. /**
  2149. * Set the last cell height.
  2150. * @param $h (float) cell height.
  2151. * @author Nicola Asuni
  2152. * @public
  2153. * @since 1.53.0.TC034
  2154. */
  2155. public function setLastH($h) {
  2156. $this->lasth = $h;
  2157. }
  2158. /**
  2159. * Return the cell height
  2160. * @param $fontsize (int) Font size in internal units
  2161. * @param $padding (boolean) If true add cell padding
  2162. * @public
  2163. */
  2164. public function getCellHeight($fontsize, $padding=TRUE) {
  2165. $height = ($fontsize * $this->cell_height_ratio);
  2166. if ($padding) {
  2167. $height += ($this->cell_padding['T'] + $this->cell_padding['B']);
  2168. }
  2169. return round($height, 6);
  2170. }
  2171. /**
  2172. * Reset the last cell height.
  2173. * @public
  2174. * @since 5.9.000 (2010-10-03)
  2175. */
  2176. public function resetLastH() {
  2177. $this->lasth = $this->getCellHeight($this->FontSize);
  2178. }
  2179. /**
  2180. * Get the last cell height.
  2181. * @return last cell height
  2182. * @public
  2183. * @since 4.0.017 (2008-08-05)
  2184. */
  2185. public function getLastH() {
  2186. return $this->lasth;
  2187. }
  2188. /**
  2189. * Set the adjusting factor to convert pixels to user units.
  2190. * @param $scale (float) adjusting factor to convert pixels to user units.
  2191. * @author Nicola Asuni
  2192. * @public
  2193. * @since 1.5.2
  2194. */
  2195. public function setImageScale($scale) {
  2196. $this->imgscale = $scale;
  2197. }
  2198. /**
  2199. * Returns the adjusting factor to convert pixels to user units.
  2200. * @return float adjusting factor to convert pixels to user units.
  2201. * @author Nicola Asuni
  2202. * @public
  2203. * @since 1.5.2
  2204. */
  2205. public function getImageScale() {
  2206. return $this->imgscale;
  2207. }
  2208. /**
  2209. * Returns an array of page dimensions:
  2210. * <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>
  2211. * @param $pagenum (int) page number (empty = current page)
  2212. * @return array of page dimensions.
  2213. * @author Nicola Asuni
  2214. * @public
  2215. * @since 4.5.027 (2009-03-16)
  2216. */
  2217. public function getPageDimensions($pagenum='') {
  2218. if (empty($pagenum)) {
  2219. $pagenum = $this->page;
  2220. }
  2221. return $this->pagedim[$pagenum];
  2222. }
  2223. /**
  2224. * Returns the page width in units.
  2225. * @param $pagenum (int) page number (empty = current page)
  2226. * @return int page width.
  2227. * @author Nicola Asuni
  2228. * @public
  2229. * @since 1.5.2
  2230. * @see getPageDimensions()
  2231. */
  2232. public function getPageWidth($pagenum='') {
  2233. if (empty($pagenum)) {
  2234. return $this->w;
  2235. }
  2236. return $this->pagedim[$pagenum]['w'];
  2237. }
  2238. /**
  2239. * Returns the page height in units.
  2240. * @param $pagenum (int) page number (empty = current page)
  2241. * @return int page height.
  2242. * @author Nicola Asuni
  2243. * @public
  2244. * @since 1.5.2
  2245. * @see getPageDimensions()
  2246. */
  2247. public function getPageHeight($pagenum='') {
  2248. if (empty($pagenum)) {
  2249. return $this->h;
  2250. }
  2251. return $this->pagedim[$pagenum]['h'];
  2252. }
  2253. /**
  2254. * Returns the page break margin.
  2255. * @param $pagenum (int) page number (empty = current page)
  2256. * @return int page break margin.
  2257. * @author Nicola Asuni
  2258. * @public
  2259. * @since 1.5.2
  2260. * @see getPageDimensions()
  2261. */
  2262. public function getBreakMargin($pagenum='') {
  2263. if (empty($pagenum)) {
  2264. return $this->bMargin;
  2265. }
  2266. return $this->pagedim[$pagenum]['bm'];
  2267. }
  2268. /**
  2269. * Returns the scale factor (number of points in user unit).
  2270. * @return int scale factor.
  2271. * @author Nicola Asuni
  2272. * @public
  2273. * @since 1.5.2
  2274. */
  2275. public function getScaleFactor() {
  2276. return $this->k;
  2277. }
  2278. /**
  2279. * Defines the left, top and right margins.
  2280. * @param $left (float) Left margin.
  2281. * @param $top (float) Top margin.
  2282. * @param $right (float) Right margin. Default value is the left one.
  2283. * @param $keepmargins (boolean) if true overwrites the default page margins
  2284. * @public
  2285. * @since 1.0
  2286. * @see SetLeftMargin(), SetTopMargin(), SetRightMargin(), SetAutoPageBreak()
  2287. */
  2288. public function SetMargins($left, $top, $right=-1, $keepmargins=false) {
  2289. //Set left, top and right margins
  2290. $this->lMargin = $left;
  2291. $this->tMargin = $top;
  2292. if ($right == -1) {
  2293. $right = $left;
  2294. }
  2295. $this->rMargin = $right;
  2296. if ($keepmargins) {
  2297. // overwrite original values
  2298. $this->original_lMargin = $this->lMargin;
  2299. $this->original_rMargin = $this->rMargin;
  2300. }
  2301. }
  2302. /**
  2303. * 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.
  2304. * @param $margin (float) The margin.
  2305. * @public
  2306. * @since 1.4
  2307. * @see SetTopMargin(), SetRightMargin(), SetAutoPageBreak(), SetMargins()
  2308. */
  2309. public function SetLeftMargin($margin) {
  2310. //Set left margin
  2311. $this->lMargin = $margin;
  2312. if (($this->page > 0) AND ($this->x < $margin)) {
  2313. $this->x = $margin;
  2314. }
  2315. }
  2316. /**
  2317. * Defines the top margin. The method can be called before creating the first page.
  2318. * @param $margin (float) The margin.
  2319. * @public
  2320. * @since 1.5
  2321. * @see SetLeftMargin(), SetRightMargin(), SetAutoPageBreak(), SetMargins()
  2322. */
  2323. public function SetTopMargin($margin) {
  2324. //Set top margin
  2325. $this->tMargin = $margin;
  2326. if (($this->page > 0) AND ($this->y < $margin)) {
  2327. $this->y = $margin;
  2328. }
  2329. }
  2330. /**
  2331. * Defines the right margin. The method can be called before creating the first page.
  2332. * @param $margin (float) The margin.
  2333. * @public
  2334. * @since 1.5
  2335. * @see SetLeftMargin(), SetTopMargin(), SetAutoPageBreak(), SetMargins()
  2336. */
  2337. public function SetRightMargin($margin) {
  2338. $this->rMargin = $margin;
  2339. if (($this->page > 0) AND ($this->x > ($this->w - $margin))) {
  2340. $this->x = $this->w - $margin;
  2341. }
  2342. }
  2343. /**
  2344. * Set the same internal Cell padding for top, right, bottom, left-
  2345. * @param $pad (float) internal padding.
  2346. * @public
  2347. * @since 2.1.000 (2008-01-09)
  2348. * @see getCellPaddings(), setCellPaddings()
  2349. */
  2350. public function SetCellPadding($pad) {
  2351. if ($pad >= 0) {
  2352. $this->cell_padding['L'] = $pad;
  2353. $this->cell_padding['T'] = $pad;
  2354. $this->cell_padding['R'] = $pad;
  2355. $this->cell_padding['B'] = $pad;
  2356. }
  2357. }
  2358. /**
  2359. * Set the internal Cell paddings.
  2360. * @param $left (float) left padding
  2361. * @param $top (float) top padding
  2362. * @param $right (float) right padding
  2363. * @param $bottom (float) bottom padding
  2364. * @public
  2365. * @since 5.9.000 (2010-10-03)
  2366. * @see getCellPaddings(), SetCellPadding()
  2367. */
  2368. public function setCellPaddings($left='', $top='', $right='', $bottom='') {
  2369. if (($left !== '') AND ($left >= 0)) {
  2370. $this->cell_padding['L'] = $left;
  2371. }
  2372. if (($top !== '') AND ($top >= 0)) {
  2373. $this->cell_padding['T'] = $top;
  2374. }
  2375. if (($right !== '') AND ($right >= 0)) {
  2376. $this->cell_padding['R'] = $right;
  2377. }
  2378. if (($bottom !== '') AND ($bottom >= 0)) {
  2379. $this->cell_padding['B'] = $bottom;
  2380. }
  2381. }
  2382. /**
  2383. * Get the internal Cell padding array.
  2384. * @return array of padding values
  2385. * @public
  2386. * @since 5.9.000 (2010-10-03)
  2387. * @see setCellPaddings(), SetCellPadding()
  2388. */
  2389. public function getCellPaddings() {
  2390. return $this->cell_padding;
  2391. }
  2392. /**
  2393. * Set the internal Cell margins.
  2394. * @param $left (float) left margin
  2395. * @param $top (float) top margin
  2396. * @param $right (float) right margin
  2397. * @param $bottom (float) bottom margin
  2398. * @public
  2399. * @since 5.9.000 (2010-10-03)
  2400. * @see getCellMargins()
  2401. */
  2402. public function setCellMargins($left='', $top='', $right='', $bottom='') {
  2403. if (($left !== '') AND ($left >= 0)) {
  2404. $this->cell_margin['L'] = $left;
  2405. }
  2406. if (($top !== '') AND ($top >= 0)) {
  2407. $this->cell_margin['T'] = $top;
  2408. }
  2409. if (($right !== '') AND ($right >= 0)) {
  2410. $this->cell_margin['R'] = $right;
  2411. }
  2412. if (($bottom !== '') AND ($bottom >= 0)) {
  2413. $this->cell_margin['B'] = $bottom;
  2414. }
  2415. }
  2416. /**
  2417. * Get the internal Cell margin array.
  2418. * @return array of margin values
  2419. * @public
  2420. * @since 5.9.000 (2010-10-03)
  2421. * @see setCellMargins()
  2422. */
  2423. public function getCellMargins() {
  2424. return $this->cell_margin;
  2425. }
  2426. /**
  2427. * Adjust the internal Cell padding array to take account of the line width.
  2428. * @param $brd (mixed) 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)))
  2429. * @return array of adjustments
  2430. * @public
  2431. * @since 5.9.000 (2010-10-03)
  2432. */
  2433. protected function adjustCellPadding($brd=0) {
  2434. if (empty($brd)) {
  2435. return;
  2436. }
  2437. if (is_string($brd)) {
  2438. // convert string to array
  2439. $slen = strlen($brd);
  2440. $newbrd = array();
  2441. for ($i = 0; $i < $slen; ++$i) {
  2442. $newbrd[$brd[$i]] = true;
  2443. }
  2444. $brd = $newbrd;
  2445. } elseif (($brd === 1) OR ($brd === true) OR (is_numeric($brd) AND (intval($brd) > 0))) {
  2446. $brd = array('LRTB' => true);
  2447. }
  2448. if (!is_array($brd)) {
  2449. return;
  2450. }
  2451. // store current cell padding
  2452. $cp = $this->cell_padding;
  2453. // select border mode
  2454. if (isset($brd['mode'])) {
  2455. $mode = $brd['mode'];
  2456. unset($brd['mode']);
  2457. } else {
  2458. $mode = 'normal';
  2459. }
  2460. // process borders
  2461. foreach ($brd as $border => $style) {
  2462. $line_width = $this->LineWidth;
  2463. if (is_array($style) AND isset($style['width'])) {
  2464. // get border width
  2465. $line_width = $style['width'];
  2466. }
  2467. $adj = 0; // line width inside the cell
  2468. switch ($mode) {
  2469. case 'ext': {
  2470. $adj = 0;
  2471. break;
  2472. }
  2473. case 'int': {
  2474. $adj = $line_width;
  2475. break;
  2476. }
  2477. case 'normal':
  2478. default: {
  2479. $adj = ($line_width / 2);
  2480. break;
  2481. }
  2482. }
  2483. // correct internal cell padding if required to avoid overlap between text and lines
  2484. if ((strpos($border,'T') !== false) AND ($this->cell_padding['T'] < $adj)) {
  2485. $this->cell_padding['T'] = $adj;
  2486. }
  2487. if ((strpos($border,'R') !== false) AND ($this->cell_padding['R'] < $adj)) {
  2488. $this->cell_padding['R'] = $adj;
  2489. }
  2490. if ((strpos($border,'B') !== false) AND ($this->cell_padding['B'] < $adj)) {
  2491. $this->cell_padding['B'] = $adj;
  2492. }
  2493. if ((strpos($border,'L') !== false) AND ($this->cell_padding['L'] < $adj)) {
  2494. $this->cell_padding['L'] = $adj;
  2495. }
  2496. }
  2497. return array('T' => ($this->cell_padding['T'] - $cp['T']), 'R' => ($this->cell_padding['R'] - $cp['R']), 'B' => ($this->cell_padding['B'] - $cp['B']), 'L' => ($this->cell_padding['L'] - $cp['L']));
  2498. }
  2499. /**
  2500. * 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.
  2501. * @param $auto (boolean) Boolean indicating if mode should be on or off.
  2502. * @param $margin (float) Distance from the bottom of the page.
  2503. * @public
  2504. * @since 1.0
  2505. * @see Cell(), MultiCell(), AcceptPageBreak()
  2506. */
  2507. public function SetAutoPageBreak($auto, $margin=0) {
  2508. $this->AutoPageBreak = $auto ? true : false;
  2509. $this->bMargin = $margin;
  2510. $this->PageBreakTrigger = $this->h - $margin;
  2511. }
  2512. /**
  2513. * Return the auto-page-break mode (true or false).
  2514. * @return boolean auto-page-break mode
  2515. * @public
  2516. * @since 5.9.088
  2517. */
  2518. public function getAutoPageBreak() {
  2519. return $this->AutoPageBreak;
  2520. }
  2521. /**
  2522. * Defines the way the document is to be displayed by the viewer.
  2523. * @param $zoom (mixed) 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>
  2524. * @param $layout (string) 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>
  2525. * @param $mode (string) 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>
  2526. * @public
  2527. * @since 1.2
  2528. */
  2529. public function SetDisplayMode($zoom, $layout='SinglePage', $mode='UseNone') {
  2530. if (($zoom == 'fullpage') OR ($zoom == 'fullwidth') OR ($zoom == 'real') OR ($zoom == 'default') OR (!is_string($zoom))) {
  2531. $this->ZoomMode = $zoom;
  2532. } else {
  2533. $this->Error('Incorrect zoom display mode: '.$zoom);
  2534. }
  2535. $this->LayoutMode = TCPDF_STATIC::getPageLayoutMode($layout);
  2536. $this->PageMode = TCPDF_STATIC::getPageMode($mode);
  2537. }
  2538. /**
  2539. * 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.
  2540. * Note: the Zlib extension is required for this feature. If not present, compression will be turned off.
  2541. * @param $compress (boolean) Boolean indicating if compression must be enabled.
  2542. * @public
  2543. * @since 1.4
  2544. */
  2545. public function SetCompression($compress=true) {
  2546. if (function_exists('gzcompress')) {
  2547. $this->compress = $compress ? true : false;
  2548. } else {
  2549. $this->compress = false;
  2550. }
  2551. }
  2552. /**
  2553. * Set flag to force sRGB_IEC61966-2.1 black scaled ICC color profile for the whole document.
  2554. * @param $mode (boolean) If true force sRGB output intent.
  2555. * @public
  2556. * @since 5.9.121 (2011-09-28)
  2557. */
  2558. public function setSRGBmode($mode=false) {
  2559. $this->force_srgb = $mode ? true : false;
  2560. }
  2561. /**
  2562. * Turn on/off Unicode mode for document information dictionary (meta tags).
  2563. * This has effect only when unicode mode is set to false.
  2564. * @param $unicode (boolean) if true set the meta information in Unicode
  2565. * @since 5.9.027 (2010-12-01)
  2566. * @public
  2567. */
  2568. public function SetDocInfoUnicode($unicode=true) {
  2569. $this->docinfounicode = $unicode ? true : false;
  2570. }
  2571. /**
  2572. * Defines the title of the document.
  2573. * @param $title (string) The title.
  2574. * @public
  2575. * @since 1.2
  2576. * @see SetAuthor(), SetCreator(), SetKeywords(), SetSubject()
  2577. */
  2578. public function SetTitle($title) {
  2579. $this->title = $title;
  2580. }
  2581. /**
  2582. * Defines the subject of the document.
  2583. * @param $subject (string) The subject.
  2584. * @public
  2585. * @since 1.2
  2586. * @see SetAuthor(), SetCreator(), SetKeywords(), SetTitle()
  2587. */
  2588. public function SetSubject($subject) {
  2589. $this->subject = $subject;
  2590. }
  2591. /**
  2592. * Defines the author of the document.
  2593. * @param $author (string) The name of the author.
  2594. * @public
  2595. * @since 1.2
  2596. * @see SetCreator(), SetKeywords(), SetSubject(), SetTitle()
  2597. */
  2598. public function SetAuthor($author) {
  2599. $this->author = $author;
  2600. }
  2601. /**
  2602. * Associates keywords with the document, generally in the form 'keyword1 keyword2 ...'.
  2603. * @param $keywords (string) The list of keywords.
  2604. * @public
  2605. * @since 1.2
  2606. * @see SetAuthor(), SetCreator(), SetSubject(), SetTitle()
  2607. */
  2608. public function SetKeywords($keywords) {
  2609. $this->keywords = $keywords;
  2610. }
  2611. /**
  2612. * Defines the creator of the document. This is typically the name of the application that generates the PDF.
  2613. * @param $creator (string) The name of the creator.
  2614. * @public
  2615. * @since 1.2
  2616. * @see SetAuthor(), SetKeywords(), SetSubject(), SetTitle()
  2617. */
  2618. public function SetCreator($creator) {
  2619. $this->creator = $creator;
  2620. }
  2621. /**
  2622. * Throw an exception or print an error message and die if the K_TCPDF_PARSER_THROW_EXCEPTION_ERROR constant is set to true.
  2623. * @param $msg (string) The error message
  2624. * @public
  2625. * @since 1.0
  2626. */
  2627. public function Error($msg) {
  2628. // unset all class variables
  2629. $this->_destroy(true);
  2630. if (defined('K_TCPDF_THROW_EXCEPTION_ERROR') AND !K_TCPDF_THROW_EXCEPTION_ERROR) {
  2631. die('<strong>TCPDF ERROR: </strong>'.$msg);
  2632. } else {
  2633. throw new Exception('TCPDF ERROR: '.$msg);
  2634. }
  2635. }
  2636. /**
  2637. * This method begins the generation of the PDF document.
  2638. * It is not necessary to call it explicitly because AddPage() does it automatically.
  2639. * Note: no page is created by this method
  2640. * @public
  2641. * @since 1.0
  2642. * @see AddPage(), Close()
  2643. */
  2644. public function Open() {
  2645. $this->state = 1;
  2646. }
  2647. /**
  2648. * Terminates the PDF document.
  2649. * It is not necessary to call this method explicitly because Output() does it automatically.
  2650. * If the document contains no page, AddPage() is called to prevent from getting an invalid document.
  2651. * @public
  2652. * @since 1.0
  2653. * @see Open(), Output()
  2654. */
  2655. public function Close() {
  2656. if ($this->state == 3) {
  2657. return;
  2658. }
  2659. if ($this->page == 0) {
  2660. $this->AddPage();
  2661. }
  2662. $this->endLayer();
  2663. if ($this->tcpdflink) {
  2664. // save current graphic settings
  2665. $gvars = $this->getGraphicVars();
  2666. $this->setEqualColumns();
  2667. $this->lastpage(true);
  2668. $this->SetAutoPageBreak(false);
  2669. $this->x = 0;
  2670. $this->y = $this->h - (1 / $this->k);
  2671. $this->lMargin = 0;
  2672. $this->_outSaveGraphicsState();
  2673. $font = defined('PDF_FONT_NAME_MAIN')?PDF_FONT_NAME_MAIN:'helvetica';
  2674. $this->SetFont($font, '', 1);
  2675. $this->setTextRenderingMode(0, false, false);
  2676. $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";
  2677. $lnk = "\x68\x74\x74\x70\x3a\x2f\x2f\x77\x77\x77\x2e\x74\x63\x70\x64\x66\x2e\x6f\x72\x67";
  2678. $this->Cell(0, 0, $msg, 0, 0, 'L', 0, $lnk, 0, false, 'D', 'B');
  2679. $this->_outRestoreGraphicsState();
  2680. // restore graphic settings
  2681. $this->setGraphicVars($gvars);
  2682. }
  2683. // close page
  2684. $this->endPage();
  2685. // close document
  2686. $this->_enddoc();
  2687. // unset all class variables (except critical ones)
  2688. $this->_destroy(false);
  2689. }
  2690. /**
  2691. * Move pointer at the specified document page and update page dimensions.
  2692. * @param $pnum (int) page number (1 ... numpages)
  2693. * @param $resetmargins (boolean) if true reset left, right, top margins and Y position.
  2694. * @public
  2695. * @since 2.1.000 (2008-01-07)
  2696. * @see getPage(), lastpage(), getNumPages()
  2697. */
  2698. public function setPage($pnum, $resetmargins=false) {
  2699. if (($pnum == $this->page) AND ($this->state == 2)) {
  2700. return;
  2701. }
  2702. if (($pnum > 0) AND ($pnum <= $this->numpages)) {
  2703. $this->state = 2;
  2704. // save current graphic settings
  2705. //$gvars = $this->getGraphicVars();
  2706. $oldpage = $this->page;
  2707. $this->page = $pnum;
  2708. $this->wPt = $this->pagedim[$this->page]['w'];
  2709. $this->hPt = $this->pagedim[$this->page]['h'];
  2710. $this->w = $this->pagedim[$this->page]['wk'];
  2711. $this->h = $this->pagedim[$this->page]['hk'];
  2712. $this->tMargin = $this->pagedim[$this->page]['tm'];
  2713. $this->bMargin = $this->pagedim[$this->page]['bm'];
  2714. $this->original_lMargin = $this->pagedim[$this->page]['olm'];
  2715. $this->original_rMargin = $this->pagedim[$this->page]['orm'];
  2716. $this->AutoPageBreak = $this->pagedim[$this->page]['pb'];
  2717. $this->CurOrientation = $this->pagedim[$this->page]['or'];
  2718. $this->SetAutoPageBreak($this->AutoPageBreak, $this->bMargin);
  2719. // restore graphic settings
  2720. //$this->setGraphicVars($gvars);
  2721. if ($resetmargins) {
  2722. $this->lMargin = $this->pagedim[$this->page]['olm'];
  2723. $this->rMargin = $this->pagedim[$this->page]['orm'];
  2724. $this->SetY($this->tMargin);
  2725. } else {
  2726. // account for booklet mode
  2727. if ($this->pagedim[$this->page]['olm'] != $this->pagedim[$oldpage]['olm']) {
  2728. $deltam = $this->pagedim[$this->page]['olm'] - $this->pagedim[$this->page]['orm'];
  2729. $this->lMargin += $deltam;
  2730. $this->rMargin -= $deltam;
  2731. }
  2732. }
  2733. } else {
  2734. $this->Error('Wrong page number on setPage() function: '.$pnum);
  2735. }
  2736. }
  2737. /**
  2738. * Reset pointer to the last document page.
  2739. * @param $resetmargins (boolean) if true reset left, right, top margins and Y position.
  2740. * @public
  2741. * @since 2.0.000 (2008-01-04)
  2742. * @see setPage(), getPage(), getNumPages()
  2743. */
  2744. public function lastPage($resetmargins=false) {
  2745. $this->setPage($this->getNumPages(), $resetmargins);
  2746. }
  2747. /**
  2748. * Get current document page number.
  2749. * @return int page number
  2750. * @public
  2751. * @since 2.1.000 (2008-01-07)
  2752. * @see setPage(), lastpage(), getNumPages()
  2753. */
  2754. public function getPage() {
  2755. return $this->page;
  2756. }
  2757. /**
  2758. * Get the total number of insered pages.
  2759. * @return int number of pages
  2760. * @public
  2761. * @since 2.1.000 (2008-01-07)
  2762. * @see setPage(), getPage(), lastpage()
  2763. */
  2764. public function getNumPages() {
  2765. return $this->numpages;
  2766. }
  2767. /**
  2768. * Adds a new TOC (Table Of Content) page to the document.
  2769. * @param $orientation (string) page orientation.
  2770. * @param $format (mixed) 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().
  2771. * @param $keepmargins (boolean) if true overwrites the default page margins with the current margins
  2772. * @public
  2773. * @since 5.0.001 (2010-05-06)
  2774. * @see AddPage(), startPage(), endPage(), endTOCPage()
  2775. */
  2776. public function addTOCPage($orientation='', $format='', $keepmargins=false) {
  2777. $this->AddPage($orientation, $format, $keepmargins, true);
  2778. }
  2779. /**
  2780. * Terminate the current TOC (Table Of Content) page
  2781. * @public
  2782. * @since 5.0.001 (2010-05-06)
  2783. * @see AddPage(), startPage(), endPage(), addTOCPage()
  2784. */
  2785. public function endTOCPage() {
  2786. $this->endPage(true);
  2787. }
  2788. /**
  2789. * 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).
  2790. * The origin of the coordinate system is at the top-left corner (or top-right for RTL) and increasing ordinates go downwards.
  2791. * @param $orientation (string) page orientation. Possible values are (case insensitive):<ul><li>P or PORTRAIT (default)</li><li>L or LANDSCAPE</li></ul>
  2792. * @param $format (mixed) 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().
  2793. * @param $keepmargins (boolean) if true overwrites the default page margins with the current margins
  2794. * @param $tocpage (boolean) if true set the tocpage state to true (the added page will be used to display Table Of Content).
  2795. * @public
  2796. * @since 1.0
  2797. * @see startPage(), endPage(), addTOCPage(), endTOCPage(), getPageSizeFromFormat(), setPageFormat()
  2798. */
  2799. public function AddPage($orientation='', $format='', $keepmargins=false, $tocpage=false) {
  2800. if ($this->inxobj) {
  2801. // we are inside an XObject template
  2802. return;
  2803. }
  2804. if (!isset($this->original_lMargin) OR $keepmargins) {
  2805. $this->original_lMargin = $this->lMargin;
  2806. }
  2807. if (!isset($this->original_rMargin) OR $keepmargins) {
  2808. $this->original_rMargin = $this->rMargin;
  2809. }
  2810. // terminate previous page
  2811. $this->endPage();
  2812. // start new page
  2813. $this->startPage($orientation, $format, $tocpage);
  2814. }
  2815. /**
  2816. * Terminate the current page
  2817. * @param $tocpage (boolean) if true set the tocpage state to false (end the page used to display Table Of Content).
  2818. * @public
  2819. * @since 4.2.010 (2008-11-14)
  2820. * @see AddPage(), startPage(), addTOCPage(), endTOCPage()
  2821. */
  2822. public function endPage($tocpage=false) {
  2823. // check if page is already closed
  2824. if (($this->page == 0) OR ($this->numpages > $this->page) OR (!$this->pageopen[$this->page])) {
  2825. return;
  2826. }
  2827. // print page footer
  2828. $this->setFooter();
  2829. // close page
  2830. $this->_endpage();
  2831. // mark page as closed
  2832. $this->pageopen[$this->page] = false;
  2833. if ($tocpage) {
  2834. $this->tocpage = false;
  2835. }
  2836. }
  2837. /**
  2838. * Starts a new page to the document. The page must be closed using the endPage() function.
  2839. * The origin of the coordinate system is at the top-left corner and increasing ordinates go downwards.
  2840. * @param $orientation (string) page orientation. Possible values are (case insensitive):<ul><li>P or PORTRAIT (default)</li><li>L or LANDSCAPE</li></ul>
  2841. * @param $format (mixed) 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().
  2842. * @param $tocpage (boolean) if true the page is designated to contain the Table-Of-Content.
  2843. * @since 4.2.010 (2008-11-14)
  2844. * @see AddPage(), endPage(), addTOCPage(), endTOCPage(), getPageSizeFromFormat(), setPageFormat()
  2845. * @public
  2846. */
  2847. public function startPage($orientation='', $format='', $tocpage=false) {
  2848. if ($tocpage) {
  2849. $this->tocpage = true;
  2850. }
  2851. // move page numbers of documents to be attached
  2852. if ($this->tocpage) {
  2853. // move reference to unexistent pages (used for page attachments)
  2854. // adjust outlines
  2855. $tmpoutlines = $this->outlines;
  2856. foreach ($tmpoutlines as $key => $outline) {
  2857. if (!$outline['f'] AND ($outline['p'] > $this->numpages)) {
  2858. $this->outlines[$key]['p'] = ($outline['p'] + 1);
  2859. }
  2860. }
  2861. // adjust dests
  2862. $tmpdests = $this->dests;
  2863. foreach ($tmpdests as $key => $dest) {
  2864. if (!$dest['f'] AND ($dest['p'] > $this->numpages)) {
  2865. $this->dests[$key]['p'] = ($dest['p'] + 1);
  2866. }
  2867. }
  2868. // adjust links
  2869. $tmplinks = $this->links;
  2870. foreach ($tmplinks as $key => $link) {
  2871. if (!$link['f'] AND ($link['p'] > $this->numpages)) {
  2872. $this->links[$key]['p'] = ($link['p'] + 1);
  2873. }
  2874. }
  2875. }
  2876. if ($this->numpages > $this->page) {
  2877. // this page has been already added
  2878. $this->setPage($this->page + 1);
  2879. $this->SetY($this->tMargin);
  2880. return;
  2881. }
  2882. // start a new page
  2883. if ($this->state == 0) {
  2884. $this->Open();
  2885. }
  2886. ++$this->numpages;
  2887. $this->swapMargins($this->booklet);
  2888. // save current graphic settings
  2889. $gvars = $this->getGraphicVars();
  2890. // start new page
  2891. $this->_beginpage($orientation, $format);
  2892. // mark page as open
  2893. $this->pageopen[$this->page] = true;
  2894. // restore graphic settings
  2895. $this->setGraphicVars($gvars);
  2896. // mark this point
  2897. $this->setPageMark();
  2898. // print page header
  2899. $this->setHeader();
  2900. // restore graphic settings
  2901. $this->setGraphicVars($gvars);
  2902. // mark this point
  2903. $this->setPageMark();
  2904. // print table header (if any)
  2905. $this->setTableHeader();
  2906. // set mark for empty page check
  2907. $this->emptypagemrk[$this->page]= $this->pagelen[$this->page];
  2908. }
  2909. /**
  2910. * Set start-writing mark on current page stream used to put borders and fills.
  2911. * Borders and fills are always created after content and inserted on the position marked by this method.
  2912. * This function must be called after calling Image() function for a background image.
  2913. * Background images must be always inserted before calling Multicell() or WriteHTMLCell() or WriteHTML() functions.
  2914. * @public
  2915. * @since 4.0.016 (2008-07-30)
  2916. */
  2917. public function setPageMark() {
  2918. $this->intmrk[$this->page] = $this->pagelen[$this->page];
  2919. $this->bordermrk[$this->page] = $this->intmrk[$this->page];
  2920. $this->setContentMark();
  2921. }
  2922. /**
  2923. * Set start-writing mark on selected page.
  2924. * Borders and fills are always created after content and inserted on the position marked by this method.
  2925. * @param $page (int) page number (default is the current page)
  2926. * @protected
  2927. * @since 4.6.021 (2009-07-20)
  2928. */
  2929. protected function setContentMark($page=0) {
  2930. if ($page <= 0) {
  2931. $page = $this->page;
  2932. }
  2933. if (isset($this->footerlen[$page])) {
  2934. $this->cntmrk[$page] = $this->pagelen[$page] - $this->footerlen[$page];
  2935. } else {
  2936. $this->cntmrk[$page] = $this->pagelen[$page];
  2937. }
  2938. }
  2939. /**
  2940. * Set header data.
  2941. * @param $ln (string) header image logo
  2942. * @param $lw (string) header image logo width in mm
  2943. * @param $ht (string) string to print as title on document header
  2944. * @param $hs (string) string to print on document header
  2945. * @param $tc (array) RGB array color for text.
  2946. * @param $lc (array) RGB array color for line.
  2947. * @public
  2948. */
  2949. public function setHeaderData($ln='', $lw=0, $ht='', $hs='', $tc=array(0,0,0), $lc=array(0,0,0)) {
  2950. $this->header_logo = $ln;
  2951. $this->header_logo_width = $lw;
  2952. $this->header_title = $ht;
  2953. $this->header_string = $hs;
  2954. $this->header_text_color = $tc;
  2955. $this->header_line_color = $lc;
  2956. }
  2957. /**
  2958. * Set footer data.
  2959. * @param $tc (array) RGB array color for text.
  2960. * @param $lc (array) RGB array color for line.
  2961. * @public
  2962. */
  2963. public function setFooterData($tc=array(0,0,0), $lc=array(0,0,0)) {
  2964. $this->footer_text_color = $tc;
  2965. $this->footer_line_color = $lc;
  2966. }
  2967. /**
  2968. * Returns header data:
  2969. * <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>
  2970. * @return array()
  2971. * @public
  2972. * @since 4.0.012 (2008-07-24)
  2973. */
  2974. public function getHeaderData() {
  2975. $ret = array();
  2976. $ret['logo'] = $this->header_logo;
  2977. $ret['logo_width'] = $this->header_logo_width;
  2978. $ret['title'] = $this->header_title;
  2979. $ret['string'] = $this->header_string;
  2980. $ret['text_color'] = $this->header_text_color;
  2981. $ret['line_color'] = $this->header_line_color;
  2982. return $ret;
  2983. }
  2984. /**
  2985. * Set header margin.
  2986. * (minimum distance between header and top page margin)
  2987. * @param $hm (int) distance in user units
  2988. * @public
  2989. */
  2990. public function setHeaderMargin($hm=10) {
  2991. $this->header_margin = $hm;
  2992. }
  2993. /**
  2994. * Returns header margin in user units.
  2995. * @return float
  2996. * @since 4.0.012 (2008-07-24)
  2997. * @public
  2998. */
  2999. public function getHeaderMargin() {
  3000. return $this->header_margin;
  3001. }
  3002. /**
  3003. * Set footer margin.
  3004. * (minimum distance between footer and bottom page margin)
  3005. * @param $fm (int) distance in user units
  3006. * @public
  3007. */
  3008. public function setFooterMargin($fm=10) {
  3009. $this->footer_margin = $fm;
  3010. }
  3011. /**
  3012. * Returns footer margin in user units.
  3013. * @return float
  3014. * @since 4.0.012 (2008-07-24)
  3015. * @public
  3016. */
  3017. public function getFooterMargin() {
  3018. return $this->footer_margin;
  3019. }
  3020. /**
  3021. * Set a flag to print page header.
  3022. * @param $val (boolean) set to true to print the page header (default), false otherwise.
  3023. * @public
  3024. */
  3025. public function setPrintHeader($val=true) {
  3026. $this->print_header = $val ? true : false;
  3027. }
  3028. /**
  3029. * Set a flag to print page footer.
  3030. * @param $val (boolean) set to true to print the page footer (default), false otherwise.
  3031. * @public
  3032. */
  3033. public function setPrintFooter($val=true) {
  3034. $this->print_footer = $val ? true : false;
  3035. }
  3036. /**
  3037. * Return the right-bottom (or left-bottom for RTL) corner X coordinate of last inserted image
  3038. * @return float
  3039. * @public
  3040. */
  3041. public function getImageRBX() {
  3042. return $this->img_rb_x;
  3043. }
  3044. /**
  3045. * Return the right-bottom (or left-bottom for RTL) corner Y coordinate of last inserted image
  3046. * @return float
  3047. * @public
  3048. */
  3049. public function getImageRBY() {
  3050. return $this->img_rb_y;
  3051. }
  3052. /**
  3053. * Reset the xobject template used by Header() method.
  3054. * @public
  3055. */
  3056. public function resetHeaderTemplate() {
  3057. $this->header_xobjid = false;
  3058. }
  3059. /**
  3060. * Set a flag to automatically reset the xobject template used by Header() method at each page.
  3061. * @param $val (boolean) set to true to reset Header xobject template at each page, false otherwise.
  3062. * @public
  3063. */
  3064. public function setHeaderTemplateAutoreset($val=true) {
  3065. $this->header_xobj_autoreset = $val ? true : false;
  3066. }
  3067. /**
  3068. * This method is used to render the page header.
  3069. * It is automatically called by AddPage() and could be overwritten in your own inherited class.
  3070. * @public
  3071. */
  3072. public function Header() {
  3073. if ($this->header_xobjid === false) {
  3074. // start a new XObject Template
  3075. $this->header_xobjid = $this->startTemplate($this->w, $this->tMargin);
  3076. $headerfont = $this->getHeaderFont();
  3077. $headerdata = $this->getHeaderData();
  3078. $this->y = $this->header_margin;
  3079. if ($this->rtl) {
  3080. $this->x = $this->w - $this->original_rMargin;
  3081. } else {
  3082. $this->x = $this->original_lMargin;
  3083. }
  3084. if (($headerdata['logo']) AND ($headerdata['logo'] != K_BLANK_IMAGE)) {
  3085. $imgtype = TCPDF_IMAGES::getImageFileType(K_PATH_IMAGES.$headerdata['logo']);
  3086. if (($imgtype == 'eps') OR ($imgtype == 'ai')) {
  3087. $this->ImageEps(K_PATH_IMAGES.$headerdata['logo'], '', '', $headerdata['logo_width']);
  3088. } elseif ($imgtype == 'svg') {
  3089. $this->ImageSVG(K_PATH_IMAGES.$headerdata['logo'], '', '', $headerdata['logo_width']);
  3090. } else {
  3091. $this->Image(K_PATH_IMAGES.$headerdata['logo'], '', '', $headerdata['logo_width']);
  3092. }
  3093. $imgy = $this->getImageRBY();
  3094. } else {
  3095. $imgy = $this->y;
  3096. }
  3097. $cell_height = $this->getCellHeight($headerfont[2] / $this->k);
  3098. // set starting margin for text data cell
  3099. if ($this->getRTL()) {
  3100. $header_x = $this->original_rMargin + ($headerdata['logo_width'] * 1.1);
  3101. } else {
  3102. $header_x = $this->original_lMargin + ($headerdata['logo_width'] * 1.1);
  3103. }
  3104. $cw = $this->w - $this->original_lMargin - $this->original_rMargin - ($headerdata['logo_width'] * 1.1);
  3105. $this->SetTextColorArray($this->header_text_color);
  3106. // header title
  3107. $this->SetFont($headerfont[0], 'B', $headerfont[2] + 1);
  3108. $this->SetX($header_x);
  3109. $this->Cell($cw, $cell_height, $headerdata['title'], 0, 1, '', 0, '', 0);
  3110. // header string
  3111. $this->SetFont($headerfont[0], $headerfont[1], $headerfont[2]);
  3112. $this->SetX($header_x);
  3113. $this->MultiCell($cw, $cell_height, $headerdata['string'], 0, '', 0, 1, '', '', true, 0, false, true, 0, 'T', false);
  3114. // print an ending header line
  3115. $this->SetLineStyle(array('width' => 0.85 / $this->k, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => $headerdata['line_color']));
  3116. $this->SetY((2.835 / $this->k) + max($imgy, $this->y));
  3117. if ($this->rtl) {
  3118. $this->SetX($this->original_rMargin);
  3119. } else {
  3120. $this->SetX($this->original_lMargin);
  3121. }
  3122. $this->Cell(($this->w - $this->original_lMargin - $this->original_rMargin), 0, '', 'T', 0, 'C');
  3123. $this->endTemplate();
  3124. }
  3125. // print header template
  3126. $x = 0;
  3127. $dx = 0;
  3128. if (!$this->header_xobj_autoreset AND $this->booklet AND (($this->page % 2) == 0)) {
  3129. // adjust margins for booklet mode
  3130. $dx = ($this->original_lMargin - $this->original_rMargin);
  3131. }
  3132. if ($this->rtl) {
  3133. $x = $this->w + $dx;
  3134. } else {
  3135. $x = 0 + $dx;
  3136. }
  3137. $this->printTemplate($this->header_xobjid, $x, 0, 0, 0, '', '', false);
  3138. if ($this->header_xobj_autoreset) {
  3139. // reset header xobject template at each page
  3140. $this->header_xobjid = false;
  3141. }
  3142. }
  3143. /**
  3144. * This method is used to render the page footer.
  3145. * It is automatically called by AddPage() and could be overwritten in your own inherited class.
  3146. * @public
  3147. */
  3148. public function Footer() {
  3149. $cur_y = $this->y;
  3150. $this->SetTextColorArray($this->footer_text_color);
  3151. //set style for cell border
  3152. $line_width = (0.85 / $this->k);
  3153. $this->SetLineStyle(array('width' => $line_width, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => $this->footer_line_color));
  3154. //print document barcode
  3155. $barcode = $this->getBarcode();
  3156. if (!empty($barcode)) {
  3157. $this->Ln($line_width);
  3158. $barcode_width = round(($this->w - $this->original_lMargin - $this->original_rMargin) / 3);
  3159. $style = array(
  3160. 'position' => $this->rtl?'R':'L',
  3161. 'align' => $this->rtl?'R':'L',
  3162. 'stretch' => false,
  3163. 'fitwidth' => true,
  3164. 'cellfitalign' => '',
  3165. 'border' => false,
  3166. 'padding' => 0,
  3167. 'fgcolor' => array(0,0,0),
  3168. 'bgcolor' => false,
  3169. 'text' => false
  3170. );
  3171. $this->write1DBarcode($barcode, 'C128', '', $cur_y + $line_width, '', (($this->footer_margin / 3) - $line_width), 0.3, $style, '');
  3172. }
  3173. $w_page = isset($this->l['w_page']) ? $this->l['w_page'].' ' : '';
  3174. if (empty($this->pagegroups)) {
  3175. $pagenumtxt = $w_page.$this->getAliasNumPage().' / '.$this->getAliasNbPages();
  3176. } else {
  3177. $pagenumtxt = $w_page.$this->getPageNumGroupAlias().' / '.$this->getPageGroupAlias();
  3178. }
  3179. $this->SetY($cur_y);
  3180. //Print page number
  3181. if ($this->getRTL()) {
  3182. $this->SetX($this->original_rMargin);
  3183. $this->Cell(0, 0, $pagenumtxt, 'T', 0, 'L');
  3184. } else {
  3185. $this->SetX($this->original_lMargin);
  3186. $this->Cell(0, 0, $this->getAliasRightShift().$pagenumtxt, 'T', 0, 'R');
  3187. }
  3188. }
  3189. /**
  3190. * This method is used to render the page header.
  3191. * @protected
  3192. * @since 4.0.012 (2008-07-24)
  3193. */
  3194. protected function setHeader() {
  3195. if (!$this->print_header OR ($this->state != 2)) {
  3196. return;
  3197. }
  3198. $this->InHeader = true;
  3199. $this->setGraphicVars($this->default_graphic_vars);
  3200. $temp_thead = $this->thead;
  3201. $temp_theadMargins = $this->theadMargins;
  3202. $lasth = $this->lasth;
  3203. $newline = $this->newline;
  3204. $this->_outSaveGraphicsState();
  3205. $this->rMargin = $this->original_rMargin;
  3206. $this->lMargin = $this->original_lMargin;
  3207. $this->SetCellPadding(0);
  3208. //set current position
  3209. if ($this->rtl) {
  3210. $this->SetXY($this->original_rMargin, $this->header_margin);
  3211. } else {
  3212. $this->SetXY($this->original_lMargin, $this->header_margin);
  3213. }
  3214. $this->SetFont($this->header_font[0], $this->header_font[1], $this->header_font[2]);
  3215. $this->Header();
  3216. //restore position
  3217. if ($this->rtl) {
  3218. $this->SetXY($this->original_rMargin, $this->tMargin);
  3219. } else {
  3220. $this->SetXY($this->original_lMargin, $this->tMargin);
  3221. }
  3222. $this->_outRestoreGraphicsState();
  3223. $this->lasth = $lasth;
  3224. $this->thead = $temp_thead;
  3225. $this->theadMargins = $temp_theadMargins;
  3226. $this->newline = $newline;
  3227. $this->InHeader = false;
  3228. }
  3229. /**
  3230. * This method is used to render the page footer.
  3231. * @protected
  3232. * @since 4.0.012 (2008-07-24)
  3233. */
  3234. protected function setFooter() {
  3235. if ($this->state != 2) {
  3236. return;
  3237. }
  3238. $this->InFooter = true;
  3239. // save current graphic settings
  3240. $gvars = $this->getGraphicVars();
  3241. // mark this point
  3242. $this->footerpos[$this->page] = $this->pagelen[$this->page];
  3243. $this->_out("\n");
  3244. if ($this->print_footer) {
  3245. $this->setGraphicVars($this->default_graphic_vars);
  3246. $this->current_column = 0;
  3247. $this->num_columns = 1;
  3248. $temp_thead = $this->thead;
  3249. $temp_theadMargins = $this->theadMargins;
  3250. $lasth = $this->lasth;
  3251. $this->_outSaveGraphicsState();
  3252. $this->rMargin = $this->original_rMargin;
  3253. $this->lMargin = $this->original_lMargin;
  3254. $this->SetCellPadding(0);
  3255. //set current position
  3256. $footer_y = $this->h - $this->footer_margin;
  3257. if ($this->rtl) {
  3258. $this->SetXY($this->original_rMargin, $footer_y);
  3259. } else {
  3260. $this->SetXY($this->original_lMargin, $footer_y);
  3261. }
  3262. $this->SetFont($this->footer_font[0], $this->footer_font[1], $this->footer_font[2]);
  3263. $this->Footer();
  3264. //restore position
  3265. if ($this->rtl) {
  3266. $this->SetXY($this->original_rMargin, $this->tMargin);
  3267. } else {
  3268. $this->SetXY($this->original_lMargin, $this->tMargin);
  3269. }
  3270. $this->_outRestoreGraphicsState();
  3271. $this->lasth = $lasth;
  3272. $this->thead = $temp_thead;
  3273. $this->theadMargins = $temp_theadMargins;
  3274. }
  3275. // restore graphic settings
  3276. $this->setGraphicVars($gvars);
  3277. $this->current_column = $gvars['current_column'];
  3278. $this->num_columns = $gvars['num_columns'];
  3279. // calculate footer length
  3280. $this->footerlen[$this->page] = $this->pagelen[$this->page] - $this->footerpos[$this->page] + 1;
  3281. $this->InFooter = false;
  3282. }
  3283. /**
  3284. * Check if we are on the page body (excluding page header and footer).
  3285. * @return true if we are not in page header nor in page footer, false otherwise.
  3286. * @protected
  3287. * @since 5.9.091 (2011-06-15)
  3288. */
  3289. protected function inPageBody() {
  3290. return (($this->InHeader === false) AND ($this->InFooter === false));
  3291. }
  3292. /**
  3293. * This method is used to render the table header on new page (if any).
  3294. * @protected
  3295. * @since 4.5.030 (2009-03-25)
  3296. */
  3297. protected function setTableHeader() {
  3298. if ($this->num_columns > 1) {
  3299. // multi column mode
  3300. return;
  3301. }
  3302. if (isset($this->theadMargins['top'])) {
  3303. // restore the original top-margin
  3304. $this->tMargin = $this->theadMargins['top'];
  3305. $this->pagedim[$this->page]['tm'] = $this->tMargin;
  3306. $this->y = $this->tMargin;
  3307. }
  3308. if (!TCPDF_STATIC::empty_string($this->thead) AND (!$this->inthead)) {
  3309. // set margins
  3310. $prev_lMargin = $this->lMargin;
  3311. $prev_rMargin = $this->rMargin;
  3312. $prev_cell_padding = $this->cell_padding;
  3313. $this->lMargin = $this->theadMargins['lmargin'] + ($this->pagedim[$this->page]['olm'] - $this->pagedim[$this->theadMargins['page']]['olm']);
  3314. $this->rMargin = $this->theadMargins['rmargin'] + ($this->pagedim[$this->page]['orm'] - $this->pagedim[$this->theadMargins['page']]['orm']);
  3315. $this->cell_padding = $this->theadMargins['cell_padding'];
  3316. if ($this->rtl) {
  3317. $this->x = $this->w - $this->rMargin;
  3318. } else {
  3319. $this->x = $this->lMargin;
  3320. }
  3321. // account for special "cell" mode
  3322. if ($this->theadMargins['cell']) {
  3323. if ($this->rtl) {
  3324. $this->x -= $this->cell_padding['R'];
  3325. } else {
  3326. $this->x += $this->cell_padding['L'];
  3327. }
  3328. }
  3329. $gvars = $this->getGraphicVars();
  3330. if (!empty($this->theadMargins['gvars'])) {
  3331. // set the correct graphic style
  3332. $this->setGraphicVars($this->theadMargins['gvars']);
  3333. $this->rMargin = $gvars['rMargin'];
  3334. $this->lMargin = $gvars['lMargin'];
  3335. }
  3336. // print table header
  3337. $this->writeHTML($this->thead, false, false, false, false, '');
  3338. $this->setGraphicVars($gvars);
  3339. // set new top margin to skip the table headers
  3340. if (!isset($this->theadMargins['top'])) {
  3341. $this->theadMargins['top'] = $this->tMargin;
  3342. }
  3343. // store end of header position
  3344. if (!isset($this->columns[0]['th'])) {
  3345. $this->columns[0]['th'] = array();
  3346. }
  3347. $this->columns[0]['th']['\''.$this->page.'\''] = $this->y;
  3348. $this->tMargin = $this->y;
  3349. $this->pagedim[$this->page]['tm'] = $this->tMargin;
  3350. $this->lasth = 0;
  3351. $this->lMargin = $prev_lMargin;
  3352. $this->rMargin = $prev_rMargin;
  3353. $this->cell_padding = $prev_cell_padding;
  3354. }
  3355. }
  3356. /**
  3357. * Returns the current page number.
  3358. * @return int page number
  3359. * @public
  3360. * @since 1.0
  3361. * @see getAliasNbPages()
  3362. */
  3363. public function PageNo() {
  3364. return $this->page;
  3365. }
  3366. /**
  3367. * Returns the array of spot colors.
  3368. * @return (array) Spot colors array.
  3369. * @public
  3370. * @since 6.0.038 (2013-09-30)
  3371. */
  3372. public function getAllSpotColors() {
  3373. return $this->spot_colors;
  3374. }
  3375. /**
  3376. * Defines a new spot color.
  3377. * It can be expressed in RGB components or gray scale.
  3378. * The method can be called before the first page is created and the value is retained from page to page.
  3379. * @param $name (string) Full name of the spot color.
  3380. * @param $c (float) Cyan color for CMYK. Value between 0 and 100.
  3381. * @param $m (float) Magenta color for CMYK. Value between 0 and 100.
  3382. * @param $y (float) Yellow color for CMYK. Value between 0 and 100.
  3383. * @param $k (float) Key (Black) color for CMYK. Value between 0 and 100.
  3384. * @public
  3385. * @since 4.0.024 (2008-09-12)
  3386. * @see SetDrawSpotColor(), SetFillSpotColor(), SetTextSpotColor()
  3387. */
  3388. public function AddSpotColor($name, $c, $m, $y, $k) {
  3389. if (!isset($this->spot_colors[$name])) {
  3390. $i = (1 + count($this->spot_colors));
  3391. $this->spot_colors[$name] = array('C' => $c, 'M' => $m, 'Y' => $y, 'K' => $k, 'name' => $name, 'i' => $i);
  3392. }
  3393. }
  3394. /**
  3395. * Set the spot color for the specified type ('draw', 'fill', 'text').
  3396. * @param $type (string) Type of object affected by this color: ('draw', 'fill', 'text').
  3397. * @param $name (string) Name of the spot color.
  3398. * @param $tint (float) Intensity of the color (from 0 to 100 ; 100 = full intensity by default).
  3399. * @return (string) PDF color command.
  3400. * @public
  3401. * @since 5.9.125 (2011-10-03)
  3402. */
  3403. public function setSpotColor($type, $name, $tint=100) {
  3404. $spotcolor = TCPDF_COLORS::getSpotColor($name, $this->spot_colors);
  3405. if ($spotcolor === false) {
  3406. $this->Error('Undefined spot color: '.$name.', you must add it using the AddSpotColor() method.');
  3407. }
  3408. $tint = (max(0, min(100, $tint)) / 100);
  3409. $pdfcolor = sprintf('/CS%d ', $this->spot_colors[$name]['i']);
  3410. switch ($type) {
  3411. case 'draw': {
  3412. $pdfcolor .= sprintf('CS %F SCN', $tint);
  3413. $this->DrawColor = $pdfcolor;
  3414. $this->strokecolor = $spotcolor;
  3415. break;
  3416. }
  3417. case 'fill': {
  3418. $pdfcolor .= sprintf('cs %F scn', $tint);
  3419. $this->FillColor = $pdfcolor;
  3420. $this->bgcolor = $spotcolor;
  3421. break;
  3422. }
  3423. case 'text': {
  3424. $pdfcolor .= sprintf('cs %F scn', $tint);
  3425. $this->TextColor = $pdfcolor;
  3426. $this->fgcolor = $spotcolor;
  3427. break;
  3428. }
  3429. }
  3430. $this->ColorFlag = ($this->FillColor != $this->TextColor);
  3431. if ($this->state == 2) {
  3432. $this->_out($pdfcolor);
  3433. }
  3434. if ($this->inxobj) {
  3435. // we are inside an XObject template
  3436. $this->xobjects[$this->xobjid]['spot_colors'][$name] = $this->spot_colors[$name];
  3437. }
  3438. return $pdfcolor;
  3439. }
  3440. /**
  3441. * Defines the spot color used for all drawing operations (lines, rectangles and cell borders).
  3442. * @param $name (string) Name of the spot color.
  3443. * @param $tint (float) Intensity of the color (from 0 to 100 ; 100 = full intensity by default).
  3444. * @public
  3445. * @since 4.0.024 (2008-09-12)
  3446. * @see AddSpotColor(), SetFillSpotColor(), SetTextSpotColor()
  3447. */
  3448. public function SetDrawSpotColor($name, $tint=100) {
  3449. $this->setSpotColor('draw', $name, $tint);
  3450. }
  3451. /**
  3452. * Defines the spot color used for all filling operations (filled rectangles and cell backgrounds).
  3453. * @param $name (string) Name of the spot color.
  3454. * @param $tint (float) Intensity of the color (from 0 to 100 ; 100 = full intensity by default).
  3455. * @public
  3456. * @since 4.0.024 (2008-09-12)
  3457. * @see AddSpotColor(), SetDrawSpotColor(), SetTextSpotColor()
  3458. */
  3459. public function SetFillSpotColor($name, $tint=100) {
  3460. $this->setSpotColor('fill', $name, $tint);
  3461. }
  3462. /**
  3463. * Defines the spot color used for text.
  3464. * @param $name (string) Name of the spot color.
  3465. * @param $tint (int) Intensity of the color (from 0 to 100 ; 100 = full intensity by default).
  3466. * @public
  3467. * @since 4.0.024 (2008-09-12)
  3468. * @see AddSpotColor(), SetDrawSpotColor(), SetFillSpotColor()
  3469. */
  3470. public function SetTextSpotColor($name, $tint=100) {
  3471. $this->setSpotColor('text', $name, $tint);
  3472. }
  3473. /**
  3474. * Set the color array for the specified type ('draw', 'fill', 'text').
  3475. * It can be expressed in RGB, CMYK or GRAY SCALE components.
  3476. * The method can be called before the first page is created and the value is retained from page to page.
  3477. * @param $type (string) Type of object affected by this color: ('draw', 'fill', 'text').
  3478. * @param $color (array) Array of colors (1=gray, 3=RGB, 4=CMYK or 5=spotcolor=CMYK+name values).
  3479. * @param $ret (boolean) If true do not send the PDF command.
  3480. * @return (string) The PDF command or empty string.
  3481. * @public
  3482. * @since 3.1.000 (2008-06-11)
  3483. */
  3484. public function setColorArray($type, $color, $ret=false) {
  3485. if (is_array($color)) {
  3486. $color = array_values($color);
  3487. // component: grey, RGB red or CMYK cyan
  3488. $c = isset($color[0]) ? $color[0] : -1;
  3489. // component: RGB green or CMYK magenta
  3490. $m = isset($color[1]) ? $color[1] : -1;
  3491. // component: RGB blue or CMYK yellow
  3492. $y = isset($color[2]) ? $color[2] : -1;
  3493. // component: CMYK black
  3494. $k = isset($color[3]) ? $color[3] : -1;
  3495. // color name
  3496. $name = isset($color[4]) ? $color[4] : '';
  3497. if ($c >= 0) {
  3498. return $this->setColor($type, $c, $m, $y, $k, $ret, $name);
  3499. }
  3500. }
  3501. return '';
  3502. }
  3503. /**
  3504. * Defines the color used for all drawing operations (lines, rectangles and cell borders).
  3505. * It can be expressed in RGB, CMYK or GRAY SCALE components.
  3506. * The method can be called before the first page is created and the value is retained from page to page.
  3507. * @param $color (array) Array of colors (1, 3 or 4 values).
  3508. * @param $ret (boolean) If true do not send the PDF command.
  3509. * @return string the PDF command
  3510. * @public
  3511. * @since 3.1.000 (2008-06-11)
  3512. * @see SetDrawColor()
  3513. */
  3514. public function SetDrawColorArray($color, $ret=false) {
  3515. return $this->setColorArray('draw', $color, $ret);
  3516. }
  3517. /**
  3518. * Defines the color used for all filling operations (filled rectangles and cell backgrounds).
  3519. * It can be expressed in RGB, CMYK or GRAY SCALE components.
  3520. * The method can be called before the first page is created and the value is retained from page to page.
  3521. * @param $color (array) Array of colors (1, 3 or 4 values).
  3522. * @param $ret (boolean) If true do not send the PDF command.
  3523. * @public
  3524. * @since 3.1.000 (2008-6-11)
  3525. * @see SetFillColor()
  3526. */
  3527. public function SetFillColorArray($color, $ret=false) {
  3528. return $this->setColorArray('fill', $color, $ret);
  3529. }
  3530. /**
  3531. * Defines the color used for text. It can be expressed in RGB components or gray scale.
  3532. * The method can be called before the first page is created and the value is retained from page to page.
  3533. * @param $color (array) Array of colors (1, 3 or 4 values).
  3534. * @param $ret (boolean) If true do not send the PDF command.
  3535. * @public
  3536. * @since 3.1.000 (2008-6-11)
  3537. * @see SetFillColor()
  3538. */
  3539. public function SetTextColorArray($color, $ret=false) {
  3540. return $this->setColorArray('text', $color, $ret);
  3541. }
  3542. /**
  3543. * Defines the color used by the specified type ('draw', 'fill', 'text').
  3544. * @param $type (string) Type of object affected by this color: ('draw', 'fill', 'text').
  3545. * @param $col1 (float) GRAY level for single color, or Red color for RGB (0-255), or CYAN color for CMYK (0-100).
  3546. * @param $col2 (float) GREEN color for RGB (0-255), or MAGENTA color for CMYK (0-100).
  3547. * @param $col3 (float) BLUE color for RGB (0-255), or YELLOW color for CMYK (0-100).
  3548. * @param $col4 (float) KEY (BLACK) color for CMYK (0-100).
  3549. * @param $ret (boolean) If true do not send the command.
  3550. * @param $name (string) spot color name (if any)
  3551. * @return (string) The PDF command or empty string.
  3552. * @public
  3553. * @since 5.9.125 (2011-10-03)
  3554. */
  3555. public function setColor($type, $col1=0, $col2=-1, $col3=-1, $col4=-1, $ret=false, $name='') {
  3556. // set default values
  3557. if (!is_numeric($col1)) {
  3558. $col1 = 0;
  3559. }
  3560. if (!is_numeric($col2)) {
  3561. $col2 = -1;
  3562. }
  3563. if (!is_numeric($col3)) {
  3564. $col3 = -1;
  3565. }
  3566. if (!is_numeric($col4)) {
  3567. $col4 = -1;
  3568. }
  3569. // set color by case
  3570. $suffix = '';
  3571. if (($col2 == -1) AND ($col3 == -1) AND ($col4 == -1)) {
  3572. // Grey scale
  3573. $col1 = max(0, min(255, $col1));
  3574. $intcolor = array('G' => $col1);
  3575. $pdfcolor = sprintf('%F ', ($col1 / 255));
  3576. $suffix = 'g';
  3577. } elseif ($col4 == -1) {
  3578. // RGB
  3579. $col1 = max(0, min(255, $col1));
  3580. $col2 = max(0, min(255, $col2));
  3581. $col3 = max(0, min(255, $col3));
  3582. $intcolor = array('R' => $col1, 'G' => $col2, 'B' => $col3);
  3583. $pdfcolor = sprintf('%F %F %F ', ($col1 / 255), ($col2 / 255), ($col3 / 255));
  3584. $suffix = 'rg';
  3585. } else {
  3586. $col1 = max(0, min(100, $col1));
  3587. $col2 = max(0, min(100, $col2));
  3588. $col3 = max(0, min(100, $col3));
  3589. $col4 = max(0, min(100, $col4));
  3590. if (empty($name)) {
  3591. // CMYK
  3592. $intcolor = array('C' => $col1, 'M' => $col2, 'Y' => $col3, 'K' => $col4);
  3593. $pdfcolor = sprintf('%F %F %F %F ', ($col1 / 100), ($col2 / 100), ($col3 / 100), ($col4 / 100));
  3594. $suffix = 'k';
  3595. } else {
  3596. // SPOT COLOR
  3597. $intcolor = array('C' => $col1, 'M' => $col2, 'Y' => $col3, 'K' => $col4, 'name' => $name);
  3598. $this->AddSpotColor($name, $col1, $col2, $col3, $col4);
  3599. $pdfcolor = $this->setSpotColor($type, $name, 100);
  3600. }
  3601. }
  3602. switch ($type) {
  3603. case 'draw': {
  3604. $pdfcolor .= strtoupper($suffix);
  3605. $this->DrawColor = $pdfcolor;
  3606. $this->strokecolor = $intcolor;
  3607. break;
  3608. }
  3609. case 'fill': {
  3610. $pdfcolor .= $suffix;
  3611. $this->FillColor = $pdfcolor;
  3612. $this->bgcolor = $intcolor;
  3613. break;
  3614. }
  3615. case 'text': {
  3616. $pdfcolor .= $suffix;
  3617. $this->TextColor = $pdfcolor;
  3618. $this->fgcolor = $intcolor;
  3619. break;
  3620. }
  3621. }
  3622. $this->ColorFlag = ($this->FillColor != $this->TextColor);
  3623. if (($type != 'text') AND ($this->state == 2)) {
  3624. if (!$ret) {
  3625. $this->_out($pdfcolor);
  3626. }
  3627. return $pdfcolor;
  3628. }
  3629. return '';
  3630. }
  3631. /**
  3632. * 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.
  3633. * @param $col1 (float) GRAY level for single color, or Red color for RGB (0-255), or CYAN color for CMYK (0-100).
  3634. * @param $col2 (float) GREEN color for RGB (0-255), or MAGENTA color for CMYK (0-100).
  3635. * @param $col3 (float) BLUE color for RGB (0-255), or YELLOW color for CMYK (0-100).
  3636. * @param $col4 (float) KEY (BLACK) color for CMYK (0-100).
  3637. * @param $ret (boolean) If true do not send the command.
  3638. * @param $name (string) spot color name (if any)
  3639. * @return string the PDF command
  3640. * @public
  3641. * @since 1.3
  3642. * @see SetDrawColorArray(), SetFillColor(), SetTextColor(), Line(), Rect(), Cell(), MultiCell()
  3643. */
  3644. public function SetDrawColor($col1=0, $col2=-1, $col3=-1, $col4=-1, $ret=false, $name='') {
  3645. return $this->setColor('draw', $col1, $col2, $col3, $col4, $ret, $name);
  3646. }
  3647. /**
  3648. * 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.
  3649. * @param $col1 (float) GRAY level for single color, or Red color for RGB (0-255), or CYAN color for CMYK (0-100).
  3650. * @param $col2 (float) GREEN color for RGB (0-255), or MAGENTA color for CMYK (0-100).
  3651. * @param $col3 (float) BLUE color for RGB (0-255), or YELLOW color for CMYK (0-100).
  3652. * @param $col4 (float) KEY (BLACK) color for CMYK (0-100).
  3653. * @param $ret (boolean) If true do not send the command.
  3654. * @param $name (string) Spot color name (if any).
  3655. * @return (string) The PDF command.
  3656. * @public
  3657. * @since 1.3
  3658. * @see SetFillColorArray(), SetDrawColor(), SetTextColor(), Rect(), Cell(), MultiCell()
  3659. */
  3660. public function SetFillColor($col1=0, $col2=-1, $col3=-1, $col4=-1, $ret=false, $name='') {
  3661. return $this->setColor('fill', $col1, $col2, $col3, $col4, $ret, $name);
  3662. }
  3663. /**
  3664. * 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.
  3665. * @param $col1 (float) GRAY level for single color, or Red color for RGB (0-255), or CYAN color for CMYK (0-100).
  3666. * @param $col2 (float) GREEN color for RGB (0-255), or MAGENTA color for CMYK (0-100).
  3667. * @param $col3 (float) BLUE color for RGB (0-255), or YELLOW color for CMYK (0-100).
  3668. * @param $col4 (float) KEY (BLACK) color for CMYK (0-100).
  3669. * @param $ret (boolean) If true do not send the command.
  3670. * @param $name (string) Spot color name (if any).
  3671. * @return (string) Empty string.
  3672. * @public
  3673. * @since 1.3
  3674. * @see SetTextColorArray(), SetDrawColor(), SetFillColor(), Text(), Cell(), MultiCell()
  3675. */
  3676. public function SetTextColor($col1=0, $col2=-1, $col3=-1, $col4=-1, $ret=false, $name='') {
  3677. return $this->setColor('text', $col1, $col2, $col3, $col4, $ret, $name);
  3678. }
  3679. /**
  3680. * Returns the length of a string in user unit. A font must be selected.<br>
  3681. * @param $s (string) The string whose length is to be computed
  3682. * @param $fontname (string) 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.
  3683. * @param $fontstyle (string) 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.
  3684. * @param $fontsize (float) Font size in points. The default value is the current size.
  3685. * @param $getarray (boolean) if true returns an array of characters widths, if false returns the total length.
  3686. * @return mixed int total string length or array of characted widths
  3687. * @author Nicola Asuni
  3688. * @public
  3689. * @since 1.2
  3690. */
  3691. public function GetStringWidth($s, $fontname='', $fontstyle='', $fontsize=0, $getarray=false) {
  3692. 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);
  3693. }
  3694. /**
  3695. * Returns the string length of an array of chars in user unit or an array of characters widths. A font must be selected.<br>
  3696. * @param $sa (string) The array of chars whose total length is to be computed
  3697. * @param $fontname (string) 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.
  3698. * @param $fontstyle (string) 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.
  3699. * @param $fontsize (float) Font size in points. The default value is the current size.
  3700. * @param $getarray (boolean) if true returns an array of characters widths, if false returns the total length.
  3701. * @return mixed int total string length or array of characted widths
  3702. * @author Nicola Asuni
  3703. * @public
  3704. * @since 2.4.000 (2008-03-06)
  3705. */
  3706. public function GetArrStringWidth($sa, $fontname='', $fontstyle='', $fontsize=0, $getarray=false) {
  3707. // store current values
  3708. if (!TCPDF_STATIC::empty_string($fontname)) {
  3709. $prev_FontFamily = $this->FontFamily;
  3710. $prev_FontStyle = $this->FontStyle;
  3711. $prev_FontSizePt = $this->FontSizePt;
  3712. $this->SetFont($fontname, $fontstyle, $fontsize, '', 'default', false);
  3713. }
  3714. // convert UTF-8 array to Latin1 if required
  3715. if ($this->isunicode AND (!$this->isUnicodeFont())) {
  3716. $sa = TCPDF_FONTS::UTF8ArrToLatin1Arr($sa);
  3717. }
  3718. $w = 0; // total width
  3719. $wa = array(); // array of characters widths
  3720. foreach ($sa as $ck => $char) {
  3721. // character width
  3722. $cw = $this->GetCharWidth($char, isset($sa[($ck + 1)]));
  3723. $wa[] = $cw;
  3724. $w += $cw;
  3725. }
  3726. // restore previous values
  3727. if (!TCPDF_STATIC::empty_string($fontname)) {
  3728. $this->SetFont($prev_FontFamily, $prev_FontStyle, $prev_FontSizePt, '', 'default', false);
  3729. }
  3730. if ($getarray) {
  3731. return $wa;
  3732. }
  3733. return $w;
  3734. }
  3735. /**
  3736. * Returns the length of the char in user unit for the current font considering current stretching and spacing (tracking).
  3737. * @param $char (int) The char code whose length is to be returned
  3738. * @param $notlast (boolean) If false ignore the font-spacing.
  3739. * @return float char width
  3740. * @author Nicola Asuni
  3741. * @public
  3742. * @since 2.4.000 (2008-03-06)
  3743. */
  3744. public function GetCharWidth($char, $notlast=true) {
  3745. // get raw width
  3746. $chw = $this->getRawCharWidth($char);
  3747. if (($this->font_spacing < 0) OR (($this->font_spacing > 0) AND $notlast)) {
  3748. // increase/decrease font spacing
  3749. $chw += $this->font_spacing;
  3750. }
  3751. if ($this->font_stretching != 100) {
  3752. // fixed stretching mode
  3753. $chw *= ($this->font_stretching / 100);
  3754. }
  3755. return $chw;
  3756. }
  3757. /**
  3758. * Returns the length of the char in user unit for the current font.
  3759. * @param $char (int) The char code whose length is to be returned
  3760. * @return float char width
  3761. * @author Nicola Asuni
  3762. * @public
  3763. * @since 5.9.000 (2010-09-28)
  3764. */
  3765. public function getRawCharWidth($char) {
  3766. if ($char == 173) {
  3767. // SHY character will not be printed
  3768. return (0);
  3769. }
  3770. if (isset($this->CurrentFont['cw'][$char])) {
  3771. $w = $this->CurrentFont['cw'][$char];
  3772. } elseif (isset($this->CurrentFont['dw'])) {
  3773. // default width
  3774. $w = $this->CurrentFont['dw'];
  3775. } elseif (isset($this->CurrentFont['cw'][32])) {
  3776. // default width
  3777. $w = $this->CurrentFont['cw'][32];
  3778. } else {
  3779. $w = 600;
  3780. }
  3781. return $this->getAbsFontMeasure($w);
  3782. }
  3783. /**
  3784. * Returns the numbero of characters in a string.
  3785. * @param $s (string) The input string.
  3786. * @return int number of characters
  3787. * @public
  3788. * @since 2.0.0001 (2008-01-07)
  3789. */
  3790. public function GetNumChars($s) {
  3791. if ($this->isUnicodeFont()) {
  3792. return count(TCPDF_FONTS::UTF8StringToArray($s, $this->isunicode, $this->CurrentFont));
  3793. }
  3794. return strlen($s);
  3795. }
  3796. /**
  3797. * Fill the list of available fonts ($this->fontlist).
  3798. * @protected
  3799. * @since 4.0.013 (2008-07-28)
  3800. */
  3801. protected function getFontsList() {
  3802. if (($fontsdir = opendir(TCPDF_FONTS::_getfontpath())) !== false) {
  3803. while (($file = readdir($fontsdir)) !== false) {
  3804. if (substr($file, -4) == '.php') {
  3805. array_push($this->fontlist, strtolower(basename($file, '.php')));
  3806. }
  3807. }
  3808. closedir($fontsdir);
  3809. }
  3810. }
  3811. /**
  3812. * Imports a TrueType, Type1, core, or CID0 font and makes it available.
  3813. * It is necessary to generate a font definition file first (read /fonts/utils/README.TXT).
  3814. * 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.
  3815. * @param $family (string) Font family. The name can be chosen arbitrarily. If it is a standard family name, it will override the corresponding font.
  3816. * @param $style (string) 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>
  3817. * @param $fontfile (string) The font definition file. By default, the name is built from the family and style, in lower case with no spaces.
  3818. * @return array containing the font data, or false in case of error.
  3819. * @param $subset (mixed) 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.
  3820. * @public
  3821. * @since 1.5
  3822. * @see SetFont(), setFontSubsetting()
  3823. */
  3824. public function AddFont($family, $style='', $fontfile='', $subset='default') {
  3825. if ($subset === 'default') {
  3826. $subset = $this->font_subsetting;
  3827. }
  3828. if ($this->pdfa_mode) {
  3829. $subset = false;
  3830. }
  3831. if (TCPDF_STATIC::empty_string($family)) {
  3832. if (!TCPDF_STATIC::empty_string($this->FontFamily)) {
  3833. $family = $this->FontFamily;
  3834. } else {
  3835. $this->Error('Empty font family');
  3836. }
  3837. }
  3838. // move embedded styles on $style
  3839. if (substr($family, -1) == 'I') {
  3840. $style .= 'I';
  3841. $family = substr($family, 0, -1);
  3842. }
  3843. if (substr($family, -1) == 'B') {
  3844. $style .= 'B';
  3845. $family = substr($family, 0, -1);
  3846. }
  3847. // normalize family name
  3848. $family = strtolower($family);
  3849. if ((!$this->isunicode) AND ($family == 'arial')) {
  3850. $family = 'helvetica';
  3851. }
  3852. if (($family == 'symbol') OR ($family == 'zapfdingbats')) {
  3853. $style = '';
  3854. }
  3855. if ($this->pdfa_mode AND (isset($this->CoreFonts[$family]))) {
  3856. // all fonts must be embedded
  3857. $family = 'pdfa'.$family;
  3858. }
  3859. $tempstyle = strtoupper($style);
  3860. $style = '';
  3861. // underline
  3862. if (strpos($tempstyle, 'U') !== false) {
  3863. $this->underline = true;
  3864. } else {
  3865. $this->underline = false;
  3866. }
  3867. // line-through (deleted)
  3868. if (strpos($tempstyle, 'D') !== false) {
  3869. $this->linethrough = true;
  3870. } else {
  3871. $this->linethrough = false;
  3872. }
  3873. // overline
  3874. if (strpos($tempstyle, 'O') !== false) {
  3875. $this->overline = true;
  3876. } else {
  3877. $this->overline = false;
  3878. }
  3879. // bold
  3880. if (strpos($tempstyle, 'B') !== false) {
  3881. $style .= 'B';
  3882. }
  3883. // oblique
  3884. if (strpos($tempstyle, 'I') !== false) {
  3885. $style .= 'I';
  3886. }
  3887. $bistyle = $style;
  3888. $fontkey = $family.$style;
  3889. $font_style = $style.($this->underline ? 'U' : '').($this->linethrough ? 'D' : '').($this->overline ? 'O' : '');
  3890. $fontdata = array('fontkey' => $fontkey, 'family' => $family, 'style' => $font_style);
  3891. // check if the font has been already added
  3892. $fb = $this->getFontBuffer($fontkey);
  3893. if ($fb !== false) {
  3894. if ($this->inxobj) {
  3895. // we are inside an XObject template
  3896. $this->xobjects[$this->xobjid]['fonts'][$fontkey] = $fb['i'];
  3897. }
  3898. return $fontdata;
  3899. }
  3900. // get specified font directory (if any)
  3901. $fontdir = false;
  3902. if (!TCPDF_STATIC::empty_string($fontfile)) {
  3903. $fontdir = dirname($fontfile);
  3904. if (TCPDF_STATIC::empty_string($fontdir) OR ($fontdir == '.')) {
  3905. $fontdir = '';
  3906. } else {
  3907. $fontdir .= '/';
  3908. }
  3909. }
  3910. // true when the font style variation is missing
  3911. $missing_style = false;
  3912. // search and include font file
  3913. if (TCPDF_STATIC::empty_string($fontfile) OR (!@file_exists($fontfile))) {
  3914. // build a standard filenames for specified font
  3915. $tmp_fontfile = str_replace(' ', '', $family).strtolower($style).'.php';
  3916. $fontfile = TCPDF_FONTS::getFontFullPath($tmp_fontfile, $fontdir);
  3917. if (TCPDF_STATIC::empty_string($fontfile)) {
  3918. $missing_style = true;
  3919. // try to remove the style part
  3920. $tmp_fontfile = str_replace(' ', '', $family).'.php';
  3921. $fontfile = TCPDF_FONTS::getFontFullPath($tmp_fontfile, $fontdir);
  3922. }
  3923. }
  3924. // include font file
  3925. if (!TCPDF_STATIC::empty_string($fontfile) AND (@file_exists($fontfile))) {
  3926. include($fontfile);
  3927. } else {
  3928. $this->Error('Could not include font definition file: '.$family.'');
  3929. }
  3930. // check font parameters
  3931. if ((!isset($type)) OR (!isset($cw))) {
  3932. $this->Error('The font definition file has a bad format: '.$fontfile.'');
  3933. }
  3934. // SET default parameters
  3935. if (!isset($file) OR TCPDF_STATIC::empty_string($file)) {
  3936. $file = '';
  3937. }
  3938. if (!isset($enc) OR TCPDF_STATIC::empty_string($enc)) {
  3939. $enc = '';
  3940. }
  3941. if (!isset($cidinfo) OR TCPDF_STATIC::empty_string($cidinfo)) {
  3942. $cidinfo = array('Registry'=>'Adobe', 'Ordering'=>'Identity', 'Supplement'=>0);
  3943. $cidinfo['uni2cid'] = array();
  3944. }
  3945. if (!isset($ctg) OR TCPDF_STATIC::empty_string($ctg)) {
  3946. $ctg = '';
  3947. }
  3948. if (!isset($desc) OR TCPDF_STATIC::empty_string($desc)) {
  3949. $desc = array();
  3950. }
  3951. if (!isset($up) OR TCPDF_STATIC::empty_string($up)) {
  3952. $up = -100;
  3953. }
  3954. if (!isset($ut) OR TCPDF_STATIC::empty_string($ut)) {
  3955. $ut = 50;
  3956. }
  3957. if (!isset($cw) OR TCPDF_STATIC::empty_string($cw)) {
  3958. $cw = array();
  3959. }
  3960. if (!isset($dw) OR TCPDF_STATIC::empty_string($dw)) {
  3961. // set default width
  3962. if (isset($desc['MissingWidth']) AND ($desc['MissingWidth'] > 0)) {
  3963. $dw = $desc['MissingWidth'];
  3964. } elseif (isset($cw[32])) {
  3965. $dw = $cw[32];
  3966. } else {
  3967. $dw = 600;
  3968. }
  3969. }
  3970. ++$this->numfonts;
  3971. if ($type == 'core') {
  3972. $name = $this->CoreFonts[$fontkey];
  3973. $subset = false;
  3974. } elseif (($type == 'TrueType') OR ($type == 'Type1')) {
  3975. $subset = false;
  3976. } elseif ($type == 'TrueTypeUnicode') {
  3977. $enc = 'Identity-H';
  3978. } elseif ($type == 'cidfont0') {
  3979. if ($this->pdfa_mode) {
  3980. $this->Error('All fonts must be embedded in PDF/A mode!');
  3981. }
  3982. } else {
  3983. $this->Error('Unknow font type: '.$type.'');
  3984. }
  3985. // set name if unset
  3986. if (!isset($name) OR empty($name)) {
  3987. $name = $fontkey;
  3988. }
  3989. // create artificial font style variations if missing (only works with non-embedded fonts)
  3990. if (($type != 'core') AND $missing_style) {
  3991. // style variations
  3992. $styles = array('' => '', 'B' => ',Bold', 'I' => ',Italic', 'BI' => ',BoldItalic');
  3993. $name .= $styles[$bistyle];
  3994. // artificial bold
  3995. if (strpos($bistyle, 'B') !== false) {
  3996. if (isset($desc['StemV'])) {
  3997. // from normal to bold
  3998. $desc['StemV'] = round($desc['StemV'] * 1.75);
  3999. } else {
  4000. // bold
  4001. $desc['StemV'] = 123;
  4002. }
  4003. }
  4004. // artificial italic
  4005. if (strpos($bistyle, 'I') !== false) {
  4006. if (isset($desc['ItalicAngle'])) {
  4007. $desc['ItalicAngle'] -= 11;
  4008. } else {
  4009. $desc['ItalicAngle'] = -11;
  4010. }
  4011. if (isset($desc['Flags'])) {
  4012. $desc['Flags'] |= 64; //bit 7
  4013. } else {
  4014. $desc['Flags'] = 64;
  4015. }
  4016. }
  4017. }
  4018. // check if the array of characters bounding boxes is defined
  4019. if (!isset($cbbox)) {
  4020. $cbbox = array();
  4021. }
  4022. // initialize subsetchars
  4023. $subsetchars = array_fill(0, 255, true);
  4024. $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));
  4025. if ($this->inxobj) {
  4026. // we are inside an XObject template
  4027. $this->xobjects[$this->xobjid]['fonts'][$fontkey] = $this->numfonts;
  4028. }
  4029. if (isset($diff) AND (!empty($diff))) {
  4030. //Search existing encodings
  4031. $d = 0;
  4032. $nb = count($this->diffs);
  4033. for ($i=1; $i <= $nb; ++$i) {
  4034. if ($this->diffs[$i] == $diff) {
  4035. $d = $i;
  4036. break;
  4037. }
  4038. }
  4039. if ($d == 0) {
  4040. $d = $nb + 1;
  4041. $this->diffs[$d] = $diff;
  4042. }
  4043. $this->setFontSubBuffer($fontkey, 'diff', $d);
  4044. }
  4045. if (!TCPDF_STATIC::empty_string($file)) {
  4046. if (!isset($this->FontFiles[$file])) {
  4047. if ((strcasecmp($type,'TrueType') == 0) OR (strcasecmp($type, 'TrueTypeUnicode') == 0)) {
  4048. $this->FontFiles[$file] = array('length1' => $originalsize, 'fontdir' => $fontdir, 'subset' => $subset, 'fontkeys' => array($fontkey));
  4049. } elseif ($type != 'core') {
  4050. $this->FontFiles[$file] = array('length1' => $size1, 'length2' => $size2, 'fontdir' => $fontdir, 'subset' => $subset, 'fontkeys' => array($fontkey));
  4051. }
  4052. } else {
  4053. // update fontkeys that are sharing this font file
  4054. $this->FontFiles[$file]['subset'] = ($this->FontFiles[$file]['subset'] AND $subset);
  4055. if (!in_array($fontkey, $this->FontFiles[$file]['fontkeys'])) {
  4056. $this->FontFiles[$file]['fontkeys'][] = $fontkey;
  4057. }
  4058. }
  4059. }
  4060. return $fontdata;
  4061. }
  4062. /**
  4063. * Sets the font used to print character strings.
  4064. * The font can be either a standard one or a font added via the AddFont() method. Standard fonts use Windows encoding cp1252 (Western Europe).
  4065. * The method can be called before the first page is created and the font is retained from page to page.
  4066. * If you just wish to change the current font size, it is simpler to call SetFontSize().
  4067. * 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 />
  4068. * @param $family (string) 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.
  4069. * @param $style (string) 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.
  4070. * @param $size (float) 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
  4071. * @param $fontfile (string) The font definition file. By default, the name is built from the family and style, in lower case with no spaces.
  4072. * @param $subset (mixed) 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.
  4073. * @param $out (boolean) if true output the font size command, otherwise only set the font properties.
  4074. * @author Nicola Asuni
  4075. * @public
  4076. * @since 1.0
  4077. * @see AddFont(), SetFontSize()
  4078. */
  4079. public function SetFont($family, $style='', $size=null, $fontfile='', $subset='default', $out=true) {
  4080. //Select a font; size given in points
  4081. if ($size === null) {
  4082. $size = $this->FontSizePt;
  4083. }
  4084. if ($size < 0) {
  4085. $size = 0;
  4086. }
  4087. // try to add font (if not already added)
  4088. $fontdata = $this->AddFont($family, $style, $fontfile, $subset);
  4089. $this->FontFamily = $fontdata['family'];
  4090. $this->FontStyle = $fontdata['style'];
  4091. if (isset($this->CurrentFont['fontkey']) AND isset($this->CurrentFont['subsetchars'])) {
  4092. // save subset chars of the previous font
  4093. $this->setFontSubBuffer($this->CurrentFont['fontkey'], 'subsetchars', $this->CurrentFont['subsetchars']);
  4094. }
  4095. $this->CurrentFont = $this->getFontBuffer($fontdata['fontkey']);
  4096. $this->SetFontSize($size, $out);
  4097. }
  4098. /**
  4099. * Defines the size of the current font.
  4100. * @param $size (float) The font size in points.
  4101. * @param $out (boolean) if true output the font size command, otherwise only set the font properties.
  4102. * @public
  4103. * @since 1.0
  4104. * @see SetFont()
  4105. */
  4106. public function SetFontSize($size, $out=true) {
  4107. // font size in points
  4108. $this->FontSizePt = $size;
  4109. // font size in user units
  4110. $this->FontSize = $size / $this->k;
  4111. // calculate some font metrics
  4112. if (isset($this->CurrentFont['desc']['FontBBox'])) {
  4113. $bbox = explode(' ', substr($this->CurrentFont['desc']['FontBBox'], 1, -1));
  4114. $font_height = ((intval($bbox[3]) - intval($bbox[1])) * $size / 1000);
  4115. } else {
  4116. $font_height = $size * 1.219;
  4117. }
  4118. if (isset($this->CurrentFont['desc']['Ascent']) AND ($this->CurrentFont['desc']['Ascent'] > 0)) {
  4119. $font_ascent = ($this->CurrentFont['desc']['Ascent'] * $size / 1000);
  4120. }
  4121. if (isset($this->CurrentFont['desc']['Descent']) AND ($this->CurrentFont['desc']['Descent'] <= 0)) {
  4122. $font_descent = (- $this->CurrentFont['desc']['Descent'] * $size / 1000);
  4123. }
  4124. if (!isset($font_ascent) AND !isset($font_descent)) {
  4125. // core font
  4126. $font_ascent = 0.76 * $font_height;
  4127. $font_descent = $font_height - $font_ascent;
  4128. } elseif (!isset($font_descent)) {
  4129. $font_descent = $font_height - $font_ascent;
  4130. } elseif (!isset($font_ascent)) {
  4131. $font_ascent = $font_height - $font_descent;
  4132. }
  4133. $this->FontAscent = ($font_ascent / $this->k);
  4134. $this->FontDescent = ($font_descent / $this->k);
  4135. if ($out AND ($this->page > 0) AND (isset($this->CurrentFont['i'])) AND ($this->state == 2)) {
  4136. $this->_out(sprintf('BT /F%d %F Tf ET', $this->CurrentFont['i'], $this->FontSizePt));
  4137. }
  4138. }
  4139. /**
  4140. * Returns the bounding box of the current font in user units.
  4141. * @return array
  4142. * @public
  4143. * @since 5.9.152 (2012-03-23)
  4144. */
  4145. public function getFontBBox() {
  4146. $fbbox = array();
  4147. if (isset($this->CurrentFont['desc']['FontBBox'])) {
  4148. $tmpbbox = explode(' ', substr($this->CurrentFont['desc']['FontBBox'], 1, -1));
  4149. $fbbox = array_map(array($this,'getAbsFontMeasure'), $tmpbbox);
  4150. } else {
  4151. // Find max width
  4152. if (isset($this->CurrentFont['desc']['MaxWidth'])) {
  4153. $maxw = $this->getAbsFontMeasure(intval($this->CurrentFont['desc']['MaxWidth']));
  4154. } else {
  4155. $maxw = 0;
  4156. if (isset($this->CurrentFont['desc']['MissingWidth'])) {
  4157. $maxw = max($maxw, $this->CurrentFont['desc']['MissingWidth']);
  4158. }
  4159. if (isset($this->CurrentFont['desc']['AvgWidth'])) {
  4160. $maxw = max($maxw, $this->CurrentFont['desc']['AvgWidth']);
  4161. }
  4162. if (isset($this->CurrentFont['dw'])) {
  4163. $maxw = max($maxw, $this->CurrentFont['dw']);
  4164. }
  4165. foreach ($this->CurrentFont['cw'] as $char => $w) {
  4166. $maxw = max($maxw, $w);
  4167. }
  4168. if ($maxw == 0) {
  4169. $maxw = 600;
  4170. }
  4171. $maxw = $this->getAbsFontMeasure($maxw);
  4172. }
  4173. $fbbox = array(0, (0 - $this->FontDescent), $maxw, $this->FontAscent);
  4174. }
  4175. return $fbbox;
  4176. }
  4177. /**
  4178. * Convert a relative font measure into absolute value.
  4179. * @param $s (int) Font measure.
  4180. * @return float Absolute measure.
  4181. * @since 5.9.186 (2012-09-13)
  4182. */
  4183. public function getAbsFontMeasure($s) {
  4184. return ($s * $this->FontSize / 1000);
  4185. }
  4186. /**
  4187. * Returns the glyph bounding box of the specified character in the current font in user units.
  4188. * @param $char (int) Input character code.
  4189. * @return mixed array(xMin, yMin, xMax, yMax) or FALSE if not defined.
  4190. * @since 5.9.186 (2012-09-13)
  4191. */
  4192. public function getCharBBox($char) {
  4193. $c = intval($char);
  4194. if (isset($this->CurrentFont['cw'][$c])) {
  4195. // glyph is defined ... use zero width & height for glyphs without outlines
  4196. $result = array(0,0,0,0);
  4197. if (isset($this->CurrentFont['cbbox'][$c])) {
  4198. $result = $this->CurrentFont['cbbox'][$c];
  4199. }
  4200. return array_map(array($this,'getAbsFontMeasure'), $result);
  4201. }
  4202. return false;
  4203. }
  4204. /**
  4205. * Return the font descent value
  4206. * @param $font (string) font name
  4207. * @param $style (string) font style
  4208. * @param $size (float) The size (in points)
  4209. * @return int font descent
  4210. * @public
  4211. * @author Nicola Asuni
  4212. * @since 4.9.003 (2010-03-30)
  4213. */
  4214. public function getFontDescent($font, $style='', $size=0) {
  4215. $fontdata = $this->AddFont($font, $style);
  4216. $fontinfo = $this->getFontBuffer($fontdata['fontkey']);
  4217. if (isset($fontinfo['desc']['Descent']) AND ($fontinfo['desc']['Descent'] <= 0)) {
  4218. $descent = (- $fontinfo['desc']['Descent'] * $size / 1000);
  4219. } else {
  4220. $descent = (1.219 * 0.24 * $size);
  4221. }
  4222. return ($descent / $this->k);
  4223. }
  4224. /**
  4225. * Return the font ascent value.
  4226. * @param $font (string) font name
  4227. * @param $style (string) font style
  4228. * @param $size (float) The size (in points)
  4229. * @return int font ascent
  4230. * @public
  4231. * @author Nicola Asuni
  4232. * @since 4.9.003 (2010-03-30)
  4233. */
  4234. public function getFontAscent($font, $style='', $size=0) {
  4235. $fontdata = $this->AddFont($font, $style);
  4236. $fontinfo = $this->getFontBuffer($fontdata['fontkey']);
  4237. if (isset($fontinfo['desc']['Ascent']) AND ($fontinfo['desc']['Ascent'] > 0)) {
  4238. $ascent = ($fontinfo['desc']['Ascent'] * $size / 1000);
  4239. } else {
  4240. $ascent = 1.219 * 0.76 * $size;
  4241. }
  4242. return ($ascent / $this->k);
  4243. }
  4244. /**
  4245. * Return true in the character is present in the specified font.
  4246. * @param $char (mixed) Character to check (integer value or string)
  4247. * @param $font (string) Font name (family name).
  4248. * @param $style (string) Font style.
  4249. * @return (boolean) true if the char is defined, false otherwise.
  4250. * @public
  4251. * @since 5.9.153 (2012-03-28)
  4252. */
  4253. public function isCharDefined($char, $font='', $style='') {
  4254. if (is_string($char)) {
  4255. // get character code
  4256. $char = TCPDF_FONTS::UTF8StringToArray($char, $this->isunicode, $this->CurrentFont);
  4257. $char = $char[0];
  4258. }
  4259. if (TCPDF_STATIC::empty_string($font)) {
  4260. if (TCPDF_STATIC::empty_string($style)) {
  4261. return (isset($this->CurrentFont['cw'][intval($char)]));
  4262. }
  4263. $font = $this->FontFamily;
  4264. }
  4265. $fontdata = $this->AddFont($font, $style);
  4266. $fontinfo = $this->getFontBuffer($fontdata['fontkey']);
  4267. return (isset($fontinfo['cw'][intval($char)]));
  4268. }
  4269. /**
  4270. * Replace missing font characters on selected font with specified substitutions.
  4271. * @param $text (string) Text to process.
  4272. * @param $font (string) Font name (family name).
  4273. * @param $style (string) Font style.
  4274. * @param $subs (array) 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.
  4275. * @return (string) Processed text.
  4276. * @public
  4277. * @since 5.9.153 (2012-03-28)
  4278. */
  4279. public function replaceMissingChars($text, $font='', $style='', $subs=array()) {
  4280. if (empty($subs)) {
  4281. return $text;
  4282. }
  4283. if (TCPDF_STATIC::empty_string($font)) {
  4284. $font = $this->FontFamily;
  4285. }
  4286. $fontdata = $this->AddFont($font, $style);
  4287. $fontinfo = $this->getFontBuffer($fontdata['fontkey']);
  4288. $uniarr = TCPDF_FONTS::UTF8StringToArray($text, $this->isunicode, $this->CurrentFont);
  4289. foreach ($uniarr as $k => $chr) {
  4290. if (!isset($fontinfo['cw'][$chr])) {
  4291. // this character is missing on the selected font
  4292. if (isset($subs[$chr])) {
  4293. // we have available substitutions
  4294. if (is_array($subs[$chr])) {
  4295. foreach($subs[$chr] as $s) {
  4296. if (isset($fontinfo['cw'][$s])) {
  4297. $uniarr[$k] = $s;
  4298. break;
  4299. }
  4300. }
  4301. } elseif (isset($fontinfo['cw'][$subs[$chr]])) {
  4302. $uniarr[$k] = $subs[$chr];
  4303. }
  4304. }
  4305. }
  4306. }
  4307. return TCPDF_FONTS::UniArrSubString(TCPDF_FONTS::UTF8ArrayToUniArray($uniarr, $this->isunicode));
  4308. }
  4309. /**
  4310. * Defines the default monospaced font.
  4311. * @param $font (string) Font name.
  4312. * @public
  4313. * @since 4.5.025
  4314. */
  4315. public function SetDefaultMonospacedFont($font) {
  4316. $this->default_monospaced_font = $font;
  4317. }
  4318. /**
  4319. * 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 />
  4320. * The identifier can then be passed to Cell(), Write(), Image() or Link(). The destination is defined with SetLink().
  4321. * @public
  4322. * @since 1.5
  4323. * @see Cell(), Write(), Image(), Link(), SetLink()
  4324. */
  4325. public function AddLink() {
  4326. // create a new internal link
  4327. $n = count($this->links) + 1;
  4328. $this->links[$n] = array('p' => 0, 'y' => 0, 'f' => false);
  4329. return $n;
  4330. }
  4331. /**
  4332. * Defines the page and position a link points to.
  4333. * @param $link (int) The link identifier returned by AddLink()
  4334. * @param $y (float) Ordinate of target position; -1 indicates the current position. The default value is 0 (top of page)
  4335. * @param $page (int) 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.
  4336. * @public
  4337. * @since 1.5
  4338. * @see AddLink()
  4339. */
  4340. public function SetLink($link, $y=0, $page=-1) {
  4341. $fixed = false;
  4342. if (!empty($page) AND ($page[0] == '*')) {
  4343. $page = intval(substr($page, 1));
  4344. // this page number will not be changed when moving/add/deleting pages
  4345. $fixed = true;
  4346. }
  4347. if ($page < 0) {
  4348. $page = $this->page;
  4349. }
  4350. if ($y == -1) {
  4351. $y = $this->y;
  4352. }
  4353. $this->links[$link] = array('p' => $page, 'y' => $y, 'f' => $fixed);
  4354. }
  4355. /**
  4356. * Puts a link on a rectangular area of the page.
  4357. * 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.
  4358. * @param $x (float) Abscissa of the upper-left corner of the rectangle
  4359. * @param $y (float) Ordinate of the upper-left corner of the rectangle
  4360. * @param $w (float) Width of the rectangle
  4361. * @param $h (float) Height of the rectangle
  4362. * @param $link (mixed) URL or identifier returned by AddLink()
  4363. * @param $spaces (int) number of spaces on the text to link
  4364. * @public
  4365. * @since 1.5
  4366. * @see AddLink(), Annotation(), Cell(), Write(), Image()
  4367. */
  4368. public function Link($x, $y, $w, $h, $link, $spaces=0) {
  4369. $this->Annotation($x, $y, $w, $h, $link, array('Subtype'=>'Link'), $spaces);
  4370. }
  4371. /**
  4372. * Puts a markup annotation on a rectangular area of the page.
  4373. * !!!!THE ANNOTATION SUPPORT IS NOT YET FULLY IMPLEMENTED !!!!
  4374. * @param $x (float) Abscissa of the upper-left corner of the rectangle
  4375. * @param $y (float) Ordinate of the upper-left corner of the rectangle
  4376. * @param $w (float) Width of the rectangle
  4377. * @param $h (float) Height of the rectangle
  4378. * @param $text (string) annotation text or alternate content
  4379. * @param $opt (array) array of options (see section 8.4 of PDF reference 1.7).
  4380. * @param $spaces (int) number of spaces on the text to link
  4381. * @public
  4382. * @since 4.0.018 (2008-08-06)
  4383. */
  4384. public function Annotation($x, $y, $w, $h, $text, $opt=array('Subtype'=>'Text'), $spaces=0) {
  4385. if ($this->inxobj) {
  4386. // store parameters for later use on template
  4387. $this->xobjects[$this->xobjid]['annotations'][] = array('x' => $x, 'y' => $y, 'w' => $w, 'h' => $h, 'text' => $text, 'opt' => $opt, 'spaces' => $spaces);
  4388. return;
  4389. }
  4390. if ($x === '') {
  4391. $x = $this->x;
  4392. }
  4393. if ($y === '') {
  4394. $y = $this->y;
  4395. }
  4396. // check page for no-write regions and adapt page margins if necessary
  4397. list($x, $y) = $this->checkPageRegions($h, $x, $y);
  4398. // recalculate coordinates to account for graphic transformations
  4399. if (isset($this->transfmatrix) AND !empty($this->transfmatrix)) {
  4400. for ($i=$this->transfmatrix_key; $i > 0; --$i) {
  4401. $maxid = count($this->transfmatrix[$i]) - 1;
  4402. for ($j=$maxid; $j >= 0; --$j) {
  4403. $ctm = $this->transfmatrix[$i][$j];
  4404. if (isset($ctm['a'])) {
  4405. $x = $x * $this->k;
  4406. $y = ($this->h - $y) * $this->k;
  4407. $w = $w * $this->k;
  4408. $h = $h * $this->k;
  4409. // top left
  4410. $xt = $x;
  4411. $yt = $y;
  4412. $x1 = ($ctm['a'] * $xt) + ($ctm['c'] * $yt) + $ctm['e'];
  4413. $y1 = ($ctm['b'] * $xt) + ($ctm['d'] * $yt) + $ctm['f'];
  4414. // top right
  4415. $xt = $x + $w;
  4416. $yt = $y;
  4417. $x2 = ($ctm['a'] * $xt) + ($ctm['c'] * $yt) + $ctm['e'];
  4418. $y2 = ($ctm['b'] * $xt) + ($ctm['d'] * $yt) + $ctm['f'];
  4419. // bottom left
  4420. $xt = $x;
  4421. $yt = $y - $h;
  4422. $x3 = ($ctm['a'] * $xt) + ($ctm['c'] * $yt) + $ctm['e'];
  4423. $y3 = ($ctm['b'] * $xt) + ($ctm['d'] * $yt) + $ctm['f'];
  4424. // bottom right
  4425. $xt = $x + $w;
  4426. $yt = $y - $h;
  4427. $x4 = ($ctm['a'] * $xt) + ($ctm['c'] * $yt) + $ctm['e'];
  4428. $y4 = ($ctm['b'] * $xt) + ($ctm['d'] * $yt) + $ctm['f'];
  4429. // new coordinates (rectangle area)
  4430. $x = min($x1, $x2, $x3, $x4);
  4431. $y = max($y1, $y2, $y3, $y4);
  4432. $w = (max($x1, $x2, $x3, $x4) - $x) / $this->k;
  4433. $h = ($y - min($y1, $y2, $y3, $y4)) / $this->k;
  4434. $x = $x / $this->k;
  4435. $y = $this->h - ($y / $this->k);
  4436. }
  4437. }
  4438. }
  4439. }
  4440. if ($this->page <= 0) {
  4441. $page = 1;
  4442. } else {
  4443. $page = $this->page;
  4444. }
  4445. if (!isset($this->PageAnnots[$page])) {
  4446. $this->PageAnnots[$page] = array();
  4447. }
  4448. $this->PageAnnots[$page][] = array('n' => ++$this->n, 'x' => $x, 'y' => $y, 'w' => $w, 'h' => $h, 'txt' => $text, 'opt' => $opt, 'numspaces' => $spaces);
  4449. if (!$this->pdfa_mode) {
  4450. if ((($opt['Subtype'] == 'FileAttachment') OR ($opt['Subtype'] == 'Sound')) AND (!TCPDF_STATIC::empty_string($opt['FS']))
  4451. AND (@file_exists($opt['FS']) OR TCPDF_STATIC::isValidURL($opt['FS']))
  4452. AND (!isset($this->embeddedfiles[basename($opt['FS'])]))) {
  4453. $this->embeddedfiles[basename($opt['FS'])] = array('f' => ++$this->n, 'n' => ++$this->n, 'file' => $opt['FS']);
  4454. }
  4455. }
  4456. // Add widgets annotation's icons
  4457. if (isset($opt['mk']['i']) AND @file_exists($opt['mk']['i'])) {
  4458. $this->Image($opt['mk']['i'], '', '', 10, 10, '', '', '', false, 300, '', false, false, 0, false, true);
  4459. }
  4460. if (isset($opt['mk']['ri']) AND @file_exists($opt['mk']['ri'])) {
  4461. $this->Image($opt['mk']['ri'], '', '', 0, 0, '', '', '', false, 300, '', false, false, 0, false, true);
  4462. }
  4463. if (isset($opt['mk']['ix']) AND @file_exists($opt['mk']['ix'])) {
  4464. $this->Image($opt['mk']['ix'], '', '', 0, 0, '', '', '', false, 300, '', false, false, 0, false, true);
  4465. }
  4466. }
  4467. /**
  4468. * Embedd the attached files.
  4469. * @since 4.4.000 (2008-12-07)
  4470. * @protected
  4471. * @see Annotation()
  4472. */
  4473. protected function _putEmbeddedFiles() {
  4474. if ($this->pdfa_mode) {
  4475. // embedded files are not allowed in PDF/A mode
  4476. return;
  4477. }
  4478. reset($this->embeddedfiles);
  4479. foreach ($this->embeddedfiles as $filename => $filedata) {
  4480. $data = TCPDF_STATIC::fileGetContents($filedata['file']);
  4481. if ($data !== FALSE) {
  4482. $rawsize = strlen($data);
  4483. if ($rawsize > 0) {
  4484. // update name tree
  4485. $this->efnames[$filename] = $filedata['f'].' 0 R';
  4486. // embedded file specification object
  4487. $out = $this->_getobj($filedata['f'])."\n";
  4488. $out .= '<</Type /Filespec /F '.$this->_datastring($filename, $filedata['f']).' /EF <</F '.$filedata['n'].' 0 R>> >>';
  4489. $out .= "\n".'endobj';
  4490. $this->_out($out);
  4491. // embedded file object
  4492. $filter = '';
  4493. if ($this->compress) {
  4494. $data = gzcompress($data);
  4495. $filter = ' /Filter /FlateDecode';
  4496. }
  4497. $stream = $this->_getrawstream($data, $filedata['n']);
  4498. $out = $this->_getobj($filedata['n'])."\n";
  4499. $out .= '<< /Type /EmbeddedFile'.$filter.' /Length '.strlen($stream).' /Params <</Size '.$rawsize.'>> >>';
  4500. $out .= ' stream'."\n".$stream."\n".'endstream';
  4501. $out .= "\n".'endobj';
  4502. $this->_out($out);
  4503. }
  4504. }
  4505. }
  4506. }
  4507. /**
  4508. * Prints a text cell at the specified position.
  4509. * This method allows to place a string precisely on the page.
  4510. * @param $x (float) Abscissa of the cell origin
  4511. * @param $y (float) Ordinate of the cell origin
  4512. * @param $txt (string) String to print
  4513. * @param $fstroke (int) outline size in user units (false = disable)
  4514. * @param $fclip (boolean) if true activate clipping mode (you must call StartTransform() before this function and StopTransform() to stop the clipping tranformation).
  4515. * @param $ffill (boolean) if true fills the text
  4516. * @param $border (mixed) 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)))
  4517. * @param $ln (int) 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.
  4518. * @param $align (string) 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>
  4519. * @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
  4520. * @param $link (mixed) URL or identifier returned by AddLink().
  4521. * @param $stretch (int) 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.
  4522. * @param $ignore_min_height (boolean) if true ignore automatic minimum height value.
  4523. * @param $calign (string) 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>
  4524. * @param $valign (string) text vertical alignment inside the cell. Possible values are:<ul><li>T : top</li><li>C : center</li><li>B : bottom</li></ul>
  4525. * @param $rtloff (boolean) if true uses the page top-left corner as origin of axis for $x and $y initial position.
  4526. * @public
  4527. * @since 1.0
  4528. * @see Cell(), Write(), MultiCell(), WriteHTML(), WriteHTMLCell()
  4529. */
  4530. public function Text($x, $y, $txt, $fstroke=false, $fclip=false, $ffill=true, $border=0, $ln=0, $align='', $fill=false, $link='', $stretch=0, $ignore_min_height=false, $calign='T', $valign='M', $rtloff=false) {
  4531. $textrendermode = $this->textrendermode;
  4532. $textstrokewidth = $this->textstrokewidth;
  4533. $this->setTextRenderingMode($fstroke, $ffill, $fclip);
  4534. $this->SetXY($x, $y, $rtloff);
  4535. $this->Cell(0, 0, $txt, $border, $ln, $align, $fill, $link, $stretch, $ignore_min_height, $calign, $valign);
  4536. // restore previous rendering mode
  4537. $this->textrendermode = $textrendermode;
  4538. $this->textstrokewidth = $textstrokewidth;
  4539. }
  4540. /**
  4541. * Whenever a page break condition is met, the method is called, and the break is issued or not depending on the returned value.
  4542. * The default implementation returns a value according to the mode selected by SetAutoPageBreak().<br />
  4543. * This method is called automatically and should not be called directly by the application.
  4544. * @return boolean
  4545. * @public
  4546. * @since 1.4
  4547. * @see SetAutoPageBreak()
  4548. */
  4549. public function AcceptPageBreak() {
  4550. if ($this->num_columns > 1) {
  4551. // multi column mode
  4552. if ($this->current_column < ($this->num_columns - 1)) {
  4553. // go to next column
  4554. $this->selectColumn($this->current_column + 1);
  4555. } elseif ($this->AutoPageBreak) {
  4556. // add a new page
  4557. $this->AddPage();
  4558. // set first column
  4559. $this->selectColumn(0);
  4560. }
  4561. // avoid page breaking from checkPageBreak()
  4562. return false;
  4563. }
  4564. return $this->AutoPageBreak;
  4565. }
  4566. /**
  4567. * Add page if needed.
  4568. * @param $h (float) Cell height. Default value: 0.
  4569. * @param $y (mixed) starting y position, leave empty for current position.
  4570. * @param $addpage (boolean) if true add a page, otherwise only return the true/false state
  4571. * @return boolean true in case of page break, false otherwise.
  4572. * @since 3.2.000 (2008-07-01)
  4573. * @protected
  4574. */
  4575. protected function checkPageBreak($h=0, $y='', $addpage=true) {
  4576. if (TCPDF_STATIC::empty_string($y)) {
  4577. $y = $this->y;
  4578. }
  4579. $current_page = $this->page;
  4580. if ((($y + $h) > $this->PageBreakTrigger) AND ($this->inPageBody()) AND ($this->AcceptPageBreak())) {
  4581. if ($addpage) {
  4582. //Automatic page break
  4583. $x = $this->x;
  4584. $this->AddPage($this->CurOrientation);
  4585. $this->y = $this->tMargin;
  4586. $oldpage = $this->page - 1;
  4587. if ($this->rtl) {
  4588. if ($this->pagedim[$this->page]['orm'] != $this->pagedim[$oldpage]['orm']) {
  4589. $this->x = $x - ($this->pagedim[$this->page]['orm'] - $this->pagedim[$oldpage]['orm']);
  4590. } else {
  4591. $this->x = $x;
  4592. }
  4593. } else {
  4594. if ($this->pagedim[$this->page]['olm'] != $this->pagedim[$oldpage]['olm']) {
  4595. $this->x = $x + ($this->pagedim[$this->page]['olm'] - $this->pagedim[$oldpage]['olm']);
  4596. } else {
  4597. $this->x = $x;
  4598. }
  4599. }
  4600. }
  4601. return true;
  4602. }
  4603. if ($current_page != $this->page) {
  4604. // account for columns mode
  4605. return true;
  4606. }
  4607. return false;
  4608. }
  4609. /**
  4610. * 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 />
  4611. * If automatic page breaking is enabled and the cell goes beyond the limit, a page break is done before outputting.
  4612. * @param $w (float) Cell width. If 0, the cell extends up to the right margin.
  4613. * @param $h (float) Cell height. Default value: 0.
  4614. * @param $txt (string) String to print. Default value: empty string.
  4615. * @param $border (mixed) 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)))
  4616. * @param $ln (int) 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.
  4617. * @param $align (string) 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>
  4618. * @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
  4619. * @param $link (mixed) URL or identifier returned by AddLink().
  4620. * @param $stretch (int) 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.
  4621. * @param $ignore_min_height (boolean) if true ignore automatic minimum height value.
  4622. * @param $calign (string) 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>
  4623. * @param $valign (string) text vertical alignment inside the cell. Possible values are:<ul><li>T : top</li><li>C : center</li><li>B : bottom</li></ul>
  4624. * @public
  4625. * @since 1.0
  4626. * @see SetFont(), SetDrawColor(), SetFillColor(), SetTextColor(), SetLineWidth(), AddLink(), Ln(), MultiCell(), Write(), SetAutoPageBreak()
  4627. */
  4628. 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') {
  4629. $prev_cell_margin = $this->cell_margin;
  4630. $prev_cell_padding = $this->cell_padding;
  4631. $this->adjustCellPadding($border);
  4632. if (!$ignore_min_height) {
  4633. $min_cell_height = $this->getCellHeight($this->FontSize);
  4634. if ($h < $min_cell_height) {
  4635. $h = $min_cell_height;
  4636. }
  4637. }
  4638. $this->checkPageBreak($h + $this->cell_margin['T'] + $this->cell_margin['B']);
  4639. // apply text shadow if enabled
  4640. if ($this->txtshadow['enabled']) {
  4641. // save data
  4642. $x = $this->x;
  4643. $y = $this->y;
  4644. $bc = $this->bgcolor;
  4645. $fc = $this->fgcolor;
  4646. $sc = $this->strokecolor;
  4647. $alpha = $this->alpha;
  4648. // print shadow
  4649. $this->x += $this->txtshadow['depth_w'];
  4650. $this->y += $this->txtshadow['depth_h'];
  4651. $this->SetFillColorArray($this->txtshadow['color']);
  4652. $this->SetTextColorArray($this->txtshadow['color']);
  4653. $this->SetDrawColorArray($this->txtshadow['color']);
  4654. if ($this->txtshadow['opacity'] != $alpha['CA']) {
  4655. $this->setAlpha($this->txtshadow['opacity'], $this->txtshadow['blend_mode']);
  4656. }
  4657. if ($this->state == 2) {
  4658. $this->_out($this->getCellCode($w, $h, $txt, $border, $ln, $align, $fill, $link, $stretch, true, $calign, $valign));
  4659. }
  4660. //restore data
  4661. $this->x = $x;
  4662. $this->y = $y;
  4663. $this->SetFillColorArray($bc);
  4664. $this->SetTextColorArray($fc);
  4665. $this->SetDrawColorArray($sc);
  4666. if ($this->txtshadow['opacity'] != $alpha['CA']) {
  4667. $this->setAlpha($alpha['CA'], $alpha['BM'], $alpha['ca'], $alpha['AIS']);
  4668. }
  4669. }
  4670. if ($this->state == 2) {
  4671. $this->_out($this->getCellCode($w, $h, $txt, $border, $ln, $align, $fill, $link, $stretch, true, $calign, $valign));
  4672. }
  4673. $this->cell_padding = $prev_cell_padding;
  4674. $this->cell_margin = $prev_cell_margin;
  4675. }
  4676. /**
  4677. * 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 />
  4678. * If automatic page breaking is enabled and the cell goes beyond the limit, a page break is done before outputting.
  4679. * @param $w (float) Cell width. If 0, the cell extends up to the right margin.
  4680. * @param $h (float) Cell height. Default value: 0.
  4681. * @param $txt (string) String to print. Default value: empty string.
  4682. * @param $border (mixed) 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)))
  4683. * @param $ln (int) 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.
  4684. * @param $align (string) 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>
  4685. * @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
  4686. * @param $link (mixed) URL or identifier returned by AddLink().
  4687. * @param $stretch (int) 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.
  4688. * @param $ignore_min_height (boolean) if true ignore automatic minimum height value.
  4689. * @param $calign (string) 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>
  4690. * @param $valign (string) text vertical alignment inside the cell. Possible values are:<ul><li>T : top</li><li>M : middle</li><li>B : bottom</li></ul>
  4691. * @return string containing cell code
  4692. * @protected
  4693. * @since 1.0
  4694. * @see Cell()
  4695. */
  4696. 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') {
  4697. // replace 'NO-BREAK SPACE' (U+00A0) character with a simple space
  4698. $txt = str_replace(TCPDF_FONTS::unichr(160, $this->isunicode), ' ', $txt);
  4699. $prev_cell_margin = $this->cell_margin;
  4700. $prev_cell_padding = $this->cell_padding;
  4701. $txt = TCPDF_STATIC::removeSHY($txt, $this->isunicode);
  4702. $rs = ''; //string to be returned
  4703. $this->adjustCellPadding($border);
  4704. if (!$ignore_min_height) {
  4705. $min_cell_height = $this->getCellHeight($this->FontSize);
  4706. if ($h < $min_cell_height) {
  4707. $h = $min_cell_height;
  4708. }
  4709. }
  4710. $k = $this->k;
  4711. // check page for no-write regions and adapt page margins if necessary
  4712. list($this->x, $this->y) = $this->checkPageRegions($h, $this->x, $this->y);
  4713. if ($this->rtl) {
  4714. $x = $this->x - $this->cell_margin['R'];
  4715. } else {
  4716. $x = $this->x + $this->cell_margin['L'];
  4717. }
  4718. $y = $this->y + $this->cell_margin['T'];
  4719. $prev_font_stretching = $this->font_stretching;
  4720. $prev_font_spacing = $this->font_spacing;
  4721. // cell vertical alignment
  4722. switch ($calign) {
  4723. case 'A': {
  4724. // font top
  4725. switch ($valign) {
  4726. case 'T': {
  4727. // top
  4728. $y -= $this->cell_padding['T'];
  4729. break;
  4730. }
  4731. case 'B': {
  4732. // bottom
  4733. $y -= ($h - $this->cell_padding['B'] - $this->FontAscent - $this->FontDescent);
  4734. break;
  4735. }
  4736. default:
  4737. case 'C':
  4738. case 'M': {
  4739. // center
  4740. $y -= (($h - $this->FontAscent - $this->FontDescent) / 2);
  4741. break;
  4742. }
  4743. }
  4744. break;
  4745. }
  4746. case 'L': {
  4747. // font baseline
  4748. switch ($valign) {
  4749. case 'T': {
  4750. // top
  4751. $y -= ($this->cell_padding['T'] + $this->FontAscent);
  4752. break;
  4753. }
  4754. case 'B': {
  4755. // bottom
  4756. $y -= ($h - $this->cell_padding['B'] - $this->FontDescent);
  4757. break;
  4758. }
  4759. default:
  4760. case 'C':
  4761. case 'M': {
  4762. // center
  4763. $y -= (($h + $this->FontAscent - $this->FontDescent) / 2);
  4764. break;
  4765. }
  4766. }
  4767. break;
  4768. }
  4769. case 'D': {
  4770. // font bottom
  4771. switch ($valign) {
  4772. case 'T': {
  4773. // top
  4774. $y -= ($this->cell_padding['T'] + $this->FontAscent + $this->FontDescent);
  4775. break;
  4776. }
  4777. case 'B': {
  4778. // bottom
  4779. $y -= ($h - $this->cell_padding['B']);
  4780. break;
  4781. }
  4782. default:
  4783. case 'C':
  4784. case 'M': {
  4785. // center
  4786. $y -= (($h + $this->FontAscent + $this->FontDescent) / 2);
  4787. break;
  4788. }
  4789. }
  4790. break;
  4791. }
  4792. case 'B': {
  4793. // cell bottom
  4794. $y -= $h;
  4795. break;
  4796. }
  4797. case 'C':
  4798. case 'M': {
  4799. // cell center
  4800. $y -= ($h / 2);
  4801. break;
  4802. }
  4803. default:
  4804. case 'T': {
  4805. // cell top
  4806. break;
  4807. }
  4808. }
  4809. // text vertical alignment
  4810. switch ($valign) {
  4811. case 'T': {
  4812. // top
  4813. $yt = $y + $this->cell_padding['T'];
  4814. break;
  4815. }
  4816. case 'B': {
  4817. // bottom
  4818. $yt = $y + $h - $this->cell_padding['B'] - $this->FontAscent - $this->FontDescent;
  4819. break;
  4820. }
  4821. default:
  4822. case 'C':
  4823. case 'M': {
  4824. // center
  4825. $yt = $y + (($h - $this->FontAscent - $this->FontDescent) / 2);
  4826. break;
  4827. }
  4828. }
  4829. $basefonty = $yt + $this->FontAscent;
  4830. if (TCPDF_STATIC::empty_string($w) OR ($w <= 0)) {
  4831. if ($this->rtl) {
  4832. $w = $x - $this->lMargin;
  4833. } else {
  4834. $w = $this->w - $this->rMargin - $x;
  4835. }
  4836. }
  4837. $s = '';
  4838. // fill and borders
  4839. if (is_string($border) AND (strlen($border) == 4)) {
  4840. // full border
  4841. $border = 1;
  4842. }
  4843. if ($fill OR ($border == 1)) {
  4844. if ($fill) {
  4845. $op = ($border == 1) ? 'B' : 'f';
  4846. } else {
  4847. $op = 'S';
  4848. }
  4849. if ($this->rtl) {
  4850. $xk = (($x - $w) * $k);
  4851. } else {
  4852. $xk = ($x * $k);
  4853. }
  4854. $s .= sprintf('%F %F %F %F re %s ', $xk, (($this->h - $y) * $k), ($w * $k), (-$h * $k), $op);
  4855. }
  4856. // draw borders
  4857. $s .= $this->getCellBorder($x, $y, $w, $h, $border);
  4858. if ($txt != '') {
  4859. $txt2 = $txt;
  4860. if ($this->isunicode) {
  4861. if (($this->CurrentFont['type'] == 'core') OR ($this->CurrentFont['type'] == 'TrueType') OR ($this->CurrentFont['type'] == 'Type1')) {
  4862. $txt2 = TCPDF_FONTS::UTF8ToLatin1($txt2, $this->isunicode, $this->CurrentFont);
  4863. } else {
  4864. $unicode = TCPDF_FONTS::UTF8StringToArray($txt, $this->isunicode, $this->CurrentFont); // array of UTF-8 unicode values
  4865. $unicode = TCPDF_FONTS::utf8Bidi($unicode, '', $this->tmprtl, $this->isunicode, $this->CurrentFont);
  4866. // replace thai chars (if any)
  4867. if (defined('K_THAI_TOPCHARS') AND (K_THAI_TOPCHARS == true)) {
  4868. // number of chars
  4869. $numchars = count($unicode);
  4870. // po pla, for far, for fan
  4871. $longtail = array(0x0e1b, 0x0e1d, 0x0e1f);
  4872. // do chada, to patak
  4873. $lowtail = array(0x0e0e, 0x0e0f);
  4874. // mai hun arkad, sara i, sara ii, sara ue, sara uee
  4875. $upvowel = array(0x0e31, 0x0e34, 0x0e35, 0x0e36, 0x0e37);
  4876. // mai ek, mai tho, mai tri, mai chattawa, karan
  4877. $tonemark = array(0x0e48, 0x0e49, 0x0e4a, 0x0e4b, 0x0e4c);
  4878. // sara u, sara uu, pinthu
  4879. $lowvowel = array(0x0e38, 0x0e39, 0x0e3a);
  4880. $output = array();
  4881. for ($i = 0; $i < $numchars; $i++) {
  4882. if (($unicode[$i] >= 0x0e00) && ($unicode[$i] <= 0x0e5b)) {
  4883. $ch0 = $unicode[$i];
  4884. $ch1 = ($i > 0) ? $unicode[($i - 1)] : 0;
  4885. $ch2 = ($i > 1) ? $unicode[($i - 2)] : 0;
  4886. $chn = ($i < ($numchars - 1)) ? $unicode[($i + 1)] : 0;
  4887. if (in_array($ch0, $tonemark)) {
  4888. if ($chn == 0x0e33) {
  4889. // sara um
  4890. if (in_array($ch1, $longtail)) {
  4891. // tonemark at upper left
  4892. $output[] = $this->replaceChar($ch0, (0xf713 + $ch0 - 0x0e48));
  4893. } else {
  4894. // tonemark at upper right (normal position)
  4895. $output[] = $ch0;
  4896. }
  4897. } elseif (in_array($ch1, $longtail) OR (in_array($ch2, $longtail) AND in_array($ch1, $lowvowel))) {
  4898. // tonemark at lower left
  4899. $output[] = $this->replaceChar($ch0, (0xf705 + $ch0 - 0x0e48));
  4900. } elseif (in_array($ch1, $upvowel)) {
  4901. if (in_array($ch2, $longtail)) {
  4902. // tonemark at upper left
  4903. $output[] = $this->replaceChar($ch0, (0xf713 + $ch0 - 0x0e48));
  4904. } else {
  4905. // tonemark at upper right (normal position)
  4906. $output[] = $ch0;
  4907. }
  4908. } else {
  4909. // tonemark at lower right
  4910. $output[] = $this->replaceChar($ch0, (0xf70a + $ch0 - 0x0e48));
  4911. }
  4912. } elseif (($ch0 == 0x0e33) AND (in_array($ch1, $longtail) OR (in_array($ch2, $longtail) AND in_array($ch1, $tonemark)))) {
  4913. // add lower left nikhahit and sara aa
  4914. if ($this->isCharDefined(0xf711) AND $this->isCharDefined(0x0e32)) {
  4915. $output[] = 0xf711;
  4916. $this->CurrentFont['subsetchars'][0xf711] = true;
  4917. $output[] = 0x0e32;
  4918. $this->CurrentFont['subsetchars'][0x0e32] = true;
  4919. } else {
  4920. $output[] = $ch0;
  4921. }
  4922. } elseif (in_array($ch1, $longtail)) {
  4923. if ($ch0 == 0x0e31) {
  4924. // lower left mai hun arkad
  4925. $output[] = $this->replaceChar($ch0, 0xf710);
  4926. } elseif (in_array($ch0, $upvowel)) {
  4927. // lower left
  4928. $output[] = $this->replaceChar($ch0, (0xf701 + $ch0 - 0x0e34));
  4929. } elseif ($ch0 == 0x0e47) {
  4930. // lower left mai tai koo
  4931. $output[] = $this->replaceChar($ch0, 0xf712);
  4932. } else {
  4933. // normal character
  4934. $output[] = $ch0;
  4935. }
  4936. } elseif (in_array($ch1, $lowtail) AND in_array($ch0, $lowvowel)) {
  4937. // lower vowel
  4938. $output[] = $this->replaceChar($ch0, (0xf718 + $ch0 - 0x0e38));
  4939. } elseif (($ch0 == 0x0e0d) AND in_array($chn, $lowvowel)) {
  4940. // yo ying without lower part
  4941. $output[] = $this->replaceChar($ch0, 0xf70f);
  4942. } elseif (($ch0 == 0x0e10) AND in_array($chn, $lowvowel)) {
  4943. // tho santan without lower part
  4944. $output[] = $this->replaceChar($ch0, 0xf700);
  4945. } else {
  4946. $output[] = $ch0;
  4947. }
  4948. } else {
  4949. // non-thai character
  4950. $output[] = $unicode[$i];
  4951. }
  4952. }
  4953. $unicode = $output;
  4954. // update font subsetchars
  4955. $this->setFontSubBuffer($this->CurrentFont['fontkey'], 'subsetchars', $this->CurrentFont['subsetchars']);
  4956. } // end of K_THAI_TOPCHARS
  4957. $txt2 = TCPDF_FONTS::arrUTF8ToUTF16BE($unicode, false);
  4958. }
  4959. }
  4960. $txt2 = TCPDF_STATIC::_escape($txt2);
  4961. // get current text width (considering general font stretching and spacing)
  4962. $txwidth = $this->GetStringWidth($txt);
  4963. $width = $txwidth;
  4964. // check for stretch mode
  4965. if ($stretch > 0) {
  4966. // calculate ratio between cell width and text width
  4967. if ($width <= 0) {
  4968. $ratio = 1;
  4969. } else {
  4970. $ratio = (($w - $this->cell_padding['L'] - $this->cell_padding['R']) / $width);
  4971. }
  4972. // check if stretching is required
  4973. if (($ratio < 1) OR (($ratio > 1) AND (($stretch % 2) == 0))) {
  4974. // the text will be stretched to fit cell width
  4975. if ($stretch > 2) {
  4976. // set new character spacing
  4977. $this->font_spacing += ($w - $this->cell_padding['L'] - $this->cell_padding['R'] - $width) / (max(($this->GetNumChars($txt) - 1), 1) * ($this->font_stretching / 100));
  4978. } else {
  4979. // set new horizontal stretching
  4980. $this->font_stretching *= $ratio;
  4981. }
  4982. // recalculate text width (the text fills the entire cell)
  4983. $width = $w - $this->cell_padding['L'] - $this->cell_padding['R'];
  4984. // reset alignment
  4985. $align = '';
  4986. }
  4987. }
  4988. if ($this->font_stretching != 100) {
  4989. // apply font stretching
  4990. $rs .= sprintf('BT %F Tz ET ', $this->font_stretching);
  4991. }
  4992. if ($this->font_spacing != 0) {
  4993. // increase/decrease font spacing
  4994. $rs .= sprintf('BT %F Tc ET ', ($this->font_spacing * $this->k));
  4995. }
  4996. if ($this->ColorFlag AND ($this->textrendermode < 4)) {
  4997. $s .= 'q '.$this->TextColor.' ';
  4998. }
  4999. // rendering mode
  5000. $s .= sprintf('BT %d Tr %F w ET ', $this->textrendermode, ($this->textstrokewidth * $this->k));
  5001. // count number of spaces
  5002. $ns = substr_count($txt, chr(32));
  5003. // Justification
  5004. $spacewidth = 0;
  5005. if (($align == 'J') AND ($ns > 0)) {
  5006. if ($this->isUnicodeFont()) {
  5007. // get string width without spaces
  5008. $width = $this->GetStringWidth(str_replace(' ', '', $txt));
  5009. // calculate average space width
  5010. $spacewidth = -1000 * ($w - $width - $this->cell_padding['L'] - $this->cell_padding['R']) / ($ns?$ns:1) / ($this->FontSize?$this->FontSize:1);
  5011. if ($this->font_stretching != 100) {
  5012. // word spacing is affected by stretching
  5013. $spacewidth /= ($this->font_stretching / 100);
  5014. }
  5015. // set word position to be used with TJ operator
  5016. $txt2 = str_replace(chr(0).chr(32), ') '.sprintf('%F', $spacewidth).' (', $txt2);
  5017. $unicode_justification = true;
  5018. } else {
  5019. // get string width
  5020. $width = $txwidth;
  5021. // new space width
  5022. $spacewidth = (($w - $width - $this->cell_padding['L'] - $this->cell_padding['R']) / ($ns?$ns:1)) * $this->k;
  5023. if ($this->font_stretching != 100) {
  5024. // word spacing (Tw) is affected by stretching
  5025. $spacewidth /= ($this->font_stretching / 100);
  5026. }
  5027. // set word spacing
  5028. $rs .= sprintf('BT %F Tw ET ', $spacewidth);
  5029. }
  5030. $width = $w - $this->cell_padding['L'] - $this->cell_padding['R'];
  5031. }
  5032. // replace carriage return characters
  5033. $txt2 = str_replace("\r", ' ', $txt2);
  5034. switch ($align) {
  5035. case 'C': {
  5036. $dx = ($w - $width) / 2;
  5037. break;
  5038. }
  5039. case 'R': {
  5040. if ($this->rtl) {
  5041. $dx = $this->cell_padding['R'];
  5042. } else {
  5043. $dx = $w - $width - $this->cell_padding['R'];
  5044. }
  5045. break;
  5046. }
  5047. case 'L': {
  5048. if ($this->rtl) {
  5049. $dx = $w - $width - $this->cell_padding['L'];
  5050. } else {
  5051. $dx = $this->cell_padding['L'];
  5052. }
  5053. break;
  5054. }
  5055. case 'J':
  5056. default: {
  5057. if ($this->rtl) {
  5058. $dx = $this->cell_padding['R'];
  5059. } else {
  5060. $dx = $this->cell_padding['L'];
  5061. }
  5062. break;
  5063. }
  5064. }
  5065. if ($this->rtl) {
  5066. $xdx = $x - $dx - $width;
  5067. } else {
  5068. $xdx = $x + $dx;
  5069. }
  5070. $xdk = $xdx * $k;
  5071. // print text
  5072. $s .= sprintf('BT %F %F Td [(%s)] TJ ET', $xdk, (($this->h - $basefonty) * $k), $txt2);
  5073. if (isset($uniblock)) {
  5074. // print overlapping characters as separate string
  5075. $xshift = 0; // horizontal shift
  5076. $ty = (($this->h - $basefonty + (0.2 * $this->FontSize)) * $k);
  5077. $spw = (($w - $txwidth - $this->cell_padding['L'] - $this->cell_padding['R']) / ($ns?$ns:1));
  5078. foreach ($uniblock as $uk => $uniarr) {
  5079. if (($uk % 2) == 0) {
  5080. // x space to skip
  5081. if ($spacewidth != 0) {
  5082. // justification shift
  5083. $xshift += (count(array_keys($uniarr, 32)) * $spw);
  5084. }
  5085. $xshift += $this->GetArrStringWidth($uniarr); // + shift justification
  5086. } else {
  5087. // character to print
  5088. $topchr = TCPDF_FONTS::arrUTF8ToUTF16BE($uniarr, false);
  5089. $topchr = TCPDF_STATIC::_escape($topchr);
  5090. $s .= sprintf(' BT %F %F Td [(%s)] TJ ET', ($xdk + ($xshift * $k)), $ty, $topchr);
  5091. }
  5092. }
  5093. }
  5094. if ($this->underline) {
  5095. $s .= ' '.$this->_dounderlinew($xdx, $basefonty, $width);
  5096. }
  5097. if ($this->linethrough) {
  5098. $s .= ' '.$this->_dolinethroughw($xdx, $basefonty, $width);
  5099. }
  5100. if ($this->overline) {
  5101. $s .= ' '.$this->_dooverlinew($xdx, $basefonty, $width);
  5102. }
  5103. if ($this->ColorFlag AND ($this->textrendermode < 4)) {
  5104. $s .= ' Q';
  5105. }
  5106. if ($link) {
  5107. $this->Link($xdx, $yt, $width, ($this->FontAscent + $this->FontDescent), $link, $ns);
  5108. }
  5109. }
  5110. // output cell
  5111. if ($s) {
  5112. // output cell
  5113. $rs .= $s;
  5114. if ($this->font_spacing != 0) {
  5115. // reset font spacing mode
  5116. $rs .= ' BT 0 Tc ET';
  5117. }
  5118. if ($this->font_stretching != 100) {
  5119. // reset font stretching mode
  5120. $rs .= ' BT 100 Tz ET';
  5121. }
  5122. }
  5123. // reset word spacing
  5124. if (!$this->isUnicodeFont() AND ($align == 'J')) {
  5125. $rs .= ' BT 0 Tw ET';
  5126. }
  5127. // reset stretching and spacing
  5128. $this->font_stretching = $prev_font_stretching;
  5129. $this->font_spacing = $prev_font_spacing;
  5130. $this->lasth = $h;
  5131. if ($ln > 0) {
  5132. //Go to the beginning of the next line
  5133. $this->y = $y + $h + $this->cell_margin['B'];
  5134. if ($ln == 1) {
  5135. if ($this->rtl) {
  5136. $this->x = $this->w - $this->rMargin;
  5137. } else {
  5138. $this->x = $this->lMargin;
  5139. }
  5140. }
  5141. } else {
  5142. // go left or right by case
  5143. if ($this->rtl) {
  5144. $this->x = $x - $w - $this->cell_margin['L'];
  5145. } else {
  5146. $this->x = $x + $w + $this->cell_margin['R'];
  5147. }
  5148. }
  5149. $gstyles = ''.$this->linestyleWidth.' '.$this->linestyleCap.' '.$this->linestyleJoin.' '.$this->linestyleDash.' '.$this->DrawColor.' '.$this->FillColor."\n";
  5150. $rs = $gstyles.$rs;
  5151. $this->cell_padding = $prev_cell_padding;
  5152. $this->cell_margin = $prev_cell_margin;
  5153. return $rs;
  5154. }
  5155. /**
  5156. * Replace a char if is defined on the current font.
  5157. * @param $oldchar (int) Integer code (unicode) of the character to replace.
  5158. * @param $newchar (int) Integer code (unicode) of the new character.
  5159. * @return int the replaced char or the old char in case the new char i not defined
  5160. * @protected
  5161. * @since 5.9.167 (2012-06-22)
  5162. */
  5163. protected function replaceChar($oldchar, $newchar) {
  5164. if ($this->isCharDefined($newchar)) {
  5165. // add the new char on the subset list
  5166. $this->CurrentFont['subsetchars'][$newchar] = true;
  5167. // return the new character
  5168. return $newchar;
  5169. }
  5170. // return the old char
  5171. return $oldchar;
  5172. }
  5173. /**
  5174. * Returns the code to draw the cell border
  5175. * @param $x (float) X coordinate.
  5176. * @param $y (float) Y coordinate.
  5177. * @param $w (float) Cell width.
  5178. * @param $h (float) Cell height.
  5179. * @param $brd (mixed) 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)))
  5180. * @return string containing cell border code
  5181. * @protected
  5182. * @see SetLineStyle()
  5183. * @since 5.7.000 (2010-08-02)
  5184. */
  5185. protected function getCellBorder($x, $y, $w, $h, $brd) {
  5186. $s = ''; // string to be returned
  5187. if (empty($brd)) {
  5188. return $s;
  5189. }
  5190. if ($brd == 1) {
  5191. $brd = array('LRTB' => true);
  5192. }
  5193. // calculate coordinates for border
  5194. $k = $this->k;
  5195. if ($this->rtl) {
  5196. $xeL = ($x - $w) * $k;
  5197. $xeR = $x * $k;
  5198. } else {
  5199. $xeL = $x * $k;
  5200. $xeR = ($x + $w) * $k;
  5201. }
  5202. $yeL = (($this->h - ($y + $h)) * $k);
  5203. $yeT = (($this->h - $y) * $k);
  5204. $xeT = $xeL;
  5205. $xeB = $xeR;
  5206. $yeR = $yeT;
  5207. $yeB = $yeL;
  5208. if (is_string($brd)) {
  5209. // convert string to array
  5210. $slen = strlen($brd);
  5211. $newbrd = array();
  5212. for ($i = 0; $i < $slen; ++$i) {
  5213. $newbrd[$brd[$i]] = array('cap' => 'square', 'join' => 'miter');
  5214. }
  5215. $brd = $newbrd;
  5216. }
  5217. if (isset($brd['mode'])) {
  5218. $mode = $brd['mode'];
  5219. unset($brd['mode']);
  5220. } else {
  5221. $mode = 'normal';
  5222. }
  5223. foreach ($brd as $border => $style) {
  5224. if (is_array($style) AND !empty($style)) {
  5225. // apply border style
  5226. $prev_style = $this->linestyleWidth.' '.$this->linestyleCap.' '.$this->linestyleJoin.' '.$this->linestyleDash.' '.$this->DrawColor.' ';
  5227. $s .= $this->SetLineStyle($style, true)."\n";
  5228. }
  5229. switch ($mode) {
  5230. case 'ext': {
  5231. $off = (($this->LineWidth / 2) * $k);
  5232. $xL = $xeL - $off;
  5233. $xR = $xeR + $off;
  5234. $yT = $yeT + $off;
  5235. $yL = $yeL - $off;
  5236. $xT = $xL;
  5237. $xB = $xR;
  5238. $yR = $yT;
  5239. $yB = $yL;
  5240. $w += $this->LineWidth;
  5241. $h += $this->LineWidth;
  5242. break;
  5243. }
  5244. case 'int': {
  5245. $off = ($this->LineWidth / 2) * $k;
  5246. $xL = $xeL + $off;
  5247. $xR = $xeR - $off;
  5248. $yT = $yeT - $off;
  5249. $yL = $yeL + $off;
  5250. $xT = $xL;
  5251. $xB = $xR;
  5252. $yR = $yT;
  5253. $yB = $yL;
  5254. $w -= $this->LineWidth;
  5255. $h -= $this->LineWidth;
  5256. break;
  5257. }
  5258. case 'normal':
  5259. default: {
  5260. $xL = $xeL;
  5261. $xT = $xeT;
  5262. $xB = $xeB;
  5263. $xR = $xeR;
  5264. $yL = $yeL;
  5265. $yT = $yeT;
  5266. $yB = $yeB;
  5267. $yR = $yeR;
  5268. break;
  5269. }
  5270. }
  5271. // draw borders by case
  5272. if (strlen($border) == 4) {
  5273. $s .= sprintf('%F %F %F %F re S ', $xT, $yT, ($w * $k), (-$h * $k));
  5274. } elseif (strlen($border) == 3) {
  5275. if (strpos($border,'B') === false) { // LTR
  5276. $s .= sprintf('%F %F m ', $xL, $yL);
  5277. $s .= sprintf('%F %F l ', $xT, $yT);
  5278. $s .= sprintf('%F %F l ', $xR, $yR);
  5279. $s .= sprintf('%F %F l ', $xB, $yB);
  5280. $s .= 'S ';
  5281. } elseif (strpos($border,'L') === false) { // TRB
  5282. $s .= sprintf('%F %F m ', $xT, $yT);
  5283. $s .= sprintf('%F %F l ', $xR, $yR);
  5284. $s .= sprintf('%F %F l ', $xB, $yB);
  5285. $s .= sprintf('%F %F l ', $xL, $yL);
  5286. $s .= 'S ';
  5287. } elseif (strpos($border,'T') === false) { // RBL
  5288. $s .= sprintf('%F %F m ', $xR, $yR);
  5289. $s .= sprintf('%F %F l ', $xB, $yB);
  5290. $s .= sprintf('%F %F l ', $xL, $yL);
  5291. $s .= sprintf('%F %F l ', $xT, $yT);
  5292. $s .= 'S ';
  5293. } elseif (strpos($border,'R') === false) { // BLT
  5294. $s .= sprintf('%F %F m ', $xB, $yB);
  5295. $s .= sprintf('%F %F l ', $xL, $yL);
  5296. $s .= sprintf('%F %F l ', $xT, $yT);
  5297. $s .= sprintf('%F %F l ', $xR, $yR);
  5298. $s .= 'S ';
  5299. }
  5300. } elseif (strlen($border) == 2) {
  5301. if ((strpos($border,'L') !== false) AND (strpos($border,'T') !== false)) { // LT
  5302. $s .= sprintf('%F %F m ', $xL, $yL);
  5303. $s .= sprintf('%F %F l ', $xT, $yT);
  5304. $s .= sprintf('%F %F l ', $xR, $yR);
  5305. $s .= 'S ';
  5306. } elseif ((strpos($border,'T') !== false) AND (strpos($border,'R') !== false)) { // TR
  5307. $s .= sprintf('%F %F m ', $xT, $yT);
  5308. $s .= sprintf('%F %F l ', $xR, $yR);
  5309. $s .= sprintf('%F %F l ', $xB, $yB);
  5310. $s .= 'S ';
  5311. } elseif ((strpos($border,'R') !== false) AND (strpos($border,'B') !== false)) { // RB
  5312. $s .= sprintf('%F %F m ', $xR, $yR);
  5313. $s .= sprintf('%F %F l ', $xB, $yB);
  5314. $s .= sprintf('%F %F l ', $xL, $yL);
  5315. $s .= 'S ';
  5316. } elseif ((strpos($border,'B') !== false) AND (strpos($border,'L') !== false)) { // BL
  5317. $s .= sprintf('%F %F m ', $xB, $yB);
  5318. $s .= sprintf('%F %F l ', $xL, $yL);
  5319. $s .= sprintf('%F %F l ', $xT, $yT);
  5320. $s .= 'S ';
  5321. } elseif ((strpos($border,'L') !== false) AND (strpos($border,'R') !== false)) { // LR
  5322. $s .= sprintf('%F %F m ', $xL, $yL);
  5323. $s .= sprintf('%F %F l ', $xT, $yT);
  5324. $s .= 'S ';
  5325. $s .= sprintf('%F %F m ', $xR, $yR);
  5326. $s .= sprintf('%F %F l ', $xB, $yB);
  5327. $s .= 'S ';
  5328. } elseif ((strpos($border,'T') !== false) AND (strpos($border,'B') !== false)) { // TB
  5329. $s .= sprintf('%F %F m ', $xT, $yT);
  5330. $s .= sprintf('%F %F l ', $xR, $yR);
  5331. $s .= 'S ';
  5332. $s .= sprintf('%F %F m ', $xB, $yB);
  5333. $s .= sprintf('%F %F l ', $xL, $yL);
  5334. $s .= 'S ';
  5335. }
  5336. } else { // strlen($border) == 1
  5337. if (strpos($border,'L') !== false) { // L
  5338. $s .= sprintf('%F %F m ', $xL, $yL);
  5339. $s .= sprintf('%F %F l ', $xT, $yT);
  5340. $s .= 'S ';
  5341. } elseif (strpos($border,'T') !== false) { // T
  5342. $s .= sprintf('%F %F m ', $xT, $yT);
  5343. $s .= sprintf('%F %F l ', $xR, $yR);
  5344. $s .= 'S ';
  5345. } elseif (strpos($border,'R') !== false) { // R
  5346. $s .= sprintf('%F %F m ', $xR, $yR);
  5347. $s .= sprintf('%F %F l ', $xB, $yB);
  5348. $s .= 'S ';
  5349. } elseif (strpos($border,'B') !== false) { // B
  5350. $s .= sprintf('%F %F m ', $xB, $yB);
  5351. $s .= sprintf('%F %F l ', $xL, $yL);
  5352. $s .= 'S ';
  5353. }
  5354. }
  5355. if (is_array($style) AND !empty($style)) {
  5356. // reset border style to previous value
  5357. $s .= "\n".$this->linestyleWidth.' '.$this->linestyleCap.' '.$this->linestyleJoin.' '.$this->linestyleDash.' '.$this->DrawColor."\n";
  5358. }
  5359. }
  5360. return $s;
  5361. }
  5362. /**
  5363. * This method allows printing text with line breaks.
  5364. * 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 />
  5365. * Text can be aligned, centered or justified. The cell block can be framed and the background painted.
  5366. * @param $w (float) Width of cells. If 0, they extend up to the right margin of the page.
  5367. * @param $h (float) Cell minimum height. The cell extends automatically if needed.
  5368. * @param $txt (string) String to print
  5369. * @param $border (mixed) 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)))
  5370. * @param $align (string) 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>
  5371. * @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
  5372. * @param $ln (int) 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>
  5373. * @param $x (float) x position in user units
  5374. * @param $y (float) y position in user units
  5375. * @param $reseth (boolean) if true reset the last cell height (default true).
  5376. * @param $stretch (int) 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.
  5377. * @param $ishtml (boolean) 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.
  5378. * @param $autopadding (boolean) if true, uses internal padding and automatically adjust it to account for line width.
  5379. * @param $maxh (float) 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.
  5380. * @param $valign (string) 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.
  5381. * @param $fitcell (boolean) 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.
  5382. * @return int Return the number of cells or 1 for html mode.
  5383. * @public
  5384. * @since 1.3
  5385. * @see SetFont(), SetDrawColor(), SetFillColor(), SetTextColor(), SetLineWidth(), Cell(), Write(), SetAutoPageBreak()
  5386. */
  5387. public function MultiCell($w, $h, $txt, $border=0, $align='J', $fill=false, $ln=1, $x='', $y='', $reseth=true, $stretch=0, $ishtml=false, $autopadding=true, $maxh=0, $valign='T', $fitcell=false) {
  5388. $prev_cell_margin = $this->cell_margin;
  5389. $prev_cell_padding = $this->cell_padding;
  5390. // adjust internal padding
  5391. $this->adjustCellPadding($border);
  5392. $mc_padding = $this->cell_padding;
  5393. $mc_margin = $this->cell_margin;
  5394. $this->cell_padding['T'] = 0;
  5395. $this->cell_padding['B'] = 0;
  5396. $this->setCellMargins(0, 0, 0, 0);
  5397. if (TCPDF_STATIC::empty_string($this->lasth) OR $reseth) {
  5398. // reset row height
  5399. $this->resetLastH();
  5400. }
  5401. if (!TCPDF_STATIC::empty_string($y)) {
  5402. $this->SetY($y);
  5403. } else {
  5404. $y = $this->GetY();
  5405. }
  5406. $resth = 0;
  5407. if (($h > 0) AND $this->inPageBody() AND (($y + $h + $mc_margin['T'] + $mc_margin['B']) > $this->PageBreakTrigger)) {
  5408. // spit cell in more pages/columns
  5409. $newh = ($this->PageBreakTrigger - $y);
  5410. $resth = ($h - $newh); // cell to be printed on the next page/column
  5411. $h = $newh;
  5412. }
  5413. // get current page number
  5414. $startpage = $this->page;
  5415. // get current column
  5416. $startcolumn = $this->current_column;
  5417. if (!TCPDF_STATIC::empty_string($x)) {
  5418. $this->SetX($x);
  5419. } else {
  5420. $x = $this->GetX();
  5421. }
  5422. // check page for no-write regions and adapt page margins if necessary
  5423. list($x, $y) = $this->checkPageRegions(0, $x, $y);
  5424. // apply margins
  5425. $oy = $y + $mc_margin['T'];
  5426. if ($this->rtl) {
  5427. $ox = ($this->w - $x - $mc_margin['R']);
  5428. } else {
  5429. $ox = ($x + $mc_margin['L']);
  5430. }
  5431. $this->x = $ox;
  5432. $this->y = $oy;
  5433. // set width
  5434. if (TCPDF_STATIC::empty_string($w) OR ($w <= 0)) {
  5435. if ($this->rtl) {
  5436. $w = ($this->x - $this->lMargin - $mc_margin['L']);
  5437. } else {
  5438. $w = ($this->w - $this->x - $this->rMargin - $mc_margin['R']);
  5439. }
  5440. }
  5441. // store original margin values
  5442. $lMargin = $this->lMargin;
  5443. $rMargin = $this->rMargin;
  5444. if ($this->rtl) {
  5445. $this->rMargin = ($this->w - $this->x);
  5446. $this->lMargin = ($this->x - $w);
  5447. } else {
  5448. $this->lMargin = ($this->x);
  5449. $this->rMargin = ($this->w - $this->x - $w);
  5450. }
  5451. $this->clMargin = $this->lMargin;
  5452. $this->crMargin = $this->rMargin;
  5453. if ($autopadding) {
  5454. // add top padding
  5455. $this->y += $mc_padding['T'];
  5456. }
  5457. if ($ishtml) { // ******* Write HTML text
  5458. $this->writeHTML($txt, true, false, $reseth, true, $align);
  5459. $nl = 1;
  5460. } else { // ******* Write simple text
  5461. $prev_FontSizePt = $this->FontSizePt;
  5462. if ($fitcell) {
  5463. // ajust height values
  5464. $tobottom = ($this->h - $this->y - $this->bMargin - $this->cell_padding['T'] - $this->cell_padding['B']);
  5465. $h = $maxh = max(min($h, $tobottom), min($maxh, $tobottom));
  5466. }
  5467. // vertical alignment
  5468. if ($maxh > 0) {
  5469. // get text height
  5470. $text_height = $this->getStringHeight($w, $txt, $reseth, $autopadding, $mc_padding, $border);
  5471. if ($fitcell AND ($text_height > $maxh) AND ($this->FontSizePt > 1)) {
  5472. // try to reduce font size to fit text on cell (use a quick search algorithm)
  5473. $fmin = 1;
  5474. $fmax = $this->FontSizePt;
  5475. $diff_epsilon = (1 / $this->k); // one point (min resolution)
  5476. $maxit = (2 * min(100, max(10, intval($fmax)))); // max number of iterations
  5477. while ($maxit >= 0) {
  5478. $fmid = (($fmax + $fmin) / 2);
  5479. $this->SetFontSize($fmid, false);
  5480. $this->resetLastH();
  5481. $text_height = $this->getStringHeight($w, $txt, $reseth, $autopadding, $mc_padding, $border);
  5482. $diff = ($maxh - $text_height);
  5483. if ($diff >= 0) {
  5484. if ($diff <= $diff_epsilon) {
  5485. break;
  5486. }
  5487. $fmin = $fmid;
  5488. } else {
  5489. $fmax = $fmid;
  5490. }
  5491. --$maxit;
  5492. }
  5493. if ($maxit < 0) {
  5494. // premature exit, we get the minimum font value to fit the cell
  5495. $this->SetFontSize($fmin);
  5496. $this->resetLastH();
  5497. $text_height = $this->getStringHeight($w, $txt, $reseth, $autopadding, $mc_padding, $border);
  5498. } else {
  5499. $this->SetFontSize($fmid);
  5500. $this->resetLastH();
  5501. }
  5502. }
  5503. if ($text_height < $maxh) {
  5504. if ($valign == 'M') {
  5505. // text vertically centered
  5506. $this->y += (($maxh - $text_height) / 2);
  5507. } elseif ($valign == 'B') {
  5508. // text vertically aligned on bottom
  5509. $this->y += ($maxh - $text_height);
  5510. }
  5511. }
  5512. }
  5513. $nl = $this->Write($this->lasth, $txt, '', 0, $align, true, $stretch, false, true, $maxh, 0, $mc_margin);
  5514. if ($fitcell) {
  5515. // restore font size
  5516. $this->SetFontSize($prev_FontSizePt);
  5517. }
  5518. }
  5519. if ($autopadding) {
  5520. // add bottom padding
  5521. $this->y += $mc_padding['B'];
  5522. }
  5523. // Get end-of-text Y position
  5524. $currentY = $this->y;
  5525. // get latest page number
  5526. $endpage = $this->page;
  5527. if ($resth > 0) {
  5528. $skip = ($endpage - $startpage);
  5529. $tmpresth = $resth;
  5530. while ($tmpresth > 0) {
  5531. if ($skip <= 0) {
  5532. // add a page (or trig AcceptPageBreak() for multicolumn mode)
  5533. $this->checkPageBreak($this->PageBreakTrigger + 1);
  5534. }
  5535. if ($this->num_columns > 1) {
  5536. $tmpresth -= ($this->h - $this->y - $this->bMargin);
  5537. } else {
  5538. $tmpresth -= ($this->h - $this->tMargin - $this->bMargin);
  5539. }
  5540. --$skip;
  5541. }
  5542. $currentY = $this->y;
  5543. $endpage = $this->page;
  5544. }
  5545. // get latest column
  5546. $endcolumn = $this->current_column;
  5547. if ($this->num_columns == 0) {
  5548. $this->num_columns = 1;
  5549. }
  5550. // disable page regions check
  5551. $check_page_regions = $this->check_page_regions;
  5552. $this->check_page_regions = false;
  5553. // get border modes
  5554. $border_start = TCPDF_STATIC::getBorderMode($border, $position='start', $this->opencell);
  5555. $border_end = TCPDF_STATIC::getBorderMode($border, $position='end', $this->opencell);
  5556. $border_middle = TCPDF_STATIC::getBorderMode($border, $position='middle', $this->opencell);
  5557. // design borders around HTML cells.
  5558. for ($page = $startpage; $page <= $endpage; ++$page) { // for each page
  5559. $ccode = '';
  5560. $this->setPage($page);
  5561. if ($this->num_columns < 2) {
  5562. // single-column mode
  5563. $this->SetX($x);
  5564. $this->y = $this->tMargin;
  5565. }
  5566. // account for margin changes
  5567. if ($page > $startpage) {
  5568. if (($this->rtl) AND ($this->pagedim[$page]['orm'] != $this->pagedim[$startpage]['orm'])) {
  5569. $this->x -= ($this->pagedim[$page]['orm'] - $this->pagedim[$startpage]['orm']);
  5570. } elseif ((!$this->rtl) AND ($this->pagedim[$page]['olm'] != $this->pagedim[$startpage]['olm'])) {
  5571. $this->x += ($this->pagedim[$page]['olm'] - $this->pagedim[$startpage]['olm']);
  5572. }
  5573. }
  5574. if ($startpage == $endpage) {
  5575. // single page
  5576. for ($column = $startcolumn; $column <= $endcolumn; ++$column) { // for each column
  5577. if ($column != $this->current_column) {
  5578. $this->selectColumn($column);
  5579. }
  5580. if ($this->rtl) {
  5581. $this->x -= $mc_margin['R'];
  5582. } else {
  5583. $this->x += $mc_margin['L'];
  5584. }
  5585. if ($startcolumn == $endcolumn) { // single column
  5586. $cborder = $border;
  5587. $h = max($h, ($currentY - $oy));
  5588. $this->y = $oy;
  5589. } elseif ($column == $startcolumn) { // first column
  5590. $cborder = $border_start;
  5591. $this->y = $oy;
  5592. $h = $this->h - $this->y - $this->bMargin;
  5593. } elseif ($column == $endcolumn) { // end column
  5594. $cborder = $border_end;
  5595. $h = $currentY - $this->y;
  5596. if ($resth > $h) {
  5597. $h = $resth;
  5598. }
  5599. } else { // middle column
  5600. $cborder = $border_middle;
  5601. $h = $this->h - $this->y - $this->bMargin;
  5602. $resth -= $h;
  5603. }
  5604. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
  5605. } // end for each column
  5606. } elseif ($page == $startpage) { // first page
  5607. for ($column = $startcolumn; $column < $this->num_columns; ++$column) { // for each column
  5608. if ($column != $this->current_column) {
  5609. $this->selectColumn($column);
  5610. }
  5611. if ($this->rtl) {
  5612. $this->x -= $mc_margin['R'];
  5613. } else {
  5614. $this->x += $mc_margin['L'];
  5615. }
  5616. if ($column == $startcolumn) { // first column
  5617. $cborder = $border_start;
  5618. $this->y = $oy;
  5619. $h = $this->h - $this->y - $this->bMargin;
  5620. } else { // middle column
  5621. $cborder = $border_middle;
  5622. $h = $this->h - $this->y - $this->bMargin;
  5623. $resth -= $h;
  5624. }
  5625. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
  5626. } // end for each column
  5627. } elseif ($page == $endpage) { // last page
  5628. for ($column = 0; $column <= $endcolumn; ++$column) { // for each column
  5629. if ($column != $this->current_column) {
  5630. $this->selectColumn($column);
  5631. }
  5632. if ($this->rtl) {
  5633. $this->x -= $mc_margin['R'];
  5634. } else {
  5635. $this->x += $mc_margin['L'];
  5636. }
  5637. if ($column == $endcolumn) {
  5638. // end column
  5639. $cborder = $border_end;
  5640. $h = $currentY - $this->y;
  5641. if ($resth > $h) {
  5642. $h = $resth;
  5643. }
  5644. } else {
  5645. // middle column
  5646. $cborder = $border_middle;
  5647. $h = $this->h - $this->y - $this->bMargin;
  5648. $resth -= $h;
  5649. }
  5650. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
  5651. } // end for each column
  5652. } else { // middle page
  5653. for ($column = 0; $column < $this->num_columns; ++$column) { // for each column
  5654. $this->selectColumn($column);
  5655. if ($this->rtl) {
  5656. $this->x -= $mc_margin['R'];
  5657. } else {
  5658. $this->x += $mc_margin['L'];
  5659. }
  5660. $cborder = $border_middle;
  5661. $h = $this->h - $this->y - $this->bMargin;
  5662. $resth -= $h;
  5663. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
  5664. } // end for each column
  5665. }
  5666. if ($cborder OR $fill) {
  5667. $offsetlen = strlen($ccode);
  5668. // draw border and fill
  5669. if ($this->inxobj) {
  5670. // we are inside an XObject template
  5671. if (end($this->xobjects[$this->xobjid]['transfmrk']) !== false) {
  5672. $pagemarkkey = key($this->xobjects[$this->xobjid]['transfmrk']);
  5673. $pagemark = $this->xobjects[$this->xobjid]['transfmrk'][$pagemarkkey];
  5674. $this->xobjects[$this->xobjid]['transfmrk'][$pagemarkkey] += $offsetlen;
  5675. } else {
  5676. $pagemark = $this->xobjects[$this->xobjid]['intmrk'];
  5677. $this->xobjects[$this->xobjid]['intmrk'] += $offsetlen;
  5678. }
  5679. $pagebuff = $this->xobjects[$this->xobjid]['outdata'];
  5680. $pstart = substr($pagebuff, 0, $pagemark);
  5681. $pend = substr($pagebuff, $pagemark);
  5682. $this->xobjects[$this->xobjid]['outdata'] = $pstart.$ccode.$pend;
  5683. } else {
  5684. if (end($this->transfmrk[$this->page]) !== false) {
  5685. $pagemarkkey = key($this->transfmrk[$this->page]);
  5686. $pagemark = $this->transfmrk[$this->page][$pagemarkkey];
  5687. $this->transfmrk[$this->page][$pagemarkkey] += $offsetlen;
  5688. } elseif ($this->InFooter) {
  5689. $pagemark = $this->footerpos[$this->page];
  5690. $this->footerpos[$this->page] += $offsetlen;
  5691. } else {
  5692. $pagemark = $this->intmrk[$this->page];
  5693. $this->intmrk[$this->page] += $offsetlen;
  5694. }
  5695. $pagebuff = $this->getPageBuffer($this->page);
  5696. $pstart = substr($pagebuff, 0, $pagemark);
  5697. $pend = substr($pagebuff, $pagemark);
  5698. $this->setPageBuffer($this->page, $pstart.$ccode.$pend);
  5699. }
  5700. }
  5701. } // end for each page
  5702. // restore page regions check
  5703. $this->check_page_regions = $check_page_regions;
  5704. // Get end-of-cell Y position
  5705. $currentY = $this->GetY();
  5706. // restore previous values
  5707. if ($this->num_columns > 1) {
  5708. $this->selectColumn();
  5709. } else {
  5710. // restore original margins
  5711. $this->lMargin = $lMargin;
  5712. $this->rMargin = $rMargin;
  5713. if ($this->page > $startpage) {
  5714. // check for margin variations between pages (i.e. booklet mode)
  5715. $dl = ($this->pagedim[$this->page]['olm'] - $this->pagedim[$startpage]['olm']);
  5716. $dr = ($this->pagedim[$this->page]['orm'] - $this->pagedim[$startpage]['orm']);
  5717. if (($dl != 0) OR ($dr != 0)) {
  5718. $this->lMargin += $dl;
  5719. $this->rMargin += $dr;
  5720. }
  5721. }
  5722. }
  5723. if ($ln > 0) {
  5724. //Go to the beginning of the next line
  5725. $this->SetY($currentY + $mc_margin['B']);
  5726. if ($ln == 2) {
  5727. $this->SetX($x + $w + $mc_margin['L'] + $mc_margin['R']);
  5728. }
  5729. } else {
  5730. // go left or right by case
  5731. $this->setPage($startpage);
  5732. $this->y = $y;
  5733. $this->SetX($x + $w + $mc_margin['L'] + $mc_margin['R']);
  5734. }
  5735. $this->setContentMark();
  5736. $this->cell_padding = $prev_cell_padding;
  5737. $this->cell_margin = $prev_cell_margin;
  5738. $this->clMargin = $this->lMargin;
  5739. $this->crMargin = $this->rMargin;
  5740. return $nl;
  5741. }
  5742. /**
  5743. * This method return the estimated number of lines for print a simple text string using Multicell() method.
  5744. * @param $txt (string) String for calculating his height
  5745. * @param $w (float) Width of cells. If 0, they extend up to the right margin of the page.
  5746. * @param $reseth (boolean) if true reset the last cell height (default false).
  5747. * @param $autopadding (boolean) if true, uses internal padding and automatically adjust it to account for line width (default true).
  5748. * @param $cellpadding (float) Internal cell padding, if empty uses default cell padding.
  5749. * @param $border (mixed) 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)))
  5750. * @return float Return the minimal height needed for multicell method for printing the $txt param.
  5751. * @author Alexander Escalona Fern\E1ndez, Nicola Asuni
  5752. * @public
  5753. * @since 4.5.011
  5754. */
  5755. public function getNumLines($txt, $w=0, $reseth=false, $autopadding=true, $cellpadding='', $border=0) {
  5756. if ($txt === NULL) {
  5757. return 0;
  5758. }
  5759. if ($txt === '') {
  5760. // empty string
  5761. return 1;
  5762. }
  5763. // adjust internal padding
  5764. $prev_cell_padding = $this->cell_padding;
  5765. $prev_lasth = $this->lasth;
  5766. if (is_array($cellpadding)) {
  5767. $this->cell_padding = $cellpadding;
  5768. }
  5769. $this->adjustCellPadding($border);
  5770. if (TCPDF_STATIC::empty_string($w) OR ($w <= 0)) {
  5771. if ($this->rtl) {
  5772. $w = $this->x - $this->lMargin;
  5773. } else {
  5774. $w = $this->w - $this->rMargin - $this->x;
  5775. }
  5776. }
  5777. $wmax = $w - $this->cell_padding['L'] - $this->cell_padding['R'];
  5778. if ($reseth) {
  5779. // reset row height
  5780. $this->resetLastH();
  5781. }
  5782. $lines = 1;
  5783. $sum = 0;
  5784. $chars = TCPDF_FONTS::utf8Bidi(TCPDF_FONTS::UTF8StringToArray($txt, $this->isunicode, $this->CurrentFont), $txt, $this->tmprtl, $this->isunicode, $this->CurrentFont);
  5785. $charsWidth = $this->GetArrStringWidth($chars, '', '', 0, true);
  5786. $length = count($chars);
  5787. $lastSeparator = -1;
  5788. for ($i = 0; $i < $length; ++$i) {
  5789. $c = $chars[$i];
  5790. $charWidth = $charsWidth[$i];
  5791. if (($c != 160)
  5792. AND (($c == 173)
  5793. OR preg_match($this->re_spaces, TCPDF_FONTS::unichr($c, $this->isunicode))
  5794. OR (($c == 45)
  5795. AND ($i > 0) AND ($i < ($length - 1))
  5796. AND @preg_match('/[\p{L}]/'.$this->re_space['m'], TCPDF_FONTS::unichr($chars[($i - 1)], $this->isunicode))
  5797. AND @preg_match('/[\p{L}]/'.$this->re_space['m'], TCPDF_FONTS::unichr($chars[($i + 1)], $this->isunicode))
  5798. )
  5799. )
  5800. ) {
  5801. $lastSeparator = $i;
  5802. }
  5803. if ((($sum + $charWidth) > $wmax) OR ($c == 10)) {
  5804. ++$lines;
  5805. if ($c == 10) {
  5806. $lastSeparator = -1;
  5807. $sum = 0;
  5808. } elseif ($lastSeparator != -1) {
  5809. $i = $lastSeparator;
  5810. $lastSeparator = -1;
  5811. $sum = 0;
  5812. } else {
  5813. $sum = $charWidth;
  5814. }
  5815. } else {
  5816. $sum += $charWidth;
  5817. }
  5818. }
  5819. if ($chars[($length - 1)] == 10) {
  5820. --$lines;
  5821. }
  5822. $this->cell_padding = $prev_cell_padding;
  5823. $this->lasth = $prev_lasth;
  5824. return $lines;
  5825. }
  5826. /**
  5827. * This method return the estimated height needed for printing a simple text string using the Multicell() method.
  5828. * Generally, if you want to know the exact height for a block of content you can use the following alternative technique:
  5829. * @pre
  5830. * // store current object
  5831. * $pdf->startTransaction();
  5832. * // store starting values
  5833. * $start_y = $pdf->GetY();
  5834. * $start_page = $pdf->getPage();
  5835. * // call your printing functions with your parameters
  5836. * // - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  5837. * $pdf->MultiCell($w=0, $h=0, $txt, $border=1, $align='L', $fill=false, $ln=1, $x='', $y='', $reseth=true, $stretch=0, $ishtml=false, $autopadding=true, $maxh=0);
  5838. * // - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  5839. * // get the new Y
  5840. * $end_y = $pdf->GetY();
  5841. * $end_page = $pdf->getPage();
  5842. * // calculate height
  5843. * $height = 0;
  5844. * if ($end_page == $start_page) {
  5845. * $height = $end_y - $start_y;
  5846. * } else {
  5847. * for ($page=$start_page; $page <= $end_page; ++$page) {
  5848. * $this->setPage($page);
  5849. * if ($page == $start_page) {
  5850. * // first page
  5851. * $height = $this->h - $start_y - $this->bMargin;
  5852. * } elseif ($page == $end_page) {
  5853. * // last page
  5854. * $height = $end_y - $this->tMargin;
  5855. * } else {
  5856. * $height = $this->h - $this->tMargin - $this->bMargin;
  5857. * }
  5858. * }
  5859. * }
  5860. * // restore previous object
  5861. * $pdf = $pdf->rollbackTransaction();
  5862. *
  5863. * @param $w (float) Width of cells. If 0, they extend up to the right margin of the page.
  5864. * @param $txt (string) String for calculating his height
  5865. * @param $reseth (boolean) if true reset the last cell height (default false).
  5866. * @param $autopadding (boolean) if true, uses internal padding and automatically adjust it to account for line width (default true).
  5867. * @param $cellpadding (float) Internal cell padding, if empty uses default cell padding.
  5868. * @param $border (mixed) 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)))
  5869. * @return float Return the minimal height needed for multicell method for printing the $txt param.
  5870. * @author Nicola Asuni, Alexander Escalona Fern\E1ndez
  5871. * @public
  5872. */
  5873. public function getStringHeight($w, $txt, $reseth=false, $autopadding=true, $cellpadding='', $border=0) {
  5874. // adjust internal padding
  5875. $prev_cell_padding = $this->cell_padding;
  5876. $prev_lasth = $this->lasth;
  5877. if (is_array($cellpadding)) {
  5878. $this->cell_padding = $cellpadding;
  5879. }
  5880. $this->adjustCellPadding($border);
  5881. $lines = $this->getNumLines($txt, $w, $reseth, $autopadding, $cellpadding, $border);
  5882. $height = $this->getCellHeight(($lines * $this->FontSize), $autopadding);
  5883. $this->cell_padding = $prev_cell_padding;
  5884. $this->lasth = $prev_lasth;
  5885. return $height;
  5886. }
  5887. /**
  5888. * This method prints text from the current position.<br />
  5889. * @param $h (float) Line height
  5890. * @param $txt (string) String to print
  5891. * @param $link (mixed) URL or identifier returned by AddLink()
  5892. * @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
  5893. * @param $align (string) 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>
  5894. * @param $ln (boolean) if true set cursor at the bottom of the line, otherwise set cursor at the top of the line.
  5895. * @param $stretch (int) 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.
  5896. * @param $firstline (boolean) if true prints only the first line and return the remaining string.
  5897. * @param $firstblock (boolean) if true the string is the starting of a line.
  5898. * @param $maxh (float) maximum height. It should be >= $h and less then remaining space to the bottom of the page, or 0 for disable this feature.
  5899. * @param $wadj (float) first line width will be reduced by this amount (used in HTML mode).
  5900. * @param $margin (array) margin array of the parent container
  5901. * @return mixed Return the number of cells or the remaining string if $firstline = true.
  5902. * @public
  5903. * @since 1.5
  5904. */
  5905. public function Write($h, $txt, $link='', $fill=false, $align='', $ln=false, $stretch=0, $firstline=false, $firstblock=false, $maxh=0, $wadj=0, $margin='') {
  5906. // check page for no-write regions and adapt page margins if necessary
  5907. list($this->x, $this->y) = $this->checkPageRegions($h, $this->x, $this->y);
  5908. if (strlen($txt) == 0) {
  5909. // fix empty text
  5910. $txt = ' ';
  5911. }
  5912. if ($margin === '') {
  5913. // set default margins
  5914. $margin = $this->cell_margin;
  5915. }
  5916. // remove carriage returns
  5917. $s = str_replace("\r", '', $txt);
  5918. // check if string contains arabic text
  5919. if (preg_match(TCPDF_FONT_DATA::$uni_RE_PATTERN_ARABIC, $s)) {
  5920. $arabic = true;
  5921. } else {
  5922. $arabic = false;
  5923. }
  5924. // check if string contains RTL text
  5925. if ($arabic OR ($this->tmprtl == 'R') OR preg_match(TCPDF_FONT_DATA::$uni_RE_PATTERN_RTL, $s)) {
  5926. $rtlmode = true;
  5927. } else {
  5928. $rtlmode = false;
  5929. }
  5930. // get a char width
  5931. $chrwidth = $this->GetCharWidth(46); // dot character
  5932. // get array of unicode values
  5933. $chars = TCPDF_FONTS::UTF8StringToArray($s, $this->isunicode, $this->CurrentFont);
  5934. // calculate maximum width for a single character on string
  5935. $chrw = $this->GetArrStringWidth($chars, '', '', 0, true);
  5936. array_walk($chrw, array($this, 'getRawCharWidth'));
  5937. $maxchwidth = max($chrw);
  5938. // get array of chars
  5939. $uchars = TCPDF_FONTS::UTF8ArrayToUniArray($chars, $this->isunicode);
  5940. // get the number of characters
  5941. $nb = count($chars);
  5942. // replacement for SHY character (minus symbol)
  5943. $shy_replacement = 45;
  5944. $shy_replacement_char = TCPDF_FONTS::unichr($shy_replacement, $this->isunicode);
  5945. // widht for SHY replacement
  5946. $shy_replacement_width = $this->GetCharWidth($shy_replacement);
  5947. // page width
  5948. $pw = $w = $this->w - $this->lMargin - $this->rMargin;
  5949. // calculate remaining line width ($w)
  5950. if ($this->rtl) {
  5951. $w = $this->x - $this->lMargin;
  5952. } else {
  5953. $w = $this->w - $this->rMargin - $this->x;
  5954. }
  5955. // max column width
  5956. $wmax = ($w - $wadj);
  5957. if (!$firstline) {
  5958. $wmax -= ($this->cell_padding['L'] + $this->cell_padding['R']);
  5959. }
  5960. if ((!$firstline) AND (($chrwidth > $wmax) OR ($maxchwidth > $wmax))) {
  5961. // the maximum width character do not fit on column
  5962. return '';
  5963. }
  5964. // minimum row height
  5965. $row_height = max($h, $this->getCellHeight($this->FontSize));
  5966. // max Y
  5967. $maxy = $this->y + $maxh - max($row_height, $h);
  5968. $start_page = $this->page;
  5969. $i = 0; // character position
  5970. $j = 0; // current starting position
  5971. $sep = -1; // position of the last blank space
  5972. $prevsep = $sep; // previous separator
  5973. $shy = false; // true if the last blank is a soft hypen (SHY)
  5974. $prevshy = $shy; // previous shy mode
  5975. $l = 0; // current string length
  5976. $nl = 0; //number of lines
  5977. $linebreak = false;
  5978. $pc = 0; // previous character
  5979. // for each character
  5980. while ($i < $nb) {
  5981. if (($maxh > 0) AND ($this->y > $maxy) ) {
  5982. break;
  5983. }
  5984. //Get the current character
  5985. $c = $chars[$i];
  5986. if ($c == 10) { // 10 = "\n" = new line
  5987. //Explicit line break
  5988. if ($align == 'J') {
  5989. if ($this->rtl) {
  5990. $talign = 'R';
  5991. } else {
  5992. $talign = 'L';
  5993. }
  5994. } else {
  5995. $talign = $align;
  5996. }
  5997. $tmpstr = TCPDF_FONTS::UniArrSubString($uchars, $j, $i);
  5998. if ($firstline) {
  5999. $startx = $this->x;
  6000. $tmparr = array_slice($chars, $j, ($i - $j));
  6001. if ($rtlmode) {
  6002. $tmparr = TCPDF_FONTS::utf8Bidi($tmparr, $tmpstr, $this->tmprtl, $this->isunicode, $this->CurrentFont);
  6003. }
  6004. $linew = $this->GetArrStringWidth($tmparr);
  6005. unset($tmparr);
  6006. if ($this->rtl) {
  6007. $this->endlinex = $startx - $linew;
  6008. } else {
  6009. $this->endlinex = $startx + $linew;
  6010. }
  6011. $w = $linew;
  6012. $tmpcellpadding = $this->cell_padding;
  6013. if ($maxh == 0) {
  6014. $this->SetCellPadding(0);
  6015. }
  6016. }
  6017. if ($firstblock AND $this->isRTLTextDir()) {
  6018. $tmpstr = $this->stringRightTrim($tmpstr);
  6019. }
  6020. // Skip newlines at the beginning of a page or column
  6021. if (!empty($tmpstr) OR ($this->y < ($this->PageBreakTrigger - $row_height))) {
  6022. $this->Cell($w, $h, $tmpstr, 0, 1, $talign, $fill, $link, $stretch);
  6023. }
  6024. unset($tmpstr);
  6025. if ($firstline) {
  6026. $this->cell_padding = $tmpcellpadding;
  6027. return (TCPDF_FONTS::UniArrSubString($uchars, $i));
  6028. }
  6029. ++$nl;
  6030. $j = $i + 1;
  6031. $l = 0;
  6032. $sep = -1;
  6033. $prevsep = $sep;
  6034. $shy = false;
  6035. // account for margin changes
  6036. if ((($this->y + $this->lasth) > $this->PageBreakTrigger) AND ($this->inPageBody())) {
  6037. $this->AcceptPageBreak();
  6038. if ($this->rtl) {
  6039. $this->x -= $margin['R'];
  6040. } else {
  6041. $this->x += $margin['L'];
  6042. }
  6043. $this->lMargin += $margin['L'];
  6044. $this->rMargin += $margin['R'];
  6045. }
  6046. $w = $this->getRemainingWidth();
  6047. $wmax = ($w - $this->cell_padding['L'] - $this->cell_padding['R']);
  6048. } else {
  6049. // 160 is the non-breaking space.
  6050. // 173 is SHY (Soft Hypen).
  6051. // \p{Z} or \p{Separator}: any kind of Unicode whitespace or invisible separator.
  6052. // \p{Lo} or \p{Other_Letter}: a Unicode letter or ideograph that does not have lowercase and uppercase variants.
  6053. // \p{Lo} is needed because Chinese characters are packed next to each other without spaces in between.
  6054. if (($c != 160)
  6055. AND (($c == 173)
  6056. OR preg_match($this->re_spaces, TCPDF_FONTS::unichr($c, $this->isunicode))
  6057. OR (($c == 45)
  6058. AND ($i < ($nb - 1))
  6059. AND @preg_match('/[\p{L}]/'.$this->re_space['m'], TCPDF_FONTS::unichr($pc, $this->isunicode))
  6060. AND @preg_match('/[\p{L}]/'.$this->re_space['m'], TCPDF_FONTS::unichr($chars[($i + 1)], $this->isunicode))
  6061. )
  6062. )
  6063. ) {
  6064. // update last blank space position
  6065. $prevsep = $sep;
  6066. $sep = $i;
  6067. // check if is a SHY
  6068. if (($c == 173) OR ($c == 45)) {
  6069. $prevshy = $shy;
  6070. $shy = true;
  6071. if ($pc == 45) {
  6072. $tmp_shy_replacement_width = 0;
  6073. $tmp_shy_replacement_char = '';
  6074. } else {
  6075. $tmp_shy_replacement_width = $shy_replacement_width;
  6076. $tmp_shy_replacement_char = $shy_replacement_char;
  6077. }
  6078. } else {
  6079. $shy = false;
  6080. }
  6081. }
  6082. // update string length
  6083. if ($this->isUnicodeFont() AND ($arabic)) {
  6084. // with bidirectional algorithm some chars may be changed affecting the line length
  6085. // *** very slow ***
  6086. $l = $this->GetArrStringWidth(TCPDF_FONTS::utf8Bidi(array_slice($chars, $j, ($i - $j)), '', $this->tmprtl, $this->isunicode, $this->CurrentFont));
  6087. } else {
  6088. $l += $this->GetCharWidth($c);
  6089. }
  6090. if (($l > $wmax) OR (($c == 173) AND (($l + $tmp_shy_replacement_width) >= $wmax))) {
  6091. if (($c == 173) AND (($l + $tmp_shy_replacement_width) > $wmax)) {
  6092. $sep = $prevsep;
  6093. $shy = $prevshy;
  6094. }
  6095. // we have reached the end of column
  6096. if ($sep == -1) {
  6097. // check if the line was already started
  6098. if (($this->rtl AND ($this->x <= ($this->w - $this->rMargin - $this->cell_padding['R'] - $margin['R'] - $chrwidth)))
  6099. OR ((!$this->rtl) AND ($this->x >= ($this->lMargin + $this->cell_padding['L'] + $margin['L'] + $chrwidth)))) {
  6100. // print a void cell and go to next line
  6101. $this->Cell($w, $h, '', 0, 1);
  6102. $linebreak = true;
  6103. if ($firstline) {
  6104. return (TCPDF_FONTS::UniArrSubString($uchars, $j));
  6105. }
  6106. } else {
  6107. // truncate the word because do not fit on column
  6108. $tmpstr = TCPDF_FONTS::UniArrSubString($uchars, $j, $i);
  6109. if ($firstline) {
  6110. $startx = $this->x;
  6111. $tmparr = array_slice($chars, $j, ($i - $j));
  6112. if ($rtlmode) {
  6113. $tmparr = TCPDF_FONTS::utf8Bidi($tmparr, $tmpstr, $this->tmprtl, $this->isunicode, $this->CurrentFont);
  6114. }
  6115. $linew = $this->GetArrStringWidth($tmparr);
  6116. unset($tmparr);
  6117. if ($this->rtl) {
  6118. $this->endlinex = $startx - $linew;
  6119. } else {
  6120. $this->endlinex = $startx + $linew;
  6121. }
  6122. $w = $linew;
  6123. $tmpcellpadding = $this->cell_padding;
  6124. if ($maxh == 0) {
  6125. $this->SetCellPadding(0);
  6126. }
  6127. }
  6128. if ($firstblock AND $this->isRTLTextDir()) {
  6129. $tmpstr = $this->stringRightTrim($tmpstr);
  6130. }
  6131. $this->Cell($w, $h, $tmpstr, 0, 1, $align, $fill, $link, $stretch);
  6132. unset($tmpstr);
  6133. if ($firstline) {
  6134. $this->cell_padding = $tmpcellpadding;
  6135. return (TCPDF_FONTS::UniArrSubString($uchars, $i));
  6136. }
  6137. $j = $i;
  6138. --$i;
  6139. }
  6140. } else {
  6141. // word wrapping
  6142. if ($this->rtl AND (!$firstblock) AND ($sep < $i)) {
  6143. $endspace = 1;
  6144. } else {
  6145. $endspace = 0;
  6146. }
  6147. // check the length of the next string
  6148. $strrest = TCPDF_FONTS::UniArrSubString($uchars, ($sep + $endspace));
  6149. $nextstr = TCPDF_STATIC::pregSplit('/'.$this->re_space['p'].'/', $this->re_space['m'], $this->stringTrim($strrest));
  6150. if (isset($nextstr[0]) AND ($this->GetStringWidth($nextstr[0]) > $pw)) {
  6151. // truncate the word because do not fit on a full page width
  6152. $tmpstr = TCPDF_FONTS::UniArrSubString($uchars, $j, $i);
  6153. if ($firstline) {
  6154. $startx = $this->x;
  6155. $tmparr = array_slice($chars, $j, ($i - $j));
  6156. if ($rtlmode) {
  6157. $tmparr = TCPDF_FONTS::utf8Bidi($tmparr, $tmpstr, $this->tmprtl, $this->isunicode, $this->CurrentFont);
  6158. }
  6159. $linew = $this->GetArrStringWidth($tmparr);
  6160. unset($tmparr);
  6161. if ($this->rtl) {
  6162. $this->endlinex = ($startx - $linew);
  6163. } else {
  6164. $this->endlinex = ($startx + $linew);
  6165. }
  6166. $w = $linew;
  6167. $tmpcellpadding = $this->cell_padding;
  6168. if ($maxh == 0) {
  6169. $this->SetCellPadding(0);
  6170. }
  6171. }
  6172. if ($firstblock AND $this->isRTLTextDir()) {
  6173. $tmpstr = $this->stringRightTrim($tmpstr);
  6174. }
  6175. $this->Cell($w, $h, $tmpstr, 0, 1, $align, $fill, $link, $stretch);
  6176. unset($tmpstr);
  6177. if ($firstline) {
  6178. $this->cell_padding = $tmpcellpadding;
  6179. return (TCPDF_FONTS::UniArrSubString($uchars, $i));
  6180. }
  6181. $j = $i;
  6182. --$i;
  6183. } else {
  6184. // word wrapping
  6185. if ($shy) {
  6186. // add hypen (minus symbol) at the end of the line
  6187. $shy_width = $tmp_shy_replacement_width;
  6188. if ($this->rtl) {
  6189. $shy_char_left = $tmp_shy_replacement_char;
  6190. $shy_char_right = '';
  6191. } else {
  6192. $shy_char_left = '';
  6193. $shy_char_right = $tmp_shy_replacement_char;
  6194. }
  6195. } else {
  6196. $shy_width = 0;
  6197. $shy_char_left = '';
  6198. $shy_char_right = '';
  6199. }
  6200. $tmpstr = TCPDF_FONTS::UniArrSubString($uchars, $j, ($sep + $endspace));
  6201. if ($firstline) {
  6202. $startx = $this->x;
  6203. $tmparr = array_slice($chars, $j, (($sep + $endspace) - $j));
  6204. if ($rtlmode) {
  6205. $tmparr = TCPDF_FONTS::utf8Bidi($tmparr, $tmpstr, $this->tmprtl, $this->isunicode, $this->CurrentFont);
  6206. }
  6207. $linew = $this->GetArrStringWidth($tmparr);
  6208. unset($tmparr);
  6209. if ($this->rtl) {
  6210. $this->endlinex = $startx - $linew - $shy_width;
  6211. } else {
  6212. $this->endlinex = $startx + $linew + $shy_width;
  6213. }
  6214. $w = $linew;
  6215. $tmpcellpadding = $this->cell_padding;
  6216. if ($maxh == 0) {
  6217. $this->SetCellPadding(0);
  6218. }
  6219. }
  6220. // print the line
  6221. if ($firstblock AND $this->isRTLTextDir()) {
  6222. $tmpstr = $this->stringRightTrim($tmpstr);
  6223. }
  6224. $this->Cell($w, $h, $shy_char_left.$tmpstr.$shy_char_right, 0, 1, $align, $fill, $link, $stretch);
  6225. unset($tmpstr);
  6226. if ($firstline) {
  6227. if ($chars[$sep] == 45) {
  6228. $endspace += 1;
  6229. }
  6230. // return the remaining text
  6231. $this->cell_padding = $tmpcellpadding;
  6232. return (TCPDF_FONTS::UniArrSubString($uchars, ($sep + $endspace)));
  6233. }
  6234. $i = $sep;
  6235. $sep = -1;
  6236. $shy = false;
  6237. $j = ($i + 1);
  6238. }
  6239. }
  6240. // account for margin changes
  6241. if ((($this->y + $this->lasth) > $this->PageBreakTrigger) AND ($this->inPageBody())) {
  6242. $this->AcceptPageBreak();
  6243. if ($this->rtl) {
  6244. $this->x -= $margin['R'];
  6245. } else {
  6246. $this->x += $margin['L'];
  6247. }
  6248. $this->lMargin += $margin['L'];
  6249. $this->rMargin += $margin['R'];
  6250. }
  6251. $w = $this->getRemainingWidth();
  6252. $wmax = $w - $this->cell_padding['L'] - $this->cell_padding['R'];
  6253. if ($linebreak) {
  6254. $linebreak = false;
  6255. } else {
  6256. ++$nl;
  6257. $l = 0;
  6258. }
  6259. }
  6260. }
  6261. // save last character
  6262. $pc = $c;
  6263. ++$i;
  6264. } // end while i < nb
  6265. // print last substring (if any)
  6266. if ($l > 0) {
  6267. switch ($align) {
  6268. case 'J':
  6269. case 'C': {
  6270. $w = $w;
  6271. break;
  6272. }
  6273. case 'L': {
  6274. if ($this->rtl) {
  6275. $w = $w;
  6276. } else {
  6277. $w = $l;
  6278. }
  6279. break;
  6280. }
  6281. case 'R': {
  6282. if ($this->rtl) {
  6283. $w = $l;
  6284. } else {
  6285. $w = $w;
  6286. }
  6287. break;
  6288. }
  6289. default: {
  6290. $w = $l;
  6291. break;
  6292. }
  6293. }
  6294. $tmpstr = TCPDF_FONTS::UniArrSubString($uchars, $j, $nb);
  6295. if ($firstline) {
  6296. $startx = $this->x;
  6297. $tmparr = array_slice($chars, $j, ($nb - $j));
  6298. if ($rtlmode) {
  6299. $tmparr = TCPDF_FONTS::utf8Bidi($tmparr, $tmpstr, $this->tmprtl, $this->isunicode, $this->CurrentFont);
  6300. }
  6301. $linew = $this->GetArrStringWidth($tmparr);
  6302. unset($tmparr);
  6303. if ($this->rtl) {
  6304. $this->endlinex = $startx - $linew;
  6305. } else {
  6306. $this->endlinex = $startx + $linew;
  6307. }
  6308. $w = $linew;
  6309. $tmpcellpadding = $this->cell_padding;
  6310. if ($maxh == 0) {
  6311. $this->SetCellPadding(0);
  6312. }
  6313. }
  6314. if ($firstblock AND $this->isRTLTextDir()) {
  6315. $tmpstr = $this->stringRightTrim($tmpstr);
  6316. }
  6317. $this->Cell($w, $h, $tmpstr, 0, $ln, $align, $fill, $link, $stretch);
  6318. unset($tmpstr);
  6319. if ($firstline) {
  6320. $this->cell_padding = $tmpcellpadding;
  6321. return (TCPDF_FONTS::UniArrSubString($uchars, $nb));
  6322. }
  6323. ++$nl;
  6324. }
  6325. if ($firstline) {
  6326. return '';
  6327. }
  6328. return $nl;
  6329. }
  6330. /**
  6331. * Returns the remaining width between the current position and margins.
  6332. * @return int Return the remaining width
  6333. * @protected
  6334. */
  6335. protected function getRemainingWidth() {
  6336. list($this->x, $this->y) = $this->checkPageRegions(0, $this->x, $this->y);
  6337. if ($this->rtl) {
  6338. return ($this->x - $this->lMargin);
  6339. } else {
  6340. return ($this->w - $this->rMargin - $this->x);
  6341. }
  6342. }
  6343. /**
  6344. * Set the block dimensions accounting for page breaks and page/column fitting
  6345. * @param $w (float) width
  6346. * @param $h (float) height
  6347. * @param $x (float) X coordinate
  6348. * @param $y (float) Y coodiante
  6349. * @param $fitonpage (boolean) if true the block is resized to not exceed page dimensions.
  6350. * @return array($w, $h, $x, $y)
  6351. * @protected
  6352. * @since 5.5.009 (2010-07-05)
  6353. */
  6354. protected function fitBlock($w, $h, $x, $y, $fitonpage=false) {
  6355. if ($w <= 0) {
  6356. // set maximum width
  6357. $w = ($this->w - $this->lMargin - $this->rMargin);
  6358. if ($w <= 0) {
  6359. $w = 1;
  6360. }
  6361. }
  6362. if ($h <= 0) {
  6363. // set maximum height
  6364. $h = ($this->PageBreakTrigger - $this->tMargin);
  6365. if ($h <= 0) {
  6366. $h = 1;
  6367. }
  6368. }
  6369. // resize the block to be vertically contained on a single page or single column
  6370. if ($fitonpage OR $this->AutoPageBreak) {
  6371. $ratio_wh = ($w / $h);
  6372. if ($h > ($this->PageBreakTrigger - $this->tMargin)) {
  6373. $h = $this->PageBreakTrigger - $this->tMargin;
  6374. $w = ($h * $ratio_wh);
  6375. }
  6376. // resize the block to be horizontally contained on a single page or single column
  6377. if ($fitonpage) {
  6378. $maxw = ($this->w - $this->lMargin - $this->rMargin);
  6379. if ($w > $maxw) {
  6380. $w = $maxw;
  6381. $h = ($w / $ratio_wh);
  6382. }
  6383. }
  6384. }
  6385. // Check whether we need a new page or new column first as this does not fit
  6386. $prev_x = $this->x;
  6387. $prev_y = $this->y;
  6388. if ($this->checkPageBreak($h, $y) OR ($this->y < $prev_y)) {
  6389. $y = $this->y;
  6390. if ($this->rtl) {
  6391. $x += ($prev_x - $this->x);
  6392. } else {
  6393. $x += ($this->x - $prev_x);
  6394. }
  6395. $this->newline = true;
  6396. }
  6397. // resize the block to be contained on the remaining available page or column space
  6398. if ($fitonpage) {
  6399. $ratio_wh = ($w / $h);
  6400. if (($y + $h) > $this->PageBreakTrigger) {
  6401. $h = $this->PageBreakTrigger - $y;
  6402. $w = ($h * $ratio_wh);
  6403. }
  6404. if ((!$this->rtl) AND (($x + $w) > ($this->w - $this->rMargin))) {
  6405. $w = $this->w - $this->rMargin - $x;
  6406. $h = ($w / $ratio_wh);
  6407. } elseif (($this->rtl) AND (($x - $w) < ($this->lMargin))) {
  6408. $w = $x - $this->lMargin;
  6409. $h = ($w / $ratio_wh);
  6410. }
  6411. }
  6412. return array($w, $h, $x, $y);
  6413. }
  6414. /**
  6415. * Puts an image in the page.
  6416. * The upper-left corner must be given.
  6417. * The dimensions can be specified in different ways:<ul>
  6418. * <li>explicit width and height (expressed in user unit)</li>
  6419. * <li>one explicit dimension, the other being calculated automatically in order to keep the original proportions</li>
  6420. * <li>no explicit dimension, in which case the image is put at 72 dpi</li></ul>
  6421. * 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;
  6422. * The format can be specified explicitly or inferred from the file extension.<br />
  6423. * It is possible to put a link on the image.<br />
  6424. * Remark: if an image is used several times, only one copy will be embedded in the file.<br />
  6425. * @param $file (string) 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').
  6426. * @param $x (float) Abscissa of the upper-left corner (LTR) or upper-right corner (RTL).
  6427. * @param $y (float) Ordinate of the upper-left corner (LTR) or upper-right corner (RTL).
  6428. * @param $w (float) Width of the image in the page. If not specified or equal to zero, it is automatically calculated.
  6429. * @param $h (float) Height of the image in the page. If not specified or equal to zero, it is automatically calculated.
  6430. * @param $type (string) 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.
  6431. * @param $link (mixed) URL or identifier returned by AddLink().
  6432. * @param $align (string) 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>
  6433. * @param $resize (mixed) 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).
  6434. * @param $dpi (int) dot-per-inch resolution used on resize
  6435. * @param $palign (string) 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>
  6436. * @param $ismask (boolean) true if this image is a mask, false otherwise
  6437. * @param $imgmask (mixed) image object returned by this function or false
  6438. * @param $border (mixed) 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)))
  6439. * @param $fitbox (mixed) 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).
  6440. * @param $hidden (boolean) If true do not display the image.
  6441. * @param $fitonpage (boolean) If true the image is resized to not exceed page dimensions.
  6442. * @param $alt (boolean) If true the image will be added as alternative and not directly printed (the ID of the image will be returned).
  6443. * @param $altimgs (array) 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.
  6444. * @return image information
  6445. * @public
  6446. * @since 1.1
  6447. */
  6448. public function Image($file, $x='', $y='', $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()) {
  6449. if ($this->state != 2) {
  6450. return;
  6451. }
  6452. if (strcmp($x, '') === 0) {
  6453. $x = $this->x;
  6454. }
  6455. if (strcmp($y, '') === 0) {
  6456. $y = $this->y;
  6457. }
  6458. // check page for no-write regions and adapt page margins if necessary
  6459. list($x, $y) = $this->checkPageRegions($h, $x, $y);
  6460. $exurl = ''; // external streams
  6461. $imsize = FALSE;
  6462. // check if we are passing an image as file or string
  6463. if ($file[0] === '@') {
  6464. // image from string
  6465. $imgdata = substr($file, 1);
  6466. } else { // image file
  6467. if ($file[0] === '*') {
  6468. // image as external stream
  6469. $file = substr($file, 1);
  6470. $exurl = $file;
  6471. }
  6472. // check if is a local file
  6473. if (!@file_exists($file)) {
  6474. // try to encode spaces on filename
  6475. $tfile = str_replace(' ', '%20', $file);
  6476. if (@file_exists($tfile)) {
  6477. $file = $tfile;
  6478. }
  6479. }
  6480. if (($imsize = @getimagesize($file)) === FALSE) {
  6481. if (in_array($file, $this->imagekeys)) {
  6482. // get existing image data
  6483. $info = $this->getImageBuffer($file);
  6484. $imsize = array($info['w'], $info['h']);
  6485. } elseif (strpos($file, '__tcpdf_'.$this->file_id.'_img') === FALSE) {
  6486. $imgdata = TCPDF_STATIC::fileGetContents($file);
  6487. }
  6488. }
  6489. }
  6490. if (!empty($imgdata)) {
  6491. // copy image to cache
  6492. $original_file = $file;
  6493. $file = TCPDF_STATIC::getObjFilename('img', $this->file_id);
  6494. $fp = TCPDF_STATIC::fopenLocal($file, 'w');
  6495. if (!$fp) {
  6496. $this->Error('Unable to write file: '.$file);
  6497. }
  6498. fwrite($fp, $imgdata);
  6499. fclose($fp);
  6500. unset($imgdata);
  6501. $imsize = @getimagesize($file);
  6502. if ($imsize === FALSE) {
  6503. unlink($file);
  6504. $file = $original_file;
  6505. }
  6506. }
  6507. if ($imsize === FALSE) {
  6508. if (($w > 0) AND ($h > 0)) {
  6509. // get measures from specified data
  6510. $pw = $this->getHTMLUnitToUnits($w, 0, $this->pdfunit, true) * $this->imgscale * $this->k;
  6511. $ph = $this->getHTMLUnitToUnits($h, 0, $this->pdfunit, true) * $this->imgscale * $this->k;
  6512. $imsize = array($pw, $ph);
  6513. } else {
  6514. $this->Error('[Image] Unable to get the size of the image: '.$file);
  6515. }
  6516. }
  6517. // file hash
  6518. $filehash = md5($file);
  6519. // get original image width and height in pixels
  6520. list($pixw, $pixh) = $imsize;
  6521. // calculate image width and height on document
  6522. if (($w <= 0) AND ($h <= 0)) {
  6523. // convert image size to document unit
  6524. $w = $this->pixelsToUnits($pixw);
  6525. $h = $this->pixelsToUnits($pixh);
  6526. } elseif ($w <= 0) {
  6527. $w = $h * $pixw / $pixh;
  6528. } elseif ($h <= 0) {
  6529. $h = $w * $pixh / $pixw;
  6530. } elseif (($fitbox !== false) AND ($w > 0) AND ($h > 0)) {
  6531. if (strlen($fitbox) !== 2) {
  6532. // set default alignment
  6533. $fitbox = '--';
  6534. }
  6535. // scale image dimensions proportionally to fit within the ($w, $h) box
  6536. if ((($w * $pixh) / ($h * $pixw)) < 1) {
  6537. // store current height
  6538. $oldh = $h;
  6539. // calculate new height
  6540. $h = $w * $pixh / $pixw;
  6541. // height difference
  6542. $hdiff = ($oldh - $h);
  6543. // vertical alignment
  6544. switch (strtoupper($fitbox[1])) {
  6545. case 'T': {
  6546. break;
  6547. }
  6548. case 'M': {
  6549. $y += ($hdiff / 2);
  6550. break;
  6551. }
  6552. case 'B': {
  6553. $y += $hdiff;
  6554. break;
  6555. }
  6556. }
  6557. } else {
  6558. // store current width
  6559. $oldw = $w;
  6560. // calculate new width
  6561. $w = $h * $pixw / $pixh;
  6562. // width difference
  6563. $wdiff = ($oldw - $w);
  6564. // horizontal alignment
  6565. switch (strtoupper($fitbox[0])) {
  6566. case 'L': {
  6567. if ($this->rtl) {
  6568. $x -= $wdiff;
  6569. }
  6570. break;
  6571. }
  6572. case 'C': {
  6573. if ($this->rtl) {
  6574. $x -= ($wdiff / 2);
  6575. } else {
  6576. $x += ($wdiff / 2);
  6577. }
  6578. break;
  6579. }
  6580. case 'R': {
  6581. if (!$this->rtl) {
  6582. $x += $wdiff;
  6583. }
  6584. break;
  6585. }
  6586. }
  6587. }
  6588. }
  6589. // fit the image on available space
  6590. list($w, $h, $x, $y) = $this->fitBlock($w, $h, $x, $y, $fitonpage);
  6591. // calculate new minimum dimensions in pixels
  6592. $neww = round($w * $this->k * $dpi / $this->dpi);
  6593. $newh = round($h * $this->k * $dpi / $this->dpi);
  6594. // check if resize is necessary (resize is used only to reduce the image)
  6595. $newsize = ($neww * $newh);
  6596. $pixsize = ($pixw * $pixh);
  6597. if (intval($resize) == 2) {
  6598. $resize = true;
  6599. } elseif ($newsize >= $pixsize) {
  6600. $resize = false;
  6601. }
  6602. // check if image has been already added on document
  6603. $newimage = true;
  6604. if (in_array($file, $this->imagekeys)) {
  6605. $newimage = false;
  6606. // get existing image data
  6607. $info = $this->getImageBuffer($file);
  6608. if (strpos($file, '__tcpdf_'.$this->file_id.'_imgmask_') === FALSE) {
  6609. // check if the newer image is larger
  6610. $oldsize = ($info['w'] * $info['h']);
  6611. if ((($oldsize < $newsize) AND ($resize)) OR (($oldsize < $pixsize) AND (!$resize))) {
  6612. $newimage = true;
  6613. }
  6614. }
  6615. } elseif (($ismask === false) AND ($imgmask === false) AND (strpos($file, '__tcpdf_'.$this->file_id.'_imgmask_') === FALSE)) {
  6616. // create temp image file (without alpha channel)
  6617. $tempfile_plain = K_PATH_CACHE.'__tcpdf_'.$this->file_id.'_imgmask_plain_'.$filehash;
  6618. // create temp alpha file
  6619. $tempfile_alpha = K_PATH_CACHE.'__tcpdf_'.$this->file_id.'_imgmask_alpha_'.$filehash;
  6620. // check for cached images
  6621. if (in_array($tempfile_plain, $this->imagekeys)) {
  6622. // get existing image data
  6623. $info = $this->getImageBuffer($tempfile_plain);
  6624. // check if the newer image is larger
  6625. $oldsize = ($info['w'] * $info['h']);
  6626. if ((($oldsize < $newsize) AND ($resize)) OR (($oldsize < $pixsize) AND (!$resize))) {
  6627. $newimage = true;
  6628. } else {
  6629. $newimage = false;
  6630. // embed mask image
  6631. $imgmask = $this->Image($tempfile_alpha, $x, $y, $w, $h, 'PNG', '', '', $resize, $dpi, '', true, false);
  6632. // embed image, masked with previously embedded mask
  6633. return $this->Image($tempfile_plain, $x, $y, $w, $h, $type, $link, $align, $resize, $dpi, $palign, false, $imgmask);
  6634. }
  6635. }
  6636. }
  6637. if ($newimage) {
  6638. //First use of image, get info
  6639. $type = strtolower($type);
  6640. if ($type == '') {
  6641. $type = TCPDF_IMAGES::getImageFileType($file, $imsize);
  6642. } elseif ($type == 'jpg') {
  6643. $type = 'jpeg';
  6644. }
  6645. $mqr = TCPDF_STATIC::get_mqr();
  6646. TCPDF_STATIC::set_mqr(false);
  6647. // Specific image handlers (defined on TCPDF_IMAGES CLASS)
  6648. $mtd = '_parse'.$type;
  6649. // GD image handler function
  6650. $gdfunction = 'imagecreatefrom'.$type;
  6651. $info = false;
  6652. if ((method_exists('TCPDF_IMAGES', $mtd)) AND (!($resize AND (function_exists($gdfunction) OR extension_loaded('imagick'))))) {
  6653. // TCPDF image functions
  6654. $info = TCPDF_IMAGES::$mtd($file);
  6655. if (($ismask === false) AND ($imgmask === false) AND (strpos($file, '__tcpdf_'.$this->file_id.'_imgmask_') === FALSE)
  6656. AND (($info === 'pngalpha') OR (isset($info['trns']) AND !empty($info['trns'])))) {
  6657. return $this->ImagePngAlpha($file, $x, $y, $pixw, $pixh, $w, $h, 'PNG', $link, $align, $resize, $dpi, $palign, $filehash);
  6658. }
  6659. }
  6660. if (($info === false) AND function_exists($gdfunction)) {
  6661. try {
  6662. // GD library
  6663. $img = $gdfunction($file);
  6664. if ($img !== false) {
  6665. if ($resize) {
  6666. $imgr = imagecreatetruecolor($neww, $newh);
  6667. if (($type == 'gif') OR ($type == 'png')) {
  6668. $imgr = TCPDF_IMAGES::setGDImageTransparency($imgr, $img);
  6669. }
  6670. imagecopyresampled($imgr, $img, 0, 0, 0, 0, $neww, $newh, $pixw, $pixh);
  6671. $img = $imgr;
  6672. }
  6673. if (($type == 'gif') OR ($type == 'png')) {
  6674. $info = TCPDF_IMAGES::_toPNG($img, TCPDF_STATIC::getObjFilename('img', $this->file_id));
  6675. } else {
  6676. $info = TCPDF_IMAGES::_toJPEG($img, $this->jpeg_quality, TCPDF_STATIC::getObjFilename('img', $this->file_id));
  6677. }
  6678. }
  6679. } catch(Exception $e) {
  6680. $info = false;
  6681. }
  6682. }
  6683. if (($info === false) AND extension_loaded('imagick')) {
  6684. try {
  6685. // ImageMagick library
  6686. $img = new Imagick();
  6687. if ($type == 'svg') {
  6688. if ($file[0] === '@') {
  6689. // image from string
  6690. $svgimg = substr($file, 1);
  6691. } else {
  6692. // get SVG file content
  6693. $svgimg = TCPDF_STATIC::fileGetContents($file);
  6694. }
  6695. if ($svgimg !== FALSE) {
  6696. // get width and height
  6697. $regs = array();
  6698. if (preg_match('/<svg([^\>]*)>/si', $svgimg, $regs)) {
  6699. $svgtag = $regs[1];
  6700. $tmp = array();
  6701. if (preg_match('/[\s]+width[\s]*=[\s]*"([^"]*)"/si', $svgtag, $tmp)) {
  6702. $ow = $this->getHTMLUnitToUnits($tmp[1], 1, $this->svgunit, false);
  6703. $owu = sprintf('%F', ($ow * $dpi / 72)).$this->pdfunit;
  6704. $svgtag = preg_replace('/[\s]+width[\s]*=[\s]*"[^"]*"/si', ' width="'.$owu.'"', $svgtag, 1);
  6705. } else {
  6706. $ow = $w;
  6707. }
  6708. $tmp = array();
  6709. if (preg_match('/[\s]+height[\s]*=[\s]*"([^"]*)"/si', $svgtag, $tmp)) {
  6710. $oh = $this->getHTMLUnitToUnits($tmp[1], 1, $this->svgunit, false);
  6711. $ohu = sprintf('%F', ($oh * $dpi / 72)).$this->pdfunit;
  6712. $svgtag = preg_replace('/[\s]+height[\s]*=[\s]*"[^"]*"/si', ' height="'.$ohu.'"', $svgtag, 1);
  6713. } else {
  6714. $oh = $h;
  6715. }
  6716. $tmp = array();
  6717. if (!preg_match('/[\s]+viewBox[\s]*=[\s]*"[\s]*([0-9\.]+)[\s]+([0-9\.]+)[\s]+([0-9\.]+)[\s]+([0-9\.]+)[\s]*"/si', $svgtag, $tmp)) {
  6718. $vbw = ($ow * $this->imgscale * $this->k);
  6719. $vbh = ($oh * $this->imgscale * $this->k);
  6720. $vbox = sprintf(' viewBox="0 0 %F %F" ', $vbw, $vbh);
  6721. $svgtag = $vbox.$svgtag;
  6722. }
  6723. $svgimg = preg_replace('/<svg([^\>]*)>/si', '<svg'.$svgtag.'>', $svgimg, 1);
  6724. }
  6725. $img->readImageBlob($svgimg);
  6726. }
  6727. } else {
  6728. $img->readImage($file);
  6729. }
  6730. if ($resize) {
  6731. $img->resizeImage($neww, $newh, 10, 1, false);
  6732. }
  6733. $img->setCompressionQuality($this->jpeg_quality);
  6734. $img->setImageFormat('jpeg');
  6735. $tempname = TCPDF_STATIC::getObjFilename('img', $this->file_id);
  6736. $img->writeImage($tempname);
  6737. $info = TCPDF_IMAGES::_parsejpeg($tempname);
  6738. unlink($tempname);
  6739. $img->destroy();
  6740. } catch(Exception $e) {
  6741. $info = false;
  6742. }
  6743. }
  6744. if ($info === false) {
  6745. // unable to process image
  6746. return;
  6747. }
  6748. TCPDF_STATIC::set_mqr($mqr);
  6749. if ($ismask) {
  6750. // force grayscale
  6751. $info['cs'] = 'DeviceGray';
  6752. }
  6753. if ($imgmask !== false) {
  6754. $info['masked'] = $imgmask;
  6755. }
  6756. if (!empty($exurl)) {
  6757. $info['exurl'] = $exurl;
  6758. }
  6759. // array of alternative images
  6760. $info['altimgs'] = $altimgs;
  6761. // add image to document
  6762. $info['i'] = $this->setImageBuffer($file, $info);
  6763. }
  6764. // set alignment
  6765. $this->img_rb_y = $y + $h;
  6766. // set alignment
  6767. if ($this->rtl) {
  6768. if ($palign == 'L') {
  6769. $ximg = $this->lMargin;
  6770. } elseif ($palign == 'C') {
  6771. $ximg = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
  6772. } elseif ($palign == 'R') {
  6773. $ximg = $this->w - $this->rMargin - $w;
  6774. } else {
  6775. $ximg = $x - $w;
  6776. }
  6777. $this->img_rb_x = $ximg;
  6778. } else {
  6779. if ($palign == 'L') {
  6780. $ximg = $this->lMargin;
  6781. } elseif ($palign == 'C') {
  6782. $ximg = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
  6783. } elseif ($palign == 'R') {
  6784. $ximg = $this->w - $this->rMargin - $w;
  6785. } else {
  6786. $ximg = $x;
  6787. }
  6788. $this->img_rb_x = $ximg + $w;
  6789. }
  6790. if ($ismask OR $hidden) {
  6791. // image is not displayed
  6792. return $info['i'];
  6793. }
  6794. $xkimg = $ximg * $this->k;
  6795. if (!$alt) {
  6796. // only non-alternative immages will be set
  6797. $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']));
  6798. }
  6799. if (!empty($border)) {
  6800. $bx = $this->x;
  6801. $by = $this->y;
  6802. $this->x = $ximg;
  6803. if ($this->rtl) {
  6804. $this->x += $w;
  6805. }
  6806. $this->y = $y;
  6807. $this->Cell($w, $h, '', $border, 0, '', 0, '', 0, true);
  6808. $this->x = $bx;
  6809. $this->y = $by;
  6810. }
  6811. if ($link) {
  6812. $this->Link($ximg, $y, $w, $h, $link, 0);
  6813. }
  6814. // set pointer to align the next text/objects
  6815. switch($align) {
  6816. case 'T': {
  6817. $this->y = $y;
  6818. $this->x = $this->img_rb_x;
  6819. break;
  6820. }
  6821. case 'M': {
  6822. $this->y = $y + round($h/2);
  6823. $this->x = $this->img_rb_x;
  6824. break;
  6825. }
  6826. case 'B': {
  6827. $this->y = $this->img_rb_y;
  6828. $this->x = $this->img_rb_x;
  6829. break;
  6830. }
  6831. case 'N': {
  6832. $this->SetY($this->img_rb_y);
  6833. break;
  6834. }
  6835. default:{
  6836. break;
  6837. }
  6838. }
  6839. $this->endlinex = $this->img_rb_x;
  6840. if ($this->inxobj) {
  6841. // we are inside an XObject template
  6842. $this->xobjects[$this->xobjid]['images'][] = $info['i'];
  6843. }
  6844. return $info['i'];
  6845. }
  6846. /**
  6847. * Extract info from a PNG image with alpha channel using the Imagick or GD library.
  6848. * @param $file (string) Name of the file containing the image.
  6849. * @param $x (float) Abscissa of the upper-left corner.
  6850. * @param $y (float) Ordinate of the upper-left corner.
  6851. * @param $wpx (float) Original width of the image in pixels.
  6852. * @param $hpx (float) original height of the image in pixels.
  6853. * @param $w (float) Width of the image in the page. If not specified or equal to zero, it is automatically calculated.
  6854. * @param $h (float) Height of the image in the page. If not specified or equal to zero, it is automatically calculated.
  6855. * @param $type (string) 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.
  6856. * @param $link (mixed) URL or identifier returned by AddLink().
  6857. * @param $align (string) 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>
  6858. * @param $resize (boolean) If true resize (reduce) the image to fit $w and $h (requires GD library).
  6859. * @param $dpi (int) dot-per-inch resolution used on resize
  6860. * @param $palign (string) 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>
  6861. * @param $filehash (string) File hash used to build unique file names.
  6862. * @author Nicola Asuni
  6863. * @protected
  6864. * @since 4.3.007 (2008-12-04)
  6865. * @see Image()
  6866. */
  6867. protected function ImagePngAlpha($file, $x, $y, $wpx, $hpx, $w, $h, $type, $link, $align, $resize, $dpi, $palign, $filehash='') {
  6868. // create temp images
  6869. if (empty($filehash)) {
  6870. $filehash = md5($file);
  6871. }
  6872. // create temp image file (without alpha channel)
  6873. $tempfile_plain = K_PATH_CACHE.'__tcpdf_'.$this->file_id.'_imgmask_plain_'.$filehash;
  6874. // create temp alpha file
  6875. $tempfile_alpha = K_PATH_CACHE.'__tcpdf_'.$this->file_id.'_imgmask_alpha_'.$filehash;
  6876. $parsed = false;
  6877. $parse_error = '';
  6878. // ImageMagick extension
  6879. if (($parsed === false) AND extension_loaded('imagick')) {
  6880. try {
  6881. // ImageMagick library
  6882. $img = new Imagick();
  6883. $img->readImage($file);
  6884. // clone image object
  6885. $imga = TCPDF_STATIC::objclone($img);
  6886. // extract alpha channel
  6887. if (method_exists($img, 'setImageAlphaChannel') AND defined('Imagick::ALPHACHANNEL_EXTRACT')) {
  6888. $img->setImageAlphaChannel(Imagick::ALPHACHANNEL_EXTRACT);
  6889. } else {
  6890. $img->separateImageChannel(8); // 8 = (imagick::CHANNEL_ALPHA | imagick::CHANNEL_OPACITY | imagick::CHANNEL_MATTE);
  6891. $img->negateImage(true);
  6892. }
  6893. $img->setImageFormat('png');
  6894. $img->writeImage($tempfile_alpha);
  6895. // remove alpha channel
  6896. if (method_exists($imga, 'setImageMatte')) {
  6897. $imga->setImageMatte(false);
  6898. } else {
  6899. $imga->separateImageChannel(39); // 39 = (imagick::CHANNEL_ALL & ~(imagick::CHANNEL_ALPHA | imagick::CHANNEL_OPACITY | imagick::CHANNEL_MATTE));
  6900. }
  6901. $imga->setImageFormat('png');
  6902. $imga->writeImage($tempfile_plain);
  6903. $parsed = true;
  6904. } catch (Exception $e) {
  6905. // Imagemagick fails, try with GD
  6906. $parse_error = 'Imagick library error: '.$e->getMessage();
  6907. }
  6908. }
  6909. // GD extension
  6910. if (($parsed === false) AND function_exists('imagecreatefrompng')) {
  6911. try {
  6912. // generate images
  6913. $img = imagecreatefrompng($file);
  6914. $imgalpha = imagecreate($wpx, $hpx);
  6915. // generate gray scale palette (0 -> 255)
  6916. for ($c = 0; $c < 256; ++$c) {
  6917. ImageColorAllocate($imgalpha, $c, $c, $c);
  6918. }
  6919. // extract alpha channel
  6920. for ($xpx = 0; $xpx < $wpx; ++$xpx) {
  6921. for ($ypx = 0; $ypx < $hpx; ++$ypx) {
  6922. $color = imagecolorat($img, $xpx, $ypx);
  6923. // get and correct gamma color
  6924. $alpha = $this->getGDgamma($img, $color);
  6925. imagesetpixel($imgalpha, $xpx, $ypx, $alpha);
  6926. }
  6927. }
  6928. imagepng($imgalpha, $tempfile_alpha);
  6929. imagedestroy($imgalpha);
  6930. // extract image without alpha channel
  6931. $imgplain = imagecreatetruecolor($wpx, $hpx);
  6932. imagecopy($imgplain, $img, 0, 0, 0, 0, $wpx, $hpx);
  6933. imagepng($imgplain, $tempfile_plain);
  6934. imagedestroy($imgplain);
  6935. $parsed = true;
  6936. } catch (Exception $e) {
  6937. // GD fails
  6938. $parse_error = 'GD library error: '.$e->getMessage();
  6939. }
  6940. }
  6941. if ($parsed === false) {
  6942. if (empty($parse_error)) {
  6943. $this->Error('TCPDF requires the Imagick or GD extension to handle PNG images with alpha channel.');
  6944. } else {
  6945. $this->Error($parse_error);
  6946. }
  6947. }
  6948. // embed mask image
  6949. $imgmask = $this->Image($tempfile_alpha, $x, $y, $w, $h, 'PNG', '', '', $resize, $dpi, '', true, false);
  6950. // embed image, masked with previously embedded mask
  6951. $this->Image($tempfile_plain, $x, $y, $w, $h, $type, $link, $align, $resize, $dpi, $palign, false, $imgmask);
  6952. }
  6953. /**
  6954. * Get the GD-corrected PNG gamma value from alpha color
  6955. * @param $img (int) GD image Resource ID.
  6956. * @param $c (int) alpha color
  6957. * @protected
  6958. * @since 4.3.007 (2008-12-04)
  6959. */
  6960. protected function getGDgamma($img, $c) {
  6961. if (!isset($this->gdgammacache['#'.$c])) {
  6962. $colors = imagecolorsforindex($img, $c);
  6963. // GD alpha is only 7 bit (0 -> 127)
  6964. $this->gdgammacache['#'.$c] = (((127 - $colors['alpha']) / 127) * 255);
  6965. // correct gamma
  6966. $this->gdgammacache['#'.$c] = (pow(($this->gdgammacache['#'.$c] / 255), 2.2) * 255);
  6967. // store the latest values on cache to improve performances
  6968. if (count($this->gdgammacache) > 8) {
  6969. // remove one element from the cache array
  6970. array_shift($this->gdgammacache);
  6971. }
  6972. }
  6973. return $this->gdgammacache['#'.$c];
  6974. }
  6975. /**
  6976. * Performs a line break.
  6977. * The current abscissa goes back to the left margin and the ordinate increases by the amount passed in parameter.
  6978. * @param $h (float) The height of the break. By default, the value equals the height of the last printed cell.
  6979. * @param $cell (boolean) if true add the current left (or right o for RTL) padding to the X coordinate
  6980. * @public
  6981. * @since 1.0
  6982. * @see Cell()
  6983. */
  6984. public function Ln($h='', $cell=false) {
  6985. 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'])) {
  6986. // revove vertical space from the top of the column
  6987. return;
  6988. }
  6989. if ($cell) {
  6990. if ($this->rtl) {
  6991. $cellpadding = $this->cell_padding['R'];
  6992. } else {
  6993. $cellpadding = $this->cell_padding['L'];
  6994. }
  6995. } else {
  6996. $cellpadding = 0;
  6997. }
  6998. if ($this->rtl) {
  6999. $this->x = $this->w - $this->rMargin - $cellpadding;
  7000. } else {
  7001. $this->x = $this->lMargin + $cellpadding;
  7002. }
  7003. if (is_string($h)) {
  7004. $h = $this->lasth;
  7005. }
  7006. $this->y += $h;
  7007. $this->newline = true;
  7008. }
  7009. /**
  7010. * Returns the relative X value of current position.
  7011. * The value is relative to the left border for LTR languages and to the right border for RTL languages.
  7012. * @return float
  7013. * @public
  7014. * @since 1.2
  7015. * @see SetX(), GetY(), SetY()
  7016. */
  7017. public function GetX() {
  7018. //Get x position
  7019. if ($this->rtl) {
  7020. return ($this->w - $this->x);
  7021. } else {
  7022. return $this->x;
  7023. }
  7024. }
  7025. /**
  7026. * Returns the absolute X value of current position.
  7027. * @return float
  7028. * @public
  7029. * @since 1.2
  7030. * @see SetX(), GetY(), SetY()
  7031. */
  7032. public function GetAbsX() {
  7033. return $this->x;
  7034. }
  7035. /**
  7036. * Returns the ordinate of the current position.
  7037. * @return float
  7038. * @public
  7039. * @since 1.0
  7040. * @see SetY(), GetX(), SetX()
  7041. */
  7042. public function GetY() {
  7043. return $this->y;
  7044. }
  7045. /**
  7046. * Defines the abscissa of the current position.
  7047. * If the passed value is negative, it is relative to the right of the page (or left if language is RTL).
  7048. * @param $x (float) The value of the abscissa in user units.
  7049. * @param $rtloff (boolean) if true always uses the page top-left corner as origin of axis.
  7050. * @public
  7051. * @since 1.2
  7052. * @see GetX(), GetY(), SetY(), SetXY()
  7053. */
  7054. public function SetX($x, $rtloff=false) {
  7055. $x = floatval($x);
  7056. if (!$rtloff AND $this->rtl) {
  7057. if ($x >= 0) {
  7058. $this->x = $this->w - $x;
  7059. } else {
  7060. $this->x = abs($x);
  7061. }
  7062. } else {
  7063. if ($x >= 0) {
  7064. $this->x = $x;
  7065. } else {
  7066. $this->x = $this->w + $x;
  7067. }
  7068. }
  7069. if ($this->x < 0) {
  7070. $this->x = 0;
  7071. }
  7072. if ($this->x > $this->w) {
  7073. $this->x = $this->w;
  7074. }
  7075. }
  7076. /**
  7077. * Moves the current abscissa back to the left margin and sets the ordinate.
  7078. * If the passed value is negative, it is relative to the bottom of the page.
  7079. * @param $y (float) The value of the ordinate in user units.
  7080. * @param $resetx (bool) if true (default) reset the X position.
  7081. * @param $rtloff (boolean) if true always uses the page top-left corner as origin of axis.
  7082. * @public
  7083. * @since 1.0
  7084. * @see GetX(), GetY(), SetY(), SetXY()
  7085. */
  7086. public function SetY($y, $resetx=true, $rtloff=false) {
  7087. $y = floatval($y);
  7088. if ($resetx) {
  7089. //reset x
  7090. if (!$rtloff AND $this->rtl) {
  7091. $this->x = $this->w - $this->rMargin;
  7092. } else {
  7093. $this->x = $this->lMargin;
  7094. }
  7095. }
  7096. if ($y >= 0) {
  7097. $this->y = $y;
  7098. } else {
  7099. $this->y = $this->h + $y;
  7100. }
  7101. if ($this->y < 0) {
  7102. $this->y = 0;
  7103. }
  7104. if ($this->y > $this->h) {
  7105. $this->y = $this->h;
  7106. }
  7107. }
  7108. /**
  7109. * Defines the abscissa and ordinate of the current position.
  7110. * If the passed values are negative, they are relative respectively to the right and bottom of the page.
  7111. * @param $x (float) The value of the abscissa.
  7112. * @param $y (float) The value of the ordinate.
  7113. * @param $rtloff (boolean) if true always uses the page top-left corner as origin of axis.
  7114. * @public
  7115. * @since 1.2
  7116. * @see SetX(), SetY()
  7117. */
  7118. public function SetXY($x, $y, $rtloff=false) {
  7119. $this->SetY($y, false, $rtloff);
  7120. $this->SetX($x, $rtloff);
  7121. }
  7122. /**
  7123. * Set the absolute X coordinate of the current pointer.
  7124. * @param $x (float) The value of the abscissa in user units.
  7125. * @public
  7126. * @since 5.9.186 (2012-09-13)
  7127. * @see setAbsX(), setAbsY(), SetAbsXY()
  7128. */
  7129. public function SetAbsX($x) {
  7130. $this->x = floatval($x);
  7131. }
  7132. /**
  7133. * Set the absolute Y coordinate of the current pointer.
  7134. * @param $y (float) (float) The value of the ordinate in user units.
  7135. * @public
  7136. * @since 5.9.186 (2012-09-13)
  7137. * @see setAbsX(), setAbsY(), SetAbsXY()
  7138. */
  7139. public function SetAbsY($y) {
  7140. $this->y = floatval($y);
  7141. }
  7142. /**
  7143. * Set the absolute X and Y coordinates of the current pointer.
  7144. * @param $x (float) The value of the abscissa in user units.
  7145. * @param $y (float) (float) The value of the ordinate in user units.
  7146. * @public
  7147. * @since 5.9.186 (2012-09-13)
  7148. * @see setAbsX(), setAbsY(), SetAbsXY()
  7149. */
  7150. public function SetAbsXY($x, $y) {
  7151. $this->SetAbsX($x);
  7152. $this->SetAbsY($y);
  7153. }
  7154. /**
  7155. * Send the document to a given destination: string, local file or browser.
  7156. * In the last case, the plug-in may be used (if present) or a download ("Save as" dialog box) may be forced.<br />
  7157. * The method first calls Close() if necessary to terminate the document.
  7158. * @param $name (string) The name of the file when saved. Note that special characters are removed and blanks characters are replaced with the underscore character.
  7159. * @param $dest (string) 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>
  7160. * @return string
  7161. * @public
  7162. * @since 1.0
  7163. * @see Close()
  7164. */
  7165. public function Output($name='doc.pdf', $dest='I') {
  7166. //Output PDF to some destination
  7167. //Finish document if necessary
  7168. if ($this->state < 3) {
  7169. $this->Close();
  7170. }
  7171. //Normalize parameters
  7172. if (is_bool($dest)) {
  7173. $dest = $dest ? 'D' : 'F';
  7174. }
  7175. $dest = strtoupper($dest);
  7176. if ($dest[0] != 'F') {
  7177. $name = preg_replace('/[\s]+/', '_', $name);
  7178. $name = preg_replace('/[^a-zA-Z0-9_\.-]/', '', $name);
  7179. }
  7180. if ($this->sign) {
  7181. // *** apply digital signature to the document ***
  7182. // get the document content
  7183. $pdfdoc = $this->getBuffer();
  7184. // remove last newline
  7185. $pdfdoc = substr($pdfdoc, 0, -1);
  7186. // remove filler space
  7187. $byterange_string_len = strlen(TCPDF_STATIC::$byterange_string);
  7188. // define the ByteRange
  7189. $byte_range = array();
  7190. $byte_range[0] = 0;
  7191. $byte_range[1] = strpos($pdfdoc, TCPDF_STATIC::$byterange_string) + $byterange_string_len + 10;
  7192. $byte_range[2] = $byte_range[1] + $this->signature_max_length + 2;
  7193. $byte_range[3] = strlen($pdfdoc) - $byte_range[2];
  7194. $pdfdoc = substr($pdfdoc, 0, $byte_range[1]).substr($pdfdoc, $byte_range[2]);
  7195. // replace the ByteRange
  7196. $byterange = sprintf('/ByteRange[0 %u %u %u]', $byte_range[1], $byte_range[2], $byte_range[3]);
  7197. $byterange .= str_repeat(' ', ($byterange_string_len - strlen($byterange)));
  7198. $pdfdoc = str_replace(TCPDF_STATIC::$byterange_string, $byterange, $pdfdoc);
  7199. // write the document to a temporary folder
  7200. $tempdoc = TCPDF_STATIC::getObjFilename('doc', $this->file_id);
  7201. $f = TCPDF_STATIC::fopenLocal($tempdoc, 'wb');
  7202. if (!$f) {
  7203. $this->Error('Unable to create temporary file: '.$tempdoc);
  7204. }
  7205. $pdfdoc_length = strlen($pdfdoc);
  7206. fwrite($f, $pdfdoc, $pdfdoc_length);
  7207. fclose($f);
  7208. // get digital signature via openssl library
  7209. $tempsign = TCPDF_STATIC::getObjFilename('sig', $this->file_id);
  7210. if (empty($this->signature_data['extracerts'])) {
  7211. openssl_pkcs7_sign($tempdoc, $tempsign, $this->signature_data['signcert'], array($this->signature_data['privkey'], $this->signature_data['password']), array(), PKCS7_BINARY | PKCS7_DETACHED);
  7212. } else {
  7213. 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']);
  7214. }
  7215. // read signature
  7216. $signature = file_get_contents($tempsign);
  7217. // extract signature
  7218. $signature = substr($signature, $pdfdoc_length);
  7219. $signature = substr($signature, (strpos($signature, "%%EOF\n\n------") + 13));
  7220. $tmparr = explode("\n\n", $signature);
  7221. $signature = $tmparr[1];
  7222. // decode signature
  7223. $signature = base64_decode(trim($signature));
  7224. // add TSA timestamp to signature
  7225. $signature = $this->applyTSA($signature);
  7226. // convert signature to hex
  7227. $signature = current(unpack('H*', $signature));
  7228. $signature = str_pad($signature, $this->signature_max_length, '0');
  7229. // Add signature to the document
  7230. $this->buffer = substr($pdfdoc, 0, $byte_range[1]).'<'.$signature.'>'.substr($pdfdoc, $byte_range[1]);
  7231. $this->bufferlen = strlen($this->buffer);
  7232. }
  7233. switch($dest) {
  7234. case 'I': {
  7235. // Send PDF to the standard output
  7236. if (ob_get_contents()) {
  7237. $this->Error('Some data has already been output, can\'t send PDF file');
  7238. }
  7239. if (php_sapi_name() != 'cli') {
  7240. // send output to a browser
  7241. header('Content-Type: application/pdf');
  7242. if (headers_sent()) {
  7243. $this->Error('Some data has already been output to browser, can\'t send PDF file');
  7244. }
  7245. header('Cache-Control: private, must-revalidate, post-check=0, pre-check=0, max-age=1');
  7246. //header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
  7247. header('Pragma: public');
  7248. header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  7249. header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  7250. header('Content-Disposition: inline; filename="'.basename($name).'"');
  7251. TCPDF_STATIC::sendOutputData($this->getBuffer(), $this->bufferlen);
  7252. } else {
  7253. echo $this->getBuffer();
  7254. }
  7255. break;
  7256. }
  7257. case 'D': {
  7258. // download PDF as file
  7259. if (ob_get_contents()) {
  7260. $this->Error('Some data has already been output, can\'t send PDF file');
  7261. }
  7262. header('Content-Description: File Transfer');
  7263. if (headers_sent()) {
  7264. $this->Error('Some data has already been output to browser, can\'t send PDF file');
  7265. }
  7266. header('Cache-Control: private, must-revalidate, post-check=0, pre-check=0, max-age=1');
  7267. //header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
  7268. header('Pragma: public');
  7269. header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  7270. header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  7271. // force download dialog
  7272. if (strpos(php_sapi_name(), 'cgi') === false) {
  7273. header('Content-Type: application/force-download');
  7274. header('Content-Type: application/octet-stream', false);
  7275. header('Content-Type: application/download', false);
  7276. header('Content-Type: application/pdf', false);
  7277. } else {
  7278. header('Content-Type: application/pdf');
  7279. }
  7280. // use the Content-Disposition header to supply a recommended filename
  7281. header('Content-Disposition: attachment; filename="'.basename($name).'"');
  7282. header('Content-Transfer-Encoding: binary');
  7283. TCPDF_STATIC::sendOutputData($this->getBuffer(), $this->bufferlen);
  7284. break;
  7285. }
  7286. case 'F':
  7287. case 'FI':
  7288. case 'FD': {
  7289. // save PDF to a local file
  7290. $f = TCPDF_STATIC::fopenLocal($name, 'wb');
  7291. if (!$f) {
  7292. $this->Error('Unable to create output file: '.$name);
  7293. }
  7294. fwrite($f, $this->getBuffer(), $this->bufferlen);
  7295. fclose($f);
  7296. if ($dest == 'FI') {
  7297. // send headers to browser
  7298. header('Content-Type: application/pdf');
  7299. header('Cache-Control: private, must-revalidate, post-check=0, pre-check=0, max-age=1');
  7300. //header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
  7301. header('Pragma: public');
  7302. header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  7303. header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  7304. header('Content-Disposition: inline; filename="'.basename($name).'"');
  7305. TCPDF_STATIC::sendOutputData(file_get_contents($name), filesize($name));
  7306. } elseif ($dest == 'FD') {
  7307. // send headers to browser
  7308. if (ob_get_contents()) {
  7309. $this->Error('Some data has already been output, can\'t send PDF file');
  7310. }
  7311. header('Content-Description: File Transfer');
  7312. if (headers_sent()) {
  7313. $this->Error('Some data has already been output to browser, can\'t send PDF file');
  7314. }
  7315. header('Cache-Control: private, must-revalidate, post-check=0, pre-check=0, max-age=1');
  7316. header('Pragma: public');
  7317. header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  7318. header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  7319. // force download dialog
  7320. if (strpos(php_sapi_name(), 'cgi') === false) {
  7321. header('Content-Type: application/force-download');
  7322. header('Content-Type: application/octet-stream', false);
  7323. header('Content-Type: application/download', false);
  7324. header('Content-Type: application/pdf', false);
  7325. } else {
  7326. header('Content-Type: application/pdf');
  7327. }
  7328. // use the Content-Disposition header to supply a recommended filename
  7329. header('Content-Disposition: attachment; filename="'.basename($name).'"');
  7330. header('Content-Transfer-Encoding: binary');
  7331. TCPDF_STATIC::sendOutputData(file_get_contents($name), filesize($name));
  7332. }
  7333. break;
  7334. }
  7335. case 'E': {
  7336. // return PDF as base64 mime multi-part email attachment (RFC 2045)
  7337. $retval = 'Content-Type: application/pdf;'."\r\n";
  7338. $retval .= ' name="'.$name.'"'."\r\n";
  7339. $retval .= 'Content-Transfer-Encoding: base64'."\r\n";
  7340. $retval .= 'Content-Disposition: attachment;'."\r\n";
  7341. $retval .= ' filename="'.$name.'"'."\r\n\r\n";
  7342. $retval .= chunk_split(base64_encode($this->getBuffer()), 76, "\r\n");
  7343. return $retval;
  7344. }
  7345. case 'S': {
  7346. // returns PDF as a string
  7347. return $this->getBuffer();
  7348. }
  7349. default: {
  7350. $this->Error('Incorrect output destination: '.$dest);
  7351. }
  7352. }
  7353. return '';
  7354. }
  7355. /**
  7356. * Unset all class variables except the following critical variables.
  7357. * @param $destroyall (boolean) if true destroys all class variables, otherwise preserves critical variables.
  7358. * @param $preserve_objcopy (boolean) if true preserves the objcopy variable
  7359. * @public
  7360. * @since 4.5.016 (2009-02-24)
  7361. */
  7362. public function _destroy($destroyall=false, $preserve_objcopy=false) {
  7363. if ($destroyall AND !$preserve_objcopy) {
  7364. // remove all temporary files
  7365. $tmpfiles = glob(K_PATH_CACHE.'__tcpdf_'.$this->file_id.'_*');
  7366. if (!empty($tmpfiles)) {
  7367. array_map('unlink', $tmpfiles);
  7368. }
  7369. }
  7370. $preserve = array(
  7371. 'file_id',
  7372. 'internal_encoding',
  7373. 'state',
  7374. 'bufferlen',
  7375. 'buffer',
  7376. 'cached_files',
  7377. 'sign',
  7378. 'signature_data',
  7379. 'signature_max_length',
  7380. 'byterange_string',
  7381. 'tsa_timestamp',
  7382. 'tsa_data'
  7383. );
  7384. foreach (array_keys(get_object_vars($this)) as $val) {
  7385. if ($destroyall OR !in_array($val, $preserve)) {
  7386. if ((!$preserve_objcopy OR ($val != 'objcopy')) AND ($val != 'file_id') AND isset($this->$val)) {
  7387. unset($this->$val);
  7388. }
  7389. }
  7390. }
  7391. }
  7392. /**
  7393. * Check for locale-related bug
  7394. * @protected
  7395. */
  7396. protected function _dochecks() {
  7397. //Check for locale-related bug
  7398. if (1.1 == 1) {
  7399. $this->Error('Don\'t alter the locale before including class file');
  7400. }
  7401. //Check for decimal separator
  7402. if (sprintf('%.1F', 1.0) != '1.0') {
  7403. setlocale(LC_NUMERIC, 'C');
  7404. }
  7405. }
  7406. /**
  7407. * Return an array containing variations for the basic page number alias.
  7408. * @param $a (string) Base alias.
  7409. * @return array of page number aliases
  7410. * @protected
  7411. */
  7412. protected function getInternalPageNumberAliases($a= '') {
  7413. $alias = array();
  7414. // build array of Unicode + ASCII variants (the order is important)
  7415. $alias = array('u' => array(), 'a' => array());
  7416. $u = '{'.$a.'}';
  7417. $alias['u'][] = TCPDF_STATIC::_escape($u);
  7418. if ($this->isunicode) {
  7419. $alias['u'][] = TCPDF_STATIC::_escape(TCPDF_FONTS::UTF8ToLatin1($u, $this->isunicode, $this->CurrentFont));
  7420. $alias['u'][] = TCPDF_STATIC::_escape(TCPDF_FONTS::utf8StrRev($u, false, $this->tmprtl, $this->isunicode, $this->CurrentFont));
  7421. $alias['a'][] = TCPDF_STATIC::_escape(TCPDF_FONTS::UTF8ToLatin1($a, $this->isunicode, $this->CurrentFont));
  7422. $alias['a'][] = TCPDF_STATIC::_escape(TCPDF_FONTS::utf8StrRev($a, false, $this->tmprtl, $this->isunicode, $this->CurrentFont));
  7423. }
  7424. $alias['a'][] = TCPDF_STATIC::_escape($a);
  7425. return $alias;
  7426. }
  7427. /**
  7428. * Return an array containing all internal page aliases.
  7429. * @return array of page number aliases
  7430. * @protected
  7431. */
  7432. protected function getAllInternalPageNumberAliases() {
  7433. $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);
  7434. $pnalias = array();
  7435. foreach($basic_alias as $k => $a) {
  7436. $pnalias[$k] = $this->getInternalPageNumberAliases($a);
  7437. }
  7438. return $pnalias;
  7439. }
  7440. /**
  7441. * Replace right shift page number aliases with spaces to correct right alignment.
  7442. * This works perfectly only when using monospaced fonts.
  7443. * @param $page (string) Page content.
  7444. * @param $aliases (array) Array of page aliases.
  7445. * @param $diff (int) initial difference to add.
  7446. * @return replaced page content.
  7447. * @protected
  7448. */
  7449. protected function replaceRightShiftPageNumAliases($page, $aliases, $diff) {
  7450. foreach ($aliases as $type => $alias) {
  7451. foreach ($alias as $a) {
  7452. // find position of compensation factor
  7453. $startnum = (strpos($a, ':') + 1);
  7454. $a = substr($a, 0, $startnum);
  7455. if (($pos = strpos($page, $a)) !== false) {
  7456. // end of alias
  7457. $endnum = strpos($page, '}', $pos);
  7458. // string to be replaced
  7459. $aa = substr($page, $pos, ($endnum - $pos + 1));
  7460. // get compensation factor
  7461. $ratio = substr($page, ($pos + $startnum), ($endnum - $pos - $startnum));
  7462. $ratio = preg_replace('/[^0-9\.]/', '', $ratio);
  7463. $ratio = floatval($ratio);
  7464. if ($type == 'u') {
  7465. $chrdiff = floor(($diff + 12) * $ratio);
  7466. $shift = str_repeat(' ', $chrdiff);
  7467. $shift = TCPDF_FONTS::UTF8ToUTF16BE($shift, false, $this->isunicode, $this->CurrentFont);
  7468. } else {
  7469. $chrdiff = floor(($diff + 11) * $ratio);
  7470. $shift = str_repeat(' ', $chrdiff);
  7471. }
  7472. $page = str_replace($aa, $shift, $page);
  7473. }
  7474. }
  7475. }
  7476. return $page;
  7477. }
  7478. /**
  7479. * Set page boxes to be included on page descriptions.
  7480. * @param $boxes (array) Array of page boxes to set on document: ('MediaBox', 'CropBox', 'BleedBox', 'TrimBox', 'ArtBox').
  7481. * @protected
  7482. */
  7483. protected function setPageBoxTypes($boxes) {
  7484. $this->page_boxes = array();
  7485. foreach ($boxes as $box) {
  7486. if (in_array($box, TCPDF_STATIC::$pageboxes)) {
  7487. $this->page_boxes[] = $box;
  7488. }
  7489. }
  7490. }
  7491. /**
  7492. * Output pages (and replace page number aliases).
  7493. * @protected
  7494. */
  7495. protected function _putpages() {
  7496. $filter = ($this->compress) ? '/Filter /FlateDecode ' : '';
  7497. // get internal aliases for page numbers
  7498. $pnalias = $this->getAllInternalPageNumberAliases();
  7499. $num_pages = $this->numpages;
  7500. $ptpa = TCPDF_STATIC::formatPageNumber(($this->starting_page_number + $num_pages - 1));
  7501. $ptpu = TCPDF_FONTS::UTF8ToUTF16BE($ptpa, false, $this->isunicode, $this->CurrentFont);
  7502. $ptp_num_chars = $this->GetNumChars($ptpa);
  7503. $pagegroupnum = 0;
  7504. $groupnum = 0;
  7505. $ptgu = 1;
  7506. $ptga = 1;
  7507. $ptg_num_chars = 1;
  7508. for ($n = 1; $n <= $num_pages; ++$n) {
  7509. // get current page
  7510. $temppage = $this->getPageBuffer($n);
  7511. $pagelen = strlen($temppage);
  7512. // set replacements for total pages number
  7513. $pnpa = TCPDF_STATIC::formatPageNumber(($this->starting_page_number + $n - 1));
  7514. $pnpu = TCPDF_FONTS::UTF8ToUTF16BE($pnpa, false, $this->isunicode, $this->CurrentFont);
  7515. $pnp_num_chars = $this->GetNumChars($pnpa);
  7516. $pdiff = 0; // difference used for right shift alignment of page numbers
  7517. $gdiff = 0; // difference used for right shift alignment of page group numbers
  7518. if (!empty($this->pagegroups)) {
  7519. if (isset($this->newpagegroup[$n])) {
  7520. $pagegroupnum = 0;
  7521. ++$groupnum;
  7522. $ptga = TCPDF_STATIC::formatPageNumber($this->pagegroups[$groupnum]);
  7523. $ptgu = TCPDF_FONTS::UTF8ToUTF16BE($ptga, false, $this->isunicode, $this->CurrentFont);
  7524. $ptg_num_chars = $this->GetNumChars($ptga);
  7525. }
  7526. ++$pagegroupnum;
  7527. $pnga = TCPDF_STATIC::formatPageNumber($pagegroupnum);
  7528. $pngu = TCPDF_FONTS::UTF8ToUTF16BE($pnga, false, $this->isunicode, $this->CurrentFont);
  7529. $png_num_chars = $this->GetNumChars($pnga);
  7530. // replace page numbers
  7531. $replace = array();
  7532. $replace[] = array($ptgu, $ptg_num_chars, 9, $pnalias[2]['u']);
  7533. $replace[] = array($ptga, $ptg_num_chars, 7, $pnalias[2]['a']);
  7534. $replace[] = array($pngu, $png_num_chars, 9, $pnalias[3]['u']);
  7535. $replace[] = array($pnga, $png_num_chars, 7, $pnalias[3]['a']);
  7536. list($temppage, $gdiff) = TCPDF_STATIC::replacePageNumAliases($temppage, $replace, $gdiff);
  7537. }
  7538. // replace page numbers
  7539. $replace = array();
  7540. $replace[] = array($ptpu, $ptp_num_chars, 9, $pnalias[0]['u']);
  7541. $replace[] = array($ptpa, $ptp_num_chars, 7, $pnalias[0]['a']);
  7542. $replace[] = array($pnpu, $pnp_num_chars, 9, $pnalias[1]['u']);
  7543. $replace[] = array($pnpa, $pnp_num_chars, 7, $pnalias[1]['a']);
  7544. list($temppage, $pdiff) = TCPDF_STATIC::replacePageNumAliases($temppage, $replace, $pdiff);
  7545. // replace right shift alias
  7546. $temppage = $this->replaceRightShiftPageNumAliases($temppage, $pnalias[4], max($pdiff, $gdiff));
  7547. // replace EPS marker
  7548. $temppage = str_replace($this->epsmarker, '', $temppage);
  7549. //Page
  7550. $this->page_obj_id[$n] = $this->_newobj();
  7551. $out = '<<';
  7552. $out .= ' /Type /Page';
  7553. $out .= ' /Parent 1 0 R';
  7554. if (empty($this->signature_data['approval']) OR ($this->signature_data['approval'] != 'A')) {
  7555. $out .= ' /LastModified '.$this->_datestring(0, $this->doc_modification_timestamp);
  7556. }
  7557. $out .= ' /Resources 2 0 R';
  7558. foreach ($this->page_boxes as $box) {
  7559. $out .= ' /'.$box;
  7560. $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']);
  7561. }
  7562. if (isset($this->pagedim[$n]['BoxColorInfo']) AND !empty($this->pagedim[$n]['BoxColorInfo'])) {
  7563. $out .= ' /BoxColorInfo <<';
  7564. foreach ($this->page_boxes as $box) {
  7565. if (isset($this->pagedim[$n]['BoxColorInfo'][$box])) {
  7566. $out .= ' /'.$box.' <<';
  7567. if (isset($this->pagedim[$n]['BoxColorInfo'][$box]['C'])) {
  7568. $color = $this->pagedim[$n]['BoxColorInfo'][$box]['C'];
  7569. $out .= ' /C [';
  7570. $out .= sprintf(' %F %F %F', ($color[0] / 255), ($color[1] / 255), ($color[2] / 255));
  7571. $out .= ' ]';
  7572. }
  7573. if (isset($this->pagedim[$n]['BoxColorInfo'][$box]['W'])) {
  7574. $out .= ' /W '.($this->pagedim[$n]['BoxColorInfo'][$box]['W'] * $this->k);
  7575. }
  7576. if (isset($this->pagedim[$n]['BoxColorInfo'][$box]['S'])) {
  7577. $out .= ' /S /'.$this->pagedim[$n]['BoxColorInfo'][$box]['S'];
  7578. }
  7579. if (isset($this->pagedim[$n]['BoxColorInfo'][$box]['D'])) {
  7580. $dashes = $this->pagedim[$n]['BoxColorInfo'][$box]['D'];
  7581. $out .= ' /D [';
  7582. foreach ($dashes as $dash) {
  7583. $out .= sprintf(' %F', ($dash * $this->k));
  7584. }
  7585. $out .= ' ]';
  7586. }
  7587. $out .= ' >>';
  7588. }
  7589. }
  7590. $out .= ' >>';
  7591. }
  7592. $out .= ' /Contents '.($this->n + 1).' 0 R';
  7593. $out .= ' /Rotate '.$this->pagedim[$n]['Rotate'];
  7594. if (!$this->pdfa_mode) {
  7595. $out .= ' /Group << /Type /Group /S /Transparency /CS /DeviceRGB >>';
  7596. }
  7597. if (isset($this->pagedim[$n]['trans']) AND !empty($this->pagedim[$n]['trans'])) {
  7598. // page transitions
  7599. if (isset($this->pagedim[$n]['trans']['Dur'])) {
  7600. $out .= ' /Dur '.$this->pagedim[$n]['trans']['Dur'];
  7601. }
  7602. $out .= ' /Trans <<';
  7603. $out .= ' /Type /Trans';
  7604. if (isset($this->pagedim[$n]['trans']['S'])) {
  7605. $out .= ' /S /'.$this->pagedim[$n]['trans']['S'];
  7606. }
  7607. if (isset($this->pagedim[$n]['trans']['D'])) {
  7608. $out .= ' /D '.$this->pagedim[$n]['trans']['D'];
  7609. }
  7610. if (isset($this->pagedim[$n]['trans']['Dm'])) {
  7611. $out .= ' /Dm /'.$this->pagedim[$n]['trans']['Dm'];
  7612. }
  7613. if (isset($this->pagedim[$n]['trans']['M'])) {
  7614. $out .= ' /M /'.$this->pagedim[$n]['trans']['M'];
  7615. }
  7616. if (isset($this->pagedim[$n]['trans']['Di'])) {
  7617. $out .= ' /Di '.$this->pagedim[$n]['trans']['Di'];
  7618. }
  7619. if (isset($this->pagedim[$n]['trans']['SS'])) {
  7620. $out .= ' /SS '.$this->pagedim[$n]['trans']['SS'];
  7621. }
  7622. if (isset($this->pagedim[$n]['trans']['B'])) {
  7623. $out .= ' /B '.$this->pagedim[$n]['trans']['B'];
  7624. }
  7625. $out .= ' >>';
  7626. }
  7627. $out .= $this->_getannotsrefs($n);
  7628. $out .= ' /PZ '.$this->pagedim[$n]['PZ'];
  7629. $out .= ' >>';
  7630. $out .= "\n".'endobj';
  7631. $this->_out($out);
  7632. //Page content
  7633. $p = ($this->compress) ? gzcompress($temppage) : $temppage;
  7634. $this->_newobj();
  7635. $p = $this->_getrawstream($p);
  7636. $this->_out('<<'.$filter.'/Length '.strlen($p).'>> stream'."\n".$p."\n".'endstream'."\n".'endobj');
  7637. }
  7638. //Pages root
  7639. $out = $this->_getobj(1)."\n";
  7640. $out .= '<< /Type /Pages /Kids [';
  7641. foreach($this->page_obj_id as $page_obj) {
  7642. $out .= ' '.$page_obj.' 0 R';
  7643. }
  7644. $out .= ' ] /Count '.$num_pages.' >>';
  7645. $out .= "\n".'endobj';
  7646. $this->_out($out);
  7647. }
  7648. /**
  7649. * Get references to page annotations.
  7650. * @param $n (int) page number
  7651. * @return string
  7652. * @protected
  7653. * @author Nicola Asuni
  7654. * @since 5.0.010 (2010-05-17)
  7655. */
  7656. protected function _getannotsrefs($n) {
  7657. if (!(isset($this->PageAnnots[$n]) OR ($this->sign AND isset($this->signature_data['cert_type'])))) {
  7658. return '';
  7659. }
  7660. $out = ' /Annots [';
  7661. if (isset($this->PageAnnots[$n])) {
  7662. foreach ($this->PageAnnots[$n] as $key => $val) {
  7663. if (!in_array($val['n'], $this->radio_groups)) {
  7664. $out .= ' '.$val['n'].' 0 R';
  7665. }
  7666. }
  7667. // add radiobutton groups
  7668. if (isset($this->radiobutton_groups[$n])) {
  7669. foreach ($this->radiobutton_groups[$n] as $key => $data) {
  7670. if (isset($data['n'])) {
  7671. $out .= ' '.$data['n'].' 0 R';
  7672. }
  7673. }
  7674. }
  7675. }
  7676. if ($this->sign AND ($n == $this->signature_appearance['page']) AND isset($this->signature_data['cert_type'])) {
  7677. // set reference for signature object
  7678. $out .= ' '.$this->sig_obj_id.' 0 R';
  7679. }
  7680. if (!empty($this->empty_signature_appearance)) {
  7681. foreach ($this->empty_signature_appearance as $esa) {
  7682. if ($esa['page'] == $n) {
  7683. // set reference for empty signature objects
  7684. $out .= ' '.$esa['objid'].' 0 R';
  7685. }
  7686. }
  7687. }
  7688. $out .= ' ]';
  7689. return $out;
  7690. }
  7691. /**
  7692. * Output annotations objects for all pages.
  7693. * !!! THIS METHOD IS NOT YET COMPLETED !!!
  7694. * See section 12.5 of PDF 32000_2008 reference.
  7695. * @protected
  7696. * @author Nicola Asuni
  7697. * @since 4.0.018 (2008-08-06)
  7698. */
  7699. protected function _putannotsobjs() {
  7700. // reset object counter
  7701. for ($n=1; $n <= $this->numpages; ++$n) {
  7702. if (isset($this->PageAnnots[$n])) {
  7703. // set page annotations
  7704. foreach ($this->PageAnnots[$n] as $key => $pl) {
  7705. $annot_obj_id = $this->PageAnnots[$n][$key]['n'];
  7706. // create annotation object for grouping radiobuttons
  7707. if (isset($this->radiobutton_groups[$n][$pl['txt']]) AND is_array($this->radiobutton_groups[$n][$pl['txt']])) {
  7708. $radio_button_obj_id = $this->radiobutton_groups[$n][$pl['txt']]['n'];
  7709. $annots = '<<';
  7710. $annots .= ' /Type /Annot';
  7711. $annots .= ' /Subtype /Widget';
  7712. $annots .= ' /Rect [0 0 0 0]';
  7713. if ($this->radiobutton_groups[$n][$pl['txt']]['#readonly#']) {
  7714. // read only
  7715. $annots .= ' /F 68';
  7716. $annots .= ' /Ff 49153';
  7717. } else {
  7718. $annots .= ' /F 4'; // default print for PDF/A
  7719. $annots .= ' /Ff 49152';
  7720. }
  7721. $annots .= ' /T '.$this->_datastring($pl['txt'], $radio_button_obj_id);
  7722. if (isset($pl['opt']['tu']) AND is_string($pl['opt']['tu'])) {
  7723. $annots .= ' /TU '.$this->_datastring($pl['opt']['tu'], $radio_button_obj_id);
  7724. }
  7725. $annots .= ' /FT /Btn';
  7726. $annots .= ' /Kids [';
  7727. $defval = '';
  7728. foreach ($this->radiobutton_groups[$n][$pl['txt']] as $key => $data) {
  7729. if (isset($data['kid'])) {
  7730. $annots .= ' '.$data['kid'].' 0 R';
  7731. if ($data['def'] !== 'Off') {
  7732. $defval = $data['def'];
  7733. }
  7734. }
  7735. }
  7736. $annots .= ' ]';
  7737. if (!empty($defval)) {
  7738. $annots .= ' /V /'.$defval;
  7739. }
  7740. $annots .= ' >>';
  7741. $this->_out($this->_getobj($radio_button_obj_id)."\n".$annots."\n".'endobj');
  7742. $this->form_obj_id[] = $radio_button_obj_id;
  7743. // store object id to be used on Parent entry of Kids
  7744. $this->radiobutton_groups[$n][$pl['txt']] = $radio_button_obj_id;
  7745. }
  7746. $formfield = false;
  7747. $pl['opt'] = array_change_key_case($pl['opt'], CASE_LOWER);
  7748. $a = $pl['x'] * $this->k;
  7749. $b = $this->pagedim[$n]['h'] - (($pl['y'] + $pl['h']) * $this->k);
  7750. $c = $pl['w'] * $this->k;
  7751. $d = $pl['h'] * $this->k;
  7752. $rect = sprintf('%F %F %F %F', $a, $b, $a+$c, $b+$d);
  7753. // create new annotation object
  7754. $annots = '<</Type /Annot';
  7755. $annots .= ' /Subtype /'.$pl['opt']['subtype'];
  7756. $annots .= ' /Rect ['.$rect.']';
  7757. $ft = array('Btn', 'Tx', 'Ch', 'Sig');
  7758. if (isset($pl['opt']['ft']) AND in_array($pl['opt']['ft'], $ft)) {
  7759. $annots .= ' /FT /'.$pl['opt']['ft'];
  7760. $formfield = true;
  7761. }
  7762. if ($pl['opt']['subtype'] !== 'Link') {
  7763. $annots .= ' /Contents '.$this->_textstring($pl['txt'], $annot_obj_id);
  7764. }
  7765. $annots .= ' /P '.$this->page_obj_id[$n].' 0 R';
  7766. $annots .= ' /NM '.$this->_datastring(sprintf('%04u-%04u', $n, $key), $annot_obj_id);
  7767. $annots .= ' /M '.$this->_datestring($annot_obj_id, $this->doc_modification_timestamp);
  7768. if (isset($pl['opt']['f'])) {
  7769. $fval = 0;
  7770. if (is_array($pl['opt']['f'])) {
  7771. foreach ($pl['opt']['f'] as $f) {
  7772. switch (strtolower($f)) {
  7773. case 'invisible': {
  7774. $fval += 1 << 0;
  7775. break;
  7776. }
  7777. case 'hidden': {
  7778. $fval += 1 << 1;
  7779. break;
  7780. }
  7781. case 'print': {
  7782. $fval += 1 << 2;
  7783. break;
  7784. }
  7785. case 'nozoom': {
  7786. $fval += 1 << 3;
  7787. break;
  7788. }
  7789. case 'norotate': {
  7790. $fval += 1 << 4;
  7791. break;
  7792. }
  7793. case 'noview': {
  7794. $fval += 1 << 5;
  7795. break;
  7796. }
  7797. case 'readonly': {
  7798. $fval += 1 << 6;
  7799. break;
  7800. }
  7801. case 'locked': {
  7802. $fval += 1 << 8;
  7803. break;
  7804. }
  7805. case 'togglenoview': {
  7806. $fval += 1 << 9;
  7807. break;
  7808. }
  7809. case 'lockedcontents': {
  7810. $fval += 1 << 10;
  7811. break;
  7812. }
  7813. default: {
  7814. break;
  7815. }
  7816. }
  7817. }
  7818. } else {
  7819. $fval = intval($pl['opt']['f']);
  7820. }
  7821. } else {
  7822. $fval = 4;
  7823. }
  7824. if ($this->pdfa_mode) {
  7825. // force print flag for PDF/A mode
  7826. $fval |= 4;
  7827. }
  7828. $annots .= ' /F '.intval($fval);
  7829. if (isset($pl['opt']['as']) AND is_string($pl['opt']['as'])) {
  7830. $annots .= ' /AS /'.$pl['opt']['as'];
  7831. }
  7832. if (isset($pl['opt']['ap'])) {
  7833. // appearance stream
  7834. $annots .= ' /AP <<';
  7835. if (is_array($pl['opt']['ap'])) {
  7836. foreach ($pl['opt']['ap'] as $apmode => $apdef) {
  7837. // $apmode can be: n = normal; r = rollover; d = down;
  7838. $annots .= ' /'.strtoupper($apmode);
  7839. if (is_array($apdef)) {
  7840. $annots .= ' <<';
  7841. foreach ($apdef as $apstate => $stream) {
  7842. // reference to XObject that define the appearance for this mode-state
  7843. $apsobjid = $this->_putAPXObject($c, $d, $stream);
  7844. $annots .= ' /'.$apstate.' '.$apsobjid.' 0 R';
  7845. }
  7846. $annots .= ' >>';
  7847. } else {
  7848. // reference to XObject that define the appearance for this mode
  7849. $apsobjid = $this->_putAPXObject($c, $d, $apdef);
  7850. $annots .= ' '.$apsobjid.' 0 R';
  7851. }
  7852. }
  7853. } else {
  7854. $annots .= $pl['opt']['ap'];
  7855. }
  7856. $annots .= ' >>';
  7857. }
  7858. if (isset($pl['opt']['bs']) AND (is_array($pl['opt']['bs']))) {
  7859. $annots .= ' /BS <<';
  7860. $annots .= ' /Type /Border';
  7861. if (isset($pl['opt']['bs']['w'])) {
  7862. $annots .= ' /W '.intval($pl['opt']['bs']['w']);
  7863. }
  7864. $bstyles = array('S', 'D', 'B', 'I', 'U');
  7865. if (isset($pl['opt']['bs']['s']) AND in_array($pl['opt']['bs']['s'], $bstyles)) {
  7866. $annots .= ' /S /'.$pl['opt']['bs']['s'];
  7867. }
  7868. if (isset($pl['opt']['bs']['d']) AND (is_array($pl['opt']['bs']['d']))) {
  7869. $annots .= ' /D [';
  7870. foreach ($pl['opt']['bs']['d'] as $cord) {
  7871. $annots .= ' '.intval($cord);
  7872. }
  7873. $annots .= ']';
  7874. }
  7875. $annots .= ' >>';
  7876. } else {
  7877. $annots .= ' /Border [';
  7878. if (isset($pl['opt']['border']) AND (count($pl['opt']['border']) >= 3)) {
  7879. $annots .= intval($pl['opt']['border'][0]).' ';
  7880. $annots .= intval($pl['opt']['border'][1]).' ';
  7881. $annots .= intval($pl['opt']['border'][2]);
  7882. if (isset($pl['opt']['border'][3]) AND is_array($pl['opt']['border'][3])) {
  7883. $annots .= ' [';
  7884. foreach ($pl['opt']['border'][3] as $dash) {
  7885. $annots .= intval($dash).' ';
  7886. }
  7887. $annots .= ']';
  7888. }
  7889. } else {
  7890. $annots .= '0 0 0';
  7891. }
  7892. $annots .= ']';
  7893. }
  7894. if (isset($pl['opt']['be']) AND (is_array($pl['opt']['be']))) {
  7895. $annots .= ' /BE <<';
  7896. $bstyles = array('S', 'C');
  7897. if (isset($pl['opt']['be']['s']) AND in_array($pl['opt']['be']['s'], $bstyles)) {
  7898. $annots .= ' /S /'.$pl['opt']['bs']['s'];
  7899. } else {
  7900. $annots .= ' /S /S';
  7901. }
  7902. if (isset($pl['opt']['be']['i']) AND ($pl['opt']['be']['i'] >= 0) AND ($pl['opt']['be']['i'] <= 2)) {
  7903. $annots .= ' /I '.sprintf(' %F', $pl['opt']['be']['i']);
  7904. }
  7905. $annots .= '>>';
  7906. }
  7907. if (isset($pl['opt']['c']) AND (is_array($pl['opt']['c'])) AND !empty($pl['opt']['c'])) {
  7908. $annots .= ' /C '.TCPDF_COLORS::getColorStringFromArray($pl['opt']['c']);
  7909. }
  7910. //$annots .= ' /StructParent ';
  7911. //$annots .= ' /OC ';
  7912. $markups = array('text', 'freetext', 'line', 'square', 'circle', 'polygon', 'polyline', 'highlight', 'underline', 'squiggly', 'strikeout', 'stamp', 'caret', 'ink', 'fileattachment', 'sound');
  7913. if (in_array(strtolower($pl['opt']['subtype']), $markups)) {
  7914. // this is a markup type
  7915. if (isset($pl['opt']['t']) AND is_string($pl['opt']['t'])) {
  7916. $annots .= ' /T '.$this->_textstring($pl['opt']['t'], $annot_obj_id);
  7917. }
  7918. //$annots .= ' /Popup ';
  7919. if (isset($pl['opt']['ca'])) {
  7920. $annots .= ' /CA '.sprintf('%F', floatval($pl['opt']['ca']));
  7921. }
  7922. if (isset($pl['opt']['rc'])) {
  7923. $annots .= ' /RC '.$this->_textstring($pl['opt']['rc'], $annot_obj_id);
  7924. }
  7925. $annots .= ' /CreationDate '.$this->_datestring($annot_obj_id, $this->doc_creation_timestamp);
  7926. //$annots .= ' /IRT ';
  7927. if (isset($pl['opt']['subj'])) {
  7928. $annots .= ' /Subj '.$this->_textstring($pl['opt']['subj'], $annot_obj_id);
  7929. }
  7930. //$annots .= ' /RT ';
  7931. //$annots .= ' /IT ';
  7932. //$annots .= ' /ExData ';
  7933. }
  7934. $lineendings = array('Square', 'Circle', 'Diamond', 'OpenArrow', 'ClosedArrow', 'None', 'Butt', 'ROpenArrow', 'RClosedArrow', 'Slash');
  7935. // Annotation types
  7936. switch (strtolower($pl['opt']['subtype'])) {
  7937. case 'text': {
  7938. if (isset($pl['opt']['open'])) {
  7939. $annots .= ' /Open '. (strtolower($pl['opt']['open']) == 'true' ? 'true' : 'false');
  7940. }
  7941. $iconsapp = array('Comment', 'Help', 'Insert', 'Key', 'NewParagraph', 'Note', 'Paragraph');
  7942. if (isset($pl['opt']['name']) AND in_array($pl['opt']['name'], $iconsapp)) {
  7943. $annots .= ' /Name /'.$pl['opt']['name'];
  7944. } else {
  7945. $annots .= ' /Name /Note';
  7946. }
  7947. $statemodels = array('Marked', 'Review');
  7948. if (isset($pl['opt']['statemodel']) AND in_array($pl['opt']['statemodel'], $statemodels)) {
  7949. $annots .= ' /StateModel /'.$pl['opt']['statemodel'];
  7950. } else {
  7951. $pl['opt']['statemodel'] = 'Marked';
  7952. $annots .= ' /StateModel /'.$pl['opt']['statemodel'];
  7953. }
  7954. if ($pl['opt']['statemodel'] == 'Marked') {
  7955. $states = array('Accepted', 'Unmarked');
  7956. } else {
  7957. $states = array('Accepted', 'Rejected', 'Cancelled', 'Completed', 'None');
  7958. }
  7959. if (isset($pl['opt']['state']) AND in_array($pl['opt']['state'], $states)) {
  7960. $annots .= ' /State /'.$pl['opt']['state'];
  7961. } else {
  7962. if ($pl['opt']['statemodel'] == 'Marked') {
  7963. $annots .= ' /State /Unmarked';
  7964. } else {
  7965. $annots .= ' /State /None';
  7966. }
  7967. }
  7968. break;
  7969. }
  7970. case 'link': {
  7971. if (is_string($pl['txt']) && !empty($pl['txt'])) {
  7972. if ($pl['txt'][0] == '#') {
  7973. // internal destination
  7974. $annots .= ' /Dest /'.TCPDF_STATIC::encodeNameObject(substr($pl['txt'], 1));
  7975. } elseif ($pl['txt'][0] == '%') {
  7976. // embedded PDF file
  7977. $filename = basename(substr($pl['txt'], 1));
  7978. $annots .= ' /A << /S /GoToE /D [0 /Fit] /NewWindow true /T << /R /C /P '.($n - 1).' /A '.$this->embeddedfiles[$filename]['a'].' >> >>';
  7979. } elseif ($pl['txt'][0] == '*') {
  7980. // embedded generic file
  7981. $filename = basename(substr($pl['txt'], 1));
  7982. $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});';
  7983. $annots .= ' /A << /S /JavaScript /JS '.$this->_textstring($jsa, $annot_obj_id).'>>';
  7984. } else {
  7985. $parsedUrl = parse_url($pl['txt']);
  7986. if (empty($parsedUrl['scheme']) AND (strtolower(substr($parsedUrl['path'], -4)) == '.pdf')) {
  7987. // relative link to a PDF file
  7988. $dest = '[0 /Fit]'; // default page 0
  7989. if (!empty($parsedUrl['fragment'])) {
  7990. // check for named destination
  7991. $tmp = explode('=', $parsedUrl['fragment']);
  7992. $dest = '('.((count($tmp) == 2) ? $tmp[1] : $tmp[0]).')';
  7993. }
  7994. $annots .= ' /A <</S /GoToR /D '.$dest.' /F '.$this->_datastring($this->unhtmlentities($parsedUrl['path']), $annot_obj_id).' /NewWindow true>>';
  7995. } else {
  7996. // external URI link
  7997. $annots .= ' /A <</S /URI /URI '.$this->_datastring($this->unhtmlentities($pl['txt']), $annot_obj_id).'>>';
  7998. }
  7999. }
  8000. } elseif (isset($this->links[$pl['txt']])) {
  8001. // internal link ID
  8002. $l = $this->links[$pl['txt']];
  8003. if (isset($this->page_obj_id[($l['p'])])) {
  8004. $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)));
  8005. }
  8006. }
  8007. $hmodes = array('N', 'I', 'O', 'P');
  8008. if (isset($pl['opt']['h']) AND in_array($pl['opt']['h'], $hmodes)) {
  8009. $annots .= ' /H /'.$pl['opt']['h'];
  8010. } else {
  8011. $annots .= ' /H /I';
  8012. }
  8013. //$annots .= ' /PA ';
  8014. //$annots .= ' /Quadpoints ';
  8015. break;
  8016. }
  8017. case 'freetext': {
  8018. if (isset($pl['opt']['da']) AND !empty($pl['opt']['da'])) {
  8019. $annots .= ' /DA ('.$pl['opt']['da'].')';
  8020. }
  8021. if (isset($pl['opt']['q']) AND ($pl['opt']['q'] >= 0) AND ($pl['opt']['q'] <= 2)) {
  8022. $annots .= ' /Q '.intval($pl['opt']['q']);
  8023. }
  8024. if (isset($pl['opt']['rc'])) {
  8025. $annots .= ' /RC '.$this->_textstring($pl['opt']['rc'], $annot_obj_id);
  8026. }
  8027. if (isset($pl['opt']['ds'])) {
  8028. $annots .= ' /DS '.$this->_textstring($pl['opt']['ds'], $annot_obj_id);
  8029. }
  8030. if (isset($pl['opt']['cl']) AND is_array($pl['opt']['cl'])) {
  8031. $annots .= ' /CL [';
  8032. foreach ($pl['opt']['cl'] as $cl) {
  8033. $annots .= sprintf('%F ', $cl * $this->k);
  8034. }
  8035. $annots .= ']';
  8036. }
  8037. $tfit = array('FreeText', 'FreeTextCallout', 'FreeTextTypeWriter');
  8038. if (isset($pl['opt']['it']) AND in_array($pl['opt']['it'], $tfit)) {
  8039. $annots .= ' /IT /'.$pl['opt']['it'];
  8040. }
  8041. if (isset($pl['opt']['rd']) AND is_array($pl['opt']['rd'])) {
  8042. $l = $pl['opt']['rd'][0] * $this->k;
  8043. $r = $pl['opt']['rd'][1] * $this->k;
  8044. $t = $pl['opt']['rd'][2] * $this->k;
  8045. $b = $pl['opt']['rd'][3] * $this->k;
  8046. $annots .= ' /RD ['.sprintf('%F %F %F %F', $l, $r, $t, $b).']';
  8047. }
  8048. if (isset($pl['opt']['le']) AND in_array($pl['opt']['le'], $lineendings)) {
  8049. $annots .= ' /LE /'.$pl['opt']['le'];
  8050. }
  8051. break;
  8052. }
  8053. case 'line': {
  8054. break;
  8055. }
  8056. case 'square': {
  8057. break;
  8058. }
  8059. case 'circle': {
  8060. break;
  8061. }
  8062. case 'polygon': {
  8063. break;
  8064. }
  8065. case 'polyline': {
  8066. break;
  8067. }
  8068. case 'highlight': {
  8069. break;
  8070. }
  8071. case 'underline': {
  8072. break;
  8073. }
  8074. case 'squiggly': {
  8075. break;
  8076. }
  8077. case 'strikeout': {
  8078. break;
  8079. }
  8080. case 'stamp': {
  8081. break;
  8082. }
  8083. case 'caret': {
  8084. break;
  8085. }
  8086. case 'ink': {
  8087. break;
  8088. }
  8089. case 'popup': {
  8090. break;
  8091. }
  8092. case 'fileattachment': {
  8093. if ($this->pdfa_mode) {
  8094. // embedded files are not allowed in PDF/A mode
  8095. break;
  8096. }
  8097. if (!isset($pl['opt']['fs'])) {
  8098. break;
  8099. }
  8100. $filename = basename($pl['opt']['fs']);
  8101. if (isset($this->embeddedfiles[$filename]['f'])) {
  8102. $annots .= ' /FS '.$this->embeddedfiles[$filename]['f'].' 0 R';
  8103. $iconsapp = array('Graph', 'Paperclip', 'PushPin', 'Tag');
  8104. if (isset($pl['opt']['name']) AND in_array($pl['opt']['name'], $iconsapp)) {
  8105. $annots .= ' /Name /'.$pl['opt']['name'];
  8106. } else {
  8107. $annots .= ' /Name /PushPin';
  8108. }
  8109. // index (zero-based) of the annotation in the Annots array of this page
  8110. $this->embeddedfiles[$filename]['a'] = $key;
  8111. }
  8112. break;
  8113. }
  8114. case 'sound': {
  8115. if (!isset($pl['opt']['fs'])) {
  8116. break;
  8117. }
  8118. $filename = basename($pl['opt']['fs']);
  8119. if (isset($this->embeddedfiles[$filename]['f'])) {
  8120. // ... TO BE COMPLETED ...
  8121. // /R /C /B /E /CO /CP
  8122. $annots .= ' /Sound '.$this->embeddedfiles[$filename]['f'].' 0 R';
  8123. $iconsapp = array('Speaker', 'Mic');
  8124. if (isset($pl['opt']['name']) AND in_array($pl['opt']['name'], $iconsapp)) {
  8125. $annots .= ' /Name /'.$pl['opt']['name'];
  8126. } else {
  8127. $annots .= ' /Name /Speaker';
  8128. }
  8129. }
  8130. break;
  8131. }
  8132. case 'movie': {
  8133. break;
  8134. }
  8135. case 'widget': {
  8136. $hmode = array('N', 'I', 'O', 'P', 'T');
  8137. if (isset($pl['opt']['h']) AND in_array($pl['opt']['h'], $hmode)) {
  8138. $annots .= ' /H /'.$pl['opt']['h'];
  8139. }
  8140. if (isset($pl['opt']['mk']) AND (is_array($pl['opt']['mk'])) AND !empty($pl['opt']['mk'])) {
  8141. $annots .= ' /MK <<';
  8142. if (isset($pl['opt']['mk']['r'])) {
  8143. $annots .= ' /R '.$pl['opt']['mk']['r'];
  8144. }
  8145. if (isset($pl['opt']['mk']['bc']) AND (is_array($pl['opt']['mk']['bc']))) {
  8146. $annots .= ' /BC '.TCPDF_COLORS::getColorStringFromArray($pl['opt']['mk']['bc']);
  8147. }
  8148. if (isset($pl['opt']['mk']['bg']) AND (is_array($pl['opt']['mk']['bg']))) {
  8149. $annots .= ' /BG '.TCPDF_COLORS::getColorStringFromArray($pl['opt']['mk']['bg']);
  8150. }
  8151. if (isset($pl['opt']['mk']['ca'])) {
  8152. $annots .= ' /CA '.$pl['opt']['mk']['ca'];
  8153. }
  8154. if (isset($pl['opt']['mk']['rc'])) {
  8155. $annots .= ' /RC '.$pl['opt']['mk']['rc'];
  8156. }
  8157. if (isset($pl['opt']['mk']['ac'])) {
  8158. $annots .= ' /AC '.$pl['opt']['mk']['ac'];
  8159. }
  8160. if (isset($pl['opt']['mk']['i'])) {
  8161. $info = $this->getImageBuffer($pl['opt']['mk']['i']);
  8162. if ($info !== false) {
  8163. $annots .= ' /I '.$info['n'].' 0 R';
  8164. }
  8165. }
  8166. if (isset($pl['opt']['mk']['ri'])) {
  8167. $info = $this->getImageBuffer($pl['opt']['mk']['ri']);
  8168. if ($info !== false) {
  8169. $annots .= ' /RI '.$info['n'].' 0 R';
  8170. }
  8171. }
  8172. if (isset($pl['opt']['mk']['ix'])) {
  8173. $info = $this->getImageBuffer($pl['opt']['mk']['ix']);
  8174. if ($info !== false) {
  8175. $annots .= ' /IX '.$info['n'].' 0 R';
  8176. }
  8177. }
  8178. if (isset($pl['opt']['mk']['if']) AND (is_array($pl['opt']['mk']['if'])) AND !empty($pl['opt']['mk']['if'])) {
  8179. $annots .= ' /IF <<';
  8180. $if_sw = array('A', 'B', 'S', 'N');
  8181. if (isset($pl['opt']['mk']['if']['sw']) AND in_array($pl['opt']['mk']['if']['sw'], $if_sw)) {
  8182. $annots .= ' /SW /'.$pl['opt']['mk']['if']['sw'];
  8183. }
  8184. $if_s = array('A', 'P');
  8185. if (isset($pl['opt']['mk']['if']['s']) AND in_array($pl['opt']['mk']['if']['s'], $if_s)) {
  8186. $annots .= ' /S /'.$pl['opt']['mk']['if']['s'];
  8187. }
  8188. if (isset($pl['opt']['mk']['if']['a']) AND (is_array($pl['opt']['mk']['if']['a'])) AND !empty($pl['opt']['mk']['if']['a'])) {
  8189. $annots .= sprintf(' /A [%F %F]', $pl['opt']['mk']['if']['a'][0], $pl['opt']['mk']['if']['a'][1]);
  8190. }
  8191. if (isset($pl['opt']['mk']['if']['fb']) AND ($pl['opt']['mk']['if']['fb'])) {
  8192. $annots .= ' /FB true';
  8193. }
  8194. $annots .= '>>';
  8195. }
  8196. if (isset($pl['opt']['mk']['tp']) AND ($pl['opt']['mk']['tp'] >= 0) AND ($pl['opt']['mk']['tp'] <= 6)) {
  8197. $annots .= ' /TP '.intval($pl['opt']['mk']['tp']);
  8198. }
  8199. $annots .= '>>';
  8200. } // end MK
  8201. // --- Entries for field dictionaries ---
  8202. if (isset($this->radiobutton_groups[$n][$pl['txt']])) {
  8203. // set parent
  8204. $annots .= ' /Parent '.$this->radiobutton_groups[$n][$pl['txt']].' 0 R';
  8205. }
  8206. if (isset($pl['opt']['t']) AND is_string($pl['opt']['t'])) {
  8207. $annots .= ' /T '.$this->_datastring($pl['opt']['t'], $annot_obj_id);
  8208. }
  8209. if (isset($pl['opt']['tu']) AND is_string($pl['opt']['tu'])) {
  8210. $annots .= ' /TU '.$this->_datastring($pl['opt']['tu'], $annot_obj_id);
  8211. }
  8212. if (isset($pl['opt']['tm']) AND is_string($pl['opt']['tm'])) {
  8213. $annots .= ' /TM '.$this->_datastring($pl['opt']['tm'], $annot_obj_id);
  8214. }
  8215. if (isset($pl['opt']['ff'])) {
  8216. if (is_array($pl['opt']['ff'])) {
  8217. // array of bit settings
  8218. $flag = 0;
  8219. foreach($pl['opt']['ff'] as $val) {
  8220. $flag += 1 << ($val - 1);
  8221. }
  8222. } else {
  8223. $flag = intval($pl['opt']['ff']);
  8224. }
  8225. $annots .= ' /Ff '.$flag;
  8226. }
  8227. if (isset($pl['opt']['maxlen'])) {
  8228. $annots .= ' /MaxLen '.intval($pl['opt']['maxlen']);
  8229. }
  8230. if (isset($pl['opt']['v'])) {
  8231. $annots .= ' /V';
  8232. if (is_array($pl['opt']['v'])) {
  8233. foreach ($pl['opt']['v'] AS $optval) {
  8234. if (is_float($optval)) {
  8235. $optval = sprintf('%F', $optval);
  8236. }
  8237. $annots .= ' '.$optval;
  8238. }
  8239. } else {
  8240. $annots .= ' '.$this->_textstring($pl['opt']['v'], $annot_obj_id);
  8241. }
  8242. }
  8243. if (isset($pl['opt']['dv'])) {
  8244. $annots .= ' /DV';
  8245. if (is_array($pl['opt']['dv'])) {
  8246. foreach ($pl['opt']['dv'] AS $optval) {
  8247. if (is_float($optval)) {
  8248. $optval = sprintf('%F', $optval);
  8249. }
  8250. $annots .= ' '.$optval;
  8251. }
  8252. } else {
  8253. $annots .= ' '.$this->_textstring($pl['opt']['dv'], $annot_obj_id);
  8254. }
  8255. }
  8256. if (isset($pl['opt']['rv'])) {
  8257. $annots .= ' /RV';
  8258. if (is_array($pl['opt']['rv'])) {
  8259. foreach ($pl['opt']['rv'] AS $optval) {
  8260. if (is_float($optval)) {
  8261. $optval = sprintf('%F', $optval);
  8262. }
  8263. $annots .= ' '.$optval;
  8264. }
  8265. } else {
  8266. $annots .= ' '.$this->_textstring($pl['opt']['rv'], $annot_obj_id);
  8267. }
  8268. }
  8269. if (isset($pl['opt']['a']) AND !empty($pl['opt']['a'])) {
  8270. $annots .= ' /A << '.$pl['opt']['a'].' >>';
  8271. }
  8272. if (isset($pl['opt']['aa']) AND !empty($pl['opt']['aa'])) {
  8273. $annots .= ' /AA << '.$pl['opt']['aa'].' >>';
  8274. }
  8275. if (isset($pl['opt']['da']) AND !empty($pl['opt']['da'])) {
  8276. $annots .= ' /DA ('.$pl['opt']['da'].')';
  8277. }
  8278. if (isset($pl['opt']['q']) AND ($pl['opt']['q'] >= 0) AND ($pl['opt']['q'] <= 2)) {
  8279. $annots .= ' /Q '.intval($pl['opt']['q']);
  8280. }
  8281. if (isset($pl['opt']['opt']) AND (is_array($pl['opt']['opt'])) AND !empty($pl['opt']['opt'])) {
  8282. $annots .= ' /Opt [';
  8283. foreach($pl['opt']['opt'] AS $copt) {
  8284. if (is_array($copt)) {
  8285. $annots .= ' ['.$this->_textstring($copt[0], $annot_obj_id).' '.$this->_textstring($copt[1], $annot_obj_id).']';
  8286. } else {
  8287. $annots .= ' '.$this->_textstring($copt, $annot_obj_id);
  8288. }
  8289. }
  8290. $annots .= ']';
  8291. }
  8292. if (isset($pl['opt']['ti'])) {
  8293. $annots .= ' /TI '.intval($pl['opt']['ti']);
  8294. }
  8295. if (isset($pl['opt']['i']) AND (is_array($pl['opt']['i'])) AND !empty($pl['opt']['i'])) {
  8296. $annots .= ' /I [';
  8297. foreach($pl['opt']['i'] AS $copt) {
  8298. $annots .= intval($copt).' ';
  8299. }
  8300. $annots .= ']';
  8301. }
  8302. break;
  8303. }
  8304. case 'screen': {
  8305. break;
  8306. }
  8307. case 'printermark': {
  8308. break;
  8309. }
  8310. case 'trapnet': {
  8311. break;
  8312. }
  8313. case 'watermark': {
  8314. break;
  8315. }
  8316. case '3d': {
  8317. break;
  8318. }
  8319. default: {
  8320. break;
  8321. }
  8322. }
  8323. $annots .= '>>';
  8324. // create new annotation object
  8325. $this->_out($this->_getobj($annot_obj_id)."\n".$annots."\n".'endobj');
  8326. if ($formfield AND !isset($this->radiobutton_groups[$n][$pl['txt']])) {
  8327. // store reference of form object
  8328. $this->form_obj_id[] = $annot_obj_id;
  8329. }
  8330. }
  8331. }
  8332. } // end for each page
  8333. }
  8334. /**
  8335. * Put appearance streams XObject used to define annotation's appearance states.
  8336. * @param $w (int) annotation width
  8337. * @param $h (int) annotation height
  8338. * @param $stream (string) appearance stream
  8339. * @return int object ID
  8340. * @protected
  8341. * @since 4.8.001 (2009-09-09)
  8342. */
  8343. protected function _putAPXObject($w=0, $h=0, $stream='') {
  8344. $stream = trim($stream);
  8345. $out = $this->_getobj()."\n";
  8346. $this->xobjects['AX'.$this->n] = array('n' => $this->n);
  8347. $out .= '<<';
  8348. $out .= ' /Type /XObject';
  8349. $out .= ' /Subtype /Form';
  8350. $out .= ' /FormType 1';
  8351. if ($this->compress) {
  8352. $stream = gzcompress($stream);
  8353. $out .= ' /Filter /FlateDecode';
  8354. }
  8355. $rect = sprintf('%F %F', $w, $h);
  8356. $out .= ' /BBox [0 0 '.$rect.']';
  8357. $out .= ' /Matrix [1 0 0 1 0 0]';
  8358. $out .= ' /Resources 2 0 R';
  8359. $stream = $this->_getrawstream($stream);
  8360. $out .= ' /Length '.strlen($stream);
  8361. $out .= ' >>';
  8362. $out .= ' stream'."\n".$stream."\n".'endstream';
  8363. $out .= "\n".'endobj';
  8364. $this->_out($out);
  8365. return $this->n;
  8366. }
  8367. /**
  8368. * Output fonts.
  8369. * @author Nicola Asuni
  8370. * @protected
  8371. */
  8372. protected function _putfonts() {
  8373. $nf = $this->n;
  8374. foreach ($this->diffs as $diff) {
  8375. //Encodings
  8376. $this->_newobj();
  8377. $this->_out('<< /Type /Encoding /BaseEncoding /WinAnsiEncoding /Differences ['.$diff.'] >>'."\n".'endobj');
  8378. }
  8379. $mqr = TCPDF_STATIC::get_mqr();
  8380. TCPDF_STATIC::set_mqr(false);
  8381. foreach ($this->FontFiles as $file => $info) {
  8382. // search and get font file to embedd
  8383. $fontfile = TCPDF_FONTS::getFontFullPath($file, $info['fontdir']);
  8384. if (!TCPDF_STATIC::empty_string($fontfile)) {
  8385. $font = file_get_contents($fontfile);
  8386. $compressed = (substr($file, -2) == '.z');
  8387. if ((!$compressed) AND (isset($info['length2']))) {
  8388. $header = (ord($font[0]) == 128);
  8389. if ($header) {
  8390. // strip first binary header
  8391. $font = substr($font, 6);
  8392. }
  8393. if ($header AND (ord($font[$info['length1']]) == 128)) {
  8394. // strip second binary header
  8395. $font = substr($font, 0, $info['length1']).substr($font, ($info['length1'] + 6));
  8396. }
  8397. } elseif ($info['subset'] AND ((!$compressed) OR ($compressed AND function_exists('gzcompress')))) {
  8398. if ($compressed) {
  8399. // uncompress font
  8400. $font = gzuncompress($font);
  8401. }
  8402. // merge subset characters
  8403. $subsetchars = array(); // used chars
  8404. foreach ($info['fontkeys'] as $fontkey) {
  8405. $fontinfo = $this->getFontBuffer($fontkey);
  8406. $subsetchars += $fontinfo['subsetchars'];
  8407. }
  8408. // rebuild a font subset
  8409. $font = TCPDF_FONTS::_getTrueTypeFontSubset($font, $subsetchars);
  8410. // calculate new font length
  8411. $info['length1'] = strlen($font);
  8412. if ($compressed) {
  8413. // recompress font
  8414. $font = gzcompress($font);
  8415. }
  8416. }
  8417. $this->_newobj();
  8418. $this->FontFiles[$file]['n'] = $this->n;
  8419. $stream = $this->_getrawstream($font);
  8420. $out = '<< /Length '.strlen($stream);
  8421. if ($compressed) {
  8422. $out .= ' /Filter /FlateDecode';
  8423. }
  8424. $out .= ' /Length1 '.$info['length1'];
  8425. if (isset($info['length2'])) {
  8426. $out .= ' /Length2 '.$info['length2'].' /Length3 0';
  8427. }
  8428. $out .= ' >>';
  8429. $out .= ' stream'."\n".$stream."\n".'endstream';
  8430. $out .= "\n".'endobj';
  8431. $this->_out($out);
  8432. }
  8433. }
  8434. TCPDF_STATIC::set_mqr($mqr);
  8435. foreach ($this->fontkeys as $k) {
  8436. //Font objects
  8437. $font = $this->getFontBuffer($k);
  8438. $type = $font['type'];
  8439. $name = $font['name'];
  8440. if ($type == 'core') {
  8441. // standard core font
  8442. $out = $this->_getobj($this->font_obj_ids[$k])."\n";
  8443. $out .= '<</Type /Font';
  8444. $out .= ' /Subtype /Type1';
  8445. $out .= ' /BaseFont /'.$name;
  8446. $out .= ' /Name /F'.$font['i'];
  8447. if ((strtolower($name) != 'symbol') AND (strtolower($name) != 'zapfdingbats')) {
  8448. $out .= ' /Encoding /WinAnsiEncoding';
  8449. }
  8450. if ($k == 'helvetica') {
  8451. // add default font for annotations
  8452. $this->annotation_fonts[$k] = $font['i'];
  8453. }
  8454. $out .= ' >>';
  8455. $out .= "\n".'endobj';
  8456. $this->_out($out);
  8457. } elseif (($type == 'Type1') OR ($type == 'TrueType')) {
  8458. // additional Type1 or TrueType font
  8459. $out = $this->_getobj($this->font_obj_ids[$k])."\n";
  8460. $out .= '<</Type /Font';
  8461. $out .= ' /Subtype /'.$type;
  8462. $out .= ' /BaseFont /'.$name;
  8463. $out .= ' /Name /F'.$font['i'];
  8464. $out .= ' /FirstChar 32 /LastChar 255';
  8465. $out .= ' /Widths '.($this->n + 1).' 0 R';
  8466. $out .= ' /FontDescriptor '.($this->n + 2).' 0 R';
  8467. if ($font['enc']) {
  8468. if (isset($font['diff'])) {
  8469. $out .= ' /Encoding '.($nf + $font['diff']).' 0 R';
  8470. } else {
  8471. $out .= ' /Encoding /WinAnsiEncoding';
  8472. }
  8473. }
  8474. $out .= ' >>';
  8475. $out .= "\n".'endobj';
  8476. $this->_out($out);
  8477. // Widths
  8478. $this->_newobj();
  8479. $s = '[';
  8480. for ($i = 32; $i < 256; ++$i) {
  8481. if (isset($font['cw'][$i])) {
  8482. $s .= $font['cw'][$i].' ';
  8483. } else {
  8484. $s .= $font['dw'].' ';
  8485. }
  8486. }
  8487. $s .= ']';
  8488. $s .= "\n".'endobj';
  8489. $this->_out($s);
  8490. //Descriptor
  8491. $this->_newobj();
  8492. $s = '<</Type /FontDescriptor /FontName /'.$name;
  8493. foreach ($font['desc'] as $fdk => $fdv) {
  8494. if (is_float($fdv)) {
  8495. $fdv = sprintf('%F', $fdv);
  8496. }
  8497. $s .= ' /'.$fdk.' '.$fdv.'';
  8498. }
  8499. if (!TCPDF_STATIC::empty_string($font['file'])) {
  8500. $s .= ' /FontFile'.($type == 'Type1' ? '' : '2').' '.$this->FontFiles[$font['file']]['n'].' 0 R';
  8501. }
  8502. $s .= '>>';
  8503. $s .= "\n".'endobj';
  8504. $this->_out($s);
  8505. } else {
  8506. // additional types
  8507. $mtd = '_put'.strtolower($type);
  8508. if (!method_exists($this, $mtd)) {
  8509. $this->Error('Unsupported font type: '.$type);
  8510. }
  8511. $this->$mtd($font);
  8512. }
  8513. }
  8514. }
  8515. /**
  8516. * Adds unicode fonts.<br>
  8517. * Based on PDF Reference 1.3 (section 5)
  8518. * @param $font (array) font data
  8519. * @protected
  8520. * @author Nicola Asuni
  8521. * @since 1.52.0.TC005 (2005-01-05)
  8522. */
  8523. protected function _puttruetypeunicode($font) {
  8524. $fontname = '';
  8525. if ($font['subset']) {
  8526. // change name for font subsetting
  8527. $subtag = sprintf('%06u', $font['i']);
  8528. $subtag = strtr($subtag, '0123456789', 'ABCDEFGHIJ');
  8529. $fontname .= $subtag.'+';
  8530. }
  8531. $fontname .= $font['name'];
  8532. // Type0 Font
  8533. // A composite font composed of other fonts, organized hierarchically
  8534. $out = $this->_getobj($this->font_obj_ids[$font['fontkey']])."\n";
  8535. $out .= '<< /Type /Font';
  8536. $out .= ' /Subtype /Type0';
  8537. $out .= ' /BaseFont /'.$fontname;
  8538. $out .= ' /Name /F'.$font['i'];
  8539. $out .= ' /Encoding /'.$font['enc'];
  8540. $out .= ' /ToUnicode '.($this->n + 1).' 0 R';
  8541. $out .= ' /DescendantFonts ['.($this->n + 2).' 0 R]';
  8542. $out .= ' >>';
  8543. $out .= "\n".'endobj';
  8544. $this->_out($out);
  8545. // ToUnicode map for Identity-H
  8546. $stream = TCPDF_FONT_DATA::$uni_identity_h;
  8547. // ToUnicode Object
  8548. $this->_newobj();
  8549. $stream = ($this->compress) ? gzcompress($stream) : $stream;
  8550. $filter = ($this->compress) ? '/Filter /FlateDecode ' : '';
  8551. $stream = $this->_getrawstream($stream);
  8552. $this->_out('<<'.$filter.'/Length '.strlen($stream).'>> stream'."\n".$stream."\n".'endstream'."\n".'endobj');
  8553. // CIDFontType2
  8554. // A CIDFont whose glyph descriptions are based on TrueType font technology
  8555. $oid = $this->_newobj();
  8556. $out = '<< /Type /Font';
  8557. $out .= ' /Subtype /CIDFontType2';
  8558. $out .= ' /BaseFont /'.$fontname;
  8559. // A dictionary containing entries that define the character collection of the CIDFont.
  8560. $cidinfo = '/Registry '.$this->_datastring($font['cidinfo']['Registry'], $oid);
  8561. $cidinfo .= ' /Ordering '.$this->_datastring($font['cidinfo']['Ordering'], $oid);
  8562. $cidinfo .= ' /Supplement '.$font['cidinfo']['Supplement'];
  8563. $out .= ' /CIDSystemInfo << '.$cidinfo.' >>';
  8564. $out .= ' /FontDescriptor '.($this->n + 1).' 0 R';
  8565. $out .= ' /DW '.$font['dw']; // default width
  8566. $out .= "\n".TCPDF_FONTS::_putfontwidths($font, 0);
  8567. if (isset($font['ctg']) AND (!TCPDF_STATIC::empty_string($font['ctg']))) {
  8568. $out .= "\n".'/CIDToGIDMap '.($this->n + 2).' 0 R';
  8569. }
  8570. $out .= ' >>';
  8571. $out .= "\n".'endobj';
  8572. $this->_out($out);
  8573. // Font descriptor
  8574. // A font descriptor describing the CIDFont default metrics other than its glyph widths
  8575. $this->_newobj();
  8576. $out = '<< /Type /FontDescriptor';
  8577. $out .= ' /FontName /'.$fontname;
  8578. foreach ($font['desc'] as $key => $value) {
  8579. if (is_float($value)) {
  8580. $value = sprintf('%F', $value);
  8581. }
  8582. $out .= ' /'.$key.' '.$value;
  8583. }
  8584. $fontdir = false;
  8585. if (!TCPDF_STATIC::empty_string($font['file'])) {
  8586. // A stream containing a TrueType font
  8587. $out .= ' /FontFile2 '.$this->FontFiles[$font['file']]['n'].' 0 R';
  8588. $fontdir = $this->FontFiles[$font['file']]['fontdir'];
  8589. }
  8590. $out .= ' >>';
  8591. $out .= "\n".'endobj';
  8592. $this->_out($out);
  8593. if (isset($font['ctg']) AND (!TCPDF_STATIC::empty_string($font['ctg']))) {
  8594. $this->_newobj();
  8595. // Embed CIDToGIDMap
  8596. // A specification of the mapping from CIDs to glyph indices
  8597. // search and get CTG font file to embedd
  8598. $ctgfile = strtolower($font['ctg']);
  8599. // search and get ctg font file to embedd
  8600. $fontfile = TCPDF_FONTS::getFontFullPath($ctgfile, $fontdir);
  8601. if (TCPDF_STATIC::empty_string($fontfile)) {
  8602. $this->Error('Font file not found: '.$ctgfile);
  8603. }
  8604. $stream = $this->_getrawstream(file_get_contents($fontfile));
  8605. $out = '<< /Length '.strlen($stream).'';
  8606. if (substr($fontfile, -2) == '.z') { // check file extension
  8607. // Decompresses data encoded using the public-domain
  8608. // zlib/deflate compression method, reproducing the
  8609. // original text or binary data
  8610. $out .= ' /Filter /FlateDecode';
  8611. }
  8612. $out .= ' >>';
  8613. $out .= ' stream'."\n".$stream."\n".'endstream';
  8614. $out .= "\n".'endobj';
  8615. $this->_out($out);
  8616. }
  8617. }
  8618. /**
  8619. * Output CID-0 fonts.
  8620. * A Type 0 CIDFont contains glyph descriptions based on the Adobe Type 1 font format
  8621. * @param $font (array) font data
  8622. * @protected
  8623. * @author Andrew Whitehead, Nicola Asuni, Yukihiro Nakadaira
  8624. * @since 3.2.000 (2008-06-23)
  8625. */
  8626. protected function _putcidfont0($font) {
  8627. $cidoffset = 0;
  8628. if (!isset($font['cw'][1])) {
  8629. $cidoffset = 31;
  8630. }
  8631. if (isset($font['cidinfo']['uni2cid'])) {
  8632. // convert unicode to cid.
  8633. $uni2cid = $font['cidinfo']['uni2cid'];
  8634. $cw = array();
  8635. foreach ($font['cw'] as $uni => $width) {
  8636. if (isset($uni2cid[$uni])) {
  8637. $cw[($uni2cid[$uni] + $cidoffset)] = $width;
  8638. } elseif ($uni < 256) {
  8639. $cw[$uni] = $width;
  8640. } // else unknown character
  8641. }
  8642. $font = array_merge($font, array('cw' => $cw));
  8643. }
  8644. $name = $font['name'];
  8645. $enc = $font['enc'];
  8646. if ($enc) {
  8647. $longname = $name.'-'.$enc;
  8648. } else {
  8649. $longname = $name;
  8650. }
  8651. $out = $this->_getobj($this->font_obj_ids[$font['fontkey']])."\n";
  8652. $out .= '<</Type /Font';
  8653. $out .= ' /Subtype /Type0';
  8654. $out .= ' /BaseFont /'.$longname;
  8655. $out .= ' /Name /F'.$font['i'];
  8656. if ($enc) {
  8657. $out .= ' /Encoding /'.$enc;
  8658. }
  8659. $out .= ' /DescendantFonts ['.($this->n + 1).' 0 R]';
  8660. $out .= ' >>';
  8661. $out .= "\n".'endobj';
  8662. $this->_out($out);
  8663. $oid = $this->_newobj();
  8664. $out = '<</Type /Font';
  8665. $out .= ' /Subtype /CIDFontType0';
  8666. $out .= ' /BaseFont /'.$name;
  8667. $cidinfo = '/Registry '.$this->_datastring($font['cidinfo']['Registry'], $oid);
  8668. $cidinfo .= ' /Ordering '.$this->_datastring($font['cidinfo']['Ordering'], $oid);
  8669. $cidinfo .= ' /Supplement '.$font['cidinfo']['Supplement'];
  8670. $out .= ' /CIDSystemInfo <<'.$cidinfo.'>>';
  8671. $out .= ' /FontDescriptor '.($this->n + 1).' 0 R';
  8672. $out .= ' /DW '.$font['dw'];
  8673. $out .= "\n".TCPDF_FONTS::_putfontwidths($font, $cidoffset);
  8674. $out .= ' >>';
  8675. $out .= "\n".'endobj';
  8676. $this->_out($out);
  8677. $this->_newobj();
  8678. $s = '<</Type /FontDescriptor /FontName /'.$name;
  8679. foreach ($font['desc'] as $k => $v) {
  8680. if ($k != 'Style') {
  8681. if (is_float($v)) {
  8682. $v = sprintf('%F', $v);
  8683. }
  8684. $s .= ' /'.$k.' '.$v.'';
  8685. }
  8686. }
  8687. $s .= '>>';
  8688. $s .= "\n".'endobj';
  8689. $this->_out($s);
  8690. }
  8691. /**
  8692. * Output images.
  8693. * @protected
  8694. */
  8695. protected function _putimages() {
  8696. $filter = ($this->compress) ? '/Filter /FlateDecode ' : '';
  8697. foreach ($this->imagekeys as $file) {
  8698. $info = $this->getImageBuffer($file);
  8699. // set object for alternate images array
  8700. if ((!$this->pdfa_mode) AND isset($info['altimgs']) AND !empty($info['altimgs'])) {
  8701. $altoid = $this->_newobj();
  8702. $out = '[';
  8703. foreach ($info['altimgs'] as $altimage) {
  8704. if (isset($this->xobjects['I'.$altimage[0]]['n'])) {
  8705. $out .= ' << /Image '.$this->xobjects['I'.$altimage[0]]['n'].' 0 R';
  8706. $out .= ' /DefaultForPrinting';
  8707. if ($altimage[1] === true) {
  8708. $out .= ' true';
  8709. } else {
  8710. $out .= ' false';
  8711. }
  8712. $out .= ' >>';
  8713. }
  8714. }
  8715. $out .= ' ]';
  8716. $out .= "\n".'endobj';
  8717. $this->_out($out);
  8718. }
  8719. // set image object
  8720. $oid = $this->_newobj();
  8721. $this->xobjects['I'.$info['i']] = array('n' => $oid);
  8722. $this->setImageSubBuffer($file, 'n', $this->n);
  8723. $out = '<</Type /XObject';
  8724. $out .= ' /Subtype /Image';
  8725. $out .= ' /Width '.$info['w'];
  8726. $out .= ' /Height '.$info['h'];
  8727. if (array_key_exists('masked', $info)) {
  8728. $out .= ' /SMask '.($this->n - 1).' 0 R';
  8729. }
  8730. // set color space
  8731. $icc = false;
  8732. if (isset($info['icc']) AND ($info['icc'] !== false)) {
  8733. // ICC Colour Space
  8734. $icc = true;
  8735. $out .= ' /ColorSpace [/ICCBased '.($this->n + 1).' 0 R]';
  8736. } elseif ($info['cs'] == 'Indexed') {
  8737. // Indexed Colour Space
  8738. $out .= ' /ColorSpace [/Indexed /DeviceRGB '.((strlen($info['pal']) / 3) - 1).' '.($this->n + 1).' 0 R]';
  8739. } else {
  8740. // Device Colour Space
  8741. $out .= ' /ColorSpace /'.$info['cs'];
  8742. }
  8743. if ($info['cs'] == 'DeviceCMYK') {
  8744. $out .= ' /Decode [1 0 1 0 1 0 1 0]';
  8745. }
  8746. $out .= ' /BitsPerComponent '.$info['bpc'];
  8747. if (isset($altoid) AND ($altoid > 0)) {
  8748. // reference to alternate images dictionary
  8749. $out .= ' /Alternates '.$altoid.' 0 R';
  8750. }
  8751. if (isset($info['exurl']) AND !empty($info['exurl'])) {
  8752. // external stream
  8753. $out .= ' /Length 0';
  8754. $out .= ' /F << /FS /URL /F '.$this->_datastring($info['exurl'], $oid).' >>';
  8755. if (isset($info['f'])) {
  8756. $out .= ' /FFilter /'.$info['f'];
  8757. }
  8758. $out .= ' >>';
  8759. $out .= ' stream'."\n".'endstream';
  8760. } else {
  8761. if (isset($info['f'])) {
  8762. $out .= ' /Filter /'.$info['f'];
  8763. }
  8764. if (isset($info['parms'])) {
  8765. $out .= ' '.$info['parms'];
  8766. }
  8767. if (isset($info['trns']) AND is_array($info['trns'])) {
  8768. $trns = '';
  8769. $count_info = count($info['trns']);
  8770. if ($info['cs'] == 'Indexed') {
  8771. $maxval =(pow(2, $info['bpc']) - 1);
  8772. for ($i = 0; $i < $count_info; ++$i) {
  8773. if (($info['trns'][$i] != 0) AND ($info['trns'][$i] != $maxval)) {
  8774. // this is not a binary type mask @TODO: create a SMask
  8775. $trns = '';
  8776. break;
  8777. } elseif (empty($trns) AND ($info['trns'][$i] == 0)) {
  8778. // store the first fully transparent value
  8779. $trns .= $i.' '.$i.' ';
  8780. }
  8781. }
  8782. } else {
  8783. // grayscale or RGB
  8784. for ($i = 0; $i < $count_info; ++$i) {
  8785. if ($info['trns'][$i] == 0) {
  8786. $trns .= $info['trns'][$i].' '.$info['trns'][$i].' ';
  8787. }
  8788. }
  8789. }
  8790. // Colour Key Masking
  8791. if (!empty($trns)) {
  8792. $out .= ' /Mask ['.$trns.']';
  8793. }
  8794. }
  8795. $stream = $this->_getrawstream($info['data']);
  8796. $out .= ' /Length '.strlen($stream).' >>';
  8797. $out .= ' stream'."\n".$stream."\n".'endstream';
  8798. }
  8799. $out .= "\n".'endobj';
  8800. $this->_out($out);
  8801. if ($icc) {
  8802. // ICC colour profile
  8803. $this->_newobj();
  8804. $icc = ($this->compress) ? gzcompress($info['icc']) : $info['icc'];
  8805. $icc = $this->_getrawstream($icc);
  8806. $this->_out('<</N '.$info['ch'].' /Alternate /'.$info['cs'].' '.$filter.'/Length '.strlen($icc).'>> stream'."\n".$icc."\n".'endstream'."\n".'endobj');
  8807. } elseif ($info['cs'] == 'Indexed') {
  8808. // colour palette
  8809. $this->_newobj();
  8810. $pal = ($this->compress) ? gzcompress($info['pal']) : $info['pal'];
  8811. $pal = $this->_getrawstream($pal);
  8812. $this->_out('<<'.$filter.'/Length '.strlen($pal).'>> stream'."\n".$pal."\n".'endstream'."\n".'endobj');
  8813. }
  8814. }
  8815. }
  8816. /**
  8817. * Output Form XObjects Templates.
  8818. * @author Nicola Asuni
  8819. * @since 5.8.017 (2010-08-24)
  8820. * @protected
  8821. * @see startTemplate(), endTemplate(), printTemplate()
  8822. */
  8823. protected function _putxobjects() {
  8824. foreach ($this->xobjects as $key => $data) {
  8825. if (isset($data['outdata'])) {
  8826. $stream = str_replace($this->epsmarker, '', trim($data['outdata']));
  8827. $out = $this->_getobj($data['n'])."\n";
  8828. $out .= '<<';
  8829. $out .= ' /Type /XObject';
  8830. $out .= ' /Subtype /Form';
  8831. $out .= ' /FormType 1';
  8832. if ($this->compress) {
  8833. $stream = gzcompress($stream);
  8834. $out .= ' /Filter /FlateDecode';
  8835. }
  8836. $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));
  8837. $out .= ' /Matrix [1 0 0 1 0 0]';
  8838. $out .= ' /Resources <<';
  8839. $out .= ' /ProcSet [/PDF /Text /ImageB /ImageC /ImageI]';
  8840. if (!$this->pdfa_mode) {
  8841. // transparency
  8842. if (isset($data['extgstates']) AND !empty($data['extgstates'])) {
  8843. $out .= ' /ExtGState <<';
  8844. foreach ($data['extgstates'] as $k => $extgstate) {
  8845. if (isset($this->extgstates[$k]['name'])) {
  8846. $out .= ' /'.$this->extgstates[$k]['name'];
  8847. } else {
  8848. $out .= ' /GS'.$k;
  8849. }
  8850. $out .= ' '.$this->extgstates[$k]['n'].' 0 R';
  8851. }
  8852. $out .= ' >>';
  8853. }
  8854. if (isset($data['gradients']) AND !empty($data['gradients'])) {
  8855. $gp = '';
  8856. $gs = '';
  8857. foreach ($data['gradients'] as $id => $grad) {
  8858. // gradient patterns
  8859. $gp .= ' /p'.$id.' '.$this->gradients[$id]['pattern'].' 0 R';
  8860. // gradient shadings
  8861. $gs .= ' /Sh'.$id.' '.$this->gradients[$id]['id'].' 0 R';
  8862. }
  8863. $out .= ' /Pattern <<'.$gp.' >>';
  8864. $out .= ' /Shading <<'.$gs.' >>';
  8865. }
  8866. }
  8867. // spot colors
  8868. if (isset($data['spot_colors']) AND !empty($data['spot_colors'])) {
  8869. $out .= ' /ColorSpace <<';
  8870. foreach ($data['spot_colors'] as $name => $color) {
  8871. $out .= ' /CS'.$color['i'].' '.$this->spot_colors[$name]['n'].' 0 R';
  8872. }
  8873. $out .= ' >>';
  8874. }
  8875. // fonts
  8876. if (!empty($data['fonts'])) {
  8877. $out .= ' /Font <<';
  8878. foreach ($data['fonts'] as $fontkey => $fontid) {
  8879. $out .= ' /F'.$fontid.' '.$this->font_obj_ids[$fontkey].' 0 R';
  8880. }
  8881. $out .= ' >>';
  8882. }
  8883. // images or nested xobjects
  8884. if (!empty($data['images']) OR !empty($data['xobjects'])) {
  8885. $out .= ' /XObject <<';
  8886. foreach ($data['images'] as $imgid) {
  8887. $out .= ' /I'.$imgid.' '.$this->xobjects['I'.$imgid]['n'].' 0 R';
  8888. }
  8889. foreach ($data['xobjects'] as $sub_id => $sub_objid) {
  8890. $out .= ' /'.$sub_id.' '.$sub_objid['n'].' 0 R';
  8891. }
  8892. $out .= ' >>';
  8893. }
  8894. $out .= ' >>'; //end resources
  8895. if (isset($data['group']) AND ($data['group'] !== false)) {
  8896. // set transparency group
  8897. $out .= ' /Group << /Type /Group /S /Transparency';
  8898. if (is_array($data['group'])) {
  8899. if (isset($data['group']['CS']) AND !empty($data['group']['CS'])) {
  8900. $out .= ' /CS /'.$data['group']['CS'];
  8901. }
  8902. if (isset($data['group']['I'])) {
  8903. $out .= ' /I /'.($data['group']['I']===true?'true':'false');
  8904. }
  8905. if (isset($data['group']['K'])) {
  8906. $out .= ' /K /'.($data['group']['K']===true?'true':'false');
  8907. }
  8908. }
  8909. $out .= ' >>';
  8910. }
  8911. $stream = $this->_getrawstream($stream, $data['n']);
  8912. $out .= ' /Length '.strlen($stream);
  8913. $out .= ' >>';
  8914. $out .= ' stream'."\n".$stream."\n".'endstream';
  8915. $out .= "\n".'endobj';
  8916. $this->_out($out);
  8917. }
  8918. }
  8919. }
  8920. /**
  8921. * Output Spot Colors Resources.
  8922. * @protected
  8923. * @since 4.0.024 (2008-09-12)
  8924. */
  8925. protected function _putspotcolors() {
  8926. foreach ($this->spot_colors as $name => $color) {
  8927. $this->_newobj();
  8928. $this->spot_colors[$name]['n'] = $this->n;
  8929. $out = '[/Separation /'.str_replace(' ', '#20', $name);
  8930. $out .= ' /DeviceCMYK <<';
  8931. $out .= ' /Range [0 1 0 1 0 1 0 1] /C0 [0 0 0 0]';
  8932. $out .= ' '.sprintf('/C1 [%F %F %F %F] ', ($color['C'] / 100), ($color['M'] / 100), ($color['Y'] / 100), ($color['K'] / 100));
  8933. $out .= ' /FunctionType 2 /Domain [0 1] /N 1>>]';
  8934. $out .= "\n".'endobj';
  8935. $this->_out($out);
  8936. }
  8937. }
  8938. /**
  8939. * Return XObjects Dictionary.
  8940. * @return string XObjects dictionary
  8941. * @protected
  8942. * @since 5.8.014 (2010-08-23)
  8943. */
  8944. protected function _getxobjectdict() {
  8945. $out = '';
  8946. foreach ($this->xobjects as $id => $objid) {
  8947. $out .= ' /'.$id.' '.$objid['n'].' 0 R';
  8948. }
  8949. return $out;
  8950. }
  8951. /**
  8952. * Output Resources Dictionary.
  8953. * @protected
  8954. */
  8955. protected function _putresourcedict() {
  8956. $out = $this->_getobj(2)."\n";
  8957. $out .= '<< /ProcSet [/PDF /Text /ImageB /ImageC /ImageI]';
  8958. $out .= ' /Font <<';
  8959. foreach ($this->fontkeys as $fontkey) {
  8960. $font = $this->getFontBuffer($fontkey);
  8961. $out .= ' /F'.$font['i'].' '.$font['n'].' 0 R';
  8962. }
  8963. $out .= ' >>';
  8964. $out .= ' /XObject <<';
  8965. $out .= $this->_getxobjectdict();
  8966. $out .= ' >>';
  8967. // layers
  8968. if (!empty($this->pdflayers)) {
  8969. $out .= ' /Properties <<';
  8970. foreach ($this->pdflayers as $layer) {
  8971. $out .= ' /'.$layer['layer'].' '.$layer['objid'].' 0 R';
  8972. }
  8973. $out .= ' >>';
  8974. }
  8975. if (!$this->pdfa_mode) {
  8976. // transparency
  8977. if (isset($this->extgstates) AND !empty($this->extgstates)) {
  8978. $out .= ' /ExtGState <<';
  8979. foreach ($this->extgstates as $k => $extgstate) {
  8980. if (isset($extgstate['name'])) {
  8981. $out .= ' /'.$extgstate['name'];
  8982. } else {
  8983. $out .= ' /GS'.$k;
  8984. }
  8985. $out .= ' '.$extgstate['n'].' 0 R';
  8986. }
  8987. $out .= ' >>';
  8988. }
  8989. if (isset($this->gradients) AND !empty($this->gradients)) {
  8990. $gp = '';
  8991. $gs = '';
  8992. foreach ($this->gradients as $id => $grad) {
  8993. // gradient patterns
  8994. $gp .= ' /p'.$id.' '.$grad['pattern'].' 0 R';
  8995. // gradient shadings
  8996. $gs .= ' /Sh'.$id.' '.$grad['id'].' 0 R';
  8997. }
  8998. $out .= ' /Pattern <<'.$gp.' >>';
  8999. $out .= ' /Shading <<'.$gs.' >>';
  9000. }
  9001. }
  9002. // spot colors
  9003. if (isset($this->spot_colors) AND !empty($this->spot_colors)) {
  9004. $out .= ' /ColorSpace <<';
  9005. foreach ($this->spot_colors as $color) {
  9006. $out .= ' /CS'.$color['i'].' '.$color['n'].' 0 R';
  9007. }
  9008. $out .= ' >>';
  9009. }
  9010. $out .= ' >>';
  9011. $out .= "\n".'endobj';
  9012. $this->_out($out);
  9013. }
  9014. /**
  9015. * Output Resources.
  9016. * @protected
  9017. */
  9018. protected function _putresources() {
  9019. $this->_putextgstates();
  9020. $this->_putocg();
  9021. $this->_putfonts();
  9022. $this->_putimages();
  9023. $this->_putspotcolors();
  9024. $this->_putshaders();
  9025. $this->_putxobjects();
  9026. $this->_putresourcedict();
  9027. $this->_putdests();
  9028. $this->_putEmbeddedFiles();
  9029. $this->_putannotsobjs();
  9030. $this->_putjavascript();
  9031. $this->_putbookmarks();
  9032. $this->_putencryption();
  9033. }
  9034. /**
  9035. * Adds some Metadata information (Document Information Dictionary)
  9036. * (see Chapter 14.3.3 Document Information Dictionary of PDF32000_2008.pdf Reference)
  9037. * @return int object id
  9038. * @protected
  9039. */
  9040. protected function _putinfo() {
  9041. $oid = $this->_newobj();
  9042. $out = '<<';
  9043. // store current isunicode value
  9044. $prev_isunicode = $this->isunicode;
  9045. if ($this->docinfounicode) {
  9046. $this->isunicode = true;
  9047. }
  9048. if (!TCPDF_STATIC::empty_string($this->title)) {
  9049. // The document's title.
  9050. $out .= ' /Title '.$this->_textstring($this->title, $oid);
  9051. }
  9052. if (!TCPDF_STATIC::empty_string($this->author)) {
  9053. // The name of the person who created the document.
  9054. $out .= ' /Author '.$this->_textstring($this->author, $oid);
  9055. }
  9056. if (!TCPDF_STATIC::empty_string($this->subject)) {
  9057. // The subject of the document.
  9058. $out .= ' /Subject '.$this->_textstring($this->subject, $oid);
  9059. }
  9060. if (!TCPDF_STATIC::empty_string($this->keywords)) {
  9061. // Keywords associated with the document.
  9062. $out .= ' /Keywords '.$this->_textstring($this->keywords, $oid);
  9063. }
  9064. if (!TCPDF_STATIC::empty_string($this->creator)) {
  9065. // 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.
  9066. $out .= ' /Creator '.$this->_textstring($this->creator, $oid);
  9067. }
  9068. // restore previous isunicode value
  9069. $this->isunicode = $prev_isunicode;
  9070. // default producer
  9071. $out .= ' /Producer '.$this->_textstring(TCPDF_STATIC::getTCPDFProducer(), $oid);
  9072. // The date and time the document was created, in human-readable form
  9073. $out .= ' /CreationDate '.$this->_datestring(0, $this->doc_creation_timestamp);
  9074. // The date and time the document was most recently modified, in human-readable form
  9075. $out .= ' /ModDate '.$this->_datestring(0, $this->doc_modification_timestamp);
  9076. // A name object indicating whether the document has been modified to include trapping information
  9077. $out .= ' /Trapped /False';
  9078. $out .= ' >>';
  9079. $out .= "\n".'endobj';
  9080. $this->_out($out);
  9081. return $oid;
  9082. }
  9083. /**
  9084. * Set additional XMP data to be added on the default XMP data just before the end of "x:xmpmeta" tag.
  9085. * IMPORTANT: This data is added as-is without controls, so you have to validate your data before using this method!
  9086. * @param $xmp (string) Custom XMP data.
  9087. * @since 5.9.128 (2011-10-06)
  9088. * @public
  9089. */
  9090. public function setExtraXMP($xmp) {
  9091. $this->custom_xmp = $xmp;
  9092. }
  9093. /**
  9094. * Put XMP data object and return ID.
  9095. * @return (int) The object ID.
  9096. * @since 5.9.121 (2011-09-28)
  9097. * @protected
  9098. */
  9099. protected function _putXMP() {
  9100. $oid = $this->_newobj();
  9101. // store current isunicode value
  9102. $prev_isunicode = $this->isunicode;
  9103. $this->isunicode = true;
  9104. $prev_encrypted = $this->encrypted;
  9105. $this->encrypted = false;
  9106. // set XMP data
  9107. $xmp = '<?xpacket begin="'.TCPDF_FONTS::unichr(0xfeff, $this->isunicode).'" id="W5M0MpCehiHzreSzNTczkc9d"?>'."\n";
  9108. $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";
  9109. $xmp .= "\t".'<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">'."\n";
  9110. $xmp .= "\t\t".'<rdf:Description rdf:about="" xmlns:dc="http://purl.org/dc/elements/1.1/">'."\n";
  9111. $xmp .= "\t\t\t".'<dc:format>application/pdf</dc:format>'."\n";
  9112. $xmp .= "\t\t\t".'<dc:title>'."\n";
  9113. $xmp .= "\t\t\t\t".'<rdf:Alt>'."\n";
  9114. $xmp .= "\t\t\t\t\t".'<rdf:li xml:lang="x-default">'.TCPDF_STATIC::_escapeXML($this->title).'</rdf:li>'."\n";
  9115. $xmp .= "\t\t\t\t".'</rdf:Alt>'."\n";
  9116. $xmp .= "\t\t\t".'</dc:title>'."\n";
  9117. $xmp .= "\t\t\t".'<dc:creator>'."\n";
  9118. $xmp .= "\t\t\t\t".'<rdf:Seq>'."\n";
  9119. $xmp .= "\t\t\t\t\t".'<rdf:li>'.TCPDF_STATIC::_escapeXML($this->author).'</rdf:li>'."\n";
  9120. $xmp .= "\t\t\t\t".'</rdf:Seq>'."\n";
  9121. $xmp .= "\t\t\t".'</dc:creator>'."\n";
  9122. $xmp .= "\t\t\t".'<dc:description>'."\n";
  9123. $xmp .= "\t\t\t\t".'<rdf:Alt>'."\n";
  9124. $xmp .= "\t\t\t\t\t".'<rdf:li xml:lang="x-default">'.TCPDF_STATIC::_escapeXML($this->subject).'</rdf:li>'."\n";
  9125. $xmp .= "\t\t\t\t".'</rdf:Alt>'."\n";
  9126. $xmp .= "\t\t\t".'</dc:description>'."\n";
  9127. $xmp .= "\t\t\t".'<dc:subject>'."\n";
  9128. $xmp .= "\t\t\t\t".'<rdf:Bag>'."\n";
  9129. $xmp .= "\t\t\t\t\t".'<rdf:li>'.TCPDF_STATIC::_escapeXML($this->keywords).'</rdf:li>'."\n";
  9130. $xmp .= "\t\t\t\t".'</rdf:Bag>'."\n";
  9131. $xmp .= "\t\t\t".'</dc:subject>'."\n";
  9132. $xmp .= "\t\t".'</rdf:Description>'."\n";
  9133. // convert doc creation date format
  9134. $dcdate = TCPDF_STATIC::getFormattedDate($this->doc_creation_timestamp);
  9135. $doccreationdate = substr($dcdate, 0, 4).'-'.substr($dcdate, 4, 2).'-'.substr($dcdate, 6, 2);
  9136. $doccreationdate .= 'T'.substr($dcdate, 8, 2).':'.substr($dcdate, 10, 2).':'.substr($dcdate, 12, 2);
  9137. $doccreationdate .= substr($dcdate, 14, 3).':'.substr($dcdate, 18, 2);
  9138. $doccreationdate = TCPDF_STATIC::_escapeXML($doccreationdate);
  9139. // convert doc modification date format
  9140. $dmdate = TCPDF_STATIC::getFormattedDate($this->doc_modification_timestamp);
  9141. $docmoddate = substr($dmdate, 0, 4).'-'.substr($dmdate, 4, 2).'-'.substr($dmdate, 6, 2);
  9142. $docmoddate .= 'T'.substr($dmdate, 8, 2).':'.substr($dmdate, 10, 2).':'.substr($dmdate, 12, 2);
  9143. $docmoddate .= substr($dmdate, 14, 3).':'.substr($dmdate, 18, 2);
  9144. $docmoddate = TCPDF_STATIC::_escapeXML($docmoddate);
  9145. $xmp .= "\t\t".'<rdf:Description rdf:about="" xmlns:xmp="http://ns.adobe.com/xap/1.0/">'."\n";
  9146. $xmp .= "\t\t\t".'<xmp:CreateDate>'.$doccreationdate.'</xmp:CreateDate>'."\n";
  9147. $xmp .= "\t\t\t".'<xmp:CreatorTool>'.$this->creator.'</xmp:CreatorTool>'."\n";
  9148. $xmp .= "\t\t\t".'<xmp:ModifyDate>'.$docmoddate.'</xmp:ModifyDate>'."\n";
  9149. $xmp .= "\t\t\t".'<xmp:MetadataDate>'.$doccreationdate.'</xmp:MetadataDate>'."\n";
  9150. $xmp .= "\t\t".'</rdf:Description>'."\n";
  9151. $xmp .= "\t\t".'<rdf:Description rdf:about="" xmlns:pdf="http://ns.adobe.com/pdf/1.3/">'."\n";
  9152. $xmp .= "\t\t\t".'<pdf:Keywords>'.TCPDF_STATIC::_escapeXML($this->keywords).'</pdf:Keywords>'."\n";
  9153. $xmp .= "\t\t\t".'<pdf:Producer>'.TCPDF_STATIC::_escapeXML(TCPDF_STATIC::getTCPDFProducer()).'</pdf:Producer>'."\n";
  9154. $xmp .= "\t\t".'</rdf:Description>'."\n";
  9155. $xmp .= "\t\t".'<rdf:Description rdf:about="" xmlns:xmpMM="http://ns.adobe.com/xap/1.0/mm/">'."\n";
  9156. $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);
  9157. $xmp .= "\t\t\t".'<xmpMM:DocumentID>'.$uuid.'</xmpMM:DocumentID>'."\n";
  9158. $xmp .= "\t\t\t".'<xmpMM:InstanceID>'.$uuid.'</xmpMM:InstanceID>'."\n";
  9159. $xmp .= "\t\t".'</rdf:Description>'."\n";
  9160. if ($this->pdfa_mode) {
  9161. $xmp .= "\t\t".'<rdf:Description rdf:about="" xmlns:pdfaid="http://www.aiim.org/pdfa/ns/id/">'."\n";
  9162. $xmp .= "\t\t\t".'<pdfaid:part>1</pdfaid:part>'."\n";
  9163. $xmp .= "\t\t\t".'<pdfaid:conformance>B</pdfaid:conformance>'."\n";
  9164. $xmp .= "\t\t".'</rdf:Description>'."\n";
  9165. }
  9166. // XMP extension schemas
  9167. $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";
  9168. $xmp .= "\t\t\t".'<pdfaExtension:schemas>'."\n";
  9169. $xmp .= "\t\t\t\t".'<rdf:Bag>'."\n";
  9170. $xmp .= "\t\t\t\t\t".'<rdf:li rdf:parseType="Resource">'."\n";
  9171. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:namespaceURI>http://ns.adobe.com/pdf/1.3/</pdfaSchema:namespaceURI>'."\n";
  9172. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:prefix>pdf</pdfaSchema:prefix>'."\n";
  9173. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:schema>Adobe PDF Schema</pdfaSchema:schema>'."\n";
  9174. $xmp .= "\t\t\t\t\t".'</rdf:li>'."\n";
  9175. $xmp .= "\t\t\t\t\t".'<rdf:li rdf:parseType="Resource">'."\n";
  9176. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:namespaceURI>http://ns.adobe.com/xap/1.0/mm/</pdfaSchema:namespaceURI>'."\n";
  9177. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:prefix>xmpMM</pdfaSchema:prefix>'."\n";
  9178. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:schema>XMP Media Management Schema</pdfaSchema:schema>'."\n";
  9179. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:property>'."\n";
  9180. $xmp .= "\t\t\t\t\t\t\t".'<rdf:Seq>'."\n";
  9181. $xmp .= "\t\t\t\t\t\t\t\t".'<rdf:li rdf:parseType="Resource">'."\n";
  9182. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:category>internal</pdfaProperty:category>'."\n";
  9183. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:description>UUID based identifier for specific incarnation of a document</pdfaProperty:description>'."\n";
  9184. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:name>InstanceID</pdfaProperty:name>'."\n";
  9185. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:valueType>URI</pdfaProperty:valueType>'."\n";
  9186. $xmp .= "\t\t\t\t\t\t\t\t".'</rdf:li>'."\n";
  9187. $xmp .= "\t\t\t\t\t\t\t".'</rdf:Seq>'."\n";
  9188. $xmp .= "\t\t\t\t\t\t".'</pdfaSchema:property>'."\n";
  9189. $xmp .= "\t\t\t\t\t".'</rdf:li>'."\n";
  9190. $xmp .= "\t\t\t\t\t".'<rdf:li rdf:parseType="Resource">'."\n";
  9191. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:namespaceURI>http://www.aiim.org/pdfa/ns/id/</pdfaSchema:namespaceURI>'."\n";
  9192. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:prefix>pdfaid</pdfaSchema:prefix>'."\n";
  9193. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:schema>PDF/A ID Schema</pdfaSchema:schema>'."\n";
  9194. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:property>'."\n";
  9195. $xmp .= "\t\t\t\t\t\t\t".'<rdf:Seq>'."\n";
  9196. $xmp .= "\t\t\t\t\t\t\t\t".'<rdf:li rdf:parseType="Resource">'."\n";
  9197. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:category>internal</pdfaProperty:category>'."\n";
  9198. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:description>Part of PDF/A standard</pdfaProperty:description>'."\n";
  9199. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:name>part</pdfaProperty:name>'."\n";
  9200. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:valueType>Integer</pdfaProperty:valueType>'."\n";
  9201. $xmp .= "\t\t\t\t\t\t\t\t".'</rdf:li>'."\n";
  9202. $xmp .= "\t\t\t\t\t\t\t\t".'<rdf:li rdf:parseType="Resource">'."\n";
  9203. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:category>internal</pdfaProperty:category>'."\n";
  9204. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:description>Amendment of PDF/A standard</pdfaProperty:description>'."\n";
  9205. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:name>amd</pdfaProperty:name>'."\n";
  9206. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:valueType>Text</pdfaProperty:valueType>'."\n";
  9207. $xmp .= "\t\t\t\t\t\t\t\t".'</rdf:li>'."\n";
  9208. $xmp .= "\t\t\t\t\t\t\t\t".'<rdf:li rdf:parseType="Resource">'."\n";
  9209. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:category>internal</pdfaProperty:category>'."\n";
  9210. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:description>Conformance level of PDF/A standard</pdfaProperty:description>'."\n";
  9211. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:name>conformance</pdfaProperty:name>'."\n";
  9212. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:valueType>Text</pdfaProperty:valueType>'."\n";
  9213. $xmp .= "\t\t\t\t\t\t\t\t".'</rdf:li>'."\n";
  9214. $xmp .= "\t\t\t\t\t\t\t".'</rdf:Seq>'."\n";
  9215. $xmp .= "\t\t\t\t\t\t".'</pdfaSchema:property>'."\n";
  9216. $xmp .= "\t\t\t\t\t".'</rdf:li>'."\n";
  9217. $xmp .= "\t\t\t\t".'</rdf:Bag>'."\n";
  9218. $xmp .= "\t\t\t".'</pdfaExtension:schemas>'."\n";
  9219. $xmp .= "\t\t".'</rdf:Description>'."\n";
  9220. $xmp .= "\t".'</rdf:RDF>'."\n";
  9221. $xmp .= $this->custom_xmp;
  9222. $xmp .= '</x:xmpmeta>'."\n";
  9223. $xmp .= '<?xpacket end="w"?>';
  9224. $out = '<< /Type /Metadata /Subtype /XML /Length '.strlen($xmp).' >> stream'."\n".$xmp."\n".'endstream'."\n".'endobj';
  9225. // restore previous isunicode value
  9226. $this->isunicode = $prev_isunicode;
  9227. $this->encrypted = $prev_encrypted;
  9228. $this->_out($out);
  9229. return $oid;
  9230. }
  9231. /**
  9232. * Output Catalog.
  9233. * @return int object id
  9234. * @protected
  9235. */
  9236. protected function _putcatalog() {
  9237. // put XMP
  9238. $xmpobj = $this->_putXMP();
  9239. // if required, add standard sRGB_IEC61966-2.1 blackscaled ICC colour profile
  9240. if ($this->pdfa_mode OR $this->force_srgb) {
  9241. $iccobj = $this->_newobj();
  9242. $icc = file_get_contents(dirname(__FILE__).'/include/sRGB.icc');
  9243. $filter = '';
  9244. if ($this->compress) {
  9245. $filter = ' /Filter /FlateDecode';
  9246. $icc = gzcompress($icc);
  9247. }
  9248. $icc = $this->_getrawstream($icc);
  9249. $this->_out('<</N 3 '.$filter.'/Length '.strlen($icc).'>> stream'."\n".$icc."\n".'endstream'."\n".'endobj');
  9250. }
  9251. // start catalog
  9252. $oid = $this->_newobj();
  9253. $out = '<< /Type /Catalog';
  9254. $out .= ' /Version /'.$this->PDFVersion;
  9255. //$out .= ' /Extensions <<>>';
  9256. $out .= ' /Pages 1 0 R';
  9257. //$out .= ' /PageLabels ' //...;
  9258. $out .= ' /Names <<';
  9259. if ((!$this->pdfa_mode) AND !empty($this->n_js)) {
  9260. $out .= ' /JavaScript '.$this->n_js;
  9261. }
  9262. if (!empty($this->efnames)) {
  9263. $out .= ' /EmbeddedFiles <</Names [';
  9264. foreach ($this->efnames AS $fn => $fref) {
  9265. $out .= ' '.$this->_datastring($fn).' '.$fref;
  9266. }
  9267. $out .= ' ]>>';
  9268. }
  9269. $out .= ' >>';
  9270. if (!empty($this->dests)) {
  9271. $out .= ' /Dests '.($this->n_dests).' 0 R';
  9272. }
  9273. $out .= $this->_putviewerpreferences();
  9274. if (isset($this->LayoutMode) AND (!TCPDF_STATIC::empty_string($this->LayoutMode))) {
  9275. $out .= ' /PageLayout /'.$this->LayoutMode;
  9276. }
  9277. if (isset($this->PageMode) AND (!TCPDF_STATIC::empty_string($this->PageMode))) {
  9278. $out .= ' /PageMode /'.$this->PageMode;
  9279. }
  9280. if (count($this->outlines) > 0) {
  9281. $out .= ' /Outlines '.$this->OutlineRoot.' 0 R';
  9282. $out .= ' /PageMode /UseOutlines';
  9283. }
  9284. //$out .= ' /Threads []';
  9285. if ($this->ZoomMode == 'fullpage') {
  9286. $out .= ' /OpenAction ['.$this->page_obj_id[1].' 0 R /Fit]';
  9287. } elseif ($this->ZoomMode == 'fullwidth') {
  9288. $out .= ' /OpenAction ['.$this->page_obj_id[1].' 0 R /FitH null]';
  9289. } elseif ($this->ZoomMode == 'real') {
  9290. $out .= ' /OpenAction ['.$this->page_obj_id[1].' 0 R /XYZ null null 1]';
  9291. } elseif (!is_string($this->ZoomMode)) {
  9292. $out .= sprintf(' /OpenAction ['.$this->page_obj_id[1].' 0 R /XYZ null null %F]', ($this->ZoomMode / 100));
  9293. }
  9294. //$out .= ' /AA <<>>';
  9295. //$out .= ' /URI <<>>';
  9296. $out .= ' /Metadata '.$xmpobj.' 0 R';
  9297. //$out .= ' /StructTreeRoot <<>>';
  9298. //$out .= ' /MarkInfo <<>>';
  9299. if (isset($this->l['a_meta_language'])) {
  9300. $out .= ' /Lang '.$this->_textstring($this->l['a_meta_language'], $oid);
  9301. }
  9302. //$out .= ' /SpiderInfo <<>>';
  9303. // set OutputIntent to sRGB IEC61966-2.1 if required
  9304. if ($this->pdfa_mode OR $this->force_srgb) {
  9305. $out .= ' /OutputIntents [<<';
  9306. $out .= ' /Type /OutputIntent';
  9307. $out .= ' /S /GTS_PDFA1';
  9308. $out .= ' /OutputCondition '.$this->_textstring('sRGB IEC61966-2.1', $oid);
  9309. $out .= ' /OutputConditionIdentifier '.$this->_textstring('sRGB IEC61966-2.1', $oid);
  9310. $out .= ' /RegistryName '.$this->_textstring('http://www.color.org', $oid);
  9311. $out .= ' /Info '.$this->_textstring('sRGB IEC61966-2.1', $oid);
  9312. $out .= ' /DestOutputProfile '.$iccobj.' 0 R';
  9313. $out .= ' >>]';
  9314. }
  9315. //$out .= ' /PieceInfo <<>>';
  9316. if (!empty($this->pdflayers)) {
  9317. $lyrobjs = '';
  9318. $lyrobjs_off = '';
  9319. $lyrobjs_lock = '';
  9320. foreach ($this->pdflayers as $layer) {
  9321. $layer_obj_ref = ' '.$layer['objid'].' 0 R';
  9322. $lyrobjs .= $layer_obj_ref;
  9323. if ($layer['view'] === false) {
  9324. $lyrobjs_off .= $layer_obj_ref;
  9325. }
  9326. if ($layer['lock']) {
  9327. $lyrobjs_lock .= $layer_obj_ref;
  9328. }
  9329. }
  9330. $out .= ' /OCProperties << /OCGs ['.$lyrobjs.']';
  9331. $out .= ' /D <<';
  9332. $out .= ' /Name '.$this->_textstring('Layers', $oid);
  9333. $out .= ' /Creator '.$this->_textstring('TCPDF', $oid);
  9334. $out .= ' /BaseState /ON';
  9335. $out .= ' /OFF ['.$lyrobjs_off.']';
  9336. $out .= ' /Locked ['.$lyrobjs_lock.']';
  9337. $out .= ' /Intent /View';
  9338. $out .= ' /AS [';
  9339. $out .= ' << /Event /Print /OCGs ['.$lyrobjs.'] /Category [/Print] >>';
  9340. $out .= ' << /Event /View /OCGs ['.$lyrobjs.'] /Category [/View] >>';
  9341. $out .= ' ]';
  9342. $out .= ' /Order ['.$lyrobjs.']';
  9343. $out .= ' /ListMode /AllPages';
  9344. //$out .= ' /RBGroups ['..']';
  9345. //$out .= ' /Locked ['..']';
  9346. $out .= ' >>';
  9347. $out .= ' >>';
  9348. }
  9349. // AcroForm
  9350. if (!empty($this->form_obj_id)
  9351. OR ($this->sign AND isset($this->signature_data['cert_type']))
  9352. OR !empty($this->empty_signature_appearance)) {
  9353. $out .= ' /AcroForm <<';
  9354. $objrefs = '';
  9355. if ($this->sign AND isset($this->signature_data['cert_type'])) {
  9356. // set reference for signature object
  9357. $objrefs .= $this->sig_obj_id.' 0 R';
  9358. }
  9359. if (!empty($this->empty_signature_appearance)) {
  9360. foreach ($this->empty_signature_appearance as $esa) {
  9361. // set reference for empty signature objects
  9362. $objrefs .= ' '.$esa['objid'].' 0 R';
  9363. }
  9364. }
  9365. if (!empty($this->form_obj_id)) {
  9366. foreach($this->form_obj_id as $objid) {
  9367. $objrefs .= ' '.$objid.' 0 R';
  9368. }
  9369. }
  9370. $out .= ' /Fields ['.$objrefs.']';
  9371. // It's better to turn off this value and set the appearance stream for each annotation (/AP) to avoid conflicts with signature fields.
  9372. if (empty($this->signature_data['approval']) OR ($this->signature_data['approval'] != 'A')) {
  9373. $out .= ' /NeedAppearances false';
  9374. }
  9375. if ($this->sign AND isset($this->signature_data['cert_type'])) {
  9376. if ($this->signature_data['cert_type'] > 0) {
  9377. $out .= ' /SigFlags 3';
  9378. } else {
  9379. $out .= ' /SigFlags 1';
  9380. }
  9381. }
  9382. //$out .= ' /CO ';
  9383. if (isset($this->annotation_fonts) AND !empty($this->annotation_fonts)) {
  9384. $out .= ' /DR <<';
  9385. $out .= ' /Font <<';
  9386. foreach ($this->annotation_fonts as $fontkey => $fontid) {
  9387. $out .= ' /F'.$fontid.' '.$this->font_obj_ids[$fontkey].' 0 R';
  9388. }
  9389. $out .= ' >> >>';
  9390. }
  9391. $font = $this->getFontBuffer('helvetica');
  9392. $out .= ' /DA (/F'.$font['i'].' 0 Tf 0 g)';
  9393. $out .= ' /Q '.(($this->rtl)?'2':'0');
  9394. //$out .= ' /XFA ';
  9395. $out .= ' >>';
  9396. // signatures
  9397. if ($this->sign AND isset($this->signature_data['cert_type'])
  9398. AND (empty($this->signature_data['approval']) OR ($this->signature_data['approval'] != 'A'))) {
  9399. if ($this->signature_data['cert_type'] > 0) {
  9400. $out .= ' /Perms << /DocMDP '.($this->sig_obj_id + 1).' 0 R >>';
  9401. } else {
  9402. $out .= ' /Perms << /UR3 '.($this->sig_obj_id + 1).' 0 R >>';
  9403. }
  9404. }
  9405. }
  9406. //$out .= ' /Legal <<>>';
  9407. //$out .= ' /Requirements []';
  9408. //$out .= ' /Collection <<>>';
  9409. //$out .= ' /NeedsRendering true';
  9410. $out .= ' >>';
  9411. $out .= "\n".'endobj';
  9412. $this->_out($out);
  9413. return $oid;
  9414. }
  9415. /**
  9416. * Output viewer preferences.
  9417. * @return string for viewer preferences
  9418. * @author Nicola asuni
  9419. * @since 3.1.000 (2008-06-09)
  9420. * @protected
  9421. */
  9422. protected function _putviewerpreferences() {
  9423. $vp = $this->viewer_preferences;
  9424. $out = ' /ViewerPreferences <<';
  9425. if ($this->rtl) {
  9426. $out .= ' /Direction /R2L';
  9427. } else {
  9428. $out .= ' /Direction /L2R';
  9429. }
  9430. if (isset($vp['HideToolbar']) AND ($vp['HideToolbar'])) {
  9431. $out .= ' /HideToolbar true';
  9432. }
  9433. if (isset($vp['HideMenubar']) AND ($vp['HideMenubar'])) {
  9434. $out .= ' /HideMenubar true';
  9435. }
  9436. if (isset($vp['HideWindowUI']) AND ($vp['HideWindowUI'])) {
  9437. $out .= ' /HideWindowUI true';
  9438. }
  9439. if (isset($vp['FitWindow']) AND ($vp['FitWindow'])) {
  9440. $out .= ' /FitWindow true';
  9441. }
  9442. if (isset($vp['CenterWindow']) AND ($vp['CenterWindow'])) {
  9443. $out .= ' /CenterWindow true';
  9444. }
  9445. if (isset($vp['DisplayDocTitle']) AND ($vp['DisplayDocTitle'])) {
  9446. $out .= ' /DisplayDocTitle true';
  9447. }
  9448. if (isset($vp['NonFullScreenPageMode'])) {
  9449. $out .= ' /NonFullScreenPageMode /'.$vp['NonFullScreenPageMode'];
  9450. }
  9451. if (isset($vp['ViewArea'])) {
  9452. $out .= ' /ViewArea /'.$vp['ViewArea'];
  9453. }
  9454. if (isset($vp['ViewClip'])) {
  9455. $out .= ' /ViewClip /'.$vp['ViewClip'];
  9456. }
  9457. if (isset($vp['PrintArea'])) {
  9458. $out .= ' /PrintArea /'.$vp['PrintArea'];
  9459. }
  9460. if (isset($vp['PrintClip'])) {
  9461. $out .= ' /PrintClip /'.$vp['PrintClip'];
  9462. }
  9463. if (isset($vp['PrintScaling'])) {
  9464. $out .= ' /PrintScaling /'.$vp['PrintScaling'];
  9465. }
  9466. if (isset($vp['Duplex']) AND (!TCPDF_STATIC::empty_string($vp['Duplex']))) {
  9467. $out .= ' /Duplex /'.$vp['Duplex'];
  9468. }
  9469. if (isset($vp['PickTrayByPDFSize'])) {
  9470. if ($vp['PickTrayByPDFSize']) {
  9471. $out .= ' /PickTrayByPDFSize true';
  9472. } else {
  9473. $out .= ' /PickTrayByPDFSize false';
  9474. }
  9475. }
  9476. if (isset($vp['PrintPageRange'])) {
  9477. $PrintPageRangeNum = '';
  9478. foreach ($vp['PrintPageRange'] as $k => $v) {
  9479. $PrintPageRangeNum .= ' '.($v - 1).'';
  9480. }
  9481. $out .= ' /PrintPageRange ['.substr($PrintPageRangeNum,1).']';
  9482. }
  9483. if (isset($vp['NumCopies'])) {
  9484. $out .= ' /NumCopies '.intval($vp['NumCopies']);
  9485. }
  9486. $out .= ' >>';
  9487. return $out;
  9488. }
  9489. /**
  9490. * Output PDF File Header (7.5.2).
  9491. * @protected
  9492. */
  9493. protected function _putheader() {
  9494. $this->_out('%PDF-'.$this->PDFVersion);
  9495. $this->_out('%'.chr(0xe2).chr(0xe3).chr(0xcf).chr(0xd3));
  9496. }
  9497. /**
  9498. * Output end of document (EOF).
  9499. * @protected
  9500. */
  9501. protected function _enddoc() {
  9502. if (isset($this->CurrentFont['fontkey']) AND isset($this->CurrentFont['subsetchars'])) {
  9503. // save subset chars of the previous font
  9504. $this->setFontSubBuffer($this->CurrentFont['fontkey'], 'subsetchars', $this->CurrentFont['subsetchars']);
  9505. }
  9506. $this->state = 1;
  9507. $this->_putheader();
  9508. $this->_putpages();
  9509. $this->_putresources();
  9510. // empty signature fields
  9511. if (!empty($this->empty_signature_appearance)) {
  9512. foreach ($this->empty_signature_appearance as $key => $esa) {
  9513. // widget annotation for empty signature
  9514. $out = $this->_getobj($esa['objid'])."\n";
  9515. $out .= '<< /Type /Annot';
  9516. $out .= ' /Subtype /Widget';
  9517. $out .= ' /Rect ['.$esa['rect'].']';
  9518. $out .= ' /P '.$this->page_obj_id[($esa['page'])].' 0 R'; // link to signature appearance page
  9519. $out .= ' /F 4';
  9520. $out .= ' /FT /Sig';
  9521. $signame = $esa['name'].sprintf(' [%03d]', ($key + 1));
  9522. $out .= ' /T '.$this->_textstring($signame, $esa['objid']);
  9523. $out .= ' /Ff 0';
  9524. $out .= ' >>';
  9525. $out .= "\n".'endobj';
  9526. $this->_out($out);
  9527. }
  9528. }
  9529. // Signature
  9530. if ($this->sign AND isset($this->signature_data['cert_type'])) {
  9531. // widget annotation for signature
  9532. $out = $this->_getobj($this->sig_obj_id)."\n";
  9533. $out .= '<< /Type /Annot';
  9534. $out .= ' /Subtype /Widget';
  9535. $out .= ' /Rect ['.$this->signature_appearance['rect'].']';
  9536. $out .= ' /P '.$this->page_obj_id[($this->signature_appearance['page'])].' 0 R'; // link to signature appearance page
  9537. $out .= ' /F 4';
  9538. $out .= ' /FT /Sig';
  9539. $out .= ' /T '.$this->_textstring($this->signature_appearance['name'], $this->sig_obj_id);
  9540. $out .= ' /Ff 0';
  9541. $out .= ' /V '.($this->sig_obj_id + 1).' 0 R';
  9542. $out .= ' >>';
  9543. $out .= "\n".'endobj';
  9544. $this->_out($out);
  9545. // signature
  9546. $this->_putsignature();
  9547. }
  9548. // Info
  9549. $objid_info = $this->_putinfo();
  9550. // Catalog
  9551. $objid_catalog = $this->_putcatalog();
  9552. // Cross-ref
  9553. $o = $this->bufferlen;
  9554. // XREF section
  9555. $this->_out('xref');
  9556. $this->_out('0 '.($this->n + 1));
  9557. $this->_out('0000000000 65535 f ');
  9558. $freegen = ($this->n + 2);
  9559. for ($i=1; $i <= $this->n; ++$i) {
  9560. if (!isset($this->offsets[$i]) AND ($i > 1)) {
  9561. $this->_out(sprintf('0000000000 %05d f ', $freegen));
  9562. ++$freegen;
  9563. } else {
  9564. $this->_out(sprintf('%010d 00000 n ', $this->offsets[$i]));
  9565. }
  9566. }
  9567. // TRAILER
  9568. $out = 'trailer'."\n";
  9569. $out .= '<<';
  9570. $out .= ' /Size '.($this->n + 1);
  9571. $out .= ' /Root '.$objid_catalog.' 0 R';
  9572. $out .= ' /Info '.$objid_info.' 0 R';
  9573. if ($this->encrypted) {
  9574. $out .= ' /Encrypt '.$this->encryptdata['objid'].' 0 R';
  9575. }
  9576. $out .= ' /ID [ <'.$this->file_id.'> <'.$this->file_id.'> ]';
  9577. $out .= ' >>';
  9578. $this->_out($out);
  9579. $this->_out('startxref');
  9580. $this->_out($o);
  9581. $this->_out('%%EOF');
  9582. $this->state = 3; // end-of-doc
  9583. }
  9584. /**
  9585. * Initialize a new page.
  9586. * @param $orientation (string) page orientation. Possible values are (case insensitive):<ul><li>P or PORTRAIT (default)</li><li>L or LANDSCAPE</li></ul>
  9587. * @param $format (mixed) 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().
  9588. * @protected
  9589. * @see getPageSizeFromFormat(), setPageFormat()
  9590. */
  9591. protected function _beginpage($orientation='', $format='') {
  9592. ++$this->page;
  9593. $this->pageobjects[$this->page] = array();
  9594. $this->setPageBuffer($this->page, '');
  9595. // initialize array for graphics tranformation positions inside a page buffer
  9596. $this->transfmrk[$this->page] = array();
  9597. $this->state = 2;
  9598. if (TCPDF_STATIC::empty_string($orientation)) {
  9599. if (isset($this->CurOrientation)) {
  9600. $orientation = $this->CurOrientation;
  9601. } elseif ($this->fwPt > $this->fhPt) {
  9602. // landscape
  9603. $orientation = 'L';
  9604. } else {
  9605. // portrait
  9606. $orientation = 'P';
  9607. }
  9608. }
  9609. if (TCPDF_STATIC::empty_string($format)) {
  9610. $this->pagedim[$this->page] = $this->pagedim[($this->page - 1)];
  9611. $this->setPageOrientation($orientation);
  9612. } else {
  9613. $this->setPageFormat($format, $orientation);
  9614. }
  9615. if ($this->rtl) {
  9616. $this->x = $this->w - $this->rMargin;
  9617. } else {
  9618. $this->x = $this->lMargin;
  9619. }
  9620. $this->y = $this->tMargin;
  9621. if (isset($this->newpagegroup[$this->page])) {
  9622. // start a new group
  9623. $this->currpagegroup = $this->newpagegroup[$this->page];
  9624. $this->pagegroups[$this->currpagegroup] = 1;
  9625. } elseif (isset($this->currpagegroup) AND ($this->currpagegroup > 0)) {
  9626. ++$this->pagegroups[$this->currpagegroup];
  9627. }
  9628. }
  9629. /**
  9630. * Mark end of page.
  9631. * @protected
  9632. */
  9633. protected function _endpage() {
  9634. $this->setVisibility('all');
  9635. $this->state = 1;
  9636. }
  9637. /**
  9638. * Begin a new object and return the object number.
  9639. * @return int object number
  9640. * @protected
  9641. */
  9642. protected function _newobj() {
  9643. $this->_out($this->_getobj());
  9644. return $this->n;
  9645. }
  9646. /**
  9647. * Return the starting object string for the selected object ID.
  9648. * @param $objid (int) Object ID (leave empty to get a new ID).
  9649. * @return string the starting object string
  9650. * @protected
  9651. * @since 5.8.009 (2010-08-20)
  9652. */
  9653. protected function _getobj($objid='') {
  9654. if ($objid === '') {
  9655. ++$this->n;
  9656. $objid = $this->n;
  9657. }
  9658. $this->offsets[$objid] = $this->bufferlen;
  9659. $this->pageobjects[$this->page][] = $objid;
  9660. return $objid.' 0 obj';
  9661. }
  9662. /**
  9663. * Underline text.
  9664. * @param $x (int) X coordinate
  9665. * @param $y (int) Y coordinate
  9666. * @param $txt (string) text to underline
  9667. * @protected
  9668. */
  9669. protected function _dounderline($x, $y, $txt) {
  9670. $w = $this->GetStringWidth($txt);
  9671. return $this->_dounderlinew($x, $y, $w);
  9672. }
  9673. /**
  9674. * Underline for rectangular text area.
  9675. * @param $x (int) X coordinate
  9676. * @param $y (int) Y coordinate
  9677. * @param $w (int) width to underline
  9678. * @protected
  9679. * @since 4.8.008 (2009-09-29)
  9680. */
  9681. protected function _dounderlinew($x, $y, $w) {
  9682. $linew = - $this->CurrentFont['ut'] / 1000 * $this->FontSizePt;
  9683. return sprintf('%F %F %F %F re f', $x * $this->k, ((($this->h - $y) * $this->k) + $linew), $w * $this->k, $linew);
  9684. }
  9685. /**
  9686. * Line through text.
  9687. * @param $x (int) X coordinate
  9688. * @param $y (int) Y coordinate
  9689. * @param $txt (string) text to linethrough
  9690. * @protected
  9691. */
  9692. protected function _dolinethrough($x, $y, $txt) {
  9693. $w = $this->GetStringWidth($txt);
  9694. return $this->_dolinethroughw($x, $y, $w);
  9695. }
  9696. /**
  9697. * Line through for rectangular text area.
  9698. * @param $x (int) X coordinate
  9699. * @param $y (int) Y coordinate
  9700. * @param $w (int) line length (width)
  9701. * @protected
  9702. * @since 4.9.008 (2009-09-29)
  9703. */
  9704. protected function _dolinethroughw($x, $y, $w) {
  9705. $linew = - $this->CurrentFont['ut'] / 1000 * $this->FontSizePt;
  9706. return sprintf('%F %F %F %F re f', $x * $this->k, ((($this->h - $y) * $this->k) + $linew + ($this->FontSizePt / 3)), $w * $this->k, $linew);
  9707. }
  9708. /**
  9709. * Overline text.
  9710. * @param $x (int) X coordinate
  9711. * @param $y (int) Y coordinate
  9712. * @param $txt (string) text to overline
  9713. * @protected
  9714. * @since 4.9.015 (2010-04-19)
  9715. */
  9716. protected function _dooverline($x, $y, $txt) {
  9717. $w = $this->GetStringWidth($txt);
  9718. return $this->_dooverlinew($x, $y, $w);
  9719. }
  9720. /**
  9721. * Overline for rectangular text area.
  9722. * @param $x (int) X coordinate
  9723. * @param $y (int) Y coordinate
  9724. * @param $w (int) width to overline
  9725. * @protected
  9726. * @since 4.9.015 (2010-04-19)
  9727. */
  9728. protected function _dooverlinew($x, $y, $w) {
  9729. $linew = - $this->CurrentFont['ut'] / 1000 * $this->FontSizePt;
  9730. return sprintf('%F %F %F %F re f', $x * $this->k, (($this->h - $y + $this->FontAscent) * $this->k) - $linew, $w * $this->k, $linew);
  9731. }
  9732. /**
  9733. * Format a data string for meta information
  9734. * @param $s (string) data string to escape.
  9735. * @param $n (int) object ID
  9736. * @return string escaped string.
  9737. * @protected
  9738. */
  9739. protected function _datastring($s, $n=0) {
  9740. if ($n == 0) {
  9741. $n = $this->n;
  9742. }
  9743. $s = $this->_encrypt_data($n, $s);
  9744. return '('. TCPDF_STATIC::_escape($s).')';
  9745. }
  9746. /**
  9747. * Set the document creation timestamp
  9748. * @param $time (mixed) Document creation timestamp in seconds or date-time string.
  9749. * @public
  9750. * @since 5.9.152 (2012-03-23)
  9751. */
  9752. public function setDocCreationTimestamp($time) {
  9753. if (is_string($time)) {
  9754. $time = TCPDF_STATIC::getTimestamp($time);
  9755. }
  9756. $this->doc_creation_timestamp = intval($time);
  9757. }
  9758. /**
  9759. * Set the document modification timestamp
  9760. * @param $time (mixed) Document modification timestamp in seconds or date-time string.
  9761. * @public
  9762. * @since 5.9.152 (2012-03-23)
  9763. */
  9764. public function setDocModificationTimestamp($time) {
  9765. if (is_string($time)) {
  9766. $time = TCPDF_STATIC::getTimestamp($time);
  9767. }
  9768. $this->doc_modification_timestamp = intval($time);
  9769. }
  9770. /**
  9771. * Returns document creation timestamp in seconds.
  9772. * @return (int) Creation timestamp in seconds.
  9773. * @public
  9774. * @since 5.9.152 (2012-03-23)
  9775. */
  9776. public function getDocCreationTimestamp() {
  9777. return $this->doc_creation_timestamp;
  9778. }
  9779. /**
  9780. * Returns document modification timestamp in seconds.
  9781. * @return (int) Modfication timestamp in seconds.
  9782. * @public
  9783. * @since 5.9.152 (2012-03-23)
  9784. */
  9785. public function getDocModificationTimestamp() {
  9786. return $this->doc_modification_timestamp;
  9787. }
  9788. /**
  9789. * Returns a formatted date for meta information
  9790. * @param $n (int) Object ID.
  9791. * @param $timestamp (int) Timestamp to convert.
  9792. * @return string escaped date string.
  9793. * @protected
  9794. * @since 4.6.028 (2009-08-25)
  9795. */
  9796. protected function _datestring($n=0, $timestamp=0) {
  9797. if ((empty($timestamp)) OR ($timestamp < 0)) {
  9798. $timestamp = $this->doc_creation_timestamp;
  9799. }
  9800. return $this->_datastring('D:'.TCPDF_STATIC::getFormattedDate($timestamp), $n);
  9801. }
  9802. /**
  9803. * Format a text string for meta information
  9804. * @param $s (string) string to escape.
  9805. * @param $n (int) object ID
  9806. * @return string escaped string.
  9807. * @protected
  9808. */
  9809. protected function _textstring($s, $n=0) {
  9810. if ($this->isunicode) {
  9811. //Convert string to UTF-16BE
  9812. $s = TCPDF_FONTS::UTF8ToUTF16BE($s, true, $this->isunicode, $this->CurrentFont);
  9813. }
  9814. return $this->_datastring($s, $n);
  9815. }
  9816. /**
  9817. * get raw output stream.
  9818. * @param $s (string) string to output.
  9819. * @param $n (int) object reference for encryption mode
  9820. * @protected
  9821. * @author Nicola Asuni
  9822. * @since 5.5.000 (2010-06-22)
  9823. */
  9824. protected function _getrawstream($s, $n=0) {
  9825. if ($n <= 0) {
  9826. // default to current object
  9827. $n = $this->n;
  9828. }
  9829. return $this->_encrypt_data($n, $s);
  9830. }
  9831. /**
  9832. * Output a string to the document.
  9833. * @param $s (string) string to output.
  9834. * @protected
  9835. */
  9836. protected function _out($s) {
  9837. if ($this->state == 2) {
  9838. if ($this->inxobj) {
  9839. // we are inside an XObject template
  9840. $this->xobjects[$this->xobjid]['outdata'] .= $s."\n";
  9841. } elseif ((!$this->InFooter) AND isset($this->footerlen[$this->page]) AND ($this->footerlen[$this->page] > 0)) {
  9842. // puts data before page footer
  9843. $pagebuff = $this->getPageBuffer($this->page);
  9844. $page = substr($pagebuff, 0, -$this->footerlen[$this->page]);
  9845. $footer = substr($pagebuff, -$this->footerlen[$this->page]);
  9846. $this->setPageBuffer($this->page, $page.$s."\n".$footer);
  9847. // update footer position
  9848. $this->footerpos[$this->page] += strlen($s."\n");
  9849. } else {
  9850. // set page data
  9851. $this->setPageBuffer($this->page, $s."\n", true);
  9852. }
  9853. } elseif ($this->state > 0) {
  9854. // set general data
  9855. $this->setBuffer($s."\n");
  9856. }
  9857. }
  9858. /**
  9859. * Set header font.
  9860. * @param $font (array) Array describing the basic font parameters: (family, style, size).
  9861. * @public
  9862. * @since 1.1
  9863. */
  9864. public function setHeaderFont($font) {
  9865. $this->header_font = $font;
  9866. }
  9867. /**
  9868. * Get header font.
  9869. * @return array() Array describing the basic font parameters: (family, style, size).
  9870. * @public
  9871. * @since 4.0.012 (2008-07-24)
  9872. */
  9873. public function getHeaderFont() {
  9874. return $this->header_font;
  9875. }
  9876. /**
  9877. * Set footer font.
  9878. * @param $font (array) Array describing the basic font parameters: (family, style, size).
  9879. * @public
  9880. * @since 1.1
  9881. */
  9882. public function setFooterFont($font) {
  9883. $this->footer_font = $font;
  9884. }
  9885. /**
  9886. * Get Footer font.
  9887. * @return array() Array describing the basic font parameters: (family, style, size).
  9888. * @public
  9889. * @since 4.0.012 (2008-07-24)
  9890. */
  9891. public function getFooterFont() {
  9892. return $this->footer_font;
  9893. }
  9894. /**
  9895. * Set language array.
  9896. * @param $language (array)
  9897. * @public
  9898. * @since 1.1
  9899. */
  9900. public function setLanguageArray($language) {
  9901. $this->l = $language;
  9902. if (isset($this->l['a_meta_dir'])) {
  9903. $this->rtl = $this->l['a_meta_dir']=='rtl' ? true : false;
  9904. } else {
  9905. $this->rtl = false;
  9906. }
  9907. }
  9908. /**
  9909. * Returns the PDF data.
  9910. * @public
  9911. */
  9912. public function getPDFData() {
  9913. if ($this->state < 3) {
  9914. $this->Close();
  9915. }
  9916. return $this->buffer;
  9917. }
  9918. /**
  9919. * Output anchor link.
  9920. * @param $url (string) 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;)
  9921. * @param $name (string) link name
  9922. * @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
  9923. * @param $firstline (boolean) if true prints only the first line and return the remaining string.
  9924. * @param $color (array) array of RGB text color
  9925. * @param $style (string) font style (U, D, B, I)
  9926. * @param $firstblock (boolean) if true the string is the starting of a line.
  9927. * @return the number of cells used or the remaining text if $firstline = true;
  9928. * @public
  9929. */
  9930. public function addHtmlLink($url, $name, $fill=false, $firstline=false, $color='', $style=-1, $firstblock=false) {
  9931. if (isset($url[1]) AND ($url[0] == '#') AND is_numeric($url[1])) {
  9932. // convert url to internal link
  9933. $lnkdata = explode(',', $url);
  9934. if (isset($lnkdata[0]) ) {
  9935. $page = substr($lnkdata[0], 1);
  9936. if (isset($lnkdata[1]) AND (strlen($lnkdata[1]) > 0)) {
  9937. $lnky = floatval($lnkdata[1]);
  9938. } else {
  9939. $lnky = 0;
  9940. }
  9941. $url = $this->AddLink();
  9942. $this->SetLink($url, $lnky, $page);
  9943. }
  9944. }
  9945. // store current settings
  9946. $prevcolor = $this->fgcolor;
  9947. $prevstyle = $this->FontStyle;
  9948. if (empty($color)) {
  9949. $this->SetTextColorArray($this->htmlLinkColorArray);
  9950. } else {
  9951. $this->SetTextColorArray($color);
  9952. }
  9953. if ($style == -1) {
  9954. $this->SetFont('', $this->FontStyle.$this->htmlLinkFontStyle);
  9955. } else {
  9956. $this->SetFont('', $this->FontStyle.$style);
  9957. }
  9958. $ret = $this->Write($this->lasth, $name, $url, $fill, '', false, 0, $firstline, $firstblock, 0);
  9959. // restore settings
  9960. $this->SetFont('', $prevstyle);
  9961. $this->SetTextColorArray($prevcolor);
  9962. return $ret;
  9963. }
  9964. /**
  9965. * Converts pixels to User's Units.
  9966. * @param $px (int) pixels
  9967. * @return float value in user's unit
  9968. * @public
  9969. * @see setImageScale(), getImageScale()
  9970. */
  9971. public function pixelsToUnits($px) {
  9972. return ($px / ($this->imgscale * $this->k));
  9973. }
  9974. /**
  9975. * Reverse function for htmlentities.
  9976. * Convert entities in UTF-8.
  9977. * @param $text_to_convert (string) Text to convert.
  9978. * @return string converted text string
  9979. * @public
  9980. */
  9981. public function unhtmlentities($text_to_convert) {
  9982. return @html_entity_decode($text_to_convert, ENT_QUOTES, $this->encoding);
  9983. }
  9984. // ENCRYPTION METHODS ----------------------------------
  9985. /**
  9986. * Compute encryption key depending on object number where the encrypted data is stored.
  9987. * This is used for all strings and streams without crypt filter specifier.
  9988. * @param $n (int) object number
  9989. * @return int object key
  9990. * @protected
  9991. * @author Nicola Asuni
  9992. * @since 2.0.000 (2008-01-02)
  9993. */
  9994. protected function _objectkey($n) {
  9995. $objkey = $this->encryptdata['key'].pack('VXxx', $n);
  9996. if ($this->encryptdata['mode'] == 2) { // AES-128
  9997. // AES padding
  9998. $objkey .= "\x73\x41\x6C\x54"; // sAlT
  9999. }
  10000. $objkey = substr(TCPDF_STATIC::_md5_16($objkey), 0, (($this->encryptdata['Length'] / 8) + 5));
  10001. $objkey = substr($objkey, 0, 16);
  10002. return $objkey;
  10003. }
  10004. /**
  10005. * Encrypt the input string.
  10006. * @param $n (int) object number
  10007. * @param $s (string) data string to encrypt
  10008. * @return encrypted string
  10009. * @protected
  10010. * @author Nicola Asuni
  10011. * @since 5.0.005 (2010-05-11)
  10012. */
  10013. protected function _encrypt_data($n, $s) {
  10014. if (!$this->encrypted) {
  10015. return $s;
  10016. }
  10017. switch ($this->encryptdata['mode']) {
  10018. case 0: // RC4-40
  10019. case 1: { // RC4-128
  10020. $s = TCPDF_STATIC::_RC4($this->_objectkey($n), $s, $this->last_enc_key, $this->last_enc_key_c);
  10021. break;
  10022. }
  10023. case 2: { // AES-128
  10024. $s = TCPDF_STATIC::_AES($this->_objectkey($n), $s);
  10025. break;
  10026. }
  10027. case 3: { // AES-256
  10028. $s = TCPDF_STATIC::_AES($this->encryptdata['key'], $s);
  10029. break;
  10030. }
  10031. }
  10032. return $s;
  10033. }
  10034. /**
  10035. * Put encryption on PDF document.
  10036. * @protected
  10037. * @author Nicola Asuni
  10038. * @since 2.0.000 (2008-01-02)
  10039. */
  10040. protected function _putencryption() {
  10041. if (!$this->encrypted) {
  10042. return;
  10043. }
  10044. $this->encryptdata['objid'] = $this->_newobj();
  10045. $out = '<<';
  10046. if (!isset($this->encryptdata['Filter']) OR empty($this->encryptdata['Filter'])) {
  10047. $this->encryptdata['Filter'] = 'Standard';
  10048. }
  10049. $out .= ' /Filter /'.$this->encryptdata['Filter'];
  10050. if (isset($this->encryptdata['SubFilter']) AND !empty($this->encryptdata['SubFilter'])) {
  10051. $out .= ' /SubFilter /'.$this->encryptdata['SubFilter'];
  10052. }
  10053. if (!isset($this->encryptdata['V']) OR empty($this->encryptdata['V'])) {
  10054. $this->encryptdata['V'] = 1;
  10055. }
  10056. // V is a code specifying the algorithm to be used in encrypting and decrypting the document
  10057. $out .= ' /V '.$this->encryptdata['V'];
  10058. if (isset($this->encryptdata['Length']) AND !empty($this->encryptdata['Length'])) {
  10059. // The length of the encryption key, in bits. The value shall be a multiple of 8, in the range 40 to 256
  10060. $out .= ' /Length '.$this->encryptdata['Length'];
  10061. } else {
  10062. $out .= ' /Length 40';
  10063. }
  10064. if ($this->encryptdata['V'] >= 4) {
  10065. if (!isset($this->encryptdata['StmF']) OR empty($this->encryptdata['StmF'])) {
  10066. $this->encryptdata['StmF'] = 'Identity';
  10067. }
  10068. if (!isset($this->encryptdata['StrF']) OR empty($this->encryptdata['StrF'])) {
  10069. // The name of the crypt filter that shall be used when decrypting all strings in the document.
  10070. $this->encryptdata['StrF'] = 'Identity';
  10071. }
  10072. // A dictionary whose keys shall be crypt filter names and whose values shall be the corresponding crypt filter dictionaries.
  10073. if (isset($this->encryptdata['CF']) AND !empty($this->encryptdata['CF'])) {
  10074. $out .= ' /CF <<';
  10075. $out .= ' /'.$this->encryptdata['StmF'].' <<';
  10076. $out .= ' /Type /CryptFilter';
  10077. if (isset($this->encryptdata['CF']['CFM']) AND !empty($this->encryptdata['CF']['CFM'])) {
  10078. // The method used
  10079. $out .= ' /CFM /'.$this->encryptdata['CF']['CFM'];
  10080. if ($this->encryptdata['pubkey']) {
  10081. $out .= ' /Recipients [';
  10082. foreach ($this->encryptdata['Recipients'] as $rec) {
  10083. $out .= ' <'.$rec.'>';
  10084. }
  10085. $out .= ' ]';
  10086. if (isset($this->encryptdata['CF']['EncryptMetadata']) AND (!$this->encryptdata['CF']['EncryptMetadata'])) {
  10087. $out .= ' /EncryptMetadata false';
  10088. } else {
  10089. $out .= ' /EncryptMetadata true';
  10090. }
  10091. }
  10092. } else {
  10093. $out .= ' /CFM /None';
  10094. }
  10095. if (isset($this->encryptdata['CF']['AuthEvent']) AND !empty($this->encryptdata['CF']['AuthEvent'])) {
  10096. // The event to be used to trigger the authorization that is required to access encryption keys used by this filter.
  10097. $out .= ' /AuthEvent /'.$this->encryptdata['CF']['AuthEvent'];
  10098. } else {
  10099. $out .= ' /AuthEvent /DocOpen';
  10100. }
  10101. if (isset($this->encryptdata['CF']['Length']) AND !empty($this->encryptdata['CF']['Length'])) {
  10102. // The bit length of the encryption key.
  10103. $out .= ' /Length '.$this->encryptdata['CF']['Length'];
  10104. }
  10105. $out .= ' >> >>';
  10106. }
  10107. // The name of the crypt filter that shall be used by default when decrypting streams.
  10108. $out .= ' /StmF /'.$this->encryptdata['StmF'];
  10109. // The name of the crypt filter that shall be used when decrypting all strings in the document.
  10110. $out .= ' /StrF /'.$this->encryptdata['StrF'];
  10111. if (isset($this->encryptdata['EFF']) AND !empty($this->encryptdata['EFF'])) {
  10112. // The name of the crypt filter that shall be used when encrypting embedded file streams that do not have their own crypt filter specifier.
  10113. $out .= ' /EFF /'.$this->encryptdata[''];
  10114. }
  10115. }
  10116. // Additional encryption dictionary entries for the standard security handler
  10117. if ($this->encryptdata['pubkey']) {
  10118. if (($this->encryptdata['V'] < 4) AND isset($this->encryptdata['Recipients']) AND !empty($this->encryptdata['Recipients'])) {
  10119. $out .= ' /Recipients [';
  10120. foreach ($this->encryptdata['Recipients'] as $rec) {
  10121. $out .= ' <'.$rec.'>';
  10122. }
  10123. $out .= ' ]';
  10124. }
  10125. } else {
  10126. $out .= ' /R';
  10127. if ($this->encryptdata['V'] == 5) { // AES-256
  10128. $out .= ' 5';
  10129. $out .= ' /OE ('.TCPDF_STATIC::_escape($this->encryptdata['OE']).')';
  10130. $out .= ' /UE ('.TCPDF_STATIC::_escape($this->encryptdata['UE']).')';
  10131. $out .= ' /Perms ('.TCPDF_STATIC::_escape($this->encryptdata['perms']).')';
  10132. } elseif ($this->encryptdata['V'] == 4) { // AES-128
  10133. $out .= ' 4';
  10134. } elseif ($this->encryptdata['V'] < 2) { // RC-40
  10135. $out .= ' 2';
  10136. } else { // RC-128
  10137. $out .= ' 3';
  10138. }
  10139. $out .= ' /O ('.TCPDF_STATIC::_escape($this->encryptdata['O']).')';
  10140. $out .= ' /U ('.TCPDF_STATIC::_escape($this->encryptdata['U']).')';
  10141. $out .= ' /P '.$this->encryptdata['P'];
  10142. if (isset($this->encryptdata['EncryptMetadata']) AND (!$this->encryptdata['EncryptMetadata'])) {
  10143. $out .= ' /EncryptMetadata false';
  10144. } else {
  10145. $out .= ' /EncryptMetadata true';
  10146. }
  10147. }
  10148. $out .= ' >>';
  10149. $out .= "\n".'endobj';
  10150. $this->_out($out);
  10151. }
  10152. /**
  10153. * Compute U value (used for encryption)
  10154. * @return string U value
  10155. * @protected
  10156. * @since 2.0.000 (2008-01-02)
  10157. * @author Nicola Asuni
  10158. */
  10159. protected function _Uvalue() {
  10160. if ($this->encryptdata['mode'] == 0) { // RC4-40
  10161. return TCPDF_STATIC::_RC4($this->encryptdata['key'], TCPDF_STATIC::$enc_padding, $this->last_enc_key, $this->last_enc_key_c);
  10162. } elseif ($this->encryptdata['mode'] < 3) { // RC4-128, AES-128
  10163. $tmp = TCPDF_STATIC::_md5_16(TCPDF_STATIC::$enc_padding.$this->encryptdata['fileid']);
  10164. $enc = TCPDF_STATIC::_RC4($this->encryptdata['key'], $tmp, $this->last_enc_key, $this->last_enc_key_c);
  10165. $len = strlen($tmp);
  10166. for ($i = 1; $i <= 19; ++$i) {
  10167. $ek = '';
  10168. for ($j = 0; $j < $len; ++$j) {
  10169. $ek .= chr(ord($this->encryptdata['key'][$j]) ^ $i);
  10170. }
  10171. $enc = TCPDF_STATIC::_RC4($ek, $enc, $this->last_enc_key, $this->last_enc_key_c);
  10172. }
  10173. $enc .= str_repeat("\x00", 16);
  10174. return substr($enc, 0, 32);
  10175. } elseif ($this->encryptdata['mode'] == 3) { // AES-256
  10176. $seed = TCPDF_STATIC::_md5_16(TCPDF_STATIC::getRandomSeed());
  10177. // User Validation Salt
  10178. $this->encryptdata['UVS'] = substr($seed, 0, 8);
  10179. // User Key Salt
  10180. $this->encryptdata['UKS'] = substr($seed, 8, 16);
  10181. return hash('sha256', $this->encryptdata['user_password'].$this->encryptdata['UVS'], true).$this->encryptdata['UVS'].$this->encryptdata['UKS'];
  10182. }
  10183. }
  10184. /**
  10185. * Compute UE value (used for encryption)
  10186. * @return string UE value
  10187. * @protected
  10188. * @since 5.9.006 (2010-10-19)
  10189. * @author Nicola Asuni
  10190. */
  10191. protected function _UEvalue() {
  10192. $hashkey = hash('sha256', $this->encryptdata['user_password'].$this->encryptdata['UKS'], true);
  10193. return TCPDF_STATIC::_AESnopad($hashkey, $this->encryptdata['key']);
  10194. }
  10195. /**
  10196. * Compute O value (used for encryption)
  10197. * @return string O value
  10198. * @protected
  10199. * @since 2.0.000 (2008-01-02)
  10200. * @author Nicola Asuni
  10201. */
  10202. protected function _Ovalue() {
  10203. if ($this->encryptdata['mode'] < 3) { // RC4-40, RC4-128, AES-128
  10204. $tmp = TCPDF_STATIC::_md5_16($this->encryptdata['owner_password']);
  10205. if ($this->encryptdata['mode'] > 0) {
  10206. for ($i = 0; $i < 50; ++$i) {
  10207. $tmp = TCPDF_STATIC::_md5_16($tmp);
  10208. }
  10209. }
  10210. $owner_key = substr($tmp, 0, ($this->encryptdata['Length'] / 8));
  10211. $enc = TCPDF_STATIC::_RC4($owner_key, $this->encryptdata['user_password'], $this->last_enc_key, $this->last_enc_key_c);
  10212. if ($this->encryptdata['mode'] > 0) {
  10213. $len = strlen($owner_key);
  10214. for ($i = 1; $i <= 19; ++$i) {
  10215. $ek = '';
  10216. for ($j = 0; $j < $len; ++$j) {
  10217. $ek .= chr(ord($owner_key[$j]) ^ $i);
  10218. }
  10219. $enc = TCPDF_STATIC::_RC4($ek, $enc, $this->last_enc_key, $this->last_enc_key_c);
  10220. }
  10221. }
  10222. return $enc;
  10223. } elseif ($this->encryptdata['mode'] == 3) { // AES-256
  10224. $seed = TCPDF_STATIC::_md5_16(TCPDF_STATIC::getRandomSeed());
  10225. // Owner Validation Salt
  10226. $this->encryptdata['OVS'] = substr($seed, 0, 8);
  10227. // Owner Key Salt
  10228. $this->encryptdata['OKS'] = substr($seed, 8, 16);
  10229. return hash('sha256', $this->encryptdata['owner_password'].$this->encryptdata['OVS'].$this->encryptdata['U'], true).$this->encryptdata['OVS'].$this->encryptdata['OKS'];
  10230. }
  10231. }
  10232. /**
  10233. * Compute OE value (used for encryption)
  10234. * @return string OE value
  10235. * @protected
  10236. * @since 5.9.006 (2010-10-19)
  10237. * @author Nicola Asuni
  10238. */
  10239. protected function _OEvalue() {
  10240. $hashkey = hash('sha256', $this->encryptdata['owner_password'].$this->encryptdata['OKS'].$this->encryptdata['U'], true);
  10241. return TCPDF_STATIC::_AESnopad($hashkey, $this->encryptdata['key']);
  10242. }
  10243. /**
  10244. * Convert password for AES-256 encryption mode
  10245. * @param $password (string) password
  10246. * @return string password
  10247. * @protected
  10248. * @since 5.9.006 (2010-10-19)
  10249. * @author Nicola Asuni
  10250. */
  10251. protected function _fixAES256Password($password) {
  10252. $psw = ''; // password to be returned
  10253. $psw_array = TCPDF_FONTS::utf8Bidi(TCPDF_FONTS::UTF8StringToArray($password, $this->isunicode, $this->CurrentFont), $password, $this->rtl, $this->isunicode, $this->CurrentFont);
  10254. foreach ($psw_array as $c) {
  10255. $psw .= TCPDF_FONTS::unichr($c, $this->isunicode);
  10256. }
  10257. return substr($psw, 0, 127);
  10258. }
  10259. /**
  10260. * Compute encryption key
  10261. * @protected
  10262. * @since 2.0.000 (2008-01-02)
  10263. * @author Nicola Asuni
  10264. */
  10265. protected function _generateencryptionkey() {
  10266. $keybytelen = ($this->encryptdata['Length'] / 8);
  10267. if (!$this->encryptdata['pubkey']) { // standard mode
  10268. if ($this->encryptdata['mode'] == 3) { // AES-256
  10269. // generate 256 bit random key
  10270. $this->encryptdata['key'] = substr(hash('sha256', TCPDF_STATIC::getRandomSeed(), true), 0, $keybytelen);
  10271. // truncate passwords
  10272. $this->encryptdata['user_password'] = $this->_fixAES256Password($this->encryptdata['user_password']);
  10273. $this->encryptdata['owner_password'] = $this->_fixAES256Password($this->encryptdata['owner_password']);
  10274. // Compute U value
  10275. $this->encryptdata['U'] = $this->_Uvalue();
  10276. // Compute UE value
  10277. $this->encryptdata['UE'] = $this->_UEvalue();
  10278. // Compute O value
  10279. $this->encryptdata['O'] = $this->_Ovalue();
  10280. // Compute OE value
  10281. $this->encryptdata['OE'] = $this->_OEvalue();
  10282. // Compute P value
  10283. $this->encryptdata['P'] = $this->encryptdata['protection'];
  10284. // Computing the encryption dictionary's Perms (permissions) value
  10285. $perms = TCPDF_STATIC::getEncPermissionsString($this->encryptdata['protection']); // bytes 0-3
  10286. $perms .= chr(255).chr(255).chr(255).chr(255); // bytes 4-7
  10287. if (isset($this->encryptdata['CF']['EncryptMetadata']) AND (!$this->encryptdata['CF']['EncryptMetadata'])) { // byte 8
  10288. $perms .= 'F';
  10289. } else {
  10290. $perms .= 'T';
  10291. }
  10292. $perms .= 'adb'; // bytes 9-11
  10293. $perms .= 'nick'; // bytes 12-15
  10294. $this->encryptdata['perms'] = TCPDF_STATIC::_AESnopad($this->encryptdata['key'], $perms);
  10295. } else { // RC4-40, RC4-128, AES-128
  10296. // Pad passwords
  10297. $this->encryptdata['user_password'] = substr($this->encryptdata['user_password'].TCPDF_STATIC::$enc_padding, 0, 32);
  10298. $this->encryptdata['owner_password'] = substr($this->encryptdata['owner_password'].TCPDF_STATIC::$enc_padding, 0, 32);
  10299. // Compute O value
  10300. $this->encryptdata['O'] = $this->_Ovalue();
  10301. // get default permissions (reverse byte order)
  10302. $permissions = TCPDF_STATIC::getEncPermissionsString($this->encryptdata['protection']);
  10303. // Compute encryption key
  10304. $tmp = TCPDF_STATIC::_md5_16($this->encryptdata['user_password'].$this->encryptdata['O'].$permissions.$this->encryptdata['fileid']);
  10305. if ($this->encryptdata['mode'] > 0) {
  10306. for ($i = 0; $i < 50; ++$i) {
  10307. $tmp = TCPDF_STATIC::_md5_16(substr($tmp, 0, $keybytelen));
  10308. }
  10309. }
  10310. $this->encryptdata['key'] = substr($tmp, 0, $keybytelen);
  10311. // Compute U value
  10312. $this->encryptdata['U'] = $this->_Uvalue();
  10313. // Compute P value
  10314. $this->encryptdata['P'] = $this->encryptdata['protection'];
  10315. }
  10316. } else { // Public-Key mode
  10317. // random 20-byte seed
  10318. $seed = sha1(TCPDF_STATIC::getRandomSeed(), true);
  10319. $recipient_bytes = '';
  10320. foreach ($this->encryptdata['pubkeys'] as $pubkey) {
  10321. // for each public certificate
  10322. if (isset($pubkey['p'])) {
  10323. $pkprotection = TCPDF_STATIC::getUserPermissionCode($pubkey['p'], $this->encryptdata['mode']);
  10324. } else {
  10325. $pkprotection = $this->encryptdata['protection'];
  10326. }
  10327. // get default permissions (reverse byte order)
  10328. $pkpermissions = TCPDF_STATIC::getEncPermissionsString($pkprotection);
  10329. // envelope data
  10330. $envelope = $seed.$pkpermissions;
  10331. // write the envelope data to a temporary file
  10332. $tempkeyfile = TCPDF_STATIC::getObjFilename('key', $this->file_id);
  10333. $f = TCPDF_STATIC::fopenLocal($tempkeyfile, 'wb');
  10334. if (!$f) {
  10335. $this->Error('Unable to create temporary key file: '.$tempkeyfile);
  10336. }
  10337. $envelope_length = strlen($envelope);
  10338. fwrite($f, $envelope, $envelope_length);
  10339. fclose($f);
  10340. $tempencfile = TCPDF_STATIC::getObjFilename('enc', $this->file_id);
  10341. if (!openssl_pkcs7_encrypt($tempkeyfile, $tempencfile, $pubkey['c'], array(), PKCS7_BINARY | PKCS7_DETACHED)) {
  10342. $this->Error('Unable to encrypt the file: '.$tempkeyfile);
  10343. }
  10344. // read encryption signature
  10345. $signature = file_get_contents($tempencfile, false, null, $envelope_length);
  10346. // extract signature
  10347. $signature = substr($signature, strpos($signature, 'Content-Disposition'));
  10348. $tmparr = explode("\n\n", $signature);
  10349. $signature = trim($tmparr[1]);
  10350. unset($tmparr);
  10351. // decode signature
  10352. $signature = base64_decode($signature);
  10353. // convert signature to hex
  10354. $hexsignature = current(unpack('H*', $signature));
  10355. // store signature on recipients array
  10356. $this->encryptdata['Recipients'][] = $hexsignature;
  10357. // The bytes of each item in the Recipients array of PKCS#7 objects in the order in which they appear in the array
  10358. $recipient_bytes .= $signature;
  10359. }
  10360. // calculate encryption key
  10361. if ($this->encryptdata['mode'] == 3) { // AES-256
  10362. $this->encryptdata['key'] = substr(hash('sha256', $seed.$recipient_bytes, true), 0, $keybytelen);
  10363. } else { // RC4-40, RC4-128, AES-128
  10364. $this->encryptdata['key'] = substr(sha1($seed.$recipient_bytes, true), 0, $keybytelen);
  10365. }
  10366. }
  10367. }
  10368. /**
  10369. * Set document protection
  10370. * Remark: the protection against modification is for people who have the full Acrobat product.
  10371. * 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.
  10372. * 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.
  10373. * @param $permissions (Array) 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>
  10374. * @param $user_pass (String) user password. Empty by default.
  10375. * @param $owner_pass (String) owner password. If not specified, a random value is used.
  10376. * @param $mode (int) encryption strength: 0 = RC4 40 bit; 1 = RC4 128 bit; 2 = AES 128 bit; 3 = AES 256 bit.
  10377. * @param $pubkeys (String) 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')))
  10378. * @public
  10379. * @since 2.0.000 (2008-01-02)
  10380. * @author Nicola Asuni
  10381. */
  10382. 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) {
  10383. if ($this->pdfa_mode) {
  10384. // encryption is not allowed in PDF/A mode
  10385. return;
  10386. }
  10387. $this->encryptdata['protection'] = TCPDF_STATIC::getUserPermissionCode($permissions, $mode);
  10388. if (($pubkeys !== null) AND (is_array($pubkeys))) {
  10389. // public-key mode
  10390. $this->encryptdata['pubkeys'] = $pubkeys;
  10391. if ($mode == 0) {
  10392. // public-Key Security requires at least 128 bit
  10393. $mode = 1;
  10394. }
  10395. if (!function_exists('openssl_pkcs7_encrypt')) {
  10396. $this->Error('Public-Key Security requires openssl library.');
  10397. }
  10398. // Set Public-Key filter (available are: Entrust.PPKEF, Adobe.PPKLite, Adobe.PubSec)
  10399. $this->encryptdata['pubkey'] = true;
  10400. $this->encryptdata['Filter'] = 'Adobe.PubSec';
  10401. $this->encryptdata['StmF'] = 'DefaultCryptFilter';
  10402. $this->encryptdata['StrF'] = 'DefaultCryptFilter';
  10403. } else {
  10404. // standard mode (password mode)
  10405. $this->encryptdata['pubkey'] = false;
  10406. $this->encryptdata['Filter'] = 'Standard';
  10407. $this->encryptdata['StmF'] = 'StdCF';
  10408. $this->encryptdata['StrF'] = 'StdCF';
  10409. }
  10410. if ($mode > 1) { // AES
  10411. if (!extension_loaded('openssl') && !extension_loaded('mcrypt')) {
  10412. $this->Error('AES encryption requires openssl or mcrypt extension (http://www.php.net/manual/en/mcrypt.requirements.php).');
  10413. }
  10414. if (extension_loaded('openssl') && !in_array('aes-256-cbc', openssl_get_cipher_methods())) {
  10415. $this->Error('AES encryption requires openssl/aes-256-cbc cypher.');
  10416. }
  10417. if (extension_loaded('mcrypt') && mcrypt_get_cipher_name(MCRYPT_RIJNDAEL_128) === false) {
  10418. $this->Error('AES encryption requires MCRYPT_RIJNDAEL_128 cypher.');
  10419. }
  10420. if (($mode == 3) AND !function_exists('hash')) {
  10421. // the Hash extension requires no external libraries and is enabled by default as of PHP 5.1.2.
  10422. $this->Error('AES 256 encryption requires HASH Message Digest Framework (http://www.php.net/manual/en/book.hash.php).');
  10423. }
  10424. }
  10425. if ($owner_pass === null) {
  10426. $owner_pass = md5(TCPDF_STATIC::getRandomSeed());
  10427. }
  10428. $this->encryptdata['user_password'] = $user_pass;
  10429. $this->encryptdata['owner_password'] = $owner_pass;
  10430. $this->encryptdata['mode'] = $mode;
  10431. switch ($mode) {
  10432. case 0: { // RC4 40 bit
  10433. $this->encryptdata['V'] = 1;
  10434. $this->encryptdata['Length'] = 40;
  10435. $this->encryptdata['CF']['CFM'] = 'V2';
  10436. break;
  10437. }
  10438. case 1: { // RC4 128 bit
  10439. $this->encryptdata['V'] = 2;
  10440. $this->encryptdata['Length'] = 128;
  10441. $this->encryptdata['CF']['CFM'] = 'V2';
  10442. if ($this->encryptdata['pubkey']) {
  10443. $this->encryptdata['SubFilter'] = 'adbe.pkcs7.s4';
  10444. $this->encryptdata['Recipients'] = array();
  10445. }
  10446. break;
  10447. }
  10448. case 2: { // AES 128 bit
  10449. $this->encryptdata['V'] = 4;
  10450. $this->encryptdata['Length'] = 128;
  10451. $this->encryptdata['CF']['CFM'] = 'AESV2';
  10452. $this->encryptdata['CF']['Length'] = 128;
  10453. if ($this->encryptdata['pubkey']) {
  10454. $this->encryptdata['SubFilter'] = 'adbe.pkcs7.s5';
  10455. $this->encryptdata['Recipients'] = array();
  10456. }
  10457. break;
  10458. }
  10459. case 3: { // AES 256 bit
  10460. $this->encryptdata['V'] = 5;
  10461. $this->encryptdata['Length'] = 256;
  10462. $this->encryptdata['CF']['CFM'] = 'AESV3';
  10463. $this->encryptdata['CF']['Length'] = 256;
  10464. if ($this->encryptdata['pubkey']) {
  10465. $this->encryptdata['SubFilter'] = 'adbe.pkcs7.s5';
  10466. $this->encryptdata['Recipients'] = array();
  10467. }
  10468. break;
  10469. }
  10470. }
  10471. $this->encrypted = true;
  10472. $this->encryptdata['fileid'] = TCPDF_STATIC::convertHexStringToString($this->file_id);
  10473. $this->_generateencryptionkey();
  10474. }
  10475. // END OF ENCRYPTION FUNCTIONS -------------------------
  10476. // START TRANSFORMATIONS SECTION -----------------------
  10477. /**
  10478. * Starts a 2D tranformation saving current graphic state.
  10479. * This function must be called before scaling, mirroring, translation, rotation and skewing.
  10480. * Use StartTransform() before, and StopTransform() after the transformations to restore the normal behavior.
  10481. * @public
  10482. * @since 2.1.000 (2008-01-07)
  10483. * @see StartTransform(), StopTransform()
  10484. */
  10485. public function StartTransform() {
  10486. if ($this->state != 2) {
  10487. return;
  10488. }
  10489. $this->_outSaveGraphicsState();
  10490. if ($this->inxobj) {
  10491. // we are inside an XObject template
  10492. $this->xobjects[$this->xobjid]['transfmrk'][] = strlen($this->xobjects[$this->xobjid]['outdata']);
  10493. } else {
  10494. $this->transfmrk[$this->page][] = $this->pagelen[$this->page];
  10495. }
  10496. ++$this->transfmatrix_key;
  10497. $this->transfmatrix[$this->transfmatrix_key] = array();
  10498. }
  10499. /**
  10500. * Stops a 2D tranformation restoring previous graphic state.
  10501. * This function must be called after scaling, mirroring, translation, rotation and skewing.
  10502. * Use StartTransform() before, and StopTransform() after the transformations to restore the normal behavior.
  10503. * @public
  10504. * @since 2.1.000 (2008-01-07)
  10505. * @see StartTransform(), StopTransform()
  10506. */
  10507. public function StopTransform() {
  10508. if ($this->state != 2) {
  10509. return;
  10510. }
  10511. $this->_outRestoreGraphicsState();
  10512. if (isset($this->transfmatrix[$this->transfmatrix_key])) {
  10513. array_pop($this->transfmatrix[$this->transfmatrix_key]);
  10514. --$this->transfmatrix_key;
  10515. }
  10516. if ($this->inxobj) {
  10517. // we are inside an XObject template
  10518. array_pop($this->xobjects[$this->xobjid]['transfmrk']);
  10519. } else {
  10520. array_pop($this->transfmrk[$this->page]);
  10521. }
  10522. }
  10523. /**
  10524. * Horizontal Scaling.
  10525. * @param $s_x (float) scaling factor for width as percent. 0 is not allowed.
  10526. * @param $x (int) abscissa of the scaling center. Default is current x position
  10527. * @param $y (int) ordinate of the scaling center. Default is current y position
  10528. * @public
  10529. * @since 2.1.000 (2008-01-07)
  10530. * @see StartTransform(), StopTransform()
  10531. */
  10532. public function ScaleX($s_x, $x='', $y='') {
  10533. $this->Scale($s_x, 100, $x, $y);
  10534. }
  10535. /**
  10536. * Vertical Scaling.
  10537. * @param $s_y (float) scaling factor for height as percent. 0 is not allowed.
  10538. * @param $x (int) abscissa of the scaling center. Default is current x position
  10539. * @param $y (int) ordinate of the scaling center. Default is current y position
  10540. * @public
  10541. * @since 2.1.000 (2008-01-07)
  10542. * @see StartTransform(), StopTransform()
  10543. */
  10544. public function ScaleY($s_y, $x='', $y='') {
  10545. $this->Scale(100, $s_y, $x, $y);
  10546. }
  10547. /**
  10548. * Vertical and horizontal proportional Scaling.
  10549. * @param $s (float) scaling factor for width and height as percent. 0 is not allowed.
  10550. * @param $x (int) abscissa of the scaling center. Default is current x position
  10551. * @param $y (int) ordinate of the scaling center. Default is current y position
  10552. * @public
  10553. * @since 2.1.000 (2008-01-07)
  10554. * @see StartTransform(), StopTransform()
  10555. */
  10556. public function ScaleXY($s, $x='', $y='') {
  10557. $this->Scale($s, $s, $x, $y);
  10558. }
  10559. /**
  10560. * Vertical and horizontal non-proportional Scaling.
  10561. * @param $s_x (float) scaling factor for width as percent. 0 is not allowed.
  10562. * @param $s_y (float) scaling factor for height as percent. 0 is not allowed.
  10563. * @param $x (int) abscissa of the scaling center. Default is current x position
  10564. * @param $y (int) ordinate of the scaling center. Default is current y position
  10565. * @public
  10566. * @since 2.1.000 (2008-01-07)
  10567. * @see StartTransform(), StopTransform()
  10568. */
  10569. public function Scale($s_x, $s_y, $x='', $y='') {
  10570. if ($x === '') {
  10571. $x = $this->x;
  10572. }
  10573. if ($y === '') {
  10574. $y = $this->y;
  10575. }
  10576. if (($s_x == 0) OR ($s_y == 0)) {
  10577. $this->Error('Please do not use values equal to zero for scaling');
  10578. }
  10579. $y = ($this->h - $y) * $this->k;
  10580. $x *= $this->k;
  10581. //calculate elements of transformation matrix
  10582. $s_x /= 100;
  10583. $s_y /= 100;
  10584. $tm = array();
  10585. $tm[0] = $s_x;
  10586. $tm[1] = 0;
  10587. $tm[2] = 0;
  10588. $tm[3] = $s_y;
  10589. $tm[4] = $x * (1 - $s_x);
  10590. $tm[5] = $y * (1 - $s_y);
  10591. //scale the coordinate system
  10592. $this->Transform($tm);
  10593. }
  10594. /**
  10595. * Horizontal Mirroring.
  10596. * @param $x (int) abscissa of the point. Default is current x position
  10597. * @public
  10598. * @since 2.1.000 (2008-01-07)
  10599. * @see StartTransform(), StopTransform()
  10600. */
  10601. public function MirrorH($x='') {
  10602. $this->Scale(-100, 100, $x);
  10603. }
  10604. /**
  10605. * Verical Mirroring.
  10606. * @param $y (int) ordinate of the point. Default is current y position
  10607. * @public
  10608. * @since 2.1.000 (2008-01-07)
  10609. * @see StartTransform(), StopTransform()
  10610. */
  10611. public function MirrorV($y='') {
  10612. $this->Scale(100, -100, '', $y);
  10613. }
  10614. /**
  10615. * Point reflection mirroring.
  10616. * @param $x (int) abscissa of the point. Default is current x position
  10617. * @param $y (int) ordinate of the point. Default is current y position
  10618. * @public
  10619. * @since 2.1.000 (2008-01-07)
  10620. * @see StartTransform(), StopTransform()
  10621. */
  10622. public function MirrorP($x='',$y='') {
  10623. $this->Scale(-100, -100, $x, $y);
  10624. }
  10625. /**
  10626. * Reflection against a straight line through point (x, y) with the gradient angle (angle).
  10627. * @param $angle (float) gradient angle of the straight line. Default is 0 (horizontal line).
  10628. * @param $x (int) abscissa of the point. Default is current x position
  10629. * @param $y (int) ordinate of the point. Default is current y position
  10630. * @public
  10631. * @since 2.1.000 (2008-01-07)
  10632. * @see StartTransform(), StopTransform()
  10633. */
  10634. public function MirrorL($angle=0, $x='',$y='') {
  10635. $this->Scale(-100, 100, $x, $y);
  10636. $this->Rotate(-2*($angle-90), $x, $y);
  10637. }
  10638. /**
  10639. * Translate graphic object horizontally.
  10640. * @param $t_x (int) movement to the right (or left for RTL)
  10641. * @public
  10642. * @since 2.1.000 (2008-01-07)
  10643. * @see StartTransform(), StopTransform()
  10644. */
  10645. public function TranslateX($t_x) {
  10646. $this->Translate($t_x, 0);
  10647. }
  10648. /**
  10649. * Translate graphic object vertically.
  10650. * @param $t_y (int) movement to the bottom
  10651. * @public
  10652. * @since 2.1.000 (2008-01-07)
  10653. * @see StartTransform(), StopTransform()
  10654. */
  10655. public function TranslateY($t_y) {
  10656. $this->Translate(0, $t_y);
  10657. }
  10658. /**
  10659. * Translate graphic object horizontally and vertically.
  10660. * @param $t_x (int) movement to the right
  10661. * @param $t_y (int) movement to the bottom
  10662. * @public
  10663. * @since 2.1.000 (2008-01-07)
  10664. * @see StartTransform(), StopTransform()
  10665. */
  10666. public function Translate($t_x, $t_y) {
  10667. //calculate elements of transformation matrix
  10668. $tm = array();
  10669. $tm[0] = 1;
  10670. $tm[1] = 0;
  10671. $tm[2] = 0;
  10672. $tm[3] = 1;
  10673. $tm[4] = $t_x * $this->k;
  10674. $tm[5] = -$t_y * $this->k;
  10675. //translate the coordinate system
  10676. $this->Transform($tm);
  10677. }
  10678. /**
  10679. * Rotate object.
  10680. * @param $angle (float) angle in degrees for counter-clockwise rotation
  10681. * @param $x (int) abscissa of the rotation center. Default is current x position
  10682. * @param $y (int) ordinate of the rotation center. Default is current y position
  10683. * @public
  10684. * @since 2.1.000 (2008-01-07)
  10685. * @see StartTransform(), StopTransform()
  10686. */
  10687. public function Rotate($angle, $x='', $y='') {
  10688. if ($x === '') {
  10689. $x = $this->x;
  10690. }
  10691. if ($y === '') {
  10692. $y = $this->y;
  10693. }
  10694. $y = ($this->h - $y) * $this->k;
  10695. $x *= $this->k;
  10696. //calculate elements of transformation matrix
  10697. $tm = array();
  10698. $tm[0] = cos(deg2rad($angle));
  10699. $tm[1] = sin(deg2rad($angle));
  10700. $tm[2] = -$tm[1];
  10701. $tm[3] = $tm[0];
  10702. $tm[4] = $x + ($tm[1] * $y) - ($tm[0] * $x);
  10703. $tm[5] = $y - ($tm[0] * $y) - ($tm[1] * $x);
  10704. //rotate the coordinate system around ($x,$y)
  10705. $this->Transform($tm);
  10706. }
  10707. /**
  10708. * Skew horizontally.
  10709. * @param $angle_x (float) angle in degrees between -90 (skew to the left) and 90 (skew to the right)
  10710. * @param $x (int) abscissa of the skewing center. default is current x position
  10711. * @param $y (int) ordinate of the skewing center. default is current y position
  10712. * @public
  10713. * @since 2.1.000 (2008-01-07)
  10714. * @see StartTransform(), StopTransform()
  10715. */
  10716. public function SkewX($angle_x, $x='', $y='') {
  10717. $this->Skew($angle_x, 0, $x, $y);
  10718. }
  10719. /**
  10720. * Skew vertically.
  10721. * @param $angle_y (float) angle in degrees between -90 (skew to the bottom) and 90 (skew to the top)
  10722. * @param $x (int) abscissa of the skewing center. default is current x position
  10723. * @param $y (int) ordinate of the skewing center. default is current y position
  10724. * @public
  10725. * @since 2.1.000 (2008-01-07)
  10726. * @see StartTransform(), StopTransform()
  10727. */
  10728. public function SkewY($angle_y, $x='', $y='') {
  10729. $this->Skew(0, $angle_y, $x, $y);
  10730. }
  10731. /**
  10732. * Skew.
  10733. * @param $angle_x (float) angle in degrees between -90 (skew to the left) and 90 (skew to the right)
  10734. * @param $angle_y (float) angle in degrees between -90 (skew to the bottom) and 90 (skew to the top)
  10735. * @param $x (int) abscissa of the skewing center. default is current x position
  10736. * @param $y (int) ordinate of the skewing center. default is current y position
  10737. * @public
  10738. * @since 2.1.000 (2008-01-07)
  10739. * @see StartTransform(), StopTransform()
  10740. */
  10741. public function Skew($angle_x, $angle_y, $x='', $y='') {
  10742. if ($x === '') {
  10743. $x = $this->x;
  10744. }
  10745. if ($y === '') {
  10746. $y = $this->y;
  10747. }
  10748. if (($angle_x <= -90) OR ($angle_x >= 90) OR ($angle_y <= -90) OR ($angle_y >= 90)) {
  10749. $this->Error('Please use values between -90 and +90 degrees for Skewing.');
  10750. }
  10751. $x *= $this->k;
  10752. $y = ($this->h - $y) * $this->k;
  10753. //calculate elements of transformation matrix
  10754. $tm = array();
  10755. $tm[0] = 1;
  10756. $tm[1] = tan(deg2rad($angle_y));
  10757. $tm[2] = tan(deg2rad($angle_x));
  10758. $tm[3] = 1;
  10759. $tm[4] = -$tm[2] * $y;
  10760. $tm[5] = -$tm[1] * $x;
  10761. //skew the coordinate system
  10762. $this->Transform($tm);
  10763. }
  10764. /**
  10765. * Apply graphic transformations.
  10766. * @param $tm (array) transformation matrix
  10767. * @protected
  10768. * @since 2.1.000 (2008-01-07)
  10769. * @see StartTransform(), StopTransform()
  10770. */
  10771. protected function Transform($tm) {
  10772. if ($this->state != 2) {
  10773. return;
  10774. }
  10775. $this->_out(sprintf('%F %F %F %F %F %F cm', $tm[0], $tm[1], $tm[2], $tm[3], $tm[4], $tm[5]));
  10776. // add tranformation matrix
  10777. $this->transfmatrix[$this->transfmatrix_key][] = array('a' => $tm[0], 'b' => $tm[1], 'c' => $tm[2], 'd' => $tm[3], 'e' => $tm[4], 'f' => $tm[5]);
  10778. // update transformation mark
  10779. if ($this->inxobj) {
  10780. // we are inside an XObject template
  10781. if (end($this->xobjects[$this->xobjid]['transfmrk']) !== false) {
  10782. $key = key($this->xobjects[$this->xobjid]['transfmrk']);
  10783. $this->xobjects[$this->xobjid]['transfmrk'][$key] = strlen($this->xobjects[$this->xobjid]['outdata']);
  10784. }
  10785. } elseif (end($this->transfmrk[$this->page]) !== false) {
  10786. $key = key($this->transfmrk[$this->page]);
  10787. $this->transfmrk[$this->page][$key] = $this->pagelen[$this->page];
  10788. }
  10789. }
  10790. // END TRANSFORMATIONS SECTION -------------------------
  10791. // START GRAPHIC FUNCTIONS SECTION ---------------------
  10792. // The following section is based on the code provided by David Hernandez Sanz
  10793. /**
  10794. * 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.
  10795. * @param $width (float) The width.
  10796. * @public
  10797. * @since 1.0
  10798. * @see Line(), Rect(), Cell(), MultiCell()
  10799. */
  10800. public function SetLineWidth($width) {
  10801. //Set line width
  10802. $this->LineWidth = $width;
  10803. $this->linestyleWidth = sprintf('%F w', ($width * $this->k));
  10804. if ($this->state == 2) {
  10805. $this->_out($this->linestyleWidth);
  10806. }
  10807. }
  10808. /**
  10809. * Returns the current the line width.
  10810. * @return int Line width
  10811. * @public
  10812. * @since 2.1.000 (2008-01-07)
  10813. * @see Line(), SetLineWidth()
  10814. */
  10815. public function GetLineWidth() {
  10816. return $this->LineWidth;
  10817. }
  10818. /**
  10819. * Set line style.
  10820. * @param $style (array) Line style. Array with keys among the following:
  10821. * <ul>
  10822. * <li>width (float): Width of the line in user units.</li>
  10823. * <li>cap (string): Type of cap to put on the line. Possible values are:
  10824. * butt, round, square. The difference between "square" and "butt" is that
  10825. * "square" projects a flat end past the end of the line.</li>
  10826. * <li>join (string): Type of join. Possible values are: miter, round,
  10827. * bevel.</li>
  10828. * <li>dash (mixed): Dash pattern. Is 0 (without dash) or string with
  10829. * series of length values, which are the lengths of the on and off dashes.
  10830. * For example: "2" represents 2 on, 2 off, 2 on, 2 off, ...; "2,1" is 2 on,
  10831. * 1 off, 2 on, 1 off, ...</li>
  10832. * <li>phase (integer): Modifier on the dash pattern which is used to shift
  10833. * the point at which the pattern starts.</li>
  10834. * <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>
  10835. * </ul>
  10836. * @param $ret (boolean) if true do not send the command.
  10837. * @return string the PDF command
  10838. * @public
  10839. * @since 2.1.000 (2008-01-08)
  10840. */
  10841. public function SetLineStyle($style, $ret=false) {
  10842. $s = ''; // string to be returned
  10843. if (!is_array($style)) {
  10844. return;
  10845. }
  10846. if (isset($style['width'])) {
  10847. $this->LineWidth = $style['width'];
  10848. $this->linestyleWidth = sprintf('%F w', ($style['width'] * $this->k));
  10849. $s .= $this->linestyleWidth.' ';
  10850. }
  10851. if (isset($style['cap'])) {
  10852. $ca = array('butt' => 0, 'round'=> 1, 'square' => 2);
  10853. if (isset($ca[$style['cap']])) {
  10854. $this->linestyleCap = $ca[$style['cap']].' J';
  10855. $s .= $this->linestyleCap.' ';
  10856. }
  10857. }
  10858. if (isset($style['join'])) {
  10859. $ja = array('miter' => 0, 'round' => 1, 'bevel' => 2);
  10860. if (isset($ja[$style['join']])) {
  10861. $this->linestyleJoin = $ja[$style['join']].' j';
  10862. $s .= $this->linestyleJoin.' ';
  10863. }
  10864. }
  10865. if (isset($style['dash'])) {
  10866. $dash_string = '';
  10867. if ($style['dash']) {
  10868. if (preg_match('/^.+,/', $style['dash']) > 0) {
  10869. $tab = explode(',', $style['dash']);
  10870. } else {
  10871. $tab = array($style['dash']);
  10872. }
  10873. $dash_string = '';
  10874. foreach ($tab as $i => $v) {
  10875. if ($i) {
  10876. $dash_string .= ' ';
  10877. }
  10878. $dash_string .= sprintf('%F', $v);
  10879. }
  10880. }
  10881. if (!isset($style['phase']) OR !$style['dash']) {
  10882. $style['phase'] = 0;
  10883. }
  10884. $this->linestyleDash = sprintf('[%s] %F d', $dash_string, $style['phase']);
  10885. $s .= $this->linestyleDash.' ';
  10886. }
  10887. if (isset($style['color'])) {
  10888. $s .= $this->SetDrawColorArray($style['color'], true).' ';
  10889. }
  10890. if (!$ret AND ($this->state == 2)) {
  10891. $this->_out($s);
  10892. }
  10893. return $s;
  10894. }
  10895. /**
  10896. * Begin a new subpath by moving the current point to coordinates (x, y), omitting any connecting line segment.
  10897. * @param $x (float) Abscissa of point.
  10898. * @param $y (float) Ordinate of point.
  10899. * @protected
  10900. * @since 2.1.000 (2008-01-08)
  10901. */
  10902. protected function _outPoint($x, $y) {
  10903. if ($this->state == 2) {
  10904. $this->_out(sprintf('%F %F m', ($x * $this->k), (($this->h - $y) * $this->k)));
  10905. }
  10906. }
  10907. /**
  10908. * Append a straight line segment from the current point to the point (x, y).
  10909. * The new current point shall be (x, y).
  10910. * @param $x (float) Abscissa of end point.
  10911. * @param $y (float) Ordinate of end point.
  10912. * @protected
  10913. * @since 2.1.000 (2008-01-08)
  10914. */
  10915. protected function _outLine($x, $y) {
  10916. if ($this->state == 2) {
  10917. $this->_out(sprintf('%F %F l', ($x * $this->k), (($this->h - $y) * $this->k)));
  10918. }
  10919. }
  10920. /**
  10921. * Append a rectangle to the current path as a complete subpath, with lower-left corner (x, y) and dimensions widthand height in user space.
  10922. * @param $x (float) Abscissa of upper-left corner.
  10923. * @param $y (float) Ordinate of upper-left corner.
  10924. * @param $w (float) Width.
  10925. * @param $h (float) Height.
  10926. * @param $op (string) options
  10927. * @protected
  10928. * @since 2.1.000 (2008-01-08)
  10929. */
  10930. protected function _outRect($x, $y, $w, $h, $op) {
  10931. if ($this->state == 2) {
  10932. $this->_out(sprintf('%F %F %F %F re %s', ($x * $this->k), (($this->h - $y) * $this->k), ($w * $this->k), (-$h * $this->k), $op));
  10933. }
  10934. }
  10935. /**
  10936. * 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.
  10937. * The new current point shall be (x3, y3).
  10938. * @param $x1 (float) Abscissa of control point 1.
  10939. * @param $y1 (float) Ordinate of control point 1.
  10940. * @param $x2 (float) Abscissa of control point 2.
  10941. * @param $y2 (float) Ordinate of control point 2.
  10942. * @param $x3 (float) Abscissa of end point.
  10943. * @param $y3 (float) Ordinate of end point.
  10944. * @protected
  10945. * @since 2.1.000 (2008-01-08)
  10946. */
  10947. protected function _outCurve($x1, $y1, $x2, $y2, $x3, $y3) {
  10948. if ($this->state == 2) {
  10949. $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)));
  10950. }
  10951. }
  10952. /**
  10953. * 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.
  10954. * The new current point shall be (x3, y3).
  10955. * @param $x2 (float) Abscissa of control point 2.
  10956. * @param $y2 (float) Ordinate of control point 2.
  10957. * @param $x3 (float) Abscissa of end point.
  10958. * @param $y3 (float) Ordinate of end point.
  10959. * @protected
  10960. * @since 4.9.019 (2010-04-26)
  10961. */
  10962. protected function _outCurveV($x2, $y2, $x3, $y3) {
  10963. if ($this->state == 2) {
  10964. $this->_out(sprintf('%F %F %F %F v', ($x2 * $this->k), (($this->h - $y2) * $this->k), ($x3 * $this->k), (($this->h - $y3) * $this->k)));
  10965. }
  10966. }
  10967. /**
  10968. * 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.
  10969. * The new current point shall be (x3, y3).
  10970. * @param $x1 (float) Abscissa of control point 1.
  10971. * @param $y1 (float) Ordinate of control point 1.
  10972. * @param $x3 (float) Abscissa of end point.
  10973. * @param $y3 (float) Ordinate of end point.
  10974. * @protected
  10975. * @since 2.1.000 (2008-01-08)
  10976. */
  10977. protected function _outCurveY($x1, $y1, $x3, $y3) {
  10978. if ($this->state == 2) {
  10979. $this->_out(sprintf('%F %F %F %F y', ($x1 * $this->k), (($this->h - $y1) * $this->k), ($x3 * $this->k), (($this->h - $y3) * $this->k)));
  10980. }
  10981. }
  10982. /**
  10983. * Draws a line between two points.
  10984. * @param $x1 (float) Abscissa of first point.
  10985. * @param $y1 (float) Ordinate of first point.
  10986. * @param $x2 (float) Abscissa of second point.
  10987. * @param $y2 (float) Ordinate of second point.
  10988. * @param $style (array) Line style. Array like for SetLineStyle(). Default value: default line style (empty array).
  10989. * @public
  10990. * @since 1.0
  10991. * @see SetLineWidth(), SetDrawColor(), SetLineStyle()
  10992. */
  10993. public function Line($x1, $y1, $x2, $y2, $style=array()) {
  10994. if ($this->state != 2) {
  10995. return;
  10996. }
  10997. if (is_array($style)) {
  10998. $this->SetLineStyle($style);
  10999. }
  11000. $this->_outPoint($x1, $y1);
  11001. $this->_outLine($x2, $y2);
  11002. $this->_out('S');
  11003. }
  11004. /**
  11005. * Draws a rectangle.
  11006. * @param $x (float) Abscissa of upper-left corner.
  11007. * @param $y (float) Ordinate of upper-left corner.
  11008. * @param $w (float) Width.
  11009. * @param $h (float) Height.
  11010. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  11011. * @param $border_style (array) Border style of rectangle. Array with keys among the following:
  11012. * <ul>
  11013. * <li>all: Line style of all borders. Array like for SetLineStyle().</li>
  11014. * <li>L, T, R, B or combinations: Line style of left, top, right or bottom border. Array like for SetLineStyle().</li>
  11015. * </ul>
  11016. * If a key is not present or is null, the correspondent border is not drawn. Default value: default line style (empty array).
  11017. * @param $fill_color (array) 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).
  11018. * @public
  11019. * @since 1.0
  11020. * @see SetLineStyle()
  11021. */
  11022. public function Rect($x, $y, $w, $h, $style='', $border_style=array(), $fill_color=array()) {
  11023. if ($this->state != 2) {
  11024. return;
  11025. }
  11026. if (empty($style)) {
  11027. $style = 'S';
  11028. }
  11029. if (!(strpos($style, 'F') === false) AND !empty($fill_color)) {
  11030. // set background color
  11031. $this->SetFillColorArray($fill_color);
  11032. }
  11033. if (!empty($border_style)) {
  11034. if (isset($border_style['all']) AND !empty($border_style['all'])) {
  11035. //set global style for border
  11036. $this->SetLineStyle($border_style['all']);
  11037. $border_style = array();
  11038. } else {
  11039. // remove stroke operator from style
  11040. $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*' );
  11041. if (isset($opnostroke[$style])) {
  11042. $style = $opnostroke[$style];
  11043. }
  11044. }
  11045. }
  11046. if (!empty($style)) {
  11047. $op = TCPDF_STATIC::getPathPaintOperator($style);
  11048. $this->_outRect($x, $y, $w, $h, $op);
  11049. }
  11050. if (!empty($border_style)) {
  11051. $border_style2 = array();
  11052. foreach ($border_style as $line => $value) {
  11053. $length = strlen($line);
  11054. for ($i = 0; $i < $length; ++$i) {
  11055. $border_style2[$line[$i]] = $value;
  11056. }
  11057. }
  11058. $border_style = $border_style2;
  11059. if (isset($border_style['L']) AND $border_style['L']) {
  11060. $this->Line($x, $y, $x, $y + $h, $border_style['L']);
  11061. }
  11062. if (isset($border_style['T']) AND $border_style['T']) {
  11063. $this->Line($x, $y, $x + $w, $y, $border_style['T']);
  11064. }
  11065. if (isset($border_style['R']) AND $border_style['R']) {
  11066. $this->Line($x + $w, $y, $x + $w, $y + $h, $border_style['R']);
  11067. }
  11068. if (isset($border_style['B']) AND $border_style['B']) {
  11069. $this->Line($x, $y + $h, $x + $w, $y + $h, $border_style['B']);
  11070. }
  11071. }
  11072. }
  11073. /**
  11074. * Draws a Bezier curve.
  11075. * The Bezier curve is a tangent to the line between the control points at
  11076. * either end of the curve.
  11077. * @param $x0 (float) Abscissa of start point.
  11078. * @param $y0 (float) Ordinate of start point.
  11079. * @param $x1 (float) Abscissa of control point 1.
  11080. * @param $y1 (float) Ordinate of control point 1.
  11081. * @param $x2 (float) Abscissa of control point 2.
  11082. * @param $y2 (float) Ordinate of control point 2.
  11083. * @param $x3 (float) Abscissa of end point.
  11084. * @param $y3 (float) Ordinate of end point.
  11085. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  11086. * @param $line_style (array) Line style of curve. Array like for SetLineStyle(). Default value: default line style (empty array).
  11087. * @param $fill_color (array) 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).
  11088. * @public
  11089. * @see SetLineStyle()
  11090. * @since 2.1.000 (2008-01-08)
  11091. */
  11092. public function Curve($x0, $y0, $x1, $y1, $x2, $y2, $x3, $y3, $style='', $line_style=array(), $fill_color=array()) {
  11093. if ($this->state != 2) {
  11094. return;
  11095. }
  11096. if (!(false === strpos($style, 'F')) AND isset($fill_color)) {
  11097. $this->SetFillColorArray($fill_color);
  11098. }
  11099. $op = TCPDF_STATIC::getPathPaintOperator($style);
  11100. if ($line_style) {
  11101. $this->SetLineStyle($line_style);
  11102. }
  11103. $this->_outPoint($x0, $y0);
  11104. $this->_outCurve($x1, $y1, $x2, $y2, $x3, $y3);
  11105. $this->_out($op);
  11106. }
  11107. /**
  11108. * Draws a poly-Bezier curve.
  11109. * Each Bezier curve segment is a tangent to the line between the control points at
  11110. * either end of the curve.
  11111. * @param $x0 (float) Abscissa of start point.
  11112. * @param $y0 (float) Ordinate of start point.
  11113. * @param $segments (float) An array of bezier descriptions. Format: array(x1, y1, x2, y2, x3, y3).
  11114. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  11115. * @param $line_style (array) Line style of curve. Array like for SetLineStyle(). Default value: default line style (empty array).
  11116. * @param $fill_color (array) 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).
  11117. * @public
  11118. * @see SetLineStyle()
  11119. * @since 3.0008 (2008-05-12)
  11120. */
  11121. public function Polycurve($x0, $y0, $segments, $style='', $line_style=array(), $fill_color=array()) {
  11122. if ($this->state != 2) {
  11123. return;
  11124. }
  11125. if (!(false === strpos($style, 'F')) AND isset($fill_color)) {
  11126. $this->SetFillColorArray($fill_color);
  11127. }
  11128. $op = TCPDF_STATIC::getPathPaintOperator($style);
  11129. if ($op == 'f') {
  11130. $line_style = array();
  11131. }
  11132. if ($line_style) {
  11133. $this->SetLineStyle($line_style);
  11134. }
  11135. $this->_outPoint($x0, $y0);
  11136. foreach ($segments as $segment) {
  11137. list($x1, $y1, $x2, $y2, $x3, $y3) = $segment;
  11138. $this->_outCurve($x1, $y1, $x2, $y2, $x3, $y3);
  11139. }
  11140. $this->_out($op);
  11141. }
  11142. /**
  11143. * Draws an ellipse.
  11144. * An ellipse is formed from n Bezier curves.
  11145. * @param $x0 (float) Abscissa of center point.
  11146. * @param $y0 (float) Ordinate of center point.
  11147. * @param $rx (float) Horizontal radius.
  11148. * @param $ry (float) Vertical radius (if ry = 0 then is a circle, see Circle()). Default value: 0.
  11149. * @param $angle: (float) Angle oriented (anti-clockwise). Default value: 0.
  11150. * @param $astart: (float) Angle start of draw line. Default value: 0.
  11151. * @param $afinish: (float) Angle finish of draw line. Default value: 360.
  11152. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  11153. * @param $line_style (array) Line style of ellipse. Array like for SetLineStyle(). Default value: default line style (empty array).
  11154. * @param $fill_color (array) 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).
  11155. * @param $nc (integer) Number of curves used to draw a 90 degrees portion of ellipse.
  11156. * @author Nicola Asuni
  11157. * @public
  11158. * @since 2.1.000 (2008-01-08)
  11159. */
  11160. public function Ellipse($x0, $y0, $rx, $ry='', $angle=0, $astart=0, $afinish=360, $style='', $line_style=array(), $fill_color=array(), $nc=2) {
  11161. if ($this->state != 2) {
  11162. return;
  11163. }
  11164. if (TCPDF_STATIC::empty_string($ry) OR ($ry == 0)) {
  11165. $ry = $rx;
  11166. }
  11167. if (!(false === strpos($style, 'F')) AND isset($fill_color)) {
  11168. $this->SetFillColorArray($fill_color);
  11169. }
  11170. $op = TCPDF_STATIC::getPathPaintOperator($style);
  11171. if ($op == 'f') {
  11172. $line_style = array();
  11173. }
  11174. if ($line_style) {
  11175. $this->SetLineStyle($line_style);
  11176. }
  11177. $this->_outellipticalarc($x0, $y0, $rx, $ry, $angle, $astart, $afinish, false, $nc, true, true, false);
  11178. $this->_out($op);
  11179. }
  11180. /**
  11181. * Append an elliptical arc to the current path.
  11182. * An ellipse is formed from n Bezier curves.
  11183. * @param $xc (float) Abscissa of center point.
  11184. * @param $yc (float) Ordinate of center point.
  11185. * @param $rx (float) Horizontal radius.
  11186. * @param $ry (float) Vertical radius (if ry = 0 then is a circle, see Circle()). Default value: 0.
  11187. * @param $xang: (float) Angle between the X-axis and the major axis of the ellipse. Default value: 0.
  11188. * @param $angs: (float) Angle start of draw line. Default value: 0.
  11189. * @param $angf: (float) Angle finish of draw line. Default value: 360.
  11190. * @param $pie (boolean) if true do not mark the border point (used to draw pie sectors).
  11191. * @param $nc (integer) Number of curves used to draw a 90 degrees portion of ellipse.
  11192. * @param $startpoint (boolean) if true output a starting point.
  11193. * @param $ccw (boolean) if true draws in counter-clockwise.
  11194. * @param $svg (boolean) if true the angles are in svg mode (already calculated).
  11195. * @return array bounding box coordinates (x min, y min, x max, y max)
  11196. * @author Nicola Asuni
  11197. * @protected
  11198. * @since 4.9.019 (2010-04-26)
  11199. */
  11200. protected function _outellipticalarc($xc, $yc, $rx, $ry, $xang=0, $angs=0, $angf=360, $pie=false, $nc=2, $startpoint=true, $ccw=true, $svg=false) {
  11201. if (($rx <= 0) OR ($ry < 0)) {
  11202. return;
  11203. }
  11204. $k = $this->k;
  11205. if ($nc < 2) {
  11206. $nc = 2;
  11207. }
  11208. $xmin = 2147483647;
  11209. $ymin = 2147483647;
  11210. $xmax = 0;
  11211. $ymax = 0;
  11212. if ($pie) {
  11213. // center of the arc
  11214. $this->_outPoint($xc, $yc);
  11215. }
  11216. $xang = deg2rad((float) $xang);
  11217. $angs = deg2rad((float) $angs);
  11218. $angf = deg2rad((float) $angf);
  11219. if ($svg) {
  11220. $as = $angs;
  11221. $af = $angf;
  11222. } else {
  11223. $as = atan2((sin($angs) / $ry), (cos($angs) / $rx));
  11224. $af = atan2((sin($angf) / $ry), (cos($angf) / $rx));
  11225. }
  11226. if ($as < 0) {
  11227. $as += (2 * M_PI);
  11228. }
  11229. if ($af < 0) {
  11230. $af += (2 * M_PI);
  11231. }
  11232. if ($ccw AND ($as > $af)) {
  11233. // reverse rotation
  11234. $as -= (2 * M_PI);
  11235. } elseif (!$ccw AND ($as < $af)) {
  11236. // reverse rotation
  11237. $af -= (2 * M_PI);
  11238. }
  11239. $total_angle = ($af - $as);
  11240. if ($nc < 2) {
  11241. $nc = 2;
  11242. }
  11243. // total arcs to draw
  11244. $nc *= (2 * abs($total_angle) / M_PI);
  11245. $nc = round($nc) + 1;
  11246. // angle of each arc
  11247. $arcang = ($total_angle / $nc);
  11248. // center point in PDF coordinates
  11249. $x0 = $xc;
  11250. $y0 = ($this->h - $yc);
  11251. // starting angle
  11252. $ang = $as;
  11253. $alpha = sin($arcang) * ((sqrt(4 + (3 * pow(tan(($arcang) / 2), 2))) - 1) / 3);
  11254. $cos_xang = cos($xang);
  11255. $sin_xang = sin($xang);
  11256. $cos_ang = cos($ang);
  11257. $sin_ang = sin($ang);
  11258. // first arc point
  11259. $px1 = $x0 + ($rx * $cos_xang * $cos_ang) - ($ry * $sin_xang * $sin_ang);
  11260. $py1 = $y0 + ($rx * $sin_xang * $cos_ang) + ($ry * $cos_xang * $sin_ang);
  11261. // first Bezier control point
  11262. $qx1 = ($alpha * ((-$rx * $cos_xang * $sin_ang) - ($ry * $sin_xang * $cos_ang)));
  11263. $qy1 = ($alpha * ((-$rx * $sin_xang * $sin_ang) + ($ry * $cos_xang * $cos_ang)));
  11264. if ($pie) {
  11265. // line from center to arc starting point
  11266. $this->_outLine($px1, $this->h - $py1);
  11267. } elseif ($startpoint) {
  11268. // arc starting point
  11269. $this->_outPoint($px1, $this->h - $py1);
  11270. }
  11271. // draw arcs
  11272. for ($i = 1; $i <= $nc; ++$i) {
  11273. // starting angle
  11274. $ang = $as + ($i * $arcang);
  11275. if ($i == $nc) {
  11276. $ang = $af;
  11277. }
  11278. $cos_ang = cos($ang);
  11279. $sin_ang = sin($ang);
  11280. // second arc point
  11281. $px2 = $x0 + ($rx * $cos_xang * $cos_ang) - ($ry * $sin_xang * $sin_ang);
  11282. $py2 = $y0 + ($rx * $sin_xang * $cos_ang) + ($ry * $cos_xang * $sin_ang);
  11283. // second Bezier control point
  11284. $qx2 = ($alpha * ((-$rx * $cos_xang * $sin_ang) - ($ry * $sin_xang * $cos_ang)));
  11285. $qy2 = ($alpha * ((-$rx * $sin_xang * $sin_ang) + ($ry * $cos_xang * $cos_ang)));
  11286. // draw arc
  11287. $cx1 = ($px1 + $qx1);
  11288. $cy1 = ($this->h - ($py1 + $qy1));
  11289. $cx2 = ($px2 - $qx2);
  11290. $cy2 = ($this->h - ($py2 - $qy2));
  11291. $cx3 = $px2;
  11292. $cy3 = ($this->h - $py2);
  11293. $this->_outCurve($cx1, $cy1, $cx2, $cy2, $cx3, $cy3);
  11294. // get bounding box coordinates
  11295. $xmin = min($xmin, $cx1, $cx2, $cx3);
  11296. $ymin = min($ymin, $cy1, $cy2, $cy3);
  11297. $xmax = max($xmax, $cx1, $cx2, $cx3);
  11298. $ymax = max($ymax, $cy1, $cy2, $cy3);
  11299. // move to next point
  11300. $px1 = $px2;
  11301. $py1 = $py2;
  11302. $qx1 = $qx2;
  11303. $qy1 = $qy2;
  11304. }
  11305. if ($pie) {
  11306. $this->_outLine($xc, $yc);
  11307. // get bounding box coordinates
  11308. $xmin = min($xmin, $xc);
  11309. $ymin = min($ymin, $yc);
  11310. $xmax = max($xmax, $xc);
  11311. $ymax = max($ymax, $yc);
  11312. }
  11313. return array($xmin, $ymin, $xmax, $ymax);
  11314. }
  11315. /**
  11316. * Draws a circle.
  11317. * A circle is formed from n Bezier curves.
  11318. * @param $x0 (float) Abscissa of center point.
  11319. * @param $y0 (float) Ordinate of center point.
  11320. * @param $r (float) Radius.
  11321. * @param $angstr: (float) Angle start of draw line. Default value: 0.
  11322. * @param $angend: (float) Angle finish of draw line. Default value: 360.
  11323. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  11324. * @param $line_style (array) Line style of circle. Array like for SetLineStyle(). Default value: default line style (empty array).
  11325. * @param $fill_color (array) Fill color. Format: array(red, green, blue). Default value: default color (empty array).
  11326. * @param $nc (integer) Number of curves used to draw a 90 degrees portion of circle.
  11327. * @public
  11328. * @since 2.1.000 (2008-01-08)
  11329. */
  11330. public function Circle($x0, $y0, $r, $angstr=0, $angend=360, $style='', $line_style=array(), $fill_color=array(), $nc=2) {
  11331. $this->Ellipse($x0, $y0, $r, $r, 0, $angstr, $angend, $style, $line_style, $fill_color, $nc);
  11332. }
  11333. /**
  11334. * Draws a polygonal line
  11335. * @param $p (array) Points 0 to ($np - 1). Array with values (x0, y0, x1, y1,..., x(np-1), y(np - 1))
  11336. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  11337. * @param $line_style (array) Line style of polygon. Array with keys among the following:
  11338. * <ul>
  11339. * <li>all: Line style of all lines. Array like for SetLineStyle().</li>
  11340. * <li>0 to ($np - 1): Line style of each line. Array like for SetLineStyle().</li>
  11341. * </ul>
  11342. * If a key is not present or is null, not draws the line. Default value is default line style (empty array).
  11343. * @param $fill_color (array) 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).
  11344. * @since 4.8.003 (2009-09-15)
  11345. * @public
  11346. */
  11347. public function PolyLine($p, $style='', $line_style=array(), $fill_color=array()) {
  11348. $this->Polygon($p, $style, $line_style, $fill_color, false);
  11349. }
  11350. /**
  11351. * Draws a polygon.
  11352. * @param $p (array) Points 0 to ($np - 1). Array with values (x0, y0, x1, y1,..., x(np-1), y(np - 1))
  11353. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  11354. * @param $line_style (array) Line style of polygon. Array with keys among the following:
  11355. * <ul>
  11356. * <li>all: Line style of all lines. Array like for SetLineStyle().</li>
  11357. * <li>0 to ($np - 1): Line style of each line. Array like for SetLineStyle().</li>
  11358. * </ul>
  11359. * If a key is not present or is null, not draws the line. Default value is default line style (empty array).
  11360. * @param $fill_color (array) 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).
  11361. * @param $closed (boolean) if true the polygon is closes, otherwise will remain open
  11362. * @public
  11363. * @since 2.1.000 (2008-01-08)
  11364. */
  11365. public function Polygon($p, $style='', $line_style=array(), $fill_color=array(), $closed=true) {
  11366. if ($this->state != 2) {
  11367. return;
  11368. }
  11369. $nc = count($p); // number of coordinates
  11370. $np = $nc / 2; // number of points
  11371. if ($closed) {
  11372. // close polygon by adding the first 2 points at the end (one line)
  11373. for ($i = 0; $i < 4; ++$i) {
  11374. $p[$nc + $i] = $p[$i];
  11375. }
  11376. // copy style for the last added line
  11377. if (isset($line_style[0])) {
  11378. $line_style[$np] = $line_style[0];
  11379. }
  11380. $nc += 4;
  11381. }
  11382. if (!(false === strpos($style, 'F')) AND isset($fill_color)) {
  11383. $this->SetFillColorArray($fill_color);
  11384. }
  11385. $op = TCPDF_STATIC::getPathPaintOperator($style);
  11386. if ($op == 'f') {
  11387. $line_style = array();
  11388. }
  11389. $draw = true;
  11390. if ($line_style) {
  11391. if (isset($line_style['all'])) {
  11392. $this->SetLineStyle($line_style['all']);
  11393. } else {
  11394. $draw = false;
  11395. if ($op == 'B') {
  11396. // draw fill
  11397. $op = 'f';
  11398. $this->_outPoint($p[0], $p[1]);
  11399. for ($i = 2; $i < $nc; $i = $i + 2) {
  11400. $this->_outLine($p[$i], $p[$i + 1]);
  11401. }
  11402. $this->_out($op);
  11403. }
  11404. // draw outline
  11405. $this->_outPoint($p[0], $p[1]);
  11406. for ($i = 2; $i < $nc; $i = $i + 2) {
  11407. $line_num = ($i / 2) - 1;
  11408. if (isset($line_style[$line_num])) {
  11409. if ($line_style[$line_num] != 0) {
  11410. if (is_array($line_style[$line_num])) {
  11411. $this->_out('S');
  11412. $this->SetLineStyle($line_style[$line_num]);
  11413. $this->_outPoint($p[$i - 2], $p[$i - 1]);
  11414. $this->_outLine($p[$i], $p[$i + 1]);
  11415. $this->_out('S');
  11416. $this->_outPoint($p[$i], $p[$i + 1]);
  11417. } else {
  11418. $this->_outLine($p[$i], $p[$i + 1]);
  11419. }
  11420. }
  11421. } else {
  11422. $this->_outLine($p[$i], $p[$i + 1]);
  11423. }
  11424. }
  11425. $this->_out($op);
  11426. }
  11427. }
  11428. if ($draw) {
  11429. $this->_outPoint($p[0], $p[1]);
  11430. for ($i = 2; $i < $nc; $i = $i + 2) {
  11431. $this->_outLine($p[$i], $p[$i + 1]);
  11432. }
  11433. $this->_out($op);
  11434. }
  11435. }
  11436. /**
  11437. * Draws a regular polygon.
  11438. * @param $x0 (float) Abscissa of center point.
  11439. * @param $y0 (float) Ordinate of center point.
  11440. * @param $r: (float) Radius of inscribed circle.
  11441. * @param $ns (integer) Number of sides.
  11442. * @param $angle (float) Angle oriented (anti-clockwise). Default value: 0.
  11443. * @param $draw_circle (boolean) Draw inscribed circle or not. Default value: false.
  11444. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  11445. * @param $line_style (array) Line style of polygon sides. Array with keys among the following:
  11446. * <ul>
  11447. * <li>all: Line style of all sides. Array like for SetLineStyle().</li>
  11448. * <li>0 to ($ns - 1): Line style of each side. Array like for SetLineStyle().</li>
  11449. * </ul>
  11450. * If a key is not present or is null, not draws the side. Default value is default line style (empty array).
  11451. * @param $fill_color (array) Fill color. Format: array(red, green, blue). Default value: default color (empty array).
  11452. * @param $circle_style (string) Style of rendering of inscribed circle (if draws). Possible values are:
  11453. * <ul>
  11454. * <li>D or empty string: Draw (default).</li>
  11455. * <li>F: Fill.</li>
  11456. * <li>DF or FD: Draw and fill.</li>
  11457. * <li>CNZ: Clipping mode (using the even-odd rule to determine which regions lie inside the clipping path).</li>
  11458. * <li>CEO: Clipping mode (using the nonzero winding number rule to determine which regions lie inside the clipping path).</li>
  11459. * </ul>
  11460. * @param $circle_outLine_style (array) Line style of inscribed circle (if draws). Array like for SetLineStyle(). Default value: default line style (empty array).
  11461. * @param $circle_fill_color (array) Fill color of inscribed circle (if draws). Format: array(red, green, blue). Default value: default color (empty array).
  11462. * @public
  11463. * @since 2.1.000 (2008-01-08)
  11464. */
  11465. 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()) {
  11466. if (3 > $ns) {
  11467. $ns = 3;
  11468. }
  11469. if ($draw_circle) {
  11470. $this->Circle($x0, $y0, $r, 0, 360, $circle_style, $circle_outLine_style, $circle_fill_color);
  11471. }
  11472. $p = array();
  11473. for ($i = 0; $i < $ns; ++$i) {
  11474. $a = $angle + ($i * 360 / $ns);
  11475. $a_rad = deg2rad((float) $a);
  11476. $p[] = $x0 + ($r * sin($a_rad));
  11477. $p[] = $y0 + ($r * cos($a_rad));
  11478. }
  11479. $this->Polygon($p, $style, $line_style, $fill_color);
  11480. }
  11481. /**
  11482. * Draws a star polygon
  11483. * @param $x0 (float) Abscissa of center point.
  11484. * @param $y0 (float) Ordinate of center point.
  11485. * @param $r (float) Radius of inscribed circle.
  11486. * @param $nv (integer) Number of vertices.
  11487. * @param $ng (integer) Number of gap (if ($ng % $nv = 1) then is a regular polygon).
  11488. * @param $angle: (float) Angle oriented (anti-clockwise). Default value: 0.
  11489. * @param $draw_circle: (boolean) Draw inscribed circle or not. Default value is false.
  11490. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  11491. * @param $line_style (array) Line style of polygon sides. Array with keys among the following:
  11492. * <ul>
  11493. * <li>all: Line style of all sides. Array like for
  11494. * SetLineStyle().</li>
  11495. * <li>0 to (n - 1): Line style of each side. Array like for SetLineStyle().</li>
  11496. * </ul>
  11497. * If a key is not present or is null, not draws the side. Default value is default line style (empty array).
  11498. * @param $fill_color (array) Fill color. Format: array(red, green, blue). Default value: default color (empty array).
  11499. * @param $circle_style (string) Style of rendering of inscribed circle (if draws). Possible values are:
  11500. * <ul>
  11501. * <li>D or empty string: Draw (default).</li>
  11502. * <li>F: Fill.</li>
  11503. * <li>DF or FD: Draw and fill.</li>
  11504. * <li>CNZ: Clipping mode (using the even-odd rule to determine which regions lie inside the clipping path).</li>
  11505. * <li>CEO: Clipping mode (using the nonzero winding number rule to determine which regions lie inside the clipping path).</li>
  11506. * </ul>
  11507. * @param $circle_outLine_style (array) Line style of inscribed circle (if draws). Array like for SetLineStyle(). Default value: default line style (empty array).
  11508. * @param $circle_fill_color (array) Fill color of inscribed circle (if draws). Format: array(red, green, blue). Default value: default color (empty array).
  11509. * @public
  11510. * @since 2.1.000 (2008-01-08)
  11511. */
  11512. 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()) {
  11513. if ($nv < 2) {
  11514. $nv = 2;
  11515. }
  11516. if ($draw_circle) {
  11517. $this->Circle($x0, $y0, $r, 0, 360, $circle_style, $circle_outLine_style, $circle_fill_color);
  11518. }
  11519. $p2 = array();
  11520. $visited = array();
  11521. for ($i = 0; $i < $nv; ++$i) {
  11522. $a = $angle + ($i * 360 / $nv);
  11523. $a_rad = deg2rad((float) $a);
  11524. $p2[] = $x0 + ($r * sin($a_rad));
  11525. $p2[] = $y0 + ($r * cos($a_rad));
  11526. $visited[] = false;
  11527. }
  11528. $p = array();
  11529. $i = 0;
  11530. do {
  11531. $p[] = $p2[$i * 2];
  11532. $p[] = $p2[($i * 2) + 1];
  11533. $visited[$i] = true;
  11534. $i += $ng;
  11535. $i %= $nv;
  11536. } while (!$visited[$i]);
  11537. $this->Polygon($p, $style, $line_style, $fill_color);
  11538. }
  11539. /**
  11540. * Draws a rounded rectangle.
  11541. * @param $x (float) Abscissa of upper-left corner.
  11542. * @param $y (float) Ordinate of upper-left corner.
  11543. * @param $w (float) Width.
  11544. * @param $h (float) Height.
  11545. * @param $r (float) the radius of the circle used to round off the corners of the rectangle.
  11546. * @param $round_corner (string) 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").
  11547. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  11548. * @param $border_style (array) Border style of rectangle. Array like for SetLineStyle(). Default value: default line style (empty array).
  11549. * @param $fill_color (array) 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).
  11550. * @public
  11551. * @since 2.1.000 (2008-01-08)
  11552. */
  11553. public function RoundedRect($x, $y, $w, $h, $r, $round_corner='1111', $style='', $border_style=array(), $fill_color=array()) {
  11554. $this->RoundedRectXY($x, $y, $w, $h, $r, $r, $round_corner, $style, $border_style, $fill_color);
  11555. }
  11556. /**
  11557. * Draws a rounded rectangle.
  11558. * @param $x (float) Abscissa of upper-left corner.
  11559. * @param $y (float) Ordinate of upper-left corner.
  11560. * @param $w (float) Width.
  11561. * @param $h (float) Height.
  11562. * @param $rx (float) the x-axis radius of the ellipse used to round off the corners of the rectangle.
  11563. * @param $ry (float) the y-axis radius of the ellipse used to round off the corners of the rectangle.
  11564. * @param $round_corner (string) 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").
  11565. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  11566. * @param $border_style (array) Border style of rectangle. Array like for SetLineStyle(). Default value: default line style (empty array).
  11567. * @param $fill_color (array) 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).
  11568. * @public
  11569. * @since 4.9.019 (2010-04-22)
  11570. */
  11571. public function RoundedRectXY($x, $y, $w, $h, $rx, $ry, $round_corner='1111', $style='', $border_style=array(), $fill_color=array()) {
  11572. if ($this->state != 2) {
  11573. return;
  11574. }
  11575. if (($round_corner == '0000') OR (($rx == $ry) AND ($rx == 0))) {
  11576. // Not rounded
  11577. $this->Rect($x, $y, $w, $h, $style, $border_style, $fill_color);
  11578. return;
  11579. }
  11580. // Rounded
  11581. if (!(false === strpos($style, 'F')) AND isset($fill_color)) {
  11582. $this->SetFillColorArray($fill_color);
  11583. }
  11584. $op = TCPDF_STATIC::getPathPaintOperator($style);
  11585. if ($op == 'f') {
  11586. $border_style = array();
  11587. }
  11588. if ($border_style) {
  11589. $this->SetLineStyle($border_style);
  11590. }
  11591. $MyArc = 4 / 3 * (sqrt(2) - 1);
  11592. $this->_outPoint($x + $rx, $y);
  11593. $xc = $x + $w - $rx;
  11594. $yc = $y + $ry;
  11595. $this->_outLine($xc, $y);
  11596. if ($round_corner[0]) {
  11597. $this->_outCurve($xc + ($rx * $MyArc), $yc - $ry, $xc + $rx, $yc - ($ry * $MyArc), $xc + $rx, $yc);
  11598. } else {
  11599. $this->_outLine($x + $w, $y);
  11600. }
  11601. $xc = $x + $w - $rx;
  11602. $yc = $y + $h - $ry;
  11603. $this->_outLine($x + $w, $yc);
  11604. if ($round_corner[1]) {
  11605. $this->_outCurve($xc + $rx, $yc + ($ry * $MyArc), $xc + ($rx * $MyArc), $yc + $ry, $xc, $yc + $ry);
  11606. } else {
  11607. $this->_outLine($x + $w, $y + $h);
  11608. }
  11609. $xc = $x + $rx;
  11610. $yc = $y + $h - $ry;
  11611. $this->_outLine($xc, $y + $h);
  11612. if ($round_corner[2]) {
  11613. $this->_outCurve($xc - ($rx * $MyArc), $yc + $ry, $xc - $rx, $yc + ($ry * $MyArc), $xc - $rx, $yc);
  11614. } else {
  11615. $this->_outLine($x, $y + $h);
  11616. }
  11617. $xc = $x + $rx;
  11618. $yc = $y + $ry;
  11619. $this->_outLine($x, $yc);
  11620. if ($round_corner[3]) {
  11621. $this->_outCurve($xc - $rx, $yc - ($ry * $MyArc), $xc - ($rx * $MyArc), $yc - $ry, $xc, $yc - $ry);
  11622. } else {
  11623. $this->_outLine($x, $y);
  11624. $this->_outLine($x + $rx, $y);
  11625. }
  11626. $this->_out($op);
  11627. }
  11628. /**
  11629. * Draws a grahic arrow.
  11630. * @param $x0 (float) Abscissa of first point.
  11631. * @param $y0 (float) Ordinate of first point.
  11632. * @param $x1 (float) Abscissa of second point.
  11633. * @param $y1 (float) Ordinate of second point.
  11634. * @param $head_style (int) (0 = draw only arrowhead arms, 1 = draw closed arrowhead, but no fill, 2 = closed and filled arrowhead, 3 = filled arrowhead)
  11635. * @param $arm_size (float) length of arrowhead arms
  11636. * @param $arm_angle (int) angle between an arm and the shaft
  11637. * @author Piotr Galecki, Nicola Asuni, Andy Meier
  11638. * @since 4.6.018 (2009-07-10)
  11639. */
  11640. public function Arrow($x0, $y0, $x1, $y1, $head_style=0, $arm_size=5, $arm_angle=15) {
  11641. // getting arrow direction angle
  11642. // 0 deg angle is when both arms go along X axis. angle grows clockwise.
  11643. $dir_angle = atan2(($y0 - $y1), ($x0 - $x1));
  11644. if ($dir_angle < 0) {
  11645. $dir_angle += (2 * M_PI);
  11646. }
  11647. $arm_angle = deg2rad($arm_angle);
  11648. $sx1 = $x1;
  11649. $sy1 = $y1;
  11650. if ($head_style > 0) {
  11651. // calculate the stopping point for the arrow shaft
  11652. $sx1 = $x1 + (($arm_size - $this->LineWidth) * cos($dir_angle));
  11653. $sy1 = $y1 + (($arm_size - $this->LineWidth) * sin($dir_angle));
  11654. }
  11655. // main arrow line / shaft
  11656. $this->Line($x0, $y0, $sx1, $sy1);
  11657. // left arrowhead arm tip
  11658. $x2L = $x1 + ($arm_size * cos($dir_angle + $arm_angle));
  11659. $y2L = $y1 + ($arm_size * sin($dir_angle + $arm_angle));
  11660. // right arrowhead arm tip
  11661. $x2R = $x1 + ($arm_size * cos($dir_angle - $arm_angle));
  11662. $y2R = $y1 + ($arm_size * sin($dir_angle - $arm_angle));
  11663. $mode = 'D';
  11664. $style = array();
  11665. switch ($head_style) {
  11666. case 0: {
  11667. // draw only arrowhead arms
  11668. $mode = 'D';
  11669. $style = array(1, 1, 0);
  11670. break;
  11671. }
  11672. case 1: {
  11673. // draw closed arrowhead, but no fill
  11674. $mode = 'D';
  11675. break;
  11676. }
  11677. case 2: {
  11678. // closed and filled arrowhead
  11679. $mode = 'DF';
  11680. break;
  11681. }
  11682. case 3: {
  11683. // filled arrowhead
  11684. $mode = 'F';
  11685. break;
  11686. }
  11687. }
  11688. $this->Polygon(array($x2L, $y2L, $x1, $y1, $x2R, $y2R), $mode, $style, array());
  11689. }
  11690. // END GRAPHIC FUNCTIONS SECTION -----------------------
  11691. /**
  11692. * Add a Named Destination.
  11693. * NOTE: destination names are unique, so only last entry will be saved.
  11694. * @param $name (string) Destination name.
  11695. * @param $y (float) Y position in user units of the destiantion on the selected page (default = -1 = current position; 0 = page start;).
  11696. * @param $page (int|string) 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.
  11697. * @param $x (float) X position in user units of the destiantion on the selected page (default = -1 = current position;).
  11698. * @return (string) Stripped named destination identifier or false in case of error.
  11699. * @public
  11700. * @author Christian Deligant, Nicola Asuni
  11701. * @since 5.9.097 (2011-06-23)
  11702. */
  11703. public function setDestination($name, $y=-1, $page='', $x=-1) {
  11704. // remove unsupported characters
  11705. $name = TCPDF_STATIC::encodeNameObject($name);
  11706. if (TCPDF_STATIC::empty_string($name)) {
  11707. return false;
  11708. }
  11709. if ($y == -1) {
  11710. $y = $this->GetY();
  11711. } elseif ($y < 0) {
  11712. $y = 0;
  11713. } elseif ($y > $this->h) {
  11714. $y = $this->h;
  11715. }
  11716. if ($x == -1) {
  11717. $x = $this->GetX();
  11718. } elseif ($x < 0) {
  11719. $x = 0;
  11720. } elseif ($x > $this->w) {
  11721. $x = $this->w;
  11722. }
  11723. $fixed = false;
  11724. if (!empty($page) AND ($page[0] == '*')) {
  11725. $page = intval(substr($page, 1));
  11726. // this page number will not be changed when moving/add/deleting pages
  11727. $fixed = true;
  11728. }
  11729. if (empty($page)) {
  11730. $page = $this->PageNo();
  11731. if (empty($page)) {
  11732. return;
  11733. }
  11734. }
  11735. $this->dests[$name] = array('x' => $x, 'y' => $y, 'p' => $page, 'f' => $fixed);
  11736. return $name;
  11737. }
  11738. /**
  11739. * Return the Named Destination array.
  11740. * @return (array) Named Destination array.
  11741. * @public
  11742. * @author Nicola Asuni
  11743. * @since 5.9.097 (2011-06-23)
  11744. */
  11745. public function getDestination() {
  11746. return $this->dests;
  11747. }
  11748. /**
  11749. * Insert Named Destinations.
  11750. * @protected
  11751. * @author Johannes G\FCntert, Nicola Asuni
  11752. * @since 5.9.098 (2011-06-23)
  11753. */
  11754. protected function _putdests() {
  11755. if (empty($this->dests)) {
  11756. return;
  11757. }
  11758. $this->n_dests = $this->_newobj();
  11759. $out = ' <<';
  11760. foreach($this->dests as $name => $o) {
  11761. $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)));
  11762. }
  11763. $out .= ' >>';
  11764. $out .= "\n".'endobj';
  11765. $this->_out($out);
  11766. }
  11767. /**
  11768. * Adds a bookmark - alias for Bookmark().
  11769. * @param $txt (string) Bookmark description.
  11770. * @param $level (int) Bookmark level (minimum value is 0).
  11771. * @param $y (float) Y position in user units of the bookmark on the selected page (default = -1 = current position; 0 = page start;).
  11772. * @param $page (int|string) 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.
  11773. * @param $style (string) Font style: B = Bold, I = Italic, BI = Bold + Italic.
  11774. * @param $color (array) RGB color array (values from 0 to 255).
  11775. * @param $x (float) X position in user units of the bookmark on the selected page (default = -1 = current position;).
  11776. * @param $link (mixed) URL, or numerical link ID, or named destination (# character followed by the destination name), or embedded file (* character followed by the file name).
  11777. * @public
  11778. */
  11779. public function setBookmark($txt, $level=0, $y=-1, $page='', $style='', $color=array(0,0,0), $x=-1, $link='') {
  11780. $this->Bookmark($txt, $level, $y, $page, $style, $color, $x, $link);
  11781. }
  11782. /**
  11783. * Adds a bookmark.
  11784. * @param $txt (string) Bookmark description.
  11785. * @param $level (int) Bookmark level (minimum value is 0).
  11786. * @param $y (float) Y position in user units of the bookmark on the selected page (default = -1 = current position; 0 = page start;).
  11787. * @param $page (int|string) 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.
  11788. * @param $style (string) Font style: B = Bold, I = Italic, BI = Bold + Italic.
  11789. * @param $color (array) RGB color array (values from 0 to 255).
  11790. * @param $x (float) X position in user units of the bookmark on the selected page (default = -1 = current position;).
  11791. * @param $link (mixed) URL, or numerical link ID, or named destination (# character followed by the destination name), or embedded file (* character followed by the file name).
  11792. * @public
  11793. * @since 2.1.002 (2008-02-12)
  11794. */
  11795. public function Bookmark($txt, $level=0, $y=-1, $page='', $style='', $color=array(0,0,0), $x=-1, $link='') {
  11796. if ($level < 0) {
  11797. $level = 0;
  11798. }
  11799. if (isset($this->outlines[0])) {
  11800. $lastoutline = end($this->outlines);
  11801. $maxlevel = $lastoutline['l'] + 1;
  11802. } else {
  11803. $maxlevel = 0;
  11804. }
  11805. if ($level > $maxlevel) {
  11806. $level = $maxlevel;
  11807. }
  11808. if ($y == -1) {
  11809. $y = $this->GetY();
  11810. } elseif ($y < 0) {
  11811. $y = 0;
  11812. } elseif ($y > $this->h) {
  11813. $y = $this->h;
  11814. }
  11815. if ($x == -1) {
  11816. $x = $this->GetX();
  11817. } elseif ($x < 0) {
  11818. $x = 0;
  11819. } elseif ($x > $this->w) {
  11820. $x = $this->w;
  11821. }
  11822. $fixed = false;
  11823. if (!empty($page) AND ($page[0] == '*')) {
  11824. $page = intval(substr($page, 1));
  11825. // this page number will not be changed when moving/add/deleting pages
  11826. $fixed = true;
  11827. }
  11828. if (empty($page)) {
  11829. $page = $this->PageNo();
  11830. if (empty($page)) {
  11831. return;
  11832. }
  11833. }
  11834. $this->outlines[] = array('t' => $txt, 'l' => $level, 'x' => $x, 'y' => $y, 'p' => $page, 'f' => $fixed, 's' => strtoupper($style), 'c' => $color, 'u' => $link);
  11835. }
  11836. /**
  11837. * Sort bookmarks for page and key.
  11838. * @protected
  11839. * @since 5.9.119 (2011-09-19)
  11840. */
  11841. protected function sortBookmarks() {
  11842. // get sorting columns
  11843. $outline_p = array();
  11844. $outline_y = array();
  11845. foreach ($this->outlines as $key => $row) {
  11846. $outline_p[$key] = $row['p'];
  11847. $outline_k[$key] = $key;
  11848. }
  11849. // sort outlines by page and original position
  11850. array_multisort($outline_p, SORT_NUMERIC, SORT_ASC, $outline_k, SORT_NUMERIC, SORT_ASC, $this->outlines);
  11851. }
  11852. /**
  11853. * Create a bookmark PDF string.
  11854. * @protected
  11855. * @author Olivier Plathey, Nicola Asuni
  11856. * @since 2.1.002 (2008-02-12)
  11857. */
  11858. protected function _putbookmarks() {
  11859. $nb = count($this->outlines);
  11860. if ($nb == 0) {
  11861. return;
  11862. }
  11863. // sort bookmarks
  11864. $this->sortBookmarks();
  11865. $lru = array();
  11866. $level = 0;
  11867. foreach ($this->outlines as $i => $o) {
  11868. if ($o['l'] > 0) {
  11869. $parent = $lru[($o['l'] - 1)];
  11870. //Set parent and last pointers
  11871. $this->outlines[$i]['parent'] = $parent;
  11872. $this->outlines[$parent]['last'] = $i;
  11873. if ($o['l'] > $level) {
  11874. //Level increasing: set first pointer
  11875. $this->outlines[$parent]['first'] = $i;
  11876. }
  11877. } else {
  11878. $this->outlines[$i]['parent'] = $nb;
  11879. }
  11880. if (($o['l'] <= $level) AND ($i > 0)) {
  11881. //Set prev and next pointers
  11882. $prev = $lru[$o['l']];
  11883. $this->outlines[$prev]['next'] = $i;
  11884. $this->outlines[$i]['prev'] = $prev;
  11885. }
  11886. $lru[$o['l']] = $i;
  11887. $level = $o['l'];
  11888. }
  11889. //Outline items
  11890. $n = $this->n + 1;
  11891. $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';
  11892. foreach ($this->outlines as $i => $o) {
  11893. $oid = $this->_newobj();
  11894. // covert HTML title to string
  11895. $title = preg_replace($nltags, "\n", $o['t']);
  11896. $title = preg_replace("/[\r]+/si", '', $title);
  11897. $title = preg_replace("/[\n]+/si", "\n", $title);
  11898. $title = strip_tags($title);
  11899. $title = $this->stringTrim($title);
  11900. $out = '<</Title '.$this->_textstring($title, $oid);
  11901. $out .= ' /Parent '.($n + $o['parent']).' 0 R';
  11902. if (isset($o['prev'])) {
  11903. $out .= ' /Prev '.($n + $o['prev']).' 0 R';
  11904. }
  11905. if (isset($o['next'])) {
  11906. $out .= ' /Next '.($n + $o['next']).' 0 R';
  11907. }
  11908. if (isset($o['first'])) {
  11909. $out .= ' /First '.($n + $o['first']).' 0 R';
  11910. }
  11911. if (isset($o['last'])) {
  11912. $out .= ' /Last '.($n + $o['last']).' 0 R';
  11913. }
  11914. if (isset($o['u']) AND !empty($o['u'])) {
  11915. // link
  11916. if (is_string($o['u'])) {
  11917. if ($o['u'][0] == '#') {
  11918. // internal destination
  11919. $out .= ' /Dest /'.TCPDF_STATIC::encodeNameObject(substr($o['u'], 1));
  11920. } elseif ($o['u'][0] == '%') {
  11921. // embedded PDF file
  11922. $filename = basename(substr($o['u'], 1));
  11923. $out .= ' /A <</S /GoToE /D [0 /Fit] /NewWindow true /T << /R /C /P '.($o['p'] - 1).' /A '.$this->embeddedfiles[$filename]['a'].' >> >>';
  11924. } elseif ($o['u'][0] == '*') {
  11925. // embedded generic file
  11926. $filename = basename(substr($o['u'], 1));
  11927. $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});';
  11928. $out .= ' /A <</S /JavaScript /JS '.$this->_textstring($jsa, $oid).'>>';
  11929. } else {
  11930. // external URI link
  11931. $out .= ' /A <</S /URI /URI '.$this->_datastring($this->unhtmlentities($o['u']), $oid).'>>';
  11932. }
  11933. } elseif (isset($this->links[$o['u']])) {
  11934. // internal link ID
  11935. $l = $this->links[$o['u']];
  11936. if (isset($this->page_obj_id[($l['p'])])) {
  11937. $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)));
  11938. }
  11939. }
  11940. } elseif (isset($this->page_obj_id[($o['p'])])) {
  11941. // link to a page
  11942. $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)));
  11943. }
  11944. // set font style
  11945. $style = 0;
  11946. if (!empty($o['s'])) {
  11947. // bold
  11948. if (strpos($o['s'], 'B') !== false) {
  11949. $style |= 2;
  11950. }
  11951. // oblique
  11952. if (strpos($o['s'], 'I') !== false) {
  11953. $style |= 1;
  11954. }
  11955. }
  11956. $out .= sprintf(' /F %d', $style);
  11957. // set bookmark color
  11958. if (isset($o['c']) AND is_array($o['c']) AND (count($o['c']) == 3)) {
  11959. $color = array_values($o['c']);
  11960. $out .= sprintf(' /C [%F %F %F]', ($color[0] / 255), ($color[1] / 255), ($color[2] / 255));
  11961. } else {
  11962. // black
  11963. $out .= ' /C [0.0 0.0 0.0]';
  11964. }
  11965. $out .= ' /Count 0'; // normally closed item
  11966. $out .= ' >>';
  11967. $out .= "\n".'endobj';
  11968. $this->_out($out);
  11969. }
  11970. //Outline root
  11971. $this->OutlineRoot = $this->_newobj();
  11972. $this->_out('<< /Type /Outlines /First '.$n.' 0 R /Last '.($n + $lru[0]).' 0 R >>'."\n".'endobj');
  11973. }
  11974. // --- JAVASCRIPT ------------------------------------------------------
  11975. /**
  11976. * Adds a javascript
  11977. * @param $script (string) Javascript code
  11978. * @public
  11979. * @author Johannes G\FCntert, Nicola Asuni
  11980. * @since 2.1.002 (2008-02-12)
  11981. */
  11982. public function IncludeJS($script) {
  11983. $this->javascript .= $script;
  11984. }
  11985. /**
  11986. * Adds a javascript object and return object ID
  11987. * @param $script (string) Javascript code
  11988. * @param $onload (boolean) if true executes this object when opening the document
  11989. * @return int internal object ID
  11990. * @public
  11991. * @author Nicola Asuni
  11992. * @since 4.8.000 (2009-09-07)
  11993. */
  11994. public function addJavascriptObject($script, $onload=false) {
  11995. if ($this->pdfa_mode) {
  11996. // javascript is not allowed in PDF/A mode
  11997. return false;
  11998. }
  11999. ++$this->n;
  12000. $this->js_objects[$this->n] = array('n' => $this->n, 'js' => $script, 'onload' => $onload);
  12001. return $this->n;
  12002. }
  12003. /**
  12004. * Create a javascript PDF string.
  12005. * @protected
  12006. * @author Johannes G\FCntert, Nicola Asuni
  12007. * @since 2.1.002 (2008-02-12)
  12008. */
  12009. protected function _putjavascript() {
  12010. if ($this->pdfa_mode OR (empty($this->javascript) AND empty($this->js_objects))) {
  12011. return;
  12012. }
  12013. if (strpos($this->javascript, 'this.addField') > 0) {
  12014. if (!$this->ur['enabled']) {
  12015. //$this->setUserRights();
  12016. }
  12017. // the following two lines are used to avoid form fields duplication after saving
  12018. // The addField method only works when releasing user rights (UR3)
  12019. $jsa = sprintf("ftcpdfdocsaved=this.addField('%s','%s',%d,[%F,%F,%F,%F]);", 'tcpdfdocsaved', 'text', 0, 0, 1, 0, 1);
  12020. $jsb = "getField('tcpdfdocsaved').value='saved';";
  12021. $this->javascript = $jsa."\n".$this->javascript."\n".$jsb;
  12022. }
  12023. // name tree for javascript
  12024. $this->n_js = '<< /Names [';
  12025. if (!empty($this->javascript)) {
  12026. $this->n_js .= ' (EmbeddedJS) '.($this->n + 1).' 0 R';
  12027. }
  12028. if (!empty($this->js_objects)) {
  12029. foreach ($this->js_objects as $key => $val) {
  12030. if ($val['onload']) {
  12031. $this->n_js .= ' (JS'.$key.') '.$key.' 0 R';
  12032. }
  12033. }
  12034. }
  12035. $this->n_js .= ' ] >>';
  12036. // default Javascript object
  12037. if (!empty($this->javascript)) {
  12038. $obj_id = $this->_newobj();
  12039. $out = '<< /S /JavaScript';
  12040. $out .= ' /JS '.$this->_textstring($this->javascript, $obj_id);
  12041. $out .= ' >>';
  12042. $out .= "\n".'endobj';
  12043. $this->_out($out);
  12044. }
  12045. // additional Javascript objects
  12046. if (!empty($this->js_objects)) {
  12047. foreach ($this->js_objects as $key => $val) {
  12048. $out = $this->_getobj($key)."\n".' << /S /JavaScript /JS '.$this->_textstring($val['js'], $key).' >>'."\n".'endobj';
  12049. $this->_out($out);
  12050. }
  12051. }
  12052. }
  12053. /**
  12054. * Adds a javascript form field.
  12055. * @param $type (string) field type
  12056. * @param $name (string) field name
  12057. * @param $x (int) horizontal position
  12058. * @param $y (int) vertical position
  12059. * @param $w (int) width
  12060. * @param $h (int) height
  12061. * @param $prop (array) javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  12062. * @protected
  12063. * @author Denis Van Nuffelen, Nicola Asuni
  12064. * @since 2.1.002 (2008-02-12)
  12065. */
  12066. protected function _addfield($type, $name, $x, $y, $w, $h, $prop) {
  12067. if ($this->rtl) {
  12068. $x = $x - $w;
  12069. }
  12070. // the followind avoid fields duplication after saving the document
  12071. $this->javascript .= "if (getField('tcpdfdocsaved').value != 'saved') {";
  12072. $k = $this->k;
  12073. $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";
  12074. $this->javascript .= 'f'.$name.'.textSize='.$this->FontSizePt.";\n";
  12075. foreach($prop as $key => $val) {
  12076. if (strcmp(substr($key, -5), 'Color') == 0) {
  12077. $val = TCPDF_COLORS::_JScolor($val);
  12078. } else {
  12079. $val = "'".$val."'";
  12080. }
  12081. $this->javascript .= 'f'.$name.'.'.$key.'='.$val.";\n";
  12082. }
  12083. if ($this->rtl) {
  12084. $this->x -= $w;
  12085. } else {
  12086. $this->x += $w;
  12087. }
  12088. $this->javascript .= '}';
  12089. }
  12090. // --- FORM FIELDS -----------------------------------------------------
  12091. /**
  12092. * Set default properties for form fields.
  12093. * @param $prop (array) javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  12094. * @public
  12095. * @author Nicola Asuni
  12096. * @since 4.8.000 (2009-09-06)
  12097. */
  12098. public function setFormDefaultProp($prop=array()) {
  12099. $this->default_form_prop = $prop;
  12100. }
  12101. /**
  12102. * Return the default properties for form fields.
  12103. * @return array $prop javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  12104. * @public
  12105. * @author Nicola Asuni
  12106. * @since 4.8.000 (2009-09-06)
  12107. */
  12108. public function getFormDefaultProp() {
  12109. return $this->default_form_prop;
  12110. }
  12111. /**
  12112. * Creates a text field
  12113. * @param $name (string) field name
  12114. * @param $w (float) Width of the rectangle
  12115. * @param $h (float) Height of the rectangle
  12116. * @param $prop (array) javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  12117. * @param $opt (array) annotation parameters. Possible values are described on official PDF32000_2008 reference.
  12118. * @param $x (float) Abscissa of the upper-left corner of the rectangle
  12119. * @param $y (float) Ordinate of the upper-left corner of the rectangle
  12120. * @param $js (boolean) if true put the field using JavaScript (requires Acrobat Writer to be rendered).
  12121. * @public
  12122. * @author Nicola Asuni
  12123. * @since 4.8.000 (2009-09-07)
  12124. */
  12125. public function TextField($name, $w, $h, $prop=array(), $opt=array(), $x='', $y='', $js=false) {
  12126. if ($x === '') {
  12127. $x = $this->x;
  12128. }
  12129. if ($y === '') {
  12130. $y = $this->y;
  12131. }
  12132. // check page for no-write regions and adapt page margins if necessary
  12133. list($x, $y) = $this->checkPageRegions($h, $x, $y);
  12134. if ($js) {
  12135. $this->_addfield('text', $name, $x, $y, $w, $h, $prop);
  12136. return;
  12137. }
  12138. // get default style
  12139. $prop = array_merge($this->getFormDefaultProp(), $prop);
  12140. // get annotation data
  12141. $popt = TCPDF_STATIC::getAnnotOptFromJSProp($prop, $this->spot_colors, $this->rtl);
  12142. // set default appearance stream
  12143. $this->annotation_fonts[$this->CurrentFont['fontkey']] = $this->CurrentFont['i'];
  12144. $fontstyle = sprintf('/F%d %F Tf %s', $this->CurrentFont['i'], $this->FontSizePt, $this->TextColor);
  12145. $popt['da'] = $fontstyle;
  12146. // build appearance stream
  12147. $popt['ap'] = array();
  12148. $popt['ap']['n'] = '/Tx BMC q '.$fontstyle.' ';
  12149. $text = '';
  12150. if (isset($prop['value']) AND !empty($prop['value'])) {
  12151. $text = $prop['value'];
  12152. } elseif (isset($opt['v']) AND !empty($opt['v'])) {
  12153. $text = $opt['v'];
  12154. }
  12155. $tmpid = $this->startTemplate($w, $h, false);
  12156. $align = '';
  12157. if (isset($popt['q'])) {
  12158. switch ($popt['q']) {
  12159. case 0: {
  12160. $align = 'L';
  12161. break;
  12162. }
  12163. case 1: {
  12164. $align = 'C';
  12165. break;
  12166. }
  12167. case 2: {
  12168. $align = 'R';
  12169. break;
  12170. }
  12171. default: {
  12172. $align = '';
  12173. break;
  12174. }
  12175. }
  12176. }
  12177. $this->MultiCell($w, $h, $text, 0, $align, false, 0, 0, 0, true, 0, false, true, 0, 'T', false);
  12178. $this->endTemplate();
  12179. --$this->n;
  12180. $popt['ap']['n'] .= $this->xobjects[$tmpid]['outdata'];
  12181. unset($this->xobjects[$tmpid]);
  12182. $popt['ap']['n'] .= 'Q EMC';
  12183. // merge options
  12184. $opt = array_merge($popt, $opt);
  12185. // remove some conflicting options
  12186. unset($opt['bs']);
  12187. // set remaining annotation data
  12188. $opt['Subtype'] = 'Widget';
  12189. $opt['ft'] = 'Tx';
  12190. $opt['t'] = $name;
  12191. // Additional annotation's parameters (check _putannotsobj() method):
  12192. //$opt['f']
  12193. //$opt['as']
  12194. //$opt['bs']
  12195. //$opt['be']
  12196. //$opt['c']
  12197. //$opt['border']
  12198. //$opt['h']
  12199. //$opt['mk'];
  12200. //$opt['mk']['r']
  12201. //$opt['mk']['bc'];
  12202. //$opt['mk']['bg'];
  12203. unset($opt['mk']['ca']);
  12204. unset($opt['mk']['rc']);
  12205. unset($opt['mk']['ac']);
  12206. unset($opt['mk']['i']);
  12207. unset($opt['mk']['ri']);
  12208. unset($opt['mk']['ix']);
  12209. unset($opt['mk']['if']);
  12210. //$opt['mk']['if']['sw'];
  12211. //$opt['mk']['if']['s'];
  12212. //$opt['mk']['if']['a'];
  12213. //$opt['mk']['if']['fb'];
  12214. unset($opt['mk']['tp']);
  12215. //$opt['tu']
  12216. //$opt['tm']
  12217. //$opt['ff']
  12218. //$opt['v']
  12219. //$opt['dv']
  12220. //$opt['a']
  12221. //$opt['aa']
  12222. //$opt['q']
  12223. $this->Annotation($x, $y, $w, $h, $name, $opt, 0);
  12224. if ($this->rtl) {
  12225. $this->x -= $w;
  12226. } else {
  12227. $this->x += $w;
  12228. }
  12229. }
  12230. /**
  12231. * Creates a RadioButton field.
  12232. * @param $name (string) Field name.
  12233. * @param $w (int) Width of the radio button.
  12234. * @param $prop (array) Javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  12235. * @param $opt (array) Annotation parameters. Possible values are described on official PDF32000_2008 reference.
  12236. * @param $onvalue (string) Value to be returned if selected.
  12237. * @param $checked (boolean) Define the initial state.
  12238. * @param $x (float) Abscissa of the upper-left corner of the rectangle
  12239. * @param $y (float) Ordinate of the upper-left corner of the rectangle
  12240. * @param $js (boolean) If true put the field using JavaScript (requires Acrobat Writer to be rendered).
  12241. * @public
  12242. * @author Nicola Asuni
  12243. * @since 4.8.000 (2009-09-07)
  12244. */
  12245. public function RadioButton($name, $w, $prop=array(), $opt=array(), $onvalue='On', $checked=false, $x='', $y='', $js=false) {
  12246. if ($x === '') {
  12247. $x = $this->x;
  12248. }
  12249. if ($y === '') {
  12250. $y = $this->y;
  12251. }
  12252. // check page for no-write regions and adapt page margins if necessary
  12253. list($x, $y) = $this->checkPageRegions($w, $x, $y);
  12254. if ($js) {
  12255. $this->_addfield('radiobutton', $name, $x, $y, $w, $w, $prop);
  12256. return;
  12257. }
  12258. if (TCPDF_STATIC::empty_string($onvalue)) {
  12259. $onvalue = 'On';
  12260. }
  12261. if ($checked) {
  12262. $defval = $onvalue;
  12263. } else {
  12264. $defval = 'Off';
  12265. }
  12266. // set font
  12267. $font = 'zapfdingbats';
  12268. if ($this->pdfa_mode) {
  12269. // all fonts must be embedded
  12270. $font = 'pdfa'.$font;
  12271. }
  12272. $this->AddFont($font);
  12273. $tmpfont = $this->getFontBuffer($font);
  12274. // set data for parent group
  12275. if (!isset($this->radiobutton_groups[$this->page])) {
  12276. $this->radiobutton_groups[$this->page] = array();
  12277. }
  12278. if (!isset($this->radiobutton_groups[$this->page][$name])) {
  12279. $this->radiobutton_groups[$this->page][$name] = array();
  12280. ++$this->n;
  12281. $this->radiobutton_groups[$this->page][$name]['n'] = $this->n;
  12282. $this->radio_groups[] = $this->n;
  12283. }
  12284. $kid = ($this->n + 1);
  12285. // save object ID to be added on Kids entry on parent object
  12286. $this->radiobutton_groups[$this->page][$name][] = array('kid' => $kid, 'def' => $defval);
  12287. // get default style
  12288. $prop = array_merge($this->getFormDefaultProp(), $prop);
  12289. $prop['NoToggleToOff'] = 'true';
  12290. $prop['Radio'] = 'true';
  12291. $prop['borderStyle'] = 'inset';
  12292. // get annotation data
  12293. $popt = TCPDF_STATIC::getAnnotOptFromJSProp($prop, $this->spot_colors, $this->rtl);
  12294. // set additional default options
  12295. $this->annotation_fonts[$tmpfont['fontkey']] = $tmpfont['i'];
  12296. $fontstyle = sprintf('/F%d %F Tf %s', $tmpfont['i'], $this->FontSizePt, $this->TextColor);
  12297. $popt['da'] = $fontstyle;
  12298. // build appearance stream
  12299. $popt['ap'] = array();
  12300. $popt['ap']['n'] = array();
  12301. $fx = ((($w - $this->getAbsFontMeasure($tmpfont['cw'][108])) / 2) * $this->k);
  12302. $fy = (($w - ((($tmpfont['desc']['Ascent'] - $tmpfont['desc']['Descent']) * $this->FontSizePt / 1000) / $this->k)) * $this->k);
  12303. $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);
  12304. $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);
  12305. if (!isset($popt['mk'])) {
  12306. $popt['mk'] = array();
  12307. }
  12308. $popt['mk']['ca'] = '(l)';
  12309. // merge options
  12310. $opt = array_merge($popt, $opt);
  12311. // set remaining annotation data
  12312. $opt['Subtype'] = 'Widget';
  12313. $opt['ft'] = 'Btn';
  12314. if ($checked) {
  12315. $opt['v'] = array('/'.$onvalue);
  12316. $opt['as'] = $onvalue;
  12317. } else {
  12318. $opt['as'] = 'Off';
  12319. }
  12320. // store readonly flag
  12321. if (!isset($this->radiobutton_groups[$this->page][$name]['#readonly#'])) {
  12322. $this->radiobutton_groups[$this->page][$name]['#readonly#'] = false;
  12323. }
  12324. $this->radiobutton_groups[$this->page][$name]['#readonly#'] |= ($opt['f'] & 64);
  12325. $this->Annotation($x, $y, $w, $w, $name, $opt, 0);
  12326. if ($this->rtl) {
  12327. $this->x -= $w;
  12328. } else {
  12329. $this->x += $w;
  12330. }
  12331. }
  12332. /**
  12333. * Creates a List-box field
  12334. * @param $name (string) field name
  12335. * @param $w (int) width
  12336. * @param $h (int) height
  12337. * @param $values (array) array containing the list of values.
  12338. * @param $prop (array) javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  12339. * @param $opt (array) annotation parameters. Possible values are described on official PDF32000_2008 reference.
  12340. * @param $x (float) Abscissa of the upper-left corner of the rectangle
  12341. * @param $y (float) Ordinate of the upper-left corner of the rectangle
  12342. * @param $js (boolean) if true put the field using JavaScript (requires Acrobat Writer to be rendered).
  12343. * @public
  12344. * @author Nicola Asuni
  12345. * @since 4.8.000 (2009-09-07)
  12346. */
  12347. public function ListBox($name, $w, $h, $values, $prop=array(), $opt=array(), $x='', $y='', $js=false) {
  12348. if ($x === '') {
  12349. $x = $this->x;
  12350. }
  12351. if ($y === '') {
  12352. $y = $this->y;
  12353. }
  12354. // check page for no-write regions and adapt page margins if necessary
  12355. list($x, $y) = $this->checkPageRegions($h, $x, $y);
  12356. if ($js) {
  12357. $this->_addfield('listbox', $name, $x, $y, $w, $h, $prop);
  12358. $s = '';
  12359. foreach ($values as $value) {
  12360. if (is_array($value)) {
  12361. $s .= ',[\''.addslashes($value[1]).'\',\''.addslashes($value[0]).'\']';
  12362. } else {
  12363. $s .= ',[\''.addslashes($value).'\',\''.addslashes($value).'\']';
  12364. }
  12365. }
  12366. $this->javascript .= 'f'.$name.'.setItems('.substr($s, 1).');'."\n";
  12367. return;
  12368. }
  12369. // get default style
  12370. $prop = array_merge($this->getFormDefaultProp(), $prop);
  12371. // get annotation data
  12372. $popt = TCPDF_STATIC::getAnnotOptFromJSProp($prop, $this->spot_colors, $this->rtl);
  12373. // set additional default values
  12374. $this->annotation_fonts[$this->CurrentFont['fontkey']] = $this->CurrentFont['i'];
  12375. $fontstyle = sprintf('/F%d %F Tf %s', $this->CurrentFont['i'], $this->FontSizePt, $this->TextColor);
  12376. $popt['da'] = $fontstyle;
  12377. // build appearance stream
  12378. $popt['ap'] = array();
  12379. $popt['ap']['n'] = '/Tx BMC q '.$fontstyle.' ';
  12380. $text = '';
  12381. foreach($values as $item) {
  12382. if (is_array($item)) {
  12383. $text .= $item[1]."\n";
  12384. } else {
  12385. $text .= $item."\n";
  12386. }
  12387. }
  12388. $tmpid = $this->startTemplate($w, $h, false);
  12389. $this->MultiCell($w, $h, $text, 0, '', false, 0, 0, 0, true, 0, false, true, 0, 'T', false);
  12390. $this->endTemplate();
  12391. --$this->n;
  12392. $popt['ap']['n'] .= $this->xobjects[$tmpid]['outdata'];
  12393. unset($this->xobjects[$tmpid]);
  12394. $popt['ap']['n'] .= 'Q EMC';
  12395. // merge options
  12396. $opt = array_merge($popt, $opt);
  12397. // set remaining annotation data
  12398. $opt['Subtype'] = 'Widget';
  12399. $opt['ft'] = 'Ch';
  12400. $opt['t'] = $name;
  12401. $opt['opt'] = $values;
  12402. unset($opt['mk']['ca']);
  12403. unset($opt['mk']['rc']);
  12404. unset($opt['mk']['ac']);
  12405. unset($opt['mk']['i']);
  12406. unset($opt['mk']['ri']);
  12407. unset($opt['mk']['ix']);
  12408. unset($opt['mk']['if']);
  12409. unset($opt['mk']['tp']);
  12410. $this->Annotation($x, $y, $w, $h, $name, $opt, 0);
  12411. if ($this->rtl) {
  12412. $this->x -= $w;
  12413. } else {
  12414. $this->x += $w;
  12415. }
  12416. }
  12417. /**
  12418. * Creates a Combo-box field
  12419. * @param $name (string) field name
  12420. * @param $w (int) width
  12421. * @param $h (int) height
  12422. * @param $values (array) array containing the list of values.
  12423. * @param $prop (array) javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  12424. * @param $opt (array) annotation parameters. Possible values are described on official PDF32000_2008 reference.
  12425. * @param $x (float) Abscissa of the upper-left corner of the rectangle
  12426. * @param $y (float) Ordinate of the upper-left corner of the rectangle
  12427. * @param $js (boolean) if true put the field using JavaScript (requires Acrobat Writer to be rendered).
  12428. * @public
  12429. * @author Nicola Asuni
  12430. * @since 4.8.000 (2009-09-07)
  12431. */
  12432. public function ComboBox($name, $w, $h, $values, $prop=array(), $opt=array(), $x='', $y='', $js=false) {
  12433. if ($x === '') {
  12434. $x = $this->x;
  12435. }
  12436. if ($y === '') {
  12437. $y = $this->y;
  12438. }
  12439. // check page for no-write regions and adapt page margins if necessary
  12440. list($x, $y) = $this->checkPageRegions($h, $x, $y);
  12441. if ($js) {
  12442. $this->_addfield('combobox', $name, $x, $y, $w, $h, $prop);
  12443. $s = '';
  12444. foreach ($values as $value) {
  12445. if (is_array($value)) {
  12446. $s .= ',[\''.addslashes($value[1]).'\',\''.addslashes($value[0]).'\']';
  12447. } else {
  12448. $s .= ',[\''.addslashes($value).'\',\''.addslashes($value).'\']';
  12449. }
  12450. }
  12451. $this->javascript .= 'f'.$name.'.setItems('.substr($s, 1).');'."\n";
  12452. return;
  12453. }
  12454. // get default style
  12455. $prop = array_merge($this->getFormDefaultProp(), $prop);
  12456. $prop['Combo'] = true;
  12457. // get annotation data
  12458. $popt = TCPDF_STATIC::getAnnotOptFromJSProp($prop, $this->spot_colors, $this->rtl);
  12459. // set additional default options
  12460. $this->annotation_fonts[$this->CurrentFont['fontkey']] = $this->CurrentFont['i'];
  12461. $fontstyle = sprintf('/F%d %F Tf %s', $this->CurrentFont['i'], $this->FontSizePt, $this->TextColor);
  12462. $popt['da'] = $fontstyle;
  12463. // build appearance stream
  12464. $popt['ap'] = array();
  12465. $popt['ap']['n'] = '/Tx BMC q '.$fontstyle.' ';
  12466. $text = '';
  12467. foreach($values as $item) {
  12468. if (is_array($item)) {
  12469. $text .= $item[1]."\n";
  12470. } else {
  12471. $text .= $item."\n";
  12472. }
  12473. }
  12474. $tmpid = $this->startTemplate($w, $h, false);
  12475. $this->MultiCell($w, $h, $text, 0, '', false, 0, 0, 0, true, 0, false, true, 0, 'T', false);
  12476. $this->endTemplate();
  12477. --$this->n;
  12478. $popt['ap']['n'] .= $this->xobjects[$tmpid]['outdata'];
  12479. unset($this->xobjects[$tmpid]);
  12480. $popt['ap']['n'] .= 'Q EMC';
  12481. // merge options
  12482. $opt = array_merge($popt, $opt);
  12483. // set remaining annotation data
  12484. $opt['Subtype'] = 'Widget';
  12485. $opt['ft'] = 'Ch';
  12486. $opt['t'] = $name;
  12487. $opt['opt'] = $values;
  12488. unset($opt['mk']['ca']);
  12489. unset($opt['mk']['rc']);
  12490. unset($opt['mk']['ac']);
  12491. unset($opt['mk']['i']);
  12492. unset($opt['mk']['ri']);
  12493. unset($opt['mk']['ix']);
  12494. unset($opt['mk']['if']);
  12495. unset($opt['mk']['tp']);
  12496. $this->Annotation($x, $y, $w, $h, $name, $opt, 0);
  12497. if ($this->rtl) {
  12498. $this->x -= $w;
  12499. } else {
  12500. $this->x += $w;
  12501. }
  12502. }
  12503. /**
  12504. * Creates a CheckBox field
  12505. * @param $name (string) field name
  12506. * @param $w (int) width
  12507. * @param $checked (boolean) define the initial state.
  12508. * @param $prop (array) javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  12509. * @param $opt (array) annotation parameters. Possible values are described on official PDF32000_2008 reference.
  12510. * @param $onvalue (string) value to be returned if selected.
  12511. * @param $x (float) Abscissa of the upper-left corner of the rectangle
  12512. * @param $y (float) Ordinate of the upper-left corner of the rectangle
  12513. * @param $js (boolean) if true put the field using JavaScript (requires Acrobat Writer to be rendered).
  12514. * @public
  12515. * @author Nicola Asuni
  12516. * @since 4.8.000 (2009-09-07)
  12517. */
  12518. public function CheckBox($name, $w, $checked=false, $prop=array(), $opt=array(), $onvalue='Yes', $x='', $y='', $js=false) {
  12519. if ($x === '') {
  12520. $x = $this->x;
  12521. }
  12522. if ($y === '') {
  12523. $y = $this->y;
  12524. }
  12525. // check page for no-write regions and adapt page margins if necessary
  12526. list($x, $y) = $this->checkPageRegions($w, $x, $y);
  12527. if ($js) {
  12528. $this->_addfield('checkbox', $name, $x, $y, $w, $w, $prop);
  12529. return;
  12530. }
  12531. if (!isset($prop['value'])) {
  12532. $prop['value'] = array('Yes');
  12533. }
  12534. // get default style
  12535. $prop = array_merge($this->getFormDefaultProp(), $prop);
  12536. $prop['borderStyle'] = 'inset';
  12537. // get annotation data
  12538. $popt = TCPDF_STATIC::getAnnotOptFromJSProp($prop, $this->spot_colors, $this->rtl);
  12539. // set additional default options
  12540. $font = 'zapfdingbats';
  12541. if ($this->pdfa_mode) {
  12542. // all fonts must be embedded
  12543. $font = 'pdfa'.$font;
  12544. }
  12545. $this->AddFont($font);
  12546. $tmpfont = $this->getFontBuffer($font);
  12547. $this->annotation_fonts[$tmpfont['fontkey']] = $tmpfont['i'];
  12548. $fontstyle = sprintf('/F%d %F Tf %s', $tmpfont['i'], $this->FontSizePt, $this->TextColor);
  12549. $popt['da'] = $fontstyle;
  12550. // build appearance stream
  12551. $popt['ap'] = array();
  12552. $popt['ap']['n'] = array();
  12553. $fx = ((($w - $this->getAbsFontMeasure($tmpfont['cw'][110])) / 2) * $this->k);
  12554. $fy = (($w - ((($tmpfont['desc']['Ascent'] - $tmpfont['desc']['Descent']) * $this->FontSizePt / 1000) / $this->k)) * $this->k);
  12555. $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);
  12556. $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);
  12557. // merge options
  12558. $opt = array_merge($popt, $opt);
  12559. // set remaining annotation data
  12560. $opt['Subtype'] = 'Widget';
  12561. $opt['ft'] = 'Btn';
  12562. $opt['t'] = $name;
  12563. if (TCPDF_STATIC::empty_string($onvalue)) {
  12564. $onvalue = 'Yes';
  12565. }
  12566. $opt['opt'] = array($onvalue);
  12567. if ($checked) {
  12568. $opt['v'] = array('/Yes');
  12569. $opt['as'] = 'Yes';
  12570. } else {
  12571. $opt['v'] = array('/Off');
  12572. $opt['as'] = 'Off';
  12573. }
  12574. $this->Annotation($x, $y, $w, $w, $name, $opt, 0);
  12575. if ($this->rtl) {
  12576. $this->x -= $w;
  12577. } else {
  12578. $this->x += $w;
  12579. }
  12580. }
  12581. /**
  12582. * Creates a button field
  12583. * @param $name (string) field name
  12584. * @param $w (int) width
  12585. * @param $h (int) height
  12586. * @param $caption (string) caption.
  12587. * @param $action (mixed) 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.
  12588. * @param $prop (array) javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  12589. * @param $opt (array) annotation parameters. Possible values are described on official PDF32000_2008 reference.
  12590. * @param $x (float) Abscissa of the upper-left corner of the rectangle
  12591. * @param $y (float) Ordinate of the upper-left corner of the rectangle
  12592. * @param $js (boolean) if true put the field using JavaScript (requires Acrobat Writer to be rendered).
  12593. * @public
  12594. * @author Nicola Asuni
  12595. * @since 4.8.000 (2009-09-07)
  12596. */
  12597. public function Button($name, $w, $h, $caption, $action, $prop=array(), $opt=array(), $x='', $y='', $js=false) {
  12598. if ($x === '') {
  12599. $x = $this->x;
  12600. }
  12601. if ($y === '') {
  12602. $y = $this->y;
  12603. }
  12604. // check page for no-write regions and adapt page margins if necessary
  12605. list($x, $y) = $this->checkPageRegions($h, $x, $y);
  12606. if ($js) {
  12607. $this->_addfield('button', $name, $this->x, $this->y, $w, $h, $prop);
  12608. $this->javascript .= 'f'.$name.".buttonSetCaption('".addslashes($caption)."');\n";
  12609. $this->javascript .= 'f'.$name.".setAction('MouseUp','".addslashes($action)."');\n";
  12610. $this->javascript .= 'f'.$name.".highlight='push';\n";
  12611. $this->javascript .= 'f'.$name.".print=false;\n";
  12612. return;
  12613. }
  12614. // get default style
  12615. $prop = array_merge($this->getFormDefaultProp(), $prop);
  12616. $prop['Pushbutton'] = 'true';
  12617. $prop['highlight'] = 'push';
  12618. $prop['display'] = 'display.noPrint';
  12619. // get annotation data
  12620. $popt = TCPDF_STATIC::getAnnotOptFromJSProp($prop, $this->spot_colors, $this->rtl);
  12621. $this->annotation_fonts[$this->CurrentFont['fontkey']] = $this->CurrentFont['i'];
  12622. $fontstyle = sprintf('/F%d %F Tf %s', $this->CurrentFont['i'], $this->FontSizePt, $this->TextColor);
  12623. $popt['da'] = $fontstyle;
  12624. // build appearance stream
  12625. $popt['ap'] = array();
  12626. $popt['ap']['n'] = '/Tx BMC q '.$fontstyle.' ';
  12627. $tmpid = $this->startTemplate($w, $h, false);
  12628. $bw = (2 / $this->k); // border width
  12629. $border = array(
  12630. 'L' => array('width' => $bw, 'cap' => 'square', 'join' => 'miter', 'dash' => 0, 'color' => array(231)),
  12631. 'R' => array('width' => $bw, 'cap' => 'square', 'join' => 'miter', 'dash' => 0, 'color' => array(51)),
  12632. 'T' => array('width' => $bw, 'cap' => 'square', 'join' => 'miter', 'dash' => 0, 'color' => array(231)),
  12633. 'B' => array('width' => $bw, 'cap' => 'square', 'join' => 'miter', 'dash' => 0, 'color' => array(51)));
  12634. $this->SetFillColor(204);
  12635. $this->Cell($w, $h, $caption, $border, 0, 'C', true, '', 1, false, 'T', 'M');
  12636. $this->endTemplate();
  12637. --$this->n;
  12638. $popt['ap']['n'] .= $this->xobjects[$tmpid]['outdata'];
  12639. unset($this->xobjects[$tmpid]);
  12640. $popt['ap']['n'] .= 'Q EMC';
  12641. // set additional default options
  12642. if (!isset($popt['mk'])) {
  12643. $popt['mk'] = array();
  12644. }
  12645. $ann_obj_id = ($this->n + 1);
  12646. if (!empty($action) AND !is_array($action)) {
  12647. $ann_obj_id = ($this->n + 2);
  12648. }
  12649. $popt['mk']['ca'] = $this->_textstring($caption, $ann_obj_id);
  12650. $popt['mk']['rc'] = $this->_textstring($caption, $ann_obj_id);
  12651. $popt['mk']['ac'] = $this->_textstring($caption, $ann_obj_id);
  12652. // merge options
  12653. $opt = array_merge($popt, $opt);
  12654. // set remaining annotation data
  12655. $opt['Subtype'] = 'Widget';
  12656. $opt['ft'] = 'Btn';
  12657. $opt['t'] = $caption;
  12658. $opt['v'] = $name;
  12659. if (!empty($action)) {
  12660. if (is_array($action)) {
  12661. // form action options as on section 12.7.5 of PDF32000_2008.
  12662. $opt['aa'] = '/D <<';
  12663. $bmode = array('SubmitForm', 'ResetForm', 'ImportData');
  12664. foreach ($action AS $key => $val) {
  12665. if (($key == 'S') AND in_array($val, $bmode)) {
  12666. $opt['aa'] .= ' /S /'.$val;
  12667. } elseif (($key == 'F') AND (!empty($val))) {
  12668. $opt['aa'] .= ' /F '.$this->_datastring($val, $ann_obj_id);
  12669. } elseif (($key == 'Fields') AND is_array($val) AND !empty($val)) {
  12670. $opt['aa'] .= ' /Fields [';
  12671. foreach ($val AS $field) {
  12672. $opt['aa'] .= ' '.$this->_textstring($field, $ann_obj_id);
  12673. }
  12674. $opt['aa'] .= ']';
  12675. } elseif (($key == 'Flags')) {
  12676. $ff = 0;
  12677. if (is_array($val)) {
  12678. foreach ($val AS $flag) {
  12679. switch ($flag) {
  12680. case 'Include/Exclude': {
  12681. $ff += 1 << 0;
  12682. break;
  12683. }
  12684. case 'IncludeNoValueFields': {
  12685. $ff += 1 << 1;
  12686. break;
  12687. }
  12688. case 'ExportFormat': {
  12689. $ff += 1 << 2;
  12690. break;
  12691. }
  12692. case 'GetMethod': {
  12693. $ff += 1 << 3;
  12694. break;
  12695. }
  12696. case 'SubmitCoordinates': {
  12697. $ff += 1 << 4;
  12698. break;
  12699. }
  12700. case 'XFDF': {
  12701. $ff += 1 << 5;
  12702. break;
  12703. }
  12704. case 'IncludeAppendSaves': {
  12705. $ff += 1 << 6;
  12706. break;
  12707. }
  12708. case 'IncludeAnnotations': {
  12709. $ff += 1 << 7;
  12710. break;
  12711. }
  12712. case 'SubmitPDF': {
  12713. $ff += 1 << 8;
  12714. break;
  12715. }
  12716. case 'CanonicalFormat': {
  12717. $ff += 1 << 9;
  12718. break;
  12719. }
  12720. case 'ExclNonUserAnnots': {
  12721. $ff += 1 << 10;
  12722. break;
  12723. }
  12724. case 'ExclFKey': {
  12725. $ff += 1 << 11;
  12726. break;
  12727. }
  12728. case 'EmbedForm': {
  12729. $ff += 1 << 13;
  12730. break;
  12731. }
  12732. }
  12733. }
  12734. } else {
  12735. $ff = intval($val);
  12736. }
  12737. $opt['aa'] .= ' /Flags '.$ff;
  12738. }
  12739. }
  12740. $opt['aa'] .= ' >>';
  12741. } else {
  12742. // Javascript action or raw action command
  12743. $js_obj_id = $this->addJavascriptObject($action);
  12744. $opt['aa'] = '/D '.$js_obj_id.' 0 R';
  12745. }
  12746. }
  12747. $this->Annotation($x, $y, $w, $h, $name, $opt, 0);
  12748. if ($this->rtl) {
  12749. $this->x -= $w;
  12750. } else {
  12751. $this->x += $w;
  12752. }
  12753. }
  12754. // --- END FORMS FIELDS ------------------------------------------------
  12755. /**
  12756. * Add certification signature (DocMDP or UR3)
  12757. * You can set only one signature type
  12758. * @protected
  12759. * @author Nicola Asuni
  12760. * @since 4.6.008 (2009-05-07)
  12761. */
  12762. protected function _putsignature() {
  12763. if ((!$this->sign) OR (!isset($this->signature_data['cert_type']))) {
  12764. return;
  12765. }
  12766. $sigobjid = ($this->sig_obj_id + 1);
  12767. $out = $this->_getobj($sigobjid)."\n";
  12768. $out .= '<< /Type /Sig';
  12769. $out .= ' /Filter /Adobe.PPKLite';
  12770. $out .= ' /SubFilter /adbe.pkcs7.detached';
  12771. $out .= ' '.TCPDF_STATIC::$byterange_string;
  12772. $out .= ' /Contents<'.str_repeat('0', $this->signature_max_length).'>';
  12773. if (empty($this->signature_data['approval']) OR ($this->signature_data['approval'] != 'A')) {
  12774. $out .= ' /Reference ['; // array of signature reference dictionaries
  12775. $out .= ' << /Type /SigRef';
  12776. if ($this->signature_data['cert_type'] > 0) {
  12777. $out .= ' /TransformMethod /DocMDP';
  12778. $out .= ' /TransformParams <<';
  12779. $out .= ' /Type /TransformParams';
  12780. $out .= ' /P '.$this->signature_data['cert_type'];
  12781. $out .= ' /V /1.2';
  12782. } else {
  12783. $out .= ' /TransformMethod /UR3';
  12784. $out .= ' /TransformParams <<';
  12785. $out .= ' /Type /TransformParams';
  12786. $out .= ' /V /2.2';
  12787. if (!TCPDF_STATIC::empty_string($this->ur['document'])) {
  12788. $out .= ' /Document['.$this->ur['document'].']';
  12789. }
  12790. if (!TCPDF_STATIC::empty_string($this->ur['form'])) {
  12791. $out .= ' /Form['.$this->ur['form'].']';
  12792. }
  12793. if (!TCPDF_STATIC::empty_string($this->ur['signature'])) {
  12794. $out .= ' /Signature['.$this->ur['signature'].']';
  12795. }
  12796. if (!TCPDF_STATIC::empty_string($this->ur['annots'])) {
  12797. $out .= ' /Annots['.$this->ur['annots'].']';
  12798. }
  12799. if (!TCPDF_STATIC::empty_string($this->ur['ef'])) {
  12800. $out .= ' /EF['.$this->ur['ef'].']';
  12801. }
  12802. if (!TCPDF_STATIC::empty_string($this->ur['formex'])) {
  12803. $out .= ' /FormEX['.$this->ur['formex'].']';
  12804. }
  12805. }
  12806. $out .= ' >>'; // close TransformParams
  12807. // optional digest data (values must be calculated and replaced later)
  12808. //$out .= ' /Data ********** 0 R';
  12809. //$out .= ' /DigestMethod/MD5';
  12810. //$out .= ' /DigestLocation[********** 34]';
  12811. //$out .= ' /DigestValue<********************************>';
  12812. $out .= ' >>';
  12813. $out .= ' ]'; // end of reference
  12814. }
  12815. if (isset($this->signature_data['info']['Name']) AND !TCPDF_STATIC::empty_string($this->signature_data['info']['Name'])) {
  12816. $out .= ' /Name '.$this->_textstring($this->signature_data['info']['Name'], $sigobjid);
  12817. }
  12818. if (isset($this->signature_data['info']['Location']) AND !TCPDF_STATIC::empty_string($this->signature_data['info']['Location'])) {
  12819. $out .= ' /Location '.$this->_textstring($this->signature_data['info']['Location'], $sigobjid);
  12820. }
  12821. if (isset($this->signature_data['info']['Reason']) AND !TCPDF_STATIC::empty_string($this->signature_data['info']['Reason'])) {
  12822. $out .= ' /Reason '.$this->_textstring($this->signature_data['info']['Reason'], $sigobjid);
  12823. }
  12824. if (isset($this->signature_data['info']['ContactInfo']) AND !TCPDF_STATIC::empty_string($this->signature_data['info']['ContactInfo'])) {
  12825. $out .= ' /ContactInfo '.$this->_textstring($this->signature_data['info']['ContactInfo'], $sigobjid);
  12826. }
  12827. $out .= ' /M '.$this->_datestring($sigobjid, $this->doc_modification_timestamp);
  12828. $out .= ' >>';
  12829. $out .= "\n".'endobj';
  12830. $this->_out($out);
  12831. }
  12832. /**
  12833. * Set User's Rights for PDF Reader
  12834. * WARNING: This is experimental and currently do not work.
  12835. * Check the PDF Reference 8.7.1 Transform Methods,
  12836. * Table 8.105 Entries in the UR transform parameters dictionary
  12837. * @param $enable (boolean) if true enable user's rights on PDF reader
  12838. * @param $document (string) 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.
  12839. * @param $annots (string) 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.
  12840. * @param $form (string) Names specifying additional form-field-related usage rights for the document. Valid names are: /Add/Delete/FillIn/Import/Export/SubmitStandalone/SpawnTemplate
  12841. * @param $signature (string) 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.
  12842. * @param $ef (string) 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
  12843. Names specifying additional embedded-files-related usage rights for the document.
  12844. * @param $formex (string) 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.
  12845. * @public
  12846. * @author Nicola Asuni
  12847. * @since 2.9.000 (2008-03-26)
  12848. */
  12849. public function setUserRights(
  12850. $enable=true,
  12851. $document='/FullSave',
  12852. $annots='/Create/Delete/Modify/Copy/Import/Export',
  12853. $form='/Add/Delete/FillIn/Import/Export/SubmitStandalone/SpawnTemplate',
  12854. $signature='/Modify',
  12855. $ef='/Create/Delete/Modify/Import',
  12856. $formex='') {
  12857. $this->ur['enabled'] = $enable;
  12858. $this->ur['document'] = $document;
  12859. $this->ur['annots'] = $annots;
  12860. $this->ur['form'] = $form;
  12861. $this->ur['signature'] = $signature;
  12862. $this->ur['ef'] = $ef;
  12863. $this->ur['formex'] = $formex;
  12864. if (!$this->sign) {
  12865. $this->setSignature('', '', '', '', 0, array());
  12866. }
  12867. }
  12868. /**
  12869. * Enable document signature (requires the OpenSSL Library).
  12870. * The digital signature improve document authenticity and integrity and allows o enable extra features on Acrobat Reader.
  12871. * To create self-signed signature: openssl req -x509 -nodes -days 365000 -newkey rsa:1024 -keyout tcpdf.crt -out tcpdf.crt
  12872. * To export crt to p12: openssl pkcs12 -export -in tcpdf.crt -out tcpdf.p12
  12873. * To convert pfx certificate to pem: openssl pkcs12 -in tcpdf.pfx -out tcpdf.crt -nodes
  12874. * @param $signing_cert (mixed) signing certificate (string or filename prefixed with 'file://')
  12875. * @param $private_key (mixed) private key (string or filename prefixed with 'file://')
  12876. * @param $private_key_password (string) password
  12877. * @param $extracerts (string) 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.
  12878. * @param $cert_type (int) 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.
  12879. * @param $info (array) array of option information: Name, Location, Reason, ContactInfo.
  12880. * @param $approval (string) Enable approval signature eg. for PDF incremental update
  12881. * @public
  12882. * @author Nicola Asuni
  12883. * @since 4.6.005 (2009-04-24)
  12884. */
  12885. public function setSignature($signing_cert='', $private_key='', $private_key_password='', $extracerts='', $cert_type=2, $info=array(), $approval='') {
  12886. // to create self-signed signature: openssl req -x509 -nodes -days 365000 -newkey rsa:1024 -keyout tcpdf.crt -out tcpdf.crt
  12887. // to export crt to p12: openssl pkcs12 -export -in tcpdf.crt -out tcpdf.p12
  12888. // to convert pfx certificate to pem: openssl
  12889. // OpenSSL> pkcs12 -in <cert.pfx> -out <cert.crt> -nodes
  12890. $this->sign = true;
  12891. ++$this->n;
  12892. $this->sig_obj_id = $this->n; // signature widget
  12893. ++$this->n; // signature object ($this->sig_obj_id + 1)
  12894. $this->signature_data = array();
  12895. if (strlen($signing_cert) == 0) {
  12896. $this->Error('Please provide a certificate file and password!');
  12897. }
  12898. if (strlen($private_key) == 0) {
  12899. $private_key = $signing_cert;
  12900. }
  12901. $this->signature_data['signcert'] = $signing_cert;
  12902. $this->signature_data['privkey'] = $private_key;
  12903. $this->signature_data['password'] = $private_key_password;
  12904. $this->signature_data['extracerts'] = $extracerts;
  12905. $this->signature_data['cert_type'] = $cert_type;
  12906. $this->signature_data['info'] = $info;
  12907. $this->signature_data['approval'] = $approval;
  12908. }
  12909. /**
  12910. * Set the digital signature appearance (a cliccable rectangle area to get signature properties)
  12911. * @param $x (float) Abscissa of the upper-left corner.
  12912. * @param $y (float) Ordinate of the upper-left corner.
  12913. * @param $w (float) Width of the signature area.
  12914. * @param $h (float) Height of the signature area.
  12915. * @param $page (int) option page number (if < 0 the current page is used).
  12916. * @param $name (string) Name of the signature.
  12917. * @public
  12918. * @author Nicola Asuni
  12919. * @since 5.3.011 (2010-06-17)
  12920. */
  12921. public function setSignatureAppearance($x=0, $y=0, $w=0, $h=0, $page=-1, $name='') {
  12922. $this->signature_appearance = $this->getSignatureAppearanceArray($x, $y, $w, $h, $page, $name);
  12923. }
  12924. /**
  12925. * Add an empty digital signature appearance (a cliccable rectangle area to get signature properties)
  12926. * @param $x (float) Abscissa of the upper-left corner.
  12927. * @param $y (float) Ordinate of the upper-left corner.
  12928. * @param $w (float) Width of the signature area.
  12929. * @param $h (float) Height of the signature area.
  12930. * @param $page (int) option page number (if < 0 the current page is used).
  12931. * @param $name (string) Name of the signature.
  12932. * @public
  12933. * @author Nicola Asuni
  12934. * @since 5.9.101 (2011-07-06)
  12935. */
  12936. public function addEmptySignatureAppearance($x=0, $y=0, $w=0, $h=0, $page=-1, $name='') {
  12937. ++$this->n;
  12938. $this->empty_signature_appearance[] = array('objid' => $this->n) + $this->getSignatureAppearanceArray($x, $y, $w, $h, $page, $name);
  12939. }
  12940. /**
  12941. * Get the array that defines the signature appearance (page and rectangle coordinates).
  12942. * @param $x (float) Abscissa of the upper-left corner.
  12943. * @param $y (float) Ordinate of the upper-left corner.
  12944. * @param $w (float) Width of the signature area.
  12945. * @param $h (float) Height of the signature area.
  12946. * @param $page (int) option page number (if < 0 the current page is used).
  12947. * @param $name (string) Name of the signature.
  12948. * @return (array) Array defining page and rectangle coordinates of signature appearance.
  12949. * @protected
  12950. * @author Nicola Asuni
  12951. * @since 5.9.101 (2011-07-06)
  12952. */
  12953. protected function getSignatureAppearanceArray($x=0, $y=0, $w=0, $h=0, $page=-1, $name='') {
  12954. $sigapp = array();
  12955. if (($page < 1) OR ($page > $this->numpages)) {
  12956. $sigapp['page'] = $this->page;
  12957. } else {
  12958. $sigapp['page'] = intval($page);
  12959. }
  12960. if (empty($name)) {
  12961. $sigapp['name'] = 'Signature';
  12962. } else {
  12963. $sigapp['name'] = $name;
  12964. }
  12965. $a = $x * $this->k;
  12966. $b = $this->pagedim[($sigapp['page'])]['h'] - (($y + $h) * $this->k);
  12967. $c = $w * $this->k;
  12968. $d = $h * $this->k;
  12969. $sigapp['rect'] = sprintf('%F %F %F %F', $a, $b, ($a + $c), ($b + $d));
  12970. return $sigapp;
  12971. }
  12972. /**
  12973. * Enable document timestamping (requires the OpenSSL Library).
  12974. * The trusted timestamping improve document security that means that no one should be able to change the document once it has been recorded.
  12975. * Use with digital signature only!
  12976. * @param $tsa_host (string) Time Stamping Authority (TSA) server (prefixed with 'https://')
  12977. * @param $tsa_username (string) Specifies the username for TSA authorization (optional) OR specifies the TSA authorization PEM file (see: example_66.php, optional)
  12978. * @param $tsa_password (string) Specifies the password for TSA authorization (optional)
  12979. * @param $tsa_cert (string) Specifies the location of TSA certificate for authorization (optional for cURL)
  12980. * @public
  12981. * @author Richard Stockinger
  12982. * @since 6.0.090 (2014-06-16)
  12983. */
  12984. public function setTimeStamp($tsa_host='', $tsa_username='', $tsa_password='', $tsa_cert='') {
  12985. $this->tsa_data = array();
  12986. if (!function_exists('curl_init')) {
  12987. $this->Error('Please enable cURL PHP extension!');
  12988. }
  12989. if (strlen($tsa_host) == 0) {
  12990. $this->Error('Please specify the host of Time Stamping Authority (TSA)!');
  12991. }
  12992. $this->tsa_data['tsa_host'] = $tsa_host;
  12993. if (is_file($tsa_username)) {
  12994. $this->tsa_data['tsa_auth'] = $tsa_username;
  12995. } else {
  12996. $this->tsa_data['tsa_username'] = $tsa_username;
  12997. }
  12998. $this->tsa_data['tsa_password'] = $tsa_password;
  12999. $this->tsa_data['tsa_cert'] = $tsa_cert;
  13000. $this->tsa_timestamp = true;
  13001. }
  13002. /**
  13003. * NOT YET IMPLEMENTED
  13004. * Request TSA for a timestamp
  13005. * @param $signature (string) Digital signature as binary string
  13006. * @return (string) Timestamped digital signature
  13007. * @protected
  13008. * @author Richard Stockinger
  13009. * @since 6.0.090 (2014-06-16)
  13010. */
  13011. protected function applyTSA($signature) {
  13012. if (!$this->tsa_timestamp) {
  13013. return $signature;
  13014. }
  13015. //@TODO: implement this feature
  13016. return $signature;
  13017. }
  13018. /**
  13019. * Create a new page group.
  13020. * NOTE: call this function before calling AddPage()
  13021. * @param $page (int) starting group page (leave empty for next page).
  13022. * @public
  13023. * @since 3.0.000 (2008-03-27)
  13024. */
  13025. public function startPageGroup($page='') {
  13026. if (empty($page)) {
  13027. $page = $this->page + 1;
  13028. }
  13029. $this->newpagegroup[$page] = sizeof($this->newpagegroup) + 1;
  13030. }
  13031. /**
  13032. * Set the starting page number.
  13033. * @param $num (int) Starting page number.
  13034. * @since 5.9.093 (2011-06-16)
  13035. * @public
  13036. */
  13037. public function setStartingPageNumber($num=1) {
  13038. $this->starting_page_number = max(0, intval($num));
  13039. }
  13040. /**
  13041. * Returns the string alias used right align page numbers.
  13042. * If the current font is unicode type, the returned string wil contain an additional open curly brace.
  13043. * @return string
  13044. * @since 5.9.099 (2011-06-27)
  13045. * @public
  13046. */
  13047. public function getAliasRightShift() {
  13048. // calculate aproximatively the ratio between widths of aliases and replacements.
  13049. $ref = '{'.TCPDF_STATIC::$alias_right_shift.'}{'.TCPDF_STATIC::$alias_tot_pages.'}{'.TCPDF_STATIC::$alias_num_page.'}';
  13050. $rep = str_repeat(' ', $this->GetNumChars($ref));
  13051. $wrep = $this->GetStringWidth($rep);
  13052. if ($wrep > 0) {
  13053. $wdiff = max(1, ($this->GetStringWidth($ref) / $wrep));
  13054. } else {
  13055. $wdiff = 1;
  13056. }
  13057. $sdiff = sprintf('%F', $wdiff);
  13058. $alias = TCPDF_STATIC::$alias_right_shift.$sdiff.'}';
  13059. if ($this->isUnicodeFont()) {
  13060. $alias = '{'.$alias;
  13061. }
  13062. return $alias;
  13063. }
  13064. /**
  13065. * Returns the string alias used for the total number of pages.
  13066. * If the current font is unicode type, the returned string is surrounded by additional curly braces.
  13067. * This alias will be replaced by the total number of pages in the document.
  13068. * @return string
  13069. * @since 4.0.018 (2008-08-08)
  13070. * @public
  13071. */
  13072. public function getAliasNbPages() {
  13073. if ($this->isUnicodeFont()) {
  13074. return '{'.TCPDF_STATIC::$alias_tot_pages.'}';
  13075. }
  13076. return TCPDF_STATIC::$alias_tot_pages;
  13077. }
  13078. /**
  13079. * Returns the string alias used for the page number.
  13080. * If the current font is unicode type, the returned string is surrounded by additional curly braces.
  13081. * This alias will be replaced by the page number.
  13082. * @return string
  13083. * @since 4.5.000 (2009-01-02)
  13084. * @public
  13085. */
  13086. public function getAliasNumPage() {
  13087. if ($this->isUnicodeFont()) {
  13088. return '{'.TCPDF_STATIC::$alias_num_page.'}';
  13089. }
  13090. return TCPDF_STATIC::$alias_num_page;
  13091. }
  13092. /**
  13093. * Return the alias for the total number of pages in the current page group.
  13094. * If the current font is unicode type, the returned string is surrounded by additional curly braces.
  13095. * This alias will be replaced by the total number of pages in this group.
  13096. * @return alias of the current page group
  13097. * @public
  13098. * @since 3.0.000 (2008-03-27)
  13099. */
  13100. public function getPageGroupAlias() {
  13101. if ($this->isUnicodeFont()) {
  13102. return '{'.TCPDF_STATIC::$alias_group_tot_pages.'}';
  13103. }
  13104. return TCPDF_STATIC::$alias_group_tot_pages;
  13105. }
  13106. /**
  13107. * Return the alias for the page number on the current page group.
  13108. * If the current font is unicode type, the returned string is surrounded by additional curly braces.
  13109. * This alias will be replaced by the page number (relative to the belonging group).
  13110. * @return alias of the current page group
  13111. * @public
  13112. * @since 4.5.000 (2009-01-02)
  13113. */
  13114. public function getPageNumGroupAlias() {
  13115. if ($this->isUnicodeFont()) {
  13116. return '{'.TCPDF_STATIC::$alias_group_num_page.'}';
  13117. }
  13118. return TCPDF_STATIC::$alias_group_num_page;
  13119. }
  13120. /**
  13121. * Return the current page in the group.
  13122. * @return current page in the group
  13123. * @public
  13124. * @since 3.0.000 (2008-03-27)
  13125. */
  13126. public function getGroupPageNo() {
  13127. return $this->pagegroups[$this->currpagegroup];
  13128. }
  13129. /**
  13130. * Returns the current group page number formatted as a string.
  13131. * @public
  13132. * @since 4.3.003 (2008-11-18)
  13133. * @see PaneNo(), formatPageNumber()
  13134. */
  13135. public function getGroupPageNoFormatted() {
  13136. return TCPDF_STATIC::formatPageNumber($this->getGroupPageNo());
  13137. }
  13138. /**
  13139. * Returns the current page number formatted as a string.
  13140. * @public
  13141. * @since 4.2.005 (2008-11-06)
  13142. * @see PaneNo(), formatPageNumber()
  13143. */
  13144. public function PageNoFormatted() {
  13145. return TCPDF_STATIC::formatPageNumber($this->PageNo());
  13146. }
  13147. /**
  13148. * Put pdf layers.
  13149. * @protected
  13150. * @since 3.0.000 (2008-03-27)
  13151. */
  13152. protected function _putocg() {
  13153. if (empty($this->pdflayers)) {
  13154. return;
  13155. }
  13156. foreach ($this->pdflayers as $key => $layer) {
  13157. $this->pdflayers[$key]['objid'] = $this->_newobj();
  13158. $out = '<< /Type /OCG';
  13159. $out .= ' /Name '.$this->_textstring($layer['name'], $this->pdflayers[$key]['objid']);
  13160. $out .= ' /Usage <<';
  13161. if (isset($layer['print']) AND ($layer['print'] !== NULL)) {
  13162. $out .= ' /Print <</PrintState /'.($layer['print']?'ON':'OFF').'>>';
  13163. }
  13164. $out .= ' /View <</ViewState /'.($layer['view']?'ON':'OFF').'>>';
  13165. $out .= ' >> >>';
  13166. $out .= "\n".'endobj';
  13167. $this->_out($out);
  13168. }
  13169. }
  13170. /**
  13171. * Start a new pdf layer.
  13172. * @param $name (string) Layer name (only a-z letters and numbers). Leave empty for automatic name.
  13173. * @param $print (boolean|null) Set to TRUE to print this layer, FALSE to not print and NULL to not set this option
  13174. * @param $view (boolean) Set to true to view this layer.
  13175. * @param $lock (boolean) If true lock the layer
  13176. * @public
  13177. * @since 5.9.102 (2011-07-13)
  13178. */
  13179. public function startLayer($name='', $print=true, $view=true, $lock=true) {
  13180. if ($this->state != 2) {
  13181. return;
  13182. }
  13183. $layer = sprintf('LYR%03d', (count($this->pdflayers) + 1));
  13184. if (empty($name)) {
  13185. $name = $layer;
  13186. } else {
  13187. $name = preg_replace('/[^a-zA-Z0-9_\-]/', '', $name);
  13188. }
  13189. $this->pdflayers[] = array('layer' => $layer, 'name' => $name, 'print' => $print, 'view' => $view, 'lock' => $lock);
  13190. $this->openMarkedContent = true;
  13191. $this->_out('/OC /'.$layer.' BDC');
  13192. }
  13193. /**
  13194. * End the current PDF layer.
  13195. * @public
  13196. * @since 5.9.102 (2011-07-13)
  13197. */
  13198. public function endLayer() {
  13199. if ($this->state != 2) {
  13200. return;
  13201. }
  13202. if ($this->openMarkedContent) {
  13203. // close existing open marked-content layer
  13204. $this->_out('EMC');
  13205. $this->openMarkedContent = false;
  13206. }
  13207. }
  13208. /**
  13209. * Set the visibility of the successive elements.
  13210. * This can be useful, for instance, to put a background
  13211. * image or color that will show on screen but won't print.
  13212. * @param $v (string) visibility mode. Legal values are: all, print, screen or view.
  13213. * @public
  13214. * @since 3.0.000 (2008-03-27)
  13215. */
  13216. public function setVisibility($v) {
  13217. if ($this->state != 2) {
  13218. return;
  13219. }
  13220. $this->endLayer();
  13221. switch($v) {
  13222. case 'print': {
  13223. $this->startLayer('Print', true, false);
  13224. break;
  13225. }
  13226. case 'view':
  13227. case 'screen': {
  13228. $this->startLayer('View', false, true);
  13229. break;
  13230. }
  13231. case 'all': {
  13232. $this->_out('');
  13233. break;
  13234. }
  13235. default: {
  13236. $this->Error('Incorrect visibility: '.$v);
  13237. break;
  13238. }
  13239. }
  13240. }
  13241. /**
  13242. * Add transparency parameters to the current extgstate
  13243. * @param $parms (array) parameters
  13244. * @return the number of extgstates
  13245. * @protected
  13246. * @since 3.0.000 (2008-03-27)
  13247. */
  13248. protected function addExtGState($parms) {
  13249. if ($this->pdfa_mode) {
  13250. // transparencies are not allowed in PDF/A mode
  13251. return;
  13252. }
  13253. // check if this ExtGState already exist
  13254. foreach ($this->extgstates as $i => $ext) {
  13255. if ($ext['parms'] == $parms) {
  13256. if ($this->inxobj) {
  13257. // we are inside an XObject template
  13258. $this->xobjects[$this->xobjid]['extgstates'][$i] = $ext;
  13259. }
  13260. // return reference to existing ExtGState
  13261. return $i;
  13262. }
  13263. }
  13264. $n = (count($this->extgstates) + 1);
  13265. $this->extgstates[$n] = array('parms' => $parms);
  13266. if ($this->inxobj) {
  13267. // we are inside an XObject template
  13268. $this->xobjects[$this->xobjid]['extgstates'][$n] = $this->extgstates[$n];
  13269. }
  13270. return $n;
  13271. }
  13272. /**
  13273. * Add an extgstate
  13274. * @param $gs (array) extgstate
  13275. * @protected
  13276. * @since 3.0.000 (2008-03-27)
  13277. */
  13278. protected function setExtGState($gs) {
  13279. if ($this->pdfa_mode OR ($this->state != 2)) {
  13280. // transparency is not allowed in PDF/A mode
  13281. return;
  13282. }
  13283. $this->_out(sprintf('/GS%d gs', $gs));
  13284. }
  13285. /**
  13286. * Put extgstates for object transparency
  13287. * @protected
  13288. * @since 3.0.000 (2008-03-27)
  13289. */
  13290. protected function _putextgstates() {
  13291. foreach ($this->extgstates as $i => $ext) {
  13292. $this->extgstates[$i]['n'] = $this->_newobj();
  13293. $out = '<< /Type /ExtGState';
  13294. foreach ($ext['parms'] as $k => $v) {
  13295. if (is_float($v)) {
  13296. $v = sprintf('%F', $v);
  13297. } elseif ($v === true) {
  13298. $v = 'true';
  13299. } elseif ($v === false) {
  13300. $v = 'false';
  13301. }
  13302. $out .= ' /'.$k.' '.$v;
  13303. }
  13304. $out .= ' >>';
  13305. $out .= "\n".'endobj';
  13306. $this->_out($out);
  13307. }
  13308. }
  13309. /**
  13310. * Set overprint mode for stroking (OP) and non-stroking (op) painting operations.
  13311. * (Check the "Entries in a Graphics State Parameter Dictionary" on PDF 32000-1:2008).
  13312. * @param $stroking (boolean) If true apply overprint for stroking operations.
  13313. * @param $nonstroking (boolean) If true apply overprint for painting operations other than stroking.
  13314. * @param $mode (integer) 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).
  13315. * @public
  13316. * @since 5.9.152 (2012-03-23)
  13317. */
  13318. public function setOverprint($stroking=true, $nonstroking='', $mode=0) {
  13319. if ($this->state != 2) {
  13320. return;
  13321. }
  13322. $stroking = $stroking ? true : false;
  13323. if (TCPDF_STATIC::empty_string($nonstroking)) {
  13324. // default value if not set
  13325. $nonstroking = $stroking;
  13326. } else {
  13327. $nonstroking = $nonstroking ? true : false;
  13328. }
  13329. if (($mode != 0) AND ($mode != 1)) {
  13330. $mode = 0;
  13331. }
  13332. $this->overprint = array('OP' => $stroking, 'op' => $nonstroking, 'OPM' => $mode);
  13333. $gs = $this->addExtGState($this->overprint);
  13334. $this->setExtGState($gs);
  13335. }
  13336. /**
  13337. * Get the overprint mode array (OP, op, OPM).
  13338. * (Check the "Entries in a Graphics State Parameter Dictionary" on PDF 32000-1:2008).
  13339. * @return array.
  13340. * @public
  13341. * @since 5.9.152 (2012-03-23)
  13342. */
  13343. public function getOverprint() {
  13344. return $this->overprint;
  13345. }
  13346. /**
  13347. * Set alpha for stroking (CA) and non-stroking (ca) operations.
  13348. * @param $stroking (float) Alpha value for stroking operations: real value from 0 (transparent) to 1 (opaque).
  13349. * @param $bm (string) blend mode, one of the following: Normal, Multiply, Screen, Overlay, Darken, Lighten, ColorDodge, ColorBurn, HardLight, SoftLight, Difference, Exclusion, Hue, Saturation, Color, Luminosity
  13350. * @param $nonstroking (float) Alpha value for non-stroking operations: real value from 0 (transparent) to 1 (opaque).
  13351. * @param $ais (boolean)
  13352. * @public
  13353. * @since 3.0.000 (2008-03-27)
  13354. */
  13355. public function setAlpha($stroking=1, $bm='Normal', $nonstroking='', $ais=false) {
  13356. if ($this->pdfa_mode) {
  13357. // transparency is not allowed in PDF/A mode
  13358. return;
  13359. }
  13360. $stroking = floatval($stroking);
  13361. if (TCPDF_STATIC::empty_string($nonstroking)) {
  13362. // default value if not set
  13363. $nonstroking = $stroking;
  13364. } else {
  13365. $nonstroking = floatval($nonstroking);
  13366. }
  13367. if ($bm[0] == '/') {
  13368. // remove trailing slash
  13369. $bm = substr($bm, 1);
  13370. }
  13371. if (!in_array($bm, array('Normal', 'Multiply', 'Screen', 'Overlay', 'Darken', 'Lighten', 'ColorDodge', 'ColorBurn', 'HardLight', 'SoftLight', 'Difference', 'Exclusion', 'Hue', 'Saturation', 'Color', 'Luminosity'))) {
  13372. $bm = 'Normal';
  13373. }
  13374. $ais = $ais ? true : false;
  13375. $this->alpha = array('CA' => $stroking, 'ca' => $nonstroking, 'BM' => '/'.$bm, 'AIS' => $ais);
  13376. $gs = $this->addExtGState($this->alpha);
  13377. $this->setExtGState($gs);
  13378. }
  13379. /**
  13380. * Get the alpha mode array (CA, ca, BM, AIS).
  13381. * (Check the "Entries in a Graphics State Parameter Dictionary" on PDF 32000-1:2008).
  13382. * @return array.
  13383. * @public
  13384. * @since 5.9.152 (2012-03-23)
  13385. */
  13386. public function getAlpha() {
  13387. return $this->alpha;
  13388. }
  13389. /**
  13390. * Set the default JPEG compression quality (1-100)
  13391. * @param $quality (int) JPEG quality, integer between 1 and 100
  13392. * @public
  13393. * @since 3.0.000 (2008-03-27)
  13394. */
  13395. public function setJPEGQuality($quality) {
  13396. if (($quality < 1) OR ($quality > 100)) {
  13397. $quality = 75;
  13398. }
  13399. $this->jpeg_quality = intval($quality);
  13400. }
  13401. /**
  13402. * Set the default number of columns in a row for HTML tables.
  13403. * @param $cols (int) number of columns
  13404. * @public
  13405. * @since 3.0.014 (2008-06-04)
  13406. */
  13407. public function setDefaultTableColumns($cols=4) {
  13408. $this->default_table_columns = intval($cols);
  13409. }
  13410. /**
  13411. * Set the height of the cell (line height) respect the font height.
  13412. * @param $h (int) cell proportion respect font height (typical value = 1.25).
  13413. * @public
  13414. * @since 3.0.014 (2008-06-04)
  13415. */
  13416. public function setCellHeightRatio($h) {
  13417. $this->cell_height_ratio = $h;
  13418. }
  13419. /**
  13420. * return the height of cell repect font height.
  13421. * @public
  13422. * @since 4.0.012 (2008-07-24)
  13423. */
  13424. public function getCellHeightRatio() {
  13425. return $this->cell_height_ratio;
  13426. }
  13427. /**
  13428. * Set the PDF version (check PDF reference for valid values).
  13429. * @param $version (string) PDF document version.
  13430. * @public
  13431. * @since 3.1.000 (2008-06-09)
  13432. */
  13433. public function setPDFVersion($version='1.7') {
  13434. if ($this->pdfa_mode) {
  13435. // PDF/A mode
  13436. $this->PDFVersion = '1.4';
  13437. } else {
  13438. $this->PDFVersion = $version;
  13439. }
  13440. }
  13441. /**
  13442. * Set the viewer preferences dictionary controlling the way the document is to be presented on the screen or in print.
  13443. * (see Section 8.1 of PDF reference, "Viewer Preferences").
  13444. * <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>
  13445. * @param $preferences (array) array of options.
  13446. * @author Nicola Asuni
  13447. * @public
  13448. * @since 3.1.000 (2008-06-09)
  13449. */
  13450. public function setViewerPreferences($preferences) {
  13451. $this->viewer_preferences = $preferences;
  13452. }
  13453. /**
  13454. * Paints color transition registration bars
  13455. * @param $x (float) abscissa of the top left corner of the rectangle.
  13456. * @param $y (float) ordinate of the top left corner of the rectangle.
  13457. * @param $w (float) width of the rectangle.
  13458. * @param $h (float) height of the rectangle.
  13459. * @param $transition (boolean) if true prints tcolor transitions to white.
  13460. * @param $vertical (boolean) if true prints bar vertically.
  13461. * @param $colors (string) 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.
  13462. * @author Nicola Asuni
  13463. * @since 4.9.000 (2010-03-26)
  13464. * @public
  13465. */
  13466. public function colorRegistrationBar($x, $y, $w, $h, $transition=true, $vertical=false, $colors='A,R,G,B,C,M,Y,K') {
  13467. if (strpos($colors, 'ALLSPOT') !== false) {
  13468. // expand spot colors
  13469. $spot_colors = '';
  13470. foreach ($this->spot_colors as $spot_color_name => $v) {
  13471. $spot_colors .= ','.$spot_color_name;
  13472. }
  13473. if (!empty($spot_colors)) {
  13474. $spot_colors = substr($spot_colors, 1);
  13475. $colors = str_replace('ALLSPOT', $spot_colors, $colors);
  13476. } else {
  13477. $colors = str_replace('ALLSPOT', 'NONE', $colors);
  13478. }
  13479. }
  13480. $bars = explode(',', $colors);
  13481. $numbars = count($bars); // number of bars to print
  13482. if ($numbars <= 0) {
  13483. return;
  13484. }
  13485. // set bar measures
  13486. if ($vertical) {
  13487. $coords = array(0, 0, 0, 1);
  13488. $wb = $w / $numbars; // bar width
  13489. $hb = $h; // bar height
  13490. $xd = $wb; // delta x
  13491. $yd = 0; // delta y
  13492. } else {
  13493. $coords = array(1, 0, 0, 0);
  13494. $wb = $w; // bar width
  13495. $hb = $h / $numbars; // bar height
  13496. $xd = 0; // delta x
  13497. $yd = $hb; // delta y
  13498. }
  13499. $xb = $x;
  13500. $yb = $y;
  13501. foreach ($bars as $col) {
  13502. switch ($col) {
  13503. // set transition colors
  13504. case 'A': { // BLACK (GRAYSCALE)
  13505. $col_a = array(255);
  13506. $col_b = array(0);
  13507. break;
  13508. }
  13509. case 'W': { // WHITE (GRAYSCALE)
  13510. $col_a = array(0);
  13511. $col_b = array(255);
  13512. break;
  13513. }
  13514. case 'R': { // RED (RGB)
  13515. $col_a = array(255,255,255);
  13516. $col_b = array(255,0,0);
  13517. break;
  13518. }
  13519. case 'G': { // GREEN (RGB)
  13520. $col_a = array(255,255,255);
  13521. $col_b = array(0,255,0);
  13522. break;
  13523. }
  13524. case 'B': { // BLUE (RGB)
  13525. $col_a = array(255,255,255);
  13526. $col_b = array(0,0,255);
  13527. break;
  13528. }
  13529. case 'C': { // CYAN (CMYK)
  13530. $col_a = array(0,0,0,0);
  13531. $col_b = array(100,0,0,0);
  13532. break;
  13533. }
  13534. case 'M': { // MAGENTA (CMYK)
  13535. $col_a = array(0,0,0,0);
  13536. $col_b = array(0,100,0,0);
  13537. break;
  13538. }
  13539. case 'Y': { // YELLOW (CMYK)
  13540. $col_a = array(0,0,0,0);
  13541. $col_b = array(0,0,100,0);
  13542. break;
  13543. }
  13544. case 'K': { // KEY - BLACK (CMYK)
  13545. $col_a = array(0,0,0,0);
  13546. $col_b = array(0,0,0,100);
  13547. break;
  13548. }
  13549. case 'RGB': { // BLACK REGISTRATION (RGB)
  13550. $col_a = array(255,255,255);
  13551. $col_b = array(0,0,0);
  13552. break;
  13553. }
  13554. case 'CMYK': { // BLACK REGISTRATION (CMYK)
  13555. $col_a = array(0,0,0,0);
  13556. $col_b = array(100,100,100,100);
  13557. break;
  13558. }
  13559. case 'ALL': { // SPOT COLOR REGISTRATION
  13560. $col_a = array(0,0,0,0,'None');
  13561. $col_b = array(100,100,100,100,'All');
  13562. break;
  13563. }
  13564. case 'NONE': { // SKIP THIS COLOR
  13565. $col_a = array(0,0,0,0,'None');
  13566. $col_b = array(0,0,0,0,'None');
  13567. break;
  13568. }
  13569. default: { // SPECIFIC SPOT COLOR NAME
  13570. $col_a = array(0,0,0,0,'None');
  13571. $col_b = TCPDF_COLORS::getSpotColor($col, $this->spot_colors);
  13572. if ($col_b === false) {
  13573. // in case of error defaults to the registration color
  13574. $col_b = array(100,100,100,100,'All');
  13575. }
  13576. break;
  13577. }
  13578. }
  13579. if ($col != 'NONE') {
  13580. if ($transition) {
  13581. // color gradient
  13582. $this->LinearGradient($xb, $yb, $wb, $hb, $col_a, $col_b, $coords);
  13583. } else {
  13584. $this->SetFillColorArray($col_b);
  13585. // colored rectangle
  13586. $this->Rect($xb, $yb, $wb, $hb, 'F', array());
  13587. }
  13588. $xb += $xd;
  13589. $yb += $yd;
  13590. }
  13591. }
  13592. }
  13593. /**
  13594. * Paints crop marks.
  13595. * @param $x (float) abscissa of the crop mark center.
  13596. * @param $y (float) ordinate of the crop mark center.
  13597. * @param $w (float) width of the crop mark.
  13598. * @param $h (float) height of the crop mark.
  13599. * @param $type (string) 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.
  13600. * @param $color (array) crop mark color (default spot registration color).
  13601. * @author Nicola Asuni
  13602. * @since 4.9.000 (2010-03-26)
  13603. * @public
  13604. */
  13605. public function cropMark($x, $y, $w, $h, $type='T,R,B,L', $color=array(100,100,100,100,'All')) {
  13606. $this->SetLineStyle(array('width' => (0.5 / $this->k), 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => $color));
  13607. $type = strtoupper($type);
  13608. $type = preg_replace('/[^A-Z\-\,]*/', '', $type);
  13609. // split type in single components
  13610. $type = str_replace('-', ',', $type);
  13611. $type = str_replace('TL', 'T,L', $type);
  13612. $type = str_replace('TR', 'T,R', $type);
  13613. $type = str_replace('BL', 'F,L', $type);
  13614. $type = str_replace('BR', 'F,R', $type);
  13615. $type = str_replace('A', 'T,L', $type);
  13616. $type = str_replace('B', 'T,R', $type);
  13617. $type = str_replace('T,RO', 'BO', $type);
  13618. $type = str_replace('C', 'F,L', $type);
  13619. $type = str_replace('D', 'F,R', $type);
  13620. $crops = explode(',', strtoupper($type));
  13621. // remove duplicates
  13622. $crops = array_unique($crops);
  13623. $dw = ($w / 4); // horizontal space to leave before the intersection point
  13624. $dh = ($h / 4); // vertical space to leave before the intersection point
  13625. foreach ($crops as $crop) {
  13626. switch ($crop) {
  13627. case 'T':
  13628. case 'TOP': {
  13629. $x1 = $x;
  13630. $y1 = ($y - $h);
  13631. $x2 = $x;
  13632. $y2 = ($y - $dh);
  13633. break;
  13634. }
  13635. case 'F':
  13636. case 'BOTTOM': {
  13637. $x1 = $x;
  13638. $y1 = ($y + $dh);
  13639. $x2 = $x;
  13640. $y2 = ($y + $h);
  13641. break;
  13642. }
  13643. case 'L':
  13644. case 'LEFT': {
  13645. $x1 = ($x - $w);
  13646. $y1 = $y;
  13647. $x2 = ($x - $dw);
  13648. $y2 = $y;
  13649. break;
  13650. }
  13651. case 'R':
  13652. case 'RIGHT': {
  13653. $x1 = ($x + $dw);
  13654. $y1 = $y;
  13655. $x2 = ($x + $w);
  13656. $y2 = $y;
  13657. break;
  13658. }
  13659. }
  13660. $this->Line($x1, $y1, $x2, $y2);
  13661. }
  13662. }
  13663. /**
  13664. * Paints a registration mark
  13665. * @param $x (float) abscissa of the registration mark center.
  13666. * @param $y (float) ordinate of the registration mark center.
  13667. * @param $r (float) radius of the crop mark.
  13668. * @param $double (boolean) if true print two concentric crop marks.
  13669. * @param $cola (array) crop mark color (default spot registration color 'All').
  13670. * @param $colb (array) second crop mark color (default spot registration color 'None').
  13671. * @author Nicola Asuni
  13672. * @since 4.9.000 (2010-03-26)
  13673. * @public
  13674. */
  13675. public function registrationMark($x, $y, $r, $double=false, $cola=array(100,100,100,100,'All'), $colb=array(0,0,0,0,'None')) {
  13676. $line_style = array('width' => max((0.5 / $this->k),($r / 30)), 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => $cola);
  13677. $this->SetFillColorArray($cola);
  13678. $this->PieSector($x, $y, $r, 90, 180, 'F');
  13679. $this->PieSector($x, $y, $r, 270, 360, 'F');
  13680. $this->Circle($x, $y, $r, 0, 360, 'C', $line_style, array(), 8);
  13681. if ($double) {
  13682. $ri = $r * 0.5;
  13683. $this->SetFillColorArray($colb);
  13684. $this->PieSector($x, $y, $ri, 90, 180, 'F');
  13685. $this->PieSector($x, $y, $ri, 270, 360, 'F');
  13686. $this->SetFillColorArray($cola);
  13687. $this->PieSector($x, $y, $ri, 0, 90, 'F');
  13688. $this->PieSector($x, $y, $ri, 180, 270, 'F');
  13689. $this->Circle($x, $y, $ri, 0, 360, 'C', $line_style, array(), 8);
  13690. }
  13691. }
  13692. /**
  13693. * Paints a CMYK registration mark
  13694. * @param $x (float) abscissa of the registration mark center.
  13695. * @param $y (float) ordinate of the registration mark center.
  13696. * @param $r (float) radius of the crop mark.
  13697. * @author Nicola Asuni
  13698. * @since 6.0.038 (2013-09-30)
  13699. * @public
  13700. */
  13701. public function registrationMarkCMYK($x, $y, $r) {
  13702. // line width
  13703. $lw = max((0.5 / $this->k),($r / 8));
  13704. // internal radius
  13705. $ri = ($r * 0.6);
  13706. // external radius
  13707. $re = ($r * 1.3);
  13708. // Cyan
  13709. $this->SetFillColorArray(array(100,0,0,0));
  13710. $this->PieSector($x, $y, $ri, 270, 360, 'F');
  13711. // Magenta
  13712. $this->SetFillColorArray(array(0,100,0,0));
  13713. $this->PieSector($x, $y, $ri, 0, 90, 'F');
  13714. // Yellow
  13715. $this->SetFillColorArray(array(0,0,100,0));
  13716. $this->PieSector($x, $y, $ri, 90, 180, 'F');
  13717. // Key - black
  13718. $this->SetFillColorArray(array(0,0,0,100));
  13719. $this->PieSector($x, $y, $ri, 180, 270, 'F');
  13720. // registration color
  13721. $line_style = array('width' => $lw, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(100,100,100,100,'All'));
  13722. $this->SetFillColorArray(array(100,100,100,100,'All'));
  13723. // external circle
  13724. $this->Circle($x, $y, $r, 0, 360, 'C', $line_style, array(), 8);
  13725. // cross lines
  13726. $this->Line($x, ($y - $re), $x, ($y - $ri));
  13727. $this->Line($x, ($y + $ri), $x, ($y + $re));
  13728. $this->Line(($x - $re), $y, ($x - $ri), $y);
  13729. $this->Line(($x + $ri), $y, ($x + $re), $y);
  13730. }
  13731. /**
  13732. * Paints a linear colour gradient.
  13733. * @param $x (float) abscissa of the top left corner of the rectangle.
  13734. * @param $y (float) ordinate of the top left corner of the rectangle.
  13735. * @param $w (float) width of the rectangle.
  13736. * @param $h (float) height of the rectangle.
  13737. * @param $col1 (array) first color (Grayscale, RGB or CMYK components).
  13738. * @param $col2 (array) second color (Grayscale, RGB or CMYK components).
  13739. * @param $coords (array) 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).
  13740. * @author Andreas W\FCrmser, Nicola Asuni
  13741. * @since 3.1.000 (2008-06-09)
  13742. * @public
  13743. */
  13744. public function LinearGradient($x, $y, $w, $h, $col1=array(), $col2=array(), $coords=array(0,0,1,0)) {
  13745. $this->Clip($x, $y, $w, $h);
  13746. $this->Gradient(2, $coords, array(array('color' => $col1, 'offset' => 0, 'exponent' => 1), array('color' => $col2, 'offset' => 1, 'exponent' => 1)), array(), false);
  13747. }
  13748. /**
  13749. * Paints a radial colour gradient.
  13750. * @param $x (float) abscissa of the top left corner of the rectangle.
  13751. * @param $y (float) ordinate of the top left corner of the rectangle.
  13752. * @param $w (float) width of the rectangle.
  13753. * @param $h (float) height of the rectangle.
  13754. * @param $col1 (array) first color (Grayscale, RGB or CMYK components).
  13755. * @param $col2 (array) second color (Grayscale, RGB or CMYK components).
  13756. * @param $coords (array) 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.
  13757. * @author Andreas W\FCrmser, Nicola Asuni
  13758. * @since 3.1.000 (2008-06-09)
  13759. * @public
  13760. */
  13761. public function RadialGradient($x, $y, $w, $h, $col1=array(), $col2=array(), $coords=array(0.5,0.5,0.5,0.5,1)) {
  13762. $this->Clip($x, $y, $w, $h);
  13763. $this->Gradient(3, $coords, array(array('color' => $col1, 'offset' => 0, 'exponent' => 1), array('color' => $col2, 'offset' => 1, 'exponent' => 1)), array(), false);
  13764. }
  13765. /**
  13766. * Paints a coons patch mesh.
  13767. * @param $x (float) abscissa of the top left corner of the rectangle.
  13768. * @param $y (float) ordinate of the top left corner of the rectangle.
  13769. * @param $w (float) width of the rectangle.
  13770. * @param $h (float) height of the rectangle.
  13771. * @param $col1 (array) first color (lower left corner) (RGB components).
  13772. * @param $col2 (array) second color (lower right corner) (RGB components).
  13773. * @param $col3 (array) third color (upper right corner) (RGB components).
  13774. * @param $col4 (array) fourth color (upper left corner) (RGB components).
  13775. * @param $coords (array) <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>
  13776. * @param $coords_min (array) minimum value used by the coordinates. If a coordinate's value is smaller than this it will be cut to coords_min. default: 0
  13777. * @param $coords_max (array) maximum value used by the coordinates. If a coordinate's value is greater than this it will be cut to coords_max. default: 1
  13778. * @param $antialias (boolean) A flag indicating whether to filter the shading function to prevent aliasing artifacts.
  13779. * @author Andreas W\FCrmser, Nicola Asuni
  13780. * @since 3.1.000 (2008-06-09)
  13781. * @public
  13782. */
  13783. 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) {
  13784. if ($this->pdfa_mode OR ($this->state != 2)) {
  13785. return;
  13786. }
  13787. $this->Clip($x, $y, $w, $h);
  13788. $n = count($this->gradients) + 1;
  13789. $this->gradients[$n] = array();
  13790. $this->gradients[$n]['type'] = 6; //coons patch mesh
  13791. $this->gradients[$n]['coords'] = array();
  13792. $this->gradients[$n]['antialias'] = $antialias;
  13793. $this->gradients[$n]['colors'] = array();
  13794. $this->gradients[$n]['transparency'] = false;
  13795. //check the coords array if it is the simple array or the multi patch array
  13796. if (!isset($coords[0]['f'])) {
  13797. //simple array -> convert to multi patch array
  13798. if (!isset($col1[1])) {
  13799. $col1[1] = $col1[2] = $col1[0];
  13800. }
  13801. if (!isset($col2[1])) {
  13802. $col2[1] = $col2[2] = $col2[0];
  13803. }
  13804. if (!isset($col3[1])) {
  13805. $col3[1] = $col3[2] = $col3[0];
  13806. }
  13807. if (!isset($col4[1])) {
  13808. $col4[1] = $col4[2] = $col4[0];
  13809. }
  13810. $patch_array[0]['f'] = 0;
  13811. $patch_array[0]['points'] = $coords;
  13812. $patch_array[0]['colors'][0]['r'] = $col1[0];
  13813. $patch_array[0]['colors'][0]['g'] = $col1[1];
  13814. $patch_array[0]['colors'][0]['b'] = $col1[2];
  13815. $patch_array[0]['colors'][1]['r'] = $col2[0];
  13816. $patch_array[0]['colors'][1]['g'] = $col2[1];
  13817. $patch_array[0]['colors'][1]['b'] = $col2[2];
  13818. $patch_array[0]['colors'][2]['r'] = $col3[0];
  13819. $patch_array[0]['colors'][2]['g'] = $col3[1];
  13820. $patch_array[0]['colors'][2]['b'] = $col3[2];
  13821. $patch_array[0]['colors'][3]['r'] = $col4[0];
  13822. $patch_array[0]['colors'][3]['g'] = $col4[1];
  13823. $patch_array[0]['colors'][3]['b'] = $col4[2];
  13824. } else {
  13825. //multi patch array
  13826. $patch_array = $coords;
  13827. }
  13828. $bpcd = 65535; //16 bits per coordinate
  13829. //build the data stream
  13830. $this->gradients[$n]['stream'] = '';
  13831. $count_patch = count($patch_array);
  13832. for ($i=0; $i < $count_patch; ++$i) {
  13833. $this->gradients[$n]['stream'] .= chr($patch_array[$i]['f']); //start with the edge flag as 8 bit
  13834. $count_points = count($patch_array[$i]['points']);
  13835. for ($j=0; $j < $count_points; ++$j) {
  13836. //each point as 16 bit
  13837. $patch_array[$i]['points'][$j] = (($patch_array[$i]['points'][$j] - $coords_min) / ($coords_max - $coords_min)) * $bpcd;
  13838. if ($patch_array[$i]['points'][$j] < 0) {
  13839. $patch_array[$i]['points'][$j] = 0;
  13840. }
  13841. if ($patch_array[$i]['points'][$j] > $bpcd) {
  13842. $patch_array[$i]['points'][$j] = $bpcd;
  13843. }
  13844. $this->gradients[$n]['stream'] .= chr(floor($patch_array[$i]['points'][$j] / 256));
  13845. $this->gradients[$n]['stream'] .= chr(floor($patch_array[$i]['points'][$j] % 256));
  13846. }
  13847. $count_cols = count($patch_array[$i]['colors']);
  13848. for ($j=0; $j < $count_cols; ++$j) {
  13849. //each color component as 8 bit
  13850. $this->gradients[$n]['stream'] .= chr($patch_array[$i]['colors'][$j]['r']);
  13851. $this->gradients[$n]['stream'] .= chr($patch_array[$i]['colors'][$j]['g']);
  13852. $this->gradients[$n]['stream'] .= chr($patch_array[$i]['colors'][$j]['b']);
  13853. }
  13854. }
  13855. //paint the gradient
  13856. $this->_out('/Sh'.$n.' sh');
  13857. //restore previous Graphic State
  13858. $this->_outRestoreGraphicsState();
  13859. if ($this->inxobj) {
  13860. // we are inside an XObject template
  13861. $this->xobjects[$this->xobjid]['gradients'][$n] = $this->gradients[$n];
  13862. }
  13863. }
  13864. /**
  13865. * Set a rectangular clipping area.
  13866. * @param $x (float) abscissa of the top left corner of the rectangle (or top right corner for RTL mode).
  13867. * @param $y (float) ordinate of the top left corner of the rectangle.
  13868. * @param $w (float) width of the rectangle.
  13869. * @param $h (float) height of the rectangle.
  13870. * @author Andreas W\FCrmser, Nicola Asuni
  13871. * @since 3.1.000 (2008-06-09)
  13872. * @protected
  13873. */
  13874. protected function Clip($x, $y, $w, $h) {
  13875. if ($this->state != 2) {
  13876. return;
  13877. }
  13878. if ($this->rtl) {
  13879. $x = $this->w - $x - $w;
  13880. }
  13881. //save current Graphic State
  13882. $s = 'q';
  13883. //set clipping area
  13884. $s .= sprintf(' %F %F %F %F re W n', $x*$this->k, ($this->h-$y)*$this->k, $w*$this->k, -$h*$this->k);
  13885. //set up transformation matrix for gradient
  13886. $s .= sprintf(' %F 0 0 %F %F %F cm', $w*$this->k, $h*$this->k, $x*$this->k, ($this->h-($y+$h))*$this->k);
  13887. $this->_out($s);
  13888. }
  13889. /**
  13890. * Output gradient.
  13891. * @param $type (int) 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)
  13892. * @param $coords (array) array of coordinates.
  13893. * @param $stops (array) 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).
  13894. * @param $background (array) An array of colour components appropriate to the colour space, specifying a single background colour value.
  13895. * @param $antialias (boolean) A flag indicating whether to filter the shading function to prevent aliasing artifacts.
  13896. * @author Nicola Asuni
  13897. * @since 3.1.000 (2008-06-09)
  13898. * @public
  13899. */
  13900. public function Gradient($type, $coords, $stops, $background=array(), $antialias=false) {
  13901. if ($this->pdfa_mode OR ($this->state != 2)) {
  13902. return;
  13903. }
  13904. $n = count($this->gradients) + 1;
  13905. $this->gradients[$n] = array();
  13906. $this->gradients[$n]['type'] = $type;
  13907. $this->gradients[$n]['coords'] = $coords;
  13908. $this->gradients[$n]['antialias'] = $antialias;
  13909. $this->gradients[$n]['colors'] = array();
  13910. $this->gradients[$n]['transparency'] = false;
  13911. // color space
  13912. $numcolspace = count($stops[0]['color']);
  13913. $bcolor = array_values($background);
  13914. switch($numcolspace) {
  13915. case 5: // SPOT
  13916. case 4: { // CMYK
  13917. $this->gradients[$n]['colspace'] = 'DeviceCMYK';
  13918. if (!empty($background)) {
  13919. $this->gradients[$n]['background'] = sprintf('%F %F %F %F', $bcolor[0]/100, $bcolor[1]/100, $bcolor[2]/100, $bcolor[3]/100);
  13920. }
  13921. break;
  13922. }
  13923. case 3: { // RGB
  13924. $this->gradients[$n]['colspace'] = 'DeviceRGB';
  13925. if (!empty($background)) {
  13926. $this->gradients[$n]['background'] = sprintf('%F %F %F', $bcolor[0]/255, $bcolor[1]/255, $bcolor[2]/255);
  13927. }
  13928. break;
  13929. }
  13930. case 1: { // GRAY SCALE
  13931. $this->gradients[$n]['colspace'] = 'DeviceGray';
  13932. if (!empty($background)) {
  13933. $this->gradients[$n]['background'] = sprintf('%F', $bcolor[0]/255);
  13934. }
  13935. break;
  13936. }
  13937. }
  13938. $num_stops = count($stops);
  13939. $last_stop_id = $num_stops - 1;
  13940. foreach ($stops as $key => $stop) {
  13941. $this->gradients[$n]['colors'][$key] = array();
  13942. // offset represents a location along the gradient vector
  13943. if (isset($stop['offset'])) {
  13944. $this->gradients[$n]['colors'][$key]['offset'] = $stop['offset'];
  13945. } else {
  13946. if ($key == 0) {
  13947. $this->gradients[$n]['colors'][$key]['offset'] = 0;
  13948. } elseif ($key == $last_stop_id) {
  13949. $this->gradients[$n]['colors'][$key]['offset'] = 1;
  13950. } else {
  13951. $offsetstep = (1 - $this->gradients[$n]['colors'][($key - 1)]['offset']) / ($num_stops - $key);
  13952. $this->gradients[$n]['colors'][$key]['offset'] = $this->gradients[$n]['colors'][($key - 1)]['offset'] + $offsetstep;
  13953. }
  13954. }
  13955. if (isset($stop['opacity'])) {
  13956. $this->gradients[$n]['colors'][$key]['opacity'] = $stop['opacity'];
  13957. if ((!$this->pdfa_mode) AND ($stop['opacity'] < 1)) {
  13958. $this->gradients[$n]['transparency'] = true;
  13959. }
  13960. } else {
  13961. $this->gradients[$n]['colors'][$key]['opacity'] = 1;
  13962. }
  13963. // exponent for the exponential interpolation function
  13964. if (isset($stop['exponent'])) {
  13965. $this->gradients[$n]['colors'][$key]['exponent'] = $stop['exponent'];
  13966. } else {
  13967. $this->gradients[$n]['colors'][$key]['exponent'] = 1;
  13968. }
  13969. // set colors
  13970. $color = array_values($stop['color']);
  13971. switch($numcolspace) {
  13972. case 5: // SPOT
  13973. case 4: { // CMYK
  13974. $this->gradients[$n]['colors'][$key]['color'] = sprintf('%F %F %F %F', $color[0]/100, $color[1]/100, $color[2]/100, $color[3]/100);
  13975. break;
  13976. }
  13977. case 3: { // RGB
  13978. $this->gradients[$n]['colors'][$key]['color'] = sprintf('%F %F %F', $color[0]/255, $color[1]/255, $color[2]/255);
  13979. break;
  13980. }
  13981. case 1: { // GRAY SCALE
  13982. $this->gradients[$n]['colors'][$key]['color'] = sprintf('%F', $color[0]/255);
  13983. break;
  13984. }
  13985. }
  13986. }
  13987. if ($this->gradients[$n]['transparency']) {
  13988. // paint luminosity gradient
  13989. $this->_out('/TGS'.$n.' gs');
  13990. }
  13991. //paint the gradient
  13992. $this->_out('/Sh'.$n.' sh');
  13993. //restore previous Graphic State
  13994. $this->_outRestoreGraphicsState();
  13995. if ($this->inxobj) {
  13996. // we are inside an XObject template
  13997. $this->xobjects[$this->xobjid]['gradients'][$n] = $this->gradients[$n];
  13998. }
  13999. }
  14000. /**
  14001. * Output gradient shaders.
  14002. * @author Nicola Asuni
  14003. * @since 3.1.000 (2008-06-09)
  14004. * @protected
  14005. */
  14006. function _putshaders() {
  14007. if ($this->pdfa_mode) {
  14008. return;
  14009. }
  14010. $idt = count($this->gradients); //index for transparency gradients
  14011. foreach ($this->gradients as $id => $grad) {
  14012. if (($grad['type'] == 2) OR ($grad['type'] == 3)) {
  14013. $fc = $this->_newobj();
  14014. $out = '<<';
  14015. $out .= ' /FunctionType 3';
  14016. $out .= ' /Domain [0 1]';
  14017. $functions = '';
  14018. $bounds = '';
  14019. $encode = '';
  14020. $i = 1;
  14021. $num_cols = count($grad['colors']);
  14022. $lastcols = $num_cols - 1;
  14023. for ($i = 1; $i < $num_cols; ++$i) {
  14024. $functions .= ($fc + $i).' 0 R ';
  14025. if ($i < $lastcols) {
  14026. $bounds .= sprintf('%F ', $grad['colors'][$i]['offset']);
  14027. }
  14028. $encode .= '0 1 ';
  14029. }
  14030. $out .= ' /Functions ['.trim($functions).']';
  14031. $out .= ' /Bounds ['.trim($bounds).']';
  14032. $out .= ' /Encode ['.trim($encode).']';
  14033. $out .= ' >>';
  14034. $out .= "\n".'endobj';
  14035. $this->_out($out);
  14036. for ($i = 1; $i < $num_cols; ++$i) {
  14037. $this->_newobj();
  14038. $out = '<<';
  14039. $out .= ' /FunctionType 2';
  14040. $out .= ' /Domain [0 1]';
  14041. $out .= ' /C0 ['.$grad['colors'][($i - 1)]['color'].']';
  14042. $out .= ' /C1 ['.$grad['colors'][$i]['color'].']';
  14043. $out .= ' /N '.$grad['colors'][$i]['exponent'];
  14044. $out .= ' >>';
  14045. $out .= "\n".'endobj';
  14046. $this->_out($out);
  14047. }
  14048. // set transparency functions
  14049. if ($grad['transparency']) {
  14050. $ft = $this->_newobj();
  14051. $out = '<<';
  14052. $out .= ' /FunctionType 3';
  14053. $out .= ' /Domain [0 1]';
  14054. $functions = '';
  14055. $i = 1;
  14056. $num_cols = count($grad['colors']);
  14057. for ($i = 1; $i < $num_cols; ++$i) {
  14058. $functions .= ($ft + $i).' 0 R ';
  14059. }
  14060. $out .= ' /Functions ['.trim($functions).']';
  14061. $out .= ' /Bounds ['.trim($bounds).']';
  14062. $out .= ' /Encode ['.trim($encode).']';
  14063. $out .= ' >>';
  14064. $out .= "\n".'endobj';
  14065. $this->_out($out);
  14066. for ($i = 1; $i < $num_cols; ++$i) {
  14067. $this->_newobj();
  14068. $out = '<<';
  14069. $out .= ' /FunctionType 2';
  14070. $out .= ' /Domain [0 1]';
  14071. $out .= ' /C0 ['.$grad['colors'][($i - 1)]['opacity'].']';
  14072. $out .= ' /C1 ['.$grad['colors'][$i]['opacity'].']';
  14073. $out .= ' /N '.$grad['colors'][$i]['exponent'];
  14074. $out .= ' >>';
  14075. $out .= "\n".'endobj';
  14076. $this->_out($out);
  14077. }
  14078. }
  14079. }
  14080. // set shading object
  14081. $this->_newobj();
  14082. $out = '<< /ShadingType '.$grad['type'];
  14083. if (isset($grad['colspace'])) {
  14084. $out .= ' /ColorSpace /'.$grad['colspace'];
  14085. } else {
  14086. $out .= ' /ColorSpace /DeviceRGB';
  14087. }
  14088. if (isset($grad['background']) AND !empty($grad['background'])) {
  14089. $out .= ' /Background ['.$grad['background'].']';
  14090. }
  14091. if (isset($grad['antialias']) AND ($grad['antialias'] === true)) {
  14092. $out .= ' /AntiAlias true';
  14093. }
  14094. if ($grad['type'] == 2) {
  14095. $out .= ' '.sprintf('/Coords [%F %F %F %F]', $grad['coords'][0], $grad['coords'][1], $grad['coords'][2], $grad['coords'][3]);
  14096. $out .= ' /Domain [0 1]';
  14097. $out .= ' /Function '.$fc.' 0 R';
  14098. $out .= ' /Extend [true true]';
  14099. $out .= ' >>';
  14100. } elseif ($grad['type'] == 3) {
  14101. //x0, y0, r0, x1, y1, r1
  14102. //at this this time radius of inner circle is 0
  14103. $out .= ' '.sprintf('/Coords [%F %F 0 %F %F %F]', $grad['coords'][0], $grad['coords'][1], $grad['coords'][2], $grad['coords'][3], $grad['coords'][4]);
  14104. $out .= ' /Domain [0 1]';
  14105. $out .= ' /Function '.$fc.' 0 R';
  14106. $out .= ' /Extend [true true]';
  14107. $out .= ' >>';
  14108. } elseif ($grad['type'] == 6) {
  14109. $out .= ' /BitsPerCoordinate 16';
  14110. $out .= ' /BitsPerComponent 8';
  14111. $out .= ' /Decode[0 1 0 1 0 1 0 1 0 1]';
  14112. $out .= ' /BitsPerFlag 8';
  14113. $stream = $this->_getrawstream($grad['stream']);
  14114. $out .= ' /Length '.strlen($stream);
  14115. $out .= ' >>';
  14116. $out .= ' stream'."\n".$stream."\n".'endstream';
  14117. }
  14118. $out .= "\n".'endobj';
  14119. $this->_out($out);
  14120. if ($grad['transparency']) {
  14121. $shading_transparency = preg_replace('/\/ColorSpace \/[^\s]+/si', '/ColorSpace /DeviceGray', $out);
  14122. $shading_transparency = preg_replace('/\/Function [0-9]+ /si', '/Function '.$ft.' ', $shading_transparency);
  14123. }
  14124. $this->gradients[$id]['id'] = $this->n;
  14125. // set pattern object
  14126. $this->_newobj();
  14127. $out = '<< /Type /Pattern /PatternType 2';
  14128. $out .= ' /Shading '.$this->gradients[$id]['id'].' 0 R';
  14129. $out .= ' >>';
  14130. $out .= "\n".'endobj';
  14131. $this->_out($out);
  14132. $this->gradients[$id]['pattern'] = $this->n;
  14133. // set shading and pattern for transparency mask
  14134. if ($grad['transparency']) {
  14135. // luminosity pattern
  14136. $idgs = $id + $idt;
  14137. $this->_newobj();
  14138. $this->_out($shading_transparency);
  14139. $this->gradients[$idgs]['id'] = $this->n;
  14140. $this->_newobj();
  14141. $out = '<< /Type /Pattern /PatternType 2';
  14142. $out .= ' /Shading '.$this->gradients[$idgs]['id'].' 0 R';
  14143. $out .= ' >>';
  14144. $out .= "\n".'endobj';
  14145. $this->_out($out);
  14146. $this->gradients[$idgs]['pattern'] = $this->n;
  14147. // luminosity XObject
  14148. $oid = $this->_newobj();
  14149. $this->xobjects['LX'.$oid] = array('n' => $oid);
  14150. $filter = '';
  14151. $stream = 'q /a0 gs /Pattern cs /p'.$idgs.' scn 0 0 '.$this->wPt.' '.$this->hPt.' re f Q';
  14152. if ($this->compress) {
  14153. $filter = ' /Filter /FlateDecode';
  14154. $stream = gzcompress($stream);
  14155. }
  14156. $stream = $this->_getrawstream($stream);
  14157. $out = '<< /Type /XObject /Subtype /Form /FormType 1'.$filter;
  14158. $out .= ' /Length '.strlen($stream);
  14159. $rect = sprintf('%F %F', $this->wPt, $this->hPt);
  14160. $out .= ' /BBox [0 0 '.$rect.']';
  14161. $out .= ' /Group << /Type /Group /S /Transparency /CS /DeviceGray >>';
  14162. $out .= ' /Resources <<';
  14163. $out .= ' /ExtGState << /a0 << /ca 1 /CA 1 >> >>';
  14164. $out .= ' /Pattern << /p'.$idgs.' '.$this->gradients[$idgs]['pattern'].' 0 R >>';
  14165. $out .= ' >>';
  14166. $out .= ' >> ';
  14167. $out .= ' stream'."\n".$stream."\n".'endstream';
  14168. $out .= "\n".'endobj';
  14169. $this->_out($out);
  14170. // SMask
  14171. $this->_newobj();
  14172. $out = '<< /Type /Mask /S /Luminosity /G '.($this->n - 1).' 0 R >>'."\n".'endobj';
  14173. $this->_out($out);
  14174. // ExtGState
  14175. $this->_newobj();
  14176. $out = '<< /Type /ExtGState /SMask '.($this->n - 1).' 0 R /AIS false >>'."\n".'endobj';
  14177. $this->_out($out);
  14178. $this->extgstates[] = array('n' => $this->n, 'name' => 'TGS'.$id);
  14179. }
  14180. }
  14181. }
  14182. /**
  14183. * Draw the sector of a circle.
  14184. * It can be used for instance to render pie charts.
  14185. * @param $xc (float) abscissa of the center.
  14186. * @param $yc (float) ordinate of the center.
  14187. * @param $r (float) radius.
  14188. * @param $a (float) start angle (in degrees).
  14189. * @param $b (float) end angle (in degrees).
  14190. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  14191. * @param $cw: (float) indicates whether to go clockwise (default: true).
  14192. * @param $o: (float) origin of angles (0 for 3 o'clock, 90 for noon, 180 for 9 o'clock, 270 for 6 o'clock). Default: 90.
  14193. * @author Maxime Delorme, Nicola Asuni
  14194. * @since 3.1.000 (2008-06-09)
  14195. * @public
  14196. */
  14197. public function PieSector($xc, $yc, $r, $a, $b, $style='FD', $cw=true, $o=90) {
  14198. $this->PieSectorXY($xc, $yc, $r, $r, $a, $b, $style, $cw, $o);
  14199. }
  14200. /**
  14201. * Draw the sector of an ellipse.
  14202. * It can be used for instance to render pie charts.
  14203. * @param $xc (float) abscissa of the center.
  14204. * @param $yc (float) ordinate of the center.
  14205. * @param $rx (float) the x-axis radius.
  14206. * @param $ry (float) the y-axis radius.
  14207. * @param $a (float) start angle (in degrees).
  14208. * @param $b (float) end angle (in degrees).
  14209. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  14210. * @param $cw: (float) indicates whether to go clockwise.
  14211. * @param $o: (float) origin of angles (0 for 3 o'clock, 90 for noon, 180 for 9 o'clock, 270 for 6 o'clock).
  14212. * @param $nc (integer) Number of curves used to draw a 90 degrees portion of arc.
  14213. * @author Maxime Delorme, Nicola Asuni
  14214. * @since 3.1.000 (2008-06-09)
  14215. * @public
  14216. */
  14217. public function PieSectorXY($xc, $yc, $rx, $ry, $a, $b, $style='FD', $cw=false, $o=0, $nc=2) {
  14218. if ($this->state != 2) {
  14219. return;
  14220. }
  14221. if ($this->rtl) {
  14222. $xc = ($this->w - $xc);
  14223. }
  14224. $op = TCPDF_STATIC::getPathPaintOperator($style);
  14225. if ($op == 'f') {
  14226. $line_style = array();
  14227. }
  14228. if ($cw) {
  14229. $d = $b;
  14230. $b = (360 - $a + $o);
  14231. $a = (360 - $d + $o);
  14232. } else {
  14233. $b += $o;
  14234. $a += $o;
  14235. }
  14236. $this->_outellipticalarc($xc, $yc, $rx, $ry, 0, $a, $b, true, $nc);
  14237. $this->_out($op);
  14238. }
  14239. /**
  14240. * Embed vector-based Adobe Illustrator (AI) or AI-compatible EPS files.
  14241. * NOTE: EPS is not yet fully implemented, use the setRasterizeVectorImages() method to enable/disable rasterization of vector images using ImageMagick library.
  14242. * Only vector drawing is supported, not text or bitmap.
  14243. * 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).
  14244. * @param $file (string) Name of the file containing the image or a '@' character followed by the EPS/AI data string.
  14245. * @param $x (float) Abscissa of the upper-left corner.
  14246. * @param $y (float) Ordinate of the upper-left corner.
  14247. * @param $w (float) Width of the image in the page. If not specified or equal to zero, it is automatically calculated.
  14248. * @param $h (float) Height of the image in the page. If not specified or equal to zero, it is automatically calculated.
  14249. * @param $link (mixed) URL or identifier returned by AddLink().
  14250. * @param $useBoundingBox (boolean) specifies whether to position the bounding box (true) or the complete canvas (false) at location (x,y). Default value is true.
  14251. * @param $align (string) 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>
  14252. * @param $palign (string) 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>
  14253. * @param $border (mixed) 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)))
  14254. * @param $fitonpage (boolean) if true the image is resized to not exceed page dimensions.
  14255. * @param $fixoutvals (boolean) if true remove values outside the bounding box.
  14256. * @author Valentin Schmidt, Nicola Asuni
  14257. * @since 3.1.000 (2008-06-09)
  14258. * @public
  14259. */
  14260. public function ImageEps($file, $x='', $y='', $w=0, $h=0, $link='', $useBoundingBox=true, $align='', $palign='', $border=0, $fitonpage=false, $fixoutvals=false) {
  14261. if ($this->state != 2) {
  14262. return;
  14263. }
  14264. if ($this->rasterize_vector_images AND ($w > 0) AND ($h > 0)) {
  14265. // convert EPS to raster image using GD or ImageMagick libraries
  14266. return $this->Image($file, $x, $y, $w, $h, 'EPS', $link, $align, true, 300, $palign, false, false, $border, false, false, $fitonpage);
  14267. }
  14268. if ($x === '') {
  14269. $x = $this->x;
  14270. }
  14271. if ($y === '') {
  14272. $y = $this->y;
  14273. }
  14274. // check page for no-write regions and adapt page margins if necessary
  14275. list($x, $y) = $this->checkPageRegions($h, $x, $y);
  14276. $k = $this->k;
  14277. if ($file[0] === '@') { // image from string
  14278. $data = substr($file, 1);
  14279. } else { // EPS/AI file
  14280. $data = TCPDF_STATIC::fileGetContents($file);
  14281. }
  14282. if ($data === FALSE) {
  14283. $this->Error('EPS file not found: '.$file);
  14284. }
  14285. $regs = array();
  14286. // EPS/AI compatibility check (only checks files created by Adobe Illustrator!)
  14287. preg_match("/%%Creator:([^\r\n]+)/", $data, $regs); # find Creator
  14288. if (count($regs) > 1) {
  14289. $version_str = trim($regs[1]); # e.g. "Adobe Illustrator(R) 8.0"
  14290. if (strpos($version_str, 'Adobe Illustrator') !== false) {
  14291. $versexp = explode(' ', $version_str);
  14292. $version = (float)array_pop($versexp);
  14293. if ($version >= 9) {
  14294. $this->Error('This version of Adobe Illustrator file is not supported: '.$file);
  14295. }
  14296. }
  14297. }
  14298. // strip binary bytes in front of PS-header
  14299. $start = strpos($data, '%!PS-Adobe');
  14300. if ($start > 0) {
  14301. $data = substr($data, $start);
  14302. }
  14303. // find BoundingBox params
  14304. preg_match("/%%BoundingBox:([^\r\n]+)/", $data, $regs);
  14305. if (count($regs) > 1) {
  14306. list($x1, $y1, $x2, $y2) = explode(' ', trim($regs[1]));
  14307. } else {
  14308. $this->Error('No BoundingBox found in EPS/AI file: '.$file);
  14309. }
  14310. $start = strpos($data, '%%EndSetup');
  14311. if ($start === false) {
  14312. $start = strpos($data, '%%EndProlog');
  14313. }
  14314. if ($start === false) {
  14315. $start = strpos($data, '%%BoundingBox');
  14316. }
  14317. $data = substr($data, $start);
  14318. $end = strpos($data, '%%PageTrailer');
  14319. if ($end===false) {
  14320. $end = strpos($data, 'showpage');
  14321. }
  14322. if ($end) {
  14323. $data = substr($data, 0, $end);
  14324. }
  14325. // calculate image width and height on document
  14326. if (($w <= 0) AND ($h <= 0)) {
  14327. $w = ($x2 - $x1) / $k;
  14328. $h = ($y2 - $y1) / $k;
  14329. } elseif ($w <= 0) {
  14330. $w = ($x2-$x1) / $k * ($h / (($y2 - $y1) / $k));
  14331. } elseif ($h <= 0) {
  14332. $h = ($y2 - $y1) / $k * ($w / (($x2 - $x1) / $k));
  14333. }
  14334. // fit the image on available space
  14335. list($w, $h, $x, $y) = $this->fitBlock($w, $h, $x, $y, $fitonpage);
  14336. if ($this->rasterize_vector_images) {
  14337. // convert EPS to raster image using GD or ImageMagick libraries
  14338. return $this->Image($file, $x, $y, $w, $h, 'EPS', $link, $align, true, 300, $palign, false, false, $border, false, false, $fitonpage);
  14339. }
  14340. // set scaling factors
  14341. $scale_x = $w / (($x2 - $x1) / $k);
  14342. $scale_y = $h / (($y2 - $y1) / $k);
  14343. // set alignment
  14344. $this->img_rb_y = $y + $h;
  14345. // set alignment
  14346. if ($this->rtl) {
  14347. if ($palign == 'L') {
  14348. $ximg = $this->lMargin;
  14349. } elseif ($palign == 'C') {
  14350. $ximg = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
  14351. } elseif ($palign == 'R') {
  14352. $ximg = $this->w - $this->rMargin - $w;
  14353. } else {
  14354. $ximg = $x - $w;
  14355. }
  14356. $this->img_rb_x = $ximg;
  14357. } else {
  14358. if ($palign == 'L') {
  14359. $ximg = $this->lMargin;
  14360. } elseif ($palign == 'C') {
  14361. $ximg = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
  14362. } elseif ($palign == 'R') {
  14363. $ximg = $this->w - $this->rMargin - $w;
  14364. } else {
  14365. $ximg = $x;
  14366. }
  14367. $this->img_rb_x = $ximg + $w;
  14368. }
  14369. if ($useBoundingBox) {
  14370. $dx = $ximg * $k - $x1;
  14371. $dy = $y * $k - $y1;
  14372. } else {
  14373. $dx = $ximg * $k;
  14374. $dy = $y * $k;
  14375. }
  14376. // save the current graphic state
  14377. $this->_out('q'.$this->epsmarker);
  14378. // translate
  14379. $this->_out(sprintf('%F %F %F %F %F %F cm', 1, 0, 0, 1, $dx, $dy + ($this->hPt - (2 * $y * $k) - ($y2 - $y1))));
  14380. // scale
  14381. if (isset($scale_x)) {
  14382. $this->_out(sprintf('%F %F %F %F %F %F cm', $scale_x, 0, 0, $scale_y, $x1 * (1 - $scale_x), $y2 * (1 - $scale_y)));
  14383. }
  14384. // handle pc/unix/mac line endings
  14385. $lines = preg_split('/[\r\n]+/si', $data, -1, PREG_SPLIT_NO_EMPTY);
  14386. $u=0;
  14387. $cnt = count($lines);
  14388. for ($i=0; $i < $cnt; ++$i) {
  14389. $line = $lines[$i];
  14390. if (($line == '') OR ($line[0] == '%')) {
  14391. continue;
  14392. }
  14393. $len = strlen($line);
  14394. // check for spot color names
  14395. $color_name = '';
  14396. if (strcasecmp('x', substr(trim($line), -1)) == 0) {
  14397. if (preg_match('/\([^\)]*\)/', $line, $matches) > 0) {
  14398. // extract spot color name
  14399. $color_name = $matches[0];
  14400. // remove color name from string
  14401. $line = str_replace(' '.$color_name, '', $line);
  14402. // remove pharentesis from color name
  14403. $color_name = substr($color_name, 1, -1);
  14404. }
  14405. }
  14406. $chunks = explode(' ', $line);
  14407. $cmd = trim(array_pop($chunks));
  14408. // RGB
  14409. if (($cmd == 'Xa') OR ($cmd == 'XA')) {
  14410. $b = array_pop($chunks);
  14411. $g = array_pop($chunks);
  14412. $r = array_pop($chunks);
  14413. $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!
  14414. continue;
  14415. }
  14416. $skip = false;
  14417. if ($fixoutvals) {
  14418. // check for values outside the bounding box
  14419. switch ($cmd) {
  14420. case 'm':
  14421. case 'l':
  14422. case 'L': {
  14423. // skip values outside bounding box
  14424. foreach ($chunks as $key => $val) {
  14425. if ((($key % 2) == 0) AND (($val < $x1) OR ($val > $x2))) {
  14426. $skip = true;
  14427. } elseif ((($key % 2) != 0) AND (($val < $y1) OR ($val > $y2))) {
  14428. $skip = true;
  14429. }
  14430. }
  14431. }
  14432. }
  14433. }
  14434. switch ($cmd) {
  14435. case 'm':
  14436. case 'l':
  14437. case 'v':
  14438. case 'y':
  14439. case 'c':
  14440. case 'k':
  14441. case 'K':
  14442. case 'g':
  14443. case 'G':
  14444. case 's':
  14445. case 'S':
  14446. case 'J':
  14447. case 'j':
  14448. case 'w':
  14449. case 'M':
  14450. case 'd':
  14451. case 'n': {
  14452. if ($skip) {
  14453. break;
  14454. }
  14455. $this->_out($line);
  14456. break;
  14457. }
  14458. case 'x': {// custom fill color
  14459. if (empty($color_name)) {
  14460. // CMYK color
  14461. list($col_c, $col_m, $col_y, $col_k) = $chunks;
  14462. $this->_out(''.$col_c.' '.$col_m.' '.$col_y.' '.$col_k.' k');
  14463. } else {
  14464. // Spot Color (CMYK + tint)
  14465. list($col_c, $col_m, $col_y, $col_k, $col_t) = $chunks;
  14466. $this->AddSpotColor($color_name, ($col_c * 100), ($col_m * 100), ($col_y * 100), ($col_k * 100));
  14467. $color_cmd = sprintf('/CS%d cs %F scn', $this->spot_colors[$color_name]['i'], (1 - $col_t));
  14468. $this->_out($color_cmd);
  14469. }
  14470. break;
  14471. }
  14472. case 'X': { // custom stroke color
  14473. if (empty($color_name)) {
  14474. // CMYK color
  14475. list($col_c, $col_m, $col_y, $col_k) = $chunks;
  14476. $this->_out(''.$col_c.' '.$col_m.' '.$col_y.' '.$col_k.' K');
  14477. } else {
  14478. // Spot Color (CMYK + tint)
  14479. list($col_c, $col_m, $col_y, $col_k, $col_t) = $chunks;
  14480. $this->AddSpotColor($color_name, ($col_c * 100), ($col_m * 100), ($col_y * 100), ($col_k * 100));
  14481. $color_cmd = sprintf('/CS%d CS %F SCN', $this->spot_colors[$color_name]['i'], (1 - $col_t));
  14482. $this->_out($color_cmd);
  14483. }
  14484. break;
  14485. }
  14486. case 'Y':
  14487. case 'N':
  14488. case 'V':
  14489. case 'L':
  14490. case 'C': {
  14491. if ($skip) {
  14492. break;
  14493. }
  14494. $line[($len - 1)] = strtolower($cmd);
  14495. $this->_out($line);
  14496. break;
  14497. }
  14498. case 'b':
  14499. case 'B': {
  14500. $this->_out($cmd . '*');
  14501. break;
  14502. }
  14503. case 'f':
  14504. case 'F': {
  14505. if ($u > 0) {
  14506. $isU = false;
  14507. $max = min(($i + 5), $cnt);
  14508. for ($j = ($i + 1); $j < $max; ++$j) {
  14509. $isU = ($isU OR (($lines[$j] == 'U') OR ($lines[$j] == '*U')));
  14510. }
  14511. if ($isU) {
  14512. $this->_out('f*');
  14513. }
  14514. } else {
  14515. $this->_out('f*');
  14516. }
  14517. break;
  14518. }
  14519. case '*u': {
  14520. ++$u;
  14521. break;
  14522. }
  14523. case '*U': {
  14524. --$u;
  14525. break;
  14526. }
  14527. }
  14528. }
  14529. // restore previous graphic state
  14530. $this->_out($this->epsmarker.'Q');
  14531. if (!empty($border)) {
  14532. $bx = $this->x;
  14533. $by = $this->y;
  14534. $this->x = $ximg;
  14535. if ($this->rtl) {
  14536. $this->x += $w;
  14537. }
  14538. $this->y = $y;
  14539. $this->Cell($w, $h, '', $border, 0, '', 0, '', 0, true);
  14540. $this->x = $bx;
  14541. $this->y = $by;
  14542. }
  14543. if ($link) {
  14544. $this->Link($ximg, $y, $w, $h, $link, 0);
  14545. }
  14546. // set pointer to align the next text/objects
  14547. switch($align) {
  14548. case 'T':{
  14549. $this->y = $y;
  14550. $this->x = $this->img_rb_x;
  14551. break;
  14552. }
  14553. case 'M':{
  14554. $this->y = $y + round($h/2);
  14555. $this->x = $this->img_rb_x;
  14556. break;
  14557. }
  14558. case 'B':{
  14559. $this->y = $this->img_rb_y;
  14560. $this->x = $this->img_rb_x;
  14561. break;
  14562. }
  14563. case 'N':{
  14564. $this->SetY($this->img_rb_y);
  14565. break;
  14566. }
  14567. default:{
  14568. break;
  14569. }
  14570. }
  14571. $this->endlinex = $this->img_rb_x;
  14572. }
  14573. /**
  14574. * Set document barcode.
  14575. * @param $bc (string) barcode
  14576. * @public
  14577. */
  14578. public function setBarcode($bc='') {
  14579. $this->barcode = $bc;
  14580. }
  14581. /**
  14582. * Get current barcode.
  14583. * @return string
  14584. * @public
  14585. * @since 4.0.012 (2008-07-24)
  14586. */
  14587. public function getBarcode() {
  14588. return $this->barcode;
  14589. }
  14590. /**
  14591. * Print a Linear Barcode.
  14592. * @param $code (string) code to print
  14593. * @param $type (string) type of barcode (see tcpdf_barcodes_1d.php for supported formats).
  14594. * @param $x (int) x position in user units (empty string = current x position)
  14595. * @param $y (int) y position in user units (empty string = current y position)
  14596. * @param $w (int) width in user units (empty string = remaining page width)
  14597. * @param $h (int) height in user units (empty string = remaining page height)
  14598. * @param $xres (float) width of the smallest bar in user units (empty string = default value = 0.4mm)
  14599. * @param $style (array) array of options:<ul>
  14600. * <li>boolean $style['border'] if true prints a border</li>
  14601. * <li>int $style['padding'] padding to leave around the barcode in user units (set to 'auto' for automatic padding)</li>
  14602. * <li>int $style['hpadding'] horizontal padding in user units (set to 'auto' for automatic padding)</li>
  14603. * <li>int $style['vpadding'] vertical padding in user units (set to 'auto' for automatic padding)</li>
  14604. * <li>array $style['fgcolor'] color array for bars and text</li>
  14605. * <li>mixed $style['bgcolor'] color array for background (set to false for transparent)</li>
  14606. * <li>boolean $style['text'] if true prints text below the barcode</li>
  14607. * <li>string $style['label'] override default label</li>
  14608. * <li>string $style['font'] font name for text</li><li>int $style['fontsize'] font size for text</li>
  14609. * <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>
  14610. * <li>string $style['position'] horizontal position of the containing barcode cell on the page: L = left margin; C = center; R = right margin.</li>
  14611. * <li>string $style['align'] horizontal position of the barcode on the containing rectangle: L = left; C = center; R = right.</li>
  14612. * <li>string $style['stretch'] if true stretch the barcode to best fit the available width, otherwise uses $xres resolution for a single bar.</li>
  14613. * <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>
  14614. * <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>
  14615. * @param $align (string) 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>
  14616. * @author Nicola Asuni
  14617. * @since 3.1.000 (2008-06-09)
  14618. * @public
  14619. */
  14620. public function write1DBarcode($code, $type, $x='', $y='', $w='', $h='', $xres='', $style=array(), $align='') {
  14621. if (TCPDF_STATIC::empty_string(trim($code))) {
  14622. return;
  14623. }
  14624. require_once(dirname(__FILE__).'/tcpdf_barcodes_1d.php');
  14625. // save current graphic settings
  14626. $gvars = $this->getGraphicVars();
  14627. // create new barcode object
  14628. $barcodeobj = new TCPDFBarcode($code, $type);
  14629. $arrcode = $barcodeobj->getBarcodeArray();
  14630. if (($arrcode === false) OR empty($arrcode) OR ($arrcode['maxw'] <= 0)) {
  14631. $this->Error('Error in 1D barcode string');
  14632. }
  14633. if ($arrcode['maxh'] <= 0) {
  14634. $arrcode['maxh'] = 1;
  14635. }
  14636. // set default values
  14637. if (!isset($style['position'])) {
  14638. $style['position'] = '';
  14639. } elseif ($style['position'] == 'S') {
  14640. // keep this for backward compatibility
  14641. $style['position'] = '';
  14642. $style['stretch'] = true;
  14643. }
  14644. if (!isset($style['fitwidth'])) {
  14645. if (!isset($style['stretch'])) {
  14646. $style['fitwidth'] = true;
  14647. } else {
  14648. $style['fitwidth'] = false;
  14649. }
  14650. }
  14651. if ($style['fitwidth']) {
  14652. // disable stretch
  14653. $style['stretch'] = false;
  14654. }
  14655. if (!isset($style['stretch'])) {
  14656. if (($w === '') OR ($w <= 0)) {
  14657. $style['stretch'] = false;
  14658. } else {
  14659. $style['stretch'] = true;
  14660. }
  14661. }
  14662. if (!isset($style['fgcolor'])) {
  14663. $style['fgcolor'] = array(0,0,0); // default black
  14664. }
  14665. if (!isset($style['bgcolor'])) {
  14666. $style['bgcolor'] = false; // default transparent
  14667. }
  14668. if (!isset($style['border'])) {
  14669. $style['border'] = false;
  14670. }
  14671. $fontsize = 0;
  14672. if (!isset($style['text'])) {
  14673. $style['text'] = false;
  14674. }
  14675. if ($style['text'] AND isset($style['font'])) {
  14676. if (isset($style['fontsize'])) {
  14677. $fontsize = $style['fontsize'];
  14678. }
  14679. $this->SetFont($style['font'], '', $fontsize);
  14680. }
  14681. if (!isset($style['stretchtext'])) {
  14682. $style['stretchtext'] = 4;
  14683. }
  14684. if ($x === '') {
  14685. $x = $this->x;
  14686. }
  14687. if ($y === '') {
  14688. $y = $this->y;
  14689. }
  14690. // check page for no-write regions and adapt page margins if necessary
  14691. list($x, $y) = $this->checkPageRegions($h, $x, $y);
  14692. if (($w === '') OR ($w <= 0)) {
  14693. if ($this->rtl) {
  14694. $w = $x - $this->lMargin;
  14695. } else {
  14696. $w = $this->w - $this->rMargin - $x;
  14697. }
  14698. }
  14699. // padding
  14700. if (!isset($style['padding'])) {
  14701. $padding = 0;
  14702. } elseif ($style['padding'] === 'auto') {
  14703. $padding = 10 * ($w / ($arrcode['maxw'] + 20));
  14704. } else {
  14705. $padding = floatval($style['padding']);
  14706. }
  14707. // horizontal padding
  14708. if (!isset($style['hpadding'])) {
  14709. $hpadding = $padding;
  14710. } elseif ($style['hpadding'] === 'auto') {
  14711. $hpadding = 10 * ($w / ($arrcode['maxw'] + 20));
  14712. } else {
  14713. $hpadding = floatval($style['hpadding']);
  14714. }
  14715. // vertical padding
  14716. if (!isset($style['vpadding'])) {
  14717. $vpadding = $padding;
  14718. } elseif ($style['vpadding'] === 'auto') {
  14719. $vpadding = ($hpadding / 2);
  14720. } else {
  14721. $vpadding = floatval($style['vpadding']);
  14722. }
  14723. // calculate xres (single bar width)
  14724. $max_xres = ($w - (2 * $hpadding)) / $arrcode['maxw'];
  14725. if ($style['stretch']) {
  14726. $xres = $max_xres;
  14727. } else {
  14728. if (TCPDF_STATIC::empty_string($xres)) {
  14729. $xres = (0.141 * $this->k); // default bar width = 0.4 mm
  14730. }
  14731. if ($xres > $max_xres) {
  14732. // correct xres to fit on $w
  14733. $xres = $max_xres;
  14734. }
  14735. if ((isset($style['padding']) AND ($style['padding'] === 'auto'))
  14736. OR (isset($style['hpadding']) AND ($style['hpadding'] === 'auto'))) {
  14737. $hpadding = 10 * $xres;
  14738. if (isset($style['vpadding']) AND ($style['vpadding'] === 'auto')) {
  14739. $vpadding = ($hpadding / 2);
  14740. }
  14741. }
  14742. }
  14743. if ($style['fitwidth']) {
  14744. $wold = $w;
  14745. $w = (($arrcode['maxw'] * $xres) + (2 * $hpadding));
  14746. if (isset($style['cellfitalign'])) {
  14747. switch ($style['cellfitalign']) {
  14748. case 'L': {
  14749. if ($this->rtl) {
  14750. $x -= ($wold - $w);
  14751. }
  14752. break;
  14753. }
  14754. case 'R': {
  14755. if (!$this->rtl) {
  14756. $x += ($wold - $w);
  14757. }
  14758. break;
  14759. }
  14760. case 'C': {
  14761. if ($this->rtl) {
  14762. $x -= (($wold - $w) / 2);
  14763. } else {
  14764. $x += (($wold - $w) / 2);
  14765. }
  14766. break;
  14767. }
  14768. default : {
  14769. break;
  14770. }
  14771. }
  14772. }
  14773. }
  14774. $text_height = $this->getCellHeight($fontsize / $this->k);
  14775. // height
  14776. if (($h === '') OR ($h <= 0)) {
  14777. // set default height
  14778. $h = (($arrcode['maxw'] * $xres) / 3) + (2 * $vpadding) + $text_height;
  14779. }
  14780. $barh = $h - $text_height - (2 * $vpadding);
  14781. if ($barh <=0) {
  14782. // try to reduce font or padding to fit barcode on available height
  14783. if ($text_height > $h) {
  14784. $fontsize = (($h * $this->k) / (4 * $this->cell_height_ratio));
  14785. $text_height = $this->getCellHeight($fontsize / $this->k);
  14786. $this->SetFont($style['font'], '', $fontsize);
  14787. }
  14788. if ($vpadding > 0) {
  14789. $vpadding = (($h - $text_height) / 4);
  14790. }
  14791. $barh = $h - $text_height - (2 * $vpadding);
  14792. }
  14793. // fit the barcode on available space
  14794. list($w, $h, $x, $y) = $this->fitBlock($w, $h, $x, $y, false);
  14795. // set alignment
  14796. $this->img_rb_y = $y + $h;
  14797. // set alignment
  14798. if ($this->rtl) {
  14799. if ($style['position'] == 'L') {
  14800. $xpos = $this->lMargin;
  14801. } elseif ($style['position'] == 'C') {
  14802. $xpos = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
  14803. } elseif ($style['position'] == 'R') {
  14804. $xpos = $this->w - $this->rMargin - $w;
  14805. } else {
  14806. $xpos = $x - $w;
  14807. }
  14808. $this->img_rb_x = $xpos;
  14809. } else {
  14810. if ($style['position'] == 'L') {
  14811. $xpos = $this->lMargin;
  14812. } elseif ($style['position'] == 'C') {
  14813. $xpos = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
  14814. } elseif ($style['position'] == 'R') {
  14815. $xpos = $this->w - $this->rMargin - $w;
  14816. } else {
  14817. $xpos = $x;
  14818. }
  14819. $this->img_rb_x = $xpos + $w;
  14820. }
  14821. $xpos_rect = $xpos;
  14822. if (!isset($style['align'])) {
  14823. $style['align'] = 'C';
  14824. }
  14825. switch ($style['align']) {
  14826. case 'L': {
  14827. $xpos = $xpos_rect + $hpadding;
  14828. break;
  14829. }
  14830. case 'R': {
  14831. $xpos = $xpos_rect + ($w - ($arrcode['maxw'] * $xres)) - $hpadding;
  14832. break;
  14833. }
  14834. case 'C':
  14835. default : {
  14836. $xpos = $xpos_rect + (($w - ($arrcode['maxw'] * $xres)) / 2);
  14837. break;
  14838. }
  14839. }
  14840. $xpos_text = $xpos;
  14841. // barcode is always printed in LTR direction
  14842. $tempRTL = $this->rtl;
  14843. $this->rtl = false;
  14844. // print background color
  14845. if ($style['bgcolor']) {
  14846. $this->Rect($xpos_rect, $y, $w, $h, $style['border'] ? 'DF' : 'F', '', $style['bgcolor']);
  14847. } elseif ($style['border']) {
  14848. $this->Rect($xpos_rect, $y, $w, $h, 'D');
  14849. }
  14850. // set foreground color
  14851. $this->SetDrawColorArray($style['fgcolor']);
  14852. $this->SetTextColorArray($style['fgcolor']);
  14853. // print bars
  14854. foreach ($arrcode['bcode'] as $k => $v) {
  14855. $bw = ($v['w'] * $xres);
  14856. if ($v['t']) {
  14857. // draw a vertical bar
  14858. $ypos = $y + $vpadding + ($v['p'] * $barh / $arrcode['maxh']);
  14859. $this->Rect($xpos, $ypos, $bw, ($v['h'] * $barh / $arrcode['maxh']), 'F', array(), $style['fgcolor']);
  14860. }
  14861. $xpos += $bw;
  14862. }
  14863. // print text
  14864. if ($style['text']) {
  14865. if (isset($style['label']) AND !TCPDF_STATIC::empty_string($style['label'])) {
  14866. $label = $style['label'];
  14867. } else {
  14868. $label = $code;
  14869. }
  14870. $txtwidth = ($arrcode['maxw'] * $xres);
  14871. if ($this->GetStringWidth($label) > $txtwidth) {
  14872. $style['stretchtext'] = 2;
  14873. }
  14874. // print text
  14875. $this->x = $xpos_text;
  14876. $this->y = $y + $vpadding + $barh;
  14877. $cellpadding = $this->cell_padding;
  14878. $this->SetCellPadding(0);
  14879. $this->Cell($txtwidth, '', $label, 0, 0, 'C', false, '', $style['stretchtext'], false, 'T', 'T');
  14880. $this->cell_padding = $cellpadding;
  14881. }
  14882. // restore original direction
  14883. $this->rtl = $tempRTL;
  14884. // restore previous settings
  14885. $this->setGraphicVars($gvars);
  14886. // set pointer to align the next text/objects
  14887. switch($align) {
  14888. case 'T':{
  14889. $this->y = $y;
  14890. $this->x = $this->img_rb_x;
  14891. break;
  14892. }
  14893. case 'M':{
  14894. $this->y = $y + round($h / 2);
  14895. $this->x = $this->img_rb_x;
  14896. break;
  14897. }
  14898. case 'B':{
  14899. $this->y = $this->img_rb_y;
  14900. $this->x = $this->img_rb_x;
  14901. break;
  14902. }
  14903. case 'N':{
  14904. $this->SetY($this->img_rb_y);
  14905. break;
  14906. }
  14907. default:{
  14908. break;
  14909. }
  14910. }
  14911. $this->endlinex = $this->img_rb_x;
  14912. }
  14913. /**
  14914. * Print 2D Barcode.
  14915. * @param $code (string) code to print
  14916. * @param $type (string) type of barcode (see tcpdf_barcodes_2d.php for supported formats).
  14917. * @param $x (int) x position in user units
  14918. * @param $y (int) y position in user units
  14919. * @param $w (int) width in user units
  14920. * @param $h (int) height in user units
  14921. * @param $style (array) array of options:<ul>
  14922. * <li>boolean $style['border'] if true prints a border around the barcode</li>
  14923. * <li>int $style['padding'] padding to leave around the barcode in barcode units (set to 'auto' for automatic padding)</li>
  14924. * <li>int $style['hpadding'] horizontal padding in barcode units (set to 'auto' for automatic padding)</li>
  14925. * <li>int $style['vpadding'] vertical padding in barcode units (set to 'auto' for automatic padding)</li>
  14926. * <li>int $style['module_width'] width of a single module in points</li>
  14927. * <li>int $style['module_height'] height of a single module in points</li>
  14928. * <li>array $style['fgcolor'] color array for bars and text</li>
  14929. * <li>mixed $style['bgcolor'] color array for background or false for transparent</li>
  14930. * <li>string $style['position'] barcode position on the page: L = left margin; C = center; R = right margin; S = stretch</li><li>$style['module_width'] width of a single module in points</li>
  14931. * <li>$style['module_height'] height of a single module in points</li></ul>
  14932. * @param $align (string) 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>
  14933. * @param $distort (boolean) if true distort the barcode to fit width and height, otherwise preserve aspect ratio
  14934. * @author Nicola Asuni
  14935. * @since 4.5.037 (2009-04-07)
  14936. * @public
  14937. */
  14938. public function write2DBarcode($code, $type, $x='', $y='', $w='', $h='', $style=array(), $align='', $distort=false) {
  14939. if (TCPDF_STATIC::empty_string(trim($code))) {
  14940. return;
  14941. }
  14942. require_once(dirname(__FILE__).'/tcpdf_barcodes_2d.php');
  14943. // save current graphic settings
  14944. $gvars = $this->getGraphicVars();
  14945. // create new barcode object
  14946. $barcodeobj = new TCPDF2DBarcode($code, $type);
  14947. $arrcode = $barcodeobj->getBarcodeArray();
  14948. if (($arrcode === false) OR empty($arrcode) OR !isset($arrcode['num_rows']) OR ($arrcode['num_rows'] == 0) OR !isset($arrcode['num_cols']) OR ($arrcode['num_cols'] == 0)) {
  14949. $this->Error('Error in 2D barcode string');
  14950. }
  14951. // set default values
  14952. if (!isset($style['position'])) {
  14953. $style['position'] = '';
  14954. }
  14955. if (!isset($style['fgcolor'])) {
  14956. $style['fgcolor'] = array(0,0,0); // default black
  14957. }
  14958. if (!isset($style['bgcolor'])) {
  14959. $style['bgcolor'] = false; // default transparent
  14960. }
  14961. if (!isset($style['border'])) {
  14962. $style['border'] = false;
  14963. }
  14964. // padding
  14965. if (!isset($style['padding'])) {
  14966. $style['padding'] = 0;
  14967. } elseif ($style['padding'] === 'auto') {
  14968. $style['padding'] = 4;
  14969. }
  14970. if (!isset($style['hpadding'])) {
  14971. $style['hpadding'] = $style['padding'];
  14972. } elseif ($style['hpadding'] === 'auto') {
  14973. $style['hpadding'] = 4;
  14974. }
  14975. if (!isset($style['vpadding'])) {
  14976. $style['vpadding'] = $style['padding'];
  14977. } elseif ($style['vpadding'] === 'auto') {
  14978. $style['vpadding'] = 4;
  14979. }
  14980. $hpad = (2 * $style['hpadding']);
  14981. $vpad = (2 * $style['vpadding']);
  14982. // cell (module) dimension
  14983. if (!isset($style['module_width'])) {
  14984. $style['module_width'] = 1; // width of a single module in points
  14985. }
  14986. if (!isset($style['module_height'])) {
  14987. $style['module_height'] = 1; // height of a single module in points
  14988. }
  14989. if ($x === '') {
  14990. $x = $this->x;
  14991. }
  14992. if ($y === '') {
  14993. $y = $this->y;
  14994. }
  14995. // check page for no-write regions and adapt page margins if necessary
  14996. list($x, $y) = $this->checkPageRegions($h, $x, $y);
  14997. // number of barcode columns and rows
  14998. $rows = $arrcode['num_rows'];
  14999. $cols = $arrcode['num_cols'];
  15000. if (($rows <= 0) || ($cols <= 0)){
  15001. $this->Error('Error in 2D barcode string');
  15002. }
  15003. // module width and height
  15004. $mw = $style['module_width'];
  15005. $mh = $style['module_height'];
  15006. if (($mw <= 0) OR ($mh <= 0)) {
  15007. $this->Error('Error in 2D barcode string');
  15008. }
  15009. // get max dimensions
  15010. if ($this->rtl) {
  15011. $maxw = $x - $this->lMargin;
  15012. } else {
  15013. $maxw = $this->w - $this->rMargin - $x;
  15014. }
  15015. $maxh = ($this->h - $this->tMargin - $this->bMargin);
  15016. $ratioHW = ((($rows * $mh) + $hpad) / (($cols * $mw) + $vpad));
  15017. $ratioWH = ((($cols * $mw) + $vpad) / (($rows * $mh) + $hpad));
  15018. if (!$distort) {
  15019. if (($maxw * $ratioHW) > $maxh) {
  15020. $maxw = $maxh * $ratioWH;
  15021. }
  15022. if (($maxh * $ratioWH) > $maxw) {
  15023. $maxh = $maxw * $ratioHW;
  15024. }
  15025. }
  15026. // set maximum dimensions
  15027. if ($w > $maxw) {
  15028. $w = $maxw;
  15029. }
  15030. if ($h > $maxh) {
  15031. $h = $maxh;
  15032. }
  15033. // set dimensions
  15034. if ((($w === '') OR ($w <= 0)) AND (($h === '') OR ($h <= 0))) {
  15035. $w = ($cols + $hpad) * ($mw / $this->k);
  15036. $h = ($rows + $vpad) * ($mh / $this->k);
  15037. } elseif (($w === '') OR ($w <= 0)) {
  15038. $w = $h * $ratioWH;
  15039. } elseif (($h === '') OR ($h <= 0)) {
  15040. $h = $w * $ratioHW;
  15041. }
  15042. // barcode size (excluding padding)
  15043. $bw = ($w * $cols) / ($cols + $hpad);
  15044. $bh = ($h * $rows) / ($rows + $vpad);
  15045. // dimension of single barcode cell unit
  15046. $cw = $bw / $cols;
  15047. $ch = $bh / $rows;
  15048. if (!$distort) {
  15049. if (($cw / $ch) > ($mw / $mh)) {
  15050. // correct horizontal distortion
  15051. $cw = $ch * $mw / $mh;
  15052. $bw = $cw * $cols;
  15053. $style['hpadding'] = ($w - $bw) / (2 * $cw);
  15054. } else {
  15055. // correct vertical distortion
  15056. $ch = $cw * $mh / $mw;
  15057. $bh = $ch * $rows;
  15058. $style['vpadding'] = ($h - $bh) / (2 * $ch);
  15059. }
  15060. }
  15061. // fit the barcode on available space
  15062. list($w, $h, $x, $y) = $this->fitBlock($w, $h, $x, $y, false);
  15063. // set alignment
  15064. $this->img_rb_y = $y + $h;
  15065. // set alignment
  15066. if ($this->rtl) {
  15067. if ($style['position'] == 'L') {
  15068. $xpos = $this->lMargin;
  15069. } elseif ($style['position'] == 'C') {
  15070. $xpos = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
  15071. } elseif ($style['position'] == 'R') {
  15072. $xpos = $this->w - $this->rMargin - $w;
  15073. } else {
  15074. $xpos = $x - $w;
  15075. }
  15076. $this->img_rb_x = $xpos;
  15077. } else {
  15078. if ($style['position'] == 'L') {
  15079. $xpos = $this->lMargin;
  15080. } elseif ($style['position'] == 'C') {
  15081. $xpos = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
  15082. } elseif ($style['position'] == 'R') {
  15083. $xpos = $this->w - $this->rMargin - $w;
  15084. } else {
  15085. $xpos = $x;
  15086. }
  15087. $this->img_rb_x = $xpos + $w;
  15088. }
  15089. $xstart = $xpos + ($style['hpadding'] * $cw);
  15090. $ystart = $y + ($style['vpadding'] * $ch);
  15091. // barcode is always printed in LTR direction
  15092. $tempRTL = $this->rtl;
  15093. $this->rtl = false;
  15094. // print background color
  15095. if ($style['bgcolor']) {
  15096. $this->Rect($xpos, $y, $w, $h, $style['border'] ? 'DF' : 'F', '', $style['bgcolor']);
  15097. } elseif ($style['border']) {
  15098. $this->Rect($xpos, $y, $w, $h, 'D');
  15099. }
  15100. // set foreground color
  15101. $this->SetDrawColorArray($style['fgcolor']);
  15102. // print barcode cells
  15103. // for each row
  15104. for ($r = 0; $r < $rows; ++$r) {
  15105. $xr = $xstart;
  15106. // for each column
  15107. for ($c = 0; $c < $cols; ++$c) {
  15108. if ($arrcode['bcode'][$r][$c] == 1) {
  15109. // draw a single barcode cell
  15110. $this->Rect($xr, $ystart, $cw, $ch, 'F', array(), $style['fgcolor']);
  15111. }
  15112. $xr += $cw;
  15113. }
  15114. $ystart += $ch;
  15115. }
  15116. // restore original direction
  15117. $this->rtl = $tempRTL;
  15118. // restore previous settings
  15119. $this->setGraphicVars($gvars);
  15120. // set pointer to align the next text/objects
  15121. switch($align) {
  15122. case 'T':{
  15123. $this->y = $y;
  15124. $this->x = $this->img_rb_x;
  15125. break;
  15126. }
  15127. case 'M':{
  15128. $this->y = $y + round($h/2);
  15129. $this->x = $this->img_rb_x;
  15130. break;
  15131. }
  15132. case 'B':{
  15133. $this->y = $this->img_rb_y;
  15134. $this->x = $this->img_rb_x;
  15135. break;
  15136. }
  15137. case 'N':{
  15138. $this->SetY($this->img_rb_y);
  15139. break;
  15140. }
  15141. default:{
  15142. break;
  15143. }
  15144. }
  15145. $this->endlinex = $this->img_rb_x;
  15146. }
  15147. /**
  15148. * Returns an array containing current margins:
  15149. * <ul>
  15150. <li>$ret['left'] = left margin</li>
  15151. <li>$ret['right'] = right margin</li>
  15152. <li>$ret['top'] = top margin</li>
  15153. <li>$ret['bottom'] = bottom margin</li>
  15154. <li>$ret['header'] = header margin</li>
  15155. <li>$ret['footer'] = footer margin</li>
  15156. <li>$ret['cell'] = cell padding array</li>
  15157. <li>$ret['padding_left'] = cell left padding</li>
  15158. <li>$ret['padding_top'] = cell top padding</li>
  15159. <li>$ret['padding_right'] = cell right padding</li>
  15160. <li>$ret['padding_bottom'] = cell bottom padding</li>
  15161. * </ul>
  15162. * @return array containing all margins measures
  15163. * @public
  15164. * @since 3.2.000 (2008-06-23)
  15165. */
  15166. public function getMargins() {
  15167. $ret = array(
  15168. 'left' => $this->lMargin,
  15169. 'right' => $this->rMargin,
  15170. 'top' => $this->tMargin,
  15171. 'bottom' => $this->bMargin,
  15172. 'header' => $this->header_margin,
  15173. 'footer' => $this->footer_margin,
  15174. 'cell' => $this->cell_padding,
  15175. 'padding_left' => $this->cell_padding['L'],
  15176. 'padding_top' => $this->cell_padding['T'],
  15177. 'padding_right' => $this->cell_padding['R'],
  15178. 'padding_bottom' => $this->cell_padding['B']
  15179. );
  15180. return $ret;
  15181. }
  15182. /**
  15183. * Returns an array containing original margins:
  15184. * <ul>
  15185. <li>$ret['left'] = left margin</li>
  15186. <li>$ret['right'] = right margin</li>
  15187. * </ul>
  15188. * @return array containing all margins measures
  15189. * @public
  15190. * @since 4.0.012 (2008-07-24)
  15191. */
  15192. public function getOriginalMargins() {
  15193. $ret = array(
  15194. 'left' => $this->original_lMargin,
  15195. 'right' => $this->original_rMargin
  15196. );
  15197. return $ret;
  15198. }
  15199. /**
  15200. * Returns the current font size.
  15201. * @return current font size
  15202. * @public
  15203. * @since 3.2.000 (2008-06-23)
  15204. */
  15205. public function getFontSize() {
  15206. return $this->FontSize;
  15207. }
  15208. /**
  15209. * Returns the current font size in points unit.
  15210. * @return current font size in points unit
  15211. * @public
  15212. * @since 3.2.000 (2008-06-23)
  15213. */
  15214. public function getFontSizePt() {
  15215. return $this->FontSizePt;
  15216. }
  15217. /**
  15218. * Returns the current font family name.
  15219. * @return string current font family name
  15220. * @public
  15221. * @since 4.3.008 (2008-12-05)
  15222. */
  15223. public function getFontFamily() {
  15224. return $this->FontFamily;
  15225. }
  15226. /**
  15227. * Returns the current font style.
  15228. * @return string current font style
  15229. * @public
  15230. * @since 4.3.008 (2008-12-05)
  15231. */
  15232. public function getFontStyle() {
  15233. return $this->FontStyle;
  15234. }
  15235. /**
  15236. * Cleanup HTML code (requires HTML Tidy library).
  15237. * @param $html (string) htmlcode to fix
  15238. * @param $default_css (string) CSS commands to add
  15239. * @param $tagvs (array) parameters for setHtmlVSpace method
  15240. * @param $tidy_options (array) options for tidy_parse_string function
  15241. * @return string XHTML code cleaned up
  15242. * @author Nicola Asuni
  15243. * @public
  15244. * @since 5.9.017 (2010-11-16)
  15245. * @see setHtmlVSpace()
  15246. */
  15247. public function fixHTMLCode($html, $default_css='', $tagvs='', $tidy_options='') {
  15248. return TCPDF_STATIC::fixHTMLCode($html, $default_css, $tagvs, $tidy_options, $this->tagvspaces);
  15249. }
  15250. /**
  15251. * Returns the border width from CSS property
  15252. * @param $width (string) border width
  15253. * @return int with in user units
  15254. * @protected
  15255. * @since 5.7.000 (2010-08-02)
  15256. */
  15257. protected function getCSSBorderWidth($width) {
  15258. if ($width == 'thin') {
  15259. $width = (2 / $this->k);
  15260. } elseif ($width == 'medium') {
  15261. $width = (4 / $this->k);
  15262. } elseif ($width == 'thick') {
  15263. $width = (6 / $this->k);
  15264. } else {
  15265. $width = $this->getHTMLUnitToUnits($width, 1, 'px', false);
  15266. }
  15267. return $width;
  15268. }
  15269. /**
  15270. * Returns the border dash style from CSS property
  15271. * @param $style (string) border style to convert
  15272. * @return int sash style (return -1 in case of none or hidden border)
  15273. * @protected
  15274. * @since 5.7.000 (2010-08-02)
  15275. */
  15276. protected function getCSSBorderDashStyle($style) {
  15277. switch (strtolower($style)) {
  15278. case 'none':
  15279. case 'hidden': {
  15280. $dash = -1;
  15281. break;
  15282. }
  15283. case 'dotted': {
  15284. $dash = 1;
  15285. break;
  15286. }
  15287. case 'dashed': {
  15288. $dash = 3;
  15289. break;
  15290. }
  15291. case 'double':
  15292. case 'groove':
  15293. case 'ridge':
  15294. case 'inset':
  15295. case 'outset':
  15296. case 'solid':
  15297. default: {
  15298. $dash = 0;
  15299. break;
  15300. }
  15301. }
  15302. return $dash;
  15303. }
  15304. /**
  15305. * Returns the border style array from CSS border properties
  15306. * @param $cssborder (string) border properties
  15307. * @return array containing border properties
  15308. * @protected
  15309. * @since 5.7.000 (2010-08-02)
  15310. */
  15311. protected function getCSSBorderStyle($cssborder) {
  15312. $bprop = preg_split('/[\s]+/', trim($cssborder));
  15313. $border = array(); // value to be returned
  15314. switch (count($bprop)) {
  15315. case 3: {
  15316. $width = $bprop[0];
  15317. $style = $bprop[1];
  15318. $color = $bprop[2];
  15319. break;
  15320. }
  15321. case 2: {
  15322. $width = 'medium';
  15323. $style = $bprop[0];
  15324. $color = $bprop[1];
  15325. break;
  15326. }
  15327. case 1: {
  15328. $width = 'medium';
  15329. $style = $bprop[0];
  15330. $color = 'black';
  15331. break;
  15332. }
  15333. default: {
  15334. $width = 'medium';
  15335. $style = 'solid';
  15336. $color = 'black';
  15337. break;
  15338. }
  15339. }
  15340. if ($style == 'none') {
  15341. return array();
  15342. }
  15343. $border['cap'] = 'square';
  15344. $border['join'] = 'miter';
  15345. $border['dash'] = $this->getCSSBorderDashStyle($style);
  15346. if ($border['dash'] < 0) {
  15347. return array();
  15348. }
  15349. $border['width'] = $this->getCSSBorderWidth($width);
  15350. $border['color'] = TCPDF_COLORS::convertHTMLColorToDec($color, $this->spot_colors);
  15351. return $border;
  15352. }
  15353. /**
  15354. * Get the internal Cell padding from CSS attribute.
  15355. * @param $csspadding (string) padding properties
  15356. * @param $width (float) width of the containing element
  15357. * @return array of cell paddings
  15358. * @public
  15359. * @since 5.9.000 (2010-10-04)
  15360. */
  15361. public function getCSSPadding($csspadding, $width=0) {
  15362. $padding = preg_split('/[\s]+/', trim($csspadding));
  15363. $cell_padding = array(); // value to be returned
  15364. switch (count($padding)) {
  15365. case 4: {
  15366. $cell_padding['T'] = $padding[0];
  15367. $cell_padding['R'] = $padding[1];
  15368. $cell_padding['B'] = $padding[2];
  15369. $cell_padding['L'] = $padding[3];
  15370. break;
  15371. }
  15372. case 3: {
  15373. $cell_padding['T'] = $padding[0];
  15374. $cell_padding['R'] = $padding[1];
  15375. $cell_padding['B'] = $padding[2];
  15376. $cell_padding['L'] = $padding[1];
  15377. break;
  15378. }
  15379. case 2: {
  15380. $cell_padding['T'] = $padding[0];
  15381. $cell_padding['R'] = $padding[1];
  15382. $cell_padding['B'] = $padding[0];
  15383. $cell_padding['L'] = $padding[1];
  15384. break;
  15385. }
  15386. case 1: {
  15387. $cell_padding['T'] = $padding[0];
  15388. $cell_padding['R'] = $padding[0];
  15389. $cell_padding['B'] = $padding[0];
  15390. $cell_padding['L'] = $padding[0];
  15391. break;
  15392. }
  15393. default: {
  15394. return $this->cell_padding;
  15395. }
  15396. }
  15397. if ($width == 0) {
  15398. $width = $this->w - $this->lMargin - $this->rMargin;
  15399. }
  15400. $cell_padding['T'] = $this->getHTMLUnitToUnits($cell_padding['T'], $width, 'px', false);
  15401. $cell_padding['R'] = $this->getHTMLUnitToUnits($cell_padding['R'], $width, 'px', false);
  15402. $cell_padding['B'] = $this->getHTMLUnitToUnits($cell_padding['B'], $width, 'px', false);
  15403. $cell_padding['L'] = $this->getHTMLUnitToUnits($cell_padding['L'], $width, 'px', false);
  15404. return $cell_padding;
  15405. }
  15406. /**
  15407. * Get the internal Cell margin from CSS attribute.
  15408. * @param $cssmargin (string) margin properties
  15409. * @param $width (float) width of the containing element
  15410. * @return array of cell margins
  15411. * @public
  15412. * @since 5.9.000 (2010-10-04)
  15413. */
  15414. public function getCSSMargin($cssmargin, $width=0) {
  15415. $margin = preg_split('/[\s]+/', trim($cssmargin));
  15416. $cell_margin = array(); // value to be returned
  15417. switch (count($margin)) {
  15418. case 4: {
  15419. $cell_margin['T'] = $margin[0];
  15420. $cell_margin['R'] = $margin[1];
  15421. $cell_margin['B'] = $margin[2];
  15422. $cell_margin['L'] = $margin[3];
  15423. break;
  15424. }
  15425. case 3: {
  15426. $cell_margin['T'] = $margin[0];
  15427. $cell_margin['R'] = $margin[1];
  15428. $cell_margin['B'] = $margin[2];
  15429. $cell_margin['L'] = $margin[1];
  15430. break;
  15431. }
  15432. case 2: {
  15433. $cell_margin['T'] = $margin[0];
  15434. $cell_margin['R'] = $margin[1];
  15435. $cell_margin['B'] = $margin[0];
  15436. $cell_margin['L'] = $margin[1];
  15437. break;
  15438. }
  15439. case 1: {
  15440. $cell_margin['T'] = $margin[0];
  15441. $cell_margin['R'] = $margin[0];
  15442. $cell_margin['B'] = $margin[0];
  15443. $cell_margin['L'] = $margin[0];
  15444. break;
  15445. }
  15446. default: {
  15447. return $this->cell_margin;
  15448. }
  15449. }
  15450. if ($width == 0) {
  15451. $width = $this->w - $this->lMargin - $this->rMargin;
  15452. }
  15453. $cell_margin['T'] = $this->getHTMLUnitToUnits(str_replace('auto', '0', $cell_margin['T']), $width, 'px', false);
  15454. $cell_margin['R'] = $this->getHTMLUnitToUnits(str_replace('auto', '0', $cell_margin['R']), $width, 'px', false);
  15455. $cell_margin['B'] = $this->getHTMLUnitToUnits(str_replace('auto', '0', $cell_margin['B']), $width, 'px', false);
  15456. $cell_margin['L'] = $this->getHTMLUnitToUnits(str_replace('auto', '0', $cell_margin['L']), $width, 'px', false);
  15457. return $cell_margin;
  15458. }
  15459. /**
  15460. * Get the border-spacing from CSS attribute.
  15461. * @param $cssbspace (string) border-spacing CSS properties
  15462. * @param $width (float) width of the containing element
  15463. * @return array of border spacings
  15464. * @public
  15465. * @since 5.9.010 (2010-10-27)
  15466. */
  15467. public function getCSSBorderMargin($cssbspace, $width=0) {
  15468. $space = preg_split('/[\s]+/', trim($cssbspace));
  15469. $border_spacing = array(); // value to be returned
  15470. switch (count($space)) {
  15471. case 2: {
  15472. $border_spacing['H'] = $space[0];
  15473. $border_spacing['V'] = $space[1];
  15474. break;
  15475. }
  15476. case 1: {
  15477. $border_spacing['H'] = $space[0];
  15478. $border_spacing['V'] = $space[0];
  15479. break;
  15480. }
  15481. default: {
  15482. return array('H' => 0, 'V' => 0);
  15483. }
  15484. }
  15485. if ($width == 0) {
  15486. $width = $this->w - $this->lMargin - $this->rMargin;
  15487. }
  15488. $border_spacing['H'] = $this->getHTMLUnitToUnits($border_spacing['H'], $width, 'px', false);
  15489. $border_spacing['V'] = $this->getHTMLUnitToUnits($border_spacing['V'], $width, 'px', false);
  15490. return $border_spacing;
  15491. }
  15492. /**
  15493. * Returns the letter-spacing value from CSS value
  15494. * @param $spacing (string) letter-spacing value
  15495. * @param $parent (float) font spacing (tracking) value of the parent element
  15496. * @return float quantity to increases or decreases the space between characters in a text.
  15497. * @protected
  15498. * @since 5.9.000 (2010-10-02)
  15499. */
  15500. protected function getCSSFontSpacing($spacing, $parent=0) {
  15501. $val = 0; // value to be returned
  15502. $spacing = trim($spacing);
  15503. switch ($spacing) {
  15504. case 'normal': {
  15505. $val = 0;
  15506. break;
  15507. }
  15508. case 'inherit': {
  15509. if ($parent == 'normal') {
  15510. $val = 0;
  15511. } else {
  15512. $val = $parent;
  15513. }
  15514. break;
  15515. }
  15516. default: {
  15517. $val = $this->getHTMLUnitToUnits($spacing, 0, 'px', false);
  15518. }
  15519. }
  15520. return $val;
  15521. }
  15522. /**
  15523. * Returns the percentage of font stretching from CSS value
  15524. * @param $stretch (string) stretch mode
  15525. * @param $parent (float) stretch value of the parent element
  15526. * @return float font stretching percentage
  15527. * @protected
  15528. * @since 5.9.000 (2010-10-02)
  15529. */
  15530. protected function getCSSFontStretching($stretch, $parent=100) {
  15531. $val = 100; // value to be returned
  15532. $stretch = trim($stretch);
  15533. switch ($stretch) {
  15534. case 'ultra-condensed': {
  15535. $val = 40;
  15536. break;
  15537. }
  15538. case 'extra-condensed': {
  15539. $val = 55;
  15540. break;
  15541. }
  15542. case 'condensed': {
  15543. $val = 70;
  15544. break;
  15545. }
  15546. case 'semi-condensed': {
  15547. $val = 85;
  15548. break;
  15549. }
  15550. case 'normal': {
  15551. $val = 100;
  15552. break;
  15553. }
  15554. case 'semi-expanded': {
  15555. $val = 115;
  15556. break;
  15557. }
  15558. case 'expanded': {
  15559. $val = 130;
  15560. break;
  15561. }
  15562. case 'extra-expanded': {
  15563. $val = 145;
  15564. break;
  15565. }
  15566. case 'ultra-expanded': {
  15567. $val = 160;
  15568. break;
  15569. }
  15570. case 'wider': {
  15571. $val = ($parent + 10);
  15572. break;
  15573. }
  15574. case 'narrower': {
  15575. $val = ($parent - 10);
  15576. break;
  15577. }
  15578. case 'inherit': {
  15579. if ($parent == 'normal') {
  15580. $val = 100;
  15581. } else {
  15582. $val = $parent;
  15583. }
  15584. break;
  15585. }
  15586. default: {
  15587. $val = $this->getHTMLUnitToUnits($stretch, 100, '%', false);
  15588. }
  15589. }
  15590. return $val;
  15591. }
  15592. /**
  15593. * Convert HTML string containing font size value to points
  15594. * @param $val (string) String containing font size value and unit.
  15595. * @param $refsize (float) Reference font size in points.
  15596. * @param $parent_size (float) Parent font size in points.
  15597. * @param $defaultunit (string) Default unit (can be one of the following: %, em, ex, px, in, mm, pc, pt).
  15598. * @return float value in points
  15599. * @public
  15600. */
  15601. public function getHTMLFontUnits($val, $refsize=12, $parent_size=12, $defaultunit='pt') {
  15602. $refsize = TCPDF_FONTS::getFontRefSize($refsize);
  15603. $parent_size = TCPDF_FONTS::getFontRefSize($parent_size, $refsize);
  15604. switch ($val) {
  15605. case 'xx-small': {
  15606. $size = ($refsize - 4);
  15607. break;
  15608. }
  15609. case 'x-small': {
  15610. $size = ($refsize - 3);
  15611. break;
  15612. }
  15613. case 'small': {
  15614. $size = ($refsize - 2);
  15615. break;
  15616. }
  15617. case 'medium': {
  15618. $size = $refsize;
  15619. break;
  15620. }
  15621. case 'large': {
  15622. $size = ($refsize + 2);
  15623. break;
  15624. }
  15625. case 'x-large': {
  15626. $size = ($refsize + 4);
  15627. break;
  15628. }
  15629. case 'xx-large': {
  15630. $size = ($refsize + 6);
  15631. break;
  15632. }
  15633. case 'smaller': {
  15634. $size = ($parent_size - 3);
  15635. break;
  15636. }
  15637. case 'larger': {
  15638. $size = ($parent_size + 3);
  15639. break;
  15640. }
  15641. default: {
  15642. $size = $this->getHTMLUnitToUnits($val, $parent_size, $defaultunit, true);
  15643. }
  15644. }
  15645. return $size;
  15646. }
  15647. /**
  15648. * Returns the HTML DOM array.
  15649. * @param $html (string) html code
  15650. * @return array
  15651. * @protected
  15652. * @since 3.2.000 (2008-06-20)
  15653. */
  15654. protected function getHtmlDomArray($html) {
  15655. // array of CSS styles ( selector => properties).
  15656. $css = array();
  15657. // get CSS array defined at previous call
  15658. $matches = array();
  15659. if (preg_match_all('/<cssarray>([^\<]*)<\/cssarray>/isU', $html, $matches) > 0) {
  15660. if (isset($matches[1][0])) {
  15661. $css = array_merge($css, json_decode($this->unhtmlentities($matches[1][0]), true));
  15662. }
  15663. $html = preg_replace('/<cssarray>(.*?)<\/cssarray>/isU', '', $html);
  15664. }
  15665. // extract external CSS files
  15666. $matches = array();
  15667. if (preg_match_all('/<link([^\>]*)>/isU', $html, $matches) > 0) {
  15668. foreach ($matches[1] as $key => $link) {
  15669. $type = array();
  15670. if (preg_match('/type[\s]*=[\s]*"text\/css"/', $link, $type)) {
  15671. $type = array();
  15672. preg_match('/media[\s]*=[\s]*"([^"]*)"/', $link, $type);
  15673. // get 'all' and 'print' media, other media types are discarded
  15674. // (all, braille, embossed, handheld, print, projection, screen, speech, tty, tv)
  15675. if (empty($type) OR (isset($type[1]) AND (($type[1] == 'all') OR ($type[1] == 'print')))) {
  15676. $type = array();
  15677. if (preg_match('/href[\s]*=[\s]*"([^"]*)"/', $link, $type) > 0) {
  15678. // read CSS data file
  15679. $cssdata = TCPDF_STATIC::fileGetContents(trim($type[1]));
  15680. if (($cssdata !== FALSE) AND (strlen($cssdata) > 0)) {
  15681. $css = array_merge($css, TCPDF_STATIC::extractCSSproperties($cssdata));
  15682. }
  15683. }
  15684. }
  15685. }
  15686. }
  15687. }
  15688. // extract style tags
  15689. $matches = array();
  15690. if (preg_match_all('/<style([^\>]*)>([^\<]*)<\/style>/isU', $html, $matches) > 0) {
  15691. foreach ($matches[1] as $key => $media) {
  15692. $type = array();
  15693. preg_match('/media[\s]*=[\s]*"([^"]*)"/', $media, $type);
  15694. // get 'all' and 'print' media, other media types are discarded
  15695. // (all, braille, embossed, handheld, print, projection, screen, speech, tty, tv)
  15696. if (empty($type) OR (isset($type[1]) AND (($type[1] == 'all') OR ($type[1] == 'print')))) {
  15697. $cssdata = $matches[2][$key];
  15698. $css = array_merge($css, TCPDF_STATIC::extractCSSproperties($cssdata));
  15699. }
  15700. }
  15701. }
  15702. // create a special tag to contain the CSS array (used for table content)
  15703. $csstagarray = '<cssarray>'.htmlentities(json_encode($css)).'</cssarray>';
  15704. // remove head and style blocks
  15705. $html = preg_replace('/<head([^\>]*)>(.*?)<\/head>/siU', '', $html);
  15706. $html = preg_replace('/<style([^\>]*)>([^\<]*)<\/style>/isU', '', $html);
  15707. // define block tags
  15708. $blocktags = array('blockquote','br','dd','dl','div','dt','h1','h2','h3','h4','h5','h6','hr','li','ol','p','pre','ul','tcpdf','table','tr','td');
  15709. // define self-closing tags
  15710. $selfclosingtags = array('area','base','basefont','br','hr','input','img','link','meta');
  15711. // remove all unsupported tags (the line below lists all supported tags)
  15712. $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>');
  15713. //replace some blank characters
  15714. $html = preg_replace('/<pre/', '<xre', $html); // preserve pre tag
  15715. $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);
  15716. $html = preg_replace('@(\r\n|\r)@', "\n", $html);
  15717. $repTable = array("\t" => ' ', "\0" => ' ', "\x0B" => ' ', "\\" => "\\\\");
  15718. $html = strtr($html, $repTable);
  15719. $offset = 0;
  15720. while (($offset < strlen($html)) AND ($pos = strpos($html, '</pre>', $offset)) !== false) {
  15721. $html_a = substr($html, 0, $offset);
  15722. $html_b = substr($html, $offset, ($pos - $offset + 6));
  15723. while (preg_match("'<xre([^\>]*)>(.*?)\n(.*?)</pre>'si", $html_b)) {
  15724. // preserve newlines on <pre> tag
  15725. $html_b = preg_replace("'<xre([^\>]*)>(.*?)\n(.*?)</pre>'si", "<xre\\1>\\2<br />\\3</pre>", $html_b);
  15726. }
  15727. while (preg_match("'<xre([^\>]*)>(.*?)".$this->re_space['p']."(.*?)</pre>'".$this->re_space['m'], $html_b)) {
  15728. // preserve spaces on <pre> tag
  15729. $html_b = preg_replace("'<xre([^\>]*)>(.*?)".$this->re_space['p']."(.*?)</pre>'".$this->re_space['m'], "<xre\\1>\\2&nbsp;\\3</pre>", $html_b);
  15730. }
  15731. $html = $html_a.$html_b.substr($html, $pos + 6);
  15732. $offset = strlen($html_a.$html_b);
  15733. }
  15734. $offset = 0;
  15735. while (($offset < strlen($html)) AND ($pos = strpos($html, '</textarea>', $offset)) !== false) {
  15736. $html_a = substr($html, 0, $offset);
  15737. $html_b = substr($html, $offset, ($pos - $offset + 11));
  15738. while (preg_match("'<textarea([^\>]*)>(.*?)\n(.*?)</textarea>'si", $html_b)) {
  15739. // preserve newlines on <textarea> tag
  15740. $html_b = preg_replace("'<textarea([^\>]*)>(.*?)\n(.*?)</textarea>'si", "<textarea\\1>\\2<TBR>\\3</textarea>", $html_b);
  15741. $html_b = preg_replace("'<textarea([^\>]*)>(.*?)[\"](.*?)</textarea>'si", "<textarea\\1>\\2''\\3</textarea>", $html_b);
  15742. }
  15743. $html = $html_a.$html_b.substr($html, $pos + 11);
  15744. $offset = strlen($html_a.$html_b);
  15745. }
  15746. $html = preg_replace('/([\s]*)<option/si', '<option', $html);
  15747. $html = preg_replace('/<\/option>([\s]*)/si', '</option>', $html);
  15748. $offset = 0;
  15749. while (($offset < strlen($html)) AND ($pos = strpos($html, '</option>', $offset)) !== false) {
  15750. $html_a = substr($html, 0, $offset);
  15751. $html_b = substr($html, $offset, ($pos - $offset + 9));
  15752. while (preg_match("'<option([^\>]*)>(.*?)</option>'si", $html_b)) {
  15753. $html_b = preg_replace("'<option([\s]+)value=\"([^\"]*)\"([^\>]*)>(.*?)</option>'si", "\\2#!TaB!#\\4#!NwL!#", $html_b);
  15754. $html_b = preg_replace("'<option([^\>]*)>(.*?)</option>'si", "\\2#!NwL!#", $html_b);
  15755. }
  15756. $html = $html_a.$html_b.substr($html, $pos + 9);
  15757. $offset = strlen($html_a.$html_b);
  15758. }
  15759. if (preg_match("'</select'si", $html)) {
  15760. $html = preg_replace("'<select([^\>]*)>'si", "<select\\1 opt=\"", $html);
  15761. $html = preg_replace("'#!NwL!#</select>'si", "\" />", $html);
  15762. }
  15763. $html = str_replace("\n", ' ', $html);
  15764. // restore textarea newlines
  15765. $html = str_replace('<TBR>', "\n", $html);
  15766. // remove extra spaces from code
  15767. $html = preg_replace('/[\s]+<\/(table|tr|ul|ol|dl)>/', '</\\1>', $html);
  15768. $html = preg_replace('/'.$this->re_space['p'].'+<\/(td|th|li|dt|dd)>/'.$this->re_space['m'], '</\\1>', $html);
  15769. $html = preg_replace('/[\s]+<(tr|td|th|li|dt|dd)/', '<\\1', $html);
  15770. $html = preg_replace('/'.$this->re_space['p'].'+<(ul|ol|dl|br)/'.$this->re_space['m'], '<\\1', $html);
  15771. $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);
  15772. $html = preg_replace('/<\/(td|th)>/', '<marker style="font-size:0"/></\\1>', $html);
  15773. $html = preg_replace('/<\/table>([\s]*)<marker style="font-size:0"\/>/', '</table>', $html);
  15774. $html = preg_replace('/'.$this->re_space['p'].'+<img/'.$this->re_space['m'], chr(32).'<img', $html);
  15775. $html = preg_replace('/<img([^\>]*)>[\s]+([^\<])/xi', '<img\\1>&nbsp;\\2', $html);
  15776. $html = preg_replace('/<img([^\>]*)>/xi', '<img\\1><span><marker style="font-size:0"/></span>', $html);
  15777. $html = preg_replace('/<xre/', '<pre', $html); // restore pre tag
  15778. $html = preg_replace('/<textarea([^\>]*)>([^\<]*)<\/textarea>/xi', '<textarea\\1 value="\\2" />', $html);
  15779. $html = preg_replace('/<li([^\>]*)><\/li>/', '<li\\1>&nbsp;</li>', $html);
  15780. $html = preg_replace('/<li([^\>]*)>'.$this->re_space['p'].'*<img/'.$this->re_space['m'], '<li\\1><font size="1">&nbsp;</font><img', $html);
  15781. $html = preg_replace('/<([^\>\/]*)>[\s]/', '<\\1>&nbsp;', $html); // preserve some spaces
  15782. $html = preg_replace('/[\s]<\/([^\>]*)>/', '&nbsp;</\\1>', $html); // preserve some spaces
  15783. $html = preg_replace('/<su([bp])/', '<zws/><su\\1', $html); // fix sub/sup alignment
  15784. $html = preg_replace('/<\/su([bp])>/', '</su\\1><zws/>', $html); // fix sub/sup alignment
  15785. $html = preg_replace('/'.$this->re_space['p'].'+/'.$this->re_space['m'], chr(32), $html); // replace multiple spaces with a single space
  15786. // trim string
  15787. $html = $this->stringTrim($html);
  15788. // fix br tag after li
  15789. $html = preg_replace('/<li><br([^\>]*)>/', '<li> <br\\1>', $html);
  15790. // fix first image tag alignment
  15791. $html = preg_replace('/^<img/', '<span style="font-size:0"><br /></span> <img', $html, 1);
  15792. // pattern for generic tag
  15793. $tagpattern = '/(<[^>]+>)/';
  15794. // explodes the string
  15795. $a = preg_split($tagpattern, $html, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
  15796. // count elements
  15797. $maxel = count($a);
  15798. $elkey = 0;
  15799. $key = 0;
  15800. // create an array of elements
  15801. $dom = array();
  15802. $dom[$key] = array();
  15803. // set inheritable properties fot the first void element
  15804. // 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
  15805. $dom[$key]['tag'] = false;
  15806. $dom[$key]['block'] = false;
  15807. $dom[$key]['value'] = '';
  15808. $dom[$key]['parent'] = 0;
  15809. $dom[$key]['hide'] = false;
  15810. $dom[$key]['fontname'] = $this->FontFamily;
  15811. $dom[$key]['fontstyle'] = $this->FontStyle;
  15812. $dom[$key]['fontsize'] = $this->FontSizePt;
  15813. $dom[$key]['font-stretch'] = $this->font_stretching;
  15814. $dom[$key]['letter-spacing'] = $this->font_spacing;
  15815. $dom[$key]['stroke'] = $this->textstrokewidth;
  15816. $dom[$key]['fill'] = (($this->textrendermode % 2) == 0);
  15817. $dom[$key]['clip'] = ($this->textrendermode > 3);
  15818. $dom[$key]['line-height'] = $this->cell_height_ratio;
  15819. $dom[$key]['bgcolor'] = false;
  15820. $dom[$key]['fgcolor'] = $this->fgcolor; // color
  15821. $dom[$key]['strokecolor'] = $this->strokecolor;
  15822. $dom[$key]['align'] = '';
  15823. $dom[$key]['listtype'] = '';
  15824. $dom[$key]['text-indent'] = 0;
  15825. $dom[$key]['text-transform'] = '';
  15826. $dom[$key]['border'] = array();
  15827. $dom[$key]['dir'] = $this->rtl?'rtl':'ltr';
  15828. $thead = false; // true when we are inside the THEAD tag
  15829. ++$key;
  15830. $level = array();
  15831. array_push($level, 0); // root
  15832. while ($elkey < $maxel) {
  15833. $dom[$key] = array();
  15834. $element = $a[$elkey];
  15835. $dom[$key]['elkey'] = $elkey;
  15836. if (preg_match($tagpattern, $element)) {
  15837. // html tag
  15838. $element = substr($element, 1, -1);
  15839. // get tag name
  15840. preg_match('/[\/]?([a-zA-Z0-9]*)/', $element, $tag);
  15841. $tagname = strtolower($tag[1]);
  15842. // check if we are inside a table header
  15843. if ($tagname == 'thead') {
  15844. if ($element[0] == '/') {
  15845. $thead = false;
  15846. } else {
  15847. $thead = true;
  15848. }
  15849. ++$elkey;
  15850. continue;
  15851. }
  15852. $dom[$key]['tag'] = true;
  15853. $dom[$key]['value'] = $tagname;
  15854. if (in_array($dom[$key]['value'], $blocktags)) {
  15855. $dom[$key]['block'] = true;
  15856. } else {
  15857. $dom[$key]['block'] = false;
  15858. }
  15859. if ($element[0] == '/') {
  15860. // *** closing html tag
  15861. $dom[$key]['opening'] = false;
  15862. $dom[$key]['parent'] = end($level);
  15863. array_pop($level);
  15864. $dom[$key]['hide'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['hide'];
  15865. $dom[$key]['fontname'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['fontname'];
  15866. $dom[$key]['fontstyle'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['fontstyle'];
  15867. $dom[$key]['fontsize'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['fontsize'];
  15868. $dom[$key]['font-stretch'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['font-stretch'];
  15869. $dom[$key]['letter-spacing'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['letter-spacing'];
  15870. $dom[$key]['stroke'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['stroke'];
  15871. $dom[$key]['fill'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['fill'];
  15872. $dom[$key]['clip'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['clip'];
  15873. $dom[$key]['line-height'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['line-height'];
  15874. $dom[$key]['bgcolor'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['bgcolor'];
  15875. $dom[$key]['fgcolor'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['fgcolor'];
  15876. $dom[$key]['strokecolor'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['strokecolor'];
  15877. $dom[$key]['align'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['align'];
  15878. $dom[$key]['text-transform'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['text-transform'];
  15879. $dom[$key]['dir'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['dir'];
  15880. if (isset($dom[($dom[($dom[$key]['parent'])]['parent'])]['listtype'])) {
  15881. $dom[$key]['listtype'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['listtype'];
  15882. }
  15883. // set the number of columns in table tag
  15884. if (($dom[$key]['value'] == 'tr') AND (!isset($dom[($dom[($dom[$key]['parent'])]['parent'])]['cols']))) {
  15885. $dom[($dom[($dom[$key]['parent'])]['parent'])]['cols'] = $dom[($dom[$key]['parent'])]['cols'];
  15886. }
  15887. if (($dom[$key]['value'] == 'td') OR ($dom[$key]['value'] == 'th')) {
  15888. $dom[($dom[$key]['parent'])]['content'] = $csstagarray;
  15889. for ($i = ($dom[$key]['parent'] + 1); $i < $key; ++$i) {
  15890. $dom[($dom[$key]['parent'])]['content'] .= stripslashes($a[$dom[$i]['elkey']]);
  15891. }
  15892. $key = $i;
  15893. // mark nested tables
  15894. $dom[($dom[$key]['parent'])]['content'] = str_replace('<table', '<table nested="true"', $dom[($dom[$key]['parent'])]['content']);
  15895. // remove thead sections from nested tables
  15896. $dom[($dom[$key]['parent'])]['content'] = str_replace('<thead>', '', $dom[($dom[$key]['parent'])]['content']);
  15897. $dom[($dom[$key]['parent'])]['content'] = str_replace('</thead>', '', $dom[($dom[$key]['parent'])]['content']);
  15898. }
  15899. // store header rows on a new table
  15900. if (($dom[$key]['value'] == 'tr') AND ($dom[($dom[$key]['parent'])]['thead'] === true)) {
  15901. if (TCPDF_STATIC::empty_string($dom[($dom[($dom[$key]['parent'])]['parent'])]['thead'])) {
  15902. $dom[($dom[($dom[$key]['parent'])]['parent'])]['thead'] = $csstagarray.$a[$dom[($dom[($dom[$key]['parent'])]['parent'])]['elkey']];
  15903. }
  15904. for ($i = $dom[$key]['parent']; $i <= $key; ++$i) {
  15905. $dom[($dom[($dom[$key]['parent'])]['parent'])]['thead'] .= $a[$dom[$i]['elkey']];
  15906. }
  15907. if (!isset($dom[($dom[$key]['parent'])]['attribute'])) {
  15908. $dom[($dom[$key]['parent'])]['attribute'] = array();
  15909. }
  15910. // header elements must be always contained in a single page
  15911. $dom[($dom[$key]['parent'])]['attribute']['nobr'] = 'true';
  15912. }
  15913. if (($dom[$key]['value'] == 'table') AND (!TCPDF_STATIC::empty_string($dom[($dom[$key]['parent'])]['thead']))) {
  15914. // remove the nobr attributes from the table header
  15915. $dom[($dom[$key]['parent'])]['thead'] = str_replace(' nobr="true"', '', $dom[($dom[$key]['parent'])]['thead']);
  15916. $dom[($dom[$key]['parent'])]['thead'] .= '</tablehead>';
  15917. }
  15918. } else {
  15919. // *** opening or self-closing html tag
  15920. $dom[$key]['opening'] = true;
  15921. $dom[$key]['parent'] = end($level);
  15922. if ((substr($element, -1, 1) == '/') OR (in_array($dom[$key]['value'], $selfclosingtags))) {
  15923. // self-closing tag
  15924. $dom[$key]['self'] = true;
  15925. } else {
  15926. // opening tag
  15927. array_push($level, $key);
  15928. $dom[$key]['self'] = false;
  15929. }
  15930. // copy some values from parent
  15931. $parentkey = 0;
  15932. if ($key > 0) {
  15933. $parentkey = $dom[$key]['parent'];
  15934. $dom[$key]['hide'] = $dom[$parentkey]['hide'];
  15935. $dom[$key]['fontname'] = $dom[$parentkey]['fontname'];
  15936. $dom[$key]['fontstyle'] = $dom[$parentkey]['fontstyle'];
  15937. $dom[$key]['fontsize'] = $dom[$parentkey]['fontsize'];
  15938. $dom[$key]['font-stretch'] = $dom[$parentkey]['font-stretch'];
  15939. $dom[$key]['letter-spacing'] = $dom[$parentkey]['letter-spacing'];
  15940. $dom[$key]['stroke'] = $dom[$parentkey]['stroke'];
  15941. $dom[$key]['fill'] = $dom[$parentkey]['fill'];
  15942. $dom[$key]['clip'] = $dom[$parentkey]['clip'];
  15943. $dom[$key]['line-height'] = $dom[$parentkey]['line-height'];
  15944. $dom[$key]['bgcolor'] = $dom[$parentkey]['bgcolor'];
  15945. $dom[$key]['fgcolor'] = $dom[$parentkey]['fgcolor'];
  15946. $dom[$key]['strokecolor'] = $dom[$parentkey]['strokecolor'];
  15947. $dom[$key]['align'] = $dom[$parentkey]['align'];
  15948. $dom[$key]['listtype'] = $dom[$parentkey]['listtype'];
  15949. $dom[$key]['text-indent'] = $dom[$parentkey]['text-indent'];
  15950. $dom[$key]['text-transform'] = $dom[$parentkey]['text-transform'];
  15951. $dom[$key]['border'] = array();
  15952. $dom[$key]['dir'] = $dom[$parentkey]['dir'];
  15953. }
  15954. // get attributes
  15955. preg_match_all('/([^=\s]*)[\s]*=[\s]*"([^"]*)"/', $element, $attr_array, PREG_PATTERN_ORDER);
  15956. $dom[$key]['attribute'] = array(); // reset attribute array
  15957. foreach($attr_array[1] as $id => $name) {
  15958. $dom[$key]['attribute'][strtolower($name)] = $attr_array[2][$id];
  15959. }
  15960. if (!empty($css)) {
  15961. // merge CSS style to current style
  15962. list($dom[$key]['csssel'], $dom[$key]['cssdata']) = TCPDF_STATIC::getCSSdataArray($dom, $key, $css);
  15963. $dom[$key]['attribute']['style'] = TCPDF_STATIC::getTagStyleFromCSSarray($dom[$key]['cssdata']);
  15964. }
  15965. // split style attributes
  15966. if (isset($dom[$key]['attribute']['style']) AND !empty($dom[$key]['attribute']['style'])) {
  15967. // get style attributes
  15968. preg_match_all('/([^;:\s]*):([^;]*)/', $dom[$key]['attribute']['style'], $style_array, PREG_PATTERN_ORDER);
  15969. $dom[$key]['style'] = array(); // reset style attribute array
  15970. foreach($style_array[1] as $id => $name) {
  15971. // in case of duplicate attribute the last replace the previous
  15972. $dom[$key]['style'][strtolower($name)] = trim($style_array[2][$id]);
  15973. }
  15974. // --- get some style attributes ---
  15975. // text direction
  15976. if (isset($dom[$key]['style']['direction'])) {
  15977. $dom[$key]['dir'] = $dom[$key]['style']['direction'];
  15978. }
  15979. // display
  15980. if (isset($dom[$key]['style']['display'])) {
  15981. $dom[$key]['hide'] = (trim(strtolower($dom[$key]['style']['display'])) == 'none');
  15982. }
  15983. // font family
  15984. if (isset($dom[$key]['style']['font-family'])) {
  15985. $dom[$key]['fontname'] = $this->getFontFamilyName($dom[$key]['style']['font-family']);
  15986. }
  15987. // list-style-type
  15988. if (isset($dom[$key]['style']['list-style-type'])) {
  15989. $dom[$key]['listtype'] = trim(strtolower($dom[$key]['style']['list-style-type']));
  15990. if ($dom[$key]['listtype'] == 'inherit') {
  15991. $dom[$key]['listtype'] = $dom[$parentkey]['listtype'];
  15992. }
  15993. }
  15994. // text-indent
  15995. if (isset($dom[$key]['style']['text-indent'])) {
  15996. $dom[$key]['text-indent'] = $this->getHTMLUnitToUnits($dom[$key]['style']['text-indent']);
  15997. if ($dom[$key]['text-indent'] == 'inherit') {
  15998. $dom[$key]['text-indent'] = $dom[$parentkey]['text-indent'];
  15999. }
  16000. }
  16001. // text-transform
  16002. if (isset($dom[$key]['style']['text-transform'])) {
  16003. $dom[$key]['text-transform'] = $dom[$key]['style']['text-transform'];
  16004. }
  16005. // font size
  16006. if (isset($dom[$key]['style']['font-size'])) {
  16007. $fsize = trim($dom[$key]['style']['font-size']);
  16008. $dom[$key]['fontsize'] = $this->getHTMLFontUnits($fsize, $dom[0]['fontsize'], $dom[$parentkey]['fontsize'], 'pt');
  16009. }
  16010. // font-stretch
  16011. if (isset($dom[$key]['style']['font-stretch'])) {
  16012. $dom[$key]['font-stretch'] = $this->getCSSFontStretching($dom[$key]['style']['font-stretch'], $dom[$parentkey]['font-stretch']);
  16013. }
  16014. // letter-spacing
  16015. if (isset($dom[$key]['style']['letter-spacing'])) {
  16016. $dom[$key]['letter-spacing'] = $this->getCSSFontSpacing($dom[$key]['style']['letter-spacing'], $dom[$parentkey]['letter-spacing']);
  16017. }
  16018. // line-height (internally is the cell height ratio)
  16019. if (isset($dom[$key]['style']['line-height'])) {
  16020. $lineheight = trim($dom[$key]['style']['line-height']);
  16021. switch ($lineheight) {
  16022. // A normal line height. This is default
  16023. case 'normal': {
  16024. $dom[$key]['line-height'] = $dom[0]['line-height'];
  16025. break;
  16026. }
  16027. case 'inherit': {
  16028. $dom[$key]['line-height'] = $dom[$parentkey]['line-height'];
  16029. }
  16030. default: {
  16031. if (is_numeric($lineheight)) {
  16032. // convert to percentage of font height
  16033. $lineheight = ($lineheight * 100).'%';
  16034. }
  16035. $dom[$key]['line-height'] = $this->getHTMLUnitToUnits($lineheight, 1, '%', true);
  16036. if (substr($lineheight, -1) !== '%') {
  16037. if ($dom[$key]['fontsize'] <= 0) {
  16038. $dom[$key]['line-height'] = 1;
  16039. } else {
  16040. $dom[$key]['line-height'] = (($dom[$key]['line-height'] - $this->cell_padding['T'] - $this->cell_padding['B']) / $dom[$key]['fontsize']);
  16041. }
  16042. }
  16043. }
  16044. }
  16045. }
  16046. // font style
  16047. if (isset($dom[$key]['style']['font-weight'])) {
  16048. if (strtolower($dom[$key]['style']['font-weight'][0]) == 'n') {
  16049. if (strpos($dom[$key]['fontstyle'], 'B') !== false) {
  16050. $dom[$key]['fontstyle'] = str_replace('B', '', $dom[$key]['fontstyle']);
  16051. }
  16052. } elseif (strtolower($dom[$key]['style']['font-weight'][0]) == 'b') {
  16053. $dom[$key]['fontstyle'] .= 'B';
  16054. }
  16055. }
  16056. if (isset($dom[$key]['style']['font-style']) AND (strtolower($dom[$key]['style']['font-style'][0]) == 'i')) {
  16057. $dom[$key]['fontstyle'] .= 'I';
  16058. }
  16059. // font color
  16060. if (isset($dom[$key]['style']['color']) AND (!TCPDF_STATIC::empty_string($dom[$key]['style']['color']))) {
  16061. $dom[$key]['fgcolor'] = TCPDF_COLORS::convertHTMLColorToDec($dom[$key]['style']['color'], $this->spot_colors);
  16062. } elseif ($dom[$key]['value'] == 'a') {
  16063. $dom[$key]['fgcolor'] = $this->htmlLinkColorArray;
  16064. }
  16065. // background color
  16066. if (isset($dom[$key]['style']['background-color']) AND (!TCPDF_STATIC::empty_string($dom[$key]['style']['background-color']))) {
  16067. $dom[$key]['bgcolor'] = TCPDF_COLORS::convertHTMLColorToDec($dom[$key]['style']['background-color'], $this->spot_colors);
  16068. }
  16069. // text-decoration
  16070. if (isset($dom[$key]['style']['text-decoration'])) {
  16071. $decors = explode(' ', strtolower($dom[$key]['style']['text-decoration']));
  16072. foreach ($decors as $dec) {
  16073. $dec = trim($dec);
  16074. if (!TCPDF_STATIC::empty_string($dec)) {
  16075. if ($dec[0] == 'u') {
  16076. // underline
  16077. $dom[$key]['fontstyle'] .= 'U';
  16078. } elseif ($dec[0] == 'l') {
  16079. // line-through
  16080. $dom[$key]['fontstyle'] .= 'D';
  16081. } elseif ($dec[0] == 'o') {
  16082. // overline
  16083. $dom[$key]['fontstyle'] .= 'O';
  16084. }
  16085. }
  16086. }
  16087. } elseif ($dom[$key]['value'] == 'a') {
  16088. $dom[$key]['fontstyle'] = $this->htmlLinkFontStyle;
  16089. }
  16090. // check for width attribute
  16091. if (isset($dom[$key]['style']['width'])) {
  16092. $dom[$key]['width'] = $dom[$key]['style']['width'];
  16093. }
  16094. // check for height attribute
  16095. if (isset($dom[$key]['style']['height'])) {
  16096. $dom[$key]['height'] = $dom[$key]['style']['height'];
  16097. }
  16098. // check for text alignment
  16099. if (isset($dom[$key]['style']['text-align'])) {
  16100. $dom[$key]['align'] = strtoupper($dom[$key]['style']['text-align'][0]);
  16101. }
  16102. // check for CSS border properties
  16103. if (isset($dom[$key]['style']['border'])) {
  16104. $borderstyle = $this->getCSSBorderStyle($dom[$key]['style']['border']);
  16105. if (!empty($borderstyle)) {
  16106. $dom[$key]['border']['LTRB'] = $borderstyle;
  16107. }
  16108. }
  16109. if (isset($dom[$key]['style']['border-color'])) {
  16110. $brd_colors = preg_split('/[\s]+/', trim($dom[$key]['style']['border-color']));
  16111. if (isset($brd_colors[3])) {
  16112. $dom[$key]['border']['L']['color'] = TCPDF_COLORS::convertHTMLColorToDec($brd_colors[3], $this->spot_colors);
  16113. }
  16114. if (isset($brd_colors[1])) {
  16115. $dom[$key]['border']['R']['color'] = TCPDF_COLORS::convertHTMLColorToDec($brd_colors[1], $this->spot_colors);
  16116. }
  16117. if (isset($brd_colors[0])) {
  16118. $dom[$key]['border']['T']['color'] = TCPDF_COLORS::convertHTMLColorToDec($brd_colors[0], $this->spot_colors);
  16119. }
  16120. if (isset($brd_colors[2])) {
  16121. $dom[$key]['border']['B']['color'] = TCPDF_COLORS::convertHTMLColorToDec($brd_colors[2], $this->spot_colors);
  16122. }
  16123. }
  16124. if (isset($dom[$key]['style']['border-width'])) {
  16125. $brd_widths = preg_split('/[\s]+/', trim($dom[$key]['style']['border-width']));
  16126. if (isset($brd_widths[3])) {
  16127. $dom[$key]['border']['L']['width'] = $this->getCSSBorderWidth($brd_widths[3]);
  16128. }
  16129. if (isset($brd_widths[1])) {
  16130. $dom[$key]['border']['R']['width'] = $this->getCSSBorderWidth($brd_widths[1]);
  16131. }
  16132. if (isset($brd_widths[0])) {
  16133. $dom[$key]['border']['T']['width'] = $this->getCSSBorderWidth($brd_widths[0]);
  16134. }
  16135. if (isset($brd_widths[2])) {
  16136. $dom[$key]['border']['B']['width'] = $this->getCSSBorderWidth($brd_widths[2]);
  16137. }
  16138. }
  16139. if (isset($dom[$key]['style']['border-style'])) {
  16140. $brd_styles = preg_split('/[\s]+/', trim($dom[$key]['style']['border-style']));
  16141. if (isset($brd_styles[3]) AND ($brd_styles[3]!='none')) {
  16142. $dom[$key]['border']['L']['cap'] = 'square';
  16143. $dom[$key]['border']['L']['join'] = 'miter';
  16144. $dom[$key]['border']['L']['dash'] = $this->getCSSBorderDashStyle($brd_styles[3]);
  16145. if ($dom[$key]['border']['L']['dash'] < 0) {
  16146. $dom[$key]['border']['L'] = array();
  16147. }
  16148. }
  16149. if (isset($brd_styles[1])) {
  16150. $dom[$key]['border']['R']['cap'] = 'square';
  16151. $dom[$key]['border']['R']['join'] = 'miter';
  16152. $dom[$key]['border']['R']['dash'] = $this->getCSSBorderDashStyle($brd_styles[1]);
  16153. if ($dom[$key]['border']['R']['dash'] < 0) {
  16154. $dom[$key]['border']['R'] = array();
  16155. }
  16156. }
  16157. if (isset($brd_styles[0])) {
  16158. $dom[$key]['border']['T']['cap'] = 'square';
  16159. $dom[$key]['border']['T']['join'] = 'miter';
  16160. $dom[$key]['border']['T']['dash'] = $this->getCSSBorderDashStyle($brd_styles[0]);
  16161. if ($dom[$key]['border']['T']['dash'] < 0) {
  16162. $dom[$key]['border']['T'] = array();
  16163. }
  16164. }
  16165. if (isset($brd_styles[2])) {
  16166. $dom[$key]['border']['B']['cap'] = 'square';
  16167. $dom[$key]['border']['B']['join'] = 'miter';
  16168. $dom[$key]['border']['B']['dash'] = $this->getCSSBorderDashStyle($brd_styles[2]);
  16169. if ($dom[$key]['border']['B']['dash'] < 0) {
  16170. $dom[$key]['border']['B'] = array();
  16171. }
  16172. }
  16173. }
  16174. $cellside = array('L' => 'left', 'R' => 'right', 'T' => 'top', 'B' => 'bottom');
  16175. foreach ($cellside as $bsk => $bsv) {
  16176. if (isset($dom[$key]['style']['border-'.$bsv])) {
  16177. $borderstyle = $this->getCSSBorderStyle($dom[$key]['style']['border-'.$bsv]);
  16178. if (!empty($borderstyle)) {
  16179. $dom[$key]['border'][$bsk] = $borderstyle;
  16180. }
  16181. }
  16182. if (isset($dom[$key]['style']['border-'.$bsv.'-color'])) {
  16183. $dom[$key]['border'][$bsk]['color'] = TCPDF_COLORS::convertHTMLColorToDec($dom[$key]['style']['border-'.$bsv.'-color'], $this->spot_colors);
  16184. }
  16185. if (isset($dom[$key]['style']['border-'.$bsv.'-width'])) {
  16186. $dom[$key]['border'][$bsk]['width'] = $this->getCSSBorderWidth($dom[$key]['style']['border-'.$bsv.'-width']);
  16187. }
  16188. if (isset($dom[$key]['style']['border-'.$bsv.'-style'])) {
  16189. $dom[$key]['border'][$bsk]['dash'] = $this->getCSSBorderDashStyle($dom[$key]['style']['border-'.$bsv.'-style']);
  16190. if ($dom[$key]['border'][$bsk]['dash'] < 0) {
  16191. $dom[$key]['border'][$bsk] = array();
  16192. }
  16193. }
  16194. }
  16195. // check for CSS padding properties
  16196. if (isset($dom[$key]['style']['padding'])) {
  16197. $dom[$key]['padding'] = $this->getCSSPadding($dom[$key]['style']['padding']);
  16198. } else {
  16199. $dom[$key]['padding'] = $this->cell_padding;
  16200. }
  16201. foreach ($cellside as $psk => $psv) {
  16202. if (isset($dom[$key]['style']['padding-'.$psv])) {
  16203. $dom[$key]['padding'][$psk] = $this->getHTMLUnitToUnits($dom[$key]['style']['padding-'.$psv], 0, 'px', false);
  16204. }
  16205. }
  16206. // check for CSS margin properties
  16207. if (isset($dom[$key]['style']['margin'])) {
  16208. $dom[$key]['margin'] = $this->getCSSMargin($dom[$key]['style']['margin']);
  16209. } else {
  16210. $dom[$key]['margin'] = $this->cell_margin;
  16211. }
  16212. foreach ($cellside as $psk => $psv) {
  16213. if (isset($dom[$key]['style']['margin-'.$psv])) {
  16214. $dom[$key]['margin'][$psk] = $this->getHTMLUnitToUnits(str_replace('auto', '0', $dom[$key]['style']['margin-'.$psv]), 0, 'px', false);
  16215. }
  16216. }
  16217. // check for CSS border-spacing properties
  16218. if (isset($dom[$key]['style']['border-spacing'])) {
  16219. $dom[$key]['border-spacing'] = $this->getCSSBorderMargin($dom[$key]['style']['border-spacing']);
  16220. }
  16221. // page-break-inside
  16222. if (isset($dom[$key]['style']['page-break-inside']) AND ($dom[$key]['style']['page-break-inside'] == 'avoid')) {
  16223. $dom[$key]['attribute']['nobr'] = 'true';
  16224. }
  16225. // page-break-before
  16226. if (isset($dom[$key]['style']['page-break-before'])) {
  16227. if ($dom[$key]['style']['page-break-before'] == 'always') {
  16228. $dom[$key]['attribute']['pagebreak'] = 'true';
  16229. } elseif ($dom[$key]['style']['page-break-before'] == 'left') {
  16230. $dom[$key]['attribute']['pagebreak'] = 'left';
  16231. } elseif ($dom[$key]['style']['page-break-before'] == 'right') {
  16232. $dom[$key]['attribute']['pagebreak'] = 'right';
  16233. }
  16234. }
  16235. // page-break-after
  16236. if (isset($dom[$key]['style']['page-break-after'])) {
  16237. if ($dom[$key]['style']['page-break-after'] == 'always') {
  16238. $dom[$key]['attribute']['pagebreakafter'] = 'true';
  16239. } elseif ($dom[$key]['style']['page-break-after'] == 'left') {
  16240. $dom[$key]['attribute']['pagebreakafter'] = 'left';
  16241. } elseif ($dom[$key]['style']['page-break-after'] == 'right') {
  16242. $dom[$key]['attribute']['pagebreakafter'] = 'right';
  16243. }
  16244. }
  16245. }
  16246. if (isset($dom[$key]['attribute']['display'])) {
  16247. $dom[$key]['hide'] = (trim(strtolower($dom[$key]['attribute']['display'])) == 'none');
  16248. }
  16249. if (isset($dom[$key]['attribute']['border']) AND ($dom[$key]['attribute']['border'] != 0)) {
  16250. $borderstyle = $this->getCSSBorderStyle($dom[$key]['attribute']['border'].' solid black');
  16251. if (!empty($borderstyle)) {
  16252. $dom[$key]['border']['LTRB'] = $borderstyle;
  16253. }
  16254. }
  16255. // check for font tag
  16256. if ($dom[$key]['value'] == 'font') {
  16257. // font family
  16258. if (isset($dom[$key]['attribute']['face'])) {
  16259. $dom[$key]['fontname'] = $this->getFontFamilyName($dom[$key]['attribute']['face']);
  16260. }
  16261. // font size
  16262. if (isset($dom[$key]['attribute']['size'])) {
  16263. if ($key > 0) {
  16264. if ($dom[$key]['attribute']['size'][0] == '+') {
  16265. $dom[$key]['fontsize'] = $dom[($dom[$key]['parent'])]['fontsize'] + intval(substr($dom[$key]['attribute']['size'], 1));
  16266. } elseif ($dom[$key]['attribute']['size'][0] == '-') {
  16267. $dom[$key]['fontsize'] = $dom[($dom[$key]['parent'])]['fontsize'] - intval(substr($dom[$key]['attribute']['size'], 1));
  16268. } else {
  16269. $dom[$key]['fontsize'] = intval($dom[$key]['attribute']['size']);
  16270. }
  16271. } else {
  16272. $dom[$key]['fontsize'] = intval($dom[$key]['attribute']['size']);
  16273. }
  16274. }
  16275. }
  16276. // force natural alignment for lists
  16277. if ((($dom[$key]['value'] == 'ul') OR ($dom[$key]['value'] == 'ol') OR ($dom[$key]['value'] == 'dl'))
  16278. AND (!isset($dom[$key]['align']) OR TCPDF_STATIC::empty_string($dom[$key]['align']) OR ($dom[$key]['align'] != 'J'))) {
  16279. if ($this->rtl) {
  16280. $dom[$key]['align'] = 'R';
  16281. } else {
  16282. $dom[$key]['align'] = 'L';
  16283. }
  16284. }
  16285. if (($dom[$key]['value'] == 'small') OR ($dom[$key]['value'] == 'sup') OR ($dom[$key]['value'] == 'sub')) {
  16286. if (!isset($dom[$key]['attribute']['size']) AND !isset($dom[$key]['style']['font-size'])) {
  16287. $dom[$key]['fontsize'] = $dom[$key]['fontsize'] * K_SMALL_RATIO;
  16288. }
  16289. }
  16290. if (($dom[$key]['value'] == 'strong') OR ($dom[$key]['value'] == 'b')) {
  16291. $dom[$key]['fontstyle'] .= 'B';
  16292. }
  16293. if (($dom[$key]['value'] == 'em') OR ($dom[$key]['value'] == 'i')) {
  16294. $dom[$key]['fontstyle'] .= 'I';
  16295. }
  16296. if ($dom[$key]['value'] == 'u') {
  16297. $dom[$key]['fontstyle'] .= 'U';
  16298. }
  16299. if (($dom[$key]['value'] == 'del') OR ($dom[$key]['value'] == 's') OR ($dom[$key]['value'] == 'strike')) {
  16300. $dom[$key]['fontstyle'] .= 'D';
  16301. }
  16302. if (!isset($dom[$key]['style']['text-decoration']) AND ($dom[$key]['value'] == 'a')) {
  16303. $dom[$key]['fontstyle'] = $this->htmlLinkFontStyle;
  16304. }
  16305. if (($dom[$key]['value'] == 'pre') OR ($dom[$key]['value'] == 'tt')) {
  16306. $dom[$key]['fontname'] = $this->default_monospaced_font;
  16307. }
  16308. 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)) {
  16309. // headings h1, h2, h3, h4, h5, h6
  16310. if (!isset($dom[$key]['attribute']['size']) AND !isset($dom[$key]['style']['font-size'])) {
  16311. $headsize = (4 - intval($dom[$key]['value']{1})) * 2;
  16312. $dom[$key]['fontsize'] = $dom[0]['fontsize'] + $headsize;
  16313. }
  16314. if (!isset($dom[$key]['style']['font-weight'])) {
  16315. $dom[$key]['fontstyle'] .= 'B';
  16316. }
  16317. }
  16318. if (($dom[$key]['value'] == 'table')) {
  16319. $dom[$key]['rows'] = 0; // number of rows
  16320. $dom[$key]['trids'] = array(); // IDs of TR elements
  16321. $dom[$key]['thead'] = ''; // table header rows
  16322. }
  16323. if (($dom[$key]['value'] == 'tr')) {
  16324. $dom[$key]['cols'] = 0;
  16325. if ($thead) {
  16326. $dom[$key]['thead'] = true;
  16327. // rows on thead block are printed as a separate table
  16328. } else {
  16329. $dom[$key]['thead'] = false;
  16330. // store the number of rows on table element
  16331. ++$dom[($dom[$key]['parent'])]['rows'];
  16332. // store the TR elements IDs on table element
  16333. array_push($dom[($dom[$key]['parent'])]['trids'], $key);
  16334. }
  16335. }
  16336. if (($dom[$key]['value'] == 'th') OR ($dom[$key]['value'] == 'td')) {
  16337. if (isset($dom[$key]['attribute']['colspan'])) {
  16338. $colspan = intval($dom[$key]['attribute']['colspan']);
  16339. } else {
  16340. $colspan = 1;
  16341. }
  16342. $dom[$key]['attribute']['colspan'] = $colspan;
  16343. $dom[($dom[$key]['parent'])]['cols'] += $colspan;
  16344. }
  16345. // text direction
  16346. if (isset($dom[$key]['attribute']['dir'])) {
  16347. $dom[$key]['dir'] = $dom[$key]['attribute']['dir'];
  16348. }
  16349. // set foreground color attribute
  16350. if (isset($dom[$key]['attribute']['color']) AND (!TCPDF_STATIC::empty_string($dom[$key]['attribute']['color']))) {
  16351. $dom[$key]['fgcolor'] = TCPDF_COLORS::convertHTMLColorToDec($dom[$key]['attribute']['color'], $this->spot_colors);
  16352. } elseif (!isset($dom[$key]['style']['color']) AND ($dom[$key]['value'] == 'a')) {
  16353. $dom[$key]['fgcolor'] = $this->htmlLinkColorArray;
  16354. }
  16355. // set background color attribute
  16356. if (isset($dom[$key]['attribute']['bgcolor']) AND (!TCPDF_STATIC::empty_string($dom[$key]['attribute']['bgcolor']))) {
  16357. $dom[$key]['bgcolor'] = TCPDF_COLORS::convertHTMLColorToDec($dom[$key]['attribute']['bgcolor'], $this->spot_colors);
  16358. }
  16359. // set stroke color attribute
  16360. if (isset($dom[$key]['attribute']['strokecolor']) AND (!TCPDF_STATIC::empty_string($dom[$key]['attribute']['strokecolor']))) {
  16361. $dom[$key]['strokecolor'] = TCPDF_COLORS::convertHTMLColorToDec($dom[$key]['attribute']['strokecolor'], $this->spot_colors);
  16362. }
  16363. // check for width attribute
  16364. if (isset($dom[$key]['attribute']['width'])) {
  16365. $dom[$key]['width'] = $dom[$key]['attribute']['width'];
  16366. }
  16367. // check for height attribute
  16368. if (isset($dom[$key]['attribute']['height'])) {
  16369. $dom[$key]['height'] = $dom[$key]['attribute']['height'];
  16370. }
  16371. // check for text alignment
  16372. if (isset($dom[$key]['attribute']['align']) AND (!TCPDF_STATIC::empty_string($dom[$key]['attribute']['align'])) AND ($dom[$key]['value'] !== 'img')) {
  16373. $dom[$key]['align'] = strtoupper($dom[$key]['attribute']['align'][0]);
  16374. }
  16375. // check for text rendering mode (the following attributes do not exist in HTML)
  16376. if (isset($dom[$key]['attribute']['stroke'])) {
  16377. // font stroke width
  16378. $dom[$key]['stroke'] = $this->getHTMLUnitToUnits($dom[$key]['attribute']['stroke'], $dom[$key]['fontsize'], 'pt', true);
  16379. }
  16380. if (isset($dom[$key]['attribute']['fill'])) {
  16381. // font fill
  16382. if ($dom[$key]['attribute']['fill'] == 'true') {
  16383. $dom[$key]['fill'] = true;
  16384. } else {
  16385. $dom[$key]['fill'] = false;
  16386. }
  16387. }
  16388. if (isset($dom[$key]['attribute']['clip'])) {
  16389. // clipping mode
  16390. if ($dom[$key]['attribute']['clip'] == 'true') {
  16391. $dom[$key]['clip'] = true;
  16392. } else {
  16393. $dom[$key]['clip'] = false;
  16394. }
  16395. }
  16396. } // end opening tag
  16397. } else {
  16398. // text
  16399. $dom[$key]['tag'] = false;
  16400. $dom[$key]['block'] = false;
  16401. $dom[$key]['parent'] = end($level);
  16402. $dom[$key]['dir'] = $dom[$dom[$key]['parent']]['dir'];
  16403. if (!empty($dom[$dom[$key]['parent']]['text-transform'])) {
  16404. // text-transform for unicode requires mb_convert_case (Multibyte String Functions)
  16405. if (function_exists('mb_convert_case')) {
  16406. $ttm = array('capitalize' => MB_CASE_TITLE, 'uppercase' => MB_CASE_UPPER, 'lowercase' => MB_CASE_LOWER);
  16407. if (isset($ttm[$dom[$dom[$key]['parent']]['text-transform']])) {
  16408. $element = mb_convert_case($element, $ttm[$dom[$dom[$key]['parent']]['text-transform']], $this->encoding);
  16409. }
  16410. } elseif (!$this->isunicode) {
  16411. switch ($dom[$dom[$key]['parent']]['text-transform']) {
  16412. case 'capitalize': {
  16413. $element = ucwords(strtolower($element));
  16414. break;
  16415. }
  16416. case 'uppercase': {
  16417. $element = strtoupper($element);
  16418. break;
  16419. }
  16420. case 'lowercase': {
  16421. $element = strtolower($element);
  16422. break;
  16423. }
  16424. }
  16425. }
  16426. }
  16427. $dom[$key]['value'] = stripslashes($this->unhtmlentities($element));
  16428. }
  16429. ++$elkey;
  16430. ++$key;
  16431. }
  16432. return $dom;
  16433. }
  16434. /**
  16435. * Returns the string used to find spaces
  16436. * @return string
  16437. * @protected
  16438. * @author Nicola Asuni
  16439. * @since 4.8.024 (2010-01-15)
  16440. */
  16441. protected function getSpaceString() {
  16442. $spacestr = chr(32);
  16443. if ($this->isUnicodeFont()) {
  16444. $spacestr = chr(0).chr(32);
  16445. }
  16446. return $spacestr;
  16447. }
  16448. /**
  16449. * Return an hash code used to ensure that the serialized data has been generated by this TCPDF instance.
  16450. * @param $data (string) serialized data
  16451. * @return string
  16452. * @public static
  16453. */
  16454. protected function getHashForTCPDFtagParams($data) {
  16455. return md5(strlen($data).$this->file_id.$data);
  16456. }
  16457. /**
  16458. * Serialize an array of parameters to be used with TCPDF tag in HTML code.
  16459. * @param $data (array) parameters array
  16460. * @return string containing serialized data
  16461. * @public static
  16462. */
  16463. public function serializeTCPDFtagParameters($data) {
  16464. $encoded = urlencode(json_encode($data));
  16465. return $this->getHashForTCPDFtagParams($encoded).$encoded;
  16466. }
  16467. /**
  16468. * Unserialize parameters to be used with TCPDF tag in HTML code.
  16469. * @param $data (string) serialized data
  16470. * @return array containing unserialized data
  16471. * @protected static
  16472. */
  16473. protected function unserializeTCPDFtagParameters($data) {
  16474. $hash = substr($data, 0, 32);
  16475. $encoded = substr($data, 32);
  16476. if ($hash != $this->getHashForTCPDFtagParams($encoded)) {
  16477. $this->Error('Invalid parameters');
  16478. }
  16479. return json_decode(urldecode($encoded), true);
  16480. }
  16481. /**
  16482. * Prints a cell (rectangular area) with optional borders, background color and html text string.
  16483. * 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 />
  16484. * If automatic page breaking is enabled and the cell goes beyond the limit, a page break is done before outputting.
  16485. * IMPORTANT: The HTML must be well formatted - try to clean-up it using an application like HTML-Tidy before submitting.
  16486. * 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
  16487. * NOTE: all the HTML attributes must be enclosed in double-quote.
  16488. * @param $w (float) Cell width. If 0, the cell extends up to the right margin.
  16489. * @param $h (float) Cell minimum height. The cell extends automatically if needed.
  16490. * @param $x (float) upper-left corner X coordinate
  16491. * @param $y (float) upper-left corner Y coordinate
  16492. * @param $html (string) html text to print. Default value: empty string.
  16493. * @param $border (mixed) 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)))
  16494. * @param $ln (int) 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>
  16495. Putting 1 is equivalent to putting 0 and calling Ln() just after. Default value: 0.
  16496. * @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
  16497. * @param $reseth (boolean) if true reset the last cell height (default true).
  16498. * @param $align (string) 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>
  16499. * @param $autopadding (boolean) if true, uses internal padding and automatically adjust it to account for line width.
  16500. * @see Multicell(), writeHTML()
  16501. * @public
  16502. */
  16503. public function writeHTMLCell($w, $h, $x, $y, $html='', $border=0, $ln=0, $fill=false, $reseth=true, $align='', $autopadding=true) {
  16504. return $this->MultiCell($w, $h, $html, $border, $align, $fill, $ln, $x, $y, $reseth, 0, true, $autopadding, 0, 'T', false);
  16505. }
  16506. /**
  16507. * Allows to preserve some HTML formatting (limited support).<br />
  16508. * IMPORTANT: The HTML must be well formatted - try to clean-up it using an application like HTML-Tidy before submitting.
  16509. * 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
  16510. * NOTE: all the HTML attributes must be enclosed in double-quote.
  16511. * @param $html (string) text to display
  16512. * @param $ln (boolean) if true add a new line after text (default = true)
  16513. * @param $fill (boolean) Indicates if the background must be painted (true) or transparent (false).
  16514. * @param $reseth (boolean) if true reset the last cell height (default false).
  16515. * @param $cell (boolean) if true add the current left (or right for RTL) padding to each Write (default false).
  16516. * @param $align (string) 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>
  16517. * @public
  16518. */
  16519. public function writeHTML($html, $ln=true, $fill=false, $reseth=false, $cell=false, $align='') {
  16520. $gvars = $this->getGraphicVars();
  16521. // store current values
  16522. $prev_cell_margin = $this->cell_margin;
  16523. $prev_cell_padding = $this->cell_padding;
  16524. $prevPage = $this->page;
  16525. $prevlMargin = $this->lMargin;
  16526. $prevrMargin = $this->rMargin;
  16527. $curfontname = $this->FontFamily;
  16528. $curfontstyle = $this->FontStyle;
  16529. $curfontsize = $this->FontSizePt;
  16530. $curfontascent = $this->getFontAscent($curfontname, $curfontstyle, $curfontsize);
  16531. $curfontdescent = $this->getFontDescent($curfontname, $curfontstyle, $curfontsize);
  16532. $curfontstretcing = $this->font_stretching;
  16533. $curfonttracking = $this->font_spacing;
  16534. $this->newline = true;
  16535. $newline = true;
  16536. $startlinepage = $this->page;
  16537. $minstartliney = $this->y;
  16538. $maxbottomliney = 0;
  16539. $startlinex = $this->x;
  16540. $startliney = $this->y;
  16541. $yshift = 0;
  16542. $loop = 0;
  16543. $curpos = 0;
  16544. $this_method_vars = array();
  16545. $undo = false;
  16546. $fontaligned = false;
  16547. $reverse_dir = false; // true when the text direction is reversed
  16548. $this->premode = false;
  16549. if ($this->inxobj) {
  16550. // we are inside an XObject template
  16551. $pask = count($this->xobjects[$this->xobjid]['annotations']);
  16552. } elseif (isset($this->PageAnnots[$this->page])) {
  16553. $pask = count($this->PageAnnots[$this->page]);
  16554. } else {
  16555. $pask = 0;
  16556. }
  16557. if ($this->inxobj) {
  16558. // we are inside an XObject template
  16559. $startlinepos = strlen($this->xobjects[$this->xobjid]['outdata']);
  16560. } elseif (!$this->InFooter) {
  16561. if (isset($this->footerlen[$this->page])) {
  16562. $this->footerpos[$this->page] = $this->pagelen[$this->page] - $this->footerlen[$this->page];
  16563. } else {
  16564. $this->footerpos[$this->page] = $this->pagelen[$this->page];
  16565. }
  16566. $startlinepos = $this->footerpos[$this->page];
  16567. } else {
  16568. // we are inside the footer
  16569. $startlinepos = $this->pagelen[$this->page];
  16570. }
  16571. $lalign = $align;
  16572. $plalign = $align;
  16573. if ($this->rtl) {
  16574. $w = $this->x - $this->lMargin;
  16575. } else {
  16576. $w = $this->w - $this->rMargin - $this->x;
  16577. }
  16578. $w -= ($this->cell_padding['L'] + $this->cell_padding['R']);
  16579. if ($cell) {
  16580. if ($this->rtl) {
  16581. $this->x -= $this->cell_padding['R'];
  16582. $this->lMargin += $this->cell_padding['L'];
  16583. } else {
  16584. $this->x += $this->cell_padding['L'];
  16585. $this->rMargin += $this->cell_padding['R'];
  16586. }
  16587. }
  16588. if ($this->customlistindent >= 0) {
  16589. $this->listindent = $this->customlistindent;
  16590. } else {
  16591. $this->listindent = $this->GetStringWidth('000000');
  16592. }
  16593. $this->listindentlevel = 0;
  16594. // save previous states
  16595. $prev_cell_height_ratio = $this->cell_height_ratio;
  16596. $prev_listnum = $this->listnum;
  16597. $prev_listordered = $this->listordered;
  16598. $prev_listcount = $this->listcount;
  16599. $prev_lispacer = $this->lispacer;
  16600. $this->listnum = 0;
  16601. $this->listordered = array();
  16602. $this->listcount = array();
  16603. $this->lispacer = '';
  16604. if ((TCPDF_STATIC::empty_string($this->lasth)) OR ($reseth)) {
  16605. // reset row height
  16606. $this->resetLastH();
  16607. }
  16608. $dom = $this->getHtmlDomArray($html);
  16609. $maxel = count($dom);
  16610. $key = 0;
  16611. while ($key < $maxel) {
  16612. if ($dom[$key]['tag'] AND $dom[$key]['opening'] AND $dom[$key]['hide']) {
  16613. // store the node key
  16614. $hidden_node_key = $key;
  16615. if ($dom[$key]['self']) {
  16616. // skip just this self-closing tag
  16617. ++$key;
  16618. } else {
  16619. // skip this and all children tags
  16620. while (($key < $maxel) AND (!$dom[$key]['tag'] OR $dom[$key]['opening'] OR ($dom[$key]['parent'] != $hidden_node_key))) {
  16621. // skip hidden objects
  16622. ++$key;
  16623. }
  16624. ++$key;
  16625. }
  16626. }
  16627. if ($dom[$key]['tag'] AND isset($dom[$key]['attribute']['pagebreak'])) {
  16628. // check for pagebreak
  16629. if (($dom[$key]['attribute']['pagebreak'] == 'true') OR ($dom[$key]['attribute']['pagebreak'] == 'left') OR ($dom[$key]['attribute']['pagebreak'] == 'right')) {
  16630. // add a page (or trig AcceptPageBreak() for multicolumn mode)
  16631. $this->checkPageBreak($this->PageBreakTrigger + 1);
  16632. $this->htmlvspace = ($this->PageBreakTrigger + 1);
  16633. }
  16634. if ((($dom[$key]['attribute']['pagebreak'] == 'left') AND (((!$this->rtl) AND (($this->page % 2) == 0)) OR (($this->rtl) AND (($this->page % 2) != 0))))
  16635. OR (($dom[$key]['attribute']['pagebreak'] == 'right') AND (((!$this->rtl) AND (($this->page % 2) != 0)) OR (($this->rtl) AND (($this->page % 2) == 0))))) {
  16636. // add a page (or trig AcceptPageBreak() for multicolumn mode)
  16637. $this->checkPageBreak($this->PageBreakTrigger + 1);
  16638. $this->htmlvspace = ($this->PageBreakTrigger + 1);
  16639. }
  16640. }
  16641. if ($dom[$key]['tag'] AND $dom[$key]['opening'] AND isset($dom[$key]['attribute']['nobr']) AND ($dom[$key]['attribute']['nobr'] == 'true')) {
  16642. if (isset($dom[($dom[$key]['parent'])]['attribute']['nobr']) AND ($dom[($dom[$key]['parent'])]['attribute']['nobr'] == 'true')) {
  16643. $dom[$key]['attribute']['nobr'] = false;
  16644. } else {
  16645. // store current object
  16646. $this->startTransaction();
  16647. // save this method vars
  16648. $this_method_vars['html'] = $html;
  16649. $this_method_vars['ln'] = $ln;
  16650. $this_method_vars['fill'] = $fill;
  16651. $this_method_vars['reseth'] = $reseth;
  16652. $this_method_vars['cell'] = $cell;
  16653. $this_method_vars['align'] = $align;
  16654. $this_method_vars['gvars'] = $gvars;
  16655. $this_method_vars['prevPage'] = $prevPage;
  16656. $this_method_vars['prev_cell_margin'] = $prev_cell_margin;
  16657. $this_method_vars['prev_cell_padding'] = $prev_cell_padding;
  16658. $this_method_vars['prevlMargin'] = $prevlMargin;
  16659. $this_method_vars['prevrMargin'] = $prevrMargin;
  16660. $this_method_vars['curfontname'] = $curfontname;
  16661. $this_method_vars['curfontstyle'] = $curfontstyle;
  16662. $this_method_vars['curfontsize'] = $curfontsize;
  16663. $this_method_vars['curfontascent'] = $curfontascent;
  16664. $this_method_vars['curfontdescent'] = $curfontdescent;
  16665. $this_method_vars['curfontstretcing'] = $curfontstretcing;
  16666. $this_method_vars['curfonttracking'] = $curfonttracking;
  16667. $this_method_vars['minstartliney'] = $minstartliney;
  16668. $this_method_vars['maxbottomliney'] = $maxbottomliney;
  16669. $this_method_vars['yshift'] = $yshift;
  16670. $this_method_vars['startlinepage'] = $startlinepage;
  16671. $this_method_vars['startlinepos'] = $startlinepos;
  16672. $this_method_vars['startlinex'] = $startlinex;
  16673. $this_method_vars['startliney'] = $startliney;
  16674. $this_method_vars['newline'] = $newline;
  16675. $this_method_vars['loop'] = $loop;
  16676. $this_method_vars['curpos'] = $curpos;
  16677. $this_method_vars['pask'] = $pask;
  16678. $this_method_vars['lalign'] = $lalign;
  16679. $this_method_vars['plalign'] = $plalign;
  16680. $this_method_vars['w'] = $w;
  16681. $this_method_vars['prev_cell_height_ratio'] = $prev_cell_height_ratio;
  16682. $this_method_vars['prev_listnum'] = $prev_listnum;
  16683. $this_method_vars['prev_listordered'] = $prev_listordered;
  16684. $this_method_vars['prev_listcount'] = $prev_listcount;
  16685. $this_method_vars['prev_lispacer'] = $prev_lispacer;
  16686. $this_method_vars['fontaligned'] = $fontaligned;
  16687. $this_method_vars['key'] = $key;
  16688. $this_method_vars['dom'] = $dom;
  16689. }
  16690. }
  16691. // print THEAD block
  16692. if (($dom[$key]['value'] == 'tr') AND isset($dom[$key]['thead']) AND $dom[$key]['thead']) {
  16693. if (isset($dom[$key]['parent']) AND isset($dom[$dom[$key]['parent']]['thead']) AND !TCPDF_STATIC::empty_string($dom[$dom[$key]['parent']]['thead'])) {
  16694. $this->inthead = true;
  16695. // print table header (thead)
  16696. $this->writeHTML($this->thead, false, false, false, false, '');
  16697. // check if we are on a new page or on a new column
  16698. if (($this->y < $this->start_transaction_y) OR ($this->checkPageBreak($this->lasth, '', false))) {
  16699. // we are on a new page or on a new column and the total object height is less than the available vertical space.
  16700. // restore previous object
  16701. $this->rollbackTransaction(true);
  16702. // restore previous values
  16703. foreach ($this_method_vars as $vkey => $vval) {
  16704. $$vkey = $vval;
  16705. }
  16706. // disable table header
  16707. $tmp_thead = $this->thead;
  16708. $this->thead = '';
  16709. // add a page (or trig AcceptPageBreak() for multicolumn mode)
  16710. $pre_y = $this->y;
  16711. if ((!$this->checkPageBreak($this->PageBreakTrigger + 1)) AND ($this->y < $pre_y)) {
  16712. // fix for multicolumn mode
  16713. $startliney = $this->y;
  16714. }
  16715. $this->start_transaction_page = $this->page;
  16716. $this->start_transaction_y = $this->y;
  16717. // restore table header
  16718. $this->thead = $tmp_thead;
  16719. // fix table border properties
  16720. if (isset($dom[$dom[$key]['parent']]['attribute']['cellspacing'])) {
  16721. $tmp_cellspacing = $this->getHTMLUnitToUnits($dom[$dom[$key]['parent']]['attribute']['cellspacing'], 1, 'px');
  16722. } elseif (isset($dom[$dom[$key]['parent']]['border-spacing'])) {
  16723. $tmp_cellspacing = $dom[$dom[$key]['parent']]['border-spacing']['V'];
  16724. } else {
  16725. $tmp_cellspacing = 0;
  16726. }
  16727. $dom[$dom[$key]['parent']]['borderposition']['page'] = $this->page;
  16728. $dom[$dom[$key]['parent']]['borderposition']['column'] = $this->current_column;
  16729. $dom[$dom[$key]['parent']]['borderposition']['y'] = $this->y + $tmp_cellspacing;
  16730. $xoffset = ($this->x - $dom[$dom[$key]['parent']]['borderposition']['x']);
  16731. $dom[$dom[$key]['parent']]['borderposition']['x'] += $xoffset;
  16732. $dom[$dom[$key]['parent']]['borderposition']['xmax'] += $xoffset;
  16733. // print table header (thead)
  16734. $this->writeHTML($this->thead, false, false, false, false, '');
  16735. }
  16736. }
  16737. // move $key index forward to skip THEAD block
  16738. while ( ($key < $maxel) AND (!(
  16739. ($dom[$key]['tag'] AND $dom[$key]['opening'] AND ($dom[$key]['value'] == 'tr') AND (!isset($dom[$key]['thead']) OR !$dom[$key]['thead']))
  16740. OR ($dom[$key]['tag'] AND (!$dom[$key]['opening']) AND ($dom[$key]['value'] == 'table'))) )) {
  16741. ++$key;
  16742. }
  16743. }
  16744. if ($dom[$key]['tag'] OR ($key == 0)) {
  16745. if ((($dom[$key]['value'] == 'table') OR ($dom[$key]['value'] == 'tr')) AND (isset($dom[$key]['align']))) {
  16746. $dom[$key]['align'] = ($this->rtl) ? 'R' : 'L';
  16747. }
  16748. // vertically align image in line
  16749. if ((!$this->newline) AND ($dom[$key]['value'] == 'img') AND (isset($dom[$key]['height'])) AND ($dom[$key]['height'] > 0)) {
  16750. // get image height
  16751. $imgh = $this->getHTMLUnitToUnits($dom[$key]['height'], ($dom[$key]['fontsize'] / $this->k), 'px');
  16752. $autolinebreak = false;
  16753. if (!empty($dom[$key]['width'])) {
  16754. $imgw = $this->getHTMLUnitToUnits($dom[$key]['width'], ($dom[$key]['fontsize'] / $this->k), 'px', false);
  16755. if (($imgw <= ($this->w - $this->lMargin - $this->rMargin - $this->cell_padding['L'] - $this->cell_padding['R']))
  16756. AND ((($this->rtl) AND (($this->x - $imgw) < ($this->lMargin + $this->cell_padding['L'])))
  16757. OR ((!$this->rtl) AND (($this->x + $imgw) > ($this->w - $this->rMargin - $this->cell_padding['R']))))) {
  16758. // add automatic line break
  16759. $autolinebreak = true;
  16760. $this->Ln('', $cell);
  16761. if ((!$dom[($key-1)]['tag']) AND ($dom[($key-1)]['value'] == ' ')) {
  16762. // go back to evaluate this line break
  16763. --$key;
  16764. }
  16765. }
  16766. }
  16767. if (!$autolinebreak) {
  16768. if ($this->inPageBody()) {
  16769. $pre_y = $this->y;
  16770. // check for page break
  16771. if ((!$this->checkPageBreak($imgh)) AND ($this->y < $pre_y)) {
  16772. // fix for multicolumn mode
  16773. $startliney = $this->y;
  16774. }
  16775. }
  16776. if ($this->page > $startlinepage) {
  16777. // fix line splitted over two pages
  16778. if (isset($this->footerlen[$startlinepage])) {
  16779. $curpos = $this->pagelen[$startlinepage] - $this->footerlen[$startlinepage];
  16780. }
  16781. // line to be moved one page forward
  16782. $pagebuff = $this->getPageBuffer($startlinepage);
  16783. $linebeg = substr($pagebuff, $startlinepos, ($curpos - $startlinepos));
  16784. $tstart = substr($pagebuff, 0, $startlinepos);
  16785. $tend = substr($this->getPageBuffer($startlinepage), $curpos);
  16786. // remove line from previous page
  16787. $this->setPageBuffer($startlinepage, $tstart.''.$tend);
  16788. $pagebuff = $this->getPageBuffer($this->page);
  16789. $tstart = substr($pagebuff, 0, $this->cntmrk[$this->page]);
  16790. $tend = substr($pagebuff, $this->cntmrk[$this->page]);
  16791. // add line start to current page
  16792. $yshift = ($minstartliney - $this->y);
  16793. if ($fontaligned) {
  16794. $yshift += ($curfontsize / $this->k);
  16795. }
  16796. $try = sprintf('1 0 0 1 0 %F cm', ($yshift * $this->k));
  16797. $this->setPageBuffer($this->page, $tstart."\nq\n".$try."\n".$linebeg."\nQ\n".$tend);
  16798. // shift the annotations and links
  16799. if (isset($this->PageAnnots[$this->page])) {
  16800. $next_pask = count($this->PageAnnots[$this->page]);
  16801. } else {
  16802. $next_pask = 0;
  16803. }
  16804. if (isset($this->PageAnnots[$startlinepage])) {
  16805. foreach ($this->PageAnnots[$startlinepage] as $pak => $pac) {
  16806. if ($pak >= $pask) {
  16807. $this->PageAnnots[$this->page][] = $pac;
  16808. unset($this->PageAnnots[$startlinepage][$pak]);
  16809. $npak = count($this->PageAnnots[$this->page]) - 1;
  16810. $this->PageAnnots[$this->page][$npak]['y'] -= $yshift;
  16811. }
  16812. }
  16813. }
  16814. $pask = $next_pask;
  16815. $startlinepos = $this->cntmrk[$this->page];
  16816. $startlinepage = $this->page;
  16817. $startliney = $this->y;
  16818. $this->newline = false;
  16819. }
  16820. $this->y += ($this->getCellHeight($curfontsize / $this->k) - ($curfontdescent * $this->cell_height_ratio) - $imgh);
  16821. $minstartliney = min($this->y, $minstartliney);
  16822. $maxbottomliney = ($startliney + $this->getCellHeight($curfontsize / $this->k));
  16823. }
  16824. } elseif (isset($dom[$key]['fontname']) OR isset($dom[$key]['fontstyle']) OR isset($dom[$key]['fontsize']) OR isset($dom[$key]['line-height'])) {
  16825. // account for different font size
  16826. $pfontname = $curfontname;
  16827. $pfontstyle = $curfontstyle;
  16828. $pfontsize = $curfontsize;
  16829. $fontname = (isset($dom[$key]['fontname']) ? $dom[$key]['fontname'] : $curfontname);
  16830. $fontstyle = (isset($dom[$key]['fontstyle']) ? $dom[$key]['fontstyle'] : $curfontstyle);
  16831. $fontsize = (isset($dom[$key]['fontsize']) ? $dom[$key]['fontsize'] : $curfontsize);
  16832. $fontascent = $this->getFontAscent($fontname, $fontstyle, $fontsize);
  16833. $fontdescent = $this->getFontDescent($fontname, $fontstyle, $fontsize);
  16834. if (($fontname != $curfontname) OR ($fontstyle != $curfontstyle) OR ($fontsize != $curfontsize)
  16835. OR ($this->cell_height_ratio != $dom[$key]['line-height'])
  16836. OR ($dom[$key]['tag'] AND $dom[$key]['opening'] AND ($dom[$key]['value'] == 'li')) ) {
  16837. if (($key < ($maxel - 1)) AND (
  16838. ($dom[$key]['tag'] AND $dom[$key]['opening'] AND ($dom[$key]['value'] == 'li'))
  16839. OR ($this->cell_height_ratio != $dom[$key]['line-height'])
  16840. OR (!$this->newline AND is_numeric($fontsize) AND is_numeric($curfontsize)
  16841. AND ($fontsize >= 0) AND ($curfontsize >= 0)
  16842. AND (($fontsize != $curfontsize) OR ($fontstyle != $curfontstyle) OR ($fontname != $curfontname)))
  16843. )) {
  16844. if ($this->page > $startlinepage) {
  16845. // fix lines splitted over two pages
  16846. if (isset($this->footerlen[$startlinepage])) {
  16847. $curpos = $this->pagelen[$startlinepage] - $this->footerlen[$startlinepage];
  16848. }
  16849. // line to be moved one page forward
  16850. $pagebuff = $this->getPageBuffer($startlinepage);
  16851. $linebeg = substr($pagebuff, $startlinepos, ($curpos - $startlinepos));
  16852. $tstart = substr($pagebuff, 0, $startlinepos);
  16853. $tend = substr($this->getPageBuffer($startlinepage), $curpos);
  16854. // remove line start from previous page
  16855. $this->setPageBuffer($startlinepage, $tstart.''.$tend);
  16856. $pagebuff = $this->getPageBuffer($this->page);
  16857. $tstart = substr($pagebuff, 0, $this->cntmrk[$this->page]);
  16858. $tend = substr($pagebuff, $this->cntmrk[$this->page]);
  16859. // add line start to current page
  16860. $yshift = ($minstartliney - $this->y);
  16861. $try = sprintf('1 0 0 1 0 %F cm', ($yshift * $this->k));
  16862. $this->setPageBuffer($this->page, $tstart."\nq\n".$try."\n".$linebeg."\nQ\n".$tend);
  16863. // shift the annotations and links
  16864. if (isset($this->PageAnnots[$this->page])) {
  16865. $next_pask = count($this->PageAnnots[$this->page]);
  16866. } else {
  16867. $next_pask = 0;
  16868. }
  16869. if (isset($this->PageAnnots[$startlinepage])) {
  16870. foreach ($this->PageAnnots[$startlinepage] as $pak => $pac) {
  16871. if ($pak >= $pask) {
  16872. $this->PageAnnots[$this->page][] = $pac;
  16873. unset($this->PageAnnots[$startlinepage][$pak]);
  16874. $npak = count($this->PageAnnots[$this->page]) - 1;
  16875. $this->PageAnnots[$this->page][$npak]['y'] -= $yshift;
  16876. }
  16877. }
  16878. }
  16879. $pask = $next_pask;
  16880. $startlinepos = $this->cntmrk[$this->page];
  16881. $startlinepage = $this->page;
  16882. $startliney = $this->y;
  16883. }
  16884. if (!isset($dom[$key]['line-height'])) {
  16885. $dom[$key]['line-height'] = $this->cell_height_ratio;
  16886. }
  16887. if (!$dom[$key]['block']) {
  16888. 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']))) {
  16889. $this->y += (((($curfontsize * $this->cell_height_ratio) - ($fontsize * $dom[$key]['line-height'])) / $this->k) + $curfontascent - $fontascent - $curfontdescent + $fontdescent) / 2;
  16890. }
  16891. if (($dom[$key]['value'] != 'sup') AND ($dom[$key]['value'] != 'sub')) {
  16892. $current_line_align_data = array($key, $minstartliney, $maxbottomliney);
  16893. 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)))) {
  16894. $minstartliney = min($this->y, $line_align_data[1]);
  16895. $maxbottomliney = max(($this->y + $this->getCellHeight($fontsize / $this->k)), $line_align_data[2]);
  16896. } else {
  16897. $minstartliney = min($this->y, $minstartliney);
  16898. $maxbottomliney = max(($this->y + $this->getCellHeight($fontsize / $this->k)), $maxbottomliney);
  16899. }
  16900. $line_align_data = $current_line_align_data;
  16901. }
  16902. }
  16903. $this->cell_height_ratio = $dom[$key]['line-height'];
  16904. $fontaligned = true;
  16905. }
  16906. $this->SetFont($fontname, $fontstyle, $fontsize);
  16907. // reset row height
  16908. $this->resetLastH();
  16909. $curfontname = $fontname;
  16910. $curfontstyle = $fontstyle;
  16911. $curfontsize = $fontsize;
  16912. $curfontascent = $fontascent;
  16913. $curfontdescent = $fontdescent;
  16914. }
  16915. }
  16916. // set text rendering mode
  16917. $textstroke = isset($dom[$key]['stroke']) ? $dom[$key]['stroke'] : $this->textstrokewidth;
  16918. $textfill = isset($dom[$key]['fill']) ? $dom[$key]['fill'] : (($this->textrendermode % 2) == 0);
  16919. $textclip = isset($dom[$key]['clip']) ? $dom[$key]['clip'] : ($this->textrendermode > 3);
  16920. $this->setTextRenderingMode($textstroke, $textfill, $textclip);
  16921. if (isset($dom[$key]['font-stretch']) AND ($dom[$key]['font-stretch'] !== false)) {
  16922. $this->setFontStretching($dom[$key]['font-stretch']);
  16923. }
  16924. if (isset($dom[$key]['letter-spacing']) AND ($dom[$key]['letter-spacing'] !== false)) {
  16925. $this->setFontSpacing($dom[$key]['letter-spacing']);
  16926. }
  16927. if (($plalign == 'J') AND $dom[$key]['block']) {
  16928. $plalign = '';
  16929. }
  16930. // get current position on page buffer
  16931. $curpos = $this->pagelen[$startlinepage];
  16932. if (isset($dom[$key]['bgcolor']) AND ($dom[$key]['bgcolor'] !== false)) {
  16933. $this->SetFillColorArray($dom[$key]['bgcolor']);
  16934. $wfill = true;
  16935. } else {
  16936. $wfill = $fill | false;
  16937. }
  16938. if (isset($dom[$key]['fgcolor']) AND ($dom[$key]['fgcolor'] !== false)) {
  16939. $this->SetTextColorArray($dom[$key]['fgcolor']);
  16940. }
  16941. if (isset($dom[$key]['strokecolor']) AND ($dom[$key]['strokecolor'] !== false)) {
  16942. $this->SetDrawColorArray($dom[$key]['strokecolor']);
  16943. }
  16944. if (isset($dom[$key]['align'])) {
  16945. $lalign = $dom[$key]['align'];
  16946. }
  16947. if (TCPDF_STATIC::empty_string($lalign)) {
  16948. $lalign = $align;
  16949. }
  16950. }
  16951. // align lines
  16952. if ($this->newline AND (strlen($dom[$key]['value']) > 0) AND ($dom[$key]['value'] != 'td') AND ($dom[$key]['value'] != 'th')) {
  16953. $newline = true;
  16954. $fontaligned = false;
  16955. // we are at the beginning of a new line
  16956. if (isset($startlinex)) {
  16957. $yshift = ($minstartliney - $startliney);
  16958. if (($yshift > 0) OR ($this->page > $startlinepage)) {
  16959. $yshift = 0;
  16960. }
  16961. $t_x = 0;
  16962. // the last line must be shifted to be aligned as requested
  16963. $linew = abs($this->endlinex - $startlinex);
  16964. if ($this->inxobj) {
  16965. // we are inside an XObject template
  16966. $pstart = substr($this->xobjects[$this->xobjid]['outdata'], 0, $startlinepos);
  16967. if (isset($opentagpos)) {
  16968. $midpos = $opentagpos;
  16969. } else {
  16970. $midpos = 0;
  16971. }
  16972. if ($midpos > 0) {
  16973. $pmid = substr($this->xobjects[$this->xobjid]['outdata'], $startlinepos, ($midpos - $startlinepos));
  16974. $pend = substr($this->xobjects[$this->xobjid]['outdata'], $midpos);
  16975. } else {
  16976. $pmid = substr($this->xobjects[$this->xobjid]['outdata'], $startlinepos);
  16977. $pend = '';
  16978. }
  16979. } else {
  16980. $pstart = substr($this->getPageBuffer($startlinepage), 0, $startlinepos);
  16981. if (isset($opentagpos) AND isset($this->footerlen[$startlinepage]) AND (!$this->InFooter)) {
  16982. $this->footerpos[$startlinepage] = $this->pagelen[$startlinepage] - $this->footerlen[$startlinepage];
  16983. $midpos = min($opentagpos, $this->footerpos[$startlinepage]);
  16984. } elseif (isset($opentagpos)) {
  16985. $midpos = $opentagpos;
  16986. } elseif (isset($this->footerlen[$startlinepage]) AND (!$this->InFooter)) {
  16987. $this->footerpos[$startlinepage] = $this->pagelen[$startlinepage] - $this->footerlen[$startlinepage];
  16988. $midpos = $this->footerpos[$startlinepage];
  16989. } else {
  16990. $midpos = 0;
  16991. }
  16992. if ($midpos > 0) {
  16993. $pmid = substr($this->getPageBuffer($startlinepage), $startlinepos, ($midpos - $startlinepos));
  16994. $pend = substr($this->getPageBuffer($startlinepage), $midpos);
  16995. } else {
  16996. $pmid = substr($this->getPageBuffer($startlinepage), $startlinepos);
  16997. $pend = '';
  16998. }
  16999. }
  17000. if ((isset($plalign) AND ((($plalign == 'C') OR ($plalign == 'J') OR (($plalign == 'R') AND (!$this->rtl)) OR (($plalign == 'L') AND ($this->rtl)))))) {
  17001. // calculate shifting amount
  17002. $tw = $w;
  17003. if (($plalign == 'J') AND $this->isRTLTextDir() AND ($this->num_columns > 1)) {
  17004. $tw += $this->cell_padding['R'];
  17005. }
  17006. if ($this->lMargin != $prevlMargin) {
  17007. $tw += ($prevlMargin - $this->lMargin);
  17008. }
  17009. if ($this->rMargin != $prevrMargin) {
  17010. $tw += ($prevrMargin - $this->rMargin);
  17011. }
  17012. $one_space_width = $this->GetStringWidth(chr(32));
  17013. $no = 0; // number of spaces on a line contained on a single block
  17014. if ($this->isRTLTextDir()) { // RTL
  17015. // remove left space if exist
  17016. $pos1 = TCPDF_STATIC::revstrpos($pmid, '[(');
  17017. if ($pos1 > 0) {
  17018. $pos1 = intval($pos1);
  17019. if ($this->isUnicodeFont()) {
  17020. $pos2 = intval(TCPDF_STATIC::revstrpos($pmid, '[('.chr(0).chr(32)));
  17021. $spacelen = 2;
  17022. } else {
  17023. $pos2 = intval(TCPDF_STATIC::revstrpos($pmid, '[('.chr(32)));
  17024. $spacelen = 1;
  17025. }
  17026. if ($pos1 == $pos2) {
  17027. $pmid = substr($pmid, 0, ($pos1 + 2)).substr($pmid, ($pos1 + 2 + $spacelen));
  17028. if (substr($pmid, $pos1, 4) == '[()]') {
  17029. $linew -= $one_space_width;
  17030. } elseif ($pos1 == strpos($pmid, '[(')) {
  17031. $no = 1;
  17032. }
  17033. }
  17034. }
  17035. } else { // LTR
  17036. // remove right space if exist
  17037. $pos1 = TCPDF_STATIC::revstrpos($pmid, ')]');
  17038. if ($pos1 > 0) {
  17039. $pos1 = intval($pos1);
  17040. if ($this->isUnicodeFont()) {
  17041. $pos2 = intval(TCPDF_STATIC::revstrpos($pmid, chr(0).chr(32).')]')) + 2;
  17042. $spacelen = 2;
  17043. } else {
  17044. $pos2 = intval(TCPDF_STATIC::revstrpos($pmid, chr(32).')]')) + 1;
  17045. $spacelen = 1;
  17046. }
  17047. if ($pos1 == $pos2) {
  17048. $pmid = substr($pmid, 0, ($pos1 - $spacelen)).substr($pmid, $pos1);
  17049. $linew -= $one_space_width;
  17050. }
  17051. }
  17052. }
  17053. $mdiff = ($tw - $linew);
  17054. if ($plalign == 'C') {
  17055. if ($this->rtl) {
  17056. $t_x = -($mdiff / 2);
  17057. } else {
  17058. $t_x = ($mdiff / 2);
  17059. }
  17060. } elseif ($plalign == 'R') {
  17061. // right alignment on LTR document
  17062. $t_x = $mdiff;
  17063. } elseif ($plalign == 'L') {
  17064. // left alignment on RTL document
  17065. $t_x = -$mdiff;
  17066. } elseif (($plalign == 'J') AND ($plalign == $lalign)) {
  17067. // Justification
  17068. if ($this->isRTLTextDir()) {
  17069. // align text on the left
  17070. $t_x = -$mdiff;
  17071. }
  17072. $ns = 0; // number of spaces
  17073. $pmidtemp = $pmid;
  17074. // escape special characters
  17075. $pmidtemp = preg_replace('/[\\\][\(]/x', '\\#!#OP#!#', $pmidtemp);
  17076. $pmidtemp = preg_replace('/[\\\][\)]/x', '\\#!#CP#!#', $pmidtemp);
  17077. // search spaces
  17078. if (preg_match_all('/\[\(([^\)]*)\)\]/x', $pmidtemp, $lnstring, PREG_PATTERN_ORDER)) {
  17079. $spacestr = $this->getSpaceString();
  17080. $maxkk = count($lnstring[1]) - 1;
  17081. for ($kk=0; $kk <= $maxkk; ++$kk) {
  17082. // restore special characters
  17083. $lnstring[1][$kk] = str_replace('#!#OP#!#', '(', $lnstring[1][$kk]);
  17084. $lnstring[1][$kk] = str_replace('#!#CP#!#', ')', $lnstring[1][$kk]);
  17085. // store number of spaces on the strings
  17086. $lnstring[2][$kk] = substr_count($lnstring[1][$kk], $spacestr);
  17087. // count total spaces on line
  17088. $ns += $lnstring[2][$kk];
  17089. $lnstring[3][$kk] = $ns;
  17090. }
  17091. if ($ns == 0) {
  17092. $ns = 1;
  17093. }
  17094. // calculate additional space to add to each existing space
  17095. $spacewidth = ($mdiff / ($ns - $no)) * $this->k;
  17096. if ($this->FontSize <= 0) {
  17097. $this->FontSize = 1;
  17098. }
  17099. $spacewidthu = -1000 * ($mdiff + (($ns + $no) * $one_space_width)) / $ns / $this->FontSize;
  17100. if ($this->font_spacing != 0) {
  17101. // fixed spacing mode
  17102. $osw = -1000 * $this->font_spacing / $this->FontSize;
  17103. $spacewidthu += $osw;
  17104. }
  17105. $nsmax = $ns;
  17106. $ns = 0;
  17107. reset($lnstring);
  17108. $offset = 0;
  17109. $strcount = 0;
  17110. $prev_epsposbeg = 0;
  17111. $textpos = 0;
  17112. if ($this->isRTLTextDir()) {
  17113. $textpos = $this->wPt;
  17114. }
  17115. while (preg_match('/([0-9\.\+\-]*)[\s](Td|cm|m|l|c|re)[\s]/x', $pmid, $strpiece, PREG_OFFSET_CAPTURE, $offset) == 1) {
  17116. // check if we are inside a string section '[( ... )]'
  17117. $stroffset = strpos($pmid, '[(', $offset);
  17118. if (($stroffset !== false) AND ($stroffset <= $strpiece[2][1])) {
  17119. // set offset to the end of string section
  17120. $offset = strpos($pmid, ')]', $stroffset);
  17121. while (($offset !== false) AND ($pmid[($offset - 1)] == '\\')) {
  17122. $offset = strpos($pmid, ')]', ($offset + 1));
  17123. }
  17124. if ($offset === false) {
  17125. $this->Error('HTML Justification: malformed PDF code.');
  17126. }
  17127. continue;
  17128. }
  17129. if ($this->isRTLTextDir()) {
  17130. $spacew = ($spacewidth * ($nsmax - $ns));
  17131. } else {
  17132. $spacew = ($spacewidth * $ns);
  17133. }
  17134. $offset = $strpiece[2][1] + strlen($strpiece[2][0]);
  17135. $epsposend = strpos($pmid, $this->epsmarker.'Q', $offset);
  17136. if ($epsposend !== null) {
  17137. $epsposend += strlen($this->epsmarker.'Q');
  17138. $epsposbeg = strpos($pmid, 'q'.$this->epsmarker, $offset);
  17139. if ($epsposbeg === null) {
  17140. $epsposbeg = strpos($pmid, 'q'.$this->epsmarker, ($prev_epsposbeg - 6));
  17141. $prev_epsposbeg = $epsposbeg;
  17142. }
  17143. if (($epsposbeg > 0) AND ($epsposend > 0) AND ($offset > $epsposbeg) AND ($offset < $epsposend)) {
  17144. // shift EPS images
  17145. $trx = sprintf('1 0 0 1 %F 0 cm', $spacew);
  17146. $pmid_b = substr($pmid, 0, $epsposbeg);
  17147. $pmid_m = substr($pmid, $epsposbeg, ($epsposend - $epsposbeg));
  17148. $pmid_e = substr($pmid, $epsposend);
  17149. $pmid = $pmid_b."\nq\n".$trx."\n".$pmid_m."\nQ\n".$pmid_e;
  17150. $offset = $epsposend;
  17151. continue;
  17152. }
  17153. }
  17154. $currentxpos = 0;
  17155. // shift blocks of code
  17156. switch ($strpiece[2][0]) {
  17157. case 'Td':
  17158. case 'cm':
  17159. case 'm':
  17160. case 'l': {
  17161. // get current X position
  17162. preg_match('/([0-9\.\+\-]*)[\s]('.$strpiece[1][0].')[\s]('.$strpiece[2][0].')([\s]*)/x', $pmid, $xmatches);
  17163. if (!isset($xmatches[1])) {
  17164. break;
  17165. }
  17166. $currentxpos = $xmatches[1];
  17167. $textpos = $currentxpos;
  17168. if (($strcount <= $maxkk) AND ($strpiece[2][0] == 'Td')) {
  17169. $ns = $lnstring[3][$strcount];
  17170. if ($this->isRTLTextDir()) {
  17171. $spacew = ($spacewidth * ($nsmax - $ns));
  17172. }
  17173. ++$strcount;
  17174. }
  17175. // justify block
  17176. if (preg_match('/([0-9\.\+\-]*)[\s]('.$strpiece[1][0].')[\s]('.$strpiece[2][0].')([\s]*)/x', $pmid, $pmatch) == 1) {
  17177. $newpmid = sprintf('%F',(floatval($pmatch[1]) + $spacew)).' '.$pmatch[2].' x*#!#*x'.$pmatch[3].$pmatch[4];
  17178. $pmid = str_replace($pmatch[0], $newpmid, $pmid);
  17179. unset($pmatch, $newpmid);
  17180. }
  17181. break;
  17182. }
  17183. case 're': {
  17184. // justify block
  17185. if (!TCPDF_STATIC::empty_string($this->lispacer)) {
  17186. $this->lispacer = '';
  17187. continue;
  17188. }
  17189. preg_match('/([0-9\.\+\-]*)[\s]([0-9\.\+\-]*)[\s]([0-9\.\+\-]*)[\s]('.$strpiece[1][0].')[\s](re)([\s]*)/x', $pmid, $xmatches);
  17190. if (!isset($xmatches[1])) {
  17191. break;
  17192. }
  17193. $currentxpos = $xmatches[1];
  17194. $x_diff = 0;
  17195. $w_diff = 0;
  17196. if ($this->isRTLTextDir()) { // RTL
  17197. if ($currentxpos < $textpos) {
  17198. $x_diff = ($spacewidth * ($nsmax - $lnstring[3][$strcount]));
  17199. $w_diff = ($spacewidth * $lnstring[2][$strcount]);
  17200. } else {
  17201. if ($strcount > 0) {
  17202. $x_diff = ($spacewidth * ($nsmax - $lnstring[3][($strcount - 1)]));
  17203. $w_diff = ($spacewidth * $lnstring[2][($strcount - 1)]);
  17204. }
  17205. }
  17206. } else { // LTR
  17207. if ($currentxpos > $textpos) {
  17208. if ($strcount > 0) {
  17209. $x_diff = ($spacewidth * $lnstring[3][($strcount - 1)]);
  17210. }
  17211. $w_diff = ($spacewidth * $lnstring[2][$strcount]);
  17212. } else {
  17213. if ($strcount > 1) {
  17214. $x_diff = ($spacewidth * $lnstring[3][($strcount - 2)]);
  17215. }
  17216. if ($strcount > 0) {
  17217. $w_diff = ($spacewidth * $lnstring[2][($strcount - 1)]);
  17218. }
  17219. }
  17220. }
  17221. if (preg_match('/('.$xmatches[1].')[\s]('.$xmatches[2].')[\s]('.$xmatches[3].')[\s]('.$strpiece[1][0].')[\s](re)([\s]*)/x', $pmid, $pmatch) == 1) {
  17222. $newx = sprintf('%F',(floatval($pmatch[1]) + $x_diff));
  17223. $neww = sprintf('%F',(floatval($pmatch[3]) + $w_diff));
  17224. $newpmid = $newx.' '.$pmatch[2].' '.$neww.' '.$pmatch[4].' x*#!#*x'.$pmatch[5].$pmatch[6];
  17225. $pmid = str_replace($pmatch[0], $newpmid, $pmid);
  17226. unset($pmatch, $newpmid, $newx, $neww);
  17227. }
  17228. break;
  17229. }
  17230. case 'c': {
  17231. // get current X position
  17232. 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);
  17233. if (!isset($xmatches[1])) {
  17234. break;
  17235. }
  17236. $currentxpos = $xmatches[1];
  17237. // justify block
  17238. 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) {
  17239. $newx1 = sprintf('%F',(floatval($pmatch[1]) + $spacew));
  17240. $newx2 = sprintf('%F',(floatval($pmatch[3]) + $spacew));
  17241. $newx3 = sprintf('%F',(floatval($pmatch[5]) + $spacew));
  17242. $newpmid = $newx1.' '.$pmatch[2].' '.$newx2.' '.$pmatch[4].' '.$newx3.' '.$pmatch[6].' x*#!#*x'.$pmatch[7].$pmatch[8];
  17243. $pmid = str_replace($pmatch[0], $newpmid, $pmid);
  17244. unset($pmatch, $newpmid, $newx1, $newx2, $newx3);
  17245. }
  17246. break;
  17247. }
  17248. }
  17249. // shift the annotations and links
  17250. $cxpos = ($currentxpos / $this->k);
  17251. $lmpos = ($this->lMargin + $this->cell_padding['L'] + $this->feps);
  17252. if ($this->inxobj) {
  17253. // we are inside an XObject template
  17254. foreach ($this->xobjects[$this->xobjid]['annotations'] as $pak => $pac) {
  17255. if (($pac['y'] >= $minstartliney) AND (($pac['x'] * $this->k) >= ($currentxpos - $this->feps)) AND (($pac['x'] * $this->k) <= ($currentxpos + $this->feps))) {
  17256. if ($cxpos > $lmpos) {
  17257. $this->xobjects[$this->xobjid]['annotations'][$pak]['x'] += ($spacew / $this->k);
  17258. $this->xobjects[$this->xobjid]['annotations'][$pak]['w'] += (($spacewidth * $pac['numspaces']) / $this->k);
  17259. } else {
  17260. $this->xobjects[$this->xobjid]['annotations'][$pak]['w'] += (($spacewidth * $pac['numspaces']) / $this->k);
  17261. }
  17262. break;
  17263. }
  17264. }
  17265. } elseif (isset($this->PageAnnots[$this->page])) {
  17266. foreach ($this->PageAnnots[$this->page] as $pak => $pac) {
  17267. if (($pac['y'] >= $minstartliney) AND (($pac['x'] * $this->k) >= ($currentxpos - $this->feps)) AND (($pac['x'] * $this->k) <= ($currentxpos + $this->feps))) {
  17268. if ($cxpos > $lmpos) {
  17269. $this->PageAnnots[$this->page][$pak]['x'] += ($spacew / $this->k);
  17270. $this->PageAnnots[$this->page][$pak]['w'] += (($spacewidth * $pac['numspaces']) / $this->k);
  17271. } else {
  17272. $this->PageAnnots[$this->page][$pak]['w'] += (($spacewidth * $pac['numspaces']) / $this->k);
  17273. }
  17274. break;
  17275. }
  17276. }
  17277. }
  17278. } // end of while
  17279. // remove markers
  17280. $pmid = str_replace('x*#!#*x', '', $pmid);
  17281. if ($this->isUnicodeFont()) {
  17282. // multibyte characters
  17283. $spacew = $spacewidthu;
  17284. if ($this->font_stretching != 100) {
  17285. // word spacing is affected by stretching
  17286. $spacew /= ($this->font_stretching / 100);
  17287. }
  17288. // escape special characters
  17289. $pos = 0;
  17290. $pmid = preg_replace('/[\\\][\(]/x', '\\#!#OP#!#', $pmid);
  17291. $pmid = preg_replace('/[\\\][\)]/x', '\\#!#CP#!#', $pmid);
  17292. if (preg_match_all('/\[\(([^\)]*)\)\]/x', $pmid, $pamatch) > 0) {
  17293. foreach($pamatch[0] as $pk => $pmatch) {
  17294. $replace = $pamatch[1][$pk];
  17295. $replace = str_replace('#!#OP#!#', '(', $replace);
  17296. $replace = str_replace('#!#CP#!#', ')', $replace);
  17297. $newpmid = '[('.str_replace(chr(0).chr(32), ') '.sprintf('%F', $spacew).' (', $replace).')]';
  17298. $pos = strpos($pmid, $pmatch, $pos);
  17299. if ($pos !== FALSE) {
  17300. $pmid = substr_replace($pmid, $newpmid, $pos, strlen($pmatch));
  17301. }
  17302. ++$pos;
  17303. }
  17304. unset($pamatch);
  17305. }
  17306. if ($this->inxobj) {
  17307. // we are inside an XObject template
  17308. $this->xobjects[$this->xobjid]['outdata'] = $pstart."\n".$pmid."\n".$pend;
  17309. } else {
  17310. $this->setPageBuffer($startlinepage, $pstart."\n".$pmid."\n".$pend);
  17311. }
  17312. $endlinepos = strlen($pstart."\n".$pmid."\n");
  17313. } else {
  17314. // non-unicode (single-byte characters)
  17315. if ($this->font_stretching != 100) {
  17316. // word spacing (Tw) is affected by stretching
  17317. $spacewidth /= ($this->font_stretching / 100);
  17318. }
  17319. $rs = sprintf('%F Tw', $spacewidth);
  17320. $pmid = preg_replace("/\[\(/x", $rs.' [(', $pmid);
  17321. if ($this->inxobj) {
  17322. // we are inside an XObject template
  17323. $this->xobjects[$this->xobjid]['outdata'] = $pstart."\n".$pmid."\nBT 0 Tw ET\n".$pend;
  17324. } else {
  17325. $this->setPageBuffer($startlinepage, $pstart."\n".$pmid."\nBT 0 Tw ET\n".$pend);
  17326. }
  17327. $endlinepos = strlen($pstart."\n".$pmid."\nBT 0 Tw ET\n");
  17328. }
  17329. }
  17330. } // end of J
  17331. } // end if $startlinex
  17332. if (($t_x != 0) OR ($yshift < 0)) {
  17333. // shift the line
  17334. $trx = sprintf('1 0 0 1 %F %F cm', ($t_x * $this->k), ($yshift * $this->k));
  17335. $pstart .= "\nq\n".$trx."\n".$pmid."\nQ\n";
  17336. $endlinepos = strlen($pstart);
  17337. if ($this->inxobj) {
  17338. // we are inside an XObject template
  17339. $this->xobjects[$this->xobjid]['outdata'] = $pstart.$pend;
  17340. foreach ($this->xobjects[$this->xobjid]['annotations'] as $pak => $pac) {
  17341. if ($pak >= $pask) {
  17342. $this->xobjects[$this->xobjid]['annotations'][$pak]['x'] += $t_x;
  17343. $this->xobjects[$this->xobjid]['annotations'][$pak]['y'] -= $yshift;
  17344. }
  17345. }
  17346. } else {
  17347. $this->setPageBuffer($startlinepage, $pstart.$pend);
  17348. // shift the annotations and links
  17349. if (isset($this->PageAnnots[$this->page])) {
  17350. foreach ($this->PageAnnots[$this->page] as $pak => $pac) {
  17351. if ($pak >= $pask) {
  17352. $this->PageAnnots[$this->page][$pak]['x'] += $t_x;
  17353. $this->PageAnnots[$this->page][$pak]['y'] -= $yshift;
  17354. }
  17355. }
  17356. }
  17357. }
  17358. $this->y -= $yshift;
  17359. }
  17360. }
  17361. $pbrk = $this->checkPageBreak($this->lasth);
  17362. $this->newline = false;
  17363. $startlinex = $this->x;
  17364. $startliney = $this->y;
  17365. if ($dom[$dom[$key]['parent']]['value'] == 'sup') {
  17366. $startliney -= ((0.3 * $this->FontSizePt) / $this->k);
  17367. } elseif ($dom[$dom[$key]['parent']]['value'] == 'sub') {
  17368. $startliney -= (($this->FontSizePt / 0.7) / $this->k);
  17369. } else {
  17370. $minstartliney = $startliney;
  17371. $maxbottomliney = ($this->y + $this->getCellHeight($fontsize / $this->k));
  17372. }
  17373. $startlinepage = $this->page;
  17374. if (isset($endlinepos) AND (!$pbrk)) {
  17375. $startlinepos = $endlinepos;
  17376. } else {
  17377. if ($this->inxobj) {
  17378. // we are inside an XObject template
  17379. $startlinepos = strlen($this->xobjects[$this->xobjid]['outdata']);
  17380. } elseif (!$this->InFooter) {
  17381. if (isset($this->footerlen[$this->page])) {
  17382. $this->footerpos[$this->page] = $this->pagelen[$this->page] - $this->footerlen[$this->page];
  17383. } else {
  17384. $this->footerpos[$this->page] = $this->pagelen[$this->page];
  17385. }
  17386. $startlinepos = $this->footerpos[$this->page];
  17387. } else {
  17388. $startlinepos = $this->pagelen[$this->page];
  17389. }
  17390. }
  17391. unset($endlinepos);
  17392. $plalign = $lalign;
  17393. if (isset($this->PageAnnots[$this->page])) {
  17394. $pask = count($this->PageAnnots[$this->page]);
  17395. } else {
  17396. $pask = 0;
  17397. }
  17398. if (!($dom[$key]['tag'] AND !$dom[$key]['opening'] AND ($dom[$key]['value'] == 'table')
  17399. AND (isset($this->emptypagemrk[$this->page]))
  17400. AND ($this->emptypagemrk[$this->page] == $this->pagelen[$this->page]))) {
  17401. $this->SetFont($fontname, $fontstyle, $fontsize);
  17402. if ($wfill) {
  17403. $this->SetFillColorArray($this->bgcolor);
  17404. }
  17405. }
  17406. } // end newline
  17407. if (isset($opentagpos)) {
  17408. unset($opentagpos);
  17409. }
  17410. if ($dom[$key]['tag']) {
  17411. if ($dom[$key]['opening']) {
  17412. // get text indentation (if any)
  17413. if (isset($dom[$key]['text-indent']) AND $dom[$key]['block']) {
  17414. $this->textindent = $dom[$key]['text-indent'];
  17415. $this->newline = true;
  17416. }
  17417. // table
  17418. if (($dom[$key]['value'] == 'table') AND isset($dom[$key]['cols']) AND ($dom[$key]['cols'] > 0)) {
  17419. // available page width
  17420. if ($this->rtl) {
  17421. $wtmp = $this->x - $this->lMargin;
  17422. } else {
  17423. $wtmp = $this->w - $this->rMargin - $this->x;
  17424. }
  17425. // get cell spacing
  17426. if (isset($dom[$key]['attribute']['cellspacing'])) {
  17427. $clsp = $this->getHTMLUnitToUnits($dom[$key]['attribute']['cellspacing'], 1, 'px');
  17428. $cellspacing = array('H' => $clsp, 'V' => $clsp);
  17429. } elseif (isset($dom[$key]['border-spacing'])) {
  17430. $cellspacing = $dom[$key]['border-spacing'];
  17431. } else {
  17432. $cellspacing = array('H' => 0, 'V' => 0);
  17433. }
  17434. // table width
  17435. if (isset($dom[$key]['width'])) {
  17436. $table_width = $this->getHTMLUnitToUnits($dom[$key]['width'], $wtmp, 'px');
  17437. } else {
  17438. $table_width = $wtmp;
  17439. }
  17440. $table_width -= (2 * $cellspacing['H']);
  17441. if (!$this->inthead) {
  17442. $this->y += $cellspacing['V'];
  17443. }
  17444. if ($this->rtl) {
  17445. $cellspacingx = -$cellspacing['H'];
  17446. } else {
  17447. $cellspacingx = $cellspacing['H'];
  17448. }
  17449. // total table width without cellspaces
  17450. $table_columns_width = ($table_width - ($cellspacing['H'] * ($dom[$key]['cols'] - 1)));
  17451. // minimum column width
  17452. $table_min_column_width = ($table_columns_width / $dom[$key]['cols']);
  17453. // array of custom column widths
  17454. $table_colwidths = array_fill(0, $dom[$key]['cols'], $table_min_column_width);
  17455. }
  17456. // table row
  17457. if ($dom[$key]['value'] == 'tr') {
  17458. // reset column counter
  17459. $colid = 0;
  17460. }
  17461. // table cell
  17462. if (($dom[$key]['value'] == 'td') OR ($dom[$key]['value'] == 'th')) {
  17463. $trid = $dom[$key]['parent'];
  17464. $table_el = $dom[$trid]['parent'];
  17465. if (!isset($dom[$table_el]['cols'])) {
  17466. $dom[$table_el]['cols'] = $dom[$trid]['cols'];
  17467. }
  17468. // store border info
  17469. $tdborder = 0;
  17470. if (isset($dom[$key]['border']) AND !empty($dom[$key]['border'])) {
  17471. $tdborder = $dom[$key]['border'];
  17472. }
  17473. $colspan = intval($dom[$key]['attribute']['colspan']);
  17474. if ($colspan <= 0) {
  17475. $colspan = 1;
  17476. }
  17477. $old_cell_padding = $this->cell_padding;
  17478. if (isset($dom[($dom[$trid]['parent'])]['attribute']['cellpadding'])) {
  17479. $crclpd = $this->getHTMLUnitToUnits($dom[($dom[$trid]['parent'])]['attribute']['cellpadding'], 1, 'px');
  17480. $current_cell_padding = array('L' => $crclpd, 'T' => $crclpd, 'R' => $crclpd, 'B' => $crclpd);
  17481. } elseif (isset($dom[($dom[$trid]['parent'])]['padding'])) {
  17482. $current_cell_padding = $dom[($dom[$trid]['parent'])]['padding'];
  17483. } else {
  17484. $current_cell_padding = array('L' => 0, 'T' => 0, 'R' => 0, 'B' => 0);
  17485. }
  17486. $this->cell_padding = $current_cell_padding;
  17487. if (isset($dom[$key]['height'])) {
  17488. // minimum cell height
  17489. $cellh = $this->getHTMLUnitToUnits($dom[$key]['height'], 0, 'px');
  17490. } else {
  17491. $cellh = 0;
  17492. }
  17493. if (isset($dom[$key]['content'])) {
  17494. $cell_content = $dom[$key]['content'];
  17495. } else {
  17496. $cell_content = '&nbsp;';
  17497. }
  17498. $tagtype = $dom[$key]['value'];
  17499. $parentid = $key;
  17500. while (($key < $maxel) AND (!(($dom[$key]['tag']) AND (!$dom[$key]['opening']) AND ($dom[$key]['value'] == $tagtype) AND ($dom[$key]['parent'] == $parentid)))) {
  17501. // move $key index forward
  17502. ++$key;
  17503. }
  17504. if (!isset($dom[$trid]['startpage'])) {
  17505. $dom[$trid]['startpage'] = $this->page;
  17506. } else {
  17507. $this->setPage($dom[$trid]['startpage']);
  17508. }
  17509. if (!isset($dom[$trid]['startcolumn'])) {
  17510. $dom[$trid]['startcolumn'] = $this->current_column;
  17511. } elseif ($this->current_column != $dom[$trid]['startcolumn']) {
  17512. $tmpx = $this->x;
  17513. $this->selectColumn($dom[$trid]['startcolumn']);
  17514. $this->x = $tmpx;
  17515. }
  17516. if (!isset($dom[$trid]['starty'])) {
  17517. $dom[$trid]['starty'] = $this->y;
  17518. } else {
  17519. $this->y = $dom[$trid]['starty'];
  17520. }
  17521. if (!isset($dom[$trid]['startx'])) {
  17522. $dom[$trid]['startx'] = $this->x;
  17523. $this->x += $cellspacingx;
  17524. } else {
  17525. $this->x += ($cellspacingx / 2);
  17526. }
  17527. if (isset($dom[$parentid]['attribute']['rowspan'])) {
  17528. $rowspan = intval($dom[$parentid]['attribute']['rowspan']);
  17529. } else {
  17530. $rowspan = 1;
  17531. }
  17532. // skip row-spanned cells started on the previous rows
  17533. if (isset($dom[$table_el]['rowspans'])) {
  17534. $rsk = 0;
  17535. $rskmax = count($dom[$table_el]['rowspans']);
  17536. while ($rsk < $rskmax) {
  17537. $trwsp = $dom[$table_el]['rowspans'][$rsk];
  17538. $rsstartx = $trwsp['startx'];
  17539. $rsendx = $trwsp['endx'];
  17540. // account for margin changes
  17541. if ($trwsp['startpage'] < $this->page) {
  17542. if (($this->rtl) AND ($this->pagedim[$this->page]['orm'] != $this->pagedim[$trwsp['startpage']]['orm'])) {
  17543. $dl = ($this->pagedim[$this->page]['orm'] - $this->pagedim[$trwsp['startpage']]['orm']);
  17544. $rsstartx -= $dl;
  17545. $rsendx -= $dl;
  17546. } elseif ((!$this->rtl) AND ($this->pagedim[$this->page]['olm'] != $this->pagedim[$trwsp['startpage']]['olm'])) {
  17547. $dl = ($this->pagedim[$this->page]['olm'] - $this->pagedim[$trwsp['startpage']]['olm']);
  17548. $rsstartx += $dl;
  17549. $rsendx += $dl;
  17550. }
  17551. }
  17552. if (($trwsp['rowspan'] > 0)
  17553. AND ($rsstartx > ($this->x - $cellspacing['H'] - $current_cell_padding['L'] - $this->feps))
  17554. AND ($rsstartx < ($this->x + $cellspacing['H'] + $current_cell_padding['R'] + $this->feps))
  17555. AND (($trwsp['starty'] < ($this->y - $this->feps)) OR ($trwsp['startpage'] < $this->page) OR ($trwsp['startcolumn'] < $this->current_column))) {
  17556. // set the starting X position of the current cell
  17557. $this->x = $rsendx + $cellspacingx;
  17558. // increment column indicator
  17559. $colid += $trwsp['colspan'];
  17560. if (($trwsp['rowspan'] == 1)
  17561. AND (isset($dom[$trid]['endy']))
  17562. AND (isset($dom[$trid]['endpage']))
  17563. AND (isset($dom[$trid]['endcolumn']))
  17564. AND ($trwsp['endpage'] == $dom[$trid]['endpage'])
  17565. AND ($trwsp['endcolumn'] == $dom[$trid]['endcolumn'])) {
  17566. // set ending Y position for row
  17567. $dom[$table_el]['rowspans'][$rsk]['endy'] = max($dom[$trid]['endy'], $trwsp['endy']);
  17568. $dom[$trid]['endy'] = $dom[$table_el]['rowspans'][$rsk]['endy'];
  17569. }
  17570. $rsk = 0;
  17571. } else {
  17572. ++$rsk;
  17573. }
  17574. }
  17575. }
  17576. if (isset($dom[$parentid]['width'])) {
  17577. // user specified width
  17578. $cellw = $this->getHTMLUnitToUnits($dom[$parentid]['width'], $table_columns_width, 'px');
  17579. $tmpcw = ($cellw / $colspan);
  17580. for ($i = 0; $i < $colspan; ++$i) {
  17581. $table_colwidths[($colid + $i)] = $tmpcw;
  17582. }
  17583. } else {
  17584. // inherit column width
  17585. $cellw = 0;
  17586. for ($i = 0; $i < $colspan; ++$i) {
  17587. $cellw += (isset($table_colwidths[($colid + $i)]) ? $table_colwidths[($colid + $i)] : 0);
  17588. }
  17589. }
  17590. $cellw += (($colspan - 1) * $cellspacing['H']);
  17591. // increment column indicator
  17592. $colid += $colspan;
  17593. // add rowspan information to table element
  17594. if ($rowspan > 1) {
  17595. $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));
  17596. }
  17597. $cellid = array_push($dom[$trid]['cellpos'], array('startx' => $this->x));
  17598. if ($rowspan > 1) {
  17599. $dom[$trid]['cellpos'][($cellid - 1)]['rowspanid'] = ($trsid - 1);
  17600. }
  17601. // push background colors
  17602. if (isset($dom[$parentid]['bgcolor']) AND ($dom[$parentid]['bgcolor'] !== false)) {
  17603. $dom[$trid]['cellpos'][($cellid - 1)]['bgcolor'] = $dom[$parentid]['bgcolor'];
  17604. }
  17605. // store border info
  17606. if (isset($tdborder) AND !empty($tdborder)) {
  17607. $dom[$trid]['cellpos'][($cellid - 1)]['border'] = $tdborder;
  17608. }
  17609. $prevLastH = $this->lasth;
  17610. // store some info for multicolumn mode
  17611. if ($this->rtl) {
  17612. $this->colxshift['x'] = $this->w - $this->x - $this->rMargin;
  17613. } else {
  17614. $this->colxshift['x'] = $this->x - $this->lMargin;
  17615. }
  17616. $this->colxshift['s'] = $cellspacing;
  17617. $this->colxshift['p'] = $current_cell_padding;
  17618. // ****** write the cell content ******
  17619. $this->MultiCell($cellw, $cellh, $cell_content, false, $lalign, false, 2, '', '', true, 0, true, true, 0, 'T', false);
  17620. // restore some values
  17621. $this->colxshift = array('x' => 0, 's' => array('H' => 0, 'V' => 0), 'p' => array('L' => 0, 'T' => 0, 'R' => 0, 'B' => 0));
  17622. $this->lasth = $prevLastH;
  17623. $this->cell_padding = $old_cell_padding;
  17624. $dom[$trid]['cellpos'][($cellid - 1)]['endx'] = $this->x;
  17625. // update the end of row position
  17626. if ($rowspan <= 1) {
  17627. if (isset($dom[$trid]['endy'])) {
  17628. if (($this->page == $dom[$trid]['endpage']) AND ($this->current_column == $dom[$trid]['endcolumn'])) {
  17629. $dom[$trid]['endy'] = max($this->y, $dom[$trid]['endy']);
  17630. } elseif (($this->page > $dom[$trid]['endpage']) OR ($this->current_column > $dom[$trid]['endcolumn'])) {
  17631. $dom[$trid]['endy'] = $this->y;
  17632. }
  17633. } else {
  17634. $dom[$trid]['endy'] = $this->y;
  17635. }
  17636. if (isset($dom[$trid]['endpage'])) {
  17637. $dom[$trid]['endpage'] = max($this->page, $dom[$trid]['endpage']);
  17638. } else {
  17639. $dom[$trid]['endpage'] = $this->page;
  17640. }
  17641. if (isset($dom[$trid]['endcolumn'])) {
  17642. $dom[$trid]['endcolumn'] = max($this->current_column, $dom[$trid]['endcolumn']);
  17643. } else {
  17644. $dom[$trid]['endcolumn'] = $this->current_column;
  17645. }
  17646. } else {
  17647. // account for row-spanned cells
  17648. $dom[$table_el]['rowspans'][($trsid - 1)]['endx'] = $this->x;
  17649. $dom[$table_el]['rowspans'][($trsid - 1)]['endy'] = $this->y;
  17650. $dom[$table_el]['rowspans'][($trsid - 1)]['endpage'] = $this->page;
  17651. $dom[$table_el]['rowspans'][($trsid - 1)]['endcolumn'] = $this->current_column;
  17652. }
  17653. if (isset($dom[$table_el]['rowspans'])) {
  17654. // update endy and endpage on rowspanned cells
  17655. foreach ($dom[$table_el]['rowspans'] as $k => $trwsp) {
  17656. if ($trwsp['rowspan'] > 0) {
  17657. if (isset($dom[$trid]['endpage'])) {
  17658. if (($trwsp['endpage'] == $dom[$trid]['endpage']) AND ($trwsp['endcolumn'] == $dom[$trid]['endcolumn'])) {
  17659. $dom[$table_el]['rowspans'][$k]['endy'] = max($dom[$trid]['endy'], $trwsp['endy']);
  17660. } elseif (($trwsp['endpage'] < $dom[$trid]['endpage']) OR ($trwsp['endcolumn'] < $dom[$trid]['endcolumn'])) {
  17661. $dom[$table_el]['rowspans'][$k]['endy'] = $dom[$trid]['endy'];
  17662. $dom[$table_el]['rowspans'][$k]['endpage'] = $dom[$trid]['endpage'];
  17663. $dom[$table_el]['rowspans'][$k]['endcolumn'] = $dom[$trid]['endcolumn'];
  17664. } else {
  17665. $dom[$trid]['endy'] = $this->pagedim[$dom[$trid]['endpage']]['hk'] - $this->pagedim[$dom[$trid]['endpage']]['bm'];
  17666. }
  17667. }
  17668. }
  17669. }
  17670. }
  17671. $this->x += ($cellspacingx / 2);
  17672. } else {
  17673. // opening tag (or self-closing tag)
  17674. if (!isset($opentagpos)) {
  17675. if ($this->inxobj) {
  17676. // we are inside an XObject template
  17677. $opentagpos = strlen($this->xobjects[$this->xobjid]['outdata']);
  17678. } elseif (!$this->InFooter) {
  17679. if (isset($this->footerlen[$this->page])) {
  17680. $this->footerpos[$this->page] = $this->pagelen[$this->page] - $this->footerlen[$this->page];
  17681. } else {
  17682. $this->footerpos[$this->page] = $this->pagelen[$this->page];
  17683. }
  17684. $opentagpos = $this->footerpos[$this->page];
  17685. }
  17686. }
  17687. $dom = $this->openHTMLTagHandler($dom, $key, $cell);
  17688. }
  17689. } else { // closing tag
  17690. $prev_numpages = $this->numpages;
  17691. $old_bordermrk = $this->bordermrk[$this->page];
  17692. $dom = $this->closeHTMLTagHandler($dom, $key, $cell, $maxbottomliney);
  17693. if ($this->bordermrk[$this->page] > $old_bordermrk) {
  17694. $startlinepos += ($this->bordermrk[$this->page] - $old_bordermrk);
  17695. }
  17696. if ($prev_numpages > $this->numpages) {
  17697. $startlinepage = $this->page;
  17698. }
  17699. }
  17700. } elseif (strlen($dom[$key]['value']) > 0) {
  17701. // print list-item
  17702. if (!TCPDF_STATIC::empty_string($this->lispacer) AND ($this->lispacer != '^')) {
  17703. $this->SetFont($pfontname, $pfontstyle, $pfontsize);
  17704. $this->resetLastH();
  17705. $minstartliney = $this->y;
  17706. $maxbottomliney = ($startliney + $this->getCellHeight($this->FontSize));
  17707. if (is_numeric($pfontsize) AND ($pfontsize > 0)) {
  17708. $this->putHtmlListBullet($this->listnum, $this->lispacer, $pfontsize);
  17709. }
  17710. $this->SetFont($curfontname, $curfontstyle, $curfontsize);
  17711. $this->resetLastH();
  17712. if (is_numeric($pfontsize) AND ($pfontsize > 0) AND is_numeric($curfontsize) AND ($curfontsize > 0) AND ($pfontsize != $curfontsize)) {
  17713. $pfontascent = $this->getFontAscent($pfontname, $pfontstyle, $pfontsize);
  17714. $pfontdescent = $this->getFontDescent($pfontname, $pfontstyle, $pfontsize);
  17715. $this->y += ($this->getCellHeight(($pfontsize - $curfontsize) / $this->k) + $pfontascent - $curfontascent - $pfontdescent + $curfontdescent) / 2;
  17716. $minstartliney = min($this->y, $minstartliney);
  17717. $maxbottomliney = max(($this->y + $this->getCellHeight($pfontsize / $this->k)), $maxbottomliney);
  17718. }
  17719. }
  17720. // text
  17721. $this->htmlvspace = 0;
  17722. if ((!$this->premode) AND $this->isRTLTextDir()) {
  17723. // reverse spaces order
  17724. $lsp = ''; // left spaces
  17725. $rsp = ''; // right spaces
  17726. if (preg_match('/^('.$this->re_space['p'].'+)/'.$this->re_space['m'], $dom[$key]['value'], $matches)) {
  17727. $lsp = $matches[1];
  17728. }
  17729. if (preg_match('/('.$this->re_space['p'].'+)$/'.$this->re_space['m'], $dom[$key]['value'], $matches)) {
  17730. $rsp = $matches[1];
  17731. }
  17732. $dom[$key]['value'] = $rsp.$this->stringTrim($dom[$key]['value']).$lsp;
  17733. }
  17734. if ($newline) {
  17735. if (!$this->premode) {
  17736. $prelen = strlen($dom[$key]['value']);
  17737. if ($this->isRTLTextDir()) {
  17738. // right trim except non-breaking space
  17739. $dom[$key]['value'] = $this->stringRightTrim($dom[$key]['value']);
  17740. } else {
  17741. // left trim except non-breaking space
  17742. $dom[$key]['value'] = $this->stringLeftTrim($dom[$key]['value']);
  17743. }
  17744. $postlen = strlen($dom[$key]['value']);
  17745. if (($postlen == 0) AND ($prelen > 0)) {
  17746. $dom[$key]['trimmed_space'] = true;
  17747. }
  17748. }
  17749. $newline = false;
  17750. $firstblock = true;
  17751. } else {
  17752. $firstblock = false;
  17753. // replace empty multiple spaces string with a single space
  17754. $dom[$key]['value'] = preg_replace('/^'.$this->re_space['p'].'+$/'.$this->re_space['m'], chr(32), $dom[$key]['value']);
  17755. }
  17756. $strrest = '';
  17757. if ($this->rtl) {
  17758. $this->x -= $this->textindent;
  17759. } else {
  17760. $this->x += $this->textindent;
  17761. }
  17762. if (!isset($dom[$key]['trimmed_space']) OR !$dom[$key]['trimmed_space']) {
  17763. $strlinelen = $this->GetStringWidth($dom[$key]['value']);
  17764. if (!empty($this->HREF) AND (isset($this->HREF['url']))) {
  17765. // HTML <a> Link
  17766. $hrefcolor = '';
  17767. if (isset($dom[($dom[$key]['parent'])]['fgcolor']) AND ($dom[($dom[$key]['parent'])]['fgcolor'] !== false)) {
  17768. $hrefcolor = $dom[($dom[$key]['parent'])]['fgcolor'];
  17769. }
  17770. $hrefstyle = -1;
  17771. if (isset($dom[($dom[$key]['parent'])]['fontstyle']) AND ($dom[($dom[$key]['parent'])]['fontstyle'] !== false)) {
  17772. $hrefstyle = $dom[($dom[$key]['parent'])]['fontstyle'];
  17773. }
  17774. $strrest = $this->addHtmlLink($this->HREF['url'], $dom[$key]['value'], $wfill, true, $hrefcolor, $hrefstyle, true);
  17775. } else {
  17776. $wadj = 0; // space to leave for block continuity
  17777. if ($this->rtl) {
  17778. $cwa = ($this->x - $this->lMargin);
  17779. } else {
  17780. $cwa = ($this->w - $this->rMargin - $this->x);
  17781. }
  17782. if (($strlinelen < $cwa) AND (isset($dom[($key + 1)])) AND ($dom[($key + 1)]['tag']) AND (!$dom[($key + 1)]['block'])) {
  17783. // check the next text blocks for continuity
  17784. $nkey = ($key + 1);
  17785. $write_block = true;
  17786. $same_textdir = true;
  17787. $tmp_fontname = $this->FontFamily;
  17788. $tmp_fontstyle = $this->FontStyle;
  17789. $tmp_fontsize = $this->FontSizePt;
  17790. while ($write_block AND isset($dom[$nkey])) {
  17791. if ($dom[$nkey]['tag']) {
  17792. if ($dom[$nkey]['block']) {
  17793. // end of block
  17794. $write_block = false;
  17795. }
  17796. $tmp_fontname = isset($dom[$nkey]['fontname']) ? $dom[$nkey]['fontname'] : $this->FontFamily;
  17797. $tmp_fontstyle = isset($dom[$nkey]['fontstyle']) ? $dom[$nkey]['fontstyle'] : $this->FontStyle;
  17798. $tmp_fontsize = isset($dom[$nkey]['fontsize']) ? $dom[$nkey]['fontsize'] : $this->FontSizePt;
  17799. $same_textdir = ($dom[$nkey]['dir'] == $dom[$key]['dir']);
  17800. } else {
  17801. $nextstr = TCPDF_STATIC::pregSplit('/'.$this->re_space['p'].'+/', $this->re_space['m'], $dom[$nkey]['value']);
  17802. if (isset($nextstr[0]) AND $same_textdir) {
  17803. $wadj += $this->GetStringWidth($nextstr[0], $tmp_fontname, $tmp_fontstyle, $tmp_fontsize);
  17804. if (isset($nextstr[1])) {
  17805. $write_block = false;
  17806. }
  17807. }
  17808. }
  17809. ++$nkey;
  17810. }
  17811. }
  17812. if (($wadj > 0) AND (($strlinelen + $wadj) >= $cwa)) {
  17813. $wadj = 0;
  17814. $nextstr = TCPDF_STATIC::pregSplit('/'.$this->re_space['p'].'/', $this->re_space['m'], $dom[$key]['value']);
  17815. $numblks = count($nextstr);
  17816. if ($numblks > 1) {
  17817. // try to split on blank spaces
  17818. $wadj = ($cwa - $strlinelen + $this->GetStringWidth($nextstr[($numblks - 1)]));
  17819. } else {
  17820. // set the entire block on new line
  17821. $wadj = $this->GetStringWidth($nextstr[0]);
  17822. }
  17823. }
  17824. // check for reversed text direction
  17825. if (($wadj > 0) AND (($this->rtl AND ($this->tmprtl === 'L')) OR (!$this->rtl AND ($this->tmprtl === 'R')))) {
  17826. // LTR text on RTL direction or RTL text on LTR direction
  17827. $reverse_dir = true;
  17828. $this->rtl = !$this->rtl;
  17829. $revshift = ($strlinelen + $wadj + 0.000001); // add little quantity for rounding problems
  17830. if ($this->rtl) {
  17831. $this->x += $revshift;
  17832. } else {
  17833. $this->x -= $revshift;
  17834. }
  17835. $xws = $this->x;
  17836. }
  17837. // ****** write only until the end of the line and get the rest ******
  17838. $strrest = $this->Write($this->lasth, $dom[$key]['value'], '', $wfill, '', false, 0, true, $firstblock, 0, $wadj);
  17839. // restore default direction
  17840. if ($reverse_dir AND ($wadj == 0)) {
  17841. $this->x = $xws;
  17842. $this->rtl = !$this->rtl;
  17843. $reverse_dir = false;
  17844. }
  17845. }
  17846. }
  17847. $this->textindent = 0;
  17848. if (strlen($strrest) > 0) {
  17849. // store the remaining string on the previous $key position
  17850. $this->newline = true;
  17851. if ($strrest == $dom[$key]['value']) {
  17852. // used to avoid infinite loop
  17853. ++$loop;
  17854. } else {
  17855. $loop = 0;
  17856. }
  17857. $dom[$key]['value'] = $strrest;
  17858. if ($cell) {
  17859. if ($this->rtl) {
  17860. $this->x -= $this->cell_padding['R'];
  17861. } else {
  17862. $this->x += $this->cell_padding['L'];
  17863. }
  17864. }
  17865. if ($loop < 3) {
  17866. --$key;
  17867. }
  17868. } else {
  17869. $loop = 0;
  17870. // add the positive font spacing of the last character (if any)
  17871. if ($this->font_spacing > 0) {
  17872. if ($this->rtl) {
  17873. $this->x -= $this->font_spacing;
  17874. } else {
  17875. $this->x += $this->font_spacing;
  17876. }
  17877. }
  17878. }
  17879. }
  17880. ++$key;
  17881. 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')) {
  17882. // check if we are on a new page or on a new column
  17883. if ((!$undo) AND (($this->y < $this->start_transaction_y) OR (($dom[$key]['value'] == 'tr') AND ($dom[($dom[$key]['parent'])]['endy'] < $this->start_transaction_y)))) {
  17884. // we are on a new page or on a new column and the total object height is less than the available vertical space.
  17885. // restore previous object
  17886. $this->rollbackTransaction(true);
  17887. // restore previous values
  17888. foreach ($this_method_vars as $vkey => $vval) {
  17889. $$vkey = $vval;
  17890. }
  17891. if (!empty($dom[$key]['thead'])) {
  17892. $this->inthead = true;
  17893. }
  17894. // add a page (or trig AcceptPageBreak() for multicolumn mode)
  17895. $pre_y = $this->y;
  17896. if ((!$this->checkPageBreak($this->PageBreakTrigger + 1)) AND ($this->y < $pre_y)) {
  17897. $startliney = $this->y;
  17898. }
  17899. $undo = true; // avoid infinite loop
  17900. } else {
  17901. $undo = false;
  17902. }
  17903. }
  17904. } // end for each $key
  17905. // align the last line
  17906. if (isset($startlinex)) {
  17907. $yshift = ($minstartliney - $startliney);
  17908. if (($yshift > 0) OR ($this->page > $startlinepage)) {
  17909. $yshift = 0;
  17910. }
  17911. $t_x = 0;
  17912. // the last line must be shifted to be aligned as requested
  17913. $linew = abs($this->endlinex - $startlinex);
  17914. if ($this->inxobj) {
  17915. // we are inside an XObject template
  17916. $pstart = substr($this->xobjects[$this->xobjid]['outdata'], 0, $startlinepos);
  17917. if (isset($opentagpos)) {
  17918. $midpos = $opentagpos;
  17919. } else {
  17920. $midpos = 0;
  17921. }
  17922. if ($midpos > 0) {
  17923. $pmid = substr($this->xobjects[$this->xobjid]['outdata'], $startlinepos, ($midpos - $startlinepos));
  17924. $pend = substr($this->xobjects[$this->xobjid]['outdata'], $midpos);
  17925. } else {
  17926. $pmid = substr($this->xobjects[$this->xobjid]['outdata'], $startlinepos);
  17927. $pend = '';
  17928. }
  17929. } else {
  17930. $pstart = substr($this->getPageBuffer($startlinepage), 0, $startlinepos);
  17931. if (isset($opentagpos) AND isset($this->footerlen[$startlinepage]) AND (!$this->InFooter)) {
  17932. $this->footerpos[$startlinepage] = $this->pagelen[$startlinepage] - $this->footerlen[$startlinepage];
  17933. $midpos = min($opentagpos, $this->footerpos[$startlinepage]);
  17934. } elseif (isset($opentagpos)) {
  17935. $midpos = $opentagpos;
  17936. } elseif (isset($this->footerlen[$startlinepage]) AND (!$this->InFooter)) {
  17937. $this->footerpos[$startlinepage] = $this->pagelen[$startlinepage] - $this->footerlen[$startlinepage];
  17938. $midpos = $this->footerpos[$startlinepage];
  17939. } else {
  17940. $midpos = 0;
  17941. }
  17942. if ($midpos > 0) {
  17943. $pmid = substr($this->getPageBuffer($startlinepage), $startlinepos, ($midpos - $startlinepos));
  17944. $pend = substr($this->getPageBuffer($startlinepage), $midpos);
  17945. } else {
  17946. $pmid = substr($this->getPageBuffer($startlinepage), $startlinepos);
  17947. $pend = '';
  17948. }
  17949. }
  17950. if ((isset($plalign) AND ((($plalign == 'C') OR (($plalign == 'R') AND (!$this->rtl)) OR (($plalign == 'L') AND ($this->rtl)))))) {
  17951. // calculate shifting amount
  17952. $tw = $w;
  17953. if ($this->lMargin != $prevlMargin) {
  17954. $tw += ($prevlMargin - $this->lMargin);
  17955. }
  17956. if ($this->rMargin != $prevrMargin) {
  17957. $tw += ($prevrMargin - $this->rMargin);
  17958. }
  17959. $one_space_width = $this->GetStringWidth(chr(32));
  17960. $no = 0; // number of spaces on a line contained on a single block
  17961. if ($this->isRTLTextDir()) { // RTL
  17962. // remove left space if exist
  17963. $pos1 = TCPDF_STATIC::revstrpos($pmid, '[(');
  17964. if ($pos1 > 0) {
  17965. $pos1 = intval($pos1);
  17966. if ($this->isUnicodeFont()) {
  17967. $pos2 = intval(TCPDF_STATIC::revstrpos($pmid, '[('.chr(0).chr(32)));
  17968. $spacelen = 2;
  17969. } else {
  17970. $pos2 = intval(TCPDF_STATIC::revstrpos($pmid, '[('.chr(32)));
  17971. $spacelen = 1;
  17972. }
  17973. if ($pos1 == $pos2) {
  17974. $pmid = substr($pmid, 0, ($pos1 + 2)).substr($pmid, ($pos1 + 2 + $spacelen));
  17975. if (substr($pmid, $pos1, 4) == '[()]') {
  17976. $linew -= $one_space_width;
  17977. } elseif ($pos1 == strpos($pmid, '[(')) {
  17978. $no = 1;
  17979. }
  17980. }
  17981. }
  17982. } else { // LTR
  17983. // remove right space if exist
  17984. $pos1 = TCPDF_STATIC::revstrpos($pmid, ')]');
  17985. if ($pos1 > 0) {
  17986. $pos1 = intval($pos1);
  17987. if ($this->isUnicodeFont()) {
  17988. $pos2 = intval(TCPDF_STATIC::revstrpos($pmid, chr(0).chr(32).')]')) + 2;
  17989. $spacelen = 2;
  17990. } else {
  17991. $pos2 = intval(TCPDF_STATIC::revstrpos($pmid, chr(32).')]')) + 1;
  17992. $spacelen = 1;
  17993. }
  17994. if ($pos1 == $pos2) {
  17995. $pmid = substr($pmid, 0, ($pos1 - $spacelen)).substr($pmid, $pos1);
  17996. $linew -= $one_space_width;
  17997. }
  17998. }
  17999. }
  18000. $mdiff = ($tw - $linew);
  18001. if ($plalign == 'C') {
  18002. if ($this->rtl) {
  18003. $t_x = -($mdiff / 2);
  18004. } else {
  18005. $t_x = ($mdiff / 2);
  18006. }
  18007. } elseif ($plalign == 'R') {
  18008. // right alignment on LTR document
  18009. $t_x = $mdiff;
  18010. } elseif ($plalign == 'L') {
  18011. // left alignment on RTL document
  18012. $t_x = -$mdiff;
  18013. }
  18014. } // end if startlinex
  18015. if (($t_x != 0) OR ($yshift < 0)) {
  18016. // shift the line
  18017. $trx = sprintf('1 0 0 1 %F %F cm', ($t_x * $this->k), ($yshift * $this->k));
  18018. $pstart .= "\nq\n".$trx."\n".$pmid."\nQ\n";
  18019. $endlinepos = strlen($pstart);
  18020. if ($this->inxobj) {
  18021. // we are inside an XObject template
  18022. $this->xobjects[$this->xobjid]['outdata'] = $pstart.$pend;
  18023. foreach ($this->xobjects[$this->xobjid]['annotations'] as $pak => $pac) {
  18024. if ($pak >= $pask) {
  18025. $this->xobjects[$this->xobjid]['annotations'][$pak]['x'] += $t_x;
  18026. $this->xobjects[$this->xobjid]['annotations'][$pak]['y'] -= $yshift;
  18027. }
  18028. }
  18029. } else {
  18030. $this->setPageBuffer($startlinepage, $pstart.$pend);
  18031. // shift the annotations and links
  18032. if (isset($this->PageAnnots[$this->page])) {
  18033. foreach ($this->PageAnnots[$this->page] as $pak => $pac) {
  18034. if ($pak >= $pask) {
  18035. $this->PageAnnots[$this->page][$pak]['x'] += $t_x;
  18036. $this->PageAnnots[$this->page][$pak]['y'] -= $yshift;
  18037. }
  18038. }
  18039. }
  18040. }
  18041. $this->y -= $yshift;
  18042. $yshift = 0;
  18043. }
  18044. }
  18045. // restore previous values
  18046. $this->setGraphicVars($gvars);
  18047. if ($this->num_columns > 1) {
  18048. $this->selectColumn();
  18049. } elseif ($this->page > $prevPage) {
  18050. $this->lMargin = $this->pagedim[$this->page]['olm'];
  18051. $this->rMargin = $this->pagedim[$this->page]['orm'];
  18052. }
  18053. // restore previous list state
  18054. $this->cell_height_ratio = $prev_cell_height_ratio;
  18055. $this->listnum = $prev_listnum;
  18056. $this->listordered = $prev_listordered;
  18057. $this->listcount = $prev_listcount;
  18058. $this->lispacer = $prev_lispacer;
  18059. if ($ln AND (!($cell AND ($dom[$key-1]['value'] == 'table')))) {
  18060. $this->Ln($this->lasth);
  18061. if (($this->y < $maxbottomliney) AND ($startlinepage == $this->page)) {
  18062. $this->y = $maxbottomliney;
  18063. }
  18064. }
  18065. unset($dom);
  18066. }
  18067. /**
  18068. * Process opening tags.
  18069. * @param $dom (array) html dom array
  18070. * @param $key (int) current element id
  18071. * @param $cell (boolean) if true add the default left (or right if RTL) padding to each new line (default false).
  18072. * @return $dom array
  18073. * @protected
  18074. */
  18075. protected function openHTMLTagHandler($dom, $key, $cell) {
  18076. $tag = $dom[$key];
  18077. $parent = $dom[($dom[$key]['parent'])];
  18078. $firsttag = ($key == 1);
  18079. // check for text direction attribute
  18080. if (isset($tag['dir'])) {
  18081. $this->setTempRTL($tag['dir']);
  18082. } else {
  18083. $this->tmprtl = false;
  18084. }
  18085. if ($tag['block']) {
  18086. $hbz = 0; // distance from y to line bottom
  18087. $hb = 0; // vertical space between block tags
  18088. // calculate vertical space for block tags
  18089. if (isset($this->tagvspaces[$tag['value']][0]['h']) AND ($this->tagvspaces[$tag['value']][0]['h'] >= 0)) {
  18090. $cur_h = $this->tagvspaces[$tag['value']][0]['h'];
  18091. } elseif (isset($tag['fontsize'])) {
  18092. $cur_h = $this->getCellHeight($tag['fontsize'] / $this->k);
  18093. } else {
  18094. $cur_h = $this->getCellHeight($this->FontSize);
  18095. }
  18096. if (isset($this->tagvspaces[$tag['value']][0]['n'])) {
  18097. $on = $this->tagvspaces[$tag['value']][0]['n'];
  18098. } elseif (preg_match('/[h][0-9]/', $tag['value']) > 0) {
  18099. $on = 0.6;
  18100. } else {
  18101. $on = 1;
  18102. }
  18103. if ((!isset($this->tagvspaces[$tag['value']])) AND (in_array($tag['value'], array('div', 'dt', 'dd', 'li', 'br', 'hr')))) {
  18104. $hb = 0;
  18105. } else {
  18106. $hb = ($on * $cur_h);
  18107. }
  18108. if (($this->htmlvspace <= 0) AND ($on > 0)) {
  18109. if (isset($parent['fontsize'])) {
  18110. $hbz = (($parent['fontsize'] / $this->k) * $this->cell_height_ratio);
  18111. } else {
  18112. $hbz = $this->getCellHeight($this->FontSize);
  18113. }
  18114. }
  18115. if (isset($dom[($key - 1)]) AND ($dom[($key - 1)]['value'] == 'table')) {
  18116. // fix vertical space after table
  18117. $hbz = 0;
  18118. }
  18119. // closing vertical space
  18120. $hbc = 0;
  18121. if (isset($this->tagvspaces[$tag['value']][1]['h']) AND ($this->tagvspaces[$tag['value']][1]['h'] >= 0)) {
  18122. $pre_h = $this->tagvspaces[$tag['value']][1]['h'];
  18123. } elseif (isset($parent['fontsize'])) {
  18124. $pre_h = $this->getCellHeight($parent['fontsize'] / $this->k);
  18125. } else {
  18126. $pre_h = $this->getCellHeight($this->FontSize);
  18127. }
  18128. if (isset($this->tagvspaces[$tag['value']][1]['n'])) {
  18129. $cn = $this->tagvspaces[$tag['value']][1]['n'];
  18130. } elseif (preg_match('/[h][0-9]/', $tag['value']) > 0) {
  18131. $cn = 0.6;
  18132. } else {
  18133. $cn = 1;
  18134. }
  18135. if (isset($this->tagvspaces[$tag['value']][1])) {
  18136. $hbc = ($cn * $pre_h);
  18137. }
  18138. }
  18139. // Opening tag
  18140. switch($tag['value']) {
  18141. case 'table': {
  18142. $cp = 0;
  18143. $cs = 0;
  18144. $dom[$key]['rowspans'] = array();
  18145. if (!isset($dom[$key]['attribute']['nested']) OR ($dom[$key]['attribute']['nested'] != 'true')) {
  18146. $this->htmlvspace = 0;
  18147. // set table header
  18148. if (!TCPDF_STATIC::empty_string($dom[$key]['thead'])) {
  18149. // set table header
  18150. $this->thead = $dom[$key]['thead'];
  18151. if (!isset($this->theadMargins) OR (empty($this->theadMargins))) {
  18152. $this->theadMargins = array();
  18153. $this->theadMargins['cell_padding'] = $this->cell_padding;
  18154. $this->theadMargins['lmargin'] = $this->lMargin;
  18155. $this->theadMargins['rmargin'] = $this->rMargin;
  18156. $this->theadMargins['page'] = $this->page;
  18157. $this->theadMargins['cell'] = $cell;
  18158. $this->theadMargins['gvars'] = $this->getGraphicVars();
  18159. }
  18160. }
  18161. }
  18162. // store current margins and page
  18163. $dom[$key]['old_cell_padding'] = $this->cell_padding;
  18164. if (isset($tag['attribute']['cellpadding'])) {
  18165. $pad = $this->getHTMLUnitToUnits($tag['attribute']['cellpadding'], 1, 'px');
  18166. $this->SetCellPadding($pad);
  18167. } elseif (isset($tag['padding'])) {
  18168. $this->cell_padding = $tag['padding'];
  18169. }
  18170. if (isset($tag['attribute']['cellspacing'])) {
  18171. $cs = $this->getHTMLUnitToUnits($tag['attribute']['cellspacing'], 1, 'px');
  18172. } elseif (isset($tag['border-spacing'])) {
  18173. $cs = $tag['border-spacing']['V'];
  18174. }
  18175. $prev_y = $this->y;
  18176. if ($this->checkPageBreak(((2 * $cp) + (2 * $cs) + $this->lasth), '', false) OR ($this->y < $prev_y)) {
  18177. $this->inthead = true;
  18178. // add a page (or trig AcceptPageBreak() for multicolumn mode)
  18179. $this->checkPageBreak($this->PageBreakTrigger + 1);
  18180. }
  18181. break;
  18182. }
  18183. case 'tr': {
  18184. // array of columns positions
  18185. $dom[$key]['cellpos'] = array();
  18186. break;
  18187. }
  18188. case 'hr': {
  18189. if ((isset($tag['height'])) AND ($tag['height'] != '')) {
  18190. $hrHeight = $this->getHTMLUnitToUnits($tag['height'], 1, 'px');
  18191. } else {
  18192. $hrHeight = $this->GetLineWidth();
  18193. }
  18194. $this->addHTMLVertSpace($hbz, max($hb, ($hrHeight / 2)), $cell, $firsttag);
  18195. $x = $this->GetX();
  18196. $y = $this->GetY();
  18197. $wtmp = $this->w - $this->lMargin - $this->rMargin;
  18198. if ($cell) {
  18199. $wtmp -= ($this->cell_padding['L'] + $this->cell_padding['R']);
  18200. }
  18201. if ((isset($tag['width'])) AND ($tag['width'] != '')) {
  18202. $hrWidth = $this->getHTMLUnitToUnits($tag['width'], $wtmp, 'px');
  18203. } else {
  18204. $hrWidth = $wtmp;
  18205. }
  18206. $prevlinewidth = $this->GetLineWidth();
  18207. $this->SetLineWidth($hrHeight);
  18208. $this->Line($x, $y, $x + $hrWidth, $y);
  18209. $this->SetLineWidth($prevlinewidth);
  18210. $this->addHTMLVertSpace(max($hbc, ($hrHeight / 2)), 0, $cell, !isset($dom[($key + 1)]));
  18211. break;
  18212. }
  18213. case 'a': {
  18214. if (array_key_exists('href', $tag['attribute'])) {
  18215. $this->HREF['url'] = $tag['attribute']['href'];
  18216. }
  18217. break;
  18218. }
  18219. case 'img': {
  18220. if (!empty($tag['attribute']['src'])) {
  18221. if ($tag['attribute']['src'][0] === '@') {
  18222. // data stream
  18223. $tag['attribute']['src'] = '@'.base64_decode(substr($tag['attribute']['src'], 1));
  18224. $type = '';
  18225. } else {
  18226. // get image type
  18227. $type = TCPDF_IMAGES::getImageFileType($tag['attribute']['src']);
  18228. }
  18229. if (!isset($tag['width'])) {
  18230. $tag['width'] = 0;
  18231. }
  18232. if (!isset($tag['height'])) {
  18233. $tag['height'] = 0;
  18234. }
  18235. //if (!isset($tag['attribute']['align'])) {
  18236. // the only alignment supported is "bottom"
  18237. // further development is required for other modes.
  18238. $tag['attribute']['align'] = 'bottom';
  18239. //}
  18240. switch($tag['attribute']['align']) {
  18241. case 'top': {
  18242. $align = 'T';
  18243. break;
  18244. }
  18245. case 'middle': {
  18246. $align = 'M';
  18247. break;
  18248. }
  18249. case 'bottom': {
  18250. $align = 'B';
  18251. break;
  18252. }
  18253. default: {
  18254. $align = 'B';
  18255. break;
  18256. }
  18257. }
  18258. $prevy = $this->y;
  18259. $xpos = $this->x;
  18260. $imglink = '';
  18261. if (isset($this->HREF['url']) AND !TCPDF_STATIC::empty_string($this->HREF['url'])) {
  18262. $imglink = $this->HREF['url'];
  18263. if ($imglink[0] == '#') {
  18264. // convert url to internal link
  18265. $lnkdata = explode(',', $imglink);
  18266. if (isset($lnkdata[0])) {
  18267. $page = intval(substr($lnkdata[0], 1));
  18268. if (empty($page) OR ($page <= 0)) {
  18269. $page = $this->page;
  18270. }
  18271. if (isset($lnkdata[1]) AND (strlen($lnkdata[1]) > 0)) {
  18272. $lnky = floatval($lnkdata[1]);
  18273. } else {
  18274. $lnky = 0;
  18275. }
  18276. $imglink = $this->AddLink();
  18277. $this->SetLink($imglink, $lnky, $page);
  18278. }
  18279. }
  18280. }
  18281. $border = 0;
  18282. if (isset($tag['border']) AND !empty($tag['border'])) {
  18283. // currently only support 1 (frame) or a combination of 'LTRB'
  18284. $border = $tag['border'];
  18285. }
  18286. $iw = '';
  18287. if (isset($tag['width'])) {
  18288. $iw = $this->getHTMLUnitToUnits($tag['width'], ($tag['fontsize'] / $this->k), 'px', false);
  18289. }
  18290. $ih = '';
  18291. if (isset($tag['height'])) {
  18292. $ih = $this->getHTMLUnitToUnits($tag['height'], ($tag['fontsize'] / $this->k), 'px', false);
  18293. }
  18294. if (($type == 'eps') OR ($type == 'ai')) {
  18295. $this->ImageEps($tag['attribute']['src'], $xpos, $this->y, $iw, $ih, $imglink, true, $align, '', $border, true);
  18296. } elseif ($type == 'svg') {
  18297. $this->ImageSVG($tag['attribute']['src'], $xpos, $this->y, $iw, $ih, $imglink, $align, '', $border, true);
  18298. } else {
  18299. $this->Image($tag['attribute']['src'], $xpos, $this->y, $iw, $ih, '', $imglink, $align, false, 300, '', false, false, $border, false, false, true);
  18300. }
  18301. switch($align) {
  18302. case 'T': {
  18303. $this->y = $prevy;
  18304. break;
  18305. }
  18306. case 'M': {
  18307. $this->y = (($this->img_rb_y + $prevy - ($this->getCellHeight($tag['fontsize'] / $this->k))) / 2);
  18308. break;
  18309. }
  18310. case 'B': {
  18311. $this->y = $this->img_rb_y - ($this->getCellHeight($tag['fontsize'] / $this->k) - ($this->getFontDescent($tag['fontname'], $tag['fontstyle'], $tag['fontsize']) * $this->cell_height_ratio));
  18312. break;
  18313. }
  18314. }
  18315. }
  18316. break;
  18317. }
  18318. case 'dl': {
  18319. ++$this->listnum;
  18320. if ($this->listnum == 1) {
  18321. $this->addHTMLVertSpace($hbz, $hb, $cell, $firsttag);
  18322. } else {
  18323. $this->addHTMLVertSpace(0, 0, $cell, $firsttag);
  18324. }
  18325. break;
  18326. }
  18327. case 'dt': {
  18328. $this->addHTMLVertSpace($hbz, $hb, $cell, $firsttag);
  18329. break;
  18330. }
  18331. case 'dd': {
  18332. if ($this->rtl) {
  18333. $this->rMargin += $this->listindent;
  18334. } else {
  18335. $this->lMargin += $this->listindent;
  18336. }
  18337. ++$this->listindentlevel;
  18338. $this->addHTMLVertSpace($hbz, $hb, $cell, $firsttag);
  18339. break;
  18340. }
  18341. case 'ul':
  18342. case 'ol': {
  18343. ++$this->listnum;
  18344. if ($tag['value'] == 'ol') {
  18345. $this->listordered[$this->listnum] = true;
  18346. } else {
  18347. $this->listordered[$this->listnum] = false;
  18348. }
  18349. if (isset($tag['attribute']['start'])) {
  18350. $this->listcount[$this->listnum] = intval($tag['attribute']['start']) - 1;
  18351. } else {
  18352. $this->listcount[$this->listnum] = 0;
  18353. }
  18354. if ($this->rtl) {
  18355. $this->rMargin += $this->listindent;
  18356. $this->x -= $this->listindent;
  18357. } else {
  18358. $this->lMargin += $this->listindent;
  18359. $this->x += $this->listindent;
  18360. }
  18361. ++$this->listindentlevel;
  18362. if ($this->listnum == 1) {
  18363. if ($key > 1) {
  18364. $this->addHTMLVertSpace($hbz, $hb, $cell, $firsttag);
  18365. }
  18366. } else {
  18367. $this->addHTMLVertSpace(0, 0, $cell, $firsttag);
  18368. }
  18369. break;
  18370. }
  18371. case 'li': {
  18372. if ($key > 2) {
  18373. $this->addHTMLVertSpace($hbz, $hb, $cell, $firsttag);
  18374. }
  18375. if ($this->listordered[$this->listnum]) {
  18376. // ordered item
  18377. if (isset($parent['attribute']['type']) AND !TCPDF_STATIC::empty_string($parent['attribute']['type'])) {
  18378. $this->lispacer = $parent['attribute']['type'];
  18379. } elseif (isset($parent['listtype']) AND !TCPDF_STATIC::empty_string($parent['listtype'])) {
  18380. $this->lispacer = $parent['listtype'];
  18381. } elseif (isset($this->lisymbol) AND !TCPDF_STATIC::empty_string($this->lisymbol)) {
  18382. $this->lispacer = $this->lisymbol;
  18383. } else {
  18384. $this->lispacer = '#';
  18385. }
  18386. ++$this->listcount[$this->listnum];
  18387. if (isset($tag['attribute']['value'])) {
  18388. $this->listcount[$this->listnum] = intval($tag['attribute']['value']);
  18389. }
  18390. } else {
  18391. // unordered item
  18392. if (isset($parent['attribute']['type']) AND !TCPDF_STATIC::empty_string($parent['attribute']['type'])) {
  18393. $this->lispacer = $parent['attribute']['type'];
  18394. } elseif (isset($parent['listtype']) AND !TCPDF_STATIC::empty_string($parent['listtype'])) {
  18395. $this->lispacer = $parent['listtype'];
  18396. } elseif (isset($this->lisymbol) AND !TCPDF_STATIC::empty_string($this->lisymbol)) {
  18397. $this->lispacer = $this->lisymbol;
  18398. } else {
  18399. $this->lispacer = '!';
  18400. }
  18401. }
  18402. break;
  18403. }
  18404. case 'blockquote': {
  18405. if ($this->rtl) {
  18406. $this->rMargin += $this->listindent;
  18407. } else {
  18408. $this->lMargin += $this->listindent;
  18409. }
  18410. ++$this->listindentlevel;
  18411. $this->addHTMLVertSpace($hbz, $hb, $cell, $firsttag);
  18412. break;
  18413. }
  18414. case 'br': {
  18415. $this->addHTMLVertSpace($hbz, $hb, $cell, $firsttag);
  18416. break;
  18417. }
  18418. case 'div': {
  18419. $this->addHTMLVertSpace($hbz, $hb, $cell, $firsttag);
  18420. break;
  18421. }
  18422. case 'p': {
  18423. $this->addHTMLVertSpace($hbz, $hb, $cell, $firsttag);
  18424. break;
  18425. }
  18426. case 'pre': {
  18427. $this->addHTMLVertSpace($hbz, $hb, $cell, $firsttag);
  18428. $this->premode = true;
  18429. break;
  18430. }
  18431. case 'sup': {
  18432. $this->SetXY($this->GetX(), $this->GetY() - ((0.7 * $this->FontSizePt) / $this->k));
  18433. break;
  18434. }
  18435. case 'sub': {
  18436. $this->SetXY($this->GetX(), $this->GetY() + ((0.3 * $this->FontSizePt) / $this->k));
  18437. break;
  18438. }
  18439. case 'h1':
  18440. case 'h2':
  18441. case 'h3':
  18442. case 'h4':
  18443. case 'h5':
  18444. case 'h6': {
  18445. $this->addHTMLVertSpace($hbz, $hb, $cell, $firsttag);
  18446. break;
  18447. }
  18448. // Form fields (since 4.8.000 - 2009-09-07)
  18449. case 'form': {
  18450. if (isset($tag['attribute']['action'])) {
  18451. $this->form_action = $tag['attribute']['action'];
  18452. } else {
  18453. $this->Error('Please explicitly set action attribute path!');
  18454. }
  18455. if (isset($tag['attribute']['enctype'])) {
  18456. $this->form_enctype = $tag['attribute']['enctype'];
  18457. } else {
  18458. $this->form_enctype = 'application/x-www-form-urlencoded';
  18459. }
  18460. if (isset($tag['attribute']['method'])) {
  18461. $this->form_mode = $tag['attribute']['method'];
  18462. } else {
  18463. $this->form_mode = 'post';
  18464. }
  18465. break;
  18466. }
  18467. case 'input': {
  18468. if (isset($tag['attribute']['name']) AND !TCPDF_STATIC::empty_string($tag['attribute']['name'])) {
  18469. $name = $tag['attribute']['name'];
  18470. } else {
  18471. break;
  18472. }
  18473. $prop = array();
  18474. $opt = array();
  18475. if (isset($tag['attribute']['readonly']) AND !TCPDF_STATIC::empty_string($tag['attribute']['readonly'])) {
  18476. $prop['readonly'] = true;
  18477. }
  18478. if (isset($tag['attribute']['value']) AND !TCPDF_STATIC::empty_string($tag['attribute']['value'])) {
  18479. $value = $tag['attribute']['value'];
  18480. }
  18481. if (isset($tag['attribute']['maxlength']) AND !TCPDF_STATIC::empty_string($tag['attribute']['maxlength'])) {
  18482. $opt['maxlen'] = intval($tag['attribute']['maxlength']);
  18483. }
  18484. $h = $this->getCellHeight($this->FontSize);
  18485. if (isset($tag['attribute']['size']) AND !TCPDF_STATIC::empty_string($tag['attribute']['size'])) {
  18486. $w = intval($tag['attribute']['size']) * $this->GetStringWidth(chr(32)) * 2;
  18487. } else {
  18488. $w = $h;
  18489. }
  18490. if (isset($tag['attribute']['checked']) AND (($tag['attribute']['checked'] == 'checked') OR ($tag['attribute']['checked'] == 'true'))) {
  18491. $checked = true;
  18492. } else {
  18493. $checked = false;
  18494. }
  18495. if (isset($tag['align'])) {
  18496. switch ($tag['align']) {
  18497. case 'C': {
  18498. $opt['q'] = 1;
  18499. break;
  18500. }
  18501. case 'R': {
  18502. $opt['q'] = 2;
  18503. break;
  18504. }
  18505. case 'L':
  18506. default: {
  18507. break;
  18508. }
  18509. }
  18510. }
  18511. switch ($tag['attribute']['type']) {
  18512. case 'text': {
  18513. if (isset($value)) {
  18514. $opt['v'] = $value;
  18515. }
  18516. $this->TextField($name, $w, $h, $prop, $opt, '', '', false);
  18517. break;
  18518. }
  18519. case 'password': {
  18520. if (isset($value)) {
  18521. $opt['v'] = $value;
  18522. }
  18523. $prop['password'] = 'true';
  18524. $this->TextField($name, $w, $h, $prop, $opt, '', '', false);
  18525. break;
  18526. }
  18527. case 'checkbox': {
  18528. if (!isset($value)) {
  18529. break;
  18530. }
  18531. $this->CheckBox($name, $w, $checked, $prop, $opt, $value, '', '', false);
  18532. break;
  18533. }
  18534. case 'radio': {
  18535. if (!isset($value)) {
  18536. break;
  18537. }
  18538. $this->RadioButton($name, $w, $prop, $opt, $value, $checked, '', '', false);
  18539. break;
  18540. }
  18541. case 'submit': {
  18542. if (!isset($value)) {
  18543. $value = 'submit';
  18544. }
  18545. $w = $this->GetStringWidth($value) * 1.5;
  18546. $h *= 1.6;
  18547. $prop = array('lineWidth'=>1, 'borderStyle'=>'beveled', 'fillColor'=>array(196, 196, 196), 'strokeColor'=>array(255, 255, 255));
  18548. $action = array();
  18549. $action['S'] = 'SubmitForm';
  18550. $action['F'] = $this->form_action;
  18551. if ($this->form_enctype != 'FDF') {
  18552. $action['Flags'] = array('ExportFormat');
  18553. }
  18554. if ($this->form_mode == 'get') {
  18555. $action['Flags'] = array('GetMethod');
  18556. }
  18557. $this->Button($name, $w, $h, $value, $action, $prop, $opt, '', '', false);
  18558. break;
  18559. }
  18560. case 'reset': {
  18561. if (!isset($value)) {
  18562. $value = 'reset';
  18563. }
  18564. $w = $this->GetStringWidth($value) * 1.5;
  18565. $h *= 1.6;
  18566. $prop = array('lineWidth'=>1, 'borderStyle'=>'beveled', 'fillColor'=>array(196, 196, 196), 'strokeColor'=>array(255, 255, 255));
  18567. $this->Button($name, $w, $h, $value, array('S'=>'ResetForm'), $prop, $opt, '', '', false);
  18568. break;
  18569. }
  18570. case 'file': {
  18571. $prop['fileSelect'] = 'true';
  18572. $this->TextField($name, $w, $h, $prop, $opt, '', '', false);
  18573. if (!isset($value)) {
  18574. $value = '*';
  18575. }
  18576. $w = $this->GetStringWidth($value) * 2;
  18577. $h *= 1.2;
  18578. $prop = array('lineWidth'=>1, 'borderStyle'=>'beveled', 'fillColor'=>array(196, 196, 196), 'strokeColor'=>array(255, 255, 255));
  18579. $jsaction = 'var f=this.getField(\''.$name.'\'); f.browseForFileToSubmit();';
  18580. $this->Button('FB_'.$name, $w, $h, $value, $jsaction, $prop, $opt, '', '', false);
  18581. break;
  18582. }
  18583. case 'hidden': {
  18584. if (isset($value)) {
  18585. $opt['v'] = $value;
  18586. }
  18587. $opt['f'] = array('invisible', 'hidden');
  18588. $this->TextField($name, 0, 0, $prop, $opt, '', '', false);
  18589. break;
  18590. }
  18591. case 'image': {
  18592. // THIS TYPE MUST BE FIXED
  18593. if (isset($tag['attribute']['src']) AND !TCPDF_STATIC::empty_string($tag['attribute']['src'])) {
  18594. $img = $tag['attribute']['src'];
  18595. } else {
  18596. break;
  18597. }
  18598. $value = 'img';
  18599. //$opt['mk'] = array('i'=>$img, 'tp'=>1, 'if'=>array('sw'=>'A', 's'=>'A', 'fb'=>false));
  18600. if (isset($tag['attribute']['onclick']) AND !empty($tag['attribute']['onclick'])) {
  18601. $jsaction = $tag['attribute']['onclick'];
  18602. } else {
  18603. $jsaction = '';
  18604. }
  18605. $this->Button($name, $w, $h, $value, $jsaction, $prop, $opt, '', '', false);
  18606. break;
  18607. }
  18608. case 'button': {
  18609. if (!isset($value)) {
  18610. $value = ' ';
  18611. }
  18612. $w = $this->GetStringWidth($value) * 1.5;
  18613. $h *= 1.6;
  18614. $prop = array('lineWidth'=>1, 'borderStyle'=>'beveled', 'fillColor'=>array(196, 196, 196), 'strokeColor'=>array(255, 255, 255));
  18615. if (isset($tag['attribute']['onclick']) AND !empty($tag['attribute']['onclick'])) {
  18616. $jsaction = $tag['attribute']['onclick'];
  18617. } else {
  18618. $jsaction = '';
  18619. }
  18620. $this->Button($name, $w, $h, $value, $jsaction, $prop, $opt, '', '', false);
  18621. break;
  18622. }
  18623. }
  18624. break;
  18625. }
  18626. case 'textarea': {
  18627. $prop = array();
  18628. $opt = array();
  18629. if (isset($tag['attribute']['readonly']) AND !TCPDF_STATIC::empty_string($tag['attribute']['readonly'])) {
  18630. $prop['readonly'] = true;
  18631. }
  18632. if (isset($tag['attribute']['name']) AND !TCPDF_STATIC::empty_string($tag['attribute']['name'])) {
  18633. $name = $tag['attribute']['name'];
  18634. } else {
  18635. break;
  18636. }
  18637. if (isset($tag['attribute']['value']) AND !TCPDF_STATIC::empty_string($tag['attribute']['value'])) {
  18638. $opt['v'] = $tag['attribute']['value'];
  18639. }
  18640. if (isset($tag['attribute']['cols']) AND !TCPDF_STATIC::empty_string($tag['attribute']['cols'])) {
  18641. $w = intval($tag['attribute']['cols']) * $this->GetStringWidth(chr(32)) * 2;
  18642. } else {
  18643. $w = 40;
  18644. }
  18645. if (isset($tag['attribute']['rows']) AND !TCPDF_STATIC::empty_string($tag['attribute']['rows'])) {
  18646. $h = intval($tag['attribute']['rows']) * $this->getCellHeight($this->FontSize);
  18647. } else {
  18648. $h = 10;
  18649. }
  18650. $prop['multiline'] = 'true';
  18651. $this->TextField($name, $w, $h, $prop, $opt, '', '', false);
  18652. break;
  18653. }
  18654. case 'select': {
  18655. $h = $this->getCellHeight($this->FontSize);
  18656. if (isset($tag['attribute']['size']) AND !TCPDF_STATIC::empty_string($tag['attribute']['size'])) {
  18657. $h *= ($tag['attribute']['size'] + 1);
  18658. }
  18659. $prop = array();
  18660. $opt = array();
  18661. if (isset($tag['attribute']['name']) AND !TCPDF_STATIC::empty_string($tag['attribute']['name'])) {
  18662. $name = $tag['attribute']['name'];
  18663. } else {
  18664. break;
  18665. }
  18666. $w = 0;
  18667. if (isset($tag['attribute']['opt']) AND !TCPDF_STATIC::empty_string($tag['attribute']['opt'])) {
  18668. $options = explode('#!NwL!#', $tag['attribute']['opt']);
  18669. $values = array();
  18670. foreach ($options as $val) {
  18671. if (strpos($val, '#!TaB!#') !== false) {
  18672. $opts = explode('#!TaB!#', $val);
  18673. $values[] = $opts;
  18674. $w = max($w, $this->GetStringWidth($opts[1]));
  18675. } else {
  18676. $values[] = $val;
  18677. $w = max($w, $this->GetStringWidth($val));
  18678. }
  18679. }
  18680. } else {
  18681. break;
  18682. }
  18683. $w *= 2;
  18684. if (isset($tag['attribute']['multiple']) AND ($tag['attribute']['multiple']='multiple')) {
  18685. $prop['multipleSelection'] = 'true';
  18686. $this->ListBox($name, $w, $h, $values, $prop, $opt, '', '', false);
  18687. } else {
  18688. $this->ComboBox($name, $w, $h, $values, $prop, $opt, '', '', false);
  18689. }
  18690. break;
  18691. }
  18692. case 'tcpdf': {
  18693. if (defined('K_TCPDF_CALLS_IN_HTML') AND (K_TCPDF_CALLS_IN_HTML === true)) {
  18694. // Special tag used to call TCPDF methods
  18695. if (isset($tag['attribute']['method'])) {
  18696. $tcpdf_method = $tag['attribute']['method'];
  18697. if (method_exists($this, $tcpdf_method)) {
  18698. if (isset($tag['attribute']['params']) AND (!empty($tag['attribute']['params']))) {
  18699. $params = $this->unserializeTCPDFtagParameters($tag['attribute']['params']);
  18700. call_user_func_array(array($this, $tcpdf_method), $params);
  18701. } else {
  18702. $this->$tcpdf_method();
  18703. }
  18704. $this->newline = true;
  18705. }
  18706. }
  18707. }
  18708. break;
  18709. }
  18710. default: {
  18711. break;
  18712. }
  18713. }
  18714. // define tags that support borders and background colors
  18715. $bordertags = array('blockquote','br','dd','dl','div','dt','h1','h2','h3','h4','h5','h6','hr','li','ol','p','pre','ul','tcpdf','table');
  18716. if (in_array($tag['value'], $bordertags)) {
  18717. // set border
  18718. $dom[$key]['borderposition'] = $this->getBorderStartPosition();
  18719. }
  18720. if ($dom[$key]['self'] AND isset($dom[$key]['attribute']['pagebreakafter'])) {
  18721. $pba = $dom[$key]['attribute']['pagebreakafter'];
  18722. // check for pagebreak
  18723. if (($pba == 'true') OR ($pba == 'left') OR ($pba == 'right')) {
  18724. // add a page (or trig AcceptPageBreak() for multicolumn mode)
  18725. $this->checkPageBreak($this->PageBreakTrigger + 1);
  18726. }
  18727. if ((($pba == 'left') AND (((!$this->rtl) AND (($this->page % 2) == 0)) OR (($this->rtl) AND (($this->page % 2) != 0))))
  18728. OR (($pba == 'right') AND (((!$this->rtl) AND (($this->page % 2) != 0)) OR (($this->rtl) AND (($this->page % 2) == 0))))) {
  18729. // add a page (or trig AcceptPageBreak() for multicolumn mode)
  18730. $this->checkPageBreak($this->PageBreakTrigger + 1);
  18731. }
  18732. }
  18733. return $dom;
  18734. }
  18735. /**
  18736. * Process closing tags.
  18737. * @param $dom (array) html dom array
  18738. * @param $key (int) current element id
  18739. * @param $cell (boolean) if true add the default left (or right if RTL) padding to each new line (default false).
  18740. * @param $maxbottomliney (int) maximum y value of current line
  18741. * @return $dom array
  18742. * @protected
  18743. */
  18744. protected function closeHTMLTagHandler($dom, $key, $cell, $maxbottomliney=0) {
  18745. $tag = $dom[$key];
  18746. $parent = $dom[($dom[$key]['parent'])];
  18747. $lasttag = ((!isset($dom[($key + 1)])) OR ((!isset($dom[($key + 2)])) AND ($dom[($key + 1)]['value'] == 'marker')));
  18748. $in_table_head = false;
  18749. // maximum x position (used to draw borders)
  18750. if ($this->rtl) {
  18751. $xmax = $this->w;
  18752. } else {
  18753. $xmax = 0;
  18754. }
  18755. if ($tag['block']) {
  18756. $hbz = 0; // distance from y to line bottom
  18757. $hb = 0; // vertical space between block tags
  18758. // calculate vertical space for block tags
  18759. if (isset($this->tagvspaces[$tag['value']][1]['h']) AND ($this->tagvspaces[$tag['value']][1]['h'] >= 0)) {
  18760. $pre_h = $this->tagvspaces[$tag['value']][1]['h'];
  18761. } elseif (isset($parent['fontsize'])) {
  18762. $pre_h = $this->getCellHeight($parent['fontsize'] / $this->k);
  18763. } else {
  18764. $pre_h = $this->getCellHeight($this->FontSize);
  18765. }
  18766. if (isset($this->tagvspaces[$tag['value']][1]['n'])) {
  18767. $cn = $this->tagvspaces[$tag['value']][1]['n'];
  18768. } elseif (preg_match('/[h][0-9]/', $tag['value']) > 0) {
  18769. $cn = 0.6;
  18770. } else {
  18771. $cn = 1;
  18772. }
  18773. if ((!isset($this->tagvspaces[$tag['value']])) AND ($tag['value'] == 'div')) {
  18774. $hb = 0;
  18775. } else {
  18776. $hb = ($cn * $pre_h);
  18777. }
  18778. if ($maxbottomliney > $this->PageBreakTrigger) {
  18779. $hbz = $this->getCellHeight($this->FontSize);
  18780. } elseif ($this->y < $maxbottomliney) {
  18781. $hbz = ($maxbottomliney - $this->y);
  18782. }
  18783. }
  18784. // Closing tag
  18785. switch($tag['value']) {
  18786. case 'tr': {
  18787. $table_el = $dom[($dom[$key]['parent'])]['parent'];
  18788. if (!isset($parent['endy'])) {
  18789. $dom[($dom[$key]['parent'])]['endy'] = $this->y;
  18790. $parent['endy'] = $this->y;
  18791. }
  18792. if (!isset($parent['endpage'])) {
  18793. $dom[($dom[$key]['parent'])]['endpage'] = $this->page;
  18794. $parent['endpage'] = $this->page;
  18795. }
  18796. if (!isset($parent['endcolumn'])) {
  18797. $dom[($dom[$key]['parent'])]['endcolumn'] = $this->current_column;
  18798. $parent['endcolumn'] = $this->current_column;
  18799. }
  18800. // update row-spanned cells
  18801. if (isset($dom[$table_el]['rowspans'])) {
  18802. foreach ($dom[$table_el]['rowspans'] as $k => $trwsp) {
  18803. $dom[$table_el]['rowspans'][$k]['rowspan'] -= 1;
  18804. if ($dom[$table_el]['rowspans'][$k]['rowspan'] == 0) {
  18805. if (($dom[$table_el]['rowspans'][$k]['endpage'] == $parent['endpage']) AND ($dom[$table_el]['rowspans'][$k]['endcolumn'] == $parent['endcolumn'])) {
  18806. $dom[($dom[$key]['parent'])]['endy'] = max($dom[$table_el]['rowspans'][$k]['endy'], $parent['endy']);
  18807. } elseif (($dom[$table_el]['rowspans'][$k]['endpage'] > $parent['endpage']) OR ($dom[$table_el]['rowspans'][$k]['endcolumn'] > $parent['endcolumn'])) {
  18808. $dom[($dom[$key]['parent'])]['endy'] = $dom[$table_el]['rowspans'][$k]['endy'];
  18809. $dom[($dom[$key]['parent'])]['endpage'] = $dom[$table_el]['rowspans'][$k]['endpage'];
  18810. $dom[($dom[$key]['parent'])]['endcolumn'] = $dom[$table_el]['rowspans'][$k]['endcolumn'];
  18811. }
  18812. }
  18813. }
  18814. // report new endy and endpage to the rowspanned cells
  18815. foreach ($dom[$table_el]['rowspans'] as $k => $trwsp) {
  18816. if ($dom[$table_el]['rowspans'][$k]['rowspan'] == 0) {
  18817. $dom[$table_el]['rowspans'][$k]['endpage'] = max($dom[$table_el]['rowspans'][$k]['endpage'], $dom[($dom[$key]['parent'])]['endpage']);
  18818. $dom[($dom[$key]['parent'])]['endpage'] = $dom[$table_el]['rowspans'][$k]['endpage'];
  18819. $dom[$table_el]['rowspans'][$k]['endcolumn'] = max($dom[$table_el]['rowspans'][$k]['endcolumn'], $dom[($dom[$key]['parent'])]['endcolumn']);
  18820. $dom[($dom[$key]['parent'])]['endcolumn'] = $dom[$table_el]['rowspans'][$k]['endcolumn'];
  18821. $dom[$table_el]['rowspans'][$k]['endy'] = max($dom[$table_el]['rowspans'][$k]['endy'], $dom[($dom[$key]['parent'])]['endy']);
  18822. $dom[($dom[$key]['parent'])]['endy'] = $dom[$table_el]['rowspans'][$k]['endy'];
  18823. }
  18824. }
  18825. // update remaining rowspanned cells
  18826. foreach ($dom[$table_el]['rowspans'] as $k => $trwsp) {
  18827. if ($dom[$table_el]['rowspans'][$k]['rowspan'] == 0) {
  18828. $dom[$table_el]['rowspans'][$k]['endpage'] = $dom[($dom[$key]['parent'])]['endpage'];
  18829. $dom[$table_el]['rowspans'][$k]['endcolumn'] = $dom[($dom[$key]['parent'])]['endcolumn'];
  18830. $dom[$table_el]['rowspans'][$k]['endy'] = $dom[($dom[$key]['parent'])]['endy'];
  18831. }
  18832. }
  18833. }
  18834. $prev_page = $this->page;
  18835. $this->setPage($dom[($dom[$key]['parent'])]['endpage']);
  18836. if ($this->num_columns > 1) {
  18837. if (($prev_page < $this->page)
  18838. AND ((($this->current_column == 0) AND ($dom[($dom[$key]['parent'])]['endcolumn'] == ($this->num_columns - 1)))
  18839. OR ($this->current_column == $dom[($dom[$key]['parent'])]['endcolumn']))) {
  18840. // page jump
  18841. $this->selectColumn(0);
  18842. $dom[($dom[$key]['parent'])]['endcolumn'] = 0;
  18843. $dom[($dom[$key]['parent'])]['endy'] = $this->y;
  18844. } else {
  18845. $this->selectColumn($dom[($dom[$key]['parent'])]['endcolumn']);
  18846. $this->y = $dom[($dom[$key]['parent'])]['endy'];
  18847. }
  18848. } else {
  18849. $this->y = $dom[($dom[$key]['parent'])]['endy'];
  18850. }
  18851. if (isset($dom[$table_el]['attribute']['cellspacing'])) {
  18852. $this->y += $this->getHTMLUnitToUnits($dom[$table_el]['attribute']['cellspacing'], 1, 'px');
  18853. } elseif (isset($dom[$table_el]['border-spacing'])) {
  18854. $this->y += $dom[$table_el]['border-spacing']['V'];
  18855. }
  18856. $this->Ln(0, $cell);
  18857. if ($this->current_column == $parent['startcolumn']) {
  18858. $this->x = $parent['startx'];
  18859. }
  18860. // account for booklet mode
  18861. if ($this->page > $parent['startpage']) {
  18862. if (($this->rtl) AND ($this->pagedim[$this->page]['orm'] != $this->pagedim[$parent['startpage']]['orm'])) {
  18863. $this->x -= ($this->pagedim[$this->page]['orm'] - $this->pagedim[$parent['startpage']]['orm']);
  18864. } elseif ((!$this->rtl) AND ($this->pagedim[$this->page]['olm'] != $this->pagedim[$parent['startpage']]['olm'])) {
  18865. $this->x += ($this->pagedim[$this->page]['olm'] - $this->pagedim[$parent['startpage']]['olm']);
  18866. }
  18867. }
  18868. break;
  18869. }
  18870. case 'tablehead':
  18871. // closing tag used for the thead part
  18872. $in_table_head = true;
  18873. $this->inthead = false;
  18874. case 'table': {
  18875. $table_el = $parent;
  18876. // set default border
  18877. if (isset($table_el['attribute']['border']) AND ($table_el['attribute']['border'] > 0)) {
  18878. // set default border
  18879. $border = array('LTRB' => array('width' => $this->getCSSBorderWidth($table_el['attribute']['border']), 'cap'=>'square', 'join'=>'miter', 'dash'=> 0, 'color'=>array(0,0,0)));
  18880. } else {
  18881. $border = 0;
  18882. }
  18883. $default_border = $border;
  18884. // fix bottom line alignment of last line before page break
  18885. foreach ($dom[($dom[$key]['parent'])]['trids'] as $j => $trkey) {
  18886. // update row-spanned cells
  18887. if (isset($dom[($dom[$key]['parent'])]['rowspans'])) {
  18888. foreach ($dom[($dom[$key]['parent'])]['rowspans'] as $k => $trwsp) {
  18889. if (isset($prevtrkey) AND ($trwsp['trid'] == $prevtrkey) AND ($trwsp['mrowspan'] > 0)) {
  18890. $dom[($dom[$key]['parent'])]['rowspans'][$k]['trid'] = $trkey;
  18891. }
  18892. if ($dom[($dom[$key]['parent'])]['rowspans'][$k]['trid'] == $trkey) {
  18893. $dom[($dom[$key]['parent'])]['rowspans'][$k]['mrowspan'] -= 1;
  18894. }
  18895. }
  18896. }
  18897. if (isset($prevtrkey) AND ($dom[$trkey]['startpage'] > $dom[$prevtrkey]['endpage'])) {
  18898. $pgendy = $this->pagedim[$dom[$prevtrkey]['endpage']]['hk'] - $this->pagedim[$dom[$prevtrkey]['endpage']]['bm'];
  18899. $dom[$prevtrkey]['endy'] = $pgendy;
  18900. // update row-spanned cells
  18901. if (isset($dom[($dom[$key]['parent'])]['rowspans'])) {
  18902. foreach ($dom[($dom[$key]['parent'])]['rowspans'] as $k => $trwsp) {
  18903. if (($trwsp['trid'] == $prevtrkey) AND ($trwsp['mrowspan'] >= 0) AND ($trwsp['endpage'] == $dom[$prevtrkey]['endpage'])) {
  18904. $dom[($dom[$key]['parent'])]['rowspans'][$k]['endy'] = $pgendy;
  18905. $dom[($dom[$key]['parent'])]['rowspans'][$k]['mrowspan'] = -1;
  18906. }
  18907. }
  18908. }
  18909. }
  18910. $prevtrkey = $trkey;
  18911. $table_el = $dom[($dom[$key]['parent'])];
  18912. }
  18913. // for each row
  18914. if (count($table_el['trids']) > 0) {
  18915. unset($xmax);
  18916. }
  18917. foreach ($table_el['trids'] as $j => $trkey) {
  18918. $parent = $dom[$trkey];
  18919. if (!isset($xmax)) {
  18920. $xmax = $parent['cellpos'][(count($parent['cellpos']) - 1)]['endx'];
  18921. }
  18922. // for each cell on the row
  18923. foreach ($parent['cellpos'] as $k => $cellpos) {
  18924. if (isset($cellpos['rowspanid']) AND ($cellpos['rowspanid'] >= 0)) {
  18925. $cellpos['startx'] = $table_el['rowspans'][($cellpos['rowspanid'])]['startx'];
  18926. $cellpos['endx'] = $table_el['rowspans'][($cellpos['rowspanid'])]['endx'];
  18927. $endy = $table_el['rowspans'][($cellpos['rowspanid'])]['endy'];
  18928. $startpage = $table_el['rowspans'][($cellpos['rowspanid'])]['startpage'];
  18929. $endpage = $table_el['rowspans'][($cellpos['rowspanid'])]['endpage'];
  18930. $startcolumn = $table_el['rowspans'][($cellpos['rowspanid'])]['startcolumn'];
  18931. $endcolumn = $table_el['rowspans'][($cellpos['rowspanid'])]['endcolumn'];
  18932. } else {
  18933. $endy = $parent['endy'];
  18934. $startpage = $parent['startpage'];
  18935. $endpage = $parent['endpage'];
  18936. $startcolumn = $parent['startcolumn'];
  18937. $endcolumn = $parent['endcolumn'];
  18938. }
  18939. if ($this->num_columns == 0) {
  18940. $this->num_columns = 1;
  18941. }
  18942. if (isset($cellpos['border'])) {
  18943. $border = $cellpos['border'];
  18944. }
  18945. if (isset($cellpos['bgcolor']) AND ($cellpos['bgcolor']) !== false) {
  18946. $this->SetFillColorArray($cellpos['bgcolor']);
  18947. $fill = true;
  18948. } else {
  18949. $fill = false;
  18950. }
  18951. $x = $cellpos['startx'];
  18952. $y = $parent['starty'];
  18953. $starty = $y;
  18954. $w = abs($cellpos['endx'] - $cellpos['startx']);
  18955. // get border modes
  18956. $border_start = TCPDF_STATIC::getBorderMode($border, $position='start', $this->opencell);
  18957. $border_end = TCPDF_STATIC::getBorderMode($border, $position='end', $this->opencell);
  18958. $border_middle = TCPDF_STATIC::getBorderMode($border, $position='middle', $this->opencell);
  18959. // design borders around HTML cells.
  18960. for ($page = $startpage; $page <= $endpage; ++$page) { // for each page
  18961. $ccode = '';
  18962. $this->setPage($page);
  18963. if ($this->num_columns < 2) {
  18964. // single-column mode
  18965. $this->x = $x;
  18966. $this->y = $this->tMargin;
  18967. }
  18968. // account for margin changes
  18969. if ($page > $startpage) {
  18970. if (($this->rtl) AND ($this->pagedim[$page]['orm'] != $this->pagedim[$startpage]['orm'])) {
  18971. $this->x -= ($this->pagedim[$page]['orm'] - $this->pagedim[$startpage]['orm']);
  18972. } elseif ((!$this->rtl) AND ($this->pagedim[$page]['olm'] != $this->pagedim[$startpage]['olm'])) {
  18973. $this->x += ($this->pagedim[$page]['olm'] - $this->pagedim[$startpage]['olm']);
  18974. }
  18975. }
  18976. if ($startpage == $endpage) { // single page
  18977. $deltacol = 0;
  18978. $deltath = 0;
  18979. for ($column = $startcolumn; $column <= $endcolumn; ++$column) { // for each column
  18980. $this->selectColumn($column);
  18981. if ($startcolumn == $endcolumn) { // single column
  18982. $cborder = $border;
  18983. $h = $endy - $parent['starty'];
  18984. $this->y = $y;
  18985. $this->x = $x;
  18986. } elseif ($column == $startcolumn) { // first column
  18987. $cborder = $border_start;
  18988. $this->y = $starty;
  18989. $this->x = $x;
  18990. $h = $this->h - $this->y - $this->bMargin;
  18991. if ($this->rtl) {
  18992. $deltacol = $this->x + $this->rMargin - $this->w;
  18993. } else {
  18994. $deltacol = $this->x - $this->lMargin;
  18995. }
  18996. } elseif ($column == $endcolumn) { // end column
  18997. $cborder = $border_end;
  18998. if (isset($this->columns[$column]['th']['\''.$page.'\''])) {
  18999. $this->y = $this->columns[$column]['th']['\''.$page.'\''];
  19000. }
  19001. $this->x += $deltacol;
  19002. $h = $endy - $this->y;
  19003. } else { // middle column
  19004. $cborder = $border_middle;
  19005. if (isset($this->columns[$column]['th']['\''.$page.'\''])) {
  19006. $this->y = $this->columns[$column]['th']['\''.$page.'\''];
  19007. }
  19008. $this->x += $deltacol;
  19009. $h = $this->h - $this->y - $this->bMargin;
  19010. }
  19011. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
  19012. } // end for each column
  19013. } elseif ($page == $startpage) { // first page
  19014. $deltacol = 0;
  19015. $deltath = 0;
  19016. for ($column = $startcolumn; $column < $this->num_columns; ++$column) { // for each column
  19017. $this->selectColumn($column);
  19018. if ($column == $startcolumn) { // first column
  19019. $cborder = $border_start;
  19020. $this->y = $starty;
  19021. $this->x = $x;
  19022. $h = $this->h - $this->y - $this->bMargin;
  19023. if ($this->rtl) {
  19024. $deltacol = $this->x + $this->rMargin - $this->w;
  19025. } else {
  19026. $deltacol = $this->x - $this->lMargin;
  19027. }
  19028. } else { // middle column
  19029. $cborder = $border_middle;
  19030. if (isset($this->columns[$column]['th']['\''.$page.'\''])) {
  19031. $this->y = $this->columns[$column]['th']['\''.$page.'\''];
  19032. }
  19033. $this->x += $deltacol;
  19034. $h = $this->h - $this->y - $this->bMargin;
  19035. }
  19036. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
  19037. } // end for each column
  19038. } elseif ($page == $endpage) { // last page
  19039. $deltacol = 0;
  19040. $deltath = 0;
  19041. for ($column = 0; $column <= $endcolumn; ++$column) { // for each column
  19042. $this->selectColumn($column);
  19043. if ($column == $endcolumn) { // end column
  19044. $cborder = $border_end;
  19045. if (isset($this->columns[$column]['th']['\''.$page.'\''])) {
  19046. $this->y = $this->columns[$column]['th']['\''.$page.'\''];
  19047. }
  19048. $this->x += $deltacol;
  19049. $h = $endy - $this->y;
  19050. } else { // middle column
  19051. $cborder = $border_middle;
  19052. if (isset($this->columns[$column]['th']['\''.$page.'\''])) {
  19053. $this->y = $this->columns[$column]['th']['\''.$page.'\''];
  19054. }
  19055. $this->x += $deltacol;
  19056. $h = $this->h - $this->y - $this->bMargin;
  19057. }
  19058. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
  19059. } // end for each column
  19060. } else { // middle page
  19061. $deltacol = 0;
  19062. $deltath = 0;
  19063. for ($column = 0; $column < $this->num_columns; ++$column) { // for each column
  19064. $this->selectColumn($column);
  19065. $cborder = $border_middle;
  19066. if (isset($this->columns[$column]['th']['\''.$page.'\''])) {
  19067. $this->y = $this->columns[$column]['th']['\''.$page.'\''];
  19068. }
  19069. $this->x += $deltacol;
  19070. $h = $this->h - $this->y - $this->bMargin;
  19071. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
  19072. } // end for each column
  19073. }
  19074. if (!empty($cborder) OR !empty($fill)) {
  19075. $offsetlen = strlen($ccode);
  19076. // draw border and fill
  19077. if ($this->inxobj) {
  19078. // we are inside an XObject template
  19079. if (end($this->xobjects[$this->xobjid]['transfmrk']) !== false) {
  19080. $pagemarkkey = key($this->xobjects[$this->xobjid]['transfmrk']);
  19081. $pagemark = $this->xobjects[$this->xobjid]['transfmrk'][$pagemarkkey];
  19082. $this->xobjects[$this->xobjid]['transfmrk'][$pagemarkkey] += $offsetlen;
  19083. } else {
  19084. $pagemark = $this->xobjects[$this->xobjid]['intmrk'];
  19085. $this->xobjects[$this->xobjid]['intmrk'] += $offsetlen;
  19086. }
  19087. $pagebuff = $this->xobjects[$this->xobjid]['outdata'];
  19088. $pstart = substr($pagebuff, 0, $pagemark);
  19089. $pend = substr($pagebuff, $pagemark);
  19090. $this->xobjects[$this->xobjid]['outdata'] = $pstart.$ccode.$pend;
  19091. } else {
  19092. // draw border and fill
  19093. if (end($this->transfmrk[$this->page]) !== false) {
  19094. $pagemarkkey = key($this->transfmrk[$this->page]);
  19095. $pagemark = $this->transfmrk[$this->page][$pagemarkkey];
  19096. } elseif ($this->InFooter) {
  19097. $pagemark = $this->footerpos[$this->page];
  19098. } else {
  19099. $pagemark = $this->intmrk[$this->page];
  19100. }
  19101. $pagebuff = $this->getPageBuffer($this->page);
  19102. $pstart = substr($pagebuff, 0, $pagemark);
  19103. $pend = substr($pagebuff, $pagemark);
  19104. $this->setPageBuffer($this->page, $pstart.$ccode.$pend);
  19105. }
  19106. }
  19107. } // end for each page
  19108. // restore default border
  19109. $border = $default_border;
  19110. } // end for each cell on the row
  19111. if (isset($table_el['attribute']['cellspacing'])) {
  19112. $this->y += $this->getHTMLUnitToUnits($table_el['attribute']['cellspacing'], 1, 'px');
  19113. } elseif (isset($table_el['border-spacing'])) {
  19114. $this->y += $table_el['border-spacing']['V'];
  19115. }
  19116. $this->Ln(0, $cell);
  19117. $this->x = $parent['startx'];
  19118. if ($endpage > $startpage) {
  19119. if (($this->rtl) AND ($this->pagedim[$endpage]['orm'] != $this->pagedim[$startpage]['orm'])) {
  19120. $this->x += ($this->pagedim[$endpage]['orm'] - $this->pagedim[$startpage]['orm']);
  19121. } elseif ((!$this->rtl) AND ($this->pagedim[$endpage]['olm'] != $this->pagedim[$startpage]['olm'])) {
  19122. $this->x += ($this->pagedim[$endpage]['olm'] - $this->pagedim[$startpage]['olm']);
  19123. }
  19124. }
  19125. }
  19126. if (!$in_table_head) { // we are not inside a thead section
  19127. $this->cell_padding = $table_el['old_cell_padding'];
  19128. // reset row height
  19129. $this->resetLastH();
  19130. if (($this->page == ($this->numpages - 1)) AND ($this->pageopen[$this->numpages])) {
  19131. $plendiff = ($this->pagelen[$this->numpages] - $this->emptypagemrk[$this->numpages]);
  19132. if (($plendiff > 0) AND ($plendiff < 60)) {
  19133. $pagediff = substr($this->getPageBuffer($this->numpages), $this->emptypagemrk[$this->numpages], $plendiff);
  19134. if (substr($pagediff, 0, 5) == 'BT /F') {
  19135. // the difference is only a font setting
  19136. $plendiff = 0;
  19137. }
  19138. }
  19139. if ($plendiff == 0) {
  19140. // remove last blank page
  19141. $this->deletePage($this->numpages);
  19142. }
  19143. }
  19144. if (isset($this->theadMargins['top'])) {
  19145. // restore top margin
  19146. $this->tMargin = $this->theadMargins['top'];
  19147. }
  19148. if (!isset($table_el['attribute']['nested']) OR ($table_el['attribute']['nested'] != 'true')) {
  19149. // reset main table header
  19150. $this->thead = '';
  19151. $this->theadMargins = array();
  19152. $this->pagedim[$this->page]['tm'] = $this->tMargin;
  19153. }
  19154. }
  19155. $parent = $table_el;
  19156. break;
  19157. }
  19158. case 'a': {
  19159. $this->HREF = array();
  19160. break;
  19161. }
  19162. case 'sup': {
  19163. $this->SetXY($this->GetX(), $this->GetY() + ((0.7 * $parent['fontsize']) / $this->k));
  19164. break;
  19165. }
  19166. case 'sub': {
  19167. $this->SetXY($this->GetX(), $this->GetY() - ((0.3 * $parent['fontsize']) / $this->k));
  19168. break;
  19169. }
  19170. case 'div': {
  19171. $this->addHTMLVertSpace($hbz, $hb, $cell, false, $lasttag);
  19172. break;
  19173. }
  19174. case 'blockquote': {
  19175. if ($this->rtl) {
  19176. $this->rMargin -= $this->listindent;
  19177. } else {
  19178. $this->lMargin -= $this->listindent;
  19179. }
  19180. --$this->listindentlevel;
  19181. $this->addHTMLVertSpace($hbz, $hb, $cell, false, $lasttag);
  19182. break;
  19183. }
  19184. case 'p': {
  19185. $this->addHTMLVertSpace($hbz, $hb, $cell, false, $lasttag);
  19186. break;
  19187. }
  19188. case 'pre': {
  19189. $this->addHTMLVertSpace($hbz, $hb, $cell, false, $lasttag);
  19190. $this->premode = false;
  19191. break;
  19192. }
  19193. case 'dl': {
  19194. --$this->listnum;
  19195. if ($this->listnum <= 0) {
  19196. $this->listnum = 0;
  19197. $this->addHTMLVertSpace($hbz, $hb, $cell, false, $lasttag);
  19198. } else {
  19199. $this->addHTMLVertSpace(0, 0, $cell, false, $lasttag);
  19200. }
  19201. $this->resetLastH();
  19202. break;
  19203. }
  19204. case 'dt': {
  19205. $this->lispacer = '';
  19206. $this->addHTMLVertSpace(0, 0, $cell, false, $lasttag);
  19207. break;
  19208. }
  19209. case 'dd': {
  19210. $this->lispacer = '';
  19211. if ($this->rtl) {
  19212. $this->rMargin -= $this->listindent;
  19213. } else {
  19214. $this->lMargin -= $this->listindent;
  19215. }
  19216. --$this->listindentlevel;
  19217. $this->addHTMLVertSpace(0, 0, $cell, false, $lasttag);
  19218. break;
  19219. }
  19220. case 'ul':
  19221. case 'ol': {
  19222. --$this->listnum;
  19223. $this->lispacer = '';
  19224. if ($this->rtl) {
  19225. $this->rMargin -= $this->listindent;
  19226. } else {
  19227. $this->lMargin -= $this->listindent;
  19228. }
  19229. --$this->listindentlevel;
  19230. if ($this->listnum <= 0) {
  19231. $this->listnum = 0;
  19232. $this->addHTMLVertSpace($hbz, $hb, $cell, false, $lasttag);
  19233. } else {
  19234. $this->addHTMLVertSpace(0, 0, $cell, false, $lasttag);
  19235. }
  19236. $this->resetLastH();
  19237. break;
  19238. }
  19239. case 'li': {
  19240. $this->lispacer = '';
  19241. $this->addHTMLVertSpace(0, 0, $cell, false, $lasttag);
  19242. break;
  19243. }
  19244. case 'h1':
  19245. case 'h2':
  19246. case 'h3':
  19247. case 'h4':
  19248. case 'h5':
  19249. case 'h6': {
  19250. $this->addHTMLVertSpace($hbz, $hb, $cell, false, $lasttag);
  19251. break;
  19252. }
  19253. // Form fields (since 4.8.000 - 2009-09-07)
  19254. case 'form': {
  19255. $this->form_action = '';
  19256. $this->form_enctype = 'application/x-www-form-urlencoded';
  19257. break;
  19258. }
  19259. default : {
  19260. break;
  19261. }
  19262. }
  19263. // draw border and background (if any)
  19264. $this->drawHTMLTagBorder($parent, $xmax);
  19265. if (isset($dom[($dom[$key]['parent'])]['attribute']['pagebreakafter'])) {
  19266. $pba = $dom[($dom[$key]['parent'])]['attribute']['pagebreakafter'];
  19267. // check for pagebreak
  19268. if (($pba == 'true') OR ($pba == 'left') OR ($pba == 'right')) {
  19269. // add a page (or trig AcceptPageBreak() for multicolumn mode)
  19270. $this->checkPageBreak($this->PageBreakTrigger + 1);
  19271. }
  19272. if ((($pba == 'left') AND (((!$this->rtl) AND (($this->page % 2) == 0)) OR (($this->rtl) AND (($this->page % 2) != 0))))
  19273. OR (($pba == 'right') AND (((!$this->rtl) AND (($this->page % 2) != 0)) OR (($this->rtl) AND (($this->page % 2) == 0))))) {
  19274. // add a page (or trig AcceptPageBreak() for multicolumn mode)
  19275. $this->checkPageBreak($this->PageBreakTrigger + 1);
  19276. }
  19277. }
  19278. $this->tmprtl = false;
  19279. return $dom;
  19280. }
  19281. /**
  19282. * Add vertical spaces if needed.
  19283. * @param $hbz (string) Distance between current y and line bottom.
  19284. * @param $hb (string) The height of the break.
  19285. * @param $cell (boolean) if true add the default left (or right if RTL) padding to each new line (default false).
  19286. * @param $firsttag (boolean) set to true when the tag is the first.
  19287. * @param $lasttag (boolean) set to true when the tag is the last.
  19288. * @protected
  19289. */
  19290. protected function addHTMLVertSpace($hbz=0, $hb=0, $cell=false, $firsttag=false, $lasttag=false) {
  19291. if ($firsttag) {
  19292. $this->Ln(0, $cell);
  19293. $this->htmlvspace = 0;
  19294. return;
  19295. }
  19296. if ($lasttag) {
  19297. $this->Ln($hbz, $cell);
  19298. $this->htmlvspace = 0;
  19299. return;
  19300. }
  19301. if ($hb < $this->htmlvspace) {
  19302. $hd = 0;
  19303. } else {
  19304. $hd = $hb - $this->htmlvspace;
  19305. $this->htmlvspace = $hb;
  19306. }
  19307. $this->Ln(($hbz + $hd), $cell);
  19308. }
  19309. /**
  19310. * Return the starting coordinates to draw an html border
  19311. * @return array containing top-left border coordinates
  19312. * @protected
  19313. * @since 5.7.000 (2010-08-03)
  19314. */
  19315. protected function getBorderStartPosition() {
  19316. if ($this->rtl) {
  19317. $xmax = $this->lMargin;
  19318. } else {
  19319. $xmax = $this->w - $this->rMargin;
  19320. }
  19321. return array('page' => $this->page, 'column' => $this->current_column, 'x' => $this->x, 'y' => $this->y, 'xmax' => $xmax);
  19322. }
  19323. /**
  19324. * Draw an HTML block border and fill
  19325. * @param $tag (array) array of tag properties.
  19326. * @param $xmax (int) end X coordinate for border.
  19327. * @protected
  19328. * @since 5.7.000 (2010-08-03)
  19329. */
  19330. protected function drawHTMLTagBorder($tag, $xmax) {
  19331. if (!isset($tag['borderposition'])) {
  19332. // nothing to draw
  19333. return;
  19334. }
  19335. $prev_x = $this->x;
  19336. $prev_y = $this->y;
  19337. $prev_lasth = $this->lasth;
  19338. $border = 0;
  19339. $fill = false;
  19340. $this->lasth = 0;
  19341. if (isset($tag['border']) AND !empty($tag['border'])) {
  19342. // get border style
  19343. $border = $tag['border'];
  19344. if (!TCPDF_STATIC::empty_string($this->thead) AND (!$this->inthead)) {
  19345. // border for table header
  19346. $border = TCPDF_STATIC::getBorderMode($border, $position='middle', $this->opencell);
  19347. }
  19348. }
  19349. if (isset($tag['bgcolor']) AND ($tag['bgcolor'] !== false)) {
  19350. // get background color
  19351. $old_bgcolor = $this->bgcolor;
  19352. $this->SetFillColorArray($tag['bgcolor']);
  19353. $fill = true;
  19354. }
  19355. if (!$border AND !$fill) {
  19356. // nothing to draw
  19357. return;
  19358. }
  19359. if (isset($tag['attribute']['cellspacing'])) {
  19360. $clsp = $this->getHTMLUnitToUnits($tag['attribute']['cellspacing'], 1, 'px');
  19361. $cellspacing = array('H' => $clsp, 'V' => $clsp);
  19362. } elseif (isset($tag['border-spacing'])) {
  19363. $cellspacing = $tag['border-spacing'];
  19364. } else {
  19365. $cellspacing = array('H' => 0, 'V' => 0);
  19366. }
  19367. if (($tag['value'] != 'table') AND (is_array($border)) AND (!empty($border))) {
  19368. // draw the border externally respect the sqare edge.
  19369. $border['mode'] = 'ext';
  19370. }
  19371. if ($this->rtl) {
  19372. if ($xmax >= $tag['borderposition']['x']) {
  19373. $xmax = $tag['borderposition']['xmax'];
  19374. }
  19375. $w = ($tag['borderposition']['x'] - $xmax);
  19376. } else {
  19377. if ($xmax <= $tag['borderposition']['x']) {
  19378. $xmax = $tag['borderposition']['xmax'];
  19379. }
  19380. $w = ($xmax - $tag['borderposition']['x']);
  19381. }
  19382. if ($w <= 0) {
  19383. return;
  19384. }
  19385. $w += $cellspacing['H'];
  19386. $startpage = $tag['borderposition']['page'];
  19387. $startcolumn = $tag['borderposition']['column'];
  19388. $x = $tag['borderposition']['x'];
  19389. $y = $tag['borderposition']['y'];
  19390. $endpage = $this->page;
  19391. $starty = $tag['borderposition']['y'] - $cellspacing['V'];
  19392. $currentY = $this->y;
  19393. $this->x = $x;
  19394. // get latest column
  19395. $endcolumn = $this->current_column;
  19396. if ($this->num_columns == 0) {
  19397. $this->num_columns = 1;
  19398. }
  19399. // get border modes
  19400. $border_start = TCPDF_STATIC::getBorderMode($border, $position='start', $this->opencell);
  19401. $border_end = TCPDF_STATIC::getBorderMode($border, $position='end', $this->opencell);
  19402. $border_middle = TCPDF_STATIC::getBorderMode($border, $position='middle', $this->opencell);
  19403. // temporary disable page regions
  19404. $temp_page_regions = $this->page_regions;
  19405. $this->page_regions = array();
  19406. // design borders around HTML cells.
  19407. for ($page = $startpage; $page <= $endpage; ++$page) { // for each page
  19408. $ccode = '';
  19409. $this->setPage($page);
  19410. if ($this->num_columns < 2) {
  19411. // single-column mode
  19412. $this->x = $x;
  19413. $this->y = $this->tMargin;
  19414. }
  19415. // account for margin changes
  19416. if ($page > $startpage) {
  19417. if (($this->rtl) AND ($this->pagedim[$page]['orm'] != $this->pagedim[$startpage]['orm'])) {
  19418. $this->x -= ($this->pagedim[$page]['orm'] - $this->pagedim[$startpage]['orm']);
  19419. } elseif ((!$this->rtl) AND ($this->pagedim[$page]['olm'] != $this->pagedim[$startpage]['olm'])) {
  19420. $this->x += ($this->pagedim[$page]['olm'] - $this->pagedim[$startpage]['olm']);
  19421. }
  19422. }
  19423. if ($startpage == $endpage) {
  19424. // single page
  19425. for ($column = $startcolumn; $column <= $endcolumn; ++$column) { // for each column
  19426. $this->selectColumn($column);
  19427. if ($startcolumn == $endcolumn) { // single column
  19428. $cborder = $border;
  19429. $h = ($currentY - $y) + $cellspacing['V'];
  19430. $this->y = $starty;
  19431. } elseif ($column == $startcolumn) { // first column
  19432. $cborder = $border_start;
  19433. $this->y = $starty;
  19434. $h = $this->h - $this->y - $this->bMargin;
  19435. } elseif ($column == $endcolumn) { // end column
  19436. $cborder = $border_end;
  19437. $h = $currentY - $this->y;
  19438. } else { // middle column
  19439. $cborder = $border_middle;
  19440. $h = $this->h - $this->y - $this->bMargin;
  19441. }
  19442. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
  19443. } // end for each column
  19444. } elseif ($page == $startpage) { // first page
  19445. for ($column = $startcolumn; $column < $this->num_columns; ++$column) { // for each column
  19446. $this->selectColumn($column);
  19447. if ($column == $startcolumn) { // first column
  19448. $cborder = $border_start;
  19449. $this->y = $starty;
  19450. $h = $this->h - $this->y - $this->bMargin;
  19451. } else { // middle column
  19452. $cborder = $border_middle;
  19453. $h = $this->h - $this->y - $this->bMargin;
  19454. }
  19455. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
  19456. } // end for each column
  19457. } elseif ($page == $endpage) { // last page
  19458. for ($column = 0; $column <= $endcolumn; ++$column) { // for each column
  19459. $this->selectColumn($column);
  19460. if ($column == $endcolumn) {
  19461. // end column
  19462. $cborder = $border_end;
  19463. $h = $currentY - $this->y;
  19464. } else {
  19465. // middle column
  19466. $cborder = $border_middle;
  19467. $h = $this->h - $this->y - $this->bMargin;
  19468. }
  19469. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
  19470. } // end for each column
  19471. } else { // middle page
  19472. for ($column = 0; $column < $this->num_columns; ++$column) { // for each column
  19473. $this->selectColumn($column);
  19474. $cborder = $border_middle;
  19475. $h = $this->h - $this->y - $this->bMargin;
  19476. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
  19477. } // end for each column
  19478. }
  19479. if ($cborder OR $fill) {
  19480. $offsetlen = strlen($ccode);
  19481. // draw border and fill
  19482. if ($this->inxobj) {
  19483. // we are inside an XObject template
  19484. if (end($this->xobjects[$this->xobjid]['transfmrk']) !== false) {
  19485. $pagemarkkey = key($this->xobjects[$this->xobjid]['transfmrk']);
  19486. $pagemark = $this->xobjects[$this->xobjid]['transfmrk'][$pagemarkkey];
  19487. $this->xobjects[$this->xobjid]['transfmrk'][$pagemarkkey] += $offsetlen;
  19488. } else {
  19489. $pagemark = $this->xobjects[$this->xobjid]['intmrk'];
  19490. $this->xobjects[$this->xobjid]['intmrk'] += $offsetlen;
  19491. }
  19492. $pagebuff = $this->xobjects[$this->xobjid]['outdata'];
  19493. $pstart = substr($pagebuff, 0, $pagemark);
  19494. $pend = substr($pagebuff, $pagemark);
  19495. $this->xobjects[$this->xobjid]['outdata'] = $pstart.$ccode.$pend;
  19496. } else {
  19497. if (end($this->transfmrk[$this->page]) !== false) {
  19498. $pagemarkkey = key($this->transfmrk[$this->page]);
  19499. $pagemark = $this->transfmrk[$this->page][$pagemarkkey];
  19500. } elseif ($this->InFooter) {
  19501. $pagemark = $this->footerpos[$this->page];
  19502. } else {
  19503. $pagemark = $this->intmrk[$this->page];
  19504. }
  19505. $pagebuff = $this->getPageBuffer($this->page);
  19506. $pstart = substr($pagebuff, 0, $pagemark);
  19507. $pend = substr($pagebuff, $pagemark);
  19508. $this->setPageBuffer($this->page, $pstart.$ccode.$pend);
  19509. $this->bordermrk[$this->page] += $offsetlen;
  19510. $this->cntmrk[$this->page] += $offsetlen;
  19511. }
  19512. }
  19513. } // end for each page
  19514. // restore page regions
  19515. $this->page_regions = $temp_page_regions;
  19516. if (isset($old_bgcolor)) {
  19517. // restore background color
  19518. $this->SetFillColorArray($old_bgcolor);
  19519. }
  19520. // restore pointer position
  19521. $this->x = $prev_x;
  19522. $this->y = $prev_y;
  19523. $this->lasth = $prev_lasth;
  19524. }
  19525. /**
  19526. * Set the default bullet to be used as LI bullet symbol
  19527. * @param $symbol (string) 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')
  19528. * @public
  19529. * @since 4.0.028 (2008-09-26)
  19530. */
  19531. public function setLIsymbol($symbol='!') {
  19532. // check for custom image symbol
  19533. if (substr($symbol, 0, 4) == 'img|') {
  19534. $this->lisymbol = $symbol;
  19535. return;
  19536. }
  19537. $symbol = strtolower($symbol);
  19538. $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');
  19539. if (in_array($symbol, $valid_symbols)) {
  19540. $this->lisymbol = $symbol;
  19541. } else {
  19542. $this->lisymbol = '';
  19543. }
  19544. }
  19545. /**
  19546. * Set the booklet mode for double-sided pages.
  19547. * @param $booklet (boolean) true set the booklet mode on, false otherwise.
  19548. * @param $inner (float) Inner page margin.
  19549. * @param $outer (float) Outer page margin.
  19550. * @public
  19551. * @since 4.2.000 (2008-10-29)
  19552. */
  19553. public function SetBooklet($booklet=true, $inner=-1, $outer=-1) {
  19554. $this->booklet = $booklet;
  19555. if ($inner >= 0) {
  19556. $this->lMargin = $inner;
  19557. }
  19558. if ($outer >= 0) {
  19559. $this->rMargin = $outer;
  19560. }
  19561. }
  19562. /**
  19563. * Swap the left and right margins.
  19564. * @param $reverse (boolean) if true swap left and right margins.
  19565. * @protected
  19566. * @since 4.2.000 (2008-10-29)
  19567. */
  19568. protected function swapMargins($reverse=true) {
  19569. if ($reverse) {
  19570. // swap left and right margins
  19571. $mtemp = $this->original_lMargin;
  19572. $this->original_lMargin = $this->original_rMargin;
  19573. $this->original_rMargin = $mtemp;
  19574. $deltam = $this->original_lMargin - $this->original_rMargin;
  19575. $this->lMargin += $deltam;
  19576. $this->rMargin -= $deltam;
  19577. }
  19578. }
  19579. /**
  19580. * Set the vertical spaces for HTML tags.
  19581. * The array must have the following structure (example):
  19582. * $tagvs = array('h1' => array(0 => array('h' => '', 'n' => 2), 1 => array('h' => 1.3, 'n' => 1)));
  19583. * The first array level contains the tag names,
  19584. * the second level contains 0 for opening tags or 1 for closing tags,
  19585. * the third level contains the vertical space unit (h) and the number spaces to add (n).
  19586. * If the h parameter is not specified, default values are used.
  19587. * @param $tagvs (array) array of tags and relative vertical spaces.
  19588. * @public
  19589. * @since 4.2.001 (2008-10-30)
  19590. */
  19591. public function setHtmlVSpace($tagvs) {
  19592. $this->tagvspaces = $tagvs;
  19593. }
  19594. /**
  19595. * Set custom width for list indentation.
  19596. * @param $width (float) width of the indentation. Use negative value to disable it.
  19597. * @public
  19598. * @since 4.2.007 (2008-11-12)
  19599. */
  19600. public function setListIndentWidth($width) {
  19601. return $this->customlistindent = floatval($width);
  19602. }
  19603. /**
  19604. * Set the top/bottom cell sides to be open or closed when the cell cross the page.
  19605. * @param $isopen (boolean) if true keeps the top/bottom border open for the cell sides that cross the page.
  19606. * @public
  19607. * @since 4.2.010 (2008-11-14)
  19608. */
  19609. public function setOpenCell($isopen) {
  19610. $this->opencell = $isopen;
  19611. }
  19612. /**
  19613. * Set the color and font style for HTML links.
  19614. * @param $color (array) RGB array of colors
  19615. * @param $fontstyle (string) additional font styles to add
  19616. * @public
  19617. * @since 4.4.003 (2008-12-09)
  19618. */
  19619. public function setHtmlLinksStyle($color=array(0,0,255), $fontstyle='U') {
  19620. $this->htmlLinkColorArray = $color;
  19621. $this->htmlLinkFontStyle = $fontstyle;
  19622. }
  19623. /**
  19624. * Convert HTML string containing value and unit of measure to user's units or points.
  19625. * @param $htmlval (string) String containing values and unit.
  19626. * @param $refsize (string) Reference value in points.
  19627. * @param $defaultunit (string) Default unit (can be one of the following: %, em, ex, px, in, mm, pc, pt).
  19628. * @param $points (boolean) If true returns points, otherwise returns value in user's units.
  19629. * @return float value in user's unit or point if $points=true
  19630. * @public
  19631. * @since 4.4.004 (2008-12-10)
  19632. */
  19633. public function getHTMLUnitToUnits($htmlval, $refsize=1, $defaultunit='px', $points=false) {
  19634. $supportedunits = array('%', 'em', 'ex', 'px', 'in', 'cm', 'mm', 'pc', 'pt');
  19635. $retval = 0;
  19636. $value = 0;
  19637. $unit = 'px';
  19638. if ($points) {
  19639. $k = 1;
  19640. } else {
  19641. $k = $this->k;
  19642. }
  19643. if (in_array($defaultunit, $supportedunits)) {
  19644. $unit = $defaultunit;
  19645. }
  19646. if (is_numeric($htmlval)) {
  19647. $value = floatval($htmlval);
  19648. } elseif (preg_match('/([0-9\.\-\+]+)/', $htmlval, $mnum)) {
  19649. $value = floatval($mnum[1]);
  19650. if (preg_match('/([a-z%]+)/', $htmlval, $munit)) {
  19651. if (in_array($munit[1], $supportedunits)) {
  19652. $unit = $munit[1];
  19653. }
  19654. }
  19655. }
  19656. switch ($unit) {
  19657. // percentage
  19658. case '%': {
  19659. $retval = (($value * $refsize) / 100);
  19660. break;
  19661. }
  19662. // relative-size
  19663. case 'em': {
  19664. $retval = ($value * $refsize);
  19665. break;
  19666. }
  19667. // height of lower case 'x' (about half the font-size)
  19668. case 'ex': {
  19669. $retval = ($value * ($refsize / 2));
  19670. break;
  19671. }
  19672. // absolute-size
  19673. case 'in': {
  19674. $retval = (($value * $this->dpi) / $k);
  19675. break;
  19676. }
  19677. // centimeters
  19678. case 'cm': {
  19679. $retval = (($value / 2.54 * $this->dpi) / $k);
  19680. break;
  19681. }
  19682. // millimeters
  19683. case 'mm': {
  19684. $retval = (($value / 25.4 * $this->dpi) / $k);
  19685. break;
  19686. }
  19687. // one pica is 12 points
  19688. case 'pc': {
  19689. $retval = (($value * 12) / $k);
  19690. break;
  19691. }
  19692. // points
  19693. case 'pt': {
  19694. $retval = ($value / $k);
  19695. break;
  19696. }
  19697. // pixels
  19698. case 'px': {
  19699. $retval = $this->pixelsToUnits($value);
  19700. if ($points) {
  19701. $retval *= $this->k;
  19702. }
  19703. break;
  19704. }
  19705. }
  19706. return $retval;
  19707. }
  19708. /**
  19709. * Output an HTML list bullet or ordered item symbol
  19710. * @param $listdepth (int) list nesting level
  19711. * @param $listtype (string) type of list
  19712. * @param $size (float) current font size
  19713. * @protected
  19714. * @since 4.4.004 (2008-12-10)
  19715. */
  19716. protected function putHtmlListBullet($listdepth, $listtype='', $size=10) {
  19717. if ($this->state != 2) {
  19718. return;
  19719. }
  19720. $size /= $this->k;
  19721. $fill = '';
  19722. $bgcolor = $this->bgcolor;
  19723. $color = $this->fgcolor;
  19724. $strokecolor = $this->strokecolor;
  19725. $width = 0;
  19726. $textitem = '';
  19727. $tmpx = $this->x;
  19728. $lspace = $this->GetStringWidth(' ');
  19729. if ($listtype == '^') {
  19730. // special symbol used for avoid justification of rect bullet
  19731. $this->lispacer = '';
  19732. return;
  19733. } elseif ($listtype == '!') {
  19734. // set default list type for unordered list
  19735. $deftypes = array('disc', 'circle', 'square');
  19736. $listtype = $deftypes[($listdepth - 1) % 3];
  19737. } elseif ($listtype == '#') {
  19738. // set default list type for ordered list
  19739. $listtype = 'decimal';
  19740. } elseif (substr($listtype, 0, 4) == 'img|') {
  19741. // custom image type ('img|type|width|height|image.ext')
  19742. $img = explode('|', $listtype);
  19743. $listtype = 'img';
  19744. }
  19745. switch ($listtype) {
  19746. // unordered types
  19747. case 'none': {
  19748. break;
  19749. }
  19750. case 'disc': {
  19751. $r = $size / 6;
  19752. $lspace += (2 * $r);
  19753. if ($this->rtl) {
  19754. $this->x += $lspace;
  19755. } else {
  19756. $this->x -= $lspace;
  19757. }
  19758. $this->Circle(($this->x + $r), ($this->y + ($this->lasth / 2)), $r, 0, 360, 'F', array(), $color, 8);
  19759. break;
  19760. }
  19761. case 'circle': {
  19762. $r = $size / 6;
  19763. $lspace += (2 * $r);
  19764. if ($this->rtl) {
  19765. $this->x += $lspace;
  19766. } else {
  19767. $this->x -= $lspace;
  19768. }
  19769. $prev_line_style = $this->linestyleWidth.' '.$this->linestyleCap.' '.$this->linestyleJoin.' '.$this->linestyleDash.' '.$this->DrawColor;
  19770. $new_line_style = array('width' => ($r / 3), 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'phase' => 0, 'color'=>$color);
  19771. $this->Circle(($this->x + $r), ($this->y + ($this->lasth / 2)), ($r * (1 - (1/6))), 0, 360, 'D', $new_line_style, array(), 8);
  19772. $this->_out($prev_line_style); // restore line settings
  19773. break;
  19774. }
  19775. case 'square': {
  19776. $l = $size / 3;
  19777. $lspace += $l;
  19778. if ($this->rtl) {;
  19779. $this->x += $lspace;
  19780. } else {
  19781. $this->x -= $lspace;
  19782. }
  19783. $this->Rect($this->x, ($this->y + (($this->lasth - $l) / 2)), $l, $l, 'F', array(), $color);
  19784. break;
  19785. }
  19786. case 'img': {
  19787. // 1=>type, 2=>width, 3=>height, 4=>image.ext
  19788. $lspace += $img[2];
  19789. if ($this->rtl) {;
  19790. $this->x += $lspace;
  19791. } else {
  19792. $this->x -= $lspace;
  19793. }
  19794. $imgtype = strtolower($img[1]);
  19795. $prev_y = $this->y;
  19796. switch ($imgtype) {
  19797. case 'svg': {
  19798. $this->ImageSVG($img[4], $this->x, ($this->y + (($this->lasth - $img[3]) / 2)), $img[2], $img[3], '', 'T', '', 0, false);
  19799. break;
  19800. }
  19801. case 'ai':
  19802. case 'eps': {
  19803. $this->ImageEps($img[4], $this->x, ($this->y + (($this->lasth - $img[3]) / 2)), $img[2], $img[3], '', true, 'T', '', 0, false);
  19804. break;
  19805. }
  19806. default: {
  19807. $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);
  19808. break;
  19809. }
  19810. }
  19811. $this->y = $prev_y;
  19812. break;
  19813. }
  19814. // ordered types
  19815. // $this->listcount[$this->listnum];
  19816. // $textitem
  19817. case '1':
  19818. case 'decimal': {
  19819. $textitem = $this->listcount[$this->listnum];
  19820. break;
  19821. }
  19822. case 'decimal-leading-zero': {
  19823. $textitem = sprintf('%02d', $this->listcount[$this->listnum]);
  19824. break;
  19825. }
  19826. case 'i':
  19827. case 'lower-roman': {
  19828. $textitem = strtolower(TCPDF_STATIC::intToRoman($this->listcount[$this->listnum]));
  19829. break;
  19830. }
  19831. case 'I':
  19832. case 'upper-roman': {
  19833. $textitem = TCPDF_STATIC::intToRoman($this->listcount[$this->listnum]);
  19834. break;
  19835. }
  19836. case 'a':
  19837. case 'lower-alpha':
  19838. case 'lower-latin': {
  19839. $textitem = chr(97 + $this->listcount[$this->listnum] - 1);
  19840. break;
  19841. }
  19842. case 'A':
  19843. case 'upper-alpha':
  19844. case 'upper-latin': {
  19845. $textitem = chr(65 + $this->listcount[$this->listnum] - 1);
  19846. break;
  19847. }
  19848. case 'lower-greek': {
  19849. $textitem = TCPDF_FONTS::unichr((945 + $this->listcount[$this->listnum] - 1), $this->isunicode);
  19850. break;
  19851. }
  19852. /*
  19853. // Types to be implemented (special handling)
  19854. case 'hebrew': {
  19855. break;
  19856. }
  19857. case 'armenian': {
  19858. break;
  19859. }
  19860. case 'georgian': {
  19861. break;
  19862. }
  19863. case 'cjk-ideographic': {
  19864. break;
  19865. }
  19866. case 'hiragana': {
  19867. break;
  19868. }
  19869. case 'katakana': {
  19870. break;
  19871. }
  19872. case 'hiragana-iroha': {
  19873. break;
  19874. }
  19875. case 'katakana-iroha': {
  19876. break;
  19877. }
  19878. */
  19879. default: {
  19880. $textitem = $this->listcount[$this->listnum];
  19881. }
  19882. }
  19883. if (!TCPDF_STATIC::empty_string($textitem)) {
  19884. // Check whether we need a new page or new column
  19885. $prev_y = $this->y;
  19886. $h = $this->getCellHeight($this->FontSize);
  19887. if ($this->checkPageBreak($h) OR ($this->y < $prev_y)) {
  19888. $tmpx = $this->x;
  19889. }
  19890. // print ordered item
  19891. if ($this->rtl) {
  19892. $textitem = '.'.$textitem;
  19893. } else {
  19894. $textitem = $textitem.'.';
  19895. }
  19896. $lspace += $this->GetStringWidth($textitem);
  19897. if ($this->rtl) {
  19898. $this->x += $lspace;
  19899. } else {
  19900. $this->x -= $lspace;
  19901. }
  19902. $this->Write($this->lasth, $textitem, '', false, '', false, 0, false);
  19903. }
  19904. $this->x = $tmpx;
  19905. $this->lispacer = '^';
  19906. // restore colors
  19907. $this->SetFillColorArray($bgcolor);
  19908. $this->SetDrawColorArray($strokecolor);
  19909. $this->SettextColorArray($color);
  19910. }
  19911. /**
  19912. * Returns current graphic variables as array.
  19913. * @return array of graphic variables
  19914. * @protected
  19915. * @since 4.2.010 (2008-11-14)
  19916. */
  19917. protected function getGraphicVars() {
  19918. $grapvars = array(
  19919. 'FontFamily' => $this->FontFamily,
  19920. 'FontStyle' => $this->FontStyle,
  19921. 'FontSizePt' => $this->FontSizePt,
  19922. 'rMargin' => $this->rMargin,
  19923. 'lMargin' => $this->lMargin,
  19924. 'cell_padding' => $this->cell_padding,
  19925. 'cell_margin' => $this->cell_margin,
  19926. 'LineWidth' => $this->LineWidth,
  19927. 'linestyleWidth' => $this->linestyleWidth,
  19928. 'linestyleCap' => $this->linestyleCap,
  19929. 'linestyleJoin' => $this->linestyleJoin,
  19930. 'linestyleDash' => $this->linestyleDash,
  19931. 'textrendermode' => $this->textrendermode,
  19932. 'textstrokewidth' => $this->textstrokewidth,
  19933. 'DrawColor' => $this->DrawColor,
  19934. 'FillColor' => $this->FillColor,
  19935. 'TextColor' => $this->TextColor,
  19936. 'ColorFlag' => $this->ColorFlag,
  19937. 'bgcolor' => $this->bgcolor,
  19938. 'fgcolor' => $this->fgcolor,
  19939. 'htmlvspace' => $this->htmlvspace,
  19940. 'listindent' => $this->listindent,
  19941. 'listindentlevel' => $this->listindentlevel,
  19942. 'listnum' => $this->listnum,
  19943. 'listordered' => $this->listordered,
  19944. 'listcount' => $this->listcount,
  19945. 'lispacer' => $this->lispacer,
  19946. 'cell_height_ratio' => $this->cell_height_ratio,
  19947. 'font_stretching' => $this->font_stretching,
  19948. 'font_spacing' => $this->font_spacing,
  19949. 'alpha' => $this->alpha,
  19950. // extended
  19951. 'lasth' => $this->lasth,
  19952. 'tMargin' => $this->tMargin,
  19953. 'bMargin' => $this->bMargin,
  19954. 'AutoPageBreak' => $this->AutoPageBreak,
  19955. 'PageBreakTrigger' => $this->PageBreakTrigger,
  19956. 'x' => $this->x,
  19957. 'y' => $this->y,
  19958. 'w' => $this->w,
  19959. 'h' => $this->h,
  19960. 'wPt' => $this->wPt,
  19961. 'hPt' => $this->hPt,
  19962. 'fwPt' => $this->fwPt,
  19963. 'fhPt' => $this->fhPt,
  19964. 'page' => $this->page,
  19965. 'current_column' => $this->current_column,
  19966. 'num_columns' => $this->num_columns
  19967. );
  19968. return $grapvars;
  19969. }
  19970. /**
  19971. * Set graphic variables.
  19972. * @param $gvars (array) array of graphic variablesto restore
  19973. * @param $extended (boolean) if true restore extended graphic variables
  19974. * @protected
  19975. * @since 4.2.010 (2008-11-14)
  19976. */
  19977. protected function setGraphicVars($gvars, $extended=false) {
  19978. if ($this->state != 2) {
  19979. return;
  19980. }
  19981. $this->FontFamily = $gvars['FontFamily'];
  19982. $this->FontStyle = $gvars['FontStyle'];
  19983. $this->FontSizePt = $gvars['FontSizePt'];
  19984. $this->rMargin = $gvars['rMargin'];
  19985. $this->lMargin = $gvars['lMargin'];
  19986. $this->cell_padding = $gvars['cell_padding'];
  19987. $this->cell_margin = $gvars['cell_margin'];
  19988. $this->LineWidth = $gvars['LineWidth'];
  19989. $this->linestyleWidth = $gvars['linestyleWidth'];
  19990. $this->linestyleCap = $gvars['linestyleCap'];
  19991. $this->linestyleJoin = $gvars['linestyleJoin'];
  19992. $this->linestyleDash = $gvars['linestyleDash'];
  19993. $this->textrendermode = $gvars['textrendermode'];
  19994. $this->textstrokewidth = $gvars['textstrokewidth'];
  19995. $this->DrawColor = $gvars['DrawColor'];
  19996. $this->FillColor = $gvars['FillColor'];
  19997. $this->TextColor = $gvars['TextColor'];
  19998. $this->ColorFlag = $gvars['ColorFlag'];
  19999. $this->bgcolor = $gvars['bgcolor'];
  20000. $this->fgcolor = $gvars['fgcolor'];
  20001. $this->htmlvspace = $gvars['htmlvspace'];
  20002. $this->listindent = $gvars['listindent'];
  20003. $this->listindentlevel = $gvars['listindentlevel'];
  20004. $this->listnum = $gvars['listnum'];
  20005. $this->listordered = $gvars['listordered'];
  20006. $this->listcount = $gvars['listcount'];
  20007. $this->lispacer = $gvars['lispacer'];
  20008. $this->cell_height_ratio = $gvars['cell_height_ratio'];
  20009. $this->font_stretching = $gvars['font_stretching'];
  20010. $this->font_spacing = $gvars['font_spacing'];
  20011. $this->alpha = $gvars['alpha'];
  20012. if ($extended) {
  20013. // restore extended values
  20014. $this->lasth = $gvars['lasth'];
  20015. $this->tMargin = $gvars['tMargin'];
  20016. $this->bMargin = $gvars['bMargin'];
  20017. $this->AutoPageBreak = $gvars['AutoPageBreak'];
  20018. $this->PageBreakTrigger = $gvars['PageBreakTrigger'];
  20019. $this->x = $gvars['x'];
  20020. $this->y = $gvars['y'];
  20021. $this->w = $gvars['w'];
  20022. $this->h = $gvars['h'];
  20023. $this->wPt = $gvars['wPt'];
  20024. $this->hPt = $gvars['hPt'];
  20025. $this->fwPt = $gvars['fwPt'];
  20026. $this->fhPt = $gvars['fhPt'];
  20027. $this->page = $gvars['page'];
  20028. $this->current_column = $gvars['current_column'];
  20029. $this->num_columns = $gvars['num_columns'];
  20030. }
  20031. $this->_out(''.$this->linestyleWidth.' '.$this->linestyleCap.' '.$this->linestyleJoin.' '.$this->linestyleDash.' '.$this->DrawColor.' '.$this->FillColor.'');
  20032. if (!TCPDF_STATIC::empty_string($this->FontFamily)) {
  20033. $this->SetFont($this->FontFamily, $this->FontStyle, $this->FontSizePt);
  20034. }
  20035. }
  20036. /**
  20037. * Outputs the "save graphics state" operator 'q'
  20038. * @protected
  20039. */
  20040. protected function _outSaveGraphicsState() {
  20041. $this->_out('q');
  20042. }
  20043. /**
  20044. * Outputs the "restore graphics state" operator 'Q'
  20045. * @protected
  20046. */
  20047. protected function _outRestoreGraphicsState() {
  20048. $this->_out('Q');
  20049. }
  20050. /**
  20051. * Set buffer content (always append data).
  20052. * @param $data (string) data
  20053. * @protected
  20054. * @since 4.5.000 (2009-01-02)
  20055. */
  20056. protected function setBuffer($data) {
  20057. $this->bufferlen += strlen($data);
  20058. $this->buffer .= $data;
  20059. }
  20060. /**
  20061. * Replace the buffer content
  20062. * @param $data (string) data
  20063. * @protected
  20064. * @since 5.5.000 (2010-06-22)
  20065. */
  20066. protected function replaceBuffer($data) {
  20067. $this->bufferlen = strlen($data);
  20068. $this->buffer = $data;
  20069. }
  20070. /**
  20071. * Get buffer content.
  20072. * @return string buffer content
  20073. * @protected
  20074. * @since 4.5.000 (2009-01-02)
  20075. */
  20076. protected function getBuffer() {
  20077. return $this->buffer;
  20078. }
  20079. /**
  20080. * Set page buffer content.
  20081. * @param $page (int) page number
  20082. * @param $data (string) page data
  20083. * @param $append (boolean) if true append data, false replace.
  20084. * @protected
  20085. * @since 4.5.000 (2008-12-31)
  20086. */
  20087. protected function setPageBuffer($page, $data, $append=false) {
  20088. if ($append) {
  20089. $this->pages[$page] .= $data;
  20090. } else {
  20091. $this->pages[$page] = $data;
  20092. }
  20093. if ($append AND isset($this->pagelen[$page])) {
  20094. $this->pagelen[$page] += strlen($data);
  20095. } else {
  20096. $this->pagelen[$page] = strlen($data);
  20097. }
  20098. }
  20099. /**
  20100. * Get page buffer content.
  20101. * @param $page (int) page number
  20102. * @return string page buffer content or false in case of error
  20103. * @protected
  20104. * @since 4.5.000 (2008-12-31)
  20105. */
  20106. protected function getPageBuffer($page) {
  20107. if (isset($this->pages[$page])) {
  20108. return $this->pages[$page];
  20109. }
  20110. return false;
  20111. }
  20112. /**
  20113. * Set image buffer content.
  20114. * @param $image (string) image key
  20115. * @param $data (array) image data
  20116. * @return int image index number
  20117. * @protected
  20118. * @since 4.5.000 (2008-12-31)
  20119. */
  20120. protected function setImageBuffer($image, $data) {
  20121. if (($data['i'] = array_search($image, $this->imagekeys)) === FALSE) {
  20122. $this->imagekeys[$this->numimages] = $image;
  20123. $data['i'] = $this->numimages;
  20124. ++$this->numimages;
  20125. }
  20126. $this->images[$image] = $data;
  20127. return $data['i'];
  20128. }
  20129. /**
  20130. * Set image buffer content for a specified sub-key.
  20131. * @param $image (string) image key
  20132. * @param $key (string) image sub-key
  20133. * @param $data (array) image data
  20134. * @protected
  20135. * @since 4.5.000 (2008-12-31)
  20136. */
  20137. protected function setImageSubBuffer($image, $key, $data) {
  20138. if (!isset($this->images[$image])) {
  20139. $this->setImageBuffer($image, array());
  20140. }
  20141. $this->images[$image][$key] = $data;
  20142. }
  20143. /**
  20144. * Get image buffer content.
  20145. * @param $image (string) image key
  20146. * @return string image buffer content or false in case of error
  20147. * @protected
  20148. * @since 4.5.000 (2008-12-31)
  20149. */
  20150. protected function getImageBuffer($image) {
  20151. if (isset($this->images[$image])) {
  20152. return $this->images[$image];
  20153. }
  20154. return false;
  20155. }
  20156. /**
  20157. * Set font buffer content.
  20158. * @param $font (string) font key
  20159. * @param $data (array) font data
  20160. * @protected
  20161. * @since 4.5.000 (2009-01-02)
  20162. */
  20163. protected function setFontBuffer($font, $data) {
  20164. $this->fonts[$font] = $data;
  20165. if (!in_array($font, $this->fontkeys)) {
  20166. $this->fontkeys[] = $font;
  20167. // store object ID for current font
  20168. ++$this->n;
  20169. $this->font_obj_ids[$font] = $this->n;
  20170. $this->setFontSubBuffer($font, 'n', $this->n);
  20171. }
  20172. }
  20173. /**
  20174. * Set font buffer content.
  20175. * @param $font (string) font key
  20176. * @param $key (string) font sub-key
  20177. * @param $data (array) font data
  20178. * @protected
  20179. * @since 4.5.000 (2009-01-02)
  20180. */
  20181. protected function setFontSubBuffer($font, $key, $data) {
  20182. if (!isset($this->fonts[$font])) {
  20183. $this->setFontBuffer($font, array());
  20184. }
  20185. $this->fonts[$font][$key] = $data;
  20186. }
  20187. /**
  20188. * Get font buffer content.
  20189. * @param $font (string) font key
  20190. * @return string font buffer content or false in case of error
  20191. * @protected
  20192. * @since 4.5.000 (2009-01-02)
  20193. */
  20194. protected function getFontBuffer($font) {
  20195. if (isset($this->fonts[$font])) {
  20196. return $this->fonts[$font];
  20197. }
  20198. return false;
  20199. }
  20200. /**
  20201. * Move a page to a previous position.
  20202. * @param $frompage (int) number of the source page
  20203. * @param $topage (int) number of the destination page (must be less than $frompage)
  20204. * @return true in case of success, false in case of error.
  20205. * @public
  20206. * @since 4.5.000 (2009-01-02)
  20207. */
  20208. public function movePage($frompage, $topage) {
  20209. if (($frompage > $this->numpages) OR ($frompage <= $topage)) {
  20210. return false;
  20211. }
  20212. if ($frompage == $this->page) {
  20213. // close the page before moving it
  20214. $this->endPage();
  20215. }
  20216. // move all page-related states
  20217. $tmppage = $this->getPageBuffer($frompage);
  20218. $tmppagedim = $this->pagedim[$frompage];
  20219. $tmppagelen = $this->pagelen[$frompage];
  20220. $tmpintmrk = $this->intmrk[$frompage];
  20221. $tmpbordermrk = $this->bordermrk[$frompage];
  20222. $tmpcntmrk = $this->cntmrk[$frompage];
  20223. $tmppageobjects = $this->pageobjects[$frompage];
  20224. if (isset($this->footerpos[$frompage])) {
  20225. $tmpfooterpos = $this->footerpos[$frompage];
  20226. }
  20227. if (isset($this->footerlen[$frompage])) {
  20228. $tmpfooterlen = $this->footerlen[$frompage];
  20229. }
  20230. if (isset($this->transfmrk[$frompage])) {
  20231. $tmptransfmrk = $this->transfmrk[$frompage];
  20232. }
  20233. if (isset($this->PageAnnots[$frompage])) {
  20234. $tmpannots = $this->PageAnnots[$frompage];
  20235. }
  20236. if (isset($this->newpagegroup) AND !empty($this->newpagegroup)) {
  20237. for ($i = $frompage; $i > $topage; --$i) {
  20238. if (isset($this->newpagegroup[$i]) AND (($i + $this->pagegroups[$this->newpagegroup[$i]]) > $frompage)) {
  20239. --$this->pagegroups[$this->newpagegroup[$i]];
  20240. break;
  20241. }
  20242. }
  20243. for ($i = $topage; $i > 0; --$i) {
  20244. if (isset($this->newpagegroup[$i]) AND (($i + $this->pagegroups[$this->newpagegroup[$i]]) > $topage)) {
  20245. ++$this->pagegroups[$this->newpagegroup[$i]];
  20246. break;
  20247. }
  20248. }
  20249. }
  20250. for ($i = $frompage; $i > $topage; --$i) {
  20251. $j = $i - 1;
  20252. // shift pages down
  20253. $this->setPageBuffer($i, $this->getPageBuffer($j));
  20254. $this->pagedim[$i] = $this->pagedim[$j];
  20255. $this->pagelen[$i] = $this->pagelen[$j];
  20256. $this->intmrk[$i] = $this->intmrk[$j];
  20257. $this->bordermrk[$i] = $this->bordermrk[$j];
  20258. $this->cntmrk[$i] = $this->cntmrk[$j];
  20259. $this->pageobjects[$i] = $this->pageobjects[$j];
  20260. if (isset($this->footerpos[$j])) {
  20261. $this->footerpos[$i] = $this->footerpos[$j];
  20262. } elseif (isset($this->footerpos[$i])) {
  20263. unset($this->footerpos[$i]);
  20264. }
  20265. if (isset($this->footerlen[$j])) {
  20266. $this->footerlen[$i] = $this->footerlen[$j];
  20267. } elseif (isset($this->footerlen[$i])) {
  20268. unset($this->footerlen[$i]);
  20269. }
  20270. if (isset($this->transfmrk[$j])) {
  20271. $this->transfmrk[$i] = $this->transfmrk[$j];
  20272. } elseif (isset($this->transfmrk[$i])) {
  20273. unset($this->transfmrk[$i]);
  20274. }
  20275. if (isset($this->PageAnnots[$j])) {
  20276. $this->PageAnnots[$i] = $this->PageAnnots[$j];
  20277. } elseif (isset($this->PageAnnots[$i])) {
  20278. unset($this->PageAnnots[$i]);
  20279. }
  20280. if (isset($this->newpagegroup[$j])) {
  20281. $this->newpagegroup[$i] = $this->newpagegroup[$j];
  20282. unset($this->newpagegroup[$j]);
  20283. }
  20284. if ($this->currpagegroup == $j) {
  20285. $this->currpagegroup = $i;
  20286. }
  20287. }
  20288. $this->setPageBuffer($topage, $tmppage);
  20289. $this->pagedim[$topage] = $tmppagedim;
  20290. $this->pagelen[$topage] = $tmppagelen;
  20291. $this->intmrk[$topage] = $tmpintmrk;
  20292. $this->bordermrk[$topage] = $tmpbordermrk;
  20293. $this->cntmrk[$topage] = $tmpcntmrk;
  20294. $this->pageobjects[$topage] = $tmppageobjects;
  20295. if (isset($tmpfooterpos)) {
  20296. $this->footerpos[$topage] = $tmpfooterpos;
  20297. } elseif (isset($this->footerpos[$topage])) {
  20298. unset($this->footerpos[$topage]);
  20299. }
  20300. if (isset($tmpfooterlen)) {
  20301. $this->footerlen[$topage] = $tmpfooterlen;
  20302. } elseif (isset($this->footerlen[$topage])) {
  20303. unset($this->footerlen[$topage]);
  20304. }
  20305. if (isset($tmptransfmrk)) {
  20306. $this->transfmrk[$topage] = $tmptransfmrk;
  20307. } elseif (isset($this->transfmrk[$topage])) {
  20308. unset($this->transfmrk[$topage]);
  20309. }
  20310. if (isset($tmpannots)) {
  20311. $this->PageAnnots[$topage] = $tmpannots;
  20312. } elseif (isset($this->PageAnnots[$topage])) {
  20313. unset($this->PageAnnots[$topage]);
  20314. }
  20315. // adjust outlines
  20316. $tmpoutlines = $this->outlines;
  20317. foreach ($tmpoutlines as $key => $outline) {
  20318. if (!$outline['f']) {
  20319. if (($outline['p'] >= $topage) AND ($outline['p'] < $frompage)) {
  20320. $this->outlines[$key]['p'] = ($outline['p'] + 1);
  20321. } elseif ($outline['p'] == $frompage) {
  20322. $this->outlines[$key]['p'] = $topage;
  20323. }
  20324. }
  20325. }
  20326. // adjust dests
  20327. $tmpdests = $this->dests;
  20328. foreach ($tmpdests as $key => $dest) {
  20329. if (!$dest['f']) {
  20330. if (($dest['p'] >= $topage) AND ($dest['p'] < $frompage)) {
  20331. $this->dests[$key]['p'] = ($dest['p'] + 1);
  20332. } elseif ($dest['p'] == $frompage) {
  20333. $this->dests[$key]['p'] = $topage;
  20334. }
  20335. }
  20336. }
  20337. // adjust links
  20338. $tmplinks = $this->links;
  20339. foreach ($tmplinks as $key => $link) {
  20340. if (!$link['f']) {
  20341. if (($link['p'] >= $topage) AND ($link['p'] < $frompage)) {
  20342. $this->links[$key]['p'] = ($link['p'] + 1);
  20343. } elseif ($link['p'] == $frompage) {
  20344. $this->links[$key]['p'] = $topage;
  20345. }
  20346. }
  20347. }
  20348. // adjust javascript
  20349. $jfrompage = $frompage;
  20350. $jtopage = $topage;
  20351. if (preg_match_all('/this\.addField\(\'([^\']*)\',\'([^\']*)\',([0-9]+)/', $this->javascript, $pamatch) > 0) {
  20352. foreach($pamatch[0] as $pk => $pmatch) {
  20353. $pagenum = intval($pamatch[3][$pk]) + 1;
  20354. if (($pagenum >= $jtopage) AND ($pagenum < $jfrompage)) {
  20355. $newpage = ($pagenum + 1);
  20356. } elseif ($pagenum == $jfrompage) {
  20357. $newpage = $jtopage;
  20358. } else {
  20359. $newpage = $pagenum;
  20360. }
  20361. --$newpage;
  20362. $newjs = "this.addField(\'".$pamatch[1][$pk]."\',\'".$pamatch[2][$pk]."\',".$newpage;
  20363. $this->javascript = str_replace($pmatch, $newjs, $this->javascript);
  20364. }
  20365. unset($pamatch);
  20366. }
  20367. // return to last page
  20368. $this->lastPage(true);
  20369. return true;
  20370. }
  20371. /**
  20372. * Remove the specified page.
  20373. * @param $page (int) page to remove
  20374. * @return true in case of success, false in case of error.
  20375. * @public
  20376. * @since 4.6.004 (2009-04-23)
  20377. */
  20378. public function deletePage($page) {
  20379. if (($page < 1) OR ($page > $this->numpages)) {
  20380. return false;
  20381. }
  20382. // delete current page
  20383. unset($this->pages[$page]);
  20384. unset($this->pagedim[$page]);
  20385. unset($this->pagelen[$page]);
  20386. unset($this->intmrk[$page]);
  20387. unset($this->bordermrk[$page]);
  20388. unset($this->cntmrk[$page]);
  20389. foreach ($this->pageobjects[$page] as $oid) {
  20390. if (isset($this->offsets[$oid])){
  20391. unset($this->offsets[$oid]);
  20392. }
  20393. }
  20394. unset($this->pageobjects[$page]);
  20395. if (isset($this->footerpos[$page])) {
  20396. unset($this->footerpos[$page]);
  20397. }
  20398. if (isset($this->footerlen[$page])) {
  20399. unset($this->footerlen[$page]);
  20400. }
  20401. if (isset($this->transfmrk[$page])) {
  20402. unset($this->transfmrk[$page]);
  20403. }
  20404. if (isset($this->PageAnnots[$page])) {
  20405. unset($this->PageAnnots[$page]);
  20406. }
  20407. if (isset($this->newpagegroup) AND !empty($this->newpagegroup)) {
  20408. for ($i = $page; $i > 0; --$i) {
  20409. if (isset($this->newpagegroup[$i]) AND (($i + $this->pagegroups[$this->newpagegroup[$i]]) > $page)) {
  20410. --$this->pagegroups[$this->newpagegroup[$i]];
  20411. break;
  20412. }
  20413. }
  20414. }
  20415. if (isset($this->pageopen[$page])) {
  20416. unset($this->pageopen[$page]);
  20417. }
  20418. if ($page < $this->numpages) {
  20419. // update remaining pages
  20420. for ($i = $page; $i < $this->numpages; ++$i) {
  20421. $j = $i + 1;
  20422. // shift pages
  20423. $this->setPageBuffer($i, $this->getPageBuffer($j));
  20424. $this->pagedim[$i] = $this->pagedim[$j];
  20425. $this->pagelen[$i] = $this->pagelen[$j];
  20426. $this->intmrk[$i] = $this->intmrk[$j];
  20427. $this->bordermrk[$i] = $this->bordermrk[$j];
  20428. $this->cntmrk[$i] = $this->cntmrk[$j];
  20429. $this->pageobjects[$i] = $this->pageobjects[$j];
  20430. if (isset($this->footerpos[$j])) {
  20431. $this->footerpos[$i] = $this->footerpos[$j];
  20432. } elseif (isset($this->footerpos[$i])) {
  20433. unset($this->footerpos[$i]);
  20434. }
  20435. if (isset($this->footerlen[$j])) {
  20436. $this->footerlen[$i] = $this->footerlen[$j];
  20437. } elseif (isset($this->footerlen[$i])) {
  20438. unset($this->footerlen[$i]);
  20439. }
  20440. if (isset($this->transfmrk[$j])) {
  20441. $this->transfmrk[$i] = $this->transfmrk[$j];
  20442. } elseif (isset($this->transfmrk[$i])) {
  20443. unset($this->transfmrk[$i]);
  20444. }
  20445. if (isset($this->PageAnnots[$j])) {
  20446. $this->PageAnnots[$i] = $this->PageAnnots[$j];
  20447. } elseif (isset($this->PageAnnots[$i])) {
  20448. unset($this->PageAnnots[$i]);
  20449. }
  20450. if (isset($this->newpagegroup[$j])) {
  20451. $this->newpagegroup[$i] = $this->newpagegroup[$j];
  20452. unset($this->newpagegroup[$j]);
  20453. }
  20454. if ($this->currpagegroup == $j) {
  20455. $this->currpagegroup = $i;
  20456. }
  20457. if (isset($this->pageopen[$j])) {
  20458. $this->pageopen[$i] = $this->pageopen[$j];
  20459. } elseif (isset($this->pageopen[$i])) {
  20460. unset($this->pageopen[$i]);
  20461. }
  20462. }
  20463. // remove last page
  20464. unset($this->pages[$this->numpages]);
  20465. unset($this->pagedim[$this->numpages]);
  20466. unset($this->pagelen[$this->numpages]);
  20467. unset($this->intmrk[$this->numpages]);
  20468. unset($this->bordermrk[$this->numpages]);
  20469. unset($this->cntmrk[$this->numpages]);
  20470. foreach ($this->pageobjects[$this->numpages] as $oid) {
  20471. if (isset($this->offsets[$oid])){
  20472. unset($this->offsets[$oid]);
  20473. }
  20474. }
  20475. unset($this->pageobjects[$this->numpages]);
  20476. if (isset($this->footerpos[$this->numpages])) {
  20477. unset($this->footerpos[$this->numpages]);
  20478. }
  20479. if (isset($this->footerlen[$this->numpages])) {
  20480. unset($this->footerlen[$this->numpages]);
  20481. }
  20482. if (isset($this->transfmrk[$this->numpages])) {
  20483. unset($this->transfmrk[$this->numpages]);
  20484. }
  20485. if (isset($this->PageAnnots[$this->numpages])) {
  20486. unset($this->PageAnnots[$this->numpages]);
  20487. }
  20488. if (isset($this->newpagegroup[$this->numpages])) {
  20489. unset($this->newpagegroup[$this->numpages]);
  20490. }
  20491. if ($this->currpagegroup == $this->numpages) {
  20492. $this->currpagegroup = ($this->numpages - 1);
  20493. }
  20494. if (isset($this->pagegroups[$this->numpages])) {
  20495. unset($this->pagegroups[$this->numpages]);
  20496. }
  20497. if (isset($this->pageopen[$this->numpages])) {
  20498. unset($this->pageopen[$this->numpages]);
  20499. }
  20500. }
  20501. --$this->numpages;
  20502. $this->page = $this->numpages;
  20503. // adjust outlines
  20504. $tmpoutlines = $this->outlines;
  20505. foreach ($tmpoutlines as $key => $outline) {
  20506. if (!$outline['f']) {
  20507. if ($outline['p'] > $page) {
  20508. $this->outlines[$key]['p'] = $outline['p'] - 1;
  20509. } elseif ($outline['p'] == $page) {
  20510. unset($this->outlines[$key]);
  20511. }
  20512. }
  20513. }
  20514. // adjust dests
  20515. $tmpdests = $this->dests;
  20516. foreach ($tmpdests as $key => $dest) {
  20517. if (!$dest['f']) {
  20518. if ($dest['p'] > $page) {
  20519. $this->dests[$key]['p'] = $dest['p'] - 1;
  20520. } elseif ($dest['p'] == $page) {
  20521. unset($this->dests[$key]);
  20522. }
  20523. }
  20524. }
  20525. // adjust links
  20526. $tmplinks = $this->links;
  20527. foreach ($tmplinks as $key => $link) {
  20528. if (!$link['f']) {
  20529. if ($link['p'] > $page) {
  20530. $this->links[$key]['p'] = $link['p'] - 1;
  20531. } elseif ($link['p'] == $page) {
  20532. unset($this->links[$key]);
  20533. }
  20534. }
  20535. }
  20536. // adjust javascript
  20537. $jpage = $page;
  20538. if (preg_match_all('/this\.addField\(\'([^\']*)\',\'([^\']*)\',([0-9]+)/', $this->javascript, $pamatch) > 0) {
  20539. foreach($pamatch[0] as $pk => $pmatch) {
  20540. $pagenum = intval($pamatch[3][$pk]) + 1;
  20541. if ($pagenum >= $jpage) {
  20542. $newpage = ($pagenum - 1);
  20543. } elseif ($pagenum == $jpage) {
  20544. $newpage = 1;
  20545. } else {
  20546. $newpage = $pagenum;
  20547. }
  20548. --$newpage;
  20549. $newjs = "this.addField(\'".$pamatch[1][$pk]."\',\'".$pamatch[2][$pk]."\',".$newpage;
  20550. $this->javascript = str_replace($pmatch, $newjs, $this->javascript);
  20551. }
  20552. unset($pamatch);
  20553. }
  20554. // return to last page
  20555. if ($this->numpages > 0) {
  20556. $this->lastPage(true);
  20557. }
  20558. return true;
  20559. }
  20560. /**
  20561. * Clone the specified page to a new page.
  20562. * @param $page (int) number of page to copy (0 = current page)
  20563. * @return true in case of success, false in case of error.
  20564. * @public
  20565. * @since 4.9.015 (2010-04-20)
  20566. */
  20567. public function copyPage($page=0) {
  20568. if ($page == 0) {
  20569. // default value
  20570. $page = $this->page;
  20571. }
  20572. if (($page < 1) OR ($page > $this->numpages)) {
  20573. return false;
  20574. }
  20575. // close the last page
  20576. $this->endPage();
  20577. // copy all page-related states
  20578. ++$this->numpages;
  20579. $this->page = $this->numpages;
  20580. $this->setPageBuffer($this->page, $this->getPageBuffer($page));
  20581. $this->pagedim[$this->page] = $this->pagedim[$page];
  20582. $this->pagelen[$this->page] = $this->pagelen[$page];
  20583. $this->intmrk[$this->page] = $this->intmrk[$page];
  20584. $this->bordermrk[$this->page] = $this->bordermrk[$page];
  20585. $this->cntmrk[$this->page] = $this->cntmrk[$page];
  20586. $this->pageobjects[$this->page] = $this->pageobjects[$page];
  20587. $this->pageopen[$this->page] = false;
  20588. if (isset($this->footerpos[$page])) {
  20589. $this->footerpos[$this->page] = $this->footerpos[$page];
  20590. }
  20591. if (isset($this->footerlen[$page])) {
  20592. $this->footerlen[$this->page] = $this->footerlen[$page];
  20593. }
  20594. if (isset($this->transfmrk[$page])) {
  20595. $this->transfmrk[$this->page] = $this->transfmrk[$page];
  20596. }
  20597. if (isset($this->PageAnnots[$page])) {
  20598. $this->PageAnnots[$this->page] = $this->PageAnnots[$page];
  20599. }
  20600. if (isset($this->newpagegroup[$page])) {
  20601. // start a new group
  20602. $this->newpagegroup[$this->page] = sizeof($this->newpagegroup) + 1;
  20603. $this->currpagegroup = $this->newpagegroup[$this->page];
  20604. $this->pagegroups[$this->currpagegroup] = 1;
  20605. } elseif (isset($this->currpagegroup) AND ($this->currpagegroup > 0)) {
  20606. ++$this->pagegroups[$this->currpagegroup];
  20607. }
  20608. // copy outlines
  20609. $tmpoutlines = $this->outlines;
  20610. foreach ($tmpoutlines as $key => $outline) {
  20611. if ($outline['p'] == $page) {
  20612. $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']);
  20613. }
  20614. }
  20615. // copy links
  20616. $tmplinks = $this->links;
  20617. foreach ($tmplinks as $key => $link) {
  20618. if ($link['p'] == $page) {
  20619. $this->links[] = array('p' => $this->page, 'y' => $link['y'], 'f' => $link['f']);
  20620. }
  20621. }
  20622. // return to last page
  20623. $this->lastPage(true);
  20624. return true;
  20625. }
  20626. /**
  20627. * Output a Table of Content Index (TOC).
  20628. * This method must be called after all Bookmarks were set.
  20629. * Before calling this method you have to open the page using the addTOCPage() method.
  20630. * After calling this method you have to call endTOCPage() to close the TOC page.
  20631. * You can override this method to achieve different styles.
  20632. * @param $page (int) page number where this TOC should be inserted (leave empty for current page).
  20633. * @param $numbersfont (string) set the font for page numbers (please use monospaced font for better alignment).
  20634. * @param $filler (string) string used to fill the space between text and page number.
  20635. * @param $toc_name (string) name to use for TOC bookmark.
  20636. * @param $style (string) Font style for title: B = Bold, I = Italic, BI = Bold + Italic.
  20637. * @param $color (array) RGB color array for bookmark title (values from 0 to 255).
  20638. * @public
  20639. * @author Nicola Asuni
  20640. * @since 4.5.000 (2009-01-02)
  20641. * @see addTOCPage(), endTOCPage(), addHTMLTOC()
  20642. */
  20643. public function addTOC($page='', $numbersfont='', $filler='.', $toc_name='TOC', $style='', $color=array(0,0,0)) {
  20644. $fontsize = $this->FontSizePt;
  20645. $fontfamily = $this->FontFamily;
  20646. $fontstyle = $this->FontStyle;
  20647. $w = $this->w - $this->lMargin - $this->rMargin;
  20648. $spacer = $this->GetStringWidth(chr(32)) * 4;
  20649. $lmargin = $this->lMargin;
  20650. $rmargin = $this->rMargin;
  20651. $x_start = $this->GetX();
  20652. $page_first = $this->page;
  20653. $current_page = $this->page;
  20654. $page_fill_start = false;
  20655. $page_fill_end = false;
  20656. $current_column = $this->current_column;
  20657. if (TCPDF_STATIC::empty_string($numbersfont)) {
  20658. $numbersfont = $this->default_monospaced_font;
  20659. }
  20660. if (TCPDF_STATIC::empty_string($filler)) {
  20661. $filler = ' ';
  20662. }
  20663. if (TCPDF_STATIC::empty_string($page)) {
  20664. $gap = ' ';
  20665. } else {
  20666. $gap = '';
  20667. if ($page < 1) {
  20668. $page = 1;
  20669. }
  20670. }
  20671. $this->SetFont($numbersfont, $fontstyle, $fontsize);
  20672. $numwidth = $this->GetStringWidth('00000');
  20673. $maxpage = 0; //used for pages on attached documents
  20674. foreach ($this->outlines as $key => $outline) {
  20675. // check for extra pages (used for attachments)
  20676. if (($this->page > $page_first) AND ($outline['p'] >= $this->numpages)) {
  20677. $outline['p'] += ($this->page - $page_first);
  20678. }
  20679. if ($this->rtl) {
  20680. $aligntext = 'R';
  20681. $alignnum = 'L';
  20682. } else {
  20683. $aligntext = 'L';
  20684. $alignnum = 'R';
  20685. }
  20686. if ($outline['l'] == 0) {
  20687. $this->SetFont($fontfamily, $outline['s'].'B', $fontsize);
  20688. } else {
  20689. $this->SetFont($fontfamily, $outline['s'], $fontsize - $outline['l']);
  20690. }
  20691. $this->SetTextColorArray($outline['c']);
  20692. // check for page break
  20693. $this->checkPageBreak(2 * $this->getCellHeight($this->FontSize));
  20694. // set margins and X position
  20695. if (($this->page == $current_page) AND ($this->current_column == $current_column)) {
  20696. $this->lMargin = $lmargin;
  20697. $this->rMargin = $rmargin;
  20698. } else {
  20699. if ($this->current_column != $current_column) {
  20700. if ($this->rtl) {
  20701. $x_start = $this->w - $this->columns[$this->current_column]['x'];
  20702. } else {
  20703. $x_start = $this->columns[$this->current_column]['x'];
  20704. }
  20705. }
  20706. $lmargin = $this->lMargin;
  20707. $rmargin = $this->rMargin;
  20708. $current_page = $this->page;
  20709. $current_column = $this->current_column;
  20710. }
  20711. $this->SetX($x_start);
  20712. $indent = ($spacer * $outline['l']);
  20713. if ($this->rtl) {
  20714. $this->x -= $indent;
  20715. $this->rMargin = $this->w - $this->x;
  20716. } else {
  20717. $this->x += $indent;
  20718. $this->lMargin = $this->x;
  20719. }
  20720. $link = $this->AddLink();
  20721. $this->SetLink($link, $outline['y'], $outline['p']);
  20722. // write the text
  20723. if ($this->rtl) {
  20724. $txt = ' '.$outline['t'];
  20725. } else {
  20726. $txt = $outline['t'].' ';
  20727. }
  20728. $this->Write(0, $txt, $link, false, $aligntext, false, 0, false, false, 0, $numwidth, '');
  20729. if ($this->rtl) {
  20730. $tw = $this->x - $this->lMargin;
  20731. } else {
  20732. $tw = $this->w - $this->rMargin - $this->x;
  20733. }
  20734. $this->SetFont($numbersfont, $fontstyle, $fontsize);
  20735. if (TCPDF_STATIC::empty_string($page)) {
  20736. $pagenum = $outline['p'];
  20737. } else {
  20738. // placemark to be replaced with the correct number
  20739. $pagenum = '{#'.($outline['p']).'}';
  20740. if ($this->isUnicodeFont()) {
  20741. $pagenum = '{'.$pagenum.'}';
  20742. }
  20743. $maxpage = max($maxpage, $outline['p']);
  20744. }
  20745. $fw = ($tw - $this->GetStringWidth($pagenum.$filler));
  20746. $wfiller = $this->GetStringWidth($filler);
  20747. if ($wfiller > 0) {
  20748. $numfills = floor($fw / $wfiller);
  20749. } else {
  20750. $numfills = 0;
  20751. }
  20752. if ($numfills > 0) {
  20753. $rowfill = str_repeat($filler, $numfills);
  20754. } else {
  20755. $rowfill = '';
  20756. }
  20757. if ($this->rtl) {
  20758. $pagenum = $pagenum.$gap.$rowfill;
  20759. } else {
  20760. $pagenum = $rowfill.$gap.$pagenum;
  20761. }
  20762. // write the number
  20763. $this->Cell($tw, 0, $pagenum, 0, 1, $alignnum, 0, $link, 0);
  20764. }
  20765. $page_last = $this->getPage();
  20766. $numpages = ($page_last - $page_first + 1);
  20767. // account for booklet mode
  20768. if ($this->booklet) {
  20769. // check if a blank page is required before TOC
  20770. $page_fill_start = ((($page_first % 2) == 0) XOR (($page % 2) == 0));
  20771. $page_fill_end = (!((($numpages % 2) == 0) XOR ($page_fill_start)));
  20772. if ($page_fill_start) {
  20773. // add a page at the end (to be moved before TOC)
  20774. $this->addPage();
  20775. ++$page_last;
  20776. ++$numpages;
  20777. }
  20778. if ($page_fill_end) {
  20779. // add a page at the end
  20780. $this->addPage();
  20781. ++$page_last;
  20782. ++$numpages;
  20783. }
  20784. }
  20785. $maxpage = max($maxpage, $page_last);
  20786. if (!TCPDF_STATIC::empty_string($page)) {
  20787. for ($p = $page_first; $p <= $page_last; ++$p) {
  20788. // get page data
  20789. $temppage = $this->getPageBuffer($p);
  20790. for ($n = 1; $n <= $maxpage; ++$n) {
  20791. // update page numbers
  20792. $a = '{#'.$n.'}';
  20793. // get page number aliases
  20794. $pnalias = $this->getInternalPageNumberAliases($a);
  20795. // calculate replacement number
  20796. if (($n >= $page) AND ($n <= $this->numpages)) {
  20797. $np = $n + $numpages;
  20798. } else {
  20799. $np = $n;
  20800. }
  20801. $na = TCPDF_STATIC::formatTOCPageNumber(($this->starting_page_number + $np - 1));
  20802. $nu = TCPDF_FONTS::UTF8ToUTF16BE($na, false, $this->isunicode, $this->CurrentFont);
  20803. // replace aliases with numbers
  20804. foreach ($pnalias['u'] as $u) {
  20805. $sfill = str_repeat($filler, max(0, (strlen($u) - strlen($nu.' '))));
  20806. if ($this->rtl) {
  20807. $nr = $nu.TCPDF_FONTS::UTF8ToUTF16BE(' '.$sfill, false, $this->isunicode, $this->CurrentFont);
  20808. } else {
  20809. $nr = TCPDF_FONTS::UTF8ToUTF16BE($sfill.' ', false, $this->isunicode, $this->CurrentFont).$nu;
  20810. }
  20811. $temppage = str_replace($u, $nr, $temppage);
  20812. }
  20813. foreach ($pnalias['a'] as $a) {
  20814. $sfill = str_repeat($filler, max(0, (strlen($a) - strlen($na.' '))));
  20815. if ($this->rtl) {
  20816. $nr = $na.' '.$sfill;
  20817. } else {
  20818. $nr = $sfill.' '.$na;
  20819. }
  20820. $temppage = str_replace($a, $nr, $temppage);
  20821. }
  20822. }
  20823. // save changes
  20824. $this->setPageBuffer($p, $temppage);
  20825. }
  20826. // move pages
  20827. $this->Bookmark($toc_name, 0, 0, $page_first, $style, $color);
  20828. if ($page_fill_start) {
  20829. $this->movePage($page_last, $page_first);
  20830. }
  20831. for ($i = 0; $i < $numpages; ++$i) {
  20832. $this->movePage($page_last, $page);
  20833. }
  20834. }
  20835. }
  20836. /**
  20837. * Output a Table Of Content Index (TOC) using HTML templates.
  20838. * This method must be called after all Bookmarks were set.
  20839. * Before calling this method you have to open the page using the addTOCPage() method.
  20840. * After calling this method you have to call endTOCPage() to close the TOC page.
  20841. * @param $page (int) page number where this TOC should be inserted (leave empty for current page).
  20842. * @param $toc_name (string) name to use for TOC bookmark.
  20843. * @param $templates (array) array of html templates. Use: "#TOC_DESCRIPTION#" for bookmark title, "#TOC_PAGE_NUMBER#" for page number.
  20844. * @param $correct_align (boolean) if true correct the number alignment (numbers must be in monospaced font like courier and right aligned on LTR, or left aligned on RTL)
  20845. * @param $style (string) Font style for title: B = Bold, I = Italic, BI = Bold + Italic.
  20846. * @param $color (array) RGB color array for title (values from 0 to 255).
  20847. * @public
  20848. * @author Nicola Asuni
  20849. * @since 5.0.001 (2010-05-06)
  20850. * @see addTOCPage(), endTOCPage(), addTOC()
  20851. */
  20852. public function addHTMLTOC($page='', $toc_name='TOC', $templates=array(), $correct_align=true, $style='', $color=array(0,0,0)) {
  20853. $filler = ' ';
  20854. $prev_htmlLinkColorArray = $this->htmlLinkColorArray;
  20855. $prev_htmlLinkFontStyle = $this->htmlLinkFontStyle;
  20856. // set new style for link
  20857. $this->htmlLinkColorArray = array();
  20858. $this->htmlLinkFontStyle = '';
  20859. $page_first = $this->getPage();
  20860. $page_fill_start = false;
  20861. $page_fill_end = false;
  20862. // get the font type used for numbers in each template
  20863. $current_font = $this->FontFamily;
  20864. foreach ($templates as $level => $html) {
  20865. $dom = $this->getHtmlDomArray($html);
  20866. foreach ($dom as $key => $value) {
  20867. if ($value['value'] == '#TOC_PAGE_NUMBER#') {
  20868. $this->SetFont($dom[($key - 1)]['fontname']);
  20869. $templates['F'.$level] = $this->isUnicodeFont();
  20870. }
  20871. }
  20872. }
  20873. $this->SetFont($current_font);
  20874. $maxpage = 0; //used for pages on attached documents
  20875. foreach ($this->outlines as $key => $outline) {
  20876. // get HTML template
  20877. $row = $templates[$outline['l']];
  20878. if (TCPDF_STATIC::empty_string($page)) {
  20879. $pagenum = $outline['p'];
  20880. } else {
  20881. // placemark to be replaced with the correct number
  20882. $pagenum = '{#'.($outline['p']).'}';
  20883. if (isset($templates['F'.$outline['l']]) && $templates['F'.$outline['l']]) {
  20884. $pagenum = '{'.$pagenum.'}';
  20885. }
  20886. $maxpage = max($maxpage, $outline['p']);
  20887. }
  20888. // replace templates with current values
  20889. $row = str_replace('#TOC_DESCRIPTION#', $outline['t'], $row);
  20890. $row = str_replace('#TOC_PAGE_NUMBER#', $pagenum, $row);
  20891. // add link to page
  20892. $row = '<a href="#'.$outline['p'].','.$outline['y'].'">'.$row.'</a>';
  20893. // write bookmark entry
  20894. $this->writeHTML($row, false, false, true, false, '');
  20895. }
  20896. // restore link styles
  20897. $this->htmlLinkColorArray = $prev_htmlLinkColorArray;
  20898. $this->htmlLinkFontStyle = $prev_htmlLinkFontStyle;
  20899. // move TOC page and replace numbers
  20900. $page_last = $this->getPage();
  20901. $numpages = ($page_last - $page_first + 1);
  20902. // account for booklet mode
  20903. if ($this->booklet) {
  20904. // check if a blank page is required before TOC
  20905. $page_fill_start = ((($page_first % 2) == 0) XOR (($page % 2) == 0));
  20906. $page_fill_end = (!((($numpages % 2) == 0) XOR ($page_fill_start)));
  20907. if ($page_fill_start) {
  20908. // add a page at the end (to be moved before TOC)
  20909. $this->addPage();
  20910. ++$page_last;
  20911. ++$numpages;
  20912. }
  20913. if ($page_fill_end) {
  20914. // add a page at the end
  20915. $this->addPage();
  20916. ++$page_last;
  20917. ++$numpages;
  20918. }
  20919. }
  20920. $maxpage = max($maxpage, $page_last);
  20921. if (!TCPDF_STATIC::empty_string($page)) {
  20922. for ($p = $page_first; $p <= $page_last; ++$p) {
  20923. // get page data
  20924. $temppage = $this->getPageBuffer($p);
  20925. for ($n = 1; $n <= $maxpage; ++$n) {
  20926. // update page numbers
  20927. $a = '{#'.$n.'}';
  20928. // get page number aliases
  20929. $pnalias = $this->getInternalPageNumberAliases($a);
  20930. // calculate replacement number
  20931. if ($n >= $page) {
  20932. $np = $n + $numpages;
  20933. } else {
  20934. $np = $n;
  20935. }
  20936. $na = TCPDF_STATIC::formatTOCPageNumber(($this->starting_page_number + $np - 1));
  20937. $nu = TCPDF_FONTS::UTF8ToUTF16BE($na, false, $this->isunicode, $this->CurrentFont);
  20938. // replace aliases with numbers
  20939. foreach ($pnalias['u'] as $u) {
  20940. if ($correct_align) {
  20941. $sfill = str_repeat($filler, (strlen($u) - strlen($nu.' ')));
  20942. if ($this->rtl) {
  20943. $nr = $nu.TCPDF_FONTS::UTF8ToUTF16BE(' '.$sfill, false, $this->isunicode, $this->CurrentFont);
  20944. } else {
  20945. $nr = TCPDF_FONTS::UTF8ToUTF16BE($sfill.' ', false, $this->isunicode, $this->CurrentFont).$nu;
  20946. }
  20947. } else {
  20948. $nr = $nu;
  20949. }
  20950. $temppage = str_replace($u, $nr, $temppage);
  20951. }
  20952. foreach ($pnalias['a'] as $a) {
  20953. if ($correct_align) {
  20954. $sfill = str_repeat($filler, (strlen($a) - strlen($na.' ')));
  20955. if ($this->rtl) {
  20956. $nr = $na.' '.$sfill;
  20957. } else {
  20958. $nr = $sfill.' '.$na;
  20959. }
  20960. } else {
  20961. $nr = $na;
  20962. }
  20963. $temppage = str_replace($a, $nr, $temppage);
  20964. }
  20965. }
  20966. // save changes
  20967. $this->setPageBuffer($p, $temppage);
  20968. }
  20969. // move pages
  20970. $this->Bookmark($toc_name, 0, 0, $page_first, $style, $color);
  20971. if ($page_fill_start) {
  20972. $this->movePage($page_last, $page_first);
  20973. }
  20974. for ($i = 0; $i < $numpages; ++$i) {
  20975. $this->movePage($page_last, $page);
  20976. }
  20977. }
  20978. }
  20979. /**
  20980. * Stores a copy of the current TCPDF object used for undo operation.
  20981. * @public
  20982. * @since 4.5.029 (2009-03-19)
  20983. */
  20984. public function startTransaction() {
  20985. if (isset($this->objcopy)) {
  20986. // remove previous copy
  20987. $this->commitTransaction();
  20988. }
  20989. // record current page number and Y position
  20990. $this->start_transaction_page = $this->page;
  20991. $this->start_transaction_y = $this->y;
  20992. // clone current object
  20993. $this->objcopy = TCPDF_STATIC::objclone($this);
  20994. }
  20995. /**
  20996. * Delete the copy of the current TCPDF object used for undo operation.
  20997. * @public
  20998. * @since 4.5.029 (2009-03-19)
  20999. */
  21000. public function commitTransaction() {
  21001. if (isset($this->objcopy)) {
  21002. $this->objcopy->_destroy(true, true);
  21003. unset($this->objcopy);
  21004. }
  21005. }
  21006. /**
  21007. * This method allows to undo the latest transaction by returning the latest saved TCPDF object with startTransaction().
  21008. * @param $self (boolean) if true restores current class object to previous state without the need of reassignment via the returned value.
  21009. * @return TCPDF object.
  21010. * @public
  21011. * @since 4.5.029 (2009-03-19)
  21012. */
  21013. public function rollbackTransaction($self=false) {
  21014. if (isset($this->objcopy)) {
  21015. $this->_destroy(true, true);
  21016. if ($self) {
  21017. $objvars = get_object_vars($this->objcopy);
  21018. foreach ($objvars as $key => $value) {
  21019. $this->$key = $value;
  21020. }
  21021. }
  21022. return $this->objcopy;
  21023. }
  21024. return $this;
  21025. }
  21026. // --- MULTI COLUMNS METHODS -----------------------
  21027. /**
  21028. * Set multiple columns of the same size
  21029. * @param $numcols (int) number of columns (set to zero to disable columns mode)
  21030. * @param $width (int) column width
  21031. * @param $y (int) column starting Y position (leave empty for current Y position)
  21032. * @public
  21033. * @since 4.9.001 (2010-03-28)
  21034. */
  21035. public function setEqualColumns($numcols=0, $width=0, $y='') {
  21036. $this->columns = array();
  21037. if ($numcols < 2) {
  21038. $numcols = 0;
  21039. $this->columns = array();
  21040. } else {
  21041. // maximum column width
  21042. $maxwidth = ($this->w - $this->original_lMargin - $this->original_rMargin) / $numcols;
  21043. if (($width == 0) OR ($width > $maxwidth)) {
  21044. $width = $maxwidth;
  21045. }
  21046. if (TCPDF_STATIC::empty_string($y)) {
  21047. $y = $this->y;
  21048. }
  21049. // space between columns
  21050. $space = (($this->w - $this->original_lMargin - $this->original_rMargin - ($numcols * $width)) / ($numcols - 1));
  21051. // fill the columns array (with, space, starting Y position)
  21052. for ($i = 0; $i < $numcols; ++$i) {
  21053. $this->columns[$i] = array('w' => $width, 's' => $space, 'y' => $y);
  21054. }
  21055. }
  21056. $this->num_columns = $numcols;
  21057. $this->current_column = 0;
  21058. $this->column_start_page = $this->page;
  21059. $this->selectColumn(0);
  21060. }
  21061. /**
  21062. * Remove columns and reset page margins.
  21063. * @public
  21064. * @since 5.9.072 (2011-04-26)
  21065. */
  21066. public function resetColumns() {
  21067. $this->lMargin = $this->original_lMargin;
  21068. $this->rMargin = $this->original_rMargin;
  21069. $this->setEqualColumns();
  21070. }
  21071. /**
  21072. * Set columns array.
  21073. * Each column is represented by an array of arrays with the following keys: (w = width, s = space between columns, y = column top position).
  21074. * @param $columns (array)
  21075. * @public
  21076. * @since 4.9.001 (2010-03-28)
  21077. */
  21078. public function setColumnsArray($columns) {
  21079. $this->columns = $columns;
  21080. $this->num_columns = count($columns);
  21081. $this->current_column = 0;
  21082. $this->column_start_page = $this->page;
  21083. $this->selectColumn(0);
  21084. }
  21085. /**
  21086. * Set position at a given column
  21087. * @param $col (int) column number (from 0 to getNumberOfColumns()-1); empty string = current column.
  21088. * @public
  21089. * @since 4.9.001 (2010-03-28)
  21090. */
  21091. public function selectColumn($col='') {
  21092. if (is_string($col)) {
  21093. $col = $this->current_column;
  21094. } elseif ($col >= $this->num_columns) {
  21095. $col = 0;
  21096. }
  21097. $xshift = array('x' => 0, 's' => array('H' => 0, 'V' => 0), 'p' => array('L' => 0, 'T' => 0, 'R' => 0, 'B' => 0));
  21098. $enable_thead = false;
  21099. if ($this->num_columns > 1) {
  21100. if ($col != $this->current_column) {
  21101. // move Y pointer at the top of the column
  21102. if ($this->column_start_page == $this->page) {
  21103. $this->y = $this->columns[$col]['y'];
  21104. } else {
  21105. $this->y = $this->tMargin;
  21106. }
  21107. // Avoid to write table headers more than once
  21108. if (($this->page > $this->maxselcol['page']) OR (($this->page == $this->maxselcol['page']) AND ($col > $this->maxselcol['column']))) {
  21109. $enable_thead = true;
  21110. $this->maxselcol['page'] = $this->page;
  21111. $this->maxselcol['column'] = $col;
  21112. }
  21113. }
  21114. $xshift = $this->colxshift;
  21115. // set X position of the current column by case
  21116. $listindent = ($this->listindentlevel * $this->listindent);
  21117. // calculate column X position
  21118. $colpos = 0;
  21119. for ($i = 0; $i < $col; ++$i) {
  21120. $colpos += ($this->columns[$i]['w'] + $this->columns[$i]['s']);
  21121. }
  21122. if ($this->rtl) {
  21123. $x = $this->w - $this->original_rMargin - $colpos;
  21124. $this->rMargin = ($this->w - $x + $listindent);
  21125. $this->lMargin = ($x - $this->columns[$col]['w']);
  21126. $this->x = $x - $listindent;
  21127. } else {
  21128. $x = $this->original_lMargin + $colpos;
  21129. $this->lMargin = ($x + $listindent);
  21130. $this->rMargin = ($this->w - $x - $this->columns[$col]['w']);
  21131. $this->x = $x + $listindent;
  21132. }
  21133. $this->columns[$col]['x'] = $x;
  21134. }
  21135. $this->current_column = $col;
  21136. // fix for HTML mode
  21137. $this->newline = true;
  21138. // print HTML table header (if any)
  21139. if ((!TCPDF_STATIC::empty_string($this->thead)) AND (!$this->inthead)) {
  21140. if ($enable_thead) {
  21141. // print table header
  21142. $this->writeHTML($this->thead, false, false, false, false, '');
  21143. $this->y += $xshift['s']['V'];
  21144. // store end of header position
  21145. if (!isset($this->columns[$col]['th'])) {
  21146. $this->columns[$col]['th'] = array();
  21147. }
  21148. $this->columns[$col]['th']['\''.$this->page.'\''] = $this->y;
  21149. $this->lasth = 0;
  21150. } elseif (isset($this->columns[$col]['th']['\''.$this->page.'\''])) {
  21151. $this->y = $this->columns[$col]['th']['\''.$this->page.'\''];
  21152. }
  21153. }
  21154. // account for an html table cell over multiple columns
  21155. if ($this->rtl) {
  21156. $this->rMargin += $xshift['x'];
  21157. $this->x -= ($xshift['x'] + $xshift['p']['R']);
  21158. } else {
  21159. $this->lMargin += $xshift['x'];
  21160. $this->x += $xshift['x'] + $xshift['p']['L'];
  21161. }
  21162. }
  21163. /**
  21164. * Return the current column number
  21165. * @return int current column number
  21166. * @public
  21167. * @since 5.5.011 (2010-07-08)
  21168. */
  21169. public function getColumn() {
  21170. return $this->current_column;
  21171. }
  21172. /**
  21173. * Return the current number of columns.
  21174. * @return int number of columns
  21175. * @public
  21176. * @since 5.8.018 (2010-08-25)
  21177. */
  21178. public function getNumberOfColumns() {
  21179. return $this->num_columns;
  21180. }
  21181. /**
  21182. * Set Text rendering mode.
  21183. * @param $stroke (int) outline size in user units (0 = disable).
  21184. * @param $fill (boolean) if true fills the text (default).
  21185. * @param $clip (boolean) if true activate clipping mode
  21186. * @public
  21187. * @since 4.9.008 (2009-04-02)
  21188. */
  21189. public function setTextRenderingMode($stroke=0, $fill=true, $clip=false) {
  21190. // Ref.: PDF 32000-1:2008 - 9.3.6 Text Rendering Mode
  21191. // convert text rendering parameters
  21192. if ($stroke < 0) {
  21193. $stroke = 0;
  21194. }
  21195. if ($fill === true) {
  21196. if ($stroke > 0) {
  21197. if ($clip === true) {
  21198. // Fill, then stroke text and add to path for clipping
  21199. $textrendermode = 6;
  21200. } else {
  21201. // Fill, then stroke text
  21202. $textrendermode = 2;
  21203. }
  21204. $textstrokewidth = $stroke;
  21205. } else {
  21206. if ($clip === true) {
  21207. // Fill text and add to path for clipping
  21208. $textrendermode = 4;
  21209. } else {
  21210. // Fill text
  21211. $textrendermode = 0;
  21212. }
  21213. }
  21214. } else {
  21215. if ($stroke > 0) {
  21216. if ($clip === true) {
  21217. // Stroke text and add to path for clipping
  21218. $textrendermode = 5;
  21219. } else {
  21220. // Stroke text
  21221. $textrendermode = 1;
  21222. }
  21223. $textstrokewidth = $stroke;
  21224. } else {
  21225. if ($clip === true) {
  21226. // Add text to path for clipping
  21227. $textrendermode = 7;
  21228. } else {
  21229. // Neither fill nor stroke text (invisible)
  21230. $textrendermode = 3;
  21231. }
  21232. }
  21233. }
  21234. $this->textrendermode = $textrendermode;
  21235. $this->textstrokewidth = $stroke;
  21236. }
  21237. /**
  21238. * Set parameters for drop shadow effect for text.
  21239. * @param $params (array) 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.
  21240. * @since 5.9.174 (2012-07-25)
  21241. * @public
  21242. */
  21243. public function setTextShadow($params=array('enabled'=>false, 'depth_w'=>0, 'depth_h'=>0, 'color'=>false, 'opacity'=>1, 'blend_mode'=>'Normal')) {
  21244. if (isset($params['enabled'])) {
  21245. $this->txtshadow['enabled'] = $params['enabled']?true:false;
  21246. } else {
  21247. $this->txtshadow['enabled'] = false;
  21248. }
  21249. if (isset($params['depth_w'])) {
  21250. $this->txtshadow['depth_w'] = floatval($params['depth_w']);
  21251. } else {
  21252. $this->txtshadow['depth_w'] = 0;
  21253. }
  21254. if (isset($params['depth_h'])) {
  21255. $this->txtshadow['depth_h'] = floatval($params['depth_h']);
  21256. } else {
  21257. $this->txtshadow['depth_h'] = 0;
  21258. }
  21259. if (isset($params['color']) AND ($params['color'] !== false) AND is_array($params['color'])) {
  21260. $this->txtshadow['color'] = $params['color'];
  21261. } else {
  21262. $this->txtshadow['color'] = $this->strokecolor;
  21263. }
  21264. if (isset($params['opacity'])) {
  21265. $this->txtshadow['opacity'] = min(1, max(0, floatval($params['opacity'])));
  21266. } else {
  21267. $this->txtshadow['opacity'] = 1;
  21268. }
  21269. 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'))) {
  21270. $this->txtshadow['blend_mode'] = $params['blend_mode'];
  21271. } else {
  21272. $this->txtshadow['blend_mode'] = 'Normal';
  21273. }
  21274. if ((($this->txtshadow['depth_w'] == 0) AND ($this->txtshadow['depth_h'] == 0)) OR ($this->txtshadow['opacity'] == 0)) {
  21275. $this->txtshadow['enabled'] = false;
  21276. }
  21277. }
  21278. /**
  21279. * Return the text shadow parameters array.
  21280. * @return Array of parameters.
  21281. * @since 5.9.174 (2012-07-25)
  21282. * @public
  21283. */
  21284. public function getTextShadow() {
  21285. return $this->txtshadow;
  21286. }
  21287. /**
  21288. * Returns an array of chars containing soft hyphens.
  21289. * @param $word (array) array of chars
  21290. * @param $patterns (array) Array of hypenation patterns.
  21291. * @param $dictionary (array) Array of words to be returned without applying the hyphenation algorithm.
  21292. * @param $leftmin (int) Minimum number of character to leave on the left of the word without applying the hyphens.
  21293. * @param $rightmin (int) Minimum number of character to leave on the right of the word without applying the hyphens.
  21294. * @param $charmin (int) Minimum word length to apply the hyphenation algorithm.
  21295. * @param $charmax (int) Maximum length of broken piece of word.
  21296. * @return array text with soft hyphens
  21297. * @author Nicola Asuni
  21298. * @since 4.9.012 (2010-04-12)
  21299. * @protected
  21300. */
  21301. protected function hyphenateWord($word, $patterns, $dictionary=array(), $leftmin=1, $rightmin=2, $charmin=1, $charmax=8) {
  21302. $hyphenword = array(); // hyphens positions
  21303. $numchars = count($word);
  21304. if ($numchars <= $charmin) {
  21305. return $word;
  21306. }
  21307. $word_string = TCPDF_FONTS::UTF8ArrSubString($word, '', '', $this->isunicode);
  21308. // some words will be returned as-is
  21309. $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})(\]?)$/';
  21310. if (preg_match($pattern, $word_string) > 0) {
  21311. // email
  21312. return $word;
  21313. }
  21314. $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})(\]?)$/';
  21315. if (preg_match($pattern, $word_string) > 0) {
  21316. // URL
  21317. return $word;
  21318. }
  21319. if (isset($dictionary[$word_string])) {
  21320. return TCPDF_FONTS::UTF8StringToArray($dictionary[$word_string], $this->isunicode, $this->CurrentFont);
  21321. }
  21322. // surround word with '_' characters
  21323. $tmpword = array_merge(array(46), $word, array(46));
  21324. $tmpnumchars = $numchars + 2;
  21325. $maxpos = $tmpnumchars - 1;
  21326. for ($pos = 0; $pos < $maxpos; ++$pos) {
  21327. $imax = min(($tmpnumchars - $pos), $charmax);
  21328. for ($i = 1; $i <= $imax; ++$i) {
  21329. $subword = strtolower(TCPDF_FONTS::UTF8ArrSubString($tmpword, $pos, ($pos + $i), $this->isunicode));
  21330. if (isset($patterns[$subword])) {
  21331. $pattern = TCPDF_FONTS::UTF8StringToArray($patterns[$subword], $this->isunicode, $this->CurrentFont);
  21332. $pattern_length = count($pattern);
  21333. $digits = 1;
  21334. for ($j = 0; $j < $pattern_length; ++$j) {
  21335. // check if $pattern[$j] is a number = hyphenation level (only numbers from 1 to 5 are valid)
  21336. if (($pattern[$j] >= 48) AND ($pattern[$j] <= 57)) {
  21337. if ($j == 0) {
  21338. $zero = $pos - 1;
  21339. } else {
  21340. $zero = $pos + $j - $digits;
  21341. }
  21342. // get hyphenation level
  21343. $level = ($pattern[$j] - 48);
  21344. // if two levels from two different patterns match at the same point, the higher one is selected.
  21345. if (!isset($hyphenword[$zero]) OR ($hyphenword[$zero] < $level)) {
  21346. $hyphenword[$zero] = $level;
  21347. }
  21348. ++$digits;
  21349. }
  21350. }
  21351. }
  21352. }
  21353. }
  21354. $inserted = 0;
  21355. $maxpos = $numchars - $rightmin;
  21356. for ($i = $leftmin; $i <= $maxpos; ++$i) {
  21357. // only odd levels indicate allowed hyphenation points
  21358. if (isset($hyphenword[$i]) AND (($hyphenword[$i] % 2) != 0)) {
  21359. // 173 = soft hyphen character
  21360. array_splice($word, $i + $inserted, 0, 173);
  21361. ++$inserted;
  21362. }
  21363. }
  21364. return $word;
  21365. }
  21366. /**
  21367. * Returns text with soft hyphens.
  21368. * @param $text (string) text to process
  21369. * @param $patterns (mixed) 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/
  21370. * @param $dictionary (array) Array of words to be returned without applying the hyphenation algorithm.
  21371. * @param $leftmin (int) Minimum number of character to leave on the left of the word without applying the hyphens.
  21372. * @param $rightmin (int) Minimum number of character to leave on the right of the word without applying the hyphens.
  21373. * @param $charmin (int) Minimum word length to apply the hyphenation algorithm.
  21374. * @param $charmax (int) Maximum length of broken piece of word.
  21375. * @return array text with soft hyphens
  21376. * @author Nicola Asuni
  21377. * @since 4.9.012 (2010-04-12)
  21378. * @public
  21379. */
  21380. public function hyphenateText($text, $patterns, $dictionary=array(), $leftmin=1, $rightmin=2, $charmin=1, $charmax=8) {
  21381. $text = $this->unhtmlentities($text);
  21382. $word = array(); // last word
  21383. $txtarr = array(); // text to be returned
  21384. $intag = false; // true if we are inside an HTML tag
  21385. $skip = false; // true to skip hyphenation
  21386. if (!is_array($patterns)) {
  21387. $patterns = TCPDF_STATIC::getHyphenPatternsFromTEX($patterns);
  21388. }
  21389. // get array of characters
  21390. $unichars = TCPDF_FONTS::UTF8StringToArray($text, $this->isunicode, $this->CurrentFont);
  21391. // for each char
  21392. foreach ($unichars as $char) {
  21393. if ((!$intag) AND (!$skip) AND TCPDF_FONT_DATA::$uni_type[$char] == 'L') {
  21394. // letter character
  21395. $word[] = $char;
  21396. } else {
  21397. // other type of character
  21398. if (!TCPDF_STATIC::empty_string($word)) {
  21399. // hypenate the word
  21400. $txtarr = array_merge($txtarr, $this->hyphenateWord($word, $patterns, $dictionary, $leftmin, $rightmin, $charmin, $charmax));
  21401. $word = array();
  21402. }
  21403. $txtarr[] = $char;
  21404. if (chr($char) == '<') {
  21405. // we are inside an HTML tag
  21406. $intag = true;
  21407. } elseif ($intag AND (chr($char) == '>')) {
  21408. // end of HTML tag
  21409. $intag = false;
  21410. // check for style tag
  21411. $expected = array(115, 116, 121, 108, 101); // = 'style'
  21412. $current = array_slice($txtarr, -6, 5); // last 5 chars
  21413. $compare = array_diff($expected, $current);
  21414. if (empty($compare)) {
  21415. // check if it is a closing tag
  21416. $expected = array(47); // = '/'
  21417. $current = array_slice($txtarr, -7, 1);
  21418. $compare = array_diff($expected, $current);
  21419. if (empty($compare)) {
  21420. // closing style tag
  21421. $skip = false;
  21422. } else {
  21423. // opening style tag
  21424. $skip = true;
  21425. }
  21426. }
  21427. }
  21428. }
  21429. }
  21430. if (!TCPDF_STATIC::empty_string($word)) {
  21431. // hypenate the word
  21432. $txtarr = array_merge($txtarr, $this->hyphenateWord($word, $patterns, $dictionary, $leftmin, $rightmin, $charmin, $charmax));
  21433. }
  21434. // convert char array to string and return
  21435. return TCPDF_FONTS::UTF8ArrSubString($txtarr, '', '', $this->isunicode);
  21436. }
  21437. /**
  21438. * Enable/disable rasterization of vector images using ImageMagick library.
  21439. * @param $mode (boolean) if true enable rasterization, false otherwise.
  21440. * @public
  21441. * @since 5.0.000 (2010-04-27)
  21442. */
  21443. public function setRasterizeVectorImages($mode) {
  21444. $this->rasterize_vector_images = $mode;
  21445. }
  21446. /**
  21447. * Enable or disable default option for font subsetting.
  21448. * @param $enable (boolean) if true enable font subsetting by default.
  21449. * @author Nicola Asuni
  21450. * @public
  21451. * @since 5.3.002 (2010-06-07)
  21452. */
  21453. public function setFontSubsetting($enable=true) {
  21454. if ($this->pdfa_mode) {
  21455. $this->font_subsetting = false;
  21456. } else {
  21457. $this->font_subsetting = $enable ? true : false;
  21458. }
  21459. }
  21460. /**
  21461. * Return the default option for font subsetting.
  21462. * @return boolean default font subsetting state.
  21463. * @author Nicola Asuni
  21464. * @public
  21465. * @since 5.3.002 (2010-06-07)
  21466. */
  21467. public function getFontSubsetting() {
  21468. return $this->font_subsetting;
  21469. }
  21470. /**
  21471. * Left trim the input string
  21472. * @param $str (string) string to trim
  21473. * @param $replace (string) string that replace spaces.
  21474. * @return left trimmed string
  21475. * @author Nicola Asuni
  21476. * @public
  21477. * @since 5.8.000 (2010-08-11)
  21478. */
  21479. public function stringLeftTrim($str, $replace='') {
  21480. return preg_replace('/^'.$this->re_space['p'].'+/'.$this->re_space['m'], $replace, $str);
  21481. }
  21482. /**
  21483. * Right trim the input string
  21484. * @param $str (string) string to trim
  21485. * @param $replace (string) string that replace spaces.
  21486. * @return right trimmed string
  21487. * @author Nicola Asuni
  21488. * @public
  21489. * @since 5.8.000 (2010-08-11)
  21490. */
  21491. public function stringRightTrim($str, $replace='') {
  21492. return preg_replace('/'.$this->re_space['p'].'+$/'.$this->re_space['m'], $replace, $str);
  21493. }
  21494. /**
  21495. * Trim the input string
  21496. * @param $str (string) string to trim
  21497. * @param $replace (string) string that replace spaces.
  21498. * @return trimmed string
  21499. * @author Nicola Asuni
  21500. * @public
  21501. * @since 5.8.000 (2010-08-11)
  21502. */
  21503. public function stringTrim($str, $replace='') {
  21504. $str = $this->stringLeftTrim($str, $replace);
  21505. $str = $this->stringRightTrim($str, $replace);
  21506. return $str;
  21507. }
  21508. /**
  21509. * Return true if the current font is unicode type.
  21510. * @return true for unicode font, false otherwise.
  21511. * @author Nicola Asuni
  21512. * @public
  21513. * @since 5.8.002 (2010-08-14)
  21514. */
  21515. public function isUnicodeFont() {
  21516. return (($this->CurrentFont['type'] == 'TrueTypeUnicode') OR ($this->CurrentFont['type'] == 'cidfont0'));
  21517. }
  21518. /**
  21519. * Return normalized font name
  21520. * @param $fontfamily (string) property string containing font family names
  21521. * @return string normalized font name
  21522. * @author Nicola Asuni
  21523. * @public
  21524. * @since 5.8.004 (2010-08-17)
  21525. */
  21526. public function getFontFamilyName($fontfamily) {
  21527. // remove spaces and symbols
  21528. $fontfamily = preg_replace('/[^a-z0-9_\,]/', '', strtolower($fontfamily));
  21529. // extract all font names
  21530. $fontslist = preg_split('/[,]/', $fontfamily);
  21531. // find first valid font name
  21532. foreach ($fontslist as $font) {
  21533. // replace font variations
  21534. $font = preg_replace('/regular$/', '', $font);
  21535. $font = preg_replace('/italic$/', 'I', $font);
  21536. $font = preg_replace('/oblique$/', 'I', $font);
  21537. $font = preg_replace('/bold([I]?)$/', 'B\\1', $font);
  21538. // replace common family names and core fonts
  21539. $pattern = array();
  21540. $replacement = array();
  21541. $pattern[] = '/^serif|^cursive|^fantasy|^timesnewroman/';
  21542. $replacement[] = 'times';
  21543. $pattern[] = '/^sansserif/';
  21544. $replacement[] = 'helvetica';
  21545. $pattern[] = '/^monospace/';
  21546. $replacement[] = 'courier';
  21547. $font = preg_replace($pattern, $replacement, $font);
  21548. if (in_array(strtolower($font), $this->fontlist) OR in_array($font, $this->fontkeys)) {
  21549. return $font;
  21550. }
  21551. }
  21552. // return current font as default
  21553. return $this->CurrentFont['fontkey'];
  21554. }
  21555. /**
  21556. * Start a new XObject Template.
  21557. * 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).
  21558. * 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.
  21559. * Note: X,Y coordinates will be reset to 0,0.
  21560. * @param $w (int) Template width in user units (empty string or zero = page width less margins).
  21561. * @param $h (int) Template height in user units (empty string or zero = page height less margins).
  21562. * @param $group (mixed) 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).
  21563. * @return int the XObject Template ID in case of success or false in case of error.
  21564. * @author Nicola Asuni
  21565. * @public
  21566. * @since 5.8.017 (2010-08-24)
  21567. * @see endTemplate(), printTemplate()
  21568. */
  21569. public function startTemplate($w=0, $h=0, $group=false) {
  21570. if ($this->inxobj) {
  21571. // we are already inside an XObject template
  21572. return false;
  21573. }
  21574. $this->inxobj = true;
  21575. ++$this->n;
  21576. // XObject ID
  21577. $this->xobjid = 'XT'.$this->n;
  21578. // object ID
  21579. $this->xobjects[$this->xobjid] = array('n' => $this->n);
  21580. // store current graphic state
  21581. $this->xobjects[$this->xobjid]['gvars'] = $this->getGraphicVars();
  21582. // initialize data
  21583. $this->xobjects[$this->xobjid]['intmrk'] = 0;
  21584. $this->xobjects[$this->xobjid]['transfmrk'] = array();
  21585. $this->xobjects[$this->xobjid]['outdata'] = '';
  21586. $this->xobjects[$this->xobjid]['xobjects'] = array();
  21587. $this->xobjects[$this->xobjid]['images'] = array();
  21588. $this->xobjects[$this->xobjid]['fonts'] = array();
  21589. $this->xobjects[$this->xobjid]['annotations'] = array();
  21590. $this->xobjects[$this->xobjid]['extgstates'] = array();
  21591. $this->xobjects[$this->xobjid]['gradients'] = array();
  21592. $this->xobjects[$this->xobjid]['spot_colors'] = array();
  21593. // set new environment
  21594. $this->num_columns = 1;
  21595. $this->current_column = 0;
  21596. $this->SetAutoPageBreak(false);
  21597. if (($w === '') OR ($w <= 0)) {
  21598. $w = $this->w - $this->lMargin - $this->rMargin;
  21599. }
  21600. if (($h === '') OR ($h <= 0)) {
  21601. $h = $this->h - $this->tMargin - $this->bMargin;
  21602. }
  21603. $this->xobjects[$this->xobjid]['x'] = 0;
  21604. $this->xobjects[$this->xobjid]['y'] = 0;
  21605. $this->xobjects[$this->xobjid]['w'] = $w;
  21606. $this->xobjects[$this->xobjid]['h'] = $h;
  21607. $this->w = $w;
  21608. $this->h = $h;
  21609. $this->wPt = $this->w * $this->k;
  21610. $this->hPt = $this->h * $this->k;
  21611. $this->fwPt = $this->wPt;
  21612. $this->fhPt = $this->hPt;
  21613. $this->x = 0;
  21614. $this->y = 0;
  21615. $this->lMargin = 0;
  21616. $this->rMargin = 0;
  21617. $this->tMargin = 0;
  21618. $this->bMargin = 0;
  21619. // set group mode
  21620. $this->xobjects[$this->xobjid]['group'] = $group;
  21621. return $this->xobjid;
  21622. }
  21623. /**
  21624. * End the current XObject Template started with startTemplate() and restore the previous graphic state.
  21625. * 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).
  21626. * 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.
  21627. * @return int the XObject Template ID in case of success or false in case of error.
  21628. * @author Nicola Asuni
  21629. * @public
  21630. * @since 5.8.017 (2010-08-24)
  21631. * @see startTemplate(), printTemplate()
  21632. */
  21633. public function endTemplate() {
  21634. if (!$this->inxobj) {
  21635. // we are not inside a template
  21636. return false;
  21637. }
  21638. $this->inxobj = false;
  21639. // restore previous graphic state
  21640. $this->setGraphicVars($this->xobjects[$this->xobjid]['gvars'], true);
  21641. return $this->xobjid;
  21642. }
  21643. /**
  21644. * Print an XObject Template.
  21645. * You can print an XObject Template inside the currently opened Template.
  21646. * 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).
  21647. * 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.
  21648. * @param $id (string) The ID of XObject Template to print.
  21649. * @param $x (int) X position in user units (empty string = current x position)
  21650. * @param $y (int) Y position in user units (empty string = current y position)
  21651. * @param $w (int) Width in user units (zero = remaining page width)
  21652. * @param $h (int) Height in user units (zero = remaining page height)
  21653. * @param $align (string) 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>
  21654. * @param $palign (string) 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>
  21655. * @param $fitonpage (boolean) If true the template is resized to not exceed page dimensions.
  21656. * @author Nicola Asuni
  21657. * @public
  21658. * @since 5.8.017 (2010-08-24)
  21659. * @see startTemplate(), endTemplate()
  21660. */
  21661. public function printTemplate($id, $x='', $y='', $w=0, $h=0, $align='', $palign='', $fitonpage=false) {
  21662. if ($this->state != 2) {
  21663. return;
  21664. }
  21665. if (!isset($this->xobjects[$id])) {
  21666. $this->Error('The XObject Template \''.$id.'\' doesn\'t exist!');
  21667. }
  21668. if ($this->inxobj) {
  21669. if ($id == $this->xobjid) {
  21670. // close current template
  21671. $this->endTemplate();
  21672. } else {
  21673. // use the template as resource for the template currently opened
  21674. $this->xobjects[$this->xobjid]['xobjects'][$id] = $this->xobjects[$id];
  21675. }
  21676. }
  21677. // set default values
  21678. if ($x === '') {
  21679. $x = $this->x;
  21680. }
  21681. if ($y === '') {
  21682. $y = $this->y;
  21683. }
  21684. // check page for no-write regions and adapt page margins if necessary
  21685. list($x, $y) = $this->checkPageRegions($h, $x, $y);
  21686. $ow = $this->xobjects[$id]['w'];
  21687. if ($ow <= 0) {
  21688. $ow = 1;
  21689. }
  21690. $oh = $this->xobjects[$id]['h'];
  21691. if ($oh <= 0) {
  21692. $oh = 1;
  21693. }
  21694. // calculate template width and height on document
  21695. if (($w <= 0) AND ($h <= 0)) {
  21696. $w = $ow;
  21697. $h = $oh;
  21698. } elseif ($w <= 0) {
  21699. $w = $h * $ow / $oh;
  21700. } elseif ($h <= 0) {
  21701. $h = $w * $oh / $ow;
  21702. }
  21703. // fit the template on available space
  21704. list($w, $h, $x, $y) = $this->fitBlock($w, $h, $x, $y, $fitonpage);
  21705. // set page alignment
  21706. $rb_y = $y + $h;
  21707. // set alignment
  21708. if ($this->rtl) {
  21709. if ($palign == 'L') {
  21710. $xt = $this->lMargin;
  21711. } elseif ($palign == 'C') {
  21712. $xt = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
  21713. } elseif ($palign == 'R') {
  21714. $xt = $this->w - $this->rMargin - $w;
  21715. } else {
  21716. $xt = $x - $w;
  21717. }
  21718. $rb_x = $xt;
  21719. } else {
  21720. if ($palign == 'L') {
  21721. $xt = $this->lMargin;
  21722. } elseif ($palign == 'C') {
  21723. $xt = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
  21724. } elseif ($palign == 'R') {
  21725. $xt = $this->w - $this->rMargin - $w;
  21726. } else {
  21727. $xt = $x;
  21728. }
  21729. $rb_x = $xt + $w;
  21730. }
  21731. // print XObject Template + Transformation matrix
  21732. $this->StartTransform();
  21733. // translate and scale
  21734. $sx = ($w / $ow);
  21735. $sy = ($h / $oh);
  21736. $tm = array();
  21737. $tm[0] = $sx;
  21738. $tm[1] = 0;
  21739. $tm[2] = 0;
  21740. $tm[3] = $sy;
  21741. $tm[4] = $xt * $this->k;
  21742. $tm[5] = ($this->h - $h - $y) * $this->k;
  21743. $this->Transform($tm);
  21744. // set object
  21745. $this->_out('/'.$id.' Do');
  21746. $this->StopTransform();
  21747. // add annotations
  21748. if (!empty($this->xobjects[$id]['annotations'])) {
  21749. foreach ($this->xobjects[$id]['annotations'] as $annot) {
  21750. // transform original coordinates
  21751. $coordlt = TCPDF_STATIC::getTransformationMatrixProduct($tm, array(1, 0, 0, 1, ($annot['x'] * $this->k), (-$annot['y'] * $this->k)));
  21752. $ax = ($coordlt[4] / $this->k);
  21753. $ay = ($this->h - $h - ($coordlt[5] / $this->k));
  21754. $coordrb = TCPDF_STATIC::getTransformationMatrixProduct($tm, array(1, 0, 0, 1, (($annot['x'] + $annot['w']) * $this->k), ((-$annot['y'] - $annot['h']) * $this->k)));
  21755. $aw = ($coordrb[4] / $this->k) - $ax;
  21756. $ah = ($this->h - $h - ($coordrb[5] / $this->k)) - $ay;
  21757. $this->Annotation($ax, $ay, $aw, $ah, $annot['text'], $annot['opt'], $annot['spaces']);
  21758. }
  21759. }
  21760. // set pointer to align the next text/objects
  21761. switch($align) {
  21762. case 'T': {
  21763. $this->y = $y;
  21764. $this->x = $rb_x;
  21765. break;
  21766. }
  21767. case 'M': {
  21768. $this->y = $y + round($h/2);
  21769. $this->x = $rb_x;
  21770. break;
  21771. }
  21772. case 'B': {
  21773. $this->y = $rb_y;
  21774. $this->x = $rb_x;
  21775. break;
  21776. }
  21777. case 'N': {
  21778. $this->SetY($rb_y);
  21779. break;
  21780. }
  21781. default:{
  21782. break;
  21783. }
  21784. }
  21785. }
  21786. /**
  21787. * Set the percentage of character stretching.
  21788. * @param $perc (int) percentage of stretching (100 = no stretching)
  21789. * @author Nicola Asuni
  21790. * @public
  21791. * @since 5.9.000 (2010-09-29)
  21792. */
  21793. public function setFontStretching($perc=100) {
  21794. $this->font_stretching = $perc;
  21795. }
  21796. /**
  21797. * Get the percentage of character stretching.
  21798. * @return float stretching value
  21799. * @author Nicola Asuni
  21800. * @public
  21801. * @since 5.9.000 (2010-09-29)
  21802. */
  21803. public function getFontStretching() {
  21804. return $this->font_stretching;
  21805. }
  21806. /**
  21807. * Set the amount to increase or decrease the space between characters in a text.
  21808. * @param $spacing (float) amount to increase or decrease the space between characters in a text (0 = default spacing)
  21809. * @author Nicola Asuni
  21810. * @public
  21811. * @since 5.9.000 (2010-09-29)
  21812. */
  21813. public function setFontSpacing($spacing=0) {
  21814. $this->font_spacing = $spacing;
  21815. }
  21816. /**
  21817. * Get the amount to increase or decrease the space between characters in a text.
  21818. * @return int font spacing (tracking) value
  21819. * @author Nicola Asuni
  21820. * @public
  21821. * @since 5.9.000 (2010-09-29)
  21822. */
  21823. public function getFontSpacing() {
  21824. return $this->font_spacing;
  21825. }
  21826. /**
  21827. * Return an array of no-write page regions
  21828. * @return array of no-write page regions
  21829. * @author Nicola Asuni
  21830. * @public
  21831. * @since 5.9.003 (2010-10-13)
  21832. * @see setPageRegions(), addPageRegion()
  21833. */
  21834. public function getPageRegions() {
  21835. return $this->page_regions;
  21836. }
  21837. /**
  21838. * Set no-write regions on page.
  21839. * 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.
  21840. * A region is always aligned on the left or right side of the page ad is defined using a vertical segment.
  21841. * You can set multiple regions for the same page.
  21842. * @param $regions (array) 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.
  21843. * @author Nicola Asuni
  21844. * @public
  21845. * @since 5.9.003 (2010-10-13)
  21846. * @see addPageRegion(), getPageRegions()
  21847. */
  21848. public function setPageRegions($regions=array()) {
  21849. // empty current regions array
  21850. $this->page_regions = array();
  21851. // add regions
  21852. foreach ($regions as $data) {
  21853. $this->addPageRegion($data);
  21854. }
  21855. }
  21856. /**
  21857. * Add a single no-write region on selected page.
  21858. * 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.
  21859. * A region is always aligned on the left or right side of the page ad is defined using a vertical segment.
  21860. * You can set multiple regions for the same page.
  21861. * @param $region (array) 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).
  21862. * @author Nicola Asuni
  21863. * @public
  21864. * @since 5.9.003 (2010-10-13)
  21865. * @see setPageRegions(), getPageRegions()
  21866. */
  21867. public function addPageRegion($region) {
  21868. if (!isset($region['page']) OR empty($region['page'])) {
  21869. $region['page'] = $this->page;
  21870. }
  21871. if (isset($region['xt']) AND isset($region['xb']) AND ($region['xt'] > 0) AND ($region['xb'] > 0)
  21872. AND isset($region['yt']) AND isset($region['yb']) AND ($region['yt'] >= 0) AND ($region['yt'] < $region['yb'])
  21873. AND isset($region['side']) AND (($region['side'] == 'L') OR ($region['side'] == 'R'))) {
  21874. $this->page_regions[] = $region;
  21875. }
  21876. }
  21877. /**
  21878. * Remove a single no-write region.
  21879. * @param $key (int) region key
  21880. * @author Nicola Asuni
  21881. * @public
  21882. * @since 5.9.003 (2010-10-13)
  21883. * @see setPageRegions(), getPageRegions()
  21884. */
  21885. public function removePageRegion($key) {
  21886. if (isset($this->page_regions[$key])) {
  21887. unset($this->page_regions[$key]);
  21888. }
  21889. }
  21890. /**
  21891. * Check page for no-write regions and adapt current coordinates and page margins if necessary.
  21892. * 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.
  21893. * A region is always aligned on the left or right side of the page ad is defined using a vertical segment.
  21894. * @param $h (float) height of the text/image/object to print in user units
  21895. * @param $x (float) current X coordinate in user units
  21896. * @param $y (float) current Y coordinate in user units
  21897. * @return array($x, $y)
  21898. * @author Nicola Asuni
  21899. * @protected
  21900. * @since 5.9.003 (2010-10-13)
  21901. */
  21902. protected function checkPageRegions($h, $x, $y) {
  21903. // set default values
  21904. if ($x === '') {
  21905. $x = $this->x;
  21906. }
  21907. if ($y === '') {
  21908. $y = $this->y;
  21909. }
  21910. if (!$this->check_page_regions OR empty($this->page_regions)) {
  21911. // no page regions defined
  21912. return array($x, $y);
  21913. }
  21914. if (empty($h)) {
  21915. $h = $this->getCellHeight($this->FontSize);
  21916. }
  21917. // check for page break
  21918. if ($this->checkPageBreak($h, $y)) {
  21919. // the content will be printed on a new page
  21920. $x = $this->x;
  21921. $y = $this->y;
  21922. }
  21923. if ($this->num_columns > 1) {
  21924. if ($this->rtl) {
  21925. $this->lMargin = ($this->columns[$this->current_column]['x'] - $this->columns[$this->current_column]['w']);
  21926. } else {
  21927. $this->rMargin = ($this->w - $this->columns[$this->current_column]['x'] - $this->columns[$this->current_column]['w']);
  21928. }
  21929. } else {
  21930. if ($this->rtl) {
  21931. $this->lMargin = max($this->clMargin, $this->original_lMargin);
  21932. } else {
  21933. $this->rMargin = max($this->crMargin, $this->original_rMargin);
  21934. }
  21935. }
  21936. // adjust coordinates and page margins
  21937. foreach ($this->page_regions as $regid => $regdata) {
  21938. if ($regdata['page'] == $this->page) {
  21939. // check region boundaries
  21940. if (($y > ($regdata['yt'] - $h)) AND ($y <= $regdata['yb'])) {
  21941. // Y is inside the region
  21942. $minv = ($regdata['xb'] - $regdata['xt']) / ($regdata['yb'] - $regdata['yt']); // inverse of angular coefficient
  21943. $yt = max($y, $regdata['yt']);
  21944. $yb = min(($yt + $h), $regdata['yb']);
  21945. $xt = (($yt - $regdata['yt']) * $minv) + $regdata['xt'];
  21946. $xb = (($yb - $regdata['yt']) * $minv) + $regdata['xt'];
  21947. if ($regdata['side'] == 'L') { // left side
  21948. $new_margin = max($xt, $xb);
  21949. if ($this->lMargin < $new_margin) {
  21950. if ($this->rtl) {
  21951. // adjust left page margin
  21952. $this->lMargin = max(0, $new_margin);
  21953. }
  21954. if ($x < $new_margin) {
  21955. // adjust x position
  21956. $x = $new_margin;
  21957. if ($new_margin > ($this->w - $this->rMargin)) {
  21958. // adjust y position
  21959. $y = $regdata['yb'] - $h;
  21960. }
  21961. }
  21962. }
  21963. } elseif ($regdata['side'] == 'R') { // right side
  21964. $new_margin = min($xt, $xb);
  21965. if (($this->w - $this->rMargin) > $new_margin) {
  21966. if (!$this->rtl) {
  21967. // adjust right page margin
  21968. $this->rMargin = max(0, ($this->w - $new_margin));
  21969. }
  21970. if ($x > $new_margin) {
  21971. // adjust x position
  21972. $x = $new_margin;
  21973. if ($new_margin > $this->lMargin) {
  21974. // adjust y position
  21975. $y = $regdata['yb'] - $h;
  21976. }
  21977. }
  21978. }
  21979. }
  21980. }
  21981. }
  21982. }
  21983. return array($x, $y);
  21984. }
  21985. // --- SVG METHODS ---------------------------------------------------------
  21986. /**
  21987. * Embedd a Scalable Vector Graphics (SVG) image.
  21988. * NOTE: SVG standard is not yet fully implemented, use the setRasterizeVectorImages() method to enable/disable rasterization of vector images using ImageMagick library.
  21989. * @param $file (string) Name of the SVG file or a '@' character followed by the SVG data string.
  21990. * @param $x (float) Abscissa of the upper-left corner.
  21991. * @param $y (float) Ordinate of the upper-left corner.
  21992. * @param $w (float) Width of the image in the page. If not specified or equal to zero, it is automatically calculated.
  21993. * @param $h (float) Height of the image in the page. If not specified or equal to zero, it is automatically calculated.
  21994. * @param $link (mixed) URL or identifier returned by AddLink().
  21995. * @param $align (string) 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.
  21996. * @param $palign (string) 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>
  21997. * @param $border (mixed) 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)))
  21998. * @param $fitonpage (boolean) if true the image is resized to not exceed page dimensions.
  21999. * @author Nicola Asuni
  22000. * @since 5.0.000 (2010-05-02)
  22001. * @public
  22002. */
  22003. public function ImageSVG($file, $x='', $y='', $w=0, $h=0, $link='', $align='', $palign='', $border=0, $fitonpage=false) {
  22004. if ($this->state != 2) {
  22005. return;
  22006. }
  22007. // reset SVG vars
  22008. $this->svggradients = array();
  22009. $this->svggradientid = 0;
  22010. $this->svgdefsmode = false;
  22011. $this->svgdefs = array();
  22012. $this->svgclipmode = false;
  22013. $this->svgclippaths = array();
  22014. $this->svgcliptm = array();
  22015. $this->svgclipid = 0;
  22016. $this->svgtext = '';
  22017. $this->svgtextmode = array();
  22018. if ($this->rasterize_vector_images AND ($w > 0) AND ($h > 0)) {
  22019. // convert SVG to raster image using GD or ImageMagick libraries
  22020. return $this->Image($file, $x, $y, $w, $h, 'SVG', $link, $align, true, 300, $palign, false, false, $border, false, false, false);
  22021. }
  22022. if ($file[0] === '@') { // image from string
  22023. $this->svgdir = '';
  22024. $svgdata = substr($file, 1);
  22025. } else { // SVG file
  22026. $this->svgdir = dirname($file);
  22027. $svgdata = TCPDF_STATIC::fileGetContents($file);
  22028. }
  22029. if ($svgdata === FALSE) {
  22030. $this->Error('SVG file not found: '.$file);
  22031. }
  22032. if ($x === '') {
  22033. $x = $this->x;
  22034. }
  22035. if ($y === '') {
  22036. $y = $this->y;
  22037. }
  22038. // check page for no-write regions and adapt page margins if necessary
  22039. list($x, $y) = $this->checkPageRegions($h, $x, $y);
  22040. $k = $this->k;
  22041. $ox = 0;
  22042. $oy = 0;
  22043. $ow = $w;
  22044. $oh = $h;
  22045. $aspect_ratio_align = 'xMidYMid';
  22046. $aspect_ratio_ms = 'meet';
  22047. $regs = array();
  22048. // get original image width and height
  22049. preg_match('/<svg([^\>]*)>/si', $svgdata, $regs);
  22050. if (isset($regs[1]) AND !empty($regs[1])) {
  22051. $tmp = array();
  22052. if (preg_match('/[\s]+x[\s]*=[\s]*"([^"]*)"/si', $regs[1], $tmp)) {
  22053. $ox = $this->getHTMLUnitToUnits($tmp[1], 0, $this->svgunit, false);
  22054. }
  22055. $tmp = array();
  22056. if (preg_match('/[\s]+y[\s]*=[\s]*"([^"]*)"/si', $regs[1], $tmp)) {
  22057. $oy = $this->getHTMLUnitToUnits($tmp[1], 0, $this->svgunit, false);
  22058. }
  22059. $tmp = array();
  22060. if (preg_match('/[\s]+width[\s]*=[\s]*"([^"]*)"/si', $regs[1], $tmp)) {
  22061. $ow = $this->getHTMLUnitToUnits($tmp[1], 1, $this->svgunit, false);
  22062. }
  22063. $tmp = array();
  22064. if (preg_match('/[\s]+height[\s]*=[\s]*"([^"]*)"/si', $regs[1], $tmp)) {
  22065. $oh = $this->getHTMLUnitToUnits($tmp[1], 1, $this->svgunit, false);
  22066. }
  22067. $tmp = array();
  22068. $view_box = array();
  22069. if (preg_match('/[\s]+viewBox[\s]*=[\s]*"[\s]*([0-9\.\-]+)[\s]+([0-9\.\-]+)[\s]+([0-9\.]+)[\s]+([0-9\.]+)[\s]*"/si', $regs[1], $tmp)) {
  22070. if (count($tmp) == 5) {
  22071. array_shift($tmp);
  22072. foreach ($tmp as $key => $val) {
  22073. $view_box[$key] = $this->getHTMLUnitToUnits($val, 0, $this->svgunit, false);
  22074. }
  22075. $ox = $view_box[0];
  22076. $oy = $view_box[1];
  22077. }
  22078. // get aspect ratio
  22079. $tmp = array();
  22080. if (preg_match('/[\s]+preserveAspectRatio[\s]*=[\s]*"([^"]*)"/si', $regs[1], $tmp)) {
  22081. $aspect_ratio = preg_split('/[\s]+/si', $tmp[1]);
  22082. switch (count($aspect_ratio)) {
  22083. case 3: {
  22084. $aspect_ratio_align = $aspect_ratio[1];
  22085. $aspect_ratio_ms = $aspect_ratio[2];
  22086. break;
  22087. }
  22088. case 2: {
  22089. $aspect_ratio_align = $aspect_ratio[0];
  22090. $aspect_ratio_ms = $aspect_ratio[1];
  22091. break;
  22092. }
  22093. case 1: {
  22094. $aspect_ratio_align = $aspect_ratio[0];
  22095. $aspect_ratio_ms = 'meet';
  22096. break;
  22097. }
  22098. }
  22099. }
  22100. }
  22101. }
  22102. if ($ow <= 0) {
  22103. $ow = 1;
  22104. }
  22105. if ($oh <= 0) {
  22106. $oh = 1;
  22107. }
  22108. // calculate image width and height on document
  22109. if (($w <= 0) AND ($h <= 0)) {
  22110. // convert image size to document unit
  22111. $w = $ow;
  22112. $h = $oh;
  22113. } elseif ($w <= 0) {
  22114. $w = $h * $ow / $oh;
  22115. } elseif ($h <= 0) {
  22116. $h = $w * $oh / $ow;
  22117. }
  22118. // fit the image on available space
  22119. list($w, $h, $x, $y) = $this->fitBlock($w, $h, $x, $y, $fitonpage);
  22120. if ($this->rasterize_vector_images) {
  22121. // convert SVG to raster image using GD or ImageMagick libraries
  22122. return $this->Image($file, $x, $y, $w, $h, 'SVG', $link, $align, true, 300, $palign, false, false, $border, false, false, false);
  22123. }
  22124. // set alignment
  22125. $this->img_rb_y = $y + $h;
  22126. // set alignment
  22127. if ($this->rtl) {
  22128. if ($palign == 'L') {
  22129. $ximg = $this->lMargin;
  22130. } elseif ($palign == 'C') {
  22131. $ximg = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
  22132. } elseif ($palign == 'R') {
  22133. $ximg = $this->w - $this->rMargin - $w;
  22134. } else {
  22135. $ximg = $x - $w;
  22136. }
  22137. $this->img_rb_x = $ximg;
  22138. } else {
  22139. if ($palign == 'L') {
  22140. $ximg = $this->lMargin;
  22141. } elseif ($palign == 'C') {
  22142. $ximg = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
  22143. } elseif ($palign == 'R') {
  22144. $ximg = $this->w - $this->rMargin - $w;
  22145. } else {
  22146. $ximg = $x;
  22147. }
  22148. $this->img_rb_x = $ximg + $w;
  22149. }
  22150. // store current graphic vars
  22151. $gvars = $this->getGraphicVars();
  22152. // store SVG position and scale factors
  22153. $svgoffset_x = ($ximg - $ox) * $this->k;
  22154. $svgoffset_y = -($y - $oy) * $this->k;
  22155. if (isset($view_box[2]) AND ($view_box[2] > 0) AND ($view_box[3] > 0)) {
  22156. $ow = $view_box[2];
  22157. $oh = $view_box[3];
  22158. } else {
  22159. if ($ow <= 0) {
  22160. $ow = $w;
  22161. }
  22162. if ($oh <= 0) {
  22163. $oh = $h;
  22164. }
  22165. }
  22166. $svgscale_x = $w / $ow;
  22167. $svgscale_y = $h / $oh;
  22168. // scaling and alignment
  22169. if ($aspect_ratio_align != 'none') {
  22170. // store current scaling values
  22171. $svgscale_old_x = $svgscale_x;
  22172. $svgscale_old_y = $svgscale_y;
  22173. // force uniform scaling
  22174. if ($aspect_ratio_ms == 'slice') {
  22175. // the entire viewport is covered by the viewBox
  22176. if ($svgscale_x > $svgscale_y) {
  22177. $svgscale_y = $svgscale_x;
  22178. } elseif ($svgscale_x < $svgscale_y) {
  22179. $svgscale_x = $svgscale_y;
  22180. }
  22181. } else { // meet
  22182. // the entire viewBox is visible within the viewport
  22183. if ($svgscale_x < $svgscale_y) {
  22184. $svgscale_y = $svgscale_x;
  22185. } elseif ($svgscale_x > $svgscale_y) {
  22186. $svgscale_x = $svgscale_y;
  22187. }
  22188. }
  22189. // correct X alignment
  22190. switch (substr($aspect_ratio_align, 1, 3)) {
  22191. case 'Min': {
  22192. // do nothing
  22193. break;
  22194. }
  22195. case 'Max': {
  22196. $svgoffset_x += (($w * $this->k) - ($ow * $this->k * $svgscale_x));
  22197. break;
  22198. }
  22199. default:
  22200. case 'Mid': {
  22201. $svgoffset_x += ((($w * $this->k) - ($ow * $this->k * $svgscale_x)) / 2);
  22202. break;
  22203. }
  22204. }
  22205. // correct Y alignment
  22206. switch (substr($aspect_ratio_align, 5)) {
  22207. case 'Min': {
  22208. // do nothing
  22209. break;
  22210. }
  22211. case 'Max': {
  22212. $svgoffset_y -= (($h * $this->k) - ($oh * $this->k * $svgscale_y));
  22213. break;
  22214. }
  22215. default:
  22216. case 'Mid': {
  22217. $svgoffset_y -= ((($h * $this->k) - ($oh * $this->k * $svgscale_y)) / 2);
  22218. break;
  22219. }
  22220. }
  22221. }
  22222. // store current page break mode
  22223. $page_break_mode = $this->AutoPageBreak;
  22224. $page_break_margin = $this->getBreakMargin();
  22225. $cell_padding = $this->cell_padding;
  22226. $this->SetCellPadding(0);
  22227. $this->SetAutoPageBreak(false);
  22228. // save the current graphic state
  22229. $this->_out('q'.$this->epsmarker);
  22230. // set initial clipping mask
  22231. $this->Rect($ximg, $y, $w, $h, 'CNZ', array(), array());
  22232. // scale and translate
  22233. $e = $ox * $this->k * (1 - $svgscale_x);
  22234. $f = ($this->h - $oy) * $this->k * (1 - $svgscale_y);
  22235. $this->_out(sprintf('%F %F %F %F %F %F cm', $svgscale_x, 0, 0, $svgscale_y, ($e + $svgoffset_x), ($f + $svgoffset_y)));
  22236. // creates a new XML parser to be used by the other XML functions
  22237. $this->parser = xml_parser_create('UTF-8');
  22238. // the following function allows to use parser inside object
  22239. xml_set_object($this->parser, $this);
  22240. // disable case-folding for this XML parser
  22241. xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, 0);
  22242. // sets the element handler functions for the XML parser
  22243. xml_set_element_handler($this->parser, 'startSVGElementHandler', 'endSVGElementHandler');
  22244. // sets the character data handler function for the XML parser
  22245. xml_set_character_data_handler($this->parser, 'segSVGContentHandler');
  22246. // start parsing an XML document
  22247. if (!xml_parse($this->parser, $svgdata)) {
  22248. $error_message = sprintf('SVG Error: %s at line %d', xml_error_string(xml_get_error_code($this->parser)), xml_get_current_line_number($this->parser));
  22249. $this->Error($error_message);
  22250. }
  22251. // free this XML parser
  22252. xml_parser_free($this->parser);
  22253. // restore previous graphic state
  22254. $this->_out($this->epsmarker.'Q');
  22255. // restore graphic vars
  22256. $this->setGraphicVars($gvars);
  22257. $this->lasth = $gvars['lasth'];
  22258. if (!empty($border)) {
  22259. $bx = $this->x;
  22260. $by = $this->y;
  22261. $this->x = $ximg;
  22262. if ($this->rtl) {
  22263. $this->x += $w;
  22264. }
  22265. $this->y = $y;
  22266. $this->Cell($w, $h, '', $border, 0, '', 0, '', 0, true);
  22267. $this->x = $bx;
  22268. $this->y = $by;
  22269. }
  22270. if ($link) {
  22271. $this->Link($ximg, $y, $w, $h, $link, 0);
  22272. }
  22273. // set pointer to align the next text/objects
  22274. switch($align) {
  22275. case 'T':{
  22276. $this->y = $y;
  22277. $this->x = $this->img_rb_x;
  22278. break;
  22279. }
  22280. case 'M':{
  22281. $this->y = $y + round($h/2);
  22282. $this->x = $this->img_rb_x;
  22283. break;
  22284. }
  22285. case 'B':{
  22286. $this->y = $this->img_rb_y;
  22287. $this->x = $this->img_rb_x;
  22288. break;
  22289. }
  22290. case 'N':{
  22291. $this->SetY($this->img_rb_y);
  22292. break;
  22293. }
  22294. default:{
  22295. // restore pointer to starting position
  22296. $this->x = $gvars['x'];
  22297. $this->y = $gvars['y'];
  22298. $this->page = $gvars['page'];
  22299. $this->current_column = $gvars['current_column'];
  22300. $this->tMargin = $gvars['tMargin'];
  22301. $this->bMargin = $gvars['bMargin'];
  22302. $this->w = $gvars['w'];
  22303. $this->h = $gvars['h'];
  22304. $this->wPt = $gvars['wPt'];
  22305. $this->hPt = $gvars['hPt'];
  22306. $this->fwPt = $gvars['fwPt'];
  22307. $this->fhPt = $gvars['fhPt'];
  22308. break;
  22309. }
  22310. }
  22311. $this->endlinex = $this->img_rb_x;
  22312. // restore page break
  22313. $this->SetAutoPageBreak($page_break_mode, $page_break_margin);
  22314. $this->cell_padding = $cell_padding;
  22315. }
  22316. /**
  22317. * Convert SVG transformation matrix to PDF.
  22318. * @param $tm (array) original SVG transformation matrix
  22319. * @return array transformation matrix
  22320. * @protected
  22321. * @since 5.0.000 (2010-05-02)
  22322. */
  22323. protected function convertSVGtMatrix($tm) {
  22324. $a = $tm[0];
  22325. $b = -$tm[1];
  22326. $c = -$tm[2];
  22327. $d = $tm[3];
  22328. $e = $this->getHTMLUnitToUnits($tm[4], 1, $this->svgunit, false) * $this->k;
  22329. $f = -$this->getHTMLUnitToUnits($tm[5], 1, $this->svgunit, false) * $this->k;
  22330. $x = 0;
  22331. $y = $this->h * $this->k;
  22332. $e = ($x * (1 - $a)) - ($y * $c) + $e;
  22333. $f = ($y * (1 - $d)) - ($x * $b) + $f;
  22334. return array($a, $b, $c, $d, $e, $f);
  22335. }
  22336. /**
  22337. * Apply SVG graphic transformation matrix.
  22338. * @param $tm (array) original SVG transformation matrix
  22339. * @protected
  22340. * @since 5.0.000 (2010-05-02)
  22341. */
  22342. protected function SVGTransform($tm) {
  22343. $this->Transform($this->convertSVGtMatrix($tm));
  22344. }
  22345. /**
  22346. * Apply the requested SVG styles (*** TO BE COMPLETED ***)
  22347. * @param $svgstyle (array) array of SVG styles to apply
  22348. * @param $prevsvgstyle (array) array of previous SVG style
  22349. * @param $x (int) X origin of the bounding box
  22350. * @param $y (int) Y origin of the bounding box
  22351. * @param $w (int) width of the bounding box
  22352. * @param $h (int) height of the bounding box
  22353. * @param $clip_function (string) clip function
  22354. * @param $clip_params (array) array of parameters for clipping function
  22355. * @return object style
  22356. * @author Nicola Asuni
  22357. * @since 5.0.000 (2010-05-02)
  22358. * @protected
  22359. */
  22360. protected function setSVGStyles($svgstyle, $prevsvgstyle, $x=0, $y=0, $w=1, $h=1, $clip_function='', $clip_params=array()) {
  22361. if ($this->state != 2) {
  22362. return;
  22363. }
  22364. $objstyle = '';
  22365. $minlen = (0.01 / $this->k); // minimum acceptable length
  22366. if (!isset($svgstyle['opacity'])) {
  22367. return $objstyle;
  22368. }
  22369. // clip-path
  22370. $regs = array();
  22371. if (preg_match('/url\([\s]*\#([^\)]*)\)/si', $svgstyle['clip-path'], $regs)) {
  22372. $clip_path = $this->svgclippaths[$regs[1]];
  22373. foreach ($clip_path as $cp) {
  22374. $this->startSVGElementHandler('clip-path', $cp['name'], $cp['attribs'], $cp['tm']);
  22375. }
  22376. }
  22377. // opacity
  22378. if ($svgstyle['opacity'] != 1) {
  22379. $this->setAlpha($svgstyle['opacity'], 'Normal', $svgstyle['opacity'], false);
  22380. }
  22381. // color
  22382. $fill_color = TCPDF_COLORS::convertHTMLColorToDec($svgstyle['color'], $this->spot_colors);
  22383. $this->SetFillColorArray($fill_color);
  22384. // text color
  22385. $text_color = TCPDF_COLORS::convertHTMLColorToDec($svgstyle['text-color'], $this->spot_colors);
  22386. $this->SetTextColorArray($text_color);
  22387. // clip
  22388. if (preg_match('/rect\(([a-z0-9\-\.]*)[\s]*([a-z0-9\-\.]*)[\s]*([a-z0-9\-\.]*)[\s]*([a-z0-9\-\.]*)\)/si', $svgstyle['clip'], $regs)) {
  22389. $top = (isset($regs[1])?$this->getHTMLUnitToUnits($regs[1], 0, $this->svgunit, false):0);
  22390. $right = (isset($regs[2])?$this->getHTMLUnitToUnits($regs[2], 0, $this->svgunit, false):0);
  22391. $bottom = (isset($regs[3])?$this->getHTMLUnitToUnits($regs[3], 0, $this->svgunit, false):0);
  22392. $left = (isset($regs[4])?$this->getHTMLUnitToUnits($regs[4], 0, $this->svgunit, false):0);
  22393. $cx = $x + $left;
  22394. $cy = $y + $top;
  22395. $cw = $w - $left - $right;
  22396. $ch = $h - $top - $bottom;
  22397. if ($svgstyle['clip-rule'] == 'evenodd') {
  22398. $clip_rule = 'CNZ';
  22399. } else {
  22400. $clip_rule = 'CEO';
  22401. }
  22402. $this->Rect($cx, $cy, $cw, $ch, $clip_rule, array(), array());
  22403. }
  22404. // fill
  22405. $regs = array();
  22406. if (preg_match('/url\([\s]*\#([^\)]*)\)/si', $svgstyle['fill'], $regs)) {
  22407. // gradient
  22408. $gradient = $this->svggradients[$regs[1]];
  22409. if (isset($gradient['xref'])) {
  22410. // reference to another gradient definition
  22411. $newgradient = $this->svggradients[$gradient['xref']];
  22412. $newgradient['coords'] = $gradient['coords'];
  22413. $newgradient['mode'] = $gradient['mode'];
  22414. $newgradient['type'] = $gradient['type'];
  22415. $newgradient['gradientUnits'] = $gradient['gradientUnits'];
  22416. if (isset($gradient['gradientTransform'])) {
  22417. $newgradient['gradientTransform'] = $gradient['gradientTransform'];
  22418. }
  22419. $gradient = $newgradient;
  22420. }
  22421. //save current Graphic State
  22422. $this->_outSaveGraphicsState();
  22423. //set clipping area
  22424. if (!empty($clip_function) AND method_exists($this, $clip_function)) {
  22425. $bbox = call_user_func_array(array($this, $clip_function), $clip_params);
  22426. if ((!isset($gradient['type']) OR ($gradient['type'] != 3)) AND is_array($bbox) AND (count($bbox) == 4)) {
  22427. list($x, $y, $w, $h) = $bbox;
  22428. }
  22429. }
  22430. if ($gradient['mode'] == 'measure') {
  22431. if (!isset($gradient['coords'][4])) {
  22432. $gradient['coords'][4] = 0.5;
  22433. }
  22434. if (isset($gradient['gradientTransform']) AND !empty($gradient['gradientTransform'])) {
  22435. $gtm = $gradient['gradientTransform'];
  22436. // apply transformation matrix
  22437. $xa = ($gtm[0] * $gradient['coords'][0]) + ($gtm[2] * $gradient['coords'][1]) + $gtm[4];
  22438. $ya = ($gtm[1] * $gradient['coords'][0]) + ($gtm[3] * $gradient['coords'][1]) + $gtm[5];
  22439. $xb = ($gtm[0] * $gradient['coords'][2]) + ($gtm[2] * $gradient['coords'][3]) + $gtm[4];
  22440. $yb = ($gtm[1] * $gradient['coords'][2]) + ($gtm[3] * $gradient['coords'][3]) + $gtm[5];
  22441. $r = sqrt(pow(($gtm[0] * $gradient['coords'][4]), 2) + pow(($gtm[1] * $gradient['coords'][4]), 2));
  22442. $gradient['coords'][0] = $xa;
  22443. $gradient['coords'][1] = $ya;
  22444. $gradient['coords'][2] = $xb;
  22445. $gradient['coords'][3] = $yb;
  22446. $gradient['coords'][4] = $r;
  22447. }
  22448. // convert SVG coordinates to user units
  22449. $gradient['coords'][0] = $this->getHTMLUnitToUnits($gradient['coords'][0], 0, $this->svgunit, false);
  22450. $gradient['coords'][1] = $this->getHTMLUnitToUnits($gradient['coords'][1], 0, $this->svgunit, false);
  22451. $gradient['coords'][2] = $this->getHTMLUnitToUnits($gradient['coords'][2], 0, $this->svgunit, false);
  22452. $gradient['coords'][3] = $this->getHTMLUnitToUnits($gradient['coords'][3], 0, $this->svgunit, false);
  22453. $gradient['coords'][4] = $this->getHTMLUnitToUnits($gradient['coords'][4], 0, $this->svgunit, false);
  22454. if ($w <= $minlen) {
  22455. $w = $minlen;
  22456. }
  22457. if ($h <= $minlen) {
  22458. $h = $minlen;
  22459. }
  22460. // shift units
  22461. if ($gradient['gradientUnits'] == 'objectBoundingBox') {
  22462. // convert to SVG coordinate system
  22463. $gradient['coords'][0] += $x;
  22464. $gradient['coords'][1] += $y;
  22465. $gradient['coords'][2] += $x;
  22466. $gradient['coords'][3] += $y;
  22467. }
  22468. // calculate percentages
  22469. $gradient['coords'][0] = (($gradient['coords'][0] - $x) / $w);
  22470. $gradient['coords'][1] = (($gradient['coords'][1] - $y) / $h);
  22471. $gradient['coords'][2] = (($gradient['coords'][2] - $x) / $w);
  22472. $gradient['coords'][3] = (($gradient['coords'][3] - $y) / $h);
  22473. $gradient['coords'][4] /= $w;
  22474. } elseif ($gradient['mode'] == 'percentage') {
  22475. foreach($gradient['coords'] as $key => $val) {
  22476. $gradient['coords'][$key] = (intval($val) / 100);
  22477. if ($val < 0) {
  22478. $gradient['coords'][$key] = 0;
  22479. } elseif ($val > 1) {
  22480. $gradient['coords'][$key] = 1;
  22481. }
  22482. }
  22483. }
  22484. if (($gradient['type'] == 2) AND ($gradient['coords'][0] == $gradient['coords'][2]) AND ($gradient['coords'][1] == $gradient['coords'][3])) {
  22485. // single color (no shading)
  22486. $gradient['coords'][0] = 1;
  22487. $gradient['coords'][1] = 0;
  22488. $gradient['coords'][2] = 0.999;
  22489. $gradient['coords'][3] = 0;
  22490. }
  22491. // swap Y coordinates
  22492. $tmp = $gradient['coords'][1];
  22493. $gradient['coords'][1] = $gradient['coords'][3];
  22494. $gradient['coords'][3] = $tmp;
  22495. // set transformation map for gradient
  22496. $cy = ($this->h - $y);
  22497. if ($gradient['type'] == 3) {
  22498. // circular gradient
  22499. $cy -= ($gradient['coords'][1] * ($w + $h));
  22500. $h = $w = max($w, $h);
  22501. } else {
  22502. $cy -= $h;
  22503. }
  22504. $this->_out(sprintf('%F 0 0 %F %F %F cm', ($w * $this->k), ($h * $this->k), ($x * $this->k), ($cy * $this->k)));
  22505. if (count($gradient['stops']) > 1) {
  22506. $this->Gradient($gradient['type'], $gradient['coords'], $gradient['stops'], array(), false);
  22507. }
  22508. } elseif ($svgstyle['fill'] != 'none') {
  22509. $fill_color = TCPDF_COLORS::convertHTMLColorToDec($svgstyle['fill'], $this->spot_colors);
  22510. if ($svgstyle['fill-opacity'] != 1) {
  22511. $this->setAlpha($this->alpha['CA'], 'Normal', $svgstyle['fill-opacity'], false);
  22512. }
  22513. $this->SetFillColorArray($fill_color);
  22514. if ($svgstyle['fill-rule'] == 'evenodd') {
  22515. $objstyle .= 'F*';
  22516. } else {
  22517. $objstyle .= 'F';
  22518. }
  22519. }
  22520. // stroke
  22521. if ($svgstyle['stroke'] != 'none') {
  22522. if ($svgstyle['stroke-opacity'] != 1) {
  22523. $this->setAlpha($svgstyle['stroke-opacity'], 'Normal', $this->alpha['ca'], false);
  22524. } elseif (preg_match('/rgba\(\d+%?,\s*\d+%?,\s*\d+%?,\s*(\d+(?:\.\d+)?)\)/i', $svgstyle['stroke'], $rgba_matches)) {
  22525. $this->setAlpha($rgba_matches[1], 'Normal', $this->alpha['ca'], false);
  22526. }
  22527. $stroke_style = array(
  22528. 'color' => TCPDF_COLORS::convertHTMLColorToDec($svgstyle['stroke'], $this->spot_colors),
  22529. 'width' => $this->getHTMLUnitToUnits($svgstyle['stroke-width'], 0, $this->svgunit, false),
  22530. 'cap' => $svgstyle['stroke-linecap'],
  22531. 'join' => $svgstyle['stroke-linejoin']
  22532. );
  22533. if (isset($svgstyle['stroke-dasharray']) AND !empty($svgstyle['stroke-dasharray']) AND ($svgstyle['stroke-dasharray'] != 'none')) {
  22534. $stroke_style['dash'] = $svgstyle['stroke-dasharray'];
  22535. }
  22536. $this->SetLineStyle($stroke_style);
  22537. $objstyle .= 'D';
  22538. }
  22539. // font
  22540. $regs = array();
  22541. if (!empty($svgstyle['font'])) {
  22542. if (preg_match('/font-family[\s]*:[\s]*([^\;\"]*)/si', $svgstyle['font'], $regs)) {
  22543. $font_family = $this->getFontFamilyName($regs[1]);
  22544. } else {
  22545. $font_family = $svgstyle['font-family'];
  22546. }
  22547. if (preg_match('/font-size[\s]*:[\s]*([^\s\;\"]*)/si', $svgstyle['font'], $regs)) {
  22548. $font_size = trim($regs[1]);
  22549. } else {
  22550. $font_size = $svgstyle['font-size'];
  22551. }
  22552. if (preg_match('/font-style[\s]*:[\s]*([^\s\;\"]*)/si', $svgstyle['font'], $regs)) {
  22553. $font_style = trim($regs[1]);
  22554. } else {
  22555. $font_style = $svgstyle['font-style'];
  22556. }
  22557. if (preg_match('/font-weight[\s]*:[\s]*([^\s\;\"]*)/si', $svgstyle['font'], $regs)) {
  22558. $font_weight = trim($regs[1]);
  22559. } else {
  22560. $font_weight = $svgstyle['font-weight'];
  22561. }
  22562. if (preg_match('/font-stretch[\s]*:[\s]*([^\s\;\"]*)/si', $svgstyle['font'], $regs)) {
  22563. $font_stretch = trim($regs[1]);
  22564. } else {
  22565. $font_stretch = $svgstyle['font-stretch'];
  22566. }
  22567. if (preg_match('/letter-spacing[\s]*:[\s]*([^\s\;\"]*)/si', $svgstyle['font'], $regs)) {
  22568. $font_spacing = trim($regs[1]);
  22569. } else {
  22570. $font_spacing = $svgstyle['letter-spacing'];
  22571. }
  22572. } else {
  22573. $font_family = $this->getFontFamilyName($svgstyle['font-family']);
  22574. $font_size = $svgstyle['font-size'];
  22575. $font_style = $svgstyle['font-style'];
  22576. $font_weight = $svgstyle['font-weight'];
  22577. $font_stretch = $svgstyle['font-stretch'];
  22578. $font_spacing = $svgstyle['letter-spacing'];
  22579. }
  22580. $font_size = $this->getHTMLFontUnits($font_size, $this->svgstyles[0]['font-size'], $prevsvgstyle['font-size'], $this->svgunit);
  22581. $font_stretch = $this->getCSSFontStretching($font_stretch, $svgstyle['font-stretch']);
  22582. $font_spacing = $this->getCSSFontSpacing($font_spacing, $svgstyle['letter-spacing']);
  22583. switch ($font_style) {
  22584. case 'italic': {
  22585. $font_style = 'I';
  22586. break;
  22587. }
  22588. case 'oblique': {
  22589. $font_style = 'I';
  22590. break;
  22591. }
  22592. default:
  22593. case 'normal': {
  22594. $font_style = '';
  22595. break;
  22596. }
  22597. }
  22598. switch ($font_weight) {
  22599. case 'bold':
  22600. case 'bolder': {
  22601. $font_style .= 'B';
  22602. break;
  22603. }
  22604. case 'normal': {
  22605. if ((substr($font_family, -1) == 'I') AND (substr($font_family, -2, 1) == 'B')) {
  22606. $font_family = substr($font_family, 0, -2).'I';
  22607. } elseif (substr($font_family, -1) == 'B') {
  22608. $font_family = substr($font_family, 0, -1);
  22609. }
  22610. break;
  22611. }
  22612. }
  22613. switch ($svgstyle['text-decoration']) {
  22614. case 'underline': {
  22615. $font_style .= 'U';
  22616. break;
  22617. }
  22618. case 'overline': {
  22619. $font_style .= 'O';
  22620. break;
  22621. }
  22622. case 'line-through': {
  22623. $font_style .= 'D';
  22624. break;
  22625. }
  22626. default:
  22627. case 'none': {
  22628. break;
  22629. }
  22630. }
  22631. $this->SetFont($font_family, $font_style, $font_size);
  22632. $this->setFontStretching($font_stretch);
  22633. $this->setFontSpacing($font_spacing);
  22634. return $objstyle;
  22635. }
  22636. /**
  22637. * Draws an SVG path
  22638. * @param $d (string) attribute d of the path SVG element
  22639. * @param $style (string) Style of rendering. Possible values are:
  22640. * <ul>
  22641. * <li>D or empty string: Draw (default).</li>
  22642. * <li>F: Fill.</li>
  22643. * <li>F*: Fill using the even-odd rule to determine which regions lie inside the clipping path.</li>
  22644. * <li>DF or FD: Draw and fill.</li>
  22645. * <li>DF* or FD*: Draw and fill using the even-odd rule to determine which regions lie inside the clipping path.</li>
  22646. * <li>CNZ: Clipping mode (using the even-odd rule to determine which regions lie inside the clipping path).</li>
  22647. * <li>CEO: Clipping mode (using the nonzero winding number rule to determine which regions lie inside the clipping path).</li>
  22648. * </ul>
  22649. * @return array of container box measures (x, y, w, h)
  22650. * @author Nicola Asuni
  22651. * @since 5.0.000 (2010-05-02)
  22652. * @protected
  22653. */
  22654. protected function SVGPath($d, $style='') {
  22655. if ($this->state != 2) {
  22656. return;
  22657. }
  22658. // set fill/stroke style
  22659. $op = TCPDF_STATIC::getPathPaintOperator($style, '');
  22660. if (empty($op)) {
  22661. return;
  22662. }
  22663. $paths = array();
  22664. $d = preg_replace('/([0-9ACHLMQSTVZ])([\-\+])/si', '\\1 \\2', $d);
  22665. preg_match_all('/([ACHLMQSTVZ])[\s]*([^ACHLMQSTVZ\"]*)/si', $d, $paths, PREG_SET_ORDER);
  22666. $x = 0;
  22667. $y = 0;
  22668. $x1 = 0;
  22669. $y1 = 0;
  22670. $x2 = 0;
  22671. $y2 = 0;
  22672. $xmin = 2147483647;
  22673. $xmax = 0;
  22674. $ymin = 2147483647;
  22675. $ymax = 0;
  22676. $relcoord = false;
  22677. $minlen = (0.01 / $this->k); // minimum acceptable length (3 point)
  22678. $firstcmd = true; // used to print first point
  22679. // draw curve pieces
  22680. foreach ($paths as $key => $val) {
  22681. // get curve type
  22682. $cmd = trim($val[1]);
  22683. if (strtolower($cmd) == $cmd) {
  22684. // use relative coordinated instead of absolute
  22685. $relcoord = true;
  22686. $xoffset = $x;
  22687. $yoffset = $y;
  22688. } else {
  22689. $relcoord = false;
  22690. $xoffset = 0;
  22691. $yoffset = 0;
  22692. }
  22693. $params = array();
  22694. if (isset($val[2])) {
  22695. // get curve parameters
  22696. $rawparams = preg_split('/([\,\s]+)/si', trim($val[2]));
  22697. $params = array();
  22698. foreach ($rawparams as $ck => $cp) {
  22699. $params[$ck] = $this->getHTMLUnitToUnits($cp, 0, $this->svgunit, false);
  22700. if (abs($params[$ck]) < $minlen) {
  22701. // approximate little values to zero
  22702. $params[$ck] = 0;
  22703. }
  22704. }
  22705. }
  22706. // store current origin point
  22707. $x0 = $x;
  22708. $y0 = $y;
  22709. switch (strtoupper($cmd)) {
  22710. case 'M': { // moveto
  22711. foreach ($params as $ck => $cp) {
  22712. if (($ck % 2) == 0) {
  22713. $x = $cp + $xoffset;
  22714. } else {
  22715. $y = $cp + $yoffset;
  22716. if ($firstcmd OR (abs($x0 - $x) >= $minlen) OR (abs($y0 - $y) >= $minlen)) {
  22717. if ($ck == 1) {
  22718. $this->_outPoint($x, $y);
  22719. $firstcmd = false;
  22720. } else {
  22721. $this->_outLine($x, $y);
  22722. }
  22723. $x0 = $x;
  22724. $y0 = $y;
  22725. }
  22726. $xmin = min($xmin, $x);
  22727. $ymin = min($ymin, $y);
  22728. $xmax = max($xmax, $x);
  22729. $ymax = max($ymax, $y);
  22730. if ($relcoord) {
  22731. $xoffset = $x;
  22732. $yoffset = $y;
  22733. }
  22734. }
  22735. }
  22736. break;
  22737. }
  22738. case 'L': { // lineto
  22739. foreach ($params as $ck => $cp) {
  22740. if (($ck % 2) == 0) {
  22741. $x = $cp + $xoffset;
  22742. } else {
  22743. $y = $cp + $yoffset;
  22744. if ((abs($x0 - $x) >= $minlen) OR (abs($y0 - $y) >= $minlen)) {
  22745. $this->_outLine($x, $y);
  22746. $x0 = $x;
  22747. $y0 = $y;
  22748. }
  22749. $xmin = min($xmin, $x);
  22750. $ymin = min($ymin, $y);
  22751. $xmax = max($xmax, $x);
  22752. $ymax = max($ymax, $y);
  22753. if ($relcoord) {
  22754. $xoffset = $x;
  22755. $yoffset = $y;
  22756. }
  22757. }
  22758. }
  22759. break;
  22760. }
  22761. case 'H': { // horizontal lineto
  22762. foreach ($params as $ck => $cp) {
  22763. $x = $cp + $xoffset;
  22764. if ((abs($x0 - $x) >= $minlen) OR (abs($y0 - $y) >= $minlen)) {
  22765. $this->_outLine($x, $y);
  22766. $x0 = $x;
  22767. $y0 = $y;
  22768. }
  22769. $xmin = min($xmin, $x);
  22770. $xmax = max($xmax, $x);
  22771. if ($relcoord) {
  22772. $xoffset = $x;
  22773. }
  22774. }
  22775. break;
  22776. }
  22777. case 'V': { // vertical lineto
  22778. foreach ($params as $ck => $cp) {
  22779. $y = $cp + $yoffset;
  22780. if ((abs($x0 - $x) >= $minlen) OR (abs($y0 - $y) >= $minlen)) {
  22781. $this->_outLine($x, $y);
  22782. $x0 = $x;
  22783. $y0 = $y;
  22784. }
  22785. $ymin = min($ymin, $y);
  22786. $ymax = max($ymax, $y);
  22787. if ($relcoord) {
  22788. $yoffset = $y;
  22789. }
  22790. }
  22791. break;
  22792. }
  22793. case 'C': { // curveto
  22794. foreach ($params as $ck => $cp) {
  22795. $params[$ck] = $cp;
  22796. if ((($ck + 1) % 6) == 0) {
  22797. $x1 = $params[($ck - 5)] + $xoffset;
  22798. $y1 = $params[($ck - 4)] + $yoffset;
  22799. $x2 = $params[($ck - 3)] + $xoffset;
  22800. $y2 = $params[($ck - 2)] + $yoffset;
  22801. $x = $params[($ck - 1)] + $xoffset;
  22802. $y = $params[($ck)] + $yoffset;
  22803. $this->_outCurve($x1, $y1, $x2, $y2, $x, $y);
  22804. $xmin = min($xmin, $x, $x1, $x2);
  22805. $ymin = min($ymin, $y, $y1, $y2);
  22806. $xmax = max($xmax, $x, $x1, $x2);
  22807. $ymax = max($ymax, $y, $y1, $y2);
  22808. if ($relcoord) {
  22809. $xoffset = $x;
  22810. $yoffset = $y;
  22811. }
  22812. }
  22813. }
  22814. break;
  22815. }
  22816. case 'S': { // shorthand/smooth curveto
  22817. foreach ($params as $ck => $cp) {
  22818. $params[$ck] = $cp;
  22819. if ((($ck + 1) % 4) == 0) {
  22820. if (($key > 0) AND ((strtoupper($paths[($key - 1)][1]) == 'C') OR (strtoupper($paths[($key - 1)][1]) == 'S'))) {
  22821. $x1 = (2 * $x) - $x2;
  22822. $y1 = (2 * $y) - $y2;
  22823. } else {
  22824. $x1 = $x;
  22825. $y1 = $y;
  22826. }
  22827. $x2 = $params[($ck - 3)] + $xoffset;
  22828. $y2 = $params[($ck - 2)] + $yoffset;
  22829. $x = $params[($ck - 1)] + $xoffset;
  22830. $y = $params[($ck)] + $yoffset;
  22831. $this->_outCurve($x1, $y1, $x2, $y2, $x, $y);
  22832. $xmin = min($xmin, $x, $x1, $x2);
  22833. $ymin = min($ymin, $y, $y1, $y2);
  22834. $xmax = max($xmax, $x, $x1, $x2);
  22835. $ymax = max($ymax, $y, $y1, $y2);
  22836. if ($relcoord) {
  22837. $xoffset = $x;
  22838. $yoffset = $y;
  22839. }
  22840. }
  22841. }
  22842. break;
  22843. }
  22844. case 'Q': { // quadratic Bezier curveto
  22845. foreach ($params as $ck => $cp) {
  22846. $params[$ck] = $cp;
  22847. if ((($ck + 1) % 4) == 0) {
  22848. // convert quadratic points to cubic points
  22849. $x1 = $params[($ck - 3)] + $xoffset;
  22850. $y1 = $params[($ck - 2)] + $yoffset;
  22851. $xa = ($x + (2 * $x1)) / 3;
  22852. $ya = ($y + (2 * $y1)) / 3;
  22853. $x = $params[($ck - 1)] + $xoffset;
  22854. $y = $params[($ck)] + $yoffset;
  22855. $xb = ($x + (2 * $x1)) / 3;
  22856. $yb = ($y + (2 * $y1)) / 3;
  22857. $this->_outCurve($xa, $ya, $xb, $yb, $x, $y);
  22858. $xmin = min($xmin, $x, $xa, $xb);
  22859. $ymin = min($ymin, $y, $ya, $yb);
  22860. $xmax = max($xmax, $x, $xa, $xb);
  22861. $ymax = max($ymax, $y, $ya, $yb);
  22862. if ($relcoord) {
  22863. $xoffset = $x;
  22864. $yoffset = $y;
  22865. }
  22866. }
  22867. }
  22868. break;
  22869. }
  22870. case 'T': { // shorthand/smooth quadratic Bezier curveto
  22871. foreach ($params as $ck => $cp) {
  22872. $params[$ck] = $cp;
  22873. if (($ck % 2) != 0) {
  22874. if (($key > 0) AND ((strtoupper($paths[($key - 1)][1]) == 'Q') OR (strtoupper($paths[($key - 1)][1]) == 'T'))) {
  22875. $x1 = (2 * $x) - $x1;
  22876. $y1 = (2 * $y) - $y1;
  22877. } else {
  22878. $x1 = $x;
  22879. $y1 = $y;
  22880. }
  22881. // convert quadratic points to cubic points
  22882. $xa = ($x + (2 * $x1)) / 3;
  22883. $ya = ($y + (2 * $y1)) / 3;
  22884. $x = $params[($ck - 1)] + $xoffset;
  22885. $y = $params[($ck)] + $yoffset;
  22886. $xb = ($x + (2 * $x1)) / 3;
  22887. $yb = ($y + (2 * $y1)) / 3;
  22888. $this->_outCurve($xa, $ya, $xb, $yb, $x, $y);
  22889. $xmin = min($xmin, $x, $xa, $xb);
  22890. $ymin = min($ymin, $y, $ya, $yb);
  22891. $xmax = max($xmax, $x, $xa, $xb);
  22892. $ymax = max($ymax, $y, $ya, $yb);
  22893. if ($relcoord) {
  22894. $xoffset = $x;
  22895. $yoffset = $y;
  22896. }
  22897. }
  22898. }
  22899. break;
  22900. }
  22901. case 'A': { // elliptical arc
  22902. foreach ($params as $ck => $cp) {
  22903. $params[$ck] = $cp;
  22904. if ((($ck + 1) % 7) == 0) {
  22905. $x0 = $x;
  22906. $y0 = $y;
  22907. $rx = abs($params[($ck - 6)]);
  22908. $ry = abs($params[($ck - 5)]);
  22909. $ang = -$rawparams[($ck - 4)];
  22910. $angle = deg2rad($ang);
  22911. $fa = $rawparams[($ck - 3)]; // large-arc-flag
  22912. $fs = $rawparams[($ck - 2)]; // sweep-flag
  22913. $x = $params[($ck - 1)] + $xoffset;
  22914. $y = $params[$ck] + $yoffset;
  22915. if ((abs($x0 - $x) < $minlen) AND (abs($y0 - $y) < $minlen)) {
  22916. // endpoints are almost identical
  22917. $xmin = min($xmin, $x);
  22918. $ymin = min($ymin, $y);
  22919. $xmax = max($xmax, $x);
  22920. $ymax = max($ymax, $y);
  22921. } else {
  22922. $cos_ang = cos($angle);
  22923. $sin_ang = sin($angle);
  22924. $a = (($x0 - $x) / 2);
  22925. $b = (($y0 - $y) / 2);
  22926. $xa = ($a * $cos_ang) - ($b * $sin_ang);
  22927. $ya = ($a * $sin_ang) + ($b * $cos_ang);
  22928. $rx2 = $rx * $rx;
  22929. $ry2 = $ry * $ry;
  22930. $xa2 = $xa * $xa;
  22931. $ya2 = $ya * $ya;
  22932. $delta = ($xa2 / $rx2) + ($ya2 / $ry2);
  22933. if ($delta > 1) {
  22934. $rx *= sqrt($delta);
  22935. $ry *= sqrt($delta);
  22936. $rx2 = $rx * $rx;
  22937. $ry2 = $ry * $ry;
  22938. }
  22939. $numerator = (($rx2 * $ry2) - ($rx2 * $ya2) - ($ry2 * $xa2));
  22940. if ($numerator < 0) {
  22941. $root = 0;
  22942. } else {
  22943. $root = sqrt($numerator / (($rx2 * $ya2) + ($ry2 * $xa2)));
  22944. }
  22945. if ($fa == $fs){
  22946. $root *= -1;
  22947. }
  22948. $cax = $root * (($rx * $ya) / $ry);
  22949. $cay = -$root * (($ry * $xa) / $rx);
  22950. // coordinates of ellipse center
  22951. $cx = ($cax * $cos_ang) - ($cay * $sin_ang) + (($x0 + $x) / 2);
  22952. $cy = ($cax * $sin_ang) + ($cay * $cos_ang) + (($y0 + $y) / 2);
  22953. // get angles
  22954. $angs = TCPDF_STATIC::getVectorsAngle(1, 0, (($xa - $cax) / $rx), (($cay - $ya) / $ry));
  22955. $dang = TCPDF_STATIC::getVectorsAngle((($xa - $cax) / $rx), (($ya - $cay) / $ry), ((-$xa - $cax) / $rx), ((-$ya - $cay) / $ry));
  22956. if (($fs == 0) AND ($dang > 0)) {
  22957. $dang -= (2 * M_PI);
  22958. } elseif (($fs == 1) AND ($dang < 0)) {
  22959. $dang += (2 * M_PI);
  22960. }
  22961. $angf = $angs - $dang;
  22962. if ((($fs == 0) AND ($angs > $angf)) OR (($fs == 1) AND ($angs < $angf))) {
  22963. // reverse angles
  22964. $tmp = $angs;
  22965. $angs = $angf;
  22966. $angf = $tmp;
  22967. }
  22968. $angs = round(rad2deg($angs), 6);
  22969. $angf = round(rad2deg($angf), 6);
  22970. // covent angles to positive values
  22971. if (($angs < 0) AND ($angf < 0)) {
  22972. $angs += 360;
  22973. $angf += 360;
  22974. }
  22975. $pie = false;
  22976. if (($key == 0) AND (isset($paths[($key + 1)][1])) AND (trim($paths[($key + 1)][1]) == 'z')) {
  22977. $pie = true;
  22978. }
  22979. list($axmin, $aymin, $axmax, $aymax) = $this->_outellipticalarc($cx, $cy, $rx, $ry, $ang, $angs, $angf, $pie, 2, false, ($fs == 0), true);
  22980. $xmin = min($xmin, $x, $axmin);
  22981. $ymin = min($ymin, $y, $aymin);
  22982. $xmax = max($xmax, $x, $axmax);
  22983. $ymax = max($ymax, $y, $aymax);
  22984. }
  22985. if ($relcoord) {
  22986. $xoffset = $x;
  22987. $yoffset = $y;
  22988. }
  22989. }
  22990. }
  22991. break;
  22992. }
  22993. case 'Z': {
  22994. $this->_out('h');
  22995. break;
  22996. }
  22997. }
  22998. $firstcmd = false;
  22999. } // end foreach
  23000. if (!empty($op)) {
  23001. $this->_out($op);
  23002. }
  23003. return array($xmin, $ymin, ($xmax - $xmin), ($ymax - $ymin));
  23004. }
  23005. /**
  23006. * Return the tag name without the namespace
  23007. * @param $name (string) Tag name
  23008. * @protected
  23009. */
  23010. protected function removeTagNamespace($name) {
  23011. if(strpos($name, ':') !== false) {
  23012. $parts = explode(':', $name);
  23013. return $parts[(sizeof($parts) - 1)];
  23014. }
  23015. return $name;
  23016. }
  23017. /**
  23018. * Sets the opening SVG element handler function for the XML parser. (*** TO BE COMPLETED ***)
  23019. * @param $parser (resource) The first parameter, parser, is a reference to the XML parser calling the handler.
  23020. * @param $name (string) 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.
  23021. * @param $attribs (array) 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.
  23022. * @param $ctm (array) tranformation matrix for clipping mode (starting transformation matrix).
  23023. * @author Nicola Asuni
  23024. * @since 5.0.000 (2010-05-02)
  23025. * @protected
  23026. */
  23027. protected function startSVGElementHandler($parser, $name, $attribs, $ctm=array()) {
  23028. $name = $this->removeTagNamespace($name);
  23029. // check if we are in clip mode
  23030. if ($this->svgclipmode) {
  23031. $this->svgclippaths[$this->svgclipid][] = array('name' => $name, 'attribs' => $attribs, 'tm' => $this->svgcliptm[$this->svgclipid]);
  23032. return;
  23033. }
  23034. if ($this->svgdefsmode AND !in_array($name, array('clipPath', 'linearGradient', 'radialGradient', 'stop'))) {
  23035. if (isset($attribs['id'])) {
  23036. $attribs['child_elements'] = array();
  23037. $this->svgdefs[$attribs['id']] = array('name' => $name, 'attribs' => $attribs);
  23038. return;
  23039. }
  23040. if (end($this->svgdefs) !== FALSE) {
  23041. $last_svgdefs_id = key($this->svgdefs);
  23042. if (isset($this->svgdefs[$last_svgdefs_id]['attribs']['child_elements'])) {
  23043. $attribs['id'] = 'DF_'.(count($this->svgdefs[$last_svgdefs_id]['attribs']['child_elements']) + 1);
  23044. $this->svgdefs[$last_svgdefs_id]['attribs']['child_elements'][$attribs['id']] = array('name' => $name, 'attribs' => $attribs);
  23045. return;
  23046. }
  23047. }
  23048. return;
  23049. }
  23050. $clipping = false;
  23051. if ($parser == 'clip-path') {
  23052. // set clipping mode
  23053. $clipping = true;
  23054. }
  23055. // get styling properties
  23056. $prev_svgstyle = $this->svgstyles[max(0,(count($this->svgstyles) - 1))]; // previous style
  23057. $svgstyle = $this->svgstyles[0]; // set default style
  23058. if ($clipping AND !isset($attribs['fill']) AND (!isset($attribs['style']) OR (!preg_match('/[;\"\s]{1}fill[\s]*:[\s]*([^;\"]*)/si', $attribs['style'], $attrval)))) {
  23059. // default fill attribute for clipping
  23060. $attribs['fill'] = 'none';
  23061. }
  23062. if (isset($attribs['style']) AND !TCPDF_STATIC::empty_string($attribs['style']) AND ($attribs['style'][0] != ';')) {
  23063. // fix style for regular expression
  23064. $attribs['style'] = ';'.$attribs['style'];
  23065. }
  23066. foreach ($prev_svgstyle as $key => $val) {
  23067. if (in_array($key, TCPDF_IMAGES::$svginheritprop)) {
  23068. // inherit previous value
  23069. $svgstyle[$key] = $val;
  23070. }
  23071. if (isset($attribs[$key]) AND !TCPDF_STATIC::empty_string($attribs[$key])) {
  23072. // specific attribute settings
  23073. if ($attribs[$key] == 'inherit') {
  23074. $svgstyle[$key] = $val;
  23075. } else {
  23076. $svgstyle[$key] = $attribs[$key];
  23077. }
  23078. } elseif (isset($attribs['style']) AND !TCPDF_STATIC::empty_string($attribs['style'])) {
  23079. // CSS style syntax
  23080. $attrval = array();
  23081. if (preg_match('/[;\"\s]{1}'.$key.'[\s]*:[\s]*([^;\"]*)/si', $attribs['style'], $attrval) AND isset($attrval[1])) {
  23082. if ($attrval[1] == 'inherit') {
  23083. $svgstyle[$key] = $val;
  23084. } else {
  23085. $svgstyle[$key] = $attrval[1];
  23086. }
  23087. }
  23088. }
  23089. }
  23090. // transformation matrix
  23091. if (!empty($ctm)) {
  23092. $tm = $ctm;
  23093. } else {
  23094. $tm = array(1,0,0,1,0,0);
  23095. }
  23096. if (isset($attribs['transform']) AND !empty($attribs['transform'])) {
  23097. $tm = TCPDF_STATIC::getTransformationMatrixProduct($tm, TCPDF_STATIC::getSVGTransformMatrix($attribs['transform']));
  23098. }
  23099. $svgstyle['transfmatrix'] = $tm;
  23100. $invisible = false;
  23101. if (($svgstyle['visibility'] == 'hidden') OR ($svgstyle['visibility'] == 'collapse') OR ($svgstyle['display'] == 'none')) {
  23102. // the current graphics element is invisible (nothing is painted)
  23103. $invisible = true;
  23104. }
  23105. // process tag
  23106. switch($name) {
  23107. case 'defs': {
  23108. $this->svgdefsmode = true;
  23109. break;
  23110. }
  23111. // clipPath
  23112. case 'clipPath': {
  23113. if ($invisible) {
  23114. break;
  23115. }
  23116. $this->svgclipmode = true;
  23117. if (!isset($attribs['id'])) {
  23118. $attribs['id'] = 'CP_'.(count($this->svgcliptm) + 1);
  23119. }
  23120. $this->svgclipid = $attribs['id'];
  23121. $this->svgclippaths[$this->svgclipid] = array();
  23122. $this->svgcliptm[$this->svgclipid] = $tm;
  23123. break;
  23124. }
  23125. case 'svg': {
  23126. // start of SVG object
  23127. if(++$this->svg_tag_depth <= 1) {
  23128. break;
  23129. }
  23130. // inner SVG
  23131. array_push($this->svgstyles, $svgstyle);
  23132. $this->StartTransform();
  23133. $svgX = (isset($attribs['x'])?$attribs['x']:0);
  23134. $svgY = (isset($attribs['y'])?$attribs['y']:0);
  23135. $svgW = (isset($attribs['width'])?$attribs['width']:0);
  23136. $svgH = (isset($attribs['height'])?$attribs['height']:0);
  23137. // set x, y position using transform matrix
  23138. $tm = TCPDF_STATIC::getTransformationMatrixProduct($tm, array( 1, 0, 0, 1, $svgX, $svgY));
  23139. $this->SVGTransform($tm);
  23140. // set clipping for width and height
  23141. $x = 0;
  23142. $y = 0;
  23143. $w = (isset($attribs['width'])?$this->getHTMLUnitToUnits($attribs['width'], 0, $this->svgunit, false):$this->w);
  23144. $h = (isset($attribs['height'])?$this->getHTMLUnitToUnits($attribs['height'], 0, $this->svgunit, false):$this->h);
  23145. // draw clipping rect
  23146. $this->Rect($x, $y, $w, $h, 'CNZ', array(), array());
  23147. // parse viewbox, calculate extra transformation matrix
  23148. if (isset($attribs['viewBox'])) {
  23149. $tmp = array();
  23150. preg_match_all("/[0-9]+/", $attribs['viewBox'], $tmp);
  23151. $tmp = $tmp[0];
  23152. if (sizeof($tmp) == 4) {
  23153. $vx = $tmp[0];
  23154. $vy = $tmp[1];
  23155. $vw = $tmp[2];
  23156. $vh = $tmp[3];
  23157. // get aspect ratio
  23158. $tmp = array();
  23159. $aspectX = 'xMid';
  23160. $aspectY = 'YMid';
  23161. $fit = 'meet';
  23162. if (isset($attribs['preserveAspectRatio'])) {
  23163. if($attribs['preserveAspectRatio'] == 'none') {
  23164. $fit = 'none';
  23165. } else {
  23166. preg_match_all('/[a-zA-Z]+/', $attribs['preserveAspectRatio'], $tmp);
  23167. $tmp = $tmp[0];
  23168. if ((sizeof($tmp) == 2) AND (strlen($tmp[0]) == 8) AND (in_array($tmp[1], array('meet', 'slice', 'none')))) {
  23169. $aspectX = substr($tmp[0], 0, 4);
  23170. $aspectY = substr($tmp[0], 4, 4);
  23171. $fit = $tmp[1];
  23172. }
  23173. }
  23174. }
  23175. $wr = ($svgW / $vw);
  23176. $hr = ($svgH / $vh);
  23177. $ax = $ay = 0;
  23178. if ((($fit == 'meet') AND ($hr < $wr)) OR (($fit == 'slice') AND ($hr > $wr))) {
  23179. if ($aspectX == 'xMax') {
  23180. $ax = (($vw * ($wr / $hr)) - $vw);
  23181. }
  23182. if ($aspectX == 'xMid') {
  23183. $ax = ((($vw * ($wr / $hr)) - $vw) / 2);
  23184. }
  23185. $wr = $hr;
  23186. } elseif ((($fit == 'meet') AND ($hr > $wr)) OR (($fit == 'slice') AND ($hr < $wr))) {
  23187. if ($aspectY == 'YMax') {
  23188. $ay = (($vh * ($hr / $wr)) - $vh);
  23189. }
  23190. if ($aspectY == 'YMid') {
  23191. $ay = ((($vh * ($hr / $wr)) - $vh) / 2);
  23192. }
  23193. $hr = $wr;
  23194. }
  23195. $newtm = array($wr, 0, 0, $hr, (($wr * ($ax - $vx)) - $svgX), (($hr * ($ay - $vy)) - $svgY));
  23196. $tm = TCPDF_STATIC::getTransformationMatrixProduct($tm, $newtm);
  23197. $this->SVGTransform($tm);
  23198. }
  23199. }
  23200. $this->setSVGStyles($svgstyle, $prev_svgstyle);
  23201. break;
  23202. }
  23203. case 'g': {
  23204. // group together related graphics elements
  23205. array_push($this->svgstyles, $svgstyle);
  23206. $this->StartTransform();
  23207. $x = (isset($attribs['x'])?$attribs['x']:0);
  23208. $y = (isset($attribs['y'])?$attribs['y']:0);
  23209. $w = 1;//(isset($attribs['width'])?$attribs['width']:1);
  23210. $h = 1;//(isset($attribs['height'])?$attribs['height']:1);
  23211. $tm = TCPDF_STATIC::getTransformationMatrixProduct($tm, array($w, 0, 0, $h, $x, $y));
  23212. $this->SVGTransform($tm);
  23213. $this->setSVGStyles($svgstyle, $prev_svgstyle);
  23214. break;
  23215. }
  23216. case 'linearGradient': {
  23217. if ($this->pdfa_mode) {
  23218. break;
  23219. }
  23220. if (!isset($attribs['id'])) {
  23221. $attribs['id'] = 'GR_'.(count($this->svggradients) + 1);
  23222. }
  23223. $this->svggradientid = $attribs['id'];
  23224. $this->svggradients[$this->svggradientid] = array();
  23225. $this->svggradients[$this->svggradientid]['type'] = 2;
  23226. $this->svggradients[$this->svggradientid]['stops'] = array();
  23227. if (isset($attribs['gradientUnits'])) {
  23228. $this->svggradients[$this->svggradientid]['gradientUnits'] = $attribs['gradientUnits'];
  23229. } else {
  23230. $this->svggradients[$this->svggradientid]['gradientUnits'] = 'objectBoundingBox';
  23231. }
  23232. //$attribs['spreadMethod']
  23233. if (((!isset($attribs['x1'])) AND (!isset($attribs['y1'])) AND (!isset($attribs['x2'])) AND (!isset($attribs['y2'])))
  23234. OR ((isset($attribs['x1']) AND (substr($attribs['x1'], -1) == '%'))
  23235. OR (isset($attribs['y1']) AND (substr($attribs['y1'], -1) == '%'))
  23236. OR (isset($attribs['x2']) AND (substr($attribs['x2'], -1) == '%'))
  23237. OR (isset($attribs['y2']) AND (substr($attribs['y2'], -1) == '%')))) {
  23238. $this->svggradients[$this->svggradientid]['mode'] = 'percentage';
  23239. } else {
  23240. $this->svggradients[$this->svggradientid]['mode'] = 'measure';
  23241. }
  23242. $x1 = (isset($attribs['x1'])?$attribs['x1']:'0');
  23243. $y1 = (isset($attribs['y1'])?$attribs['y1']:'0');
  23244. $x2 = (isset($attribs['x2'])?$attribs['x2']:'100');
  23245. $y2 = (isset($attribs['y2'])?$attribs['y2']:'0');
  23246. if (isset($attribs['gradientTransform'])) {
  23247. $this->svggradients[$this->svggradientid]['gradientTransform'] = TCPDF_STATIC::getSVGTransformMatrix($attribs['gradientTransform']);
  23248. }
  23249. $this->svggradients[$this->svggradientid]['coords'] = array($x1, $y1, $x2, $y2);
  23250. if (isset($attribs['xlink:href']) AND !empty($attribs['xlink:href'])) {
  23251. // gradient is defined on another place
  23252. $this->svggradients[$this->svggradientid]['xref'] = substr($attribs['xlink:href'], 1);
  23253. }
  23254. break;
  23255. }
  23256. case 'radialGradient': {
  23257. if ($this->pdfa_mode) {
  23258. break;
  23259. }
  23260. if (!isset($attribs['id'])) {
  23261. $attribs['id'] = 'GR_'.(count($this->svggradients) + 1);
  23262. }
  23263. $this->svggradientid = $attribs['id'];
  23264. $this->svggradients[$this->svggradientid] = array();
  23265. $this->svggradients[$this->svggradientid]['type'] = 3;
  23266. $this->svggradients[$this->svggradientid]['stops'] = array();
  23267. if (isset($attribs['gradientUnits'])) {
  23268. $this->svggradients[$this->svggradientid]['gradientUnits'] = $attribs['gradientUnits'];
  23269. } else {
  23270. $this->svggradients[$this->svggradientid]['gradientUnits'] = 'objectBoundingBox';
  23271. }
  23272. //$attribs['spreadMethod']
  23273. if (((!isset($attribs['cx'])) AND (!isset($attribs['cy'])))
  23274. OR ((isset($attribs['cx']) AND (substr($attribs['cx'], -1) == '%'))
  23275. OR (isset($attribs['cy']) AND (substr($attribs['cy'], -1) == '%')))) {
  23276. $this->svggradients[$this->svggradientid]['mode'] = 'percentage';
  23277. } elseif (isset($attribs['r']) AND is_numeric($attribs['r']) AND ($attribs['r']) <= 1) {
  23278. $this->svggradients[$this->svggradientid]['mode'] = 'ratio';
  23279. } else {
  23280. $this->svggradients[$this->svggradientid]['mode'] = 'measure';
  23281. }
  23282. $cx = (isset($attribs['cx']) ? $attribs['cx'] : 0.5);
  23283. $cy = (isset($attribs['cy']) ? $attribs['cy'] : 0.5);
  23284. $fx = (isset($attribs['fx']) ? $attribs['fx'] : $cx);
  23285. $fy = (isset($attribs['fy']) ? $attribs['fy'] : $cy);
  23286. $r = (isset($attribs['r']) ? $attribs['r'] : 0.5);
  23287. if (isset($attribs['gradientTransform'])) {
  23288. $this->svggradients[$this->svggradientid]['gradientTransform'] = TCPDF_STATIC::getSVGTransformMatrix($attribs['gradientTransform']);
  23289. }
  23290. $this->svggradients[$this->svggradientid]['coords'] = array($cx, $cy, $fx, $fy, $r);
  23291. if (isset($attribs['xlink:href']) AND !empty($attribs['xlink:href'])) {
  23292. // gradient is defined on another place
  23293. $this->svggradients[$this->svggradientid]['xref'] = substr($attribs['xlink:href'], 1);
  23294. }
  23295. break;
  23296. }
  23297. case 'stop': {
  23298. // gradient stops
  23299. if (substr($attribs['offset'], -1) == '%') {
  23300. $offset = floatval(substr($attribs['offset'], -1)) / 100;
  23301. } else {
  23302. $offset = floatval($attribs['offset']);
  23303. if ($offset > 1) {
  23304. $offset /= 100;
  23305. }
  23306. }
  23307. $stop_color = isset($svgstyle['stop-color'])?TCPDF_COLORS::convertHTMLColorToDec($svgstyle['stop-color'], $this->spot_colors):'black';
  23308. $opacity = isset($svgstyle['stop-opacity'])?$svgstyle['stop-opacity']:1;
  23309. $this->svggradients[$this->svggradientid]['stops'][] = array('offset' => $offset, 'color' => $stop_color, 'opacity' => $opacity);
  23310. break;
  23311. }
  23312. // paths
  23313. case 'path': {
  23314. if ($invisible) {
  23315. break;
  23316. }
  23317. if (isset($attribs['d'])) {
  23318. $d = trim($attribs['d']);
  23319. if (!empty($d)) {
  23320. $x = (isset($attribs['x'])?$attribs['x']:0);
  23321. $y = (isset($attribs['y'])?$attribs['y']:0);
  23322. $w = (isset($attribs['width'])?$attribs['width']:1);
  23323. $h = (isset($attribs['height'])?$attribs['height']:1);
  23324. $tm = TCPDF_STATIC::getTransformationMatrixProduct($tm, array($w, 0, 0, $h, $x, $y));
  23325. if ($clipping) {
  23326. $this->SVGTransform($tm);
  23327. $this->SVGPath($d, 'CNZ');
  23328. } else {
  23329. $this->StartTransform();
  23330. $this->SVGTransform($tm);
  23331. $obstyle = $this->setSVGStyles($svgstyle, $prev_svgstyle, $x, $y, $w, $h, 'SVGPath', array($d, 'CNZ'));
  23332. if (!empty($obstyle)) {
  23333. $this->SVGPath($d, $obstyle);
  23334. }
  23335. $this->StopTransform();
  23336. }
  23337. }
  23338. }
  23339. break;
  23340. }
  23341. // shapes
  23342. case 'rect': {
  23343. if ($invisible) {
  23344. break;
  23345. }
  23346. $x = (isset($attribs['x'])?$this->getHTMLUnitToUnits($attribs['x'], 0, $this->svgunit, false):0);
  23347. $y = (isset($attribs['y'])?$this->getHTMLUnitToUnits($attribs['y'], 0, $this->svgunit, false):0);
  23348. $w = (isset($attribs['width'])?$this->getHTMLUnitToUnits($attribs['width'], 0, $this->svgunit, false):0);
  23349. $h = (isset($attribs['height'])?$this->getHTMLUnitToUnits($attribs['height'], 0, $this->svgunit, false):0);
  23350. $rx = (isset($attribs['rx'])?$this->getHTMLUnitToUnits($attribs['rx'], 0, $this->svgunit, false):0);
  23351. $ry = (isset($attribs['ry'])?$this->getHTMLUnitToUnits($attribs['ry'], 0, $this->svgunit, false):$rx);
  23352. if ($clipping) {
  23353. $this->SVGTransform($tm);
  23354. $this->RoundedRectXY($x, $y, $w, $h, $rx, $ry, '1111', 'CNZ', array(), array());
  23355. } else {
  23356. $this->StartTransform();
  23357. $this->SVGTransform($tm);
  23358. $obstyle = $this->setSVGStyles($svgstyle, $prev_svgstyle, $x, $y, $w, $h, 'RoundedRectXY', array($x, $y, $w, $h, $rx, $ry, '1111', 'CNZ'));
  23359. if (!empty($obstyle)) {
  23360. $this->RoundedRectXY($x, $y, $w, $h, $rx, $ry, '1111', $obstyle, array(), array());
  23361. }
  23362. $this->StopTransform();
  23363. }
  23364. break;
  23365. }
  23366. case 'circle': {
  23367. if ($invisible) {
  23368. break;
  23369. }
  23370. $r = (isset($attribs['r']) ? $this->getHTMLUnitToUnits($attribs['r'], 0, $this->svgunit, false) : 0);
  23371. $cx = (isset($attribs['cx']) ? $this->getHTMLUnitToUnits($attribs['cx'], 0, $this->svgunit, false) : (isset($attribs['x']) ? $this->getHTMLUnitToUnits($attribs['x'], 0, $this->svgunit, false) : 0));
  23372. $cy = (isset($attribs['cy']) ? $this->getHTMLUnitToUnits($attribs['cy'], 0, $this->svgunit, false) : (isset($attribs['y']) ? $this->getHTMLUnitToUnits($attribs['y'], 0, $this->svgunit, false) : 0));
  23373. $x = ($cx - $r);
  23374. $y = ($cy - $r);
  23375. $w = (2 * $r);
  23376. $h = $w;
  23377. if ($clipping) {
  23378. $this->SVGTransform($tm);
  23379. $this->Circle($cx, $cy, $r, 0, 360, 'CNZ', array(), array(), 8);
  23380. } else {
  23381. $this->StartTransform();
  23382. $this->SVGTransform($tm);
  23383. $obstyle = $this->setSVGStyles($svgstyle, $prev_svgstyle, $x, $y, $w, $h, 'Circle', array($cx, $cy, $r, 0, 360, 'CNZ'));
  23384. if (!empty($obstyle)) {
  23385. $this->Circle($cx, $cy, $r, 0, 360, $obstyle, array(), array(), 8);
  23386. }
  23387. $this->StopTransform();
  23388. }
  23389. break;
  23390. }
  23391. case 'ellipse': {
  23392. if ($invisible) {
  23393. break;
  23394. }
  23395. $rx = (isset($attribs['rx']) ? $this->getHTMLUnitToUnits($attribs['rx'], 0, $this->svgunit, false) : 0);
  23396. $ry = (isset($attribs['ry']) ? $this->getHTMLUnitToUnits($attribs['ry'], 0, $this->svgunit, false) : 0);
  23397. $cx = (isset($attribs['cx']) ? $this->getHTMLUnitToUnits($attribs['cx'], 0, $this->svgunit, false) : (isset($attribs['x']) ? $this->getHTMLUnitToUnits($attribs['x'], 0, $this->svgunit, false) : 0));
  23398. $cy = (isset($attribs['cy']) ? $this->getHTMLUnitToUnits($attribs['cy'], 0, $this->svgunit, false) : (isset($attribs['y']) ? $this->getHTMLUnitToUnits($attribs['y'], 0, $this->svgunit, false) : 0));
  23399. $x = ($cx - $rx);
  23400. $y = ($cy - $ry);
  23401. $w = (2 * $rx);
  23402. $h = (2 * $ry);
  23403. if ($clipping) {
  23404. $this->SVGTransform($tm);
  23405. $this->Ellipse($cx, $cy, $rx, $ry, 0, 0, 360, 'CNZ', array(), array(), 8);
  23406. } else {
  23407. $this->StartTransform();
  23408. $this->SVGTransform($tm);
  23409. $obstyle = $this->setSVGStyles($svgstyle, $prev_svgstyle, $x, $y, $w, $h, 'Ellipse', array($cx, $cy, $rx, $ry, 0, 0, 360, 'CNZ'));
  23410. if (!empty($obstyle)) {
  23411. $this->Ellipse($cx, $cy, $rx, $ry, 0, 0, 360, $obstyle, array(), array(), 8);
  23412. }
  23413. $this->StopTransform();
  23414. }
  23415. break;
  23416. }
  23417. case 'line': {
  23418. if ($invisible) {
  23419. break;
  23420. }
  23421. $x1 = (isset($attribs['x1'])?$this->getHTMLUnitToUnits($attribs['x1'], 0, $this->svgunit, false):0);
  23422. $y1 = (isset($attribs['y1'])?$this->getHTMLUnitToUnits($attribs['y1'], 0, $this->svgunit, false):0);
  23423. $x2 = (isset($attribs['x2'])?$this->getHTMLUnitToUnits($attribs['x2'], 0, $this->svgunit, false):0);
  23424. $y2 = (isset($attribs['y2'])?$this->getHTMLUnitToUnits($attribs['y2'], 0, $this->svgunit, false):0);
  23425. $x = $x1;
  23426. $y = $y1;
  23427. $w = abs($x2 - $x1);
  23428. $h = abs($y2 - $y1);
  23429. if (!$clipping) {
  23430. $this->StartTransform();
  23431. $this->SVGTransform($tm);
  23432. $obstyle = $this->setSVGStyles($svgstyle, $prev_svgstyle, $x, $y, $w, $h, 'Line', array($x1, $y1, $x2, $y2));
  23433. $this->Line($x1, $y1, $x2, $y2);
  23434. $this->StopTransform();
  23435. }
  23436. break;
  23437. }
  23438. case 'polyline':
  23439. case 'polygon': {
  23440. if ($invisible) {
  23441. break;
  23442. }
  23443. $points = (isset($attribs['points'])?$attribs['points']:'0 0');
  23444. $points = trim($points);
  23445. // note that point may use a complex syntax not covered here
  23446. $points = preg_split('/[\,\s]+/si', $points);
  23447. if (count($points) < 4) {
  23448. break;
  23449. }
  23450. $p = array();
  23451. $xmin = 2147483647;
  23452. $xmax = 0;
  23453. $ymin = 2147483647;
  23454. $ymax = 0;
  23455. foreach ($points as $key => $val) {
  23456. $p[$key] = $this->getHTMLUnitToUnits($val, 0, $this->svgunit, false);
  23457. if (($key % 2) == 0) {
  23458. // X coordinate
  23459. $xmin = min($xmin, $p[$key]);
  23460. $xmax = max($xmax, $p[$key]);
  23461. } else {
  23462. // Y coordinate
  23463. $ymin = min($ymin, $p[$key]);
  23464. $ymax = max($ymax, $p[$key]);
  23465. }
  23466. }
  23467. $x = $xmin;
  23468. $y = $ymin;
  23469. $w = ($xmax - $xmin);
  23470. $h = ($ymax - $ymin);
  23471. if ($name == 'polyline') {
  23472. $this->StartTransform();
  23473. $this->SVGTransform($tm);
  23474. $obstyle = $this->setSVGStyles($svgstyle, $prev_svgstyle, $x, $y, $w, $h, 'PolyLine', array($p, 'CNZ'));
  23475. if (!empty($obstyle)) {
  23476. $this->PolyLine($p, $obstyle, array(), array());
  23477. }
  23478. $this->StopTransform();
  23479. } else { // polygon
  23480. if ($clipping) {
  23481. $this->SVGTransform($tm);
  23482. $this->Polygon($p, 'CNZ', array(), array(), true);
  23483. } else {
  23484. $this->StartTransform();
  23485. $this->SVGTransform($tm);
  23486. $obstyle = $this->setSVGStyles($svgstyle, $prev_svgstyle, $x, $y, $w, $h, 'Polygon', array($p, 'CNZ'));
  23487. if (!empty($obstyle)) {
  23488. $this->Polygon($p, $obstyle, array(), array(), true);
  23489. }
  23490. $this->StopTransform();
  23491. }
  23492. }
  23493. break;
  23494. }
  23495. // image
  23496. case 'image': {
  23497. if ($invisible) {
  23498. break;
  23499. }
  23500. if (!isset($attribs['xlink:href']) OR empty($attribs['xlink:href'])) {
  23501. break;
  23502. }
  23503. $x = (isset($attribs['x'])?$this->getHTMLUnitToUnits($attribs['x'], 0, $this->svgunit, false):0);
  23504. $y = (isset($attribs['y'])?$this->getHTMLUnitToUnits($attribs['y'], 0, $this->svgunit, false):0);
  23505. $w = (isset($attribs['width'])?$this->getHTMLUnitToUnits($attribs['width'], 0, $this->svgunit, false):0);
  23506. $h = (isset($attribs['height'])?$this->getHTMLUnitToUnits($attribs['height'], 0, $this->svgunit, false):0);
  23507. $img = $attribs['xlink:href'];
  23508. if (!$clipping) {
  23509. $this->StartTransform();
  23510. $this->SVGTransform($tm);
  23511. $obstyle = $this->setSVGStyles($svgstyle, $prev_svgstyle, $x, $y, $w, $h);
  23512. if (preg_match('/^data:image\/[^;]+;base64,/', $img, $m) > 0) {
  23513. // embedded image encoded as base64
  23514. $img = '@'.base64_decode(substr($img, strlen($m[0])));
  23515. } else {
  23516. // fix image path
  23517. if (!TCPDF_STATIC::empty_string($this->svgdir) AND (($img[0] == '.') OR (basename($img) == $img))) {
  23518. // replace relative path with full server path
  23519. $img = $this->svgdir.'/'.$img;
  23520. }
  23521. if (($img[0] == '/') AND !empty($_SERVER['DOCUMENT_ROOT']) AND ($_SERVER['DOCUMENT_ROOT'] != '/')) {
  23522. $findroot = strpos($img, $_SERVER['DOCUMENT_ROOT']);
  23523. if (($findroot === false) OR ($findroot > 1)) {
  23524. if (substr($_SERVER['DOCUMENT_ROOT'], -1) == '/') {
  23525. $img = substr($_SERVER['DOCUMENT_ROOT'], 0, -1).$img;
  23526. } else {
  23527. $img = $_SERVER['DOCUMENT_ROOT'].$img;
  23528. }
  23529. }
  23530. }
  23531. $img = urldecode($img);
  23532. $testscrtype = @parse_url($img);
  23533. if (!isset($testscrtype['query']) OR empty($testscrtype['query'])) {
  23534. // convert URL to server path
  23535. $img = str_replace(K_PATH_URL, K_PATH_MAIN, $img);
  23536. }
  23537. }
  23538. // get image type
  23539. $imgtype = TCPDF_IMAGES::getImageFileType($img);
  23540. if (($imgtype == 'eps') OR ($imgtype == 'ai')) {
  23541. $this->ImageEps($img, $x, $y, $w, $h);
  23542. } elseif ($imgtype == 'svg') {
  23543. // store SVG vars
  23544. $svggradients = $this->svggradients;
  23545. $svggradientid = $this->svggradientid;
  23546. $svgdefsmode = $this->svgdefsmode;
  23547. $svgdefs = $this->svgdefs;
  23548. $svgclipmode = $this->svgclipmode;
  23549. $svgclippaths = $this->svgclippaths;
  23550. $svgcliptm = $this->svgcliptm;
  23551. $svgclipid = $this->svgclipid;
  23552. $svgtext = $this->svgtext;
  23553. $svgtextmode = $this->svgtextmode;
  23554. $this->ImageSVG($img, $x, $y, $w, $h);
  23555. // restore SVG vars
  23556. $this->svggradients = $svggradients;
  23557. $this->svggradientid = $svggradientid;
  23558. $this->svgdefsmode = $svgdefsmode;
  23559. $this->svgdefs = $svgdefs;
  23560. $this->svgclipmode = $svgclipmode;
  23561. $this->svgclippaths = $svgclippaths;
  23562. $this->svgcliptm = $svgcliptm;
  23563. $this->svgclipid = $svgclipid;
  23564. $this->svgtext = $svgtext;
  23565. $this->svgtextmode = $svgtextmode;
  23566. } else {
  23567. $this->Image($img, $x, $y, $w, $h);
  23568. }
  23569. $this->StopTransform();
  23570. }
  23571. break;
  23572. }
  23573. // text
  23574. case 'text':
  23575. case 'tspan': {
  23576. if (isset($this->svgtextmode['text-anchor']) AND !empty($this->svgtext)) {
  23577. // @TODO: unsupported feature
  23578. }
  23579. // only basic support - advanced features must be implemented
  23580. $this->svgtextmode['invisible'] = $invisible;
  23581. if ($invisible) {
  23582. break;
  23583. }
  23584. array_push($this->svgstyles, $svgstyle);
  23585. if (isset($attribs['x'])) {
  23586. $x = $this->getHTMLUnitToUnits($attribs['x'], 0, $this->svgunit, false);
  23587. } elseif ($name == 'tspan') {
  23588. $x = $this->x;
  23589. } else {
  23590. $x = 0;
  23591. }
  23592. if (isset($attribs['dx'])) {
  23593. $x += $this->getHTMLUnitToUnits($attribs['dx'], 0, $this->svgunit, false);
  23594. }
  23595. if (isset($attribs['y'])) {
  23596. $y = $this->getHTMLUnitToUnits($attribs['y'], 0, $this->svgunit, false);
  23597. } elseif ($name == 'tspan') {
  23598. $y = $this->y;
  23599. } else {
  23600. $y = 0;
  23601. }
  23602. if (isset($attribs['dy'])) {
  23603. $y += $this->getHTMLUnitToUnits($attribs['dy'], 0, $this->svgunit, false);
  23604. }
  23605. $svgstyle['text-color'] = $svgstyle['fill'];
  23606. $this->svgtext = '';
  23607. if (isset($svgstyle['text-anchor'])) {
  23608. $this->svgtextmode['text-anchor'] = $svgstyle['text-anchor'];
  23609. } else {
  23610. $this->svgtextmode['text-anchor'] = 'start';
  23611. }
  23612. if (isset($svgstyle['direction'])) {
  23613. if ($svgstyle['direction'] == 'rtl') {
  23614. $this->svgtextmode['rtl'] = true;
  23615. } else {
  23616. $this->svgtextmode['rtl'] = false;
  23617. }
  23618. } else {
  23619. $this->svgtextmode['rtl'] = false;
  23620. }
  23621. if (isset($svgstyle['stroke']) AND ($svgstyle['stroke'] != 'none') AND isset($svgstyle['stroke-width']) AND ($svgstyle['stroke-width'] > 0)) {
  23622. $this->svgtextmode['stroke'] = $this->getHTMLUnitToUnits($svgstyle['stroke-width'], 0, $this->svgunit, false);
  23623. } else {
  23624. $this->svgtextmode['stroke'] = false;
  23625. }
  23626. $this->StartTransform();
  23627. $this->SVGTransform($tm);
  23628. $obstyle = $this->setSVGStyles($svgstyle, $prev_svgstyle, $x, $y, 1, 1);
  23629. $this->x = $x;
  23630. $this->y = $y;
  23631. break;
  23632. }
  23633. // use
  23634. case 'use': {
  23635. if (isset($attribs['xlink:href']) AND !empty($attribs['xlink:href'])) {
  23636. $svgdefid = substr($attribs['xlink:href'], 1);
  23637. if (isset($this->svgdefs[$svgdefid])) {
  23638. $use = $this->svgdefs[$svgdefid];
  23639. if (isset($attribs['xlink:href'])) {
  23640. unset($attribs['xlink:href']);
  23641. }
  23642. if (isset($attribs['id'])) {
  23643. unset($attribs['id']);
  23644. }
  23645. if (isset($use['attribs']['x']) AND isset($attribs['x'])) {
  23646. $attribs['x'] += $use['attribs']['x'];
  23647. }
  23648. if (isset($use['attribs']['y']) AND isset($attribs['y'])) {
  23649. $attribs['y'] += $use['attribs']['y'];
  23650. }
  23651. if (empty($attribs['style'])) {
  23652. $attribs['style'] = '';
  23653. }
  23654. if (!empty($use['attribs']['style'])) {
  23655. // merge styles
  23656. $attribs['style'] = str_replace(';;',';',';'.$use['attribs']['style'].$attribs['style']);
  23657. }
  23658. $attribs = array_merge($use['attribs'], $attribs);
  23659. $this->startSVGElementHandler($parser, $use['name'], $attribs);
  23660. return;
  23661. }
  23662. }
  23663. break;
  23664. }
  23665. default: {
  23666. break;
  23667. }
  23668. } // end of switch
  23669. // process child elements
  23670. if (!empty($attribs['child_elements'])) {
  23671. $child_elements = $attribs['child_elements'];
  23672. unset($attribs['child_elements']);
  23673. foreach($child_elements as $child_element) {
  23674. if (empty($child_element['attribs']['closing_tag'])) {
  23675. $this->startSVGElementHandler('child-tag', $child_element['name'], $child_element['attribs']);
  23676. } else {
  23677. if (isset($child_element['attribs']['content'])) {
  23678. $this->svgtext = $child_element['attribs']['content'];
  23679. }
  23680. $this->endSVGElementHandler('child-tag', $child_element['name']);
  23681. }
  23682. }
  23683. }
  23684. }
  23685. /**
  23686. * Sets the closing SVG element handler function for the XML parser.
  23687. * @param $parser (resource) The first parameter, parser, is a reference to the XML parser calling the handler.
  23688. * @param $name (string) 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.
  23689. * @author Nicola Asuni
  23690. * @since 5.0.000 (2010-05-02)
  23691. * @protected
  23692. */
  23693. protected function endSVGElementHandler($parser, $name) {
  23694. $name = $this->removeTagNamespace($name);
  23695. if ($this->svgdefsmode AND !in_array($name, array('defs', 'clipPath', 'linearGradient', 'radialGradient', 'stop'))) {;
  23696. if (end($this->svgdefs) !== FALSE) {
  23697. $last_svgdefs_id = key($this->svgdefs);
  23698. if (isset($this->svgdefs[$last_svgdefs_id]['attribs']['child_elements'])) {
  23699. foreach($this->svgdefs[$last_svgdefs_id]['attribs']['child_elements'] as $child_element) {
  23700. if (isset($child_element['attribs']['id']) AND ($child_element['name'] == $name)) {
  23701. $this->svgdefs[$last_svgdefs_id]['attribs']['child_elements'][$child_element['attribs']['id'].'_CLOSE'] = array('name' => $name, 'attribs' => array('closing_tag' => TRUE, 'content' => $this->svgtext));
  23702. return;
  23703. }
  23704. }
  23705. if ($this->svgdefs[$last_svgdefs_id]['name'] == $name) {
  23706. $this->svgdefs[$last_svgdefs_id]['attribs']['child_elements'][$last_svgdefs_id.'_CLOSE'] = array('name' => $name, 'attribs' => array('closing_tag' => TRUE, 'content' => $this->svgtext));
  23707. return;
  23708. }
  23709. }
  23710. }
  23711. return;
  23712. }
  23713. switch($name) {
  23714. case 'defs': {
  23715. $this->svgdefsmode = false;
  23716. break;
  23717. }
  23718. // clipPath
  23719. case 'clipPath': {
  23720. $this->svgclipmode = false;
  23721. break;
  23722. }
  23723. case 'svg': {
  23724. if (--$this->svg_tag_depth <= 0) {
  23725. break;
  23726. }
  23727. }
  23728. case 'g': {
  23729. // ungroup: remove last style from array
  23730. array_pop($this->svgstyles);
  23731. $this->StopTransform();
  23732. break;
  23733. }
  23734. case 'text':
  23735. case 'tspan': {
  23736. if ($this->svgtextmode['invisible']) {
  23737. // This implementation must be fixed to following the rule:
  23738. // 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.
  23739. break;
  23740. }
  23741. // print text
  23742. $text = $this->svgtext;
  23743. //$text = $this->stringTrim($text);
  23744. $textlen = $this->GetStringWidth($text);
  23745. if ($this->svgtextmode['text-anchor'] != 'start') {
  23746. // check if string is RTL text
  23747. if ($this->svgtextmode['text-anchor'] == 'end') {
  23748. if ($this->svgtextmode['rtl']) {
  23749. $this->x += $textlen;
  23750. } else {
  23751. $this->x -= $textlen;
  23752. }
  23753. } elseif ($this->svgtextmode['text-anchor'] == 'middle') {
  23754. if ($this->svgtextmode['rtl']) {
  23755. $this->x += ($textlen / 2);
  23756. } else {
  23757. $this->x -= ($textlen / 2);
  23758. }
  23759. }
  23760. }
  23761. $textrendermode = $this->textrendermode;
  23762. $textstrokewidth = $this->textstrokewidth;
  23763. $this->setTextRenderingMode($this->svgtextmode['stroke'], true, false);
  23764. if ($name == 'text') {
  23765. // store current coordinates
  23766. $tmpx = $this->x;
  23767. $tmpy = $this->y;
  23768. }
  23769. // print the text
  23770. $this->Cell($textlen, 0, $text, 0, 0, '', false, '', 0, false, 'L', 'T');
  23771. if ($name == 'text') {
  23772. // restore coordinates
  23773. $this->x = $tmpx;
  23774. $this->y = $tmpy;
  23775. }
  23776. // restore previous rendering mode
  23777. $this->textrendermode = $textrendermode;
  23778. $this->textstrokewidth = $textstrokewidth;
  23779. $this->svgtext = '';
  23780. $this->StopTransform();
  23781. if (!$this->svgdefsmode) {
  23782. array_pop($this->svgstyles);
  23783. }
  23784. break;
  23785. }
  23786. default: {
  23787. break;
  23788. }
  23789. }
  23790. }
  23791. /**
  23792. * Sets the character data handler function for the XML parser.
  23793. * @param $parser (resource) The first parameter, parser, is a reference to the XML parser calling the handler.
  23794. * @param $data (string) The second parameter, data, contains the character data as a string.
  23795. * @author Nicola Asuni
  23796. * @since 5.0.000 (2010-05-02)
  23797. * @protected
  23798. */
  23799. protected function segSVGContentHandler($parser, $data) {
  23800. $this->svgtext .= $data;
  23801. }
  23802. // --- END SVG METHODS -----------------------------------------------------
  23803. } // END OF TCPDF CLASS
  23804. //============================================================+
  23805. // END OF FILE
  23806. //============================================================+