tcpdf.php 1023 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390253912539225393253942539525396253972539825399254002540125402254032540425405254062540725408254092541025411254122541325414254152541625417254182541925420254212542225423254242542525426254272542825429254302543125432254332543425435254362543725438254392544025441254422544325444254452544625447254482544925450254512545225453254542545525456254572545825459254602546125462254632546425465254662546725468254692547025471254722547325474254752547625477254782547925480254812548225483254842548525486254872548825489254902549125492254932549425495254962549725498254992550025501255022550325504255052550625507255082550925510255112551225513255142551525516255172551825519255202552125522255232552425525255262552725528255292553025531255322553325534255352553625537255382553925540255412554225543255442554525546255472554825549255502555125552255532555425555255562555725558255592556025561255622556325564255652556625567255682556925570255712557225573255742557525576255772557825579255802558125582255832558425585255862558725588255892559025591255922559325594255952559625597255982559925600256012560225603256042560525606256072560825609256102561125612256132561425615256162561725618256192562025621256222562325624256252562625627256282562925630256312563225633256342563525636256372563825639256402564125642256432564425645256462564725648256492565025651256522565325654256552565625657256582565925660256612566225663256642566525666256672566825669256702567125672256732567425675256762567725678256792568025681256822568325684256852568625687256882568925690256912569225693256942569525696256972569825699257002570125702257032570425705257062570725708257092571025711257122571325714257152571625717257182571925720257212572225723257242572525726257272572825729257302573125732257332573425735257362573725738257392574025741257422574325744257452574625747257482574925750257512575225753257542575525756257572575825759257602576125762257632576425765257662576725768257692577025771257722577325774257752577625777257782577925780257812578225783257842578525786257872578825789257902579125792257932579425795257962579725798257992580025801258022580325804258052580625807258082580925810258112581225813258142581525816258172581825819258202582125822258232582425825258262582725828258292583025831258322583325834258352583625837258382583925840258412584225843258442584525846258472584825849258502585125852258532585425855258562585725858258592586025861258622586325864258652586625867258682586925870258712587225873258742587525876258772587825879258802588125882258832588425885258862588725888258892589025891258922589325894258952589625897258982589925900259012590225903259042590525906259072590825909259102591125912259132591425915259162591725918259192592025921259222592325924259252592625927259282592925930259312593225933259342593525936259372593825939259402594125942259432594425945259462594725948259492595025951259522595325954259552595625957259582595925960259612596225963259642596525966259672596825969259702597125972259732597425975259762597725978259792598025981259822598325984259852598625987259882598925990259912599225993259942599525996259972599825999260002600126002260032600426005260062600726008260092601026011260122601326014260152601626017260182601926020260212602226023260242602526026260272602826029260302603126032260332603426035260362603726038260392604026041260422604326044260452604626047260482604926050260512605226053260542605526056260572605826059260602606126062260632606426065260662606726068260692607026071260722607326074260752607626077260782607926080260812608226083260842608526086260872608826089260902609126092260932609426095260962609726098260992610026101261022610326104261052610626107261082610926110261112611226113261142611526116261172611826119261202612126122261232612426125261262612726128261292613026131261322613326134261352613626137261382613926140261412614226143261442614526146261472614826149261502615126152261532615426155261562615726158261592616026161261622616326164261652616626167261682616926170261712617226173261742617526176261772617826179261802618126182261832618426185261862618726188261892619026191261922619326194261952619626197261982619926200262012620226203262042620526206262072620826209262102621126212262132621426215262162621726218262192622026221262222622326224262252622626227262282622926230262312623226233262342623526236262372623826239262402624126242262432624426245262462624726248262492625026251262522625326254262552625626257262582625926260262612626226263262642626526266262672626826269262702627126272262732627426275262762627726278262792628026281262822628326284262852628626287262882628926290262912629226293262942629526296262972629826299263002630126302263032630426305263062630726308263092631026311263122631326314263152631626317263182631926320263212632226323263242632526326263272632826329263302633126332263332633426335263362633726338263392634026341263422634326344263452634626347263482634926350263512635226353263542635526356263572635826359263602636126362263632636426365263662636726368263692637026371263722637326374263752637626377263782637926380263812638226383263842638526386263872638826389263902639126392263932639426395263962639726398263992640026401264022640326404264052640626407264082640926410264112641226413264142641526416264172641826419264202642126422264232642426425264262642726428264292643026431264322643326434264352643626437264382643926440264412644226443264442644526446264472644826449264502645126452264532645426455264562645726458264592646026461264622646326464264652646626467264682646926470264712647226473264742647526476264772647826479264802648126482264832648426485264862648726488264892649026491264922649326494264952649626497264982649926500265012650226503265042650526506265072650826509265102651126512265132651426515265162651726518265192652026521265222652326524265252652626527265282652926530265312653226533265342653526536265372653826539265402654126542265432654426545265462654726548265492655026551265522655326554265552655626557265582655926560265612656226563265642656526566265672656826569265702657126572265732657426575265762657726578265792658026581265822658326584265852658626587265882658926590265912659226593265942659526596265972659826599266002660126602266032660426605266062660726608266092661026611266122661326614266152661626617266182661926620266212662226623266242662526626266272662826629266302663126632266332663426635266362663726638266392664026641266422664326644266452664626647266482664926650266512665226653266542665526656266572665826659266602666126662266632666426665266662666726668266692667026671266722667326674266752667626677266782667926680266812668226683266842668526686266872668826689266902669126692266932669426695266962669726698266992670026701267022670326704267052670626707267082670926710267112671226713267142671526716267172671826719267202672126722267232672426725267262672726728267292673026731267322673326734267352673626737267382673926740267412674226743267442674526746267472674826749267502675126752267532675426755267562675726758267592676026761267622676326764267652676626767267682676926770267712677226773267742677526776267772677826779267802678126782267832678426785267862678726788267892679026791267922679326794267952679626797267982679926800268012680226803268042680526806268072680826809268102681126812268132681426815268162681726818268192682026821268222682326824268252682626827268282682926830268312683226833268342683526836268372683826839268402684126842268432684426845268462684726848268492685026851268522685326854268552685626857268582685926860268612686226863268642686526866268672686826869268702687126872268732687426875268762687726878268792688026881268822688326884268852688626887268882688926890268912689226893268942689526896268972689826899269002690126902269032690426905269062690726908269092691026911269122691326914269152691626917269182691926920269212692226923269242692526926269272692826929269302693126932269332693426935269362693726938269392694026941269422694326944269452694626947269482694926950269512695226953269542695526956269572695826959269602696126962269632696426965269662696726968269692697026971269722697326974269752697626977269782697926980269812698226983269842698526986269872698826989269902699126992269932699426995269962699726998269992700027001270022700327004270052700627007270082700927010270112701227013270142701527016270172701827019270202702127022270232702427025270262702727028270292703027031270322703327034270352703627037270382703927040270412704227043270442704527046270472704827049270502705127052270532705427055270562705727058270592706027061270622706327064270652706627067270682706927070270712707227073270742707527076270772707827079270802708127082270832708427085270862708727088270892709027091270922709327094270952709627097270982709927100271012710227103271042710527106271072710827109271102711127112271132711427115271162711727118271192712027121271222712327124271252712627127271282712927130271312713227133271342713527136271372713827139271402714127142271432714427145271462714727148271492715027151271522715327154271552715627157271582715927160271612716227163271642716527166271672716827169271702717127172271732717427175271762717727178271792718027181271822718327184271852718627187271882718927190271912719227193271942719527196271972719827199272002720127202272032720427205272062720727208272092721027211272122721327214272152721627217272182721927220272212722227223272242722527226272272722827229272302723127232272332723427235272362723727238272392724027241272422724327244272452724627247272482724927250272512725227253272542725527256272572725827259272602726127262272632726427265272662726727268272692727027271272722727327274272752727627277272782727927280272812728227283272842728527286272872728827289272902729127292272932729427295272962729727298272992730027301273022730327304273052730627307273082730927310273112731227313273142731527316273172731827319273202732127322273232732427325273262732727328273292733027331273322733327334273352733627337273382733927340273412734227343273442734527346273472734827349273502735127352273532735427355273562735727358273592736027361273622736327364273652736627367273682736927370273712737227373273742737527376273772737827379273802738127382273832738427385273862738727388273892739027391273922739327394273952739627397273982739927400274012740227403274042740527406274072740827409274102741127412274132741427415274162741727418274192742027421274222742327424274252742627427274282742927430274312743227433274342743527436274372743827439274402744127442274432744427445274462744727448274492745027451274522745327454274552745627457274582745927460274612746227463274642746527466274672746827469274702747127472274732747427475274762747727478274792748027481274822748327484274852748627487274882748927490274912749227493274942749527496274972749827499275002750127502275032750427505275062750727508275092751027511275122751327514275152751627517275182751927520275212752227523275242752527526275272752827529275302753127532275332753427535275362753727538275392754027541275422754327544275452754627547275482754927550275512755227553275542755527556275572755827559275602756127562275632756427565275662756727568275692757027571275722757327574275752757627577275782757927580275812758227583275842758527586275872758827589275902759127592275932759427595275962759727598275992760027601276022760327604276052760627607276082760927610276112761227613276142761527616276172761827619276202762127622276232762427625276262762727628276292763027631276322763327634276352763627637276382763927640276412764227643276442764527646276472764827649276502765127652276532765427655276562765727658276592766027661276622766327664276652766627667276682766927670276712767227673276742767527676276772767827679276802768127682276832768427685276862768727688276892769027691276922769327694276952769627697276982769927700277012770227703277042770527706277072770827709277102771127712277132771427715277162771727718277192772027721277222772327724277252772627727277282772927730277312773227733277342773527736277372773827739277402774127742277432774427745277462774727748277492775027751277522775327754277552775627757277582775927760277612776227763277642776527766277672776827769277702777127772277732777427775277762777727778277792778027781277822778327784277852778627787277882778927790277912779227793277942779527796277972779827799278002780127802278032780427805278062780727808278092781027811278122781327814278152781627817278182781927820278212782227823278242782527826278272782827829278302783127832278332783427835278362783727838278392784027841278422784327844278452784627847278482784927850278512785227853278542785527856278572785827859278602786127862278632786427865278662786727868278692787027871278722787327874278752787627877278782787927880278812788227883278842788527886278872788827889278902789127892278932789427895278962789727898278992790027901279022790327904279052790627907279082790927910279112791227913279142791527916279172791827919279202792127922279232792427925279262792727928279292793027931279322793327934279352793627937279382793927940279412794227943279442794527946279472794827949279502795127952279532795427955279562795727958279592796027961279622796327964279652796627967279682796927970279712797227973279742797527976279772797827979279802798127982279832798427985279862798727988279892799027991279922799327994279952799627997279982799928000280012800228003280042800528006280072800828009280102801128012280132801428015280162801728018280192802028021280222802328024280252802628027280282802928030280312803228033280342803528036280372803828039280402804128042280432804428045280462804728048280492805028051280522805328054280552805628057280582805928060280612806228063280642806528066280672806828069280702807128072280732807428075280762807728078280792808028081280822808328084280852808628087280882808928090280912809228093280942809528096280972809828099281002810128102281032810428105281062810728108281092811028111281122811328114281152811628117281182811928120281212812228123281242812528126281272812828129281302813128132281332813428135281362813728138281392814028141281422814328144281452814628147281482814928150281512815228153281542815528156281572815828159281602816128162281632816428165281662816728168281692817028171281722817328174281752817628177281782817928180281812818228183281842818528186281872818828189281902819128192281932819428195281962819728198281992820028201282022820328204282052820628207282082820928210282112821228213282142821528216282172821828219282202822128222282232822428225282262822728228282292823028231282322823328234282352823628237282382823928240282412824228243282442824528246282472824828249282502825128252282532825428255282562825728258282592826028261282622826328264282652826628267282682826928270282712827228273282742827528276282772827828279282802828128282282832828428285282862828728288282892829028291282922829328294282952829628297282982829928300283012830228303283042830528306283072830828309283102831128312283132831428315283162831728318283192832028321283222832328324283252832628327283282832928330283312833228333283342833528336283372833828339283402834128342283432834428345283462834728348283492835028351283522835328354283552835628357283582835928360283612836228363283642836528366283672836828369283702837128372283732837428375283762837728378283792838028381283822838328384283852838628387283882838928390283912839228393283942839528396283972839828399284002840128402284032840428405284062840728408284092841028411284122841328414284152841628417284182841928420284212842228423284242842528426284272842828429284302843128432284332843428435284362843728438284392844028441284422844328444284452844628447284482844928450284512845228453284542845528456284572845828459284602846128462284632846428465284662846728468284692847028471284722847328474284752847628477284782847928480284812848228483284842848528486284872848828489284902849128492284932849428495284962849728498284992850028501285022850328504285052850628507285082850928510285112851228513285142851528516285172851828519285202852128522285232852428525285262852728528285292853028531285322853328534285352853628537285382853928540285412854228543285442854528546285472854828549285502855128552285532855428555285562855728558285592856028561285622856328564285652856628567285682856928570285712857228573285742857528576285772857828579285802858128582285832858428585285862858728588285892859028591285922859328594285952859628597285982859928600286012860228603286042860528606286072860828609286102861128612286132861428615286162861728618286192862028621286222862328624286252862628627286282862928630286312863228633286342863528636286372863828639286402864128642286432864428645286462864728648286492865028651286522865328654286552865628657286582865928660286612866228663286642866528666286672866828669286702867128672286732867428675286762867728678286792868028681286822868328684286852868628687286882868928690286912869228693286942869528696286972869828699287002870128702287032870428705287062870728708287092871028711287122871328714287152871628717287182871928720287212872228723287242872528726287272872828729287302873128732287332873428735287362873728738287392874028741287422874328744287452874628747
  1. <?php
  2. //============================================================+
  3. // File name : tcpdf.php
  4. // Version : 5.9.134
  5. // Begin : 2002-08-03
  6. // Last Update : 2011-10-29
  7. // Author : Nicola Asuni - Tecnick.com S.r.l - Via Della Pace, 11 - 09044 - Quartucciu (CA) - ITALY - www.tecnick.com - info@tecnick.com
  8. // License : http://www.tecnick.com/pagefiles/tcpdf/LICENSE.TXT GNU-LGPLv3 + YOU CAN'T REMOVE ANY TCPDF COPYRIGHT NOTICE OR LINK FROM THE GENERATED PDF DOCUMENTS.
  9. // -------------------------------------------------------------------
  10. //
  11. // This file is part of TCPDF software library.
  12. //
  13. // TCPDF is free software: you can redistribute it and/or modify it
  14. // under the terms of the GNU Lesser General Public License as
  15. // published by the Free Software Foundation, either version 3 of the
  16. // License, or (at your option) any later version. Additionally,
  17. // YOU CAN'T REMOVE ANY TCPDF COPYRIGHT NOTICE OR LINK FROM THE
  18. // GENERATED PDF DOCUMENTS.
  19. //
  20. // TCPDF is distributed in the hope that it will be useful, but
  21. // WITHOUT ANY WARRANTY; without even the implied warranty of
  22. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  23. // See the GNU Lesser General Public License for more details.
  24. //
  25. // You should have received a copy of the License
  26. // along with TCPDF. If not, see
  27. // <http://www.tecnick.com/pagefiles/tcpdf/LICENSE.TXT>.
  28. //
  29. // See LICENSE.TXT file for more information.
  30. // -------------------------------------------------------------------
  31. //
  32. // Description :
  33. // This is a PHP class for generating PDF documents without requiring external extensions.
  34. //
  35. // NOTE:
  36. // This class was originally derived in 2002 from the Public
  37. // Domain FPDF class by Olivier Plathey (http://www.fpdf.org),
  38. // but now is almost entirely rewritten and contains thousands of
  39. // new lines of code and hundreds new features.
  40. //
  41. // Main features:
  42. // * no external libraries are required for the basic functions;
  43. // * all standard page formats, custom page formats, custom margins and units of measure;
  44. // * UTF-8 Unicode and Right-To-Left languages;
  45. // * TrueTypeUnicode, TrueType, Type1 and CID-0 fonts;
  46. // * font subsetting;
  47. // * methods to publish some XHTML + CSS code, Javascript and Forms;
  48. // * images, graphic (geometric figures) and transformation methods;
  49. // * 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)
  50. // * 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 Extention, 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;
  51. // * JPEG and PNG ICC profiles, Grayscale, RGB, CMYK, Spot Colors and Transparencies;
  52. // * automatic page header and footer management;
  53. // * document encryption up to 256 bit and digital signature certifications;
  54. // * transactions to UNDO commands;
  55. // * PDF annotations, including links, text and file attachments;
  56. // * text rendering modes (fill, stroke and clipping);
  57. // * multiple columns mode;
  58. // * no-write page regions;
  59. // * bookmarks, named destinations and table of content;
  60. // * text hyphenation;
  61. // * text stretching and spacing (tracking/kerning);
  62. // * automatic page break, line break and text alignments including justification;
  63. // * automatic page numbering and page groups;
  64. // * move and delete pages;
  65. // * page compression (requires php-zlib extension);
  66. // * XOBject Templates;
  67. // * Layers and object visibility.
  68. // * PDF/A-1b support.
  69. //
  70. // -----------------------------------------------------------
  71. // THANKS TO:
  72. //
  73. // Olivier Plathey (http://www.fpdf.org) for original FPDF.
  74. // Efthimios Mavrogeorgiadis (emavro@yahoo.com) for suggestions on RTL language support.
  75. // Klemen Vodopivec (http://www.fpdf.de/downloads/addons/37/) for Encryption algorithm.
  76. // Warren Sherliker (wsherliker@gmail.com) for better image handling.
  77. // dullus for text Justification.
  78. // Bob Vincent (pillarsdotnet@users.sourceforge.net) for <li> value attribute.
  79. // Patrick Benny for text stretch suggestion on Cell().
  80. // Johannes Güntert for JavaScript support.
  81. // Denis Van Nuffelen for Dynamic Form.
  82. // Jacek Czekaj for multibyte justification
  83. // Anthony Ferrara for the reintroduction of legacy image methods.
  84. // Sourceforge user 1707880 (hucste) for line-trough mode.
  85. // Larry Stanbery for page groups.
  86. // Martin Hall-May for transparency.
  87. // Aaron C. Spike for Polycurve method.
  88. // Mohamad Ali Golkar, Saleh AlMatrafe, Charles Abbott for Arabic and Persian support.
  89. // Moritz Wagner and Andreas Wurmser for graphic functions.
  90. // Andrew Whitehead for core fonts support.
  91. // Esteban Joël Marín for OpenType font conversion.
  92. // Teus Hagen for several suggestions and fixes.
  93. // Yukihiro Nakadaira for CID-0 CJK fonts fixes.
  94. // Kosmas Papachristos for some CSS improvements.
  95. // Marcel Partap for some fixes.
  96. // Won Kyu Park for several suggestions, fixes and patches.
  97. // Dominik Dzienia for QR-code support.
  98. // Laurent Minguet for some suggestions.
  99. // Christian Deligant for some suggestions and fixes.
  100. // Anyone that has reported a bug or sent a suggestion.
  101. //============================================================+
  102. /**
  103. * @file
  104. * This is a PHP class for generating PDF documents without requiring external extensions.<br>
  105. * 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>
  106. * <h3>TCPDF main features are:</h3>
  107. * <ul>
  108. * <li>no external libraries are required for the basic functions;</li>
  109. * <li>all standard page formats, custom page formats, custom margins and units of measure;</li>
  110. * <li>UTF-8 Unicode and Right-To-Left languages;</li>
  111. * <li>TrueTypeUnicode, TrueType, Type1 and CID-0 fonts;</li>
  112. * <li>font subsetting;</li>
  113. * <li>methods to publish some XHTML + CSS code, Javascript and Forms;</li>
  114. * <li>images, graphic (geometric figures) and transformation methods;
  115. * <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>
  116. * <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 Extention, 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>
  117. * <li>JPEG and PNG ICC profiles, Grayscale, RGB, CMYK, Spot Colors and Transparencies;</li>
  118. * <li>automatic page header and footer management;</li>
  119. * <li>document encryption up to 256 bit and digital signature certifications;</li>
  120. * <li>transactions to UNDO commands;</li>
  121. * <li>PDF annotations, including links, text and file attachments;</li>
  122. * <li>text rendering modes (fill, stroke and clipping);</li>
  123. * <li>multiple columns mode;</li>
  124. * <li>no-write page regions;</li>
  125. * <li>bookmarks, named destinations and table of content;</li>
  126. * <li>text hyphenation;</li>
  127. * <li>text stretching and spacing (tracking/kerning);</li>
  128. * <li>automatic page break, line break and text alignments including justification;</li>
  129. * <li>automatic page numbering and page groups;</li>
  130. * <li>move and delete pages;</li>
  131. * <li>page compression (requires php-zlib extension);</li>
  132. * <li>XOBject Templates;</li>
  133. * <li>Layers and object visibility;</li>
  134. * <li>PDF/A-1b support.</li>
  135. * </ul>
  136. * Tools to encode your unicode fonts are on fonts/utils directory.</p>
  137. * @package com.tecnick.tcpdf
  138. * @author Nicola Asuni
  139. * @version 5.9.134
  140. */
  141. // Main configuration file. Define the K_TCPDF_EXTERNAL_CONFIG constant to skip this file.
  142. require_once(dirname(__FILE__).'/config/tcpdf_config.php');
  143. /**
  144. * @class TCPDF
  145. * PHP class for generating PDF documents without requiring external extensions.
  146. * 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>
  147. * @package com.tecnick.tcpdf
  148. * @brief PHP class for generating PDF documents without requiring external extensions.
  149. * @version 5.9.134
  150. * @author Nicola Asuni - info@tecnick.com
  151. */
  152. class TCPDF {
  153. // private properties
  154. /**
  155. * Current TCPDF version.
  156. * @private
  157. */
  158. private $tcpdf_version = '5.9.134';
  159. // Protected properties
  160. /**
  161. * Current page number.
  162. * @protected
  163. */
  164. protected $page;
  165. /**
  166. * Current object number.
  167. * @protected
  168. */
  169. protected $n;
  170. /**
  171. * Array of object offsets.
  172. * @protected
  173. */
  174. protected $offsets;
  175. /**
  176. * Buffer holding in-memory PDF.
  177. * @protected
  178. */
  179. protected $buffer;
  180. /**
  181. * Array containing pages.
  182. * @protected
  183. */
  184. protected $pages = array();
  185. /**
  186. * Current document state.
  187. * @protected
  188. */
  189. protected $state;
  190. /**
  191. * Compression flag.
  192. * @protected
  193. */
  194. protected $compress;
  195. /**
  196. * Current page orientation (P = Portrait, L = Landscape).
  197. * @protected
  198. */
  199. protected $CurOrientation;
  200. /**
  201. * Page dimensions.
  202. * @protected
  203. */
  204. protected $pagedim = array();
  205. /**
  206. * Scale factor (number of points in user unit).
  207. * @protected
  208. */
  209. protected $k;
  210. /**
  211. * Width of page format in points.
  212. * @protected
  213. */
  214. protected $fwPt;
  215. /**
  216. * Height of page format in points.
  217. * @protected
  218. */
  219. protected $fhPt;
  220. /**
  221. * Current width of page in points.
  222. * @protected
  223. */
  224. protected $wPt;
  225. /**
  226. * Current height of page in points.
  227. * @protected
  228. */
  229. protected $hPt;
  230. /**
  231. * Current width of page in user unit.
  232. * @protected
  233. */
  234. protected $w;
  235. /**
  236. * Current height of page in user unit.
  237. * @protected
  238. */
  239. protected $h;
  240. /**
  241. * Left margin.
  242. * @protected
  243. */
  244. protected $lMargin;
  245. /**
  246. * Top margin.
  247. * @protected
  248. */
  249. protected $tMargin;
  250. /**
  251. * Right margin.
  252. * @protected
  253. */
  254. protected $rMargin;
  255. /**
  256. * Page break margin.
  257. * @protected
  258. */
  259. protected $bMargin;
  260. /**
  261. * Array of cell internal paddings ('T' => top, 'R' => right, 'B' => bottom, 'L' => left).
  262. * @since 5.9.000 (2010-10-03)
  263. * @protected
  264. */
  265. protected $cell_padding = array('T' => 0, 'R' => 0, 'B' => 0, 'L' => 0);
  266. /**
  267. * Array of cell margins ('T' => top, 'R' => right, 'B' => bottom, 'L' => left).
  268. * @since 5.9.000 (2010-10-04)
  269. * @protected
  270. */
  271. protected $cell_margin = array('T' => 0, 'R' => 0, 'B' => 0, 'L' => 0);
  272. /**
  273. * Current horizontal position in user unit for cell positioning.
  274. * @protected
  275. */
  276. protected $x;
  277. /**
  278. * Current vertical position in user unit for cell positioning.
  279. * @protected
  280. */
  281. protected $y;
  282. /**
  283. * Height of last cell printed.
  284. * @protected
  285. */
  286. protected $lasth;
  287. /**
  288. * Line width in user unit.
  289. * @protected
  290. */
  291. protected $LineWidth;
  292. /**
  293. * Array of standard font names.
  294. * @protected
  295. */
  296. protected $CoreFonts;
  297. /**
  298. * Array of used fonts.
  299. * @protected
  300. */
  301. protected $fonts = array();
  302. /**
  303. * Array of font files.
  304. * @protected
  305. */
  306. protected $FontFiles = array();
  307. /**
  308. * Array of encoding differences.
  309. * @protected
  310. */
  311. protected $diffs = array();
  312. /**
  313. * Array of used images.
  314. * @protected
  315. */
  316. protected $images = array();
  317. /**
  318. * Array of Annotations in pages.
  319. * @protected
  320. */
  321. protected $PageAnnots = array();
  322. /**
  323. * Array of internal links.
  324. * @protected
  325. */
  326. protected $links = array();
  327. /**
  328. * Current font family.
  329. * @protected
  330. */
  331. protected $FontFamily;
  332. /**
  333. * Current font style.
  334. * @protected
  335. */
  336. protected $FontStyle;
  337. /**
  338. * Current font ascent (distance between font top and baseline).
  339. * @protected
  340. * @since 2.8.000 (2007-03-29)
  341. */
  342. protected $FontAscent;
  343. /**
  344. * Current font descent (distance between font bottom and baseline).
  345. * @protected
  346. * @since 2.8.000 (2007-03-29)
  347. */
  348. protected $FontDescent;
  349. /**
  350. * Underlining flag.
  351. * @protected
  352. */
  353. protected $underline;
  354. /**
  355. * Overlining flag.
  356. * @protected
  357. */
  358. protected $overline;
  359. /**
  360. * Current font info.
  361. * @protected
  362. */
  363. protected $CurrentFont;
  364. /**
  365. * Current font size in points.
  366. * @protected
  367. */
  368. protected $FontSizePt;
  369. /**
  370. * Current font size in user unit.
  371. * @protected
  372. */
  373. protected $FontSize;
  374. /**
  375. * Commands for drawing color.
  376. * @protected
  377. */
  378. protected $DrawColor;
  379. /**
  380. * Commands for filling color.
  381. * @protected
  382. */
  383. protected $FillColor;
  384. /**
  385. * Commands for text color.
  386. * @protected
  387. */
  388. protected $TextColor;
  389. /**
  390. * Indicates whether fill and text colors are different.
  391. * @protected
  392. */
  393. protected $ColorFlag;
  394. /**
  395. * Automatic page breaking.
  396. * @protected
  397. */
  398. protected $AutoPageBreak;
  399. /**
  400. * Threshold used to trigger page breaks.
  401. * @protected
  402. */
  403. protected $PageBreakTrigger;
  404. /**
  405. * Flag set when processing page header.
  406. * @protected
  407. */
  408. protected $InHeader = false;
  409. /**
  410. * Flag set when processing page footer.
  411. * @protected
  412. */
  413. protected $InFooter = false;
  414. /**
  415. * Zoom display mode.
  416. * @protected
  417. */
  418. protected $ZoomMode;
  419. /**
  420. * Layout display mode.
  421. * @protected
  422. */
  423. protected $LayoutMode;
  424. /**
  425. * If true set the document information dictionary in Unicode.
  426. * @protected
  427. */
  428. protected $docinfounicode = true;
  429. /**
  430. * Document title.
  431. * @protected
  432. */
  433. protected $title = '';
  434. /**
  435. * Document subject.
  436. * @protected
  437. */
  438. protected $subject = '';
  439. /**
  440. * Document author.
  441. * @protected
  442. */
  443. protected $author = '';
  444. /**
  445. * Document keywords.
  446. * @protected
  447. */
  448. protected $keywords = '';
  449. /**
  450. * Document creator.
  451. * @protected
  452. */
  453. protected $creator = '';
  454. /**
  455. * Starting page number.
  456. * @protected
  457. */
  458. protected $starting_page_number = 1;
  459. /**
  460. * String alias for total number of pages.
  461. * @protected
  462. */
  463. protected $alias_tot_pages = '{:ptp:}';
  464. /**
  465. * String alias for page number.
  466. * @protected
  467. */
  468. protected $alias_num_page = '{:pnp:}';
  469. /**
  470. * String alias for total number of pages in a single group.
  471. * @protected
  472. */
  473. protected $alias_group_tot_pages = '{:ptg:}';
  474. /**
  475. * String alias for group page number.
  476. * @protected
  477. */
  478. protected $alias_group_num_page = '{:png:}';
  479. /**
  480. * String alias for right shift compensation used to correctly align page numbers on the right.
  481. * @protected
  482. */
  483. protected $alias_right_shift = '{rsc:';
  484. /**
  485. * The right-bottom (or left-bottom for RTL) corner X coordinate of last inserted image.
  486. * @since 2002-07-31
  487. * @author Nicola Asuni
  488. * @protected
  489. */
  490. protected $img_rb_x;
  491. /**
  492. * The right-bottom corner Y coordinate of last inserted image.
  493. * @since 2002-07-31
  494. * @author Nicola Asuni
  495. * @protected
  496. */
  497. protected $img_rb_y;
  498. /**
  499. * Adjusting factor to convert pixels to user units.
  500. * @since 2004-06-14
  501. * @author Nicola Asuni
  502. * @protected
  503. */
  504. protected $imgscale = 1;
  505. /**
  506. * Boolean flag set to true when the input text is unicode (require unicode fonts).
  507. * @since 2005-01-02
  508. * @author Nicola Asuni
  509. * @protected
  510. */
  511. protected $isunicode = false;
  512. /**
  513. * Object containing unicode data.
  514. * @since 5.9.004 (2010-10-18)
  515. * @author Nicola Asuni
  516. * @protected
  517. */
  518. protected $unicode;
  519. /**
  520. * Object containing font encoding maps.
  521. * @since 5.9.123 (2011-10-01)
  522. * @author Nicola Asuni
  523. * @protected
  524. */
  525. protected $encmaps;
  526. /**
  527. * PDF version.
  528. * @since 1.5.3
  529. * @protected
  530. */
  531. protected $PDFVersion = '1.7';
  532. /**
  533. * ID of the stored default header template (-1 = not set).
  534. * @protected
  535. */
  536. protected $header_xobjid = -1;
  537. /**
  538. * If true reset the Header Xobject template at each page
  539. * @protected
  540. */
  541. protected $header_xobj_autoreset = false;
  542. /**
  543. * Minimum distance between header and top page margin.
  544. * @protected
  545. */
  546. protected $header_margin;
  547. /**
  548. * Minimum distance between footer and bottom page margin.
  549. * @protected
  550. */
  551. protected $footer_margin;
  552. /**
  553. * Original left margin value.
  554. * @protected
  555. * @since 1.53.0.TC013
  556. */
  557. protected $original_lMargin;
  558. /**
  559. * Original right margin value.
  560. * @protected
  561. * @since 1.53.0.TC013
  562. */
  563. protected $original_rMargin;
  564. /**
  565. * Default font used on page header.
  566. * @protected
  567. */
  568. protected $header_font;
  569. /**
  570. * Default font used on page footer.
  571. * @protected
  572. */
  573. protected $footer_font;
  574. /**
  575. * Language templates.
  576. * @protected
  577. */
  578. protected $l;
  579. /**
  580. * Barcode to print on page footer (only if set).
  581. * @protected
  582. */
  583. protected $barcode = false;
  584. /**
  585. * Boolean flag to print/hide page header.
  586. * @protected
  587. */
  588. protected $print_header = true;
  589. /**
  590. * Boolean flag to print/hide page footer.
  591. * @protected
  592. */
  593. protected $print_footer = true;
  594. /**
  595. * Header image logo.
  596. * @protected
  597. */
  598. protected $header_logo = '';
  599. /**
  600. * Width of header image logo in user units.
  601. * @protected
  602. */
  603. protected $header_logo_width = 30;
  604. /**
  605. * Title to be printed on default page header.
  606. * @protected
  607. */
  608. protected $header_title = '';
  609. /**
  610. * String to pring on page header after title.
  611. * @protected
  612. */
  613. protected $header_string = '';
  614. /**
  615. * Default number of columns for html table.
  616. * @protected
  617. */
  618. protected $default_table_columns = 4;
  619. // variables for html parser
  620. /**
  621. * HTML PARSER: array to store current link and rendering styles.
  622. * @protected
  623. */
  624. protected $HREF = array();
  625. /**
  626. * List of available fonts on filesystem.
  627. * @protected
  628. */
  629. protected $fontlist = array();
  630. /**
  631. * Current foreground color.
  632. * @protected
  633. */
  634. protected $fgcolor;
  635. /**
  636. * HTML PARSER: array of boolean values, true in case of ordered list (OL), false otherwise.
  637. * @protected
  638. */
  639. protected $listordered = array();
  640. /**
  641. * HTML PARSER: array count list items on nested lists.
  642. * @protected
  643. */
  644. protected $listcount = array();
  645. /**
  646. * HTML PARSER: current list nesting level.
  647. * @protected
  648. */
  649. protected $listnum = 0;
  650. /**
  651. * HTML PARSER: indent amount for lists.
  652. * @protected
  653. */
  654. protected $listindent = 0;
  655. /**
  656. * HTML PARSER: current list indententation level.
  657. * @protected
  658. */
  659. protected $listindentlevel = 0;
  660. /**
  661. * Current background color.
  662. * @protected
  663. */
  664. protected $bgcolor;
  665. /**
  666. * Temporary font size in points.
  667. * @protected
  668. */
  669. protected $tempfontsize = 10;
  670. /**
  671. * Spacer string for LI tags.
  672. * @protected
  673. */
  674. protected $lispacer = '';
  675. /**
  676. * Default encoding.
  677. * @protected
  678. * @since 1.53.0.TC010
  679. */
  680. protected $encoding = 'UTF-8';
  681. /**
  682. * PHP internal encoding.
  683. * @protected
  684. * @since 1.53.0.TC016
  685. */
  686. protected $internal_encoding;
  687. /**
  688. * Boolean flag to indicate if the document language is Right-To-Left.
  689. * @protected
  690. * @since 2.0.000
  691. */
  692. protected $rtl = false;
  693. /**
  694. * Boolean flag used to force RTL or LTR string direction.
  695. * @protected
  696. * @since 2.0.000
  697. */
  698. protected $tmprtl = false;
  699. // --- Variables used for document encryption:
  700. /**
  701. * IBoolean flag indicating whether document is protected.
  702. * @protected
  703. * @since 2.0.000 (2008-01-02)
  704. */
  705. protected $encrypted;
  706. /**
  707. * Array containing encryption settings.
  708. * @protected
  709. * @since 5.0.005 (2010-05-11)
  710. */
  711. protected $encryptdata = array();
  712. /**
  713. * Last RC4 key encrypted (cached for optimisation).
  714. * @protected
  715. * @since 2.0.000 (2008-01-02)
  716. */
  717. protected $last_enc_key;
  718. /**
  719. * Last RC4 computed key.
  720. * @protected
  721. * @since 2.0.000 (2008-01-02)
  722. */
  723. protected $last_enc_key_c;
  724. /**
  725. * Encryption padding string.
  726. * @protected
  727. */
  728. protected $enc_padding = "\x28\xBF\x4E\x5E\x4E\x75\x8A\x41\x64\x00\x4E\x56\xFF\xFA\x01\x08\x2E\x2E\x00\xB6\xD0\x68\x3E\x80\x2F\x0C\xA9\xFE\x64\x53\x69\x7A";
  729. /**
  730. * File ID (used on document trailer).
  731. * @protected
  732. * @since 5.0.005 (2010-05-12)
  733. */
  734. protected $file_id;
  735. // --- bookmark ---
  736. /**
  737. * Outlines for bookmark.
  738. * @protected
  739. * @since 2.1.002 (2008-02-12)
  740. */
  741. protected $outlines = array();
  742. /**
  743. * Outline root for bookmark.
  744. * @protected
  745. * @since 2.1.002 (2008-02-12)
  746. */
  747. protected $OutlineRoot;
  748. // --- javascript and form ---
  749. /**
  750. * Javascript code.
  751. * @protected
  752. * @since 2.1.002 (2008-02-12)
  753. */
  754. protected $javascript = '';
  755. /**
  756. * Javascript counter.
  757. * @protected
  758. * @since 2.1.002 (2008-02-12)
  759. */
  760. protected $n_js;
  761. /**
  762. * line trough state
  763. * @protected
  764. * @since 2.8.000 (2008-03-19)
  765. */
  766. protected $linethrough;
  767. /**
  768. * Array with additional document-wide usage rights for the document.
  769. * @protected
  770. * @since 5.8.014 (2010-08-23)
  771. */
  772. protected $ur = array();
  773. /**
  774. * DPI (Dot Per Inch) Document Resolution (do not change).
  775. * @protected
  776. * @since 3.0.000 (2008-03-27)
  777. */
  778. protected $dpi = 72;
  779. /**
  780. * Array of page numbers were a new page group was started (the page numbers are the keys of the array).
  781. * @protected
  782. * @since 3.0.000 (2008-03-27)
  783. */
  784. protected $newpagegroup = array();
  785. /**
  786. * Array that contains the number of pages in each page group.
  787. * @protected
  788. * @since 3.0.000 (2008-03-27)
  789. */
  790. protected $pagegroups = array();
  791. /**
  792. * Current page group number.
  793. * @protected
  794. * @since 3.0.000 (2008-03-27)
  795. */
  796. protected $currpagegroup = 0;
  797. /**
  798. * Array of transparency objects and parameters.
  799. * @protected
  800. * @since 3.0.000 (2008-03-27)
  801. */
  802. protected $extgstates;
  803. /**
  804. * Set the default JPEG compression quality (1-100).
  805. * @protected
  806. * @since 3.0.000 (2008-03-27)
  807. */
  808. protected $jpeg_quality;
  809. /**
  810. * Default cell height ratio.
  811. * @protected
  812. * @since 3.0.014 (2008-05-23)
  813. */
  814. protected $cell_height_ratio = K_CELL_HEIGHT_RATIO;
  815. /**
  816. * PDF viewer preferences.
  817. * @protected
  818. * @since 3.1.000 (2008-06-09)
  819. */
  820. protected $viewer_preferences;
  821. /**
  822. * A name object specifying how the document should be displayed when opened.
  823. * @protected
  824. * @since 3.1.000 (2008-06-09)
  825. */
  826. protected $PageMode;
  827. /**
  828. * Array for storing gradient information.
  829. * @protected
  830. * @since 3.1.000 (2008-06-09)
  831. */
  832. protected $gradients = array();
  833. /**
  834. * Array used to store positions inside the pages buffer (keys are the page numbers).
  835. * @protected
  836. * @since 3.2.000 (2008-06-26)
  837. */
  838. protected $intmrk = array();
  839. /**
  840. * Array used to store positions inside the pages buffer (keys are the page numbers).
  841. * @protected
  842. * @since 5.7.000 (2010-08-03)
  843. */
  844. protected $bordermrk = array();
  845. /**
  846. * Array used to store page positions to track empty pages (keys are the page numbers).
  847. * @protected
  848. * @since 5.8.007 (2010-08-18)
  849. */
  850. protected $emptypagemrk = array();
  851. /**
  852. * Array used to store content positions inside the pages buffer (keys are the page numbers).
  853. * @protected
  854. * @since 4.6.021 (2009-07-20)
  855. */
  856. protected $cntmrk = array();
  857. /**
  858. * Array used to store footer positions of each page.
  859. * @protected
  860. * @since 3.2.000 (2008-07-01)
  861. */
  862. protected $footerpos = array();
  863. /**
  864. * Array used to store footer length of each page.
  865. * @protected
  866. * @since 4.0.014 (2008-07-29)
  867. */
  868. protected $footerlen = array();
  869. /**
  870. * Boolean flag to indicate if a new line is created.
  871. * @protected
  872. * @since 3.2.000 (2008-07-01)
  873. */
  874. protected $newline = true;
  875. /**
  876. * End position of the latest inserted line.
  877. * @protected
  878. * @since 3.2.000 (2008-07-01)
  879. */
  880. protected $endlinex = 0;
  881. /**
  882. * PDF string for width value of the last line.
  883. * @protected
  884. * @since 4.0.006 (2008-07-16)
  885. */
  886. protected $linestyleWidth = '';
  887. /**
  888. * PDF string for CAP value of the last line.
  889. * @protected
  890. * @since 4.0.006 (2008-07-16)
  891. */
  892. protected $linestyleCap = '0 J';
  893. /**
  894. * PDF string for join value of the last line.
  895. * @protected
  896. * @since 4.0.006 (2008-07-16)
  897. */
  898. protected $linestyleJoin = '0 j';
  899. /**
  900. * PDF string for dash value of the last line.
  901. * @protected
  902. * @since 4.0.006 (2008-07-16)
  903. */
  904. protected $linestyleDash = '[] 0 d';
  905. /**
  906. * Boolean flag to indicate if marked-content sequence is open.
  907. * @protected
  908. * @since 4.0.013 (2008-07-28)
  909. */
  910. protected $openMarkedContent = false;
  911. /**
  912. * Count the latest inserted vertical spaces on HTML.
  913. * @protected
  914. * @since 4.0.021 (2008-08-24)
  915. */
  916. protected $htmlvspace = 0;
  917. /**
  918. * Array of Spot colors.
  919. * @protected
  920. * @since 4.0.024 (2008-09-12)
  921. */
  922. protected $spot_colors = array();
  923. /**
  924. * Symbol used for HTML unordered list items.
  925. * @protected
  926. * @since 4.0.028 (2008-09-26)
  927. */
  928. protected $lisymbol = '';
  929. /**
  930. * String used to mark the beginning and end of EPS image blocks.
  931. * @protected
  932. * @since 4.1.000 (2008-10-18)
  933. */
  934. protected $epsmarker = 'x#!#EPS#!#x';
  935. /**
  936. * Array of transformation matrix.
  937. * @protected
  938. * @since 4.2.000 (2008-10-29)
  939. */
  940. protected $transfmatrix = array();
  941. /**
  942. * Current key for transformation matrix.
  943. * @protected
  944. * @since 4.8.005 (2009-09-17)
  945. */
  946. protected $transfmatrix_key = 0;
  947. /**
  948. * Booklet mode for double-sided pages.
  949. * @protected
  950. * @since 4.2.000 (2008-10-29)
  951. */
  952. protected $booklet = false;
  953. /**
  954. * Epsilon value used for float calculations.
  955. * @protected
  956. * @since 4.2.000 (2008-10-29)
  957. */
  958. protected $feps = 0.005;
  959. /**
  960. * Array used for custom vertical spaces for HTML tags.
  961. * @protected
  962. * @since 4.2.001 (2008-10-30)
  963. */
  964. protected $tagvspaces = array();
  965. /**
  966. * HTML PARSER: custom indent amount for lists. Negative value means disabled.
  967. * @protected
  968. * @since 4.2.007 (2008-11-12)
  969. */
  970. protected $customlistindent = -1;
  971. /**
  972. * Boolean flag to indicate if the border of the cell sides that cross the page should be removed.
  973. * @protected
  974. * @since 4.2.010 (2008-11-14)
  975. */
  976. protected $opencell = true;
  977. /**
  978. * Array of files to embedd.
  979. * @protected
  980. * @since 4.4.000 (2008-12-07)
  981. */
  982. protected $embeddedfiles = array();
  983. /**
  984. * Boolean flag to indicate if we are inside a PRE tag.
  985. * @protected
  986. * @since 4.4.001 (2008-12-08)
  987. */
  988. protected $premode = false;
  989. /**
  990. * Array used to store positions of graphics transformation blocks inside the page buffer.
  991. * keys are the page numbers
  992. * @protected
  993. * @since 4.4.002 (2008-12-09)
  994. */
  995. protected $transfmrk = array();
  996. /**
  997. * Default color for html links.
  998. * @protected
  999. * @since 4.4.003 (2008-12-09)
  1000. */
  1001. protected $htmlLinkColorArray = array(0, 0, 255);
  1002. /**
  1003. * Default font style to add to html links.
  1004. * @protected
  1005. * @since 4.4.003 (2008-12-09)
  1006. */
  1007. protected $htmlLinkFontStyle = 'U';
  1008. /**
  1009. * Counts the number of pages.
  1010. * @protected
  1011. * @since 4.5.000 (2008-12-31)
  1012. */
  1013. protected $numpages = 0;
  1014. /**
  1015. * Array containing page lengths in bytes.
  1016. * @protected
  1017. * @since 4.5.000 (2008-12-31)
  1018. */
  1019. protected $pagelen = array();
  1020. /**
  1021. * Counts the number of pages.
  1022. * @protected
  1023. * @since 4.5.000 (2008-12-31)
  1024. */
  1025. protected $numimages = 0;
  1026. /**
  1027. * Store the image keys.
  1028. * @protected
  1029. * @since 4.5.000 (2008-12-31)
  1030. */
  1031. protected $imagekeys = array();
  1032. /**
  1033. * Length of the buffer in bytes.
  1034. * @protected
  1035. * @since 4.5.000 (2008-12-31)
  1036. */
  1037. protected $bufferlen = 0;
  1038. /**
  1039. * If true enables disk caching.
  1040. * @protected
  1041. * @since 4.5.000 (2008-12-31)
  1042. */
  1043. protected $diskcache = false;
  1044. /**
  1045. * Counts the number of fonts.
  1046. * @protected
  1047. * @since 4.5.000 (2009-01-02)
  1048. */
  1049. protected $numfonts = 0;
  1050. /**
  1051. * Store the font keys.
  1052. * @protected
  1053. * @since 4.5.000 (2009-01-02)
  1054. */
  1055. protected $fontkeys = array();
  1056. /**
  1057. * Store the font object IDs.
  1058. * @protected
  1059. * @since 4.8.001 (2009-09-09)
  1060. */
  1061. protected $font_obj_ids = array();
  1062. /**
  1063. * Store the fage status (true when opened, false when closed).
  1064. * @protected
  1065. * @since 4.5.000 (2009-01-02)
  1066. */
  1067. protected $pageopen = array();
  1068. /**
  1069. * Default monospace font.
  1070. * @protected
  1071. * @since 4.5.025 (2009-03-10)
  1072. */
  1073. protected $default_monospaced_font = 'courier';
  1074. /**
  1075. * Cloned copy of the current class object.
  1076. * @protected
  1077. * @since 4.5.029 (2009-03-19)
  1078. */
  1079. protected $objcopy;
  1080. /**
  1081. * Array used to store the lengths of cache files.
  1082. * @protected
  1083. * @since 4.5.029 (2009-03-19)
  1084. */
  1085. protected $cache_file_length = array();
  1086. /**
  1087. * Table header content to be repeated on each new page.
  1088. * @protected
  1089. * @since 4.5.030 (2009-03-20)
  1090. */
  1091. protected $thead = '';
  1092. /**
  1093. * Margins used for table header.
  1094. * @protected
  1095. * @since 4.5.030 (2009-03-20)
  1096. */
  1097. protected $theadMargins = array();
  1098. /**
  1099. * Cache array for UTF8StringToArray() method.
  1100. * @protected
  1101. * @since 4.5.037 (2009-04-07)
  1102. */
  1103. protected $cache_UTF8StringToArray = array();
  1104. /**
  1105. * Maximum size of cache array used for UTF8StringToArray() method.
  1106. * @protected
  1107. * @since 4.5.037 (2009-04-07)
  1108. */
  1109. protected $cache_maxsize_UTF8StringToArray = 8;
  1110. /**
  1111. * Current size of cache array used for UTF8StringToArray() method.
  1112. * @protected
  1113. * @since 4.5.037 (2009-04-07)
  1114. */
  1115. protected $cache_size_UTF8StringToArray = 0;
  1116. /**
  1117. * Boolean flag to enable document digital signature.
  1118. * @protected
  1119. * @since 4.6.005 (2009-04-24)
  1120. */
  1121. protected $sign = false;
  1122. /**
  1123. * Digital signature data.
  1124. * @protected
  1125. * @since 4.6.005 (2009-04-24)
  1126. */
  1127. protected $signature_data = array();
  1128. /**
  1129. * Digital signature max length.
  1130. * @protected
  1131. * @since 4.6.005 (2009-04-24)
  1132. */
  1133. protected $signature_max_length = 11742;
  1134. /**
  1135. * Data for digital signature appearance.
  1136. * @protected
  1137. * @since 5.3.011 (2010-06-16)
  1138. */
  1139. protected $signature_appearance = array('page' => 1, 'rect' => '0 0 0 0');
  1140. /**
  1141. * Array of empty digital signature appearances.
  1142. * @protected
  1143. * @since 5.9.101 (2011-07-06)
  1144. */
  1145. protected $empty_signature_appearance = array();
  1146. /**
  1147. * Regular expression used to find blank characters (required for word-wrapping).
  1148. * @protected
  1149. * @since 4.6.006 (2009-04-28)
  1150. */
  1151. protected $re_spaces = '/[^\S\xa0]/';
  1152. /**
  1153. * Array of $re_spaces parts.
  1154. * @protected
  1155. * @since 5.5.011 (2010-07-09)
  1156. */
  1157. protected $re_space = array('p' => '[^\S\xa0]', 'm' => '');
  1158. /**
  1159. * Digital signature object ID.
  1160. * @protected
  1161. * @since 4.6.022 (2009-06-23)
  1162. */
  1163. protected $sig_obj_id = 0;
  1164. /**
  1165. * ByteRange placemark used during digital signature process.
  1166. * @protected
  1167. * @since 4.6.028 (2009-08-25)
  1168. */
  1169. protected $byterange_string = '/ByteRange[0 ********** ********** **********]';
  1170. /**
  1171. * Placemark used during digital signature process.
  1172. * @protected
  1173. * @since 4.6.028 (2009-08-25)
  1174. */
  1175. protected $sig_annot_ref = '***SIGANNREF*** 0 R';
  1176. /**
  1177. * ID of page objects.
  1178. * @protected
  1179. * @since 4.7.000 (2009-08-29)
  1180. */
  1181. protected $page_obj_id = array();
  1182. /**
  1183. * List of form annotations IDs.
  1184. * @protected
  1185. * @since 4.8.000 (2009-09-07)
  1186. */
  1187. protected $form_obj_id = array();
  1188. /**
  1189. * 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.
  1190. * @protected
  1191. * @since 4.8.000 (2009-09-07)
  1192. */
  1193. protected $default_form_prop = array('lineWidth'=>1, 'borderStyle'=>'solid', 'fillColor'=>array(255, 255, 255), 'strokeColor'=>array(128, 128, 128));
  1194. /**
  1195. * Javascript objects array.
  1196. * @protected
  1197. * @since 4.8.000 (2009-09-07)
  1198. */
  1199. protected $js_objects = array();
  1200. /**
  1201. * Current form action (used during XHTML rendering).
  1202. * @protected
  1203. * @since 4.8.000 (2009-09-07)
  1204. */
  1205. protected $form_action = '';
  1206. /**
  1207. * Current form encryption type (used during XHTML rendering).
  1208. * @protected
  1209. * @since 4.8.000 (2009-09-07)
  1210. */
  1211. protected $form_enctype = 'application/x-www-form-urlencoded';
  1212. /**
  1213. * Current method to submit forms.
  1214. * @protected
  1215. * @since 4.8.000 (2009-09-07)
  1216. */
  1217. protected $form_mode = 'post';
  1218. /**
  1219. * List of fonts used on form fields (fontname => fontkey).
  1220. * @protected
  1221. * @since 4.8.001 (2009-09-09)
  1222. */
  1223. protected $annotation_fonts = array();
  1224. /**
  1225. * List of radio buttons parent objects.
  1226. * @protected
  1227. * @since 4.8.001 (2009-09-09)
  1228. */
  1229. protected $radiobutton_groups = array();
  1230. /**
  1231. * List of radio group objects IDs.
  1232. * @protected
  1233. * @since 4.8.001 (2009-09-09)
  1234. */
  1235. protected $radio_groups = array();
  1236. /**
  1237. * Text indentation value (used for text-indent CSS attribute).
  1238. * @protected
  1239. * @since 4.8.006 (2009-09-23)
  1240. */
  1241. protected $textindent = 0;
  1242. /**
  1243. * Store page number when startTransaction() is called.
  1244. * @protected
  1245. * @since 4.8.006 (2009-09-23)
  1246. */
  1247. protected $start_transaction_page = 0;
  1248. /**
  1249. * Store Y position when startTransaction() is called.
  1250. * @protected
  1251. * @since 4.9.001 (2010-03-28)
  1252. */
  1253. protected $start_transaction_y = 0;
  1254. /**
  1255. * True when we are printing the thead section on a new page.
  1256. * @protected
  1257. * @since 4.8.027 (2010-01-25)
  1258. */
  1259. protected $inthead = false;
  1260. /**
  1261. * Array of column measures (width, space, starting Y position).
  1262. * @protected
  1263. * @since 4.9.001 (2010-03-28)
  1264. */
  1265. protected $columns = array();
  1266. /**
  1267. * Number of colums.
  1268. * @protected
  1269. * @since 4.9.001 (2010-03-28)
  1270. */
  1271. protected $num_columns = 1;
  1272. /**
  1273. * Current column number.
  1274. * @protected
  1275. * @since 4.9.001 (2010-03-28)
  1276. */
  1277. protected $current_column = 0;
  1278. /**
  1279. * Starting page for columns.
  1280. * @protected
  1281. * @since 4.9.001 (2010-03-28)
  1282. */
  1283. protected $column_start_page = 0;
  1284. /**
  1285. * Maximum page and column selected.
  1286. * @protected
  1287. * @since 5.8.000 (2010-08-11)
  1288. */
  1289. protected $maxselcol = array('page' => 0, 'column' => 0);
  1290. /**
  1291. * Array of: X difference between table cell x start and starting page margin, cellspacing, cellpadding.
  1292. * @protected
  1293. * @since 5.8.000 (2010-08-11)
  1294. */
  1295. protected $colxshift = array('x' => 0, 's' => array('H' => 0, 'V' => 0), 'p' => array('L' => 0, 'T' => 0, 'R' => 0, 'B' => 0));
  1296. /**
  1297. * 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.
  1298. * @protected
  1299. * @since 4.9.008 (2010-04-03)
  1300. */
  1301. protected $textrendermode = 0;
  1302. /**
  1303. * Text stroke width in doc units.
  1304. * @protected
  1305. * @since 4.9.008 (2010-04-03)
  1306. */
  1307. protected $textstrokewidth = 0;
  1308. /**
  1309. * Current stroke color.
  1310. * @protected
  1311. * @since 4.9.008 (2010-04-03)
  1312. */
  1313. protected $strokecolor;
  1314. /**
  1315. * Default unit of measure for document.
  1316. * @protected
  1317. * @since 5.0.000 (2010-04-22)
  1318. */
  1319. protected $pdfunit = 'mm';
  1320. /**
  1321. * Boolean flag true when we are on TOC (Table Of Content) page.
  1322. * @protected
  1323. */
  1324. protected $tocpage = false;
  1325. /**
  1326. * Boolean flag: if true convert vector images (SVG, EPS) to raster image using GD or ImageMagick library.
  1327. * @protected
  1328. * @since 5.0.000 (2010-04-26)
  1329. */
  1330. protected $rasterize_vector_images = false;
  1331. /**
  1332. * Boolean flag: if true enables font subsetting by default.
  1333. * @protected
  1334. * @since 5.3.002 (2010-06-07)
  1335. */
  1336. protected $font_subsetting = true;
  1337. /**
  1338. * Array of default graphic settings.
  1339. * @protected
  1340. * @since 5.5.008 (2010-07-02)
  1341. */
  1342. protected $default_graphic_vars = array();
  1343. /**
  1344. * Array of XObjects.
  1345. * @protected
  1346. * @since 5.8.014 (2010-08-23)
  1347. */
  1348. protected $xobjects = array();
  1349. /**
  1350. * Boolean value true when we are inside an XObject.
  1351. * @protected
  1352. * @since 5.8.017 (2010-08-24)
  1353. */
  1354. protected $inxobj = false;
  1355. /**
  1356. * Current XObject ID.
  1357. * @protected
  1358. * @since 5.8.017 (2010-08-24)
  1359. */
  1360. protected $xobjid = '';
  1361. /**
  1362. * Percentage of character stretching.
  1363. * @protected
  1364. * @since 5.9.000 (2010-09-29)
  1365. */
  1366. protected $font_stretching = 100;
  1367. /**
  1368. * Increases or decreases the space between characters in a text by the specified amount (tracking/kerning).
  1369. * @protected
  1370. * @since 5.9.000 (2010-09-29)
  1371. */
  1372. protected $font_spacing = 0;
  1373. /**
  1374. * Array of no-write regions.
  1375. * ('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)
  1376. * @protected
  1377. * @since 5.9.003 (2010-10-14)
  1378. */
  1379. protected $page_regions = array();
  1380. /**
  1381. * Array containing HTML color names and values.
  1382. * @protected
  1383. * @since 5.9.004 (2010-10-18)
  1384. */
  1385. protected $webcolor = array();
  1386. /**
  1387. * Array containing spot color names and values.
  1388. * @protected
  1389. * @since 5.9.012 (2010-11-11)
  1390. */
  1391. protected $spotcolor = array();
  1392. /**
  1393. * Array of PDF layers data.
  1394. * @protected
  1395. * @since 5.9.102 (2011-07-13)
  1396. */
  1397. protected $pdflayers = array();
  1398. /**
  1399. * A dictionary of names and corresponding destinations (Dests key on document Catalog).
  1400. * @protected
  1401. * @since 5.9.097 (2011-06-23)
  1402. */
  1403. protected $dests = array();
  1404. /**
  1405. * Object ID for Named Destinations
  1406. * @protected
  1407. * @since 5.9.097 (2011-06-23)
  1408. */
  1409. protected $n_dests;
  1410. /**
  1411. * Directory used for the last SVG image.
  1412. * @protected
  1413. * @since 5.0.000 (2010-05-05)
  1414. */
  1415. protected $svgdir = '';
  1416. /**
  1417. * Deafult unit of measure for SVG.
  1418. * @protected
  1419. * @since 5.0.000 (2010-05-02)
  1420. */
  1421. protected $svgunit = 'px';
  1422. /**
  1423. * Array of SVG gradients.
  1424. * @protected
  1425. * @since 5.0.000 (2010-05-02)
  1426. */
  1427. protected $svggradients = array();
  1428. /**
  1429. * ID of last SVG gradient.
  1430. * @protected
  1431. * @since 5.0.000 (2010-05-02)
  1432. */
  1433. protected $svggradientid = 0;
  1434. /**
  1435. * Boolean value true when in SVG defs group.
  1436. * @protected
  1437. * @since 5.0.000 (2010-05-02)
  1438. */
  1439. protected $svgdefsmode = false;
  1440. /**
  1441. * Array of SVG defs.
  1442. * @protected
  1443. * @since 5.0.000 (2010-05-02)
  1444. */
  1445. protected $svgdefs = array();
  1446. /**
  1447. * Boolean value true when in SVG clipPath tag.
  1448. * @protected
  1449. * @since 5.0.000 (2010-04-26)
  1450. */
  1451. protected $svgclipmode = false;
  1452. /**
  1453. * Array of SVG clipPath commands.
  1454. * @protected
  1455. * @since 5.0.000 (2010-05-02)
  1456. */
  1457. protected $svgclippaths = array();
  1458. /**
  1459. * Array of SVG clipPath tranformation matrix.
  1460. * @protected
  1461. * @since 5.8.022 (2010-08-31)
  1462. */
  1463. protected $svgcliptm = array();
  1464. /**
  1465. * ID of last SVG clipPath.
  1466. * @protected
  1467. * @since 5.0.000 (2010-05-02)
  1468. */
  1469. protected $svgclipid = 0;
  1470. /**
  1471. * SVG text.
  1472. * @protected
  1473. * @since 5.0.000 (2010-05-02)
  1474. */
  1475. protected $svgtext = '';
  1476. /**
  1477. * SVG text properties.
  1478. * @protected
  1479. * @since 5.8.013 (2010-08-23)
  1480. */
  1481. protected $svgtextmode = array();
  1482. /**
  1483. * Array of hinheritable SVG properties.
  1484. * @protected
  1485. * @since 5.0.000 (2010-05-02)
  1486. */
  1487. protected $svginheritprop = array('clip-rule', 'color', 'color-interpolation', 'color-interpolation-filters', 'color-profile', 'color-rendering', 'cursor', 'direction', 'fill', 'fill-opacity', 'fill-rule', 'font', 'font-family', 'font-size', 'font-size-adjust', 'font-stretch', 'font-style', 'font-variant', 'font-weight', 'glyph-orientation-horizontal', 'glyph-orientation-vertical', 'image-rendering', 'kerning', 'letter-spacing', 'marker', 'marker-end', 'marker-mid', 'marker-start', 'pointer-events', 'shape-rendering', 'stroke', 'stroke-dasharray', 'stroke-dashoffset', 'stroke-linecap', 'stroke-linejoin', 'stroke-miterlimit', 'stroke-opacity', 'stroke-width', 'text-anchor', 'text-rendering', 'visibility', 'word-spacing', 'writing-mode');
  1488. /**
  1489. * Array of SVG properties.
  1490. * @protected
  1491. * @since 5.0.000 (2010-05-02)
  1492. */
  1493. protected $svgstyles = array(array(
  1494. 'alignment-baseline' => 'auto',
  1495. 'baseline-shift' => 'baseline',
  1496. 'clip' => 'auto',
  1497. 'clip-path' => 'none',
  1498. 'clip-rule' => 'nonzero',
  1499. 'color' => 'black',
  1500. 'color-interpolation' => 'sRGB',
  1501. 'color-interpolation-filters' => 'linearRGB',
  1502. 'color-profile' => 'auto',
  1503. 'color-rendering' => 'auto',
  1504. 'cursor' => 'auto',
  1505. 'direction' => 'ltr',
  1506. 'display' => 'inline',
  1507. 'dominant-baseline' => 'auto',
  1508. 'enable-background' => 'accumulate',
  1509. 'fill' => 'black',
  1510. 'fill-opacity' => 1,
  1511. 'fill-rule' => 'nonzero',
  1512. 'filter' => 'none',
  1513. 'flood-color' => 'black',
  1514. 'flood-opacity' => 1,
  1515. 'font' => '',
  1516. 'font-family' => 'helvetica',
  1517. 'font-size' => 'medium',
  1518. 'font-size-adjust' => 'none',
  1519. 'font-stretch' => 'normal',
  1520. 'font-style' => 'normal',
  1521. 'font-variant' => 'normal',
  1522. 'font-weight' => 'normal',
  1523. 'glyph-orientation-horizontal' => '0deg',
  1524. 'glyph-orientation-vertical' => 'auto',
  1525. 'image-rendering' => 'auto',
  1526. 'kerning' => 'auto',
  1527. 'letter-spacing' => 'normal',
  1528. 'lighting-color' => 'white',
  1529. 'marker' => '',
  1530. 'marker-end' => 'none',
  1531. 'marker-mid' => 'none',
  1532. 'marker-start' => 'none',
  1533. 'mask' => 'none',
  1534. 'opacity' => 1,
  1535. 'overflow' => 'auto',
  1536. 'pointer-events' => 'visiblePainted',
  1537. 'shape-rendering' => 'auto',
  1538. 'stop-color' => 'black',
  1539. 'stop-opacity' => 1,
  1540. 'stroke' => 'none',
  1541. 'stroke-dasharray' => 'none',
  1542. 'stroke-dashoffset' => 0,
  1543. 'stroke-linecap' => 'butt',
  1544. 'stroke-linejoin' => 'miter',
  1545. 'stroke-miterlimit' => 4,
  1546. 'stroke-opacity' => 1,
  1547. 'stroke-width' => 1,
  1548. 'text-anchor' => 'start',
  1549. 'text-decoration' => 'none',
  1550. 'text-rendering' => 'auto',
  1551. 'unicode-bidi' => 'normal',
  1552. 'visibility' => 'visible',
  1553. 'word-spacing' => 'normal',
  1554. 'writing-mode' => 'lr-tb',
  1555. 'text-color' => 'black',
  1556. 'transfmatrix' => array(1, 0, 0, 1, 0, 0)
  1557. ));
  1558. /**
  1559. * If true force sRGB color profile for all document.
  1560. * @protected
  1561. * @since 5.9.121 (2011-09-28)
  1562. */
  1563. protected $force_srgb = false;
  1564. /**
  1565. * If true set the document to PDF/A mode.
  1566. * @protected
  1567. * @since 5.9.121 (2011-09-27)
  1568. */
  1569. protected $pdfa_mode = false;
  1570. /**
  1571. * Document creation date
  1572. * @protected
  1573. * @since 5.9.121 (2011-09-28)
  1574. */
  1575. protected $doc_date;
  1576. /**
  1577. * Custom XMP data.
  1578. * @protected
  1579. * @since 5.9.128 (2011-10-06)
  1580. */
  1581. protected $custom_xmp = '';
  1582. //------------------------------------------------------------
  1583. // METHODS
  1584. //------------------------------------------------------------
  1585. /**
  1586. * This is the class constructor.
  1587. * It allows to set up the page format, the orientation and the measure unit used in all the methods (except for the font sizes).
  1588. * @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>
  1589. * @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.
  1590. * @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().
  1591. * @param $unicode (boolean) TRUE means that the input text is unicode (default = true)
  1592. * @param $encoding (string) Charset encoding; default is UTF-8.
  1593. * @param $diskcache (boolean) If TRUE reduce the RAM memory usage by caching temporary data on filesystem (slower).
  1594. * @param $pdfa (boolean) If TRUE set the document to PDF/A mode.
  1595. * @public
  1596. * @see getPageSizeFromFormat(), setPageFormat()
  1597. */
  1598. public function __construct($orientation='P', $unit='mm', $format='A4', $unicode=true, $encoding='UTF-8', $diskcache=false, $pdfa=false) {
  1599. /* Set internal character encoding to ASCII */
  1600. if (function_exists('mb_internal_encoding') AND mb_internal_encoding()) {
  1601. $this->internal_encoding = mb_internal_encoding();
  1602. mb_internal_encoding('ASCII');
  1603. }
  1604. // get array of HTML colors
  1605. require(dirname(__FILE__).'/htmlcolors.php');
  1606. $this->webcolor = $webcolor;
  1607. // get array of custom spot colors
  1608. if (file_exists(dirname(__FILE__).'/spotcolors.php')) {
  1609. require(dirname(__FILE__).'/spotcolors.php');
  1610. $this->spotcolor = $spotcolor;
  1611. } else {
  1612. $this->spotcolor = array();
  1613. }
  1614. require_once(dirname(__FILE__).'/unicode_data.php');
  1615. $this->unicode = new TCPDF_UNICODE_DATA();
  1616. require_once(dirname(__FILE__).'/encodings_maps.php');
  1617. $this->encmaps = new TCPDF_ENCODING_MAPS();
  1618. $this->font_obj_ids = array();
  1619. $this->page_obj_id = array();
  1620. $this->form_obj_id = array();
  1621. // set pdf/a mode
  1622. $this->pdfa_mode = $pdfa;
  1623. $this->force_srgb = false;
  1624. // set disk caching
  1625. $this->diskcache = $diskcache ? true : false;
  1626. // set language direction
  1627. $this->rtl = false;
  1628. $this->tmprtl = false;
  1629. // some checks
  1630. $this->_dochecks();
  1631. // initialization of properties
  1632. $this->isunicode = $unicode;
  1633. $this->page = 0;
  1634. $this->transfmrk[0] = array();
  1635. $this->pagedim = array();
  1636. $this->n = 2;
  1637. $this->buffer = '';
  1638. $this->pages = array();
  1639. $this->state = 0;
  1640. $this->fonts = array();
  1641. $this->FontFiles = array();
  1642. $this->diffs = array();
  1643. $this->images = array();
  1644. $this->links = array();
  1645. $this->gradients = array();
  1646. $this->InFooter = false;
  1647. $this->lasth = 0;
  1648. $this->FontFamily = 'helvetica';
  1649. $this->FontStyle = '';
  1650. $this->FontSizePt = 12;
  1651. $this->underline = false;
  1652. $this->overline = false;
  1653. $this->linethrough = false;
  1654. $this->DrawColor = '0 G';
  1655. $this->FillColor = '0 g';
  1656. $this->TextColor = '0 g';
  1657. $this->ColorFlag = false;
  1658. $this->pdflayers = array();
  1659. // encryption values
  1660. $this->encrypted = false;
  1661. $this->last_enc_key = '';
  1662. // standard Unicode fonts
  1663. $this->CoreFonts = array(
  1664. 'courier'=>'Courier',
  1665. 'courierB'=>'Courier-Bold',
  1666. 'courierI'=>'Courier-Oblique',
  1667. 'courierBI'=>'Courier-BoldOblique',
  1668. 'helvetica'=>'Helvetica',
  1669. 'helveticaB'=>'Helvetica-Bold',
  1670. 'helveticaI'=>'Helvetica-Oblique',
  1671. 'helveticaBI'=>'Helvetica-BoldOblique',
  1672. 'times'=>'Times-Roman',
  1673. 'timesB'=>'Times-Bold',
  1674. 'timesI'=>'Times-Italic',
  1675. 'timesBI'=>'Times-BoldItalic',
  1676. 'symbol'=>'Symbol',
  1677. 'zapfdingbats'=>'ZapfDingbats'
  1678. );
  1679. // set scale factor
  1680. $this->setPageUnit($unit);
  1681. // set page format and orientation
  1682. $this->setPageFormat($format, $orientation);
  1683. // page margins (1 cm)
  1684. $margin = 28.35 / $this->k;
  1685. $this->SetMargins($margin, $margin);
  1686. // internal cell padding
  1687. $cpadding = $margin / 10;
  1688. $this->setCellPaddings($cpadding, 0, $cpadding, 0);
  1689. // cell margins
  1690. $this->setCellMargins(0, 0, 0, 0);
  1691. // line width (0.2 mm)
  1692. $this->LineWidth = 0.57 / $this->k;
  1693. $this->linestyleWidth = sprintf('%.2F w', ($this->LineWidth * $this->k));
  1694. $this->linestyleCap = '0 J';
  1695. $this->linestyleJoin = '0 j';
  1696. $this->linestyleDash = '[] 0 d';
  1697. // automatic page break
  1698. $this->SetAutoPageBreak(true, (2 * $margin));
  1699. // full width display mode
  1700. $this->SetDisplayMode('fullwidth');
  1701. // compression
  1702. $this->SetCompression();
  1703. // set default PDF version number
  1704. $this->setPDFVersion();
  1705. $this->encoding = $encoding;
  1706. $this->HREF = array();
  1707. $this->getFontsList();
  1708. $this->fgcolor = array('R' => 0, 'G' => 0, 'B' => 0);
  1709. $this->strokecolor = array('R' => 0, 'G' => 0, 'B' => 0);
  1710. $this->bgcolor = array('R' => 255, 'G' => 255, 'B' => 255);
  1711. $this->extgstates = array();
  1712. // user's rights
  1713. $this->sign = false;
  1714. $this->ur['enabled'] = false;
  1715. $this->ur['document'] = '/FullSave';
  1716. $this->ur['annots'] = '/Create/Delete/Modify/Copy/Import/Export';
  1717. $this->ur['form'] = '/Add/Delete/FillIn/Import/Export/SubmitStandalone/SpawnTemplate';
  1718. $this->ur['signature'] = '/Modify';
  1719. $this->ur['ef'] = '/Create/Delete/Modify/Import';
  1720. $this->ur['formex'] = '';
  1721. $this->signature_appearance = array('page' => 1, 'rect' => '0 0 0 0');
  1722. $this->empty_signature_appearance = array();
  1723. // set default JPEG quality
  1724. $this->jpeg_quality = 75;
  1725. // initialize some settings
  1726. $this->utf8Bidi(array(''), '');
  1727. // set default font
  1728. $this->SetFont($this->FontFamily, $this->FontStyle, $this->FontSizePt);
  1729. // check if PCRE Unicode support is enabled
  1730. if ($this->isunicode AND (@preg_match('/\pL/u', 'a') == 1)) {
  1731. // PCRE unicode support is turned ON
  1732. // \p{Z} or \p{Separator}: any kind of Unicode whitespace or invisible separator.
  1733. // \p{Lo} or \p{Other_Letter}: a Unicode letter or ideograph that does not have lowercase and uppercase variants.
  1734. // \p{Lo} is needed because Chinese characters are packed next to each other without spaces in between.
  1735. //$this->setSpacesRE('/[^\S\P{Z}\P{Lo}\xa0]/u');
  1736. $this->setSpacesRE('/[^\S\P{Z}\xa0]/u');
  1737. } else {
  1738. // PCRE unicode support is turned OFF
  1739. $this->setSpacesRE('/[^\S\xa0]/');
  1740. }
  1741. $this->default_form_prop = array('lineWidth'=>1, 'borderStyle'=>'solid', 'fillColor'=>array(255, 255, 255), 'strokeColor'=>array(128, 128, 128));
  1742. // set file ID for trailer
  1743. $this->file_id = md5($this->getRandomSeed('TCPDF'.$orientation.$unit.$format.$encoding));
  1744. // set document date
  1745. $this->doc_date = substr_replace(date('YmdHisO'), '\'', (0 - 2), 0).'\'';
  1746. // get default graphic vars
  1747. $this->default_graphic_vars = $this->getGraphicVars();
  1748. $this->header_xobj_autoreset = false;
  1749. $this->custom_xmp = '';
  1750. }
  1751. /**
  1752. * Default destructor.
  1753. * @public
  1754. * @since 1.53.0.TC016
  1755. */
  1756. public function __destruct() {
  1757. // restore internal encoding
  1758. if (isset($this->internal_encoding) AND !empty($this->internal_encoding)) {
  1759. mb_internal_encoding($this->internal_encoding);
  1760. }
  1761. // unset all class variables
  1762. $this->_destroy(true);
  1763. }
  1764. /**
  1765. * Return the current TCPDF version.
  1766. * @return TCPDF version string
  1767. * @public
  1768. * @since 5.9.012 (2010-11-10)
  1769. */
  1770. public function getTCPDFVersion() {
  1771. return $this->tcpdf_version;
  1772. }
  1773. /**
  1774. * Set the units of measure for the document.
  1775. * @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.
  1776. * @public
  1777. * @since 3.0.015 (2008-06-06)
  1778. */
  1779. public function setPageUnit($unit) {
  1780. $unit = strtolower($unit);
  1781. //Set scale factor
  1782. switch ($unit) {
  1783. // points
  1784. case 'px':
  1785. case 'pt': {
  1786. $this->k = 1;
  1787. break;
  1788. }
  1789. // millimeters
  1790. case 'mm': {
  1791. $this->k = $this->dpi / 25.4;
  1792. break;
  1793. }
  1794. // centimeters
  1795. case 'cm': {
  1796. $this->k = $this->dpi / 2.54;
  1797. break;
  1798. }
  1799. // inches
  1800. case 'in': {
  1801. $this->k = $this->dpi;
  1802. break;
  1803. }
  1804. // unsupported unit
  1805. default : {
  1806. $this->Error('Incorrect unit: '.$unit);
  1807. break;
  1808. }
  1809. }
  1810. $this->pdfunit = $unit;
  1811. if (isset($this->CurOrientation)) {
  1812. $this->setPageOrientation($this->CurOrientation);
  1813. }
  1814. }
  1815. /**
  1816. * Get page dimensions from format name.
  1817. * @param $format (mixed) The format name. It can be: <ul>
  1818. * <li><b>ISO 216 A Series + 2 SIS 014711 extensions</b></li>
  1819. * <li>A0 (841x1189 mm ; 33.11x46.81 in)</li>
  1820. * <li>A1 (594x841 mm ; 23.39x33.11 in)</li>
  1821. * <li>A2 (420x594 mm ; 16.54x23.39 in)</li>
  1822. * <li>A3 (297x420 mm ; 11.69x16.54 in)</li>
  1823. * <li>A4 (210x297 mm ; 8.27x11.69 in)</li>
  1824. * <li>A5 (148x210 mm ; 5.83x8.27 in)</li>
  1825. * <li>A6 (105x148 mm ; 4.13x5.83 in)</li>
  1826. * <li>A7 (74x105 mm ; 2.91x4.13 in)</li>
  1827. * <li>A8 (52x74 mm ; 2.05x2.91 in)</li>
  1828. * <li>A9 (37x52 mm ; 1.46x2.05 in)</li>
  1829. * <li>A10 (26x37 mm ; 1.02x1.46 in)</li>
  1830. * <li>A11 (18x26 mm ; 0.71x1.02 in)</li>
  1831. * <li>A12 (13x18 mm ; 0.51x0.71 in)</li>
  1832. * <li><b>ISO 216 B Series + 2 SIS 014711 extensions</b></li>
  1833. * <li>B0 (1000x1414 mm ; 39.37x55.67 in)</li>
  1834. * <li>B1 (707x1000 mm ; 27.83x39.37 in)</li>
  1835. * <li>B2 (500x707 mm ; 19.69x27.83 in)</li>
  1836. * <li>B3 (353x500 mm ; 13.90x19.69 in)</li>
  1837. * <li>B4 (250x353 mm ; 9.84x13.90 in)</li>
  1838. * <li>B5 (176x250 mm ; 6.93x9.84 in)</li>
  1839. * <li>B6 (125x176 mm ; 4.92x6.93 in)</li>
  1840. * <li>B7 (88x125 mm ; 3.46x4.92 in)</li>
  1841. * <li>B8 (62x88 mm ; 2.44x3.46 in)</li>
  1842. * <li>B9 (44x62 mm ; 1.73x2.44 in)</li>
  1843. * <li>B10 (31x44 mm ; 1.22x1.73 in)</li>
  1844. * <li>B11 (22x31 mm ; 0.87x1.22 in)</li>
  1845. * <li>B12 (15x22 mm ; 0.59x0.87 in)</li>
  1846. * <li><b>ISO 216 C Series + 2 SIS 014711 extensions + 2 EXTENSION</b></li>
  1847. * <li>C0 (917x1297 mm ; 36.10x51.06 in)</li>
  1848. * <li>C1 (648x917 mm ; 25.51x36.10 in)</li>
  1849. * <li>C2 (458x648 mm ; 18.03x25.51 in)</li>
  1850. * <li>C3 (324x458 mm ; 12.76x18.03 in)</li>
  1851. * <li>C4 (229x324 mm ; 9.02x12.76 in)</li>
  1852. * <li>C5 (162x229 mm ; 6.38x9.02 in)</li>
  1853. * <li>C6 (114x162 mm ; 4.49x6.38 in)</li>
  1854. * <li>C7 (81x114 mm ; 3.19x4.49 in)</li>
  1855. * <li>C8 (57x81 mm ; 2.24x3.19 in)</li>
  1856. * <li>C9 (40x57 mm ; 1.57x2.24 in)</li>
  1857. * <li>C10 (28x40 mm ; 1.10x1.57 in)</li>
  1858. * <li>C11 (20x28 mm ; 0.79x1.10 in)</li>
  1859. * <li>C12 (14x20 mm ; 0.55x0.79 in)</li>
  1860. * <li>C76 (81x162 mm ; 3.19x6.38 in)</li>
  1861. * <li>DL (110x220 mm ; 4.33x8.66 in)</li>
  1862. * <li><b>SIS 014711 E Series</b></li>
  1863. * <li>E0 (879x1241 mm ; 34.61x48.86 in)</li>
  1864. * <li>E1 (620x879 mm ; 24.41x34.61 in)</li>
  1865. * <li>E2 (440x620 mm ; 17.32x24.41 in)</li>
  1866. * <li>E3 (310x440 mm ; 12.20x17.32 in)</li>
  1867. * <li>E4 (220x310 mm ; 8.66x12.20 in)</li>
  1868. * <li>E5 (155x220 mm ; 6.10x8.66 in)</li>
  1869. * <li>E6 (110x155 mm ; 4.33x6.10 in)</li>
  1870. * <li>E7 (78x110 mm ; 3.07x4.33 in)</li>
  1871. * <li>E8 (55x78 mm ; 2.17x3.07 in)</li>
  1872. * <li>E9 (39x55 mm ; 1.54x2.17 in)</li>
  1873. * <li>E10 (27x39 mm ; 1.06x1.54 in)</li>
  1874. * <li>E11 (19x27 mm ; 0.75x1.06 in)</li>
  1875. * <li>E12 (13x19 mm ; 0.51x0.75 in)</li>
  1876. * <li><b>SIS 014711 G Series</b></li>
  1877. * <li>G0 (958x1354 mm ; 37.72x53.31 in)</li>
  1878. * <li>G1 (677x958 mm ; 26.65x37.72 in)</li>
  1879. * <li>G2 (479x677 mm ; 18.86x26.65 in)</li>
  1880. * <li>G3 (338x479 mm ; 13.31x18.86 in)</li>
  1881. * <li>G4 (239x338 mm ; 9.41x13.31 in)</li>
  1882. * <li>G5 (169x239 mm ; 6.65x9.41 in)</li>
  1883. * <li>G6 (119x169 mm ; 4.69x6.65 in)</li>
  1884. * <li>G7 (84x119 mm ; 3.31x4.69 in)</li>
  1885. * <li>G8 (59x84 mm ; 2.32x3.31 in)</li>
  1886. * <li>G9 (42x59 mm ; 1.65x2.32 in)</li>
  1887. * <li>G10 (29x42 mm ; 1.14x1.65 in)</li>
  1888. * <li>G11 (21x29 mm ; 0.83x1.14 in)</li>
  1889. * <li>G12 (14x21 mm ; 0.55x0.83 in)</li>
  1890. * <li><b>ISO Press</b></li>
  1891. * <li>RA0 (860x1220 mm ; 33.86x48.03 in)</li>
  1892. * <li>RA1 (610x860 mm ; 24.02x33.86 in)</li>
  1893. * <li>RA2 (430x610 mm ; 16.93x24.02 in)</li>
  1894. * <li>RA3 (305x430 mm ; 12.01x16.93 in)</li>
  1895. * <li>RA4 (215x305 mm ; 8.46x12.01 in)</li>
  1896. * <li>SRA0 (900x1280 mm ; 35.43x50.39 in)</li>
  1897. * <li>SRA1 (640x900 mm ; 25.20x35.43 in)</li>
  1898. * <li>SRA2 (450x640 mm ; 17.72x25.20 in)</li>
  1899. * <li>SRA3 (320x450 mm ; 12.60x17.72 in)</li>
  1900. * <li>SRA4 (225x320 mm ; 8.86x12.60 in)</li>
  1901. * <li><b>German DIN 476</b></li>
  1902. * <li>4A0 (1682x2378 mm ; 66.22x93.62 in)</li>
  1903. * <li>2A0 (1189x1682 mm ; 46.81x66.22 in)</li>
  1904. * <li><b>Variations on the ISO Standard</b></li>
  1905. * <li>A2_EXTRA (445x619 mm ; 17.52x24.37 in)</li>
  1906. * <li>A3+ (329x483 mm ; 12.95x19.02 in)</li>
  1907. * <li>A3_EXTRA (322x445 mm ; 12.68x17.52 in)</li>
  1908. * <li>A3_SUPER (305x508 mm ; 12.01x20.00 in)</li>
  1909. * <li>SUPER_A3 (305x487 mm ; 12.01x19.17 in)</li>
  1910. * <li>A4_EXTRA (235x322 mm ; 9.25x12.68 in)</li>
  1911. * <li>A4_SUPER (229x322 mm ; 9.02x12.68 in)</li>
  1912. * <li>SUPER_A4 (227x356 mm ; 8.94x14.02 in)</li>
  1913. * <li>A4_LONG (210x348 mm ; 8.27x13.70 in)</li>
  1914. * <li>F4 (210x330 mm ; 8.27x12.99 in)</li>
  1915. * <li>SO_B5_EXTRA (202x276 mm ; 7.95x10.87 in)</li>
  1916. * <li>A5_EXTRA (173x235 mm ; 6.81x9.25 in)</li>
  1917. * <li><b>ANSI Series</b></li>
  1918. * <li>ANSI_E (864x1118 mm ; 34.00x44.00 in)</li>
  1919. * <li>ANSI_D (559x864 mm ; 22.00x34.00 in)</li>
  1920. * <li>ANSI_C (432x559 mm ; 17.00x22.00 in)</li>
  1921. * <li>ANSI_B (279x432 mm ; 11.00x17.00 in)</li>
  1922. * <li>ANSI_A (216x279 mm ; 8.50x11.00 in)</li>
  1923. * <li><b>Traditional 'Loose' North American Paper Sizes</b></li>
  1924. * <li>LEDGER, USLEDGER (432x279 mm ; 17.00x11.00 in)</li>
  1925. * <li>TABLOID, USTABLOID, BIBLE, ORGANIZERK (279x432 mm ; 11.00x17.00 in)</li>
  1926. * <li>LETTER, USLETTER, ORGANIZERM (216x279 mm ; 8.50x11.00 in)</li>
  1927. * <li>LEGAL, USLEGAL (216x356 mm ; 8.50x14.00 in)</li>
  1928. * <li>GLETTER, GOVERNMENTLETTER (203x267 mm ; 8.00x10.50 in)</li>
  1929. * <li>JLEGAL, JUNIORLEGAL (203x127 mm ; 8.00x5.00 in)</li>
  1930. * <li><b>Other North American Paper Sizes</b></li>
  1931. * <li>QUADDEMY (889x1143 mm ; 35.00x45.00 in)</li>
  1932. * <li>SUPER_B (330x483 mm ; 13.00x19.00 in)</li>
  1933. * <li>QUARTO (229x279 mm ; 9.00x11.00 in)</li>
  1934. * <li>FOLIO, GOVERNMENTLEGAL (216x330 mm ; 8.50x13.00 in)</li>
  1935. * <li>EXECUTIVE, MONARCH (184x267 mm ; 7.25x10.50 in)</li>
  1936. * <li>MEMO, STATEMENT, ORGANIZERL (140x216 mm ; 5.50x8.50 in)</li>
  1937. * <li>FOOLSCAP (210x330 mm ; 8.27x13.00 in)</li>
  1938. * <li>COMPACT (108x171 mm ; 4.25x6.75 in)</li>
  1939. * <li>ORGANIZERJ (70x127 mm ; 2.75x5.00 in)</li>
  1940. * <li><b>Canadian standard CAN 2-9.60M</b></li>
  1941. * <li>P1 (560x860 mm ; 22.05x33.86 in)</li>
  1942. * <li>P2 (430x560 mm ; 16.93x22.05 in)</li>
  1943. * <li>P3 (280x430 mm ; 11.02x16.93 in)</li>
  1944. * <li>P4 (215x280 mm ; 8.46x11.02 in)</li>
  1945. * <li>P5 (140x215 mm ; 5.51x8.46 in)</li>
  1946. * <li>P6 (107x140 mm ; 4.21x5.51 in)</li>
  1947. * <li><b>North American Architectural Sizes</b></li>
  1948. * <li>ARCH_E (914x1219 mm ; 36.00x48.00 in)</li>
  1949. * <li>ARCH_E1 (762x1067 mm ; 30.00x42.00 in)</li>
  1950. * <li>ARCH_D (610x914 mm ; 24.00x36.00 in)</li>
  1951. * <li>ARCH_C, BROADSHEET (457x610 mm ; 18.00x24.00 in)</li>
  1952. * <li>ARCH_B (305x457 mm ; 12.00x18.00 in)</li>
  1953. * <li>ARCH_A (229x305 mm ; 9.00x12.00 in)</li>
  1954. * <li><b>Announcement Envelopes</b></li>
  1955. * <li>ANNENV_A2 (111x146 mm ; 4.37x5.75 in)</li>
  1956. * <li>ANNENV_A6 (121x165 mm ; 4.75x6.50 in)</li>
  1957. * <li>ANNENV_A7 (133x184 mm ; 5.25x7.25 in)</li>
  1958. * <li>ANNENV_A8 (140x206 mm ; 5.50x8.12 in)</li>
  1959. * <li>ANNENV_A10 (159x244 mm ; 6.25x9.62 in)</li>
  1960. * <li>ANNENV_SLIM (98x225 mm ; 3.87x8.87 in)</li>
  1961. * <li><b>Commercial Envelopes</b></li>
  1962. * <li>COMMENV_N6_1/4 (89x152 mm ; 3.50x6.00 in)</li>
  1963. * <li>COMMENV_N6_3/4 (92x165 mm ; 3.62x6.50 in)</li>
  1964. * <li>COMMENV_N8 (98x191 mm ; 3.87x7.50 in)</li>
  1965. * <li>COMMENV_N9 (98x225 mm ; 3.87x8.87 in)</li>
  1966. * <li>COMMENV_N10 (105x241 mm ; 4.12x9.50 in)</li>
  1967. * <li>COMMENV_N11 (114x263 mm ; 4.50x10.37 in)</li>
  1968. * <li>COMMENV_N12 (121x279 mm ; 4.75x11.00 in)</li>
  1969. * <li>COMMENV_N14 (127x292 mm ; 5.00x11.50 in)</li>
  1970. * <li><b>Catalogue Envelopes</b></li>
  1971. * <li>CATENV_N1 (152x229 mm ; 6.00x9.00 in)</li>
  1972. * <li>CATENV_N1_3/4 (165x241 mm ; 6.50x9.50 in)</li>
  1973. * <li>CATENV_N2 (165x254 mm ; 6.50x10.00 in)</li>
  1974. * <li>CATENV_N3 (178x254 mm ; 7.00x10.00 in)</li>
  1975. * <li>CATENV_N6 (191x267 mm ; 7.50x10.50 in)</li>
  1976. * <li>CATENV_N7 (203x279 mm ; 8.00x11.00 in)</li>
  1977. * <li>CATENV_N8 (210x286 mm ; 8.25x11.25 in)</li>
  1978. * <li>CATENV_N9_1/2 (216x267 mm ; 8.50x10.50 in)</li>
  1979. * <li>CATENV_N9_3/4 (222x286 mm ; 8.75x11.25 in)</li>
  1980. * <li>CATENV_N10_1/2 (229x305 mm ; 9.00x12.00 in)</li>
  1981. * <li>CATENV_N12_1/2 (241x318 mm ; 9.50x12.50 in)</li>
  1982. * <li>CATENV_N13_1/2 (254x330 mm ; 10.00x13.00 in)</li>
  1983. * <li>CATENV_N14_1/4 (286x311 mm ; 11.25x12.25 in)</li>
  1984. * <li>CATENV_N14_1/2 (292x368 mm ; 11.50x14.50 in)</li>
  1985. * <li><b>Japanese (JIS P 0138-61) Standard B-Series</b></li>
  1986. * <li>JIS_B0 (1030x1456 mm ; 40.55x57.32 in)</li>
  1987. * <li>JIS_B1 (728x1030 mm ; 28.66x40.55 in)</li>
  1988. * <li>JIS_B2 (515x728 mm ; 20.28x28.66 in)</li>
  1989. * <li>JIS_B3 (364x515 mm ; 14.33x20.28 in)</li>
  1990. * <li>JIS_B4 (257x364 mm ; 10.12x14.33 in)</li>
  1991. * <li>JIS_B5 (182x257 mm ; 7.17x10.12 in)</li>
  1992. * <li>JIS_B6 (128x182 mm ; 5.04x7.17 in)</li>
  1993. * <li>JIS_B7 (91x128 mm ; 3.58x5.04 in)</li>
  1994. * <li>JIS_B8 (64x91 mm ; 2.52x3.58 in)</li>
  1995. * <li>JIS_B9 (45x64 mm ; 1.77x2.52 in)</li>
  1996. * <li>JIS_B10 (32x45 mm ; 1.26x1.77 in)</li>
  1997. * <li>JIS_B11 (22x32 mm ; 0.87x1.26 in)</li>
  1998. * <li>JIS_B12 (16x22 mm ; 0.63x0.87 in)</li>
  1999. * <li><b>PA Series</b></li>
  2000. * <li>PA0 (840x1120 mm ; 33.07x44.09 in)</li>
  2001. * <li>PA1 (560x840 mm ; 22.05x33.07 in)</li>
  2002. * <li>PA2 (420x560 mm ; 16.54x22.05 in)</li>
  2003. * <li>PA3 (280x420 mm ; 11.02x16.54 in)</li>
  2004. * <li>PA4 (210x280 mm ; 8.27x11.02 in)</li>
  2005. * <li>PA5 (140x210 mm ; 5.51x8.27 in)</li>
  2006. * <li>PA6 (105x140 mm ; 4.13x5.51 in)</li>
  2007. * <li>PA7 (70x105 mm ; 2.76x4.13 in)</li>
  2008. * <li>PA8 (52x70 mm ; 2.05x2.76 in)</li>
  2009. * <li>PA9 (35x52 mm ; 1.38x2.05 in)</li>
  2010. * <li>PA10 (26x35 mm ; 1.02x1.38 in)</li>
  2011. * <li><b>Standard Photographic Print Sizes</b></li>
  2012. * <li>PASSPORT_PHOTO (35x45 mm ; 1.38x1.77 in)</li>
  2013. * <li>E (82x120 mm ; 3.25x4.72 in)</li>
  2014. * <li>3R, L (89x127 mm ; 3.50x5.00 in)</li>
  2015. * <li>4R, KG (102x152 mm ; 4.02x5.98 in)</li>
  2016. * <li>4D (120x152 mm ; 4.72x5.98 in)</li>
  2017. * <li>5R, 2L (127x178 mm ; 5.00x7.01 in)</li>
  2018. * <li>6R, 8P (152x203 mm ; 5.98x7.99 in)</li>
  2019. * <li>8R, 6P (203x254 mm ; 7.99x10.00 in)</li>
  2020. * <li>S8R, 6PW (203x305 mm ; 7.99x12.01 in)</li>
  2021. * <li>10R, 4P (254x305 mm ; 10.00x12.01 in)</li>
  2022. * <li>S10R, 4PW (254x381 mm ; 10.00x15.00 in)</li>
  2023. * <li>11R (279x356 mm ; 10.98x14.02 in)</li>
  2024. * <li>S11R (279x432 mm ; 10.98x17.01 in)</li>
  2025. * <li>12R (305x381 mm ; 12.01x15.00 in)</li>
  2026. * <li>S12R (305x456 mm ; 12.01x17.95 in)</li>
  2027. * <li><b>Common Newspaper Sizes</b></li>
  2028. * <li>NEWSPAPER_BROADSHEET (750x600 mm ; 29.53x23.62 in)</li>
  2029. * <li>NEWSPAPER_BERLINER (470x315 mm ; 18.50x12.40 in)</li>
  2030. * <li>NEWSPAPER_COMPACT, NEWSPAPER_TABLOID (430x280 mm ; 16.93x11.02 in)</li>
  2031. * <li><b>Business Cards</b></li>
  2032. * <li>CREDIT_CARD, BUSINESS_CARD, BUSINESS_CARD_ISO7810 (54x86 mm ; 2.13x3.37 in)</li>
  2033. * <li>BUSINESS_CARD_ISO216 (52x74 mm ; 2.05x2.91 in)</li>
  2034. * <li>BUSINESS_CARD_IT, BUSINESS_CARD_UK, BUSINESS_CARD_FR, BUSINESS_CARD_DE, BUSINESS_CARD_ES (55x85 mm ; 2.17x3.35 in)</li>
  2035. * <li>BUSINESS_CARD_US, BUSINESS_CARD_CA (51x89 mm ; 2.01x3.50 in)</li>
  2036. * <li>BUSINESS_CARD_JP (55x91 mm ; 2.17x3.58 in)</li>
  2037. * <li>BUSINESS_CARD_HK (54x90 mm ; 2.13x3.54 in)</li>
  2038. * <li>BUSINESS_CARD_AU, BUSINESS_CARD_DK, BUSINESS_CARD_SE (55x90 mm ; 2.17x3.54 in)</li>
  2039. * <li>BUSINESS_CARD_RU, BUSINESS_CARD_CZ, BUSINESS_CARD_FI, BUSINESS_CARD_HU, BUSINESS_CARD_IL (50x90 mm ; 1.97x3.54 in)</li>
  2040. * <li><b>Billboards</b></li>
  2041. * <li>4SHEET (1016x1524 mm ; 40.00x60.00 in)</li>
  2042. * <li>6SHEET (1200x1800 mm ; 47.24x70.87 in)</li>
  2043. * <li>12SHEET (3048x1524 mm ; 120.00x60.00 in)</li>
  2044. * <li>16SHEET (2032x3048 mm ; 80.00x120.00 in)</li>
  2045. * <li>32SHEET (4064x3048 mm ; 160.00x120.00 in)</li>
  2046. * <li>48SHEET (6096x3048 mm ; 240.00x120.00 in)</li>
  2047. * <li>64SHEET (8128x3048 mm ; 320.00x120.00 in)</li>
  2048. * <li>96SHEET (12192x3048 mm ; 480.00x120.00 in)</li>
  2049. * <li><b>Old Imperial English (some are still used in USA)</b></li>
  2050. * <li>EN_EMPEROR (1219x1829 mm ; 48.00x72.00 in)</li>
  2051. * <li>EN_ANTIQUARIAN (787x1346 mm ; 31.00x53.00 in)</li>
  2052. * <li>EN_GRAND_EAGLE (730x1067 mm ; 28.75x42.00 in)</li>
  2053. * <li>EN_DOUBLE_ELEPHANT (679x1016 mm ; 26.75x40.00 in)</li>
  2054. * <li>EN_ATLAS (660x864 mm ; 26.00x34.00 in)</li>
  2055. * <li>EN_COLOMBIER (597x876 mm ; 23.50x34.50 in)</li>
  2056. * <li>EN_ELEPHANT (584x711 mm ; 23.00x28.00 in)</li>
  2057. * <li>EN_DOUBLE_DEMY (572x902 mm ; 22.50x35.50 in)</li>
  2058. * <li>EN_IMPERIAL (559x762 mm ; 22.00x30.00 in)</li>
  2059. * <li>EN_PRINCESS (546x711 mm ; 21.50x28.00 in)</li>
  2060. * <li>EN_CARTRIDGE (533x660 mm ; 21.00x26.00 in)</li>
  2061. * <li>EN_DOUBLE_LARGE_POST (533x838 mm ; 21.00x33.00 in)</li>
  2062. * <li>EN_ROYAL (508x635 mm ; 20.00x25.00 in)</li>
  2063. * <li>EN_SHEET, EN_HALF_POST (495x597 mm ; 19.50x23.50 in)</li>
  2064. * <li>EN_SUPER_ROYAL (483x686 mm ; 19.00x27.00 in)</li>
  2065. * <li>EN_DOUBLE_POST (483x775 mm ; 19.00x30.50 in)</li>
  2066. * <li>EN_MEDIUM (445x584 mm ; 17.50x23.00 in)</li>
  2067. * <li>EN_DEMY (445x572 mm ; 17.50x22.50 in)</li>
  2068. * <li>EN_LARGE_POST (419x533 mm ; 16.50x21.00 in)</li>
  2069. * <li>EN_COPY_DRAUGHT (406x508 mm ; 16.00x20.00 in)</li>
  2070. * <li>EN_POST (394x489 mm ; 15.50x19.25 in)</li>
  2071. * <li>EN_CROWN (381x508 mm ; 15.00x20.00 in)</li>
  2072. * <li>EN_PINCHED_POST (375x470 mm ; 14.75x18.50 in)</li>
  2073. * <li>EN_BRIEF (343x406 mm ; 13.50x16.00 in)</li>
  2074. * <li>EN_FOOLSCAP (343x432 mm ; 13.50x17.00 in)</li>
  2075. * <li>EN_SMALL_FOOLSCAP (337x419 mm ; 13.25x16.50 in)</li>
  2076. * <li>EN_POTT (318x381 mm ; 12.50x15.00 in)</li>
  2077. * <li><b>Old Imperial Belgian</b></li>
  2078. * <li>BE_GRAND_AIGLE (700x1040 mm ; 27.56x40.94 in)</li>
  2079. * <li>BE_COLOMBIER (620x850 mm ; 24.41x33.46 in)</li>
  2080. * <li>BE_DOUBLE_CARRE (620x920 mm ; 24.41x36.22 in)</li>
  2081. * <li>BE_ELEPHANT (616x770 mm ; 24.25x30.31 in)</li>
  2082. * <li>BE_PETIT_AIGLE (600x840 mm ; 23.62x33.07 in)</li>
  2083. * <li>BE_GRAND_JESUS (550x730 mm ; 21.65x28.74 in)</li>
  2084. * <li>BE_JESUS (540x730 mm ; 21.26x28.74 in)</li>
  2085. * <li>BE_RAISIN (500x650 mm ; 19.69x25.59 in)</li>
  2086. * <li>BE_GRAND_MEDIAN (460x605 mm ; 18.11x23.82 in)</li>
  2087. * <li>BE_DOUBLE_POSTE (435x565 mm ; 17.13x22.24 in)</li>
  2088. * <li>BE_COQUILLE (430x560 mm ; 16.93x22.05 in)</li>
  2089. * <li>BE_PETIT_MEDIAN (415x530 mm ; 16.34x20.87 in)</li>
  2090. * <li>BE_RUCHE (360x460 mm ; 14.17x18.11 in)</li>
  2091. * <li>BE_PROPATRIA (345x430 mm ; 13.58x16.93 in)</li>
  2092. * <li>BE_LYS (317x397 mm ; 12.48x15.63 in)</li>
  2093. * <li>BE_POT (307x384 mm ; 12.09x15.12 in)</li>
  2094. * <li>BE_ROSETTE (270x347 mm ; 10.63x13.66 in)</li>
  2095. * <li><b>Old Imperial French</b></li>
  2096. * <li>FR_UNIVERS (1000x1300 mm ; 39.37x51.18 in)</li>
  2097. * <li>FR_DOUBLE_COLOMBIER (900x1260 mm ; 35.43x49.61 in)</li>
  2098. * <li>FR_GRANDE_MONDE (900x1260 mm ; 35.43x49.61 in)</li>
  2099. * <li>FR_DOUBLE_SOLEIL (800x1200 mm ; 31.50x47.24 in)</li>
  2100. * <li>FR_DOUBLE_JESUS (760x1120 mm ; 29.92x44.09 in)</li>
  2101. * <li>FR_GRAND_AIGLE (750x1060 mm ; 29.53x41.73 in)</li>
  2102. * <li>FR_PETIT_AIGLE (700x940 mm ; 27.56x37.01 in)</li>
  2103. * <li>FR_DOUBLE_RAISIN (650x1000 mm ; 25.59x39.37 in)</li>
  2104. * <li>FR_JOURNAL (650x940 mm ; 25.59x37.01 in)</li>
  2105. * <li>FR_COLOMBIER_AFFICHE (630x900 mm ; 24.80x35.43 in)</li>
  2106. * <li>FR_DOUBLE_CAVALIER (620x920 mm ; 24.41x36.22 in)</li>
  2107. * <li>FR_CLOCHE (600x800 mm ; 23.62x31.50 in)</li>
  2108. * <li>FR_SOLEIL (600x800 mm ; 23.62x31.50 in)</li>
  2109. * <li>FR_DOUBLE_CARRE (560x900 mm ; 22.05x35.43 in)</li>
  2110. * <li>FR_DOUBLE_COQUILLE (560x880 mm ; 22.05x34.65 in)</li>
  2111. * <li>FR_JESUS (560x760 mm ; 22.05x29.92 in)</li>
  2112. * <li>FR_RAISIN (500x650 mm ; 19.69x25.59 in)</li>
  2113. * <li>FR_CAVALIER (460x620 mm ; 18.11x24.41 in)</li>
  2114. * <li>FR_DOUBLE_COURONNE (460x720 mm ; 18.11x28.35 in)</li>
  2115. * <li>FR_CARRE (450x560 mm ; 17.72x22.05 in)</li>
  2116. * <li>FR_COQUILLE (440x560 mm ; 17.32x22.05 in)</li>
  2117. * <li>FR_DOUBLE_TELLIERE (440x680 mm ; 17.32x26.77 in)</li>
  2118. * <li>FR_DOUBLE_CLOCHE (400x600 mm ; 15.75x23.62 in)</li>
  2119. * <li>FR_DOUBLE_POT (400x620 mm ; 15.75x24.41 in)</li>
  2120. * <li>FR_ECU (400x520 mm ; 15.75x20.47 in)</li>
  2121. * <li>FR_COURONNE (360x460 mm ; 14.17x18.11 in)</li>
  2122. * <li>FR_TELLIERE (340x440 mm ; 13.39x17.32 in)</li>
  2123. * <li>FR_POT (310x400 mm ; 12.20x15.75 in)</li>
  2124. * </ul>
  2125. * @return array containing page width and height in points
  2126. * @public
  2127. * @since 5.0.010 (2010-05-17)
  2128. */
  2129. public function getPageSizeFromFormat($format) {
  2130. // Paper cordinates are calculated in this way: (inches * 72) where (1 inch = 25.4 mm)
  2131. switch (strtoupper($format)) {
  2132. // ISO 216 A Series + 2 SIS 014711 extensions
  2133. case 'A0' : {$pf = array( 2383.937, 3370.394); break;}
  2134. case 'A1' : {$pf = array( 1683.780, 2383.937); break;}
  2135. case 'A2' : {$pf = array( 1190.551, 1683.780); break;}
  2136. case 'A3' : {$pf = array( 841.890, 1190.551); break;}
  2137. case 'A4' : {$pf = array( 595.276, 841.890); break;}
  2138. case 'A5' : {$pf = array( 419.528, 595.276); break;}
  2139. case 'A6' : {$pf = array( 297.638, 419.528); break;}
  2140. case 'A7' : {$pf = array( 209.764, 297.638); break;}
  2141. case 'A8' : {$pf = array( 147.402, 209.764); break;}
  2142. case 'A9' : {$pf = array( 104.882, 147.402); break;}
  2143. case 'A10': {$pf = array( 73.701, 104.882); break;}
  2144. case 'A11': {$pf = array( 51.024, 73.701); break;}
  2145. case 'A12': {$pf = array( 36.850, 51.024); break;}
  2146. // ISO 216 B Series + 2 SIS 014711 extensions
  2147. case 'B0' : {$pf = array( 2834.646, 4008.189); break;}
  2148. case 'B1' : {$pf = array( 2004.094, 2834.646); break;}
  2149. case 'B2' : {$pf = array( 1417.323, 2004.094); break;}
  2150. case 'B3' : {$pf = array( 1000.630, 1417.323); break;}
  2151. case 'B4' : {$pf = array( 708.661, 1000.630); break;}
  2152. case 'B5' : {$pf = array( 498.898, 708.661); break;}
  2153. case 'B6' : {$pf = array( 354.331, 498.898); break;}
  2154. case 'B7' : {$pf = array( 249.449, 354.331); break;}
  2155. case 'B8' : {$pf = array( 175.748, 249.449); break;}
  2156. case 'B9' : {$pf = array( 124.724, 175.748); break;}
  2157. case 'B10': {$pf = array( 87.874, 124.724); break;}
  2158. case 'B11': {$pf = array( 62.362, 87.874); break;}
  2159. case 'B12': {$pf = array( 42.520, 62.362); break;}
  2160. // ISO 216 C Series + 2 SIS 014711 extensions + 2 EXTENSION
  2161. case 'C0' : {$pf = array( 2599.370, 3676.535); break;}
  2162. case 'C1' : {$pf = array( 1836.850, 2599.370); break;}
  2163. case 'C2' : {$pf = array( 1298.268, 1836.850); break;}
  2164. case 'C3' : {$pf = array( 918.425, 1298.268); break;}
  2165. case 'C4' : {$pf = array( 649.134, 918.425); break;}
  2166. case 'C5' : {$pf = array( 459.213, 649.134); break;}
  2167. case 'C6' : {$pf = array( 323.150, 459.213); break;}
  2168. case 'C7' : {$pf = array( 229.606, 323.150); break;}
  2169. case 'C8' : {$pf = array( 161.575, 229.606); break;}
  2170. case 'C9' : {$pf = array( 113.386, 161.575); break;}
  2171. case 'C10': {$pf = array( 79.370, 113.386); break;}
  2172. case 'C11': {$pf = array( 56.693, 79.370); break;}
  2173. case 'C12': {$pf = array( 39.685, 56.693); break;}
  2174. case 'C76': {$pf = array( 229.606, 459.213); break;}
  2175. case 'DL' : {$pf = array( 311.811, 623.622); break;}
  2176. // SIS 014711 E Series
  2177. case 'E0' : {$pf = array( 2491.654, 3517.795); break;}
  2178. case 'E1' : {$pf = array( 1757.480, 2491.654); break;}
  2179. case 'E2' : {$pf = array( 1247.244, 1757.480); break;}
  2180. case 'E3' : {$pf = array( 878.740, 1247.244); break;}
  2181. case 'E4' : {$pf = array( 623.622, 878.740); break;}
  2182. case 'E5' : {$pf = array( 439.370, 623.622); break;}
  2183. case 'E6' : {$pf = array( 311.811, 439.370); break;}
  2184. case 'E7' : {$pf = array( 221.102, 311.811); break;}
  2185. case 'E8' : {$pf = array( 155.906, 221.102); break;}
  2186. case 'E9' : {$pf = array( 110.551, 155.906); break;}
  2187. case 'E10': {$pf = array( 76.535, 110.551); break;}
  2188. case 'E11': {$pf = array( 53.858, 76.535); break;}
  2189. case 'E12': {$pf = array( 36.850, 53.858); break;}
  2190. // SIS 014711 G Series
  2191. case 'G0' : {$pf = array( 2715.591, 3838.110); break;}
  2192. case 'G1' : {$pf = array( 1919.055, 2715.591); break;}
  2193. case 'G2' : {$pf = array( 1357.795, 1919.055); break;}
  2194. case 'G3' : {$pf = array( 958.110, 1357.795); break;}
  2195. case 'G4' : {$pf = array( 677.480, 958.110); break;}
  2196. case 'G5' : {$pf = array( 479.055, 677.480); break;}
  2197. case 'G6' : {$pf = array( 337.323, 479.055); break;}
  2198. case 'G7' : {$pf = array( 238.110, 337.323); break;}
  2199. case 'G8' : {$pf = array( 167.244, 238.110); break;}
  2200. case 'G9' : {$pf = array( 119.055, 167.244); break;}
  2201. case 'G10': {$pf = array( 82.205, 119.055); break;}
  2202. case 'G11': {$pf = array( 59.528, 82.205); break;}
  2203. case 'G12': {$pf = array( 39.685, 59.528); break;}
  2204. // ISO Press
  2205. case 'RA0': {$pf = array( 2437.795, 3458.268); break;}
  2206. case 'RA1': {$pf = array( 1729.134, 2437.795); break;}
  2207. case 'RA2': {$pf = array( 1218.898, 1729.134); break;}
  2208. case 'RA3': {$pf = array( 864.567, 1218.898); break;}
  2209. case 'RA4': {$pf = array( 609.449, 864.567); break;}
  2210. case 'SRA0': {$pf = array( 2551.181, 3628.346); break;}
  2211. case 'SRA1': {$pf = array( 1814.173, 2551.181); break;}
  2212. case 'SRA2': {$pf = array( 1275.591, 1814.173); break;}
  2213. case 'SRA3': {$pf = array( 907.087, 1275.591); break;}
  2214. case 'SRA4': {$pf = array( 637.795, 907.087); break;}
  2215. // German DIN 476
  2216. case '4A0': {$pf = array( 4767.874, 6740.787); break;}
  2217. case '2A0': {$pf = array( 3370.394, 4767.874); break;}
  2218. // Variations on the ISO Standard
  2219. case 'A2_EXTRA' : {$pf = array( 1261.417, 1754.646); break;}
  2220. case 'A3+' : {$pf = array( 932.598, 1369.134); break;}
  2221. case 'A3_EXTRA' : {$pf = array( 912.756, 1261.417); break;}
  2222. case 'A3_SUPER' : {$pf = array( 864.567, 1440.000); break;}
  2223. case 'SUPER_A3' : {$pf = array( 864.567, 1380.472); break;}
  2224. case 'A4_EXTRA' : {$pf = array( 666.142, 912.756); break;}
  2225. case 'A4_SUPER' : {$pf = array( 649.134, 912.756); break;}
  2226. case 'SUPER_A4' : {$pf = array( 643.465, 1009.134); break;}
  2227. case 'A4_LONG' : {$pf = array( 595.276, 986.457); break;}
  2228. case 'F4' : {$pf = array( 595.276, 935.433); break;}
  2229. case 'SO_B5_EXTRA': {$pf = array( 572.598, 782.362); break;}
  2230. case 'A5_EXTRA' : {$pf = array( 490.394, 666.142); break;}
  2231. // ANSI Series
  2232. case 'ANSI_E': {$pf = array( 2448.000, 3168.000); break;}
  2233. case 'ANSI_D': {$pf = array( 1584.000, 2448.000); break;}
  2234. case 'ANSI_C': {$pf = array( 1224.000, 1584.000); break;}
  2235. case 'ANSI_B': {$pf = array( 792.000, 1224.000); break;}
  2236. case 'ANSI_A': {$pf = array( 612.000, 792.000); break;}
  2237. // Traditional 'Loose' North American Paper Sizes
  2238. case 'USLEDGER':
  2239. case 'LEDGER' : {$pf = array( 1224.000, 792.000); break;}
  2240. case 'ORGANIZERK':
  2241. case 'BIBLE':
  2242. case 'USTABLOID':
  2243. case 'TABLOID': {$pf = array( 792.000, 1224.000); break;}
  2244. case 'ORGANIZERM':
  2245. case 'USLETTER':
  2246. case 'LETTER' : {$pf = array( 612.000, 792.000); break;}
  2247. case 'USLEGAL':
  2248. case 'LEGAL' : {$pf = array( 612.000, 1008.000); break;}
  2249. case 'GOVERNMENTLETTER':
  2250. case 'GLETTER': {$pf = array( 576.000, 756.000); break;}
  2251. case 'JUNIORLEGAL':
  2252. case 'JLEGAL' : {$pf = array( 576.000, 360.000); break;}
  2253. // Other North American Paper Sizes
  2254. case 'QUADDEMY': {$pf = array( 2520.000, 3240.000); break;}
  2255. case 'SUPER_B': {$pf = array( 936.000, 1368.000); break;}
  2256. case 'QUARTO': {$pf = array( 648.000, 792.000); break;}
  2257. case 'GOVERNMENTLEGAL':
  2258. case 'FOLIO': {$pf = array( 612.000, 936.000); break;}
  2259. case 'MONARCH':
  2260. case 'EXECUTIVE': {$pf = array( 522.000, 756.000); break;}
  2261. case 'ORGANIZERL':
  2262. case 'STATEMENT':
  2263. case 'MEMO': {$pf = array( 396.000, 612.000); break;}
  2264. case 'FOOLSCAP': {$pf = array( 595.440, 936.000); break;}
  2265. case 'COMPACT': {$pf = array( 306.000, 486.000); break;}
  2266. case 'ORGANIZERJ': {$pf = array( 198.000, 360.000); break;}
  2267. // Canadian standard CAN 2-9.60M
  2268. case 'P1': {$pf = array( 1587.402, 2437.795); break;}
  2269. case 'P2': {$pf = array( 1218.898, 1587.402); break;}
  2270. case 'P3': {$pf = array( 793.701, 1218.898); break;}
  2271. case 'P4': {$pf = array( 609.449, 793.701); break;}
  2272. case 'P5': {$pf = array( 396.850, 609.449); break;}
  2273. case 'P6': {$pf = array( 303.307, 396.850); break;}
  2274. // North American Architectural Sizes
  2275. case 'ARCH_E' : {$pf = array( 2592.000, 3456.000); break;}
  2276. case 'ARCH_E1': {$pf = array( 2160.000, 3024.000); break;}
  2277. case 'ARCH_D' : {$pf = array( 1728.000, 2592.000); break;}
  2278. case 'BROADSHEET':
  2279. case 'ARCH_C' : {$pf = array( 1296.000, 1728.000); break;}
  2280. case 'ARCH_B' : {$pf = array( 864.000, 1296.000); break;}
  2281. case 'ARCH_A' : {$pf = array( 648.000, 864.000); break;}
  2282. // --- North American Envelope Sizes ---
  2283. // - Announcement Envelopes
  2284. case 'ANNENV_A2' : {$pf = array( 314.640, 414.000); break;}
  2285. case 'ANNENV_A6' : {$pf = array( 342.000, 468.000); break;}
  2286. case 'ANNENV_A7' : {$pf = array( 378.000, 522.000); break;}
  2287. case 'ANNENV_A8' : {$pf = array( 396.000, 584.640); break;}
  2288. case 'ANNENV_A10' : {$pf = array( 450.000, 692.640); break;}
  2289. case 'ANNENV_SLIM': {$pf = array( 278.640, 638.640); break;}
  2290. // - Commercial Envelopes
  2291. case 'COMMENV_N6_1/4': {$pf = array( 252.000, 432.000); break;}
  2292. case 'COMMENV_N6_3/4': {$pf = array( 260.640, 468.000); break;}
  2293. case 'COMMENV_N8' : {$pf = array( 278.640, 540.000); break;}
  2294. case 'COMMENV_N9' : {$pf = array( 278.640, 638.640); break;}
  2295. case 'COMMENV_N10' : {$pf = array( 296.640, 684.000); break;}
  2296. case 'COMMENV_N11' : {$pf = array( 324.000, 746.640); break;}
  2297. case 'COMMENV_N12' : {$pf = array( 342.000, 792.000); break;}
  2298. case 'COMMENV_N14' : {$pf = array( 360.000, 828.000); break;}
  2299. // - Catalogue Envelopes
  2300. case 'CATENV_N1' : {$pf = array( 432.000, 648.000); break;}
  2301. case 'CATENV_N1_3/4' : {$pf = array( 468.000, 684.000); break;}
  2302. case 'CATENV_N2' : {$pf = array( 468.000, 720.000); break;}
  2303. case 'CATENV_N3' : {$pf = array( 504.000, 720.000); break;}
  2304. case 'CATENV_N6' : {$pf = array( 540.000, 756.000); break;}
  2305. case 'CATENV_N7' : {$pf = array( 576.000, 792.000); break;}
  2306. case 'CATENV_N8' : {$pf = array( 594.000, 810.000); break;}
  2307. case 'CATENV_N9_1/2' : {$pf = array( 612.000, 756.000); break;}
  2308. case 'CATENV_N9_3/4' : {$pf = array( 630.000, 810.000); break;}
  2309. case 'CATENV_N10_1/2': {$pf = array( 648.000, 864.000); break;}
  2310. case 'CATENV_N12_1/2': {$pf = array( 684.000, 900.000); break;}
  2311. case 'CATENV_N13_1/2': {$pf = array( 720.000, 936.000); break;}
  2312. case 'CATENV_N14_1/4': {$pf = array( 810.000, 882.000); break;}
  2313. case 'CATENV_N14_1/2': {$pf = array( 828.000, 1044.000); break;}
  2314. // Japanese (JIS P 0138-61) Standard B-Series
  2315. case 'JIS_B0' : {$pf = array( 2919.685, 4127.244); break;}
  2316. case 'JIS_B1' : {$pf = array( 2063.622, 2919.685); break;}
  2317. case 'JIS_B2' : {$pf = array( 1459.843, 2063.622); break;}
  2318. case 'JIS_B3' : {$pf = array( 1031.811, 1459.843); break;}
  2319. case 'JIS_B4' : {$pf = array( 728.504, 1031.811); break;}
  2320. case 'JIS_B5' : {$pf = array( 515.906, 728.504); break;}
  2321. case 'JIS_B6' : {$pf = array( 362.835, 515.906); break;}
  2322. case 'JIS_B7' : {$pf = array( 257.953, 362.835); break;}
  2323. case 'JIS_B8' : {$pf = array( 181.417, 257.953); break;}
  2324. case 'JIS_B9' : {$pf = array( 127.559, 181.417); break;}
  2325. case 'JIS_B10': {$pf = array( 90.709, 127.559); break;}
  2326. case 'JIS_B11': {$pf = array( 62.362, 90.709); break;}
  2327. case 'JIS_B12': {$pf = array( 45.354, 62.362); break;}
  2328. // PA Series
  2329. case 'PA0' : {$pf = array( 2381.102, 3174.803,); break;}
  2330. case 'PA1' : {$pf = array( 1587.402, 2381.102); break;}
  2331. case 'PA2' : {$pf = array( 1190.551, 1587.402); break;}
  2332. case 'PA3' : {$pf = array( 793.701, 1190.551); break;}
  2333. case 'PA4' : {$pf = array( 595.276, 793.701); break;}
  2334. case 'PA5' : {$pf = array( 396.850, 595.276); break;}
  2335. case 'PA6' : {$pf = array( 297.638, 396.850); break;}
  2336. case 'PA7' : {$pf = array( 198.425, 297.638); break;}
  2337. case 'PA8' : {$pf = array( 147.402, 198.425); break;}
  2338. case 'PA9' : {$pf = array( 99.213, 147.402); break;}
  2339. case 'PA10': {$pf = array( 73.701, 99.213); break;}
  2340. // Standard Photographic Print Sizes
  2341. case 'PASSPORT_PHOTO': {$pf = array( 99.213, 127.559); break;}
  2342. case 'E' : {$pf = array( 233.858, 340.157); break;}
  2343. case 'L':
  2344. case '3R' : {$pf = array( 252.283, 360.000); break;}
  2345. case 'KG':
  2346. case '4R' : {$pf = array( 289.134, 430.866); break;}
  2347. case '4D' : {$pf = array( 340.157, 430.866); break;}
  2348. case '2L':
  2349. case '5R' : {$pf = array( 360.000, 504.567); break;}
  2350. case '8P':
  2351. case '6R' : {$pf = array( 430.866, 575.433); break;}
  2352. case '6P':
  2353. case '8R' : {$pf = array( 575.433, 720.000); break;}
  2354. case '6PW':
  2355. case 'S8R' : {$pf = array( 575.433, 864.567); break;}
  2356. case '4P':
  2357. case '10R' : {$pf = array( 720.000, 864.567); break;}
  2358. case '4PW':
  2359. case 'S10R': {$pf = array( 720.000, 1080.000); break;}
  2360. case '11R' : {$pf = array( 790.866, 1009.134); break;}
  2361. case 'S11R': {$pf = array( 790.866, 1224.567); break;}
  2362. case '12R' : {$pf = array( 864.567, 1080.000); break;}
  2363. case 'S12R': {$pf = array( 864.567, 1292.598); break;}
  2364. // Common Newspaper Sizes
  2365. case 'NEWSPAPER_BROADSHEET': {$pf = array( 2125.984, 1700.787); break;}
  2366. case 'NEWSPAPER_BERLINER' : {$pf = array( 1332.283, 892.913); break;}
  2367. case 'NEWSPAPER_TABLOID':
  2368. case 'NEWSPAPER_COMPACT' : {$pf = array( 1218.898, 793.701); break;}
  2369. // Business Cards
  2370. case 'CREDIT_CARD':
  2371. case 'BUSINESS_CARD':
  2372. case 'BUSINESS_CARD_ISO7810': {$pf = array( 153.014, 242.646); break;}
  2373. case 'BUSINESS_CARD_ISO216' : {$pf = array( 147.402, 209.764); break;}
  2374. case 'BUSINESS_CARD_IT':
  2375. case 'BUSINESS_CARD_UK':
  2376. case 'BUSINESS_CARD_FR':
  2377. case 'BUSINESS_CARD_DE':
  2378. case 'BUSINESS_CARD_ES' : {$pf = array( 155.906, 240.945); break;}
  2379. case 'BUSINESS_CARD_CA':
  2380. case 'BUSINESS_CARD_US' : {$pf = array( 144.567, 252.283); break;}
  2381. case 'BUSINESS_CARD_JP' : {$pf = array( 155.906, 257.953); break;}
  2382. case 'BUSINESS_CARD_HK' : {$pf = array( 153.071, 255.118); break;}
  2383. case 'BUSINESS_CARD_AU':
  2384. case 'BUSINESS_CARD_DK':
  2385. case 'BUSINESS_CARD_SE' : {$pf = array( 155.906, 255.118); break;}
  2386. case 'BUSINESS_CARD_RU':
  2387. case 'BUSINESS_CARD_CZ':
  2388. case 'BUSINESS_CARD_FI':
  2389. case 'BUSINESS_CARD_HU':
  2390. case 'BUSINESS_CARD_IL' : {$pf = array( 141.732, 255.118); break;}
  2391. // Billboards
  2392. case '4SHEET' : {$pf = array( 2880.000, 4320.000); break;}
  2393. case '6SHEET' : {$pf = array( 3401.575, 5102.362); break;}
  2394. case '12SHEET': {$pf = array( 8640.000, 4320.000); break;}
  2395. case '16SHEET': {$pf = array( 5760.000, 8640.000); break;}
  2396. case '32SHEET': {$pf = array(11520.000, 8640.000); break;}
  2397. case '48SHEET': {$pf = array(17280.000, 8640.000); break;}
  2398. case '64SHEET': {$pf = array(23040.000, 8640.000); break;}
  2399. case '96SHEET': {$pf = array(34560.000, 8640.000); break;}
  2400. // Old European Sizes
  2401. // - Old Imperial English Sizes
  2402. case 'EN_EMPEROR' : {$pf = array( 3456.000, 5184.000); break;}
  2403. case 'EN_ANTIQUARIAN' : {$pf = array( 2232.000, 3816.000); break;}
  2404. case 'EN_GRAND_EAGLE' : {$pf = array( 2070.000, 3024.000); break;}
  2405. case 'EN_DOUBLE_ELEPHANT' : {$pf = array( 1926.000, 2880.000); break;}
  2406. case 'EN_ATLAS' : {$pf = array( 1872.000, 2448.000); break;}
  2407. case 'EN_COLOMBIER' : {$pf = array( 1692.000, 2484.000); break;}
  2408. case 'EN_ELEPHANT' : {$pf = array( 1656.000, 2016.000); break;}
  2409. case 'EN_DOUBLE_DEMY' : {$pf = array( 1620.000, 2556.000); break;}
  2410. case 'EN_IMPERIAL' : {$pf = array( 1584.000, 2160.000); break;}
  2411. case 'EN_PRINCESS' : {$pf = array( 1548.000, 2016.000); break;}
  2412. case 'EN_CARTRIDGE' : {$pf = array( 1512.000, 1872.000); break;}
  2413. case 'EN_DOUBLE_LARGE_POST': {$pf = array( 1512.000, 2376.000); break;}
  2414. case 'EN_ROYAL' : {$pf = array( 1440.000, 1800.000); break;}
  2415. case 'EN_SHEET':
  2416. case 'EN_HALF_POST' : {$pf = array( 1404.000, 1692.000); break;}
  2417. case 'EN_SUPER_ROYAL' : {$pf = array( 1368.000, 1944.000); break;}
  2418. case 'EN_DOUBLE_POST' : {$pf = array( 1368.000, 2196.000); break;}
  2419. case 'EN_MEDIUM' : {$pf = array( 1260.000, 1656.000); break;}
  2420. case 'EN_DEMY' : {$pf = array( 1260.000, 1620.000); break;}
  2421. case 'EN_LARGE_POST' : {$pf = array( 1188.000, 1512.000); break;}
  2422. case 'EN_COPY_DRAUGHT' : {$pf = array( 1152.000, 1440.000); break;}
  2423. case 'EN_POST' : {$pf = array( 1116.000, 1386.000); break;}
  2424. case 'EN_CROWN' : {$pf = array( 1080.000, 1440.000); break;}
  2425. case 'EN_PINCHED_POST' : {$pf = array( 1062.000, 1332.000); break;}
  2426. case 'EN_BRIEF' : {$pf = array( 972.000, 1152.000); break;}
  2427. case 'EN_FOOLSCAP' : {$pf = array( 972.000, 1224.000); break;}
  2428. case 'EN_SMALL_FOOLSCAP' : {$pf = array( 954.000, 1188.000); break;}
  2429. case 'EN_POTT' : {$pf = array( 900.000, 1080.000); break;}
  2430. // - Old Imperial Belgian Sizes
  2431. case 'BE_GRAND_AIGLE' : {$pf = array( 1984.252, 2948.031); break;}
  2432. case 'BE_COLOMBIER' : {$pf = array( 1757.480, 2409.449); break;}
  2433. case 'BE_DOUBLE_CARRE': {$pf = array( 1757.480, 2607.874); break;}
  2434. case 'BE_ELEPHANT' : {$pf = array( 1746.142, 2182.677); break;}
  2435. case 'BE_PETIT_AIGLE' : {$pf = array( 1700.787, 2381.102); break;}
  2436. case 'BE_GRAND_JESUS' : {$pf = array( 1559.055, 2069.291); break;}
  2437. case 'BE_JESUS' : {$pf = array( 1530.709, 2069.291); break;}
  2438. case 'BE_RAISIN' : {$pf = array( 1417.323, 1842.520); break;}
  2439. case 'BE_GRAND_MEDIAN': {$pf = array( 1303.937, 1714.961); break;}
  2440. case 'BE_DOUBLE_POSTE': {$pf = array( 1233.071, 1601.575); break;}
  2441. case 'BE_COQUILLE' : {$pf = array( 1218.898, 1587.402); break;}
  2442. case 'BE_PETIT_MEDIAN': {$pf = array( 1176.378, 1502.362); break;}
  2443. case 'BE_RUCHE' : {$pf = array( 1020.472, 1303.937); break;}
  2444. case 'BE_PROPATRIA' : {$pf = array( 977.953, 1218.898); break;}
  2445. case 'BE_LYS' : {$pf = array( 898.583, 1125.354); break;}
  2446. case 'BE_POT' : {$pf = array( 870.236, 1088.504); break;}
  2447. case 'BE_ROSETTE' : {$pf = array( 765.354, 983.622); break;}
  2448. // - Old Imperial French Sizes
  2449. case 'FR_UNIVERS' : {$pf = array( 2834.646, 3685.039); break;}
  2450. case 'FR_DOUBLE_COLOMBIER' : {$pf = array( 2551.181, 3571.654); break;}
  2451. case 'FR_GRANDE_MONDE' : {$pf = array( 2551.181, 3571.654); break;}
  2452. case 'FR_DOUBLE_SOLEIL' : {$pf = array( 2267.717, 3401.575); break;}
  2453. case 'FR_DOUBLE_JESUS' : {$pf = array( 2154.331, 3174.803); break;}
  2454. case 'FR_GRAND_AIGLE' : {$pf = array( 2125.984, 3004.724); break;}
  2455. case 'FR_PETIT_AIGLE' : {$pf = array( 1984.252, 2664.567); break;}
  2456. case 'FR_DOUBLE_RAISIN' : {$pf = array( 1842.520, 2834.646); break;}
  2457. case 'FR_JOURNAL' : {$pf = array( 1842.520, 2664.567); break;}
  2458. case 'FR_COLOMBIER_AFFICHE': {$pf = array( 1785.827, 2551.181); break;}
  2459. case 'FR_DOUBLE_CAVALIER' : {$pf = array( 1757.480, 2607.874); break;}
  2460. case 'FR_CLOCHE' : {$pf = array( 1700.787, 2267.717); break;}
  2461. case 'FR_SOLEIL' : {$pf = array( 1700.787, 2267.717); break;}
  2462. case 'FR_DOUBLE_CARRE' : {$pf = array( 1587.402, 2551.181); break;}
  2463. case 'FR_DOUBLE_COQUILLE' : {$pf = array( 1587.402, 2494.488); break;}
  2464. case 'FR_JESUS' : {$pf = array( 1587.402, 2154.331); break;}
  2465. case 'FR_RAISIN' : {$pf = array( 1417.323, 1842.520); break;}
  2466. case 'FR_CAVALIER' : {$pf = array( 1303.937, 1757.480); break;}
  2467. case 'FR_DOUBLE_COURONNE' : {$pf = array( 1303.937, 2040.945); break;}
  2468. case 'FR_CARRE' : {$pf = array( 1275.591, 1587.402); break;}
  2469. case 'FR_COQUILLE' : {$pf = array( 1247.244, 1587.402); break;}
  2470. case 'FR_DOUBLE_TELLIERE' : {$pf = array( 1247.244, 1927.559); break;}
  2471. case 'FR_DOUBLE_CLOCHE' : {$pf = array( 1133.858, 1700.787); break;}
  2472. case 'FR_DOUBLE_POT' : {$pf = array( 1133.858, 1757.480); break;}
  2473. case 'FR_ECU' : {$pf = array( 1133.858, 1474.016); break;}
  2474. case 'FR_COURONNE' : {$pf = array( 1020.472, 1303.937); break;}
  2475. case 'FR_TELLIERE' : {$pf = array( 963.780, 1247.244); break;}
  2476. case 'FR_POT' : {$pf = array( 878.740, 1133.858); break;}
  2477. // DEFAULT ISO A4
  2478. default: {$pf = array( 595.276, 841.890); break;}
  2479. }
  2480. return $pf;
  2481. }
  2482. /**
  2483. * Change the format of the current page
  2484. * @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 numners (width, height) or an array containing the following measures and options:<ul>
  2485. * <li>['format'] = page format name (one of the above);</li>
  2486. * <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>
  2487. * <li>['PZ'] : The page's preferred zoom (magnification) factor.</li>
  2488. * <li>['MediaBox'] : the boundaries of the physical medium on which the page shall be displayed or printed:</li>
  2489. * <li>['MediaBox']['llx'] : lower-left x coordinate in points</li>
  2490. * <li>['MediaBox']['lly'] : lower-left y coordinate in points</li>
  2491. * <li>['MediaBox']['urx'] : upper-right x coordinate in points</li>
  2492. * <li>['MediaBox']['ury'] : upper-right y coordinate in points</li>
  2493. * <li>['CropBox'] : the visible region of default user space:</li>
  2494. * <li>['CropBox']['llx'] : lower-left x coordinate in points</li>
  2495. * <li>['CropBox']['lly'] : lower-left y coordinate in points</li>
  2496. * <li>['CropBox']['urx'] : upper-right x coordinate in points</li>
  2497. * <li>['CropBox']['ury'] : upper-right y coordinate in points</li>
  2498. * <li>['BleedBox'] : the region to which the contents of the page shall be clipped when output in a production environment:</li>
  2499. * <li>['BleedBox']['llx'] : lower-left x coordinate in points</li>
  2500. * <li>['BleedBox']['lly'] : lower-left y coordinate in points</li>
  2501. * <li>['BleedBox']['urx'] : upper-right x coordinate in points</li>
  2502. * <li>['BleedBox']['ury'] : upper-right y coordinate in points</li>
  2503. * <li>['TrimBox'] : the intended dimensions of the finished page after trimming:</li>
  2504. * <li>['TrimBox']['llx'] : lower-left x coordinate in points</li>
  2505. * <li>['TrimBox']['lly'] : lower-left y coordinate in points</li>
  2506. * <li>['TrimBox']['urx'] : upper-right x coordinate in points</li>
  2507. * <li>['TrimBox']['ury'] : upper-right y coordinate in points</li>
  2508. * <li>['ArtBox'] : the extent of the page's meaningful content:</li>
  2509. * <li>['ArtBox']['llx'] : lower-left x coordinate in points</li>
  2510. * <li>['ArtBox']['lly'] : lower-left y coordinate in points</li>
  2511. * <li>['ArtBox']['urx'] : upper-right x coordinate in points</li>
  2512. * <li>['ArtBox']['ury'] : upper-right y coordinate in points</li>
  2513. * <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>
  2514. * <li>['BoxColorInfo'][BOXTYPE]['C'] : an array of three numbers in the range 0-255, representing the components in the DeviceRGB colour space.</li>
  2515. * <li>['BoxColorInfo'][BOXTYPE]['W'] : the guideline width in default user units</li>
  2516. * <li>['BoxColorInfo'][BOXTYPE]['S'] : the guideline style: S = Solid; D = Dashed</li>
  2517. * <li>['BoxColorInfo'][BOXTYPE]['D'] : dash array defining a pattern of dashes and gaps to be used in drawing dashed guidelines</li>
  2518. * <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>
  2519. * <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>
  2520. * <li>['trans']['S'] : transition style : Split, Blinds, Box, Wipe, Dissolve, Glitter, R, Fly, Push, Cover, Uncover, Fade</li>
  2521. * <li>['trans']['D'] : The duration of the transition effect, in seconds.</li>
  2522. * <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>
  2523. * <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>
  2524. * <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>
  2525. * <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>
  2526. * <li>['trans']['B'] : (Fly transition style only) If true, the area that shall be flown in is rectangular and opaque. Default: false.</li>
  2527. * </ul>
  2528. * @param $orientation (string) page orientation. Possible values are (case insensitive):<ul>
  2529. * <li>P or Portrait (default)</li>
  2530. * <li>L or Landscape</li>
  2531. * <li>'' (empty string) for automatic orientation</li>
  2532. * </ul>
  2533. * @protected
  2534. * @since 3.0.015 (2008-06-06)
  2535. * @see getPageSizeFromFormat()
  2536. */
  2537. protected function setPageFormat($format, $orientation='P') {
  2538. if (!empty($format) AND isset($this->pagedim[$this->page])) {
  2539. // remove inherited values
  2540. unset($this->pagedim[$this->page]);
  2541. }
  2542. if (is_string($format)) {
  2543. // get page measures from format name
  2544. $pf = $this->getPageSizeFromFormat($format);
  2545. $this->fwPt = $pf[0];
  2546. $this->fhPt = $pf[1];
  2547. } else {
  2548. // the boundaries of the physical medium on which the page shall be displayed or printed
  2549. if (isset($format['MediaBox'])) {
  2550. $this->setPageBoxes($this->page, 'MediaBox', $format['MediaBox']['llx'], $format['MediaBox']['lly'], $format['MediaBox']['urx'], $format['MediaBox']['ury'], false);
  2551. $this->fwPt = (($format['MediaBox']['urx'] - $format['MediaBox']['llx']) * $this->k);
  2552. $this->fhPt = (($format['MediaBox']['ury'] - $format['MediaBox']['lly']) * $this->k);
  2553. } else {
  2554. if (isset($format[0]) AND is_numeric($format[0]) AND isset($format[1]) AND is_numeric($format[1])) {
  2555. $pf = array(($format[0] * $this->k), ($format[1] * $this->k));
  2556. } else {
  2557. if (!isset($format['format'])) {
  2558. // default value
  2559. $format['format'] = 'A4';
  2560. }
  2561. $pf = $this->getPageSizeFromFormat($format['format']);
  2562. }
  2563. $this->fwPt = $pf[0];
  2564. $this->fhPt = $pf[1];
  2565. $this->setPageBoxes($this->page, 'MediaBox', 0, 0, $this->fwPt, $this->fhPt, true);
  2566. }
  2567. // the visible region of default user space
  2568. if (isset($format['CropBox'])) {
  2569. $this->setPageBoxes($this->page, 'CropBox', $format['CropBox']['llx'], $format['CropBox']['lly'], $format['CropBox']['urx'], $format['CropBox']['ury'], false);
  2570. }
  2571. // the region to which the contents of the page shall be clipped when output in a production environment
  2572. if (isset($format['BleedBox'])) {
  2573. $this->setPageBoxes($this->page, 'BleedBox', $format['BleedBox']['llx'], $format['BleedBox']['lly'], $format['BleedBox']['urx'], $format['BleedBox']['ury'], false);
  2574. }
  2575. // the intended dimensions of the finished page after trimming
  2576. if (isset($format['TrimBox'])) {
  2577. $this->setPageBoxes($this->page, 'TrimBox', $format['TrimBox']['llx'], $format['TrimBox']['lly'], $format['TrimBox']['urx'], $format['TrimBox']['ury'], false);
  2578. }
  2579. // the page's meaningful content (including potential white space)
  2580. if (isset($format['ArtBox'])) {
  2581. $this->setPageBoxes($this->page, 'ArtBox', $format['ArtBox']['llx'], $format['ArtBox']['lly'], $format['ArtBox']['urx'], $format['ArtBox']['ury'], false);
  2582. }
  2583. // specify the colours and other visual characteristics that should be used in displaying guidelines on the screen for the various page boundaries
  2584. if (isset($format['BoxColorInfo'])) {
  2585. $this->pagedim[$this->page]['BoxColorInfo'] = $format['BoxColorInfo'];
  2586. }
  2587. if (isset($format['Rotate']) AND (($format['Rotate'] % 90) == 0)) {
  2588. // The number of degrees by which the page shall be rotated clockwise when displayed or printed. The value shall be a multiple of 90.
  2589. $this->pagedim[$this->page]['Rotate'] = intval($format['Rotate']);
  2590. }
  2591. if (isset($format['PZ'])) {
  2592. // The page's preferred zoom (magnification) factor
  2593. $this->pagedim[$this->page]['PZ'] = floatval($format['PZ']);
  2594. }
  2595. if (isset($format['trans'])) {
  2596. // The style and duration of the visual transition to use when moving from another page to the given page during a presentation
  2597. if (isset($format['trans']['Dur'])) {
  2598. // The page's display duration
  2599. $this->pagedim[$this->page]['trans']['Dur'] = floatval($format['trans']['Dur']);
  2600. }
  2601. $stansition_styles = array('Split', 'Blinds', 'Box', 'Wipe', 'Dissolve', 'Glitter', 'R', 'Fly', 'Push', 'Cover', 'Uncover', 'Fade');
  2602. if (isset($format['trans']['S']) AND in_array($format['trans']['S'], $stansition_styles)) {
  2603. // The transition style that shall be used when moving to this page from another during a presentation
  2604. $this->pagedim[$this->page]['trans']['S'] = $format['trans']['S'];
  2605. $valid_effect = array('Split', 'Blinds');
  2606. $valid_vals = array('H', 'V');
  2607. if (isset($format['trans']['Dm']) AND in_array($format['trans']['S'], $valid_effect) AND in_array($format['trans']['Dm'], $valid_vals)) {
  2608. $this->pagedim[$this->page]['trans']['Dm'] = $format['trans']['Dm'];
  2609. }
  2610. $valid_effect = array('Split', 'Box', 'Fly');
  2611. $valid_vals = array('I', 'O');
  2612. if (isset($format['trans']['M']) AND in_array($format['trans']['S'], $valid_effect) AND in_array($format['trans']['M'], $valid_vals)) {
  2613. $this->pagedim[$this->page]['trans']['M'] = $format['trans']['M'];
  2614. }
  2615. $valid_effect = array('Wipe', 'Glitter', 'Fly', 'Cover', 'Uncover', 'Push');
  2616. if (isset($format['trans']['Di']) AND in_array($format['trans']['S'], $valid_effect)) {
  2617. if (((($format['trans']['Di'] == 90) OR ($format['trans']['Di'] == 180)) AND ($format['trans']['S'] == 'Wipe'))
  2618. OR (($format['trans']['Di'] == 315) AND ($format['trans']['S'] == 'Glitter'))
  2619. OR (($format['trans']['Di'] == 0) OR ($format['trans']['Di'] == 270))) {
  2620. $this->pagedim[$this->page]['trans']['Di'] = intval($format['trans']['Di']);
  2621. }
  2622. }
  2623. if (isset($format['trans']['SS']) AND ($format['trans']['S'] == 'Fly')) {
  2624. $this->pagedim[$this->page]['trans']['SS'] = floatval($format['trans']['SS']);
  2625. }
  2626. if (isset($format['trans']['B']) AND ($format['trans']['B'] === true) AND ($format['trans']['S'] == 'Fly')) {
  2627. $this->pagedim[$this->page]['trans']['B'] = 'true';
  2628. }
  2629. } else {
  2630. $this->pagedim[$this->page]['trans']['S'] = 'R';
  2631. }
  2632. if (isset($format['trans']['D'])) {
  2633. // The duration of the transition effect, in seconds
  2634. $this->pagedim[$this->page]['trans']['D'] = floatval($format['trans']['D']);
  2635. } else {
  2636. $this->pagedim[$this->page]['trans']['D'] = 1;
  2637. }
  2638. }
  2639. }
  2640. $this->setPageOrientation($orientation);
  2641. }
  2642. /**
  2643. * Set page boundaries.
  2644. * @param $page (int) page number
  2645. * @param $type (string) valid values are: <ul><li>'MediaBox' : the boundaries of the physical medium on which the page shall be displayed or printed;</li><li>'CropBox' : the visible region of default user space;</li><li>'BleedBox' : the region to which the contents of the page shall be clipped when output in a production environment;</li><li>'TrimBox' : the intended dimensions of the finished page after trimming;</li><li>'ArtBox' : the page's meaningful content (including potential white space).</li></ul>
  2646. * @param $llx (float) lower-left x coordinate in user units
  2647. * @param $lly (float) lower-left y coordinate in user units
  2648. * @param $urx (float) upper-right x coordinate in user units
  2649. * @param $ury (float) upper-right y coordinate in user units
  2650. * @param $points (boolean) if true uses user units as unit of measure, otherwise uses PDF points
  2651. * @public
  2652. * @since 5.0.010 (2010-05-17)
  2653. */
  2654. public function setPageBoxes($page, $type, $llx, $lly, $urx, $ury, $points=false) {
  2655. if (!isset($this->pagedim[$page])) {
  2656. // initialize array
  2657. $this->pagedim[$page] = array();
  2658. }
  2659. $pageboxes = array('MediaBox', 'CropBox', 'BleedBox', 'TrimBox', 'ArtBox');
  2660. if (!in_array($type, $pageboxes)) {
  2661. return;
  2662. }
  2663. if ($points) {
  2664. $k = 1;
  2665. } else {
  2666. $k = $this->k;
  2667. }
  2668. $this->pagedim[$page][$type]['llx'] = ($llx * $k);
  2669. $this->pagedim[$page][$type]['lly'] = ($lly * $k);
  2670. $this->pagedim[$page][$type]['urx'] = ($urx * $k);
  2671. $this->pagedim[$page][$type]['ury'] = ($ury * $k);
  2672. }
  2673. /**
  2674. * Swap X and Y coordinates of page boxes (change page boxes orientation).
  2675. * @param $page (int) page number
  2676. * @protected
  2677. * @since 5.0.010 (2010-05-17)
  2678. */
  2679. protected function swapPageBoxCoordinates($page) {
  2680. $pageboxes = array('MediaBox', 'CropBox', 'BleedBox', 'TrimBox', 'ArtBox');
  2681. foreach ($pageboxes as $type) {
  2682. // swap X and Y coordinates
  2683. if (isset($this->pagedim[$page][$type])) {
  2684. $tmp = $this->pagedim[$page][$type]['llx'];
  2685. $this->pagedim[$page][$type]['llx'] = $this->pagedim[$page][$type]['lly'];
  2686. $this->pagedim[$page][$type]['lly'] = $tmp;
  2687. $tmp = $this->pagedim[$page][$type]['urx'];
  2688. $this->pagedim[$page][$type]['urx'] = $this->pagedim[$page][$type]['ury'];
  2689. $this->pagedim[$page][$type]['ury'] = $tmp;
  2690. }
  2691. }
  2692. }
  2693. /**
  2694. * Set page orientation.
  2695. * @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>
  2696. * @param $autopagebreak (boolean) Boolean indicating if auto-page-break mode should be on or off.
  2697. * @param $bottommargin (float) bottom margin of the page.
  2698. * @public
  2699. * @since 3.0.015 (2008-06-06)
  2700. */
  2701. public function setPageOrientation($orientation, $autopagebreak='', $bottommargin='') {
  2702. if (!isset($this->pagedim[$this->page]['MediaBox'])) {
  2703. // the boundaries of the physical medium on which the page shall be displayed or printed
  2704. $this->setPageBoxes($this->page, 'MediaBox', 0, 0, $this->fwPt, $this->fhPt, true);
  2705. }
  2706. if (!isset($this->pagedim[$this->page]['CropBox'])) {
  2707. // the visible region of default user space
  2708. $this->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);
  2709. }
  2710. if (!isset($this->pagedim[$this->page]['BleedBox'])) {
  2711. // the region to which the contents of the page shall be clipped when output in a production environment
  2712. $this->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);
  2713. }
  2714. if (!isset($this->pagedim[$this->page]['TrimBox'])) {
  2715. // the intended dimensions of the finished page after trimming
  2716. $this->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);
  2717. }
  2718. if (!isset($this->pagedim[$this->page]['ArtBox'])) {
  2719. // the page's meaningful content (including potential white space)
  2720. $this->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);
  2721. }
  2722. if (!isset($this->pagedim[$this->page]['Rotate'])) {
  2723. // The number of degrees by which the page shall be rotated clockwise when displayed or printed. The value shall be a multiple of 90.
  2724. $this->pagedim[$this->page]['Rotate'] = 0;
  2725. }
  2726. if (!isset($this->pagedim[$this->page]['PZ'])) {
  2727. // The page's preferred zoom (magnification) factor
  2728. $this->pagedim[$this->page]['PZ'] = 1;
  2729. }
  2730. if ($this->fwPt > $this->fhPt) {
  2731. // landscape
  2732. $default_orientation = 'L';
  2733. } else {
  2734. // portrait
  2735. $default_orientation = 'P';
  2736. }
  2737. $valid_orientations = array('P', 'L');
  2738. if (empty($orientation)) {
  2739. $orientation = $default_orientation;
  2740. } else {
  2741. $orientation = strtoupper($orientation{0});
  2742. }
  2743. if (in_array($orientation, $valid_orientations) AND ($orientation != $default_orientation)) {
  2744. $this->CurOrientation = $orientation;
  2745. $this->wPt = $this->fhPt;
  2746. $this->hPt = $this->fwPt;
  2747. } else {
  2748. $this->CurOrientation = $default_orientation;
  2749. $this->wPt = $this->fwPt;
  2750. $this->hPt = $this->fhPt;
  2751. }
  2752. if ((abs($this->pagedim[$this->page]['MediaBox']['urx'] - $this->hPt) < $this->feps) AND (abs($this->pagedim[$this->page]['MediaBox']['ury'] - $this->wPt) < $this->feps)){
  2753. // swap X and Y coordinates (change page orientation)
  2754. $this->swapPageBoxCoordinates($this->page);
  2755. }
  2756. $this->w = $this->wPt / $this->k;
  2757. $this->h = $this->hPt / $this->k;
  2758. if ($this->empty_string($autopagebreak)) {
  2759. if (isset($this->AutoPageBreak)) {
  2760. $autopagebreak = $this->AutoPageBreak;
  2761. } else {
  2762. $autopagebreak = true;
  2763. }
  2764. }
  2765. if ($this->empty_string($bottommargin)) {
  2766. if (isset($this->bMargin)) {
  2767. $bottommargin = $this->bMargin;
  2768. } else {
  2769. // default value = 2 cm
  2770. $bottommargin = 2 * 28.35 / $this->k;
  2771. }
  2772. }
  2773. $this->SetAutoPageBreak($autopagebreak, $bottommargin);
  2774. // store page dimensions
  2775. $this->pagedim[$this->page]['w'] = $this->wPt;
  2776. $this->pagedim[$this->page]['h'] = $this->hPt;
  2777. $this->pagedim[$this->page]['wk'] = $this->w;
  2778. $this->pagedim[$this->page]['hk'] = $this->h;
  2779. $this->pagedim[$this->page]['tm'] = $this->tMargin;
  2780. $this->pagedim[$this->page]['bm'] = $bottommargin;
  2781. $this->pagedim[$this->page]['lm'] = $this->lMargin;
  2782. $this->pagedim[$this->page]['rm'] = $this->rMargin;
  2783. $this->pagedim[$this->page]['pb'] = $autopagebreak;
  2784. $this->pagedim[$this->page]['or'] = $this->CurOrientation;
  2785. $this->pagedim[$this->page]['olm'] = $this->original_lMargin;
  2786. $this->pagedim[$this->page]['orm'] = $this->original_rMargin;
  2787. }
  2788. /**
  2789. * Set regular expression to detect withespaces or word separators.
  2790. * The pattern delimiter must be the forward-slash character "/".
  2791. * Some example patterns are:
  2792. * <pre>
  2793. * Non-Unicode or missing PCRE unicode support: "/[^\S\xa0]/"
  2794. * Unicode and PCRE unicode support: "/[^\S\P{Z}\xa0]/u"
  2795. * Unicode and PCRE unicode support in Chinese mode: "/[^\S\P{Z}\P{Lo}\xa0]/u"
  2796. * if PCRE unicode support is turned ON ("\P" is the negate class of "\p"):
  2797. * "\p{Z}" or "\p{Separator}": any kind of Unicode whitespace or invisible separator.
  2798. * "\p{Lo}" or "\p{Other_Letter}": a Unicode letter or ideograph that does not have lowercase and uppercase variants.
  2799. * "\p{Lo}" is needed for Chinese characters because are packed next to each other without spaces in between.
  2800. * </pre>
  2801. * @param $re (string) regular expression (leave empty for default).
  2802. * @public
  2803. * @since 4.6.016 (2009-06-15)
  2804. */
  2805. public function setSpacesRE($re='/[^\S\xa0]/') {
  2806. $this->re_spaces = $re;
  2807. $re_parts = explode('/', $re);
  2808. // get pattern parts
  2809. $this->re_space = array();
  2810. if (isset($re_parts[1]) AND !empty($re_parts[1])) {
  2811. $this->re_space['p'] = $re_parts[1];
  2812. } else {
  2813. $this->re_space['p'] = '[\s]';
  2814. }
  2815. // set pattern modifiers
  2816. if (isset($re_parts[2]) AND !empty($re_parts[2])) {
  2817. $this->re_space['m'] = $re_parts[2];
  2818. } else {
  2819. $this->re_space['m'] = '';
  2820. }
  2821. }
  2822. /**
  2823. * Enable or disable Right-To-Left language mode
  2824. * @param $enable (Boolean) if true enable Right-To-Left language mode.
  2825. * @param $resetx (Boolean) if true reset the X position on direction change.
  2826. * @public
  2827. * @since 2.0.000 (2008-01-03)
  2828. */
  2829. public function setRTL($enable, $resetx=true) {
  2830. $enable = $enable ? true : false;
  2831. $resetx = ($resetx AND ($enable != $this->rtl));
  2832. $this->rtl = $enable;
  2833. $this->tmprtl = false;
  2834. if ($resetx) {
  2835. $this->Ln(0);
  2836. }
  2837. }
  2838. /**
  2839. * Return the RTL status
  2840. * @return boolean
  2841. * @public
  2842. * @since 4.0.012 (2008-07-24)
  2843. */
  2844. public function getRTL() {
  2845. return $this->rtl;
  2846. }
  2847. /**
  2848. * Force temporary RTL language direction
  2849. * @param $mode (mixed) can be false, 'L' for LTR or 'R' for RTL
  2850. * @public
  2851. * @since 2.1.000 (2008-01-09)
  2852. */
  2853. public function setTempRTL($mode) {
  2854. $newmode = false;
  2855. switch (strtoupper($mode)) {
  2856. case 'LTR':
  2857. case 'L': {
  2858. if ($this->rtl) {
  2859. $newmode = 'L';
  2860. }
  2861. break;
  2862. }
  2863. case 'RTL':
  2864. case 'R': {
  2865. if (!$this->rtl) {
  2866. $newmode = 'R';
  2867. }
  2868. break;
  2869. }
  2870. case false:
  2871. default: {
  2872. $newmode = false;
  2873. break;
  2874. }
  2875. }
  2876. $this->tmprtl = $newmode;
  2877. }
  2878. /**
  2879. * Return the current temporary RTL status
  2880. * @return boolean
  2881. * @public
  2882. * @since 4.8.014 (2009-11-04)
  2883. */
  2884. public function isRTLTextDir() {
  2885. return ($this->rtl OR ($this->tmprtl == 'R'));
  2886. }
  2887. /**
  2888. * Set the last cell height.
  2889. * @param $h (float) cell height.
  2890. * @author Nicola Asuni
  2891. * @public
  2892. * @since 1.53.0.TC034
  2893. */
  2894. public function setLastH($h) {
  2895. $this->lasth = $h;
  2896. }
  2897. /**
  2898. * Reset the last cell height.
  2899. * @public
  2900. * @since 5.9.000 (2010-10-03)
  2901. */
  2902. public function resetLastH() {
  2903. $this->lasth = ($this->FontSize * $this->cell_height_ratio) + $this->cell_padding['T'] + $this->cell_padding['B'];
  2904. }
  2905. /**
  2906. * Get the last cell height.
  2907. * @return last cell height
  2908. * @public
  2909. * @since 4.0.017 (2008-08-05)
  2910. */
  2911. public function getLastH() {
  2912. return $this->lasth;
  2913. }
  2914. /**
  2915. * Set the adjusting factor to convert pixels to user units.
  2916. * @param $scale (float) adjusting factor to convert pixels to user units.
  2917. * @author Nicola Asuni
  2918. * @public
  2919. * @since 1.5.2
  2920. */
  2921. public function setImageScale($scale) {
  2922. $this->imgscale = $scale;
  2923. }
  2924. /**
  2925. * Returns the adjusting factor to convert pixels to user units.
  2926. * @return float adjusting factor to convert pixels to user units.
  2927. * @author Nicola Asuni
  2928. * @public
  2929. * @since 1.5.2
  2930. */
  2931. public function getImageScale() {
  2932. return $this->imgscale;
  2933. }
  2934. /**
  2935. * Returns an array of page dimensions:
  2936. * <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>
  2937. * @param $pagenum (int) page number (empty = current page)
  2938. * @return array of page dimensions.
  2939. * @author Nicola Asuni
  2940. * @public
  2941. * @since 4.5.027 (2009-03-16)
  2942. */
  2943. public function getPageDimensions($pagenum='') {
  2944. if (empty($pagenum)) {
  2945. $pagenum = $this->page;
  2946. }
  2947. return $this->pagedim[$pagenum];
  2948. }
  2949. /**
  2950. * Returns the page width in units.
  2951. * @param $pagenum (int) page number (empty = current page)
  2952. * @return int page width.
  2953. * @author Nicola Asuni
  2954. * @public
  2955. * @since 1.5.2
  2956. * @see getPageDimensions()
  2957. */
  2958. public function getPageWidth($pagenum='') {
  2959. if (empty($pagenum)) {
  2960. return $this->w;
  2961. }
  2962. return $this->pagedim[$pagenum]['w'];
  2963. }
  2964. /**
  2965. * Returns the page height in units.
  2966. * @param $pagenum (int) page number (empty = current page)
  2967. * @return int page height.
  2968. * @author Nicola Asuni
  2969. * @public
  2970. * @since 1.5.2
  2971. * @see getPageDimensions()
  2972. */
  2973. public function getPageHeight($pagenum='') {
  2974. if (empty($pagenum)) {
  2975. return $this->h;
  2976. }
  2977. return $this->pagedim[$pagenum]['h'];
  2978. }
  2979. /**
  2980. * Returns the page break margin.
  2981. * @param $pagenum (int) page number (empty = current page)
  2982. * @return int page break margin.
  2983. * @author Nicola Asuni
  2984. * @public
  2985. * @since 1.5.2
  2986. * @see getPageDimensions()
  2987. */
  2988. public function getBreakMargin($pagenum='') {
  2989. if (empty($pagenum)) {
  2990. return $this->bMargin;
  2991. }
  2992. return $this->pagedim[$pagenum]['bm'];
  2993. }
  2994. /**
  2995. * Returns the scale factor (number of points in user unit).
  2996. * @return int scale factor.
  2997. * @author Nicola Asuni
  2998. * @public
  2999. * @since 1.5.2
  3000. */
  3001. public function getScaleFactor() {
  3002. return $this->k;
  3003. }
  3004. /**
  3005. * Defines the left, top and right margins.
  3006. * @param $left (float) Left margin.
  3007. * @param $top (float) Top margin.
  3008. * @param $right (float) Right margin. Default value is the left one.
  3009. * @param $keepmargins (boolean) if true overwrites the default page margins
  3010. * @public
  3011. * @since 1.0
  3012. * @see SetLeftMargin(), SetTopMargin(), SetRightMargin(), SetAutoPageBreak()
  3013. */
  3014. public function SetMargins($left, $top, $right=-1, $keepmargins=false) {
  3015. //Set left, top and right margins
  3016. $this->lMargin = $left;
  3017. $this->tMargin = $top;
  3018. if ($right == -1) {
  3019. $right = $left;
  3020. }
  3021. $this->rMargin = $right;
  3022. if ($keepmargins) {
  3023. // overwrite original values
  3024. $this->original_lMargin = $this->lMargin;
  3025. $this->original_rMargin = $this->rMargin;
  3026. }
  3027. }
  3028. /**
  3029. * 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.
  3030. * @param $margin (float) The margin.
  3031. * @public
  3032. * @since 1.4
  3033. * @see SetTopMargin(), SetRightMargin(), SetAutoPageBreak(), SetMargins()
  3034. */
  3035. public function SetLeftMargin($margin) {
  3036. //Set left margin
  3037. $this->lMargin = $margin;
  3038. if (($this->page > 0) AND ($this->x < $margin)) {
  3039. $this->x = $margin;
  3040. }
  3041. }
  3042. /**
  3043. * Defines the top margin. The method can be called before creating the first page.
  3044. * @param $margin (float) The margin.
  3045. * @public
  3046. * @since 1.5
  3047. * @see SetLeftMargin(), SetRightMargin(), SetAutoPageBreak(), SetMargins()
  3048. */
  3049. public function SetTopMargin($margin) {
  3050. //Set top margin
  3051. $this->tMargin = $margin;
  3052. if (($this->page > 0) AND ($this->y < $margin)) {
  3053. $this->y = $margin;
  3054. }
  3055. }
  3056. /**
  3057. * Defines the right margin. The method can be called before creating the first page.
  3058. * @param $margin (float) The margin.
  3059. * @public
  3060. * @since 1.5
  3061. * @see SetLeftMargin(), SetTopMargin(), SetAutoPageBreak(), SetMargins()
  3062. */
  3063. public function SetRightMargin($margin) {
  3064. $this->rMargin = $margin;
  3065. if (($this->page > 0) AND ($this->x > ($this->w - $margin))) {
  3066. $this->x = $this->w - $margin;
  3067. }
  3068. }
  3069. /**
  3070. * Set the same internal Cell padding for top, right, bottom, left-
  3071. * @param $pad (float) internal padding.
  3072. * @public
  3073. * @since 2.1.000 (2008-01-09)
  3074. * @see getCellPaddings(), setCellPaddings()
  3075. */
  3076. public function SetCellPadding($pad) {
  3077. if ($pad >= 0) {
  3078. $this->cell_padding['L'] = $pad;
  3079. $this->cell_padding['T'] = $pad;
  3080. $this->cell_padding['R'] = $pad;
  3081. $this->cell_padding['B'] = $pad;
  3082. }
  3083. }
  3084. /**
  3085. * Set the internal Cell paddings.
  3086. * @param $left (float) left padding
  3087. * @param $top (float) top padding
  3088. * @param $right (float) right padding
  3089. * @param $bottom (float) bottom padding
  3090. * @public
  3091. * @since 5.9.000 (2010-10-03)
  3092. * @see getCellPaddings(), SetCellPadding()
  3093. */
  3094. public function setCellPaddings($left='', $top='', $right='', $bottom='') {
  3095. if (($left !== '') AND ($left >= 0)) {
  3096. $this->cell_padding['L'] = $left;
  3097. }
  3098. if (($top !== '') AND ($top >= 0)) {
  3099. $this->cell_padding['T'] = $top;
  3100. }
  3101. if (($right !== '') AND ($right >= 0)) {
  3102. $this->cell_padding['R'] = $right;
  3103. }
  3104. if (($bottom !== '') AND ($bottom >= 0)) {
  3105. $this->cell_padding['B'] = $bottom;
  3106. }
  3107. }
  3108. /**
  3109. * Get the internal Cell padding array.
  3110. * @return array of padding values
  3111. * @public
  3112. * @since 5.9.000 (2010-10-03)
  3113. * @see setCellPaddings(), SetCellPadding()
  3114. */
  3115. public function getCellPaddings() {
  3116. return $this->cell_padding;
  3117. }
  3118. /**
  3119. * Set the internal Cell margins.
  3120. * @param $left (float) left margin
  3121. * @param $top (float) top margin
  3122. * @param $right (float) right margin
  3123. * @param $bottom (float) bottom margin
  3124. * @public
  3125. * @since 5.9.000 (2010-10-03)
  3126. * @see getCellMargins()
  3127. */
  3128. public function setCellMargins($left='', $top='', $right='', $bottom='') {
  3129. if (($left !== '') AND ($left >= 0)) {
  3130. $this->cell_margin['L'] = $left;
  3131. }
  3132. if (($top !== '') AND ($top >= 0)) {
  3133. $this->cell_margin['T'] = $top;
  3134. }
  3135. if (($right !== '') AND ($right >= 0)) {
  3136. $this->cell_margin['R'] = $right;
  3137. }
  3138. if (($bottom !== '') AND ($bottom >= 0)) {
  3139. $this->cell_margin['B'] = $bottom;
  3140. }
  3141. }
  3142. /**
  3143. * Get the internal Cell margin array.
  3144. * @return array of margin values
  3145. * @public
  3146. * @since 5.9.000 (2010-10-03)
  3147. * @see setCellMargins()
  3148. */
  3149. public function getCellMargins() {
  3150. return $this->cell_margin;
  3151. }
  3152. /**
  3153. * Adjust the internal Cell padding array to take account of the line width.
  3154. * @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)))
  3155. * @return array of adjustments
  3156. * @public
  3157. * @since 5.9.000 (2010-10-03)
  3158. */
  3159. protected function adjustCellPadding($brd=0) {
  3160. if (empty($brd)) {
  3161. return;
  3162. }
  3163. if (is_string($brd)) {
  3164. // convert string to array
  3165. $slen = strlen($brd);
  3166. $newbrd = array();
  3167. for ($i = 0; $i < $slen; ++$i) {
  3168. $newbrd[$brd[$i]] = true;
  3169. }
  3170. $brd = $newbrd;
  3171. } elseif (($brd === 1) OR ($brd === true) OR (is_numeric($brd) AND (intval($brd) > 0))) {
  3172. $brd = array('LRTB' => true);
  3173. }
  3174. if (!is_array($brd)) {
  3175. return;
  3176. }
  3177. // store current cell padding
  3178. $cp = $this->cell_padding;
  3179. // select border mode
  3180. if (isset($brd['mode'])) {
  3181. $mode = $brd['mode'];
  3182. unset($brd['mode']);
  3183. } else {
  3184. $mode = 'normal';
  3185. }
  3186. // process borders
  3187. foreach ($brd as $border => $style) {
  3188. $line_width = $this->LineWidth;
  3189. if (is_array($style) AND isset($style['width'])) {
  3190. // get border width
  3191. $line_width = $style['width'];
  3192. }
  3193. $adj = 0; // line width inside the cell
  3194. switch ($mode) {
  3195. case 'ext': {
  3196. $adj = 0;
  3197. break;
  3198. }
  3199. case 'int': {
  3200. $adj = $line_width;
  3201. break;
  3202. }
  3203. case 'normal':
  3204. default: {
  3205. $adj = ($line_width / 2);
  3206. break;
  3207. }
  3208. }
  3209. // correct internal cell padding if required to avoid overlap between text and lines
  3210. if ((strpos($border,'T') !== false) AND ($this->cell_padding['T'] < $adj)) {
  3211. $this->cell_padding['T'] = $adj;
  3212. }
  3213. if ((strpos($border,'R') !== false) AND ($this->cell_padding['R'] < $adj)) {
  3214. $this->cell_padding['R'] = $adj;
  3215. }
  3216. if ((strpos($border,'B') !== false) AND ($this->cell_padding['B'] < $adj)) {
  3217. $this->cell_padding['B'] = $adj;
  3218. }
  3219. if ((strpos($border,'L') !== false) AND ($this->cell_padding['L'] < $adj)) {
  3220. $this->cell_padding['L'] = $adj;
  3221. }
  3222. }
  3223. 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']));
  3224. }
  3225. /**
  3226. * 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.
  3227. * @param $auto (boolean) Boolean indicating if mode should be on or off.
  3228. * @param $margin (float) Distance from the bottom of the page.
  3229. * @public
  3230. * @since 1.0
  3231. * @see Cell(), MultiCell(), AcceptPageBreak()
  3232. */
  3233. public function SetAutoPageBreak($auto, $margin=0) {
  3234. $this->AutoPageBreak = $auto ? true : false;
  3235. $this->bMargin = $margin;
  3236. $this->PageBreakTrigger = $this->h - $margin;
  3237. }
  3238. /**
  3239. * Return the auto-page-break mode (true or false).
  3240. * @return boolean auto-page-break mode
  3241. * @public
  3242. * @since 5.9.088
  3243. */
  3244. public function getAutoPageBreak() {
  3245. return $this->AutoPageBreak;
  3246. }
  3247. /**
  3248. * Defines the way the document is to be displayed by the viewer.
  3249. * @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>
  3250. * @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>
  3251. * @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>
  3252. * @public
  3253. * @since 1.2
  3254. */
  3255. public function SetDisplayMode($zoom, $layout='SinglePage', $mode='UseNone') {
  3256. if (($zoom == 'fullpage') OR ($zoom == 'fullwidth') OR ($zoom == 'real') OR ($zoom == 'default') OR (!is_string($zoom))) {
  3257. $this->ZoomMode = $zoom;
  3258. } else {
  3259. $this->Error('Incorrect zoom display mode: '.$zoom);
  3260. }
  3261. switch ($layout) {
  3262. case 'default':
  3263. case 'single':
  3264. case 'SinglePage': {
  3265. $this->LayoutMode = 'SinglePage';
  3266. break;
  3267. }
  3268. case 'continuous':
  3269. case 'OneColumn': {
  3270. $this->LayoutMode = 'OneColumn';
  3271. break;
  3272. }
  3273. case 'two':
  3274. case 'TwoColumnLeft': {
  3275. $this->LayoutMode = 'TwoColumnLeft';
  3276. break;
  3277. }
  3278. case 'TwoColumnRight': {
  3279. $this->LayoutMode = 'TwoColumnRight';
  3280. break;
  3281. }
  3282. case 'TwoPageLeft': {
  3283. $this->LayoutMode = 'TwoPageLeft';
  3284. break;
  3285. }
  3286. case 'TwoPageRight': {
  3287. $this->LayoutMode = 'TwoPageRight';
  3288. break;
  3289. }
  3290. default: {
  3291. $this->LayoutMode = 'SinglePage';
  3292. }
  3293. }
  3294. switch ($mode) {
  3295. case 'UseNone': {
  3296. $this->PageMode = 'UseNone';
  3297. break;
  3298. }
  3299. case 'UseOutlines': {
  3300. $this->PageMode = 'UseOutlines';
  3301. break;
  3302. }
  3303. case 'UseThumbs': {
  3304. $this->PageMode = 'UseThumbs';
  3305. break;
  3306. }
  3307. case 'FullScreen': {
  3308. $this->PageMode = 'FullScreen';
  3309. break;
  3310. }
  3311. case 'UseOC': {
  3312. $this->PageMode = 'UseOC';
  3313. break;
  3314. }
  3315. case '': {
  3316. $this->PageMode = 'UseAttachments';
  3317. break;
  3318. }
  3319. default: {
  3320. $this->PageMode = 'UseNone';
  3321. }
  3322. }
  3323. }
  3324. /**
  3325. * 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.
  3326. * Note: the Zlib extension is required for this feature. If not present, compression will be turned off.
  3327. * @param $compress (boolean) Boolean indicating if compression must be enabled.
  3328. * @public
  3329. * @since 1.4
  3330. */
  3331. public function SetCompression($compress=true) {
  3332. if (function_exists('gzcompress')) {
  3333. $this->compress = $compress ? true : false;
  3334. } else {
  3335. $this->compress = false;
  3336. }
  3337. }
  3338. /**
  3339. * Set flag to force sRGB_IEC61966-2.1 black scaled ICC color profile for the whole document.
  3340. * @param $mode (boolean) If true force sRGB output intent.
  3341. * @public
  3342. * @since 5.9.121 (2011-09-28)
  3343. */
  3344. public function setSRGBmode($mode=false) {
  3345. $this->force_srgb = $mode ? true : false;
  3346. }
  3347. /**
  3348. * Turn on/off Unicode mode for document information dictionary (meta tags).
  3349. * This has effect only when unicode mode is set to false.
  3350. * @param $unicode (boolean) if true set the meta information in Unicode
  3351. * @since 5.9.027 (2010-12-01)
  3352. * @public
  3353. */
  3354. public function SetDocInfoUnicode($unicode=true) {
  3355. $this->docinfounicode = $unicode ? true : false;
  3356. }
  3357. /**
  3358. * Defines the title of the document.
  3359. * @param $title (string) The title.
  3360. * @public
  3361. * @since 1.2
  3362. * @see SetAuthor(), SetCreator(), SetKeywords(), SetSubject()
  3363. */
  3364. public function SetTitle($title) {
  3365. $this->title = $title;
  3366. }
  3367. /**
  3368. * Defines the subject of the document.
  3369. * @param $subject (string) The subject.
  3370. * @public
  3371. * @since 1.2
  3372. * @see SetAuthor(), SetCreator(), SetKeywords(), SetTitle()
  3373. */
  3374. public function SetSubject($subject) {
  3375. $this->subject = $subject;
  3376. }
  3377. /**
  3378. * Defines the author of the document.
  3379. * @param $author (string) The name of the author.
  3380. * @public
  3381. * @since 1.2
  3382. * @see SetCreator(), SetKeywords(), SetSubject(), SetTitle()
  3383. */
  3384. public function SetAuthor($author) {
  3385. $this->author = $author;
  3386. }
  3387. /**
  3388. * Associates keywords with the document, generally in the form 'keyword1 keyword2 ...'.
  3389. * @param $keywords (string) The list of keywords.
  3390. * @public
  3391. * @since 1.2
  3392. * @see SetAuthor(), SetCreator(), SetSubject(), SetTitle()
  3393. */
  3394. public function SetKeywords($keywords) {
  3395. $this->keywords = $keywords;
  3396. }
  3397. /**
  3398. * Defines the creator of the document. This is typically the name of the application that generates the PDF.
  3399. * @param $creator (string) The name of the creator.
  3400. * @public
  3401. * @since 1.2
  3402. * @see SetAuthor(), SetKeywords(), SetSubject(), SetTitle()
  3403. */
  3404. public function SetCreator($creator) {
  3405. $this->creator = $creator;
  3406. }
  3407. /**
  3408. * This method is automatically called in case of fatal error; it simply outputs the message and halts the execution. An inherited class may override it to customize the error handling but should always halt the script, or the resulting document would probably be invalid.
  3409. * 2004-06-11 :: Nicola Asuni : changed bold tag with strong
  3410. * @param $msg (string) The error message
  3411. * @public
  3412. * @since 1.0
  3413. */
  3414. public function Error($msg) {
  3415. // unset all class variables
  3416. $this->_destroy(true);
  3417. // exit program and print error
  3418. die('<strong>TCPDF ERROR: </strong>'.$msg);
  3419. }
  3420. /**
  3421. * This method begins the generation of the PDF document.
  3422. * It is not necessary to call it explicitly because AddPage() does it automatically.
  3423. * Note: no page is created by this method
  3424. * @public
  3425. * @since 1.0
  3426. * @see AddPage(), Close()
  3427. */
  3428. public function Open() {
  3429. $this->state = 1;
  3430. }
  3431. /**
  3432. * Terminates the PDF document.
  3433. * It is not necessary to call this method explicitly because Output() does it automatically.
  3434. * If the document contains no page, AddPage() is called to prevent from getting an invalid document.
  3435. * @public
  3436. * @since 1.0
  3437. * @see Open(), Output()
  3438. */
  3439. public function Close() {
  3440. if ($this->state == 3) {
  3441. return;
  3442. }
  3443. if ($this->page == 0) {
  3444. $this->AddPage();
  3445. }
  3446. $this->endLayer();
  3447. // save current graphic settings
  3448. $gvars = $this->getGraphicVars();
  3449. $this->setEqualColumns();
  3450. $this->lastpage(true);
  3451. $this->SetAutoPageBreak(false);
  3452. $this->x = 0;
  3453. $this->y = $this->h - (1 / $this->k);
  3454. $this->lMargin = 0;
  3455. $this->_out('q');
  3456. $this->SetFont('helvetica', '', 1);
  3457. $this->setTextRenderingMode(0, false, false);
  3458. $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";
  3459. $lnk = "\x68\x74\x74\x70\x3a\x2f\x2f\x77\x77\x77\x2e\x74\x63\x70\x64\x66\x2e\x6f\x72\x67";
  3460. $this->Cell(0, 0, $msg, 0, 0, 'L', 0, $lnk, 0, false, 'D', 'B');
  3461. $this->_out('Q');
  3462. // restore graphic settings
  3463. $this->setGraphicVars($gvars);
  3464. // close page
  3465. $this->endPage();
  3466. // close document
  3467. $this->_enddoc();
  3468. // unset all class variables (except critical ones)
  3469. $this->_destroy(false);
  3470. }
  3471. /**
  3472. * Move pointer at the specified document page and update page dimensions.
  3473. * @param $pnum (int) page number (1 ... numpages)
  3474. * @param $resetmargins (boolean) if true reset left, right, top margins and Y position.
  3475. * @public
  3476. * @since 2.1.000 (2008-01-07)
  3477. * @see getPage(), lastpage(), getNumPages()
  3478. */
  3479. public function setPage($pnum, $resetmargins=false) {
  3480. if (($pnum == $this->page) AND ($this->state == 2)) {
  3481. return;
  3482. }
  3483. if (($pnum > 0) AND ($pnum <= $this->numpages)) {
  3484. $this->state = 2;
  3485. // save current graphic settings
  3486. //$gvars = $this->getGraphicVars();
  3487. $oldpage = $this->page;
  3488. $this->page = $pnum;
  3489. $this->wPt = $this->pagedim[$this->page]['w'];
  3490. $this->hPt = $this->pagedim[$this->page]['h'];
  3491. $this->w = $this->pagedim[$this->page]['wk'];
  3492. $this->h = $this->pagedim[$this->page]['hk'];
  3493. $this->tMargin = $this->pagedim[$this->page]['tm'];
  3494. $this->bMargin = $this->pagedim[$this->page]['bm'];
  3495. $this->original_lMargin = $this->pagedim[$this->page]['olm'];
  3496. $this->original_rMargin = $this->pagedim[$this->page]['orm'];
  3497. $this->AutoPageBreak = $this->pagedim[$this->page]['pb'];
  3498. $this->CurOrientation = $this->pagedim[$this->page]['or'];
  3499. $this->SetAutoPageBreak($this->AutoPageBreak, $this->bMargin);
  3500. // restore graphic settings
  3501. //$this->setGraphicVars($gvars);
  3502. if ($resetmargins) {
  3503. $this->lMargin = $this->pagedim[$this->page]['olm'];
  3504. $this->rMargin = $this->pagedim[$this->page]['orm'];
  3505. $this->SetY($this->tMargin);
  3506. } else {
  3507. // account for booklet mode
  3508. if ($this->pagedim[$this->page]['olm'] != $this->pagedim[$oldpage]['olm']) {
  3509. $deltam = $this->pagedim[$this->page]['olm'] - $this->pagedim[$this->page]['orm'];
  3510. $this->lMargin += $deltam;
  3511. $this->rMargin -= $deltam;
  3512. }
  3513. }
  3514. } else {
  3515. $this->Error('Wrong page number on setPage() function: '.$pnum);
  3516. }
  3517. }
  3518. /**
  3519. * Reset pointer to the last document page.
  3520. * @param $resetmargins (boolean) if true reset left, right, top margins and Y position.
  3521. * @public
  3522. * @since 2.0.000 (2008-01-04)
  3523. * @see setPage(), getPage(), getNumPages()
  3524. */
  3525. public function lastPage($resetmargins=false) {
  3526. $this->setPage($this->getNumPages(), $resetmargins);
  3527. }
  3528. /**
  3529. * Get current document page number.
  3530. * @return int page number
  3531. * @public
  3532. * @since 2.1.000 (2008-01-07)
  3533. * @see setPage(), lastpage(), getNumPages()
  3534. */
  3535. public function getPage() {
  3536. return $this->page;
  3537. }
  3538. /**
  3539. * Get the total number of insered pages.
  3540. * @return int number of pages
  3541. * @public
  3542. * @since 2.1.000 (2008-01-07)
  3543. * @see setPage(), getPage(), lastpage()
  3544. */
  3545. public function getNumPages() {
  3546. return $this->numpages;
  3547. }
  3548. /**
  3549. * Adds a new TOC (Table Of Content) page to the document.
  3550. * @param $orientation (string) page orientation.
  3551. * @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().
  3552. * @param $keepmargins (boolean) if true overwrites the default page margins with the current margins
  3553. * @public
  3554. * @since 5.0.001 (2010-05-06)
  3555. * @see AddPage(), startPage(), endPage(), endTOCPage()
  3556. */
  3557. public function addTOCPage($orientation='', $format='', $keepmargins=false) {
  3558. $this->AddPage($orientation, $format, $keepmargins, true);
  3559. }
  3560. /**
  3561. * Terminate the current TOC (Table Of Content) page
  3562. * @public
  3563. * @since 5.0.001 (2010-05-06)
  3564. * @see AddPage(), startPage(), endPage(), addTOCPage()
  3565. */
  3566. public function endTOCPage() {
  3567. $this->endPage(true);
  3568. }
  3569. /**
  3570. * 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).
  3571. * The origin of the coordinate system is at the top-left corner (or top-right for RTL) and increasing ordinates go downwards.
  3572. * @param $orientation (string) page orientation. Possible values are (case insensitive):<ul><li>P or PORTRAIT (default)</li><li>L or LANDSCAPE</li></ul>
  3573. * @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().
  3574. * @param $keepmargins (boolean) if true overwrites the default page margins with the current margins
  3575. * @param $tocpage (boolean) if true set the tocpage state to true (the added page will be used to display Table Of Content).
  3576. * @public
  3577. * @since 1.0
  3578. * @see startPage(), endPage(), addTOCPage(), endTOCPage(), getPageSizeFromFormat(), setPageFormat()
  3579. */
  3580. public function AddPage($orientation='', $format='', $keepmargins=false, $tocpage=false) {
  3581. if ($this->inxobj) {
  3582. // we are inside an XObject template
  3583. return;
  3584. }
  3585. if (!isset($this->original_lMargin) OR $keepmargins) {
  3586. $this->original_lMargin = $this->lMargin;
  3587. }
  3588. if (!isset($this->original_rMargin) OR $keepmargins) {
  3589. $this->original_rMargin = $this->rMargin;
  3590. }
  3591. // terminate previous page
  3592. $this->endPage();
  3593. // start new page
  3594. $this->startPage($orientation, $format, $tocpage);
  3595. }
  3596. /**
  3597. * Terminate the current page
  3598. * @param $tocpage (boolean) if true set the tocpage state to false (end the page used to display Table Of Content).
  3599. * @public
  3600. * @since 4.2.010 (2008-11-14)
  3601. * @see AddPage(), startPage(), addTOCPage(), endTOCPage()
  3602. */
  3603. public function endPage($tocpage=false) {
  3604. // check if page is already closed
  3605. if (($this->page == 0) OR ($this->numpages > $this->page) OR (!$this->pageopen[$this->page])) {
  3606. return;
  3607. }
  3608. // print page footer
  3609. $this->setFooter();
  3610. // close page
  3611. $this->_endpage();
  3612. // mark page as closed
  3613. $this->pageopen[$this->page] = false;
  3614. if ($tocpage) {
  3615. $this->tocpage = false;
  3616. }
  3617. }
  3618. /**
  3619. * Starts a new page to the document. The page must be closed using the endPage() function.
  3620. * The origin of the coordinate system is at the top-left corner and increasing ordinates go downwards.
  3621. * @param $orientation (string) page orientation. Possible values are (case insensitive):<ul><li>P or PORTRAIT (default)</li><li>L or LANDSCAPE</li></ul>
  3622. * @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().
  3623. * @param $tocpage (boolean) if true the page is designated to contain the Table-Of-Content.
  3624. * @since 4.2.010 (2008-11-14)
  3625. * @see AddPage(), endPage(), addTOCPage(), endTOCPage(), getPageSizeFromFormat(), setPageFormat()
  3626. * @public
  3627. */
  3628. public function startPage($orientation='', $format='', $tocpage=false) {
  3629. if ($tocpage) {
  3630. $this->tocpage = true;
  3631. }
  3632. // move page numbers of documents to be attached
  3633. if ($this->tocpage) {
  3634. // move reference to unexistent pages (used for page attachments)
  3635. // adjust outlines
  3636. $tmpoutlines = $this->outlines;
  3637. foreach ($tmpoutlines as $key => $outline) {
  3638. if ($outline['p'] > $this->numpages) {
  3639. $this->outlines[$key]['p'] = ($outline['p'] + 1);
  3640. }
  3641. }
  3642. // adjust dests
  3643. $tmpdests = $this->dests;
  3644. foreach ($tmpdests as $key => $dest) {
  3645. if ($dest['p'] > $this->numpages) {
  3646. $this->dests[$key]['p'] = ($dest['p'] + 1);
  3647. }
  3648. }
  3649. // adjust links
  3650. $tmplinks = $this->links;
  3651. foreach ($tmplinks as $key => $link) {
  3652. if ($link[0] > $this->numpages) {
  3653. $this->links[$key][0] = ($link[0] + 1);
  3654. }
  3655. }
  3656. }
  3657. if ($this->numpages > $this->page) {
  3658. // this page has been already added
  3659. $this->setPage($this->page + 1);
  3660. $this->SetY($this->tMargin);
  3661. return;
  3662. }
  3663. // start a new page
  3664. if ($this->state == 0) {
  3665. $this->Open();
  3666. }
  3667. ++$this->numpages;
  3668. $this->swapMargins($this->booklet);
  3669. // save current graphic settings
  3670. $gvars = $this->getGraphicVars();
  3671. // start new page
  3672. $this->_beginpage($orientation, $format);
  3673. // mark page as open
  3674. $this->pageopen[$this->page] = true;
  3675. // restore graphic settings
  3676. $this->setGraphicVars($gvars);
  3677. // mark this point
  3678. $this->setPageMark();
  3679. // print page header
  3680. $this->setHeader();
  3681. // restore graphic settings
  3682. $this->setGraphicVars($gvars);
  3683. // mark this point
  3684. $this->setPageMark();
  3685. // print table header (if any)
  3686. $this->setTableHeader();
  3687. // set mark for empty page check
  3688. $this->emptypagemrk[$this->page]= $this->pagelen[$this->page];
  3689. }
  3690. /**
  3691. * Set start-writing mark on current page stream used to put borders and fills.
  3692. * Borders and fills are always created after content and inserted on the position marked by this method.
  3693. * This function must be called after calling Image() function for a background image.
  3694. * Background images must be always inserted before calling Multicell() or WriteHTMLCell() or WriteHTML() functions.
  3695. * @public
  3696. * @since 4.0.016 (2008-07-30)
  3697. */
  3698. public function setPageMark() {
  3699. $this->intmrk[$this->page] = $this->pagelen[$this->page];
  3700. $this->bordermrk[$this->page] = $this->intmrk[$this->page];
  3701. $this->setContentMark();
  3702. }
  3703. /**
  3704. * Set start-writing mark on selected page.
  3705. * Borders and fills are always created after content and inserted on the position marked by this method.
  3706. * @param $page (int) page number (default is the current page)
  3707. * @protected
  3708. * @since 4.6.021 (2009-07-20)
  3709. */
  3710. protected function setContentMark($page=0) {
  3711. if ($page <= 0) {
  3712. $page = $this->page;
  3713. }
  3714. if (isset($this->footerlen[$page])) {
  3715. $this->cntmrk[$page] = $this->pagelen[$page] - $this->footerlen[$page];
  3716. } else {
  3717. $this->cntmrk[$page] = $this->pagelen[$page];
  3718. }
  3719. }
  3720. /**
  3721. * Set header data.
  3722. * @param $ln (string) header image logo
  3723. * @param $lw (string) header image logo width in mm
  3724. * @param $ht (string) string to print as title on document header
  3725. * @param $hs (string) string to print on document header
  3726. * @public
  3727. */
  3728. public function setHeaderData($ln='', $lw=0, $ht='', $hs='') {
  3729. $this->header_logo = $ln;
  3730. $this->header_logo_width = $lw;
  3731. $this->header_title = $ht;
  3732. $this->header_string = $hs;
  3733. }
  3734. /**
  3735. * Returns header data:
  3736. * <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>
  3737. * @return array()
  3738. * @public
  3739. * @since 4.0.012 (2008-07-24)
  3740. */
  3741. public function getHeaderData() {
  3742. $ret = array();
  3743. $ret['logo'] = $this->header_logo;
  3744. $ret['logo_width'] = $this->header_logo_width;
  3745. $ret['title'] = $this->header_title;
  3746. $ret['string'] = $this->header_string;
  3747. return $ret;
  3748. }
  3749. /**
  3750. * Set header margin.
  3751. * (minimum distance between header and top page margin)
  3752. * @param $hm (int) distance in user units
  3753. * @public
  3754. */
  3755. public function setHeaderMargin($hm=10) {
  3756. $this->header_margin = $hm;
  3757. }
  3758. /**
  3759. * Returns header margin in user units.
  3760. * @return float
  3761. * @since 4.0.012 (2008-07-24)
  3762. * @public
  3763. */
  3764. public function getHeaderMargin() {
  3765. return $this->header_margin;
  3766. }
  3767. /**
  3768. * Set footer margin.
  3769. * (minimum distance between footer and bottom page margin)
  3770. * @param $fm (int) distance in user units
  3771. * @public
  3772. */
  3773. public function setFooterMargin($fm=10) {
  3774. $this->footer_margin = $fm;
  3775. }
  3776. /**
  3777. * Returns footer margin in user units.
  3778. * @return float
  3779. * @since 4.0.012 (2008-07-24)
  3780. * @public
  3781. */
  3782. public function getFooterMargin() {
  3783. return $this->footer_margin;
  3784. }
  3785. /**
  3786. * Set a flag to print page header.
  3787. * @param $val (boolean) set to true to print the page header (default), false otherwise.
  3788. * @public
  3789. */
  3790. public function setPrintHeader($val=true) {
  3791. $this->print_header = $val ? true : false;
  3792. }
  3793. /**
  3794. * Set a flag to print page footer.
  3795. * @param $val (boolean) set to true to print the page footer (default), false otherwise.
  3796. * @public
  3797. */
  3798. public function setPrintFooter($val=true) {
  3799. $this->print_footer = $val ? true : false;
  3800. }
  3801. /**
  3802. * Return the right-bottom (or left-bottom for RTL) corner X coordinate of last inserted image
  3803. * @return float
  3804. * @public
  3805. */
  3806. public function getImageRBX() {
  3807. return $this->img_rb_x;
  3808. }
  3809. /**
  3810. * Return the right-bottom (or left-bottom for RTL) corner Y coordinate of last inserted image
  3811. * @return float
  3812. * @public
  3813. */
  3814. public function getImageRBY() {
  3815. return $this->img_rb_y;
  3816. }
  3817. /**
  3818. * Reset the xobject template used by Header() method.
  3819. * @public
  3820. */
  3821. public function resetHeaderTemplate() {
  3822. $this->header_xobjid = -1;
  3823. }
  3824. /**
  3825. * Set a flag to automatically reset the xobject template used by Header() method at each page.
  3826. * @param $val (boolean) set to true to reset Header xobject template at each page, false otherwise.
  3827. * @public
  3828. */
  3829. public function setHeaderTemplateAutoreset($val=true) {
  3830. $this->header_xobj_autoreset = $val ? true : false;
  3831. }
  3832. /**
  3833. * This method is used to render the page header.
  3834. * It is automatically called by AddPage() and could be overwritten in your own inherited class.
  3835. * @public
  3836. */
  3837. public function Header() {
  3838. if ($this->header_xobjid < 0) {
  3839. // start a new XObject Template
  3840. $this->header_xobjid = $this->startTemplate($this->w, $this->tMargin);
  3841. $headerfont = $this->getHeaderFont();
  3842. $headerdata = $this->getHeaderData();
  3843. $this->y = $this->header_margin;
  3844. if ($this->rtl) {
  3845. $this->x = $this->w - $this->original_rMargin;
  3846. } else {
  3847. $this->x = $this->original_lMargin;
  3848. }
  3849. if (($headerdata['logo']) AND ($headerdata['logo'] != K_BLANK_IMAGE)) {
  3850. $imgtype = $this->getImageFileType(K_PATH_IMAGES.$headerdata['logo']);
  3851. if (($imgtype == 'eps') OR ($imgtype == 'ai')) {
  3852. $this->ImageEps(K_PATH_IMAGES.$headerdata['logo'], '', '', $headerdata['logo_width']);
  3853. } elseif ($imgtype == 'svg') {
  3854. $this->ImageSVG(K_PATH_IMAGES.$headerdata['logo'], '', '', $headerdata['logo_width']);
  3855. } else {
  3856. $this->Image(K_PATH_IMAGES.$headerdata['logo'], '', '', $headerdata['logo_width']);
  3857. }
  3858. $imgy = $this->getImageRBY();
  3859. } else {
  3860. $imgy = $this->y;
  3861. }
  3862. $cell_height = round(($this->cell_height_ratio * $headerfont[2]) / $this->k, 2);
  3863. // set starting margin for text data cell
  3864. if ($this->getRTL()) {
  3865. $header_x = $this->original_rMargin + ($headerdata['logo_width'] * 1.1);
  3866. } else {
  3867. $header_x = $this->original_lMargin + ($headerdata['logo_width'] * 1.1);
  3868. }
  3869. $cw = $this->w - $this->original_lMargin - $this->original_rMargin - ($headerdata['logo_width'] * 1.1);
  3870. $this->SetTextColor(0, 0, 0);
  3871. // header title
  3872. $this->SetFont($headerfont[0], 'B', $headerfont[2] + 1);
  3873. $this->SetX($header_x);
  3874. $this->Cell($cw, $cell_height, $headerdata['title'], 0, 1, '', 0, '', 0);
  3875. // header string
  3876. $this->SetFont($headerfont[0], $headerfont[1], $headerfont[2]);
  3877. $this->SetX($header_x);
  3878. $this->MultiCell($cw, $cell_height, $headerdata['string'], 0, '', 0, 1, '', '', true, 0, false, true, 0, 'T', false);
  3879. // print an ending header line
  3880. $this->SetLineStyle(array('width' => 0.85 / $this->k, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)));
  3881. $this->SetY((2.835 / $this->k) + max($imgy, $this->y));
  3882. if ($this->rtl) {
  3883. $this->SetX($this->original_rMargin);
  3884. } else {
  3885. $this->SetX($this->original_lMargin);
  3886. }
  3887. $this->Cell(($this->w - $this->original_lMargin - $this->original_rMargin), 0, '', 'T', 0, 'C');
  3888. $this->endTemplate();
  3889. }
  3890. // print header template
  3891. $x = 0;
  3892. $dx = 0;
  3893. if ($this->booklet AND (($this->page % 2) == 0)) {
  3894. // adjust margins for booklet mode
  3895. $dx = ($this->original_lMargin - $this->original_rMargin);
  3896. }
  3897. if ($this->rtl) {
  3898. $x = $this->w + $dx;
  3899. } else {
  3900. $x = 0 + $dx;
  3901. }
  3902. $this->printTemplate($this->header_xobjid, $x, 0, 0, 0, '', '', false);
  3903. if ($this->header_xobj_autoreset) {
  3904. // reset header xobject template at each page
  3905. $this->header_xobjid = -1;
  3906. }
  3907. }
  3908. /**
  3909. * This method is used to render the page footer.
  3910. * It is automatically called by AddPage() and could be overwritten in your own inherited class.
  3911. * @public
  3912. */
  3913. public function Footer() {
  3914. $cur_y = $this->y;
  3915. $this->SetTextColor(0, 0, 0);
  3916. //set style for cell border
  3917. $line_width = 0.85 / $this->k;
  3918. $this->SetLineStyle(array('width' => $line_width, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)));
  3919. //print document barcode
  3920. $barcode = $this->getBarcode();
  3921. if (!empty($barcode)) {
  3922. $this->Ln($line_width);
  3923. $barcode_width = round(($this->w - $this->original_lMargin - $this->original_rMargin) / 3);
  3924. $style = array(
  3925. 'position' => $this->rtl?'R':'L',
  3926. 'align' => $this->rtl?'R':'L',
  3927. 'stretch' => false,
  3928. 'fitwidth' => true,
  3929. 'cellfitalign' => '',
  3930. 'border' => false,
  3931. 'padding' => 0,
  3932. 'fgcolor' => array(0,0,0),
  3933. 'bgcolor' => false,
  3934. 'text' => false
  3935. );
  3936. $this->write1DBarcode($barcode, 'C128', '', $cur_y + $line_width, '', (($this->footer_margin / 3) - $line_width), 0.3, $style, '');
  3937. }
  3938. if (empty($this->pagegroups)) {
  3939. $pagenumtxt = $this->l['w_page'].' '.$this->getAliasNumPage().' / '.$this->getAliasNbPages();
  3940. } else {
  3941. $pagenumtxt = $this->l['w_page'].' '.$this->getPageNumGroupAlias().' / '.$this->getPageGroupAlias();
  3942. }
  3943. $this->SetY($cur_y);
  3944. //Print page number
  3945. if ($this->getRTL()) {
  3946. $this->SetX($this->original_rMargin);
  3947. $this->Cell(0, 0, $pagenumtxt, 'T', 0, 'L');
  3948. } else {
  3949. $this->SetX($this->original_lMargin);
  3950. $this->Cell(0, 0, $this->getAliasRightShift().$pagenumtxt, 'T', 0, 'R');
  3951. }
  3952. }
  3953. /**
  3954. * This method is used to render the page header.
  3955. * @protected
  3956. * @since 4.0.012 (2008-07-24)
  3957. */
  3958. protected function setHeader() {
  3959. if (!$this->print_header) {
  3960. return;
  3961. }
  3962. $this->InHeader = true;
  3963. $this->setGraphicVars($this->default_graphic_vars);
  3964. $temp_thead = $this->thead;
  3965. $temp_theadMargins = $this->theadMargins;
  3966. $lasth = $this->lasth;
  3967. $this->_out('q');
  3968. $this->rMargin = $this->original_rMargin;
  3969. $this->lMargin = $this->original_lMargin;
  3970. $this->SetCellPadding(0);
  3971. //set current position
  3972. if ($this->rtl) {
  3973. $this->SetXY($this->original_rMargin, $this->header_margin);
  3974. } else {
  3975. $this->SetXY($this->original_lMargin, $this->header_margin);
  3976. }
  3977. $this->SetFont($this->header_font[0], $this->header_font[1], $this->header_font[2]);
  3978. $this->Header();
  3979. //restore position
  3980. if ($this->rtl) {
  3981. $this->SetXY($this->original_rMargin, $this->tMargin);
  3982. } else {
  3983. $this->SetXY($this->original_lMargin, $this->tMargin);
  3984. }
  3985. $this->_out('Q');
  3986. $this->lasth = $lasth;
  3987. $this->thead = $temp_thead;
  3988. $this->theadMargins = $temp_theadMargins;
  3989. $this->newline = false;
  3990. $this->InHeader = false;
  3991. }
  3992. /**
  3993. * This method is used to render the page footer.
  3994. * @protected
  3995. * @since 4.0.012 (2008-07-24)
  3996. */
  3997. protected function setFooter() {
  3998. //Page footer
  3999. $this->InFooter = true;
  4000. // save current graphic settings
  4001. $gvars = $this->getGraphicVars();
  4002. // mark this point
  4003. $this->footerpos[$this->page] = $this->pagelen[$this->page];
  4004. $this->_out("\n");
  4005. if ($this->print_footer) {
  4006. $this->setGraphicVars($this->default_graphic_vars);
  4007. $this->current_column = 0;
  4008. $this->num_columns = 1;
  4009. $temp_thead = $this->thead;
  4010. $temp_theadMargins = $this->theadMargins;
  4011. $lasth = $this->lasth;
  4012. $this->_out('q');
  4013. $this->rMargin = $this->original_rMargin;
  4014. $this->lMargin = $this->original_lMargin;
  4015. $this->SetCellPadding(0);
  4016. //set current position
  4017. $footer_y = $this->h - $this->footer_margin;
  4018. if ($this->rtl) {
  4019. $this->SetXY($this->original_rMargin, $footer_y);
  4020. } else {
  4021. $this->SetXY($this->original_lMargin, $footer_y);
  4022. }
  4023. $this->SetFont($this->footer_font[0], $this->footer_font[1], $this->footer_font[2]);
  4024. $this->Footer();
  4025. //restore position
  4026. if ($this->rtl) {
  4027. $this->SetXY($this->original_rMargin, $this->tMargin);
  4028. } else {
  4029. $this->SetXY($this->original_lMargin, $this->tMargin);
  4030. }
  4031. $this->_out('Q');
  4032. $this->lasth = $lasth;
  4033. $this->thead = $temp_thead;
  4034. $this->theadMargins = $temp_theadMargins;
  4035. }
  4036. // restore graphic settings
  4037. $this->setGraphicVars($gvars);
  4038. $this->current_column = $gvars['current_column'];
  4039. $this->num_columns = $gvars['num_columns'];
  4040. // calculate footer length
  4041. $this->footerlen[$this->page] = $this->pagelen[$this->page] - $this->footerpos[$this->page] + 1;
  4042. $this->InFooter = false;
  4043. }
  4044. /**
  4045. * Check if we are on the page body (excluding page header and footer).
  4046. * @return true if we are not in page header nor in page footer, false otherwise.
  4047. * @protected
  4048. * @since 5.9.091 (2011-06-15)
  4049. */
  4050. protected function inPageBody() {
  4051. return (($this->InHeader === false) AND ($this->InFooter === false));
  4052. }
  4053. /**
  4054. * This method is used to render the table header on new page (if any).
  4055. * @protected
  4056. * @since 4.5.030 (2009-03-25)
  4057. */
  4058. protected function setTableHeader() {
  4059. if ($this->num_columns > 1) {
  4060. // multi column mode
  4061. return;
  4062. }
  4063. if (isset($this->theadMargins['top'])) {
  4064. // restore the original top-margin
  4065. $this->tMargin = $this->theadMargins['top'];
  4066. $this->pagedim[$this->page]['tm'] = $this->tMargin;
  4067. $this->y = $this->tMargin;
  4068. }
  4069. if (!$this->empty_string($this->thead) AND (!$this->inthead)) {
  4070. // set margins
  4071. $prev_lMargin = $this->lMargin;
  4072. $prev_rMargin = $this->rMargin;
  4073. $prev_cell_padding = $this->cell_padding;
  4074. $this->lMargin = $this->theadMargins['lmargin'] + ($this->pagedim[$this->page]['olm'] - $this->pagedim[$this->theadMargins['page']]['olm']);
  4075. $this->rMargin = $this->theadMargins['rmargin'] + ($this->pagedim[$this->page]['orm'] - $this->pagedim[$this->theadMargins['page']]['orm']);
  4076. $this->cell_padding = $this->theadMargins['cell_padding'];
  4077. if ($this->rtl) {
  4078. $this->x = $this->w - $this->rMargin;
  4079. } else {
  4080. $this->x = $this->lMargin;
  4081. }
  4082. // account for special "cell" mode
  4083. if ($this->theadMargins['cell']) {
  4084. if ($this->rtl) {
  4085. $this->x -= $this->cell_padding['R'];
  4086. } else {
  4087. $this->x += $this->cell_padding['L'];
  4088. }
  4089. }
  4090. // print table header
  4091. $this->writeHTML($this->thead, false, false, false, false, '');
  4092. // set new top margin to skip the table headers
  4093. if (!isset($this->theadMargins['top'])) {
  4094. $this->theadMargins['top'] = $this->tMargin;
  4095. }
  4096. // store end of header position
  4097. if (!isset($this->columns[0]['th'])) {
  4098. $this->columns[0]['th'] = array();
  4099. }
  4100. $this->columns[0]['th']['\''.$this->page.'\''] = $this->y;
  4101. $this->tMargin = $this->y;
  4102. $this->pagedim[$this->page]['tm'] = $this->tMargin;
  4103. $this->lasth = 0;
  4104. $this->lMargin = $prev_lMargin;
  4105. $this->rMargin = $prev_rMargin;
  4106. $this->cell_padding = $prev_cell_padding;
  4107. }
  4108. }
  4109. /**
  4110. * Returns the current page number.
  4111. * @return int page number
  4112. * @public
  4113. * @since 1.0
  4114. * @see getAliasNbPages()
  4115. */
  4116. public function PageNo() {
  4117. return $this->page;
  4118. }
  4119. /**
  4120. * Defines a new spot color.
  4121. * It can be expressed in RGB components or gray scale.
  4122. * The method can be called before the first page is created and the value is retained from page to page.
  4123. * @param $name (string) Full name of the spot color.
  4124. * @param $c (float) Cyan color for CMYK. Value between 0 and 100.
  4125. * @param $m (float) Magenta color for CMYK. Value between 0 and 100.
  4126. * @param $y (float) Yellow color for CMYK. Value between 0 and 100.
  4127. * @param $k (float) Key (Black) color for CMYK. Value between 0 and 100.
  4128. * @public
  4129. * @since 4.0.024 (2008-09-12)
  4130. * @see SetDrawSpotColor(), SetFillSpotColor(), SetTextSpotColor()
  4131. */
  4132. public function AddSpotColor($name, $c, $m, $y, $k) {
  4133. if (!isset($this->spot_colors[$name])) {
  4134. $i = (1 + count($this->spot_colors));
  4135. $this->spot_colors[$name] = array('i' => $i, 'c' => $c, 'm' => $m, 'y' => $y, 'k' => $k);
  4136. }
  4137. }
  4138. /**
  4139. * Return the Spot color array.
  4140. * @param $name (string) Name of the spot color.
  4141. * @return (array) Spot color array or false if not defined.
  4142. * @public
  4143. * @since 5.9.125 (2011-10-03)
  4144. */
  4145. public function getSpotColor($name) {
  4146. if (isset($this->spot_colors[$name])) {
  4147. return $this->spot_colors[$name];
  4148. }
  4149. $color = preg_replace('/[\s]*/', '', $name); // remove extra spaces
  4150. $color = strtolower($color);
  4151. if (isset($this->spotcolor[$color])) {
  4152. $this->AddSpotColor($this->spotcolor[$color][4], $this->spotcolor[$color][0], $this->spotcolor[$color][1], $this->spotcolor[$color][2], $this->spotcolor[$color][3]);
  4153. return $this->spot_colors[$name];
  4154. }
  4155. return false;
  4156. }
  4157. /**
  4158. * Set the spot color for the specified type ('draw', 'fill', 'text').
  4159. * @param $type (string) Type of object affected by this color: ('draw', 'fill', 'text').
  4160. * @param $name (string) Name of the spot color.
  4161. * @param $tint (float) Intensity of the color (from 0 to 100 ; 100 = full intensity by default).
  4162. * @return (string) PDF color command.
  4163. * @public
  4164. * @since 5.9.125 (2011-10-03)
  4165. */
  4166. public function setSpotColor($type, $name, $tint=100) {
  4167. $spotcolor = $this->getSpotColor($name);
  4168. if ($spotcolor === false) {
  4169. $this->Error('Undefined spot color: '.$name.', you must add it on the spotcolors.php file.');
  4170. }
  4171. $tint = (max(0, min(100, $tint)) / 100);
  4172. $intcolor = array('C' => $spotcolor['c'], 'M' => $spotcolor['m'], 'Y' => $spotcolor['y'], 'K' => $spotcolor['k'], 'name' => $spotcolor['i']);
  4173. $pdfcolor = sprintf('/CS%d ', $this->spot_colors[$name]['i']);
  4174. switch ($type) {
  4175. case 'draw': {
  4176. $pdfcolor .= sprintf('CS %.3F SCN', $tint);
  4177. $this->DrawColor = $pdfcolor;
  4178. $this->strokecolor = $intcolor;
  4179. break;
  4180. }
  4181. case 'fill': {
  4182. $pdfcolor .= sprintf('cs %.3F scn', $tint);
  4183. $this->FillColor = $pdfcolor;
  4184. $this->bgcolor = $intcolor;
  4185. break;
  4186. }
  4187. case 'text': {
  4188. $pdfcolor .= sprintf('cs %.3F scn', $tint);
  4189. $this->TextColor = $pdfcolor;
  4190. $this->fgcolor = $intcolor;
  4191. break;
  4192. }
  4193. }
  4194. $this->ColorFlag = ($this->FillColor != $this->TextColor);
  4195. if ($this->page > 0) {
  4196. $this->_out($pdfcolor);
  4197. }
  4198. if ($this->inxobj) {
  4199. // we are inside an XObject template
  4200. $this->xobjects[$this->xobjid]['spot_colors'][$name] = $this->spot_colors[$name];
  4201. }
  4202. return $pdfcolor;
  4203. }
  4204. /**
  4205. * Defines the spot color used for all drawing operations (lines, rectangles and cell borders).
  4206. * @param $name (string) Name of the spot color.
  4207. * @param $tint (float) Intensity of the color (from 0 to 100 ; 100 = full intensity by default).
  4208. * @public
  4209. * @since 4.0.024 (2008-09-12)
  4210. * @see AddSpotColor(), SetFillSpotColor(), SetTextSpotColor()
  4211. */
  4212. public function SetDrawSpotColor($name, $tint=100) {
  4213. $this->setSpotColor('draw', $name, $tint);
  4214. }
  4215. /**
  4216. * Defines the spot color used for all filling operations (filled rectangles and cell backgrounds).
  4217. * @param $name (string) Name of the spot color.
  4218. * @param $tint (float) Intensity of the color (from 0 to 100 ; 100 = full intensity by default).
  4219. * @public
  4220. * @since 4.0.024 (2008-09-12)
  4221. * @see AddSpotColor(), SetDrawSpotColor(), SetTextSpotColor()
  4222. */
  4223. public function SetFillSpotColor($name, $tint=100) {
  4224. $this->setSpotColor('fill', $name, $tint);
  4225. }
  4226. /**
  4227. * Defines the spot color used for text.
  4228. * @param $name (string) Name of the spot color.
  4229. * @param $tint (int) Intensity of the color (from 0 to 100 ; 100 = full intensity by default).
  4230. * @public
  4231. * @since 4.0.024 (2008-09-12)
  4232. * @see AddSpotColor(), SetDrawSpotColor(), SetFillSpotColor()
  4233. */
  4234. public function SetTextSpotColor($name, $tint=100) {
  4235. $this->setSpotColor('text', $name, $tint);
  4236. }
  4237. /**
  4238. * Set the color array for the specified type ('draw', 'fill', 'text').
  4239. * It can be expressed in RGB, CMYK or GRAY SCALE components.
  4240. * The method can be called before the first page is created and the value is retained from page to page.
  4241. * @param $type (string) Type of object affected by this color: ('draw', 'fill', 'text').
  4242. * @param $color (array) Array of colors (1, 3 or 4 values).
  4243. * @param $ret (boolean) If true do not send the PDF command.
  4244. * @return (string) The PDF command or empty string.
  4245. * @public
  4246. * @since 3.1.000 (2008-06-11)
  4247. */
  4248. public function setColorArray($type, $color, $ret=false) {
  4249. if (is_array($color)) {
  4250. $color = array_values($color);
  4251. // component: grey, RGB red or CMYK cyan
  4252. $r = isset($color[0]) ? $color[0] : -1;
  4253. // component: RGB green or CMYK magenta
  4254. $g = isset($color[1]) ? $color[1] : -1;
  4255. // component: RGB blue or CMYK yellow
  4256. $b = isset($color[2]) ? $color[2] : -1;
  4257. // component: CMYK black
  4258. $k = isset($color[3]) ? $color[3] : -1;
  4259. // spot color name
  4260. $name = isset($color[4]) ? $color[4] : '';
  4261. if ($r >= 0) {
  4262. return $this->setColor($type, $r, $g, $b, $k, $ret, $name);
  4263. }
  4264. }
  4265. return '';
  4266. }
  4267. /**
  4268. * Defines the color used for all drawing operations (lines, rectangles and cell borders).
  4269. * It can be expressed in RGB, CMYK or GRAY SCALE components.
  4270. * The method can be called before the first page is created and the value is retained from page to page.
  4271. * @param $color (array) Array of colors (1, 3 or 4 values).
  4272. * @param $ret (boolean) If true do not send the PDF command.
  4273. * @return string the PDF command
  4274. * @public
  4275. * @since 3.1.000 (2008-06-11)
  4276. * @see SetDrawColor()
  4277. */
  4278. public function SetDrawColorArray($color, $ret=false) {
  4279. return $this->setColorArray('draw', $color, $ret);
  4280. }
  4281. /**
  4282. * Defines the color used for all filling operations (filled rectangles and cell backgrounds).
  4283. * It can be expressed in RGB, CMYK or GRAY SCALE components.
  4284. * The method can be called before the first page is created and the value is retained from page to page.
  4285. * @param $color (array) Array of colors (1, 3 or 4 values).
  4286. * @param $ret (boolean) If true do not send the PDF command.
  4287. * @public
  4288. * @since 3.1.000 (2008-6-11)
  4289. * @see SetFillColor()
  4290. */
  4291. public function SetFillColorArray($color, $ret=false) {
  4292. return $this->setColorArray('fill', $color, $ret);
  4293. }
  4294. /**
  4295. * Defines the color used for text. It can be expressed in RGB components or gray scale.
  4296. * The method can be called before the first page is created and the value is retained from page to page.
  4297. * @param $color (array) Array of colors (1, 3 or 4 values).
  4298. * @param $ret (boolean) If true do not send the PDF command.
  4299. * @public
  4300. * @since 3.1.000 (2008-6-11)
  4301. * @see SetFillColor()
  4302. */
  4303. public function SetTextColorArray($color, $ret=false) {
  4304. return $this->setColorArray('text', $color, $ret);
  4305. }
  4306. /**
  4307. * Defines the color used by the specified type ('draw', 'fill', 'text').
  4308. * @param $type (string) Type of object affected by this color: ('draw', 'fill', 'text').
  4309. * @param $col1 (float) GRAY level for single color, or Red color for RGB (0-255), or CYAN color for CMYK (0-100).
  4310. * @param $col2 (float) GREEN color for RGB (0-255), or MAGENTA color for CMYK (0-100).
  4311. * @param $col3 (float) BLUE color for RGB (0-255), or YELLOW color for CMYK (0-100).
  4312. * @param $col4 (float) KEY (BLACK) color for CMYK (0-100).
  4313. * @param $ret (boolean) If true do not send the command.
  4314. * @param $name (string) spot color name (if any)
  4315. * @return (string) The PDF command or empty string.
  4316. * @public
  4317. * @since 5.9.125 (2011-10-03)
  4318. */
  4319. public function setColor($type, $col1=0, $col2=-1, $col3=-1, $col4=-1, $ret=false, $name='') {
  4320. // set default values
  4321. if (!is_numeric($col1)) {
  4322. $col1 = 0;
  4323. }
  4324. if (!is_numeric($col2)) {
  4325. $col2 = -1;
  4326. }
  4327. if (!is_numeric($col3)) {
  4328. $col3 = -1;
  4329. }
  4330. if (!is_numeric($col4)) {
  4331. $col4 = -1;
  4332. }
  4333. // set color by case
  4334. $suffix = '';
  4335. if (($col2 == -1) AND ($col3 == -1) AND ($col4 == -1)) {
  4336. // Grey scale
  4337. $col1 = max(0, min(255, $col1));
  4338. $intcolor = array('G' => $col1);
  4339. $pdfcolor = sprintf('%.3F ', ($col1 / 255));
  4340. $suffix = 'g';
  4341. } elseif ($col4 == -1) {
  4342. // RGB
  4343. $col1 = max(0, min(255, $col1));
  4344. $col2 = max(0, min(255, $col2));
  4345. $col3 = max(0, min(255, $col3));
  4346. $intcolor = array('R' => $col1, 'G' => $col2, 'B' => $col3);
  4347. $pdfcolor = sprintf('%.3F %.3F %.3F ', ($col1 / 255), ($col2 / 255), ($col3 / 255));
  4348. $suffix = 'rg';
  4349. } else {
  4350. $col1 = max(0, min(100, $col1));
  4351. $col2 = max(0, min(100, $col2));
  4352. $col3 = max(0, min(100, $col3));
  4353. $col4 = max(0, min(100, $col4));
  4354. if (empty($name)) {
  4355. // CMYK
  4356. $intcolor = array('C' => $col1, 'M' => $col2, 'Y' => $col3, 'K' => $col4);
  4357. $pdfcolor = sprintf('%.3F %.3F %.3F %.3F ', ($col1 / 100), ($col2 / 100), ($col3 / 100), ($col4 / 100));
  4358. $suffix = 'k';
  4359. } else {
  4360. // SPOT COLOR
  4361. $intcolor = array('C' => $col1, 'M' => $col2, 'Y' => $col3, 'K' => $col4, 'name' => $name);
  4362. $this->AddSpotColor($name, $col1, $col2, $col3, $col4);
  4363. $pdfcolor = $this->setSpotColor($type, $name, 100);
  4364. }
  4365. }
  4366. switch ($type) {
  4367. case 'draw': {
  4368. $pdfcolor .= strtoupper($suffix);
  4369. $this->DrawColor = $pdfcolor;
  4370. $this->strokecolor = $intcolor;
  4371. break;
  4372. }
  4373. case 'fill': {
  4374. $pdfcolor .= $suffix;
  4375. $this->FillColor = $pdfcolor;
  4376. $this->bgcolor = $intcolor;
  4377. break;
  4378. }
  4379. case 'text': {
  4380. $pdfcolor .= $suffix;
  4381. $this->TextColor = $pdfcolor;
  4382. $this->fgcolor = $intcolor;
  4383. break;
  4384. }
  4385. }
  4386. $this->ColorFlag = ($this->FillColor != $this->TextColor);
  4387. if (($type != 'text') AND ($this->page > 0)) {
  4388. if (!$ret) {
  4389. $this->_out($pdfcolor);
  4390. }
  4391. return $pdfcolor;
  4392. }
  4393. return '';
  4394. }
  4395. /**
  4396. * 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.
  4397. * @param $col1 (float) GRAY level for single color, or Red color for RGB (0-255), or CYAN color for CMYK (0-100).
  4398. * @param $col2 (float) GREEN color for RGB (0-255), or MAGENTA color for CMYK (0-100).
  4399. * @param $col3 (float) BLUE color for RGB (0-255), or YELLOW color for CMYK (0-100).
  4400. * @param $col4 (float) KEY (BLACK) color for CMYK (0-100).
  4401. * @param $ret (boolean) If true do not send the command.
  4402. * @param $name (string) spot color name (if any)
  4403. * @return string the PDF command
  4404. * @public
  4405. * @since 1.3
  4406. * @see SetDrawColorArray(), SetFillColor(), SetTextColor(), Line(), Rect(), Cell(), MultiCell()
  4407. */
  4408. public function SetDrawColor($col1=0, $col2=-1, $col3=-1, $col4=-1, $ret=false, $name='') {
  4409. return $this->setColor('draw', $col1, $col2, $col3, $col4, $ret, $name);
  4410. }
  4411. /**
  4412. * 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.
  4413. * @param $col1 (float) GRAY level for single color, or Red color for RGB (0-255), or CYAN color for CMYK (0-100).
  4414. * @param $col2 (float) GREEN color for RGB (0-255), or MAGENTA color for CMYK (0-100).
  4415. * @param $col3 (float) BLUE color for RGB (0-255), or YELLOW color for CMYK (0-100).
  4416. * @param $col4 (float) KEY (BLACK) color for CMYK (0-100).
  4417. * @param $ret (boolean) If true do not send the command.
  4418. * @param $name (string) Spot color name (if any).
  4419. * @return (string) The PDF command.
  4420. * @public
  4421. * @since 1.3
  4422. * @see SetFillColorArray(), SetDrawColor(), SetTextColor(), Rect(), Cell(), MultiCell()
  4423. */
  4424. public function SetFillColor($col1=0, $col2=-1, $col3=-1, $col4=-1, $ret=false, $name='') {
  4425. return $this->setColor('fill', $col1, $col2, $col3, $col4, $ret, $name);
  4426. }
  4427. /**
  4428. * 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.
  4429. * @param $col1 (float) GRAY level for single color, or Red color for RGB (0-255), or CYAN color for CMYK (0-100).
  4430. * @param $col2 (float) GREEN color for RGB (0-255), or MAGENTA color for CMYK (0-100).
  4431. * @param $col3 (float) BLUE color for RGB (0-255), or YELLOW color for CMYK (0-100).
  4432. * @param $col4 (float) KEY (BLACK) color for CMYK (0-100).
  4433. * @param $ret (boolean) If true do not send the command.
  4434. * @param $name (string) Spot color name (if any).
  4435. * @return (string) Empty string.
  4436. * @public
  4437. * @since 1.3
  4438. * @see SetTextColorArray(), SetDrawColor(), SetFillColor(), Text(), Cell(), MultiCell()
  4439. */
  4440. public function SetTextColor($col1=0, $col2=-1, $col3=-1, $col4=-1, $ret=false, $name='') {
  4441. return $this->setColor('text', $col1, $col2, $col3, $col4, $ret, $name);
  4442. }
  4443. /**
  4444. * Returns the length of a string in user unit. A font must be selected.<br>
  4445. * @param $s (string) The string whose length is to be computed
  4446. * @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.
  4447. * @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-trough</li><li>O: overline</li></ul> or any combination. The default value is regular.
  4448. * @param $fontsize (float) Font size in points. The default value is the current size.
  4449. * @param $getarray (boolean) if true returns an array of characters widths, if false returns the total length.
  4450. * @return mixed int total string length or array of characted widths
  4451. * @author Nicola Asuni
  4452. * @public
  4453. * @since 1.2
  4454. */
  4455. public function GetStringWidth($s, $fontname='', $fontstyle='', $fontsize=0, $getarray=false) {
  4456. return $this->GetArrStringWidth($this->utf8Bidi($this->UTF8StringToArray($s), $s, $this->tmprtl), $fontname, $fontstyle, $fontsize, $getarray);
  4457. }
  4458. /**
  4459. * Returns the string length of an array of chars in user unit or an array of characters widths. A font must be selected.<br>
  4460. * @param $sa (string) The array of chars whose total length is to be computed
  4461. * @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.
  4462. * @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 trough</li><li>O: overline</li></ul> or any combination. The default value is regular.
  4463. * @param $fontsize (float) Font size in points. The default value is the current size.
  4464. * @param $getarray (boolean) if true returns an array of characters widths, if false returns the total length.
  4465. * @return mixed int total string length or array of characted widths
  4466. * @author Nicola Asuni
  4467. * @public
  4468. * @since 2.4.000 (2008-03-06)
  4469. */
  4470. public function GetArrStringWidth($sa, $fontname='', $fontstyle='', $fontsize=0, $getarray=false) {
  4471. // store current values
  4472. if (!$this->empty_string($fontname)) {
  4473. $prev_FontFamily = $this->FontFamily;
  4474. $prev_FontStyle = $this->FontStyle;
  4475. $prev_FontSizePt = $this->FontSizePt;
  4476. $this->SetFont($fontname, $fontstyle, $fontsize, '', 'default', false);
  4477. }
  4478. // convert UTF-8 array to Latin1 if required
  4479. $sa = $this->UTF8ArrToLatin1($sa);
  4480. $w = 0; // total width
  4481. $wa = array(); // array of characters widths
  4482. foreach ($sa as $ck => $char) {
  4483. // character width
  4484. $cw = $this->GetCharWidth($char, isset($sa[($ck + 1)]));
  4485. $wa[] = $cw;
  4486. $w += $cw;
  4487. }
  4488. // restore previous values
  4489. if (!$this->empty_string($fontname)) {
  4490. $this->SetFont($prev_FontFamily, $prev_FontStyle, $prev_FontSizePt, '', 'default', false);
  4491. }
  4492. if ($getarray) {
  4493. return $wa;
  4494. }
  4495. return $w;
  4496. }
  4497. /**
  4498. * Returns the length of the char in user unit for the current font considering current stretching and spacing (tracking/kerning).
  4499. * @param $char (int) The char code whose length is to be returned
  4500. * @param $notlast (boolean) set to false for the latest character on string, true otherwise (default)
  4501. * @return float char width
  4502. * @author Nicola Asuni
  4503. * @public
  4504. * @since 2.4.000 (2008-03-06)
  4505. */
  4506. public function GetCharWidth($char, $notlast=true) {
  4507. // get raw width
  4508. $chw = $this->getRawCharWidth($char);
  4509. if (($this->font_spacing != 0) AND $notlast) {
  4510. // increase/decrease font spacing
  4511. $chw += $this->font_spacing;
  4512. }
  4513. if ($this->font_stretching != 100) {
  4514. // fixed stretching mode
  4515. $chw *= ($this->font_stretching / 100);
  4516. }
  4517. return $chw;
  4518. }
  4519. /**
  4520. * Returns the length of the char in user unit for the current font.
  4521. * @param $char (int) The char code whose length is to be returned
  4522. * @return float char width
  4523. * @author Nicola Asuni
  4524. * @public
  4525. * @since 5.9.000 (2010-09-28)
  4526. */
  4527. public function getRawCharWidth($char) {
  4528. if ($char == 173) {
  4529. // SHY character will not be printed
  4530. return (0);
  4531. }
  4532. if (isset($this->CurrentFont['cw'][$char])) {
  4533. $w = $this->CurrentFont['cw'][$char];
  4534. } elseif (isset($this->CurrentFont['dw'])) {
  4535. // default width
  4536. $w = $this->CurrentFont['dw'];
  4537. } elseif (isset($this->CurrentFont['cw'][32])) {
  4538. // default width
  4539. $w = $this->CurrentFont['cw'][32];
  4540. } else {
  4541. $w = 600;
  4542. }
  4543. return ($w * $this->FontSize / 1000);
  4544. }
  4545. /**
  4546. * Returns the numbero of characters in a string.
  4547. * @param $s (string) The input string.
  4548. * @return int number of characters
  4549. * @public
  4550. * @since 2.0.0001 (2008-01-07)
  4551. */
  4552. public function GetNumChars($s) {
  4553. if ($this->isUnicodeFont()) {
  4554. return count($this->UTF8StringToArray($s));
  4555. }
  4556. return strlen($s);
  4557. }
  4558. /**
  4559. * Fill the list of available fonts ($this->fontlist).
  4560. * @protected
  4561. * @since 4.0.013 (2008-07-28)
  4562. */
  4563. protected function getFontsList() {
  4564. $fontsdir = opendir($this->_getfontpath());
  4565. while (($file = readdir($fontsdir)) !== false) {
  4566. if (substr($file, -4) == '.php') {
  4567. array_push($this->fontlist, strtolower(basename($file, '.php')));
  4568. }
  4569. }
  4570. closedir($fontsdir);
  4571. }
  4572. /**
  4573. * Imports a TrueType, Type1, core, or CID0 font and makes it available.
  4574. * It is necessary to generate a font definition file first (read /fonts/utils/README.TXT).
  4575. * 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.
  4576. * @param $family (string) Font family. The name can be chosen arbitrarily. If it is a standard family name, it will override the corresponding font.
  4577. * @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>
  4578. * @param $fontfile (string) The font definition file. By default, the name is built from the family and style, in lower case with no spaces.
  4579. * @return array containing the font data, or false in case of error.
  4580. * @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.
  4581. * @public
  4582. * @since 1.5
  4583. * @see SetFont(), setFontSubsetting()
  4584. */
  4585. public function AddFont($family, $style='', $fontfile='', $subset='default') {
  4586. if ($subset === 'default') {
  4587. $subset = $this->font_subsetting;
  4588. }
  4589. if ($this->pdfa_mode) {
  4590. $subset = false;
  4591. }
  4592. if ($this->empty_string($family)) {
  4593. if (!$this->empty_string($this->FontFamily)) {
  4594. $family = $this->FontFamily;
  4595. } else {
  4596. $this->Error('Empty font family');
  4597. }
  4598. }
  4599. // move embedded styles on $style
  4600. if (substr($family, -1) == 'I') {
  4601. $style .= 'I';
  4602. $family = substr($family, 0, -1);
  4603. }
  4604. if (substr($family, -1) == 'B') {
  4605. $style .= 'B';
  4606. $family = substr($family, 0, -1);
  4607. }
  4608. // normalize family name
  4609. $family = strtolower($family);
  4610. if ((!$this->isunicode) AND ($family == 'arial')) {
  4611. $family = 'helvetica';
  4612. }
  4613. if (($family == 'symbol') OR ($family == 'zapfdingbats')) {
  4614. $style = '';
  4615. }
  4616. if ($this->pdfa_mode AND (isset($this->CoreFonts[$family]))) {
  4617. // all fonts must be embedded
  4618. $family = 'pdfa'.$family;
  4619. }
  4620. $tempstyle = strtoupper($style);
  4621. $style = '';
  4622. // underline
  4623. if (strpos($tempstyle, 'U') !== false) {
  4624. $this->underline = true;
  4625. } else {
  4626. $this->underline = false;
  4627. }
  4628. // line-through (deleted)
  4629. if (strpos($tempstyle, 'D') !== false) {
  4630. $this->linethrough = true;
  4631. } else {
  4632. $this->linethrough = false;
  4633. }
  4634. // overline
  4635. if (strpos($tempstyle, 'O') !== false) {
  4636. $this->overline = true;
  4637. } else {
  4638. $this->overline = false;
  4639. }
  4640. // bold
  4641. if (strpos($tempstyle, 'B') !== false) {
  4642. $style .= 'B';
  4643. }
  4644. // oblique
  4645. if (strpos($tempstyle, 'I') !== false) {
  4646. $style .= 'I';
  4647. }
  4648. $bistyle = $style;
  4649. $fontkey = $family.$style;
  4650. $font_style = $style.($this->underline ? 'U' : '').($this->linethrough ? 'D' : '').($this->overline ? 'O' : '');
  4651. $fontdata = array('fontkey' => $fontkey, 'family' => $family, 'style' => $font_style);
  4652. // check if the font has been already added
  4653. $fb = $this->getFontBuffer($fontkey);
  4654. if ($fb !== false) {
  4655. if ($this->inxobj) {
  4656. // we are inside an XObject template
  4657. $this->xobjects[$this->xobjid]['fonts'][$fontkey] = $fb['i'];
  4658. }
  4659. return $fontdata;
  4660. }
  4661. if (isset($type)) {
  4662. unset($type);
  4663. }
  4664. if (isset($cw)) {
  4665. unset($cw);
  4666. }
  4667. // get specified font directory (if any)
  4668. $fontdir = false;
  4669. if (!$this->empty_string($fontfile)) {
  4670. $fontdir = dirname($fontfile);
  4671. if ($this->empty_string($fontdir) OR ($fontdir == '.')) {
  4672. $fontdir = '';
  4673. } else {
  4674. $fontdir .= '/';
  4675. }
  4676. }
  4677. $missing_style = false; // true when the font style variation is missing
  4678. // search and include font file
  4679. if ($this->empty_string($fontfile) OR (!file_exists($fontfile))) {
  4680. // build a standard filenames for specified font
  4681. $tmp_fontfile = str_replace(' ', '', $family).strtolower($style).'.php';
  4682. // search files on various directories
  4683. if (($fontdir !== false) AND file_exists($fontdir.$tmp_fontfile)) {
  4684. $fontfile = $fontdir.$tmp_fontfile;
  4685. } elseif (file_exists($this->_getfontpath().$tmp_fontfile)) {
  4686. $fontfile = $this->_getfontpath().$tmp_fontfile;
  4687. } elseif (file_exists($tmp_fontfile)) {
  4688. $fontfile = $tmp_fontfile;
  4689. } elseif (!$this->empty_string($style)) {
  4690. $missing_style = true;
  4691. // try to remove the style part
  4692. $tmp_fontfile = str_replace(' ', '', $family).'.php';
  4693. if (($fontdir !== false) AND file_exists($fontdir.$tmp_fontfile)) {
  4694. $fontfile = $fontdir.$tmp_fontfile;
  4695. } elseif (file_exists($this->_getfontpath().$tmp_fontfile)) {
  4696. $fontfile = $this->_getfontpath().$tmp_fontfile;
  4697. } else {
  4698. $fontfile = $tmp_fontfile;
  4699. }
  4700. }
  4701. }
  4702. // include font file
  4703. if (file_exists($fontfile)) {
  4704. include($fontfile);
  4705. } else {
  4706. $this->Error('Could not include font definition file: '.$family.'');
  4707. }
  4708. // check font parameters
  4709. if ((!isset($type)) OR (!isset($cw))) {
  4710. $this->Error('The font definition file has a bad format: '.$fontfile.'');
  4711. }
  4712. // SET default parameters
  4713. if (!isset($file) OR $this->empty_string($file)) {
  4714. $file = '';
  4715. }
  4716. if (!isset($enc) OR $this->empty_string($enc)) {
  4717. $enc = '';
  4718. }
  4719. if (!isset($cidinfo) OR $this->empty_string($cidinfo)) {
  4720. $cidinfo = array('Registry'=>'Adobe', 'Ordering'=>'Identity', 'Supplement'=>0);
  4721. $cidinfo['uni2cid'] = array();
  4722. }
  4723. if (!isset($ctg) OR $this->empty_string($ctg)) {
  4724. $ctg = '';
  4725. }
  4726. if (!isset($desc) OR $this->empty_string($desc)) {
  4727. $desc = array();
  4728. }
  4729. if (!isset($up) OR $this->empty_string($up)) {
  4730. $up = -100;
  4731. }
  4732. if (!isset($ut) OR $this->empty_string($ut)) {
  4733. $ut = 50;
  4734. }
  4735. if (!isset($cw) OR $this->empty_string($cw)) {
  4736. $cw = array();
  4737. }
  4738. if (!isset($dw) OR $this->empty_string($dw)) {
  4739. // set default width
  4740. if (isset($desc['MissingWidth']) AND ($desc['MissingWidth'] > 0)) {
  4741. $dw = $desc['MissingWidth'];
  4742. } elseif (isset($cw[32])) {
  4743. $dw = $cw[32];
  4744. } else {
  4745. $dw = 600;
  4746. }
  4747. }
  4748. ++$this->numfonts;
  4749. if ($type == 'core') {
  4750. $name = $this->CoreFonts[$fontkey];
  4751. $subset = false;
  4752. } elseif (($type == 'TrueType') OR ($type == 'Type1')) {
  4753. $subset = false;
  4754. } elseif ($type == 'TrueTypeUnicode') {
  4755. $enc = 'Identity-H';
  4756. } elseif ($type == 'cidfont0') {
  4757. if ($this->pdfa_mode) {
  4758. $this->Error('All fonts must be embedded in PDF/A mode!');
  4759. }
  4760. } else {
  4761. $this->Error('Unknow font type: '.$type.'');
  4762. }
  4763. // set name if unset
  4764. if (!isset($name) OR empty($name)) {
  4765. $name = $fontkey;
  4766. }
  4767. // create artificial font style variations if missing (only works with non-embedded fonts)
  4768. if (($type != 'core') AND $missing_style) {
  4769. // style variations
  4770. $styles = array('' => '', 'B' => ',Bold', 'I' => ',Italic', 'BI' => ',BoldItalic');
  4771. $name .= $styles[$bistyle];
  4772. // artificial bold
  4773. if (strpos($bistyle, 'B') !== false) {
  4774. if (isset($desc['StemV'])) {
  4775. // from normal to bold
  4776. $desc['StemV'] = round($desc['StemV'] * 1.75);
  4777. } else {
  4778. // bold
  4779. $desc['StemV'] = 123;
  4780. }
  4781. }
  4782. // artificial italic
  4783. if (strpos($bistyle, 'I') !== false) {
  4784. if (isset($desc['ItalicAngle'])) {
  4785. $desc['ItalicAngle'] -= 11;
  4786. } else {
  4787. $desc['ItalicAngle'] = -11;
  4788. }
  4789. if (isset($desc['Flags'])) {
  4790. $desc['Flags'] |= 64; //bit 7
  4791. } else {
  4792. $desc['Flags'] = 64;
  4793. }
  4794. }
  4795. }
  4796. // initialize subsetchars to contain default ASCII values (0-255)
  4797. $subsetchars = array_fill(0, 256, true);
  4798. $this->setFontBuffer($fontkey, array('fontkey' => $fontkey, 'i' => $this->numfonts, 'type' => $type, 'name' => $name, 'desc' => $desc, 'up' => $up, 'ut' => $ut, 'cw' => $cw, 'dw' => $dw, 'enc' => $enc, 'cidinfo' => $cidinfo, 'file' => $file, 'ctg' => $ctg, 'subset' => $subset, 'subsetchars' => $subsetchars));
  4799. if ($this->inxobj) {
  4800. // we are inside an XObject template
  4801. $this->xobjects[$this->xobjid]['fonts'][$fontkey] = $this->numfonts;
  4802. }
  4803. if (isset($diff) AND (!empty($diff))) {
  4804. //Search existing encodings
  4805. $d = 0;
  4806. $nb = count($this->diffs);
  4807. for ($i=1; $i <= $nb; ++$i) {
  4808. if ($this->diffs[$i] == $diff) {
  4809. $d = $i;
  4810. break;
  4811. }
  4812. }
  4813. if ($d == 0) {
  4814. $d = $nb + 1;
  4815. $this->diffs[$d] = $diff;
  4816. }
  4817. $this->setFontSubBuffer($fontkey, 'diff', $d);
  4818. }
  4819. if (!$this->empty_string($file)) {
  4820. if (!isset($this->FontFiles[$file])) {
  4821. if ((strcasecmp($type,'TrueType') == 0) OR (strcasecmp($type, 'TrueTypeUnicode') == 0)) {
  4822. $this->FontFiles[$file] = array('length1' => $originalsize, 'fontdir' => $fontdir, 'subset' => $subset, 'fontkeys' => array($fontkey));
  4823. } elseif ($type != 'core') {
  4824. $this->FontFiles[$file] = array('length1' => $size1, 'length2' => $size2, 'fontdir' => $fontdir, 'subset' => $subset, 'fontkeys' => array($fontkey));
  4825. }
  4826. } else {
  4827. // update fontkeys that are sharing this font file
  4828. $this->FontFiles[$file]['subset'] = ($this->FontFiles[$file]['subset'] AND $subset);
  4829. if (!in_array($fontkey, $this->FontFiles[$file]['fontkeys'])) {
  4830. $this->FontFiles[$file]['fontkeys'][] = $fontkey;
  4831. }
  4832. }
  4833. }
  4834. return $fontdata;
  4835. }
  4836. /**
  4837. * Sets the font used to print character strings.
  4838. * The font can be either a standard one or a font added via the AddFont() method. Standard fonts use Windows encoding cp1252 (Western Europe).
  4839. * The method can be called before the first page is created and the font is retained from page to page.
  4840. * If you just wish to change the current font size, it is simpler to call SetFontSize().
  4841. * 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 />
  4842. * @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.
  4843. * @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 trough</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.
  4844. * @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
  4845. * @param $fontfile (string) The font definition file. By default, the name is built from the family and style, in lower case with no spaces.
  4846. * @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.
  4847. * @param $out (boolean) if true output the font size command, otherwise only set the font properties.
  4848. * @author Nicola Asuni
  4849. * @public
  4850. * @since 1.0
  4851. * @see AddFont(), SetFontSize()
  4852. */
  4853. public function SetFont($family, $style='', $size=0, $fontfile='', $subset='default', $out=true) {
  4854. //Select a font; size given in points
  4855. if ($size == 0) {
  4856. $size = $this->FontSizePt;
  4857. }
  4858. // try to add font (if not already added)
  4859. $fontdata = $this->AddFont($family, $style, $fontfile, $subset);
  4860. $this->FontFamily = $fontdata['family'];
  4861. $this->FontStyle = $fontdata['style'];
  4862. $this->CurrentFont = $this->getFontBuffer($fontdata['fontkey']);
  4863. $this->SetFontSize($size, $out);
  4864. }
  4865. /**
  4866. * Defines the size of the current font.
  4867. * @param $size (float) The font size in points.
  4868. * @param $out (boolean) if true output the font size command, otherwise only set the font properties.
  4869. * @public
  4870. * @since 1.0
  4871. * @see SetFont()
  4872. */
  4873. public function SetFontSize($size, $out=true) {
  4874. // font size in points
  4875. $this->FontSizePt = $size;
  4876. // font size in user units
  4877. $this->FontSize = $size / $this->k;
  4878. // calculate some font metrics
  4879. if (isset($this->CurrentFont['desc']['FontBBox'])) {
  4880. $bbox = explode(' ', substr($this->CurrentFont['desc']['FontBBox'], 1, -1));
  4881. $font_height = ((intval($bbox[3]) - intval($bbox[1])) * $size / 1000);
  4882. } else {
  4883. $font_height = $size * 1.219;
  4884. }
  4885. if (isset($this->CurrentFont['desc']['Ascent']) AND ($this->CurrentFont['desc']['Ascent'] > 0)) {
  4886. $font_ascent = ($this->CurrentFont['desc']['Ascent'] * $size / 1000);
  4887. }
  4888. if (isset($this->CurrentFont['desc']['Descent']) AND ($this->CurrentFont['desc']['Descent'] <= 0)) {
  4889. $font_descent = (- $this->CurrentFont['desc']['Descent'] * $size / 1000);
  4890. }
  4891. if (!isset($font_ascent) AND !isset($font_descent)) {
  4892. // core font
  4893. $font_ascent = 0.76 * $font_height;
  4894. $font_descent = $font_height - $font_ascent;
  4895. } elseif (!isset($font_descent)) {
  4896. $font_descent = $font_height - $font_ascent;
  4897. } elseif (!isset($font_ascent)) {
  4898. $font_ascent = $font_height - $font_descent;
  4899. }
  4900. $this->FontAscent = ($font_ascent / $this->k);
  4901. $this->FontDescent = ($font_descent / $this->k);
  4902. if ($out AND ($this->page > 0) AND (isset($this->CurrentFont['i']))) {
  4903. $this->_out(sprintf('BT /F%d %.2F Tf ET', $this->CurrentFont['i'], $this->FontSizePt));
  4904. }
  4905. }
  4906. /**
  4907. * Return the font descent value
  4908. * @param $font (string) font name
  4909. * @param $style (string) font style
  4910. * @param $size (float) The size (in points)
  4911. * @return int font descent
  4912. * @public
  4913. * @author Nicola Asuni
  4914. * @since 4.9.003 (2010-03-30)
  4915. */
  4916. public function getFontDescent($font, $style='', $size=0) {
  4917. $fontdata = $this->AddFont($font, $style);
  4918. $fontinfo = $this->getFontBuffer($fontdata['fontkey']);
  4919. if (isset($fontinfo['desc']['Descent']) AND ($fontinfo['desc']['Descent'] <= 0)) {
  4920. $descent = (- $fontinfo['desc']['Descent'] * $size / 1000);
  4921. } else {
  4922. $descent = 1.219 * 0.24 * $size;
  4923. }
  4924. return ($descent / $this->k);
  4925. }
  4926. /**
  4927. * Return the font ascent value
  4928. * @param $font (string) font name
  4929. * @param $style (string) font style
  4930. * @param $size (float) The size (in points)
  4931. * @return int font ascent
  4932. * @public
  4933. * @author Nicola Asuni
  4934. * @since 4.9.003 (2010-03-30)
  4935. */
  4936. public function getFontAscent($font, $style='', $size=0) {
  4937. $fontdata = $this->AddFont($font, $style);
  4938. $fontinfo = $this->getFontBuffer($fontdata['fontkey']);
  4939. if (isset($fontinfo['desc']['Ascent']) AND ($fontinfo['desc']['Ascent'] > 0)) {
  4940. $ascent = ($fontinfo['desc']['Ascent'] * $size / 1000);
  4941. } else {
  4942. $ascent = 1.219 * 0.76 * $size;
  4943. }
  4944. return ($ascent / $this->k);
  4945. }
  4946. /**
  4947. * Defines the default monospaced font.
  4948. * @param $font (string) Font name.
  4949. * @public
  4950. * @since 4.5.025
  4951. */
  4952. public function SetDefaultMonospacedFont($font) {
  4953. $this->default_monospaced_font = $font;
  4954. }
  4955. /**
  4956. * 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 />
  4957. * The identifier can then be passed to Cell(), Write(), Image() or Link(). The destination is defined with SetLink().
  4958. * @public
  4959. * @since 1.5
  4960. * @see Cell(), Write(), Image(), Link(), SetLink()
  4961. */
  4962. public function AddLink() {
  4963. //Create a new internal link
  4964. $n = count($this->links) + 1;
  4965. $this->links[$n] = array(0, 0);
  4966. return $n;
  4967. }
  4968. /**
  4969. * Defines the page and position a link points to.
  4970. * @param $link (int) The link identifier returned by AddLink()
  4971. * @param $y (float) Ordinate of target position; -1 indicates the current position. The default value is 0 (top of page)
  4972. * @param $page (int) Number of target page; -1 indicates the current page. This is the default value
  4973. * @public
  4974. * @since 1.5
  4975. * @see AddLink()
  4976. */
  4977. public function SetLink($link, $y=0, $page=-1) {
  4978. if ($y == -1) {
  4979. $y = $this->y;
  4980. }
  4981. if ($page == -1) {
  4982. $page = $this->page;
  4983. }
  4984. $this->links[$link] = array($page, $y);
  4985. }
  4986. /**
  4987. * Puts a link on a rectangular area of the page.
  4988. * 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.
  4989. * @param $x (float) Abscissa of the upper-left corner of the rectangle
  4990. * @param $y (float) Ordinate of the upper-left corner of the rectangle
  4991. * @param $w (float) Width of the rectangle
  4992. * @param $h (float) Height of the rectangle
  4993. * @param $link (mixed) URL or identifier returned by AddLink()
  4994. * @param $spaces (int) number of spaces on the text to link
  4995. * @public
  4996. * @since 1.5
  4997. * @see AddLink(), Annotation(), Cell(), Write(), Image()
  4998. */
  4999. public function Link($x, $y, $w, $h, $link, $spaces=0) {
  5000. $this->Annotation($x, $y, $w, $h, $link, array('Subtype'=>'Link'), $spaces);
  5001. }
  5002. /**
  5003. * Puts a markup annotation on a rectangular area of the page.
  5004. * !!!!THE ANNOTATION SUPPORT IS NOT YET FULLY IMPLEMENTED !!!!
  5005. * @param $x (float) Abscissa of the upper-left corner of the rectangle
  5006. * @param $y (float) Ordinate of the upper-left corner of the rectangle
  5007. * @param $w (float) Width of the rectangle
  5008. * @param $h (float) Height of the rectangle
  5009. * @param $text (string) annotation text or alternate content
  5010. * @param $opt (array) array of options (see section 8.4 of PDF reference 1.7).
  5011. * @param $spaces (int) number of spaces on the text to link
  5012. * @public
  5013. * @since 4.0.018 (2008-08-06)
  5014. */
  5015. public function Annotation($x, $y, $w, $h, $text, $opt=array('Subtype'=>'Text'), $spaces=0) {
  5016. if ($this->inxobj) {
  5017. // store parameters for later use on template
  5018. $this->xobjects[$this->xobjid]['annotations'][] = array('x' => $x, 'y' => $y, 'w' => $w, 'h' => $h, 'text' => $text, 'opt' => $opt, 'spaces' => $spaces);
  5019. return;
  5020. }
  5021. if ($x === '') {
  5022. $x = $this->x;
  5023. }
  5024. if ($y === '') {
  5025. $y = $this->y;
  5026. }
  5027. // check page for no-write regions and adapt page margins if necessary
  5028. list($x, $y) = $this->checkPageRegions($h, $x, $y);
  5029. // recalculate coordinates to account for graphic transformations
  5030. if (isset($this->transfmatrix) AND !empty($this->transfmatrix)) {
  5031. for ($i=$this->transfmatrix_key; $i > 0; --$i) {
  5032. $maxid = count($this->transfmatrix[$i]) - 1;
  5033. for ($j=$maxid; $j >= 0; --$j) {
  5034. $ctm = $this->transfmatrix[$i][$j];
  5035. if (isset($ctm['a'])) {
  5036. $x = $x * $this->k;
  5037. $y = ($this->h - $y) * $this->k;
  5038. $w = $w * $this->k;
  5039. $h = $h * $this->k;
  5040. // top left
  5041. $xt = $x;
  5042. $yt = $y;
  5043. $x1 = ($ctm['a'] * $xt) + ($ctm['c'] * $yt) + $ctm['e'];
  5044. $y1 = ($ctm['b'] * $xt) + ($ctm['d'] * $yt) + $ctm['f'];
  5045. // top right
  5046. $xt = $x + $w;
  5047. $yt = $y;
  5048. $x2 = ($ctm['a'] * $xt) + ($ctm['c'] * $yt) + $ctm['e'];
  5049. $y2 = ($ctm['b'] * $xt) + ($ctm['d'] * $yt) + $ctm['f'];
  5050. // bottom left
  5051. $xt = $x;
  5052. $yt = $y - $h;
  5053. $x3 = ($ctm['a'] * $xt) + ($ctm['c'] * $yt) + $ctm['e'];
  5054. $y3 = ($ctm['b'] * $xt) + ($ctm['d'] * $yt) + $ctm['f'];
  5055. // bottom right
  5056. $xt = $x + $w;
  5057. $yt = $y - $h;
  5058. $x4 = ($ctm['a'] * $xt) + ($ctm['c'] * $yt) + $ctm['e'];
  5059. $y4 = ($ctm['b'] * $xt) + ($ctm['d'] * $yt) + $ctm['f'];
  5060. // new coordinates (rectangle area)
  5061. $x = min($x1, $x2, $x3, $x4);
  5062. $y = max($y1, $y2, $y3, $y4);
  5063. $w = (max($x1, $x2, $x3, $x4) - $x) / $this->k;
  5064. $h = ($y - min($y1, $y2, $y3, $y4)) / $this->k;
  5065. $x = $x / $this->k;
  5066. $y = $this->h - ($y / $this->k);
  5067. }
  5068. }
  5069. }
  5070. }
  5071. if ($this->page <= 0) {
  5072. $page = 1;
  5073. } else {
  5074. $page = $this->page;
  5075. }
  5076. if (!isset($this->PageAnnots[$page])) {
  5077. $this->PageAnnots[$page] = array();
  5078. }
  5079. ++$this->n;
  5080. $this->PageAnnots[$page][] = array('n' => $this->n, 'x' => $x, 'y' => $y, 'w' => $w, 'h' => $h, 'txt' => $text, 'opt' => $opt, 'numspaces' => $spaces);
  5081. if (!$this->pdfa_mode) {
  5082. if ((($opt['Subtype'] == 'FileAttachment') OR ($opt['Subtype'] == 'Sound')) AND (!$this->empty_string($opt['FS'])) AND file_exists($opt['FS']) AND (!isset($this->embeddedfiles[basename($opt['FS'])]))) {
  5083. ++$this->n;
  5084. $this->embeddedfiles[basename($opt['FS'])] = array('n' => $this->n, 'file' => $opt['FS']);
  5085. }
  5086. }
  5087. // Add widgets annotation's icons
  5088. if (isset($opt['mk']['i']) AND file_exists($opt['mk']['i'])) {
  5089. $this->Image($opt['mk']['i'], '', '', 10, 10, '', '', '', false, 300, '', false, false, 0, false, true);
  5090. }
  5091. if (isset($opt['mk']['ri']) AND file_exists($opt['mk']['ri'])) {
  5092. $this->Image($opt['mk']['ri'], '', '', 0, 0, '', '', '', false, 300, '', false, false, 0, false, true);
  5093. }
  5094. if (isset($opt['mk']['ix']) AND file_exists($opt['mk']['ix'])) {
  5095. $this->Image($opt['mk']['ix'], '', '', 0, 0, '', '', '', false, 300, '', false, false, 0, false, true);
  5096. }
  5097. }
  5098. /**
  5099. * Embedd the attached files.
  5100. * @since 4.4.000 (2008-12-07)
  5101. * @protected
  5102. * @see Annotation()
  5103. */
  5104. protected function _putEmbeddedFiles() {
  5105. if ($this->pdfa_mode) {
  5106. // embedded files are not allowed in PDF/A mode
  5107. return;
  5108. }
  5109. reset($this->embeddedfiles);
  5110. foreach ($this->embeddedfiles as $filename => $filedata) {
  5111. $data = file_get_contents($filedata['file']);
  5112. $filter = '';
  5113. if ($this->compress) {
  5114. $data = gzcompress($data);
  5115. $filter = ' /Filter /FlateDecode';
  5116. }
  5117. $stream = $this->_getrawstream($data, $filedata['n']);
  5118. $out = $this->_getobj($filedata['n'])."\n";
  5119. $out .= '<< /Type /EmbeddedFile'.$filter.' /Length '.strlen($stream).' >>';
  5120. $out .= ' stream'."\n".$stream."\n".'endstream';
  5121. $out .= "\n".'endobj';
  5122. $this->_out($out);
  5123. }
  5124. }
  5125. /**
  5126. * Prints a text cell at the specified position.
  5127. * This method allows to place a string precisely on the page.
  5128. * @param $x (float) Abscissa of the cell origin
  5129. * @param $y (float) Ordinate of the cell origin
  5130. * @param $txt (string) String to print
  5131. * @param $fstroke (int) outline size in user units (false = disable)
  5132. * @param $fclip (boolean) if true activate clipping mode (you must call StartTransform() before this function and StopTransform() to stop the clipping tranformation).
  5133. * @param $ffill (boolean) if true fills the text
  5134. * @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)))
  5135. * @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.
  5136. * @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>
  5137. * @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
  5138. * @param $link (mixed) URL or identifier returned by AddLink().
  5139. * @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.
  5140. * @param $ignore_min_height (boolean) if true ignore automatic minimum height value.
  5141. * @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>
  5142. * @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>
  5143. * @param $rtloff (boolean) if true uses the page top-left corner as origin of axis for $x and $y initial position.
  5144. * @public
  5145. * @since 1.0
  5146. * @see Cell(), Write(), MultiCell(), WriteHTML(), WriteHTMLCell()
  5147. */
  5148. 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) {
  5149. $textrendermode = $this->textrendermode;
  5150. $textstrokewidth = $this->textstrokewidth;
  5151. $this->setTextRenderingMode($fstroke, $ffill, $fclip);
  5152. $this->SetXY($x, $y, $rtloff);
  5153. $this->Cell(0, 0, $txt, $border, $ln, $align, $fill, $link, $stretch, $ignore_min_height, $calign, $valign);
  5154. // restore previous rendering mode
  5155. $this->textrendermode = $textrendermode;
  5156. $this->textstrokewidth = $textstrokewidth;
  5157. }
  5158. /**
  5159. * Whenever a page break condition is met, the method is called, and the break is issued or not depending on the returned value.
  5160. * The default implementation returns a value according to the mode selected by SetAutoPageBreak().<br />
  5161. * This method is called automatically and should not be called directly by the application.
  5162. * @return boolean
  5163. * @public
  5164. * @since 1.4
  5165. * @see SetAutoPageBreak()
  5166. */
  5167. public function AcceptPageBreak() {
  5168. if ($this->num_columns > 1) {
  5169. // multi column mode
  5170. if ($this->current_column < ($this->num_columns - 1)) {
  5171. // go to next column
  5172. $this->selectColumn($this->current_column + 1);
  5173. } else {
  5174. // add a new page
  5175. $this->AddPage();
  5176. // set first column
  5177. $this->selectColumn(0);
  5178. }
  5179. // avoid page breaking from checkPageBreak()
  5180. return false;
  5181. }
  5182. return $this->AutoPageBreak;
  5183. }
  5184. /**
  5185. * Add page if needed.
  5186. * @param $h (float) Cell height. Default value: 0.
  5187. * @param $y (mixed) starting y position, leave empty for current position.
  5188. * @param $addpage (boolean) if true add a page, otherwise only return the true/false state
  5189. * @return boolean true in case of page break, false otherwise.
  5190. * @since 3.2.000 (2008-07-01)
  5191. * @protected
  5192. */
  5193. protected function checkPageBreak($h=0, $y='', $addpage=true) {
  5194. if ($this->empty_string($y)) {
  5195. $y = $this->y;
  5196. }
  5197. $current_page = $this->page;
  5198. if ((($y + $h) > $this->PageBreakTrigger) AND ($this->inPageBody()) AND ($this->AcceptPageBreak())) {
  5199. if ($addpage) {
  5200. //Automatic page break
  5201. $x = $this->x;
  5202. $this->AddPage($this->CurOrientation);
  5203. $this->y = $this->tMargin;
  5204. $oldpage = $this->page - 1;
  5205. if ($this->rtl) {
  5206. if ($this->pagedim[$this->page]['orm'] != $this->pagedim[$oldpage]['orm']) {
  5207. $this->x = $x - ($this->pagedim[$this->page]['orm'] - $this->pagedim[$oldpage]['orm']);
  5208. } else {
  5209. $this->x = $x;
  5210. }
  5211. } else {
  5212. if ($this->pagedim[$this->page]['olm'] != $this->pagedim[$oldpage]['olm']) {
  5213. $this->x = $x + ($this->pagedim[$this->page]['olm'] - $this->pagedim[$oldpage]['olm']);
  5214. } else {
  5215. $this->x = $x;
  5216. }
  5217. }
  5218. }
  5219. return true;
  5220. }
  5221. if ($current_page != $this->page) {
  5222. // account for columns mode
  5223. return true;
  5224. }
  5225. return false;
  5226. }
  5227. /**
  5228. * Removes SHY characters from text.
  5229. * Unicode Data:<ul>
  5230. * <li>Name : SOFT HYPHEN, commonly abbreviated as SHY</li>
  5231. * <li>HTML Entity (decimal): "&amp;#173;"</li>
  5232. * <li>HTML Entity (hex): "&amp;#xad;"</li>
  5233. * <li>HTML Entity (named): "&amp;shy;"</li>
  5234. * <li>How to type in Microsoft Windows: [Alt +00AD] or [Alt 0173]</li>
  5235. * <li>UTF-8 (hex): 0xC2 0xAD (c2ad)</li>
  5236. * <li>UTF-8 character: chr(194).chr(173)</li>
  5237. * </ul>
  5238. * @param $txt (string) input string
  5239. * @return string without SHY characters.
  5240. * @public
  5241. * @since (4.5.019) 2009-02-28
  5242. */
  5243. public function removeSHY($txt='') {
  5244. $txt = preg_replace('/([\\xc2]{1}[\\xad]{1})/', '', $txt);
  5245. if (!$this->isunicode) {
  5246. $txt = preg_replace('/([\\xad]{1})/', '', $txt);
  5247. }
  5248. return $txt;
  5249. }
  5250. /**
  5251. * 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 />
  5252. * If automatic page breaking is enabled and the cell goes beyond the limit, a page break is done before outputting.
  5253. * @param $w (float) Cell width. If 0, the cell extends up to the right margin.
  5254. * @param $h (float) Cell height. Default value: 0.
  5255. * @param $txt (string) String to print. Default value: empty string.
  5256. * @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)))
  5257. * @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.
  5258. * @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>
  5259. * @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
  5260. * @param $link (mixed) URL or identifier returned by AddLink().
  5261. * @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.
  5262. * @param $ignore_min_height (boolean) if true ignore automatic minimum height value.
  5263. * @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>
  5264. * @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>
  5265. * @public
  5266. * @since 1.0
  5267. * @see SetFont(), SetDrawColor(), SetFillColor(), SetTextColor(), SetLineWidth(), AddLink(), Ln(), MultiCell(), Write(), SetAutoPageBreak()
  5268. */
  5269. 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') {
  5270. $prev_cell_margin = $this->cell_margin;
  5271. $prev_cell_padding = $this->cell_padding;
  5272. $this->adjustCellPadding($border);
  5273. if (!$ignore_min_height) {
  5274. $min_cell_height = ($this->FontSize * $this->cell_height_ratio) + $this->cell_padding['T'] + $this->cell_padding['B'];
  5275. if ($h < $min_cell_height) {
  5276. $h = $min_cell_height;
  5277. }
  5278. }
  5279. $this->checkPageBreak($h + $this->cell_margin['T'] + $this->cell_margin['B']);
  5280. $this->_out($this->getCellCode($w, $h, $txt, $border, $ln, $align, $fill, $link, $stretch, true, $calign, $valign));
  5281. $this->cell_padding = $prev_cell_padding;
  5282. $this->cell_margin = $prev_cell_margin;
  5283. }
  5284. /**
  5285. * 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 />
  5286. * If automatic page breaking is enabled and the cell goes beyond the limit, a page break is done before outputting.
  5287. * @param $w (float) Cell width. If 0, the cell extends up to the right margin.
  5288. * @param $h (float) Cell height. Default value: 0.
  5289. * @param $txt (string) String to print. Default value: empty string.
  5290. * @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)))
  5291. * @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.
  5292. * @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>
  5293. * @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
  5294. * @param $link (mixed) URL or identifier returned by AddLink().
  5295. * @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.
  5296. * @param $ignore_min_height (boolean) if true ignore automatic minimum height value.
  5297. * @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>
  5298. * @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>
  5299. * @return string containing cell code
  5300. * @protected
  5301. * @since 1.0
  5302. * @see Cell()
  5303. */
  5304. 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') {
  5305. // replace 'NO-BREAK SPACE' (U+00A0) character with a simple space
  5306. $txt = str_replace($this->unichr(160), ' ', $txt);
  5307. $prev_cell_margin = $this->cell_margin;
  5308. $prev_cell_padding = $this->cell_padding;
  5309. $txt = $this->removeSHY($txt);
  5310. $rs = ''; //string to be returned
  5311. $this->adjustCellPadding($border);
  5312. if (!$ignore_min_height) {
  5313. $min_cell_height = ($this->FontSize * $this->cell_height_ratio) + $this->cell_padding['T'] + $this->cell_padding['B'];
  5314. if ($h < $min_cell_height) {
  5315. $h = $min_cell_height;
  5316. }
  5317. }
  5318. $k = $this->k;
  5319. // check page for no-write regions and adapt page margins if necessary
  5320. list($this->x, $this->y) = $this->checkPageRegions($h, $this->x, $this->y);
  5321. if ($this->rtl) {
  5322. $x = $this->x - $this->cell_margin['R'];
  5323. } else {
  5324. $x = $this->x + $this->cell_margin['L'];
  5325. }
  5326. $y = $this->y + $this->cell_margin['T'];
  5327. $prev_font_stretching = $this->font_stretching;
  5328. $prev_font_spacing = $this->font_spacing;
  5329. // cell vertical alignment
  5330. switch ($calign) {
  5331. case 'A': {
  5332. // font top
  5333. switch ($valign) {
  5334. case 'T': {
  5335. // top
  5336. $y -= $this->cell_padding['T'];
  5337. break;
  5338. }
  5339. case 'B': {
  5340. // bottom
  5341. $y -= ($h - $this->cell_padding['B'] - $this->FontAscent - $this->FontDescent);
  5342. break;
  5343. }
  5344. default:
  5345. case 'C':
  5346. case 'M': {
  5347. // center
  5348. $y -= (($h - $this->FontAscent - $this->FontDescent) / 2);
  5349. break;
  5350. }
  5351. }
  5352. break;
  5353. }
  5354. case 'L': {
  5355. // font baseline
  5356. switch ($valign) {
  5357. case 'T': {
  5358. // top
  5359. $y -= ($this->cell_padding['T'] + $this->FontAscent);
  5360. break;
  5361. }
  5362. case 'B': {
  5363. // bottom
  5364. $y -= ($h - $this->cell_padding['B'] - $this->FontDescent);
  5365. break;
  5366. }
  5367. default:
  5368. case 'C':
  5369. case 'M': {
  5370. // center
  5371. $y -= (($h + $this->FontAscent - $this->FontDescent) / 2);
  5372. break;
  5373. }
  5374. }
  5375. break;
  5376. }
  5377. case 'D': {
  5378. // font bottom
  5379. switch ($valign) {
  5380. case 'T': {
  5381. // top
  5382. $y -= ($this->cell_padding['T'] + $this->FontAscent + $this->FontDescent);
  5383. break;
  5384. }
  5385. case 'B': {
  5386. // bottom
  5387. $y -= ($h - $this->cell_padding['B']);
  5388. break;
  5389. }
  5390. default:
  5391. case 'C':
  5392. case 'M': {
  5393. // center
  5394. $y -= (($h + $this->FontAscent + $this->FontDescent) / 2);
  5395. break;
  5396. }
  5397. }
  5398. break;
  5399. }
  5400. case 'B': {
  5401. // cell bottom
  5402. $y -= $h;
  5403. break;
  5404. }
  5405. case 'C':
  5406. case 'M': {
  5407. // cell center
  5408. $y -= ($h / 2);
  5409. break;
  5410. }
  5411. default:
  5412. case 'T': {
  5413. // cell top
  5414. break;
  5415. }
  5416. }
  5417. // text vertical alignment
  5418. switch ($valign) {
  5419. case 'T': {
  5420. // top
  5421. $yt = $y + $this->cell_padding['T'];
  5422. break;
  5423. }
  5424. case 'B': {
  5425. // bottom
  5426. $yt = $y + $h - $this->cell_padding['B'] - $this->FontAscent - $this->FontDescent;
  5427. break;
  5428. }
  5429. default:
  5430. case 'C':
  5431. case 'M': {
  5432. // center
  5433. $yt = $y + (($h - $this->FontAscent - $this->FontDescent) / 2);
  5434. break;
  5435. }
  5436. }
  5437. $basefonty = $yt + $this->FontAscent;
  5438. if ($this->empty_string($w) OR ($w <= 0)) {
  5439. if ($this->rtl) {
  5440. $w = $x - $this->lMargin;
  5441. } else {
  5442. $w = $this->w - $this->rMargin - $x;
  5443. }
  5444. }
  5445. $s = '';
  5446. // fill and borders
  5447. if (is_string($border) AND (strlen($border) == 4)) {
  5448. // full border
  5449. $border = 1;
  5450. }
  5451. if ($fill OR ($border == 1)) {
  5452. if ($fill) {
  5453. $op = ($border == 1) ? 'B' : 'f';
  5454. } else {
  5455. $op = 'S';
  5456. }
  5457. if ($this->rtl) {
  5458. $xk = (($x - $w) * $k);
  5459. } else {
  5460. $xk = ($x * $k);
  5461. }
  5462. $s .= sprintf('%.2F %.2F %.2F %.2F re %s ', $xk, (($this->h - $y) * $k), ($w * $k), (-$h * $k), $op);
  5463. }
  5464. // draw borders
  5465. $s .= $this->getCellBorder($x, $y, $w, $h, $border);
  5466. if ($txt != '') {
  5467. $txt2 = $txt;
  5468. if ($this->isunicode) {
  5469. if (($this->CurrentFont['type'] == 'core') OR ($this->CurrentFont['type'] == 'TrueType') OR ($this->CurrentFont['type'] == 'Type1')) {
  5470. $txt2 = $this->UTF8ToLatin1($txt2);
  5471. } else {
  5472. $unicode = $this->UTF8StringToArray($txt); // array of UTF-8 unicode values
  5473. $unicode = $this->utf8Bidi($unicode, '', $this->tmprtl);
  5474. if (defined('K_THAI_TOPCHARS') AND (K_THAI_TOPCHARS == true)) {
  5475. // ---- Fix for bug #2977340 "Incorrect Thai characters position arrangement" ----
  5476. // NOTE: this doesn't work with HTML justification
  5477. // Symbols that could overlap on the font top (only works in LTR)
  5478. $topchar = array(3611, 3613, 3615, 3650, 3651, 3652); // chars that extends on top
  5479. $topsym = array(3633, 3636, 3637, 3638, 3639, 3655, 3656, 3657, 3658, 3659, 3660, 3661, 3662); // symbols with top position
  5480. $numchars = count($unicode); // number of chars
  5481. $unik = 0;
  5482. $uniblock = array();
  5483. $uniblock[$unik] = array();
  5484. $uniblock[$unik][] = $unicode[0];
  5485. // resolve overlapping conflicts by splitting the string in several parts
  5486. for ($i = 1; $i < $numchars; ++$i) {
  5487. // check if symbols overlaps at top
  5488. if (in_array($unicode[$i], $topsym) AND (in_array($unicode[($i - 1)], $topsym) OR in_array($unicode[($i - 1)], $topchar))) {
  5489. // move symbols to another array
  5490. ++$unik;
  5491. $uniblock[$unik] = array();
  5492. $uniblock[$unik][] = $unicode[$i];
  5493. ++$unik;
  5494. $uniblock[$unik] = array();
  5495. $unicode[$i] = 0x200b; // Unicode Character 'ZERO WIDTH SPACE' (DEC:8203, U+200B)
  5496. } else {
  5497. $uniblock[$unik][] = $unicode[$i];
  5498. }
  5499. }
  5500. // ---- END OF Fix for bug #2977340
  5501. }
  5502. $txt2 = $this->arrUTF8ToUTF16BE($unicode, false);
  5503. }
  5504. }
  5505. $txt2 = $this->_escape($txt2);
  5506. // get current text width (considering general font stretching and spacing)
  5507. $txwidth = $this->GetStringWidth($txt);
  5508. $width = $txwidth;
  5509. // check for stretch mode
  5510. if ($stretch > 0) {
  5511. // calculate ratio between cell width and text width
  5512. if ($width <= 0) {
  5513. $ratio = 1;
  5514. } else {
  5515. $ratio = (($w - $this->cell_padding['L'] - $this->cell_padding['R']) / $width);
  5516. }
  5517. // check if stretching is required
  5518. if (($ratio < 1) OR (($ratio > 1) AND (($stretch % 2) == 0))) {
  5519. // the text will be stretched to fit cell width
  5520. if ($stretch > 2) {
  5521. // set new character spacing
  5522. $this->font_spacing += ($w - $this->cell_padding['L'] - $this->cell_padding['R'] - $width) / (max(($this->GetNumChars($txt) - 1), 1) * ($this->font_stretching / 100));
  5523. } else {
  5524. // set new horizontal stretching
  5525. $this->font_stretching *= $ratio;
  5526. }
  5527. // recalculate text width (the text fills the entire cell)
  5528. $width = $w - $this->cell_padding['L'] - $this->cell_padding['R'];
  5529. // reset alignment
  5530. $align = '';
  5531. }
  5532. }
  5533. if ($this->font_stretching != 100) {
  5534. // apply font stretching
  5535. $rs .= sprintf('BT %.2F Tz ET ', $this->font_stretching);
  5536. }
  5537. if ($this->font_spacing != 0) {
  5538. // increase/decrease font spacing
  5539. $rs .= sprintf('BT %.2F Tc ET ', ($this->font_spacing * $this->k));
  5540. }
  5541. if ($this->ColorFlag AND ($this->textrendermode < 4)) {
  5542. $s .= 'q '.$this->TextColor.' ';
  5543. }
  5544. // rendering mode
  5545. $s .= sprintf('BT %d Tr %.2F w ET ', $this->textrendermode, $this->textstrokewidth);
  5546. // count number of spaces
  5547. $ns = substr_count($txt, chr(32));
  5548. // Justification
  5549. $spacewidth = 0;
  5550. if (($align == 'J') AND ($ns > 0)) {
  5551. if ($this->isUnicodeFont()) {
  5552. // get string width without spaces
  5553. $width = $this->GetStringWidth(str_replace(' ', '', $txt));
  5554. // calculate average space width
  5555. $spacewidth = -1000 * ($w - $width - $this->cell_padding['L'] - $this->cell_padding['R']) / ($ns?$ns:1) / $this->FontSize;
  5556. if ($this->font_stretching != 100) {
  5557. // word spacing is affected by stretching
  5558. $spacewidth /= ($this->font_stretching / 100);
  5559. }
  5560. // set word position to be used with TJ operator
  5561. $txt2 = str_replace(chr(0).chr(32), ') '.sprintf('%.3F', $spacewidth).' (', $txt2);
  5562. $unicode_justification = true;
  5563. } else {
  5564. // get string width
  5565. $width = $txwidth;
  5566. // new space width
  5567. $spacewidth = (($w - $width - $this->cell_padding['L'] - $this->cell_padding['R']) / ($ns?$ns:1)) * $this->k;
  5568. if ($this->font_stretching != 100) {
  5569. // word spacing (Tw) is affected by stretching
  5570. $spacewidth /= ($this->font_stretching / 100);
  5571. }
  5572. // set word spacing
  5573. $rs .= sprintf('BT %.3F Tw ET ', $spacewidth);
  5574. }
  5575. $width = $w - $this->cell_padding['L'] - $this->cell_padding['R'];
  5576. }
  5577. // replace carriage return characters
  5578. $txt2 = str_replace("\r", ' ', $txt2);
  5579. switch ($align) {
  5580. case 'C': {
  5581. $dx = ($w - $width) / 2;
  5582. break;
  5583. }
  5584. case 'R': {
  5585. if ($this->rtl) {
  5586. $dx = $this->cell_padding['R'];
  5587. } else {
  5588. $dx = $w - $width - $this->cell_padding['R'];
  5589. }
  5590. break;
  5591. }
  5592. case 'L': {
  5593. if ($this->rtl) {
  5594. $dx = $w - $width - $this->cell_padding['L'];
  5595. } else {
  5596. $dx = $this->cell_padding['L'];
  5597. }
  5598. break;
  5599. }
  5600. case 'J':
  5601. default: {
  5602. if ($this->rtl) {
  5603. $dx = $this->cell_padding['R'];
  5604. } else {
  5605. $dx = $this->cell_padding['L'];
  5606. }
  5607. break;
  5608. }
  5609. }
  5610. if ($this->rtl) {
  5611. $xdx = $x - $dx - $width;
  5612. } else {
  5613. $xdx = $x + $dx;
  5614. }
  5615. $xdk = $xdx * $k;
  5616. // print text
  5617. $s .= sprintf('BT %.2F %.2F Td [(%s)] TJ ET', $xdk, (($this->h - $basefonty) * $k), $txt2);
  5618. if (isset($uniblock)) {
  5619. // print overlapping characters as separate string
  5620. $xshift = 0; // horizontal shift
  5621. $ty = (($this->h - $basefonty + (0.2 * $this->FontSize)) * $k);
  5622. $spw = (($w - $txwidth - $this->cell_padding['L'] - $this->cell_padding['R']) / ($ns?$ns:1));
  5623. foreach ($uniblock as $uk => $uniarr) {
  5624. if (($uk % 2) == 0) {
  5625. // x space to skip
  5626. if ($spacewidth != 0) {
  5627. // justification shift
  5628. $xshift += (count(array_keys($uniarr, 32)) * $spw);
  5629. }
  5630. $xshift += $this->GetArrStringWidth($uniarr); // + shift justification
  5631. } else {
  5632. // character to print
  5633. $topchr = $this->arrUTF8ToUTF16BE($uniarr, false);
  5634. $topchr = $this->_escape($topchr);
  5635. $s .= sprintf(' BT %.2F %.2F Td [(%s)] TJ ET', ($xdk + ($xshift * $k)), $ty, $topchr);
  5636. }
  5637. }
  5638. }
  5639. if ($this->underline) {
  5640. $s .= ' '.$this->_dounderlinew($xdx, $basefonty, $width);
  5641. }
  5642. if ($this->linethrough) {
  5643. $s .= ' '.$this->_dolinethroughw($xdx, $basefonty, $width);
  5644. }
  5645. if ($this->overline) {
  5646. $s .= ' '.$this->_dooverlinew($xdx, $basefonty, $width);
  5647. }
  5648. if ($this->ColorFlag AND ($this->textrendermode < 4)) {
  5649. $s .= ' Q';
  5650. }
  5651. if ($link) {
  5652. $this->Link($xdx, $yt, $width, ($this->FontAscent + $this->FontDescent), $link, $ns);
  5653. }
  5654. }
  5655. // output cell
  5656. if ($s) {
  5657. // output cell
  5658. $rs .= $s;
  5659. if ($this->font_spacing != 0) {
  5660. // reset font spacing mode
  5661. $rs .= ' BT 0 Tc ET';
  5662. }
  5663. if ($this->font_stretching != 100) {
  5664. // reset font stretching mode
  5665. $rs .= ' BT 100 Tz ET';
  5666. }
  5667. }
  5668. // reset word spacing
  5669. if (!$this->isUnicodeFont() AND ($align == 'J')) {
  5670. $rs .= ' BT 0 Tw ET';
  5671. }
  5672. // reset stretching and spacing
  5673. $this->font_stretching = $prev_font_stretching;
  5674. $this->font_spacing = $prev_font_spacing;
  5675. $this->lasth = $h;
  5676. if ($ln > 0) {
  5677. //Go to the beginning of the next line
  5678. $this->y = $y + $h + $this->cell_margin['B'];
  5679. if ($ln == 1) {
  5680. if ($this->rtl) {
  5681. $this->x = $this->w - $this->rMargin;
  5682. } else {
  5683. $this->x = $this->lMargin;
  5684. }
  5685. }
  5686. } else {
  5687. // go left or right by case
  5688. if ($this->rtl) {
  5689. $this->x = $x - $w - $this->cell_margin['L'];
  5690. } else {
  5691. $this->x = $x + $w + $this->cell_margin['R'];
  5692. }
  5693. }
  5694. $gstyles = ''.$this->linestyleWidth.' '.$this->linestyleCap.' '.$this->linestyleJoin.' '.$this->linestyleDash.' '.$this->DrawColor.' '.$this->FillColor."\n";
  5695. $rs = $gstyles.$rs;
  5696. $this->cell_padding = $prev_cell_padding;
  5697. $this->cell_margin = $prev_cell_margin;
  5698. return $rs;
  5699. }
  5700. /**
  5701. * Returns the code to draw the cell border
  5702. * @param $x (float) X coordinate.
  5703. * @param $y (float) Y coordinate.
  5704. * @param $w (float) Cell width.
  5705. * @param $h (float) Cell height.
  5706. * @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)))
  5707. * @return string containing cell border code
  5708. * @protected
  5709. * @see SetLineStyle()
  5710. * @since 5.7.000 (2010-08-02)
  5711. */
  5712. protected function getCellBorder($x, $y, $w, $h, $brd) {
  5713. $s = ''; // string to be returned
  5714. if (empty($brd)) {
  5715. return $s;
  5716. }
  5717. if ($brd == 1) {
  5718. $brd = array('LRTB' => true);
  5719. }
  5720. // calculate coordinates for border
  5721. $k = $this->k;
  5722. if ($this->rtl) {
  5723. $xeL = ($x - $w) * $k;
  5724. $xeR = $x * $k;
  5725. } else {
  5726. $xeL = $x * $k;
  5727. $xeR = ($x + $w) * $k;
  5728. }
  5729. $yeL = (($this->h - ($y + $h)) * $k);
  5730. $yeT = (($this->h - $y) * $k);
  5731. $xeT = $xeL;
  5732. $xeB = $xeR;
  5733. $yeR = $yeT;
  5734. $yeB = $yeL;
  5735. if (is_string($brd)) {
  5736. // convert string to array
  5737. $slen = strlen($brd);
  5738. $newbrd = array();
  5739. for ($i = 0; $i < $slen; ++$i) {
  5740. $newbrd[$brd[$i]] = array('cap' => 'square', 'join' => 'miter');
  5741. }
  5742. $brd = $newbrd;
  5743. }
  5744. if (isset($brd['mode'])) {
  5745. $mode = $brd['mode'];
  5746. unset($brd['mode']);
  5747. } else {
  5748. $mode = 'normal';
  5749. }
  5750. foreach ($brd as $border => $style) {
  5751. if (is_array($style) AND !empty($style)) {
  5752. // apply border style
  5753. $prev_style = $this->linestyleWidth.' '.$this->linestyleCap.' '.$this->linestyleJoin.' '.$this->linestyleDash.' '.$this->DrawColor.' ';
  5754. $s .= $this->SetLineStyle($style, true)."\n";
  5755. }
  5756. switch ($mode) {
  5757. case 'ext': {
  5758. $off = (($this->LineWidth / 2) * $k);
  5759. $xL = $xeL - $off;
  5760. $xR = $xeR + $off;
  5761. $yT = $yeT + $off;
  5762. $yL = $yeL - $off;
  5763. $xT = $xL;
  5764. $xB = $xR;
  5765. $yR = $yT;
  5766. $yB = $yL;
  5767. $w += $this->LineWidth;
  5768. $h += $this->LineWidth;
  5769. break;
  5770. }
  5771. case 'int': {
  5772. $off = ($this->LineWidth / 2) * $k;
  5773. $xL = $xeL + $off;
  5774. $xR = $xeR - $off;
  5775. $yT = $yeT - $off;
  5776. $yL = $yeL + $off;
  5777. $xT = $xL;
  5778. $xB = $xR;
  5779. $yR = $yT;
  5780. $yB = $yL;
  5781. $w -= $this->LineWidth;
  5782. $h -= $this->LineWidth;
  5783. break;
  5784. }
  5785. case 'normal':
  5786. default: {
  5787. $xL = $xeL;
  5788. $xT = $xeT;
  5789. $xB = $xeB;
  5790. $xR = $xeR;
  5791. $yL = $yeL;
  5792. $yT = $yeT;
  5793. $yB = $yeB;
  5794. $yR = $yeR;
  5795. break;
  5796. }
  5797. }
  5798. // draw borders by case
  5799. if (strlen($border) == 4) {
  5800. $s .= sprintf('%.2F %.2F %.2F %.2F re S ', $xT, $yT, ($w * $k), (-$h * $k));
  5801. } elseif (strlen($border) == 3) {
  5802. if (strpos($border,'B') === false) { // LTR
  5803. $s .= sprintf('%.2F %.2F m ', $xL, $yL);
  5804. $s .= sprintf('%.2F %.2F l ', $xT, $yT);
  5805. $s .= sprintf('%.2F %.2F l ', $xR, $yR);
  5806. $s .= sprintf('%.2F %.2F l ', $xB, $yB);
  5807. $s .= 'S ';
  5808. } elseif (strpos($border,'L') === false) { // TRB
  5809. $s .= sprintf('%.2F %.2F m ', $xT, $yT);
  5810. $s .= sprintf('%.2F %.2F l ', $xR, $yR);
  5811. $s .= sprintf('%.2F %.2F l ', $xB, $yB);
  5812. $s .= sprintf('%.2F %.2F l ', $xL, $yL);
  5813. $s .= 'S ';
  5814. } elseif (strpos($border,'T') === false) { // RBL
  5815. $s .= sprintf('%.2F %.2F m ', $xR, $yR);
  5816. $s .= sprintf('%.2F %.2F l ', $xB, $yB);
  5817. $s .= sprintf('%.2F %.2F l ', $xL, $yL);
  5818. $s .= sprintf('%.2F %.2F l ', $xT, $yT);
  5819. $s .= 'S ';
  5820. } elseif (strpos($border,'R') === false) { // BLT
  5821. $s .= sprintf('%.2F %.2F m ', $xB, $yB);
  5822. $s .= sprintf('%.2F %.2F l ', $xL, $yL);
  5823. $s .= sprintf('%.2F %.2F l ', $xT, $yT);
  5824. $s .= sprintf('%.2F %.2F l ', $xR, $yR);
  5825. $s .= 'S ';
  5826. }
  5827. } elseif (strlen($border) == 2) {
  5828. if ((strpos($border,'L') !== false) AND (strpos($border,'T') !== false)) { // LT
  5829. $s .= sprintf('%.2F %.2F m ', $xL, $yL);
  5830. $s .= sprintf('%.2F %.2F l ', $xT, $yT);
  5831. $s .= sprintf('%.2F %.2F l ', $xR, $yR);
  5832. $s .= 'S ';
  5833. } elseif ((strpos($border,'T') !== false) AND (strpos($border,'R') !== false)) { // TR
  5834. $s .= sprintf('%.2F %.2F m ', $xT, $yT);
  5835. $s .= sprintf('%.2F %.2F l ', $xR, $yR);
  5836. $s .= sprintf('%.2F %.2F l ', $xB, $yB);
  5837. $s .= 'S ';
  5838. } elseif ((strpos($border,'R') !== false) AND (strpos($border,'B') !== false)) { // RB
  5839. $s .= sprintf('%.2F %.2F m ', $xR, $yR);
  5840. $s .= sprintf('%.2F %.2F l ', $xB, $yB);
  5841. $s .= sprintf('%.2F %.2F l ', $xL, $yL);
  5842. $s .= 'S ';
  5843. } elseif ((strpos($border,'B') !== false) AND (strpos($border,'L') !== false)) { // BL
  5844. $s .= sprintf('%.2F %.2F m ', $xB, $yB);
  5845. $s .= sprintf('%.2F %.2F l ', $xL, $yL);
  5846. $s .= sprintf('%.2F %.2F l ', $xT, $yT);
  5847. $s .= 'S ';
  5848. } elseif ((strpos($border,'L') !== false) AND (strpos($border,'R') !== false)) { // LR
  5849. $s .= sprintf('%.2F %.2F m ', $xL, $yL);
  5850. $s .= sprintf('%.2F %.2F l ', $xT, $yT);
  5851. $s .= 'S ';
  5852. $s .= sprintf('%.2F %.2F m ', $xR, $yR);
  5853. $s .= sprintf('%.2F %.2F l ', $xB, $yB);
  5854. $s .= 'S ';
  5855. } elseif ((strpos($border,'T') !== false) AND (strpos($border,'B') !== false)) { // TB
  5856. $s .= sprintf('%.2F %.2F m ', $xT, $yT);
  5857. $s .= sprintf('%.2F %.2F l ', $xR, $yR);
  5858. $s .= 'S ';
  5859. $s .= sprintf('%.2F %.2F m ', $xB, $yB);
  5860. $s .= sprintf('%.2F %.2F l ', $xL, $yL);
  5861. $s .= 'S ';
  5862. }
  5863. } else { // strlen($border) == 1
  5864. if (strpos($border,'L') !== false) { // L
  5865. $s .= sprintf('%.2F %.2F m ', $xL, $yL);
  5866. $s .= sprintf('%.2F %.2F l ', $xT, $yT);
  5867. $s .= 'S ';
  5868. } elseif (strpos($border,'T') !== false) { // T
  5869. $s .= sprintf('%.2F %.2F m ', $xT, $yT);
  5870. $s .= sprintf('%.2F %.2F l ', $xR, $yR);
  5871. $s .= 'S ';
  5872. } elseif (strpos($border,'R') !== false) { // R
  5873. $s .= sprintf('%.2F %.2F m ', $xR, $yR);
  5874. $s .= sprintf('%.2F %.2F l ', $xB, $yB);
  5875. $s .= 'S ';
  5876. } elseif (strpos($border,'B') !== false) { // B
  5877. $s .= sprintf('%.2F %.2F m ', $xB, $yB);
  5878. $s .= sprintf('%.2F %.2F l ', $xL, $yL);
  5879. $s .= 'S ';
  5880. }
  5881. }
  5882. if (is_array($style) AND !empty($style)) {
  5883. // reset border style to previous value
  5884. $s .= "\n".$this->linestyleWidth.' '.$this->linestyleCap.' '.$this->linestyleJoin.' '.$this->linestyleDash.' '.$this->DrawColor."\n";
  5885. }
  5886. }
  5887. return $s;
  5888. }
  5889. /**
  5890. * This method allows printing text with line breaks.
  5891. * 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 />
  5892. * Text can be aligned, centered or justified. The cell block can be framed and the background painted.
  5893. * @param $w (float) Width of cells. If 0, they extend up to the right margin of the page.
  5894. * @param $h (float) Cell minimum height. The cell extends automatically if needed.
  5895. * @param $txt (string) String to print
  5896. * @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)))
  5897. * @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>
  5898. * @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
  5899. * @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>
  5900. * @param $x (float) x position in user units
  5901. * @param $y (float) y position in user units
  5902. * @param $reseth (boolean) if true reset the last cell height (default true).
  5903. * @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.
  5904. * @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.
  5905. * @param $autopadding (boolean) if true, uses internal padding and automatically adjust it to account for line width.
  5906. * @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.
  5907. * @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.
  5908. * @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).
  5909. * @return int Return the number of cells or 1 for html mode.
  5910. * @public
  5911. * @since 1.3
  5912. * @see SetFont(), SetDrawColor(), SetFillColor(), SetTextColor(), SetLineWidth(), Cell(), Write(), SetAutoPageBreak()
  5913. */
  5914. 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) {
  5915. $prev_cell_margin = $this->cell_margin;
  5916. $prev_cell_padding = $this->cell_padding;
  5917. // adjust internal padding
  5918. $this->adjustCellPadding($border);
  5919. $mc_padding = $this->cell_padding;
  5920. $mc_margin = $this->cell_margin;
  5921. $this->cell_padding['T'] = 0;
  5922. $this->cell_padding['B'] = 0;
  5923. $this->setCellMargins(0, 0, 0, 0);
  5924. if ($this->empty_string($this->lasth) OR $reseth) {
  5925. // reset row height
  5926. $this->resetLastH();
  5927. }
  5928. if (!$this->empty_string($y)) {
  5929. $this->SetY($y);
  5930. } else {
  5931. $y = $this->GetY();
  5932. }
  5933. $resth = 0;
  5934. if (($h > 0) AND $this->inPageBody() AND (($y + $h + $mc_margin['T'] + $mc_margin['B']) > $this->PageBreakTrigger)) {
  5935. // spit cell in more pages/columns
  5936. $newh = ($this->PageBreakTrigger - $y);
  5937. $resth = ($h - $newh); // cell to be printed on the next page/column
  5938. $h = $newh;
  5939. }
  5940. // get current page number
  5941. $startpage = $this->page;
  5942. // get current column
  5943. $startcolumn = $this->current_column;
  5944. if (!$this->empty_string($x)) {
  5945. $this->SetX($x);
  5946. } else {
  5947. $x = $this->GetX();
  5948. }
  5949. // check page for no-write regions and adapt page margins if necessary
  5950. list($x, $y) = $this->checkPageRegions(0, $x, $y);
  5951. // apply margins
  5952. $oy = $y + $mc_margin['T'];
  5953. if ($this->rtl) {
  5954. $ox = $this->w - $x - $mc_margin['R'];
  5955. } else {
  5956. $ox = $x + $mc_margin['L'];
  5957. }
  5958. $this->x = $ox;
  5959. $this->y = $oy;
  5960. // set width
  5961. if ($this->empty_string($w) OR ($w <= 0)) {
  5962. if ($this->rtl) {
  5963. $w = $this->x - $this->lMargin - $mc_margin['L'];
  5964. } else {
  5965. $w = $this->w - $this->x - $this->rMargin - $mc_margin['R'];
  5966. }
  5967. }
  5968. // store original margin values
  5969. $lMargin = $this->lMargin;
  5970. $rMargin = $this->rMargin;
  5971. if ($this->rtl) {
  5972. $this->rMargin = $this->w - $this->x;
  5973. $this->lMargin = $this->x - $w;
  5974. } else {
  5975. $this->lMargin = $this->x;
  5976. $this->rMargin = $this->w - $this->x - $w;
  5977. }
  5978. if ($autopadding) {
  5979. // add top padding
  5980. $this->y += $mc_padding['T'];
  5981. }
  5982. if ($ishtml) { // ******* Write HTML text
  5983. $this->writeHTML($txt, true, false, $reseth, true, $align);
  5984. $nl = 1;
  5985. } else { // ******* Write simple text
  5986. $prev_FontSizePt = $this->FontSizePt;
  5987. // vertical alignment
  5988. if ($maxh > 0) {
  5989. // get text height
  5990. $text_height = $this->getStringHeight($w, $txt, $reseth, $autopadding, $mc_padding, $border);
  5991. if ($fitcell) {
  5992. // try to reduce font size to fit text on cell (use a quick search algorithm)
  5993. $fmin = 1;
  5994. $fmax = $this->FontSizePt;
  5995. $prev_text_height = $text_height;
  5996. $maxit = 100; // max number of iterations
  5997. while ($maxit > 0) {
  5998. $fmid = (($fmax + $fmin) / 2);
  5999. $this->SetFontSize($fmid, false);
  6000. $this->resetLastH();
  6001. $text_height = $this->getStringHeight($w, $txt, $reseth, $autopadding, $mc_padding, $border);
  6002. if (($text_height == $maxh) OR (($text_height < $maxh) AND ($fmin >= ($fmax - 0.01)))) {
  6003. break;
  6004. } elseif ($text_height < $maxh) {
  6005. $fmin = $fmid;
  6006. } else {
  6007. $fmax = $fmid;
  6008. }
  6009. --$maxit;
  6010. }
  6011. $this->SetFontSize($this->FontSizePt);
  6012. }
  6013. if ($text_height < $maxh) {
  6014. if ($valign == 'M') {
  6015. // text vertically centered
  6016. $this->y += (($maxh - $text_height) / 2);
  6017. } elseif ($valign == 'B') {
  6018. // text vertically aligned on bottom
  6019. $this->y += ($maxh - $text_height);
  6020. }
  6021. }
  6022. }
  6023. $nl = $this->Write($this->lasth, $txt, '', 0, $align, true, $stretch, false, true, $maxh, 0, $mc_margin);
  6024. if ($fitcell) {
  6025. // restore font size
  6026. $this->SetFontSize($prev_FontSizePt);
  6027. }
  6028. }
  6029. if ($autopadding) {
  6030. // add bottom padding
  6031. $this->y += $mc_padding['B'];
  6032. }
  6033. // Get end-of-text Y position
  6034. $currentY = $this->y;
  6035. // get latest page number
  6036. $endpage = $this->page;
  6037. if ($resth > 0) {
  6038. $skip = ($endpage - $startpage);
  6039. $tmpresth = $resth;
  6040. while ($tmpresth > 0) {
  6041. if ($skip <= 0) {
  6042. // add a page (or trig AcceptPageBreak() for multicolumn mode)
  6043. $this->checkPageBreak($this->PageBreakTrigger + 1);
  6044. }
  6045. if ($this->num_columns > 1) {
  6046. $tmpresth -= ($this->h - $this->y - $this->bMargin);
  6047. } else {
  6048. $tmpresth -= ($this->h - $this->tMargin - $this->bMargin);
  6049. }
  6050. --$skip;
  6051. }
  6052. $currentY = $this->y;
  6053. $endpage = $this->page;
  6054. }
  6055. // get latest column
  6056. $endcolumn = $this->current_column;
  6057. if ($this->num_columns == 0) {
  6058. $this->num_columns = 1;
  6059. }
  6060. // get border modes
  6061. $border_start = $this->getBorderMode($border, $position='start');
  6062. $border_end = $this->getBorderMode($border, $position='end');
  6063. $border_middle = $this->getBorderMode($border, $position='middle');
  6064. // design borders around HTML cells.
  6065. for ($page = $startpage; $page <= $endpage; ++$page) { // for each page
  6066. $ccode = '';
  6067. $this->setPage($page);
  6068. if ($this->num_columns < 2) {
  6069. // single-column mode
  6070. $this->SetX($x);
  6071. $this->y = $this->tMargin;
  6072. }
  6073. // account for margin changes
  6074. if ($page > $startpage) {
  6075. if (($this->rtl) AND ($this->pagedim[$page]['orm'] != $this->pagedim[$startpage]['orm'])) {
  6076. $this->x -= ($this->pagedim[$page]['orm'] - $this->pagedim[$startpage]['orm']);
  6077. } elseif ((!$this->rtl) AND ($this->pagedim[$page]['olm'] != $this->pagedim[$startpage]['olm'])) {
  6078. $this->x += ($this->pagedim[$page]['olm'] - $this->pagedim[$startpage]['olm']);
  6079. }
  6080. }
  6081. if ($startpage == $endpage) {
  6082. // single page
  6083. for ($column = $startcolumn; $column <= $endcolumn; ++$column) { // for each column
  6084. $this->selectColumn($column);
  6085. if ($this->rtl) {
  6086. $this->x -= $mc_margin['R'];
  6087. } else {
  6088. $this->x += $mc_margin['L'];
  6089. }
  6090. if ($startcolumn == $endcolumn) { // single column
  6091. $cborder = $border;
  6092. $h = max($h, ($currentY - $oy));
  6093. $this->y = $oy;
  6094. } elseif ($column == $startcolumn) { // first column
  6095. $cborder = $border_start;
  6096. $this->y = $oy;
  6097. $h = $this->h - $this->y - $this->bMargin;
  6098. } elseif ($column == $endcolumn) { // end column
  6099. $cborder = $border_end;
  6100. $h = $currentY - $this->y;
  6101. if ($resth > $h) {
  6102. $h = $resth;
  6103. }
  6104. } else { // middle column
  6105. $cborder = $border_middle;
  6106. $h = $this->h - $this->y - $this->bMargin;
  6107. $resth -= $h;
  6108. }
  6109. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
  6110. } // end for each column
  6111. } elseif ($page == $startpage) { // first page
  6112. for ($column = $startcolumn; $column < $this->num_columns; ++$column) { // for each column
  6113. $this->selectColumn($column);
  6114. if ($this->rtl) {
  6115. $this->x -= $mc_margin['R'];
  6116. } else {
  6117. $this->x += $mc_margin['L'];
  6118. }
  6119. if ($column == $startcolumn) { // first column
  6120. $cborder = $border_start;
  6121. $this->y = $oy;
  6122. $h = $this->h - $this->y - $this->bMargin;
  6123. } else { // middle column
  6124. $cborder = $border_middle;
  6125. $h = $this->h - $this->y - $this->bMargin;
  6126. $resth -= $h;
  6127. }
  6128. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
  6129. } // end for each column
  6130. } elseif ($page == $endpage) { // last page
  6131. for ($column = 0; $column <= $endcolumn; ++$column) { // for each column
  6132. $this->selectColumn($column);
  6133. if ($this->rtl) {
  6134. $this->x -= $mc_margin['R'];
  6135. } else {
  6136. $this->x += $mc_margin['L'];
  6137. }
  6138. if ($column == $endcolumn) {
  6139. // end column
  6140. $cborder = $border_end;
  6141. $h = $currentY - $this->y;
  6142. if ($resth > $h) {
  6143. $h = $resth;
  6144. }
  6145. } else {
  6146. // middle column
  6147. $cborder = $border_middle;
  6148. $h = $this->h - $this->y - $this->bMargin;
  6149. $resth -= $h;
  6150. }
  6151. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
  6152. } // end for each column
  6153. } else { // middle page
  6154. for ($column = 0; $column < $this->num_columns; ++$column) { // for each column
  6155. $this->selectColumn($column);
  6156. if ($this->rtl) {
  6157. $this->x -= $mc_margin['R'];
  6158. } else {
  6159. $this->x += $mc_margin['L'];
  6160. }
  6161. $cborder = $border_middle;
  6162. $h = $this->h - $this->y - $this->bMargin;
  6163. $resth -= $h;
  6164. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
  6165. } // end for each column
  6166. }
  6167. if ($cborder OR $fill) {
  6168. $offsetlen = strlen($ccode);
  6169. // draw border and fill
  6170. if ($this->inxobj) {
  6171. // we are inside an XObject template
  6172. if (end($this->xobjects[$this->xobjid]['transfmrk']) !== false) {
  6173. $pagemarkkey = key($this->xobjects[$this->xobjid]['transfmrk']);
  6174. $pagemark = $this->xobjects[$this->xobjid]['transfmrk'][$pagemarkkey];
  6175. $this->xobjects[$this->xobjid]['transfmrk'][$pagemarkkey] += $offsetlen;
  6176. } else {
  6177. $pagemark = $this->xobjects[$this->xobjid]['intmrk'];
  6178. $this->xobjects[$this->xobjid]['intmrk'] += $offsetlen;
  6179. }
  6180. $pagebuff = $this->xobjects[$this->xobjid]['outdata'];
  6181. $pstart = substr($pagebuff, 0, $pagemark);
  6182. $pend = substr($pagebuff, $pagemark);
  6183. $this->xobjects[$this->xobjid]['outdata'] = $pstart.$ccode.$pend;
  6184. } else {
  6185. if (end($this->transfmrk[$this->page]) !== false) {
  6186. $pagemarkkey = key($this->transfmrk[$this->page]);
  6187. $pagemark = $this->transfmrk[$this->page][$pagemarkkey];
  6188. $this->transfmrk[$this->page][$pagemarkkey] += $offsetlen;
  6189. } elseif ($this->InFooter) {
  6190. $pagemark = $this->footerpos[$this->page];
  6191. $this->footerpos[$this->page] += $offsetlen;
  6192. } else {
  6193. $pagemark = $this->intmrk[$this->page];
  6194. $this->intmrk[$this->page] += $offsetlen;
  6195. }
  6196. $pagebuff = $this->getPageBuffer($this->page);
  6197. $pstart = substr($pagebuff, 0, $pagemark);
  6198. $pend = substr($pagebuff, $pagemark);
  6199. $this->setPageBuffer($this->page, $pstart.$ccode.$pend);
  6200. }
  6201. }
  6202. } // end for each page
  6203. // Get end-of-cell Y position
  6204. $currentY = $this->GetY();
  6205. // restore previous values
  6206. if ($this->num_columns > 1) {
  6207. $this->selectColumn();
  6208. } else {
  6209. // restore original margins
  6210. $this->lMargin = $lMargin;
  6211. $this->rMargin = $rMargin;
  6212. if ($this->page > $startpage) {
  6213. // check for margin variations between pages (i.e. booklet mode)
  6214. $dl = ($this->pagedim[$this->page]['olm'] - $this->pagedim[$startpage]['olm']);
  6215. $dr = ($this->pagedim[$this->page]['orm'] - $this->pagedim[$startpage]['orm']);
  6216. if (($dl != 0) OR ($dr != 0)) {
  6217. $this->lMargin += $dl;
  6218. $this->rMargin += $dr;
  6219. }
  6220. }
  6221. }
  6222. if ($ln > 0) {
  6223. //Go to the beginning of the next line
  6224. $this->SetY($currentY + $mc_margin['B']);
  6225. if ($ln == 2) {
  6226. $this->SetX($x + $w + $mc_margin['L'] + $mc_margin['R']);
  6227. }
  6228. } else {
  6229. // go left or right by case
  6230. $this->setPage($startpage);
  6231. $this->y = $y;
  6232. $this->SetX($x + $w + $mc_margin['L'] + $mc_margin['R']);
  6233. }
  6234. $this->setContentMark();
  6235. $this->cell_padding = $prev_cell_padding;
  6236. $this->cell_margin = $prev_cell_margin;
  6237. return $nl;
  6238. }
  6239. /**
  6240. * Get the border mode accounting for multicell position (opens bottom side of multicell crossing pages)
  6241. * @param $brd (mixed) Indicates if borders must be drawn around the cell block. 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: array('LTRB' => array('width' => 2, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)))
  6242. * @param $position (string) multicell position: 'start', 'middle', 'end'
  6243. * @return border mode array
  6244. * @protected
  6245. * @since 4.4.002 (2008-12-09)
  6246. */
  6247. protected function getBorderMode($brd, $position='start') {
  6248. if ((!$this->opencell) OR empty($brd)) {
  6249. return $brd;
  6250. }
  6251. if ($brd == 1) {
  6252. $brd = 'LTRB';
  6253. }
  6254. if (is_string($brd)) {
  6255. // convert string to array
  6256. $slen = strlen($brd);
  6257. $newbrd = array();
  6258. for ($i = 0; $i < $slen; ++$i) {
  6259. $newbrd[$brd[$i]] = array('cap' => 'square', 'join' => 'miter');
  6260. }
  6261. $brd = $newbrd;
  6262. }
  6263. foreach ($brd as $border => $style) {
  6264. switch ($position) {
  6265. case 'start': {
  6266. if (strpos($border, 'B') !== false) {
  6267. // remove bottom line
  6268. $newkey = str_replace('B', '', $border);
  6269. if (strlen($newkey) > 0) {
  6270. $brd[$newkey] = $style;
  6271. }
  6272. unset($brd[$border]);
  6273. }
  6274. break;
  6275. }
  6276. case 'middle': {
  6277. if (strpos($border, 'B') !== false) {
  6278. // remove bottom line
  6279. $newkey = str_replace('B', '', $border);
  6280. if (strlen($newkey) > 0) {
  6281. $brd[$newkey] = $style;
  6282. }
  6283. unset($brd[$border]);
  6284. $border = $newkey;
  6285. }
  6286. if (strpos($border, 'T') !== false) {
  6287. // remove bottom line
  6288. $newkey = str_replace('T', '', $border);
  6289. if (strlen($newkey) > 0) {
  6290. $brd[$newkey] = $style;
  6291. }
  6292. unset($brd[$border]);
  6293. }
  6294. break;
  6295. }
  6296. case 'end': {
  6297. if (strpos($border, 'T') !== false) {
  6298. // remove bottom line
  6299. $newkey = str_replace('T', '', $border);
  6300. if (strlen($newkey) > 0) {
  6301. $brd[$newkey] = $style;
  6302. }
  6303. unset($brd[$border]);
  6304. }
  6305. break;
  6306. }
  6307. }
  6308. }
  6309. return $brd;
  6310. }
  6311. /**
  6312. * This method return the estimated number of lines for print a simple text string using Multicell() method.
  6313. * @param $txt (string) String for calculating his height
  6314. * @param $w (float) Width of cells. If 0, they extend up to the right margin of the page.
  6315. * @param $reseth (boolean) if true reset the last cell height (default false).
  6316. * @param $autopadding (boolean) if true, uses internal padding and automatically adjust it to account for line width (default true).
  6317. * @param $cellpadding (float) Internal cell padding, if empty uses default cell padding.
  6318. * @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)))
  6319. * @return float Return the minimal height needed for multicell method for printing the $txt param.
  6320. * @author Alexander Escalona Fernández, Nicola Asuni
  6321. * @public
  6322. * @since 4.5.011
  6323. */
  6324. public function getNumLines($txt, $w=0, $reseth=false, $autopadding=true, $cellpadding='', $border=0) {
  6325. if ($txt === '') {
  6326. // empty string
  6327. return 1;
  6328. }
  6329. // adjust internal padding
  6330. $prev_cell_padding = $this->cell_padding;
  6331. $prev_lasth = $this->lasth;
  6332. if (is_array($cellpadding)) {
  6333. $this->cell_padding = $cellpadding;
  6334. }
  6335. $this->adjustCellPadding($border);
  6336. if ($this->empty_string($w) OR ($w <= 0)) {
  6337. if ($this->rtl) {
  6338. $w = $this->x - $this->lMargin;
  6339. } else {
  6340. $w = $this->w - $this->rMargin - $this->x;
  6341. }
  6342. }
  6343. $wmax = $w - $this->cell_padding['L'] - $this->cell_padding['R'];
  6344. if ($reseth) {
  6345. // reset row height
  6346. $this->resetLastH();
  6347. }
  6348. $lines = 1;
  6349. $sum = 0;
  6350. $chars = $this->utf8Bidi($this->UTF8StringToArray($txt), $txt, $this->tmprtl);
  6351. $charsWidth = $this->GetArrStringWidth($chars, '', '', 0, true);
  6352. $length = count($chars);
  6353. $lastSeparator = -1;
  6354. for ($i = 0; $i < $length; ++$i) {
  6355. $charWidth = $charsWidth[$i];
  6356. if (preg_match($this->re_spaces, $this->unichr($chars[$i]))) {
  6357. $lastSeparator = $i;
  6358. }
  6359. if ((($sum + $charWidth) > $wmax) OR ($chars[$i] == 10)) {
  6360. ++$lines;
  6361. if ($chars[$i] == 10) {
  6362. $lastSeparator = -1;
  6363. $sum = 0;
  6364. } elseif ($lastSeparator != -1) {
  6365. $i = $lastSeparator;
  6366. $lastSeparator = -1;
  6367. $sum = 0;
  6368. } else {
  6369. $sum = $charWidth;
  6370. }
  6371. } else {
  6372. $sum += $charWidth;
  6373. }
  6374. }
  6375. if ($chars[($length - 1)] == 10) {
  6376. --$lines;
  6377. }
  6378. $this->cell_padding = $prev_cell_padding;
  6379. $this->lasth = $prev_lasth;
  6380. return $lines;
  6381. }
  6382. /**
  6383. * This method return the estimated height needed for printing a simple text string using the Multicell() method.
  6384. * Generally, if you want to know the exact height for a block of content you can use the following alternative technique:
  6385. * @pre
  6386. * // store current object
  6387. * $pdf->startTransaction();
  6388. * // store starting values
  6389. * $start_y = $pdf->GetY();
  6390. * $start_page = $pdf->getPage();
  6391. * // call your printing functions with your parameters
  6392. * // - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  6393. * $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);
  6394. * // - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  6395. * // get the new Y
  6396. * $end_y = $pdf->GetY();
  6397. * $end_page = $pdf->getPage();
  6398. * // calculate height
  6399. * $height = 0;
  6400. * if ($end_page == $start_page) {
  6401. * $height = $end_y - $start_y;
  6402. * } else {
  6403. * for ($page=$start_page; $page <= $end_page; ++$page) {
  6404. * $this->setPage($page);
  6405. * if ($page == $start_page) {
  6406. * // first page
  6407. * $height = $this->h - $start_y - $this->bMargin;
  6408. * } elseif ($page == $end_page) {
  6409. * // last page
  6410. * $height = $end_y - $this->tMargin;
  6411. * } else {
  6412. * $height = $this->h - $this->tMargin - $this->bMargin;
  6413. * }
  6414. * }
  6415. * }
  6416. * // restore previous object
  6417. * $pdf = $pdf->rollbackTransaction();
  6418. *
  6419. * @param $w (float) Width of cells. If 0, they extend up to the right margin of the page.
  6420. * @param $txt (string) String for calculating his height
  6421. * @param $reseth (boolean) if true reset the last cell height (default false).
  6422. * @param $autopadding (boolean) if true, uses internal padding and automatically adjust it to account for line width (default true).
  6423. * @param $cellpadding (float) Internal cell padding, if empty uses default cell padding.
  6424. * @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)))
  6425. * @return float Return the minimal height needed for multicell method for printing the $txt param.
  6426. * @author Nicola Asuni, Alexander Escalona Fernández
  6427. * @public
  6428. */
  6429. public function getStringHeight($w, $txt, $reseth=false, $autopadding=true, $cellpadding='', $border=0) {
  6430. // adjust internal padding
  6431. $prev_cell_padding = $this->cell_padding;
  6432. $prev_lasth = $this->lasth;
  6433. if (is_array($cellpadding)) {
  6434. $this->cell_padding = $cellpadding;
  6435. }
  6436. $this->adjustCellPadding($border);
  6437. $lines = $this->getNumLines($txt, $w, $reseth, $autopadding, $cellpadding, $border);
  6438. $height = $lines * ($this->FontSize * $this->cell_height_ratio);
  6439. if ($autopadding) {
  6440. // add top and bottom padding
  6441. $height += ($this->cell_padding['T'] + $this->cell_padding['B']);
  6442. }
  6443. $this->cell_padding = $prev_cell_padding;
  6444. $this->lasth = $prev_lasth;
  6445. return $height;
  6446. }
  6447. /**
  6448. * This method prints text from the current position.<br />
  6449. * @param $h (float) Line height
  6450. * @param $txt (string) String to print
  6451. * @param $link (mixed) URL or identifier returned by AddLink()
  6452. * @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
  6453. * @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>
  6454. * @param $ln (boolean) if true set cursor at the bottom of the line, otherwise set cursor at the top of the line.
  6455. * @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.
  6456. * @param $firstline (boolean) if true prints only the first line and return the remaining string.
  6457. * @param $firstblock (boolean) if true the string is the starting of a line.
  6458. * @param $maxh (float) maximum height. The remaining unprinted text will be returned. It should be >= $h and less then remaining space to the bottom of the page, or 0 for disable this feature.
  6459. * @param $wadj (float) first line width will be reduced by this amount (used in HTML mode).
  6460. * @param $margin (array) margin array of the parent container
  6461. * @return mixed Return the number of cells or the remaining string if $firstline = true.
  6462. * @public
  6463. * @since 1.5
  6464. */
  6465. public function Write($h, $txt, $link='', $fill=false, $align='', $ln=false, $stretch=0, $firstline=false, $firstblock=false, $maxh=0, $wadj=0, $margin='') {
  6466. // check page for no-write regions and adapt page margins if necessary
  6467. list($this->x, $this->y) = $this->checkPageRegions($h, $this->x, $this->y);
  6468. if (strlen($txt) == 0) {
  6469. // fix empty text
  6470. $txt = ' ';
  6471. }
  6472. if ($margin === '') {
  6473. // set default margins
  6474. $margin = $this->cell_margin;
  6475. }
  6476. // remove carriage returns
  6477. $s = str_replace("\r", '', $txt);
  6478. // check if string contains arabic text
  6479. if (preg_match($this->unicode->uni_RE_PATTERN_ARABIC, $s)) {
  6480. $arabic = true;
  6481. } else {
  6482. $arabic = false;
  6483. }
  6484. // check if string contains RTL text
  6485. if ($arabic OR ($this->tmprtl == 'R') OR preg_match($this->unicode->uni_RE_PATTERN_RTL, $s)) {
  6486. $rtlmode = true;
  6487. } else {
  6488. $rtlmode = false;
  6489. }
  6490. // get a char width
  6491. $chrwidth = $this->GetCharWidth(46); // dot character
  6492. // get array of unicode values
  6493. $chars = $this->UTF8StringToArray($s);
  6494. // get array of chars
  6495. $uchars = $this->UTF8ArrayToUniArray($chars);
  6496. // get the number of characters
  6497. $nb = count($chars);
  6498. // replacement for SHY character (minus symbol)
  6499. $shy_replacement = 45;
  6500. $shy_replacement_char = $this->unichr($shy_replacement);
  6501. // widht for SHY replacement
  6502. $shy_replacement_width = $this->GetCharWidth($shy_replacement);
  6503. // max Y
  6504. $maxy = $this->y + $maxh - $h - $this->cell_padding['T'] - $this->cell_padding['B'];
  6505. // page width
  6506. $pw = $w = $this->w - $this->lMargin - $this->rMargin;
  6507. // calculate remaining line width ($w)
  6508. if ($this->rtl) {
  6509. $w = $this->x - $this->lMargin;
  6510. } else {
  6511. $w = $this->w - $this->rMargin - $this->x;
  6512. }
  6513. // max column width
  6514. $wmax = $w - $wadj;
  6515. if (!$firstline) {
  6516. $wmax -= ($this->cell_padding['L'] + $this->cell_padding['R']);
  6517. }
  6518. if ((!$firstline) AND (($chrwidth > $wmax) OR ($this->GetCharWidth($chars[0]) > $wmax))) {
  6519. // a single character do not fit on column
  6520. return '';
  6521. }
  6522. // minimum row height
  6523. $row_height = max($h, $this->FontSize * $this->cell_height_ratio);
  6524. $start_page = $this->page;
  6525. $i = 0; // character position
  6526. $j = 0; // current starting position
  6527. $sep = -1; // position of the last blank space
  6528. $shy = false; // true if the last blank is a soft hypen (SHY)
  6529. $l = 0; // current string length
  6530. $nl = 0; //number of lines
  6531. $linebreak = false;
  6532. $pc = 0; // previous character
  6533. // for each character
  6534. while ($i < $nb) {
  6535. if (($maxh > 0) AND ($this->y >= $maxy) ) {
  6536. break;
  6537. }
  6538. //Get the current character
  6539. $c = $chars[$i];
  6540. if ($c == 10) { // 10 = "\n" = new line
  6541. //Explicit line break
  6542. if ($align == 'J') {
  6543. if ($this->rtl) {
  6544. $talign = 'R';
  6545. } else {
  6546. $talign = 'L';
  6547. }
  6548. } else {
  6549. $talign = $align;
  6550. }
  6551. $tmpstr = $this->UniArrSubString($uchars, $j, $i);
  6552. if ($firstline) {
  6553. $startx = $this->x;
  6554. $tmparr = array_slice($chars, $j, ($i - $j));
  6555. if ($rtlmode) {
  6556. $tmparr = $this->utf8Bidi($tmparr, $tmpstr, $this->tmprtl);
  6557. }
  6558. $linew = $this->GetArrStringWidth($tmparr);
  6559. unset($tmparr);
  6560. if ($this->rtl) {
  6561. $this->endlinex = $startx - $linew;
  6562. } else {
  6563. $this->endlinex = $startx + $linew;
  6564. }
  6565. $w = $linew;
  6566. $tmpcellpadding = $this->cell_padding;
  6567. if ($maxh == 0) {
  6568. $this->SetCellPadding(0);
  6569. }
  6570. }
  6571. if ($firstblock AND $this->isRTLTextDir()) {
  6572. $tmpstr = $this->stringRightTrim($tmpstr);
  6573. }
  6574. // Skip newlines at the begining of a page or column
  6575. if (!empty($tmpstr) OR ($this->y < ($this->PageBreakTrigger - $row_height))) {
  6576. $this->Cell($w, $h, $tmpstr, 0, 1, $talign, $fill, $link, $stretch);
  6577. }
  6578. unset($tmpstr);
  6579. if ($firstline) {
  6580. $this->cell_padding = $tmpcellpadding;
  6581. return ($this->UniArrSubString($uchars, $i));
  6582. }
  6583. ++$nl;
  6584. $j = $i + 1;
  6585. $l = 0;
  6586. $sep = -1;
  6587. $shy = false;
  6588. // account for margin changes
  6589. if ((($this->y + $this->lasth) > $this->PageBreakTrigger) AND ($this->inPageBody())) {
  6590. $this->AcceptPageBreak();
  6591. if ($this->rtl) {
  6592. $this->x -= $margin['R'];
  6593. } else {
  6594. $this->x += $margin['L'];
  6595. }
  6596. $this->lMargin += $margin['L'];
  6597. $this->rMargin += $margin['R'];
  6598. }
  6599. $w = $this->getRemainingWidth();
  6600. $wmax = $w - $this->cell_padding['L'] - $this->cell_padding['R'];
  6601. } else {
  6602. // 160 is the non-breaking space.
  6603. // 173 is SHY (Soft Hypen).
  6604. // \p{Z} or \p{Separator}: any kind of Unicode whitespace or invisible separator.
  6605. // \p{Lo} or \p{Other_Letter}: a Unicode letter or ideograph that does not have lowercase and uppercase variants.
  6606. // \p{Lo} is needed because Chinese characters are packed next to each other without spaces in between.
  6607. if (($c != 160) AND (($c == 173) OR preg_match($this->re_spaces, $this->unichr($c)))) {
  6608. // update last blank space position
  6609. $sep = $i;
  6610. // check if is a SHY
  6611. if ($c == 173) {
  6612. $shy = true;
  6613. if ($pc == 45) {
  6614. $tmp_shy_replacement_width = 0;
  6615. $tmp_shy_replacement_char = '';
  6616. } else {
  6617. $tmp_shy_replacement_width = $shy_replacement_width;
  6618. $tmp_shy_replacement_char = $shy_replacement_char;
  6619. }
  6620. } else {
  6621. $shy = false;
  6622. }
  6623. }
  6624. // update string length
  6625. if ($this->isUnicodeFont() AND ($arabic)) {
  6626. // with bidirectional algorithm some chars may be changed affecting the line length
  6627. // *** very slow ***
  6628. $l = $this->GetArrStringWidth($this->utf8Bidi(array_slice($chars, $j, ($i - $j)), '', $this->tmprtl));
  6629. } else {
  6630. $l += $this->GetCharWidth($c);
  6631. }
  6632. if (($l > $wmax) OR (($c == 173) AND (($l + $tmp_shy_replacement_width) > $wmax)) ) {
  6633. // we have reached the end of column
  6634. if ($sep == -1) {
  6635. // check if the line was already started
  6636. if (($this->rtl AND ($this->x <= ($this->w - $this->rMargin - $chrwidth)))
  6637. OR ((!$this->rtl) AND ($this->x >= ($this->lMargin + $chrwidth)))) {
  6638. // print a void cell and go to next line
  6639. $this->Cell($w, $h, '', 0, 1);
  6640. $linebreak = true;
  6641. if ($firstline) {
  6642. return ($this->UniArrSubString($uchars, $j));
  6643. }
  6644. } else {
  6645. // truncate the word because do not fit on column
  6646. $tmpstr = $this->UniArrSubString($uchars, $j, $i);
  6647. if ($firstline) {
  6648. $startx = $this->x;
  6649. $tmparr = array_slice($chars, $j, ($i - $j));
  6650. if ($rtlmode) {
  6651. $tmparr = $this->utf8Bidi($tmparr, $tmpstr, $this->tmprtl);
  6652. }
  6653. $linew = $this->GetArrStringWidth($tmparr);
  6654. unset($tmparr);
  6655. if ($this->rtl) {
  6656. $this->endlinex = $startx - $linew;
  6657. } else {
  6658. $this->endlinex = $startx + $linew;
  6659. }
  6660. $w = $linew;
  6661. $tmpcellpadding = $this->cell_padding;
  6662. if ($maxh == 0) {
  6663. $this->SetCellPadding(0);
  6664. }
  6665. }
  6666. if ($firstblock AND $this->isRTLTextDir()) {
  6667. $tmpstr = $this->stringRightTrim($tmpstr);
  6668. }
  6669. $this->Cell($w, $h, $tmpstr, 0, 1, $align, $fill, $link, $stretch);
  6670. unset($tmpstr);
  6671. if ($firstline) {
  6672. $this->cell_padding = $tmpcellpadding;
  6673. return ($this->UniArrSubString($uchars, $i));
  6674. }
  6675. $j = $i;
  6676. --$i;
  6677. }
  6678. } else {
  6679. // word wrapping
  6680. if ($this->rtl AND (!$firstblock) AND ($sep < $i)) {
  6681. $endspace = 1;
  6682. } else {
  6683. $endspace = 0;
  6684. }
  6685. // check the length of the next string
  6686. $strrest = $this->UniArrSubString($uchars, ($sep + $endspace));
  6687. $nextstr = preg_split('/'.$this->re_space['p'].'/'.$this->re_space['m'], $this->stringTrim($strrest));
  6688. if (isset($nextstr[0]) AND ($this->GetStringWidth($nextstr[0]) > $pw)) {
  6689. // truncate the word because do not fit on a full page width
  6690. $tmpstr = $this->UniArrSubString($uchars, $j, $i);
  6691. if ($firstline) {
  6692. $startx = $this->x;
  6693. $tmparr = array_slice($chars, $j, ($i - $j));
  6694. if ($rtlmode) {
  6695. $tmparr = $this->utf8Bidi($tmparr, $tmpstr, $this->tmprtl);
  6696. }
  6697. $linew = $this->GetArrStringWidth($tmparr);
  6698. unset($tmparr);
  6699. if ($this->rtl) {
  6700. $this->endlinex = $startx - $linew;
  6701. } else {
  6702. $this->endlinex = $startx + $linew;
  6703. }
  6704. $w = $linew;
  6705. $tmpcellpadding = $this->cell_padding;
  6706. if ($maxh == 0) {
  6707. $this->SetCellPadding(0);
  6708. }
  6709. }
  6710. if ($firstblock AND $this->isRTLTextDir()) {
  6711. $tmpstr = $this->stringRightTrim($tmpstr);
  6712. }
  6713. $this->Cell($w, $h, $tmpstr, 0, 1, $align, $fill, $link, $stretch);
  6714. unset($tmpstr);
  6715. if ($firstline) {
  6716. $this->cell_padding = $tmpcellpadding;
  6717. return ($this->UniArrSubString($uchars, $i));
  6718. }
  6719. $j = $i;
  6720. --$i;
  6721. } else {
  6722. // word wrapping
  6723. if ($shy) {
  6724. // add hypen (minus symbol) at the end of the line
  6725. $shy_width = $tmp_shy_replacement_width;
  6726. if ($this->rtl) {
  6727. $shy_char_left = $tmp_shy_replacement_char;
  6728. $shy_char_right = '';
  6729. } else {
  6730. $shy_char_left = '';
  6731. $shy_char_right = $tmp_shy_replacement_char;
  6732. }
  6733. } else {
  6734. $shy_width = 0;
  6735. $shy_char_left = '';
  6736. $shy_char_right = '';
  6737. }
  6738. $tmpstr = $this->UniArrSubString($uchars, $j, ($sep + $endspace));
  6739. if ($firstline) {
  6740. $startx = $this->x;
  6741. $tmparr = array_slice($chars, $j, (($sep + $endspace) - $j));
  6742. if ($rtlmode) {
  6743. $tmparr = $this->utf8Bidi($tmparr, $tmpstr, $this->tmprtl);
  6744. }
  6745. $linew = $this->GetArrStringWidth($tmparr);
  6746. unset($tmparr);
  6747. if ($this->rtl) {
  6748. $this->endlinex = $startx - $linew - $shy_width;
  6749. } else {
  6750. $this->endlinex = $startx + $linew + $shy_width;
  6751. }
  6752. $w = $linew;
  6753. $tmpcellpadding = $this->cell_padding;
  6754. if ($maxh == 0) {
  6755. $this->SetCellPadding(0);
  6756. }
  6757. }
  6758. // print the line
  6759. if ($firstblock AND $this->isRTLTextDir()) {
  6760. $tmpstr = $this->stringRightTrim($tmpstr);
  6761. }
  6762. $this->Cell($w, $h, $shy_char_left.$tmpstr.$shy_char_right, 0, 1, $align, $fill, $link, $stretch);
  6763. unset($tmpstr);
  6764. if ($firstline) {
  6765. // return the remaining text
  6766. $this->cell_padding = $tmpcellpadding;
  6767. return ($this->UniArrSubString($uchars, ($sep + $endspace)));
  6768. }
  6769. $i = $sep;
  6770. $sep = -1;
  6771. $shy = false;
  6772. $j = ($i+1);
  6773. }
  6774. }
  6775. // account for margin changes
  6776. if ((($this->y + $this->lasth) > $this->PageBreakTrigger) AND ($this->inPageBody())) {
  6777. $this->AcceptPageBreak();
  6778. if ($this->rtl) {
  6779. $this->x -= $margin['R'];
  6780. } else {
  6781. $this->x += $margin['L'];
  6782. }
  6783. $this->lMargin += $margin['L'];
  6784. $this->rMargin += $margin['R'];
  6785. }
  6786. $w = $this->getRemainingWidth();
  6787. $wmax = $w - $this->cell_padding['L'] - $this->cell_padding['R'];
  6788. if ($linebreak) {
  6789. $linebreak = false;
  6790. } else {
  6791. ++$nl;
  6792. $l = 0;
  6793. }
  6794. }
  6795. }
  6796. // save last character
  6797. $pc = $c;
  6798. ++$i;
  6799. } // end while i < nb
  6800. // print last substring (if any)
  6801. if ($l > 0) {
  6802. switch ($align) {
  6803. case 'J':
  6804. case 'C': {
  6805. $w = $w;
  6806. break;
  6807. }
  6808. case 'L': {
  6809. if ($this->rtl) {
  6810. $w = $w;
  6811. } else {
  6812. $w = $l;
  6813. }
  6814. break;
  6815. }
  6816. case 'R': {
  6817. if ($this->rtl) {
  6818. $w = $l;
  6819. } else {
  6820. $w = $w;
  6821. }
  6822. break;
  6823. }
  6824. default: {
  6825. $w = $l;
  6826. break;
  6827. }
  6828. }
  6829. $tmpstr = $this->UniArrSubString($uchars, $j, $nb);
  6830. if ($firstline) {
  6831. $startx = $this->x;
  6832. $tmparr = array_slice($chars, $j, ($nb - $j));
  6833. if ($rtlmode) {
  6834. $tmparr = $this->utf8Bidi($tmparr, $tmpstr, $this->tmprtl);
  6835. }
  6836. $linew = $this->GetArrStringWidth($tmparr);
  6837. unset($tmparr);
  6838. if ($this->rtl) {
  6839. $this->endlinex = $startx - $linew;
  6840. } else {
  6841. $this->endlinex = $startx + $linew;
  6842. }
  6843. $w = $linew;
  6844. $tmpcellpadding = $this->cell_padding;
  6845. if ($maxh == 0) {
  6846. $this->SetCellPadding(0);
  6847. }
  6848. }
  6849. if ($firstblock AND $this->isRTLTextDir()) {
  6850. $tmpstr = $this->stringRightTrim($tmpstr);
  6851. }
  6852. $this->Cell($w, $h, $tmpstr, 0, $ln, $align, $fill, $link, $stretch);
  6853. unset($tmpstr);
  6854. if ($firstline) {
  6855. $this->cell_padding = $tmpcellpadding;
  6856. return ($this->UniArrSubString($uchars, $nb));
  6857. }
  6858. ++$nl;
  6859. }
  6860. if ($firstline) {
  6861. return '';
  6862. }
  6863. return $nl;
  6864. }
  6865. /**
  6866. * Returns the remaining width between the current position and margins.
  6867. * @return int Return the remaining width
  6868. * @protected
  6869. */
  6870. protected function getRemainingWidth() {
  6871. list($this->x, $this->y) = $this->checkPageRegions(0, $this->x, $this->y);
  6872. if ($this->rtl) {
  6873. return ($this->x - $this->lMargin);
  6874. } else {
  6875. return ($this->w - $this->rMargin - $this->x);
  6876. }
  6877. }
  6878. /**
  6879. * Extract a slice of the $strarr array and return it as string.
  6880. * @param $strarr (string) The input array of characters.
  6881. * @param $start (int) the starting element of $strarr.
  6882. * @param $end (int) first element that will not be returned.
  6883. * @return Return part of a string
  6884. * @public
  6885. */
  6886. public function UTF8ArrSubString($strarr, $start='', $end='') {
  6887. if (strlen($start) == 0) {
  6888. $start = 0;
  6889. }
  6890. if (strlen($end) == 0) {
  6891. $end = count($strarr);
  6892. }
  6893. $string = '';
  6894. for ($i=$start; $i < $end; ++$i) {
  6895. $string .= $this->unichr($strarr[$i]);
  6896. }
  6897. return $string;
  6898. }
  6899. /**
  6900. * Extract a slice of the $uniarr array and return it as string.
  6901. * @param $uniarr (string) The input array of characters.
  6902. * @param $start (int) the starting element of $strarr.
  6903. * @param $end (int) first element that will not be returned.
  6904. * @return Return part of a string
  6905. * @public
  6906. * @since 4.5.037 (2009-04-07)
  6907. */
  6908. public function UniArrSubString($uniarr, $start='', $end='') {
  6909. if (strlen($start) == 0) {
  6910. $start = 0;
  6911. }
  6912. if (strlen($end) == 0) {
  6913. $end = count($uniarr);
  6914. }
  6915. $string = '';
  6916. for ($i=$start; $i < $end; ++$i) {
  6917. $string .= $uniarr[$i];
  6918. }
  6919. return $string;
  6920. }
  6921. /**
  6922. * Convert an array of UTF8 values to array of unicode characters
  6923. * @param $ta (string) The input array of UTF8 values.
  6924. * @return Return array of unicode characters
  6925. * @public
  6926. * @since 4.5.037 (2009-04-07)
  6927. */
  6928. public function UTF8ArrayToUniArray($ta) {
  6929. return array_map(array($this, 'unichr'), $ta);
  6930. }
  6931. /**
  6932. * Returns the unicode caracter specified by UTF-8 value
  6933. * @param $c (int) UTF-8 value
  6934. * @return Returns the specified character.
  6935. * @author Miguel Perez, Nicola Asuni
  6936. * @public
  6937. * @since 2.3.000 (2008-03-05)
  6938. */
  6939. public function unichr($c) {
  6940. if (!$this->isunicode) {
  6941. return chr($c);
  6942. } elseif ($c <= 0x7F) {
  6943. // one byte
  6944. return chr($c);
  6945. } elseif ($c <= 0x7FF) {
  6946. // two bytes
  6947. return chr(0xC0 | $c >> 6).chr(0x80 | $c & 0x3F);
  6948. } elseif ($c <= 0xFFFF) {
  6949. // three bytes
  6950. return chr(0xE0 | $c >> 12).chr(0x80 | $c >> 6 & 0x3F).chr(0x80 | $c & 0x3F);
  6951. } elseif ($c <= 0x10FFFF) {
  6952. // four bytes
  6953. return chr(0xF0 | $c >> 18).chr(0x80 | $c >> 12 & 0x3F).chr(0x80 | $c >> 6 & 0x3F).chr(0x80 | $c & 0x3F);
  6954. } else {
  6955. return '';
  6956. }
  6957. }
  6958. /**
  6959. * Return the image type given the file name or array returned by getimagesize() function.
  6960. * @param $imgfile (string) image file name
  6961. * @param $iminfo (array) array of image information returned by getimagesize() function.
  6962. * @return string image type
  6963. * @since 4.8.017 (2009-11-27)
  6964. */
  6965. public function getImageFileType($imgfile, $iminfo=array()) {
  6966. $type = '';
  6967. if (isset($iminfo['mime']) AND !empty($iminfo['mime'])) {
  6968. $mime = explode('/', $iminfo['mime']);
  6969. if ((count($mime) > 1) AND ($mime[0] == 'image') AND (!empty($mime[1]))) {
  6970. $type = strtolower(trim($mime[1]));
  6971. }
  6972. }
  6973. if (empty($type)) {
  6974. $fileinfo = pathinfo($imgfile);
  6975. if (isset($fileinfo['extension']) AND (!$this->empty_string($fileinfo['extension']))) {
  6976. $type = strtolower(trim($fileinfo['extension']));
  6977. }
  6978. }
  6979. if ($type == 'jpg') {
  6980. $type = 'jpeg';
  6981. }
  6982. return $type;
  6983. }
  6984. /**
  6985. * Set the block dimensions accounting for page breaks and page/column fitting
  6986. * @param $w (float) width
  6987. * @param $h (float) height
  6988. * @param $x (float) X coordinate
  6989. * @param $y (float) Y coodiante
  6990. * @param $fitonpage (boolean) if true the block is resized to not exceed page dimensions.
  6991. * @return array($w, $h, $x, $y)
  6992. * @protected
  6993. * @since 5.5.009 (2010-07-05)
  6994. */
  6995. protected function fitBlock($w, $h, $x, $y, $fitonpage=false) {
  6996. if ($w <= 0) {
  6997. // set maximum width
  6998. $w = ($this->w - $this->lMargin - $this->rMargin);
  6999. }
  7000. if ($h <= 0) {
  7001. // set maximum height
  7002. $h = ($this->PageBreakTrigger - $this->tMargin);
  7003. }
  7004. // resize the block to be vertically contained on a single page or single column
  7005. if ($fitonpage OR $this->AutoPageBreak) {
  7006. $ratio_wh = ($w / $h);
  7007. if ($h > ($this->PageBreakTrigger - $this->tMargin)) {
  7008. $h = $this->PageBreakTrigger - $this->tMargin;
  7009. $w = ($h * $ratio_wh);
  7010. }
  7011. // resize the block to be horizontally contained on a single page or single column
  7012. if ($fitonpage) {
  7013. $maxw = ($this->w - $this->lMargin - $this->rMargin);
  7014. if ($w > $maxw) {
  7015. $w = $maxw;
  7016. $h = ($w / $ratio_wh);
  7017. }
  7018. }
  7019. }
  7020. // Check whether we need a new page or new column first as this does not fit
  7021. $prev_x = $this->x;
  7022. $prev_y = $this->y;
  7023. if ($this->checkPageBreak($h, $y) OR ($this->y < $prev_y)) {
  7024. $y = $this->y;
  7025. if ($this->rtl) {
  7026. $x += ($prev_x - $this->x);
  7027. } else {
  7028. $x += ($this->x - $prev_x);
  7029. }
  7030. $this->newline = true;
  7031. }
  7032. // resize the block to be contained on the remaining available page or column space
  7033. if ($fitonpage) {
  7034. $ratio_wh = ($w / $h);
  7035. if (($y + $h) > $this->PageBreakTrigger) {
  7036. $h = $this->PageBreakTrigger - $y;
  7037. $w = ($h * $ratio_wh);
  7038. }
  7039. if ((!$this->rtl) AND (($x + $w) > ($this->w - $this->rMargin))) {
  7040. $w = $this->w - $this->rMargin - $x;
  7041. $h = ($w / $ratio_wh);
  7042. } elseif (($this->rtl) AND (($x - $w) < ($this->lMargin))) {
  7043. $w = $x - $this->lMargin;
  7044. $h = ($w / $ratio_wh);
  7045. }
  7046. }
  7047. return array($w, $h, $x, $y);
  7048. }
  7049. /**
  7050. * Puts an image in the page.
  7051. * The upper-left corner must be given.
  7052. * The dimensions can be specified in different ways:<ul>
  7053. * <li>explicit width and height (expressed in user unit)</li>
  7054. * <li>one explicit dimension, the other being calculated automatically in order to keep the original proportions</li>
  7055. * <li>no explicit dimension, in which case the image is put at 72 dpi</li></ul>
  7056. * 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;
  7057. * The format can be specified explicitly or inferred from the file extension.<br />
  7058. * It is possible to put a link on the image.<br />
  7059. * Remark: if an image is used several times, only one copy will be embedded in the file.<br />
  7060. * @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').
  7061. * @param $x (float) Abscissa of the upper-left corner (LTR) or upper-right corner (RTL).
  7062. * @param $y (float) Ordinate of the upper-left corner (LTR) or upper-right corner (RTL).
  7063. * @param $w (float) Width of the image in the page. If not specified or equal to zero, it is automatically calculated.
  7064. * @param $h (float) Height of the image in the page. If not specified or equal to zero, it is automatically calculated.
  7065. * @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.
  7066. * @param $link (mixed) URL or identifier returned by AddLink().
  7067. * @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>
  7068. * @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).
  7069. * @param $dpi (int) dot-per-inch resolution used on resize
  7070. * @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>
  7071. * @param $ismask (boolean) true if this image is a mask, false otherwise
  7072. * @param $imgmask (mixed) image object returned by this function or false
  7073. * @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)))
  7074. * @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).
  7075. * @param $hidden (boolean) If true do not display the image.
  7076. * @param $fitonpage (boolean) If true the image is resized to not exceed page dimensions.
  7077. * @param $alt (boolean) If true the image will be added as alternative and not directly printed (the ID of the image will be returned).
  7078. * @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.
  7079. * @return image information
  7080. * @public
  7081. * @since 1.1
  7082. */
  7083. 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()) {
  7084. if ($x === '') {
  7085. $x = $this->x;
  7086. }
  7087. if ($y === '') {
  7088. $y = $this->y;
  7089. }
  7090. // check page for no-write regions and adapt page margins if necessary
  7091. list($x, $y) = $this->checkPageRegions($h, $x, $y);
  7092. $cached_file = false; // true when the file is cached
  7093. $exurl = ''; // external streams
  7094. // check if we are passing an image as file or string
  7095. if ($file{0} === '@') {
  7096. // image from string
  7097. $imgdata = substr($file, 1);
  7098. $file = K_PATH_CACHE.'img_'.md5($imgdata);
  7099. $fp = fopen($file, 'w');
  7100. fwrite($fp, $imgdata);
  7101. fclose($fp);
  7102. unset($imgdata);
  7103. $cached_file = true;
  7104. $imsize = @getimagesize($file);
  7105. if ($imsize === FALSE) {
  7106. unlink($file);
  7107. $cached_file = false;
  7108. }
  7109. } else { // image file
  7110. if ($file{0} === '*') {
  7111. // image as external stream
  7112. $file = substr($file, 1);
  7113. $exurl = $file;
  7114. }
  7115. // check if is local file
  7116. if (!@file_exists($file)) {
  7117. // encode spaces on filename (file is probably an URL)
  7118. $file = str_replace(' ', '%20', $file);
  7119. }
  7120. if (@file_exists($file)) {
  7121. // get image dimensions
  7122. $imsize = @getimagesize($file);
  7123. } else {
  7124. $imsize = false;
  7125. }
  7126. if ($imsize === FALSE) {
  7127. if (function_exists('curl_init')) {
  7128. // try to get remote file data using cURL
  7129. $cs = curl_init(); // curl session
  7130. curl_setopt($cs, CURLOPT_URL, $file);
  7131. curl_setopt($cs, CURLOPT_BINARYTRANSFER, true);
  7132. curl_setopt($cs, CURLOPT_FAILONERROR, true);
  7133. curl_setopt($cs, CURLOPT_RETURNTRANSFER, true);
  7134. curl_setopt($cs, CURLOPT_FOLLOWLOCATION, true);
  7135. curl_setopt($cs, CURLOPT_CONNECTTIMEOUT, 5);
  7136. curl_setopt($cs, CURLOPT_TIMEOUT, 30);
  7137. curl_setopt($cs, CURLOPT_SSL_VERIFYPEER, false);
  7138. curl_setopt($cs, CURLOPT_SSL_VERIFYHOST, false);
  7139. curl_setopt($cs, CURLOPT_USERAGENT, 'TCPDF');
  7140. $imgdata = curl_exec($cs);
  7141. curl_close($cs);
  7142. if ($imgdata !== FALSE) {
  7143. // copy image to cache
  7144. $file = K_PATH_CACHE.'img_'.md5($imgdata);
  7145. $fp = fopen($file, 'w');
  7146. fwrite($fp, $imgdata);
  7147. fclose($fp);
  7148. unset($imgdata);
  7149. $cached_file = true;
  7150. $imsize = @getimagesize($file);
  7151. if ($imsize === FALSE) {
  7152. unlink($file);
  7153. $cached_file = false;
  7154. }
  7155. }
  7156. } elseif (($w > 0) AND ($h > 0)) {
  7157. // get measures from specified data
  7158. $pw = $this->getHTMLUnitToUnits($w, 0, $this->pdfunit, true) * $this->imgscale * $this->k;
  7159. $ph = $this->getHTMLUnitToUnits($h, 0, $this->pdfunit, true) * $this->imgscale * $this->k;
  7160. $imsize = array($pw, $ph);
  7161. }
  7162. }
  7163. }
  7164. if ($imsize === FALSE) {
  7165. if (substr($file, 0, -34) == K_PATH_CACHE.'msk') { // mask file
  7166. // get measures from specified data
  7167. $pw = $this->getHTMLUnitToUnits($w, 0, $this->pdfunit, true) * $this->imgscale * $this->k;
  7168. $ph = $this->getHTMLUnitToUnits($h, 0, $this->pdfunit, true) * $this->imgscale * $this->k;
  7169. $imsize = array($pw, $ph);
  7170. } else {
  7171. $this->Error('[Image] Unable to get image: '.$file);
  7172. }
  7173. }
  7174. // get original image width and height in pixels
  7175. list($pixw, $pixh) = $imsize;
  7176. // calculate image width and height on document
  7177. if (($w <= 0) AND ($h <= 0)) {
  7178. // convert image size to document unit
  7179. $w = $this->pixelsToUnits($pixw);
  7180. $h = $this->pixelsToUnits($pixh);
  7181. } elseif ($w <= 0) {
  7182. $w = $h * $pixw / $pixh;
  7183. } elseif ($h <= 0) {
  7184. $h = $w * $pixh / $pixw;
  7185. } elseif (($fitbox !== false) AND ($w > 0) AND ($h > 0)) {
  7186. if (strlen($fitbox) !== 2) {
  7187. // set default alignment
  7188. $fitbox = '--';
  7189. }
  7190. // scale image dimensions proportionally to fit within the ($w, $h) box
  7191. if ((($w * $pixh) / ($h * $pixw)) < 1) {
  7192. // store current height
  7193. $oldh = $h;
  7194. // calculate new height
  7195. $h = $w * $pixh / $pixw;
  7196. // height difference
  7197. $hdiff = ($oldh - $h);
  7198. // vertical alignment
  7199. switch (strtoupper($fitbox{1})) {
  7200. case 'T': {
  7201. break;
  7202. }
  7203. case 'M': {
  7204. $y += ($hdiff / 2);
  7205. break;
  7206. }
  7207. case 'B': {
  7208. $y += $hdiff;
  7209. break;
  7210. }
  7211. }
  7212. } else {
  7213. // store current width
  7214. $oldw = $w;
  7215. // calculate new width
  7216. $w = $h * $pixw / $pixh;
  7217. // width difference
  7218. $wdiff = ($oldw - $w);
  7219. // horizontal alignment
  7220. switch (strtoupper($fitbox{0})) {
  7221. case 'L': {
  7222. if ($this->rtl) {
  7223. $x -= $wdiff;
  7224. }
  7225. break;
  7226. }
  7227. case 'C': {
  7228. if ($this->rtl) {
  7229. $x -= ($wdiff / 2);
  7230. } else {
  7231. $x += ($wdiff / 2);
  7232. }
  7233. break;
  7234. }
  7235. case 'R': {
  7236. if (!$this->rtl) {
  7237. $x += $wdiff;
  7238. }
  7239. break;
  7240. }
  7241. }
  7242. }
  7243. }
  7244. // fit the image on available space
  7245. list($w, $h, $x, $y) = $this->fitBlock($w, $h, $x, $y, $fitonpage);
  7246. // calculate new minimum dimensions in pixels
  7247. $neww = round($w * $this->k * $dpi / $this->dpi);
  7248. $newh = round($h * $this->k * $dpi / $this->dpi);
  7249. // check if resize is necessary (resize is used only to reduce the image)
  7250. $newsize = ($neww * $newh);
  7251. $pixsize = ($pixw * $pixh);
  7252. if (intval($resize) == 2) {
  7253. $resize = true;
  7254. } elseif ($newsize >= $pixsize) {
  7255. $resize = false;
  7256. }
  7257. // check if image has been already added on document
  7258. $newimage = true;
  7259. if (in_array($file, $this->imagekeys)) {
  7260. $newimage = false;
  7261. // get existing image data
  7262. $info = $this->getImageBuffer($file);
  7263. if (substr($file, 0, -34) != K_PATH_CACHE.'msk') {
  7264. // check if the newer image is larger
  7265. $oldsize = ($info['w'] * $info['h']);
  7266. if ((($oldsize < $newsize) AND ($resize)) OR (($oldsize < $pixsize) AND (!$resize))) {
  7267. $newimage = true;
  7268. }
  7269. }
  7270. } elseif (substr($file, 0, -34) != K_PATH_CACHE.'msk') {
  7271. // check for cached images with alpha channel
  7272. $filehash = md5($file);
  7273. $tempfile_plain = K_PATH_CACHE.'mskp_'.$filehash;
  7274. $tempfile_alpha = K_PATH_CACHE.'mska_'.$filehash;
  7275. if (in_array($tempfile_plain, $this->imagekeys)) {
  7276. // get existing image data
  7277. $info = $this->getImageBuffer($tempfile_plain);
  7278. // check if the newer image is larger
  7279. $oldsize = ($info['w'] * $info['h']);
  7280. if ((($oldsize < $newsize) AND ($resize)) OR (($oldsize < $pixsize) AND (!$resize))) {
  7281. $newimage = true;
  7282. } else {
  7283. $newimage = false;
  7284. // embed mask image
  7285. $imgmask = $this->Image($tempfile_alpha, $x, $y, $w, $h, 'PNG', '', '', $resize, $dpi, '', true, false);
  7286. // embed image, masked with previously embedded mask
  7287. return $this->Image($tempfile_plain, $x, $y, $w, $h, $type, $link, $align, $resize, $dpi, $palign, false, $imgmask);
  7288. }
  7289. }
  7290. }
  7291. if ($newimage) {
  7292. //First use of image, get info
  7293. $type = strtolower($type);
  7294. if ($type == '') {
  7295. $type = $this->getImageFileType($file, $imsize);
  7296. } elseif ($type == 'jpg') {
  7297. $type = 'jpeg';
  7298. }
  7299. $mqr = $this->get_mqr();
  7300. $this->set_mqr(false);
  7301. // Specific image handlers
  7302. $mtd = '_parse'.$type;
  7303. // GD image handler function
  7304. $gdfunction = 'imagecreatefrom'.$type;
  7305. $info = false;
  7306. if ((method_exists($this, $mtd)) AND (!($resize AND (function_exists($gdfunction) OR extension_loaded('imagick'))))) {
  7307. // TCPDF image functions
  7308. $info = $this->$mtd($file);
  7309. if ($info == 'pngalpha') {
  7310. return $this->ImagePngAlpha($file, $x, $y, $pixw, $pixh, $w, $h, 'PNG', $link, $align, $resize, $dpi, $palign, $filehash);
  7311. }
  7312. }
  7313. if (!$info) {
  7314. if (function_exists($gdfunction)) {
  7315. // GD library
  7316. $img = $gdfunction($file);
  7317. if ($resize) {
  7318. $imgr = imagecreatetruecolor($neww, $newh);
  7319. if (($type == 'gif') OR ($type == 'png')) {
  7320. $imgr = $this->_setGDImageTransparency($imgr, $img);
  7321. }
  7322. imagecopyresampled($imgr, $img, 0, 0, 0, 0, $neww, $newh, $pixw, $pixh);
  7323. if (($type == 'gif') OR ($type == 'png')) {
  7324. $info = $this->_toPNG($imgr);
  7325. } else {
  7326. $info = $this->_toJPEG($imgr);
  7327. }
  7328. } else {
  7329. if (($type == 'gif') OR ($type == 'png')) {
  7330. $info = $this->_toPNG($img);
  7331. } else {
  7332. $info = $this->_toJPEG($img);
  7333. }
  7334. }
  7335. } elseif (extension_loaded('imagick')) {
  7336. // ImageMagick library
  7337. $img = new Imagick();
  7338. if ($type == 'SVG') {
  7339. // get SVG file content
  7340. $svgimg = file_get_contents($file);
  7341. // get width and height
  7342. $regs = array();
  7343. if (preg_match('/<svg([^\>]*)>/si', $svgimg, $regs)) {
  7344. $svgtag = $regs[1];
  7345. $tmp = array();
  7346. if (preg_match('/[\s]+width[\s]*=[\s]*"([^"]*)"/si', $svgtag, $tmp)) {
  7347. $ow = $this->getHTMLUnitToUnits($tmp[1], 1, $this->svgunit, false);
  7348. $owu = sprintf('%.3F', ($ow * $dpi / 72)).$this->pdfunit;
  7349. $svgtag = preg_replace('/[\s]+width[\s]*=[\s]*"[^"]*"/si', ' width="'.$owu.'"', $svgtag, 1);
  7350. } else {
  7351. $ow = $w;
  7352. }
  7353. $tmp = array();
  7354. if (preg_match('/[\s]+height[\s]*=[\s]*"([^"]*)"/si', $svgtag, $tmp)) {
  7355. $oh = $this->getHTMLUnitToUnits($tmp[1], 1, $this->svgunit, false);
  7356. $ohu = sprintf('%.3F', ($oh * $dpi / 72)).$this->pdfunit;
  7357. $svgtag = preg_replace('/[\s]+height[\s]*=[\s]*"[^"]*"/si', ' height="'.$ohu.'"', $svgtag, 1);
  7358. } else {
  7359. $oh = $h;
  7360. }
  7361. $tmp = array();
  7362. if (!preg_match('/[\s]+viewBox[\s]*=[\s]*"[\s]*([0-9\.]+)[\s]+([0-9\.]+)[\s]+([0-9\.]+)[\s]+([0-9\.]+)[\s]*"/si', $svgtag, $tmp)) {
  7363. $vbw = ($ow * $this->imgscale * $this->k);
  7364. $vbh = ($oh * $this->imgscale * $this->k);
  7365. $vbox = sprintf(' viewBox="0 0 %.3F %.3F" ', $vbw, $vbh);
  7366. $svgtag = $vbox.$svgtag;
  7367. }
  7368. $svgimg = preg_replace('/<svg([^\>]*)>/si', '<svg'.$svgtag.'>', $svgimg, 1);
  7369. }
  7370. $img->readImageBlob($svgimg);
  7371. } else {
  7372. $img->readImage($file);
  7373. }
  7374. if ($resize) {
  7375. $img->resizeImage($neww, $newh, 10, 1, false);
  7376. }
  7377. $img->setCompressionQuality($this->jpeg_quality);
  7378. $img->setImageFormat('jpeg');
  7379. $tempname = tempnam(K_PATH_CACHE, 'jpg_');
  7380. $img->writeImage($tempname);
  7381. $info = $this->_parsejpeg($tempname);
  7382. unlink($tempname);
  7383. $img->destroy();
  7384. } else {
  7385. return;
  7386. }
  7387. }
  7388. if ($info === false) {
  7389. //If false, we cannot process image
  7390. return;
  7391. }
  7392. $this->set_mqr($mqr);
  7393. if ($ismask) {
  7394. // force grayscale
  7395. $info['cs'] = 'DeviceGray';
  7396. }
  7397. $info['i'] = $this->numimages;
  7398. if (!in_array($file, $this->imagekeys)) {
  7399. ++$info['i'];
  7400. }
  7401. if ($imgmask !== false) {
  7402. $info['masked'] = $imgmask;
  7403. }
  7404. if (!empty($exurl)) {
  7405. $info['exurl'] = $exurl;
  7406. }
  7407. // array of alternative images
  7408. $info['altimgs'] = $altimgs;
  7409. // add image to document
  7410. $this->setImageBuffer($file, $info);
  7411. }
  7412. if ($cached_file) {
  7413. // remove cached file
  7414. unlink($file);
  7415. }
  7416. // set alignment
  7417. $this->img_rb_y = $y + $h;
  7418. // set alignment
  7419. if ($this->rtl) {
  7420. if ($palign == 'L') {
  7421. $ximg = $this->lMargin;
  7422. } elseif ($palign == 'C') {
  7423. $ximg = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
  7424. } elseif ($palign == 'R') {
  7425. $ximg = $this->w - $this->rMargin - $w;
  7426. } else {
  7427. $ximg = $x - $w;
  7428. }
  7429. $this->img_rb_x = $ximg;
  7430. } else {
  7431. if ($palign == 'L') {
  7432. $ximg = $this->lMargin;
  7433. } elseif ($palign == 'C') {
  7434. $ximg = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
  7435. } elseif ($palign == 'R') {
  7436. $ximg = $this->w - $this->rMargin - $w;
  7437. } else {
  7438. $ximg = $x;
  7439. }
  7440. $this->img_rb_x = $ximg + $w;
  7441. }
  7442. if ($ismask OR $hidden) {
  7443. // image is not displayed
  7444. return $info['i'];
  7445. }
  7446. $xkimg = $ximg * $this->k;
  7447. if (!$alt) {
  7448. // only non-alternative immages will be set
  7449. $this->_out(sprintf('q %.2F 0 0 %.2F %.2F %.2F cm /I%u Do Q', ($w * $this->k), ($h * $this->k), $xkimg, (($this->h - ($y + $h)) * $this->k), $info['i']));
  7450. }
  7451. if (!empty($border)) {
  7452. $bx = $this->x;
  7453. $by = $this->y;
  7454. $this->x = $ximg;
  7455. if ($this->rtl) {
  7456. $this->x += $w;
  7457. }
  7458. $this->y = $y;
  7459. $this->Cell($w, $h, '', $border, 0, '', 0, '', 0, true);
  7460. $this->x = $bx;
  7461. $this->y = $by;
  7462. }
  7463. if ($link) {
  7464. $this->Link($ximg, $y, $w, $h, $link, 0);
  7465. }
  7466. // set pointer to align the next text/objects
  7467. switch($align) {
  7468. case 'T': {
  7469. $this->y = $y;
  7470. $this->x = $this->img_rb_x;
  7471. break;
  7472. }
  7473. case 'M': {
  7474. $this->y = $y + round($h/2);
  7475. $this->x = $this->img_rb_x;
  7476. break;
  7477. }
  7478. case 'B': {
  7479. $this->y = $this->img_rb_y;
  7480. $this->x = $this->img_rb_x;
  7481. break;
  7482. }
  7483. case 'N': {
  7484. $this->SetY($this->img_rb_y);
  7485. break;
  7486. }
  7487. default:{
  7488. break;
  7489. }
  7490. }
  7491. $this->endlinex = $this->img_rb_x;
  7492. if ($this->inxobj) {
  7493. // we are inside an XObject template
  7494. $this->xobjects[$this->xobjid]['images'][] = $info['i'];
  7495. }
  7496. return $info['i'];
  7497. }
  7498. /**
  7499. * Sets the current active configuration setting of magic_quotes_runtime (if the set_magic_quotes_runtime function exist)
  7500. * @param $mqr (boolean) FALSE for off, TRUE for on.
  7501. * @since 4.6.025 (2009-08-17)
  7502. */
  7503. public function set_mqr($mqr) {
  7504. if (!defined('PHP_VERSION_ID')) {
  7505. $version = PHP_VERSION;
  7506. define('PHP_VERSION_ID', (($version{0} * 10000) + ($version{2} * 100) + $version{4}));
  7507. }
  7508. if (PHP_VERSION_ID < 50300) {
  7509. @set_magic_quotes_runtime($mqr);
  7510. }
  7511. }
  7512. /**
  7513. * Gets the current active configuration setting of magic_quotes_runtime (if the get_magic_quotes_runtime function exist)
  7514. * @return Returns 0 if magic quotes runtime is off or get_magic_quotes_runtime doesn't exist, 1 otherwise.
  7515. * @since 4.6.025 (2009-08-17)
  7516. */
  7517. public function get_mqr() {
  7518. if (!defined('PHP_VERSION_ID')) {
  7519. $version = PHP_VERSION;
  7520. define('PHP_VERSION_ID', (($version{0} * 10000) + ($version{2} * 100) + $version{4}));
  7521. }
  7522. if (PHP_VERSION_ID < 50300) {
  7523. return @get_magic_quotes_runtime();
  7524. }
  7525. return 0;
  7526. }
  7527. /**
  7528. * Convert the loaded image to a JPEG and then return a structure for the PDF creator.
  7529. * This function requires GD library and write access to the directory defined on K_PATH_CACHE constant.
  7530. * @param $image (image) Image object.
  7531. * return image JPEG image object.
  7532. * @protected
  7533. */
  7534. protected function _toJPEG($image) {
  7535. $tempname = tempnam(K_PATH_CACHE, 'jpg_');
  7536. imagejpeg($image, $tempname, $this->jpeg_quality);
  7537. imagedestroy($image);
  7538. $retvars = $this->_parsejpeg($tempname);
  7539. // tidy up by removing temporary image
  7540. unlink($tempname);
  7541. return $retvars;
  7542. }
  7543. /**
  7544. * Convert the loaded image to a PNG and then return a structure for the PDF creator.
  7545. * This function requires GD library and write access to the directory defined on K_PATH_CACHE constant.
  7546. * @param $image (image) Image object.
  7547. * return image PNG image object.
  7548. * @protected
  7549. * @since 4.9.016 (2010-04-20)
  7550. */
  7551. protected function _toPNG($image) {
  7552. // set temporary image file name
  7553. $tempname = tempnam(K_PATH_CACHE, 'jpg_');
  7554. // turn off interlaced mode
  7555. imageinterlace($image, 0);
  7556. // create temporary PNG image
  7557. imagepng($image, $tempname);
  7558. // remove image from memory
  7559. imagedestroy($image);
  7560. // get PNG image data
  7561. $retvars = $this->_parsepng($tempname);
  7562. // tidy up by removing temporary image
  7563. unlink($tempname);
  7564. return $retvars;
  7565. }
  7566. /**
  7567. * Set the transparency for the given GD image.
  7568. * @param $new_image (image) GD image object
  7569. * @param $image (image) GD image object.
  7570. * return GD image object.
  7571. * @protected
  7572. * @since 4.9.016 (2010-04-20)
  7573. */
  7574. protected function _setGDImageTransparency($new_image, $image) {
  7575. // transparency index
  7576. $tid = imagecolortransparent($image);
  7577. // default transparency color
  7578. $tcol = array('red' => 255, 'green' => 255, 'blue' => 255);
  7579. if ($tid >= 0) {
  7580. // get the colors for the transparency index
  7581. $tcol = imagecolorsforindex($image, $tid);
  7582. }
  7583. $tid = imagecolorallocate($new_image, $tcol['red'], $tcol['green'], $tcol['blue']);
  7584. imagefill($new_image, 0, 0, $tid);
  7585. imagecolortransparent($new_image, $tid);
  7586. return $new_image;
  7587. }
  7588. /**
  7589. * Extract info from a JPEG file without using the GD library.
  7590. * @param $file (string) image file to parse
  7591. * @return array structure containing the image data
  7592. * @protected
  7593. */
  7594. protected function _parsejpeg($file) {
  7595. $a = getimagesize($file);
  7596. if (empty($a)) {
  7597. $this->Error('Missing or incorrect image file: '.$file);
  7598. }
  7599. if ($a[2] != 2) {
  7600. $this->Error('Not a JPEG file: '.$file);
  7601. }
  7602. // bits per pixel
  7603. $bpc = isset($a['bits']) ? intval($a['bits']) : 8;
  7604. // number of image channels
  7605. if (!isset($a['channels'])) {
  7606. $channels = 3;
  7607. } else {
  7608. $channels = intval($a['channels']);
  7609. }
  7610. // default colour space
  7611. switch ($channels) {
  7612. case 1: {
  7613. $colspace = 'DeviceGray';
  7614. break;
  7615. }
  7616. case 3: {
  7617. $colspace = 'DeviceRGB';
  7618. break;
  7619. }
  7620. case 4: {
  7621. $colspace = 'DeviceCMYK';
  7622. break;
  7623. }
  7624. default: {
  7625. $channels = 3;
  7626. $colspace = 'DeviceRGB';
  7627. break;
  7628. }
  7629. }
  7630. // get file content
  7631. $data = file_get_contents($file);
  7632. // check for embedded ICC profile
  7633. $icc = array();
  7634. $offset = 0;
  7635. while (($pos = strpos($data, "ICC_PROFILE\0", $offset)) !== false) {
  7636. // get ICC sequence length
  7637. $length = ($this->_getUSHORT($data, ($pos - 2)) - 16);
  7638. // marker sequence number
  7639. $msn = max(1, ord($data[($pos + 12)]));
  7640. // number of markers (total of APP2 used)
  7641. $nom = max(1, ord($data[($pos + 13)]));
  7642. // get sequence segment
  7643. $icc[($msn - 1)] = substr($data, ($pos + 14), $length);
  7644. // move forward to next sequence
  7645. $offset = ($pos + 14 + $length);
  7646. }
  7647. // order and compact ICC segments
  7648. if (count($icc) > 0) {
  7649. ksort($icc);
  7650. $icc = implode('', $icc);
  7651. if ((ord($icc{36}) != 0x61) OR (ord($icc{37}) != 0x63) OR (ord($icc{38}) != 0x73) OR (ord($icc{39}) != 0x70)) {
  7652. // invalid ICC profile
  7653. $icc = false;
  7654. }
  7655. } else {
  7656. $icc = false;
  7657. }
  7658. return array('w' => $a[0], 'h' => $a[1], 'ch' => $channels, 'icc' => $icc, 'cs' => $colspace, 'bpc' => $bpc, 'f' => 'DCTDecode', 'data' => $data);
  7659. }
  7660. /**
  7661. * Extract info from a PNG file without using the GD library.
  7662. * @param $file (string) image file to parse
  7663. * @return array structure containing the image data
  7664. * @protected
  7665. */
  7666. protected function _parsepng($file) {
  7667. $f = fopen($file, 'rb');
  7668. if ($f === false) {
  7669. $this->Error('Can\'t open image file: '.$file);
  7670. }
  7671. //Check signature
  7672. if (fread($f, 8) != chr(137).'PNG'.chr(13).chr(10).chr(26).chr(10)) {
  7673. $this->Error('Not a PNG file: '.$file);
  7674. }
  7675. //Read header chunk
  7676. fread($f, 4);
  7677. if (fread($f, 4) != 'IHDR') {
  7678. $this->Error('Incorrect PNG file: '.$file);
  7679. }
  7680. $w = $this->_freadint($f);
  7681. $h = $this->_freadint($f);
  7682. $bpc = ord(fread($f, 1));
  7683. if ($bpc > 8) {
  7684. //$this->Error('16-bit depth not supported: '.$file);
  7685. fclose($f);
  7686. return false;
  7687. }
  7688. $ct = ord(fread($f, 1));
  7689. if ($ct == 0) {
  7690. $colspace = 'DeviceGray';
  7691. } elseif ($ct == 2) {
  7692. $colspace = 'DeviceRGB';
  7693. } elseif ($ct == 3) {
  7694. $colspace = 'Indexed';
  7695. } else {
  7696. // alpha channel
  7697. fclose($f);
  7698. return 'pngalpha';
  7699. }
  7700. if (ord(fread($f, 1)) != 0) {
  7701. //$this->Error('Unknown compression method: '.$file);
  7702. fclose($f);
  7703. return false;
  7704. }
  7705. if (ord(fread($f, 1)) != 0) {
  7706. //$this->Error('Unknown filter method: '.$file);
  7707. fclose($f);
  7708. return false;
  7709. }
  7710. if (ord(fread($f, 1)) != 0) {
  7711. //$this->Error('Interlacing not supported: '.$file);
  7712. fclose($f);
  7713. return false;
  7714. }
  7715. fread($f, 4);
  7716. $channels = ($ct == 2 ? 3 : 1);
  7717. $parms = '/DecodeParms << /Predictor 15 /Colors '.$channels.' /BitsPerComponent '.$bpc.' /Columns '.$w.' >>';
  7718. //Scan chunks looking for palette, transparency and image data
  7719. $pal = '';
  7720. $trns = '';
  7721. $data = '';
  7722. $icc = false;
  7723. do {
  7724. $n = $this->_freadint($f);
  7725. $type = fread($f, 4);
  7726. if ($type == 'PLTE') {
  7727. // read palette
  7728. $pal = $this->rfread($f, $n);
  7729. fread($f, 4);
  7730. } elseif ($type == 'tRNS') {
  7731. // read transparency info
  7732. $t = $this->rfread($f, $n);
  7733. if ($ct == 0) {
  7734. $trns = array(ord($t{1}));
  7735. } elseif ($ct == 2) {
  7736. $trns = array(ord($t{1}), ord($t{3}), ord($t{5}));
  7737. } else {
  7738. $pos = strpos($t, chr(0));
  7739. if ($pos !== false) {
  7740. $trns = array($pos);
  7741. }
  7742. }
  7743. fread($f, 4);
  7744. } elseif ($type == 'IDAT') {
  7745. // read image data block
  7746. $data .= $this->rfread($f, $n);
  7747. fread($f, 4);
  7748. } elseif ($type == 'iCCP') {
  7749. // skip profile name and null separator
  7750. $len = 0;
  7751. while ((ord(fread($f, 1)) > 0) AND ($len < 80)) {
  7752. ++$len;
  7753. }
  7754. // get compression method
  7755. if (ord(fread($f, 1)) != 0) {
  7756. //$this->Error('Unknown filter method: '.$file);
  7757. fclose($f);
  7758. return false;
  7759. }
  7760. // read ICC Color Profile
  7761. $icc = $this->rfread($f, ($n - $len - 2));
  7762. // decompress profile
  7763. $icc = gzuncompress($icc);
  7764. fread($f, 4);
  7765. } elseif ($type == 'IEND') {
  7766. break;
  7767. } else {
  7768. $this->rfread($f, $n + 4);
  7769. }
  7770. } while ($n);
  7771. if (($colspace == 'Indexed') AND (empty($pal))) {
  7772. //$this->Error('Missing palette in '.$file);
  7773. fclose($f);
  7774. return false;
  7775. }
  7776. fclose($f);
  7777. return array('w' => $w, 'h' => $h, 'ch' => $channels, 'icc' => $icc, 'cs' => $colspace, 'bpc' => $bpc, 'f' => 'FlateDecode', 'parms' => $parms, 'pal' => $pal, 'trns' => $trns, 'data' => $data);
  7778. }
  7779. /**
  7780. * Binary-safe and URL-safe file read.
  7781. * Reads up to length bytes from the file pointer referenced by handle. Reading stops as soon as one of the following conditions is met: length bytes have been read; EOF (end of file) is reached.
  7782. * @param $handle (resource)
  7783. * @param $length (int)
  7784. * @return Returns the read string or FALSE in case of error.
  7785. * @author Nicola Asuni
  7786. * @protected
  7787. * @since 4.5.027 (2009-03-16)
  7788. */
  7789. protected function rfread($handle, $length) {
  7790. $data = fread($handle, $length);
  7791. if ($data === false) {
  7792. return false;
  7793. }
  7794. $rest = $length - strlen($data);
  7795. if ($rest > 0) {
  7796. $data .= $this->rfread($handle, $rest);
  7797. }
  7798. return $data;
  7799. }
  7800. /**
  7801. * Extract info from a PNG image with alpha channel using the GD library.
  7802. * @param $file (string) Name of the file containing the image.
  7803. * @param $x (float) Abscissa of the upper-left corner.
  7804. * @param $y (float) Ordinate of the upper-left corner.
  7805. * @param $wpx (float) Original width of the image in pixels.
  7806. * @param $hpx (float) original height of the image in pixels.
  7807. * @param $w (float) Width of the image in the page. If not specified or equal to zero, it is automatically calculated.
  7808. * @param $h (float) Height of the image in the page. If not specified or equal to zero, it is automatically calculated.
  7809. * @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.
  7810. * @param $link (mixed) URL or identifier returned by AddLink().
  7811. * @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>
  7812. * @param $resize (boolean) If true resize (reduce) the image to fit $w and $h (requires GD library).
  7813. * @param $dpi (int) dot-per-inch resolution used on resize
  7814. * @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>
  7815. * @param $filehash (string) File hash used to build unique file names.
  7816. * @author Nicola Asuni
  7817. * @protected
  7818. * @since 4.3.007 (2008-12-04)
  7819. * @see Image()
  7820. */
  7821. protected function ImagePngAlpha($file, $x, $y, $wpx, $hpx, $w, $h, $type, $link, $align, $resize, $dpi, $palign, $filehash='') {
  7822. if (empty($filehash)) {
  7823. $filehash = md5($file);
  7824. }
  7825. // create temp image file (without alpha channel)
  7826. $tempfile_plain = K_PATH_CACHE.'mskp_'.$filehash;
  7827. // create temp alpha file
  7828. $tempfile_alpha = K_PATH_CACHE.'mska_'.$filehash;
  7829. if (extension_loaded('imagick')) { // ImageMagick extension
  7830. // ImageMagick library
  7831. $img = new Imagick();
  7832. $img->readImage($file);
  7833. // clone image object
  7834. $imga = $img->clone();
  7835. // extract alpha channel
  7836. $img->separateImageChannel(8); // 8 = (imagick::CHANNEL_ALPHA | imagick::CHANNEL_OPACITY | imagick::CHANNEL_MATTE);
  7837. $img->negateImage(true);
  7838. $img->setImageFormat('png');
  7839. $img->writeImage($tempfile_alpha);
  7840. // remove alpha channel
  7841. $imga->separateImageChannel(39); // 39 = (imagick::CHANNEL_ALL & ~(imagick::CHANNEL_ALPHA | imagick::CHANNEL_OPACITY | imagick::CHANNEL_MATTE));
  7842. $imga->setImageFormat('png');
  7843. $imga->writeImage($tempfile_plain);
  7844. } elseif (function_exists('imagecreatefrompng')) { // GD extension
  7845. // generate images
  7846. $img = imagecreatefrompng($file);
  7847. $imgalpha = imagecreate($wpx, $hpx);
  7848. // generate gray scale palette (0 -> 255)
  7849. for ($c = 0; $c < 256; ++$c) {
  7850. ImageColorAllocate($imgalpha, $c, $c, $c);
  7851. }
  7852. // extract alpha channel
  7853. for ($xpx = 0; $xpx < $wpx; ++$xpx) {
  7854. for ($ypx = 0; $ypx < $hpx; ++$ypx) {
  7855. $color = imagecolorat($img, $xpx, $ypx);
  7856. $alpha = ($color >> 24); // shifts off the first 24 bits (where 8x3 are used for each color), and returns the remaining 7 allocated bits (commonly used for alpha)
  7857. $alpha = (((127 - $alpha) / 127) * 255); // GD alpha is only 7 bit (0 -> 127)
  7858. $alpha = $this->getGDgamma($alpha); // correct gamma
  7859. imagesetpixel($imgalpha, $xpx, $ypx, $alpha);
  7860. }
  7861. }
  7862. imagepng($imgalpha, $tempfile_alpha);
  7863. imagedestroy($imgalpha);
  7864. // extract image without alpha channel
  7865. $imgplain = imagecreatetruecolor($wpx, $hpx);
  7866. imagecopy($imgplain, $img, 0, 0, 0, 0, $wpx, $hpx);
  7867. imagepng($imgplain, $tempfile_plain);
  7868. imagedestroy($imgplain);
  7869. } else {
  7870. $this->Error('TCPDF requires the Imagick or GD extension to handle PNG images with alpha channel.');
  7871. }
  7872. // embed mask image
  7873. $imgmask = $this->Image($tempfile_alpha, $x, $y, $w, $h, 'PNG', '', '', $resize, $dpi, '', true, false);
  7874. // embed image, masked with previously embedded mask
  7875. $this->Image($tempfile_plain, $x, $y, $w, $h, $type, $link, $align, $resize, $dpi, $palign, false, $imgmask);
  7876. // remove temp files
  7877. unlink($tempfile_alpha);
  7878. unlink($tempfile_plain);
  7879. }
  7880. /**
  7881. * Correct the gamma value to be used with GD library
  7882. * @param $v (float) the gamma value to be corrected
  7883. * @protected
  7884. * @since 4.3.007 (2008-12-04)
  7885. */
  7886. protected function getGDgamma($v) {
  7887. return (pow(($v / 255), 2.2) * 255);
  7888. }
  7889. /**
  7890. * Performs a line break.
  7891. * The current abscissa goes back to the left margin and the ordinate increases by the amount passed in parameter.
  7892. * @param $h (float) The height of the break. By default, the value equals the height of the last printed cell.
  7893. * @param $cell (boolean) if true add the current left (or right o for RTL) padding to the X coordinate
  7894. * @public
  7895. * @since 1.0
  7896. * @see Cell()
  7897. */
  7898. public function Ln($h='', $cell=false) {
  7899. 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'])) {
  7900. // revove vertical space from the top of the column
  7901. return;
  7902. }
  7903. if ($cell) {
  7904. if ($this->rtl) {
  7905. $cellpadding = $this->cell_padding['R'];
  7906. } else {
  7907. $cellpadding = $this->cell_padding['L'];
  7908. }
  7909. } else {
  7910. $cellpadding = 0;
  7911. }
  7912. if ($this->rtl) {
  7913. $this->x = $this->w - $this->rMargin - $cellpadding;
  7914. } else {
  7915. $this->x = $this->lMargin + $cellpadding;
  7916. }
  7917. if (is_string($h)) {
  7918. $this->y += $this->lasth;
  7919. } else {
  7920. $this->y += $h;
  7921. }
  7922. $this->newline = true;
  7923. }
  7924. /**
  7925. * Returns the relative X value of current position.
  7926. * The value is relative to the left border for LTR languages and to the right border for RTL languages.
  7927. * @return float
  7928. * @public
  7929. * @since 1.2
  7930. * @see SetX(), GetY(), SetY()
  7931. */
  7932. public function GetX() {
  7933. //Get x position
  7934. if ($this->rtl) {
  7935. return ($this->w - $this->x);
  7936. } else {
  7937. return $this->x;
  7938. }
  7939. }
  7940. /**
  7941. * Returns the absolute X value of current position.
  7942. * @return float
  7943. * @public
  7944. * @since 1.2
  7945. * @see SetX(), GetY(), SetY()
  7946. */
  7947. public function GetAbsX() {
  7948. return $this->x;
  7949. }
  7950. /**
  7951. * Returns the ordinate of the current position.
  7952. * @return float
  7953. * @public
  7954. * @since 1.0
  7955. * @see SetY(), GetX(), SetX()
  7956. */
  7957. public function GetY() {
  7958. return $this->y;
  7959. }
  7960. /**
  7961. * Defines the abscissa of the current position.
  7962. * If the passed value is negative, it is relative to the right of the page (or left if language is RTL).
  7963. * @param $x (float) The value of the abscissa.
  7964. * @param $rtloff (boolean) if true always uses the page top-left corner as origin of axis.
  7965. * @public
  7966. * @since 1.2
  7967. * @see GetX(), GetY(), SetY(), SetXY()
  7968. */
  7969. public function SetX($x, $rtloff=false) {
  7970. if (!$rtloff AND $this->rtl) {
  7971. if ($x >= 0) {
  7972. $this->x = $this->w - $x;
  7973. } else {
  7974. $this->x = abs($x);
  7975. }
  7976. } else {
  7977. if ($x >= 0) {
  7978. $this->x = $x;
  7979. } else {
  7980. $this->x = $this->w + $x;
  7981. }
  7982. }
  7983. if ($this->x < 0) {
  7984. $this->x = 0;
  7985. }
  7986. if ($this->x > $this->w) {
  7987. $this->x = $this->w;
  7988. }
  7989. }
  7990. /**
  7991. * Moves the current abscissa back to the left margin and sets the ordinate.
  7992. * If the passed value is negative, it is relative to the bottom of the page.
  7993. * @param $y (float) The value of the ordinate.
  7994. * @param $resetx (bool) if true (default) reset the X position.
  7995. * @param $rtloff (boolean) if true always uses the page top-left corner as origin of axis.
  7996. * @public
  7997. * @since 1.0
  7998. * @see GetX(), GetY(), SetY(), SetXY()
  7999. */
  8000. public function SetY($y, $resetx=true, $rtloff=false) {
  8001. if ($resetx) {
  8002. //reset x
  8003. if (!$rtloff AND $this->rtl) {
  8004. $this->x = $this->w - $this->rMargin;
  8005. } else {
  8006. $this->x = $this->lMargin;
  8007. }
  8008. }
  8009. if ($y >= 0) {
  8010. $this->y = $y;
  8011. } else {
  8012. $this->y = $this->h + $y;
  8013. }
  8014. if ($this->y < 0) {
  8015. $this->y = 0;
  8016. }
  8017. if ($this->y > $this->h) {
  8018. $this->y = $this->h;
  8019. }
  8020. }
  8021. /**
  8022. * Defines the abscissa and ordinate of the current position.
  8023. * If the passed values are negative, they are relative respectively to the right and bottom of the page.
  8024. * @param $x (float) The value of the abscissa.
  8025. * @param $y (float) The value of the ordinate.
  8026. * @param $rtloff (boolean) if true always uses the page top-left corner as origin of axis.
  8027. * @public
  8028. * @since 1.2
  8029. * @see SetX(), SetY()
  8030. */
  8031. public function SetXY($x, $y, $rtloff=false) {
  8032. $this->SetY($y, false, $rtloff);
  8033. $this->SetX($x, $rtloff);
  8034. }
  8035. /**
  8036. * Ouput input data and compress it if possible.
  8037. * @param $data (string) Data to output.
  8038. * @param $length (int) Data length in bytes.
  8039. * @protected
  8040. * @since 5.9.086
  8041. */
  8042. protected function sendOutputData($data, $length) {
  8043. if (!isset($_SERVER['HTTP_ACCEPT_ENCODING']) OR empty($_SERVER['HTTP_ACCEPT_ENCODING'])) {
  8044. // the content length may vary if the server is using compression
  8045. header('Content-Length: '.$length);
  8046. }
  8047. echo $data;
  8048. }
  8049. /**
  8050. * Send the document to a given destination: string, local file or browser.
  8051. * In the last case, the plug-in may be used (if present) or a download ("Save as" dialog box) may be forced.<br />
  8052. * The method first calls Close() if necessary to terminate the document.
  8053. * @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.
  8054. * @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>
  8055. * @public
  8056. * @since 1.0
  8057. * @see Close()
  8058. */
  8059. public function Output($name='doc.pdf', $dest='I') {
  8060. //Output PDF to some destination
  8061. //Finish document if necessary
  8062. if ($this->state < 3) {
  8063. $this->Close();
  8064. }
  8065. //Normalize parameters
  8066. if (is_bool($dest)) {
  8067. $dest = $dest ? 'D' : 'F';
  8068. }
  8069. $dest = strtoupper($dest);
  8070. if ($dest{0} != 'F') {
  8071. $name = preg_replace('/[\s]+/', '_', $name);
  8072. $name = preg_replace('/[^a-zA-Z0-9_\.-]/', '', $name);
  8073. }
  8074. if ($this->sign) {
  8075. // *** apply digital signature to the document ***
  8076. // get the document content
  8077. $pdfdoc = $this->getBuffer();
  8078. // remove last newline
  8079. $pdfdoc = substr($pdfdoc, 0, -1);
  8080. // Remove the original buffer
  8081. if (isset($this->diskcache) AND $this->diskcache) {
  8082. // remove buffer file from cache
  8083. unlink($this->buffer);
  8084. }
  8085. unset($this->buffer);
  8086. // remove filler space
  8087. $byterange_string_len = strlen($this->byterange_string);
  8088. // define the ByteRange
  8089. $byte_range = array();
  8090. $byte_range[0] = 0;
  8091. $byte_range[1] = strpos($pdfdoc, $this->byterange_string) + $byterange_string_len + 10;
  8092. $byte_range[2] = $byte_range[1] + $this->signature_max_length + 2;
  8093. $byte_range[3] = strlen($pdfdoc) - $byte_range[2];
  8094. $pdfdoc = substr($pdfdoc, 0, $byte_range[1]).substr($pdfdoc, $byte_range[2]);
  8095. // replace the ByteRange
  8096. $byterange = sprintf('/ByteRange[0 %u %u %u]', $byte_range[1], $byte_range[2], $byte_range[3]);
  8097. $byterange .= str_repeat(' ', ($byterange_string_len - strlen($byterange)));
  8098. $pdfdoc = str_replace($this->byterange_string, $byterange, $pdfdoc);
  8099. // write the document to a temporary folder
  8100. $tempdoc = tempnam(K_PATH_CACHE, 'tmppdf_');
  8101. $f = fopen($tempdoc, 'wb');
  8102. if (!$f) {
  8103. $this->Error('Unable to create temporary file: '.$tempdoc);
  8104. }
  8105. $pdfdoc_length = strlen($pdfdoc);
  8106. fwrite($f, $pdfdoc, $pdfdoc_length);
  8107. fclose($f);
  8108. // get digital signature via openssl library
  8109. $tempsign = tempnam(K_PATH_CACHE, 'tmpsig_');
  8110. if (empty($this->signature_data['extracerts'])) {
  8111. openssl_pkcs7_sign($tempdoc, $tempsign, $this->signature_data['signcert'], array($this->signature_data['privkey'], $this->signature_data['password']), array(), PKCS7_BINARY | PKCS7_DETACHED);
  8112. } else {
  8113. 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']);
  8114. }
  8115. unlink($tempdoc);
  8116. // read signature
  8117. $signature = file_get_contents($tempsign);
  8118. unlink($tempsign);
  8119. // extract signature
  8120. $signature = substr($signature, $pdfdoc_length);
  8121. $signature = substr($signature, (strpos($signature, "%%EOF\n\n------") + 13));
  8122. $tmparr = explode("\n\n", $signature);
  8123. $signature = $tmparr[1];
  8124. unset($tmparr);
  8125. // decode signature
  8126. $signature = base64_decode(trim($signature));
  8127. // convert signature to hex
  8128. $signature = current(unpack('H*', $signature));
  8129. $signature = str_pad($signature, $this->signature_max_length, '0');
  8130. // disable disk caching
  8131. $this->diskcache = false;
  8132. // Add signature to the document
  8133. $this->buffer = substr($pdfdoc, 0, $byte_range[1]).'<'.$signature.'>'.substr($pdfdoc, $byte_range[1]);
  8134. $this->bufferlen = strlen($this->buffer);
  8135. }
  8136. switch($dest) {
  8137. case 'I': {
  8138. // Send PDF to the standard output
  8139. if (ob_get_contents()) {
  8140. $this->Error('Some data has already been output, can\'t send PDF file');
  8141. }
  8142. if (php_sapi_name() != 'cli') {
  8143. // send output to a browser
  8144. header('Content-Type: application/pdf');
  8145. if (headers_sent()) {
  8146. $this->Error('Some data has already been output to browser, can\'t send PDF file');
  8147. }
  8148. header('Cache-Control: private, must-revalidate, post-check=0, pre-check=0, max-age=1');
  8149. //header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
  8150. header('Pragma: public');
  8151. header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  8152. header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  8153. header('Content-Disposition: inline; filename="'.basename($name).'";');
  8154. $this->sendOutputData($this->getBuffer(), $this->bufferlen);
  8155. } else {
  8156. echo $this->getBuffer();
  8157. }
  8158. break;
  8159. }
  8160. case 'D': {
  8161. // download PDF as file
  8162. if (ob_get_contents()) {
  8163. $this->Error('Some data has already been output, can\'t send PDF file');
  8164. }
  8165. header('Content-Description: File Transfer');
  8166. if (headers_sent()) {
  8167. $this->Error('Some data has already been output to browser, can\'t send PDF file');
  8168. }
  8169. header('Cache-Control: private, must-revalidate, post-check=0, pre-check=0, max-age=1');
  8170. //header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
  8171. header('Pragma: public');
  8172. header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  8173. header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  8174. // force download dialog
  8175. if (strpos(php_sapi_name(), 'cgi') === false) {
  8176. header('Content-Type: application/force-download');
  8177. header('Content-Type: application/octet-stream', false);
  8178. header('Content-Type: application/download', false);
  8179. header('Content-Type: application/pdf', false);
  8180. } else {
  8181. header('Content-Type: application/pdf');
  8182. }
  8183. // use the Content-Disposition header to supply a recommended filename
  8184. header('Content-Disposition: attachment; filename="'.basename($name).'";');
  8185. header('Content-Transfer-Encoding: binary');
  8186. $this->sendOutputData($this->getBuffer(), $this->bufferlen);
  8187. break;
  8188. }
  8189. case 'F':
  8190. case 'FI':
  8191. case 'FD': {
  8192. // save PDF to a local file
  8193. if ($this->diskcache) {
  8194. copy($this->buffer, $name);
  8195. } else {
  8196. $f = fopen($name, 'wb');
  8197. if (!$f) {
  8198. $this->Error('Unable to create output file: '.$name);
  8199. }
  8200. fwrite($f, $this->getBuffer(), $this->bufferlen);
  8201. fclose($f);
  8202. }
  8203. if ($dest == 'FI') {
  8204. // send headers to browser
  8205. header('Content-Type: application/pdf');
  8206. header('Cache-Control: private, must-revalidate, post-check=0, pre-check=0, max-age=1');
  8207. //header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
  8208. header('Pragma: public');
  8209. header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  8210. header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  8211. header('Content-Disposition: inline; filename="'.basename($name).'";');
  8212. $this->sendOutputData(file_get_contents($name), filesize($name));
  8213. } elseif ($dest == 'FD') {
  8214. // send headers to browser
  8215. if (ob_get_contents()) {
  8216. $this->Error('Some data has already been output, can\'t send PDF file');
  8217. }
  8218. header('Content-Description: File Transfer');
  8219. if (headers_sent()) {
  8220. $this->Error('Some data has already been output to browser, can\'t send PDF file');
  8221. }
  8222. header('Cache-Control: private, must-revalidate, post-check=0, pre-check=0, max-age=1');
  8223. header('Pragma: public');
  8224. header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  8225. header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  8226. // force download dialog
  8227. if (strpos(php_sapi_name(), 'cgi') === false) {
  8228. header('Content-Type: application/force-download');
  8229. header('Content-Type: application/octet-stream', false);
  8230. header('Content-Type: application/download', false);
  8231. header('Content-Type: application/pdf', false);
  8232. } else {
  8233. header('Content-Type: application/pdf');
  8234. }
  8235. // use the Content-Disposition header to supply a recommended filename
  8236. header('Content-Disposition: attachment; filename="'.basename($name).'";');
  8237. header('Content-Transfer-Encoding: binary');
  8238. $this->sendOutputData(file_get_contents($name), filesize($name));
  8239. }
  8240. break;
  8241. }
  8242. case 'E': {
  8243. // return PDF as base64 mime multi-part email attachment (RFC 2045)
  8244. $retval = 'Content-Type: application/pdf;'."\r\n";
  8245. $retval .= ' name="'.$name.'"'."\r\n";
  8246. $retval .= 'Content-Transfer-Encoding: base64'."\r\n";
  8247. $retval .= 'Content-Disposition: attachment;'."\r\n";
  8248. $retval .= ' filename="'.$name.'"'."\r\n\r\n";
  8249. $retval .= chunk_split(base64_encode($this->getBuffer()), 76, "\r\n");
  8250. return $retval;
  8251. }
  8252. case 'S': {
  8253. // returns PDF as a string
  8254. return $this->getBuffer();
  8255. }
  8256. default: {
  8257. $this->Error('Incorrect output destination: '.$dest);
  8258. }
  8259. }
  8260. return '';
  8261. }
  8262. /**
  8263. * Unset all class variables except the following critical variables: internal_encoding, state, bufferlen, buffer and diskcache.
  8264. * @param $destroyall (boolean) if true destroys all class variables, otherwise preserves critical variables.
  8265. * @param $preserve_objcopy (boolean) if true preserves the objcopy variable
  8266. * @public
  8267. * @since 4.5.016 (2009-02-24)
  8268. */
  8269. public function _destroy($destroyall=false, $preserve_objcopy=false) {
  8270. if ($destroyall AND isset($this->diskcache) AND $this->diskcache AND (!$preserve_objcopy) AND (!$this->empty_string($this->buffer))) {
  8271. // remove buffer file from cache
  8272. unlink($this->buffer);
  8273. }
  8274. foreach (array_keys(get_object_vars($this)) as $val) {
  8275. if ($destroyall OR (
  8276. ($val != 'internal_encoding')
  8277. AND ($val != 'state')
  8278. AND ($val != 'bufferlen')
  8279. AND ($val != 'buffer')
  8280. AND ($val != 'diskcache')
  8281. AND ($val != 'sign')
  8282. AND ($val != 'signature_data')
  8283. AND ($val != 'signature_max_length')
  8284. AND ($val != 'byterange_string')
  8285. )) {
  8286. if ((!$preserve_objcopy OR ($val != 'objcopy')) AND isset($this->$val)) {
  8287. unset($this->$val);
  8288. }
  8289. }
  8290. }
  8291. }
  8292. /**
  8293. * Check for locale-related bug
  8294. * @protected
  8295. */
  8296. protected function _dochecks() {
  8297. //Check for locale-related bug
  8298. if (1.1 == 1) {
  8299. $this->Error('Don\'t alter the locale before including class file');
  8300. }
  8301. //Check for decimal separator
  8302. if (sprintf('%.1F', 1.0) != '1.0') {
  8303. setlocale(LC_NUMERIC, 'C');
  8304. }
  8305. }
  8306. /**
  8307. * Return fonts path
  8308. * @return string
  8309. * @protected
  8310. */
  8311. protected function _getfontpath() {
  8312. if (!defined('K_PATH_FONTS') AND is_dir(dirname(__FILE__).'/fonts')) {
  8313. define('K_PATH_FONTS', dirname(__FILE__).'/fonts/');
  8314. }
  8315. return defined('K_PATH_FONTS') ? K_PATH_FONTS : '';
  8316. }
  8317. /**
  8318. * Return an array containing variations for the basic page number alias.
  8319. * @param $a (string) Base alias.
  8320. * @return array of page number aliases
  8321. * @protected
  8322. */
  8323. protected function getInternalPageNumberAliases($a= '') {
  8324. $alias = array();
  8325. // build array of Unicode + ASCII variants (the order is important)
  8326. $alias = array('u' => array(), 'a' => array());
  8327. $u = '{'.$a.'}';
  8328. $alias['u'][] = $this->_escape($u);
  8329. if ($this->isunicode) {
  8330. $alias['u'][] = $this->_escape($this->UTF8ToLatin1($u));
  8331. $alias['u'][] = $this->_escape($this->utf8StrRev($u, false, $this->tmprtl));
  8332. $alias['a'][] = $this->_escape($this->UTF8ToLatin1($a));
  8333. $alias['a'][] = $this->_escape($this->utf8StrRev($a, false, $this->tmprtl));
  8334. }
  8335. $alias['a'][] = $this->_escape($a);
  8336. return $alias;
  8337. }
  8338. /**
  8339. * Return an array containing all internal page aliases.
  8340. * @return array of page number aliases
  8341. * @protected
  8342. */
  8343. protected function getAllInternalPageNumberAliases() {
  8344. $basic_alias = array($this->alias_tot_pages, $this->alias_num_page, $this->alias_group_tot_pages, $this->alias_group_num_page, $this->alias_right_shift);
  8345. $pnalias = array();
  8346. foreach($basic_alias as $k => $a) {
  8347. $pnalias[$k] = $this->getInternalPageNumberAliases($a);
  8348. }
  8349. return $pnalias;
  8350. }
  8351. /**
  8352. * Replace page number aliases with number.
  8353. * @param $page (string) Page content.
  8354. * @param $replace (array) Array of replacements (array keys are replacement strings, values are alias arrays).
  8355. * @param $diff (int) If passed, this will be set to the total char number difference between alias and replacements.
  8356. * @return replaced page content and updated $diff parameter as array.
  8357. * @protected
  8358. */
  8359. protected function replacePageNumAliases($page, $replace, $diff=0) {
  8360. foreach ($replace as $rep) {
  8361. foreach ($rep[3] as $a) {
  8362. if (strpos($page, $a) !== false) {
  8363. $page = str_replace($a, $rep[0], $page);
  8364. $diff += ($rep[2] - $rep[1]);
  8365. }
  8366. }
  8367. }
  8368. return array($page, $diff);
  8369. }
  8370. /**
  8371. * Replace right shift page number aliases with spaces to correct right alignment.
  8372. * This works perfectly only when using monospaced fonts.
  8373. * @param $page (string) Page content.
  8374. * @param $aliases (array) Array of page aliases.
  8375. * @param $diff (int) initial difference to add.
  8376. * @return replaced page content.
  8377. * @protected
  8378. */
  8379. protected function replaceRightShiftPageNumAliases($page, $aliases, $diff) {
  8380. foreach ($aliases as $type => $alias) {
  8381. foreach ($alias as $a) {
  8382. // find position of compensation factor
  8383. $startnum = (strpos($a, ':') + 1);
  8384. $a = substr($a, 0, $startnum);
  8385. if (($pos = strpos($page, $a)) !== false) {
  8386. // end of alias
  8387. $endnum = strpos($page, '}', $pos);
  8388. // string to be replaced
  8389. $aa = substr($page, $pos, ($endnum - $pos + 1));
  8390. // get compensation factor
  8391. $ratio = substr($page, ($pos + $startnum), ($endnum - $pos - $startnum));
  8392. $ratio = preg_replace('/[^0-9\.]/', '', $ratio);
  8393. $ratio = floatval($ratio);
  8394. if ($type == 'u') {
  8395. $chrdiff = floor(($diff + 12) * $ratio);
  8396. $shift = str_repeat(' ', $chrdiff);
  8397. $shift = $this->UTF8ToUTF16BE($shift, false);
  8398. } else {
  8399. $chrdiff = floor(($diff + 11) * $ratio);
  8400. $shift = str_repeat(' ', $chrdiff);
  8401. }
  8402. $page = str_replace($aa, $shift, $page);
  8403. }
  8404. }
  8405. }
  8406. return $page;
  8407. }
  8408. /**
  8409. * Output pages (and replace page number aliases).
  8410. * @protected
  8411. */
  8412. protected function _putpages() {
  8413. $filter = ($this->compress) ? '/Filter /FlateDecode ' : '';
  8414. // get internal aliases for page numbers
  8415. $pnalias = $this->getAllInternalPageNumberAliases();
  8416. $num_pages = $this->numpages;
  8417. $ptpa = $this->formatPageNumber(($this->starting_page_number + $num_pages - 1));
  8418. $ptpu = $this->UTF8ToUTF16BE($ptpa, false);
  8419. $ptp_num_chars = $this->GetNumChars($ptpa);
  8420. $pagegroupnum = 0;
  8421. $groupnum = 0;
  8422. $ptgu = 1;
  8423. $ptga = 1;
  8424. for ($n = 1; $n <= $num_pages; ++$n) {
  8425. // get current page
  8426. $temppage = $this->getPageBuffer($n);
  8427. $pagelen = strlen($temppage);
  8428. // set replacements for total pages number
  8429. $pnpa = $this->formatPageNumber(($this->starting_page_number + $n - 1));
  8430. $pnpu = $this->UTF8ToUTF16BE($pnpa, false);
  8431. $pnp_num_chars = $this->GetNumChars($pnpa);
  8432. $pdiff = 0; // difference used for right shift alignment of page numbers
  8433. $gdiff = 0; // difference used for right shift alignment of page group numbers
  8434. if (!empty($this->pagegroups)) {
  8435. if (isset($this->newpagegroup[$n])) {
  8436. $pagegroupnum = 0;
  8437. ++$groupnum;
  8438. $ptga = $this->formatPageNumber($this->pagegroups[$groupnum]);
  8439. $ptgu = $this->UTF8ToUTF16BE($ptga, false);
  8440. $ptg_num_chars = $this->GetNumChars($ptga);
  8441. }
  8442. ++$pagegroupnum;
  8443. $pnga = $this->formatPageNumber($pagegroupnum);
  8444. $pngu = $this->UTF8ToUTF16BE($pnga, false);
  8445. $png_num_chars = $this->GetNumChars($pnga);
  8446. // replace page numbers
  8447. $replace = array();
  8448. $replace[] = array($ptgu, $ptg_num_chars, 9, $pnalias[2]['u']);
  8449. $replace[] = array($ptga, $ptg_num_chars, 7, $pnalias[2]['a']);
  8450. $replace[] = array($pngu, $png_num_chars, 9, $pnalias[3]['u']);
  8451. $replace[] = array($pnga, $png_num_chars, 7, $pnalias[3]['a']);
  8452. list($temppage, $gdiff) = $this->replacePageNumAliases($temppage, $replace, $gdiff);
  8453. }
  8454. // replace page numbers
  8455. $replace = array();
  8456. $replace[] = array($ptpu, $ptp_num_chars, 9, $pnalias[0]['u']);
  8457. $replace[] = array($ptpa, $ptp_num_chars, 7, $pnalias[0]['a']);
  8458. $replace[] = array($pnpu, $pnp_num_chars, 9, $pnalias[1]['u']);
  8459. $replace[] = array($pnpa, $pnp_num_chars, 7, $pnalias[1]['a']);
  8460. list($temppage, $pdiff) = $this->replacePageNumAliases($temppage, $replace, $pdiff);
  8461. // replace right shift alias
  8462. $temppage = $this->replaceRightShiftPageNumAliases($temppage, $pnalias[4], max($pdiff, $gdiff));
  8463. // replace EPS marker
  8464. $temppage = str_replace($this->epsmarker, '', $temppage);
  8465. //Page
  8466. $this->page_obj_id[$n] = $this->_newobj();
  8467. $out = '<<';
  8468. $out .= ' /Type /Page';
  8469. $out .= ' /Parent 1 0 R';
  8470. $out .= ' /LastModified '.$this->_datestring();
  8471. $out .= ' /Resources 2 0 R';
  8472. $boxes = array('MediaBox', 'CropBox', 'BleedBox', 'TrimBox', 'ArtBox');
  8473. foreach ($boxes as $box) {
  8474. $out .= ' /'.$box;
  8475. $out .= sprintf(' [%.2F %.2F %.2F %.2F]', $this->pagedim[$n][$box]['llx'], $this->pagedim[$n][$box]['lly'], $this->pagedim[$n][$box]['urx'], $this->pagedim[$n][$box]['ury']);
  8476. }
  8477. if (isset($this->pagedim[$n]['BoxColorInfo']) AND !empty($this->pagedim[$n]['BoxColorInfo'])) {
  8478. $out .= ' /BoxColorInfo <<';
  8479. foreach ($boxes as $box) {
  8480. if (isset($this->pagedim[$n]['BoxColorInfo'][$box])) {
  8481. $out .= ' /'.$box.' <<';
  8482. if (isset($this->pagedim[$n]['BoxColorInfo'][$box]['C'])) {
  8483. $color = $this->pagedim[$n]['BoxColorInfo'][$box]['C'];
  8484. $out .= ' /C [';
  8485. $out .= sprintf(' %.3F %.3F %.3F', $color[0]/255, $color[1]/255, $color[2]/255);
  8486. $out .= ' ]';
  8487. }
  8488. if (isset($this->pagedim[$n]['BoxColorInfo'][$box]['W'])) {
  8489. $out .= ' /W '.($this->pagedim[$n]['BoxColorInfo'][$box]['W'] * $this->k);
  8490. }
  8491. if (isset($this->pagedim[$n]['BoxColorInfo'][$box]['S'])) {
  8492. $out .= ' /S /'.$this->pagedim[$n]['BoxColorInfo'][$box]['S'];
  8493. }
  8494. if (isset($this->pagedim[$n]['BoxColorInfo'][$box]['D'])) {
  8495. $dashes = $this->pagedim[$n]['BoxColorInfo'][$box]['D'];
  8496. $out .= ' /D [';
  8497. foreach ($dashes as $dash) {
  8498. $out .= sprintf(' %.3F', ($dash * $this->k));
  8499. }
  8500. $out .= ' ]';
  8501. }
  8502. $out .= ' >>';
  8503. }
  8504. }
  8505. $out .= ' >>';
  8506. }
  8507. $out .= ' /Contents '.($this->n + 1).' 0 R';
  8508. $out .= ' /Rotate '.$this->pagedim[$n]['Rotate'];
  8509. if (!$this->pdfa_mode) {
  8510. $out .= ' /Group << /Type /Group /S /Transparency /CS /DeviceRGB >>';
  8511. }
  8512. if (isset($this->pagedim[$n]['trans']) AND !empty($this->pagedim[$n]['trans'])) {
  8513. // page transitions
  8514. if (isset($this->pagedim[$n]['trans']['Dur'])) {
  8515. $out .= ' /Dur '.$this->pagedim[$n]['trans']['Dur'];
  8516. }
  8517. $out .= ' /Trans <<';
  8518. $out .= ' /Type /Trans';
  8519. if (isset($this->pagedim[$n]['trans']['S'])) {
  8520. $out .= ' /S /'.$this->pagedim[$n]['trans']['S'];
  8521. }
  8522. if (isset($this->pagedim[$n]['trans']['D'])) {
  8523. $out .= ' /D '.$this->pagedim[$n]['trans']['D'];
  8524. }
  8525. if (isset($this->pagedim[$n]['trans']['Dm'])) {
  8526. $out .= ' /Dm /'.$this->pagedim[$n]['trans']['Dm'];
  8527. }
  8528. if (isset($this->pagedim[$n]['trans']['M'])) {
  8529. $out .= ' /M /'.$this->pagedim[$n]['trans']['M'];
  8530. }
  8531. if (isset($this->pagedim[$n]['trans']['Di'])) {
  8532. $out .= ' /Di '.$this->pagedim[$n]['trans']['Di'];
  8533. }
  8534. if (isset($this->pagedim[$n]['trans']['SS'])) {
  8535. $out .= ' /SS '.$this->pagedim[$n]['trans']['SS'];
  8536. }
  8537. if (isset($this->pagedim[$n]['trans']['B'])) {
  8538. $out .= ' /B '.$this->pagedim[$n]['trans']['B'];
  8539. }
  8540. $out .= ' >>';
  8541. }
  8542. $out .= $this->_getannotsrefs($n);
  8543. $out .= ' /PZ '.$this->pagedim[$n]['PZ'];
  8544. $out .= ' >>';
  8545. $out .= "\n".'endobj';
  8546. $this->_out($out);
  8547. //Page content
  8548. $p = ($this->compress) ? gzcompress($temppage) : $temppage;
  8549. $this->_newobj();
  8550. $p = $this->_getrawstream($p);
  8551. $this->_out('<<'.$filter.'/Length '.strlen($p).'>> stream'."\n".$p."\n".'endstream'."\n".'endobj');
  8552. if ($this->diskcache) {
  8553. // remove temporary files
  8554. unlink($this->pages[$n]);
  8555. }
  8556. }
  8557. //Pages root
  8558. $out = $this->_getobj(1)."\n";
  8559. $out .= '<< /Type /Pages /Kids [';
  8560. foreach($this->page_obj_id as $page_obj) {
  8561. $out .= ' '.$page_obj.' 0 R';
  8562. }
  8563. $out .= ' ] /Count '.$num_pages.' >>';
  8564. $out .= "\n".'endobj';
  8565. $this->_out($out);
  8566. }
  8567. /**
  8568. * Output references to page annotations
  8569. * @param $n (int) page number
  8570. * @protected
  8571. * @author Nicola Asuni
  8572. * @since 4.7.000 (2008-08-29)
  8573. * @deprecated
  8574. */
  8575. protected function _putannotsrefs($n) {
  8576. $this->_out($this->_getannotsrefs($n));
  8577. }
  8578. /**
  8579. * Get references to page annotations.
  8580. * @param $n (int) page number
  8581. * @return string
  8582. * @protected
  8583. * @author Nicola Asuni
  8584. * @since 5.0.010 (2010-05-17)
  8585. */
  8586. protected function _getannotsrefs($n) {
  8587. if (!(isset($this->PageAnnots[$n]) OR ($this->sign AND isset($this->signature_data['cert_type'])))) {
  8588. return '';
  8589. }
  8590. $out = ' /Annots [';
  8591. if (isset($this->PageAnnots[$n])) {
  8592. foreach ($this->PageAnnots[$n] as $key => $val) {
  8593. if (!in_array($val['n'], $this->radio_groups)) {
  8594. $out .= ' '.$val['n'].' 0 R';
  8595. }
  8596. }
  8597. // add radiobutton groups
  8598. if (isset($this->radiobutton_groups[$n])) {
  8599. foreach ($this->radiobutton_groups[$n] as $key => $data) {
  8600. if (isset($data['n'])) {
  8601. $out .= ' '.$data['n'].' 0 R';
  8602. }
  8603. }
  8604. }
  8605. }
  8606. if ($this->sign AND ($n == $this->signature_appearance['page']) AND isset($this->signature_data['cert_type'])) {
  8607. // set reference for signature object
  8608. $out .= ' '.$this->sig_obj_id.' 0 R';
  8609. }
  8610. if (!empty($this->empty_signature_appearance)) {
  8611. foreach ($this->empty_signature_appearance as $esa) {
  8612. if ($esa['page'] == $n) {
  8613. // set reference for empty signature objects
  8614. $out .= ' '.$esa['objid'].' 0 R';
  8615. }
  8616. }
  8617. }
  8618. $out .= ' ]';
  8619. return $out;
  8620. }
  8621. /**
  8622. * Output annotations objects for all pages.
  8623. * !!! THIS METHOD IS NOT YET COMPLETED !!!
  8624. * See section 12.5 of PDF 32000_2008 reference.
  8625. * @protected
  8626. * @author Nicola Asuni
  8627. * @since 4.0.018 (2008-08-06)
  8628. */
  8629. protected function _putannotsobjs() {
  8630. // reset object counter
  8631. for ($n=1; $n <= $this->numpages; ++$n) {
  8632. if (isset($this->PageAnnots[$n])) {
  8633. // set page annotations
  8634. foreach ($this->PageAnnots[$n] as $key => $pl) {
  8635. $annot_obj_id = $this->PageAnnots[$n][$key]['n'];
  8636. // create annotation object for grouping radiobuttons
  8637. if (isset($this->radiobutton_groups[$n][$pl['txt']]) AND is_array($this->radiobutton_groups[$n][$pl['txt']])) {
  8638. $radio_button_obj_id = $this->radiobutton_groups[$n][$pl['txt']]['n'];
  8639. $annots = '<<';
  8640. $annots .= ' /Type /Annot';
  8641. $annots .= ' /Subtype /Widget';
  8642. $annots .= ' /Rect [0 0 0 0]';
  8643. if ($this->radiobutton_groups[$n][$pl['txt']]['#readonly#']) {
  8644. // read only
  8645. $annots .= ' /F 68';
  8646. $annots .= ' /Ff 49153';
  8647. } else {
  8648. $annots .= ' /F 4'; // default print for PDF/A
  8649. $annots .= ' /Ff 49152';
  8650. }
  8651. $annots .= ' /T '.$this->_datastring($pl['txt'], $radio_button_obj_id);
  8652. $annots .= ' /FT /Btn';
  8653. $annots .= ' /Kids [';
  8654. foreach ($this->radiobutton_groups[$n][$pl['txt']] as $key => $data) {
  8655. if (isset($data['kid'])) {
  8656. $annots .= ' '.$data['kid'].' 0 R';
  8657. if ($data['def'] !== 'Off') {
  8658. $defval = $data['def'];
  8659. }
  8660. }
  8661. }
  8662. $annots .= ' ]';
  8663. if (isset($defval)) {
  8664. $annots .= ' /V /'.$defval;
  8665. }
  8666. $annots .= ' >>';
  8667. $this->_out($this->_getobj($radio_button_obj_id)."\n".$annots."\n".'endobj');
  8668. $this->form_obj_id[] = $radio_button_obj_id;
  8669. // store object id to be used on Parent entry of Kids
  8670. $this->radiobutton_groups[$n][$pl['txt']] = $radio_button_obj_id;
  8671. }
  8672. $formfield = false;
  8673. $pl['opt'] = array_change_key_case($pl['opt'], CASE_LOWER);
  8674. $a = $pl['x'] * $this->k;
  8675. $b = $this->pagedim[$n]['h'] - (($pl['y'] + $pl['h']) * $this->k);
  8676. $c = $pl['w'] * $this->k;
  8677. $d = $pl['h'] * $this->k;
  8678. $rect = sprintf('%.2F %.2F %.2F %.2F', $a, $b, $a+$c, $b+$d);
  8679. // create new annotation object
  8680. $annots = '<</Type /Annot';
  8681. $annots .= ' /Subtype /'.$pl['opt']['subtype'];
  8682. $annots .= ' /Rect ['.$rect.']';
  8683. $ft = array('Btn', 'Tx', 'Ch', 'Sig');
  8684. if (isset($pl['opt']['ft']) AND in_array($pl['opt']['ft'], $ft)) {
  8685. $annots .= ' /FT /'.$pl['opt']['ft'];
  8686. $formfield = true;
  8687. }
  8688. $annots .= ' /Contents '.$this->_textstring($pl['txt'], $annot_obj_id);
  8689. $annots .= ' /P '.$this->page_obj_id[$n].' 0 R';
  8690. $annots .= ' /NM '.$this->_datastring(sprintf('%04u-%04u', $n, $key), $annot_obj_id);
  8691. $annots .= ' /M '.$this->_datestring($annot_obj_id);
  8692. if (isset($pl['opt']['f'])) {
  8693. $fval = 0;
  8694. if (is_array($pl['opt']['f'])) {
  8695. foreach ($pl['opt']['f'] as $f) {
  8696. switch (strtolower($f)) {
  8697. case 'invisible': {
  8698. $fval += 1 << 0;
  8699. break;
  8700. }
  8701. case 'hidden': {
  8702. $fval += 1 << 1;
  8703. break;
  8704. }
  8705. case 'print': {
  8706. $fval += 1 << 2;
  8707. break;
  8708. }
  8709. case 'nozoom': {
  8710. $fval += 1 << 3;
  8711. break;
  8712. }
  8713. case 'norotate': {
  8714. $fval += 1 << 4;
  8715. break;
  8716. }
  8717. case 'noview': {
  8718. $fval += 1 << 5;
  8719. break;
  8720. }
  8721. case 'readonly': {
  8722. $fval += 1 << 6;
  8723. break;
  8724. }
  8725. case 'locked': {
  8726. $fval += 1 << 8;
  8727. break;
  8728. }
  8729. case 'togglenoview': {
  8730. $fval += 1 << 9;
  8731. break;
  8732. }
  8733. case 'lockedcontents': {
  8734. $fval += 1 << 10;
  8735. break;
  8736. }
  8737. default: {
  8738. break;
  8739. }
  8740. }
  8741. }
  8742. } else {
  8743. $fval = intval($pl['opt']['f']);
  8744. }
  8745. } else {
  8746. $fval = 4;
  8747. }
  8748. if ($this->pdfa_mode) {
  8749. // force print flag for PDF/A mode
  8750. $fval |= 4;
  8751. }
  8752. $annots .= ' /F '.intval($fval);
  8753. if (isset($pl['opt']['as']) AND is_string($pl['opt']['as'])) {
  8754. $annots .= ' /AS /'.$pl['opt']['as'];
  8755. }
  8756. if (isset($pl['opt']['ap'])) {
  8757. // appearance stream
  8758. $annots .= ' /AP <<';
  8759. if (is_array($pl['opt']['ap'])) {
  8760. foreach ($pl['opt']['ap'] as $apmode => $apdef) {
  8761. // $apmode can be: n = normal; r = rollover; d = down;
  8762. $annots .= ' /'.strtoupper($apmode);
  8763. if (is_array($apdef)) {
  8764. $annots .= ' <<';
  8765. foreach ($apdef as $apstate => $stream) {
  8766. // reference to XObject that define the appearance for this mode-state
  8767. $apsobjid = $this->_putAPXObject($c, $d, $stream);
  8768. $annots .= ' /'.$apstate.' '.$apsobjid.' 0 R';
  8769. }
  8770. $annots .= ' >>';
  8771. } else {
  8772. // reference to XObject that define the appearance for this mode
  8773. $apsobjid = $this->_putAPXObject($c, $d, $apdef);
  8774. $annots .= ' '.$apsobjid.' 0 R';
  8775. }
  8776. }
  8777. } else {
  8778. $annots .= $pl['opt']['ap'];
  8779. }
  8780. $annots .= ' >>';
  8781. }
  8782. if (isset($pl['opt']['bs']) AND (is_array($pl['opt']['bs']))) {
  8783. $annots .= ' /BS <<';
  8784. $annots .= ' /Type /Border';
  8785. if (isset($pl['opt']['bs']['w'])) {
  8786. $annots .= ' /W '.intval($pl['opt']['bs']['w']);
  8787. }
  8788. $bstyles = array('S', 'D', 'B', 'I', 'U');
  8789. if (isset($pl['opt']['bs']['s']) AND in_array($pl['opt']['bs']['s'], $bstyles)) {
  8790. $annots .= ' /S /'.$pl['opt']['bs']['s'];
  8791. }
  8792. if (isset($pl['opt']['bs']['d']) AND (is_array($pl['opt']['bs']['d']))) {
  8793. $annots .= ' /D [';
  8794. foreach ($pl['opt']['bs']['d'] as $cord) {
  8795. $annots .= ' '.intval($cord);
  8796. }
  8797. $annots .= ']';
  8798. }
  8799. $annots .= ' >>';
  8800. } else {
  8801. $annots .= ' /Border [';
  8802. if (isset($pl['opt']['border']) AND (count($pl['opt']['border']) >= 3)) {
  8803. $annots .= intval($pl['opt']['border'][0]).' ';
  8804. $annots .= intval($pl['opt']['border'][1]).' ';
  8805. $annots .= intval($pl['opt']['border'][2]);
  8806. if (isset($pl['opt']['border'][3]) AND is_array($pl['opt']['border'][3])) {
  8807. $annots .= ' [';
  8808. foreach ($pl['opt']['border'][3] as $dash) {
  8809. $annots .= intval($dash).' ';
  8810. }
  8811. $annots .= ']';
  8812. }
  8813. } else {
  8814. $annots .= '0 0 0';
  8815. }
  8816. $annots .= ']';
  8817. }
  8818. if (isset($pl['opt']['be']) AND (is_array($pl['opt']['be']))) {
  8819. $annots .= ' /BE <<';
  8820. $bstyles = array('S', 'C');
  8821. if (isset($pl['opt']['be']['s']) AND in_array($pl['opt']['be']['s'], $markups)) {
  8822. $annots .= ' /S /'.$pl['opt']['bs']['s'];
  8823. } else {
  8824. $annots .= ' /S /S';
  8825. }
  8826. if (isset($pl['opt']['be']['i']) AND ($pl['opt']['be']['i'] >= 0) AND ($pl['opt']['be']['i'] <= 2)) {
  8827. $annots .= ' /I '.sprintf(' %.4F', $pl['opt']['be']['i']);
  8828. }
  8829. $annots .= '>>';
  8830. }
  8831. if (isset($pl['opt']['c']) AND (is_array($pl['opt']['c'])) AND !empty($pl['opt']['c'])) {
  8832. $annots .= ' /C [';
  8833. foreach ($pl['opt']['c'] as $col) {
  8834. $col = intval($col);
  8835. $color = $col <= 0 ? 0 : ($col >= 255 ? 1 : $col / 255);
  8836. $annots .= sprintf(' %.4F', $color);
  8837. }
  8838. $annots .= ']';
  8839. }
  8840. //$annots .= ' /StructParent ';
  8841. //$annots .= ' /OC ';
  8842. $markups = array('text', 'freetext', 'line', 'square', 'circle', 'polygon', 'polyline', 'highlight', 'underline', 'squiggly', 'strikeout', 'stamp', 'caret', 'ink', 'fileattachment', 'sound');
  8843. if (in_array(strtolower($pl['opt']['subtype']), $markups)) {
  8844. // this is a markup type
  8845. if (isset($pl['opt']['t']) AND is_string($pl['opt']['t'])) {
  8846. $annots .= ' /T '.$this->_textstring($pl['opt']['t'], $annot_obj_id);
  8847. }
  8848. //$annots .= ' /Popup ';
  8849. if (isset($pl['opt']['ca'])) {
  8850. $annots .= ' /CA '.sprintf('%.4F', floatval($pl['opt']['ca']));
  8851. }
  8852. if (isset($pl['opt']['rc'])) {
  8853. $annots .= ' /RC '.$this->_textstring($pl['opt']['rc'], $annot_obj_id);
  8854. }
  8855. $annots .= ' /CreationDate '.$this->_datestring($annot_obj_id);
  8856. //$annots .= ' /IRT ';
  8857. if (isset($pl['opt']['subj'])) {
  8858. $annots .= ' /Subj '.$this->_textstring($pl['opt']['subj'], $annot_obj_id);
  8859. }
  8860. //$annots .= ' /RT ';
  8861. //$annots .= ' /IT ';
  8862. //$annots .= ' /ExData ';
  8863. }
  8864. $lineendings = array('Square', 'Circle', 'Diamond', 'OpenArrow', 'ClosedArrow', 'None', 'Butt', 'ROpenArrow', 'RClosedArrow', 'Slash');
  8865. // Annotation types
  8866. switch (strtolower($pl['opt']['subtype'])) {
  8867. case 'text': {
  8868. if (isset($pl['opt']['open'])) {
  8869. $annots .= ' /Open '. (strtolower($pl['opt']['open']) == 'true' ? 'true' : 'false');
  8870. }
  8871. $iconsapp = array('Comment', 'Help', 'Insert', 'Key', 'NewParagraph', 'Note', 'Paragraph');
  8872. if (isset($pl['opt']['name']) AND in_array($pl['opt']['name'], $iconsapp)) {
  8873. $annots .= ' /Name /'.$pl['opt']['name'];
  8874. } else {
  8875. $annots .= ' /Name /Note';
  8876. }
  8877. $statemodels = array('Marked', 'Review');
  8878. if (isset($pl['opt']['statemodel']) AND in_array($pl['opt']['statemodel'], $statemodels)) {
  8879. $annots .= ' /StateModel /'.$pl['opt']['statemodel'];
  8880. } else {
  8881. $pl['opt']['statemodel'] = 'Marked';
  8882. $annots .= ' /StateModel /'.$pl['opt']['statemodel'];
  8883. }
  8884. if ($pl['opt']['statemodel'] == 'Marked') {
  8885. $states = array('Accepted', 'Unmarked');
  8886. } else {
  8887. $states = array('Accepted', 'Rejected', 'Cancelled', 'Completed', 'None');
  8888. }
  8889. if (isset($pl['opt']['state']) AND in_array($pl['opt']['state'], $states)) {
  8890. $annots .= ' /State /'.$pl['opt']['state'];
  8891. } else {
  8892. if ($pl['opt']['statemodel'] == 'Marked') {
  8893. $annots .= ' /State /Unmarked';
  8894. } else {
  8895. $annots .= ' /State /None';
  8896. }
  8897. }
  8898. break;
  8899. }
  8900. case 'link': {
  8901. if (is_string($pl['txt'])) {
  8902. // external URI link
  8903. $annots .= ' /A <</S /URI /URI '.$this->_datastring($this->unhtmlentities($pl['txt']), $annot_obj_id).'>>';
  8904. } else {
  8905. // internal link
  8906. $l = $this->links[$pl['txt']];
  8907. if (isset($this->page_obj_id[($l[0])])) {
  8908. $annots .= sprintf(' /Dest [%u 0 R /XYZ 0 %.2F null]', $this->page_obj_id[($l[0])], ($this->pagedim[$l[0]]['h'] - ($l[1] * $this->k)));
  8909. }
  8910. }
  8911. $hmodes = array('N', 'I', 'O', 'P');
  8912. if (isset($pl['opt']['h']) AND in_array($pl['opt']['h'], $hmodes)) {
  8913. $annots .= ' /H /'.$pl['opt']['h'];
  8914. } else {
  8915. $annots .= ' /H /I';
  8916. }
  8917. //$annots .= ' /PA ';
  8918. //$annots .= ' /Quadpoints ';
  8919. break;
  8920. }
  8921. case 'freetext': {
  8922. if (isset($pl['opt']['da']) AND !empty($pl['opt']['da'])) {
  8923. $annots .= ' /DA ('.$pl['opt']['da'].')';
  8924. }
  8925. if (isset($pl['opt']['q']) AND ($pl['opt']['q'] >= 0) AND ($pl['opt']['q'] <= 2)) {
  8926. $annots .= ' /Q '.intval($pl['opt']['q']);
  8927. }
  8928. if (isset($pl['opt']['rc'])) {
  8929. $annots .= ' /RC '.$this->_textstring($pl['opt']['rc'], $annot_obj_id);
  8930. }
  8931. if (isset($pl['opt']['ds'])) {
  8932. $annots .= ' /DS '.$this->_textstring($pl['opt']['ds'], $annot_obj_id);
  8933. }
  8934. if (isset($pl['opt']['cl']) AND is_array($pl['opt']['cl'])) {
  8935. $annots .= ' /CL [';
  8936. foreach ($pl['opt']['cl'] as $cl) {
  8937. $annots .= sprintf('%.4F ', $cl * $this->k);
  8938. }
  8939. $annots .= ']';
  8940. }
  8941. $tfit = array('FreeText', 'FreeTextCallout', 'FreeTextTypeWriter');
  8942. if (isset($pl['opt']['it']) AND in_array($pl['opt']['it'], $tfit)) {
  8943. $annots .= ' /IT /'.$pl['opt']['it'];
  8944. }
  8945. if (isset($pl['opt']['rd']) AND is_array($pl['opt']['rd'])) {
  8946. $l = $pl['opt']['rd'][0] * $this->k;
  8947. $r = $pl['opt']['rd'][1] * $this->k;
  8948. $t = $pl['opt']['rd'][2] * $this->k;
  8949. $b = $pl['opt']['rd'][3] * $this->k;
  8950. $annots .= ' /RD ['.sprintf('%.2F %.2F %.2F %.2F', $l, $r, $t, $b).']';
  8951. }
  8952. if (isset($pl['opt']['le']) AND in_array($pl['opt']['le'], $lineendings)) {
  8953. $annots .= ' /LE /'.$pl['opt']['le'];
  8954. }
  8955. break;
  8956. }
  8957. case 'line': {
  8958. break;
  8959. }
  8960. case 'square': {
  8961. break;
  8962. }
  8963. case 'circle': {
  8964. break;
  8965. }
  8966. case 'polygon': {
  8967. break;
  8968. }
  8969. case 'polyline': {
  8970. break;
  8971. }
  8972. case 'highlight': {
  8973. break;
  8974. }
  8975. case 'underline': {
  8976. break;
  8977. }
  8978. case 'squiggly': {
  8979. break;
  8980. }
  8981. case 'strikeout': {
  8982. break;
  8983. }
  8984. case 'stamp': {
  8985. break;
  8986. }
  8987. case 'caret': {
  8988. break;
  8989. }
  8990. case 'ink': {
  8991. break;
  8992. }
  8993. case 'popup': {
  8994. break;
  8995. }
  8996. case 'fileattachment': {
  8997. if ($this->pdfa_mode) {
  8998. // embedded files are not allowed in PDF/A mode
  8999. break;
  9000. }
  9001. if (!isset($pl['opt']['fs'])) {
  9002. break;
  9003. }
  9004. $filename = basename($pl['opt']['fs']);
  9005. if (isset($this->embeddedfiles[$filename]['n'])) {
  9006. $annots .= ' /FS <</Type /Filespec /F '.$this->_datastring($filename, $annot_obj_id).' /EF <</F '.$this->embeddedfiles[$filename]['n'].' 0 R>> >>';
  9007. $iconsapp = array('Graph', 'Paperclip', 'PushPin', 'Tag');
  9008. if (isset($pl['opt']['name']) AND in_array($pl['opt']['name'], $iconsapp)) {
  9009. $annots .= ' /Name /'.$pl['opt']['name'];
  9010. } else {
  9011. $annots .= ' /Name /PushPin';
  9012. }
  9013. }
  9014. break;
  9015. }
  9016. case 'sound': {
  9017. if (!isset($pl['opt']['fs'])) {
  9018. break;
  9019. }
  9020. $filename = basename($pl['opt']['fs']);
  9021. if (isset($this->embeddedfiles[$filename]['n'])) {
  9022. // ... TO BE COMPLETED ...
  9023. // /R /C /B /E /CO /CP
  9024. $annots .= ' /Sound <</Type /Filespec /F '.$this->_datastring($filename, $annot_obj_id).' /EF <</F '.$this->embeddedfiles[$filename]['n'].' 0 R>> >>';
  9025. $iconsapp = array('Speaker', 'Mic');
  9026. if (isset($pl['opt']['name']) AND in_array($pl['opt']['name'], $iconsapp)) {
  9027. $annots .= ' /Name /'.$pl['opt']['name'];
  9028. } else {
  9029. $annots .= ' /Name /Speaker';
  9030. }
  9031. }
  9032. break;
  9033. }
  9034. case 'movie': {
  9035. break;
  9036. }
  9037. case 'widget': {
  9038. $hmode = array('N', 'I', 'O', 'P', 'T');
  9039. if (isset($pl['opt']['h']) AND in_array($pl['opt']['h'], $hmode)) {
  9040. $annots .= ' /H /'.$pl['opt']['h'];
  9041. }
  9042. if (isset($pl['opt']['mk']) AND (is_array($pl['opt']['mk'])) AND !empty($pl['opt']['mk'])) {
  9043. $annots .= ' /MK <<';
  9044. if (isset($pl['opt']['mk']['r'])) {
  9045. $annots .= ' /R '.$pl['opt']['mk']['r'];
  9046. }
  9047. if (isset($pl['opt']['mk']['bc']) AND (is_array($pl['opt']['mk']['bc']))) {
  9048. $annots .= ' /BC [';
  9049. foreach($pl['opt']['mk']['bc'] AS $col) {
  9050. $col = intval($col);
  9051. $color = $col <= 0 ? 0 : ($col >= 255 ? 1 : $col / 255);
  9052. $annots .= sprintf(' %.2F', $color);
  9053. }
  9054. $annots .= ']';
  9055. }
  9056. if (isset($pl['opt']['mk']['bg']) AND (is_array($pl['opt']['mk']['bg']))) {
  9057. $annots .= ' /BG [';
  9058. foreach($pl['opt']['mk']['bg'] AS $col) {
  9059. $col = intval($col);
  9060. $color = $col <= 0 ? 0 : ($col >= 255 ? 1 : $col / 255);
  9061. $annots .= sprintf(' %.2F', $color);
  9062. }
  9063. $annots .= ']';
  9064. }
  9065. if (isset($pl['opt']['mk']['ca'])) {
  9066. $annots .= ' /CA '.$pl['opt']['mk']['ca'];
  9067. }
  9068. if (isset($pl['opt']['mk']['rc'])) {
  9069. $annots .= ' /RC '.$pl['opt']['mk']['rc'];
  9070. }
  9071. if (isset($pl['opt']['mk']['ac'])) {
  9072. $annots .= ' /AC '.$pl['opt']['mk']['ac'];
  9073. }
  9074. if (isset($pl['opt']['mk']['i'])) {
  9075. $info = $this->getImageBuffer($pl['opt']['mk']['i']);
  9076. if ($info !== false) {
  9077. $annots .= ' /I '.$info['n'].' 0 R';
  9078. }
  9079. }
  9080. if (isset($pl['opt']['mk']['ri'])) {
  9081. $info = $this->getImageBuffer($pl['opt']['mk']['ri']);
  9082. if ($info !== false) {
  9083. $annots .= ' /RI '.$info['n'].' 0 R';
  9084. }
  9085. }
  9086. if (isset($pl['opt']['mk']['ix'])) {
  9087. $info = $this->getImageBuffer($pl['opt']['mk']['ix']);
  9088. if ($info !== false) {
  9089. $annots .= ' /IX '.$info['n'].' 0 R';
  9090. }
  9091. }
  9092. if (isset($pl['opt']['mk']['if']) AND (is_array($pl['opt']['mk']['if'])) AND !empty($pl['opt']['mk']['if'])) {
  9093. $annots .= ' /IF <<';
  9094. $if_sw = array('A', 'B', 'S', 'N');
  9095. if (isset($pl['opt']['mk']['if']['sw']) AND in_array($pl['opt']['mk']['if']['sw'], $if_sw)) {
  9096. $annots .= ' /SW /'.$pl['opt']['mk']['if']['sw'];
  9097. }
  9098. $if_s = array('A', 'P');
  9099. if (isset($pl['opt']['mk']['if']['s']) AND in_array($pl['opt']['mk']['if']['s'], $if_s)) {
  9100. $annots .= ' /S /'.$pl['opt']['mk']['if']['s'];
  9101. }
  9102. if (isset($pl['opt']['mk']['if']['a']) AND (is_array($pl['opt']['mk']['if']['a'])) AND !empty($pl['opt']['mk']['if']['a'])) {
  9103. $annots .= sprintf(' /A [%.2F %.2F]', $pl['opt']['mk']['if']['a'][0], $pl['opt']['mk']['if']['a'][1]);
  9104. }
  9105. if (isset($pl['opt']['mk']['if']['fb']) AND ($pl['opt']['mk']['if']['fb'])) {
  9106. $annots .= ' /FB true';
  9107. }
  9108. $annots .= '>>';
  9109. }
  9110. if (isset($pl['opt']['mk']['tp']) AND ($pl['opt']['mk']['tp'] >= 0) AND ($pl['opt']['mk']['tp'] <= 6)) {
  9111. $annots .= ' /TP '.intval($pl['opt']['mk']['tp']);
  9112. }
  9113. $annots .= '>>';
  9114. } // end MK
  9115. // --- Entries for field dictionaries ---
  9116. if (isset($this->radiobutton_groups[$n][$pl['txt']])) {
  9117. // set parent
  9118. $annots .= ' /Parent '.$this->radiobutton_groups[$n][$pl['txt']].' 0 R';
  9119. }
  9120. if (isset($pl['opt']['t']) AND is_string($pl['opt']['t'])) {
  9121. $annots .= ' /T '.$this->_datastring($pl['opt']['t'], $annot_obj_id);
  9122. }
  9123. if (isset($pl['opt']['tu']) AND is_string($pl['opt']['tu'])) {
  9124. $annots .= ' /TU '.$this->_datastring($pl['opt']['tu'], $annot_obj_id);
  9125. }
  9126. if (isset($pl['opt']['tm']) AND is_string($pl['opt']['tm'])) {
  9127. $annots .= ' /TM '.$this->_datastring($pl['opt']['tm'], $annot_obj_id);
  9128. }
  9129. if (isset($pl['opt']['ff'])) {
  9130. if (is_array($pl['opt']['ff'])) {
  9131. // array of bit settings
  9132. $flag = 0;
  9133. foreach($pl['opt']['ff'] as $val) {
  9134. $flag += 1 << ($val - 1);
  9135. }
  9136. } else {
  9137. $flag = intval($pl['opt']['ff']);
  9138. }
  9139. $annots .= ' /Ff '.$flag;
  9140. }
  9141. if (isset($pl['opt']['maxlen'])) {
  9142. $annots .= ' /MaxLen '.intval($pl['opt']['maxlen']);
  9143. }
  9144. if (isset($pl['opt']['v'])) {
  9145. $annots .= ' /V';
  9146. if (is_array($pl['opt']['v'])) {
  9147. foreach ($pl['opt']['v'] AS $optval) {
  9148. if (is_float($optval)) {
  9149. $optval = sprintf('%.2F', $optval);
  9150. }
  9151. $annots .= ' '.$optval;
  9152. }
  9153. } else {
  9154. $annots .= ' '.$this->_textstring($pl['opt']['v'], $annot_obj_id);
  9155. }
  9156. }
  9157. if (isset($pl['opt']['dv'])) {
  9158. $annots .= ' /DV';
  9159. if (is_array($pl['opt']['dv'])) {
  9160. foreach ($pl['opt']['dv'] AS $optval) {
  9161. if (is_float($optval)) {
  9162. $optval = sprintf('%.2F', $optval);
  9163. }
  9164. $annots .= ' '.$optval;
  9165. }
  9166. } else {
  9167. $annots .= ' '.$this->_textstring($pl['opt']['dv'], $annot_obj_id);
  9168. }
  9169. }
  9170. if (isset($pl['opt']['rv'])) {
  9171. $annots .= ' /RV';
  9172. if (is_array($pl['opt']['rv'])) {
  9173. foreach ($pl['opt']['rv'] AS $optval) {
  9174. if (is_float($optval)) {
  9175. $optval = sprintf('%.2F', $optval);
  9176. }
  9177. $annots .= ' '.$optval;
  9178. }
  9179. } else {
  9180. $annots .= ' '.$this->_textstring($pl['opt']['rv'], $annot_obj_id);
  9181. }
  9182. }
  9183. if (isset($pl['opt']['a']) AND !empty($pl['opt']['a'])) {
  9184. $annots .= ' /A << '.$pl['opt']['a'].' >>';
  9185. }
  9186. if (isset($pl['opt']['aa']) AND !empty($pl['opt']['aa'])) {
  9187. $annots .= ' /AA << '.$pl['opt']['aa'].' >>';
  9188. }
  9189. if (isset($pl['opt']['da']) AND !empty($pl['opt']['da'])) {
  9190. $annots .= ' /DA ('.$pl['opt']['da'].')';
  9191. }
  9192. if (isset($pl['opt']['q']) AND ($pl['opt']['q'] >= 0) AND ($pl['opt']['q'] <= 2)) {
  9193. $annots .= ' /Q '.intval($pl['opt']['q']);
  9194. }
  9195. if (isset($pl['opt']['opt']) AND (is_array($pl['opt']['opt'])) AND !empty($pl['opt']['opt'])) {
  9196. $annots .= ' /Opt [';
  9197. foreach($pl['opt']['opt'] AS $copt) {
  9198. if (is_array($copt)) {
  9199. $annots .= ' ['.$this->_textstring($copt[0], $annot_obj_id).' '.$this->_textstring($copt[1], $annot_obj_id).']';
  9200. } else {
  9201. $annots .= ' '.$this->_textstring($copt, $annot_obj_id);
  9202. }
  9203. }
  9204. $annots .= ']';
  9205. }
  9206. if (isset($pl['opt']['ti'])) {
  9207. $annots .= ' /TI '.intval($pl['opt']['ti']);
  9208. }
  9209. if (isset($pl['opt']['i']) AND (is_array($pl['opt']['i'])) AND !empty($pl['opt']['i'])) {
  9210. $annots .= ' /I [';
  9211. foreach($pl['opt']['i'] AS $copt) {
  9212. $annots .= intval($copt).' ';
  9213. }
  9214. $annots .= ']';
  9215. }
  9216. break;
  9217. }
  9218. case 'screen': {
  9219. break;
  9220. }
  9221. case 'printermark': {
  9222. break;
  9223. }
  9224. case 'trapnet': {
  9225. break;
  9226. }
  9227. case 'watermark': {
  9228. break;
  9229. }
  9230. case '3d': {
  9231. break;
  9232. }
  9233. default: {
  9234. break;
  9235. }
  9236. }
  9237. $annots .= '>>';
  9238. // create new annotation object
  9239. $this->_out($this->_getobj($annot_obj_id)."\n".$annots."\n".'endobj');
  9240. if ($formfield AND !isset($this->radiobutton_groups[$n][$pl['txt']])) {
  9241. // store reference of form object
  9242. $this->form_obj_id[] = $annot_obj_id;
  9243. }
  9244. }
  9245. }
  9246. } // end for each page
  9247. }
  9248. /**
  9249. * Put appearance streams XObject used to define annotation's appearance states.
  9250. * @param $w (int) annotation width
  9251. * @param $h (int) annotation height
  9252. * @param $stream (string) appearance stream
  9253. * @return int object ID
  9254. * @protected
  9255. * @since 4.8.001 (2009-09-09)
  9256. */
  9257. protected function _putAPXObject($w=0, $h=0, $stream='') {
  9258. $stream = trim($stream);
  9259. $out = $this->_getobj()."\n";
  9260. $this->xobjects['AX'.$this->n] = array('n' => $this->n);
  9261. $out .= '<<';
  9262. $out .= ' /Type /XObject';
  9263. $out .= ' /Subtype /Form';
  9264. $out .= ' /FormType 1';
  9265. if ($this->compress) {
  9266. $stream = gzcompress($stream);
  9267. $out .= ' /Filter /FlateDecode';
  9268. }
  9269. $rect = sprintf('%.2F %.2F', $w, $h);
  9270. $out .= ' /BBox [0 0 '.$rect.']';
  9271. $out .= ' /Matrix [1 0 0 1 0 0]';
  9272. $out .= ' /Resources 2 0 R';
  9273. $stream = $this->_getrawstream($stream);
  9274. $out .= ' /Length '.strlen($stream);
  9275. $out .= ' >>';
  9276. $out .= ' stream'."\n".$stream."\n".'endstream';
  9277. $out .= "\n".'endobj';
  9278. $this->_out($out);
  9279. return $this->n;
  9280. }
  9281. /**
  9282. * Get ULONG from string (Big Endian 32-bit unsigned integer).
  9283. * @param $str (string) string from where to extract value
  9284. * @param $offset (int) point from where to read the data
  9285. * @return int 32 bit value
  9286. * @author Nicola Asuni
  9287. * @protected
  9288. * @since 5.2.000 (2010-06-02)
  9289. */
  9290. protected function _getULONG($str, $offset) {
  9291. $v = unpack('Ni', substr($str, $offset, 4));
  9292. return $v['i'];
  9293. }
  9294. /**
  9295. * Get USHORT from string (Big Endian 16-bit unsigned integer).
  9296. * @param $str (string) string from where to extract value
  9297. * @param $offset (int) point from where to read the data
  9298. * @return int 16 bit value
  9299. * @author Nicola Asuni
  9300. * @protected
  9301. * @since 5.2.000 (2010-06-02)
  9302. */
  9303. protected function _getUSHORT($str, $offset) {
  9304. $v = unpack('ni', substr($str, $offset, 2));
  9305. return $v['i'];
  9306. }
  9307. /**
  9308. * Get SHORT from string (Big Endian 16-bit signed integer).
  9309. * @param $str (string) string from where to extract value
  9310. * @param $offset (int) point from where to read the data
  9311. * @return int 16 bit value
  9312. * @author Nicola Asuni
  9313. * @protected
  9314. * @since 5.2.000 (2010-06-02)
  9315. */
  9316. protected function _getSHORT($str, $offset) {
  9317. $v = unpack('si', substr($str, $offset, 2));
  9318. return $v['i'];
  9319. }
  9320. /**
  9321. * Get FWORD from string (Big Endian 16-bit signed integer).
  9322. * @param $str (string) string from where to extract value
  9323. * @param $offset (int) point from where to read the data
  9324. * @return int 16 bit value
  9325. * @author Nicola Asuni
  9326. * @protected
  9327. * @since 5.9.123 (2011-09-30)
  9328. */
  9329. protected function _getFWORD($str, $offset) {
  9330. $v = $this->_getUSHORT($str, $offset);
  9331. if ($v > 0x7fff) {
  9332. $v -= 0x10000;
  9333. }
  9334. return $v;
  9335. }
  9336. /**
  9337. * Get UFWORD from string (Big Endian 16-bit unsigned integer).
  9338. * @param $str (string) string from where to extract value
  9339. * @param $offset (int) point from where to read the data
  9340. * @return int 16 bit value
  9341. * @author Nicola Asuni
  9342. * @protected
  9343. * @since 5.9.123 (2011-09-30)
  9344. */
  9345. protected function _getUFWORD($str, $offset) {
  9346. $v = $this->_getUSHORT($str, $offset);
  9347. return $v;
  9348. }
  9349. /**
  9350. * Get FIXED from string (32-bit signed fixed-point number (16.16).
  9351. * @param $str (string) string from where to extract value
  9352. * @param $offset (int) point from where to read the data
  9353. * @return int 16 bit value
  9354. * @author Nicola Asuni
  9355. * @protected
  9356. * @since 5.9.123 (2011-09-30)
  9357. */
  9358. protected function _getFIXED($str, $offset) {
  9359. // mantissa
  9360. $m = $this->_getFWORD($str, $offset);
  9361. // fraction
  9362. $f = $this->_getUSHORT($str, ($offset + 2));
  9363. $v = floatval(''.$m.'.'.$f.'');
  9364. return $v;
  9365. }
  9366. /**
  9367. * Get BYTE from string (8-bit unsigned integer).
  9368. * @param $str (string) String from where to extract value.
  9369. * @param $offset (int) Point from where to read the data.
  9370. * @return int 8 bit value
  9371. * @author Nicola Asuni
  9372. * @protected
  9373. * @since 5.2.000 (2010-06-02)
  9374. */
  9375. protected function _getBYTE($str, $offset) {
  9376. $v = unpack('Ci', substr($str, $offset, 1));
  9377. return $v['i'];
  9378. }
  9379. /**
  9380. * Update the CIDToGIDMap string with a new value.
  9381. * @param $map (string) CIDToGIDMap.
  9382. * @param $cid (int) CID value.
  9383. * @param $gid (int) GID value.
  9384. * @return (string) CIDToGIDMap.
  9385. * @author Nicola Asuni
  9386. * @protected
  9387. * @since 5.9.123 (2011-09-29)
  9388. */
  9389. protected function updateCIDtoGIDmap($map, $cid, $gid) {
  9390. if (($cid >= 0) AND ($cid <= 0xFFFF) AND ($gid >= 0)) {
  9391. if ($gid > 0xFFFF) {
  9392. $gid -= 0x10000;
  9393. }
  9394. $map[($cid * 2)] = chr($gid >> 8);
  9395. $map[(($cid * 2) + 1)] = chr($gid & 0xFF);
  9396. }
  9397. return $map;
  9398. }
  9399. /**
  9400. * Convert and add the selected TrueType or Type1 font to the fonts folder (that must be writeable).
  9401. * @param $fontfile (string) TrueType or Type1 font file (full path).
  9402. * @param $fonttype (string) Font type. Leave empty for autodetect mode. Valid values are: TrueTypeUnicode, TrueType, Type1, CID0JP = CID-0 Japanese, CID0KR = CID-0 Korean, CID0CS = CID-0 Chinese Simplified, CID0CT = CID-0 Chinese Traditional.
  9403. * @param $enc (string) Name of the encoding table to use. Leave empty for default mode. Omit this parameter for TrueType Unicode and symbolic fonts like Symbol or ZapfDingBats.
  9404. * @param $flags (int) Unsigned 32-bit integer containing flags specifying various characteristics of the font (PDF32000:2008 - 9.8.2 Font Descriptor Flags): +1 for fixed font; +4 for symbol or +32 for non-symbol; +64 for italic. Fixed and Italic mode are generally autodetected so you have to set it to 32 = non-symbolic font (default) or 4 = symbolic font.
  9405. * @param $outpath (string) Output path for generated font files (must be writeable by the web server). Leave empty for default font folder.
  9406. * @return (string) TCPDF font name.
  9407. * @author Nicola Asuni
  9408. * @public
  9409. * @since 5.9.123 (2010-09-30)
  9410. */
  9411. public function addTTFfont($fontfile, $fonttype='', $enc='', $flags=32, $outpath='') {
  9412. if (!file_exists($fontfile)) {
  9413. $this->Error('Could not find file: '.$fontfile.'');
  9414. }
  9415. // font metrics
  9416. $fmetric = array();
  9417. // build new font name for TCPDF compatibility
  9418. $font_path_parts = pathinfo($fontfile);
  9419. if (!isset($font_path_parts['filename'])) {
  9420. $font_path_parts['filename'] = substr($font_path_parts['basename'], 0, -(strlen($font_path_parts['extension']) + 1));
  9421. }
  9422. $font_name = strtolower($font_path_parts['filename']);
  9423. $font_name = preg_replace('/[^a-z0-9_]/', '', $font_name);
  9424. $search = array('bold', 'oblique', 'italic', 'regular');
  9425. $replace = array('b', 'i', 'i', '');
  9426. $font_name = str_replace($search, $replace, $font_name);
  9427. if (empty($font_name)) {
  9428. // set generic name
  9429. $font_name = 'tcpdffont';
  9430. }
  9431. // set output path
  9432. if (empty($outpath)) {
  9433. $outpath = $this->_getfontpath();
  9434. }
  9435. // check if this font already exist
  9436. if (file_exists($outpath.$font_name.'.php')) {
  9437. // this font already exist (delete it from fonts folder to rebuild it)
  9438. return $font_name;
  9439. }
  9440. $fmetric['file'] = $font_name.'.z';
  9441. $fmetric['ctg'] = $font_name.'.ctg.z';
  9442. // get font data
  9443. $font = file_get_contents($fontfile);
  9444. $fmetric['originalsize'] = strlen($font);
  9445. // autodetect font type
  9446. if (empty($fonttype)) {
  9447. if ($this->_getULONG($font, 0) == 0x10000) {
  9448. // True Type (Unicode or not)
  9449. $fonttype = 'TrueTypeUnicode';
  9450. } elseif (substr($font, 0, 4) == 'OTTO') {
  9451. // Open Type (Unicode or not)
  9452. $this->Error('Unsupported font format: OpenType with CFF data.');
  9453. } else {
  9454. // Type 1
  9455. $fonttype = 'Type1';
  9456. }
  9457. }
  9458. // set font type
  9459. switch ($fonttype) {
  9460. case 'CID0CT':
  9461. case 'CID0CS':
  9462. case 'CID0KR':
  9463. case 'CID0JP': {
  9464. $fmetric['type'] = 'cidfont0';
  9465. break;
  9466. }
  9467. case 'Type1': {
  9468. $fmetric['type'] = 'Type1';
  9469. if (empty($enc) AND (($flags & 4) == 0)) {
  9470. $enc = 'cp1252';
  9471. }
  9472. break;
  9473. }
  9474. case 'TrueType': {
  9475. $fmetric['type'] = 'TrueType';
  9476. break;
  9477. }
  9478. case 'TrueTypeUnicode':
  9479. default: {
  9480. $fmetric['type'] = 'TrueTypeUnicode';
  9481. break;
  9482. }
  9483. }
  9484. // set encoding maps (if any)
  9485. $fmetric['enc'] = preg_replace('/[^A-Za-z0-9_\-]/', '', $enc);
  9486. $fmetric['diff'] = '';
  9487. if (($fmetric['type'] == 'TrueType') OR ($fmetric['type'] == 'Type1')) {
  9488. if (!empty($enc) AND ($enc != 'cp1252') AND isset($this->encmaps->encmap[$enc])) {
  9489. // build differences from reference encoding
  9490. $enc_ref = $this->encmaps->encmap['cp1252'];
  9491. $enc_target = $this->encmaps->encmap[$enc];
  9492. $last = 0;
  9493. for ($i = 32; $i <= 255; ++$i) {
  9494. if ($enc_target != $enc_ref[$i]) {
  9495. if ($i != ($last + 1)) {
  9496. $fmetric['diff'] .= $i.' ';
  9497. }
  9498. $last = $i;
  9499. $fmetric['diff'] .= '/'.$enc_target[$i].' ';
  9500. }
  9501. }
  9502. }
  9503. }
  9504. // parse the font by type
  9505. if ($fmetric['type'] == 'Type1') {
  9506. // ---------- TYPE 1 ----------
  9507. // read first segment
  9508. $a = unpack('Cmarker/Ctype/Vsize', substr($font, 0, 6));
  9509. if ($a['marker'] != 128) {
  9510. $this->Error('Font file is not a valid binary Type1');
  9511. }
  9512. $fmetric['size1'] = $a['size'];
  9513. $data = substr($font, 6, $fmetric['size1']);
  9514. // read second segment
  9515. $a = unpack('Cmarker/Ctype/Vsize', substr($font, (6 + $fmetric['size1']), 6));
  9516. if ($a['marker'] != 128) {
  9517. $this->Error('Font file is not a valid binary Type1');
  9518. }
  9519. $fmetric['size2'] = $a['size'];
  9520. $encrypted = substr($font, (12 + $fmetric['size1']), $fmetric['size2']);
  9521. $data .= $encrypted;
  9522. // store compressed font
  9523. $fp = fopen($outpath.$fmetric['file'], 'wb');
  9524. fwrite($fp, gzcompress($data));
  9525. fclose($fp);
  9526. // get font info
  9527. $fmetric['Flags'] = $flags;
  9528. preg_match ('#/FullName[\s]*\(([^\)]*)#', $font, $matches);
  9529. $fmetric['name'] = preg_replace('/[^a-zA-Z0-9_\-]/', '', $matches[1]);
  9530. preg_match('#/FontBBox[\s]*{([^}]*)#', $font, $matches);
  9531. $fmetric['bbox'] = trim($matches[1]);
  9532. $bv = explode(' ', $fmetric['bbox']);
  9533. $fmetric['Ascent'] = intval($bv[3]);
  9534. $fmetric['Descent'] = intval($bv[1]);
  9535. preg_match('#/ItalicAngle[\s]*([0-9\+\-]*)#', $font, $matches);
  9536. $fmetric['italicAngle'] = intval($matches[1]);
  9537. if ($fmetric['italicAngle'] != 0) {
  9538. $fmetric['Flags'] |= 64;
  9539. }
  9540. preg_match('#/UnderlinePosition[\s]*([0-9\+\-]*)#', $font, $matches);
  9541. $fmetric['underlinePosition'] = intval($matches[1]);
  9542. preg_match('#/UnderlineThickness[\s]*([0-9\+\-]*)#', $font, $matches);
  9543. $fmetric['underlineThickness'] = intval($matches[1]);
  9544. preg_match('#/isFixedPitch[\s]*([^\s]*)#', $font, $matches);
  9545. if ($matches[1] == 'true') {
  9546. $fmetric['Flags'] |= 1;
  9547. }
  9548. // get internal map
  9549. $imap = array();
  9550. if (preg_match_all('#dup[\s]([0-9]+)[\s]*/([^\s]*)[\s]put#sU', $font, $fmap, PREG_SET_ORDER) > 0) {
  9551. foreach ($fmap as $v) {
  9552. $imap[$v[2]] = $v[1];
  9553. }
  9554. }
  9555. // decrypt eexec encrypted part
  9556. $r = 55665; // eexec encryption constant
  9557. $c1 = 52845;
  9558. $c2 = 22719;
  9559. $elen = strlen($encrypted);
  9560. $eplain = '';
  9561. for ($i = 0; $i < $elen; ++$i) {
  9562. $chr = ord($encrypted[$i]);
  9563. $eplain .= chr($chr ^ ($r >> 8));
  9564. $r = ((($chr + $r) * $c1 + $c2) % 65536);
  9565. }
  9566. if (preg_match('#/ForceBold[\s]*([^\s]*)#', $eplain, $matches) > 0) {
  9567. if ($matches[1] == 'true') {
  9568. $fmetric['Flags'] |= 0x40000;
  9569. }
  9570. }
  9571. if (preg_match('#/StdVW[\s]*\[([^\]]*)#', $eplain, $matches) > 0) {
  9572. $fmetric['StemV'] = intval($matches[1]);
  9573. } else {
  9574. $fmetric['StemV'] = 70;
  9575. }
  9576. if (preg_match('#/StdHW[\s]*\[([^\]]*)#', $eplain, $matches) > 0) {
  9577. $fmetric['StemH'] = intval($matches[1]);
  9578. } else {
  9579. $fmetric['StemH'] = 30;
  9580. }
  9581. if (preg_match('#/BlueValues[\s]*\[([^\]]*)#', $eplain, $matches) > 0) {
  9582. $bv = explode(' ', $matches[1]);
  9583. if (count($bv) >= 6) {
  9584. $v1 = intval($bv[2]);
  9585. $v2 = intval($bv[4]);
  9586. if ($v1 <= $v2) {
  9587. $fmetric['XHeight'] = $v1;
  9588. $fmetric['CapHeight'] = $v2;
  9589. } else {
  9590. $fmetric['XHeight'] = $v2;
  9591. $fmetric['CapHeight'] = $v1;
  9592. }
  9593. } else {
  9594. $fmetric['XHeight'] = 450;
  9595. $fmetric['CapHeight'] = 700;
  9596. }
  9597. } else {
  9598. $fmetric['XHeight'] = 450;
  9599. $fmetric['CapHeight'] = 700;
  9600. }
  9601. // get the number of random bytes at the beginning of charstrings
  9602. if (preg_match('#/lenIV[\s]*([0-9]*)#', $eplain, $matches) > 0) {
  9603. $lenIV = intval($matches[1]);
  9604. } else {
  9605. $lenIV = 4;
  9606. }
  9607. $fmetric['Leading'] = 0;
  9608. // get charstring data
  9609. $eplain = substr($eplain, (strpos($eplain, '/CharStrings') + 1));
  9610. preg_match_all('#/([A-Za-z0-9\.]*)[\s][0-9]+[\s]RD[\s](.*)[\s]ND#sU', $eplain, $matches, PREG_SET_ORDER);
  9611. if (!empty($enc) AND isset($this->encmaps->encmap[$enc])) {
  9612. $enc_map = $this->encmaps->encmap[$enc];
  9613. } else {
  9614. $enc_map = false;
  9615. }
  9616. $fmetric['cw'] = '';
  9617. $fmetric['MaxWidth'] = 0;
  9618. $cwidths = array();
  9619. foreach ($matches as $k => $v) {
  9620. $cid = 0;
  9621. if (isset($imap[$v[1]])) {
  9622. $cid = $imap[$v[1]];
  9623. } elseif ($enc_map !== false) {
  9624. $cid = array_search($v[1], $enc_map);
  9625. if ($cid === false) {
  9626. $cid = 0;
  9627. } elseif ($cid > 1000) {
  9628. $cid -= 1000;
  9629. }
  9630. }
  9631. // decrypt charstring encrypted part
  9632. $r = 4330; // charstring encryption constant
  9633. $c1 = 52845;
  9634. $c2 = 22719;
  9635. $cd = $v[2];
  9636. $clen = strlen($cd);
  9637. $ccom = array();
  9638. for ($i = 0; $i < $clen; ++$i) {
  9639. $chr = ord($cd[$i]);
  9640. $ccom[] = ($chr ^ ($r >> 8));
  9641. $r = ((($chr + $r) * $c1 + $c2) % 65536);
  9642. }
  9643. // decode numbers
  9644. $cdec = array();
  9645. $ck = 0;
  9646. $i = $lenIV;
  9647. while ($i < $clen) {
  9648. if ($ccom[$i] < 32) {
  9649. $cdec[$ck] = $ccom[$i];
  9650. if (($ck > 0) AND ($cdec[$ck] == 13)) {
  9651. // hsbw command: update width
  9652. $cwidths[$cid] = $cdec[($ck - 1)];
  9653. }
  9654. ++$i;
  9655. } elseif (($ccom[$i] >= 32) AND ($ccom[$i] <= 246)) {
  9656. $cdec[$ck] = ($ccom[$i] - 139);
  9657. ++$i;
  9658. } elseif (($ccom[$i] >= 247) AND ($ccom[$i] <= 250)) {
  9659. $cdec[$ck] = ((($ccom[$i] - 247) * 256) + $ccom[($i + 1)] + 108);
  9660. $i += 2;
  9661. } elseif (($ccom[$i] >= 251) AND ($ccom[$i] <= 254)) {
  9662. $cdec[$ck] = ((-($ccom[$i] - 251) * 256) - $ccom[($i + 1)] - 108);
  9663. $i += 2;
  9664. } elseif ($ccom[$i] == 255) {
  9665. $sval = chr($ccom[($i + 1)]).chr($ccom[($i + 2)]).chr($ccom[($i + 3)]).chr($ccom[($i + 4)]);
  9666. $vsval = unpack('li', $sval);
  9667. $cdec[$ck] = $vsval['i'];
  9668. $i += 5;
  9669. }
  9670. ++$ck;
  9671. }
  9672. } // end for each matches
  9673. $fmetric['MissingWidth'] = $cwidths[0];
  9674. $fmetric['MaxWidth'] = $fmetric['MissingWidth'];
  9675. $fmetric['AvgWidth'] = 0;
  9676. // set chars widths
  9677. for ($cid = 0; $cid <= 255; ++$cid) {
  9678. if (isset($cwidths[$cid])) {
  9679. if ($cwidths[$cid] > $fmetric['MaxWidth']) {
  9680. $fmetric['MaxWidth'] = $cwidths[$cid];
  9681. }
  9682. $fmetric['AvgWidth'] += $cwidths[$cid];
  9683. $fmetric['cw'] .= ','.$cid.'=>'.$cwidths[$cid];
  9684. } else {
  9685. $fmetric['cw'] .= ','.$cid.'=>'.$fmetric['MissingWidth'];
  9686. }
  9687. }
  9688. $fmetric['AvgWidth'] = round($fmetric['AvgWidth'] / count($cwidths));
  9689. } else {
  9690. // ---------- TRUE TYPE ----------
  9691. if ($fmetric['type'] != 'cidfont0') {
  9692. // store compressed font
  9693. $fp = fopen($outpath.$fmetric['file'], 'wb');
  9694. fwrite($fp, gzcompress($font));
  9695. fclose($fp);
  9696. }
  9697. $offset = 0; // offset position of the font data
  9698. if ($this->_getULONG($font, $offset) != 0x10000) {
  9699. // sfnt version must be 0x00010000 for TrueType version 1.0.
  9700. return $font;
  9701. }
  9702. $offset += 4;
  9703. // get number of tables
  9704. $numTables = $this->_getUSHORT($font, $offset);
  9705. $offset += 2;
  9706. // skip searchRange, entrySelector and rangeShift
  9707. $offset += 6;
  9708. // tables array
  9709. $table = array();
  9710. // ---------- get tables ----------
  9711. for ($i = 0; $i < $numTables; ++$i) {
  9712. // get table info
  9713. $tag = substr($font, $offset, 4);
  9714. $offset += 4;
  9715. $table[$tag] = array();
  9716. $table[$tag]['checkSum'] = $this->_getULONG($font, $offset);
  9717. $offset += 4;
  9718. $table[$tag]['offset'] = $this->_getULONG($font, $offset);
  9719. $offset += 4;
  9720. $table[$tag]['length'] = $this->_getULONG($font, $offset);
  9721. $offset += 4;
  9722. }
  9723. // check magicNumber
  9724. $offset = $table['head']['offset'] + 12;
  9725. if ($this->_getULONG($font, $offset) != 0x5F0F3CF5) {
  9726. // magicNumber must be 0x5F0F3CF5
  9727. return $font;
  9728. }
  9729. $offset += 4;
  9730. $offset += 2; // skip flags
  9731. // get FUnits
  9732. $fmetric['unitsPerEm'] = $this->_getUSHORT($font, $offset);
  9733. $offset += 2;
  9734. // units ratio constant
  9735. $urk = (1000 / $fmetric['unitsPerEm']);
  9736. $offset += 16; // skip created, modified
  9737. $xMin = round($this->_getFWORD($font, $offset) * $urk);
  9738. $offset += 2;
  9739. $yMin = round($this->_getFWORD($font, $offset) * $urk);
  9740. $offset += 2;
  9741. $xMax = round($this->_getFWORD($font, $offset) * $urk);
  9742. $offset += 2;
  9743. $yMax = round($this->_getFWORD($font, $offset) * $urk);
  9744. $offset += 2;
  9745. $fmetric['bbox'] = ''.$xMin.' '.$yMin.' '.$xMax.' '.$yMax.'';
  9746. $macStyle = $this->_getUSHORT($font, $offset);
  9747. $offset += 2;
  9748. // PDF font flags
  9749. $fmetric['Flags'] = $flags;
  9750. if (($macStyle & 2) == 2) {
  9751. // italic flag
  9752. $fmetric['Flags'] |= 64;
  9753. }
  9754. // get offset mode (indexToLocFormat : 0 = short, 1 = long)
  9755. $offset = $table['head']['offset'] + 50;
  9756. $short_offset = ($this->_getSHORT($font, $offset) == 0);
  9757. $offset += 2;
  9758. // get the offsets to the locations of the glyphs in the font, relative to the beginning of the glyphData table
  9759. $indexToLoc = array();
  9760. $offset = $table['loca']['offset'];
  9761. if ($short_offset) {
  9762. // short version
  9763. $tot_num_glyphs = ($table['loca']['length'] / 2); // numGlyphs + 1
  9764. for ($i = 0; $i < $tot_num_glyphs; ++$i) {
  9765. $indexToLoc[$i] = $this->_getUSHORT($font, $offset) * 2;
  9766. $offset += 2;
  9767. }
  9768. } else {
  9769. // long version
  9770. $tot_num_glyphs = ($table['loca']['length'] / 4); // numGlyphs + 1
  9771. for ($i = 0; $i < $tot_num_glyphs; ++$i) {
  9772. $indexToLoc[$i] = $this->_getULONG($font, $offset);
  9773. $offset += 4;
  9774. }
  9775. }
  9776. // get glyphs indexes of chars from cmap table
  9777. $offset = $table['cmap']['offset'] + 2;
  9778. $numEncodingTables = $this->_getUSHORT($font, $offset);
  9779. $offset += 2;
  9780. $encodingTables = array();
  9781. for ($i = 0; $i < $numEncodingTables; ++$i) {
  9782. $encodingTables[$i]['platformID'] = $this->_getUSHORT($font, $offset);
  9783. $offset += 2;
  9784. $encodingTables[$i]['encodingID'] = $this->_getUSHORT($font, $offset);
  9785. $offset += 2;
  9786. $encodingTables[$i]['offset'] = $this->_getULONG($font, $offset);
  9787. $offset += 4;
  9788. }
  9789. // ---------- get os/2 metrics ----------
  9790. $offset = $table['OS/2']['offset'];
  9791. $offset += 2; // skip version
  9792. // xAvgCharWidth
  9793. $fmetric['AvgWidth'] = round($this->_getFWORD($font, $offset) * $urk);
  9794. $offset += 2;
  9795. // usWeightClass
  9796. $usWeightClass = round($this->_getUFWORD($font, $offset) * $urk);
  9797. // estimate StemV and StemH (400 = usWeightClass for Normal - Regular font)
  9798. $fmetric['StemV'] = round((70 * $usWeightClass) / 400);
  9799. $fmetric['StemH'] = round((30 * $usWeightClass) / 400);
  9800. $offset += 2;
  9801. $offset += 2; // usWidthClass
  9802. $fsType = $this->_getSHORT($font, $offset);
  9803. $offset += 2;
  9804. if ($fsType == 2) {
  9805. $this->Error('This Font cannot be modified, embedded or exchanged in any manner without first obtaining permission of the legal owner.');
  9806. }
  9807. // ---------- get font name ----------
  9808. $fmetric['name'] = '';
  9809. $offset = $table['name']['offset'];
  9810. $offset += 2; // skip Format selector (=0).
  9811. // Number of NameRecords that follow n.
  9812. $numNameRecords = $this->_getUSHORT($font, $offset);
  9813. $offset += 2;
  9814. // Offset to start of string storage (from start of table).
  9815. $stringStorageOffset = $this->_getUSHORT($font, $offset);
  9816. $offset += 2;
  9817. for ($i = 0; $i < $numNameRecords; ++$i) {
  9818. $offset += 6; // skip Platform ID, Platform-specific encoding ID, Language ID.
  9819. // Name ID.
  9820. $nameID = $this->_getUSHORT($font, $offset);
  9821. $offset += 2;
  9822. if ($nameID == 6) {
  9823. // String length (in bytes).
  9824. $stringLength = $this->_getUSHORT($font, $offset);
  9825. $offset += 2;
  9826. // String offset from start of storage area (in bytes).
  9827. $stringOffset = $this->_getUSHORT($font, $offset);
  9828. $offset += 2;
  9829. $offset = ($table['name']['offset'] + $stringStorageOffset + $stringOffset);
  9830. $fmetric['name'] = substr($font, $offset, $stringLength);
  9831. $fmetric['name'] = preg_replace('/[^a-zA-Z0-9_\-]/', '', $fmetric['name']);
  9832. break;
  9833. } else {
  9834. $offset += 4; // skip String length, String offset
  9835. }
  9836. }
  9837. if (empty($fmetric['name'])) {
  9838. $fmetric['name'] = $font_name;
  9839. }
  9840. // ---------- get post data ----------
  9841. $offset = $table['post']['offset'];
  9842. $offset += 4; // skip Format Type
  9843. $fmetric['italicAngle'] = $this->_getFIXED($font, $offset);
  9844. $offset += 4;
  9845. $fmetric['underlinePosition'] = round($this->_getFWORD($font, $offset) * $urk);
  9846. $offset += 2;
  9847. $fmetric['underlineThickness'] = round($this->_getFWORD($font, $offset) * $urk);
  9848. $offset += 2;
  9849. $isFixedPitch = ($this->_getULONG($font, $offset) == 0) ? false : true;
  9850. $offset += 2;
  9851. if ($isFixedPitch) {
  9852. $fmetric['Flags'] |= 1;
  9853. }
  9854. // ---------- get hhea data ----------
  9855. $offset = $table['hhea']['offset'];
  9856. $offset += 4; // skip Table version number
  9857. // Ascender
  9858. $fmetric['Ascent'] = round($this->_getFWORD($font, $offset) * $urk);
  9859. $offset += 2;
  9860. // Descender
  9861. $fmetric['Descent'] = round($this->_getFWORD($font, $offset) * $urk);
  9862. $offset += 2;
  9863. // LineGap
  9864. $fmetric['Leading'] = round($this->_getFWORD($font, $offset) * $urk);
  9865. $offset += 2;
  9866. // advanceWidthMax
  9867. $fmetric['MaxWidth'] = round($this->_getUFWORD($font, $offset) * $urk);
  9868. $offset += 2;
  9869. $offset += 22; // skip some values
  9870. // get the number of hMetric entries in hmtx table
  9871. $numberOfHMetrics = $this->_getUSHORT($font, $offset);
  9872. // ---------- get maxp data ----------
  9873. $offset = $table['maxp']['offset'];
  9874. $offset += 4; // skip Table version number
  9875. // get the the number of glyphs in the font.
  9876. $numGlyphs = $this->_getUSHORT($font, $offset);
  9877. // ---------- get CIDToGIDMap ----------
  9878. $ctg = array();
  9879. foreach ($encodingTables as $enctable) {
  9880. if (($enctable['platformID'] == 3) AND ($enctable['encodingID'] == 0)) {
  9881. $modesymbol = true;
  9882. } else {
  9883. $modesymbol = false;
  9884. }
  9885. $offset = $table['cmap']['offset'] + $enctable['offset'];
  9886. $format = $this->_getUSHORT($font, $offset);
  9887. $offset += 2;
  9888. switch ($format) {
  9889. case 0: { // Format 0: Byte encoding table
  9890. $offset += 4; // skip length and version/language
  9891. for ($c = 0; $c < 256; ++$c) {
  9892. $g = $this->_getBYTE($font, $offset);
  9893. $ctg[$c] = $g;
  9894. ++$offset;
  9895. }
  9896. break;
  9897. }
  9898. case 2: { // Format 2: High-byte mapping through table
  9899. $offset += 4; // skip length and version/language
  9900. $numSubHeaders = 0;
  9901. for ($i = 0; $i < 256; ++$i) {
  9902. // Array that maps high bytes to subHeaders: value is subHeader index * 8.
  9903. $subHeaderKeys[$i] = ($this->_getUSHORT($font, $offset) / 8);
  9904. $offset += 2;
  9905. if ($numSubHeaders < $subHeaderKeys[$i]) {
  9906. $numSubHeaders = $subHeaderKeys[$i];
  9907. }
  9908. }
  9909. // the number of subHeaders is equal to the max of subHeaderKeys + 1
  9910. ++$numSubHeaders;
  9911. // read subHeader structures
  9912. $subHeaders = array();
  9913. $numGlyphIndexArray = 0;
  9914. for ($k = 0; $k < $numSubHeaders; ++$k) {
  9915. $subHeaders[$k]['firstCode'] = $this->_getUSHORT($font, $offset);
  9916. $offset += 2;
  9917. $subHeaders[$k]['entryCount'] = $this->_getUSHORT($font, $offset);
  9918. $offset += 2;
  9919. $subHeaders[$k]['idDelta'] = $this->_getSHORT($font, $offset);
  9920. $offset += 2;
  9921. $subHeaders[$k]['idRangeOffset'] = $this->_getUSHORT($font, $offset);
  9922. $offset += 2;
  9923. $subHeaders[$k]['idRangeOffset'] -= (2 + (($numSubHeaders - $k - 1) * 8));
  9924. $subHeaders[$k]['idRangeOffset'] /= 2;
  9925. $numGlyphIndexArray += $subHeaders[$k]['entryCount'];
  9926. }
  9927. for ($k = 0; $k < $numGlyphIndexArray; ++$k) {
  9928. $glyphIndexArray[$k] = $this->_getUSHORT($font, $offset);
  9929. $offset += 2;
  9930. }
  9931. for ($i = 0; $i < 256; ++$i) {
  9932. $k = $subHeaderKeys[$i];
  9933. if ($k == 0) {
  9934. // one byte code
  9935. $c = $i;
  9936. $g = $glyphIndexArray[0];
  9937. $ctg[$c] = $g;
  9938. } else {
  9939. // two bytes code
  9940. $start_byte = $subHeaders[$k]['firstCode'];
  9941. $end_byte = $start_byte + $subHeaders[$k]['entryCount'];
  9942. for ($j = $start_byte; $j < $end_byte; ++$j) {
  9943. // combine high and low bytes
  9944. $c = (($i << 8) + $j);
  9945. $idRangeOffset = ($subHeaders[$k]['idRangeOffset'] + $j - $subHeaders[$k]['firstCode']);
  9946. $g = $glyphIndexArray[$idRangeOffset];
  9947. $g += ($idDelta[$k] - 65536);
  9948. if ($g < 0) {
  9949. $g = 0;
  9950. }
  9951. $ctg[$c] = $g;
  9952. }
  9953. }
  9954. }
  9955. break;
  9956. }
  9957. case 4: { // Format 4: Segment mapping to delta values
  9958. $length = $this->_getUSHORT($font, $offset);
  9959. $offset += 2;
  9960. $offset += 2; // skip version/language
  9961. $segCount = ($this->_getUSHORT($font, $offset) / 2);
  9962. $offset += 2;
  9963. $offset += 6; // skip searchRange, entrySelector, rangeShift
  9964. $endCount = array(); // array of end character codes for each segment
  9965. for ($k = 0; $k < $segCount; ++$k) {
  9966. $endCount[$k] = $this->_getUSHORT($font, $offset);
  9967. $offset += 2;
  9968. }
  9969. $offset += 2; // skip reservedPad
  9970. $startCount = array(); // array of start character codes for each segment
  9971. for ($k = 0; $k < $segCount; ++$k) {
  9972. $startCount[$k] = $this->_getUSHORT($font, $offset);
  9973. $offset += 2;
  9974. }
  9975. $idDelta = array(); // delta for all character codes in segment
  9976. for ($k = 0; $k < $segCount; ++$k) {
  9977. $idDelta[$k] = $this->_getUSHORT($font, $offset);
  9978. $offset += 2;
  9979. }
  9980. $idRangeOffset = array(); // Offsets into glyphIdArray or 0
  9981. for ($k = 0; $k < $segCount; ++$k) {
  9982. $idRangeOffset[$k] = $this->_getUSHORT($font, $offset);
  9983. $offset += 2;
  9984. }
  9985. $gidlen = ($length / 2) - 8 - (4 * $segCount);
  9986. $glyphIdArray = array(); // glyph index array
  9987. for ($k = 0; $k < $gidlen; ++$k) {
  9988. $glyphIdArray[$k] = $this->_getUSHORT($font, $offset);
  9989. $offset += 2;
  9990. }
  9991. for ($k = 0; $k < $segCount; ++$k) {
  9992. for ($c = $startCount[$k]; $c <= $endCount[$k]; ++$c) {
  9993. if ($idRangeOffset[$k] == 0) {
  9994. $g = $c;
  9995. } else {
  9996. $gid = (($idRangeOffset[$k] / 2) + ($c - $startCount[$k]) - ($segCount - $k));
  9997. $g = $glyphIdArray[$gid];
  9998. }
  9999. $g += ($idDelta[$k] - 65536);
  10000. if ($g < 0) {
  10001. $g = 0;
  10002. }
  10003. $ctg[$c] = $g;
  10004. }
  10005. }
  10006. break;
  10007. }
  10008. case 6: { // Format 6: Trimmed table mapping
  10009. $offset += 4; // skip length and version/language
  10010. $firstCode = $this->_getUSHORT($font, $offset);
  10011. $offset += 2;
  10012. $entryCount = $this->_getUSHORT($font, $offset);
  10013. $offset += 2;
  10014. for ($k = 0; $k < $entryCount; ++$k) {
  10015. $c = ($k + $firstCode);
  10016. $g = $this->_getUSHORT($font, $offset);
  10017. $ctg[$c] = $g;
  10018. $offset += 2;
  10019. }
  10020. break;
  10021. }
  10022. case 8: { // Format 8: Mixed 16-bit and 32-bit coverage
  10023. $offset += 10; // skip reserved, length and version/language
  10024. for ($k = 0; $k < 8192; ++$k) {
  10025. $is32[$k] = $this->_getBYTE($font, $offset);
  10026. ++$offset;
  10027. }
  10028. $nGroups = $this->_getULONG($font, $offset);
  10029. $offset += 4;
  10030. for ($i = 0; $i < $nGroups; ++$i) {
  10031. $startCharCode = $this->_getULONG($font, $offset);
  10032. $offset += 4;
  10033. $endCharCode = $this->_getULONG($font, $offset);
  10034. $offset += 4;
  10035. $startGlyphID = $this->_getULONG($font, $offset);
  10036. $offset += 4;
  10037. for ($k = $startCharCode; $k <= $endCharCode; ++$k) {
  10038. $is32idx = floor($c / 8);
  10039. if ((isset($is32[$is32idx])) AND (($is32[$is32idx] & (1 << (7 - ($c % 8)))) == 0)) {
  10040. $c = $k;
  10041. } else {
  10042. // 32 bit format
  10043. // convert to decimal (http://www.unicode.org/faq//utf_bom.html#utf16-4)
  10044. //LEAD_OFFSET = (0xD800 - (0x10000 >> 10)) = 55232
  10045. //SURROGATE_OFFSET = (0x10000 - (0xD800 << 10) - 0xDC00) = -56613888
  10046. $c = ((55232 + ($k >> 10)) << 10) + (0xDC00 + ($k & 0x3FF)) -56613888;
  10047. }
  10048. $ctg[$c] = $g;
  10049. ++$startGlyphID;
  10050. }
  10051. }
  10052. break;
  10053. }
  10054. case 10: { // Format 10: Trimmed array
  10055. $offset += 10; // skip reserved, length and version/language
  10056. $startCharCode = $this->_getULONG($font, $offset);
  10057. $offset += 4;
  10058. $numChars = $this->_getULONG($font, $offset);
  10059. $offset += 4;
  10060. for ($k = 0; $k < $numChars; ++$k) {
  10061. $c = ($k + $startCharCode);
  10062. $g = $this->_getUSHORT($font, $offset);
  10063. $ctg[$c] = $g;
  10064. $offset += 2;
  10065. }
  10066. break;
  10067. }
  10068. case 12: { // Format 12: Segmented coverage
  10069. $offset += 10; // skip length and version/language
  10070. $nGroups = $this->_getULONG($font, $offset);
  10071. $offset += 4;
  10072. for ($k = 0; $k < $nGroups; ++$k) {
  10073. $startCharCode = $this->_getULONG($font, $offset);
  10074. $offset += 4;
  10075. $endCharCode = $this->_getULONG($font, $offset);
  10076. $offset += 4;
  10077. $startGlyphCode = $this->_getULONG($font, $offset);
  10078. $offset += 4;
  10079. for ($c = $startCharCode; $c <= $endCharCode; ++$c) {
  10080. $ctg[$c] = $startGlyphCode;
  10081. ++$startGlyphCode;
  10082. }
  10083. }
  10084. break;
  10085. }
  10086. case 13: { // Format 13: Many-to-one range mappings
  10087. // to be implemented ...
  10088. break;
  10089. }
  10090. case 14: { // Format 14: Unicode Variation Sequences
  10091. // to be implemented ...
  10092. break;
  10093. }
  10094. }
  10095. }
  10096. if (!isset($ctg[0])) {
  10097. $ctg[0] = 0;
  10098. }
  10099. // get xHeight (height of x)
  10100. $offset = ($table['glyf']['offset'] + $indexToLoc[$ctg[120]] + 4);
  10101. $yMin = $this->_getFWORD($font, $offset);
  10102. $offset += 4;
  10103. $yMax = $this->_getFWORD($font, $offset);
  10104. $offset += 2;
  10105. $fmetric['XHeight'] = round(($yMax - $yMin) * $urk);
  10106. // get CapHeight (height of H)
  10107. $offset = ($table['glyf']['offset'] + $indexToLoc[$ctg[72]] + 4);
  10108. $yMin = $this->_getFWORD($font, $offset);
  10109. $offset += 4;
  10110. $yMax = $this->_getFWORD($font, $offset);
  10111. $offset += 2;
  10112. $fmetric['CapHeight'] = round(($yMax - $yMin) * $urk);
  10113. // ceate widths array
  10114. $cw = array();
  10115. $offset = $table['hmtx']['offset'];
  10116. for ($i = 0 ; $i < $numberOfHMetrics; ++$i) {
  10117. $cw[$i] = round($this->_getUFWORD($font, $offset) * $urk);
  10118. $offset += 4; // skip lsb
  10119. }
  10120. if ($numberOfHMetrics < $numGlyphs) {
  10121. // fill missing widths with the last value
  10122. $cw = array_pad($cw, $numGlyphs, $cw[($numberOfHMetrics - 1)]);
  10123. }
  10124. $fmetric['MissingWidth'] = $cw[0];
  10125. $fmetric['cw'] = '';
  10126. for ($cid = 0; $cid <= 65535; ++$cid) {
  10127. if (isset($ctg[$cid]) AND isset($cw[$ctg[$cid]])) {
  10128. $fmetric['cw'] .= ','.$cid.'=>'.$cw[$ctg[$cid]];
  10129. }
  10130. }
  10131. } // end of true type
  10132. if (($fmetric['type'] == 'TrueTypeUnicode') AND (count($ctg) == 256)) {
  10133. $fmetric['type'] == 'TrueType';
  10134. }
  10135. // ---------- create php font file ----------
  10136. $pfile = '<'.'?'.'php'."\n";
  10137. $pfile .= '// TCPDF FONT FILE DESCRIPTION'."\n";
  10138. $pfile .= '$type=\''.$fmetric['type'].'\';'."\n";
  10139. $pfile .= '$name=\''.$fmetric['name'].'\';'."\n";
  10140. $pfile .= '$up='.$fmetric['underlinePosition'].';'."\n";
  10141. $pfile .= '$ut='.$fmetric['underlineThickness'].';'."\n";
  10142. if ($fmetric['MissingWidth'] > 0) {
  10143. $pfile .= '$dw='.$fmetric['MissingWidth'].';'."\n";
  10144. } else {
  10145. $pfile .= '$dw='.$fmetric['AvgWidth'].';'."\n";
  10146. }
  10147. $pfile .= '$diff=\''.$fmetric['diff'].'\';'."\n";
  10148. if ($fmetric['type'] == 'Type1') {
  10149. // Type 1
  10150. $pfile .= '$enc=\''.$fmetric['enc'].'\';'."\n";
  10151. $pfile .= '$file=\''.$fmetric['file'].'\';'."\n";
  10152. $pfile .= '$size1='.$fmetric['size1'].';'."\n";
  10153. $pfile .= '$size2='.$fmetric['size2'].';'."\n";
  10154. } else {
  10155. $pfile .= '$originalsize='.$fmetric['originalsize'].';'."\n";
  10156. if ($fmetric['type'] == 'cidfont0') {
  10157. // CID-0
  10158. switch ($fonttype) {
  10159. case 'CID0JP': {
  10160. $pfile .= '// Japanese'."\n";
  10161. $pfile .= '$enc=\'UniJIS-UTF16-H\';'."\n";
  10162. $pfile .= '$cidinfo=array(\'Registry\'=>\'Adobe\', \'Ordering\'=>\'Japan1\',\'Supplement\'=>5);'."\n";
  10163. $pfile .= 'include(dirname(__FILE__).\'/uni2cid_aj16.php\');'."\n";
  10164. break;
  10165. }
  10166. case 'CID0KR': {
  10167. $pfile .= '// Korean'."\n";
  10168. $pfile .= '$enc=\'UniKS-UTF16-H\';'."\n";
  10169. $pfile .= '$cidinfo=array(\'Registry\'=>\'Adobe\', \'Ordering\'=>\'Korea1\',\'Supplement\'=>0);'."\n";
  10170. $pfile .= 'include(dirname(__FILE__).\'/uni2cid_ak12.php\');'."\n";
  10171. break;
  10172. }
  10173. case 'CID0CS': {
  10174. $pfile .= '// Chinese Simplified'."\n";
  10175. $pfile .= '$enc=\'UniGB-UTF16-H\';'."\n";
  10176. $pfile .= '$cidinfo=array(\'Registry\'=>\'Adobe\', \'Ordering\'=>\'GB1\',\'Supplement\'=>2);'."\n";
  10177. $pfile .= 'include(dirname(__FILE__).\'/uni2cid_ag15.php\');'."\n";
  10178. break;
  10179. }
  10180. case 'CID0CT':
  10181. default: {
  10182. $pfile .= '// Chinese Traditional'."\n";
  10183. $pfile .= '$enc=\'UniCNS-UTF16-H\';'."\n";
  10184. $pfile .= '$cidinfo=array(\'Registry\'=>\'Adobe\', \'Ordering\'=>\'CNS1\',\'Supplement\'=>0);'."\n";
  10185. $pfile .= 'include(dirname(__FILE__).\'/uni2cid_aj16.php\');'."\n";
  10186. break;
  10187. }
  10188. }
  10189. } else {
  10190. // TrueType
  10191. $pfile .= '$enc=\''.$fmetric['enc'].'\';'."\n";
  10192. $pfile .= '$file=\''.$fmetric['file'].'\';'."\n";
  10193. $pfile .= '$ctg=\''.$fmetric['ctg'].'\';'."\n";
  10194. // create CIDToGIDMap
  10195. $cidtogidmap = str_pad('', 131072, "\x00"); // (256 * 256 * 2) = 131072
  10196. foreach ($ctg as $cid => $gid) {
  10197. $cidtogidmap = $this->updateCIDtoGIDmap($cidtogidmap, $cid, $ctg[$cid]);
  10198. }
  10199. // store compressed CIDToGIDMap
  10200. $fp = fopen($outpath.$fmetric['ctg'], 'wb');
  10201. fwrite($fp, gzcompress($cidtogidmap));
  10202. fclose($fp);
  10203. }
  10204. }
  10205. $pfile .= '$desc=array(';
  10206. $pfile .= '\'Flags\'=>'.$fmetric['Flags'].',';
  10207. $pfile .= '\'FontBBox\'=>\'['.$fmetric['bbox'].']\',';
  10208. $pfile .= '\'ItalicAngle\'=>'.$fmetric['italicAngle'].',';
  10209. $pfile .= '\'Ascent\'=>'.$fmetric['Ascent'].',';
  10210. $pfile .= '\'Descent\'=>'.$fmetric['Descent'].',';
  10211. $pfile .= '\'Leading\'=>'.$fmetric['Leading'].',';
  10212. $pfile .= '\'CapHeight\'=>'.$fmetric['CapHeight'].',';
  10213. $pfile .= '\'XHeight\'=>'.$fmetric['XHeight'].',';
  10214. $pfile .= '\'StemV\'=>'.$fmetric['StemV'].',';
  10215. $pfile .= '\'StemH\'=>'.$fmetric['StemH'].',';
  10216. $pfile .= '\'AvgWidth\'=>'.$fmetric['AvgWidth'].',';
  10217. $pfile .= '\'MaxWidth\'=>'.$fmetric['MaxWidth'].',';
  10218. $pfile .= '\'MissingWidth\'=>'.$fmetric['MissingWidth'].'';
  10219. $pfile .= ');'."\n";
  10220. $pfile .= '$cw=array('.substr($fmetric['cw'], 1).');'."\n";
  10221. $pfile .= '// --- EOF ---'."\n";
  10222. // store file
  10223. $fp = fopen($outpath.$font_name.'.php', 'w');
  10224. fwrite($fp, $pfile);
  10225. fclose($fp);
  10226. // return TCPDF font name
  10227. return $font_name;
  10228. }
  10229. /**
  10230. * Returns a subset of the TrueType font data without the unused glyphs.
  10231. * @param $font (string) TrueType font data.
  10232. * @param $subsetchars (array) Array of used characters (the glyphs to keep).
  10233. * @return (string) A subset of TrueType font data without the unused glyphs.
  10234. * @author Nicola Asuni
  10235. * @protected
  10236. * @since 5.2.000 (2010-06-02)
  10237. */
  10238. protected function _getTrueTypeFontSubset($font, $subsetchars) {
  10239. ksort($subsetchars);
  10240. $offset = 0; // offset position of the font data
  10241. if ($this->_getULONG($font, $offset) != 0x10000) {
  10242. // sfnt version must be 0x00010000 for TrueType version 1.0.
  10243. return $font;
  10244. }
  10245. $offset += 4;
  10246. // get number of tables
  10247. $numTables = $this->_getUSHORT($font, $offset);
  10248. $offset += 2;
  10249. // skip searchRange, entrySelector and rangeShift
  10250. $offset += 6;
  10251. // tables array
  10252. $table = array();
  10253. // for each table
  10254. for ($i = 0; $i < $numTables; ++$i) {
  10255. // get table info
  10256. $tag = substr($font, $offset, 4);
  10257. $offset += 4;
  10258. $table[$tag] = array();
  10259. $table[$tag]['checkSum'] = $this->_getULONG($font, $offset);
  10260. $offset += 4;
  10261. $table[$tag]['offset'] = $this->_getULONG($font, $offset);
  10262. $offset += 4;
  10263. $table[$tag]['length'] = $this->_getULONG($font, $offset);
  10264. $offset += 4;
  10265. }
  10266. // check magicNumber
  10267. $offset = $table['head']['offset'] + 12;
  10268. if ($this->_getULONG($font, $offset) != 0x5F0F3CF5) {
  10269. // magicNumber must be 0x5F0F3CF5
  10270. return $font;
  10271. }
  10272. $offset += 4;
  10273. // get offset mode (indexToLocFormat : 0 = short, 1 = long)
  10274. $offset = $table['head']['offset'] + 50;
  10275. $short_offset = ($this->_getSHORT($font, $offset) == 0);
  10276. $offset += 2;
  10277. // get the offsets to the locations of the glyphs in the font, relative to the beginning of the glyphData table
  10278. $indexToLoc = array();
  10279. $offset = $table['loca']['offset'];
  10280. if ($short_offset) {
  10281. // short version
  10282. $tot_num_glyphs = ($table['loca']['length'] / 2); // numGlyphs + 1
  10283. for ($i = 0; $i < $tot_num_glyphs; ++$i) {
  10284. $indexToLoc[$i] = $this->_getUSHORT($font, $offset) * 2;
  10285. $offset += 2;
  10286. }
  10287. } else {
  10288. // long version
  10289. $tot_num_glyphs = ($table['loca']['length'] / 4); // numGlyphs + 1
  10290. for ($i = 0; $i < $tot_num_glyphs; ++$i) {
  10291. $indexToLoc[$i] = $this->_getULONG($font, $offset);
  10292. $offset += 4;
  10293. }
  10294. }
  10295. // get glyphs indexes of chars from cmap table
  10296. $subsetglyphs = array(); // glyph IDs on key
  10297. $subsetglyphs[0] = true; // character codes that do not correspond to any glyph in the font should be mapped to glyph index 0
  10298. $offset = $table['cmap']['offset'] + 2;
  10299. $numEncodingTables = $this->_getUSHORT($font, $offset);
  10300. $offset += 2;
  10301. $encodingTables = array();
  10302. for ($i = 0; $i < $numEncodingTables; ++$i) {
  10303. $encodingTables[$i]['platformID'] = $this->_getUSHORT($font, $offset);
  10304. $offset += 2;
  10305. $encodingTables[$i]['encodingID'] = $this->_getUSHORT($font, $offset);
  10306. $offset += 2;
  10307. $encodingTables[$i]['offset'] = $this->_getULONG($font, $offset);
  10308. $offset += 4;
  10309. }
  10310. foreach ($encodingTables as $enctable) {
  10311. if (($enctable['platformID'] == 3) AND ($enctable['encodingID'] == 0)) {
  10312. $modesymbol = true;
  10313. } else {
  10314. $modesymbol = false;
  10315. }
  10316. $offset = $table['cmap']['offset'] + $enctable['offset'];
  10317. $format = $this->_getUSHORT($font, $offset);
  10318. $offset += 2;
  10319. switch ($format) {
  10320. case 0: { // Format 0: Byte encoding table
  10321. $offset += 4; // skip length and version/language
  10322. for ($c = 0; $c < 256; ++$c) {
  10323. if (isset($subsetchars[$c])) {
  10324. $g = $this->_getBYTE($font, $offset);
  10325. $subsetglyphs[$g] = true;
  10326. }
  10327. ++$offset;
  10328. }
  10329. break;
  10330. }
  10331. case 2: { // Format 2: High-byte mapping through table
  10332. $offset += 4; // skip length and version/language
  10333. $numSubHeaders = 0;
  10334. for ($i = 0; $i < 256; ++$i) {
  10335. // Array that maps high bytes to subHeaders: value is subHeader index * 8.
  10336. $subHeaderKeys[$i] = ($this->_getUSHORT($font, $offset) / 8);
  10337. $offset += 2;
  10338. if ($numSubHeaders < $subHeaderKeys[$i]) {
  10339. $numSubHeaders = $subHeaderKeys[$i];
  10340. }
  10341. }
  10342. // the number of subHeaders is equal to the max of subHeaderKeys + 1
  10343. ++$numSubHeaders;
  10344. // read subHeader structures
  10345. $subHeaders = array();
  10346. $numGlyphIndexArray = 0;
  10347. for ($k = 0; $k < $numSubHeaders; ++$k) {
  10348. $subHeaders[$k]['firstCode'] = $this->_getUSHORT($font, $offset);
  10349. $offset += 2;
  10350. $subHeaders[$k]['entryCount'] = $this->_getUSHORT($font, $offset);
  10351. $offset += 2;
  10352. $subHeaders[$k]['idDelta'] = $this->_getSHORT($font, $offset);
  10353. $offset += 2;
  10354. $subHeaders[$k]['idRangeOffset'] = $this->_getUSHORT($font, $offset);
  10355. $offset += 2;
  10356. $subHeaders[$k]['idRangeOffset'] -= (2 + (($numSubHeaders - $k - 1) * 8));
  10357. $subHeaders[$k]['idRangeOffset'] /= 2;
  10358. $numGlyphIndexArray += $subHeaders[$k]['entryCount'];
  10359. }
  10360. for ($k = 0; $k < $numGlyphIndexArray; ++$k) {
  10361. $glyphIndexArray[$k] = $this->_getUSHORT($font, $offset);
  10362. $offset += 2;
  10363. }
  10364. for ($i = 0; $i < 256; ++$i) {
  10365. $k = $subHeaderKeys[$i];
  10366. if ($k == 0) {
  10367. // one byte code
  10368. $c = $i;
  10369. if (isset($subsetchars[$c])) {
  10370. $g = $glyphIndexArray[0];
  10371. $subsetglyphs[$g] = true;
  10372. }
  10373. } else {
  10374. // two bytes code
  10375. $start_byte = $subHeaders[$k]['firstCode'];
  10376. $end_byte = $start_byte + $subHeaders[$k]['entryCount'];
  10377. for ($j = $start_byte; $j < $end_byte; ++$j) {
  10378. // combine high and low bytes
  10379. $c = (($i << 8) + $j);
  10380. if (isset($subsetchars[$c])) {
  10381. $idRangeOffset = ($subHeaders[$k]['idRangeOffset'] + $j - $subHeaders[$k]['firstCode']);
  10382. $g = $glyphIndexArray[$idRangeOffset];
  10383. $g += ($idDelta[$k] - 65536);
  10384. if ($g < 0) {
  10385. $g = 0;
  10386. }
  10387. $subsetglyphs[$g] = true;
  10388. }
  10389. }
  10390. }
  10391. }
  10392. break;
  10393. }
  10394. case 4: { // Format 4: Segment mapping to delta values
  10395. $length = $this->_getUSHORT($font, $offset);
  10396. $offset += 2;
  10397. $offset += 2; // skip version/language
  10398. $segCount = ($this->_getUSHORT($font, $offset) / 2);
  10399. $offset += 2;
  10400. $offset += 6; // skip searchRange, entrySelector, rangeShift
  10401. $endCount = array(); // array of end character codes for each segment
  10402. for ($k = 0; $k < $segCount; ++$k) {
  10403. $endCount[$k] = $this->_getUSHORT($font, $offset);
  10404. $offset += 2;
  10405. }
  10406. $offset += 2; // skip reservedPad
  10407. $startCount = array(); // array of start character codes for each segment
  10408. for ($k = 0; $k < $segCount; ++$k) {
  10409. $startCount[$k] = $this->_getUSHORT($font, $offset);
  10410. $offset += 2;
  10411. }
  10412. $idDelta = array(); // delta for all character codes in segment
  10413. for ($k = 0; $k < $segCount; ++$k) {
  10414. $idDelta[$k] = $this->_getUSHORT($font, $offset);
  10415. $offset += 2;
  10416. }
  10417. $idRangeOffset = array(); // Offsets into glyphIdArray or 0
  10418. for ($k = 0; $k < $segCount; ++$k) {
  10419. $idRangeOffset[$k] = $this->_getUSHORT($font, $offset);
  10420. $offset += 2;
  10421. }
  10422. $gidlen = ($length / 2) - 8 - (4 * $segCount);
  10423. $glyphIdArray = array(); // glyph index array
  10424. for ($k = 0; $k < $gidlen; ++$k) {
  10425. $glyphIdArray[$k] = $this->_getUSHORT($font, $offset);
  10426. $offset += 2;
  10427. }
  10428. for ($k = 0; $k < $segCount; ++$k) {
  10429. for ($c = $startCount[$k]; $c <= $endCount[$k]; ++$c) {
  10430. if (isset($subsetchars[$c])) {
  10431. if ($idRangeOffset[$k] == 0) {
  10432. $g = $c;
  10433. } else {
  10434. $gid = (($idRangeOffset[$k] / 2) + ($c - $startCount[$k]) - ($segCount - $k));
  10435. $g = $glyphIdArray[$gid];
  10436. }
  10437. $g += ($idDelta[$k] - 65536);
  10438. if ($g < 0) {
  10439. $g = 0;
  10440. }
  10441. $subsetglyphs[$g] = true;
  10442. }
  10443. }
  10444. }
  10445. break;
  10446. }
  10447. case 6: { // Format 6: Trimmed table mapping
  10448. $offset += 4; // skip length and version/language
  10449. $firstCode = $this->_getUSHORT($font, $offset);
  10450. $offset += 2;
  10451. $entryCount = $this->_getUSHORT($font, $offset);
  10452. $offset += 2;
  10453. for ($k = 0; $k < $entryCount; ++$k) {
  10454. $c = ($k + $firstCode);
  10455. if (isset($subsetchars[$c])) {
  10456. $g = $this->_getUSHORT($font, $offset);
  10457. $subsetglyphs[$g] = true;
  10458. }
  10459. $offset += 2;
  10460. }
  10461. break;
  10462. }
  10463. case 8: { // Format 8: Mixed 16-bit and 32-bit coverage
  10464. $offset += 10; // skip reserved, length and version/language
  10465. for ($k = 0; $k < 8192; ++$k) {
  10466. $is32[$k] = $this->_getBYTE($font, $offset);
  10467. ++$offset;
  10468. }
  10469. $nGroups = $this->_getULONG($font, $offset);
  10470. $offset += 4;
  10471. for ($i = 0; $i < $nGroups; ++$i) {
  10472. $startCharCode = $this->_getULONG($font, $offset);
  10473. $offset += 4;
  10474. $endCharCode = $this->_getULONG($font, $offset);
  10475. $offset += 4;
  10476. $startGlyphID = $this->_getULONG($font, $offset);
  10477. $offset += 4;
  10478. for ($k = $startCharCode; $k <= $endCharCode; ++$k) {
  10479. $is32idx = floor($c / 8);
  10480. if ((isset($is32[$is32idx])) AND (($is32[$is32idx] & (1 << (7 - ($c % 8)))) == 0)) {
  10481. $c = $k;
  10482. } else {
  10483. // 32 bit format
  10484. // convert to decimal (http://www.unicode.org/faq//utf_bom.html#utf16-4)
  10485. //LEAD_OFFSET = (0xD800 - (0x10000 >> 10)) = 55232
  10486. //SURROGATE_OFFSET = (0x10000 - (0xD800 << 10) - 0xDC00) = -56613888
  10487. $c = ((55232 + ($k >> 10)) << 10) + (0xDC00 + ($k & 0x3FF)) -56613888;
  10488. }
  10489. if (isset($subsetchars[$c])) {
  10490. $subsetglyphs[$startGlyphID] = true;
  10491. }
  10492. ++$startGlyphID;
  10493. }
  10494. }
  10495. break;
  10496. }
  10497. case 10: { // Format 10: Trimmed array
  10498. $offset += 10; // skip reserved, length and version/language
  10499. $startCharCode = $this->_getULONG($font, $offset);
  10500. $offset += 4;
  10501. $numChars = $this->_getULONG($font, $offset);
  10502. $offset += 4;
  10503. for ($k = 0; $k < $numChars; ++$k) {
  10504. $c = ($k + $startCharCode);
  10505. if (isset($subsetchars[$c])) {
  10506. $g = $this->_getUSHORT($font, $offset);
  10507. $subsetglyphs[$g] = true;
  10508. }
  10509. $offset += 2;
  10510. }
  10511. break;
  10512. }
  10513. case 12: { // Format 12: Segmented coverage
  10514. $offset += 10; // skip length and version/language
  10515. $nGroups = $this->_getULONG($font, $offset);
  10516. $offset += 4;
  10517. for ($k = 0; $k < $nGroups; ++$k) {
  10518. $startCharCode = $this->_getULONG($font, $offset);
  10519. $offset += 4;
  10520. $endCharCode = $this->_getULONG($font, $offset);
  10521. $offset += 4;
  10522. $startGlyphCode = $this->_getULONG($font, $offset);
  10523. $offset += 4;
  10524. for ($c = $startCharCode; $c <= $endCharCode; ++$c) {
  10525. if (isset($subsetchars[$c])) {
  10526. $subsetglyphs[$startGlyphCode] = true;
  10527. }
  10528. ++$startGlyphCode;
  10529. }
  10530. }
  10531. break;
  10532. }
  10533. case 13: { // Format 13: Many-to-one range mappings
  10534. // to be implemented ...
  10535. break;
  10536. }
  10537. case 14: { // Format 14: Unicode Variation Sequences
  10538. // to be implemented ...
  10539. break;
  10540. }
  10541. }
  10542. }
  10543. // include all parts of composite glyphs
  10544. $new_sga = $subsetglyphs;
  10545. while (!empty($new_sga)) {
  10546. $sga = $new_sga;
  10547. $new_sga = array();
  10548. foreach ($sga as $key => $val) {
  10549. if (isset($indexToLoc[$key])) {
  10550. $offset = ($table['glyf']['offset'] + $indexToLoc[$key]);
  10551. $numberOfContours = $this->_getSHORT($font, $offset);
  10552. $offset += 2;
  10553. if ($numberOfContours < 0) { // composite glyph
  10554. $offset += 8; // skip xMin, yMin, xMax, yMax
  10555. do {
  10556. $flags = $this->_getUSHORT($font, $offset);
  10557. $offset += 2;
  10558. $glyphIndex = $this->_getUSHORT($font, $offset);
  10559. $offset += 2;
  10560. if (!isset($subsetglyphs[$glyphIndex])) {
  10561. // add missing glyphs
  10562. $new_sga[$glyphIndex] = true;
  10563. }
  10564. // skip some bytes by case
  10565. if ($flags & 1) {
  10566. $offset += 4;
  10567. } else {
  10568. $offset += 2;
  10569. }
  10570. if ($flags & 8) {
  10571. $offset += 2;
  10572. } elseif ($flags & 64) {
  10573. $offset += 4;
  10574. } elseif ($flags & 128) {
  10575. $offset += 8;
  10576. }
  10577. } while ($flags & 32);
  10578. }
  10579. }
  10580. }
  10581. $subsetglyphs += $new_sga;
  10582. }
  10583. // sort glyphs by key (and remove duplicates)
  10584. ksort($subsetglyphs);
  10585. // build new glyf and loca tables
  10586. $glyf = '';
  10587. $loca = '';
  10588. $offset = 0;
  10589. $glyf_offset = $table['glyf']['offset'];
  10590. for ($i = 0; $i < $tot_num_glyphs; ++$i) {
  10591. if (isset($subsetglyphs[$i])) {
  10592. $length = ($indexToLoc[($i + 1)] - $indexToLoc[$i]);
  10593. $glyf .= substr($font, ($glyf_offset + $indexToLoc[$i]), $length);
  10594. } else {
  10595. $length = 0;
  10596. }
  10597. if ($short_offset) {
  10598. $loca .= pack('n', ($offset / 2));
  10599. } else {
  10600. $loca .= pack('N', $offset);
  10601. }
  10602. $offset += $length;
  10603. }
  10604. // array of table names to preserve (loca and glyf tables will be added later)
  10605. // the cmap table is not needed and shall not be present, since the mapping from character codes to glyph descriptions is provided separately
  10606. $table_names = array ('head', 'hhea', 'hmtx', 'maxp', 'cvt ', 'fpgm', 'prep'); // minimum required table names
  10607. // get the tables to preserve
  10608. $offset = 12;
  10609. foreach ($table as $tag => $val) {
  10610. if (in_array($tag, $table_names)) {
  10611. $table[$tag]['data'] = substr($font, $table[$tag]['offset'], $table[$tag]['length']);
  10612. if ($tag == 'head') {
  10613. // set the checkSumAdjustment to 0
  10614. $table[$tag]['data'] = substr($table[$tag]['data'], 0, 8)."\x0\x0\x0\x0".substr($table[$tag]['data'], 12);
  10615. }
  10616. $pad = 4 - ($table[$tag]['length'] % 4);
  10617. if ($pad != 4) {
  10618. // the length of a table must be a multiple of four bytes
  10619. $table[$tag]['length'] += $pad;
  10620. $table[$tag]['data'] .= str_repeat("\x0", $pad);
  10621. }
  10622. $table[$tag]['offset'] = $offset;
  10623. $offset += $table[$tag]['length'];
  10624. // check sum is not changed (so keep the following line commented)
  10625. //$table[$tag]['checkSum'] = $this->_getTTFtableChecksum($table[$tag]['data'], $table[$tag]['length']);
  10626. } else {
  10627. unset($table[$tag]);
  10628. }
  10629. }
  10630. // add loca
  10631. $table['loca']['data'] = $loca;
  10632. $table['loca']['length'] = strlen($loca);
  10633. $pad = 4 - ($table['loca']['length'] % 4);
  10634. if ($pad != 4) {
  10635. // the length of a table must be a multiple of four bytes
  10636. $table['loca']['length'] += $pad;
  10637. $table['loca']['data'] .= str_repeat("\x0", $pad);
  10638. }
  10639. $table['loca']['offset'] = $offset;
  10640. $table['loca']['checkSum'] = $this->_getTTFtableChecksum($table['loca']['data'], $table['loca']['length']);
  10641. $offset += $table['loca']['length'];
  10642. // add glyf
  10643. $table['glyf']['data'] = $glyf;
  10644. $table['glyf']['length'] = strlen($glyf);
  10645. $pad = 4 - ($table['glyf']['length'] % 4);
  10646. if ($pad != 4) {
  10647. // the length of a table must be a multiple of four bytes
  10648. $table['glyf']['length'] += $pad;
  10649. $table['glyf']['data'] .= str_repeat("\x0", $pad);
  10650. }
  10651. $table['glyf']['offset'] = $offset;
  10652. $table['glyf']['checkSum'] = $this->_getTTFtableChecksum($table['glyf']['data'], $table['glyf']['length']);
  10653. // rebuild font
  10654. $font = '';
  10655. $font .= pack('N', 0x10000); // sfnt version
  10656. $numTables = count($table);
  10657. $font .= pack('n', $numTables); // numTables
  10658. $entrySelector = floor(log($numTables, 2));
  10659. $searchRange = pow(2, $entrySelector) * 16;
  10660. $rangeShift = ($numTables * 16) - $searchRange;
  10661. $font .= pack('n', $searchRange); // searchRange
  10662. $font .= pack('n', $entrySelector); // entrySelector
  10663. $font .= pack('n', $rangeShift); // rangeShift
  10664. $offset = ($numTables * 16);
  10665. foreach ($table as $tag => $data) {
  10666. $font .= $tag; // tag
  10667. $font .= pack('N', $data['checkSum']); // checkSum
  10668. $font .= pack('N', ($data['offset'] + $offset)); // offset
  10669. $font .= pack('N', $data['length']); // length
  10670. }
  10671. foreach ($table as $data) {
  10672. $font .= $data['data'];
  10673. }
  10674. // set checkSumAdjustment on head table
  10675. $checkSumAdjustment = 0xB1B0AFBA - $this->_getTTFtableChecksum($font, strlen($font));
  10676. $font = substr($font, 0, $table['head']['offset'] + 8).pack('N', $checkSumAdjustment).substr($font, $table['head']['offset'] + 12);
  10677. return $font;
  10678. }
  10679. /**
  10680. * Returs the checksum of a TTF table.
  10681. * @param $table (string) table to check
  10682. * @param $length (int) length of table in bytes
  10683. * @return int checksum
  10684. * @author Nicola Asuni
  10685. * @protected
  10686. * @since 5.2.000 (2010-06-02)
  10687. */
  10688. protected function _getTTFtableChecksum($table, $length) {
  10689. $sum = 0;
  10690. $tlen = ($length / 4);
  10691. $offset = 0;
  10692. for ($i = 0; $i < $tlen; ++$i) {
  10693. $v = unpack('Ni', substr($table, $offset, 4));
  10694. $sum += $v['i'];
  10695. $offset += 4;
  10696. }
  10697. $sum = unpack('Ni', pack('N', $sum));
  10698. return $sum['i'];
  10699. }
  10700. /**
  10701. * Outputs font widths
  10702. * @param $font (array) font data
  10703. * @param $cidoffset (int) offset for CID values
  10704. * @return PDF command string for font widths
  10705. * @author Nicola Asuni
  10706. * @protected
  10707. * @since 4.4.000 (2008-12-07)
  10708. */
  10709. protected function _putfontwidths($font, $cidoffset=0) {
  10710. ksort($font['cw']);
  10711. $rangeid = 0;
  10712. $range = array();
  10713. $prevcid = -2;
  10714. $prevwidth = -1;
  10715. $interval = false;
  10716. // for each character
  10717. foreach ($font['cw'] as $cid => $width) {
  10718. $cid -= $cidoffset;
  10719. if ($font['subset'] AND ($cid > 255) AND (!isset($font['subsetchars'][$cid]))) {
  10720. // ignore the unused characters (font subsetting)
  10721. continue;
  10722. }
  10723. if ($width != $font['dw']) {
  10724. if ($cid == ($prevcid + 1)) {
  10725. // consecutive CID
  10726. if ($width == $prevwidth) {
  10727. if ($width == $range[$rangeid][0]) {
  10728. $range[$rangeid][] = $width;
  10729. } else {
  10730. array_pop($range[$rangeid]);
  10731. // new range
  10732. $rangeid = $prevcid;
  10733. $range[$rangeid] = array();
  10734. $range[$rangeid][] = $prevwidth;
  10735. $range[$rangeid][] = $width;
  10736. }
  10737. $interval = true;
  10738. $range[$rangeid]['interval'] = true;
  10739. } else {
  10740. if ($interval) {
  10741. // new range
  10742. $rangeid = $cid;
  10743. $range[$rangeid] = array();
  10744. $range[$rangeid][] = $width;
  10745. } else {
  10746. $range[$rangeid][] = $width;
  10747. }
  10748. $interval = false;
  10749. }
  10750. } else {
  10751. // new range
  10752. $rangeid = $cid;
  10753. $range[$rangeid] = array();
  10754. $range[$rangeid][] = $width;
  10755. $interval = false;
  10756. }
  10757. $prevcid = $cid;
  10758. $prevwidth = $width;
  10759. }
  10760. }
  10761. // optimize ranges
  10762. $prevk = -1;
  10763. $nextk = -1;
  10764. $prevint = false;
  10765. foreach ($range as $k => $ws) {
  10766. $cws = count($ws);
  10767. if (($k == $nextk) AND (!$prevint) AND ((!isset($ws['interval'])) OR ($cws < 4))) {
  10768. if (isset($range[$k]['interval'])) {
  10769. unset($range[$k]['interval']);
  10770. }
  10771. $range[$prevk] = array_merge($range[$prevk], $range[$k]);
  10772. unset($range[$k]);
  10773. } else {
  10774. $prevk = $k;
  10775. }
  10776. $nextk = $k + $cws;
  10777. if (isset($ws['interval'])) {
  10778. if ($cws > 3) {
  10779. $prevint = true;
  10780. } else {
  10781. $prevint = false;
  10782. }
  10783. unset($range[$k]['interval']);
  10784. --$nextk;
  10785. } else {
  10786. $prevint = false;
  10787. }
  10788. }
  10789. // output data
  10790. $w = '';
  10791. foreach ($range as $k => $ws) {
  10792. if (count(array_count_values($ws)) == 1) {
  10793. // interval mode is more compact
  10794. $w .= ' '.$k.' '.($k + count($ws) - 1).' '.$ws[0];
  10795. } else {
  10796. // range mode
  10797. $w .= ' '.$k.' [ '.implode(' ', $ws).' ]';
  10798. }
  10799. }
  10800. return '/W ['.$w.' ]';
  10801. }
  10802. /**
  10803. * Output fonts.
  10804. * @author Nicola Asuni
  10805. * @protected
  10806. */
  10807. protected function _putfonts() {
  10808. $nf = $this->n;
  10809. foreach ($this->diffs as $diff) {
  10810. //Encodings
  10811. $this->_newobj();
  10812. $this->_out('<< /Type /Encoding /BaseEncoding /WinAnsiEncoding /Differences ['.$diff.'] >>'."\n".'endobj');
  10813. }
  10814. $mqr = $this->get_mqr();
  10815. $this->set_mqr(false);
  10816. foreach ($this->FontFiles as $file => $info) {
  10817. // search and get font file to embedd
  10818. $fontdir = $info['fontdir'];
  10819. $file = strtolower($file);
  10820. $fontfile = '';
  10821. // search files on various directories
  10822. if (($fontdir !== false) AND file_exists($fontdir.$file)) {
  10823. $fontfile = $fontdir.$file;
  10824. } elseif (file_exists($this->_getfontpath().$file)) {
  10825. $fontfile = $this->_getfontpath().$file;
  10826. } elseif (file_exists($file)) {
  10827. $fontfile = $file;
  10828. }
  10829. if (!$this->empty_string($fontfile)) {
  10830. $font = file_get_contents($fontfile);
  10831. $compressed = (substr($file, -2) == '.z');
  10832. if ((!$compressed) AND (isset($info['length2']))) {
  10833. $header = (ord($font{0}) == 128);
  10834. if ($header) {
  10835. // strip first binary header
  10836. $font = substr($font, 6);
  10837. }
  10838. if ($header AND (ord($font[$info['length1']]) == 128)) {
  10839. // strip second binary header
  10840. $font = substr($font, 0, $info['length1']).substr($font, ($info['length1'] + 6));
  10841. }
  10842. } elseif ($info['subset'] AND ((!$compressed) OR ($compressed AND function_exists('gzcompress')))) {
  10843. if ($compressed) {
  10844. // uncompress font
  10845. $font = gzuncompress($font);
  10846. }
  10847. // merge subset characters
  10848. $subsetchars = array(); // used chars
  10849. foreach ($info['fontkeys'] as $fontkey) {
  10850. $fontinfo = $this->getFontBuffer($fontkey);
  10851. $subsetchars += $fontinfo['subsetchars'];
  10852. }
  10853. // rebuild a font subset
  10854. $font = $this->_getTrueTypeFontSubset($font, $subsetchars);
  10855. // calculate new font length
  10856. $info['length1'] = strlen($font);
  10857. if ($compressed) {
  10858. // recompress font
  10859. $font = gzcompress($font);
  10860. }
  10861. }
  10862. $this->_newobj();
  10863. $this->FontFiles[$file]['n'] = $this->n;
  10864. $stream = $this->_getrawstream($font);
  10865. $out = '<< /Length '.strlen($stream);
  10866. if ($compressed) {
  10867. $out .= ' /Filter /FlateDecode';
  10868. }
  10869. $out .= ' /Length1 '.$info['length1'];
  10870. if (isset($info['length2'])) {
  10871. $out .= ' /Length2 '.$info['length2'].' /Length3 0';
  10872. }
  10873. $out .= ' >>';
  10874. $out .= ' stream'."\n".$stream."\n".'endstream';
  10875. $out .= "\n".'endobj';
  10876. $this->_out($out);
  10877. }
  10878. }
  10879. $this->set_mqr($mqr);
  10880. foreach ($this->fontkeys as $k) {
  10881. //Font objects
  10882. $font = $this->getFontBuffer($k);
  10883. $type = $font['type'];
  10884. $name = $font['name'];
  10885. if ($type == 'core') {
  10886. // standard core font
  10887. $out = $this->_getobj($this->font_obj_ids[$k])."\n";
  10888. $out .= '<</Type /Font';
  10889. $out .= ' /Subtype /Type1';
  10890. $out .= ' /BaseFont /'.$name;
  10891. $out .= ' /Name /F'.$font['i'];
  10892. if ((strtolower($name) != 'symbol') AND (strtolower($name) != 'zapfdingbats')) {
  10893. $out .= ' /Encoding /WinAnsiEncoding';
  10894. }
  10895. if ($k == 'helvetica') {
  10896. // add default font for annotations
  10897. $this->annotation_fonts[$k] = $font['i'];
  10898. }
  10899. $out .= ' >>';
  10900. $out .= "\n".'endobj';
  10901. $this->_out($out);
  10902. } elseif (($type == 'Type1') OR ($type == 'TrueType')) {
  10903. // additional Type1 or TrueType font
  10904. $out = $this->_getobj($this->font_obj_ids[$k])."\n";
  10905. $out .= '<</Type /Font';
  10906. $out .= ' /Subtype /'.$type;
  10907. $out .= ' /BaseFont /'.$name;
  10908. $out .= ' /Name /F'.$font['i'];
  10909. $out .= ' /FirstChar 32 /LastChar 255';
  10910. $out .= ' /Widths '.($this->n + 1).' 0 R';
  10911. $out .= ' /FontDescriptor '.($this->n + 2).' 0 R';
  10912. if ($font['enc']) {
  10913. if (isset($font['diff'])) {
  10914. $out .= ' /Encoding '.($nf + $font['diff']).' 0 R';
  10915. } else {
  10916. $out .= ' /Encoding /WinAnsiEncoding';
  10917. }
  10918. }
  10919. $out .= ' >>';
  10920. $out .= "\n".'endobj';
  10921. $this->_out($out);
  10922. // Widths
  10923. $this->_newobj();
  10924. $s = '[';
  10925. for ($i = 32; $i < 256; ++$i) {
  10926. $s .= $font['cw'][$i].' ';
  10927. }
  10928. $s .= ']';
  10929. $s .= "\n".'endobj';
  10930. $this->_out($s);
  10931. //Descriptor
  10932. $this->_newobj();
  10933. $s = '<</Type /FontDescriptor /FontName /'.$name;
  10934. foreach ($font['desc'] as $fdk => $fdv) {
  10935. if (is_float($fdv)) {
  10936. $fdv = sprintf('%.3F', $fdv);
  10937. }
  10938. $s .= ' /'.$fdk.' '.$fdv.'';
  10939. }
  10940. if (!$this->empty_string($font['file'])) {
  10941. $s .= ' /FontFile'.($type == 'Type1' ? '' : '2').' '.$this->FontFiles[$font['file']]['n'].' 0 R';
  10942. }
  10943. $s .= '>>';
  10944. $s .= "\n".'endobj';
  10945. $this->_out($s);
  10946. } else {
  10947. // additional types
  10948. $mtd = '_put'.strtolower($type);
  10949. if (!method_exists($this, $mtd)) {
  10950. $this->Error('Unsupported font type: '.$type);
  10951. }
  10952. $this->$mtd($font);
  10953. }
  10954. }
  10955. }
  10956. /**
  10957. * Adds unicode fonts.<br>
  10958. * Based on PDF Reference 1.3 (section 5)
  10959. * @param $font (array) font data
  10960. * @protected
  10961. * @author Nicola Asuni
  10962. * @since 1.52.0.TC005 (2005-01-05)
  10963. */
  10964. protected function _puttruetypeunicode($font) {
  10965. $fontname = '';
  10966. if ($font['subset']) {
  10967. // change name for font subsetting
  10968. $subtag = sprintf('%06u', $font['i']);
  10969. $subtag = strtr($subtag, '0123456789', 'ABCDEFGHIJ');
  10970. $fontname .= $subtag.'+';
  10971. }
  10972. $fontname .= $font['name'];
  10973. // Type0 Font
  10974. // A composite font composed of other fonts, organized hierarchically
  10975. $out = $this->_getobj($this->font_obj_ids[$font['fontkey']])."\n";
  10976. $out .= '<< /Type /Font';
  10977. $out .= ' /Subtype /Type0';
  10978. $out .= ' /BaseFont /'.$fontname;
  10979. $out .= ' /Name /F'.$font['i'];
  10980. $out .= ' /Encoding /'.$font['enc'];
  10981. $out .= ' /ToUnicode '.($this->n + 1).' 0 R';
  10982. $out .= ' /DescendantFonts ['.($this->n + 2).' 0 R]';
  10983. $out .= ' >>';
  10984. $out .= "\n".'endobj';
  10985. $this->_out($out);
  10986. // ToUnicode map for Identity-H
  10987. $stream = "/CIDInit /ProcSet findresource begin\n";
  10988. $stream .= "12 dict begin\n";
  10989. $stream .= "begincmap\n";
  10990. $stream .= "/CIDSystemInfo << /Registry (Adobe) /Ordering (UCS) /Supplement 0 >> def\n";
  10991. $stream .= "/CMapName /Adobe-Identity-UCS def\n";
  10992. $stream .= "/CMapType 2 def\n";
  10993. $stream .= "/WMode 0 def\n";
  10994. $stream .= "1 begincodespacerange\n";
  10995. $stream .= "<0000> <FFFF>\n";
  10996. $stream .= "endcodespacerange\n";
  10997. $stream .= "100 beginbfrange\n";
  10998. $stream .= "<0000> <00ff> <0000>\n";
  10999. $stream .= "<0100> <01ff> <0100>\n";
  11000. $stream .= "<0200> <02ff> <0200>\n";
  11001. $stream .= "<0300> <03ff> <0300>\n";
  11002. $stream .= "<0400> <04ff> <0400>\n";
  11003. $stream .= "<0500> <05ff> <0500>\n";
  11004. $stream .= "<0600> <06ff> <0600>\n";
  11005. $stream .= "<0700> <07ff> <0700>\n";
  11006. $stream .= "<0800> <08ff> <0800>\n";
  11007. $stream .= "<0900> <09ff> <0900>\n";
  11008. $stream .= "<0a00> <0aff> <0a00>\n";
  11009. $stream .= "<0b00> <0bff> <0b00>\n";
  11010. $stream .= "<0c00> <0cff> <0c00>\n";
  11011. $stream .= "<0d00> <0dff> <0d00>\n";
  11012. $stream .= "<0e00> <0eff> <0e00>\n";
  11013. $stream .= "<0f00> <0fff> <0f00>\n";
  11014. $stream .= "<1000> <10ff> <1000>\n";
  11015. $stream .= "<1100> <11ff> <1100>\n";
  11016. $stream .= "<1200> <12ff> <1200>\n";
  11017. $stream .= "<1300> <13ff> <1300>\n";
  11018. $stream .= "<1400> <14ff> <1400>\n";
  11019. $stream .= "<1500> <15ff> <1500>\n";
  11020. $stream .= "<1600> <16ff> <1600>\n";
  11021. $stream .= "<1700> <17ff> <1700>\n";
  11022. $stream .= "<1800> <18ff> <1800>\n";
  11023. $stream .= "<1900> <19ff> <1900>\n";
  11024. $stream .= "<1a00> <1aff> <1a00>\n";
  11025. $stream .= "<1b00> <1bff> <1b00>\n";
  11026. $stream .= "<1c00> <1cff> <1c00>\n";
  11027. $stream .= "<1d00> <1dff> <1d00>\n";
  11028. $stream .= "<1e00> <1eff> <1e00>\n";
  11029. $stream .= "<1f00> <1fff> <1f00>\n";
  11030. $stream .= "<2000> <20ff> <2000>\n";
  11031. $stream .= "<2100> <21ff> <2100>\n";
  11032. $stream .= "<2200> <22ff> <2200>\n";
  11033. $stream .= "<2300> <23ff> <2300>\n";
  11034. $stream .= "<2400> <24ff> <2400>\n";
  11035. $stream .= "<2500> <25ff> <2500>\n";
  11036. $stream .= "<2600> <26ff> <2600>\n";
  11037. $stream .= "<2700> <27ff> <2700>\n";
  11038. $stream .= "<2800> <28ff> <2800>\n";
  11039. $stream .= "<2900> <29ff> <2900>\n";
  11040. $stream .= "<2a00> <2aff> <2a00>\n";
  11041. $stream .= "<2b00> <2bff> <2b00>\n";
  11042. $stream .= "<2c00> <2cff> <2c00>\n";
  11043. $stream .= "<2d00> <2dff> <2d00>\n";
  11044. $stream .= "<2e00> <2eff> <2e00>\n";
  11045. $stream .= "<2f00> <2fff> <2f00>\n";
  11046. $stream .= "<3000> <30ff> <3000>\n";
  11047. $stream .= "<3100> <31ff> <3100>\n";
  11048. $stream .= "<3200> <32ff> <3200>\n";
  11049. $stream .= "<3300> <33ff> <3300>\n";
  11050. $stream .= "<3400> <34ff> <3400>\n";
  11051. $stream .= "<3500> <35ff> <3500>\n";
  11052. $stream .= "<3600> <36ff> <3600>\n";
  11053. $stream .= "<3700> <37ff> <3700>\n";
  11054. $stream .= "<3800> <38ff> <3800>\n";
  11055. $stream .= "<3900> <39ff> <3900>\n";
  11056. $stream .= "<3a00> <3aff> <3a00>\n";
  11057. $stream .= "<3b00> <3bff> <3b00>\n";
  11058. $stream .= "<3c00> <3cff> <3c00>\n";
  11059. $stream .= "<3d00> <3dff> <3d00>\n";
  11060. $stream .= "<3e00> <3eff> <3e00>\n";
  11061. $stream .= "<3f00> <3fff> <3f00>\n";
  11062. $stream .= "<4000> <40ff> <4000>\n";
  11063. $stream .= "<4100> <41ff> <4100>\n";
  11064. $stream .= "<4200> <42ff> <4200>\n";
  11065. $stream .= "<4300> <43ff> <4300>\n";
  11066. $stream .= "<4400> <44ff> <4400>\n";
  11067. $stream .= "<4500> <45ff> <4500>\n";
  11068. $stream .= "<4600> <46ff> <4600>\n";
  11069. $stream .= "<4700> <47ff> <4700>\n";
  11070. $stream .= "<4800> <48ff> <4800>\n";
  11071. $stream .= "<4900> <49ff> <4900>\n";
  11072. $stream .= "<4a00> <4aff> <4a00>\n";
  11073. $stream .= "<4b00> <4bff> <4b00>\n";
  11074. $stream .= "<4c00> <4cff> <4c00>\n";
  11075. $stream .= "<4d00> <4dff> <4d00>\n";
  11076. $stream .= "<4e00> <4eff> <4e00>\n";
  11077. $stream .= "<4f00> <4fff> <4f00>\n";
  11078. $stream .= "<5000> <50ff> <5000>\n";
  11079. $stream .= "<5100> <51ff> <5100>\n";
  11080. $stream .= "<5200> <52ff> <5200>\n";
  11081. $stream .= "<5300> <53ff> <5300>\n";
  11082. $stream .= "<5400> <54ff> <5400>\n";
  11083. $stream .= "<5500> <55ff> <5500>\n";
  11084. $stream .= "<5600> <56ff> <5600>\n";
  11085. $stream .= "<5700> <57ff> <5700>\n";
  11086. $stream .= "<5800> <58ff> <5800>\n";
  11087. $stream .= "<5900> <59ff> <5900>\n";
  11088. $stream .= "<5a00> <5aff> <5a00>\n";
  11089. $stream .= "<5b00> <5bff> <5b00>\n";
  11090. $stream .= "<5c00> <5cff> <5c00>\n";
  11091. $stream .= "<5d00> <5dff> <5d00>\n";
  11092. $stream .= "<5e00> <5eff> <5e00>\n";
  11093. $stream .= "<5f00> <5fff> <5f00>\n";
  11094. $stream .= "<6000> <60ff> <6000>\n";
  11095. $stream .= "<6100> <61ff> <6100>\n";
  11096. $stream .= "<6200> <62ff> <6200>\n";
  11097. $stream .= "<6300> <63ff> <6300>\n";
  11098. $stream .= "endbfrange\n";
  11099. $stream .= "100 beginbfrange\n";
  11100. $stream .= "<6400> <64ff> <6400>\n";
  11101. $stream .= "<6500> <65ff> <6500>\n";
  11102. $stream .= "<6600> <66ff> <6600>\n";
  11103. $stream .= "<6700> <67ff> <6700>\n";
  11104. $stream .= "<6800> <68ff> <6800>\n";
  11105. $stream .= "<6900> <69ff> <6900>\n";
  11106. $stream .= "<6a00> <6aff> <6a00>\n";
  11107. $stream .= "<6b00> <6bff> <6b00>\n";
  11108. $stream .= "<6c00> <6cff> <6c00>\n";
  11109. $stream .= "<6d00> <6dff> <6d00>\n";
  11110. $stream .= "<6e00> <6eff> <6e00>\n";
  11111. $stream .= "<6f00> <6fff> <6f00>\n";
  11112. $stream .= "<7000> <70ff> <7000>\n";
  11113. $stream .= "<7100> <71ff> <7100>\n";
  11114. $stream .= "<7200> <72ff> <7200>\n";
  11115. $stream .= "<7300> <73ff> <7300>\n";
  11116. $stream .= "<7400> <74ff> <7400>\n";
  11117. $stream .= "<7500> <75ff> <7500>\n";
  11118. $stream .= "<7600> <76ff> <7600>\n";
  11119. $stream .= "<7700> <77ff> <7700>\n";
  11120. $stream .= "<7800> <78ff> <7800>\n";
  11121. $stream .= "<7900> <79ff> <7900>\n";
  11122. $stream .= "<7a00> <7aff> <7a00>\n";
  11123. $stream .= "<7b00> <7bff> <7b00>\n";
  11124. $stream .= "<7c00> <7cff> <7c00>\n";
  11125. $stream .= "<7d00> <7dff> <7d00>\n";
  11126. $stream .= "<7e00> <7eff> <7e00>\n";
  11127. $stream .= "<7f00> <7fff> <7f00>\n";
  11128. $stream .= "<8000> <80ff> <8000>\n";
  11129. $stream .= "<8100> <81ff> <8100>\n";
  11130. $stream .= "<8200> <82ff> <8200>\n";
  11131. $stream .= "<8300> <83ff> <8300>\n";
  11132. $stream .= "<8400> <84ff> <8400>\n";
  11133. $stream .= "<8500> <85ff> <8500>\n";
  11134. $stream .= "<8600> <86ff> <8600>\n";
  11135. $stream .= "<8700> <87ff> <8700>\n";
  11136. $stream .= "<8800> <88ff> <8800>\n";
  11137. $stream .= "<8900> <89ff> <8900>\n";
  11138. $stream .= "<8a00> <8aff> <8a00>\n";
  11139. $stream .= "<8b00> <8bff> <8b00>\n";
  11140. $stream .= "<8c00> <8cff> <8c00>\n";
  11141. $stream .= "<8d00> <8dff> <8d00>\n";
  11142. $stream .= "<8e00> <8eff> <8e00>\n";
  11143. $stream .= "<8f00> <8fff> <8f00>\n";
  11144. $stream .= "<9000> <90ff> <9000>\n";
  11145. $stream .= "<9100> <91ff> <9100>\n";
  11146. $stream .= "<9200> <92ff> <9200>\n";
  11147. $stream .= "<9300> <93ff> <9300>\n";
  11148. $stream .= "<9400> <94ff> <9400>\n";
  11149. $stream .= "<9500> <95ff> <9500>\n";
  11150. $stream .= "<9600> <96ff> <9600>\n";
  11151. $stream .= "<9700> <97ff> <9700>\n";
  11152. $stream .= "<9800> <98ff> <9800>\n";
  11153. $stream .= "<9900> <99ff> <9900>\n";
  11154. $stream .= "<9a00> <9aff> <9a00>\n";
  11155. $stream .= "<9b00> <9bff> <9b00>\n";
  11156. $stream .= "<9c00> <9cff> <9c00>\n";
  11157. $stream .= "<9d00> <9dff> <9d00>\n";
  11158. $stream .= "<9e00> <9eff> <9e00>\n";
  11159. $stream .= "<9f00> <9fff> <9f00>\n";
  11160. $stream .= "<a000> <a0ff> <a000>\n";
  11161. $stream .= "<a100> <a1ff> <a100>\n";
  11162. $stream .= "<a200> <a2ff> <a200>\n";
  11163. $stream .= "<a300> <a3ff> <a300>\n";
  11164. $stream .= "<a400> <a4ff> <a400>\n";
  11165. $stream .= "<a500> <a5ff> <a500>\n";
  11166. $stream .= "<a600> <a6ff> <a600>\n";
  11167. $stream .= "<a700> <a7ff> <a700>\n";
  11168. $stream .= "<a800> <a8ff> <a800>\n";
  11169. $stream .= "<a900> <a9ff> <a900>\n";
  11170. $stream .= "<aa00> <aaff> <aa00>\n";
  11171. $stream .= "<ab00> <abff> <ab00>\n";
  11172. $stream .= "<ac00> <acff> <ac00>\n";
  11173. $stream .= "<ad00> <adff> <ad00>\n";
  11174. $stream .= "<ae00> <aeff> <ae00>\n";
  11175. $stream .= "<af00> <afff> <af00>\n";
  11176. $stream .= "<b000> <b0ff> <b000>\n";
  11177. $stream .= "<b100> <b1ff> <b100>\n";
  11178. $stream .= "<b200> <b2ff> <b200>\n";
  11179. $stream .= "<b300> <b3ff> <b300>\n";
  11180. $stream .= "<b400> <b4ff> <b400>\n";
  11181. $stream .= "<b500> <b5ff> <b500>\n";
  11182. $stream .= "<b600> <b6ff> <b600>\n";
  11183. $stream .= "<b700> <b7ff> <b700>\n";
  11184. $stream .= "<b800> <b8ff> <b800>\n";
  11185. $stream .= "<b900> <b9ff> <b900>\n";
  11186. $stream .= "<ba00> <baff> <ba00>\n";
  11187. $stream .= "<bb00> <bbff> <bb00>\n";
  11188. $stream .= "<bc00> <bcff> <bc00>\n";
  11189. $stream .= "<bd00> <bdff> <bd00>\n";
  11190. $stream .= "<be00> <beff> <be00>\n";
  11191. $stream .= "<bf00> <bfff> <bf00>\n";
  11192. $stream .= "<c000> <c0ff> <c000>\n";
  11193. $stream .= "<c100> <c1ff> <c100>\n";
  11194. $stream .= "<c200> <c2ff> <c200>\n";
  11195. $stream .= "<c300> <c3ff> <c300>\n";
  11196. $stream .= "<c400> <c4ff> <c400>\n";
  11197. $stream .= "<c500> <c5ff> <c500>\n";
  11198. $stream .= "<c600> <c6ff> <c600>\n";
  11199. $stream .= "<c700> <c7ff> <c700>\n";
  11200. $stream .= "endbfrange\n";
  11201. $stream .= "56 beginbfrange\n";
  11202. $stream .= "<c800> <c8ff> <c800>\n";
  11203. $stream .= "<c900> <c9ff> <c900>\n";
  11204. $stream .= "<ca00> <caff> <ca00>\n";
  11205. $stream .= "<cb00> <cbff> <cb00>\n";
  11206. $stream .= "<cc00> <ccff> <cc00>\n";
  11207. $stream .= "<cd00> <cdff> <cd00>\n";
  11208. $stream .= "<ce00> <ceff> <ce00>\n";
  11209. $stream .= "<cf00> <cfff> <cf00>\n";
  11210. $stream .= "<d000> <d0ff> <d000>\n";
  11211. $stream .= "<d100> <d1ff> <d100>\n";
  11212. $stream .= "<d200> <d2ff> <d200>\n";
  11213. $stream .= "<d300> <d3ff> <d300>\n";
  11214. $stream .= "<d400> <d4ff> <d400>\n";
  11215. $stream .= "<d500> <d5ff> <d500>\n";
  11216. $stream .= "<d600> <d6ff> <d600>\n";
  11217. $stream .= "<d700> <d7ff> <d700>\n";
  11218. $stream .= "<d800> <d8ff> <d800>\n";
  11219. $stream .= "<d900> <d9ff> <d900>\n";
  11220. $stream .= "<da00> <daff> <da00>\n";
  11221. $stream .= "<db00> <dbff> <db00>\n";
  11222. $stream .= "<dc00> <dcff> <dc00>\n";
  11223. $stream .= "<dd00> <ddff> <dd00>\n";
  11224. $stream .= "<de00> <deff> <de00>\n";
  11225. $stream .= "<df00> <dfff> <df00>\n";
  11226. $stream .= "<e000> <e0ff> <e000>\n";
  11227. $stream .= "<e100> <e1ff> <e100>\n";
  11228. $stream .= "<e200> <e2ff> <e200>\n";
  11229. $stream .= "<e300> <e3ff> <e300>\n";
  11230. $stream .= "<e400> <e4ff> <e400>\n";
  11231. $stream .= "<e500> <e5ff> <e500>\n";
  11232. $stream .= "<e600> <e6ff> <e600>\n";
  11233. $stream .= "<e700> <e7ff> <e700>\n";
  11234. $stream .= "<e800> <e8ff> <e800>\n";
  11235. $stream .= "<e900> <e9ff> <e900>\n";
  11236. $stream .= "<ea00> <eaff> <ea00>\n";
  11237. $stream .= "<eb00> <ebff> <eb00>\n";
  11238. $stream .= "<ec00> <ecff> <ec00>\n";
  11239. $stream .= "<ed00> <edff> <ed00>\n";
  11240. $stream .= "<ee00> <eeff> <ee00>\n";
  11241. $stream .= "<ef00> <efff> <ef00>\n";
  11242. $stream .= "<f000> <f0ff> <f000>\n";
  11243. $stream .= "<f100> <f1ff> <f100>\n";
  11244. $stream .= "<f200> <f2ff> <f200>\n";
  11245. $stream .= "<f300> <f3ff> <f300>\n";
  11246. $stream .= "<f400> <f4ff> <f400>\n";
  11247. $stream .= "<f500> <f5ff> <f500>\n";
  11248. $stream .= "<f600> <f6ff> <f600>\n";
  11249. $stream .= "<f700> <f7ff> <f700>\n";
  11250. $stream .= "<f800> <f8ff> <f800>\n";
  11251. $stream .= "<f900> <f9ff> <f900>\n";
  11252. $stream .= "<fa00> <faff> <fa00>\n";
  11253. $stream .= "<fb00> <fbff> <fb00>\n";
  11254. $stream .= "<fc00> <fcff> <fc00>\n";
  11255. $stream .= "<fd00> <fdff> <fd00>\n";
  11256. $stream .= "<fe00> <feff> <fe00>\n";
  11257. $stream .= "<ff00> <ffff> <ff00>\n";
  11258. $stream .= "endbfrange\n";
  11259. $stream .= "endcmap\n";
  11260. $stream .= "CMapName currentdict /CMap defineresource pop\n";
  11261. $stream .= "end\n";
  11262. $stream .= "end";
  11263. // ToUnicode Object
  11264. $this->_newobj();
  11265. $stream = ($this->compress) ? gzcompress($stream) : $stream;
  11266. $filter = ($this->compress) ? '/Filter /FlateDecode ' : '';
  11267. $stream = $this->_getrawstream($stream);
  11268. $this->_out('<<'.$filter.'/Length '.strlen($stream).'>> stream'."\n".$stream."\n".'endstream'."\n".'endobj');
  11269. // CIDFontType2
  11270. // A CIDFont whose glyph descriptions are based on TrueType font technology
  11271. $oid = $this->_newobj();
  11272. $out = '<< /Type /Font';
  11273. $out .= ' /Subtype /CIDFontType2';
  11274. $out .= ' /BaseFont /'.$fontname;
  11275. // A dictionary containing entries that define the character collection of the CIDFont.
  11276. $cidinfo = '/Registry '.$this->_datastring($font['cidinfo']['Registry'], $oid);
  11277. $cidinfo .= ' /Ordering '.$this->_datastring($font['cidinfo']['Ordering'], $oid);
  11278. $cidinfo .= ' /Supplement '.$font['cidinfo']['Supplement'];
  11279. $out .= ' /CIDSystemInfo << '.$cidinfo.' >>';
  11280. $out .= ' /FontDescriptor '.($this->n + 1).' 0 R';
  11281. $out .= ' /DW '.$font['dw']; // default width
  11282. $out .= "\n".$this->_putfontwidths($font, 0);
  11283. if (isset($font['ctg']) AND (!$this->empty_string($font['ctg']))) {
  11284. $out .= "\n".'/CIDToGIDMap '.($this->n + 2).' 0 R';
  11285. }
  11286. $out .= ' >>';
  11287. $out .= "\n".'endobj';
  11288. $this->_out($out);
  11289. // Font descriptor
  11290. // A font descriptor describing the CIDFont default metrics other than its glyph widths
  11291. $this->_newobj();
  11292. $out = '<< /Type /FontDescriptor';
  11293. $out .= ' /FontName /'.$fontname;
  11294. foreach ($font['desc'] as $key => $value) {
  11295. if (is_float($value)) {
  11296. $value = sprintf('%.3F', $value);
  11297. }
  11298. $out .= ' /'.$key.' '.$value;
  11299. }
  11300. $fontdir = false;
  11301. if (!$this->empty_string($font['file'])) {
  11302. // A stream containing a TrueType font
  11303. $out .= ' /FontFile2 '.$this->FontFiles[$font['file']]['n'].' 0 R';
  11304. $fontdir = $this->FontFiles[$font['file']]['fontdir'];
  11305. }
  11306. $out .= ' >>';
  11307. $out .= "\n".'endobj';
  11308. $this->_out($out);
  11309. if (isset($font['ctg']) AND (!$this->empty_string($font['ctg']))) {
  11310. $this->_newobj();
  11311. // Embed CIDToGIDMap
  11312. // A specification of the mapping from CIDs to glyph indices
  11313. // search and get CTG font file to embedd
  11314. $ctgfile = strtolower($font['ctg']);
  11315. // search and get ctg font file to embedd
  11316. $fontfile = '';
  11317. // search files on various directories
  11318. if (($fontdir !== false) AND file_exists($fontdir.$ctgfile)) {
  11319. $fontfile = $fontdir.$ctgfile;
  11320. } elseif (file_exists($this->_getfontpath().$ctgfile)) {
  11321. $fontfile = $this->_getfontpath().$ctgfile;
  11322. } elseif (file_exists($ctgfile)) {
  11323. $fontfile = $ctgfile;
  11324. }
  11325. if ($this->empty_string($fontfile)) {
  11326. $this->Error('Font file not found: '.$ctgfile);
  11327. }
  11328. $stream = $this->_getrawstream(file_get_contents($fontfile));
  11329. $out = '<< /Length '.strlen($stream).'';
  11330. if (substr($fontfile, -2) == '.z') { // check file extension
  11331. // Decompresses data encoded using the public-domain
  11332. // zlib/deflate compression method, reproducing the
  11333. // original text or binary data
  11334. $out .= ' /Filter /FlateDecode';
  11335. }
  11336. $out .= ' >>';
  11337. $out .= ' stream'."\n".$stream."\n".'endstream';
  11338. $out .= "\n".'endobj';
  11339. $this->_out($out);
  11340. }
  11341. }
  11342. /**
  11343. * Output CID-0 fonts.
  11344. * A Type 0 CIDFont contains glyph descriptions based on the Adobe Type 1 font format
  11345. * @param $font (array) font data
  11346. * @protected
  11347. * @author Andrew Whitehead, Nicola Asuni, Yukihiro Nakadaira
  11348. * @since 3.2.000 (2008-06-23)
  11349. */
  11350. protected function _putcidfont0($font) {
  11351. $cidoffset = 0;
  11352. if (!isset($font['cw'][1])) {
  11353. $cidoffset = 31;
  11354. }
  11355. if (isset($font['cidinfo']['uni2cid'])) {
  11356. // convert unicode to cid.
  11357. $uni2cid = $font['cidinfo']['uni2cid'];
  11358. $cw = array();
  11359. foreach ($font['cw'] as $uni => $width) {
  11360. if (isset($uni2cid[$uni])) {
  11361. $cw[($uni2cid[$uni] + $cidoffset)] = $width;
  11362. } elseif ($uni < 256) {
  11363. $cw[$uni] = $width;
  11364. } // else unknown character
  11365. }
  11366. $font = array_merge($font, array('cw' => $cw));
  11367. }
  11368. $name = $font['name'];
  11369. $enc = $font['enc'];
  11370. if ($enc) {
  11371. $longname = $name.'-'.$enc;
  11372. } else {
  11373. $longname = $name;
  11374. }
  11375. $out = $this->_getobj($this->font_obj_ids[$font['fontkey']])."\n";
  11376. $out .= '<</Type /Font';
  11377. $out .= ' /Subtype /Type0';
  11378. $out .= ' /BaseFont /'.$longname;
  11379. $out .= ' /Name /F'.$font['i'];
  11380. if ($enc) {
  11381. $out .= ' /Encoding /'.$enc;
  11382. }
  11383. $out .= ' /DescendantFonts ['.($this->n + 1).' 0 R]';
  11384. $out .= ' >>';
  11385. $out .= "\n".'endobj';
  11386. $this->_out($out);
  11387. $oid = $this->_newobj();
  11388. $out = '<</Type /Font';
  11389. $out .= ' /Subtype /CIDFontType0';
  11390. $out .= ' /BaseFont /'.$name;
  11391. $cidinfo = '/Registry '.$this->_datastring($font['cidinfo']['Registry'], $oid);
  11392. $cidinfo .= ' /Ordering '.$this->_datastring($font['cidinfo']['Ordering'], $oid);
  11393. $cidinfo .= ' /Supplement '.$font['cidinfo']['Supplement'];
  11394. $out .= ' /CIDSystemInfo <<'.$cidinfo.'>>';
  11395. $out .= ' /FontDescriptor '.($this->n + 1).' 0 R';
  11396. $out .= ' /DW '.$font['dw'];
  11397. $out .= "\n".$this->_putfontwidths($font, $cidoffset);
  11398. $out .= ' >>';
  11399. $out .= "\n".'endobj';
  11400. $this->_out($out);
  11401. $this->_newobj();
  11402. $s = '<</Type /FontDescriptor /FontName /'.$name;
  11403. foreach ($font['desc'] as $k => $v) {
  11404. if ($k != 'Style') {
  11405. if (is_float($v)) {
  11406. $v = sprintf('%.3F', $v);
  11407. }
  11408. $s .= ' /'.$k.' '.$v.'';
  11409. }
  11410. }
  11411. $s .= '>>';
  11412. $s .= "\n".'endobj';
  11413. $this->_out($s);
  11414. }
  11415. /**
  11416. * Output images.
  11417. * @protected
  11418. */
  11419. protected function _putimages() {
  11420. $filter = ($this->compress) ? '/Filter /FlateDecode ' : '';
  11421. foreach ($this->imagekeys as $file) {
  11422. $info = $this->getImageBuffer($file);
  11423. // set object for alternate images array
  11424. if ((!$this->pdfa_mode) AND isset($info['altimgs']) AND !empty($info['altimgs'])) {
  11425. $altoid = $this->_newobj();
  11426. $out = '[';
  11427. foreach ($info['altimgs'] as $altimage) {
  11428. if (isset($this->xobjects['I'.$altimage[0]]['n'])) {
  11429. $out .= ' << /Image '.$this->xobjects['I'.$altimage[0]]['n'].' 0 R';
  11430. $out .= ' /DefaultForPrinting';
  11431. if ($altimage[1] === true) {
  11432. $out .= ' true';
  11433. } else {
  11434. $out .= ' false';
  11435. }
  11436. $out .= ' >>';
  11437. }
  11438. }
  11439. $out .= ' ]';
  11440. $out .= "\n".'endobj';
  11441. $this->_out($out);
  11442. }
  11443. // set image object
  11444. $oid = $this->_newobj();
  11445. $this->xobjects['I'.$info['i']] = array('n' => $oid);
  11446. $this->setImageSubBuffer($file, 'n', $this->n);
  11447. $out = '<</Type /XObject';
  11448. $out .= ' /Subtype /Image';
  11449. $out .= ' /Width '.$info['w'];
  11450. $out .= ' /Height '.$info['h'];
  11451. if (array_key_exists('masked', $info)) {
  11452. $out .= ' /SMask '.($this->n - 1).' 0 R';
  11453. }
  11454. // set color space
  11455. $icc = false;
  11456. if (isset($info['icc']) AND ($info['icc'] !== false)) {
  11457. // ICC Colour Space
  11458. $icc = true;
  11459. $out .= ' /ColorSpace [/ICCBased '.($this->n + 1).' 0 R]';
  11460. } elseif ($info['cs'] == 'Indexed') {
  11461. // Indexed Colour Space
  11462. $out .= ' /ColorSpace [/Indexed /DeviceRGB '.((strlen($info['pal']) / 3) - 1).' '.($this->n + 1).' 0 R]';
  11463. } else {
  11464. // Device Colour Space
  11465. $out .= ' /ColorSpace /'.$info['cs'];
  11466. }
  11467. if ($info['cs'] == 'DeviceCMYK') {
  11468. $out .= ' /Decode [1 0 1 0 1 0 1 0]';
  11469. }
  11470. $out .= ' /BitsPerComponent '.$info['bpc'];
  11471. if (isset($altoid) AND ($altoid > 0)) {
  11472. // reference to alternate images dictionary
  11473. $out .= ' /Alternates '.$altoid.' 0 R';
  11474. }
  11475. if (isset($info['exurl']) AND !empty($info['exurl'])) {
  11476. // external stream
  11477. $out .= ' /Length 0';
  11478. $out .= ' /F << /FS /URL /F '.$this->_datastring($info['exurl'], $oid).' >>';
  11479. if (isset($info['f'])) {
  11480. $out .= ' /FFilter /'.$info['f'];
  11481. }
  11482. $out .= ' >>';
  11483. $out .= ' stream'."\n".'endstream';
  11484. } else {
  11485. if (isset($info['f'])) {
  11486. $out .= ' /Filter /'.$info['f'];
  11487. }
  11488. if (isset($info['parms'])) {
  11489. $out .= ' '.$info['parms'];
  11490. }
  11491. if (isset($info['trns']) AND is_array($info['trns'])) {
  11492. $trns = '';
  11493. $count_info = count($info['trns']);
  11494. for ($i=0; $i < $count_info; ++$i) {
  11495. $trns .= $info['trns'][$i].' '.$info['trns'][$i].' ';
  11496. }
  11497. $out .= ' /Mask ['.$trns.']';
  11498. }
  11499. $stream = $this->_getrawstream($info['data']);
  11500. $out .= ' /Length '.strlen($stream).' >>';
  11501. $out .= ' stream'."\n".$stream."\n".'endstream';
  11502. }
  11503. $out .= "\n".'endobj';
  11504. $this->_out($out);
  11505. if ($icc) {
  11506. // ICC colour profile
  11507. $this->_newobj();
  11508. $icc = ($this->compress) ? gzcompress($info['icc']) : $info['icc'];
  11509. $icc = $this->_getrawstream($icc);
  11510. $this->_out('<</N '.$info['ch'].' /Alternate /'.$info['cs'].' '.$filter.'/Length '.strlen($icc).'>> stream'."\n".$icc."\n".'endstream'."\n".'endobj');
  11511. } elseif ($info['cs'] == 'Indexed') {
  11512. // colour palette
  11513. $this->_newobj();
  11514. $pal = ($this->compress) ? gzcompress($info['pal']) : $info['pal'];
  11515. $pal = $this->_getrawstream($pal);
  11516. $this->_out('<<'.$filter.'/Length '.strlen($pal).'>> stream'."\n".$pal."\n".'endstream'."\n".'endobj');
  11517. }
  11518. }
  11519. }
  11520. /**
  11521. * Output Form XObjects Templates.
  11522. * @author Nicola Asuni
  11523. * @since 5.8.017 (2010-08-24)
  11524. * @protected
  11525. * @see startTemplate(), endTemplate(), printTemplate()
  11526. */
  11527. protected function _putxobjects() {
  11528. foreach ($this->xobjects as $key => $data) {
  11529. if (isset($data['outdata'])) {
  11530. $stream = trim($data['outdata']);
  11531. $out = $this->_getobj($data['n'])."\n";
  11532. $out .= '<<';
  11533. $out .= ' /Type /XObject';
  11534. $out .= ' /Subtype /Form';
  11535. $out .= ' /FormType 1';
  11536. if ($this->compress) {
  11537. $stream = gzcompress($stream);
  11538. $out .= ' /Filter /FlateDecode';
  11539. }
  11540. $out .= sprintf(' /BBox [%.2F %.2F %.2F %.2F]', ($data['x'] * $this->k), (-$data['y'] * $this->k), (($data['w'] + $data['x']) * $this->k), (($data['h'] - $data['y']) * $this->k));
  11541. $out .= ' /Matrix [1 0 0 1 0 0]';
  11542. $out .= ' /Resources <<';
  11543. $out .= ' /ProcSet [/PDF /Text /ImageB /ImageC /ImageI]';
  11544. if (!$this->pdfa_mode) {
  11545. // transparency
  11546. if (isset($data['extgstates']) AND !empty($data['extgstates'])) {
  11547. $out .= ' /ExtGState <<';
  11548. foreach ($data['extgstates'] as $k => $extgstate) {
  11549. if (isset($this->extgstates[$k]['name'])) {
  11550. $out .= ' /'.$this->extgstates[$k]['name'];
  11551. } else {
  11552. $out .= ' /GS'.$k;
  11553. }
  11554. $out .= ' '.$this->extgstates[$k]['n'].' 0 R';
  11555. }
  11556. $out .= ' >>';
  11557. }
  11558. if (isset($data['gradients']) AND !empty($data['gradients'])) {
  11559. $gp = '';
  11560. $gs = '';
  11561. foreach ($data['gradients'] as $id => $grad) {
  11562. // gradient patterns
  11563. $gp .= ' /p'.$id.' '.$this->gradients[$id]['pattern'].' 0 R';
  11564. // gradient shadings
  11565. $gs .= ' /Sh'.$id.' '.$this->gradients[$id]['id'].' 0 R';
  11566. }
  11567. $out .= ' /Pattern <<'.$gp.' >>';
  11568. $out .= ' /Shading <<'.$gs.' >>';
  11569. }
  11570. }
  11571. // spot colors
  11572. if (isset($data['spot_colors']) AND !empty($data['spot_colors'])) {
  11573. $out .= ' /ColorSpace <<';
  11574. foreach ($data['spot_colors'] as $name => $color) {
  11575. $out .= ' /CS'.$color['i'].' '.$this->spot_colors[$name]['n'].' 0 R';
  11576. }
  11577. $out .= ' >>';
  11578. }
  11579. // fonts
  11580. if (!empty($data['fonts'])) {
  11581. $out .= ' /Font <<';
  11582. foreach ($data['fonts'] as $fontkey => $fontid) {
  11583. $out .= ' /F'.$fontid.' '.$this->font_obj_ids[$fontkey].' 0 R';
  11584. }
  11585. $out .= ' >>';
  11586. }
  11587. // images or nested xobjects
  11588. if (!empty($data['images']) OR !empty($data['xobjects'])) {
  11589. $out .= ' /XObject <<';
  11590. foreach ($data['images'] as $imgid) {
  11591. $out .= ' /I'.$imgid.' '.$this->xobjects['I'.$imgid]['n'].' 0 R';
  11592. }
  11593. foreach ($data['xobjects'] as $sub_id => $sub_objid) {
  11594. $out .= ' /'.$sub_id.' '.$sub_objid['n'].' 0 R';
  11595. }
  11596. $out .= ' >>';
  11597. }
  11598. $out .= ' >>'; //end resources
  11599. if (isset($data['group']) AND ($data['group'] !== false)) {
  11600. // set transparency group
  11601. $out .= ' /Group << /Type /Group /S /Transparency';
  11602. if (is_array($data['group'])) {
  11603. if (isset($data['group']['CS']) AND !empty($data['group']['CS'])) {
  11604. $out .= ' /CS /'.$data['group']['CS'];
  11605. }
  11606. if (isset($data['group']['I'])) {
  11607. $out .= ' /I /'.($data['group']['I']===true?'true':'false');
  11608. }
  11609. if (isset($data['group']['K'])) {
  11610. $out .= ' /K /'.($data['group']['K']===true?'true':'false');
  11611. }
  11612. }
  11613. $out .= ' >>';
  11614. }
  11615. $stream = $this->_getrawstream($stream, $data['n']);
  11616. $out .= ' /Length '.strlen($stream);
  11617. $out .= ' >>';
  11618. $out .= ' stream'."\n".$stream."\n".'endstream';
  11619. $out .= "\n".'endobj';
  11620. $this->_out($out);
  11621. }
  11622. }
  11623. }
  11624. /**
  11625. * Output Spot Colors Resources.
  11626. * @protected
  11627. * @since 4.0.024 (2008-09-12)
  11628. */
  11629. protected function _putspotcolors() {
  11630. foreach ($this->spot_colors as $name => $color) {
  11631. $this->_newobj();
  11632. $this->spot_colors[$name]['n'] = $this->n;
  11633. $out = '[/Separation /'.str_replace(' ', '#20', $name);
  11634. $out .= ' /DeviceCMYK <<';
  11635. $out .= ' /Range [0 1 0 1 0 1 0 1] /C0 [0 0 0 0]';
  11636. $out .= ' '.sprintf('/C1 [%.4F %.4F %.4F %.4F] ', ($color['c'] / 100), ($color['m'] / 100), ($color['y'] / 100), ($color['k'] / 100));
  11637. $out .= ' /FunctionType 2 /Domain [0 1] /N 1>>]';
  11638. $out .= "\n".'endobj';
  11639. $this->_out($out);
  11640. }
  11641. }
  11642. /**
  11643. * Return XObjects Dictionary.
  11644. * @return string XObjects dictionary
  11645. * @protected
  11646. * @since 5.8.014 (2010-08-23)
  11647. */
  11648. protected function _getxobjectdict() {
  11649. $out = '';
  11650. foreach ($this->xobjects as $id => $objid) {
  11651. $out .= ' /'.$id.' '.$objid['n'].' 0 R';
  11652. }
  11653. return $out;
  11654. }
  11655. /**
  11656. * Output Resources Dictionary.
  11657. * @protected
  11658. */
  11659. protected function _putresourcedict() {
  11660. $out = $this->_getobj(2)."\n";
  11661. $out .= '<< /ProcSet [/PDF /Text /ImageB /ImageC /ImageI]';
  11662. $out .= ' /Font <<';
  11663. foreach ($this->fontkeys as $fontkey) {
  11664. $font = $this->getFontBuffer($fontkey);
  11665. $out .= ' /F'.$font['i'].' '.$font['n'].' 0 R';
  11666. }
  11667. $out .= ' >>';
  11668. $out .= ' /XObject <<';
  11669. $out .= $this->_getxobjectdict();
  11670. $out .= ' >>';
  11671. // layers
  11672. if (!empty($this->pdflayers)) {
  11673. $out .= ' /Properties <<';
  11674. foreach ($this->pdflayers as $layer) {
  11675. $out .= ' /'.$layer['layer'].' '.$layer['objid'].' 0 R';
  11676. }
  11677. $out .= ' >>';
  11678. }
  11679. if (!$this->pdfa_mode) {
  11680. // transparency
  11681. if (isset($this->extgstates) AND !empty($this->extgstates)) {
  11682. $out .= ' /ExtGState <<';
  11683. foreach ($this->extgstates as $k => $extgstate) {
  11684. if (isset($extgstate['name'])) {
  11685. $out .= ' /'.$extgstate['name'];
  11686. } else {
  11687. $out .= ' /GS'.$k;
  11688. }
  11689. $out .= ' '.$extgstate['n'].' 0 R';
  11690. }
  11691. $out .= ' >>';
  11692. }
  11693. if (isset($this->gradients) AND !empty($this->gradients)) {
  11694. $gp = '';
  11695. $gs = '';
  11696. foreach ($this->gradients as $id => $grad) {
  11697. // gradient patterns
  11698. $gp .= ' /p'.$id.' '.$grad['pattern'].' 0 R';
  11699. // gradient shadings
  11700. $gs .= ' /Sh'.$id.' '.$grad['id'].' 0 R';
  11701. }
  11702. $out .= ' /Pattern <<'.$gp.' >>';
  11703. $out .= ' /Shading <<'.$gs.' >>';
  11704. }
  11705. }
  11706. // spot colors
  11707. if (isset($this->spot_colors) AND !empty($this->spot_colors)) {
  11708. $out .= ' /ColorSpace <<';
  11709. foreach ($this->spot_colors as $color) {
  11710. $out .= ' /CS'.$color['i'].' '.$color['n'].' 0 R';
  11711. }
  11712. $out .= ' >>';
  11713. }
  11714. $out .= ' >>';
  11715. $out .= "\n".'endobj';
  11716. $this->_out($out);
  11717. }
  11718. /**
  11719. * Output Resources.
  11720. * @protected
  11721. */
  11722. protected function _putresources() {
  11723. $this->_putextgstates();
  11724. $this->_putocg();
  11725. $this->_putfonts();
  11726. $this->_putimages();
  11727. $this->_putspotcolors();
  11728. $this->_putshaders();
  11729. $this->_putxobjects();
  11730. $this->_putresourcedict();
  11731. $this->_putdests();
  11732. $this->_putbookmarks();
  11733. $this->_putEmbeddedFiles();
  11734. $this->_putannotsobjs();
  11735. $this->_putjavascript();
  11736. $this->_putencryption();
  11737. }
  11738. /**
  11739. * Adds some Metadata information (Document Information Dictionary)
  11740. * (see Chapter 14.3.3 Document Information Dictionary of PDF32000_2008.pdf Reference)
  11741. * @return int object id
  11742. * @protected
  11743. */
  11744. protected function _putinfo() {
  11745. $oid = $this->_newobj();
  11746. $out = '<<';
  11747. // store current isunicode value
  11748. $prev_isunicode = $this->isunicode;
  11749. if ($this->docinfounicode) {
  11750. $this->isunicode = true;
  11751. }
  11752. if (!$this->empty_string($this->title)) {
  11753. // The document's title.
  11754. $out .= ' /Title '.$this->_textstring($this->title, $oid);
  11755. }
  11756. if (!$this->empty_string($this->author)) {
  11757. // The name of the person who created the document.
  11758. $out .= ' /Author '.$this->_textstring($this->author, $oid);
  11759. }
  11760. if (!$this->empty_string($this->subject)) {
  11761. // The subject of the document.
  11762. $out .= ' /Subject '.$this->_textstring($this->subject, $oid);
  11763. }
  11764. if (!$this->empty_string($this->keywords)) {
  11765. // Keywords associated with the document.
  11766. $out .= ' /Keywords '.$this->_textstring($this->keywords.' TCPDF', $oid);
  11767. }
  11768. if (!$this->empty_string($this->creator)) {
  11769. // 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.
  11770. $out .= ' /Creator '.$this->_textstring($this->creator, $oid);
  11771. }
  11772. // restore previous isunicode value
  11773. $this->isunicode = $prev_isunicode;
  11774. // default producer
  11775. $out .= ' /Producer '.$this->_textstring("\x54\x43\x50\x44\x46\x20".$this->tcpdf_version."\x20\x28\x68\x74\x74\x70\x3a\x2f\x2f\x77\x77\x77\x2e\x74\x63\x70\x64\x66\x2e\x6f\x72\x67\x29", $oid);
  11776. // The date and time the document was created, in human-readable form
  11777. $out .= ' /CreationDate '.$this->_datestring();
  11778. // The date and time the document was most recently modified, in human-readable form
  11779. $out .= ' /ModDate '.$this->_datestring();
  11780. // A name object indicating whether the document has been modified to include trapping information
  11781. $out .= ' /Trapped /False';
  11782. $out .= ' >>';
  11783. $out .= "\n".'endobj';
  11784. $this->_out($out);
  11785. return $oid;
  11786. }
  11787. /**
  11788. * Set additional XMP data to be added on the default XMP data just before the end of "x:xmpmeta" tag.
  11789. * IMPORTANT: This data is added as-is without controls, so you have to validate your data before using this method!
  11790. * @param $xmp (string) Custom XMP data.
  11791. * @since 5.9.128 (2011-10-06)
  11792. * @public
  11793. */
  11794. public function setExtraXMP($xmp) {
  11795. $this->custom_xmp = $xmp;
  11796. }
  11797. /**
  11798. * Put XMP data object and return ID.
  11799. * @return (int) The object ID.
  11800. * @since 5.9.121 (2011-09-28)
  11801. * @protected
  11802. */
  11803. protected function _putXMP() {
  11804. $oid = $this->_newobj();
  11805. // store current isunicode value
  11806. $prev_isunicode = $this->isunicode;
  11807. $this->isunicode = true;
  11808. $prev_encrypted = $this->encrypted;
  11809. $this->encrypted = false;
  11810. // set XMP data
  11811. $xmp = '<?xpacket begin="'.$this->unichr(0xfeff).'" id="W5M0MpCehiHzreSzNTczkc9d"?>'."\n";
  11812. $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";
  11813. $xmp .= "\t".'<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">'."\n";
  11814. $xmp .= "\t\t".'<rdf:Description rdf:about="" xmlns:dc="http://purl.org/dc/elements/1.1/">'."\n";
  11815. $xmp .= "\t\t\t".'<dc:format>application/pdf</dc:format>'."\n";
  11816. $xmp .= "\t\t\t".'<dc:title>'."\n";
  11817. $xmp .= "\t\t\t\t".'<rdf:Alt>'."\n";
  11818. $xmp .= "\t\t\t\t\t".'<rdf:li xml:lang="x-default">'.$this->_escapeXML($this->title).'</rdf:li>'."\n";
  11819. $xmp .= "\t\t\t\t".'</rdf:Alt>'."\n";
  11820. $xmp .= "\t\t\t".'</dc:title>'."\n";
  11821. $xmp .= "\t\t\t".'<dc:creator>'."\n";
  11822. $xmp .= "\t\t\t\t".'<rdf:Seq>'."\n";
  11823. $xmp .= "\t\t\t\t\t".'<rdf:li>'.$this->_escapeXML($this->author).'</rdf:li>'."\n";
  11824. $xmp .= "\t\t\t\t".'</rdf:Seq>'."\n";
  11825. $xmp .= "\t\t\t".'</dc:creator>'."\n";
  11826. $xmp .= "\t\t\t".'<dc:description>'."\n";
  11827. $xmp .= "\t\t\t\t".'<rdf:Alt>'."\n";
  11828. $xmp .= "\t\t\t\t\t".'<rdf:li xml:lang="x-default">'.$this->_escapeXML($this->subject).'</rdf:li>'."\n";
  11829. $xmp .= "\t\t\t\t".'</rdf:Alt>'."\n";
  11830. $xmp .= "\t\t\t".'</dc:description>'."\n";
  11831. $xmp .= "\t\t\t".'<dc:subject>'."\n";
  11832. $xmp .= "\t\t\t\t".'<rdf:Bag>'."\n";
  11833. $xmp .= "\t\t\t\t\t".'<rdf:li>'.$this->_escapeXML($this->keywords).'</rdf:li>'."\n";
  11834. $xmp .= "\t\t\t\t".'</rdf:Bag>'."\n";
  11835. $xmp .= "\t\t\t".'</dc:subject>'."\n";
  11836. $xmp .= "\t\t".'</rdf:Description>'."\n";
  11837. // convert date format
  11838. $docdate = substr($this->doc_date, 0, 4).'-'.substr($this->doc_date, 4, 2).'-'.substr($this->doc_date, 6, 2);
  11839. $docdate .= 'T'.substr($this->doc_date, 8, 2).':'.substr($this->doc_date, 10, 2).':'.substr($this->doc_date, 12, 2);
  11840. $docdate .= '+'.substr($this->doc_date, 15, 2).':'.substr($this->doc_date, 18, 2);
  11841. $docdate = $this->_escapeXML($docdate);
  11842. $xmp .= "\t\t".'<rdf:Description rdf:about="" xmlns:xmp="http://ns.adobe.com/xap/1.0/">'."\n";
  11843. $xmp .= "\t\t\t".'<xmp:CreateDate>'.$docdate.'</xmp:CreateDate>'."\n";
  11844. $xmp .= "\t\t\t".'<xmp:CreatorTool>TCPDF</xmp:CreatorTool>'."\n";
  11845. $xmp .= "\t\t\t".'<xmp:ModifyDate>'.$docdate.'</xmp:ModifyDate>'."\n";
  11846. $xmp .= "\t\t\t".'<xmp:MetadataDate>'.$docdate.'</xmp:MetadataDate>'."\n";
  11847. $xmp .= "\t\t".'</rdf:Description>'."\n";
  11848. $xmp .= "\t\t".'<rdf:Description rdf:about="" xmlns:pdf="http://ns.adobe.com/pdf/1.3/">'."\n";
  11849. $xmp .= "\t\t\t".'<pdf:Keywords>'.$this->_escapeXML($this->keywords).' TCPDF</pdf:Keywords>'."\n";
  11850. $xmp .= "\t\t\t".'<pdf:Producer>'.$this->_escapeXML("\x54\x43\x50\x44\x46\x20".$this->tcpdf_version."\x20\x28\x68\x74\x74\x70\x3a\x2f\x2f\x77\x77\x77\x2e\x74\x63\x70\x64\x66\x2e\x6f\x72\x67\x29").'</pdf:Producer>'."\n";
  11851. $xmp .= "\t\t".'</rdf:Description>'."\n";
  11852. $xmp .= "\t\t".'<rdf:Description rdf:about="" xmlns:xmpMM="http://ns.adobe.com/xap/1.0/mm/">'."\n";
  11853. $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);
  11854. $xmp .= "\t\t\t".'<xmpMM:DocumentID>'.$uuid.'</xmpMM:DocumentID>'."\n";
  11855. $xmp .= "\t\t\t".'<xmpMM:InstanceID>'.$uuid.'</xmpMM:InstanceID>'."\n";
  11856. $xmp .= "\t\t".'</rdf:Description>'."\n";
  11857. if ($this->pdfa_mode) {
  11858. $xmp .= "\t\t".'<rdf:Description rdf:about="" xmlns:pdfaid="http://www.aiim.org/pdfa/ns/id/">'."\n";
  11859. $xmp .= "\t\t\t".'<pdfaid:part>1</pdfaid:part>'."\n";
  11860. $xmp .= "\t\t\t".'<pdfaid:conformance>B</pdfaid:conformance>'."\n";
  11861. $xmp .= "\t\t".'</rdf:Description>'."\n";
  11862. }
  11863. // XMP extension schemas
  11864. $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";
  11865. $xmp .= "\t\t\t".'<pdfaExtension:schemas>'."\n";
  11866. $xmp .= "\t\t\t\t".'<rdf:Bag>'."\n";
  11867. $xmp .= "\t\t\t\t\t".'<rdf:li rdf:parseType="Resource">'."\n";
  11868. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:namespaceURI>http://ns.adobe.com/pdf/1.3/</pdfaSchema:namespaceURI>'."\n";
  11869. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:prefix>pdf</pdfaSchema:prefix>'."\n";
  11870. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:schema>Adobe PDF Schema</pdfaSchema:schema>'."\n";
  11871. $xmp .= "\t\t\t\t\t".'</rdf:li>'."\n";
  11872. $xmp .= "\t\t\t\t\t".'<rdf:li rdf:parseType="Resource">'."\n";
  11873. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:namespaceURI>http://ns.adobe.com/xap/1.0/mm/</pdfaSchema:namespaceURI>'."\n";
  11874. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:prefix>xmpMM</pdfaSchema:prefix>'."\n";
  11875. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:schema>XMP Media Management Schema</pdfaSchema:schema>'."\n";
  11876. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:property>'."\n";
  11877. $xmp .= "\t\t\t\t\t\t\t".'<rdf:Seq>'."\n";
  11878. $xmp .= "\t\t\t\t\t\t\t\t".'<rdf:li rdf:parseType="Resource">'."\n";
  11879. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:category>internal</pdfaProperty:category>'."\n";
  11880. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:description>UUID based identifier for specific incarnation of a document</pdfaProperty:description>'."\n";
  11881. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:name>InstanceID</pdfaProperty:name>'."\n";
  11882. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:valueType>URI</pdfaProperty:valueType>'."\n";
  11883. $xmp .= "\t\t\t\t\t\t\t\t".'</rdf:li>'."\n";
  11884. $xmp .= "\t\t\t\t\t\t\t".'</rdf:Seq>'."\n";
  11885. $xmp .= "\t\t\t\t\t\t".'</pdfaSchema:property>'."\n";
  11886. $xmp .= "\t\t\t\t\t".'</rdf:li>'."\n";
  11887. $xmp .= "\t\t\t\t\t".'<rdf:li rdf:parseType="Resource">'."\n";
  11888. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:namespaceURI>http://www.aiim.org/pdfa/ns/id/</pdfaSchema:namespaceURI>'."\n";
  11889. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:prefix>pdfaid</pdfaSchema:prefix>'."\n";
  11890. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:schema>PDF/A ID Schema</pdfaSchema:schema>'."\n";
  11891. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:property>'."\n";
  11892. $xmp .= "\t\t\t\t\t\t\t".'<rdf:Seq>'."\n";
  11893. $xmp .= "\t\t\t\t\t\t\t\t".'<rdf:li rdf:parseType="Resource">'."\n";
  11894. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:category>internal</pdfaProperty:category>'."\n";
  11895. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:description>Part of PDF/A standard</pdfaProperty:description>'."\n";
  11896. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:name>part</pdfaProperty:name>'."\n";
  11897. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:valueType>Integer</pdfaProperty:valueType>'."\n";
  11898. $xmp .= "\t\t\t\t\t\t\t\t".'</rdf:li>'."\n";
  11899. $xmp .= "\t\t\t\t\t\t\t\t".'<rdf:li rdf:parseType="Resource">'."\n";
  11900. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:category>internal</pdfaProperty:category>'."\n";
  11901. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:description>Amendment of PDF/A standard</pdfaProperty:description>'."\n";
  11902. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:name>amd</pdfaProperty:name>'."\n";
  11903. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:valueType>Text</pdfaProperty:valueType>'."\n";
  11904. $xmp .= "\t\t\t\t\t\t\t\t".'</rdf:li>'."\n";
  11905. $xmp .= "\t\t\t\t\t\t\t\t".'<rdf:li rdf:parseType="Resource">'."\n";
  11906. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:category>internal</pdfaProperty:category>'."\n";
  11907. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:description>Conformance level of PDF/A standard</pdfaProperty:description>'."\n";
  11908. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:name>conformance</pdfaProperty:name>'."\n";
  11909. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:valueType>Text</pdfaProperty:valueType>'."\n";
  11910. $xmp .= "\t\t\t\t\t\t\t\t".'</rdf:li>'."\n";
  11911. $xmp .= "\t\t\t\t\t\t\t".'</rdf:Seq>'."\n";
  11912. $xmp .= "\t\t\t\t\t\t".'</pdfaSchema:property>'."\n";
  11913. $xmp .= "\t\t\t\t\t".'</rdf:li>'."\n";
  11914. $xmp .= "\t\t\t\t".'</rdf:Bag>'."\n";
  11915. $xmp .= "\t\t\t".'</pdfaExtension:schemas>'."\n";
  11916. $xmp .= "\t\t".'</rdf:Description>'."\n";
  11917. $xmp .= "\t".'</rdf:RDF>'."\n";
  11918. $xmp .= $this->custom_xmp;
  11919. $xmp .= '</x:xmpmeta>'."\n";
  11920. $xmp .= '<?xpacket end="w"?>';
  11921. $out = '<< /Type /Metadata /Subtype /XML /Length '.strlen($xmp).' >> stream'."\n".$xmp."\n".'endstream'."\n".'endobj';
  11922. // restore previous isunicode value
  11923. $this->isunicode = $prev_isunicode;
  11924. $this->encrypted = $prev_encrypted;
  11925. $this->_out($out);
  11926. return $oid;
  11927. }
  11928. /**
  11929. * Output Catalog.
  11930. * @return int object id
  11931. * @protected
  11932. */
  11933. protected function _putcatalog() {
  11934. // put XMP
  11935. $xmpobj = $this->_putXMP();
  11936. // if required, add standard sRGB_IEC61966-2.1 blackscaled ICC colour profile
  11937. if ($this->pdfa_mode OR $this->force_srgb) {
  11938. $iccobj = $this->_newobj();
  11939. $icc = file_get_contents(dirname(__FILE__).'/sRGB.icc');
  11940. $filter = '';
  11941. if ($this->compress) {
  11942. $filter = ' /Filter /FlateDecode';
  11943. $icc = gzcompress($icc);
  11944. }
  11945. $icc = $this->_getrawstream($icc);
  11946. $this->_out('<</N 3 '.$filter.'/Length '.strlen($icc).'>> stream'."\n".$icc."\n".'endstream'."\n".'endobj');
  11947. }
  11948. // start catalog
  11949. $oid = $this->_newobj();
  11950. $out = '<< /Type /Catalog';
  11951. $out .= ' /Version /'.$this->PDFVersion;
  11952. //$out .= ' /Extensions <<>>';
  11953. $out .= ' /Pages 1 0 R';
  11954. //$out .= ' /PageLabels ' //...;
  11955. $out .= ' /Names <<';
  11956. if ((!$this->pdfa_mode) AND ((!empty($this->javascript)) OR (!empty($this->js_objects)))) {
  11957. $out .= ' /JavaScript '.($this->n_js).' 0 R';
  11958. }
  11959. $out .= ' >>';
  11960. if (!empty($this->dests)) {
  11961. $out .= ' /Dests '.$this->n_dests.' 0 R';
  11962. }
  11963. $out .= $this->_putviewerpreferences();
  11964. if (isset($this->LayoutMode) AND (!$this->empty_string($this->LayoutMode))) {
  11965. $out .= ' /PageLayout /'.$this->LayoutMode;
  11966. }
  11967. if (isset($this->PageMode) AND (!$this->empty_string($this->PageMode))) {
  11968. $out .= ' /PageMode /'.$this->PageMode;
  11969. }
  11970. if (count($this->outlines) > 0) {
  11971. $out .= ' /Outlines '.$this->OutlineRoot.' 0 R';
  11972. $out .= ' /PageMode /UseOutlines';
  11973. }
  11974. //$out .= ' /Threads []';
  11975. if ($this->ZoomMode == 'fullpage') {
  11976. $out .= ' /OpenAction ['.$this->page_obj_id[1].' 0 R /Fit]';
  11977. } elseif ($this->ZoomMode == 'fullwidth') {
  11978. $out .= ' /OpenAction ['.$this->page_obj_id[1].' 0 R /FitH null]';
  11979. } elseif ($this->ZoomMode == 'real') {
  11980. $out .= ' /OpenAction ['.$this->page_obj_id[1].' 0 R /XYZ null null 1]';
  11981. } elseif (!is_string($this->ZoomMode)) {
  11982. $out .= sprintf(' /OpenAction ['.$this->page_obj_id[1].' 0 R /XYZ null null %.2F]', ($this->ZoomMode / 100));
  11983. }
  11984. //$out .= ' /AA <<>>';
  11985. //$out .= ' /URI <<>>';
  11986. $out .= ' /Metadata '.$xmpobj.' 0 R';
  11987. //$out .= ' /StructTreeRoot <<>>';
  11988. //$out .= ' /MarkInfo <<>>';
  11989. if (isset($this->l['a_meta_language'])) {
  11990. $out .= ' /Lang '.$this->_textstring($this->l['a_meta_language'], $oid);
  11991. }
  11992. //$out .= ' /SpiderInfo <<>>';
  11993. // set OutputIntent to sRGB IEC61966-2.1 if required
  11994. if ($this->pdfa_mode OR $this->force_srgb) {
  11995. $out .= ' /OutputIntents [<<';
  11996. $out .= ' /Type /OutputIntent';
  11997. $out .= ' /S /GTS_PDFA1';
  11998. $out .= ' /OutputCondition '.$this->_textstring('sRGB IEC61966-2.1', $oid);
  11999. $out .= ' /OutputConditionIdentifier '.$this->_textstring('sRGB IEC61966-2.1', $oid);
  12000. $out .= ' /RegistryName '.$this->_textstring('http://www.color.org', $oid);
  12001. $out .= ' /Info '.$this->_textstring('sRGB IEC61966-2.1', $oid);
  12002. $out .= ' /DestOutputProfile '.$iccobj.' 0 R';
  12003. $out .= ' >>]';
  12004. }
  12005. //$out .= ' /PieceInfo <<>>';
  12006. if (!empty($this->pdflayers)) {
  12007. $lyrobjs = '';
  12008. $lyrobjs_print = '';
  12009. $lyrobjs_view = '';
  12010. foreach ($this->pdflayers as $layer) {
  12011. $lyrobjs .= ' '.$layer['objid'].' 0 R';
  12012. if ($layer['print']) {
  12013. $lyrobjs_print .= ' '.$layer['objid'].' 0 R';
  12014. }
  12015. if ($layer['view']) {
  12016. $lyrobjs_view .= ' '.$layer['objid'].' 0 R';
  12017. }
  12018. }
  12019. $out .= ' /OCProperties << /OCGs ['.$lyrobjs.']';
  12020. $out .= ' /D <<';
  12021. $out .= ' /Name '.$this->_textstring('Layers', $oid);
  12022. $out .= ' /Creator '.$this->_textstring('TCPDF', $oid);
  12023. $out .= ' /BaseState /ON';
  12024. $out .= ' /ON ['.$lyrobjs_print.']';
  12025. $out .= ' /OFF ['.$lyrobjs_view.']';
  12026. $out .= ' /Intent /View';
  12027. $out .= ' /AS [';
  12028. $out .= ' << /Event /Print /OCGs ['.$lyrobjs.'] /Category [/Print] >>';
  12029. $out .= ' << /Event /View /OCGs ['.$lyrobjs.'] /Category [/View] >>';
  12030. $out .= ' ]';
  12031. $out .= ' /Order ['.$lyrobjs.']';
  12032. $out .= ' /ListMode /AllPages';
  12033. //$out .= ' /RBGroups ['..']';
  12034. //$out .= ' /Locked ['..']';
  12035. $out .= ' >>';
  12036. $out .= ' >>';
  12037. }
  12038. // AcroForm
  12039. if (!empty($this->form_obj_id) OR ($this->sign AND isset($this->signature_data['cert_type']))) {
  12040. $out .= ' /AcroForm <<';
  12041. $objrefs = '';
  12042. if ($this->sign AND isset($this->signature_data['cert_type'])) {
  12043. // set reference for signature object
  12044. $objrefs .= $this->sig_obj_id.' 0 R';
  12045. }
  12046. if (!empty($this->empty_signature_appearance)) {
  12047. foreach ($this->empty_signature_appearance as $esa) {
  12048. // set reference for empty signature objects
  12049. $objrefs .= ' '.$esa['objid'].' 0 R';
  12050. }
  12051. }
  12052. if (!empty($this->form_obj_id)) {
  12053. foreach($this->form_obj_id as $objid) {
  12054. $objrefs .= ' '.$objid.' 0 R';
  12055. }
  12056. }
  12057. $out .= ' /Fields ['.$objrefs.']';
  12058. // It's better to turn off this value and set the appearance stream for each annotation (/AP) to avoid conflicts with signature fields.
  12059. $out .= ' /NeedAppearances false';
  12060. if ($this->sign AND isset($this->signature_data['cert_type'])) {
  12061. if ($this->signature_data['cert_type'] > 0) {
  12062. $out .= ' /SigFlags 3';
  12063. } else {
  12064. $out .= ' /SigFlags 1';
  12065. }
  12066. }
  12067. //$out .= ' /CO ';
  12068. if (isset($this->annotation_fonts) AND !empty($this->annotation_fonts)) {
  12069. $out .= ' /DR <<';
  12070. $out .= ' /Font <<';
  12071. foreach ($this->annotation_fonts as $fontkey => $fontid) {
  12072. $out .= ' /F'.$fontid.' '.$this->font_obj_ids[$fontkey].' 0 R';
  12073. }
  12074. $out .= ' >> >>';
  12075. }
  12076. $font = $this->getFontBuffer('helvetica');
  12077. $out .= ' /DA (/F'.$font['i'].' 0 Tf 0 g)';
  12078. $out .= ' /Q '.(($this->rtl)?'2':'0');
  12079. //$out .= ' /XFA ';
  12080. $out .= ' >>';
  12081. // signatures
  12082. if ($this->sign AND isset($this->signature_data['cert_type'])) {
  12083. if ($this->signature_data['cert_type'] > 0) {
  12084. $out .= ' /Perms << /DocMDP '.($this->sig_obj_id + 1).' 0 R >>';
  12085. } else {
  12086. $out .= ' /Perms << /UR3 '.($this->sig_obj_id + 1).' 0 R >>';
  12087. }
  12088. }
  12089. }
  12090. //$out .= ' /Legal <<>>';
  12091. //$out .= ' /Requirements []';
  12092. //$out .= ' /Collection <<>>';
  12093. //$out .= ' /NeedsRendering true';
  12094. $out .= ' >>';
  12095. $out .= "\n".'endobj';
  12096. $this->_out($out);
  12097. return $oid;
  12098. }
  12099. /**
  12100. * Output viewer preferences.
  12101. * @return string for viewer preferences
  12102. * @author Nicola asuni
  12103. * @since 3.1.000 (2008-06-09)
  12104. * @protected
  12105. */
  12106. protected function _putviewerpreferences() {
  12107. $out = ' /ViewerPreferences <<';
  12108. if ($this->rtl) {
  12109. $out .= ' /Direction /R2L';
  12110. } else {
  12111. $out .= ' /Direction /L2R';
  12112. }
  12113. if (isset($this->viewer_preferences['HideToolbar']) AND ($this->viewer_preferences['HideToolbar'])) {
  12114. $out .= ' /HideToolbar true';
  12115. }
  12116. if (isset($this->viewer_preferences['HideMenubar']) AND ($this->viewer_preferences['HideMenubar'])) {
  12117. $out .= ' /HideMenubar true';
  12118. }
  12119. if (isset($this->viewer_preferences['HideWindowUI']) AND ($this->viewer_preferences['HideWindowUI'])) {
  12120. $out .= ' /HideWindowUI true';
  12121. }
  12122. if (isset($this->viewer_preferences['FitWindow']) AND ($this->viewer_preferences['FitWindow'])) {
  12123. $out .= ' /FitWindow true';
  12124. }
  12125. if (isset($this->viewer_preferences['CenterWindow']) AND ($this->viewer_preferences['CenterWindow'])) {
  12126. $out .= ' /CenterWindow true';
  12127. }
  12128. if (isset($this->viewer_preferences['DisplayDocTitle']) AND ($this->viewer_preferences['DisplayDocTitle'])) {
  12129. $out .= ' /DisplayDocTitle true';
  12130. }
  12131. if (isset($this->viewer_preferences['NonFullScreenPageMode'])) {
  12132. $out .= ' /NonFullScreenPageMode /'.$this->viewer_preferences['NonFullScreenPageMode'];
  12133. }
  12134. if (isset($this->viewer_preferences['ViewArea'])) {
  12135. $out .= ' /ViewArea /'.$this->viewer_preferences['ViewArea'];
  12136. }
  12137. if (isset($this->viewer_preferences['ViewClip'])) {
  12138. $out .= ' /ViewClip /'.$this->viewer_preferences['ViewClip'];
  12139. }
  12140. if (isset($this->viewer_preferences['PrintArea'])) {
  12141. $out .= ' /PrintArea /'.$this->viewer_preferences['PrintArea'];
  12142. }
  12143. if (isset($this->viewer_preferences['PrintClip'])) {
  12144. $out .= ' /PrintClip /'.$this->viewer_preferences['PrintClip'];
  12145. }
  12146. if (isset($this->viewer_preferences['PrintScaling'])) {
  12147. $out .= ' /PrintScaling /'.$this->viewer_preferences['PrintScaling'];
  12148. }
  12149. if (isset($this->viewer_preferences['Duplex']) AND (!$this->empty_string($this->viewer_preferences['Duplex']))) {
  12150. $out .= ' /Duplex /'.$this->viewer_preferences['Duplex'];
  12151. }
  12152. if (isset($this->viewer_preferences['PickTrayByPDFSize'])) {
  12153. if ($this->viewer_preferences['PickTrayByPDFSize']) {
  12154. $out .= ' /PickTrayByPDFSize true';
  12155. } else {
  12156. $out .= ' /PickTrayByPDFSize false';
  12157. }
  12158. }
  12159. if (isset($this->viewer_preferences['PrintPageRange'])) {
  12160. $PrintPageRangeNum = '';
  12161. foreach ($this->viewer_preferences['PrintPageRange'] as $k => $v) {
  12162. $PrintPageRangeNum .= ' '.($v - 1).'';
  12163. }
  12164. $out .= ' /PrintPageRange ['.substr($PrintPageRangeNum,1).']';
  12165. }
  12166. if (isset($this->viewer_preferences['NumCopies'])) {
  12167. $out .= ' /NumCopies '.intval($this->viewer_preferences['NumCopies']);
  12168. }
  12169. $out .= ' >>';
  12170. return $out;
  12171. }
  12172. /**
  12173. * Output PDF File Header (7.5.2).
  12174. * @protected
  12175. */
  12176. protected function _putheader() {
  12177. $this->_out('%PDF-'.$this->PDFVersion);
  12178. $this->_out('%'.chr(0xe2).chr(0xe3).chr(0xcf).chr(0xd3));
  12179. }
  12180. /**
  12181. * Output end of document (EOF).
  12182. * @protected
  12183. */
  12184. protected function _enddoc() {
  12185. $this->state = 1;
  12186. $this->_putheader();
  12187. $this->_putpages();
  12188. $this->_putresources();
  12189. // empty signature fields
  12190. if (!empty($this->empty_signature_appearance)) {
  12191. foreach ($this->empty_signature_appearance as $key => $esa) {
  12192. // widget annotation for empty signature
  12193. $out = $this->_getobj($esa['objid'])."\n";
  12194. $out .= '<< /Type /Annot';
  12195. $out .= ' /Subtype /Widget';
  12196. $out .= ' /Rect ['.$esa['rect'].']';
  12197. $out .= ' /P '.$this->page_obj_id[($esa['page'])].' 0 R'; // link to signature appearance page
  12198. $out .= ' /F 4';
  12199. $out .= ' /FT /Sig';
  12200. $signame = sprintf('Signature_%03d', ($key + 1));
  12201. $out .= ' /T '.$this->_textstring($signame, $esa['objid']);
  12202. $out .= ' /Ff 0';
  12203. $out .= ' >>';
  12204. $out .= "\n".'endobj';
  12205. $this->_out($out);
  12206. }
  12207. }
  12208. // Signature
  12209. if ($this->sign AND isset($this->signature_data['cert_type'])) {
  12210. // widget annotation for signature
  12211. $out = $this->_getobj($this->sig_obj_id)."\n";
  12212. $out .= '<< /Type /Annot';
  12213. $out .= ' /Subtype /Widget';
  12214. $out .= ' /Rect ['.$this->signature_appearance['rect'].']';
  12215. $out .= ' /P '.$this->page_obj_id[($this->signature_appearance['page'])].' 0 R'; // link to signature appearance page
  12216. $out .= ' /F 4';
  12217. $out .= ' /FT /Sig';
  12218. $out .= ' /T '.$this->_textstring('Signature_000', $this->sig_obj_id);
  12219. $out .= ' /Ff 0';
  12220. $out .= ' /V '.($this->sig_obj_id + 1).' 0 R';
  12221. $out .= ' >>';
  12222. $out .= "\n".'endobj';
  12223. $this->_out($out);
  12224. // signature
  12225. $this->_putsignature();
  12226. }
  12227. // Info
  12228. $objid_info = $this->_putinfo();
  12229. // Catalog
  12230. $objid_catalog = $this->_putcatalog();
  12231. // Cross-ref
  12232. $o = $this->bufferlen;
  12233. // XREF section
  12234. $this->_out('xref');
  12235. $this->_out('0 '.($this->n + 1));
  12236. $this->_out('0000000000 65535 f ');
  12237. for ($i=1; $i <= $this->n; ++$i) {
  12238. if (!isset($this->offsets[$i]) AND ($i > 1)) {
  12239. $this->offsets[$i] = $this->offsets[($i - 1)];
  12240. }
  12241. $this->_out(sprintf('%010d 00000 n ', $this->offsets[$i]));
  12242. }
  12243. // TRAILER
  12244. $out = 'trailer'."\n";
  12245. $out .= '<<';
  12246. $out .= ' /Size '.($this->n + 1);
  12247. $out .= ' /Root '.$objid_catalog.' 0 R';
  12248. $out .= ' /Info '.$objid_info.' 0 R';
  12249. if ($this->encrypted) {
  12250. $out .= ' /Encrypt '.$this->encryptdata['objid'].' 0 R';
  12251. }
  12252. $out .= ' /ID [ <'.$this->file_id.'> <'.$this->file_id.'> ]';
  12253. $out .= ' >>';
  12254. $this->_out($out);
  12255. $this->_out('startxref');
  12256. $this->_out($o);
  12257. $this->_out('%%EOF');
  12258. $this->state = 3; // end-of-doc
  12259. if ($this->diskcache) {
  12260. // remove temporary files used for images
  12261. foreach ($this->imagekeys as $key) {
  12262. // remove temporary files
  12263. unlink($this->images[$key]);
  12264. }
  12265. foreach ($this->fontkeys as $key) {
  12266. // remove temporary files
  12267. unlink($this->fonts[$key]);
  12268. }
  12269. }
  12270. }
  12271. /**
  12272. * Initialize a new page.
  12273. * @param $orientation (string) page orientation. Possible values are (case insensitive):<ul><li>P or PORTRAIT (default)</li><li>L or LANDSCAPE</li></ul>
  12274. * @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().
  12275. * @protected
  12276. * @see getPageSizeFromFormat(), setPageFormat()
  12277. */
  12278. protected function _beginpage($orientation='', $format='') {
  12279. ++$this->page;
  12280. $this->setPageBuffer($this->page, '');
  12281. // initialize array for graphics tranformation positions inside a page buffer
  12282. $this->transfmrk[$this->page] = array();
  12283. $this->state = 2;
  12284. if ($this->empty_string($orientation)) {
  12285. if (isset($this->CurOrientation)) {
  12286. $orientation = $this->CurOrientation;
  12287. } elseif ($this->fwPt > $this->fhPt) {
  12288. // landscape
  12289. $orientation = 'L';
  12290. } else {
  12291. // portrait
  12292. $orientation = 'P';
  12293. }
  12294. }
  12295. if ($this->empty_string($format)) {
  12296. $this->pagedim[$this->page] = $this->pagedim[($this->page - 1)];
  12297. $this->setPageOrientation($orientation);
  12298. } else {
  12299. $this->setPageFormat($format, $orientation);
  12300. }
  12301. if ($this->rtl) {
  12302. $this->x = $this->w - $this->rMargin;
  12303. } else {
  12304. $this->x = $this->lMargin;
  12305. }
  12306. $this->y = $this->tMargin;
  12307. if (isset($this->newpagegroup[$this->page])) {
  12308. // start a new group
  12309. $this->currpagegroup = $this->newpagegroup[$this->page];
  12310. $this->pagegroups[$this->currpagegroup] = 1;
  12311. } elseif (isset($this->currpagegroup) AND ($this->currpagegroup > 0)) {
  12312. ++$this->pagegroups[$this->currpagegroup];
  12313. }
  12314. }
  12315. /**
  12316. * Mark end of page.
  12317. * @protected
  12318. */
  12319. protected function _endpage() {
  12320. $this->setVisibility('all');
  12321. $this->state = 1;
  12322. }
  12323. /**
  12324. * Begin a new object and return the object number.
  12325. * @return int object number
  12326. * @protected
  12327. */
  12328. protected function _newobj() {
  12329. $this->_out($this->_getobj());
  12330. return $this->n;
  12331. }
  12332. /**
  12333. * Return the starting object string for the selected object ID.
  12334. * @param $objid (int) Object ID (leave empty to get a new ID).
  12335. * @return string the starting object string
  12336. * @protected
  12337. * @since 5.8.009 (2010-08-20)
  12338. */
  12339. protected function _getobj($objid='') {
  12340. if ($objid === '') {
  12341. ++$this->n;
  12342. $objid = $this->n;
  12343. }
  12344. $this->offsets[$objid] = $this->bufferlen;
  12345. return $objid.' 0 obj';
  12346. }
  12347. /**
  12348. * Underline text.
  12349. * @param $x (int) X coordinate
  12350. * @param $y (int) Y coordinate
  12351. * @param $txt (string) text to underline
  12352. * @protected
  12353. */
  12354. protected function _dounderline($x, $y, $txt) {
  12355. $w = $this->GetStringWidth($txt);
  12356. return $this->_dounderlinew($x, $y, $w);
  12357. }
  12358. /**
  12359. * Underline for rectangular text area.
  12360. * @param $x (int) X coordinate
  12361. * @param $y (int) Y coordinate
  12362. * @param $w (int) width to underline
  12363. * @protected
  12364. * @since 4.8.008 (2009-09-29)
  12365. */
  12366. protected function _dounderlinew($x, $y, $w) {
  12367. $linew = - $this->CurrentFont['ut'] / 1000 * $this->FontSizePt;
  12368. return sprintf('%.2F %.2F %.2F %.2F re f', $x * $this->k, ((($this->h - $y) * $this->k) + $linew), $w * $this->k, $linew);
  12369. }
  12370. /**
  12371. * Line through text.
  12372. * @param $x (int) X coordinate
  12373. * @param $y (int) Y coordinate
  12374. * @param $txt (string) text to linethrough
  12375. * @protected
  12376. */
  12377. protected function _dolinethrough($x, $y, $txt) {
  12378. $w = $this->GetStringWidth($txt);
  12379. return $this->_dolinethroughw($x, $y, $w);
  12380. }
  12381. /**
  12382. * Line through for rectangular text area.
  12383. * @param $x (int) X coordinate
  12384. * @param $y (int) Y coordinate
  12385. * @param $w (int) line length (width)
  12386. * @protected
  12387. * @since 4.9.008 (2009-09-29)
  12388. */
  12389. protected function _dolinethroughw($x, $y, $w) {
  12390. $linew = - $this->CurrentFont['ut'] / 1000 * $this->FontSizePt;
  12391. return sprintf('%.2F %.2F %.2F %.2F re f', $x * $this->k, ((($this->h - $y) * $this->k) + $linew + ($this->FontSizePt / 3)), $w * $this->k, $linew);
  12392. }
  12393. /**
  12394. * Overline text.
  12395. * @param $x (int) X coordinate
  12396. * @param $y (int) Y coordinate
  12397. * @param $txt (string) text to overline
  12398. * @protected
  12399. * @since 4.9.015 (2010-04-19)
  12400. */
  12401. protected function _dooverline($x, $y, $txt) {
  12402. $w = $this->GetStringWidth($txt);
  12403. return $this->_dooverlinew($x, $y, $w);
  12404. }
  12405. /**
  12406. * Overline for rectangular text area.
  12407. * @param $x (int) X coordinate
  12408. * @param $y (int) Y coordinate
  12409. * @param $w (int) width to overline
  12410. * @protected
  12411. * @since 4.9.015 (2010-04-19)
  12412. */
  12413. protected function _dooverlinew($x, $y, $w) {
  12414. $linew = - $this->CurrentFont['ut'] / 1000 * $this->FontSizePt;
  12415. return sprintf('%.2F %.2F %.2F %.2F re f', $x * $this->k, (($this->h - $y + $this->FontAscent) * $this->k) - $linew, $w * $this->k, $linew);
  12416. }
  12417. /**
  12418. * Read a 4-byte (32 bit) integer from file.
  12419. * @param $f (string) file name.
  12420. * @return 4-byte integer
  12421. * @protected
  12422. */
  12423. protected function _freadint($f) {
  12424. $a = unpack('Ni', fread($f, 4));
  12425. return $a['i'];
  12426. }
  12427. /**
  12428. * Add "\" before "\", "(" and ")"
  12429. * @param $s (string) string to escape.
  12430. * @return string escaped string.
  12431. * @protected
  12432. */
  12433. protected function _escape($s) {
  12434. // the chr(13) substitution fixes the Bugs item #1421290.
  12435. return strtr($s, array(')' => '\\)', '(' => '\\(', '\\' => '\\\\', chr(13) => '\r'));
  12436. }
  12437. /**
  12438. * Format a data string for meta information
  12439. * @param $s (string) data string to escape.
  12440. * @param $n (int) object ID
  12441. * @return string escaped string.
  12442. * @protected
  12443. */
  12444. protected function _datastring($s, $n=0) {
  12445. if ($n == 0) {
  12446. $n = $this->n;
  12447. }
  12448. $s = $this->_encrypt_data($n, $s);
  12449. return '('. $this->_escape($s).')';
  12450. }
  12451. /**
  12452. * Returns a formatted date for meta information
  12453. * @param $n (int) object ID
  12454. * @return string escaped date string.
  12455. * @protected
  12456. * @since 4.6.028 (2009-08-25)
  12457. */
  12458. protected function _datestring($n=0) {
  12459. return $this->_datastring('D:'.$this->doc_date, $n);
  12460. }
  12461. /**
  12462. * Format a text string for meta information
  12463. * @param $s (string) string to escape.
  12464. * @param $n (int) object ID
  12465. * @return string escaped string.
  12466. * @protected
  12467. */
  12468. protected function _textstring($s, $n=0) {
  12469. if ($this->isunicode) {
  12470. //Convert string to UTF-16BE
  12471. $s = $this->UTF8ToUTF16BE($s, true);
  12472. }
  12473. return $this->_datastring($s, $n);
  12474. }
  12475. /**
  12476. * THIS METHOD IS DEPRECATED
  12477. * Format a text string
  12478. * @param $s (string) string to escape.
  12479. * @return string escaped string.
  12480. * @protected
  12481. * @deprecated
  12482. */
  12483. protected function _escapetext($s) {
  12484. if ($this->isunicode) {
  12485. if (($this->CurrentFont['type'] == 'core') OR ($this->CurrentFont['type'] == 'TrueType') OR ($this->CurrentFont['type'] == 'Type1')) {
  12486. $s = $this->UTF8ToLatin1($s);
  12487. } else {
  12488. //Convert string to UTF-16BE and reverse RTL language
  12489. $s = $this->utf8StrRev($s, false, $this->tmprtl);
  12490. }
  12491. }
  12492. return $this->_escape($s);
  12493. }
  12494. /**
  12495. * Escape some special characters (&lt; &gt; &amp;) for XML output.
  12496. * @param $str (string) Input string to convert.
  12497. * @return converted string
  12498. * @since 5.9.121 (2011-09-28)
  12499. * @protected
  12500. */
  12501. protected function _escapeXML($str) {
  12502. $replaceTable = array("\0" => '', '&' => '&amp;', '<' => '&lt;', '>' => '&gt;');
  12503. $str = strtr($str, $replaceTable);
  12504. return $str;
  12505. }
  12506. /**
  12507. * get raw output stream.
  12508. * @param $s (string) string to output.
  12509. * @param $n (int) object reference for encryption mode
  12510. * @protected
  12511. * @author Nicola Asuni
  12512. * @since 5.5.000 (2010-06-22)
  12513. */
  12514. protected function _getrawstream($s, $n=0) {
  12515. if ($n <= 0) {
  12516. // default to current object
  12517. $n = $this->n;
  12518. }
  12519. return $this->_encrypt_data($n, $s);
  12520. }
  12521. /**
  12522. * Format output stream (DEPRECATED).
  12523. * @param $s (string) string to output.
  12524. * @param $n (int) object reference for encryption mode
  12525. * @protected
  12526. * @deprecated
  12527. */
  12528. protected function _getstream($s, $n=0) {
  12529. return 'stream'."\n".$this->_getrawstream($s, $n)."\n".'endstream';
  12530. }
  12531. /**
  12532. * Output a stream (DEPRECATED).
  12533. * @param $s (string) string to output.
  12534. * @param $n (int) object reference for encryption mode
  12535. * @protected
  12536. * @deprecated
  12537. */
  12538. protected function _putstream($s, $n=0) {
  12539. $this->_out($this->_getstream($s, $n));
  12540. }
  12541. /**
  12542. * Output a string to the document.
  12543. * @param $s (string) string to output.
  12544. * @protected
  12545. */
  12546. protected function _out($s) {
  12547. if ($this->state == 2) {
  12548. if ($this->inxobj) {
  12549. // we are inside an XObject template
  12550. $this->xobjects[$this->xobjid]['outdata'] .= $s."\n";
  12551. } elseif ((!$this->InFooter) AND isset($this->footerlen[$this->page]) AND ($this->footerlen[$this->page] > 0)) {
  12552. // puts data before page footer
  12553. $pagebuff = $this->getPageBuffer($this->page);
  12554. $page = substr($pagebuff, 0, -$this->footerlen[$this->page]);
  12555. $footer = substr($pagebuff, -$this->footerlen[$this->page]);
  12556. $this->setPageBuffer($this->page, $page.$s."\n".$footer);
  12557. // update footer position
  12558. $this->footerpos[$this->page] += strlen($s."\n");
  12559. } else {
  12560. $this->setPageBuffer($this->page, $s."\n", true);
  12561. }
  12562. } else {
  12563. $this->setBuffer($s."\n");
  12564. }
  12565. }
  12566. /**
  12567. * Converts UTF-8 strings to codepoints array.<br>
  12568. * Invalid byte sequences will be replaced with 0xFFFD (replacement character)<br>
  12569. * Based on: http://www.faqs.org/rfcs/rfc3629.html
  12570. * <pre>
  12571. * Char. number range | UTF-8 octet sequence
  12572. * (hexadecimal) | (binary)
  12573. * --------------------+-----------------------------------------------
  12574. * 0000 0000-0000 007F | 0xxxxxxx
  12575. * 0000 0080-0000 07FF | 110xxxxx 10xxxxxx
  12576. * 0000 0800-0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx
  12577. * 0001 0000-0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
  12578. * ---------------------------------------------------------------------
  12579. *
  12580. * ABFN notation:
  12581. * ---------------------------------------------------------------------
  12582. * UTF8-octets = *( UTF8-char )
  12583. * UTF8-char = UTF8-1 / UTF8-2 / UTF8-3 / UTF8-4
  12584. * UTF8-1 = %x00-7F
  12585. * UTF8-2 = %xC2-DF UTF8-tail
  12586. *
  12587. * UTF8-3 = %xE0 %xA0-BF UTF8-tail / %xE1-EC 2( UTF8-tail ) /
  12588. * %xED %x80-9F UTF8-tail / %xEE-EF 2( UTF8-tail )
  12589. * UTF8-4 = %xF0 %x90-BF 2( UTF8-tail ) / %xF1-F3 3( UTF8-tail ) /
  12590. * %xF4 %x80-8F 2( UTF8-tail )
  12591. * UTF8-tail = %x80-BF
  12592. * ---------------------------------------------------------------------
  12593. * </pre>
  12594. * @param $str (string) string to process.
  12595. * @return array containing codepoints (UTF-8 characters values)
  12596. * @protected
  12597. * @author Nicola Asuni
  12598. * @since 1.53.0.TC005 (2005-01-05)
  12599. */
  12600. protected function UTF8StringToArray($str) {
  12601. // build a unique string key
  12602. $strkey = md5($str);
  12603. if (isset($this->cache_UTF8StringToArray[$strkey])) {
  12604. // return cached value
  12605. $chrarray = $this->cache_UTF8StringToArray[$strkey]['s'];
  12606. if (!isset($this->cache_UTF8StringToArray[$strkey]['f'][$this->CurrentFont['fontkey']])) {
  12607. if ($this->isunicode) {
  12608. foreach ($chrarray as $chr) {
  12609. // store this char for font subsetting
  12610. $this->CurrentFont['subsetchars'][$chr] = true;
  12611. }
  12612. // update font subsetchars
  12613. $this->setFontSubBuffer($this->CurrentFont['fontkey'], 'subsetchars', $this->CurrentFont['subsetchars']);
  12614. }
  12615. $this->cache_UTF8StringToArray[$strkey]['f'][$this->CurrentFont['fontkey']] = true;
  12616. }
  12617. return $chrarray;
  12618. }
  12619. // check cache size
  12620. if ($this->cache_size_UTF8StringToArray >= $this->cache_maxsize_UTF8StringToArray) {
  12621. // remove first element
  12622. array_shift($this->cache_UTF8StringToArray);
  12623. }
  12624. // new cache array for selected string
  12625. $this->cache_UTF8StringToArray[$strkey] = array('s' => array(), 'f' => array());
  12626. ++$this->cache_size_UTF8StringToArray;
  12627. if (!$this->isunicode) {
  12628. // split string into array of equivalent codes
  12629. $strarr = array();
  12630. $strlen = strlen($str);
  12631. for ($i=0; $i < $strlen; ++$i) {
  12632. $strarr[] = ord($str[$i]);
  12633. }
  12634. // insert new value on cache
  12635. $this->cache_UTF8StringToArray[$strkey]['s'] = $strarr;
  12636. $this->cache_UTF8StringToArray[$strkey]['f'][$this->CurrentFont['fontkey']] = true;
  12637. return $strarr;
  12638. }
  12639. $unichar = -1; // last unicode char
  12640. $unicode = array(); // array containing unicode values
  12641. $bytes = array(); // array containing single character byte sequences
  12642. $numbytes = 1; // number of octetc needed to represent the UTF-8 character
  12643. $str .= ''; // force $str to be a string
  12644. $length = strlen($str);
  12645. for ($i = 0; $i < $length; ++$i) {
  12646. $char = ord($str[$i]); // get one string character at time
  12647. if (count($bytes) == 0) { // get starting octect
  12648. if ($char <= 0x7F) {
  12649. $unichar = $char; // use the character "as is" because is ASCII
  12650. $numbytes = 1;
  12651. } elseif (($char >> 0x05) == 0x06) { // 2 bytes character (0x06 = 110 BIN)
  12652. $bytes[] = ($char - 0xC0) << 0x06;
  12653. $numbytes = 2;
  12654. } elseif (($char >> 0x04) == 0x0E) { // 3 bytes character (0x0E = 1110 BIN)
  12655. $bytes[] = ($char - 0xE0) << 0x0C;
  12656. $numbytes = 3;
  12657. } elseif (($char >> 0x03) == 0x1E) { // 4 bytes character (0x1E = 11110 BIN)
  12658. $bytes[] = ($char - 0xF0) << 0x12;
  12659. $numbytes = 4;
  12660. } else {
  12661. // use replacement character for other invalid sequences
  12662. $unichar = 0xFFFD;
  12663. $bytes = array();
  12664. $numbytes = 1;
  12665. }
  12666. } elseif (($char >> 0x06) == 0x02) { // bytes 2, 3 and 4 must start with 0x02 = 10 BIN
  12667. $bytes[] = $char - 0x80;
  12668. if (count($bytes) == $numbytes) {
  12669. // compose UTF-8 bytes to a single unicode value
  12670. $char = $bytes[0];
  12671. for ($j = 1; $j < $numbytes; ++$j) {
  12672. $char += ($bytes[$j] << (($numbytes - $j - 1) * 0x06));
  12673. }
  12674. if ((($char >= 0xD800) AND ($char <= 0xDFFF)) OR ($char >= 0x10FFFF)) {
  12675. /* The definition of UTF-8 prohibits encoding character numbers between
  12676. U+D800 and U+DFFF, which are reserved for use with the UTF-16
  12677. encoding form (as surrogate pairs) and do not directly represent
  12678. characters. */
  12679. $unichar = 0xFFFD; // use replacement character
  12680. } else {
  12681. $unichar = $char; // add char to array
  12682. }
  12683. // reset data for next char
  12684. $bytes = array();
  12685. $numbytes = 1;
  12686. }
  12687. } else {
  12688. // use replacement character for other invalid sequences
  12689. $unichar = 0xFFFD;
  12690. $bytes = array();
  12691. $numbytes = 1;
  12692. }
  12693. if ($unichar >= 0) {
  12694. // insert unicode value into array
  12695. $unicode[] = $unichar;
  12696. // store this char for font subsetting
  12697. $this->CurrentFont['subsetchars'][$unichar] = true;
  12698. $unichar = -1;
  12699. }
  12700. }
  12701. // update font subsetchars
  12702. $this->setFontSubBuffer($this->CurrentFont['fontkey'], 'subsetchars', $this->CurrentFont['subsetchars']);
  12703. // insert new value on cache
  12704. $this->cache_UTF8StringToArray[$strkey]['s'] = $unicode;
  12705. $this->cache_UTF8StringToArray[$strkey]['f'][$this->CurrentFont['fontkey']] = true;
  12706. return $unicode;
  12707. }
  12708. /**
  12709. * Converts UTF-8 strings to UTF16-BE.<br>
  12710. * @param $str (string) string to process.
  12711. * @param $setbom (boolean) if true set the Byte Order Mark (BOM = 0xFEFF)
  12712. * @return string
  12713. * @author Nicola Asuni
  12714. * @since 1.53.0.TC005 (2005-01-05)
  12715. * @see UTF8StringToArray(), arrUTF8ToUTF16BE()
  12716. * @protected
  12717. */
  12718. protected function UTF8ToUTF16BE($str, $setbom=false) {
  12719. if (!$this->isunicode) {
  12720. return $str; // string is not in unicode
  12721. }
  12722. $unicode = $this->UTF8StringToArray($str); // array containing UTF-8 unicode values
  12723. return $this->arrUTF8ToUTF16BE($unicode, $setbom);
  12724. }
  12725. /**
  12726. * Converts UTF-8 strings to Latin1 when using the standard 14 core fonts.<br>
  12727. * @param $str (string) string to process.
  12728. * @return string
  12729. * @author Andrew Whitehead, Nicola Asuni
  12730. * @protected
  12731. * @since 3.2.000 (2008-06-23)
  12732. */
  12733. protected function UTF8ToLatin1($str) {
  12734. if (!$this->isunicode) {
  12735. return $str; // string is not in unicode
  12736. }
  12737. $outstr = ''; // string to be returned
  12738. $unicode = $this->UTF8StringToArray($str); // array containing UTF-8 unicode values
  12739. foreach ($unicode as $char) {
  12740. if ($char < 256) {
  12741. $outstr .= chr($char);
  12742. } elseif (array_key_exists($char, $this->unicode->uni_utf8tolatin)) {
  12743. // map from UTF-8
  12744. $outstr .= chr($this->unicode->uni_utf8tolatin[$char]);
  12745. } elseif ($char == 0xFFFD) {
  12746. // skip
  12747. } else {
  12748. $outstr .= '?';
  12749. }
  12750. }
  12751. return $outstr;
  12752. }
  12753. /**
  12754. * Converts UTF-8 characters array to array of Latin1 characters<br>
  12755. * @param $unicode (array) array containing UTF-8 unicode values
  12756. * @return array
  12757. * @author Nicola Asuni
  12758. * @protected
  12759. * @since 4.8.023 (2010-01-15)
  12760. */
  12761. protected function UTF8ArrToLatin1($unicode) {
  12762. if ((!$this->isunicode) OR $this->isUnicodeFont()) {
  12763. return $unicode;
  12764. }
  12765. $outarr = array(); // array to be returned
  12766. foreach ($unicode as $char) {
  12767. if ($char < 256) {
  12768. $outarr[] = $char;
  12769. } elseif (array_key_exists($char, $this->unicode->uni_utf8tolatin)) {
  12770. // map from UTF-8
  12771. $outarr[] = $this->unicode->uni_utf8tolatin[$char];
  12772. } elseif ($char == 0xFFFD) {
  12773. // skip
  12774. } else {
  12775. $outarr[] = 63; // '?' character
  12776. }
  12777. }
  12778. return $outarr;
  12779. }
  12780. /**
  12781. * Converts array of UTF-8 characters to UTF16-BE string.<br>
  12782. * Based on: http://www.faqs.org/rfcs/rfc2781.html
  12783. * <pre>
  12784. * Encoding UTF-16:
  12785. *
  12786. * Encoding of a single character from an ISO 10646 character value to
  12787. * UTF-16 proceeds as follows. Let U be the character number, no greater
  12788. * than 0x10FFFF.
  12789. *
  12790. * 1) If U < 0x10000, encode U as a 16-bit unsigned integer and
  12791. * terminate.
  12792. *
  12793. * 2) Let U' = U - 0x10000. Because U is less than or equal to 0x10FFFF,
  12794. * U' must be less than or equal to 0xFFFFF. That is, U' can be
  12795. * represented in 20 bits.
  12796. *
  12797. * 3) Initialize two 16-bit unsigned integers, W1 and W2, to 0xD800 and
  12798. * 0xDC00, respectively. These integers each have 10 bits free to
  12799. * encode the character value, for a total of 20 bits.
  12800. *
  12801. * 4) Assign the 10 high-order bits of the 20-bit U' to the 10 low-order
  12802. * bits of W1 and the 10 low-order bits of U' to the 10 low-order
  12803. * bits of W2. Terminate.
  12804. *
  12805. * Graphically, steps 2 through 4 look like:
  12806. * U' = yyyyyyyyyyxxxxxxxxxx
  12807. * W1 = 110110yyyyyyyyyy
  12808. * W2 = 110111xxxxxxxxxx
  12809. * </pre>
  12810. * @param $unicode (array) array containing UTF-8 unicode values
  12811. * @param $setbom (boolean) if true set the Byte Order Mark (BOM = 0xFEFF)
  12812. * @return string
  12813. * @protected
  12814. * @author Nicola Asuni
  12815. * @since 2.1.000 (2008-01-08)
  12816. * @see UTF8ToUTF16BE()
  12817. */
  12818. protected function arrUTF8ToUTF16BE($unicode, $setbom=false) {
  12819. $outstr = ''; // string to be returned
  12820. if ($setbom) {
  12821. $outstr .= "\xFE\xFF"; // Byte Order Mark (BOM)
  12822. }
  12823. foreach ($unicode as $char) {
  12824. if ($char == 0x200b) {
  12825. // skip Unicode Character 'ZERO WIDTH SPACE' (DEC:8203, U+200B)
  12826. } elseif ($char == 0xFFFD) {
  12827. $outstr .= "\xFF\xFD"; // replacement character
  12828. } elseif ($char < 0x10000) {
  12829. $outstr .= chr($char >> 0x08);
  12830. $outstr .= chr($char & 0xFF);
  12831. } else {
  12832. $char -= 0x10000;
  12833. $w1 = 0xD800 | ($char >> 0x0a);
  12834. $w2 = 0xDC00 | ($char & 0x3FF);
  12835. $outstr .= chr($w1 >> 0x08);
  12836. $outstr .= chr($w1 & 0xFF);
  12837. $outstr .= chr($w2 >> 0x08);
  12838. $outstr .= chr($w2 & 0xFF);
  12839. }
  12840. }
  12841. return $outstr;
  12842. }
  12843. // ====================================================
  12844. /**
  12845. * Set header font.
  12846. * @param $font (array) font
  12847. * @public
  12848. * @since 1.1
  12849. */
  12850. public function setHeaderFont($font) {
  12851. $this->header_font = $font;
  12852. }
  12853. /**
  12854. * Get header font.
  12855. * @return array()
  12856. * @public
  12857. * @since 4.0.012 (2008-07-24)
  12858. */
  12859. public function getHeaderFont() {
  12860. return $this->header_font;
  12861. }
  12862. /**
  12863. * Set footer font.
  12864. * @param $font (array) font
  12865. * @public
  12866. * @since 1.1
  12867. */
  12868. public function setFooterFont($font) {
  12869. $this->footer_font = $font;
  12870. }
  12871. /**
  12872. * Get Footer font.
  12873. * @return array()
  12874. * @public
  12875. * @since 4.0.012 (2008-07-24)
  12876. */
  12877. public function getFooterFont() {
  12878. return $this->footer_font;
  12879. }
  12880. /**
  12881. * Set language array.
  12882. * @param $language (array)
  12883. * @public
  12884. * @since 1.1
  12885. */
  12886. public function setLanguageArray($language) {
  12887. $this->l = $language;
  12888. if (isset($this->l['a_meta_dir'])) {
  12889. $this->rtl = $this->l['a_meta_dir']=='rtl' ? true : false;
  12890. } else {
  12891. $this->rtl = false;
  12892. }
  12893. }
  12894. /**
  12895. * Returns the PDF data.
  12896. * @public
  12897. */
  12898. public function getPDFData() {
  12899. if ($this->state < 3) {
  12900. $this->Close();
  12901. }
  12902. return $this->buffer;
  12903. }
  12904. /**
  12905. * Output anchor link.
  12906. * @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;)
  12907. * @param $name (string) link name
  12908. * @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
  12909. * @param $firstline (boolean) if true prints only the first line and return the remaining string.
  12910. * @param $color (array) array of RGB text color
  12911. * @param $style (string) font style (U, D, B, I)
  12912. * @param $firstblock (boolean) if true the string is the starting of a line.
  12913. * @return the number of cells used or the remaining text if $firstline = true;
  12914. * @public
  12915. */
  12916. public function addHtmlLink($url, $name, $fill=false, $firstline=false, $color='', $style=-1, $firstblock=false) {
  12917. if (!$this->empty_string($url) AND ($url{0} == '#')) {
  12918. // convert url to internal link
  12919. $lnkdata = explode(',', $url);
  12920. if (isset($lnkdata[0])) {
  12921. $page = intval(substr($lnkdata[0], 1));
  12922. if (empty($page) OR ($page <= 0)) {
  12923. $page = $this->page;
  12924. }
  12925. if (isset($lnkdata[1]) AND (strlen($lnkdata[1]) > 0)) {
  12926. $lnky = floatval($lnkdata[1]);
  12927. } else {
  12928. $lnky = 0;
  12929. }
  12930. $url = $this->AddLink();
  12931. $this->SetLink($url, $lnky, $page);
  12932. }
  12933. }
  12934. // store current settings
  12935. $prevcolor = $this->fgcolor;
  12936. $prevstyle = $this->FontStyle;
  12937. if (empty($color)) {
  12938. $this->SetTextColorArray($this->htmlLinkColorArray);
  12939. } else {
  12940. $this->SetTextColorArray($color);
  12941. }
  12942. if ($style == -1) {
  12943. $this->SetFont('', $this->FontStyle.$this->htmlLinkFontStyle);
  12944. } else {
  12945. $this->SetFont('', $this->FontStyle.$style);
  12946. }
  12947. $ret = $this->Write($this->lasth, $name, $url, $fill, '', false, 0, $firstline, $firstblock, 0);
  12948. // restore settings
  12949. $this->SetFont('', $prevstyle);
  12950. $this->SetTextColorArray($prevcolor);
  12951. return $ret;
  12952. }
  12953. /**
  12954. * Returns an array (RGB or CMYK) from an html color name or a six-digit (i.e. #3FE5AA) or three-digit (i.e. #7FF) hexadecimal color representation.
  12955. * @param $hcolor (string) HTML color.
  12956. * @param $defcol (array) Color to return in case of error.
  12957. * @return array RGB or CMYK color, or false in case of error.
  12958. * @public
  12959. */
  12960. public function convertHTMLColorToDec($hcolor='#FFFFFF', $defcol=array('R'=>128,'G'=>128,'B'=>128)) {
  12961. $color = preg_replace('/[\s]*/', '', $hcolor); // remove extra spaces
  12962. $color = strtolower($color);
  12963. if (($dotpos = strpos($color, '.')) !== false) {
  12964. // remove class parent (i.e.: color.red)
  12965. $color = substr($color, ($dotpos + 1));
  12966. }
  12967. if (strlen($color) == 0) {
  12968. return $defcol;
  12969. }
  12970. // RGB ARRAY
  12971. if (substr($color, 0, 3) == 'rgb') {
  12972. $codes = substr($color, 4);
  12973. $codes = str_replace(')', '', $codes);
  12974. $returncolor = explode(',', $codes);
  12975. foreach ($returncolor as $key => $val) {
  12976. if (strpos($val, '%') > 0) {
  12977. // percentage
  12978. $returncolor[$key] = (255 * intval($val) / 100);
  12979. } else {
  12980. $returncolor[$key] = intval($val);
  12981. }
  12982. // normalize value
  12983. $returncolor[$key] = max(0, min(255, $returncolor[$key]));
  12984. }
  12985. return $returncolor;
  12986. }
  12987. // CMYK ARRAY
  12988. if (substr($color, 0, 4) == 'cmyk') {
  12989. $codes = substr($color, 5);
  12990. $codes = str_replace(')', '', $codes);
  12991. $returncolor = explode(',', $codes);
  12992. foreach ($returncolor as $key => $val) {
  12993. if (strpos($val, '%') !== false) {
  12994. // percentage
  12995. $returncolor[$key] = (100 * intval($val) / 100);
  12996. } else {
  12997. $returncolor[$key] = intval($val);
  12998. }
  12999. // normalize value
  13000. $returncolor[$key] = max(0, min(100, $returncolor[$key]));
  13001. }
  13002. return $returncolor;
  13003. }
  13004. if ($color{0} != '#') {
  13005. // COLOR NAME
  13006. if (isset($this->webcolor[$color])) {
  13007. // web color
  13008. $color_code = $this->webcolor[$color];
  13009. } else {
  13010. // spot color
  13011. $returncolor = $this->getSpotColor($color);
  13012. if ($returncolor === false) {
  13013. $returncolor = $defcol;
  13014. }
  13015. return $returncolor;
  13016. }
  13017. } else {
  13018. $color_code = substr($color, 1);
  13019. }
  13020. // HEXADECIMAL REPRESENTATION
  13021. switch (strlen($color_code)) {
  13022. case 3: {
  13023. // 3-digit RGB hexadecimal representation
  13024. $r = substr($color_code, 0, 1);
  13025. $g = substr($color_code, 1, 1);
  13026. $b = substr($color_code, 2, 1);
  13027. $returncolor = array();
  13028. $returncolor['R'] = max(0, min(255, hexdec($r.$r)));
  13029. $returncolor['G'] = max(0, min(255, hexdec($g.$g)));
  13030. $returncolor['B'] = max(0, min(255, hexdec($b.$b)));
  13031. break;
  13032. }
  13033. case 6: {
  13034. // 6-digit RGB hexadecimal representation
  13035. $returncolor = array();
  13036. $returncolor['R'] = max(0, min(255, hexdec(substr($color_code, 0, 2))));
  13037. $returncolor['G'] = max(0, min(255, hexdec(substr($color_code, 2, 2))));
  13038. $returncolor['B'] = max(0, min(255, hexdec(substr($color_code, 4, 2))));
  13039. break;
  13040. }
  13041. case 8: {
  13042. // 8-digit CMYK hexadecimal representation
  13043. $returncolor = array();
  13044. $returncolor['C'] = max(0, min(100, round(hexdec(substr($color_code, 0, 2)) / 2.55)));
  13045. $returncolor['M'] = max(0, min(100, round(hexdec(substr($color_code, 2, 2)) / 2.55)));
  13046. $returncolor['Y'] = max(0, min(100, round(hexdec(substr($color_code, 4, 2)) / 2.55)));
  13047. $returncolor['K'] = max(0, min(100, round(hexdec(substr($color_code, 6, 2)) / 2.55)));
  13048. break;
  13049. }
  13050. default: {
  13051. $returncolor = $defcol;
  13052. break;
  13053. }
  13054. }
  13055. return $returncolor;
  13056. }
  13057. /**
  13058. * Converts pixels to User's Units.
  13059. * @param $px (int) pixels
  13060. * @return float value in user's unit
  13061. * @public
  13062. * @see setImageScale(), getImageScale()
  13063. */
  13064. public function pixelsToUnits($px) {
  13065. return ($px / ($this->imgscale * $this->k));
  13066. }
  13067. /**
  13068. * Reverse function for htmlentities.
  13069. * Convert entities in UTF-8.
  13070. * @param $text_to_convert (string) Text to convert.
  13071. * @return string converted text string
  13072. * @public
  13073. */
  13074. public function unhtmlentities($text_to_convert) {
  13075. return @html_entity_decode($text_to_convert, ENT_QUOTES, $this->encoding);
  13076. }
  13077. // ENCRYPTION METHODS ----------------------------------
  13078. /**
  13079. * Returns a string containing random data to be used as a seed for encryption methods.
  13080. * @param $seed (string) starting seed value
  13081. * @return string containing random data
  13082. * @author Nicola Asuni
  13083. * @since 5.9.006 (2010-10-19)
  13084. * @protected
  13085. */
  13086. protected function getRandomSeed($seed='') {
  13087. $seed .= microtime();
  13088. if (function_exists('openssl_random_pseudo_bytes') AND (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN')) {
  13089. // this is not used on windows systems because it is very slow for a know bug
  13090. $seed .= openssl_random_pseudo_bytes(512);
  13091. } else {
  13092. for ($i = 0; $i < 23; ++$i) {
  13093. $seed .= uniqid('', true);
  13094. }
  13095. }
  13096. $seed .= uniqid('', true);
  13097. $seed .= rand();
  13098. $seed .= getmypid();
  13099. $seed .= __FILE__;
  13100. $seed .= $this->bufferlen;
  13101. if (isset($_SERVER['REMOTE_ADDR'])) {
  13102. $seed .= $_SERVER['REMOTE_ADDR'];
  13103. }
  13104. if (isset($_SERVER['HTTP_USER_AGENT'])) {
  13105. $seed .= $_SERVER['HTTP_USER_AGENT'];
  13106. }
  13107. if (isset($_SERVER['HTTP_ACCEPT'])) {
  13108. $seed .= $_SERVER['HTTP_ACCEPT'];
  13109. }
  13110. if (isset($_SERVER['HTTP_ACCEPT_ENCODING'])) {
  13111. $seed .= $_SERVER['HTTP_ACCEPT_ENCODING'];
  13112. }
  13113. if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
  13114. $seed .= $_SERVER['HTTP_ACCEPT_LANGUAGE'];
  13115. }
  13116. if (isset($_SERVER['HTTP_ACCEPT_CHARSET'])) {
  13117. $seed .= $_SERVER['HTTP_ACCEPT_CHARSET'];
  13118. }
  13119. $seed .= rand();
  13120. $seed .= uniqid('', true);
  13121. $seed .= microtime();
  13122. return $seed;
  13123. }
  13124. /**
  13125. * Compute encryption key depending on object number where the encrypted data is stored.
  13126. * This is used for all strings and streams without crypt filter specifier.
  13127. * @param $n (int) object number
  13128. * @return int object key
  13129. * @protected
  13130. * @author Nicola Asuni
  13131. * @since 2.0.000 (2008-01-02)
  13132. */
  13133. protected function _objectkey($n) {
  13134. $objkey = $this->encryptdata['key'].pack('VXxx', $n);
  13135. if ($this->encryptdata['mode'] == 2) { // AES-128
  13136. // AES padding
  13137. $objkey .= "\x73\x41\x6C\x54"; // sAlT
  13138. }
  13139. $objkey = substr($this->_md5_16($objkey), 0, (($this->encryptdata['Length'] / 8) + 5));
  13140. $objkey = substr($objkey, 0, 16);
  13141. return $objkey;
  13142. }
  13143. /**
  13144. * Encrypt the input string.
  13145. * @param $n (int) object number
  13146. * @param $s (string) data string to encrypt
  13147. * @return encrypted string
  13148. * @protected
  13149. * @author Nicola Asuni
  13150. * @since 5.0.005 (2010-05-11)
  13151. */
  13152. protected function _encrypt_data($n, $s) {
  13153. if (!$this->encrypted) {
  13154. return $s;
  13155. }
  13156. switch ($this->encryptdata['mode']) {
  13157. case 0: // RC4-40
  13158. case 1: { // RC4-128
  13159. $s = $this->_RC4($this->_objectkey($n), $s);
  13160. break;
  13161. }
  13162. case 2: { // AES-128
  13163. $s = $this->_AES($this->_objectkey($n), $s);
  13164. break;
  13165. }
  13166. case 3: { // AES-256
  13167. $s = $this->_AES($this->encryptdata['key'], $s);
  13168. break;
  13169. }
  13170. }
  13171. return $s;
  13172. }
  13173. /**
  13174. * Put encryption on PDF document.
  13175. * @protected
  13176. * @author Nicola Asuni
  13177. * @since 2.0.000 (2008-01-02)
  13178. */
  13179. protected function _putencryption() {
  13180. if (!$this->encrypted) {
  13181. return;
  13182. }
  13183. $this->encryptdata['objid'] = $this->_newobj();
  13184. $out = '<<';
  13185. if (!isset($this->encryptdata['Filter']) OR empty($this->encryptdata['Filter'])) {
  13186. $this->encryptdata['Filter'] = 'Standard';
  13187. }
  13188. $out .= ' /Filter /'.$this->encryptdata['Filter'];
  13189. if (isset($this->encryptdata['SubFilter']) AND !empty($this->encryptdata['SubFilter'])) {
  13190. $out .= ' /SubFilter /'.$this->encryptdata['SubFilter'];
  13191. }
  13192. if (!isset($this->encryptdata['V']) OR empty($this->encryptdata['V'])) {
  13193. $this->encryptdata['V'] = 1;
  13194. }
  13195. // V is a code specifying the algorithm to be used in encrypting and decrypting the document
  13196. $out .= ' /V '.$this->encryptdata['V'];
  13197. if (isset($this->encryptdata['Length']) AND !empty($this->encryptdata['Length'])) {
  13198. // The length of the encryption key, in bits. The value shall be a multiple of 8, in the range 40 to 256
  13199. $out .= ' /Length '.$this->encryptdata['Length'];
  13200. } else {
  13201. $out .= ' /Length 40';
  13202. }
  13203. if ($this->encryptdata['V'] >= 4) {
  13204. if (!isset($this->encryptdata['StmF']) OR empty($this->encryptdata['StmF'])) {
  13205. $this->encryptdata['StmF'] = 'Identity';
  13206. }
  13207. if (!isset($this->encryptdata['StrF']) OR empty($this->encryptdata['StrF'])) {
  13208. // The name of the crypt filter that shall be used when decrypting all strings in the document.
  13209. $this->encryptdata['StrF'] = 'Identity';
  13210. }
  13211. // A dictionary whose keys shall be crypt filter names and whose values shall be the corresponding crypt filter dictionaries.
  13212. if (isset($this->encryptdata['CF']) AND !empty($this->encryptdata['CF'])) {
  13213. $out .= ' /CF <<';
  13214. $out .= ' /'.$this->encryptdata['StmF'].' <<';
  13215. $out .= ' /Type /CryptFilter';
  13216. if (isset($this->encryptdata['CF']['CFM']) AND !empty($this->encryptdata['CF']['CFM'])) {
  13217. // The method used
  13218. $out .= ' /CFM /'.$this->encryptdata['CF']['CFM'];
  13219. if ($this->encryptdata['pubkey']) {
  13220. $out .= ' /Recipients [';
  13221. foreach ($this->encryptdata['Recipients'] as $rec) {
  13222. $out .= ' <'.$rec.'>';
  13223. }
  13224. $out .= ' ]';
  13225. if (isset($this->encryptdata['CF']['EncryptMetadata']) AND (!$this->encryptdata['CF']['EncryptMetadata'])) {
  13226. $out .= ' /EncryptMetadata false';
  13227. } else {
  13228. $out .= ' /EncryptMetadata true';
  13229. }
  13230. }
  13231. } else {
  13232. $out .= ' /CFM /None';
  13233. }
  13234. if (isset($this->encryptdata['CF']['AuthEvent']) AND !empty($this->encryptdata['CF']['AuthEvent'])) {
  13235. // The event to be used to trigger the authorization that is required to access encryption keys used by this filter.
  13236. $out .= ' /AuthEvent /'.$this->encryptdata['CF']['AuthEvent'];
  13237. } else {
  13238. $out .= ' /AuthEvent /DocOpen';
  13239. }
  13240. if (isset($this->encryptdata['CF']['Length']) AND !empty($this->encryptdata['CF']['Length'])) {
  13241. // The bit length of the encryption key.
  13242. $out .= ' /Length '.$this->encryptdata['CF']['Length'];
  13243. }
  13244. $out .= ' >> >>';
  13245. }
  13246. // The name of the crypt filter that shall be used by default when decrypting streams.
  13247. $out .= ' /StmF /'.$this->encryptdata['StmF'];
  13248. // The name of the crypt filter that shall be used when decrypting all strings in the document.
  13249. $out .= ' /StrF /'.$this->encryptdata['StrF'];
  13250. if (isset($this->encryptdata['EFF']) AND !empty($this->encryptdata['EFF'])) {
  13251. // The name of the crypt filter that shall be used when encrypting embedded file streams that do not have their own crypt filter specifier.
  13252. $out .= ' /EFF /'.$this->encryptdata[''];
  13253. }
  13254. }
  13255. // Additional encryption dictionary entries for the standard security handler
  13256. if ($this->encryptdata['pubkey']) {
  13257. if (($this->encryptdata['V'] < 4) AND isset($this->encryptdata['Recipients']) AND !empty($this->encryptdata['Recipients'])) {
  13258. $out .= ' /Recipients [';
  13259. foreach ($this->encryptdata['Recipients'] as $rec) {
  13260. $out .= ' <'.$rec.'>';
  13261. }
  13262. $out .= ' ]';
  13263. }
  13264. } else {
  13265. $out .= ' /R';
  13266. if ($this->encryptdata['V'] == 5) { // AES-256
  13267. $out .= ' 5';
  13268. $out .= ' /OE ('.$this->_escape($this->encryptdata['OE']).')';
  13269. $out .= ' /UE ('.$this->_escape($this->encryptdata['UE']).')';
  13270. $out .= ' /Perms ('.$this->_escape($this->encryptdata['perms']).')';
  13271. } elseif ($this->encryptdata['V'] == 4) { // AES-128
  13272. $out .= ' 4';
  13273. } elseif ($this->encryptdata['V'] < 2) { // RC-40
  13274. $out .= ' 2';
  13275. } else { // RC-128
  13276. $out .= ' 3';
  13277. }
  13278. $out .= ' /O ('.$this->_escape($this->encryptdata['O']).')';
  13279. $out .= ' /U ('.$this->_escape($this->encryptdata['U']).')';
  13280. $out .= ' /P '.$this->encryptdata['P'];
  13281. if (isset($this->encryptdata['EncryptMetadata']) AND (!$this->encryptdata['EncryptMetadata'])) {
  13282. $out .= ' /EncryptMetadata false';
  13283. } else {
  13284. $out .= ' /EncryptMetadata true';
  13285. }
  13286. }
  13287. $out .= ' >>';
  13288. $out .= "\n".'endobj';
  13289. $this->_out($out);
  13290. }
  13291. /**
  13292. * Returns the input text encrypted using RC4 algorithm and the specified key.
  13293. * RC4 is the standard encryption algorithm used in PDF format
  13294. * @param $key (string) encryption key
  13295. * @param $text (String) input text to be encrypted
  13296. * @return String encrypted text
  13297. * @protected
  13298. * @since 2.0.000 (2008-01-02)
  13299. * @author Klemen Vodopivec, Nicola Asuni
  13300. */
  13301. protected function _RC4($key, $text) {
  13302. if (function_exists('mcrypt_decrypt') AND ($out = @mcrypt_decrypt(MCRYPT_ARCFOUR, $key, $text, MCRYPT_MODE_STREAM, ''))) {
  13303. // try to use mcrypt function if exist
  13304. return $out;
  13305. }
  13306. if ($this->last_enc_key != $key) {
  13307. $k = str_repeat($key, ((256 / strlen($key)) + 1));
  13308. $rc4 = range(0, 255);
  13309. $j = 0;
  13310. for ($i = 0; $i < 256; ++$i) {
  13311. $t = $rc4[$i];
  13312. $j = ($j + $t + ord($k[$i])) % 256;
  13313. $rc4[$i] = $rc4[$j];
  13314. $rc4[$j] = $t;
  13315. }
  13316. $this->last_enc_key = $key;
  13317. $this->last_enc_key_c = $rc4;
  13318. } else {
  13319. $rc4 = $this->last_enc_key_c;
  13320. }
  13321. $len = strlen($text);
  13322. $a = 0;
  13323. $b = 0;
  13324. $out = '';
  13325. for ($i = 0; $i < $len; ++$i) {
  13326. $a = ($a + 1) % 256;
  13327. $t = $rc4[$a];
  13328. $b = ($b + $t) % 256;
  13329. $rc4[$a] = $rc4[$b];
  13330. $rc4[$b] = $t;
  13331. $k = $rc4[($rc4[$a] + $rc4[$b]) % 256];
  13332. $out .= chr(ord($text[$i]) ^ $k);
  13333. }
  13334. return $out;
  13335. }
  13336. /**
  13337. * Returns the input text exrypted using AES algorithm and the specified key.
  13338. * This method requires mcrypt.
  13339. * @param $key (string) encryption key
  13340. * @param $text (String) input text to be encrypted
  13341. * @return String encrypted text
  13342. * @protected
  13343. * @author Nicola Asuni
  13344. * @since 5.0.005 (2010-05-11)
  13345. */
  13346. protected function _AES($key, $text) {
  13347. // padding (RFC 2898, PKCS #5: Password-Based Cryptography Specification Version 2.0)
  13348. $padding = 16 - (strlen($text) % 16);
  13349. $text .= str_repeat(chr($padding), $padding);
  13350. $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC), MCRYPT_RAND);
  13351. $text = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $text, MCRYPT_MODE_CBC, $iv);
  13352. $text = $iv.$text;
  13353. return $text;
  13354. }
  13355. /**
  13356. * Encrypts a string using MD5 and returns it's value as a binary string.
  13357. * @param $str (string) input string
  13358. * @return String MD5 encrypted binary string
  13359. * @protected
  13360. * @since 2.0.000 (2008-01-02)
  13361. * @author Klemen Vodopivec
  13362. */
  13363. protected function _md5_16($str) {
  13364. return pack('H*', md5($str));
  13365. }
  13366. /**
  13367. * Compute U value (used for encryption)
  13368. * @return string U value
  13369. * @protected
  13370. * @since 2.0.000 (2008-01-02)
  13371. * @author Nicola Asuni
  13372. */
  13373. protected function _Uvalue() {
  13374. if ($this->encryptdata['mode'] == 0) { // RC4-40
  13375. return $this->_RC4($this->encryptdata['key'], $this->enc_padding);
  13376. } elseif ($this->encryptdata['mode'] < 3) { // RC4-128, AES-128
  13377. $tmp = $this->_md5_16($this->enc_padding.$this->encryptdata['fileid']);
  13378. $enc = $this->_RC4($this->encryptdata['key'], $tmp);
  13379. $len = strlen($tmp);
  13380. for ($i = 1; $i <= 19; ++$i) {
  13381. $ek = '';
  13382. for ($j = 0; $j < $len; ++$j) {
  13383. $ek .= chr(ord($this->encryptdata['key'][$j]) ^ $i);
  13384. }
  13385. $enc = $this->_RC4($ek, $enc);
  13386. }
  13387. $enc .= str_repeat("\x00", 16);
  13388. return substr($enc, 0, 32);
  13389. } elseif ($this->encryptdata['mode'] == 3) { // AES-256
  13390. $seed = $this->_md5_16($this->getRandomSeed());
  13391. // User Validation Salt
  13392. $this->encryptdata['UVS'] = substr($seed, 0, 8);
  13393. // User Key Salt
  13394. $this->encryptdata['UKS'] = substr($seed, 8, 16);
  13395. return hash('sha256', $this->encryptdata['user_password'].$this->encryptdata['UVS'], true).$this->encryptdata['UVS'].$this->encryptdata['UKS'];
  13396. }
  13397. }
  13398. /**
  13399. * Compute UE value (used for encryption)
  13400. * @return string UE value
  13401. * @protected
  13402. * @since 5.9.006 (2010-10-19)
  13403. * @author Nicola Asuni
  13404. */
  13405. protected function _UEvalue() {
  13406. $hashkey = hash('sha256', $this->encryptdata['user_password'].$this->encryptdata['UKS'], true);
  13407. $iv = str_repeat("\x00", mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC));
  13408. return mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $hashkey, $this->encryptdata['key'], MCRYPT_MODE_CBC, $iv);
  13409. }
  13410. /**
  13411. * Compute O value (used for encryption)
  13412. * @return string O value
  13413. * @protected
  13414. * @since 2.0.000 (2008-01-02)
  13415. * @author Nicola Asuni
  13416. */
  13417. protected function _Ovalue() {
  13418. if ($this->encryptdata['mode'] < 3) { // RC4-40, RC4-128, AES-128
  13419. $tmp = $this->_md5_16($this->encryptdata['owner_password']);
  13420. if ($this->encryptdata['mode'] > 0) {
  13421. for ($i = 0; $i < 50; ++$i) {
  13422. $tmp = $this->_md5_16($tmp);
  13423. }
  13424. }
  13425. $owner_key = substr($tmp, 0, ($this->encryptdata['Length'] / 8));
  13426. $enc = $this->_RC4($owner_key, $this->encryptdata['user_password']);
  13427. if ($this->encryptdata['mode'] > 0) {
  13428. $len = strlen($owner_key);
  13429. for ($i = 1; $i <= 19; ++$i) {
  13430. $ek = '';
  13431. for ($j = 0; $j < $len; ++$j) {
  13432. $ek .= chr(ord($owner_key[$j]) ^ $i);
  13433. }
  13434. $enc = $this->_RC4($ek, $enc);
  13435. }
  13436. }
  13437. return $enc;
  13438. } elseif ($this->encryptdata['mode'] == 3) { // AES-256
  13439. $seed = $this->_md5_16($this->getRandomSeed());
  13440. // Owner Validation Salt
  13441. $this->encryptdata['OVS'] = substr($seed, 0, 8);
  13442. // Owner Key Salt
  13443. $this->encryptdata['OKS'] = substr($seed, 8, 16);
  13444. return hash('sha256', $this->encryptdata['owner_password'].$this->encryptdata['OVS'].$this->encryptdata['U'], true).$this->encryptdata['OVS'].$this->encryptdata['OKS'];
  13445. }
  13446. }
  13447. /**
  13448. * Compute OE value (used for encryption)
  13449. * @return string OE value
  13450. * @protected
  13451. * @since 5.9.006 (2010-10-19)
  13452. * @author Nicola Asuni
  13453. */
  13454. protected function _OEvalue() {
  13455. $hashkey = hash('sha256', $this->encryptdata['owner_password'].$this->encryptdata['OKS'].$this->encryptdata['U'], true);
  13456. $iv = str_repeat("\x00", mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC));
  13457. return mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $hashkey, $this->encryptdata['key'], MCRYPT_MODE_CBC, $iv);
  13458. }
  13459. /**
  13460. * Convert password for AES-256 encryption mode
  13461. * @param $password (string) password
  13462. * @return string password
  13463. * @protected
  13464. * @since 5.9.006 (2010-10-19)
  13465. * @author Nicola Asuni
  13466. */
  13467. protected function _fixAES256Password($password) {
  13468. $psw = ''; // password to be returned
  13469. $psw_array = $this->utf8Bidi($this->UTF8StringToArray($password), $password, $this->rtl);
  13470. foreach ($psw_array as $c) {
  13471. $psw .= $this->unichr($c);
  13472. }
  13473. return substr($psw, 0, 127);
  13474. }
  13475. /**
  13476. * Compute encryption key
  13477. * @protected
  13478. * @since 2.0.000 (2008-01-02)
  13479. * @author Nicola Asuni
  13480. */
  13481. protected function _generateencryptionkey() {
  13482. $keybytelen = ($this->encryptdata['Length'] / 8);
  13483. if (!$this->encryptdata['pubkey']) { // standard mode
  13484. if ($this->encryptdata['mode'] == 3) { // AES-256
  13485. // generate 256 bit random key
  13486. $this->encryptdata['key'] = substr(hash('sha256', $this->getRandomSeed(), true), 0, $keybytelen);
  13487. // truncate passwords
  13488. $this->encryptdata['user_password'] = $this->_fixAES256Password($this->encryptdata['user_password']);
  13489. $this->encryptdata['owner_password'] = $this->_fixAES256Password($this->encryptdata['owner_password']);
  13490. // Compute U value
  13491. $this->encryptdata['U'] = $this->_Uvalue();
  13492. // Compute UE value
  13493. $this->encryptdata['UE'] = $this->_UEvalue();
  13494. // Compute O value
  13495. $this->encryptdata['O'] = $this->_Ovalue();
  13496. // Compute OE value
  13497. $this->encryptdata['OE'] = $this->_OEvalue();
  13498. // Compute P value
  13499. $this->encryptdata['P'] = $this->encryptdata['protection'];
  13500. // Computing the encryption dictionary's Perms (permissions) value
  13501. $perms = $this->getEncPermissionsString($this->encryptdata['protection']); // bytes 0-3
  13502. $perms .= chr(255).chr(255).chr(255).chr(255); // bytes 4-7
  13503. if (isset($this->encryptdata['CF']['EncryptMetadata']) AND (!$this->encryptdata['CF']['EncryptMetadata'])) { // byte 8
  13504. $perms .= 'F';
  13505. } else {
  13506. $perms .= 'T';
  13507. }
  13508. $perms .= 'adb'; // bytes 9-11
  13509. $perms .= 'nick'; // bytes 12-15
  13510. $iv = str_repeat("\x00", mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB));
  13511. $this->encryptdata['perms'] = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $this->encryptdata['key'], $perms, MCRYPT_MODE_ECB, $iv);
  13512. } else { // RC4-40, RC4-128, AES-128
  13513. // Pad passwords
  13514. $this->encryptdata['user_password'] = substr($this->encryptdata['user_password'].$this->enc_padding, 0, 32);
  13515. $this->encryptdata['owner_password'] = substr($this->encryptdata['owner_password'].$this->enc_padding, 0, 32);
  13516. // Compute O value
  13517. $this->encryptdata['O'] = $this->_Ovalue();
  13518. // get default permissions (reverse byte order)
  13519. $permissions = $this->getEncPermissionsString($this->encryptdata['protection']);
  13520. // Compute encryption key
  13521. $tmp = $this->_md5_16($this->encryptdata['user_password'].$this->encryptdata['O'].$permissions.$this->encryptdata['fileid']);
  13522. if ($this->encryptdata['mode'] > 0) {
  13523. for ($i = 0; $i < 50; ++$i) {
  13524. $tmp = $this->_md5_16(substr($tmp, 0, $keybytelen));
  13525. }
  13526. }
  13527. $this->encryptdata['key'] = substr($tmp, 0, $keybytelen);
  13528. // Compute U value
  13529. $this->encryptdata['U'] = $this->_Uvalue();
  13530. // Compute P value
  13531. $this->encryptdata['P'] = $this->encryptdata['protection'];
  13532. }
  13533. } else { // Public-Key mode
  13534. // random 20-byte seed
  13535. $seed = sha1($this->getRandomSeed(), true);
  13536. $recipient_bytes = '';
  13537. foreach ($this->encryptdata['pubkeys'] as $pubkey) {
  13538. // for each public certificate
  13539. if (isset($pubkey['p'])) {
  13540. $pkprotection = $this->getUserPermissionCode($pubkey['p'], $this->encryptdata['mode']);
  13541. } else {
  13542. $pkprotection = $this->encryptdata['protection'];
  13543. }
  13544. // get default permissions (reverse byte order)
  13545. $pkpermissions = $this->getEncPermissionsString($pkprotection);
  13546. // envelope data
  13547. $envelope = $seed.$pkpermissions;
  13548. // write the envelope data to a temporary file
  13549. $tempkeyfile = tempnam(K_PATH_CACHE, 'tmpkey_');
  13550. $f = fopen($tempkeyfile, 'wb');
  13551. if (!$f) {
  13552. $this->Error('Unable to create temporary key file: '.$tempkeyfile);
  13553. }
  13554. $envelope_length = strlen($envelope);
  13555. fwrite($f, $envelope, $envelope_length);
  13556. fclose($f);
  13557. $tempencfile = tempnam(K_PATH_CACHE, 'tmpenc_');
  13558. if (!openssl_pkcs7_encrypt($tempkeyfile, $tempencfile, $pubkey['c'], array(), PKCS7_DETACHED | PKCS7_BINARY)) {
  13559. $this->Error('Unable to encrypt the file: '.$tempkeyfile);
  13560. }
  13561. unlink($tempkeyfile);
  13562. // read encryption signature
  13563. $signature = file_get_contents($tempencfile, false, null, $envelope_length);
  13564. unlink($tempencfile);
  13565. // extract signature
  13566. $signature = substr($signature, strpos($signature, 'Content-Disposition'));
  13567. $tmparr = explode("\n\n", $signature);
  13568. $signature = trim($tmparr[1]);
  13569. unset($tmparr);
  13570. // decode signature
  13571. $signature = base64_decode($signature);
  13572. // convert signature to hex
  13573. $hexsignature = current(unpack('H*', $signature));
  13574. // store signature on recipients array
  13575. $this->encryptdata['Recipients'][] = $hexsignature;
  13576. // The bytes of each item in the Recipients array of PKCS#7 objects in the order in which they appear in the array
  13577. $recipient_bytes .= $signature;
  13578. }
  13579. // calculate encryption key
  13580. if ($this->encryptdata['mode'] == 3) { // AES-256
  13581. $this->encryptdata['key'] = substr(hash('sha256', $seed.$recipient_bytes, true), 0, $keybytelen);
  13582. } else { // RC4-40, RC4-128, AES-128
  13583. $this->encryptdata['key'] = substr(sha1($seed.$recipient_bytes, true), 0, $keybytelen);
  13584. }
  13585. }
  13586. }
  13587. /**
  13588. * Return the premission code used on encryption (P value).
  13589. * @param $permissions (Array) the set of permissions (specify the ones you want to block).
  13590. * @param $mode (int) encryption strength: 0 = RC4 40 bit; 1 = RC4 128 bit; 2 = AES 128 bit; 3 = AES 256 bit.
  13591. * @protected
  13592. * @since 5.0.005 (2010-05-12)
  13593. * @author Nicola Asuni
  13594. */
  13595. protected function getUserPermissionCode($permissions, $mode=0) {
  13596. $options = array(
  13597. 'owner' => 2, // bit 2 -- inverted logic: cleared by default
  13598. 'print' => 4, // bit 3
  13599. 'modify' => 8, // bit 4
  13600. 'copy' => 16, // bit 5
  13601. 'annot-forms' => 32, // bit 6
  13602. 'fill-forms' => 256, // bit 9
  13603. 'extract' => 512, // bit 10
  13604. 'assemble' => 1024,// bit 11
  13605. 'print-high' => 2048 // bit 12
  13606. );
  13607. $protection = 2147422012; // 32 bit: (01111111 11111111 00001111 00111100)
  13608. foreach ($permissions as $permission) {
  13609. if (!isset($options[$permission])) {
  13610. $this->Error('Incorrect permission: '.$permission);
  13611. }
  13612. if (($mode > 0) OR ($options[$permission] <= 32)) {
  13613. // set only valid permissions
  13614. if ($options[$permission] == 2) {
  13615. // the logic for bit 2 is inverted (cleared by default)
  13616. $protection += $options[$permission];
  13617. } else {
  13618. $protection -= $options[$permission];
  13619. }
  13620. }
  13621. }
  13622. return $protection;
  13623. }
  13624. /**
  13625. * Set document protection
  13626. * Remark: the protection against modification is for people who have the full Acrobat product.
  13627. * 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.
  13628. * 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.
  13629. * @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>
  13630. * @param $user_pass (String) user password. Empty by default.
  13631. * @param $owner_pass (String) owner password. If not specified, a random value is used.
  13632. * @param $mode (int) encryption strength: 0 = RC4 40 bit; 1 = RC4 128 bit; 2 = AES 128 bit; 3 = AES 256 bit.
  13633. * @param $pubkeys (String) array of recipients containing public-key certificates ('c') and permissions ('p'). For example: array(array('c' => 'file://../tcpdf.crt', 'p' => array('print')))
  13634. * @public
  13635. * @since 2.0.000 (2008-01-02)
  13636. * @author Nicola Asuni
  13637. */
  13638. 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) {
  13639. if ($this->pdfa_mode) {
  13640. // encryption is not allowed in PDF/A mode
  13641. return;
  13642. }
  13643. $this->encryptdata['protection'] = $this->getUserPermissionCode($permissions, $mode);
  13644. if (($pubkeys !== null) AND (is_array($pubkeys))) {
  13645. // public-key mode
  13646. $this->encryptdata['pubkeys'] = $pubkeys;
  13647. if ($mode == 0) {
  13648. // public-Key Security requires at least 128 bit
  13649. $mode = 1;
  13650. }
  13651. if (!function_exists('openssl_pkcs7_encrypt')) {
  13652. $this->Error('Public-Key Security requires openssl library.');
  13653. }
  13654. // Set Public-Key filter (availabe are: Entrust.PPKEF, Adobe.PPKLite, Adobe.PubSec)
  13655. $this->encryptdata['pubkey'] = true;
  13656. $this->encryptdata['Filter'] = 'Adobe.PubSec';
  13657. $this->encryptdata['StmF'] = 'DefaultCryptFilter';
  13658. $this->encryptdata['StrF'] = 'DefaultCryptFilter';
  13659. } else {
  13660. // standard mode (password mode)
  13661. $this->encryptdata['pubkey'] = false;
  13662. $this->encryptdata['Filter'] = 'Standard';
  13663. $this->encryptdata['StmF'] = 'StdCF';
  13664. $this->encryptdata['StrF'] = 'StdCF';
  13665. }
  13666. if ($mode > 1) { // AES
  13667. if (!extension_loaded('mcrypt')) {
  13668. $this->Error('AES encryption requires mcrypt library (http://www.php.net/manual/en/mcrypt.requirements.php).');
  13669. }
  13670. if (mcrypt_get_cipher_name(MCRYPT_RIJNDAEL_128) === false) {
  13671. $this->Error('AES encryption requires MCRYPT_RIJNDAEL_128 cypher.');
  13672. }
  13673. if (($mode == 3) AND !function_exists('hash')) {
  13674. // the Hash extension requires no external libraries and is enabled by default as of PHP 5.1.2.
  13675. $this->Error('AES 256 encryption requires HASH Message Digest Framework (http://www.php.net/manual/en/book.hash.php).');
  13676. }
  13677. }
  13678. if ($owner_pass === null) {
  13679. $owner_pass = md5($this->getRandomSeed());
  13680. }
  13681. $this->encryptdata['user_password'] = $user_pass;
  13682. $this->encryptdata['owner_password'] = $owner_pass;
  13683. $this->encryptdata['mode'] = $mode;
  13684. switch ($mode) {
  13685. case 0: { // RC4 40 bit
  13686. $this->encryptdata['V'] = 1;
  13687. $this->encryptdata['Length'] = 40;
  13688. $this->encryptdata['CF']['CFM'] = 'V2';
  13689. break;
  13690. }
  13691. case 1: { // RC4 128 bit
  13692. $this->encryptdata['V'] = 2;
  13693. $this->encryptdata['Length'] = 128;
  13694. $this->encryptdata['CF']['CFM'] = 'V2';
  13695. if ($this->encryptdata['pubkey']) {
  13696. $this->encryptdata['SubFilter'] = 'adbe.pkcs7.s4';
  13697. $this->encryptdata['Recipients'] = array();
  13698. }
  13699. break;
  13700. }
  13701. case 2: { // AES 128 bit
  13702. $this->encryptdata['V'] = 4;
  13703. $this->encryptdata['Length'] = 128;
  13704. $this->encryptdata['CF']['CFM'] = 'AESV2';
  13705. $this->encryptdata['CF']['Length'] = 128;
  13706. if ($this->encryptdata['pubkey']) {
  13707. $this->encryptdata['SubFilter'] = 'adbe.pkcs7.s5';
  13708. $this->encryptdata['Recipients'] = array();
  13709. }
  13710. break;
  13711. }
  13712. case 3: { // AES 256 bit
  13713. $this->encryptdata['V'] = 5;
  13714. $this->encryptdata['Length'] = 256;
  13715. $this->encryptdata['CF']['CFM'] = 'AESV3';
  13716. $this->encryptdata['CF']['Length'] = 256;
  13717. if ($this->encryptdata['pubkey']) {
  13718. $this->encryptdata['SubFilter'] = 'adbe.pkcs7.s5';
  13719. $this->encryptdata['Recipients'] = array();
  13720. }
  13721. break;
  13722. }
  13723. }
  13724. $this->encrypted = true;
  13725. $this->encryptdata['fileid'] = $this->convertHexStringToString($this->file_id);
  13726. $this->_generateencryptionkey();
  13727. }
  13728. /**
  13729. * Convert hexadecimal string to string
  13730. * @param $bs (string) byte-string to convert
  13731. * @return String
  13732. * @protected
  13733. * @since 5.0.005 (2010-05-12)
  13734. * @author Nicola Asuni
  13735. */
  13736. protected function convertHexStringToString($bs) {
  13737. $string = ''; // string to be returned
  13738. $bslength = strlen($bs);
  13739. if (($bslength % 2) != 0) {
  13740. // padding
  13741. $bs .= '0';
  13742. ++$bslength;
  13743. }
  13744. for ($i = 0; $i < $bslength; $i += 2) {
  13745. $string .= chr(hexdec($bs[$i].$bs[($i + 1)]));
  13746. }
  13747. return $string;
  13748. }
  13749. /**
  13750. * Convert string to hexadecimal string (byte string)
  13751. * @param $s (string) string to convert
  13752. * @return byte string
  13753. * @protected
  13754. * @since 5.0.010 (2010-05-17)
  13755. * @author Nicola Asuni
  13756. */
  13757. protected function convertStringToHexString($s) {
  13758. $bs = '';
  13759. $chars = preg_split('//', $s, -1, PREG_SPLIT_NO_EMPTY);
  13760. foreach ($chars as $c) {
  13761. $bs .= sprintf('%02s', dechex(ord($c)));
  13762. }
  13763. return $bs;
  13764. }
  13765. /**
  13766. * Convert encryption P value to a string of bytes, low-order byte first.
  13767. * @param $protection (string) 32bit encryption permission value (P value)
  13768. * @return String
  13769. * @protected
  13770. * @since 5.0.005 (2010-05-12)
  13771. * @author Nicola Asuni
  13772. */
  13773. protected function getEncPermissionsString($protection) {
  13774. $binprot = sprintf('%032b', $protection);
  13775. $str = chr(bindec(substr($binprot, 24, 8)));
  13776. $str .= chr(bindec(substr($binprot, 16, 8)));
  13777. $str .= chr(bindec(substr($binprot, 8, 8)));
  13778. $str .= chr(bindec(substr($binprot, 0, 8)));
  13779. return $str;
  13780. }
  13781. // END OF ENCRYPTION FUNCTIONS -------------------------
  13782. // START TRANSFORMATIONS SECTION -----------------------
  13783. /**
  13784. * Starts a 2D tranformation saving current graphic state.
  13785. * This function must be called before scaling, mirroring, translation, rotation and skewing.
  13786. * Use StartTransform() before, and StopTransform() after the transformations to restore the normal behavior.
  13787. * @public
  13788. * @since 2.1.000 (2008-01-07)
  13789. * @see StartTransform(), StopTransform()
  13790. */
  13791. public function StartTransform() {
  13792. $this->_out('q');
  13793. if ($this->inxobj) {
  13794. // we are inside an XObject template
  13795. $this->xobjects[$this->xobjid]['transfmrk'][] = strlen($this->xobjects[$this->xobjid]['outdata']);
  13796. } else {
  13797. $this->transfmrk[$this->page][] = $this->pagelen[$this->page];
  13798. }
  13799. ++$this->transfmatrix_key;
  13800. $this->transfmatrix[$this->transfmatrix_key] = array();
  13801. }
  13802. /**
  13803. * Stops a 2D tranformation restoring previous graphic state.
  13804. * This function must be called after scaling, mirroring, translation, rotation and skewing.
  13805. * Use StartTransform() before, and StopTransform() after the transformations to restore the normal behavior.
  13806. * @public
  13807. * @since 2.1.000 (2008-01-07)
  13808. * @see StartTransform(), StopTransform()
  13809. */
  13810. public function StopTransform() {
  13811. $this->_out('Q');
  13812. if (isset($this->transfmatrix[$this->transfmatrix_key])) {
  13813. array_pop($this->transfmatrix[$this->transfmatrix_key]);
  13814. --$this->transfmatrix_key;
  13815. }
  13816. if ($this->inxobj) {
  13817. // we are inside an XObject template
  13818. array_pop($this->xobjects[$this->xobjid]['transfmrk']);
  13819. } else {
  13820. array_pop($this->transfmrk[$this->page]);
  13821. }
  13822. }
  13823. /**
  13824. * Horizontal Scaling.
  13825. * @param $s_x (float) scaling factor for width as percent. 0 is not allowed.
  13826. * @param $x (int) abscissa of the scaling center. Default is current x position
  13827. * @param $y (int) ordinate of the scaling center. Default is current y position
  13828. * @public
  13829. * @since 2.1.000 (2008-01-07)
  13830. * @see StartTransform(), StopTransform()
  13831. */
  13832. public function ScaleX($s_x, $x='', $y='') {
  13833. $this->Scale($s_x, 100, $x, $y);
  13834. }
  13835. /**
  13836. * Vertical Scaling.
  13837. * @param $s_y (float) scaling factor for height as percent. 0 is not allowed.
  13838. * @param $x (int) abscissa of the scaling center. Default is current x position
  13839. * @param $y (int) ordinate of the scaling center. Default is current y position
  13840. * @public
  13841. * @since 2.1.000 (2008-01-07)
  13842. * @see StartTransform(), StopTransform()
  13843. */
  13844. public function ScaleY($s_y, $x='', $y='') {
  13845. $this->Scale(100, $s_y, $x, $y);
  13846. }
  13847. /**
  13848. * Vertical and horizontal proportional Scaling.
  13849. * @param $s (float) scaling factor for width and height as percent. 0 is not allowed.
  13850. * @param $x (int) abscissa of the scaling center. Default is current x position
  13851. * @param $y (int) ordinate of the scaling center. Default is current y position
  13852. * @public
  13853. * @since 2.1.000 (2008-01-07)
  13854. * @see StartTransform(), StopTransform()
  13855. */
  13856. public function ScaleXY($s, $x='', $y='') {
  13857. $this->Scale($s, $s, $x, $y);
  13858. }
  13859. /**
  13860. * Vertical and horizontal non-proportional Scaling.
  13861. * @param $s_x (float) scaling factor for width as percent. 0 is not allowed.
  13862. * @param $s_y (float) scaling factor for height as percent. 0 is not allowed.
  13863. * @param $x (int) abscissa of the scaling center. Default is current x position
  13864. * @param $y (int) ordinate of the scaling center. Default is current y position
  13865. * @public
  13866. * @since 2.1.000 (2008-01-07)
  13867. * @see StartTransform(), StopTransform()
  13868. */
  13869. public function Scale($s_x, $s_y, $x='', $y='') {
  13870. if ($x === '') {
  13871. $x = $this->x;
  13872. }
  13873. if ($y === '') {
  13874. $y = $this->y;
  13875. }
  13876. if (($s_x == 0) OR ($s_y == 0)) {
  13877. $this->Error('Please do not use values equal to zero for scaling');
  13878. }
  13879. $y = ($this->h - $y) * $this->k;
  13880. $x *= $this->k;
  13881. //calculate elements of transformation matrix
  13882. $s_x /= 100;
  13883. $s_y /= 100;
  13884. $tm = array();
  13885. $tm[0] = $s_x;
  13886. $tm[1] = 0;
  13887. $tm[2] = 0;
  13888. $tm[3] = $s_y;
  13889. $tm[4] = $x * (1 - $s_x);
  13890. $tm[5] = $y * (1 - $s_y);
  13891. //scale the coordinate system
  13892. $this->Transform($tm);
  13893. }
  13894. /**
  13895. * Horizontal Mirroring.
  13896. * @param $x (int) abscissa of the point. Default is current x position
  13897. * @public
  13898. * @since 2.1.000 (2008-01-07)
  13899. * @see StartTransform(), StopTransform()
  13900. */
  13901. public function MirrorH($x='') {
  13902. $this->Scale(-100, 100, $x);
  13903. }
  13904. /**
  13905. * Verical Mirroring.
  13906. * @param $y (int) ordinate of the point. Default is current y position
  13907. * @public
  13908. * @since 2.1.000 (2008-01-07)
  13909. * @see StartTransform(), StopTransform()
  13910. */
  13911. public function MirrorV($y='') {
  13912. $this->Scale(100, -100, '', $y);
  13913. }
  13914. /**
  13915. * Point reflection mirroring.
  13916. * @param $x (int) abscissa of the point. Default is current x position
  13917. * @param $y (int) ordinate of the point. Default is current y position
  13918. * @public
  13919. * @since 2.1.000 (2008-01-07)
  13920. * @see StartTransform(), StopTransform()
  13921. */
  13922. public function MirrorP($x='',$y='') {
  13923. $this->Scale(-100, -100, $x, $y);
  13924. }
  13925. /**
  13926. * Reflection against a straight line through point (x, y) with the gradient angle (angle).
  13927. * @param $angle (float) gradient angle of the straight line. Default is 0 (horizontal line).
  13928. * @param $x (int) abscissa of the point. Default is current x position
  13929. * @param $y (int) ordinate of the point. Default is current y position
  13930. * @public
  13931. * @since 2.1.000 (2008-01-07)
  13932. * @see StartTransform(), StopTransform()
  13933. */
  13934. public function MirrorL($angle=0, $x='',$y='') {
  13935. $this->Scale(-100, 100, $x, $y);
  13936. $this->Rotate(-2*($angle-90), $x, $y);
  13937. }
  13938. /**
  13939. * Translate graphic object horizontally.
  13940. * @param $t_x (int) movement to the right (or left for RTL)
  13941. * @public
  13942. * @since 2.1.000 (2008-01-07)
  13943. * @see StartTransform(), StopTransform()
  13944. */
  13945. public function TranslateX($t_x) {
  13946. $this->Translate($t_x, 0);
  13947. }
  13948. /**
  13949. * Translate graphic object vertically.
  13950. * @param $t_y (int) movement to the bottom
  13951. * @public
  13952. * @since 2.1.000 (2008-01-07)
  13953. * @see StartTransform(), StopTransform()
  13954. */
  13955. public function TranslateY($t_y) {
  13956. $this->Translate(0, $t_y);
  13957. }
  13958. /**
  13959. * Translate graphic object horizontally and vertically.
  13960. * @param $t_x (int) movement to the right
  13961. * @param $t_y (int) movement to the bottom
  13962. * @public
  13963. * @since 2.1.000 (2008-01-07)
  13964. * @see StartTransform(), StopTransform()
  13965. */
  13966. public function Translate($t_x, $t_y) {
  13967. //calculate elements of transformation matrix
  13968. $tm = array();
  13969. $tm[0] = 1;
  13970. $tm[1] = 0;
  13971. $tm[2] = 0;
  13972. $tm[3] = 1;
  13973. $tm[4] = $t_x * $this->k;
  13974. $tm[5] = -$t_y * $this->k;
  13975. //translate the coordinate system
  13976. $this->Transform($tm);
  13977. }
  13978. /**
  13979. * Rotate object.
  13980. * @param $angle (float) angle in degrees for counter-clockwise rotation
  13981. * @param $x (int) abscissa of the rotation center. Default is current x position
  13982. * @param $y (int) ordinate of the rotation center. Default is current y position
  13983. * @public
  13984. * @since 2.1.000 (2008-01-07)
  13985. * @see StartTransform(), StopTransform()
  13986. */
  13987. public function Rotate($angle, $x='', $y='') {
  13988. if ($x === '') {
  13989. $x = $this->x;
  13990. }
  13991. if ($y === '') {
  13992. $y = $this->y;
  13993. }
  13994. $y = ($this->h - $y) * $this->k;
  13995. $x *= $this->k;
  13996. //calculate elements of transformation matrix
  13997. $tm = array();
  13998. $tm[0] = cos(deg2rad($angle));
  13999. $tm[1] = sin(deg2rad($angle));
  14000. $tm[2] = -$tm[1];
  14001. $tm[3] = $tm[0];
  14002. $tm[4] = $x + ($tm[1] * $y) - ($tm[0] * $x);
  14003. $tm[5] = $y - ($tm[0] * $y) - ($tm[1] * $x);
  14004. //rotate the coordinate system around ($x,$y)
  14005. $this->Transform($tm);
  14006. }
  14007. /**
  14008. * Skew horizontally.
  14009. * @param $angle_x (float) angle in degrees between -90 (skew to the left) and 90 (skew to the right)
  14010. * @param $x (int) abscissa of the skewing center. default is current x position
  14011. * @param $y (int) ordinate of the skewing center. default is current y position
  14012. * @public
  14013. * @since 2.1.000 (2008-01-07)
  14014. * @see StartTransform(), StopTransform()
  14015. */
  14016. public function SkewX($angle_x, $x='', $y='') {
  14017. $this->Skew($angle_x, 0, $x, $y);
  14018. }
  14019. /**
  14020. * Skew vertically.
  14021. * @param $angle_y (float) angle in degrees between -90 (skew to the bottom) and 90 (skew to the top)
  14022. * @param $x (int) abscissa of the skewing center. default is current x position
  14023. * @param $y (int) ordinate of the skewing center. default is current y position
  14024. * @public
  14025. * @since 2.1.000 (2008-01-07)
  14026. * @see StartTransform(), StopTransform()
  14027. */
  14028. public function SkewY($angle_y, $x='', $y='') {
  14029. $this->Skew(0, $angle_y, $x, $y);
  14030. }
  14031. /**
  14032. * Skew.
  14033. * @param $angle_x (float) angle in degrees between -90 (skew to the left) and 90 (skew to the right)
  14034. * @param $angle_y (float) angle in degrees between -90 (skew to the bottom) and 90 (skew to the top)
  14035. * @param $x (int) abscissa of the skewing center. default is current x position
  14036. * @param $y (int) ordinate of the skewing center. default is current y position
  14037. * @public
  14038. * @since 2.1.000 (2008-01-07)
  14039. * @see StartTransform(), StopTransform()
  14040. */
  14041. public function Skew($angle_x, $angle_y, $x='', $y='') {
  14042. if ($x === '') {
  14043. $x = $this->x;
  14044. }
  14045. if ($y === '') {
  14046. $y = $this->y;
  14047. }
  14048. if (($angle_x <= -90) OR ($angle_x >= 90) OR ($angle_y <= -90) OR ($angle_y >= 90)) {
  14049. $this->Error('Please use values between -90 and +90 degrees for Skewing.');
  14050. }
  14051. $x *= $this->k;
  14052. $y = ($this->h - $y) * $this->k;
  14053. //calculate elements of transformation matrix
  14054. $tm = array();
  14055. $tm[0] = 1;
  14056. $tm[1] = tan(deg2rad($angle_y));
  14057. $tm[2] = tan(deg2rad($angle_x));
  14058. $tm[3] = 1;
  14059. $tm[4] = -$tm[2] * $y;
  14060. $tm[5] = -$tm[1] * $x;
  14061. //skew the coordinate system
  14062. $this->Transform($tm);
  14063. }
  14064. /**
  14065. * Apply graphic transformations.
  14066. * @param $tm (array) transformation matrix
  14067. * @protected
  14068. * @since 2.1.000 (2008-01-07)
  14069. * @see StartTransform(), StopTransform()
  14070. */
  14071. protected function Transform($tm) {
  14072. $this->_out(sprintf('%.3F %.3F %.3F %.3F %.3F %.3F cm', $tm[0], $tm[1], $tm[2], $tm[3], $tm[4], $tm[5]));
  14073. // add tranformation matrix
  14074. $this->transfmatrix[$this->transfmatrix_key][] = array('a' => $tm[0], 'b' => $tm[1], 'c' => $tm[2], 'd' => $tm[3], 'e' => $tm[4], 'f' => $tm[5]);
  14075. // update transformation mark
  14076. if ($this->inxobj) {
  14077. // we are inside an XObject template
  14078. if (end($this->xobjects[$this->xobjid]['transfmrk']) !== false) {
  14079. $key = key($this->xobjects[$this->xobjid]['transfmrk']);
  14080. $this->xobjects[$this->xobjid]['transfmrk'][$key] = strlen($this->xobjects[$this->xobjid]['outdata']);
  14081. }
  14082. } elseif (end($this->transfmrk[$this->page]) !== false) {
  14083. $key = key($this->transfmrk[$this->page]);
  14084. $this->transfmrk[$this->page][$key] = $this->pagelen[$this->page];
  14085. }
  14086. }
  14087. // END TRANSFORMATIONS SECTION -------------------------
  14088. // START GRAPHIC FUNCTIONS SECTION ---------------------
  14089. // The following section is based on the code provided by David Hernandez Sanz
  14090. /**
  14091. * 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.
  14092. * @param $width (float) The width.
  14093. * @public
  14094. * @since 1.0
  14095. * @see Line(), Rect(), Cell(), MultiCell()
  14096. */
  14097. public function SetLineWidth($width) {
  14098. //Set line width
  14099. $this->LineWidth = $width;
  14100. $this->linestyleWidth = sprintf('%.2F w', ($width * $this->k));
  14101. if ($this->page > 0) {
  14102. $this->_out($this->linestyleWidth);
  14103. }
  14104. }
  14105. /**
  14106. * Returns the current the line width.
  14107. * @return int Line width
  14108. * @public
  14109. * @since 2.1.000 (2008-01-07)
  14110. * @see Line(), SetLineWidth()
  14111. */
  14112. public function GetLineWidth() {
  14113. return $this->LineWidth;
  14114. }
  14115. /**
  14116. * Set line style.
  14117. * @param $style (array) Line style. Array with keys among the following:
  14118. * <ul>
  14119. * <li>width (float): Width of the line in user units.</li>
  14120. * <li>cap (string): Type of cap to put on the line. Possible values are:
  14121. * butt, round, square. The difference between "square" and "butt" is that
  14122. * "square" projects a flat end past the end of the line.</li>
  14123. * <li>join (string): Type of join. Possible values are: miter, round,
  14124. * bevel.</li>
  14125. * <li>dash (mixed): Dash pattern. Is 0 (without dash) or string with
  14126. * series of length values, which are the lengths of the on and off dashes.
  14127. * For example: "2" represents 2 on, 2 off, 2 on, 2 off, ...; "2,1" is 2 on,
  14128. * 1 off, 2 on, 1 off, ...</li>
  14129. * <li>phase (integer): Modifier on the dash pattern which is used to shift
  14130. * the point at which the pattern starts.</li>
  14131. * <li>color (array): Draw color. Format: array(GREY) or array(R,G,B) or array(C,M,Y,K).</li>
  14132. * </ul>
  14133. * @param $ret (boolean) if true do not send the command.
  14134. * @return string the PDF command
  14135. * @public
  14136. * @since 2.1.000 (2008-01-08)
  14137. */
  14138. public function SetLineStyle($style, $ret=false) {
  14139. $s = ''; // string to be returned
  14140. if (!is_array($style)) {
  14141. return;
  14142. }
  14143. if (isset($style['width'])) {
  14144. $this->LineWidth = $style['width'];
  14145. $this->linestyleWidth = sprintf('%.2F w', ($style['width'] * $this->k));
  14146. $s .= $this->linestyleWidth.' ';
  14147. }
  14148. if (isset($style['cap'])) {
  14149. $ca = array('butt' => 0, 'round'=> 1, 'square' => 2);
  14150. if (isset($ca[$style['cap']])) {
  14151. $this->linestyleCap = $ca[$style['cap']].' J';
  14152. $s .= $this->linestyleCap.' ';
  14153. }
  14154. }
  14155. if (isset($style['join'])) {
  14156. $ja = array('miter' => 0, 'round' => 1, 'bevel' => 2);
  14157. if (isset($ja[$style['join']])) {
  14158. $this->linestyleJoin = $ja[$style['join']].' j';
  14159. $s .= $this->linestyleJoin.' ';
  14160. }
  14161. }
  14162. if (isset($style['dash'])) {
  14163. $dash_string = '';
  14164. if ($style['dash']) {
  14165. if (preg_match('/^.+,/', $style['dash']) > 0) {
  14166. $tab = explode(',', $style['dash']);
  14167. } else {
  14168. $tab = array($style['dash']);
  14169. }
  14170. $dash_string = '';
  14171. foreach ($tab as $i => $v) {
  14172. if ($i) {
  14173. $dash_string .= ' ';
  14174. }
  14175. $dash_string .= sprintf('%.2F', $v);
  14176. }
  14177. }
  14178. if (!isset($style['phase']) OR !$style['dash']) {
  14179. $style['phase'] = 0;
  14180. }
  14181. $this->linestyleDash = sprintf('[%s] %.2F d', $dash_string, $style['phase']);
  14182. $s .= $this->linestyleDash.' ';
  14183. }
  14184. if (isset($style['color'])) {
  14185. $s .= $this->SetDrawColorArray($style['color'], true).' ';
  14186. }
  14187. if (!$ret) {
  14188. $this->_out($s);
  14189. }
  14190. return $s;
  14191. }
  14192. /**
  14193. * Begin a new subpath by moving the current point to coordinates (x, y), omitting any connecting line segment.
  14194. * @param $x (float) Abscissa of point.
  14195. * @param $y (float) Ordinate of point.
  14196. * @protected
  14197. * @since 2.1.000 (2008-01-08)
  14198. */
  14199. protected function _outPoint($x, $y) {
  14200. $this->_out(sprintf('%.2F %.2F m', $x * $this->k, ($this->h - $y) * $this->k));
  14201. }
  14202. /**
  14203. * Append a straight line segment from the current point to the point (x, y).
  14204. * The new current point shall be (x, y).
  14205. * @param $x (float) Abscissa of end point.
  14206. * @param $y (float) Ordinate of end point.
  14207. * @protected
  14208. * @since 2.1.000 (2008-01-08)
  14209. */
  14210. protected function _outLine($x, $y) {
  14211. $this->_out(sprintf('%.2F %.2F l', $x * $this->k, ($this->h - $y) * $this->k));
  14212. }
  14213. /**
  14214. * Append a rectangle to the current path as a complete subpath, with lower-left corner (x, y) and dimensions widthand height in user space.
  14215. * @param $x (float) Abscissa of upper-left corner.
  14216. * @param $y (float) Ordinate of upper-left corner.
  14217. * @param $w (float) Width.
  14218. * @param $h (float) Height.
  14219. * @param $op (string) options
  14220. * @protected
  14221. * @since 2.1.000 (2008-01-08)
  14222. */
  14223. protected function _outRect($x, $y, $w, $h, $op) {
  14224. $this->_out(sprintf('%.2F %.2F %.2F %.2F re %s', $x * $this->k, ($this->h - $y) * $this->k, $w * $this->k, -$h * $this->k, $op));
  14225. }
  14226. /**
  14227. * Append a cubic Bézier 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 Bézier control points.
  14228. * The new current point shall be (x3, y3).
  14229. * @param $x1 (float) Abscissa of control point 1.
  14230. * @param $y1 (float) Ordinate of control point 1.
  14231. * @param $x2 (float) Abscissa of control point 2.
  14232. * @param $y2 (float) Ordinate of control point 2.
  14233. * @param $x3 (float) Abscissa of end point.
  14234. * @param $y3 (float) Ordinate of end point.
  14235. * @protected
  14236. * @since 2.1.000 (2008-01-08)
  14237. */
  14238. protected function _outCurve($x1, $y1, $x2, $y2, $x3, $y3) {
  14239. $this->_out(sprintf('%.2F %.2F %.2F %.2F %.2F %.2F 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));
  14240. }
  14241. /**
  14242. * Append a cubic Bézier 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 Bézier control points.
  14243. * The new current point shall be (x3, y3).
  14244. * @param $x2 (float) Abscissa of control point 2.
  14245. * @param $y2 (float) Ordinate of control point 2.
  14246. * @param $x3 (float) Abscissa of end point.
  14247. * @param $y3 (float) Ordinate of end point.
  14248. * @protected
  14249. * @since 4.9.019 (2010-04-26)
  14250. */
  14251. protected function _outCurveV($x2, $y2, $x3, $y3) {
  14252. $this->_out(sprintf('%.2F %.2F %.2F %.2F v', $x2 * $this->k, ($this->h - $y2) * $this->k, $x3 * $this->k, ($this->h - $y3) * $this->k));
  14253. }
  14254. /**
  14255. * Append a cubic Bézier 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 Bézier control points.
  14256. * The new current point shall be (x3, y3).
  14257. * @param $x1 (float) Abscissa of control point 1.
  14258. * @param $y1 (float) Ordinate of control point 1.
  14259. * @param $x3 (float) Abscissa of end point.
  14260. * @param $y3 (float) Ordinate of end point.
  14261. * @protected
  14262. * @since 2.1.000 (2008-01-08)
  14263. */
  14264. protected function _outCurveY($x1, $y1, $x3, $y3) {
  14265. $this->_out(sprintf('%.2F %.2F %.2F %.2F y', $x1 * $this->k, ($this->h - $y1) * $this->k, $x3 * $this->k, ($this->h - $y3) * $this->k));
  14266. }
  14267. /**
  14268. * Draws a line between two points.
  14269. * @param $x1 (float) Abscissa of first point.
  14270. * @param $y1 (float) Ordinate of first point.
  14271. * @param $x2 (float) Abscissa of second point.
  14272. * @param $y2 (float) Ordinate of second point.
  14273. * @param $style (array) Line style. Array like for SetLineStyle(). Default value: default line style (empty array).
  14274. * @public
  14275. * @since 1.0
  14276. * @see SetLineWidth(), SetDrawColor(), SetLineStyle()
  14277. */
  14278. public function Line($x1, $y1, $x2, $y2, $style=array()) {
  14279. if (is_array($style)) {
  14280. $this->SetLineStyle($style);
  14281. }
  14282. $this->_outPoint($x1, $y1);
  14283. $this->_outLine($x2, $y2);
  14284. $this->_out('S');
  14285. }
  14286. /**
  14287. * Draws a rectangle.
  14288. * @param $x (float) Abscissa of upper-left corner.
  14289. * @param $y (float) Ordinate of upper-left corner.
  14290. * @param $w (float) Width.
  14291. * @param $h (float) Height.
  14292. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  14293. * @param $border_style (array) Border style of rectangle. Array with keys among the following:
  14294. * <ul>
  14295. * <li>all: Line style of all borders. Array like for SetLineStyle().</li>
  14296. * <li>L, T, R, B or combinations: Line style of left, top, right or bottom border. Array like for SetLineStyle().</li>
  14297. * </ul>
  14298. * If a key is not present or is null, not draws the border. Default value: default line style (empty array).
  14299. * @param $border_style (array) Border style of rectangle. Array like for SetLineStyle(). Default value: default line style (empty array).
  14300. * @param $fill_color (array) Fill color. Format: array(GREY) or array(R,G,B) or array(C,M,Y,K). Default value: default color (empty array).
  14301. * @public
  14302. * @since 1.0
  14303. * @see SetLineStyle()
  14304. */
  14305. public function Rect($x, $y, $w, $h, $style='', $border_style=array(), $fill_color=array()) {
  14306. if (!(false === strpos($style, 'F')) AND !empty($fill_color)) {
  14307. $this->SetFillColorArray($fill_color);
  14308. }
  14309. $op = $this->getPathPaintOperator($style);
  14310. if ((!$border_style) OR (isset($border_style['all']))) {
  14311. if (isset($border_style['all']) AND $border_style['all']) {
  14312. $this->SetLineStyle($border_style['all']);
  14313. $border_style = array();
  14314. }
  14315. }
  14316. $this->_outRect($x, $y, $w, $h, $op);
  14317. if ($border_style) {
  14318. $border_style2 = array();
  14319. foreach ($border_style as $line => $value) {
  14320. $length = strlen($line);
  14321. for ($i = 0; $i < $length; ++$i) {
  14322. $border_style2[$line[$i]] = $value;
  14323. }
  14324. }
  14325. $border_style = $border_style2;
  14326. if (isset($border_style['L']) AND $border_style['L']) {
  14327. $this->Line($x, $y, $x, $y + $h, $border_style['L']);
  14328. }
  14329. if (isset($border_style['T']) AND $border_style['T']) {
  14330. $this->Line($x, $y, $x + $w, $y, $border_style['T']);
  14331. }
  14332. if (isset($border_style['R']) AND $border_style['R']) {
  14333. $this->Line($x + $w, $y, $x + $w, $y + $h, $border_style['R']);
  14334. }
  14335. if (isset($border_style['B']) AND $border_style['B']) {
  14336. $this->Line($x, $y + $h, $x + $w, $y + $h, $border_style['B']);
  14337. }
  14338. }
  14339. }
  14340. /**
  14341. * Draws a Bezier curve.
  14342. * The Bezier curve is a tangent to the line between the control points at
  14343. * either end of the curve.
  14344. * @param $x0 (float) Abscissa of start point.
  14345. * @param $y0 (float) Ordinate of start point.
  14346. * @param $x1 (float) Abscissa of control point 1.
  14347. * @param $y1 (float) Ordinate of control point 1.
  14348. * @param $x2 (float) Abscissa of control point 2.
  14349. * @param $y2 (float) Ordinate of control point 2.
  14350. * @param $x3 (float) Abscissa of end point.
  14351. * @param $y3 (float) Ordinate of end point.
  14352. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  14353. * @param $line_style (array) Line style of curve. Array like for SetLineStyle(). Default value: default line style (empty array).
  14354. * @param $fill_color (array) Fill color. Format: array(GREY) or array(R,G,B) or array(C,M,Y,K). Default value: default color (empty array).
  14355. * @public
  14356. * @see SetLineStyle()
  14357. * @since 2.1.000 (2008-01-08)
  14358. */
  14359. public function Curve($x0, $y0, $x1, $y1, $x2, $y2, $x3, $y3, $style='', $line_style=array(), $fill_color=array()) {
  14360. if (!(false === strpos($style, 'F')) AND isset($fill_color)) {
  14361. $this->SetFillColorArray($fill_color);
  14362. }
  14363. $op = $this->getPathPaintOperator($style);
  14364. if ($line_style) {
  14365. $this->SetLineStyle($line_style);
  14366. }
  14367. $this->_outPoint($x0, $y0);
  14368. $this->_outCurve($x1, $y1, $x2, $y2, $x3, $y3);
  14369. $this->_out($op);
  14370. }
  14371. /**
  14372. * Draws a poly-Bezier curve.
  14373. * Each Bezier curve segment is a tangent to the line between the control points at
  14374. * either end of the curve.
  14375. * @param $x0 (float) Abscissa of start point.
  14376. * @param $y0 (float) Ordinate of start point.
  14377. * @param $segments (float) An array of bezier descriptions. Format: array(x1, y1, x2, y2, x3, y3).
  14378. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  14379. * @param $line_style (array) Line style of curve. Array like for SetLineStyle(). Default value: default line style (empty array).
  14380. * @param $fill_color (array) Fill color. Format: array(GREY) or array(R,G,B) or array(C,M,Y,K). Default value: default color (empty array).
  14381. * @public
  14382. * @see SetLineStyle()
  14383. * @since 3.0008 (2008-05-12)
  14384. */
  14385. public function Polycurve($x0, $y0, $segments, $style='', $line_style=array(), $fill_color=array()) {
  14386. if (!(false === strpos($style, 'F')) AND isset($fill_color)) {
  14387. $this->SetFillColorArray($fill_color);
  14388. }
  14389. $op = $this->getPathPaintOperator($style);
  14390. if ($op == 'f') {
  14391. $line_style = array();
  14392. }
  14393. if ($line_style) {
  14394. $this->SetLineStyle($line_style);
  14395. }
  14396. $this->_outPoint($x0, $y0);
  14397. foreach ($segments as $segment) {
  14398. list($x1, $y1, $x2, $y2, $x3, $y3) = $segment;
  14399. $this->_outCurve($x1, $y1, $x2, $y2, $x3, $y3);
  14400. }
  14401. $this->_out($op);
  14402. }
  14403. /**
  14404. * Draws an ellipse.
  14405. * An ellipse is formed from n Bezier curves.
  14406. * @param $x0 (float) Abscissa of center point.
  14407. * @param $y0 (float) Ordinate of center point.
  14408. * @param $rx (float) Horizontal radius.
  14409. * @param $ry (float) Vertical radius (if ry = 0 then is a circle, see Circle()). Default value: 0.
  14410. * @param $angle: (float) Angle oriented (anti-clockwise). Default value: 0.
  14411. * @param $astart: (float) Angle start of draw line. Default value: 0.
  14412. * @param $afinish: (float) Angle finish of draw line. Default value: 360.
  14413. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  14414. * @param $line_style (array) Line style of ellipse. Array like for SetLineStyle(). Default value: default line style (empty array).
  14415. * @param $fill_color (array) Fill color. Format: array(GREY) or array(R,G,B) or array(C,M,Y,K). Default value: default color (empty array).
  14416. * @param $nc (integer) Number of curves used to draw a 90 degrees portion of ellipse.
  14417. * @author Nicola Asuni
  14418. * @public
  14419. * @since 2.1.000 (2008-01-08)
  14420. */
  14421. public function Ellipse($x0, $y0, $rx, $ry='', $angle=0, $astart=0, $afinish=360, $style='', $line_style=array(), $fill_color=array(), $nc=2) {
  14422. if ($this->empty_string($ry) OR ($ry == 0)) {
  14423. $ry = $rx;
  14424. }
  14425. if (!(false === strpos($style, 'F')) AND isset($fill_color)) {
  14426. $this->SetFillColorArray($fill_color);
  14427. }
  14428. $op = $this->getPathPaintOperator($style);
  14429. if ($op == 'f') {
  14430. $line_style = array();
  14431. }
  14432. if ($line_style) {
  14433. $this->SetLineStyle($line_style);
  14434. }
  14435. $this->_outellipticalarc($x0, $y0, $rx, $ry, $angle, $astart, $afinish, false, $nc, true, true, false);
  14436. $this->_out($op);
  14437. }
  14438. /**
  14439. * Append an elliptical arc to the current path.
  14440. * An ellipse is formed from n Bezier curves.
  14441. * @param $xc (float) Abscissa of center point.
  14442. * @param $yc (float) Ordinate of center point.
  14443. * @param $rx (float) Horizontal radius.
  14444. * @param $ry (float) Vertical radius (if ry = 0 then is a circle, see Circle()). Default value: 0.
  14445. * @param $xang: (float) Angle between the X-axis and the major axis of the ellipse. Default value: 0.
  14446. * @param $angs: (float) Angle start of draw line. Default value: 0.
  14447. * @param $angf: (float) Angle finish of draw line. Default value: 360.
  14448. * @param $pie (boolean) if true do not mark the border point (used to draw pie sectors).
  14449. * @param $nc (integer) Number of curves used to draw a 90 degrees portion of ellipse.
  14450. * @param $startpoint (boolean) if true output a starting point.
  14451. * @param $ccw (boolean) if true draws in counter-clockwise.
  14452. * @param $svg (boolean) if true the angles are in svg mode (already calculated).
  14453. * @return array bounding box coordinates (x min, y min, x max, y max)
  14454. * @author Nicola Asuni
  14455. * @protected
  14456. * @since 4.9.019 (2010-04-26)
  14457. */
  14458. protected function _outellipticalarc($xc, $yc, $rx, $ry, $xang=0, $angs=0, $angf=360, $pie=false, $nc=2, $startpoint=true, $ccw=true, $svg=false) {
  14459. $k = $this->k;
  14460. if ($nc < 2) {
  14461. $nc = 2;
  14462. }
  14463. $xmin = 2147483647;
  14464. $ymin = 2147483647;
  14465. $xmax = 0;
  14466. $ymax = 0;
  14467. if ($pie) {
  14468. // center of the arc
  14469. $this->_outPoint($xc, $yc);
  14470. }
  14471. $xang = deg2rad((float) $xang);
  14472. $angs = deg2rad((float) $angs);
  14473. $angf = deg2rad((float) $angf);
  14474. if ($svg) {
  14475. $as = $angs;
  14476. $af = $angf;
  14477. } else {
  14478. $as = atan2((sin($angs) / $ry), (cos($angs) / $rx));
  14479. $af = atan2((sin($angf) / $ry), (cos($angf) / $rx));
  14480. }
  14481. if ($as < 0) {
  14482. $as += (2 * M_PI);
  14483. }
  14484. if ($af < 0) {
  14485. $af += (2 * M_PI);
  14486. }
  14487. if ($ccw AND ($as > $af)) {
  14488. // reverse rotation
  14489. $as -= (2 * M_PI);
  14490. } elseif (!$ccw AND ($as < $af)) {
  14491. // reverse rotation
  14492. $af -= (2 * M_PI);
  14493. }
  14494. $total_angle = ($af - $as);
  14495. if ($nc < 2) {
  14496. $nc = 2;
  14497. }
  14498. // total arcs to draw
  14499. $nc *= (2 * abs($total_angle) / M_PI);
  14500. $nc = round($nc) + 1;
  14501. // angle of each arc
  14502. $arcang = ($total_angle / $nc);
  14503. // center point in PDF coordinates
  14504. $x0 = $xc;
  14505. $y0 = ($this->h - $yc);
  14506. // starting angle
  14507. $ang = $as;
  14508. $alpha = sin($arcang) * ((sqrt(4 + (3 * pow(tan(($arcang) / 2), 2))) - 1) / 3);
  14509. $cos_xang = cos($xang);
  14510. $sin_xang = sin($xang);
  14511. $cos_ang = cos($ang);
  14512. $sin_ang = sin($ang);
  14513. // first arc point
  14514. $px1 = $x0 + ($rx * $cos_xang * $cos_ang) - ($ry * $sin_xang * $sin_ang);
  14515. $py1 = $y0 + ($rx * $sin_xang * $cos_ang) + ($ry * $cos_xang * $sin_ang);
  14516. // first Bezier control point
  14517. $qx1 = ($alpha * ((-$rx * $cos_xang * $sin_ang) - ($ry * $sin_xang * $cos_ang)));
  14518. $qy1 = ($alpha * ((-$rx * $sin_xang * $sin_ang) + ($ry * $cos_xang * $cos_ang)));
  14519. if ($pie) {
  14520. // line from center to arc starting point
  14521. $this->_outLine($px1, $this->h - $py1);
  14522. } elseif ($startpoint) {
  14523. // arc starting point
  14524. $this->_outPoint($px1, $this->h - $py1);
  14525. }
  14526. // draw arcs
  14527. for ($i = 1; $i <= $nc; ++$i) {
  14528. // starting angle
  14529. $ang = $as + ($i * $arcang);
  14530. if ($i == $nc) {
  14531. $ang = $af;
  14532. }
  14533. $cos_ang = cos($ang);
  14534. $sin_ang = sin($ang);
  14535. // second arc point
  14536. $px2 = $x0 + ($rx * $cos_xang * $cos_ang) - ($ry * $sin_xang * $sin_ang);
  14537. $py2 = $y0 + ($rx * $sin_xang * $cos_ang) + ($ry * $cos_xang * $sin_ang);
  14538. // second Bezier control point
  14539. $qx2 = ($alpha * ((-$rx * $cos_xang * $sin_ang) - ($ry * $sin_xang * $cos_ang)));
  14540. $qy2 = ($alpha * ((-$rx * $sin_xang * $sin_ang) + ($ry * $cos_xang * $cos_ang)));
  14541. // draw arc
  14542. $cx1 = ($px1 + $qx1);
  14543. $cy1 = ($this->h - ($py1 + $qy1));
  14544. $cx2 = ($px2 - $qx2);
  14545. $cy2 = ($this->h - ($py2 - $qy2));
  14546. $cx3 = $px2;
  14547. $cy3 = ($this->h - $py2);
  14548. $this->_outCurve($cx1, $cy1, $cx2, $cy2, $cx3, $cy3);
  14549. // get bounding box coordinates
  14550. $xmin = min($xmin, $cx1, $cx2, $cx3);
  14551. $ymin = min($ymin, $cy1, $cy2, $cy3);
  14552. $xmax = max($xmax, $cx1, $cx2, $cx3);
  14553. $ymax = max($ymax, $cy1, $cy2, $cy3);
  14554. // move to next point
  14555. $px1 = $px2;
  14556. $py1 = $py2;
  14557. $qx1 = $qx2;
  14558. $qy1 = $qy2;
  14559. }
  14560. if ($pie) {
  14561. $this->_outLine($xc, $yc);
  14562. // get bounding box coordinates
  14563. $xmin = min($xmin, $xc);
  14564. $ymin = min($ymin, $yc);
  14565. $xmax = max($xmax, $xc);
  14566. $ymax = max($ymax, $yc);
  14567. }
  14568. return array($xmin, $ymin, $xmax, $ymax);
  14569. }
  14570. /**
  14571. * Draws a circle.
  14572. * A circle is formed from n Bezier curves.
  14573. * @param $x0 (float) Abscissa of center point.
  14574. * @param $y0 (float) Ordinate of center point.
  14575. * @param $r (float) Radius.
  14576. * @param $angstr: (float) Angle start of draw line. Default value: 0.
  14577. * @param $angend: (float) Angle finish of draw line. Default value: 360.
  14578. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  14579. * @param $line_style (array) Line style of circle. Array like for SetLineStyle(). Default value: default line style (empty array).
  14580. * @param $fill_color (array) Fill color. Format: array(red, green, blue). Default value: default color (empty array).
  14581. * @param $nc (integer) Number of curves used to draw a 90 degrees portion of circle.
  14582. * @public
  14583. * @since 2.1.000 (2008-01-08)
  14584. */
  14585. public function Circle($x0, $y0, $r, $angstr=0, $angend=360, $style='', $line_style=array(), $fill_color=array(), $nc=2) {
  14586. $this->Ellipse($x0, $y0, $r, $r, 0, $angstr, $angend, $style, $line_style, $fill_color, $nc);
  14587. }
  14588. /**
  14589. * Draws a polygonal line
  14590. * @param $p (array) Points 0 to ($np - 1). Array with values (x0, y0, x1, y1,..., x(np-1), y(np - 1))
  14591. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  14592. * @param $line_style (array) Line style of polygon. Array with keys among the following:
  14593. * <ul>
  14594. * <li>all: Line style of all lines. Array like for SetLineStyle().</li>
  14595. * <li>0 to ($np - 1): Line style of each line. Array like for SetLineStyle().</li>
  14596. * </ul>
  14597. * If a key is not present or is null, not draws the line. Default value is default line style (empty array).
  14598. * @param $fill_color (array) Fill color. Format: array(GREY) or array(R,G,B) or array(C,M,Y,K). Default value: default color (empty array).
  14599. * @since 4.8.003 (2009-09-15)
  14600. * @public
  14601. */
  14602. public function PolyLine($p, $style='', $line_style=array(), $fill_color=array()) {
  14603. $this->Polygon($p, $style, $line_style, $fill_color, false);
  14604. }
  14605. /**
  14606. * Draws a polygon.
  14607. * @param $p (array) Points 0 to ($np - 1). Array with values (x0, y0, x1, y1,..., x(np-1), y(np - 1))
  14608. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  14609. * @param $line_style (array) Line style of polygon. Array with keys among the following:
  14610. * <ul>
  14611. * <li>all: Line style of all lines. Array like for SetLineStyle().</li>
  14612. * <li>0 to ($np - 1): Line style of each line. Array like for SetLineStyle().</li>
  14613. * </ul>
  14614. * If a key is not present or is null, not draws the line. Default value is default line style (empty array).
  14615. * @param $fill_color (array) Fill color. Format: array(GREY) or array(R,G,B) or array(C,M,Y,K). Default value: default color (empty array).
  14616. * @param $closed (boolean) if true the polygon is closes, otherwise will remain open
  14617. * @public
  14618. * @since 2.1.000 (2008-01-08)
  14619. */
  14620. public function Polygon($p, $style='', $line_style=array(), $fill_color=array(), $closed=true) {
  14621. $nc = count($p); // number of coordinates
  14622. $np = $nc / 2; // number of points
  14623. if ($closed) {
  14624. // close polygon by adding the first 2 points at the end (one line)
  14625. for ($i = 0; $i < 4; ++$i) {
  14626. $p[$nc + $i] = $p[$i];
  14627. }
  14628. // copy style for the last added line
  14629. if (isset($line_style[0])) {
  14630. $line_style[$np] = $line_style[0];
  14631. }
  14632. $nc += 4;
  14633. }
  14634. if (!(false === strpos($style, 'F')) AND isset($fill_color)) {
  14635. $this->SetFillColorArray($fill_color);
  14636. }
  14637. $op = $this->getPathPaintOperator($style);
  14638. if ($op == 'f') {
  14639. $line_style = array();
  14640. }
  14641. $draw = true;
  14642. if ($line_style) {
  14643. if (isset($line_style['all'])) {
  14644. $this->SetLineStyle($line_style['all']);
  14645. } else {
  14646. $draw = false;
  14647. if ($op == 'B') {
  14648. // draw fill
  14649. $op = 'f';
  14650. $this->_outPoint($p[0], $p[1]);
  14651. for ($i = 2; $i < $nc; $i = $i + 2) {
  14652. $this->_outLine($p[$i], $p[$i + 1]);
  14653. }
  14654. $this->_out($op);
  14655. }
  14656. // draw outline
  14657. $this->_outPoint($p[0], $p[1]);
  14658. for ($i = 2; $i < $nc; $i = $i + 2) {
  14659. $line_num = ($i / 2) - 1;
  14660. if (isset($line_style[$line_num])) {
  14661. if ($line_style[$line_num] != 0) {
  14662. if (is_array($line_style[$line_num])) {
  14663. $this->_out('S');
  14664. $this->SetLineStyle($line_style[$line_num]);
  14665. $this->_outPoint($p[$i - 2], $p[$i - 1]);
  14666. $this->_outLine($p[$i], $p[$i + 1]);
  14667. $this->_out('S');
  14668. $this->_outPoint($p[$i], $p[$i + 1]);
  14669. } else {
  14670. $this->_outLine($p[$i], $p[$i + 1]);
  14671. }
  14672. }
  14673. } else {
  14674. $this->_outLine($p[$i], $p[$i + 1]);
  14675. }
  14676. }
  14677. $this->_out($op);
  14678. }
  14679. }
  14680. if ($draw) {
  14681. $this->_outPoint($p[0], $p[1]);
  14682. for ($i = 2; $i < $nc; $i = $i + 2) {
  14683. $this->_outLine($p[$i], $p[$i + 1]);
  14684. }
  14685. $this->_out($op);
  14686. }
  14687. }
  14688. /**
  14689. * Draws a regular polygon.
  14690. * @param $x0 (float) Abscissa of center point.
  14691. * @param $y0 (float) Ordinate of center point.
  14692. * @param $r: (float) Radius of inscribed circle.
  14693. * @param $ns (integer) Number of sides.
  14694. * @param $angle (float) Angle oriented (anti-clockwise). Default value: 0.
  14695. * @param $draw_circle (boolean) Draw inscribed circle or not. Default value: false.
  14696. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  14697. * @param $line_style (array) Line style of polygon sides. Array with keys among the following:
  14698. * <ul>
  14699. * <li>all: Line style of all sides. Array like for SetLineStyle().</li>
  14700. * <li>0 to ($ns - 1): Line style of each side. Array like for SetLineStyle().</li>
  14701. * </ul>
  14702. * If a key is not present or is null, not draws the side. Default value is default line style (empty array).
  14703. * @param $fill_color (array) Fill color. Format: array(red, green, blue). Default value: default color (empty array).
  14704. * @param $circle_style (string) Style of rendering of inscribed circle (if draws). Possible values are:
  14705. * <ul>
  14706. * <li>D or empty string: Draw (default).</li>
  14707. * <li>F: Fill.</li>
  14708. * <li>DF or FD: Draw and fill.</li>
  14709. * <li>CNZ: Clipping mode (using the even-odd rule to determine which regions lie inside the clipping path).</li>
  14710. * <li>CEO: Clipping mode (using the nonzero winding number rule to determine which regions lie inside the clipping path).</li>
  14711. * </ul>
  14712. * @param $circle_outLine_style (array) Line style of inscribed circle (if draws). Array like for SetLineStyle(). Default value: default line style (empty array).
  14713. * @param $circle_fill_color (array) Fill color of inscribed circle (if draws). Format: array(red, green, blue). Default value: default color (empty array).
  14714. * @public
  14715. * @since 2.1.000 (2008-01-08)
  14716. */
  14717. 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()) {
  14718. if (3 > $ns) {
  14719. $ns = 3;
  14720. }
  14721. if ($draw_circle) {
  14722. $this->Circle($x0, $y0, $r, 0, 360, $circle_style, $circle_outLine_style, $circle_fill_color);
  14723. }
  14724. $p = array();
  14725. for ($i = 0; $i < $ns; ++$i) {
  14726. $a = $angle + ($i * 360 / $ns);
  14727. $a_rad = deg2rad((float) $a);
  14728. $p[] = $x0 + ($r * sin($a_rad));
  14729. $p[] = $y0 + ($r * cos($a_rad));
  14730. }
  14731. $this->Polygon($p, $style, $line_style, $fill_color);
  14732. }
  14733. /**
  14734. * Draws a star polygon
  14735. * @param $x0 (float) Abscissa of center point.
  14736. * @param $y0 (float) Ordinate of center point.
  14737. * @param $r (float) Radius of inscribed circle.
  14738. * @param $nv (integer) Number of vertices.
  14739. * @param $ng (integer) Number of gap (if ($ng % $nv = 1) then is a regular polygon).
  14740. * @param $angle: (float) Angle oriented (anti-clockwise). Default value: 0.
  14741. * @param $draw_circle: (boolean) Draw inscribed circle or not. Default value is false.
  14742. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  14743. * @param $line_style (array) Line style of polygon sides. Array with keys among the following:
  14744. * <ul>
  14745. * <li>all: Line style of all sides. Array like for
  14746. * SetLineStyle().</li>
  14747. * <li>0 to (n - 1): Line style of each side. Array like for SetLineStyle().</li>
  14748. * </ul>
  14749. * If a key is not present or is null, not draws the side. Default value is default line style (empty array).
  14750. * @param $fill_color (array) Fill color. Format: array(red, green, blue). Default value: default color (empty array).
  14751. * @param $circle_style (string) Style of rendering of inscribed circle (if draws). Possible values are:
  14752. * <ul>
  14753. * <li>D or empty string: Draw (default).</li>
  14754. * <li>F: Fill.</li>
  14755. * <li>DF or FD: Draw and fill.</li>
  14756. * <li>CNZ: Clipping mode (using the even-odd rule to determine which regions lie inside the clipping path).</li>
  14757. * <li>CEO: Clipping mode (using the nonzero winding number rule to determine which regions lie inside the clipping path).</li>
  14758. * </ul>
  14759. * @param $circle_outLine_style (array) Line style of inscribed circle (if draws). Array like for SetLineStyle(). Default value: default line style (empty array).
  14760. * @param $circle_fill_color (array) Fill color of inscribed circle (if draws). Format: array(red, green, blue). Default value: default color (empty array).
  14761. * @public
  14762. * @since 2.1.000 (2008-01-08)
  14763. */
  14764. 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()) {
  14765. if ($nv < 2) {
  14766. $nv = 2;
  14767. }
  14768. if ($draw_circle) {
  14769. $this->Circle($x0, $y0, $r, 0, 360, $circle_style, $circle_outLine_style, $circle_fill_color);
  14770. }
  14771. $p2 = array();
  14772. $visited = array();
  14773. for ($i = 0; $i < $nv; ++$i) {
  14774. $a = $angle + ($i * 360 / $nv);
  14775. $a_rad = deg2rad((float) $a);
  14776. $p2[] = $x0 + ($r * sin($a_rad));
  14777. $p2[] = $y0 + ($r * cos($a_rad));
  14778. $visited[] = false;
  14779. }
  14780. $p = array();
  14781. $i = 0;
  14782. do {
  14783. $p[] = $p2[$i * 2];
  14784. $p[] = $p2[($i * 2) + 1];
  14785. $visited[$i] = true;
  14786. $i += $ng;
  14787. $i %= $nv;
  14788. } while (!$visited[$i]);
  14789. $this->Polygon($p, $style, $line_style, $fill_color);
  14790. }
  14791. /**
  14792. * Draws a rounded rectangle.
  14793. * @param $x (float) Abscissa of upper-left corner.
  14794. * @param $y (float) Ordinate of upper-left corner.
  14795. * @param $w (float) Width.
  14796. * @param $h (float) Height.
  14797. * @param $r (float) the radius of the circle used to round off the corners of the rectangle.
  14798. * @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 left, top right, bottom right and bottom left. Default value: all rounded corner ("1111").
  14799. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  14800. * @param $border_style (array) Border style of rectangle. Array like for SetLineStyle(). Default value: default line style (empty array).
  14801. * @param $fill_color (array) Fill color. Format: array(GREY) or array(R,G,B) or array(C,M,Y,K). Default value: default color (empty array).
  14802. * @public
  14803. * @since 2.1.000 (2008-01-08)
  14804. */
  14805. public function RoundedRect($x, $y, $w, $h, $r, $round_corner='1111', $style='', $border_style=array(), $fill_color=array()) {
  14806. $this->RoundedRectXY($x, $y, $w, $h, $r, $r, $round_corner, $style, $border_style, $fill_color);
  14807. }
  14808. /**
  14809. * Draws a rounded rectangle.
  14810. * @param $x (float) Abscissa of upper-left corner.
  14811. * @param $y (float) Ordinate of upper-left corner.
  14812. * @param $w (float) Width.
  14813. * @param $h (float) Height.
  14814. * @param $rx (float) the x-axis radius of the ellipse used to round off the corners of the rectangle.
  14815. * @param $ry (float) the y-axis radius of the ellipse used to round off the corners of the rectangle.
  14816. * @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 left, top right, bottom right and bottom left. Default value: all rounded corner ("1111").
  14817. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  14818. * @param $border_style (array) Border style of rectangle. Array like for SetLineStyle(). Default value: default line style (empty array).
  14819. * @param $fill_color (array) Fill color. Format: array(GREY) or array(R,G,B) or array(C,M,Y,K). Default value: default color (empty array).
  14820. * @public
  14821. * @since 4.9.019 (2010-04-22)
  14822. */
  14823. public function RoundedRectXY($x, $y, $w, $h, $rx, $ry, $round_corner='1111', $style='', $border_style=array(), $fill_color=array()) {
  14824. if (($round_corner == '0000') OR (($rx == $ry) AND ($rx == 0))) {
  14825. // Not rounded
  14826. $this->Rect($x, $y, $w, $h, $style, $border_style, $fill_color);
  14827. return;
  14828. }
  14829. // Rounded
  14830. if (!(false === strpos($style, 'F')) AND isset($fill_color)) {
  14831. $this->SetFillColorArray($fill_color);
  14832. }
  14833. $op = $this->getPathPaintOperator($style);
  14834. if ($op == 'f') {
  14835. $border_style = array();
  14836. }
  14837. if ($border_style) {
  14838. $this->SetLineStyle($border_style);
  14839. }
  14840. $MyArc = 4 / 3 * (sqrt(2) - 1);
  14841. $this->_outPoint($x + $rx, $y);
  14842. $xc = $x + $w - $rx;
  14843. $yc = $y + $ry;
  14844. $this->_outLine($xc, $y);
  14845. if ($round_corner[0]) {
  14846. $this->_outCurve($xc + ($rx * $MyArc), $yc - $ry, $xc + $rx, $yc - ($ry * $MyArc), $xc + $rx, $yc);
  14847. } else {
  14848. $this->_outLine($x + $w, $y);
  14849. }
  14850. $xc = $x + $w - $rx;
  14851. $yc = $y + $h - $ry;
  14852. $this->_outLine($x + $w, $yc);
  14853. if ($round_corner[1]) {
  14854. $this->_outCurve($xc + $rx, $yc + ($ry * $MyArc), $xc + ($rx * $MyArc), $yc + $ry, $xc, $yc + $ry);
  14855. } else {
  14856. $this->_outLine($x + $w, $y + $h);
  14857. }
  14858. $xc = $x + $rx;
  14859. $yc = $y + $h - $ry;
  14860. $this->_outLine($xc, $y + $h);
  14861. if ($round_corner[2]) {
  14862. $this->_outCurve($xc - ($rx * $MyArc), $yc + $ry, $xc - $rx, $yc + ($ry * $MyArc), $xc - $rx, $yc);
  14863. } else {
  14864. $this->_outLine($x, $y + $h);
  14865. }
  14866. $xc = $x + $rx;
  14867. $yc = $y + $ry;
  14868. $this->_outLine($x, $yc);
  14869. if ($round_corner[3]) {
  14870. $this->_outCurve($xc - $rx, $yc - ($ry * $MyArc), $xc - ($rx * $MyArc), $yc - $ry, $xc, $yc - $ry);
  14871. } else {
  14872. $this->_outLine($x, $y);
  14873. $this->_outLine($x + $rx, $y);
  14874. }
  14875. $this->_out($op);
  14876. }
  14877. /**
  14878. * Draws a grahic arrow.
  14879. * @param $x0 (float) Abscissa of first point.
  14880. * @param $y0 (float) Ordinate of first point.
  14881. * @param $x1 (float) Abscissa of second point.
  14882. * @param $y1 (float) Ordinate of second point.
  14883. * @param $head_style (int) (0 = draw only arrowhead arms, 1 = draw closed arrowhead, but no fill, 2 = closed and filled arrowhead, 3 = filled arrowhead)
  14884. * @param $arm_size (float) length of arrowhead arms
  14885. * @param $arm_angle (int) angle between an arm and the shaft
  14886. * @author Piotr Galecki, Nicola Asuni, Andy Meier
  14887. * @since 4.6.018 (2009-07-10)
  14888. */
  14889. public function Arrow($x0, $y0, $x1, $y1, $head_style=0, $arm_size=5, $arm_angle=15) {
  14890. // getting arrow direction angle
  14891. // 0 deg angle is when both arms go along X axis. angle grows clockwise.
  14892. $dir_angle = atan2(($y0 - $y1), ($x0 - $x1));
  14893. if ($dir_angle < 0) {
  14894. $dir_angle += (2 * M_PI);
  14895. }
  14896. $arm_angle = deg2rad($arm_angle);
  14897. $sx1 = $x1;
  14898. $sy1 = $y1;
  14899. if ($head_style > 0) {
  14900. // calculate the stopping point for the arrow shaft
  14901. $sx1 = $x1 + (($arm_size - $this->LineWidth) * cos($dir_angle));
  14902. $sy1 = $y1 + (($arm_size - $this->LineWidth) * sin($dir_angle));
  14903. }
  14904. // main arrow line / shaft
  14905. $this->Line($x0, $y0, $sx1, $sy1);
  14906. // left arrowhead arm tip
  14907. $x2L = $x1 + ($arm_size * cos($dir_angle + $arm_angle));
  14908. $y2L = $y1 + ($arm_size * sin($dir_angle + $arm_angle));
  14909. // right arrowhead arm tip
  14910. $x2R = $x1 + ($arm_size * cos($dir_angle - $arm_angle));
  14911. $y2R = $y1 + ($arm_size * sin($dir_angle - $arm_angle));
  14912. $mode = 'D';
  14913. $style = array();
  14914. switch ($head_style) {
  14915. case 0: {
  14916. // draw only arrowhead arms
  14917. $mode = 'D';
  14918. $style = array(1, 1, 0);
  14919. break;
  14920. }
  14921. case 1: {
  14922. // draw closed arrowhead, but no fill
  14923. $mode = 'D';
  14924. break;
  14925. }
  14926. case 2: {
  14927. // closed and filled arrowhead
  14928. $mode = 'DF';
  14929. break;
  14930. }
  14931. case 3: {
  14932. // filled arrowhead
  14933. $mode = 'F';
  14934. break;
  14935. }
  14936. }
  14937. $this->Polygon(array($x2L, $y2L, $x1, $y1, $x2R, $y2R), $mode, $style, array());
  14938. }
  14939. // END GRAPHIC FUNCTIONS SECTION -----------------------
  14940. // BIDIRECTIONAL TEXT SECTION --------------------------
  14941. /**
  14942. * Reverse the RLT substrings using the Bidirectional Algorithm (http://unicode.org/reports/tr9/).
  14943. * @param $str (string) string to manipulate.
  14944. * @param $setbom (bool) if true set the Byte Order Mark (BOM = 0xFEFF)
  14945. * @param $forcertl (bool) if true forces RTL text direction
  14946. * @return string
  14947. * @protected
  14948. * @author Nicola Asuni
  14949. * @since 2.1.000 (2008-01-08)
  14950. */
  14951. protected function utf8StrRev($str, $setbom=false, $forcertl=false) {
  14952. return $this->utf8StrArrRev($this->UTF8StringToArray($str), $str, $setbom, $forcertl);
  14953. }
  14954. /**
  14955. * Reverse the RLT substrings array using the Bidirectional Algorithm (http://unicode.org/reports/tr9/).
  14956. * @param $arr (array) array of unicode values.
  14957. * @param $str (string) string to manipulate (or empty value).
  14958. * @param $setbom (bool) if true set the Byte Order Mark (BOM = 0xFEFF)
  14959. * @param $forcertl (bool) if true forces RTL text direction
  14960. * @return string
  14961. * @protected
  14962. * @author Nicola Asuni
  14963. * @since 4.9.000 (2010-03-27)
  14964. */
  14965. protected function utf8StrArrRev($arr, $str='', $setbom=false, $forcertl=false) {
  14966. return $this->arrUTF8ToUTF16BE($this->utf8Bidi($arr, $str, $forcertl), $setbom);
  14967. }
  14968. /**
  14969. * Reverse the RLT substrings using the Bidirectional Algorithm (http://unicode.org/reports/tr9/).
  14970. * @param $ta (array) array of characters composing the string.
  14971. * @param $str (string) string to process
  14972. * @param $forcertl (bool) if 'R' forces RTL, if 'L' forces LTR
  14973. * @return array of unicode chars
  14974. * @author Nicola Asuni
  14975. * @protected
  14976. * @since 2.4.000 (2008-03-06)
  14977. */
  14978. protected function utf8Bidi($ta, $str='', $forcertl=false) {
  14979. // paragraph embedding level
  14980. $pel = 0;
  14981. // max level
  14982. $maxlevel = 0;
  14983. if ($this->empty_string($str)) {
  14984. // create string from array
  14985. $str = $this->UTF8ArrSubString($ta);
  14986. }
  14987. // check if string contains arabic text
  14988. if (preg_match($this->unicode->uni_RE_PATTERN_ARABIC, $str)) {
  14989. $arabic = true;
  14990. } else {
  14991. $arabic = false;
  14992. }
  14993. // check if string contains RTL text
  14994. if (!($forcertl OR $arabic OR preg_match($this->unicode->uni_RE_PATTERN_RTL, $str))) {
  14995. return $ta;
  14996. }
  14997. // get number of chars
  14998. $numchars = count($ta);
  14999. if ($forcertl == 'R') {
  15000. $pel = 1;
  15001. } elseif ($forcertl == 'L') {
  15002. $pel = 0;
  15003. } else {
  15004. // P2. In each paragraph, find the first character of type L, AL, or R.
  15005. // P3. If a character is found in P2 and it is of type AL or R, then set the paragraph embedding level to one; otherwise, set it to zero.
  15006. for ($i=0; $i < $numchars; ++$i) {
  15007. $type = $this->unicode->uni_type[$ta[$i]];
  15008. if ($type == 'L') {
  15009. $pel = 0;
  15010. break;
  15011. } elseif (($type == 'AL') OR ($type == 'R')) {
  15012. $pel = 1;
  15013. break;
  15014. }
  15015. }
  15016. }
  15017. // Current Embedding Level
  15018. $cel = $pel;
  15019. // directional override status
  15020. $dos = 'N';
  15021. $remember = array();
  15022. // start-of-level-run
  15023. $sor = $pel % 2 ? 'R' : 'L';
  15024. $eor = $sor;
  15025. // Array of characters data
  15026. $chardata = Array();
  15027. // X1. Begin by setting the current embedding level to the paragraph embedding level. Set the directional override status to neutral. Process each character iteratively, applying rules X2 through X9. Only embedding levels from 0 to 61 are valid in this phase.
  15028. // In the resolution of levels in rules I1 and I2, the maximum embedding level of 62 can be reached.
  15029. for ($i=0; $i < $numchars; ++$i) {
  15030. if ($ta[$i] == $this->unicode->uni_RLE) {
  15031. // X2. With each RLE, compute the least greater odd embedding level.
  15032. // a. If this new level would be valid, then this embedding code is valid. Remember (push) the current embedding level and override status. Reset the current level to this new level, and reset the override status to neutral.
  15033. // b. If the new level would not be valid, then this code is invalid. Do not change the current level or override status.
  15034. $next_level = $cel + ($cel % 2) + 1;
  15035. if ($next_level < 62) {
  15036. $remember[] = array('num' => $this->unicode->uni_RLE, 'cel' => $cel, 'dos' => $dos);
  15037. $cel = $next_level;
  15038. $dos = 'N';
  15039. $sor = $eor;
  15040. $eor = $cel % 2 ? 'R' : 'L';
  15041. }
  15042. } elseif ($ta[$i] == $this->unicode->uni_LRE) {
  15043. // X3. With each LRE, compute the least greater even embedding level.
  15044. // a. If this new level would be valid, then this embedding code is valid. Remember (push) the current embedding level and override status. Reset the current level to this new level, and reset the override status to neutral.
  15045. // b. If the new level would not be valid, then this code is invalid. Do not change the current level or override status.
  15046. $next_level = $cel + 2 - ($cel % 2);
  15047. if ( $next_level < 62 ) {
  15048. $remember[] = array('num' => $this->unicode->uni_LRE, 'cel' => $cel, 'dos' => $dos);
  15049. $cel = $next_level;
  15050. $dos = 'N';
  15051. $sor = $eor;
  15052. $eor = $cel % 2 ? 'R' : 'L';
  15053. }
  15054. } elseif ($ta[$i] == $this->unicode->uni_RLO) {
  15055. // X4. With each RLO, compute the least greater odd embedding level.
  15056. // a. If this new level would be valid, then this embedding code is valid. Remember (push) the current embedding level and override status. Reset the current level to this new level, and reset the override status to right-to-left.
  15057. // b. If the new level would not be valid, then this code is invalid. Do not change the current level or override status.
  15058. $next_level = $cel + ($cel % 2) + 1;
  15059. if ($next_level < 62) {
  15060. $remember[] = array('num' => $this->unicode->uni_RLO, 'cel' => $cel, 'dos' => $dos);
  15061. $cel = $next_level;
  15062. $dos = 'R';
  15063. $sor = $eor;
  15064. $eor = $cel % 2 ? 'R' : 'L';
  15065. }
  15066. } elseif ($ta[$i] == $this->unicode->uni_LRO) {
  15067. // X5. With each LRO, compute the least greater even embedding level.
  15068. // a. If this new level would be valid, then this embedding code is valid. Remember (push) the current embedding level and override status. Reset the current level to this new level, and reset the override status to left-to-right.
  15069. // b. If the new level would not be valid, then this code is invalid. Do not change the current level or override status.
  15070. $next_level = $cel + 2 - ($cel % 2);
  15071. if ( $next_level < 62 ) {
  15072. $remember[] = array('num' => $this->unicode->uni_LRO, 'cel' => $cel, 'dos' => $dos);
  15073. $cel = $next_level;
  15074. $dos = 'L';
  15075. $sor = $eor;
  15076. $eor = $cel % 2 ? 'R' : 'L';
  15077. }
  15078. } elseif ($ta[$i] == $this->unicode->uni_PDF) {
  15079. // X7. With each PDF, determine the matching embedding or override code. If there was a valid matching code, restore (pop) the last remembered (pushed) embedding level and directional override.
  15080. if (count($remember)) {
  15081. $last = count($remember ) - 1;
  15082. if (($remember[$last]['num'] == $this->unicode->uni_RLE) OR
  15083. ($remember[$last]['num'] == $this->unicode->uni_LRE) OR
  15084. ($remember[$last]['num'] == $this->unicode->uni_RLO) OR
  15085. ($remember[$last]['num'] == $this->unicode->uni_LRO)) {
  15086. $match = array_pop($remember);
  15087. $cel = $match['cel'];
  15088. $dos = $match['dos'];
  15089. $sor = $eor;
  15090. $eor = ($cel > $match['cel'] ? $cel : $match['cel']) % 2 ? 'R' : 'L';
  15091. }
  15092. }
  15093. } elseif (($ta[$i] != $this->unicode->uni_RLE) AND
  15094. ($ta[$i] != $this->unicode->uni_LRE) AND
  15095. ($ta[$i] != $this->unicode->uni_RLO) AND
  15096. ($ta[$i] != $this->unicode->uni_LRO) AND
  15097. ($ta[$i] != $this->unicode->uni_PDF)) {
  15098. // X6. For all types besides RLE, LRE, RLO, LRO, and PDF:
  15099. // a. Set the level of the current character to the current embedding level.
  15100. // b. Whenever the directional override status is not neutral, reset the current character type to the directional override status.
  15101. if ($dos != 'N') {
  15102. $chardir = $dos;
  15103. } else {
  15104. if (isset($this->unicode->uni_type[$ta[$i]])) {
  15105. $chardir = $this->unicode->uni_type[$ta[$i]];
  15106. } else {
  15107. $chardir = 'L';
  15108. }
  15109. }
  15110. // stores string characters and other information
  15111. $chardata[] = array('char' => $ta[$i], 'level' => $cel, 'type' => $chardir, 'sor' => $sor, 'eor' => $eor);
  15112. }
  15113. } // end for each char
  15114. // X8. All explicit directional embeddings and overrides are completely terminated at the end of each paragraph. Paragraph separators are not included in the embedding.
  15115. // X9. Remove all RLE, LRE, RLO, LRO, PDF, and BN codes.
  15116. // X10. The remaining rules are applied to each run of characters at the same level. For each run, determine the start-of-level-run (sor) and end-of-level-run (eor) type, either L or R. This depends on the higher of the two levels on either side of the boundary (at the start or end of the paragraph, the level of the 'other' run is the base embedding level). If the higher level is odd, the type is R; otherwise, it is L.
  15117. // 3.3.3 Resolving Weak Types
  15118. // Weak types are now resolved one level run at a time. At level run boundaries where the type of the character on the other side of the boundary is required, the type assigned to sor or eor is used.
  15119. // Nonspacing marks are now resolved based on the previous characters.
  15120. $numchars = count($chardata);
  15121. // W1. Examine each nonspacing mark (NSM) in the level run, and change the type of the NSM to the type of the previous character. If the NSM is at the start of the level run, it will get the type of sor.
  15122. $prevlevel = -1; // track level changes
  15123. $levcount = 0; // counts consecutive chars at the same level
  15124. for ($i=0; $i < $numchars; ++$i) {
  15125. if ($chardata[$i]['type'] == 'NSM') {
  15126. if ($levcount) {
  15127. $chardata[$i]['type'] = $chardata[$i]['sor'];
  15128. } elseif ($i > 0) {
  15129. $chardata[$i]['type'] = $chardata[($i-1)]['type'];
  15130. }
  15131. }
  15132. if ($chardata[$i]['level'] != $prevlevel) {
  15133. $levcount = 0;
  15134. } else {
  15135. ++$levcount;
  15136. }
  15137. $prevlevel = $chardata[$i]['level'];
  15138. }
  15139. // W2. Search backward from each instance of a European number until the first strong type (R, L, AL, or sor) is found. If an AL is found, change the type of the European number to Arabic number.
  15140. $prevlevel = -1;
  15141. $levcount = 0;
  15142. for ($i=0; $i < $numchars; ++$i) {
  15143. if ($chardata[$i]['char'] == 'EN') {
  15144. for ($j=$levcount; $j >= 0; $j--) {
  15145. if ($chardata[$j]['type'] == 'AL') {
  15146. $chardata[$i]['type'] = 'AN';
  15147. } elseif (($chardata[$j]['type'] == 'L') OR ($chardata[$j]['type'] == 'R')) {
  15148. break;
  15149. }
  15150. }
  15151. }
  15152. if ($chardata[$i]['level'] != $prevlevel) {
  15153. $levcount = 0;
  15154. } else {
  15155. ++$levcount;
  15156. }
  15157. $prevlevel = $chardata[$i]['level'];
  15158. }
  15159. // W3. Change all ALs to R.
  15160. for ($i=0; $i < $numchars; ++$i) {
  15161. if ($chardata[$i]['type'] == 'AL') {
  15162. $chardata[$i]['type'] = 'R';
  15163. }
  15164. }
  15165. // W4. A single European separator between two European numbers changes to a European number. A single common separator between two numbers of the same type changes to that type.
  15166. $prevlevel = -1;
  15167. $levcount = 0;
  15168. for ($i=0; $i < $numchars; ++$i) {
  15169. if (($levcount > 0) AND (($i+1) < $numchars) AND ($chardata[($i+1)]['level'] == $prevlevel)) {
  15170. if (($chardata[$i]['type'] == 'ES') AND ($chardata[($i-1)]['type'] == 'EN') AND ($chardata[($i+1)]['type'] == 'EN')) {
  15171. $chardata[$i]['type'] = 'EN';
  15172. } elseif (($chardata[$i]['type'] == 'CS') AND ($chardata[($i-1)]['type'] == 'EN') AND ($chardata[($i+1)]['type'] == 'EN')) {
  15173. $chardata[$i]['type'] = 'EN';
  15174. } elseif (($chardata[$i]['type'] == 'CS') AND ($chardata[($i-1)]['type'] == 'AN') AND ($chardata[($i+1)]['type'] == 'AN')) {
  15175. $chardata[$i]['type'] = 'AN';
  15176. }
  15177. }
  15178. if ($chardata[$i]['level'] != $prevlevel) {
  15179. $levcount = 0;
  15180. } else {
  15181. ++$levcount;
  15182. }
  15183. $prevlevel = $chardata[$i]['level'];
  15184. }
  15185. // W5. A sequence of European terminators adjacent to European numbers changes to all European numbers.
  15186. $prevlevel = -1;
  15187. $levcount = 0;
  15188. for ($i=0; $i < $numchars; ++$i) {
  15189. if ($chardata[$i]['type'] == 'ET') {
  15190. if (($levcount > 0) AND ($chardata[($i-1)]['type'] == 'EN')) {
  15191. $chardata[$i]['type'] = 'EN';
  15192. } else {
  15193. $j = $i+1;
  15194. while (($j < $numchars) AND ($chardata[$j]['level'] == $prevlevel)) {
  15195. if ($chardata[$j]['type'] == 'EN') {
  15196. $chardata[$i]['type'] = 'EN';
  15197. break;
  15198. } elseif ($chardata[$j]['type'] != 'ET') {
  15199. break;
  15200. }
  15201. ++$j;
  15202. }
  15203. }
  15204. }
  15205. if ($chardata[$i]['level'] != $prevlevel) {
  15206. $levcount = 0;
  15207. } else {
  15208. ++$levcount;
  15209. }
  15210. $prevlevel = $chardata[$i]['level'];
  15211. }
  15212. // W6. Otherwise, separators and terminators change to Other Neutral.
  15213. $prevlevel = -1;
  15214. $levcount = 0;
  15215. for ($i=0; $i < $numchars; ++$i) {
  15216. if (($chardata[$i]['type'] == 'ET') OR ($chardata[$i]['type'] == 'ES') OR ($chardata[$i]['type'] == 'CS')) {
  15217. $chardata[$i]['type'] = 'ON';
  15218. }
  15219. if ($chardata[$i]['level'] != $prevlevel) {
  15220. $levcount = 0;
  15221. } else {
  15222. ++$levcount;
  15223. }
  15224. $prevlevel = $chardata[$i]['level'];
  15225. }
  15226. //W7. Search backward from each instance of a European number until the first strong type (R, L, or sor) is found. If an L is found, then change the type of the European number to L.
  15227. $prevlevel = -1;
  15228. $levcount = 0;
  15229. for ($i=0; $i < $numchars; ++$i) {
  15230. if ($chardata[$i]['char'] == 'EN') {
  15231. for ($j=$levcount; $j >= 0; $j--) {
  15232. if ($chardata[$j]['type'] == 'L') {
  15233. $chardata[$i]['type'] = 'L';
  15234. } elseif ($chardata[$j]['type'] == 'R') {
  15235. break;
  15236. }
  15237. }
  15238. }
  15239. if ($chardata[$i]['level'] != $prevlevel) {
  15240. $levcount = 0;
  15241. } else {
  15242. ++$levcount;
  15243. }
  15244. $prevlevel = $chardata[$i]['level'];
  15245. }
  15246. // N1. A sequence of neutrals takes the direction of the surrounding strong text if the text on both sides has the same direction. European and Arabic numbers act as if they were R in terms of their influence on neutrals. Start-of-level-run (sor) and end-of-level-run (eor) are used at level run boundaries.
  15247. $prevlevel = -1;
  15248. $levcount = 0;
  15249. for ($i=0; $i < $numchars; ++$i) {
  15250. if (($levcount > 0) AND (($i+1) < $numchars) AND ($chardata[($i+1)]['level'] == $prevlevel)) {
  15251. if (($chardata[$i]['type'] == 'N') AND ($chardata[($i-1)]['type'] == 'L') AND ($chardata[($i+1)]['type'] == 'L')) {
  15252. $chardata[$i]['type'] = 'L';
  15253. } elseif (($chardata[$i]['type'] == 'N') AND
  15254. (($chardata[($i-1)]['type'] == 'R') OR ($chardata[($i-1)]['type'] == 'EN') OR ($chardata[($i-1)]['type'] == 'AN')) AND
  15255. (($chardata[($i+1)]['type'] == 'R') OR ($chardata[($i+1)]['type'] == 'EN') OR ($chardata[($i+1)]['type'] == 'AN'))) {
  15256. $chardata[$i]['type'] = 'R';
  15257. } elseif ($chardata[$i]['type'] == 'N') {
  15258. // N2. Any remaining neutrals take the embedding direction
  15259. $chardata[$i]['type'] = $chardata[$i]['sor'];
  15260. }
  15261. } elseif (($levcount == 0) AND (($i+1) < $numchars) AND ($chardata[($i+1)]['level'] == $prevlevel)) {
  15262. // first char
  15263. if (($chardata[$i]['type'] == 'N') AND ($chardata[$i]['sor'] == 'L') AND ($chardata[($i+1)]['type'] == 'L')) {
  15264. $chardata[$i]['type'] = 'L';
  15265. } elseif (($chardata[$i]['type'] == 'N') AND
  15266. (($chardata[$i]['sor'] == 'R') OR ($chardata[$i]['sor'] == 'EN') OR ($chardata[$i]['sor'] == 'AN')) AND
  15267. (($chardata[($i+1)]['type'] == 'R') OR ($chardata[($i+1)]['type'] == 'EN') OR ($chardata[($i+1)]['type'] == 'AN'))) {
  15268. $chardata[$i]['type'] = 'R';
  15269. } elseif ($chardata[$i]['type'] == 'N') {
  15270. // N2. Any remaining neutrals take the embedding direction
  15271. $chardata[$i]['type'] = $chardata[$i]['sor'];
  15272. }
  15273. } elseif (($levcount > 0) AND ((($i+1) == $numchars) OR (($i+1) < $numchars) AND ($chardata[($i+1)]['level'] != $prevlevel))) {
  15274. //last char
  15275. if (($chardata[$i]['type'] == 'N') AND ($chardata[($i-1)]['type'] == 'L') AND ($chardata[$i]['eor'] == 'L')) {
  15276. $chardata[$i]['type'] = 'L';
  15277. } elseif (($chardata[$i]['type'] == 'N') AND
  15278. (($chardata[($i-1)]['type'] == 'R') OR ($chardata[($i-1)]['type'] == 'EN') OR ($chardata[($i-1)]['type'] == 'AN')) AND
  15279. (($chardata[$i]['eor'] == 'R') OR ($chardata[$i]['eor'] == 'EN') OR ($chardata[$i]['eor'] == 'AN'))) {
  15280. $chardata[$i]['type'] = 'R';
  15281. } elseif ($chardata[$i]['type'] == 'N') {
  15282. // N2. Any remaining neutrals take the embedding direction
  15283. $chardata[$i]['type'] = $chardata[$i]['sor'];
  15284. }
  15285. } elseif ($chardata[$i]['type'] == 'N') {
  15286. // N2. Any remaining neutrals take the embedding direction
  15287. $chardata[$i]['type'] = $chardata[$i]['sor'];
  15288. }
  15289. if ($chardata[$i]['level'] != $prevlevel) {
  15290. $levcount = 0;
  15291. } else {
  15292. ++$levcount;
  15293. }
  15294. $prevlevel = $chardata[$i]['level'];
  15295. }
  15296. // I1. For all characters with an even (left-to-right) embedding direction, those of type R go up one level and those of type AN or EN go up two levels.
  15297. // I2. For all characters with an odd (right-to-left) embedding direction, those of type L, EN or AN go up one level.
  15298. for ($i=0; $i < $numchars; ++$i) {
  15299. $odd = $chardata[$i]['level'] % 2;
  15300. if ($odd) {
  15301. if (($chardata[$i]['type'] == 'L') OR ($chardata[$i]['type'] == 'AN') OR ($chardata[$i]['type'] == 'EN')) {
  15302. $chardata[$i]['level'] += 1;
  15303. }
  15304. } else {
  15305. if ($chardata[$i]['type'] == 'R') {
  15306. $chardata[$i]['level'] += 1;
  15307. } elseif (($chardata[$i]['type'] == 'AN') OR ($chardata[$i]['type'] == 'EN')) {
  15308. $chardata[$i]['level'] += 2;
  15309. }
  15310. }
  15311. $maxlevel = max($chardata[$i]['level'],$maxlevel);
  15312. }
  15313. // L1. On each line, reset the embedding level of the following characters to the paragraph embedding level:
  15314. // 1. Segment separators,
  15315. // 2. Paragraph separators,
  15316. // 3. Any sequence of whitespace characters preceding a segment separator or paragraph separator, and
  15317. // 4. Any sequence of white space characters at the end of the line.
  15318. for ($i=0; $i < $numchars; ++$i) {
  15319. if (($chardata[$i]['type'] == 'B') OR ($chardata[$i]['type'] == 'S')) {
  15320. $chardata[$i]['level'] = $pel;
  15321. } elseif ($chardata[$i]['type'] == 'WS') {
  15322. $j = $i+1;
  15323. while ($j < $numchars) {
  15324. if ((($chardata[$j]['type'] == 'B') OR ($chardata[$j]['type'] == 'S')) OR
  15325. (($j == ($numchars-1)) AND ($chardata[$j]['type'] == 'WS'))) {
  15326. $chardata[$i]['level'] = $pel;
  15327. break;
  15328. } elseif ($chardata[$j]['type'] != 'WS') {
  15329. break;
  15330. }
  15331. ++$j;
  15332. }
  15333. }
  15334. }
  15335. // Arabic Shaping
  15336. // Cursively connected scripts, such as Arabic or Syriac, require the selection of positional character shapes that depend on adjacent characters. Shaping is logically applied after the Bidirectional Algorithm is used and is limited to characters within the same directional run.
  15337. if ($arabic) {
  15338. $endedletter = array(1569,1570,1571,1572,1573,1575,1577,1583,1584,1585,1586,1608,1688);
  15339. $alfletter = array(1570,1571,1573,1575);
  15340. $chardata2 = $chardata;
  15341. $laaletter = false;
  15342. $charAL = array();
  15343. $x = 0;
  15344. for ($i=0; $i < $numchars; ++$i) {
  15345. if (($this->unicode->uni_type[$chardata[$i]['char']] == 'AL') OR ($chardata[$i]['char'] == 32) OR ($chardata[$i]['char'] == 8204)) {
  15346. $charAL[$x] = $chardata[$i];
  15347. $charAL[$x]['i'] = $i;
  15348. $chardata[$i]['x'] = $x;
  15349. ++$x;
  15350. }
  15351. }
  15352. $numAL = $x;
  15353. for ($i=0; $i < $numchars; ++$i) {
  15354. $thischar = $chardata[$i];
  15355. if ($i > 0) {
  15356. $prevchar = $chardata[($i-1)];
  15357. } else {
  15358. $prevchar = false;
  15359. }
  15360. if (($i+1) < $numchars) {
  15361. $nextchar = $chardata[($i+1)];
  15362. } else {
  15363. $nextchar = false;
  15364. }
  15365. if ($this->unicode->uni_type[$thischar['char']] == 'AL') {
  15366. $x = $thischar['x'];
  15367. if ($x > 0) {
  15368. $prevchar = $charAL[($x-1)];
  15369. } else {
  15370. $prevchar = false;
  15371. }
  15372. if (($x+1) < $numAL) {
  15373. $nextchar = $charAL[($x+1)];
  15374. } else {
  15375. $nextchar = false;
  15376. }
  15377. // if laa letter
  15378. if (($prevchar !== false) AND ($prevchar['char'] == 1604) AND (in_array($thischar['char'], $alfletter))) {
  15379. $arabicarr = $this->unicode->uni_laa_array;
  15380. $laaletter = true;
  15381. if ($x > 1) {
  15382. $prevchar = $charAL[($x-2)];
  15383. } else {
  15384. $prevchar = false;
  15385. }
  15386. } else {
  15387. $arabicarr = $this->unicode->uni_arabicsubst;
  15388. $laaletter = false;
  15389. }
  15390. if (($prevchar !== false) AND ($nextchar !== false) AND
  15391. (($this->unicode->uni_type[$prevchar['char']] == 'AL') OR ($this->unicode->uni_type[$prevchar['char']] == 'NSM')) AND
  15392. (($this->unicode->uni_type[$nextchar['char']] == 'AL') OR ($this->unicode->uni_type[$nextchar['char']] == 'NSM')) AND
  15393. ($prevchar['type'] == $thischar['type']) AND
  15394. ($nextchar['type'] == $thischar['type']) AND
  15395. ($nextchar['char'] != 1567)) {
  15396. if (in_array($prevchar['char'], $endedletter)) {
  15397. if (isset($arabicarr[$thischar['char']][2])) {
  15398. // initial
  15399. $chardata2[$i]['char'] = $arabicarr[$thischar['char']][2];
  15400. }
  15401. } else {
  15402. if (isset($arabicarr[$thischar['char']][3])) {
  15403. // medial
  15404. $chardata2[$i]['char'] = $arabicarr[$thischar['char']][3];
  15405. }
  15406. }
  15407. } elseif (($nextchar !== false) AND
  15408. (($this->unicode->uni_type[$nextchar['char']] == 'AL') OR ($this->unicode->uni_type[$nextchar['char']] == 'NSM')) AND
  15409. ($nextchar['type'] == $thischar['type']) AND
  15410. ($nextchar['char'] != 1567)) {
  15411. if (isset($arabicarr[$chardata[$i]['char']][2])) {
  15412. // initial
  15413. $chardata2[$i]['char'] = $arabicarr[$thischar['char']][2];
  15414. }
  15415. } elseif ((($prevchar !== false) AND
  15416. (($this->unicode->uni_type[$prevchar['char']] == 'AL') OR ($this->unicode->uni_type[$prevchar['char']] == 'NSM')) AND
  15417. ($prevchar['type'] == $thischar['type'])) OR
  15418. (($nextchar !== false) AND ($nextchar['char'] == 1567))) {
  15419. // final
  15420. if (($i > 1) AND ($thischar['char'] == 1607) AND
  15421. ($chardata[$i-1]['char'] == 1604) AND
  15422. ($chardata[$i-2]['char'] == 1604)) {
  15423. //Allah Word
  15424. // mark characters to delete with false
  15425. $chardata2[$i-2]['char'] = false;
  15426. $chardata2[$i-1]['char'] = false;
  15427. $chardata2[$i]['char'] = 65010;
  15428. } else {
  15429. if (($prevchar !== false) AND in_array($prevchar['char'], $endedletter)) {
  15430. if (isset($arabicarr[$thischar['char']][0])) {
  15431. // isolated
  15432. $chardata2[$i]['char'] = $arabicarr[$thischar['char']][0];
  15433. }
  15434. } else {
  15435. if (isset($arabicarr[$thischar['char']][1])) {
  15436. // final
  15437. $chardata2[$i]['char'] = $arabicarr[$thischar['char']][1];
  15438. }
  15439. }
  15440. }
  15441. } elseif (isset($arabicarr[$thischar['char']][0])) {
  15442. // isolated
  15443. $chardata2[$i]['char'] = $arabicarr[$thischar['char']][0];
  15444. }
  15445. // if laa letter
  15446. if ($laaletter) {
  15447. // mark characters to delete with false
  15448. $chardata2[($charAL[($x-1)]['i'])]['char'] = false;
  15449. }
  15450. } // end if AL (Arabic Letter)
  15451. } // end for each char
  15452. /*
  15453. * Combining characters that can occur with Arabic Shadda (0651 HEX, 1617 DEC) are replaced.
  15454. * Putting the combining mark and shadda in the same glyph allows us to avoid the two marks overlapping each other in an illegible manner.
  15455. */
  15456. for ($i = 0; $i < ($numchars-1); ++$i) {
  15457. if (($chardata2[$i]['char'] == 1617) AND (isset($this->unicode->uni_diacritics[($chardata2[$i+1]['char'])]))) {
  15458. // check if the subtitution font is defined on current font
  15459. if (isset($this->CurrentFont['cw'][($this->unicode->uni_diacritics[($chardata2[$i+1]['char'])])])) {
  15460. $chardata2[$i]['char'] = false;
  15461. $chardata2[$i+1]['char'] = $this->unicode->uni_diacritics[($chardata2[$i+1]['char'])];
  15462. }
  15463. }
  15464. }
  15465. // remove marked characters
  15466. foreach ($chardata2 as $key => $value) {
  15467. if ($value['char'] === false) {
  15468. unset($chardata2[$key]);
  15469. }
  15470. }
  15471. $chardata = array_values($chardata2);
  15472. $numchars = count($chardata);
  15473. unset($chardata2);
  15474. unset($arabicarr);
  15475. unset($laaletter);
  15476. unset($charAL);
  15477. }
  15478. // L2. From the highest level found in the text to the lowest odd level on each line, including intermediate levels not actually present in the text, reverse any contiguous sequence of characters that are at that level or higher.
  15479. for ($j=$maxlevel; $j > 0; $j--) {
  15480. $ordarray = Array();
  15481. $revarr = Array();
  15482. $onlevel = false;
  15483. for ($i=0; $i < $numchars; ++$i) {
  15484. if ($chardata[$i]['level'] >= $j) {
  15485. $onlevel = true;
  15486. if (isset($this->unicode->uni_mirror[$chardata[$i]['char']])) {
  15487. // L4. A character is depicted by a mirrored glyph if and only if (a) the resolved directionality of that character is R, and (b) the Bidi_Mirrored property value of that character is true.
  15488. $chardata[$i]['char'] = $this->unicode->uni_mirror[$chardata[$i]['char']];
  15489. }
  15490. $revarr[] = $chardata[$i];
  15491. } else {
  15492. if ($onlevel) {
  15493. $revarr = array_reverse($revarr);
  15494. $ordarray = array_merge($ordarray, $revarr);
  15495. $revarr = Array();
  15496. $onlevel = false;
  15497. }
  15498. $ordarray[] = $chardata[$i];
  15499. }
  15500. }
  15501. if ($onlevel) {
  15502. $revarr = array_reverse($revarr);
  15503. $ordarray = array_merge($ordarray, $revarr);
  15504. }
  15505. $chardata = $ordarray;
  15506. }
  15507. $ordarray = array();
  15508. for ($i=0; $i < $numchars; ++$i) {
  15509. $ordarray[] = $chardata[$i]['char'];
  15510. // store char values for subsetting
  15511. $this->CurrentFont['subsetchars'][$chardata[$i]['char']] = true;
  15512. }
  15513. // update font subsetchars
  15514. $this->setFontSubBuffer($this->CurrentFont['fontkey'], 'subsetchars', $this->CurrentFont['subsetchars']);
  15515. return $ordarray;
  15516. }
  15517. // END OF BIDIRECTIONAL TEXT SECTION -------------------
  15518. /**
  15519. * Encode a name object.
  15520. * @param $name (string) Name object to encode.
  15521. * @return (string) Encoded name object.
  15522. * @protected
  15523. * @author Nicola Asuni
  15524. * @since 5.9.097 (2011-06-23)
  15525. */
  15526. protected function encodeNameObject($name) {
  15527. $escname = '';
  15528. $length = strlen($name);
  15529. for ($i = 0; $i < $length; ++$i) {
  15530. $chr = $name[$i];
  15531. if (preg_match('/[0-9a-zA-Z]/', $chr) == 1) {
  15532. $escname .= $chr;
  15533. } else {
  15534. $escname .= sprintf('#%02X', ord($chr));
  15535. }
  15536. }
  15537. return $escname;
  15538. }
  15539. /**
  15540. * Add a Named Destination.
  15541. * NOTE: destination names are unique, so only last entry will be saved.
  15542. * @param $name (string) Destination name.
  15543. * @param $y (float) Y position in user units of the destiantion on the selected page (default = -1 = current position; 0 = page start;).
  15544. * @param $page (int) Target page number (leave empty for current page).
  15545. * @return (string) Stripped named destination identifier or false in case of error.
  15546. * @public
  15547. * @author Christian Deligant, Nicola Asuni
  15548. * @since 5.9.097 (2011-06-23)
  15549. */
  15550. public function setDestination($name, $y=-1, $page='') {
  15551. // remove unsupported characters
  15552. $name = $this->encodeNameObject($name);
  15553. if ($this->empty_string($name)) {
  15554. return false;
  15555. }
  15556. if ($y == -1) {
  15557. $y = $this->GetY();
  15558. }
  15559. if (empty($page)) {
  15560. $page = $this->PageNo();
  15561. if (empty($page)) {
  15562. return;
  15563. }
  15564. }
  15565. $this->dests[$name] = array('y' => $y, 'p' => $page);
  15566. return $name;
  15567. }
  15568. /**
  15569. * Return the Named Destination array.
  15570. * @return (array) Named Destination array.
  15571. * @public
  15572. * @author Nicola Asuni
  15573. * @since 5.9.097 (2011-06-23)
  15574. */
  15575. public function getDestination() {
  15576. return $this->dests;
  15577. }
  15578. /**
  15579. * Create a javascript PDF string.
  15580. * @protected
  15581. * @author Johannes Güntert, Nicola Asuni
  15582. * @since 5.9.098 (2011-06-23)
  15583. */
  15584. protected function _putdests() {
  15585. if (empty($this->dests)) {
  15586. return;
  15587. }
  15588. $this->n_dests = $this->_newobj();
  15589. $out = ' <<';
  15590. foreach($this->dests as $name => $o) {
  15591. $out .= ' /'.$name.' '.sprintf('[%u 0 R /XYZ 0 %.2F null]', $this->page_obj_id[($o['p'])], ($this->pagedim[$o['p']]['h'] - ($o['y'] * $this->k)));
  15592. }
  15593. $out .= ' >>';
  15594. $out .= "\n".'endobj';
  15595. $this->_out($out);
  15596. }
  15597. /**
  15598. * Adds a bookmark - alias for Bookmark().
  15599. * @param $txt (string) Bookmark description.
  15600. * @param $level (int) Bookmark level (minimum value is 0).
  15601. * @param $y (float) Y position in user units of the bookmark on the selected page (default = -1 = current position; 0 = page start;).
  15602. * @param $page (int) Target page number (leave empty for current page).
  15603. * @param $style (string) Font style: B = Bold, I = Italic, BI = Bold + Italic.
  15604. * @param $color (array) RGB color array (values from 0 to 255).
  15605. * @public
  15606. */
  15607. public function setBookmark($txt, $level=0, $y=-1, $page='', $style='', $color=array(0,0,0)) {
  15608. $this->Bookmark($txt, $level, $y, $page, $style, $color);
  15609. }
  15610. /**
  15611. * Adds a bookmark.
  15612. * @param $txt (string) Bookmark description.
  15613. * @param $level (int) Bookmark level (minimum value is 0).
  15614. * @param $y (float) Y position in user units of the bookmark on the selected page (default = -1 = current position; 0 = page start;).
  15615. * @param $page (int) Target page number (leave empty for current page).
  15616. * @param $style (string) Font style: B = Bold, I = Italic, BI = Bold + Italic.
  15617. * @param $color (array) RGB color array (values from 0 to 255).
  15618. * @public
  15619. * @author Olivier Plathey, Nicola Asuni
  15620. * @since 2.1.002 (2008-02-12)
  15621. */
  15622. public function Bookmark($txt, $level=0, $y=-1, $page='', $style='', $color=array(0,0,0)) {
  15623. if ($level < 0) {
  15624. $level = 0;
  15625. }
  15626. if (isset($this->outlines[0])) {
  15627. $lastoutline = end($this->outlines);
  15628. $maxlevel = $lastoutline['l'] + 1;
  15629. } else {
  15630. $maxlevel = 0;
  15631. }
  15632. if ($level > $maxlevel) {
  15633. $level = $maxlevel;
  15634. }
  15635. if ($y == -1) {
  15636. $y = $this->GetY();
  15637. }
  15638. if (empty($page)) {
  15639. $page = $this->PageNo();
  15640. if (empty($page)) {
  15641. return;
  15642. }
  15643. }
  15644. $this->outlines[] = array('t' => $txt, 'l' => $level, 'y' => $y, 'p' => $page, 's' => strtoupper($style), 'c' => $color);
  15645. }
  15646. /**
  15647. * Sort bookmarks for page and key.
  15648. * @protected
  15649. * @since 5.9.119 (2011-09-19)
  15650. */
  15651. protected function sortBookmarks() {
  15652. // get sorting columns
  15653. $outline_p = array();
  15654. $outline_y = array();
  15655. foreach ($this->outlines as $key => $row) {
  15656. $outline_p[$key] = $row['p'];
  15657. $outline_k[$key] = $key;
  15658. }
  15659. // sort outlines by page and original position
  15660. array_multisort($outline_p, SORT_NUMERIC, SORT_ASC, $outline_k, SORT_NUMERIC, SORT_ASC, $this->outlines);
  15661. }
  15662. /**
  15663. * Create a bookmark PDF string.
  15664. * @protected
  15665. * @author Olivier Plathey, Nicola Asuni
  15666. * @since 2.1.002 (2008-02-12)
  15667. */
  15668. protected function _putbookmarks() {
  15669. $nb = count($this->outlines);
  15670. if ($nb == 0) {
  15671. return;
  15672. }
  15673. // sort bookmarks
  15674. $this->sortBookmarks();
  15675. $lru = array();
  15676. $level = 0;
  15677. foreach ($this->outlines as $i => $o) {
  15678. if ($o['l'] > 0) {
  15679. $parent = $lru[($o['l'] - 1)];
  15680. //Set parent and last pointers
  15681. $this->outlines[$i]['parent'] = $parent;
  15682. $this->outlines[$parent]['last'] = $i;
  15683. if ($o['l'] > $level) {
  15684. //Level increasing: set first pointer
  15685. $this->outlines[$parent]['first'] = $i;
  15686. }
  15687. } else {
  15688. $this->outlines[$i]['parent'] = $nb;
  15689. }
  15690. if (($o['l'] <= $level) AND ($i > 0)) {
  15691. //Set prev and next pointers
  15692. $prev = $lru[$o['l']];
  15693. $this->outlines[$prev]['next'] = $i;
  15694. $this->outlines[$i]['prev'] = $prev;
  15695. }
  15696. $lru[$o['l']] = $i;
  15697. $level = $o['l'];
  15698. }
  15699. //Outline items
  15700. $n = $this->n + 1;
  15701. $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';
  15702. foreach ($this->outlines as $i => $o) {
  15703. $oid = $this->_newobj();
  15704. // covert HTML title to string
  15705. $title = preg_replace($nltags, "\n", $o['t']);
  15706. $title = preg_replace("/[\r]+/si", '', $title);
  15707. $title = preg_replace("/[\n]+/si", "\n", $title);
  15708. $title = strip_tags($title);
  15709. $title = $this->stringTrim($title);
  15710. $out = '<</Title '.$this->_textstring($title, $oid);
  15711. $out .= ' /Parent '.($n + $o['parent']).' 0 R';
  15712. if (isset($o['prev'])) {
  15713. $out .= ' /Prev '.($n + $o['prev']).' 0 R';
  15714. }
  15715. if (isset($o['next'])) {
  15716. $out .= ' /Next '.($n + $o['next']).' 0 R';
  15717. }
  15718. if (isset($o['first'])) {
  15719. $out .= ' /First '.($n + $o['first']).' 0 R';
  15720. }
  15721. if (isset($o['last'])) {
  15722. $out .= ' /Last '.($n + $o['last']).' 0 R';
  15723. }
  15724. if (isset($this->page_obj_id[($o['p'])])) {
  15725. $out .= ' '.sprintf('/Dest [%u 0 R /XYZ 0 %.2F null]', $this->page_obj_id[($o['p'])], ($this->pagedim[$o['p']]['h'] - ($o['y'] * $this->k)));
  15726. }
  15727. // set font style
  15728. $style = 0;
  15729. if (!empty($o['s'])) {
  15730. // bold
  15731. if (strpos($o['s'], 'B') !== false) {
  15732. $style |= 2;
  15733. }
  15734. // oblique
  15735. if (strpos($o['s'], 'I') !== false) {
  15736. $style |= 1;
  15737. }
  15738. }
  15739. $out .= sprintf(' /F %d', $style);
  15740. // set bookmark color
  15741. if (isset($o['c']) AND is_array($o['c']) AND (count($o['c']) == 3)) {
  15742. $color = array_values($o['c']);
  15743. $out .= sprintf(' /C [%.3F %.3F %.3F]', ($color[0] / 255), ($color[1] / 255), ($color[2] / 255));
  15744. } else {
  15745. // black
  15746. $out .= ' /C [0.0 0.0 0.0]';
  15747. }
  15748. $out .= ' /Count 0'; // normally closed item
  15749. $out .= ' >>';
  15750. $out .= "\n".'endobj';
  15751. $this->_out($out);
  15752. }
  15753. //Outline root
  15754. $this->OutlineRoot = $this->_newobj();
  15755. $this->_out('<< /Type /Outlines /First '.$n.' 0 R /Last '.($n + $lru[0]).' 0 R >>'."\n".'endobj');
  15756. }
  15757. // --- JAVASCRIPT ------------------------------------------------------
  15758. /**
  15759. * Adds a javascript
  15760. * @param $script (string) Javascript code
  15761. * @public
  15762. * @author Johannes Güntert, Nicola Asuni
  15763. * @since 2.1.002 (2008-02-12)
  15764. */
  15765. public function IncludeJS($script) {
  15766. $this->javascript .= $script;
  15767. }
  15768. /**
  15769. * Adds a javascript object and return object ID
  15770. * @param $script (string) Javascript code
  15771. * @param $onload (boolean) if true executes this object when opening the document
  15772. * @return int internal object ID
  15773. * @public
  15774. * @author Nicola Asuni
  15775. * @since 4.8.000 (2009-09-07)
  15776. */
  15777. public function addJavascriptObject($script, $onload=false) {
  15778. if ($this->pdfa_mode) {
  15779. // javascript is not allowed in PDF/A mode
  15780. return false;
  15781. }
  15782. ++$this->n;
  15783. $this->js_objects[$this->n] = array('n' => $this->n, 'js' => $script, 'onload' => $onload);
  15784. return $this->n;
  15785. }
  15786. /**
  15787. * Create a javascript PDF string.
  15788. * @protected
  15789. * @author Johannes Güntert, Nicola Asuni
  15790. * @since 2.1.002 (2008-02-12)
  15791. */
  15792. protected function _putjavascript() {
  15793. if ($this->pdfa_mode OR (empty($this->javascript) AND empty($this->js_objects))) {
  15794. return;
  15795. }
  15796. if (strpos($this->javascript, 'this.addField') > 0) {
  15797. if (!$this->ur['enabled']) {
  15798. //$this->setUserRights();
  15799. }
  15800. // the following two lines are used to avoid form fields duplication after saving
  15801. // The addField method only works when releasing user rights (UR3)
  15802. $jsa = sprintf("ftcpdfdocsaved=this.addField('%s','%s',%d,[%.2F,%.2F,%.2F,%.2F]);", 'tcpdfdocsaved', 'text', 0, 0, 1, 0, 1);
  15803. $jsb = "getField('tcpdfdocsaved').value='saved';";
  15804. $this->javascript = $jsa."\n".$this->javascript."\n".$jsb;
  15805. }
  15806. $this->n_js = $this->_newobj();
  15807. $out = ' << /Names [';
  15808. if (!empty($this->javascript)) {
  15809. $out .= ' (EmbeddedJS) '.($this->n + 1).' 0 R';
  15810. }
  15811. if (!empty($this->js_objects)) {
  15812. foreach ($this->js_objects as $key => $val) {
  15813. if ($val['onload']) {
  15814. $out .= ' (JS'.$key.') '.$key.' 0 R';
  15815. }
  15816. }
  15817. }
  15818. $out .= ' ] >>';
  15819. $out .= "\n".'endobj';
  15820. $this->_out($out);
  15821. // default Javascript object
  15822. if (!empty($this->javascript)) {
  15823. $obj_id = $this->_newobj();
  15824. $out = '<< /S /JavaScript';
  15825. $out .= ' /JS '.$this->_textstring($this->javascript, $obj_id);
  15826. $out .= ' >>';
  15827. $out .= "\n".'endobj';
  15828. $this->_out($out);
  15829. }
  15830. // additional Javascript objects
  15831. if (!empty($this->js_objects)) {
  15832. foreach ($this->js_objects as $key => $val) {
  15833. $out = $this->_getobj($key)."\n".' << /S /JavaScript /JS '.$this->_textstring($val['js'], $key).' >>'."\n".'endobj';
  15834. $this->_out($out);
  15835. }
  15836. }
  15837. }
  15838. /**
  15839. * Convert color to javascript color.
  15840. * @param $color (string) color name or "#RRGGBB"
  15841. * @protected
  15842. * @author Denis Van Nuffelen, Nicola Asuni
  15843. * @since 2.1.002 (2008-02-12)
  15844. */
  15845. protected function _JScolor($color) {
  15846. static $aColors = array('transparent', 'black', 'white', 'red', 'green', 'blue', 'cyan', 'magenta', 'yellow', 'dkGray', 'gray', 'ltGray');
  15847. if (substr($color,0,1) == '#') {
  15848. return sprintf("['RGB',%.3F,%.3F,%.3F]", hexdec(substr($color,1,2))/255, hexdec(substr($color,3,2))/255, hexdec(substr($color,5,2))/255);
  15849. }
  15850. if (!in_array($color,$aColors)) {
  15851. $this->Error('Invalid color: '.$color);
  15852. }
  15853. return 'color.'.$color;
  15854. }
  15855. /**
  15856. * Adds a javascript form field.
  15857. * @param $type (string) field type
  15858. * @param $name (string) field name
  15859. * @param $x (int) horizontal position
  15860. * @param $y (int) vertical position
  15861. * @param $w (int) width
  15862. * @param $h (int) height
  15863. * @param $prop (array) javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  15864. * @protected
  15865. * @author Denis Van Nuffelen, Nicola Asuni
  15866. * @since 2.1.002 (2008-02-12)
  15867. */
  15868. protected function _addfield($type, $name, $x, $y, $w, $h, $prop) {
  15869. if ($this->rtl) {
  15870. $x = $x - $w;
  15871. }
  15872. // the followind avoid fields duplication after saving the document
  15873. $this->javascript .= "if (getField('tcpdfdocsaved').value != 'saved') {";
  15874. $k = $this->k;
  15875. $this->javascript .= sprintf("f".$name."=this.addField('%s','%s',%u,[%.2F,%.2F,%.2F,%.2F]);", $name, $type, $this->PageNo()-1, $x*$k, ($this->h-$y)*$k+1, ($x+$w)*$k, ($this->h-$y-$h)*$k+1)."\n";
  15876. $this->javascript .= 'f'.$name.'.textSize='.$this->FontSizePt.";\n";
  15877. while (list($key, $val) = each($prop)) {
  15878. if (strcmp(substr($key, -5), 'Color') == 0) {
  15879. $val = $this->_JScolor($val);
  15880. } else {
  15881. $val = "'".$val."'";
  15882. }
  15883. $this->javascript .= 'f'.$name.'.'.$key.'='.$val.";\n";
  15884. }
  15885. if ($this->rtl) {
  15886. $this->x -= $w;
  15887. } else {
  15888. $this->x += $w;
  15889. }
  15890. $this->javascript .= '}';
  15891. }
  15892. // --- FORM FIELDS -----------------------------------------------------
  15893. /**
  15894. * Convert JavaScript form fields properties array to Annotation Properties array.
  15895. * @param $prop (array) javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  15896. * @return array of annotation properties
  15897. * @protected
  15898. * @author Nicola Asuni
  15899. * @since 4.8.000 (2009-09-06)
  15900. */
  15901. protected function getAnnotOptFromJSProp($prop) {
  15902. if (isset($prop['aopt']) AND is_array($prop['aopt'])) {
  15903. // the annotation options area lready defined
  15904. return $prop['aopt'];
  15905. }
  15906. $opt = array(); // value to be returned
  15907. // alignment: Controls how the text is laid out within the text field.
  15908. if (isset($prop['alignment'])) {
  15909. switch ($prop['alignment']) {
  15910. case 'left': {
  15911. $opt['q'] = 0;
  15912. break;
  15913. }
  15914. case 'center': {
  15915. $opt['q'] = 1;
  15916. break;
  15917. }
  15918. case 'right': {
  15919. $opt['q'] = 2;
  15920. break;
  15921. }
  15922. default: {
  15923. $opt['q'] = ($this->rtl)?2:0;
  15924. break;
  15925. }
  15926. }
  15927. }
  15928. // lineWidth: Specifies the thickness of the border when stroking the perimeter of a field's rectangle.
  15929. if (isset($prop['lineWidth'])) {
  15930. $linewidth = intval($prop['lineWidth']);
  15931. } else {
  15932. $linewidth = 1;
  15933. }
  15934. // borderStyle: The border style for a field.
  15935. if (isset($prop['borderStyle'])) {
  15936. switch ($prop['borderStyle']) {
  15937. case 'border.d':
  15938. case 'dashed': {
  15939. $opt['border'] = array(0, 0, $linewidth, array(3, 2));
  15940. $opt['bs'] = array('w'=>$linewidth, 's'=>'D', 'd'=>array(3, 2));
  15941. break;
  15942. }
  15943. case 'border.b':
  15944. case 'beveled': {
  15945. $opt['border'] = array(0, 0, $linewidth);
  15946. $opt['bs'] = array('w'=>$linewidth, 's'=>'B');
  15947. break;
  15948. }
  15949. case 'border.i':
  15950. case 'inset': {
  15951. $opt['border'] = array(0, 0, $linewidth);
  15952. $opt['bs'] = array('w'=>$linewidth, 's'=>'I');
  15953. break;
  15954. }
  15955. case 'border.u':
  15956. case 'underline': {
  15957. $opt['border'] = array(0, 0, $linewidth);
  15958. $opt['bs'] = array('w'=>$linewidth, 's'=>'U');
  15959. break;
  15960. }
  15961. case 'border.s':
  15962. case 'solid': {
  15963. $opt['border'] = array(0, 0, $linewidth);
  15964. $opt['bs'] = array('w'=>$linewidth, 's'=>'S');
  15965. break;
  15966. }
  15967. default: {
  15968. break;
  15969. }
  15970. }
  15971. }
  15972. if (isset($prop['border']) AND is_array($prop['border'])) {
  15973. $opt['border'] = $prop['border'];
  15974. }
  15975. if (!isset($opt['mk'])) {
  15976. $opt['mk'] = array();
  15977. }
  15978. if (!isset($opt['mk']['if'])) {
  15979. $opt['mk']['if'] = array();
  15980. }
  15981. $opt['mk']['if']['a'] = array(0.5, 0.5);
  15982. // buttonAlignX: Controls how space is distributed from the left of the button face with respect to the icon.
  15983. if (isset($prop['buttonAlignX'])) {
  15984. $opt['mk']['if']['a'][0] = $prop['buttonAlignX'];
  15985. }
  15986. // buttonAlignY: Controls how unused space is distributed from the bottom of the button face with respect to the icon.
  15987. if (isset($prop['buttonAlignY'])) {
  15988. $opt['mk']['if']['a'][1] = $prop['buttonAlignY'];
  15989. }
  15990. // buttonFitBounds: If true, the extent to which the icon may be scaled is set to the bounds of the button field.
  15991. if (isset($prop['buttonFitBounds']) AND ($prop['buttonFitBounds'] == 'true')) {
  15992. $opt['mk']['if']['fb'] = true;
  15993. }
  15994. // buttonScaleHow: Controls how the icon is scaled (if necessary) to fit inside the button face.
  15995. if (isset($prop['buttonScaleHow'])) {
  15996. switch ($prop['buttonScaleHow']) {
  15997. case 'scaleHow.proportional': {
  15998. $opt['mk']['if']['s'] = 'P';
  15999. break;
  16000. }
  16001. case 'scaleHow.anamorphic': {
  16002. $opt['mk']['if']['s'] = 'A';
  16003. break;
  16004. }
  16005. }
  16006. }
  16007. // buttonScaleWhen: Controls when an icon is scaled to fit inside the button face.
  16008. if (isset($prop['buttonScaleWhen'])) {
  16009. switch ($prop['buttonScaleWhen']) {
  16010. case 'scaleWhen.always': {
  16011. $opt['mk']['if']['sw'] = 'A';
  16012. break;
  16013. }
  16014. case 'scaleWhen.never': {
  16015. $opt['mk']['if']['sw'] = 'N';
  16016. break;
  16017. }
  16018. case 'scaleWhen.tooBig': {
  16019. $opt['mk']['if']['sw'] = 'B';
  16020. break;
  16021. }
  16022. case 'scaleWhen.tooSmall': {
  16023. $opt['mk']['if']['sw'] = 'S';
  16024. break;
  16025. }
  16026. }
  16027. }
  16028. // buttonPosition: Controls how the text and the icon of the button are positioned with respect to each other within the button face.
  16029. if (isset($prop['buttonPosition'])) {
  16030. switch ($prop['buttonPosition']) {
  16031. case 0:
  16032. case 'position.textOnly': {
  16033. $opt['mk']['tp'] = 0;
  16034. break;
  16035. }
  16036. case 1:
  16037. case 'position.iconOnly': {
  16038. $opt['mk']['tp'] = 1;
  16039. break;
  16040. }
  16041. case 2:
  16042. case 'position.iconTextV': {
  16043. $opt['mk']['tp'] = 2;
  16044. break;
  16045. }
  16046. case 3:
  16047. case 'position.textIconV': {
  16048. $opt['mk']['tp'] = 3;
  16049. break;
  16050. }
  16051. case 4:
  16052. case 'position.iconTextH': {
  16053. $opt['mk']['tp'] = 4;
  16054. break;
  16055. }
  16056. case 5:
  16057. case 'position.textIconH': {
  16058. $opt['mk']['tp'] = 5;
  16059. break;
  16060. }
  16061. case 6:
  16062. case 'position.overlay': {
  16063. $opt['mk']['tp'] = 6;
  16064. break;
  16065. }
  16066. }
  16067. }
  16068. // fillColor: Specifies the background color for a field.
  16069. if (isset($prop['fillColor'])) {
  16070. if (is_array($prop['fillColor'])) {
  16071. $opt['mk']['bg'] = $prop['fillColor'];
  16072. } else {
  16073. $opt['mk']['bg'] = $this->convertHTMLColorToDec($prop['fillColor']);
  16074. }
  16075. }
  16076. // strokeColor: Specifies the stroke color for a field that is used to stroke the rectangle of the field with a line as large as the line width.
  16077. if (isset($prop['strokeColor'])) {
  16078. if (is_array($prop['strokeColor'])) {
  16079. $opt['mk']['bc'] = $prop['strokeColor'];
  16080. } else {
  16081. $opt['mk']['bc'] = $this->convertHTMLColorToDec($prop['strokeColor']);
  16082. }
  16083. }
  16084. // rotation: The rotation of a widget in counterclockwise increments.
  16085. if (isset($prop['rotation'])) {
  16086. $opt['mk']['r'] = $prop['rotation'];
  16087. }
  16088. // charLimit: Limits the number of characters that a user can type into a text field.
  16089. if (isset($prop['charLimit'])) {
  16090. $opt['maxlen'] = intval($prop['charLimit']);
  16091. }
  16092. if (!isset($ff)) {
  16093. $ff = 0; // default value
  16094. }
  16095. // readonly: The read-only characteristic of a field. If a field is read-only, the user can see the field but cannot change it.
  16096. if (isset($prop['readonly']) AND ($prop['readonly'] == 'true')) {
  16097. $ff += 1 << 0;
  16098. }
  16099. // required: Specifies whether a field requires a value.
  16100. if (isset($prop['required']) AND ($prop['required'] == 'true')) {
  16101. $ff += 1 << 1;
  16102. }
  16103. // multiline: Controls how text is wrapped within the field.
  16104. if (isset($prop['multiline']) AND ($prop['multiline'] == 'true')) {
  16105. $ff += 1 << 12;
  16106. }
  16107. // password: Specifies whether the field should display asterisks when data is entered in the field.
  16108. if (isset($prop['password']) AND ($prop['password'] == 'true')) {
  16109. $ff += 1 << 13;
  16110. }
  16111. // NoToggleToOff: If set, exactly one radio button shall be selected at all times; selecting the currently selected button has no effect.
  16112. if (isset($prop['NoToggleToOff']) AND ($prop['NoToggleToOff'] == 'true')) {
  16113. $ff += 1 << 14;
  16114. }
  16115. // Radio: If set, the field is a set of radio buttons.
  16116. if (isset($prop['Radio']) AND ($prop['Radio'] == 'true')) {
  16117. $ff += 1 << 15;
  16118. }
  16119. // Pushbutton: If set, the field is a pushbutton that does not retain a permanent value.
  16120. if (isset($prop['Pushbutton']) AND ($prop['Pushbutton'] == 'true')) {
  16121. $ff += 1 << 16;
  16122. }
  16123. // Combo: If set, the field is a combo box; if clear, the field is a list box.
  16124. if (isset($prop['Combo']) AND ($prop['Combo'] == 'true')) {
  16125. $ff += 1 << 17;
  16126. }
  16127. // editable: Controls whether a combo box is editable.
  16128. if (isset($prop['editable']) AND ($prop['editable'] == 'true')) {
  16129. $ff += 1 << 18;
  16130. }
  16131. // Sort: If set, the field's option items shall be sorted alphabetically.
  16132. if (isset($prop['Sort']) AND ($prop['Sort'] == 'true')) {
  16133. $ff += 1 << 19;
  16134. }
  16135. // fileSelect: If true, sets the file-select flag in the Options tab of the text field (Field is Used for File Selection).
  16136. if (isset($prop['fileSelect']) AND ($prop['fileSelect'] == 'true')) {
  16137. $ff += 1 << 20;
  16138. }
  16139. // multipleSelection: If true, indicates that a list box allows a multiple selection of items.
  16140. if (isset($prop['multipleSelection']) AND ($prop['multipleSelection'] == 'true')) {
  16141. $ff += 1 << 21;
  16142. }
  16143. // doNotSpellCheck: If true, spell checking is not performed on this editable text field.
  16144. if (isset($prop['doNotSpellCheck']) AND ($prop['doNotSpellCheck'] == 'true')) {
  16145. $ff += 1 << 22;
  16146. }
  16147. // doNotScroll: If true, the text field does not scroll and the user, therefore, is limited by the rectangular region designed for the field.
  16148. if (isset($prop['doNotScroll']) AND ($prop['doNotScroll'] == 'true')) {
  16149. $ff += 1 << 23;
  16150. }
  16151. // comb: If set to true, the field background is drawn as series of boxes (one for each character in the value of the field) and each character of the content is drawn within those boxes. The number of boxes drawn is determined from the charLimit property. It applies only to text fields. The setter will also raise if any of the following field properties are also set multiline, password, and fileSelect. A side-effect of setting this property is that the doNotScroll property is also set.
  16152. if (isset($prop['comb']) AND ($prop['comb'] == 'true')) {
  16153. $ff += 1 << 24;
  16154. }
  16155. // radiosInUnison: If false, even if a group of radio buttons have the same name and export value, they behave in a mutually exclusive fashion, like HTML radio buttons.
  16156. if (isset($prop['radiosInUnison']) AND ($prop['radiosInUnison'] == 'true')) {
  16157. $ff += 1 << 25;
  16158. }
  16159. // richText: If true, the field allows rich text formatting.
  16160. if (isset($prop['richText']) AND ($prop['richText'] == 'true')) {
  16161. $ff += 1 << 25;
  16162. }
  16163. // commitOnSelChange: Controls whether a field value is committed after a selection change.
  16164. if (isset($prop['commitOnSelChange']) AND ($prop['commitOnSelChange'] == 'true')) {
  16165. $ff += 1 << 26;
  16166. }
  16167. $opt['ff'] = $ff;
  16168. // defaultValue: The default value of a field - that is, the value that the field is set to when the form is reset.
  16169. if (isset($prop['defaultValue'])) {
  16170. $opt['dv'] = $prop['defaultValue'];
  16171. }
  16172. $f = 4; // default value for annotation flags
  16173. // readonly: The read-only characteristic of a field. If a field is read-only, the user can see the field but cannot change it.
  16174. if (isset($prop['readonly']) AND ($prop['readonly'] == 'true')) {
  16175. $f += 1 << 6;
  16176. }
  16177. // display: Controls whether the field is hidden or visible on screen and in print.
  16178. if (isset($prop['display'])) {
  16179. if ($prop['display'] == 'display.visible') {
  16180. //
  16181. } elseif ($prop['display'] == 'display.hidden') {
  16182. $f += 1 << 1;
  16183. } elseif ($prop['display'] == 'display.noPrint') {
  16184. $f -= 1 << 2;
  16185. } elseif ($prop['display'] == 'display.noView') {
  16186. $f += 1 << 5;
  16187. }
  16188. }
  16189. $opt['f'] = $f;
  16190. // currentValueIndices: Reads and writes single or multiple values of a list box or combo box.
  16191. if (isset($prop['currentValueIndices']) AND is_array($prop['currentValueIndices'])) {
  16192. $opt['i'] = $prop['currentValueIndices'];
  16193. }
  16194. // value: The value of the field data that the user has entered.
  16195. if (isset($prop['value'])) {
  16196. if (is_array($prop['value'])) {
  16197. $opt['opt'] = array();
  16198. foreach ($prop['value'] AS $key => $optval) {
  16199. // exportValues: An array of strings representing the export values for the field.
  16200. if (isset($prop['exportValues'][$key])) {
  16201. $opt['opt'][$key] = array($prop['exportValues'][$key], $prop['value'][$key]);
  16202. } else {
  16203. $opt['opt'][$key] = $prop['value'][$key];
  16204. }
  16205. }
  16206. } else {
  16207. $opt['v'] = $prop['value'];
  16208. }
  16209. }
  16210. // richValue: This property specifies the text contents and formatting of a rich text field.
  16211. if (isset($prop['richValue'])) {
  16212. $opt['rv'] = $prop['richValue'];
  16213. }
  16214. // submitName: If nonempty, used during form submission instead of name. Only applicable if submitting in HTML format (that is, URL-encoded).
  16215. if (isset($prop['submitName'])) {
  16216. $opt['tm'] = $prop['submitName'];
  16217. }
  16218. // name: Fully qualified field name.
  16219. if (isset($prop['name'])) {
  16220. $opt['t'] = $prop['name'];
  16221. }
  16222. // userName: The user name (short description string) of the field.
  16223. if (isset($prop['userName'])) {
  16224. $opt['tu'] = $prop['userName'];
  16225. }
  16226. // highlight: Defines how a button reacts when a user clicks it.
  16227. if (isset($prop['highlight'])) {
  16228. switch ($prop['highlight']) {
  16229. case 'none':
  16230. case 'highlight.n': {
  16231. $opt['h'] = 'N';
  16232. break;
  16233. }
  16234. case 'invert':
  16235. case 'highlight.i': {
  16236. $opt['h'] = 'i';
  16237. break;
  16238. }
  16239. case 'push':
  16240. case 'highlight.p': {
  16241. $opt['h'] = 'P';
  16242. break;
  16243. }
  16244. case 'outline':
  16245. case 'highlight.o': {
  16246. $opt['h'] = 'O';
  16247. break;
  16248. }
  16249. }
  16250. }
  16251. // Unsupported options:
  16252. // - calcOrderIndex: Changes the calculation order of fields in the document.
  16253. // - delay: Delays the redrawing of a field's appearance.
  16254. // - defaultStyle: This property defines the default style attributes for the form field.
  16255. // - style: Allows the user to set the glyph style of a check box or radio button.
  16256. // - textColor, textFont, textSize
  16257. return $opt;
  16258. }
  16259. /**
  16260. * Set default properties for form fields.
  16261. * @param $prop (array) javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  16262. * @public
  16263. * @author Nicola Asuni
  16264. * @since 4.8.000 (2009-09-06)
  16265. */
  16266. public function setFormDefaultProp($prop=array()) {
  16267. $this->default_form_prop = $prop;
  16268. }
  16269. /**
  16270. * Return the default properties for form fields.
  16271. * @return array $prop javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  16272. * @public
  16273. * @author Nicola Asuni
  16274. * @since 4.8.000 (2009-09-06)
  16275. */
  16276. public function getFormDefaultProp() {
  16277. return $this->default_form_prop;
  16278. }
  16279. /**
  16280. * Creates a text field
  16281. * @param $name (string) field name
  16282. * @param $w (float) Width of the rectangle
  16283. * @param $h (float) Height of the rectangle
  16284. * @param $prop (array) javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  16285. * @param $opt (array) annotation parameters. Possible values are described on official PDF32000_2008 reference.
  16286. * @param $x (float) Abscissa of the upper-left corner of the rectangle
  16287. * @param $y (float) Ordinate of the upper-left corner of the rectangle
  16288. * @param $js (boolean) if true put the field using JavaScript (requires Acrobat Writer to be rendered).
  16289. * @public
  16290. * @author Nicola Asuni
  16291. * @since 4.8.000 (2009-09-07)
  16292. */
  16293. public function TextField($name, $w, $h, $prop=array(), $opt=array(), $x='', $y='', $js=false) {
  16294. if ($x === '') {
  16295. $x = $this->x;
  16296. }
  16297. if ($y === '') {
  16298. $y = $this->y;
  16299. }
  16300. // check page for no-write regions and adapt page margins if necessary
  16301. list($x, $y) = $this->checkPageRegions($h, $x, $y);
  16302. if ($js) {
  16303. $this->_addfield('text', $name, $x, $y, $w, $h, $prop);
  16304. return;
  16305. }
  16306. // get default style
  16307. $prop = array_merge($this->getFormDefaultProp(), $prop);
  16308. // get annotation data
  16309. $popt = $this->getAnnotOptFromJSProp($prop);
  16310. // set default appearance stream
  16311. $this->annotation_fonts[$this->CurrentFont['fontkey']] = $this->CurrentFont['i'];
  16312. $fontstyle = sprintf('/F%d %.2F Tf %s', $this->CurrentFont['i'], $this->FontSizePt, $this->TextColor);
  16313. $popt['da'] = $fontstyle;
  16314. // build appearance stream
  16315. $popt['ap'] = array();
  16316. $popt['ap']['n'] = '/Tx BMC q '.$fontstyle.' ';
  16317. $text = '';
  16318. if (isset($prop['value']) AND !empty($prop['value'])) {
  16319. $text = $prop['value'];
  16320. } elseif (isset($opt['v']) AND !empty($opt['v'])) {
  16321. $text = $opt['v'];
  16322. }
  16323. $tmpid = $this->startTemplate($w, $h, false);
  16324. $this->MultiCell($w, $h, $text, 0, '', false, 0, 0, 0, true, 0, false, true, 0, 'T', false);
  16325. $this->endTemplate();
  16326. --$this->n;
  16327. $popt['ap']['n'] .= $this->xobjects[$tmpid]['outdata'];
  16328. unset($this->xobjects[$tmpid]);
  16329. $popt['ap']['n'] .= 'Q EMC';
  16330. // merge options
  16331. $opt = array_merge($popt, $opt);
  16332. // remove some conflicting options
  16333. unset($opt['bs']);
  16334. // set remaining annotation data
  16335. $opt['Subtype'] = 'Widget';
  16336. $opt['ft'] = 'Tx';
  16337. $opt['t'] = $name;
  16338. // Additional annotation's parameters (check _putannotsobj() method):
  16339. //$opt['f']
  16340. //$opt['as']
  16341. //$opt['bs']
  16342. //$opt['be']
  16343. //$opt['c']
  16344. //$opt['border']
  16345. //$opt['h']
  16346. //$opt['mk'];
  16347. //$opt['mk']['r']
  16348. //$opt['mk']['bc'];
  16349. //$opt['mk']['bg'];
  16350. unset($opt['mk']['ca']);
  16351. unset($opt['mk']['rc']);
  16352. unset($opt['mk']['ac']);
  16353. unset($opt['mk']['i']);
  16354. unset($opt['mk']['ri']);
  16355. unset($opt['mk']['ix']);
  16356. unset($opt['mk']['if']);
  16357. //$opt['mk']['if']['sw'];
  16358. //$opt['mk']['if']['s'];
  16359. //$opt['mk']['if']['a'];
  16360. //$opt['mk']['if']['fb'];
  16361. unset($opt['mk']['tp']);
  16362. //$opt['tu']
  16363. //$opt['tm']
  16364. //$opt['ff']
  16365. //$opt['v']
  16366. //$opt['dv']
  16367. //$opt['a']
  16368. //$opt['aa']
  16369. //$opt['q']
  16370. $this->Annotation($x, $y, $w, $h, $name, $opt, 0);
  16371. if ($this->rtl) {
  16372. $this->x -= $w;
  16373. } else {
  16374. $this->x += $w;
  16375. }
  16376. }
  16377. /**
  16378. * Creates a RadioButton field.
  16379. * @param $name (string) Field name.
  16380. * @param $w (int) Width or the radio button.
  16381. * @param $prop (array) Javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  16382. * @param $opt (array) Annotation parameters. Possible values are described on official PDF32000_2008 reference.
  16383. * @param $onvalue (string) Value to be returned if selected.
  16384. * @param $checked (boolean) Define the initial state.
  16385. * @param $x (float) Abscissa of the upper-left corner of the rectangle
  16386. * @param $y (float) Ordinate of the upper-left corner of the rectangle
  16387. * @param $js (boolean) If true put the field using JavaScript (requires Acrobat Writer to be rendered).
  16388. * @public
  16389. * @author Nicola Asuni
  16390. * @since 4.8.000 (2009-09-07)
  16391. */
  16392. public function RadioButton($name, $w, $prop=array(), $opt=array(), $onvalue='On', $checked=false, $x='', $y='', $js=false) {
  16393. if ($x === '') {
  16394. $x = $this->x;
  16395. }
  16396. if ($y === '') {
  16397. $y = $this->y;
  16398. }
  16399. // check page for no-write regions and adapt page margins if necessary
  16400. list($x, $y) = $this->checkPageRegions($w, $x, $y);
  16401. if ($js) {
  16402. $this->_addfield('radiobutton', $name, $x, $y, $w, $w, $prop);
  16403. return;
  16404. }
  16405. if ($this->empty_string($onvalue)) {
  16406. $onvalue = 'On';
  16407. }
  16408. if ($checked) {
  16409. $defval = $onvalue;
  16410. } else {
  16411. $defval = 'Off';
  16412. }
  16413. // set font
  16414. $font = 'zapfdingbats';
  16415. $this->AddFont($font);
  16416. $tmpfont = $this->getFontBuffer($font);
  16417. // set data for parent group
  16418. if (!isset($this->radiobutton_groups[$this->page])) {
  16419. $this->radiobutton_groups[$this->page] = array();
  16420. }
  16421. if (!isset($this->radiobutton_groups[$this->page][$name])) {
  16422. $this->radiobutton_groups[$this->page][$name] = array();
  16423. ++$this->n;
  16424. $this->radiobutton_groups[$this->page][$name]['n'] = $this->n;
  16425. $this->radio_groups[] = $this->n;
  16426. }
  16427. $kid = ($this->n + 1);
  16428. // save object ID to be added on Kids entry on parent object
  16429. $this->radiobutton_groups[$this->page][$name][] = array('kid' => $kid, 'def' => $defval);
  16430. // get default style
  16431. $prop = array_merge($this->getFormDefaultProp(), $prop);
  16432. $prop['NoToggleToOff'] = 'true';
  16433. $prop['Radio'] = 'true';
  16434. $prop['borderStyle'] = 'inset';
  16435. // get annotation data
  16436. $popt = $this->getAnnotOptFromJSProp($prop);
  16437. // set additional default options
  16438. $this->annotation_fonts[$tmpfont['fontkey']] = $tmpfont['i'];
  16439. $fontstyle = sprintf('/F%d %.2F Tf %s', $tmpfont['i'], $this->FontSizePt, $this->TextColor);
  16440. $popt['da'] = $fontstyle;
  16441. // build appearance stream
  16442. $popt['ap'] = array();
  16443. $popt['ap']['n'] = array();
  16444. $fy = (($w - ((($tmpfont['desc']['Ascent'] - $tmpfont['desc']['Descent']) * $this->FontSizePt / 1000) / $this->k)) * $this->k);
  16445. $popt['ap']['n'][$onvalue] = sprintf('q %s BT /F%d %.2F Tf %.2F %.2F Td ('.chr(108).') Tj ET Q', $this->TextColor, $tmpfont['i'], $this->FontSizePt, 0, $fy);
  16446. $popt['ap']['n']['Off'] = sprintf('q %s BT /F%d %.2F Tf %.2F %.2F Td ('.chr(109).') Tj ET Q', $this->TextColor, $tmpfont['i'], $this->FontSizePt, 0, $fy);
  16447. if (!isset($popt['mk'])) {
  16448. $popt['mk'] = array();
  16449. }
  16450. $popt['mk']['ca'] = '(l)';
  16451. // merge options
  16452. $opt = array_merge($popt, $opt);
  16453. // set remaining annotation data
  16454. $opt['Subtype'] = 'Widget';
  16455. $opt['ft'] = 'Btn';
  16456. if ($checked) {
  16457. $opt['v'] = array('/'.$onvalue);
  16458. $opt['as'] = $onvalue;
  16459. } else {
  16460. $opt['as'] = 'Off';
  16461. }
  16462. // store readonly flag
  16463. if (!isset($this->radiobutton_groups[$this->page][$name]['#readonly#'])) {
  16464. $this->radiobutton_groups[$this->page][$name]['#readonly#'] = false;
  16465. }
  16466. $this->radiobutton_groups[$this->page][$name]['#readonly#'] |= ($opt['f'] & 64);
  16467. $this->Annotation($x, $y, $w, $w, $name, $opt, 0);
  16468. if ($this->rtl) {
  16469. $this->x -= $w;
  16470. } else {
  16471. $this->x += $w;
  16472. }
  16473. }
  16474. /**
  16475. * Creates a List-box field
  16476. * @param $name (string) field name
  16477. * @param $w (int) width
  16478. * @param $h (int) height
  16479. * @param $values (array) array containing the list of values.
  16480. * @param $prop (array) javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  16481. * @param $opt (array) annotation parameters. Possible values are described on official PDF32000_2008 reference.
  16482. * @param $x (float) Abscissa of the upper-left corner of the rectangle
  16483. * @param $y (float) Ordinate of the upper-left corner of the rectangle
  16484. * @param $js (boolean) if true put the field using JavaScript (requires Acrobat Writer to be rendered).
  16485. * @public
  16486. * @author Nicola Asuni
  16487. * @since 4.8.000 (2009-09-07)
  16488. */
  16489. public function ListBox($name, $w, $h, $values, $prop=array(), $opt=array(), $x='', $y='', $js=false) {
  16490. if ($x === '') {
  16491. $x = $this->x;
  16492. }
  16493. if ($y === '') {
  16494. $y = $this->y;
  16495. }
  16496. // check page for no-write regions and adapt page margins if necessary
  16497. list($x, $y) = $this->checkPageRegions($h, $x, $y);
  16498. if ($js) {
  16499. $this->_addfield('listbox', $name, $x, $y, $w, $h, $prop);
  16500. $s = '';
  16501. foreach ($values as $value) {
  16502. $s .= '\''.addslashes($value).'\',';
  16503. }
  16504. $this->javascript .= 'f'.$name.'.setItems(['.substr($s, 0, -1)."]);\n";
  16505. return;
  16506. }
  16507. // get default style
  16508. $prop = array_merge($this->getFormDefaultProp(), $prop);
  16509. // get annotation data
  16510. $popt = $this->getAnnotOptFromJSProp($prop);
  16511. // set additional default values
  16512. $this->annotation_fonts[$this->CurrentFont['fontkey']] = $this->CurrentFont['i'];
  16513. $fontstyle = sprintf('/F%d %.2F Tf %s', $this->CurrentFont['i'], $this->FontSizePt, $this->TextColor);
  16514. $popt['da'] = $fontstyle;
  16515. // build appearance stream
  16516. $popt['ap'] = array();
  16517. $popt['ap']['n'] = '/Tx BMC q '.$fontstyle.' ';
  16518. $text = '';
  16519. foreach($values as $item) {
  16520. $text .= $item."\n";
  16521. }
  16522. $tmpid = $this->startTemplate($w, $h, false);
  16523. $this->MultiCell($w, $h, $text, 0, '', false, 0, 0, 0, true, 0, false, true, 0, 'T', false);
  16524. $this->endTemplate();
  16525. --$this->n;
  16526. $popt['ap']['n'] .= $this->xobjects[$tmpid]['outdata'];
  16527. unset($this->xobjects[$tmpid]);
  16528. $popt['ap']['n'] .= 'Q EMC';
  16529. // merge options
  16530. $opt = array_merge($popt, $opt);
  16531. // set remaining annotation data
  16532. $opt['Subtype'] = 'Widget';
  16533. $opt['ft'] = 'Ch';
  16534. $opt['t'] = $name;
  16535. $opt['opt'] = $values;
  16536. unset($opt['mk']['ca']);
  16537. unset($opt['mk']['rc']);
  16538. unset($opt['mk']['ac']);
  16539. unset($opt['mk']['i']);
  16540. unset($opt['mk']['ri']);
  16541. unset($opt['mk']['ix']);
  16542. unset($opt['mk']['if']);
  16543. unset($opt['mk']['tp']);
  16544. $this->Annotation($x, $y, $w, $h, $name, $opt, 0);
  16545. if ($this->rtl) {
  16546. $this->x -= $w;
  16547. } else {
  16548. $this->x += $w;
  16549. }
  16550. }
  16551. /**
  16552. * Creates a Combo-box field
  16553. * @param $name (string) field name
  16554. * @param $w (int) width
  16555. * @param $h (int) height
  16556. * @param $values (array) array containing the list of values.
  16557. * @param $prop (array) javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  16558. * @param $opt (array) annotation parameters. Possible values are described on official PDF32000_2008 reference.
  16559. * @param $x (float) Abscissa of the upper-left corner of the rectangle
  16560. * @param $y (float) Ordinate of the upper-left corner of the rectangle
  16561. * @param $js (boolean) if true put the field using JavaScript (requires Acrobat Writer to be rendered).
  16562. * @public
  16563. * @author Nicola Asuni
  16564. * @since 4.8.000 (2009-09-07)
  16565. */
  16566. public function ComboBox($name, $w, $h, $values, $prop=array(), $opt=array(), $x='', $y='', $js=false) {
  16567. if ($x === '') {
  16568. $x = $this->x;
  16569. }
  16570. if ($y === '') {
  16571. $y = $this->y;
  16572. }
  16573. // check page for no-write regions and adapt page margins if necessary
  16574. list($x, $y) = $this->checkPageRegions($h, $x, $y);
  16575. if ($js) {
  16576. $this->_addfield('combobox', $name, $x, $y, $w, $h, $prop);
  16577. $s = '';
  16578. foreach ($values as $value) {
  16579. $s .= "'".addslashes($value)."',";
  16580. }
  16581. $this->javascript .= 'f'.$name.'.setItems(['.substr($s, 0, -1)."]);\n";
  16582. return;
  16583. }
  16584. // get default style
  16585. $prop = array_merge($this->getFormDefaultProp(), $prop);
  16586. $prop['Combo'] = true;
  16587. // get annotation data
  16588. $popt = $this->getAnnotOptFromJSProp($prop);
  16589. // set additional default options
  16590. $this->annotation_fonts[$this->CurrentFont['fontkey']] = $this->CurrentFont['i'];
  16591. $fontstyle = sprintf('/F%d %.2F Tf %s', $this->CurrentFont['i'], $this->FontSizePt, $this->TextColor);
  16592. $popt['da'] = $fontstyle;
  16593. // build appearance stream
  16594. $popt['ap'] = array();
  16595. $popt['ap']['n'] = '/Tx BMC q '.$fontstyle.' ';
  16596. $text = '';
  16597. foreach($values as $item) {
  16598. $text .= $item[1]."\n";
  16599. }
  16600. $tmpid = $this->startTemplate($w, $h, false);
  16601. $this->MultiCell($w, $h, $text, 0, '', false, 0, 0, 0, true, 0, false, true, 0, 'T', false);
  16602. $this->endTemplate();
  16603. --$this->n;
  16604. $popt['ap']['n'] .= $this->xobjects[$tmpid]['outdata'];
  16605. unset($this->xobjects[$tmpid]);
  16606. $popt['ap']['n'] .= 'Q EMC';
  16607. // merge options
  16608. $opt = array_merge($popt, $opt);
  16609. // set remaining annotation data
  16610. $opt['Subtype'] = 'Widget';
  16611. $opt['ft'] = 'Ch';
  16612. $opt['t'] = $name;
  16613. $opt['opt'] = $values;
  16614. unset($opt['mk']['ca']);
  16615. unset($opt['mk']['rc']);
  16616. unset($opt['mk']['ac']);
  16617. unset($opt['mk']['i']);
  16618. unset($opt['mk']['ri']);
  16619. unset($opt['mk']['ix']);
  16620. unset($opt['mk']['if']);
  16621. unset($opt['mk']['tp']);
  16622. $this->Annotation($x, $y, $w, $h, $name, $opt, 0);
  16623. if ($this->rtl) {
  16624. $this->x -= $w;
  16625. } else {
  16626. $this->x += $w;
  16627. }
  16628. }
  16629. /**
  16630. * Creates a CheckBox field
  16631. * @param $name (string) field name
  16632. * @param $w (int) width
  16633. * @param $checked (boolean) define the initial state.
  16634. * @param $prop (array) javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  16635. * @param $opt (array) annotation parameters. Possible values are described on official PDF32000_2008 reference.
  16636. * @param $onvalue (string) value to be returned if selected.
  16637. * @param $x (float) Abscissa of the upper-left corner of the rectangle
  16638. * @param $y (float) Ordinate of the upper-left corner of the rectangle
  16639. * @param $js (boolean) if true put the field using JavaScript (requires Acrobat Writer to be rendered).
  16640. * @public
  16641. * @author Nicola Asuni
  16642. * @since 4.8.000 (2009-09-07)
  16643. */
  16644. public function CheckBox($name, $w, $checked=false, $prop=array(), $opt=array(), $onvalue='Yes', $x='', $y='', $js=false) {
  16645. if ($x === '') {
  16646. $x = $this->x;
  16647. }
  16648. if ($y === '') {
  16649. $y = $this->y;
  16650. }
  16651. // check page for no-write regions and adapt page margins if necessary
  16652. list($x, $y) = $this->checkPageRegions($w, $x, $y);
  16653. if ($js) {
  16654. $this->_addfield('checkbox', $name, $x, $y, $w, $w, $prop);
  16655. return;
  16656. }
  16657. if (!isset($prop['value'])) {
  16658. $prop['value'] = array('Yes');
  16659. }
  16660. // get default style
  16661. $prop = array_merge($this->getFormDefaultProp(), $prop);
  16662. $prop['borderStyle'] = 'inset';
  16663. // get annotation data
  16664. $popt = $this->getAnnotOptFromJSProp($prop);
  16665. // set additional default options
  16666. $font = 'zapfdingbats';
  16667. $this->AddFont($font);
  16668. $tmpfont = $this->getFontBuffer($font);
  16669. $this->annotation_fonts[$tmpfont['fontkey']] = $tmpfont['i'];
  16670. $fontstyle = sprintf('/F%d %.2F Tf %s', $tmpfont['i'], $this->FontSizePt, $this->TextColor);
  16671. $popt['da'] = $fontstyle;
  16672. // build appearance stream
  16673. $popt['ap'] = array();
  16674. $popt['ap']['n'] = array();
  16675. $fy = ((($tmpfont['desc']['Ascent'] + $tmpfont['desc']['Descent']) * $this->FontSizePt) / (1000 * $this->k));
  16676. $fy = (($w - ((($tmpfont['desc']['Ascent'] - $tmpfont['desc']['Descent']) * $this->FontSizePt / 1000) / $this->k)) * $this->k);
  16677. $popt['ap']['n']['Yes'] = sprintf('q %s BT /F%d %.2F Tf %.2F %.2F Td ('.chr(110).') Tj ET Q', $this->TextColor, $tmpfont['i'], $this->FontSizePt, 0, $fy);
  16678. $popt['ap']['n']['Off'] = sprintf('q %s BT /F%d %.2F Tf %.2F %.2F Td ('.chr(111).') Tj ET Q', $this->TextColor, $tmpfont['i'], $this->FontSizePt, 0, $fy);
  16679. // merge options
  16680. $opt = array_merge($popt, $opt);
  16681. // set remaining annotation data
  16682. $opt['Subtype'] = 'Widget';
  16683. $opt['ft'] = 'Btn';
  16684. $opt['t'] = $name;
  16685. $opt['opt'] = array($onvalue);
  16686. if ($checked) {
  16687. $opt['v'] = array('/Yes');
  16688. $opt['as'] = 'Yes';
  16689. } else {
  16690. $opt['v'] = array('/Off');
  16691. $opt['as'] = 'Off';
  16692. }
  16693. $this->Annotation($x, $y, $w, $w, $name, $opt, 0);
  16694. if ($this->rtl) {
  16695. $this->x -= $w;
  16696. } else {
  16697. $this->x += $w;
  16698. }
  16699. }
  16700. /**
  16701. * Creates a button field
  16702. * @param $name (string) field name
  16703. * @param $w (int) width
  16704. * @param $h (int) height
  16705. * @param $caption (string) caption.
  16706. * @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.
  16707. * @param $prop (array) javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  16708. * @param $opt (array) annotation parameters. Possible values are described on official PDF32000_2008 reference.
  16709. * @param $x (float) Abscissa of the upper-left corner of the rectangle
  16710. * @param $y (float) Ordinate of the upper-left corner of the rectangle
  16711. * @param $js (boolean) if true put the field using JavaScript (requires Acrobat Writer to be rendered).
  16712. * @public
  16713. * @author Nicola Asuni
  16714. * @since 4.8.000 (2009-09-07)
  16715. */
  16716. public function Button($name, $w, $h, $caption, $action, $prop=array(), $opt=array(), $x='', $y='', $js=false) {
  16717. if ($x === '') {
  16718. $x = $this->x;
  16719. }
  16720. if ($y === '') {
  16721. $y = $this->y;
  16722. }
  16723. // check page for no-write regions and adapt page margins if necessary
  16724. list($x, $y) = $this->checkPageRegions($h, $x, $y);
  16725. if ($js) {
  16726. $this->_addfield('button', $name, $this->x, $this->y, $w, $h, $prop);
  16727. $this->javascript .= 'f'.$name.".buttonSetCaption('".addslashes($caption)."');\n";
  16728. $this->javascript .= 'f'.$name.".setAction('MouseUp','".addslashes($action)."');\n";
  16729. $this->javascript .= 'f'.$name.".highlight='push';\n";
  16730. $this->javascript .= 'f'.$name.".print=false;\n";
  16731. return;
  16732. }
  16733. // get default style
  16734. $prop = array_merge($this->getFormDefaultProp(), $prop);
  16735. $prop['Pushbutton'] = 'true';
  16736. $prop['highlight'] = 'push';
  16737. $prop['display'] = 'display.noPrint';
  16738. // get annotation data
  16739. $popt = $this->getAnnotOptFromJSProp($prop);
  16740. $this->annotation_fonts[$this->CurrentFont['fontkey']] = $this->CurrentFont['i'];
  16741. $fontstyle = sprintf('/F%d %.2F Tf %s', $this->CurrentFont['i'], $this->FontSizePt, $this->TextColor);
  16742. $popt['da'] = $fontstyle;
  16743. // build appearance stream
  16744. $popt['ap'] = array();
  16745. $popt['ap']['n'] = '/Tx BMC q '.$fontstyle.' ';
  16746. $tmpid = $this->startTemplate($w, $h, false);
  16747. $bw = (2 / $this->k); // border width
  16748. $border = array(
  16749. 'L' => array('width' => $bw, 'cap' => 'square', 'join' => 'miter', 'dash' => 0, 'color' => array(231)),
  16750. 'R' => array('width' => $bw, 'cap' => 'square', 'join' => 'miter', 'dash' => 0, 'color' => array(51)),
  16751. 'T' => array('width' => $bw, 'cap' => 'square', 'join' => 'miter', 'dash' => 0, 'color' => array(231)),
  16752. 'B' => array('width' => $bw, 'cap' => 'square', 'join' => 'miter', 'dash' => 0, 'color' => array(51)));
  16753. $this->SetFillColor(204);
  16754. $this->Cell($w, $h, $caption, $border, 0, 'C', true, '', 1, false, 'T', 'M');
  16755. $this->endTemplate();
  16756. --$this->n;
  16757. $popt['ap']['n'] .= $this->xobjects[$tmpid]['outdata'];
  16758. unset($this->xobjects[$tmpid]);
  16759. $popt['ap']['n'] .= 'Q EMC';
  16760. // set additional default options
  16761. if (!isset($popt['mk'])) {
  16762. $popt['mk'] = array();
  16763. }
  16764. $ann_obj_id = ($this->n + 1);
  16765. if (!empty($action) AND !is_array($action)) {
  16766. $ann_obj_id = ($this->n + 2);
  16767. }
  16768. $popt['mk']['ca'] = $this->_textstring($caption, $ann_obj_id);
  16769. $popt['mk']['rc'] = $this->_textstring($caption, $ann_obj_id);
  16770. $popt['mk']['ac'] = $this->_textstring($caption, $ann_obj_id);
  16771. // merge options
  16772. $opt = array_merge($popt, $opt);
  16773. // set remaining annotation data
  16774. $opt['Subtype'] = 'Widget';
  16775. $opt['ft'] = 'Btn';
  16776. $opt['t'] = $caption;
  16777. $opt['v'] = $name;
  16778. if (!empty($action)) {
  16779. if (is_array($action)) {
  16780. // form action options as on section 12.7.5 of PDF32000_2008.
  16781. $opt['aa'] = '/D <<';
  16782. $bmode = array('SubmitForm', 'ResetForm', 'ImportData');
  16783. foreach ($action AS $key => $val) {
  16784. if (($key == 'S') AND in_array($val, $bmode)) {
  16785. $opt['aa'] .= ' /S /'.$val;
  16786. } elseif (($key == 'F') AND (!empty($val))) {
  16787. $opt['aa'] .= ' /F '.$this->_datastring($val, $ann_obj_id);
  16788. } elseif (($key == 'Fields') AND is_array($val) AND !empty($val)) {
  16789. $opt['aa'] .= ' /Fields [';
  16790. foreach ($val AS $field) {
  16791. $opt['aa'] .= ' '.$this->_textstring($field, $ann_obj_id);
  16792. }
  16793. $opt['aa'] .= ']';
  16794. } elseif (($key == 'Flags')) {
  16795. $ff = 0;
  16796. if (is_array($val)) {
  16797. foreach ($val AS $flag) {
  16798. switch ($flag) {
  16799. case 'Include/Exclude': {
  16800. $ff += 1 << 0;
  16801. break;
  16802. }
  16803. case 'IncludeNoValueFields': {
  16804. $ff += 1 << 1;
  16805. break;
  16806. }
  16807. case 'ExportFormat': {
  16808. $ff += 1 << 2;
  16809. break;
  16810. }
  16811. case 'GetMethod': {
  16812. $ff += 1 << 3;
  16813. break;
  16814. }
  16815. case 'SubmitCoordinates': {
  16816. $ff += 1 << 4;
  16817. break;
  16818. }
  16819. case 'XFDF': {
  16820. $ff += 1 << 5;
  16821. break;
  16822. }
  16823. case 'IncludeAppendSaves': {
  16824. $ff += 1 << 6;
  16825. break;
  16826. }
  16827. case 'IncludeAnnotations': {
  16828. $ff += 1 << 7;
  16829. break;
  16830. }
  16831. case 'SubmitPDF': {
  16832. $ff += 1 << 8;
  16833. break;
  16834. }
  16835. case 'CanonicalFormat': {
  16836. $ff += 1 << 9;
  16837. break;
  16838. }
  16839. case 'ExclNonUserAnnots': {
  16840. $ff += 1 << 10;
  16841. break;
  16842. }
  16843. case 'ExclFKey': {
  16844. $ff += 1 << 11;
  16845. break;
  16846. }
  16847. case 'EmbedForm': {
  16848. $ff += 1 << 13;
  16849. break;
  16850. }
  16851. }
  16852. }
  16853. } else {
  16854. $ff = intval($val);
  16855. }
  16856. $opt['aa'] .= ' /Flags '.$ff;
  16857. }
  16858. }
  16859. $opt['aa'] .= ' >>';
  16860. } else {
  16861. // Javascript action or raw action command
  16862. $js_obj_id = $this->addJavascriptObject($action);
  16863. $opt['aa'] = '/D '.$js_obj_id.' 0 R';
  16864. }
  16865. }
  16866. $this->Annotation($x, $y, $w, $h, $name, $opt, 0);
  16867. if ($this->rtl) {
  16868. $this->x -= $w;
  16869. } else {
  16870. $this->x += $w;
  16871. }
  16872. }
  16873. // --- END FORMS FIELDS ------------------------------------------------
  16874. /**
  16875. * Add certification signature (DocMDP or UR3)
  16876. * You can set only one signature type
  16877. * @protected
  16878. * @author Nicola Asuni
  16879. * @since 4.6.008 (2009-05-07)
  16880. */
  16881. protected function _putsignature() {
  16882. if ((!$this->sign) OR (!isset($this->signature_data['cert_type']))) {
  16883. return;
  16884. }
  16885. $sigobjid = ($this->sig_obj_id + 1);
  16886. $out = $this->_getobj($sigobjid)."\n";
  16887. $out .= '<< /Type /Sig';
  16888. $out .= ' /Filter /Adobe.PPKLite';
  16889. $out .= ' /SubFilter /adbe.pkcs7.detached';
  16890. $out .= ' '.$this->byterange_string;
  16891. $out .= ' /Contents<'.str_repeat('0', $this->signature_max_length).'>';
  16892. $out .= ' /Reference ['; // array of signature reference dictionaries
  16893. $out .= ' << /Type /SigRef';
  16894. if ($this->signature_data['cert_type'] > 0) {
  16895. $out .= ' /TransformMethod /DocMDP';
  16896. $out .= ' /TransformParams <<';
  16897. $out .= ' /Type /TransformParams';
  16898. $out .= ' /P '.$this->signature_data['cert_type'];
  16899. $out .= ' /V /1.2';
  16900. } else {
  16901. $out .= ' /TransformMethod /UR3';
  16902. $out .= ' /TransformParams <<';
  16903. $out .= ' /Type /TransformParams';
  16904. $out .= ' /V /2.2';
  16905. if (!$this->empty_string($this->ur['document'])) {
  16906. $out .= ' /Document['.$this->ur['document'].']';
  16907. }
  16908. if (!$this->empty_string($this->ur['form'])) {
  16909. $out .= ' /Form['.$this->ur['form'].']';
  16910. }
  16911. if (!$this->empty_string($this->ur['signature'])) {
  16912. $out .= ' /Signature['.$this->ur['signature'].']';
  16913. }
  16914. if (!$this->empty_string($this->ur['annots'])) {
  16915. $out .= ' /Annots['.$this->ur['annots'].']';
  16916. }
  16917. if (!$this->empty_string($this->ur['ef'])) {
  16918. $out .= ' /EF['.$this->ur['ef'].']';
  16919. }
  16920. if (!$this->empty_string($this->ur['formex'])) {
  16921. $out .= ' /FormEX['.$this->ur['formex'].']';
  16922. }
  16923. }
  16924. $out .= ' >>'; // close TransformParams
  16925. // optional digest data (values must be calculated and replaced later)
  16926. //$out .= ' /Data ********** 0 R';
  16927. //$out .= ' /DigestMethod/MD5';
  16928. //$out .= ' /DigestLocation[********** 34]';
  16929. //$out .= ' /DigestValue<********************************>';
  16930. $out .= ' >>';
  16931. $out .= ' ]'; // end of reference
  16932. if (isset($this->signature_data['info']['Name']) AND !$this->empty_string($this->signature_data['info']['Name'])) {
  16933. $out .= ' /Name '.$this->_textstring($this->signature_data['info']['Name'], $sigobjid);
  16934. }
  16935. if (isset($this->signature_data['info']['Location']) AND !$this->empty_string($this->signature_data['info']['Location'])) {
  16936. $out .= ' /Location '.$this->_textstring($this->signature_data['info']['Location'], $sigobjid);
  16937. }
  16938. if (isset($this->signature_data['info']['Reason']) AND !$this->empty_string($this->signature_data['info']['Reason'])) {
  16939. $out .= ' /Reason '.$this->_textstring($this->signature_data['info']['Reason'], $sigobjid);
  16940. }
  16941. if (isset($this->signature_data['info']['ContactInfo']) AND !$this->empty_string($this->signature_data['info']['ContactInfo'])) {
  16942. $out .= ' /ContactInfo '.$this->_textstring($this->signature_data['info']['ContactInfo'], $sigobjid);
  16943. }
  16944. $out .= ' /M '.$this->_datestring($sigobjid);
  16945. $out .= ' >>';
  16946. $out .= "\n".'endobj';
  16947. $this->_out($out);
  16948. }
  16949. /**
  16950. * Set User's Rights for PDF Reader
  16951. * WARNING: This is experimental and currently do not work.
  16952. * Check the PDF Reference 8.7.1 Transform Methods,
  16953. * Table 8.105 Entries in the UR transform parameters dictionary
  16954. * @param $enable (boolean) if true enable user's rights on PDF reader
  16955. * @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.
  16956. * @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.
  16957. * @param $form (string) Names specifying additional form-field-related usage rights for the document. Valid names are: /Add/Delete/FillIn/Import/Export/SubmitStandalone/SpawnTemplate
  16958. * @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.
  16959. * @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
  16960. Names specifying additional embedded-files-related usage rights for the document.
  16961. * @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.
  16962. * @public
  16963. * @author Nicola Asuni
  16964. * @since 2.9.000 (2008-03-26)
  16965. */
  16966. public function setUserRights(
  16967. $enable=true,
  16968. $document='/FullSave',
  16969. $annots='/Create/Delete/Modify/Copy/Import/Export',
  16970. $form='/Add/Delete/FillIn/Import/Export/SubmitStandalone/SpawnTemplate',
  16971. $signature='/Modify',
  16972. $ef='/Create/Delete/Modify/Import',
  16973. $formex='') {
  16974. $this->ur['enabled'] = $enable;
  16975. $this->ur['document'] = $document;
  16976. $this->ur['annots'] = $annots;
  16977. $this->ur['form'] = $form;
  16978. $this->ur['signature'] = $signature;
  16979. $this->ur['ef'] = $ef;
  16980. $this->ur['formex'] = $formex;
  16981. if (!$this->sign) {
  16982. $this->setSignature('', '', '', '', 0, array());
  16983. }
  16984. }
  16985. /**
  16986. * Enable document signature (requires the OpenSSL Library).
  16987. * The digital signature improve document authenticity and integrity and allows o enable extra features on Acrobat Reader.
  16988. * To create self-signed signature: openssl req -x509 -nodes -days 365000 -newkey rsa:1024 -keyout tcpdf.crt -out tcpdf.crt
  16989. * To export crt to p12: openssl pkcs12 -export -in tcpdf.crt -out tcpdf.p12
  16990. * To convert pfx certificate to pem: openssl pkcs12 -in tcpdf.pfx -out tcpdf.crt -nodes
  16991. * @param $signing_cert (mixed) signing certificate (string or filename prefixed with 'file://')
  16992. * @param $private_key (mixed) private key (string or filename prefixed with 'file://')
  16993. * @param $private_key_password (string) password
  16994. * @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.
  16995. * @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.
  16996. * @param $info (array) array of option information: Name, Location, Reason, ContactInfo.
  16997. * @public
  16998. * @author Nicola Asuni
  16999. * @since 4.6.005 (2009-04-24)
  17000. */
  17001. public function setSignature($signing_cert='', $private_key='', $private_key_password='', $extracerts='', $cert_type=2, $info=array()) {
  17002. // to create self-signed signature: openssl req -x509 -nodes -days 365000 -newkey rsa:1024 -keyout tcpdf.crt -out tcpdf.crt
  17003. // to export crt to p12: openssl pkcs12 -export -in tcpdf.crt -out tcpdf.p12
  17004. // to convert pfx certificate to pem: openssl
  17005. // OpenSSL> pkcs12 -in <cert.pfx> -out <cert.crt> -nodes
  17006. $this->sign = true;
  17007. ++$this->n;
  17008. $this->sig_obj_id = $this->n; // signature widget
  17009. ++$this->n; // signature object ($this->sig_obj_id + 1)
  17010. $this->signature_data = array();
  17011. if (strlen($signing_cert) == 0) {
  17012. $signing_cert = 'file://'.dirname(__FILE__).'/tcpdf.crt';
  17013. $private_key_password = 'tcpdfdemo';
  17014. }
  17015. if (strlen($private_key) == 0) {
  17016. $private_key = $signing_cert;
  17017. }
  17018. $this->signature_data['signcert'] = $signing_cert;
  17019. $this->signature_data['privkey'] = $private_key;
  17020. $this->signature_data['password'] = $private_key_password;
  17021. $this->signature_data['extracerts'] = $extracerts;
  17022. $this->signature_data['cert_type'] = $cert_type;
  17023. $this->signature_data['info'] = $info;
  17024. }
  17025. /**
  17026. * Set the digital signature appearance (a cliccable rectangle area to get signature properties)
  17027. * @param $x (float) Abscissa of the upper-left corner.
  17028. * @param $y (float) Ordinate of the upper-left corner.
  17029. * @param $w (float) Width of the signature area.
  17030. * @param $h (float) Height of the signature area.
  17031. * @param $page (int) option page number (if < 0 the current page is used).
  17032. * @public
  17033. * @author Nicola Asuni
  17034. * @since 5.3.011 (2010-06-17)
  17035. */
  17036. public function setSignatureAppearance($x=0, $y=0, $w=0, $h=0, $page=-1) {
  17037. $this->signature_appearance = $this->getSignatureAppearanceArray($x, $y, $w, $h, $page);
  17038. }
  17039. /**
  17040. * Add an empty digital signature appearance (a cliccable rectangle area to get signature properties)
  17041. * @param $x (float) Abscissa of the upper-left corner.
  17042. * @param $y (float) Ordinate of the upper-left corner.
  17043. * @param $w (float) Width of the signature area.
  17044. * @param $h (float) Height of the signature area.
  17045. * @param $page (int) option page number (if < 0 the current page is used).
  17046. * @public
  17047. * @author Nicola Asuni
  17048. * @since 5.9.101 (2011-07-06)
  17049. */
  17050. public function addEmptySignatureAppearance($x=0, $y=0, $w=0, $h=0, $page=-1) {
  17051. ++$this->n;
  17052. $this->empty_signature_appearance[] = array('objid' => $this->n) + $this->getSignatureAppearanceArray($x, $y, $w, $h, $page);
  17053. }
  17054. /**
  17055. * Get the array that defines the signature appearance (page and rectangle coordinates).
  17056. * @param $x (float) Abscissa of the upper-left corner.
  17057. * @param $y (float) Ordinate of the upper-left corner.
  17058. * @param $w (float) Width of the signature area.
  17059. * @param $h (float) Height of the signature area.
  17060. * @param $page (int) option page number (if < 0 the current page is used).
  17061. * @return (array) Array defining page and rectangle coordinates of signature appearance.
  17062. * @protected
  17063. * @author Nicola Asuni
  17064. * @since 5.9.101 (2011-07-06)
  17065. */
  17066. protected function getSignatureAppearanceArray($x=0, $y=0, $w=0, $h=0, $page=-1) {
  17067. $sigapp = array();
  17068. if (($page < 1) OR ($page > $this->numpages)) {
  17069. $sigapp['page'] = $this->page;
  17070. } else {
  17071. $sigapp['page'] = intval($page);
  17072. }
  17073. $a = $x * $this->k;
  17074. $b = $this->pagedim[($sigapp['page'])]['h'] - (($y + $h) * $this->k);
  17075. $c = $w * $this->k;
  17076. $d = $h * $this->k;
  17077. $sigapp['rect'] = sprintf('%.2F %.2F %.2F %.2F', $a, $b, ($a + $c), ($b + $d));
  17078. return $sigapp;
  17079. }
  17080. /**
  17081. * Create a new page group.
  17082. * NOTE: call this function before calling AddPage()
  17083. * @param $page (int) starting group page (leave empty for next page).
  17084. * @public
  17085. * @since 3.0.000 (2008-03-27)
  17086. */
  17087. public function startPageGroup($page='') {
  17088. if (empty($page)) {
  17089. $page = $this->page + 1;
  17090. }
  17091. $this->newpagegroup[$page] = sizeof($this->newpagegroup) + 1;
  17092. }
  17093. /**
  17094. * This method is DEPRECATED and doesn't have any effect.
  17095. * Please remove any reference to this method.
  17096. * @param $s (string) Empty parameter.
  17097. * @deprecated deprecated since version 5.9.089 (2011-06-13)
  17098. * @public
  17099. */
  17100. public function AliasNbPages($s='') {}
  17101. /**
  17102. * This method is DEPRECATED and doesn't have any effect.
  17103. * Please remove any reference to this method.
  17104. * @param $s (string) Empty parameter.
  17105. * @deprecated deprecated since version 5.9.089 (2011-06-13)
  17106. * @public
  17107. */
  17108. public function AliasNumPage($s='') {}
  17109. /**
  17110. * Set the starting page number.
  17111. * @param $num (int) Starting page number.
  17112. * @since 5.9.093 (2011-06-16)
  17113. * @public
  17114. */
  17115. public function setStartingPageNumber($num=1) {
  17116. $this->starting_page_number = max(0, intval($num));
  17117. }
  17118. /**
  17119. * Returns the string alias used right align page numbers.
  17120. * If the current font is unicode type, the returned string wil contain an additional open curly brace.
  17121. * @return string
  17122. * @since 5.9.099 (2011-06-27)
  17123. * @public
  17124. */
  17125. public function getAliasRightShift() {
  17126. // calculate aproximatively the ratio between widths of aliases and replacements.
  17127. $ref = '{'.$this->alias_right_shift.'}{'.$this->alias_tot_pages.'}{'.$this->alias_num_page.'}';
  17128. $rep = str_repeat(' ', $this->GetNumChars($ref));
  17129. $wdiff = max(1, ($this->GetStringWidth($ref) / $this->GetStringWidth($rep)));
  17130. $sdiff = sprintf('%.3F', $wdiff);
  17131. $alias = $this->alias_right_shift.$sdiff.'}';
  17132. if ($this->isUnicodeFont()) {
  17133. $alias = '{'.$alias;
  17134. }
  17135. return $alias;
  17136. }
  17137. /**
  17138. * Returns the string alias used for the total number of pages.
  17139. * If the current font is unicode type, the returned string is surrounded by additional curly braces.
  17140. * This alias will be replaced by the total number of pages in the document.
  17141. * @return string
  17142. * @since 4.0.018 (2008-08-08)
  17143. * @public
  17144. */
  17145. public function getAliasNbPages() {
  17146. if ($this->isUnicodeFont()) {
  17147. return '{'.$this->alias_tot_pages.'}';
  17148. }
  17149. return $this->alias_tot_pages;
  17150. }
  17151. /**
  17152. * Returns the string alias used for the page number.
  17153. * If the current font is unicode type, the returned string is surrounded by additional curly braces.
  17154. * This alias will be replaced by the page number.
  17155. * @return string
  17156. * @since 4.5.000 (2009-01-02)
  17157. * @public
  17158. */
  17159. public function getAliasNumPage() {
  17160. if ($this->isUnicodeFont()) {
  17161. return '{'.$this->alias_num_page.'}';
  17162. }
  17163. return $this->alias_num_page;
  17164. }
  17165. /**
  17166. * Return the alias for the total number of pages in the current page group.
  17167. * If the current font is unicode type, the returned string is surrounded by additional curly braces.
  17168. * This alias will be replaced by the total number of pages in this group.
  17169. * @return alias of the current page group
  17170. * @public
  17171. * @since 3.0.000 (2008-03-27)
  17172. */
  17173. public function getPageGroupAlias() {
  17174. if ($this->isUnicodeFont()) {
  17175. return '{'.$this->alias_group_tot_pages.'}';
  17176. }
  17177. return $this->alias_group_tot_pages;
  17178. }
  17179. /**
  17180. * Return the alias for the page number on the current page group.
  17181. * If the current font is unicode type, the returned string is surrounded by additional curly braces.
  17182. * This alias will be replaced by the page number (relative to the belonging group).
  17183. * @return alias of the current page group
  17184. * @public
  17185. * @since 4.5.000 (2009-01-02)
  17186. */
  17187. public function getPageNumGroupAlias() {
  17188. if ($this->isUnicodeFont()) {
  17189. return '{'.$this->alias_group_num_page.'}';
  17190. }
  17191. return $this->alias_group_num_page;
  17192. }
  17193. /**
  17194. * Return the current page in the group.
  17195. * @return current page in the group
  17196. * @public
  17197. * @since 3.0.000 (2008-03-27)
  17198. */
  17199. public function getGroupPageNo() {
  17200. return $this->pagegroups[$this->currpagegroup];
  17201. }
  17202. /**
  17203. * Returns the current group page number formatted as a string.
  17204. * @public
  17205. * @since 4.3.003 (2008-11-18)
  17206. * @see PaneNo(), formatPageNumber()
  17207. */
  17208. public function getGroupPageNoFormatted() {
  17209. return $this->formatPageNumber($this->getGroupPageNo());
  17210. }
  17211. /**
  17212. * Format the page numbers.
  17213. * This method can be overriden for custom formats.
  17214. * @param $num (int) page number
  17215. * @protected
  17216. * @since 4.2.005 (2008-11-06)
  17217. */
  17218. protected function formatPageNumber($num) {
  17219. return number_format((float)$num, 0, '', '.');
  17220. }
  17221. /**
  17222. * Format the page numbers on the Table Of Content.
  17223. * This method can be overriden for custom formats.
  17224. * @param $num (int) page number
  17225. * @protected
  17226. * @since 4.5.001 (2009-01-04)
  17227. * @see addTOC(), addHTMLTOC()
  17228. */
  17229. protected function formatTOCPageNumber($num) {
  17230. return number_format((float)$num, 0, '', '.');
  17231. }
  17232. /**
  17233. * Returns the current page number formatted as a string.
  17234. * @public
  17235. * @since 4.2.005 (2008-11-06)
  17236. * @see PaneNo(), formatPageNumber()
  17237. */
  17238. public function PageNoFormatted() {
  17239. return $this->formatPageNumber($this->PageNo());
  17240. }
  17241. /**
  17242. * Put pdf layers.
  17243. * @protected
  17244. * @since 3.0.000 (2008-03-27)
  17245. */
  17246. protected function _putocg() {
  17247. if (empty($this->pdflayers)) {
  17248. return;
  17249. }
  17250. foreach ($this->pdflayers as $key => $layer) {
  17251. $this->pdflayers[$key]['objid'] = $this->_newobj();
  17252. $out = '<< /Type /OCG';
  17253. $out .= ' /Name '.$this->_textstring($layer['name'], $this->pdflayers[$key]['objid']);
  17254. $out .= ' /Usage <<';
  17255. $out .= ' /Print <</PrintState /'.($layer['print']?'ON':'OFF').'>>';
  17256. $out .= ' /View <</ViewState /'.($layer['view']?'ON':'OFF').'>>';
  17257. $out .= ' >> >>';
  17258. $out .= "\n".'endobj';
  17259. $this->_out($out);
  17260. }
  17261. }
  17262. /**
  17263. * Start a new pdf layer.
  17264. * @param $name (string) Layer name (only a-z letters and numbers). Leave empty for automatic name.
  17265. * @param $print (boolean) Set to true to print this layer.
  17266. * @param $view (boolean) Set to true to view this layer.
  17267. * @public
  17268. * @since 5.9.102 (2011-07-13)
  17269. */
  17270. public function startLayer($name='', $print=true, $view=true) {
  17271. $layer = sprintf('LYR%03d', (count($this->pdflayers) + 1));
  17272. if (empty($name)) {
  17273. $name = $layer;
  17274. } else {
  17275. $name = preg_replace('/[^a-zA-Z0-9_\-]/', '', $name);
  17276. }
  17277. $this->pdflayers[] = array('layer' => $layer, 'name' => $name, 'print' => $print, 'view' => $view);
  17278. $this->openMarkedContent = true;
  17279. $this->_out('/OC /'.$layer.' BDC');
  17280. }
  17281. /**
  17282. * End the current PDF layer.
  17283. * @public
  17284. * @since 5.9.102 (2011-07-13)
  17285. */
  17286. public function endLayer() {
  17287. if ($this->openMarkedContent) {
  17288. // close existing open marked-content layer
  17289. $this->_out('EMC');
  17290. $this->openMarkedContent = false;
  17291. }
  17292. }
  17293. /**
  17294. * Set the visibility of the successive elements.
  17295. * This can be useful, for instance, to put a background
  17296. * image or color that will show on screen but won't print.
  17297. * @param $v (string) visibility mode. Legal values are: all, print, screen or view.
  17298. * @public
  17299. * @since 3.0.000 (2008-03-27)
  17300. */
  17301. public function setVisibility($v) {
  17302. $this->endLayer();
  17303. switch($v) {
  17304. case 'print': {
  17305. $this->startLayer('Print', true, false);
  17306. break;
  17307. }
  17308. case 'view':
  17309. case 'screen': {
  17310. $this->startLayer('View', false, true);
  17311. break;
  17312. }
  17313. case 'all': {
  17314. $this->_out('');
  17315. break;
  17316. }
  17317. default: {
  17318. $this->Error('Incorrect visibility: '.$v);
  17319. break;
  17320. }
  17321. }
  17322. }
  17323. /**
  17324. * Add transparency parameters to the current extgstate
  17325. * @param $parms (array) parameters
  17326. * @return the number of extgstates
  17327. * @protected
  17328. * @since 3.0.000 (2008-03-27)
  17329. */
  17330. protected function addExtGState($parms) {
  17331. if ($this->pdfa_mode) {
  17332. // transparencies are not allowed in PDF/A mode
  17333. return;
  17334. }
  17335. // check if this ExtGState already exist
  17336. foreach ($this->extgstates as $i => $ext) {
  17337. if ($ext['parms'] == $parms) {
  17338. if ($this->inxobj) {
  17339. // we are inside an XObject template
  17340. $this->xobjects[$this->xobjid]['extgstates'][$i] = $ext;
  17341. }
  17342. // return reference to existing ExtGState
  17343. return $i;
  17344. }
  17345. }
  17346. $n = (count($this->extgstates) + 1);
  17347. $this->extgstates[$n] = array('parms' => $parms);
  17348. if ($this->inxobj) {
  17349. // we are inside an XObject template
  17350. $this->xobjects[$this->xobjid]['extgstates'][$n] = $this->extgstates[$n];
  17351. }
  17352. return $n;
  17353. }
  17354. /**
  17355. * Add an extgstate
  17356. * @param $gs (array) extgstate
  17357. * @protected
  17358. * @since 3.0.000 (2008-03-27)
  17359. */
  17360. protected function setExtGState($gs) {
  17361. if ($this->pdfa_mode) {
  17362. // transparency is not allowed in PDF/A mode
  17363. return;
  17364. }
  17365. $this->_out(sprintf('/GS%d gs', $gs));
  17366. }
  17367. /**
  17368. * Put extgstates for object transparency
  17369. * @protected
  17370. * @since 3.0.000 (2008-03-27)
  17371. */
  17372. protected function _putextgstates() {
  17373. if ($this->pdfa_mode) {
  17374. // transparencies are not allowed in PDF/A mode
  17375. return;
  17376. }
  17377. foreach ($this->extgstates as $i => $ext) {
  17378. $this->extgstates[$i]['n'] = $this->_newobj();
  17379. $out = '<< /Type /ExtGState';
  17380. foreach ($ext['parms'] as $k => $v) {
  17381. if (is_float($v)) {
  17382. $v = sprintf('%.2F', $v);
  17383. }
  17384. $out .= ' /'.$k.' '.$v;
  17385. }
  17386. $out .= ' >>';
  17387. $out .= "\n".'endobj';
  17388. $this->_out($out);
  17389. }
  17390. }
  17391. /**
  17392. * Set alpha for stroking (CA) and non-stroking (ca) operations.
  17393. * @param $alpha (float) real value from 0 (transparent) to 1 (opaque)
  17394. * @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
  17395. * @public
  17396. * @since 3.0.000 (2008-03-27)
  17397. */
  17398. public function setAlpha($alpha, $bm='Normal') {
  17399. if ($this->pdfa_mode) {
  17400. // transparency is not allowed in PDF/A mode
  17401. return;
  17402. }
  17403. $gs = $this->addExtGState(array('ca' => $alpha, 'CA' => $alpha, 'BM' => '/'.$bm, 'AIS' => 'false'));
  17404. $this->setExtGState($gs);
  17405. }
  17406. /**
  17407. * Set the default JPEG compression quality (1-100)
  17408. * @param $quality (int) JPEG quality, integer between 1 and 100
  17409. * @public
  17410. * @since 3.0.000 (2008-03-27)
  17411. */
  17412. public function setJPEGQuality($quality) {
  17413. if (($quality < 1) OR ($quality > 100)) {
  17414. $quality = 75;
  17415. }
  17416. $this->jpeg_quality = intval($quality);
  17417. }
  17418. /**
  17419. * Set the default number of columns in a row for HTML tables.
  17420. * @param $cols (int) number of columns
  17421. * @public
  17422. * @since 3.0.014 (2008-06-04)
  17423. */
  17424. public function setDefaultTableColumns($cols=4) {
  17425. $this->default_table_columns = intval($cols);
  17426. }
  17427. /**
  17428. * Set the height of the cell (line height) respect the font height.
  17429. * @param $h (int) cell proportion respect font height (typical value = 1.25).
  17430. * @public
  17431. * @since 3.0.014 (2008-06-04)
  17432. */
  17433. public function setCellHeightRatio($h) {
  17434. $this->cell_height_ratio = $h;
  17435. }
  17436. /**
  17437. * return the height of cell repect font height.
  17438. * @public
  17439. * @since 4.0.012 (2008-07-24)
  17440. */
  17441. public function getCellHeightRatio() {
  17442. return $this->cell_height_ratio;
  17443. }
  17444. /**
  17445. * Set the PDF version (check PDF reference for valid values).
  17446. * @param $version (string) PDF document version.
  17447. * @public
  17448. * @since 3.1.000 (2008-06-09)
  17449. */
  17450. public function setPDFVersion($version='1.7') {
  17451. if ($this->pdfa_mode) {
  17452. // PDF/A mode
  17453. $this->PDFVersion = '1.4';
  17454. } else {
  17455. $this->PDFVersion = $version;
  17456. }
  17457. }
  17458. /**
  17459. * Set the viewer preferences dictionary controlling the way the document is to be presented on the screen or in print.
  17460. * (see Section 8.1 of PDF reference, "Viewer Preferences").
  17461. * <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>
  17462. * @param $preferences (array) array of options.
  17463. * @author Nicola Asuni
  17464. * @public
  17465. * @since 3.1.000 (2008-06-09)
  17466. */
  17467. public function setViewerPreferences($preferences) {
  17468. $this->viewer_preferences = $preferences;
  17469. }
  17470. /**
  17471. * Paints color transition registration bars
  17472. * @param $x (float) abscissa of the top left corner of the rectangle.
  17473. * @param $y (float) ordinate of the top left corner of the rectangle.
  17474. * @param $w (float) width of the rectangle.
  17475. * @param $h (float) height of the rectangle.
  17476. * @param $transition (boolean) if true prints tcolor transitions to white.
  17477. * @param $vertical (boolean) if true prints bar vertically.
  17478. * @param $colors (string) colors to print, one letter per color separated by comma (for example 'A,W,R,G,B,C,M,Y,K'): A=black, W=white, R=red, G=green, B=blue, C=cyan, M=magenta, Y=yellow, K=black.
  17479. * @author Nicola Asuni
  17480. * @since 4.9.000 (2010-03-26)
  17481. * @public
  17482. */
  17483. public function colorRegistrationBar($x, $y, $w, $h, $transition=true, $vertical=false, $colors='A,R,G,B,C,M,Y,K') {
  17484. $bars = explode(',', $colors);
  17485. $numbars = count($bars); // number of bars to print
  17486. // set bar measures
  17487. if ($vertical) {
  17488. $coords = array(0, 0, 0, 1);
  17489. $wb = $w / $numbars; // bar width
  17490. $hb = $h; // bar height
  17491. $xd = $wb; // delta x
  17492. $yd = 0; // delta y
  17493. } else {
  17494. $coords = array(1, 0, 0, 0);
  17495. $wb = $w; // bar width
  17496. $hb = $h / $numbars; // bar height
  17497. $xd = 0; // delta x
  17498. $yd = $hb; // delta y
  17499. }
  17500. $xb = $x;
  17501. $yb = $y;
  17502. foreach ($bars as $col) {
  17503. switch ($col) {
  17504. // set transition colors
  17505. case 'A': { // BLACK
  17506. $col_a = array(255);
  17507. $col_b = array(0);
  17508. break;
  17509. }
  17510. case 'W': { // WHITE
  17511. $col_a = array(0);
  17512. $col_b = array(255);
  17513. break;
  17514. }
  17515. case 'R': { // R
  17516. $col_a = array(255,255,255);
  17517. $col_b = array(255,0,0);
  17518. break;
  17519. }
  17520. case 'G': { // G
  17521. $col_a = array(255,255,255);
  17522. $col_b = array(0,255,0);
  17523. break;
  17524. }
  17525. case 'B': { // B
  17526. $col_a = array(255,255,255);
  17527. $col_b = array(0,0,255);
  17528. break;
  17529. }
  17530. case 'C': { // C
  17531. $col_a = array(0,0,0,0);
  17532. $col_b = array(100,0,0,0);
  17533. break;
  17534. }
  17535. case 'M': { // M
  17536. $col_a = array(0,0,0,0);
  17537. $col_b = array(0,100,0,0);
  17538. break;
  17539. }
  17540. case 'Y': { // Y
  17541. $col_a = array(0,0,0,0);
  17542. $col_b = array(0,0,100,0);
  17543. break;
  17544. }
  17545. case 'K': { // K
  17546. $col_a = array(0,0,0,0);
  17547. $col_b = array(0,0,0,100);
  17548. break;
  17549. }
  17550. default: { // GRAY
  17551. $col_a = array(255);
  17552. $col_b = array(0);
  17553. break;
  17554. }
  17555. }
  17556. if ($transition) {
  17557. // color gradient
  17558. $this->LinearGradient($xb, $yb, $wb, $hb, $col_a, $col_b, $coords);
  17559. } else {
  17560. // color rectangle
  17561. $this->SetFillColorArray($col_b);
  17562. $this->Rect($xb, $yb, $wb, $hb, 'F', array());
  17563. }
  17564. $xb += $xd;
  17565. $yb += $yd;
  17566. }
  17567. }
  17568. /**
  17569. * Paints crop mark
  17570. * @param $x (float) abscissa of the crop mark center.
  17571. * @param $y (float) ordinate of the crop mark center.
  17572. * @param $w (float) width of the crop mark.
  17573. * @param $h (float) height of the crop mark.
  17574. * @param $type (string) type of crop mark, one sybol per type separated by comma: A = top left, B = top right, C = bottom left, D = bottom right.
  17575. * @param $color (array) crop mark color (default black).
  17576. * @author Nicola Asuni
  17577. * @since 4.9.000 (2010-03-26)
  17578. * @public
  17579. */
  17580. public function cropMark($x, $y, $w, $h, $type='A,B,C,D', $color=array(0,0,0)) {
  17581. $this->SetLineStyle(array('width' => (0.5 / $this->k), 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => $color));
  17582. $crops = explode(',', $type);
  17583. $numcrops = count($crops); // number of crop marks to print
  17584. $dw = $w / 4; // horizontal space to leave before the intersection point
  17585. $dh = $h / 4; // vertical space to leave before the intersection point
  17586. foreach ($crops as $crop) {
  17587. switch ($crop) {
  17588. case 'A': {
  17589. $x1 = $x;
  17590. $y1 = $y - $h;
  17591. $x2 = $x;
  17592. $y2 = $y - $dh;
  17593. $x3 = $x - $w;
  17594. $y3 = $y;
  17595. $x4 = $x - $dw;
  17596. $y4 = $y;
  17597. break;
  17598. }
  17599. case 'B': {
  17600. $x1 = $x;
  17601. $y1 = $y - $h;
  17602. $x2 = $x;
  17603. $y2 = $y - $dh;
  17604. $x3 = $x + $dw;
  17605. $y3 = $y;
  17606. $x4 = $x + $w;
  17607. $y4 = $y;
  17608. break;
  17609. }
  17610. case 'C': {
  17611. $x1 = $x - $w;
  17612. $y1 = $y;
  17613. $x2 = $x - $dw;
  17614. $y2 = $y;
  17615. $x3 = $x;
  17616. $y3 = $y + $dh;
  17617. $x4 = $x;
  17618. $y4 = $y + $h;
  17619. break;
  17620. }
  17621. case 'D': {
  17622. $x1 = $x + $dw;
  17623. $y1 = $y;
  17624. $x2 = $x + $w;
  17625. $y2 = $y;
  17626. $x3 = $x;
  17627. $y3 = $y + $dh;
  17628. $x4 = $x;
  17629. $y4 = $y + $h;
  17630. break;
  17631. }
  17632. }
  17633. $this->Line($x1, $y1, $x2, $y2);
  17634. $this->Line($x3, $y3, $x4, $y4);
  17635. }
  17636. }
  17637. /**
  17638. * Paints a registration mark
  17639. * @param $x (float) abscissa of the registration mark center.
  17640. * @param $y (float) ordinate of the registration mark center.
  17641. * @param $r (float) radius of the crop mark.
  17642. * @param $double (boolean) if true print two concentric crop marks.
  17643. * @param $cola (array) crop mark color (default black).
  17644. * @param $colb (array) second crop mark color.
  17645. * @author Nicola Asuni
  17646. * @since 4.9.000 (2010-03-26)
  17647. * @public
  17648. */
  17649. public function registrationMark($x, $y, $r, $double=false, $cola=array(0,0,0), $colb=array(255,255,255)) {
  17650. $line_style = array('width' => (0.5 / $this->k), 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => $cola);
  17651. $this->SetFillColorArray($cola);
  17652. $this->PieSector($x, $y, $r, 90, 180, 'F');
  17653. $this->PieSector($x, $y, $r, 270, 360, 'F');
  17654. $this->Circle($x, $y, $r, 0, 360, 'C', $line_style, array(), 8);
  17655. if ($double) {
  17656. $r2 = $r * 0.5;
  17657. $this->SetFillColorArray($colb);
  17658. $this->PieSector($x, $y, $r2, 90, 180, 'F');
  17659. $this->PieSector($x, $y, $r2, 270, 360, 'F');
  17660. $this->SetFillColorArray($cola);
  17661. $this->PieSector($x, $y, $r2, 0, 90, 'F');
  17662. $this->PieSector($x, $y, $r2, 180, 270, 'F');
  17663. $this->Circle($x, $y, $r2, 0, 360, 'C', $line_style, array(), 8);
  17664. }
  17665. }
  17666. /**
  17667. * Paints a linear colour gradient.
  17668. * @param $x (float) abscissa of the top left corner of the rectangle.
  17669. * @param $y (float) ordinate of the top left corner of the rectangle.
  17670. * @param $w (float) width of the rectangle.
  17671. * @param $h (float) height of the rectangle.
  17672. * @param $col1 (array) first color (Grayscale, RGB or CMYK components).
  17673. * @param $col2 (array) second color (Grayscale, RGB or CMYK components).
  17674. * @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).
  17675. * @author Andreas Würmser, Nicola Asuni
  17676. * @since 3.1.000 (2008-06-09)
  17677. * @public
  17678. */
  17679. public function LinearGradient($x, $y, $w, $h, $col1=array(), $col2=array(), $coords=array(0,0,1,0)) {
  17680. $this->Clip($x, $y, $w, $h);
  17681. $this->Gradient(2, $coords, array(array('color' => $col1, 'offset' => 0, 'exponent' => 1), array('color' => $col2, 'offset' => 1, 'exponent' => 1)), array(), false);
  17682. }
  17683. /**
  17684. * Paints a radial colour gradient.
  17685. * @param $x (float) abscissa of the top left corner of the rectangle.
  17686. * @param $y (float) ordinate of the top left corner of the rectangle.
  17687. * @param $w (float) width of the rectangle.
  17688. * @param $h (float) height of the rectangle.
  17689. * @param $col1 (array) first color (Grayscale, RGB or CMYK components).
  17690. * @param $col2 (array) second color (Grayscale, RGB or CMYK components).
  17691. * @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.
  17692. * @author Andreas Würmser, Nicola Asuni
  17693. * @since 3.1.000 (2008-06-09)
  17694. * @public
  17695. */
  17696. public function RadialGradient($x, $y, $w, $h, $col1=array(), $col2=array(), $coords=array(0.5,0.5,0.5,0.5,1)) {
  17697. $this->Clip($x, $y, $w, $h);
  17698. $this->Gradient(3, $coords, array(array('color' => $col1, 'offset' => 0, 'exponent' => 1), array('color' => $col2, 'offset' => 1, 'exponent' => 1)), array(), false);
  17699. }
  17700. /**
  17701. * Paints a coons patch mesh.
  17702. * @param $x (float) abscissa of the top left corner of the rectangle.
  17703. * @param $y (float) ordinate of the top left corner of the rectangle.
  17704. * @param $w (float) width of the rectangle.
  17705. * @param $h (float) height of the rectangle.
  17706. * @param $col1 (array) first color (lower left corner) (RGB components).
  17707. * @param $col2 (array) second color (lower right corner) (RGB components).
  17708. * @param $col3 (array) third color (upper right corner) (RGB components).
  17709. * @param $col4 (array) fourth color (upper left corner) (RGB components).
  17710. * @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>
  17711. * @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
  17712. * @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
  17713. * @param $antialias (boolean) A flag indicating whether to filter the shading function to prevent aliasing artifacts.
  17714. * @author Andreas Würmser, Nicola Asuni
  17715. * @since 3.1.000 (2008-06-09)
  17716. * @public
  17717. */
  17718. 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) {
  17719. if ($this->pdfa_mode) {
  17720. return;
  17721. }
  17722. $this->Clip($x, $y, $w, $h);
  17723. $n = count($this->gradients) + 1;
  17724. $this->gradients[$n] = array();
  17725. $this->gradients[$n]['type'] = 6; //coons patch mesh
  17726. $this->gradients[$n]['coords'] = array();
  17727. $this->gradients[$n]['antialias'] = $antialias;
  17728. $this->gradients[$n]['colors'] = array();
  17729. $this->gradients[$n]['transparency'] = false;
  17730. //check the coords array if it is the simple array or the multi patch array
  17731. if (!isset($coords[0]['f'])) {
  17732. //simple array -> convert to multi patch array
  17733. if (!isset($col1[1])) {
  17734. $col1[1] = $col1[2] = $col1[0];
  17735. }
  17736. if (!isset($col2[1])) {
  17737. $col2[1] = $col2[2] = $col2[0];
  17738. }
  17739. if (!isset($col3[1])) {
  17740. $col3[1] = $col3[2] = $col3[0];
  17741. }
  17742. if (!isset($col4[1])) {
  17743. $col4[1] = $col4[2] = $col4[0];
  17744. }
  17745. $patch_array[0]['f'] = 0;
  17746. $patch_array[0]['points'] = $coords;
  17747. $patch_array[0]['colors'][0]['r'] = $col1[0];
  17748. $patch_array[0]['colors'][0]['g'] = $col1[1];
  17749. $patch_array[0]['colors'][0]['b'] = $col1[2];
  17750. $patch_array[0]['colors'][1]['r'] = $col2[0];
  17751. $patch_array[0]['colors'][1]['g'] = $col2[1];
  17752. $patch_array[0]['colors'][1]['b'] = $col2[2];
  17753. $patch_array[0]['colors'][2]['r'] = $col3[0];
  17754. $patch_array[0]['colors'][2]['g'] = $col3[1];
  17755. $patch_array[0]['colors'][2]['b'] = $col3[2];
  17756. $patch_array[0]['colors'][3]['r'] = $col4[0];
  17757. $patch_array[0]['colors'][3]['g'] = $col4[1];
  17758. $patch_array[0]['colors'][3]['b'] = $col4[2];
  17759. } else {
  17760. //multi patch array
  17761. $patch_array = $coords;
  17762. }
  17763. $bpcd = 65535; //16 bits per coordinate
  17764. //build the data stream
  17765. $this->gradients[$n]['stream'] = '';
  17766. $count_patch = count($patch_array);
  17767. for ($i=0; $i < $count_patch; ++$i) {
  17768. $this->gradients[$n]['stream'] .= chr($patch_array[$i]['f']); //start with the edge flag as 8 bit
  17769. $count_points = count($patch_array[$i]['points']);
  17770. for ($j=0; $j < $count_points; ++$j) {
  17771. //each point as 16 bit
  17772. $patch_array[$i]['points'][$j] = (($patch_array[$i]['points'][$j] - $coords_min) / ($coords_max - $coords_min)) * $bpcd;
  17773. if ($patch_array[$i]['points'][$j] < 0) {
  17774. $patch_array[$i]['points'][$j] = 0;
  17775. }
  17776. if ($patch_array[$i]['points'][$j] > $bpcd) {
  17777. $patch_array[$i]['points'][$j] = $bpcd;
  17778. }
  17779. $this->gradients[$n]['stream'] .= chr(floor($patch_array[$i]['points'][$j] / 256));
  17780. $this->gradients[$n]['stream'] .= chr(floor($patch_array[$i]['points'][$j] % 256));
  17781. }
  17782. $count_cols = count($patch_array[$i]['colors']);
  17783. for ($j=0; $j < $count_cols; ++$j) {
  17784. //each color component as 8 bit
  17785. $this->gradients[$n]['stream'] .= chr($patch_array[$i]['colors'][$j]['r']);
  17786. $this->gradients[$n]['stream'] .= chr($patch_array[$i]['colors'][$j]['g']);
  17787. $this->gradients[$n]['stream'] .= chr($patch_array[$i]['colors'][$j]['b']);
  17788. }
  17789. }
  17790. //paint the gradient
  17791. $this->_out('/Sh'.$n.' sh');
  17792. //restore previous Graphic State
  17793. $this->_out('Q');
  17794. if ($this->inxobj) {
  17795. // we are inside an XObject template
  17796. $this->xobjects[$this->xobjid]['gradients'][$n] = $this->gradients[$n];
  17797. }
  17798. }
  17799. /**
  17800. * Set a rectangular clipping area.
  17801. * @param $x (float) abscissa of the top left corner of the rectangle (or top right corner for RTL mode).
  17802. * @param $y (float) ordinate of the top left corner of the rectangle.
  17803. * @param $w (float) width of the rectangle.
  17804. * @param $h (float) height of the rectangle.
  17805. * @author Andreas Würmser, Nicola Asuni
  17806. * @since 3.1.000 (2008-06-09)
  17807. * @protected
  17808. */
  17809. protected function Clip($x, $y, $w, $h) {
  17810. if ($this->rtl) {
  17811. $x = $this->w - $x - $w;
  17812. }
  17813. //save current Graphic State
  17814. $s = 'q';
  17815. //set clipping area
  17816. $s .= sprintf(' %.2F %.2F %.2F %.2F re W n', $x*$this->k, ($this->h-$y)*$this->k, $w*$this->k, -$h*$this->k);
  17817. //set up transformation matrix for gradient
  17818. $s .= sprintf(' %.3F 0 0 %.3F %.3F %.3F cm', $w*$this->k, $h*$this->k, $x*$this->k, ($this->h-($y+$h))*$this->k);
  17819. $this->_out($s);
  17820. }
  17821. /**
  17822. * Output gradient.
  17823. * @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)
  17824. * @param $coords (array) array of coordinates.
  17825. * @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).
  17826. * @param $background (array) An array of colour components appropriate to the colour space, specifying a single background colour value.
  17827. * @param $antialias (boolean) A flag indicating whether to filter the shading function to prevent aliasing artifacts.
  17828. * @author Nicola Asuni
  17829. * @since 3.1.000 (2008-06-09)
  17830. * @public
  17831. */
  17832. public function Gradient($type, $coords, $stops, $background=array(), $antialias=false) {
  17833. if ($this->pdfa_mode) {
  17834. return;
  17835. }
  17836. $n = count($this->gradients) + 1;
  17837. $this->gradients[$n] = array();
  17838. $this->gradients[$n]['type'] = $type;
  17839. $this->gradients[$n]['coords'] = $coords;
  17840. $this->gradients[$n]['antialias'] = $antialias;
  17841. $this->gradients[$n]['colors'] = array();
  17842. $this->gradients[$n]['transparency'] = false;
  17843. // color space
  17844. $numcolspace = count($stops[0]['color']);
  17845. $bcolor = array_values($background);
  17846. switch($numcolspace) {
  17847. case 4: { // CMYK
  17848. $this->gradients[$n]['colspace'] = 'DeviceCMYK';
  17849. if (!empty($background)) {
  17850. $this->gradients[$n]['background'] = sprintf('%.3F %.3F %.3F %.3F', $bcolor[0]/100, $bcolor[1]/100, $bcolor[2]/100, $bcolor[3]/100);
  17851. }
  17852. break;
  17853. }
  17854. case 3: { // RGB
  17855. $this->gradients[$n]['colspace'] = 'DeviceRGB';
  17856. if (!empty($background)) {
  17857. $this->gradients[$n]['background'] = sprintf('%.3F %.3F %.3F', $bcolor[0]/255, $bcolor[1]/255, $bcolor[2]/255);
  17858. }
  17859. break;
  17860. }
  17861. case 1: { // Gray scale
  17862. $this->gradients[$n]['colspace'] = 'DeviceGray';
  17863. if (!empty($background)) {
  17864. $this->gradients[$n]['background'] = sprintf('%.3F', $bcolor[0]/255);
  17865. }
  17866. break;
  17867. }
  17868. }
  17869. $num_stops = count($stops);
  17870. $last_stop_id = $num_stops - 1;
  17871. foreach ($stops as $key => $stop) {
  17872. $this->gradients[$n]['colors'][$key] = array();
  17873. // offset represents a location along the gradient vector
  17874. if (isset($stop['offset'])) {
  17875. $this->gradients[$n]['colors'][$key]['offset'] = $stop['offset'];
  17876. } else {
  17877. if ($key == 0) {
  17878. $this->gradients[$n]['colors'][$key]['offset'] = 0;
  17879. } elseif ($key == $last_stop_id) {
  17880. $this->gradients[$n]['colors'][$key]['offset'] = 1;
  17881. } else {
  17882. $offsetstep = (1 - $this->gradients[$n]['colors'][($key - 1)]['offset']) / ($num_stops - $key);
  17883. $this->gradients[$n]['colors'][$key]['offset'] = $this->gradients[$n]['colors'][($key - 1)]['offset'] + $offsetstep;
  17884. }
  17885. }
  17886. if (isset($stop['opacity'])) {
  17887. $this->gradients[$n]['colors'][$key]['opacity'] = $stop['opacity'];
  17888. if ((!$this->pdfa_mode) AND ($stop['opacity'] < 1)) {
  17889. $this->gradients[$n]['transparency'] = true;
  17890. }
  17891. } else {
  17892. $this->gradients[$n]['colors'][$key]['opacity'] = 1;
  17893. }
  17894. // exponent for the exponential interpolation function
  17895. if (isset($stop['exponent'])) {
  17896. $this->gradients[$n]['colors'][$key]['exponent'] = $stop['exponent'];
  17897. } else {
  17898. $this->gradients[$n]['colors'][$key]['exponent'] = 1;
  17899. }
  17900. // set colors
  17901. $color = array_values($stop['color']);
  17902. switch($numcolspace) {
  17903. case 4: { // CMYK
  17904. $this->gradients[$n]['colors'][$key]['color'] = sprintf('%.3F %.3F %.3F %.3F', $color[0]/100, $color[1]/100, $color[2]/100, $color[3]/100);
  17905. break;
  17906. }
  17907. case 3: { // RGB
  17908. $this->gradients[$n]['colors'][$key]['color'] = sprintf('%.3F %.3F %.3F', $color[0]/255, $color[1]/255, $color[2]/255);
  17909. break;
  17910. }
  17911. case 1: { // Gray scale
  17912. $this->gradients[$n]['colors'][$key]['color'] = sprintf('%.3F', $color[0]/255);
  17913. break;
  17914. }
  17915. }
  17916. }
  17917. if ($this->gradients[$n]['transparency']) {
  17918. // paint luminosity gradient
  17919. $this->_out('/TGS'.$n.' gs');
  17920. }
  17921. //paint the gradient
  17922. $this->_out('/Sh'.$n.' sh');
  17923. //restore previous Graphic State
  17924. $this->_out('Q');
  17925. if ($this->inxobj) {
  17926. // we are inside an XObject template
  17927. $this->xobjects[$this->xobjid]['gradients'][$n] = $this->gradients[$n];
  17928. }
  17929. }
  17930. /**
  17931. * Output gradient shaders.
  17932. * @author Nicola Asuni
  17933. * @since 3.1.000 (2008-06-09)
  17934. * @protected
  17935. */
  17936. function _putshaders() {
  17937. if ($this->pdfa_mode) {
  17938. return;
  17939. }
  17940. $idt = count($this->gradients); //index for transparency gradients
  17941. foreach ($this->gradients as $id => $grad) {
  17942. if (($grad['type'] == 2) OR ($grad['type'] == 3)) {
  17943. $fc = $this->_newobj();
  17944. $out = '<<';
  17945. $out .= ' /FunctionType 3';
  17946. $out .= ' /Domain [0 1]';
  17947. $functions = '';
  17948. $bounds = '';
  17949. $encode = '';
  17950. $i = 1;
  17951. $num_cols = count($grad['colors']);
  17952. $lastcols = $num_cols - 1;
  17953. for ($i = 1; $i < $num_cols; ++$i) {
  17954. $functions .= ($fc + $i).' 0 R ';
  17955. if ($i < $lastcols) {
  17956. $bounds .= sprintf('%.3F ', $grad['colors'][$i]['offset']);
  17957. }
  17958. $encode .= '0 1 ';
  17959. }
  17960. $out .= ' /Functions ['.trim($functions).']';
  17961. $out .= ' /Bounds ['.trim($bounds).']';
  17962. $out .= ' /Encode ['.trim($encode).']';
  17963. $out .= ' >>';
  17964. $out .= "\n".'endobj';
  17965. $this->_out($out);
  17966. for ($i = 1; $i < $num_cols; ++$i) {
  17967. $this->_newobj();
  17968. $out = '<<';
  17969. $out .= ' /FunctionType 2';
  17970. $out .= ' /Domain [0 1]';
  17971. $out .= ' /C0 ['.$grad['colors'][($i - 1)]['color'].']';
  17972. $out .= ' /C1 ['.$grad['colors'][$i]['color'].']';
  17973. $out .= ' /N '.$grad['colors'][$i]['exponent'];
  17974. $out .= ' >>';
  17975. $out .= "\n".'endobj';
  17976. $this->_out($out);
  17977. }
  17978. // set transparency fuctions
  17979. if ($grad['transparency']) {
  17980. $ft = $this->_newobj();
  17981. $out = '<<';
  17982. $out .= ' /FunctionType 3';
  17983. $out .= ' /Domain [0 1]';
  17984. $functions = '';
  17985. $i = 1;
  17986. $num_cols = count($grad['colors']);
  17987. for ($i = 1; $i < $num_cols; ++$i) {
  17988. $functions .= ($ft + $i).' 0 R ';
  17989. }
  17990. $out .= ' /Functions ['.trim($functions).']';
  17991. $out .= ' /Bounds ['.trim($bounds).']';
  17992. $out .= ' /Encode ['.trim($encode).']';
  17993. $out .= ' >>';
  17994. $out .= "\n".'endobj';
  17995. $this->_out($out);
  17996. for ($i = 1; $i < $num_cols; ++$i) {
  17997. $this->_newobj();
  17998. $out = '<<';
  17999. $out .= ' /FunctionType 2';
  18000. $out .= ' /Domain [0 1]';
  18001. $out .= ' /C0 ['.$grad['colors'][($i - 1)]['opacity'].']';
  18002. $out .= ' /C1 ['.$grad['colors'][$i]['opacity'].']';
  18003. $out .= ' /N '.$grad['colors'][$i]['exponent'];
  18004. $out .= ' >>';
  18005. $out .= "\n".'endobj';
  18006. $this->_out($out);
  18007. }
  18008. }
  18009. }
  18010. // set shading object
  18011. $this->_newobj();
  18012. $out = '<< /ShadingType '.$grad['type'];
  18013. if (isset($grad['colspace'])) {
  18014. $out .= ' /ColorSpace /'.$grad['colspace'];
  18015. } else {
  18016. $out .= ' /ColorSpace /DeviceRGB';
  18017. }
  18018. if (isset($grad['background']) AND !empty($grad['background'])) {
  18019. $out .= ' /Background ['.$grad['background'].']';
  18020. }
  18021. if (isset($grad['antialias']) AND ($grad['antialias'] === true)) {
  18022. $out .= ' /AntiAlias true';
  18023. }
  18024. if ($grad['type'] == 2) {
  18025. $out .= ' '.sprintf('/Coords [%.3F %.3F %.3F %.3F]', $grad['coords'][0], $grad['coords'][1], $grad['coords'][2], $grad['coords'][3]);
  18026. $out .= ' /Domain [0 1]';
  18027. $out .= ' /Function '.$fc.' 0 R';
  18028. $out .= ' /Extend [true true]';
  18029. $out .= ' >>';
  18030. } elseif ($grad['type'] == 3) {
  18031. //x0, y0, r0, x1, y1, r1
  18032. //at this this time radius of inner circle is 0
  18033. $out .= ' '.sprintf('/Coords [%.3F %.3F 0 %.3F %.3F %.3F]', $grad['coords'][0], $grad['coords'][1], $grad['coords'][2], $grad['coords'][3], $grad['coords'][4]);
  18034. $out .= ' /Domain [0 1]';
  18035. $out .= ' /Function '.$fc.' 0 R';
  18036. $out .= ' /Extend [true true]';
  18037. $out .= ' >>';
  18038. } elseif ($grad['type'] == 6) {
  18039. $out .= ' /BitsPerCoordinate 16';
  18040. $out .= ' /BitsPerComponent 8';
  18041. $out .= ' /Decode[0 1 0 1 0 1 0 1 0 1]';
  18042. $out .= ' /BitsPerFlag 8';
  18043. $stream = $this->_getrawstream($grad['stream']);
  18044. $out .= ' /Length '.strlen($stream);
  18045. $out .= ' >>';
  18046. $out .= ' stream'."\n".$stream."\n".'endstream';
  18047. }
  18048. $out .= "\n".'endobj';
  18049. $this->_out($out);
  18050. if ($grad['transparency']) {
  18051. $shading_transparency = preg_replace('/\/ColorSpace \/[^\s]+/si', '/ColorSpace /DeviceGray', $out);
  18052. $shading_transparency = preg_replace('/\/Function [0-9]+ /si', '/Function '.$ft.' ', $shading_transparency);
  18053. }
  18054. $this->gradients[$id]['id'] = $this->n;
  18055. // set pattern object
  18056. $this->_newobj();
  18057. $out = '<< /Type /Pattern /PatternType 2';
  18058. $out .= ' /Shading '.$this->gradients[$id]['id'].' 0 R';
  18059. $out .= ' >>';
  18060. $out .= "\n".'endobj';
  18061. $this->_out($out);
  18062. $this->gradients[$id]['pattern'] = $this->n;
  18063. // set shading and pattern for transparency mask
  18064. if ($grad['transparency']) {
  18065. // luminosity pattern
  18066. $idgs = $id + $idt;
  18067. $this->_newobj();
  18068. $this->_out($shading_transparency);
  18069. $this->gradients[$idgs]['id'] = $this->n;
  18070. $this->_newobj();
  18071. $out = '<< /Type /Pattern /PatternType 2';
  18072. $out .= ' /Shading '.$this->gradients[$idgs]['id'].' 0 R';
  18073. $out .= ' >>';
  18074. $out .= "\n".'endobj';
  18075. $this->_out($out);
  18076. $this->gradients[$idgs]['pattern'] = $this->n;
  18077. // luminosity XObject
  18078. $oid = $this->_newobj();
  18079. $this->xobjects['LX'.$oid] = array('n' => $oid);
  18080. $filter = '';
  18081. $stream = 'q /a0 gs /Pattern cs /p'.$idgs.' scn 0 0 '.$this->wPt.' '.$this->hPt.' re f Q';
  18082. if ($this->compress) {
  18083. $filter = ' /Filter /FlateDecode';
  18084. $stream = gzcompress($stream);
  18085. }
  18086. $stream = $this->_getrawstream($stream);
  18087. $out = '<< /Type /XObject /Subtype /Form /FormType 1'.$filter;
  18088. $out .= ' /Length '.strlen($stream);
  18089. $rect = sprintf('%.2F %.2F', $this->wPt, $this->hPt);
  18090. $out .= ' /BBox [0 0 '.$rect.']';
  18091. $out .= ' /Group << /Type /Group /S /Transparency /CS /DeviceGray >>';
  18092. $out .= ' /Resources <<';
  18093. $out .= ' /ExtGState << /a0 << /ca 1 /CA 1 >> >>';
  18094. $out .= ' /Pattern << /p'.$idgs.' '.$this->gradients[$idgs]['pattern'].' 0 R >>';
  18095. $out .= ' >>';
  18096. $out .= ' >> ';
  18097. $out .= ' stream'."\n".$stream."\n".'endstream';
  18098. $out .= "\n".'endobj';
  18099. $this->_out($out);
  18100. // SMask
  18101. $this->_newobj();
  18102. $out = '<< /Type /Mask /S /Luminosity /G '.($this->n - 1).' 0 R >>'."\n".'endobj';
  18103. $this->_out($out);
  18104. // ExtGState
  18105. $this->_newobj();
  18106. $out = '<< /Type /ExtGState /SMask '.($this->n - 1).' 0 R /AIS false >>'."\n".'endobj';
  18107. $this->_out($out);
  18108. $this->extgstates[] = array('n' => $this->n, 'name' => 'TGS'.$id);
  18109. }
  18110. }
  18111. }
  18112. /**
  18113. * Draw the sector of a circle.
  18114. * It can be used for instance to render pie charts.
  18115. * @param $xc (float) abscissa of the center.
  18116. * @param $yc (float) ordinate of the center.
  18117. * @param $r (float) radius.
  18118. * @param $a (float) start angle (in degrees).
  18119. * @param $b (float) end angle (in degrees).
  18120. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  18121. * @param $cw: (float) indicates whether to go clockwise (default: true).
  18122. * @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.
  18123. * @author Maxime Delorme, Nicola Asuni
  18124. * @since 3.1.000 (2008-06-09)
  18125. * @public
  18126. */
  18127. public function PieSector($xc, $yc, $r, $a, $b, $style='FD', $cw=true, $o=90) {
  18128. $this->PieSectorXY($xc, $yc, $r, $r, $a, $b, $style, $cw, $o);
  18129. }
  18130. /**
  18131. * Draw the sector of an ellipse.
  18132. * It can be used for instance to render pie charts.
  18133. * @param $xc (float) abscissa of the center.
  18134. * @param $yc (float) ordinate of the center.
  18135. * @param $rx (float) the x-axis radius.
  18136. * @param $ry (float) the y-axis radius.
  18137. * @param $a (float) start angle (in degrees).
  18138. * @param $b (float) end angle (in degrees).
  18139. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  18140. * @param $cw: (float) indicates whether to go clockwise.
  18141. * @param $o: (float) origin of angles (0 for 3 o'clock, 90 for noon, 180 for 9 o'clock, 270 for 6 o'clock).
  18142. * @param $nc (integer) Number of curves used to draw a 90 degrees portion of arc.
  18143. * @author Maxime Delorme, Nicola Asuni
  18144. * @since 3.1.000 (2008-06-09)
  18145. * @public
  18146. */
  18147. public function PieSectorXY($xc, $yc, $rx, $ry, $a, $b, $style='FD', $cw=false, $o=0, $nc=2) {
  18148. if ($this->rtl) {
  18149. $xc = $this->w - $xc;
  18150. }
  18151. $op = $this->getPathPaintOperator($style);
  18152. if ($op == 'f') {
  18153. $line_style = array();
  18154. }
  18155. if ($cw) {
  18156. $d = $b;
  18157. $b = 360 - $a + $o;
  18158. $a = 360 - $d + $o;
  18159. } else {
  18160. $b += $o;
  18161. $a += $o;
  18162. }
  18163. $this->_outellipticalarc($xc, $yc, $rx, $ry, 0, $a, $b, true, $nc);
  18164. $this->_out($op);
  18165. }
  18166. /**
  18167. * Embed vector-based Adobe Illustrator (AI) or AI-compatible EPS files.
  18168. * NOTE: EPS is not yet fully implemented, use the setRasterizeVectorImages() method to enable/disable rasterization of vector images using ImageMagick library.
  18169. * Only vector drawing is supported, not text or bitmap.
  18170. * 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).
  18171. * @param $file (string) Name of the file containing the image or a '@' character followed by the EPS/AI data string.
  18172. * @param $x (float) Abscissa of the upper-left corner.
  18173. * @param $y (float) Ordinate of the upper-left corner.
  18174. * @param $w (float) Width of the image in the page. If not specified or equal to zero, it is automatically calculated.
  18175. * @param $h (float) Height of the image in the page. If not specified or equal to zero, it is automatically calculated.
  18176. * @param $link (mixed) URL or identifier returned by AddLink().
  18177. * @param $useBoundingBox (boolean) specifies whether to position the bounding box (true) or the complete canvas (false) at location (x,y). Default value is true.
  18178. * @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>
  18179. * @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>
  18180. * @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)))
  18181. * @param $fitonpage (boolean) if true the image is resized to not exceed page dimensions.
  18182. * @param $fixoutvals (boolean) if true remove values outside the bounding box.
  18183. * @author Valentin Schmidt, Nicola Asuni
  18184. * @since 3.1.000 (2008-06-09)
  18185. * @public
  18186. */
  18187. public function ImageEps($file, $x='', $y='', $w=0, $h=0, $link='', $useBoundingBox=true, $align='', $palign='', $border=0, $fitonpage=false, $fixoutvals=false) {
  18188. if ($this->rasterize_vector_images AND ($w > 0) AND ($h > 0)) {
  18189. // convert EPS to raster image using GD or ImageMagick libraries
  18190. return $this->Image($file, $x, $y, $w, $h, 'EPS', $link, $align, true, 300, $palign, false, false, $border, false, false, $fitonpage);
  18191. }
  18192. if ($x === '') {
  18193. $x = $this->x;
  18194. }
  18195. if ($y === '') {
  18196. $y = $this->y;
  18197. }
  18198. // check page for no-write regions and adapt page margins if necessary
  18199. list($x, $y) = $this->checkPageRegions($h, $x, $y);
  18200. $k = $this->k;
  18201. if ($file{0} === '@') { // image from string
  18202. $data = substr($file, 1);
  18203. } else { // EPS/AI file
  18204. $data = file_get_contents($file);
  18205. }
  18206. if ($data === false) {
  18207. $this->Error('EPS file not found: '.$file);
  18208. }
  18209. $regs = array();
  18210. // EPS/AI compatibility check (only checks files created by Adobe Illustrator!)
  18211. preg_match("/%%Creator:([^\r\n]+)/", $data, $regs); # find Creator
  18212. if (count($regs) > 1) {
  18213. $version_str = trim($regs[1]); # e.g. "Adobe Illustrator(R) 8.0"
  18214. if (strpos($version_str, 'Adobe Illustrator') !== false) {
  18215. $versexp = explode(' ', $version_str);
  18216. $version = (float)array_pop($versexp);
  18217. if ($version >= 9) {
  18218. $this->Error('This version of Adobe Illustrator file is not supported: '.$file);
  18219. }
  18220. }
  18221. }
  18222. // strip binary bytes in front of PS-header
  18223. $start = strpos($data, '%!PS-Adobe');
  18224. if ($start > 0) {
  18225. $data = substr($data, $start);
  18226. }
  18227. // find BoundingBox params
  18228. preg_match("/%%BoundingBox:([^\r\n]+)/", $data, $regs);
  18229. if (count($regs) > 1) {
  18230. list($x1, $y1, $x2, $y2) = explode(' ', trim($regs[1]));
  18231. } else {
  18232. $this->Error('No BoundingBox found in EPS/AI file: '.$file);
  18233. }
  18234. $start = strpos($data, '%%EndSetup');
  18235. if ($start === false) {
  18236. $start = strpos($data, '%%EndProlog');
  18237. }
  18238. if ($start === false) {
  18239. $start = strpos($data, '%%BoundingBox');
  18240. }
  18241. $data = substr($data, $start);
  18242. $end = strpos($data, '%%PageTrailer');
  18243. if ($end===false) {
  18244. $end = strpos($data, 'showpage');
  18245. }
  18246. if ($end) {
  18247. $data = substr($data, 0, $end);
  18248. }
  18249. // calculate image width and height on document
  18250. if (($w <= 0) AND ($h <= 0)) {
  18251. $w = ($x2 - $x1) / $k;
  18252. $h = ($y2 - $y1) / $k;
  18253. } elseif ($w <= 0) {
  18254. $w = ($x2-$x1) / $k * ($h / (($y2 - $y1) / $k));
  18255. } elseif ($h <= 0) {
  18256. $h = ($y2 - $y1) / $k * ($w / (($x2 - $x1) / $k));
  18257. }
  18258. // fit the image on available space
  18259. list($w, $h, $x, $y) = $this->fitBlock($w, $h, $x, $y, $fitonpage);
  18260. if ($this->rasterize_vector_images) {
  18261. // convert EPS to raster image using GD or ImageMagick libraries
  18262. return $this->Image($file, $x, $y, $w, $h, 'EPS', $link, $align, true, 300, $palign, false, false, $border, false, false, $fitonpage);
  18263. }
  18264. // set scaling factors
  18265. $scale_x = $w / (($x2 - $x1) / $k);
  18266. $scale_y = $h / (($y2 - $y1) / $k);
  18267. // set alignment
  18268. $this->img_rb_y = $y + $h;
  18269. // set alignment
  18270. if ($this->rtl) {
  18271. if ($palign == 'L') {
  18272. $ximg = $this->lMargin;
  18273. } elseif ($palign == 'C') {
  18274. $ximg = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
  18275. } elseif ($palign == 'R') {
  18276. $ximg = $this->w - $this->rMargin - $w;
  18277. } else {
  18278. $ximg = $x - $w;
  18279. }
  18280. $this->img_rb_x = $ximg;
  18281. } else {
  18282. if ($palign == 'L') {
  18283. $ximg = $this->lMargin;
  18284. } elseif ($palign == 'C') {
  18285. $ximg = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
  18286. } elseif ($palign == 'R') {
  18287. $ximg = $this->w - $this->rMargin - $w;
  18288. } else {
  18289. $ximg = $x;
  18290. }
  18291. $this->img_rb_x = $ximg + $w;
  18292. }
  18293. if ($useBoundingBox) {
  18294. $dx = $ximg * $k - $x1;
  18295. $dy = $y * $k - $y1;
  18296. } else {
  18297. $dx = $ximg * $k;
  18298. $dy = $y * $k;
  18299. }
  18300. // save the current graphic state
  18301. $this->_out('q'.$this->epsmarker);
  18302. // translate
  18303. $this->_out(sprintf('%.3F %.3F %.3F %.3F %.3F %.3F cm', 1, 0, 0, 1, $dx, $dy + ($this->hPt - (2 * $y * $k) - ($y2 - $y1))));
  18304. // scale
  18305. if (isset($scale_x)) {
  18306. $this->_out(sprintf('%.3F %.3F %.3F %.3F %.3F %.3F cm', $scale_x, 0, 0, $scale_y, $x1 * (1 - $scale_x), $y2 * (1 - $scale_y)));
  18307. }
  18308. // handle pc/unix/mac line endings
  18309. $lines = preg_split('/[\r\n]+/si', $data, -1, PREG_SPLIT_NO_EMPTY);
  18310. $u=0;
  18311. $cnt = count($lines);
  18312. for ($i=0; $i < $cnt; ++$i) {
  18313. $line = $lines[$i];
  18314. if (($line == '') OR ($line{0} == '%')) {
  18315. continue;
  18316. }
  18317. $len = strlen($line);
  18318. // check for spot color names
  18319. $color_name = '';
  18320. if (strcasecmp('x', substr(trim($line), -1)) == 0) {
  18321. if (preg_match('/\([^\)]*\)/', $line, $matches) > 0) {
  18322. // extract spot color name
  18323. $color_name = $matches[0];
  18324. // remove color name from string
  18325. $line = str_replace(' '.$color_name, '', $line);
  18326. // remove pharentesis from color name
  18327. $color_name = substr($color_name, 1, -1);
  18328. }
  18329. }
  18330. $chunks = explode(' ', $line);
  18331. $cmd = trim(array_pop($chunks));
  18332. // RGB
  18333. if (($cmd == 'Xa') OR ($cmd == 'XA')) {
  18334. $b = array_pop($chunks);
  18335. $g = array_pop($chunks);
  18336. $r = array_pop($chunks);
  18337. $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!
  18338. continue;
  18339. }
  18340. $skip = false;
  18341. if ($fixoutvals) {
  18342. // check for values outside the bounding box
  18343. switch ($cmd) {
  18344. case 'm':
  18345. case 'l':
  18346. case 'L': {
  18347. // skip values outside bounding box
  18348. foreach ($chunks as $key => $val) {
  18349. if ((($key % 2) == 0) AND (($val < $x1) OR ($val > $x2))) {
  18350. $skip = true;
  18351. } elseif ((($key % 2) != 0) AND (($val < $y1) OR ($val > $y2))) {
  18352. $skip = true;
  18353. }
  18354. }
  18355. }
  18356. }
  18357. }
  18358. switch ($cmd) {
  18359. case 'm':
  18360. case 'l':
  18361. case 'v':
  18362. case 'y':
  18363. case 'c':
  18364. case 'k':
  18365. case 'K':
  18366. case 'g':
  18367. case 'G':
  18368. case 's':
  18369. case 'S':
  18370. case 'J':
  18371. case 'j':
  18372. case 'w':
  18373. case 'M':
  18374. case 'd':
  18375. case 'n': {
  18376. if ($skip) {
  18377. break;
  18378. }
  18379. $this->_out($line);
  18380. break;
  18381. }
  18382. case 'x': {// custom fill color
  18383. if (empty($color_name)) {
  18384. // CMYK color
  18385. list($col_c, $col_m, $col_y, $col_k) = $chunks;
  18386. $this->_out(''.$col_c.' '.$col_m.' '.$col_y.' '.$col_k.' k');
  18387. } else {
  18388. // Spot Color (CMYK + tint)
  18389. list($col_c, $col_m, $col_y, $col_k, $col_t) = $chunks;
  18390. $this->AddSpotColor($color_name, ($col_c * 100), ($col_m * 100), ($col_y * 100), ($col_k * 100));
  18391. $color_cmd = sprintf('/CS%d cs %.3F scn', $this->spot_colors[$color_name]['i'], (1 - $col_t));
  18392. $this->_out($color_cmd);
  18393. }
  18394. break;
  18395. }
  18396. case 'X': { // custom stroke color
  18397. if (empty($color_name)) {
  18398. // CMYK color
  18399. list($col_c, $col_m, $col_y, $col_k) = $chunks;
  18400. $this->_out(''.$col_c.' '.$col_m.' '.$col_y.' '.$col_k.' K');
  18401. } else {
  18402. // Spot Color (CMYK + tint)
  18403. list($col_c, $col_m, $col_y, $col_k, $col_t) = $chunks;
  18404. $this->AddSpotColor($color_name, ($col_c * 100), ($col_m * 100), ($col_y * 100), ($col_k * 100));
  18405. $color_cmd = sprintf('/CS%d CS %.3F SCN', $this->spot_colors[$color_name]['i'], (1 - $col_t));
  18406. $this->_out($color_cmd);
  18407. }
  18408. break;
  18409. }
  18410. case 'Y':
  18411. case 'N':
  18412. case 'V':
  18413. case 'L':
  18414. case 'C': {
  18415. if ($skip) {
  18416. break;
  18417. }
  18418. $line[($len - 1)] = strtolower($cmd);
  18419. $this->_out($line);
  18420. break;
  18421. }
  18422. case 'b':
  18423. case 'B': {
  18424. $this->_out($cmd . '*');
  18425. break;
  18426. }
  18427. case 'f':
  18428. case 'F': {
  18429. if ($u > 0) {
  18430. $isU = false;
  18431. $max = min(($i + 5), $cnt);
  18432. for ($j = ($i + 1); $j < $max; ++$j) {
  18433. $isU = ($isU OR (($lines[$j] == 'U') OR ($lines[$j] == '*U')));
  18434. }
  18435. if ($isU) {
  18436. $this->_out('f*');
  18437. }
  18438. } else {
  18439. $this->_out('f*');
  18440. }
  18441. break;
  18442. }
  18443. case '*u': {
  18444. ++$u;
  18445. break;
  18446. }
  18447. case '*U': {
  18448. --$u;
  18449. break;
  18450. }
  18451. }
  18452. }
  18453. // restore previous graphic state
  18454. $this->_out($this->epsmarker.'Q');
  18455. if (!empty($border)) {
  18456. $bx = $this->x;
  18457. $by = $this->y;
  18458. $this->x = $ximg;
  18459. if ($this->rtl) {
  18460. $this->x += $w;
  18461. }
  18462. $this->y = $y;
  18463. $this->Cell($w, $h, '', $border, 0, '', 0, '', 0, true);
  18464. $this->x = $bx;
  18465. $this->y = $by;
  18466. }
  18467. if ($link) {
  18468. $this->Link($ximg, $y, $w, $h, $link, 0);
  18469. }
  18470. // set pointer to align the next text/objects
  18471. switch($align) {
  18472. case 'T':{
  18473. $this->y = $y;
  18474. $this->x = $this->img_rb_x;
  18475. break;
  18476. }
  18477. case 'M':{
  18478. $this->y = $y + round($h/2);
  18479. $this->x = $this->img_rb_x;
  18480. break;
  18481. }
  18482. case 'B':{
  18483. $this->y = $this->img_rb_y;
  18484. $this->x = $this->img_rb_x;
  18485. break;
  18486. }
  18487. case 'N':{
  18488. $this->SetY($this->img_rb_y);
  18489. break;
  18490. }
  18491. default:{
  18492. break;
  18493. }
  18494. }
  18495. $this->endlinex = $this->img_rb_x;
  18496. }
  18497. /**
  18498. * Set document barcode.
  18499. * @param $bc (string) barcode
  18500. * @public
  18501. */
  18502. public function setBarcode($bc='') {
  18503. $this->barcode = $bc;
  18504. }
  18505. /**
  18506. * Get current barcode.
  18507. * @return string
  18508. * @public
  18509. * @since 4.0.012 (2008-07-24)
  18510. */
  18511. public function getBarcode() {
  18512. return $this->barcode;
  18513. }
  18514. /**
  18515. * Print a Linear Barcode.
  18516. * @param $code (string) code to print
  18517. * @param $type (string) type of barcode (see barcodes.php for supported formats).
  18518. * @param $x (int) x position in user units (empty string = current x position)
  18519. * @param $y (int) y position in user units (empty string = current y position)
  18520. * @param $w (int) width in user units (empty string = remaining page width)
  18521. * @param $h (int) height in user units (empty string = remaining page height)
  18522. * @param $xres (float) width of the smallest bar in user units (empty string = default value = 0.4mm)
  18523. * @param $style (array) array of options:<ul>
  18524. * <li>boolean $style['border'] if true prints a border</li>
  18525. * <li>int $style['padding'] padding to leave around the barcode in user units (set to 'auto' for automatic padding)</li>
  18526. * <li>int $style['hpadding'] horizontal padding in user units (set to 'auto' for automatic padding)</li>
  18527. * <li>int $style['vpadding'] vertical padding in user units (set to 'auto' for automatic padding)</li>
  18528. * <li>array $style['fgcolor'] color array for bars and text</li>
  18529. * <li>mixed $style['bgcolor'] color array for background (set to false for transparent)</li>
  18530. * <li>boolean $style['text'] if true prints text below the barcode</li>
  18531. * <li>string $style['label'] override default label</li>
  18532. * <li>string $style['font'] font name for text</li><li>int $style['fontsize'] font size for text</li>
  18533. * <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>
  18534. * <li>string $style['position'] horizontal position of the containing barcode cell on the page: L = left margin; C = center; R = right margin.</li>
  18535. * <li>string $style['align'] horizontal position of the barcode on the containing rectangle: L = left; C = center; R = right.</li>
  18536. * <li>string $style['stretch'] if true stretch the barcode to best fit the available width, otherwise uses $xres resolution for a single bar.</li>
  18537. * <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>
  18538. * <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>
  18539. * @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>
  18540. * @author Nicola Asuni
  18541. * @since 3.1.000 (2008-06-09)
  18542. * @public
  18543. */
  18544. public function write1DBarcode($code, $type, $x='', $y='', $w='', $h='', $xres='', $style='', $align='') {
  18545. if ($this->empty_string(trim($code))) {
  18546. return;
  18547. }
  18548. require_once(dirname(__FILE__).'/barcodes.php');
  18549. // save current graphic settings
  18550. $gvars = $this->getGraphicVars();
  18551. // create new barcode object
  18552. $barcodeobj = new TCPDFBarcode($code, $type);
  18553. $arrcode = $barcodeobj->getBarcodeArray();
  18554. if ($arrcode === false) {
  18555. $this->Error('Error in 1D barcode string');
  18556. }
  18557. // set default values
  18558. if (!isset($style['position'])) {
  18559. $style['position'] = '';
  18560. } elseif ($style['position'] == 'S') {
  18561. // keep this for backward compatibility
  18562. $style['position'] = '';
  18563. $style['stretch'] = true;
  18564. }
  18565. if (!isset($style['fitwidth'])) {
  18566. if (!isset($style['stretch'])) {
  18567. $style['fitwidth'] = true;
  18568. } else {
  18569. $style['fitwidth'] = false;
  18570. }
  18571. }
  18572. if ($style['fitwidth']) {
  18573. // disable stretch
  18574. $style['stretch'] = false;
  18575. }
  18576. if (!isset($style['stretch'])) {
  18577. if (($w === '') OR ($w <= 0)) {
  18578. $style['stretch'] = false;
  18579. } else {
  18580. $style['stretch'] = true;
  18581. }
  18582. }
  18583. if (!isset($style['fgcolor'])) {
  18584. $style['fgcolor'] = array(0,0,0); // default black
  18585. }
  18586. if (!isset($style['bgcolor'])) {
  18587. $style['bgcolor'] = false; // default transparent
  18588. }
  18589. if (!isset($style['border'])) {
  18590. $style['border'] = false;
  18591. }
  18592. $fontsize = 0;
  18593. if (!isset($style['text'])) {
  18594. $style['text'] = false;
  18595. }
  18596. if ($style['text'] AND isset($style['font'])) {
  18597. if (isset($style['fontsize'])) {
  18598. $fontsize = $style['fontsize'];
  18599. }
  18600. $this->SetFont($style['font'], '', $fontsize);
  18601. }
  18602. if (!isset($style['stretchtext'])) {
  18603. $style['stretchtext'] = 4;
  18604. }
  18605. if ($x === '') {
  18606. $x = $this->x;
  18607. }
  18608. if ($y === '') {
  18609. $y = $this->y;
  18610. }
  18611. // check page for no-write regions and adapt page margins if necessary
  18612. list($x, $y) = $this->checkPageRegions($h, $x, $y);
  18613. if (($w === '') OR ($w <= 0)) {
  18614. if ($this->rtl) {
  18615. $w = $x - $this->lMargin;
  18616. } else {
  18617. $w = $this->w - $this->rMargin - $x;
  18618. }
  18619. }
  18620. // padding
  18621. if (!isset($style['padding'])) {
  18622. $padding = 0;
  18623. } elseif ($style['padding'] === 'auto') {
  18624. $padding = 10 * ($w / ($arrcode['maxw'] + 20));
  18625. } else {
  18626. $padding = floatval($style['padding']);
  18627. }
  18628. // horizontal padding
  18629. if (!isset($style['hpadding'])) {
  18630. $hpadding = $padding;
  18631. } elseif ($style['hpadding'] === 'auto') {
  18632. $hpadding = 10 * ($w / ($arrcode['maxw'] + 20));
  18633. } else {
  18634. $hpadding = floatval($style['hpadding']);
  18635. }
  18636. // vertical padding
  18637. if (!isset($style['vpadding'])) {
  18638. $vpadding = $padding;
  18639. } elseif ($style['vpadding'] === 'auto') {
  18640. $vpadding = ($hpadding / 2);
  18641. } else {
  18642. $vpadding = floatval($style['vpadding']);
  18643. }
  18644. // calculate xres (single bar width)
  18645. $max_xres = ($w - (2 * $hpadding)) / $arrcode['maxw'];
  18646. if ($style['stretch']) {
  18647. $xres = $max_xres;
  18648. } else {
  18649. if ($this->empty_string($xres)) {
  18650. $xres = (0.141 * $this->k); // default bar width = 0.4 mm
  18651. }
  18652. if ($xres > $max_xres) {
  18653. // correct xres to fit on $w
  18654. $xres = $max_xres;
  18655. }
  18656. if ((isset($style['padding']) AND ($style['padding'] === 'auto'))
  18657. OR (isset($style['hpadding']) AND ($style['hpadding'] === 'auto'))) {
  18658. $hpadding = 10 * $xres;
  18659. if (isset($style['vpadding']) AND ($style['vpadding'] === 'auto')) {
  18660. $vpadding = ($hpadding / 2);
  18661. }
  18662. }
  18663. }
  18664. if ($style['fitwidth']) {
  18665. $wold = $w;
  18666. $w = (($arrcode['maxw'] * $xres) + (2 * $hpadding));
  18667. if (isset($style['cellfitalign'])) {
  18668. switch ($style['cellfitalign']) {
  18669. case 'L': {
  18670. if ($this->rtl) {
  18671. $x -= ($wold - $w);
  18672. }
  18673. break;
  18674. }
  18675. case 'R': {
  18676. if (!$this->rtl) {
  18677. $x += ($wold - $w);
  18678. }
  18679. break;
  18680. }
  18681. case 'C': {
  18682. if ($this->rtl) {
  18683. $x -= (($wold - $w) / 2);
  18684. } else {
  18685. $x += (($wold - $w) / 2);
  18686. }
  18687. break;
  18688. }
  18689. default : {
  18690. break;
  18691. }
  18692. }
  18693. }
  18694. }
  18695. $text_height = ($this->cell_height_ratio * $fontsize / $this->k);
  18696. // height
  18697. if (($h === '') OR ($h <= 0)) {
  18698. // set default height
  18699. $h = (($arrcode['maxw'] * $xres) / 3) + (2 * $vpadding) + $text_height;
  18700. }
  18701. $barh = $h - $text_height - (2 * $vpadding);
  18702. if ($barh <=0) {
  18703. // try to reduce font or padding to fit barcode on available height
  18704. if ($text_height > $h) {
  18705. $fontsize = (($h * $this->k) / (4 * $this->cell_height_ratio));
  18706. $text_height = ($this->cell_height_ratio * $fontsize / $this->k);
  18707. $this->SetFont($style['font'], '', $fontsize);
  18708. }
  18709. if ($vpadding > 0) {
  18710. $vpadding = (($h - $text_height) / 4);
  18711. }
  18712. $barh = $h - $text_height - (2 * $vpadding);
  18713. }
  18714. // fit the barcode on available space
  18715. list($w, $h, $x, $y) = $this->fitBlock($w, $h, $x, $y, false);
  18716. // set alignment
  18717. $this->img_rb_y = $y + $h;
  18718. // set alignment
  18719. if ($this->rtl) {
  18720. if ($style['position'] == 'L') {
  18721. $xpos = $this->lMargin;
  18722. } elseif ($style['position'] == 'C') {
  18723. $xpos = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
  18724. } elseif ($style['position'] == 'R') {
  18725. $xpos = $this->w - $this->rMargin - $w;
  18726. } else {
  18727. $xpos = $x - $w;
  18728. }
  18729. $this->img_rb_x = $xpos;
  18730. } else {
  18731. if ($style['position'] == 'L') {
  18732. $xpos = $this->lMargin;
  18733. } elseif ($style['position'] == 'C') {
  18734. $xpos = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
  18735. } elseif ($style['position'] == 'R') {
  18736. $xpos = $this->w - $this->rMargin - $w;
  18737. } else {
  18738. $xpos = $x;
  18739. }
  18740. $this->img_rb_x = $xpos + $w;
  18741. }
  18742. $xpos_rect = $xpos;
  18743. if (!isset($style['align'])) {
  18744. $style['align'] = 'C';
  18745. }
  18746. switch ($style['align']) {
  18747. case 'L': {
  18748. $xpos = $xpos_rect + $hpadding;
  18749. break;
  18750. }
  18751. case 'R': {
  18752. $xpos = $xpos_rect + ($w - ($arrcode['maxw'] * $xres)) - $hpadding;
  18753. break;
  18754. }
  18755. case 'C':
  18756. default : {
  18757. $xpos = $xpos_rect + (($w - ($arrcode['maxw'] * $xres)) / 2);
  18758. break;
  18759. }
  18760. }
  18761. $xpos_text = $xpos;
  18762. // barcode is always printed in LTR direction
  18763. $tempRTL = $this->rtl;
  18764. $this->rtl = false;
  18765. // print background color
  18766. if ($style['bgcolor']) {
  18767. $this->Rect($xpos_rect, $y, $w, $h, $style['border'] ? 'DF' : 'F', '', $style['bgcolor']);
  18768. } elseif ($style['border']) {
  18769. $this->Rect($xpos_rect, $y, $w, $h, 'D');
  18770. }
  18771. // set foreground color
  18772. $this->SetDrawColorArray($style['fgcolor']);
  18773. $this->SetTextColorArray($style['fgcolor']);
  18774. // print bars
  18775. foreach ($arrcode['bcode'] as $k => $v) {
  18776. $bw = ($v['w'] * $xres);
  18777. if ($v['t']) {
  18778. // draw a vertical bar
  18779. $ypos = $y + $vpadding + ($v['p'] * $barh / $arrcode['maxh']);
  18780. $this->Rect($xpos, $ypos, $bw, ($v['h'] * $barh / $arrcode['maxh']), 'F', array(), $style['fgcolor']);
  18781. }
  18782. $xpos += $bw;
  18783. }
  18784. // print text
  18785. if ($style['text']) {
  18786. if (isset($style['label']) AND !$this->empty_string($style['label'])) {
  18787. $label = $style['label'];
  18788. } else {
  18789. $label = $code;
  18790. }
  18791. $txtwidth = ($arrcode['maxw'] * $xres);
  18792. if ($this->GetStringWidth($label) > $txtwidth) {
  18793. $style['stretchtext'] = 2;
  18794. }
  18795. // print text
  18796. $this->x = $xpos_text;
  18797. $this->y = $y + $vpadding + $barh;
  18798. $cellpadding = $this->cell_padding;
  18799. $this->SetCellPadding(0);
  18800. $this->Cell($txtwidth, '', $label, 0, 0, 'C', false, '', $style['stretchtext'], false, 'T', 'T');
  18801. $this->cell_padding = $cellpadding;
  18802. }
  18803. // restore original direction
  18804. $this->rtl = $tempRTL;
  18805. // restore previous settings
  18806. $this->setGraphicVars($gvars);
  18807. // set pointer to align the next text/objects
  18808. switch($align) {
  18809. case 'T':{
  18810. $this->y = $y;
  18811. $this->x = $this->img_rb_x;
  18812. break;
  18813. }
  18814. case 'M':{
  18815. $this->y = $y + round($h / 2);
  18816. $this->x = $this->img_rb_x;
  18817. break;
  18818. }
  18819. case 'B':{
  18820. $this->y = $this->img_rb_y;
  18821. $this->x = $this->img_rb_x;
  18822. break;
  18823. }
  18824. case 'N':{
  18825. $this->SetY($this->img_rb_y);
  18826. break;
  18827. }
  18828. default:{
  18829. break;
  18830. }
  18831. }
  18832. $this->endlinex = $this->img_rb_x;
  18833. }
  18834. /**
  18835. * This function is DEPRECATED, please use the new write1DBarcode() function.
  18836. * @param $x (int) x position in user units
  18837. * @param $y (int) y position in user units
  18838. * @param $w (int) width in user units
  18839. * @param $h (int) height position in user units
  18840. * @param $type (string) type of barcode
  18841. * @param $style (string) barcode style
  18842. * @param $font (string) font for text
  18843. * @param $xres (int) x resolution
  18844. * @param $code (string) code to print
  18845. * @deprecated deprecated since version 3.1.000 (2008-06-10)
  18846. * @public
  18847. * @see write1DBarcode()
  18848. */
  18849. public function writeBarcode($x, $y, $w, $h, $type, $style, $font, $xres, $code) {
  18850. // convert old settings for the new write1DBarcode() function.
  18851. $xres = 1 / $xres;
  18852. $newstyle = array(
  18853. 'position' => '',
  18854. 'align' => '',
  18855. 'stretch' => false,
  18856. 'fitwidth' => false,
  18857. 'cellfitalign' => '',
  18858. 'border' => false,
  18859. 'padding' => 0,
  18860. 'fgcolor' => array(0,0,0),
  18861. 'bgcolor' => false,
  18862. 'text' => true,
  18863. 'font' => $font,
  18864. 'fontsize' => 8,
  18865. 'stretchtext' => 4
  18866. );
  18867. if ($style & 1) {
  18868. $newstyle['border'] = true;
  18869. }
  18870. if ($style & 2) {
  18871. $newstyle['bgcolor'] = false;
  18872. }
  18873. if ($style & 4) {
  18874. $newstyle['position'] = 'C';
  18875. } elseif ($style & 8) {
  18876. $newstyle['position'] = 'L';
  18877. } elseif ($style & 16) {
  18878. $newstyle['position'] = 'R';
  18879. }
  18880. if ($style & 128) {
  18881. $newstyle['text'] = true;
  18882. }
  18883. if ($style & 256) {
  18884. $newstyle['stretchtext'] = 4;
  18885. }
  18886. $this->write1DBarcode($code, $type, $x, $y, $w, $h, $xres, $newstyle, '');
  18887. }
  18888. /**
  18889. * Print 2D Barcode.
  18890. * @param $code (string) code to print
  18891. * @param $type (string) type of barcode (see 2dbarcodes.php for supported formats).
  18892. * @param $x (int) x position in user units
  18893. * @param $y (int) y position in user units
  18894. * @param $w (int) width in user units
  18895. * @param $h (int) height in user units
  18896. * @param $style (array) array of options:<ul>
  18897. * <li>boolean $style['border'] if true prints a border around the barcode</li>
  18898. * <li>int $style['padding'] padding to leave around the barcode in barcode units (set to 'auto' for automatic padding)</li>
  18899. * <li>int $style['hpadding'] horizontal padding in barcode units (set to 'auto' for automatic padding)</li>
  18900. * <li>int $style['vpadding'] vertical padding in barcode units (set to 'auto' for automatic padding)</li>
  18901. * <li>int $style['module_width'] width of a single module in points</li>
  18902. * <li>int $style['module_height'] height of a single module in points</li>
  18903. * <li>array $style['fgcolor'] color array for bars and text</li>
  18904. * <li>mixed $style['bgcolor'] color array for background or false for transparent</li>
  18905. * <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>
  18906. * <li>$style['module_height'] height of a single module in points</li></ul>
  18907. * @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>
  18908. * @param $distort (boolean) if true distort the barcode to fit width and height, otherwise preserve aspect ratio
  18909. * @author Nicola Asuni
  18910. * @since 4.5.037 (2009-04-07)
  18911. * @public
  18912. */
  18913. public function write2DBarcode($code, $type, $x='', $y='', $w='', $h='', $style='', $align='', $distort=false) {
  18914. if ($this->empty_string(trim($code))) {
  18915. return;
  18916. }
  18917. require_once(dirname(__FILE__).'/2dbarcodes.php');
  18918. // save current graphic settings
  18919. $gvars = $this->getGraphicVars();
  18920. // create new barcode object
  18921. $barcodeobj = new TCPDF2DBarcode($code, $type);
  18922. $arrcode = $barcodeobj->getBarcodeArray();
  18923. if (($arrcode === false) OR empty($arrcode)) {
  18924. $this->Error('Error in 2D barcode string');
  18925. }
  18926. // set default values
  18927. if (!isset($style['position'])) {
  18928. $style['position'] = '';
  18929. }
  18930. if (!isset($style['fgcolor'])) {
  18931. $style['fgcolor'] = array(0,0,0); // default black
  18932. }
  18933. if (!isset($style['bgcolor'])) {
  18934. $style['bgcolor'] = false; // default transparent
  18935. }
  18936. if (!isset($style['border'])) {
  18937. $style['border'] = false;
  18938. }
  18939. // padding
  18940. if (!isset($style['padding'])) {
  18941. $style['padding'] = 0;
  18942. } elseif ($style['padding'] === 'auto') {
  18943. $style['padding'] = 4;
  18944. }
  18945. if (!isset($style['hpadding'])) {
  18946. $style['hpadding'] = $style['padding'];
  18947. } elseif ($style['hpadding'] === 'auto') {
  18948. $style['hpadding'] = 4;
  18949. }
  18950. if (!isset($style['vpadding'])) {
  18951. $style['vpadding'] = $style['padding'];
  18952. } elseif ($style['vpadding'] === 'auto') {
  18953. $style['vpadding'] = 4;
  18954. }
  18955. $hpad = (2 * $style['hpadding']);
  18956. $vpad = (2 * $style['vpadding']);
  18957. // cell (module) dimension
  18958. if (!isset($style['module_width'])) {
  18959. $style['module_width'] = 1; // width of a single module in points
  18960. }
  18961. if (!isset($style['module_height'])) {
  18962. $style['module_height'] = 1; // height of a single module in points
  18963. }
  18964. if ($x === '') {
  18965. $x = $this->x;
  18966. }
  18967. if ($y === '') {
  18968. $y = $this->y;
  18969. }
  18970. // check page for no-write regions and adapt page margins if necessary
  18971. list($x, $y) = $this->checkPageRegions($h, $x, $y);
  18972. // number of barcode columns and rows
  18973. $rows = $arrcode['num_rows'];
  18974. $cols = $arrcode['num_cols'];
  18975. // module width and height
  18976. $mw = $style['module_width'];
  18977. $mh = $style['module_height'];
  18978. // get max dimensions
  18979. if ($this->rtl) {
  18980. $maxw = $x - $this->lMargin;
  18981. } else {
  18982. $maxw = $this->w - $this->rMargin - $x;
  18983. }
  18984. $maxh = ($this->h - $this->tMargin - $this->bMargin);
  18985. $ratioHW = ((($rows * $mh) + $hpad) / (($cols * $mw) + $vpad));
  18986. $ratioWH = ((($cols * $mw) + $vpad) / (($rows * $mh) + $hpad));
  18987. if (!$distort) {
  18988. if (($maxw * $ratioHW) > $maxh) {
  18989. $maxw = $maxh * $ratioWH;
  18990. }
  18991. if (($maxh * $ratioWH) > $maxw) {
  18992. $maxh = $maxw * $ratioHW;
  18993. }
  18994. }
  18995. // set maximum dimesions
  18996. if ($w > $maxw) {
  18997. $w = $maxw;
  18998. }
  18999. if ($h > $maxh) {
  19000. $h = $maxh;
  19001. }
  19002. // set dimensions
  19003. if ((($w === '') OR ($w <= 0)) AND (($h === '') OR ($h <= 0))) {
  19004. $w = ($cols + $hpad) * ($mw / $this->k);
  19005. $h = ($rows + $vpad) * ($mh / $this->k);
  19006. } elseif (($w === '') OR ($w <= 0)) {
  19007. $w = $h * $ratioWH;
  19008. } elseif (($h === '') OR ($h <= 0)) {
  19009. $h = $w * $ratioHW;
  19010. }
  19011. // barcode size (excluding padding)
  19012. $bw = ($w * $cols) / ($cols + $hpad);
  19013. $bh = ($h * $rows) / ($rows + $vpad);
  19014. // dimension of single barcode cell unit
  19015. $cw = $bw / $cols;
  19016. $ch = $bh / $rows;
  19017. if (!$distort) {
  19018. if (($cw / $ch) > ($mw / $mh)) {
  19019. // correct horizontal distortion
  19020. $cw = $ch * $mw / $mh;
  19021. $bw = $cw * $cols;
  19022. $style['hpadding'] = ($w - $bw) / (2 * $cw);
  19023. } else {
  19024. // correct vertical distortion
  19025. $ch = $cw * $mh / $mw;
  19026. $bh = $ch * $rows;
  19027. $style['vpadding'] = ($h - $bh) / (2 * $ch);
  19028. }
  19029. }
  19030. // fit the barcode on available space
  19031. list($w, $h, $x, $y) = $this->fitBlock($w, $h, $x, $y, false);
  19032. // set alignment
  19033. $this->img_rb_y = $y + $h;
  19034. // set alignment
  19035. if ($this->rtl) {
  19036. if ($style['position'] == 'L') {
  19037. $xpos = $this->lMargin;
  19038. } elseif ($style['position'] == 'C') {
  19039. $xpos = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
  19040. } elseif ($style['position'] == 'R') {
  19041. $xpos = $this->w - $this->rMargin - $w;
  19042. } else {
  19043. $xpos = $x - $w;
  19044. }
  19045. $this->img_rb_x = $xpos;
  19046. } else {
  19047. if ($style['position'] == 'L') {
  19048. $xpos = $this->lMargin;
  19049. } elseif ($style['position'] == 'C') {
  19050. $xpos = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
  19051. } elseif ($style['position'] == 'R') {
  19052. $xpos = $this->w - $this->rMargin - $w;
  19053. } else {
  19054. $xpos = $x;
  19055. }
  19056. $this->img_rb_x = $xpos + $w;
  19057. }
  19058. $xstart = $xpos + ($style['hpadding'] * $cw);
  19059. $ystart = $y + ($style['vpadding'] * $ch);
  19060. // barcode is always printed in LTR direction
  19061. $tempRTL = $this->rtl;
  19062. $this->rtl = false;
  19063. // print background color
  19064. if ($style['bgcolor']) {
  19065. $this->Rect($xpos, $y, $w, $h, $style['border'] ? 'DF' : 'F', '', $style['bgcolor']);
  19066. } elseif ($style['border']) {
  19067. $this->Rect($xpos, $y, $w, $h, 'D');
  19068. }
  19069. // set foreground color
  19070. $this->SetDrawColorArray($style['fgcolor']);
  19071. // print barcode cells
  19072. // for each row
  19073. for ($r = 0; $r < $rows; ++$r) {
  19074. $xr = $xstart;
  19075. // for each column
  19076. for ($c = 0; $c < $cols; ++$c) {
  19077. if ($arrcode['bcode'][$r][$c] == 1) {
  19078. // draw a single barcode cell
  19079. $this->Rect($xr, $ystart, $cw, $ch, 'F', array(), $style['fgcolor']);
  19080. }
  19081. $xr += $cw;
  19082. }
  19083. $ystart += $ch;
  19084. }
  19085. // restore original direction
  19086. $this->rtl = $tempRTL;
  19087. // restore previous settings
  19088. $this->setGraphicVars($gvars);
  19089. // set pointer to align the next text/objects
  19090. switch($align) {
  19091. case 'T':{
  19092. $this->y = $y;
  19093. $this->x = $this->img_rb_x;
  19094. break;
  19095. }
  19096. case 'M':{
  19097. $this->y = $y + round($h/2);
  19098. $this->x = $this->img_rb_x;
  19099. break;
  19100. }
  19101. case 'B':{
  19102. $this->y = $this->img_rb_y;
  19103. $this->x = $this->img_rb_x;
  19104. break;
  19105. }
  19106. case 'N':{
  19107. $this->SetY($this->img_rb_y);
  19108. break;
  19109. }
  19110. default:{
  19111. break;
  19112. }
  19113. }
  19114. $this->endlinex = $this->img_rb_x;
  19115. }
  19116. /**
  19117. * Returns an array containing current margins:
  19118. * <ul>
  19119. <li>$ret['left'] = left margin</li>
  19120. <li>$ret['right'] = right margin</li>
  19121. <li>$ret['top'] = top margin</li>
  19122. <li>$ret['bottom'] = bottom margin</li>
  19123. <li>$ret['header'] = header margin</li>
  19124. <li>$ret['footer'] = footer margin</li>
  19125. <li>$ret['cell'] = cell padding array</li>
  19126. <li>$ret['padding_left'] = cell left padding</li>
  19127. <li>$ret['padding_top'] = cell top padding</li>
  19128. <li>$ret['padding_right'] = cell right padding</li>
  19129. <li>$ret['padding_bottom'] = cell bottom padding</li>
  19130. * </ul>
  19131. * @return array containing all margins measures
  19132. * @public
  19133. * @since 3.2.000 (2008-06-23)
  19134. */
  19135. public function getMargins() {
  19136. $ret = array(
  19137. 'left' => $this->lMargin,
  19138. 'right' => $this->rMargin,
  19139. 'top' => $this->tMargin,
  19140. 'bottom' => $this->bMargin,
  19141. 'header' => $this->header_margin,
  19142. 'footer' => $this->footer_margin,
  19143. 'cell' => $this->cell_padding,
  19144. 'padding_left' => $this->cell_padding['L'],
  19145. 'padding_top' => $this->cell_padding['T'],
  19146. 'padding_right' => $this->cell_padding['R'],
  19147. 'padding_bottom' => $this->cell_padding['B']
  19148. );
  19149. return $ret;
  19150. }
  19151. /**
  19152. * Returns an array containing original margins:
  19153. * <ul>
  19154. <li>$ret['left'] = left margin</li>
  19155. <li>$ret['right'] = right margin</li>
  19156. * </ul>
  19157. * @return array containing all margins measures
  19158. * @public
  19159. * @since 4.0.012 (2008-07-24)
  19160. */
  19161. public function getOriginalMargins() {
  19162. $ret = array(
  19163. 'left' => $this->original_lMargin,
  19164. 'right' => $this->original_rMargin
  19165. );
  19166. return $ret;
  19167. }
  19168. /**
  19169. * Returns the current font size.
  19170. * @return current font size
  19171. * @public
  19172. * @since 3.2.000 (2008-06-23)
  19173. */
  19174. public function getFontSize() {
  19175. return $this->FontSize;
  19176. }
  19177. /**
  19178. * Returns the current font size in points unit.
  19179. * @return current font size in points unit
  19180. * @public
  19181. * @since 3.2.000 (2008-06-23)
  19182. */
  19183. public function getFontSizePt() {
  19184. return $this->FontSizePt;
  19185. }
  19186. /**
  19187. * Returns the current font family name.
  19188. * @return string current font family name
  19189. * @public
  19190. * @since 4.3.008 (2008-12-05)
  19191. */
  19192. public function getFontFamily() {
  19193. return $this->FontFamily;
  19194. }
  19195. /**
  19196. * Returns the current font style.
  19197. * @return string current font style
  19198. * @public
  19199. * @since 4.3.008 (2008-12-05)
  19200. */
  19201. public function getFontStyle() {
  19202. return $this->FontStyle;
  19203. }
  19204. /**
  19205. * Cleanup HTML code (requires HTML Tidy library).
  19206. * @param $html (string) htmlcode to fix
  19207. * @param $default_css (string) CSS commands to add
  19208. * @param $tagvs (array) parameters for setHtmlVSpace method
  19209. * @param $tidy_options (array) options for tidy_parse_string function
  19210. * @return string XHTML code cleaned up
  19211. * @author Nicola Asuni
  19212. * @public
  19213. * @since 5.9.017 (2010-11-16)
  19214. * @see setHtmlVSpace()
  19215. */
  19216. public function fixHTMLCode($html, $default_css='', $tagvs='', $tidy_options='') {
  19217. // configure parameters for HTML Tidy
  19218. if ($tidy_options === '') {
  19219. $tidy_options = array (
  19220. 'clean' => 1,
  19221. 'drop-empty-paras' => 0,
  19222. 'drop-proprietary-attributes' => 1,
  19223. 'fix-backslash' => 1,
  19224. 'hide-comments' => 1,
  19225. 'join-styles' => 1,
  19226. 'lower-literals' => 1,
  19227. 'merge-divs' => 1,
  19228. 'merge-spans' => 1,
  19229. 'output-xhtml' => 1,
  19230. 'word-2000' => 1,
  19231. 'wrap' => 0,
  19232. 'output-bom' => 0,
  19233. //'char-encoding' => 'utf8',
  19234. //'input-encoding' => 'utf8',
  19235. //'output-encoding' => 'utf8'
  19236. );
  19237. }
  19238. // clean up the HTML code
  19239. $tidy = tidy_parse_string($html, $tidy_options);
  19240. // fix the HTML
  19241. $tidy->cleanRepair();
  19242. // get the CSS part
  19243. $tidy_head = tidy_get_head($tidy);
  19244. $css = $tidy_head->value;
  19245. $css = preg_replace('/<style([^>]+)>/ims', '<style>', $css);
  19246. $css = preg_replace('/<\/style>(.*)<style>/ims', "\n", $css);
  19247. $css = str_replace('/*<![CDATA[*/', '', $css);
  19248. $css = str_replace('/*]]>*/', '', $css);
  19249. preg_match('/<style>(.*)<\/style>/ims', $css, $matches);
  19250. if (isset($matches[1])) {
  19251. $css = strtolower($matches[1]);
  19252. } else {
  19253. $css = '';
  19254. }
  19255. // include default css
  19256. $css = '<style>'.$default_css.$css.'</style>';
  19257. // get the body part
  19258. $tidy_body = tidy_get_body($tidy);
  19259. $html = $tidy_body->value;
  19260. // fix some self-closing tags
  19261. $html = str_replace('<br>', '<br />', $html);
  19262. // remove some empty tag blocks
  19263. $html = preg_replace('/<div([^\>]*)><\/div>/', '', $html);
  19264. $html = preg_replace('/<p([^\>]*)><\/p>/', '', $html);
  19265. if ($tagvs !== '') {
  19266. // set vertical space for some XHTML tags
  19267. $this->setHtmlVSpace($tagvs);
  19268. }
  19269. // return the cleaned XHTML code + CSS
  19270. return $css.$html;
  19271. }
  19272. /**
  19273. * Extracts the CSS properties from a CSS string.
  19274. * @param $cssdata (string) string containing CSS definitions.
  19275. * @return An array where the keys are the CSS selectors and the values are the CSS properties.
  19276. * @author Nicola Asuni
  19277. * @since 5.1.000 (2010-05-25)
  19278. * @protected
  19279. */
  19280. protected function extractCSSproperties($cssdata) {
  19281. if (empty($cssdata)) {
  19282. return array();
  19283. }
  19284. // remove comments
  19285. $cssdata = preg_replace('/\/\*[^\*]*\*\//', '', $cssdata);
  19286. // remove newlines and multiple spaces
  19287. $cssdata = preg_replace('/[\s]+/', ' ', $cssdata);
  19288. // remove some spaces
  19289. $cssdata = preg_replace('/[\s]*([;:\{\}]{1})[\s]*/', '\\1', $cssdata);
  19290. // remove empty blocks
  19291. $cssdata = preg_replace('/([^\}\{]+)\{\}/', '', $cssdata);
  19292. // replace media type parenthesis
  19293. $cssdata = preg_replace('/@media[\s]+([^\{]*)\{/i', '@media \\1§', $cssdata);
  19294. $cssdata = preg_replace('/\}\}/si', '}§', $cssdata);
  19295. // trim string
  19296. $cssdata = trim($cssdata);
  19297. // find media blocks (all, braille, embossed, handheld, print, projection, screen, speech, tty, tv)
  19298. $cssblocks = array();
  19299. $matches = array();
  19300. if (preg_match_all('/@media[\s]+([^\§]*)§([^§]*)§/i', $cssdata, $matches) > 0) {
  19301. foreach ($matches[1] as $key => $type) {
  19302. $cssblocks[$type] = $matches[2][$key];
  19303. }
  19304. // remove media blocks
  19305. $cssdata = preg_replace('/@media[\s]+([^\§]*)§([^§]*)§/i', '', $cssdata);
  19306. }
  19307. // keep 'all' and 'print' media, other media types are discarded
  19308. if (isset($cssblocks['all']) AND !empty($cssblocks['all'])) {
  19309. $cssdata .= $cssblocks['all'];
  19310. }
  19311. if (isset($cssblocks['print']) AND !empty($cssblocks['print'])) {
  19312. $cssdata .= $cssblocks['print'];
  19313. }
  19314. // reset css blocks array
  19315. $cssblocks = array();
  19316. $matches = array();
  19317. // explode css data string into array
  19318. if (substr($cssdata, -1) == '}') {
  19319. // remove last parethesis
  19320. $cssdata = substr($cssdata, 0, -1);
  19321. }
  19322. $matches = explode('}', $cssdata);
  19323. foreach ($matches as $key => $block) {
  19324. // index 0 contains the CSS selector, index 1 contains CSS properties
  19325. $cssblocks[$key] = explode('{', $block);
  19326. if (!isset($cssblocks[$key][1])) {
  19327. // remove empty definitions
  19328. unset($cssblocks[$key]);
  19329. }
  19330. }
  19331. // split groups of selectors (comma-separated list of selectors)
  19332. foreach ($cssblocks as $key => $block) {
  19333. if (strpos($block[0], ',') > 0) {
  19334. $selectors = explode(',', $block[0]);
  19335. foreach ($selectors as $sel) {
  19336. $cssblocks[] = array(0 => trim($sel), 1 => $block[1]);
  19337. }
  19338. unset($cssblocks[$key]);
  19339. }
  19340. }
  19341. // covert array to selector => properties
  19342. $cssdata = array();
  19343. foreach ($cssblocks as $block) {
  19344. $selector = $block[0];
  19345. // calculate selector's specificity
  19346. $matches = array();
  19347. $a = 0; // the declaration is not from is a 'style' attribute
  19348. $b = intval(preg_match_all('/[\#]/', $selector, $matches)); // number of ID attributes
  19349. $c = intval(preg_match_all('/[\[\.]/', $selector, $matches)); // number of other attributes
  19350. $c += intval(preg_match_all('/[\:]link|visited|hover|active|focus|target|lang|enabled|disabled|checked|indeterminate|root|nth|first|last|only|empty|contains|not/i', $selector, $matches)); // number of pseudo-classes
  19351. $d = intval(preg_match_all('/[\>\+\~\s]{1}[a-zA-Z0-9]+/', ' '.$selector, $matches)); // number of element names
  19352. $d += intval(preg_match_all('/[\:][\:]/', $selector, $matches)); // number of pseudo-elements
  19353. $specificity = $a.$b.$c.$d;
  19354. // add specificity to the beginning of the selector
  19355. $cssdata[$specificity.' '.$selector] = $block[1];
  19356. }
  19357. // sort selectors alphabetically to account for specificity
  19358. ksort($cssdata, SORT_STRING);
  19359. // return array
  19360. return $cssdata;
  19361. }
  19362. /**
  19363. * Returns true if the CSS selector is valid for the selected HTML tag
  19364. * @param $dom (array) array of HTML tags and properties
  19365. * @param $key (int) key of the current HTML tag
  19366. * @param $selector (string) CSS selector string
  19367. * @return true if the selector is valid, false otherwise
  19368. * @protected
  19369. * @since 5.1.000 (2010-05-25)
  19370. */
  19371. protected function isValidCSSSelectorForTag($dom, $key, $selector) {
  19372. $valid = false; // value to be returned
  19373. $tag = $dom[$key]['value'];
  19374. $class = array();
  19375. if (isset($dom[$key]['attribute']['class']) AND !empty($dom[$key]['attribute']['class'])) {
  19376. $class = explode(' ', strtolower($dom[$key]['attribute']['class']));
  19377. }
  19378. $id = '';
  19379. if (isset($dom[$key]['attribute']['id']) AND !empty($dom[$key]['attribute']['id'])) {
  19380. $id = strtolower($dom[$key]['attribute']['id']);
  19381. }
  19382. $selector = preg_replace('/([\>\+\~\s]{1})([\.]{1})([^\>\+\~\s]*)/si', '\\1*.\\3', $selector);
  19383. $matches = array();
  19384. if (preg_match_all('/([\>\+\~\s]{1})([a-zA-Z0-9\*]+)([^\>\+\~\s]*)/si', $selector, $matches, PREG_PATTERN_ORDER | PREG_OFFSET_CAPTURE) > 0) {
  19385. $parentop = array_pop($matches[1]);
  19386. $operator = $parentop[0];
  19387. $offset = $parentop[1];
  19388. $lasttag = array_pop($matches[2]);
  19389. $lasttag = strtolower(trim($lasttag[0]));
  19390. if (($lasttag == '*') OR ($lasttag == $tag)) {
  19391. // the last element on selector is our tag or 'any tag'
  19392. $attrib = array_pop($matches[3]);
  19393. $attrib = strtolower(trim($attrib[0]));
  19394. if (!empty($attrib)) {
  19395. // check if matches class, id, attribute, pseudo-class or pseudo-element
  19396. switch ($attrib{0}) {
  19397. case '.': { // class
  19398. if (in_array(substr($attrib, 1), $class)) {
  19399. $valid = true;
  19400. }
  19401. break;
  19402. }
  19403. case '#': { // ID
  19404. if (substr($attrib, 1) == $id) {
  19405. $valid = true;
  19406. }
  19407. break;
  19408. }
  19409. case '[': { // attribute
  19410. $attrmatch = array();
  19411. if (preg_match('/\[([a-zA-Z0-9]*)[\s]*([\~\^\$\*\|\=]*)[\s]*["]?([^"\]]*)["]?\]/i', $attrib, $attrmatch) > 0) {
  19412. $att = strtolower($attrmatch[1]);
  19413. $val = $attrmatch[3];
  19414. if (isset($dom[$key]['attribute'][$att])) {
  19415. switch ($attrmatch[2]) {
  19416. case '=': {
  19417. if ($dom[$key]['attribute'][$att] == $val) {
  19418. $valid = true;
  19419. }
  19420. break;
  19421. }
  19422. case '~=': {
  19423. if (in_array($val, explode(' ', $dom[$key]['attribute'][$att]))) {
  19424. $valid = true;
  19425. }
  19426. break;
  19427. }
  19428. case '^=': {
  19429. if ($val == substr($dom[$key]['attribute'][$att], 0, strlen($val))) {
  19430. $valid = true;
  19431. }
  19432. break;
  19433. }
  19434. case '$=': {
  19435. if ($val == substr($dom[$key]['attribute'][$att], -strlen($val))) {
  19436. $valid = true;
  19437. }
  19438. break;
  19439. }
  19440. case '*=': {
  19441. if (strpos($dom[$key]['attribute'][$att], $val) !== false) {
  19442. $valid = true;
  19443. }
  19444. break;
  19445. }
  19446. case '|=': {
  19447. if ($dom[$key]['attribute'][$att] == $val) {
  19448. $valid = true;
  19449. } elseif (preg_match('/'.$val.'[\-]{1}/i', $dom[$key]['attribute'][$att]) > 0) {
  19450. $valid = true;
  19451. }
  19452. break;
  19453. }
  19454. default: {
  19455. $valid = true;
  19456. }
  19457. }
  19458. }
  19459. }
  19460. break;
  19461. }
  19462. case ':': { // pseudo-class or pseudo-element
  19463. if ($attrib{1} == ':') { // pseudo-element
  19464. // pseudo-elements are not supported!
  19465. // (::first-line, ::first-letter, ::before, ::after)
  19466. } else { // pseudo-class
  19467. // pseudo-classes are not supported!
  19468. // (:root, :nth-child(n), :nth-last-child(n), :nth-of-type(n), :nth-last-of-type(n), :first-child, :last-child, :first-of-type, :last-of-type, :only-child, :only-of-type, :empty, :link, :visited, :active, :hover, :focus, :target, :lang(fr), :enabled, :disabled, :checked)
  19469. }
  19470. break;
  19471. }
  19472. } // end of switch
  19473. } else {
  19474. $valid = true;
  19475. }
  19476. if ($valid AND ($offset > 0)) {
  19477. $valid = false;
  19478. // check remaining selector part
  19479. $selector = substr($selector, 0, $offset);
  19480. switch ($operator) {
  19481. case ' ': { // descendant of an element
  19482. while ($dom[$key]['parent'] > 0) {
  19483. if ($this->isValidCSSSelectorForTag($dom, $dom[$key]['parent'], $selector)) {
  19484. $valid = true;
  19485. break;
  19486. } else {
  19487. $key = $dom[$key]['parent'];
  19488. }
  19489. }
  19490. break;
  19491. }
  19492. case '>': { // child of an element
  19493. $valid = $this->isValidCSSSelectorForTag($dom, $dom[$key]['parent'], $selector);
  19494. break;
  19495. }
  19496. case '+': { // immediately preceded by an element
  19497. for ($i = ($key - 1); $i > $dom[$key]['parent']; --$i) {
  19498. if ($dom[$i]['tag'] AND $dom[$i]['opening']) {
  19499. $valid = $this->isValidCSSSelectorForTag($dom, $i, $selector);
  19500. break;
  19501. }
  19502. }
  19503. break;
  19504. }
  19505. case '~': { // preceded by an element
  19506. for ($i = ($key - 1); $i > $dom[$key]['parent']; --$i) {
  19507. if ($dom[$i]['tag'] AND $dom[$i]['opening']) {
  19508. if ($this->isValidCSSSelectorForTag($dom, $i, $selector)) {
  19509. break;
  19510. }
  19511. }
  19512. }
  19513. break;
  19514. }
  19515. }
  19516. }
  19517. }
  19518. }
  19519. return $valid;
  19520. }
  19521. /**
  19522. * Returns the styles array that apply for the selected HTML tag.
  19523. * @param $dom (array) array of HTML tags and properties
  19524. * @param $key (int) key of the current HTML tag
  19525. * @param $css (array) array of CSS properties
  19526. * @return array containing CSS properties
  19527. * @protected
  19528. * @since 5.1.000 (2010-05-25)
  19529. */
  19530. protected function getCSSdataArray($dom, $key, $css) {
  19531. $cssarray = array(); // style to be returned
  19532. // get parent CSS selectors
  19533. $selectors = array();
  19534. if (isset($dom[($dom[$key]['parent'])]['csssel'])) {
  19535. $selectors = $dom[($dom[$key]['parent'])]['csssel'];
  19536. }
  19537. // get all styles that apply
  19538. foreach($css as $selector => $style) {
  19539. $pos = strpos($selector, ' ');
  19540. // get specificity
  19541. $specificity = substr($selector, 0, $pos);
  19542. // remove specificity
  19543. $selector = substr($selector, $pos);
  19544. // check if this selector apply to current tag
  19545. if ($this->isValidCSSSelectorForTag($dom, $key, $selector)) {
  19546. if (!in_array($selector, $selectors)) {
  19547. // add style if not already added on parent selector
  19548. $cssarray[] = array('k' => $selector, 's' => $specificity, 'c' => $style);
  19549. $selectors[] = $selector;
  19550. }
  19551. }
  19552. }
  19553. if (isset($dom[$key]['attribute']['style'])) {
  19554. // attach inline style (latest properties have high priority)
  19555. $cssarray[] = array('k' => '', 's' => '1000', 'c' => $dom[$key]['attribute']['style']);
  19556. }
  19557. // order the css array to account for specificity
  19558. $cssordered = array();
  19559. foreach ($cssarray as $key => $val) {
  19560. $skey = sprintf('%04d', $key);
  19561. $cssordered[$val['s'].'_'.$skey] = $val;
  19562. }
  19563. // sort selectors alphabetically to account for specificity
  19564. ksort($cssordered, SORT_STRING);
  19565. return array($selectors, $cssordered);
  19566. }
  19567. /**
  19568. * Compact CSS data array into single string.
  19569. * @param $css (array) array of CSS properties
  19570. * @return string containing merged CSS properties
  19571. * @protected
  19572. * @since 5.9.070 (2011-04-19)
  19573. */
  19574. protected function getTagStyleFromCSSarray($css) {
  19575. $tagstyle = ''; // value to be returned
  19576. foreach ($css as $style) {
  19577. // split single css commands
  19578. $csscmds = explode(';', $style['c']);
  19579. foreach ($csscmds as $cmd) {
  19580. if (!empty($cmd)) {
  19581. $pos = strpos($cmd, ':');
  19582. if ($pos !== false) {
  19583. $cmd = substr($cmd, 0, ($pos + 1));
  19584. if (strpos($tagstyle, $cmd) !== false) {
  19585. // remove duplicate commands (last commands have high priority)
  19586. $tagstyle = preg_replace('/'.$cmd.'[^;]+/i', '', $tagstyle);
  19587. }
  19588. }
  19589. }
  19590. }
  19591. $tagstyle .= ';'.$style['c'];
  19592. }
  19593. // remove multiple semicolons
  19594. $tagstyle = preg_replace('/[;]+/', ';', $tagstyle);
  19595. return $tagstyle;
  19596. }
  19597. /**
  19598. * Returns the border width from CSS property
  19599. * @param $width (string) border width
  19600. * @return int with in user units
  19601. * @protected
  19602. * @since 5.7.000 (2010-08-02)
  19603. */
  19604. protected function getCSSBorderWidth($width) {
  19605. if ($width == 'thin') {
  19606. $width = (2 / $this->k);
  19607. } elseif ($width == 'medium') {
  19608. $width = (4 / $this->k);
  19609. } elseif ($width == 'thick') {
  19610. $width = (6 / $this->k);
  19611. } else {
  19612. $width = $this->getHTMLUnitToUnits($width, 1, 'px', false);
  19613. }
  19614. return $width;
  19615. }
  19616. /**
  19617. * Returns the border dash style from CSS property
  19618. * @param $style (string) border style to convert
  19619. * @return int sash style (return -1 in case of none or hidden border)
  19620. * @protected
  19621. * @since 5.7.000 (2010-08-02)
  19622. */
  19623. protected function getCSSBorderDashStyle($style) {
  19624. switch (strtolower($style)) {
  19625. case 'none':
  19626. case 'hidden': {
  19627. $dash = -1;
  19628. break;
  19629. }
  19630. case 'dotted': {
  19631. $dash = 1;
  19632. break;
  19633. }
  19634. case 'dashed': {
  19635. $dash = 3;
  19636. break;
  19637. }
  19638. case 'double':
  19639. case 'groove':
  19640. case 'ridge':
  19641. case 'inset':
  19642. case 'outset':
  19643. case 'solid':
  19644. default: {
  19645. $dash = 0;
  19646. break;
  19647. }
  19648. }
  19649. return $dash;
  19650. }
  19651. /**
  19652. * Returns the border style array from CSS border properties
  19653. * @param $cssborder (string) border properties
  19654. * @return array containing border properties
  19655. * @protected
  19656. * @since 5.7.000 (2010-08-02)
  19657. */
  19658. protected function getCSSBorderStyle($cssborder) {
  19659. $bprop = preg_split('/[\s]+/', trim($cssborder));
  19660. $border = array(); // value to be returned
  19661. switch (count($bprop)) {
  19662. case 3: {
  19663. $width = $bprop[0];
  19664. $style = $bprop[1];
  19665. $color = $bprop[2];
  19666. break;
  19667. }
  19668. case 2: {
  19669. $width = 'medium';
  19670. $style = $bprop[0];
  19671. $color = $bprop[1];
  19672. break;
  19673. }
  19674. case 1: {
  19675. $width = 'medium';
  19676. $style = $bprop[0];
  19677. $color = 'black';
  19678. break;
  19679. }
  19680. default: {
  19681. $width = 'medium';
  19682. $style = 'solid';
  19683. $color = 'black';
  19684. break;
  19685. }
  19686. }
  19687. if ($style == 'none') {
  19688. return array();
  19689. }
  19690. $border['cap'] = 'square';
  19691. $border['join'] = 'miter';
  19692. $border['dash'] = $this->getCSSBorderDashStyle($style);
  19693. if ($border['dash'] < 0) {
  19694. return array();
  19695. }
  19696. $border['width'] = $this->getCSSBorderWidth($width);
  19697. $border['color'] = $this->convertHTMLColorToDec($color);
  19698. return $border;
  19699. }
  19700. /**
  19701. * Get the internal Cell padding from CSS attribute.
  19702. * @param $csspadding (string) padding properties
  19703. * @param $width (float) width of the containing element
  19704. * @return array of cell paddings
  19705. * @public
  19706. * @since 5.9.000 (2010-10-04)
  19707. */
  19708. public function getCSSPadding($csspadding, $width=0) {
  19709. $padding = preg_split('/[\s]+/', trim($csspadding));
  19710. $cell_padding = array(); // value to be returned
  19711. switch (count($padding)) {
  19712. case 4: {
  19713. $cell_padding['T'] = $padding[0];
  19714. $cell_padding['R'] = $padding[1];
  19715. $cell_padding['B'] = $padding[2];
  19716. $cell_padding['L'] = $padding[3];
  19717. break;
  19718. }
  19719. case 3: {
  19720. $cell_padding['T'] = $padding[0];
  19721. $cell_padding['R'] = $padding[1];
  19722. $cell_padding['B'] = $padding[2];
  19723. $cell_padding['L'] = $padding[1];
  19724. break;
  19725. }
  19726. case 2: {
  19727. $cell_padding['T'] = $padding[0];
  19728. $cell_padding['R'] = $padding[1];
  19729. $cell_padding['B'] = $padding[0];
  19730. $cell_padding['L'] = $padding[1];
  19731. break;
  19732. }
  19733. case 1: {
  19734. $cell_padding['T'] = $padding[0];
  19735. $cell_padding['R'] = $padding[0];
  19736. $cell_padding['B'] = $padding[0];
  19737. $cell_padding['L'] = $padding[0];
  19738. break;
  19739. }
  19740. default: {
  19741. return $this->cell_padding;
  19742. }
  19743. }
  19744. if ($width == 0) {
  19745. $width = $this->w - $this->lMargin - $this->rMargin;
  19746. }
  19747. $cell_padding['T'] = $this->getHTMLUnitToUnits($cell_padding['T'], $width, 'px', false);
  19748. $cell_padding['R'] = $this->getHTMLUnitToUnits($cell_padding['R'], $width, 'px', false);
  19749. $cell_padding['B'] = $this->getHTMLUnitToUnits($cell_padding['B'], $width, 'px', false);
  19750. $cell_padding['L'] = $this->getHTMLUnitToUnits($cell_padding['L'], $width, 'px', false);
  19751. return $cell_padding;
  19752. }
  19753. /**
  19754. * Get the internal Cell margin from CSS attribute.
  19755. * @param $cssmargin (string) margin properties
  19756. * @param $width (float) width of the containing element
  19757. * @return array of cell margins
  19758. * @public
  19759. * @since 5.9.000 (2010-10-04)
  19760. */
  19761. public function getCSSMargin($cssmargin, $width=0) {
  19762. $margin = preg_split('/[\s]+/', trim($cssmargin));
  19763. $cell_margin = array(); // value to be returned
  19764. switch (count($margin)) {
  19765. case 4: {
  19766. $cell_margin['T'] = $margin[0];
  19767. $cell_margin['R'] = $margin[1];
  19768. $cell_margin['B'] = $margin[2];
  19769. $cell_margin['L'] = $margin[3];
  19770. break;
  19771. }
  19772. case 3: {
  19773. $cell_margin['T'] = $margin[0];
  19774. $cell_margin['R'] = $margin[1];
  19775. $cell_margin['B'] = $margin[2];
  19776. $cell_margin['L'] = $margin[1];
  19777. break;
  19778. }
  19779. case 2: {
  19780. $cell_margin['T'] = $margin[0];
  19781. $cell_margin['R'] = $margin[1];
  19782. $cell_margin['B'] = $margin[0];
  19783. $cell_margin['L'] = $margin[1];
  19784. break;
  19785. }
  19786. case 1: {
  19787. $cell_margin['T'] = $margin[0];
  19788. $cell_margin['R'] = $margin[0];
  19789. $cell_margin['B'] = $margin[0];
  19790. $cell_margin['L'] = $margin[0];
  19791. break;
  19792. }
  19793. default: {
  19794. return $this->cell_margin;
  19795. }
  19796. }
  19797. if ($width == 0) {
  19798. $width = $this->w - $this->lMargin - $this->rMargin;
  19799. }
  19800. $cell_margin['T'] = $this->getHTMLUnitToUnits(str_replace('auto', '0', $cell_margin['T']), $width, 'px', false);
  19801. $cell_margin['R'] = $this->getHTMLUnitToUnits(str_replace('auto', '0', $cell_margin['R']), $width, 'px', false);
  19802. $cell_margin['B'] = $this->getHTMLUnitToUnits(str_replace('auto', '0', $cell_margin['B']), $width, 'px', false);
  19803. $cell_margin['L'] = $this->getHTMLUnitToUnits(str_replace('auto', '0', $cell_margin['L']), $width, 'px', false);
  19804. return $cell_margin;
  19805. }
  19806. /**
  19807. * Get the border-spacing from CSS attribute.
  19808. * @param $cssbspace (string) border-spacing CSS properties
  19809. * @param $width (float) width of the containing element
  19810. * @return array of border spacings
  19811. * @public
  19812. * @since 5.9.010 (2010-10-27)
  19813. */
  19814. public function getCSSBorderMargin($cssbspace, $width=0) {
  19815. $space = preg_split('/[\s]+/', trim($cssbspace));
  19816. $border_spacing = array(); // value to be returned
  19817. switch (count($space)) {
  19818. case 2: {
  19819. $border_spacing['H'] = $space[0];
  19820. $border_spacing['V'] = $space[1];
  19821. break;
  19822. }
  19823. case 1: {
  19824. $border_spacing['H'] = $space[0];
  19825. $border_spacing['V'] = $space[0];
  19826. break;
  19827. }
  19828. default: {
  19829. return array('H' => 0, 'V' => 0);
  19830. }
  19831. }
  19832. if ($width == 0) {
  19833. $width = $this->w - $this->lMargin - $this->rMargin;
  19834. }
  19835. $border_spacing['H'] = $this->getHTMLUnitToUnits($border_spacing['H'], $width, 'px', false);
  19836. $border_spacing['V'] = $this->getHTMLUnitToUnits($border_spacing['V'], $width, 'px', false);
  19837. return $border_spacing;
  19838. }
  19839. /**
  19840. * Returns the letter-spacing value from CSS value
  19841. * @param $spacing (string) letter-spacing value
  19842. * @param $parent (float) font spacing (tracking/kerning) value of the parent element
  19843. * @return float quantity to increases or decreases the space between characters in a text.
  19844. * @protected
  19845. * @since 5.9.000 (2010-10-02)
  19846. */
  19847. protected function getCSSFontSpacing($spacing, $parent=0) {
  19848. $val = 0; // value to be returned
  19849. $spacing = trim($spacing);
  19850. switch ($spacing) {
  19851. case 'normal': {
  19852. $val = 0;
  19853. break;
  19854. }
  19855. case 'inherit': {
  19856. if ($parent == 'normal') {
  19857. $val = 0;
  19858. } else {
  19859. $val = $parent;
  19860. }
  19861. break;
  19862. }
  19863. default: {
  19864. $val = $this->getHTMLUnitToUnits($spacing, 0, 'px', false);
  19865. }
  19866. }
  19867. return $val;
  19868. }
  19869. /**
  19870. * Returns the percentage of font stretching from CSS value
  19871. * @param $stretch (string) stretch mode
  19872. * @param $parent (float) stretch value of the parent element
  19873. * @return float font stretching percentage
  19874. * @protected
  19875. * @since 5.9.000 (2010-10-02)
  19876. */
  19877. protected function getCSSFontStretching($stretch, $parent=100) {
  19878. $val = 100; // value to be returned
  19879. $stretch = trim($stretch);
  19880. switch ($stretch) {
  19881. case 'ultra-condensed': {
  19882. $val = 40;
  19883. break;
  19884. }
  19885. case 'extra-condensed': {
  19886. $val = 55;
  19887. break;
  19888. }
  19889. case 'condensed': {
  19890. $val = 70;
  19891. break;
  19892. }
  19893. case 'semi-condensed': {
  19894. $val = 85;
  19895. break;
  19896. }
  19897. case 'normal': {
  19898. $val = 100;
  19899. break;
  19900. }
  19901. case 'semi-expanded': {
  19902. $val = 115;
  19903. break;
  19904. }
  19905. case 'expanded': {
  19906. $val = 130;
  19907. break;
  19908. }
  19909. case 'extra-expanded': {
  19910. $val = 145;
  19911. break;
  19912. }
  19913. case 'ultra-expanded': {
  19914. $val = 160;
  19915. break;
  19916. }
  19917. case 'wider': {
  19918. $val = $parent + 10;
  19919. break;
  19920. }
  19921. case 'narrower': {
  19922. $val = $parent - 10;
  19923. break;
  19924. }
  19925. case 'inherit': {
  19926. if ($parent == 'normal') {
  19927. $val = 100;
  19928. } else {
  19929. $val = $parent;
  19930. }
  19931. break;
  19932. }
  19933. default: {
  19934. $val = $this->getHTMLUnitToUnits($stretch, 100, '%', false);
  19935. }
  19936. }
  19937. return $val;
  19938. }
  19939. /**
  19940. * Returns the HTML DOM array.
  19941. * @param $html (string) html code
  19942. * @return array
  19943. * @protected
  19944. * @since 3.2.000 (2008-06-20)
  19945. */
  19946. protected function getHtmlDomArray($html) {
  19947. // array of CSS styles ( selector => properties).
  19948. $css = array();
  19949. // get CSS array defined at previous call
  19950. $matches = array();
  19951. if (preg_match_all('/<cssarray>([^\<]*)<\/cssarray>/isU', $html, $matches) > 0) {
  19952. if (isset($matches[1][0])) {
  19953. $css = array_merge($css, unserialize($this->unhtmlentities($matches[1][0])));
  19954. }
  19955. $html = preg_replace('/<cssarray>(.*?)<\/cssarray>/isU', '', $html);
  19956. }
  19957. // extract external CSS files
  19958. $matches = array();
  19959. if (preg_match_all('/<link([^\>]*)>/isU', $html, $matches) > 0) {
  19960. foreach ($matches[1] as $key => $link) {
  19961. $type = array();
  19962. if (preg_match('/type[\s]*=[\s]*"text\/css"/', $link, $type)) {
  19963. $type = array();
  19964. preg_match('/media[\s]*=[\s]*"([^"]*)"/', $link, $type);
  19965. // get 'all' and 'print' media, other media types are discarded
  19966. // (all, braille, embossed, handheld, print, projection, screen, speech, tty, tv)
  19967. if (empty($type) OR (isset($type[1]) AND (($type[1] == 'all') OR ($type[1] == 'print')))) {
  19968. $type = array();
  19969. if (preg_match('/href[\s]*=[\s]*"([^"]*)"/', $link, $type) > 0) {
  19970. // read CSS data file
  19971. $cssdata = file_get_contents(trim($type[1]));
  19972. $css = array_merge($css, $this->extractCSSproperties($cssdata));
  19973. }
  19974. }
  19975. }
  19976. }
  19977. }
  19978. // extract style tags
  19979. $matches = array();
  19980. if (preg_match_all('/<style([^\>]*)>([^\<]*)<\/style>/isU', $html, $matches) > 0) {
  19981. foreach ($matches[1] as $key => $media) {
  19982. $type = array();
  19983. preg_match('/media[\s]*=[\s]*"([^"]*)"/', $media, $type);
  19984. // get 'all' and 'print' media, other media types are discarded
  19985. // (all, braille, embossed, handheld, print, projection, screen, speech, tty, tv)
  19986. if (empty($type) OR (isset($type[1]) AND (($type[1] == 'all') OR ($type[1] == 'print')))) {
  19987. $cssdata = $matches[2][$key];
  19988. $css = array_merge($css, $this->extractCSSproperties($cssdata));
  19989. }
  19990. }
  19991. }
  19992. // create a special tag to contain the CSS array (used for table content)
  19993. $csstagarray = '<cssarray>'.htmlentities(serialize($css)).'</cssarray>';
  19994. // remove head and style blocks
  19995. $html = preg_replace('/<head([^\>]*)>(.*?)<\/head>/siU', '', $html);
  19996. $html = preg_replace('/<style([^\>]*)>([^\<]*)<\/style>/isU', '', $html);
  19997. // define block tags
  19998. $blocktags = array('blockquote','br','dd','dl','div','dt','h1','h2','h3','h4','h5','h6','hr','li','ol','p','pre','ul','tcpdf','table','tr','td');
  19999. // define self-closing tags
  20000. $selfclosingtags = array('area','base','basefont','br','hr','input','img','link','meta');
  20001. // remove all unsupported tags (the line below lists all supported tags)
  20002. $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>');
  20003. //replace some blank characters
  20004. $html = preg_replace('/<pre/', '<xre', $html); // preserve pre tag
  20005. $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);
  20006. $html = preg_replace('@(\r\n|\r)@', "\n", $html);
  20007. $repTable = array("\t" => ' ', "\0" => ' ', "\x0B" => ' ', "\\" => "\\\\");
  20008. $html = strtr($html, $repTable);
  20009. $offset = 0;
  20010. while (($offset < strlen($html)) AND ($pos = strpos($html, '</pre>', $offset)) !== false) {
  20011. $html_a = substr($html, 0, $offset);
  20012. $html_b = substr($html, $offset, ($pos - $offset + 6));
  20013. while (preg_match("'<xre([^\>]*)>(.*?)\n(.*?)</pre>'si", $html_b)) {
  20014. // preserve newlines on <pre> tag
  20015. $html_b = preg_replace("'<xre([^\>]*)>(.*?)\n(.*?)</pre>'si", "<xre\\1>\\2<br />\\3</pre>", $html_b);
  20016. }
  20017. while (preg_match("'<xre([^\>]*)>(.*?)".$this->re_space['p']."(.*?)</pre>'".$this->re_space['m'], $html_b)) {
  20018. // preserve spaces on <pre> tag
  20019. $html_b = preg_replace("'<xre([^\>]*)>(.*?)".$this->re_space['p']."(.*?)</pre>'".$this->re_space['m'], "<xre\\1>\\2&nbsp;\\3</pre>", $html_b);
  20020. }
  20021. $html = $html_a.$html_b.substr($html, $pos + 6);
  20022. $offset = strlen($html_a.$html_b);
  20023. }
  20024. $offset = 0;
  20025. while (($offset < strlen($html)) AND ($pos = strpos($html, '</textarea>', $offset)) !== false) {
  20026. $html_a = substr($html, 0, $offset);
  20027. $html_b = substr($html, $offset, ($pos - $offset + 11));
  20028. while (preg_match("'<textarea([^\>]*)>(.*?)\n(.*?)</textarea>'si", $html_b)) {
  20029. // preserve newlines on <textarea> tag
  20030. $html_b = preg_replace("'<textarea([^\>]*)>(.*?)\n(.*?)</textarea>'si", "<textarea\\1>\\2<TBR>\\3</textarea>", $html_b);
  20031. $html_b = preg_replace("'<textarea([^\>]*)>(.*?)[\"](.*?)</textarea>'si", "<textarea\\1>\\2''\\3</textarea>", $html_b);
  20032. }
  20033. $html = $html_a.$html_b.substr($html, $pos + 11);
  20034. $offset = strlen($html_a.$html_b);
  20035. }
  20036. $html = preg_replace('/([\s]*)<option/si', '<option', $html);
  20037. $html = preg_replace('/<\/option>([\s]*)/si', '</option>', $html);
  20038. $offset = 0;
  20039. while (($offset < strlen($html)) AND ($pos = strpos($html, '</option>', $offset)) !== false) {
  20040. $html_a = substr($html, 0, $offset);
  20041. $html_b = substr($html, $offset, ($pos - $offset + 9));
  20042. while (preg_match("'<option([^\>]*)>(.*?)</option>'si", $html_b)) {
  20043. $html_b = preg_replace("'<option([\s]+)value=\"([^\"]*)\"([^\>]*)>(.*?)</option>'si", "\\2#!TaB!#\\4#!NwL!#", $html_b);
  20044. $html_b = preg_replace("'<option([^\>]*)>(.*?)</option>'si", "\\2#!NwL!#", $html_b);
  20045. }
  20046. $html = $html_a.$html_b.substr($html, $pos + 9);
  20047. $offset = strlen($html_a.$html_b);
  20048. }
  20049. if (preg_match("'</select'si", $html)) {
  20050. $html = preg_replace("'<select([^\>]*)>'si", "<select\\1 opt=\"", $html);
  20051. $html = preg_replace("'#!NwL!#</select>'si", "\" />", $html);
  20052. }
  20053. $html = str_replace("\n", ' ', $html);
  20054. // restore textarea newlines
  20055. $html = str_replace('<TBR>', "\n", $html);
  20056. // remove extra spaces from code
  20057. $html = preg_replace('/[\s]+<\/(table|tr|ul|ol|dl)>/', '</\\1>', $html);
  20058. $html = preg_replace('/'.$this->re_space['p'].'+<\/(td|th|li|dt|dd)>/'.$this->re_space['m'], '</\\1>', $html);
  20059. $html = preg_replace('/[\s]+<(tr|td|th|li|dt|dd)/', '<\\1', $html);
  20060. $html = preg_replace('/'.$this->re_space['p'].'+<(ul|ol|dl|br)/'.$this->re_space['m'], '<\\1', $html);
  20061. $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);
  20062. $html = preg_replace('/<\/(td|th)>/', '<marker style="font-size:0"/></\\1>', $html);
  20063. $html = preg_replace('/<\/table>([\s]*)<marker style="font-size:0"\/>/', '</table>', $html);
  20064. $html = preg_replace('/'.$this->re_space['p'].'+<img/'.$this->re_space['m'], chr(32).'<img', $html);
  20065. $html = preg_replace('/<img([^\>]*)>[\s]+([^\<])/xi', '<img\\1>&nbsp;\\2', $html);
  20066. $html = preg_replace('/<img([^\>]*)>/xi', '<img\\1><span><marker style="font-size:0"/></span>', $html);
  20067. $html = preg_replace('/<xre/', '<pre', $html); // restore pre tag
  20068. $html = preg_replace('/<textarea([^\>]*)>([^\<]*)<\/textarea>/xi', '<textarea\\1 value="\\2" />', $html);
  20069. $html = preg_replace('/<li([^\>]*)><\/li>/', '<li\\1>&nbsp;</li>', $html);
  20070. $html = preg_replace('/<li([^\>]*)>'.$this->re_space['p'].'*<img/'.$this->re_space['m'], '<li\\1><font size="1">&nbsp;</font><img', $html);
  20071. $html = preg_replace('/<([^\>\/]*)>[\s]/', '<\\1>&nbsp;', $html); // preserve some spaces
  20072. $html = preg_replace('/[\s]<\/([^\>]*)>/', '&nbsp;</\\1>', $html); // preserve some spaces
  20073. $html = preg_replace('/'.$this->re_space['p'].'+/'.$this->re_space['m'], chr(32), $html); // replace multiple spaces with a single space
  20074. // trim string
  20075. $html = $this->stringTrim($html);
  20076. // fix first image tag alignment
  20077. $html = preg_replace('/^<img/', '<span style="font-size:0"><br /></span> <img', $html, 1);
  20078. // pattern for generic tag
  20079. $tagpattern = '/(<[^>]+>)/';
  20080. // explodes the string
  20081. $a = preg_split($tagpattern, $html, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
  20082. // count elements
  20083. $maxel = count($a);
  20084. $elkey = 0;
  20085. $key = 0;
  20086. // create an array of elements
  20087. $dom = array();
  20088. $dom[$key] = array();
  20089. // set inheritable properties fot the first void element
  20090. // 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
  20091. $dom[$key]['tag'] = false;
  20092. $dom[$key]['block'] = false;
  20093. $dom[$key]['value'] = '';
  20094. $dom[$key]['parent'] = 0;
  20095. $dom[$key]['hide'] = false;
  20096. $dom[$key]['fontname'] = $this->FontFamily;
  20097. $dom[$key]['fontstyle'] = $this->FontStyle;
  20098. $dom[$key]['fontsize'] = $this->FontSizePt;
  20099. $dom[$key]['font-stretch'] = $this->font_stretching;
  20100. $dom[$key]['letter-spacing'] = $this->font_spacing;
  20101. $dom[$key]['stroke'] = $this->textstrokewidth;
  20102. $dom[$key]['fill'] = (($this->textrendermode % 2) == 0);
  20103. $dom[$key]['clip'] = ($this->textrendermode > 3);
  20104. $dom[$key]['line-height'] = $this->cell_height_ratio;
  20105. $dom[$key]['bgcolor'] = false;
  20106. $dom[$key]['fgcolor'] = $this->fgcolor; // color
  20107. $dom[$key]['strokecolor'] = $this->strokecolor;
  20108. $dom[$key]['align'] = '';
  20109. $dom[$key]['listtype'] = '';
  20110. $dom[$key]['text-indent'] = 0;
  20111. $dom[$key]['border'] = array();
  20112. $dom[$key]['dir'] = $this->rtl?'rtl':'ltr';
  20113. $thead = false; // true when we are inside the THEAD tag
  20114. ++$key;
  20115. $level = array();
  20116. array_push($level, 0); // root
  20117. while ($elkey < $maxel) {
  20118. $dom[$key] = array();
  20119. $element = $a[$elkey];
  20120. $dom[$key]['elkey'] = $elkey;
  20121. if (preg_match($tagpattern, $element)) {
  20122. // html tag
  20123. $element = substr($element, 1, -1);
  20124. // get tag name
  20125. preg_match('/[\/]?([a-zA-Z0-9]*)/', $element, $tag);
  20126. $tagname = strtolower($tag[1]);
  20127. // check if we are inside a table header
  20128. if ($tagname == 'thead') {
  20129. if ($element{0} == '/') {
  20130. $thead = false;
  20131. } else {
  20132. $thead = true;
  20133. }
  20134. ++$elkey;
  20135. continue;
  20136. }
  20137. $dom[$key]['tag'] = true;
  20138. $dom[$key]['value'] = $tagname;
  20139. if (in_array($dom[$key]['value'], $blocktags)) {
  20140. $dom[$key]['block'] = true;
  20141. } else {
  20142. $dom[$key]['block'] = false;
  20143. }
  20144. if ($element{0} == '/') {
  20145. // *** closing html tag
  20146. $dom[$key]['opening'] = false;
  20147. $dom[$key]['parent'] = end($level);
  20148. array_pop($level);
  20149. $dom[$key]['hide'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['hide'];
  20150. $dom[$key]['fontname'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['fontname'];
  20151. $dom[$key]['fontstyle'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['fontstyle'];
  20152. $dom[$key]['fontsize'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['fontsize'];
  20153. $dom[$key]['font-stretch'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['font-stretch'];
  20154. $dom[$key]['letter-spacing'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['letter-spacing'];
  20155. $dom[$key]['stroke'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['stroke'];
  20156. $dom[$key]['fill'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['fill'];
  20157. $dom[$key]['clip'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['clip'];
  20158. $dom[$key]['line-height'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['line-height'];
  20159. $dom[$key]['bgcolor'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['bgcolor'];
  20160. $dom[$key]['fgcolor'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['fgcolor'];
  20161. $dom[$key]['strokecolor'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['strokecolor'];
  20162. $dom[$key]['align'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['align'];
  20163. $dom[$key]['dir'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['dir'];
  20164. if (isset($dom[($dom[($dom[$key]['parent'])]['parent'])]['listtype'])) {
  20165. $dom[$key]['listtype'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['listtype'];
  20166. }
  20167. // set the number of columns in table tag
  20168. if (($dom[$key]['value'] == 'tr') AND (!isset($dom[($dom[($dom[$key]['parent'])]['parent'])]['cols']))) {
  20169. $dom[($dom[($dom[$key]['parent'])]['parent'])]['cols'] = $dom[($dom[$key]['parent'])]['cols'];
  20170. }
  20171. if (($dom[$key]['value'] == 'td') OR ($dom[$key]['value'] == 'th')) {
  20172. $dom[($dom[$key]['parent'])]['content'] = $csstagarray;
  20173. for ($i = ($dom[$key]['parent'] + 1); $i < $key; ++$i) {
  20174. $dom[($dom[$key]['parent'])]['content'] .= $a[$dom[$i]['elkey']];
  20175. }
  20176. $key = $i;
  20177. // mark nested tables
  20178. $dom[($dom[$key]['parent'])]['content'] = str_replace('<table', '<table nested="true"', $dom[($dom[$key]['parent'])]['content']);
  20179. // remove thead sections from nested tables
  20180. $dom[($dom[$key]['parent'])]['content'] = str_replace('<thead>', '', $dom[($dom[$key]['parent'])]['content']);
  20181. $dom[($dom[$key]['parent'])]['content'] = str_replace('</thead>', '', $dom[($dom[$key]['parent'])]['content']);
  20182. }
  20183. // store header rows on a new table
  20184. if (($dom[$key]['value'] == 'tr') AND ($dom[($dom[$key]['parent'])]['thead'] === true)) {
  20185. if ($this->empty_string($dom[($dom[($dom[$key]['parent'])]['parent'])]['thead'])) {
  20186. $dom[($dom[($dom[$key]['parent'])]['parent'])]['thead'] = $csstagarray.$a[$dom[($dom[($dom[$key]['parent'])]['parent'])]['elkey']];
  20187. }
  20188. for ($i = $dom[$key]['parent']; $i <= $key; ++$i) {
  20189. $dom[($dom[($dom[$key]['parent'])]['parent'])]['thead'] .= $a[$dom[$i]['elkey']];
  20190. }
  20191. if (!isset($dom[($dom[$key]['parent'])]['attribute'])) {
  20192. $dom[($dom[$key]['parent'])]['attribute'] = array();
  20193. }
  20194. // header elements must be always contained in a single page
  20195. $dom[($dom[$key]['parent'])]['attribute']['nobr'] = 'true';
  20196. }
  20197. if (($dom[$key]['value'] == 'table') AND (!$this->empty_string($dom[($dom[$key]['parent'])]['thead']))) {
  20198. // remove the nobr attributes from the table header
  20199. $dom[($dom[$key]['parent'])]['thead'] = str_replace(' nobr="true"', '', $dom[($dom[$key]['parent'])]['thead']);
  20200. $dom[($dom[$key]['parent'])]['thead'] .= '</tablehead>';
  20201. }
  20202. } else {
  20203. // *** opening or self-closing html tag
  20204. $dom[$key]['opening'] = true;
  20205. $dom[$key]['parent'] = end($level);
  20206. if ((substr($element, -1, 1) == '/') OR (in_array($dom[$key]['value'], $selfclosingtags))) {
  20207. // self-closing tag
  20208. $dom[$key]['self'] = true;
  20209. } else {
  20210. // opening tag
  20211. array_push($level, $key);
  20212. $dom[$key]['self'] = false;
  20213. }
  20214. // copy some values from parent
  20215. $parentkey = 0;
  20216. if ($key > 0) {
  20217. $parentkey = $dom[$key]['parent'];
  20218. $dom[$key]['hide'] = $dom[$parentkey]['hide'];
  20219. $dom[$key]['fontname'] = $dom[$parentkey]['fontname'];
  20220. $dom[$key]['fontstyle'] = $dom[$parentkey]['fontstyle'];
  20221. $dom[$key]['fontsize'] = $dom[$parentkey]['fontsize'];
  20222. $dom[$key]['font-stretch'] = $dom[$parentkey]['font-stretch'];
  20223. $dom[$key]['letter-spacing'] = $dom[$parentkey]['letter-spacing'];
  20224. $dom[$key]['stroke'] = $dom[$parentkey]['stroke'];
  20225. $dom[$key]['fill'] = $dom[$parentkey]['fill'];
  20226. $dom[$key]['clip'] = $dom[$parentkey]['clip'];
  20227. $dom[$key]['line-height'] = $dom[$parentkey]['line-height'];
  20228. $dom[$key]['bgcolor'] = $dom[$parentkey]['bgcolor'];
  20229. $dom[$key]['fgcolor'] = $dom[$parentkey]['fgcolor'];
  20230. $dom[$key]['strokecolor'] = $dom[$parentkey]['strokecolor'];
  20231. $dom[$key]['align'] = $dom[$parentkey]['align'];
  20232. $dom[$key]['listtype'] = $dom[$parentkey]['listtype'];
  20233. $dom[$key]['text-indent'] = $dom[$parentkey]['text-indent'];
  20234. $dom[$key]['border'] = array();
  20235. $dom[$key]['dir'] = $dom[$parentkey]['dir'];
  20236. }
  20237. // get attributes
  20238. preg_match_all('/([^=\s]*)[\s]*=[\s]*"([^"]*)"/', $element, $attr_array, PREG_PATTERN_ORDER);
  20239. $dom[$key]['attribute'] = array(); // reset attribute array
  20240. while (list($id, $name) = each($attr_array[1])) {
  20241. $dom[$key]['attribute'][strtolower($name)] = $attr_array[2][$id];
  20242. }
  20243. if (!empty($css)) {
  20244. // merge CSS style to current style
  20245. list($dom[$key]['csssel'], $dom[$key]['cssdata']) = $this->getCSSdataArray($dom, $key, $css);
  20246. $dom[$key]['attribute']['style'] = $this->getTagStyleFromCSSarray($dom[$key]['cssdata']);
  20247. }
  20248. // split style attributes
  20249. if (isset($dom[$key]['attribute']['style']) AND !empty($dom[$key]['attribute']['style'])) {
  20250. // get style attributes
  20251. preg_match_all('/([^;:\s]*):([^;]*)/', $dom[$key]['attribute']['style'], $style_array, PREG_PATTERN_ORDER);
  20252. $dom[$key]['style'] = array(); // reset style attribute array
  20253. while (list($id, $name) = each($style_array[1])) {
  20254. // in case of duplicate attribute the last replace the previous
  20255. $dom[$key]['style'][strtolower($name)] = trim($style_array[2][$id]);
  20256. }
  20257. // --- get some style attributes ---
  20258. // text direction
  20259. if (isset($dom[$key]['style']['direction'])) {
  20260. $dom[$key]['dir'] = $dom[$key]['style']['direction'];
  20261. }
  20262. // display
  20263. if (isset($dom[$key]['style']['display'])) {
  20264. $dom[$key]['hide'] = (trim(strtolower($dom[$key]['style']['display'])) == 'none');
  20265. }
  20266. // font family
  20267. if (isset($dom[$key]['style']['font-family'])) {
  20268. $dom[$key]['fontname'] = $this->getFontFamilyName($dom[$key]['style']['font-family']);
  20269. }
  20270. // list-style-type
  20271. if (isset($dom[$key]['style']['list-style-type'])) {
  20272. $dom[$key]['listtype'] = trim(strtolower($dom[$key]['style']['list-style-type']));
  20273. if ($dom[$key]['listtype'] == 'inherit') {
  20274. $dom[$key]['listtype'] = $dom[$parentkey]['listtype'];
  20275. }
  20276. }
  20277. // text-indent
  20278. if (isset($dom[$key]['style']['text-indent'])) {
  20279. $dom[$key]['text-indent'] = $this->getHTMLUnitToUnits($dom[$key]['style']['text-indent']);
  20280. if ($dom[$key]['text-indent'] == 'inherit') {
  20281. $dom[$key]['text-indent'] = $dom[$parentkey]['text-indent'];
  20282. }
  20283. }
  20284. // font size
  20285. if (isset($dom[$key]['style']['font-size'])) {
  20286. $fsize = trim($dom[$key]['style']['font-size']);
  20287. switch ($fsize) {
  20288. // absolute-size
  20289. case 'xx-small': {
  20290. $dom[$key]['fontsize'] = $dom[0]['fontsize'] - 4;
  20291. break;
  20292. }
  20293. case 'x-small': {
  20294. $dom[$key]['fontsize'] = $dom[0]['fontsize'] - 3;
  20295. break;
  20296. }
  20297. case 'small': {
  20298. $dom[$key]['fontsize'] = $dom[0]['fontsize'] - 2;
  20299. break;
  20300. }
  20301. case 'medium': {
  20302. $dom[$key]['fontsize'] = $dom[0]['fontsize'];
  20303. break;
  20304. }
  20305. case 'large': {
  20306. $dom[$key]['fontsize'] = $dom[0]['fontsize'] + 2;
  20307. break;
  20308. }
  20309. case 'x-large': {
  20310. $dom[$key]['fontsize'] = $dom[0]['fontsize'] + 4;
  20311. break;
  20312. }
  20313. case 'xx-large': {
  20314. $dom[$key]['fontsize'] = $dom[0]['fontsize'] + 6;
  20315. break;
  20316. }
  20317. // relative-size
  20318. case 'smaller': {
  20319. $dom[$key]['fontsize'] = $dom[$parentkey]['fontsize'] - 3;
  20320. break;
  20321. }
  20322. case 'larger': {
  20323. $dom[$key]['fontsize'] = $dom[$parentkey]['fontsize'] + 3;
  20324. break;
  20325. }
  20326. default: {
  20327. $dom[$key]['fontsize'] = $this->getHTMLUnitToUnits($fsize, $dom[$parentkey]['fontsize'], 'pt', true);
  20328. }
  20329. }
  20330. }
  20331. // font-stretch
  20332. if (isset($dom[$key]['style']['font-stretch'])) {
  20333. $dom[$key]['font-stretch'] = $this->getCSSFontStretching($dom[$key]['style']['font-stretch'], $dom[$parentkey]['font-stretch']);
  20334. }
  20335. // letter-spacing
  20336. if (isset($dom[$key]['style']['letter-spacing'])) {
  20337. $dom[$key]['letter-spacing'] = $this->getCSSFontSpacing($dom[$key]['style']['letter-spacing'], $dom[$parentkey]['letter-spacing']);
  20338. }
  20339. // line-height
  20340. if (isset($dom[$key]['style']['line-height'])) {
  20341. $lineheight = trim($dom[$key]['style']['line-height']);
  20342. switch ($lineheight) {
  20343. // A normal line height. This is default
  20344. case 'normal': {
  20345. $dom[$key]['line-height'] = $dom[0]['line-height'];
  20346. break;
  20347. }
  20348. default: {
  20349. if (is_numeric($lineheight)) {
  20350. $lineheight = $lineheight * 100;
  20351. }
  20352. $dom[$key]['line-height'] = $this->getHTMLUnitToUnits($lineheight, 1, '%', true);
  20353. }
  20354. }
  20355. }
  20356. // font style
  20357. if (isset($dom[$key]['style']['font-weight'])) {
  20358. if (strtolower($dom[$key]['style']['font-weight']{0}) == 'n') {
  20359. if (strpos($dom[$key]['fontstyle'], 'B') !== false) {
  20360. $dom[$key]['fontstyle'] = str_replace('B', '', $dom[$key]['fontstyle']);
  20361. }
  20362. } elseif (strtolower($dom[$key]['style']['font-weight']{0}) == 'b') {
  20363. $dom[$key]['fontstyle'] .= 'B';
  20364. }
  20365. }
  20366. if (isset($dom[$key]['style']['font-style']) AND (strtolower($dom[$key]['style']['font-style']{0}) == 'i')) {
  20367. $dom[$key]['fontstyle'] .= 'I';
  20368. }
  20369. // font color
  20370. if (isset($dom[$key]['style']['color']) AND (!$this->empty_string($dom[$key]['style']['color']))) {
  20371. $dom[$key]['fgcolor'] = $this->convertHTMLColorToDec($dom[$key]['style']['color']);
  20372. } elseif ($dom[$key]['value'] == 'a') {
  20373. $dom[$key]['fgcolor'] = $this->htmlLinkColorArray;
  20374. }
  20375. // background color
  20376. if (isset($dom[$key]['style']['background-color']) AND (!$this->empty_string($dom[$key]['style']['background-color']))) {
  20377. $dom[$key]['bgcolor'] = $this->convertHTMLColorToDec($dom[$key]['style']['background-color']);
  20378. }
  20379. // text-decoration
  20380. if (isset($dom[$key]['style']['text-decoration'])) {
  20381. $decors = explode(' ', strtolower($dom[$key]['style']['text-decoration']));
  20382. foreach ($decors as $dec) {
  20383. $dec = trim($dec);
  20384. if (!$this->empty_string($dec)) {
  20385. if ($dec{0} == 'u') {
  20386. // underline
  20387. $dom[$key]['fontstyle'] .= 'U';
  20388. } elseif ($dec{0} == 'l') {
  20389. // line-trough
  20390. $dom[$key]['fontstyle'] .= 'D';
  20391. } elseif ($dec{0} == 'o') {
  20392. // overline
  20393. $dom[$key]['fontstyle'] .= 'O';
  20394. }
  20395. }
  20396. }
  20397. } elseif ($dom[$key]['value'] == 'a') {
  20398. $dom[$key]['fontstyle'] = $this->htmlLinkFontStyle;
  20399. }
  20400. // check for width attribute
  20401. if (isset($dom[$key]['style']['width'])) {
  20402. $dom[$key]['width'] = $dom[$key]['style']['width'];
  20403. }
  20404. // check for height attribute
  20405. if (isset($dom[$key]['style']['height'])) {
  20406. $dom[$key]['height'] = $dom[$key]['style']['height'];
  20407. }
  20408. // check for text alignment
  20409. if (isset($dom[$key]['style']['text-align'])) {
  20410. $dom[$key]['align'] = strtoupper($dom[$key]['style']['text-align']{0});
  20411. }
  20412. // check for CSS border properties
  20413. if (isset($dom[$key]['style']['border'])) {
  20414. $borderstyle = $this->getCSSBorderStyle($dom[$key]['style']['border']);
  20415. if (!empty($borderstyle)) {
  20416. $dom[$key]['border']['LTRB'] = $borderstyle;
  20417. }
  20418. }
  20419. if (isset($dom[$key]['style']['border-color'])) {
  20420. $brd_colors = preg_split('/[\s]+/', trim($dom[$key]['style']['border-color']));
  20421. if (isset($brd_colors[3])) {
  20422. $dom[$key]['border']['L']['color'] = $this->convertHTMLColorToDec($brd_colors[3]);
  20423. }
  20424. if (isset($brd_colors[1])) {
  20425. $dom[$key]['border']['R']['color'] = $this->convertHTMLColorToDec($brd_colors[1]);
  20426. }
  20427. if (isset($brd_colors[0])) {
  20428. $dom[$key]['border']['T']['color'] = $this->convertHTMLColorToDec($brd_colors[0]);
  20429. }
  20430. if (isset($brd_colors[2])) {
  20431. $dom[$key]['border']['B']['color'] = $this->convertHTMLColorToDec($brd_colors[2]);
  20432. }
  20433. }
  20434. if (isset($dom[$key]['style']['border-width'])) {
  20435. $brd_widths = preg_split('/[\s]+/', trim($dom[$key]['style']['border-width']));
  20436. if (isset($brd_widths[3])) {
  20437. $dom[$key]['border']['L']['width'] = $this->getCSSBorderWidth($brd_widths[3]);
  20438. }
  20439. if (isset($brd_widths[1])) {
  20440. $dom[$key]['border']['R']['width'] = $this->getCSSBorderWidth($brd_widths[1]);
  20441. }
  20442. if (isset($brd_widths[0])) {
  20443. $dom[$key]['border']['T']['width'] = $this->getCSSBorderWidth($brd_widths[0]);
  20444. }
  20445. if (isset($brd_widths[2])) {
  20446. $dom[$key]['border']['B']['width'] = $this->getCSSBorderWidth($brd_widths[2]);
  20447. }
  20448. }
  20449. if (isset($dom[$key]['style']['border-style'])) {
  20450. $brd_styles = preg_split('/[\s]+/', trim($dom[$key]['style']['border-style']));
  20451. if (isset($brd_styles[3])) {
  20452. $dom[$key]['border']['L']['cap'] = 'square';
  20453. $dom[$key]['border']['L']['join'] = 'miter';
  20454. $dom[$key]['border']['L']['dash'] = $this->getCSSBorderDashStyle($brd_styles[3]);
  20455. if ($dom[$key]['border']['L']['dash'] < 0) {
  20456. $dom[$key]['border']['L'] = array();
  20457. }
  20458. }
  20459. if (isset($brd_styles[1])) {
  20460. $dom[$key]['border']['R']['cap'] = 'square';
  20461. $dom[$key]['border']['R']['join'] = 'miter';
  20462. $dom[$key]['border']['R']['dash'] = $this->getCSSBorderDashStyle($brd_styles[1]);
  20463. if ($dom[$key]['border']['R']['dash'] < 0) {
  20464. $dom[$key]['border']['R'] = array();
  20465. }
  20466. }
  20467. if (isset($brd_styles[0])) {
  20468. $dom[$key]['border']['T']['cap'] = 'square';
  20469. $dom[$key]['border']['T']['join'] = 'miter';
  20470. $dom[$key]['border']['T']['dash'] = $this->getCSSBorderDashStyle($brd_styles[0]);
  20471. if ($dom[$key]['border']['T']['dash'] < 0) {
  20472. $dom[$key]['border']['T'] = array();
  20473. }
  20474. }
  20475. if (isset($brd_styles[2])) {
  20476. $dom[$key]['border']['B']['cap'] = 'square';
  20477. $dom[$key]['border']['B']['join'] = 'miter';
  20478. $dom[$key]['border']['B']['dash'] = $this->getCSSBorderDashStyle($brd_styles[2]);
  20479. if ($dom[$key]['border']['B']['dash'] < 0) {
  20480. $dom[$key]['border']['B'] = array();
  20481. }
  20482. }
  20483. }
  20484. $cellside = array('L' => 'left', 'R' => 'right', 'T' => 'top', 'B' => 'bottom');
  20485. foreach ($cellside as $bsk => $bsv) {
  20486. if (isset($dom[$key]['style']['border-'.$bsv])) {
  20487. $borderstyle = $this->getCSSBorderStyle($dom[$key]['style']['border-'.$bsv]);
  20488. if (!empty($borderstyle)) {
  20489. $dom[$key]['border'][$bsk] = $borderstyle;
  20490. }
  20491. }
  20492. if (isset($dom[$key]['style']['border-'.$bsv.'-color'])) {
  20493. $dom[$key]['border'][$bsk]['color'] = $this->convertHTMLColorToDec($dom[$key]['style']['border-'.$bsv.'-color']);
  20494. }
  20495. if (isset($dom[$key]['style']['border-'.$bsv.'-width'])) {
  20496. $dom[$key]['border'][$bsk]['width'] = $this->getCSSBorderWidth($dom[$key]['style']['border-'.$bsv.'-width']);
  20497. }
  20498. if (isset($dom[$key]['style']['border-'.$bsv.'-style'])) {
  20499. $dom[$key]['border'][$bsk]['dash'] = $this->getCSSBorderDashStyle($dom[$key]['style']['border-'.$bsv.'-style']);
  20500. if ($dom[$key]['border'][$bsk]['dash'] < 0) {
  20501. $dom[$key]['border'][$bsk] = array();
  20502. }
  20503. }
  20504. }
  20505. // check for CSS padding properties
  20506. if (isset($dom[$key]['style']['padding'])) {
  20507. $dom[$key]['padding'] = $this->getCSSPadding($dom[$key]['style']['padding']);
  20508. } else {
  20509. $dom[$key]['padding'] = $this->cell_padding;
  20510. }
  20511. foreach ($cellside as $psk => $psv) {
  20512. if (isset($dom[$key]['style']['padding-'.$psv])) {
  20513. $dom[$key]['padding'][$psk] = $this->getHTMLUnitToUnits($dom[$key]['style']['padding-'.$psv], 0, 'px', false);
  20514. }
  20515. }
  20516. // check for CSS margin properties
  20517. if (isset($dom[$key]['style']['margin'])) {
  20518. $dom[$key]['margin'] = $this->getCSSMargin($dom[$key]['style']['margin']);
  20519. } else {
  20520. $dom[$key]['margin'] = $this->cell_margin;
  20521. }
  20522. foreach ($cellside as $psk => $psv) {
  20523. if (isset($dom[$key]['style']['margin-'.$psv])) {
  20524. $dom[$key]['margin'][$psk] = $this->getHTMLUnitToUnits(str_replace('auto', '0', $dom[$key]['style']['margin-'.$psv]), 0, 'px', false);
  20525. }
  20526. }
  20527. // check for CSS border-spacing properties
  20528. if (isset($dom[$key]['style']['border-spacing'])) {
  20529. $dom[$key]['border-spacing'] = $this->getCSSBorderMargin($dom[$key]['style']['border-spacing']);
  20530. }
  20531. // page-break-inside
  20532. if (isset($dom[$key]['style']['page-break-inside']) AND ($dom[$key]['style']['page-break-inside'] == 'avoid')) {
  20533. $dom[$key]['attribute']['nobr'] = 'true';
  20534. }
  20535. // page-break-before
  20536. if (isset($dom[$key]['style']['page-break-before'])) {
  20537. if ($dom[$key]['style']['page-break-before'] == 'always') {
  20538. $dom[$key]['attribute']['pagebreak'] = 'true';
  20539. } elseif ($dom[$key]['style']['page-break-before'] == 'left') {
  20540. $dom[$key]['attribute']['pagebreak'] = 'left';
  20541. } elseif ($dom[$key]['style']['page-break-before'] == 'right') {
  20542. $dom[$key]['attribute']['pagebreak'] = 'right';
  20543. }
  20544. }
  20545. // page-break-after
  20546. if (isset($dom[$key]['style']['page-break-after'])) {
  20547. if ($dom[$key]['style']['page-break-after'] == 'always') {
  20548. $dom[$key]['attribute']['pagebreakafter'] = 'true';
  20549. } elseif ($dom[$key]['style']['page-break-after'] == 'left') {
  20550. $dom[$key]['attribute']['pagebreakafter'] = 'left';
  20551. } elseif ($dom[$key]['style']['page-break-after'] == 'right') {
  20552. $dom[$key]['attribute']['pagebreakafter'] = 'right';
  20553. }
  20554. }
  20555. }
  20556. if (isset($dom[$key]['attribute']['display'])) {
  20557. $dom[$key]['hide'] = (trim(strtolower($dom[$key]['attribute']['display'])) == 'none');
  20558. }
  20559. if (isset($dom[$key]['attribute']['border']) AND ($dom[$key]['attribute']['border'] != 0)) {
  20560. $borderstyle = $this->getCSSBorderStyle($dom[$key]['attribute']['border'].' solid black');
  20561. if (!empty($borderstyle)) {
  20562. $dom[$key]['border']['LTRB'] = $borderstyle;
  20563. }
  20564. }
  20565. // check for font tag
  20566. if ($dom[$key]['value'] == 'font') {
  20567. // font family
  20568. if (isset($dom[$key]['attribute']['face'])) {
  20569. $dom[$key]['fontname'] = $this->getFontFamilyName($dom[$key]['attribute']['face']);
  20570. }
  20571. // font size
  20572. if (isset($dom[$key]['attribute']['size'])) {
  20573. if ($key > 0) {
  20574. if ($dom[$key]['attribute']['size']{0} == '+') {
  20575. $dom[$key]['fontsize'] = $dom[($dom[$key]['parent'])]['fontsize'] + intval(substr($dom[$key]['attribute']['size'], 1));
  20576. } elseif ($dom[$key]['attribute']['size']{0} == '-') {
  20577. $dom[$key]['fontsize'] = $dom[($dom[$key]['parent'])]['fontsize'] - intval(substr($dom[$key]['attribute']['size'], 1));
  20578. } else {
  20579. $dom[$key]['fontsize'] = intval($dom[$key]['attribute']['size']);
  20580. }
  20581. } else {
  20582. $dom[$key]['fontsize'] = intval($dom[$key]['attribute']['size']);
  20583. }
  20584. }
  20585. }
  20586. // force natural alignment for lists
  20587. if ((($dom[$key]['value'] == 'ul') OR ($dom[$key]['value'] == 'ol') OR ($dom[$key]['value'] == 'dl'))
  20588. AND (!isset($dom[$key]['align']) OR $this->empty_string($dom[$key]['align']) OR ($dom[$key]['align'] != 'J'))) {
  20589. if ($this->rtl) {
  20590. $dom[$key]['align'] = 'R';
  20591. } else {
  20592. $dom[$key]['align'] = 'L';
  20593. }
  20594. }
  20595. if (($dom[$key]['value'] == 'small') OR ($dom[$key]['value'] == 'sup') OR ($dom[$key]['value'] == 'sub')) {
  20596. if (!isset($dom[$key]['attribute']['size']) AND !isset($dom[$key]['style']['font-size'])) {
  20597. $dom[$key]['fontsize'] = $dom[$key]['fontsize'] * K_SMALL_RATIO;
  20598. }
  20599. }
  20600. if (($dom[$key]['value'] == 'strong') OR ($dom[$key]['value'] == 'b')) {
  20601. $dom[$key]['fontstyle'] .= 'B';
  20602. }
  20603. if (($dom[$key]['value'] == 'em') OR ($dom[$key]['value'] == 'i')) {
  20604. $dom[$key]['fontstyle'] .= 'I';
  20605. }
  20606. if ($dom[$key]['value'] == 'u') {
  20607. $dom[$key]['fontstyle'] .= 'U';
  20608. }
  20609. if (($dom[$key]['value'] == 'del') OR ($dom[$key]['value'] == 's') OR ($dom[$key]['value'] == 'strike')) {
  20610. $dom[$key]['fontstyle'] .= 'D';
  20611. }
  20612. if (!isset($dom[$key]['style']['text-decoration']) AND ($dom[$key]['value'] == 'a')) {
  20613. $dom[$key]['fontstyle'] = $this->htmlLinkFontStyle;
  20614. }
  20615. if (($dom[$key]['value'] == 'pre') OR ($dom[$key]['value'] == 'tt')) {
  20616. $dom[$key]['fontname'] = $this->default_monospaced_font;
  20617. }
  20618. if (($dom[$key]['value']{0} == 'h') AND (intval($dom[$key]['value']{1}) > 0) AND (intval($dom[$key]['value']{1}) < 7)) {
  20619. // headings h1, h2, h3, h4, h5, h6
  20620. if (!isset($dom[$key]['attribute']['size']) AND !isset($dom[$key]['style']['font-size'])) {
  20621. $headsize = (4 - intval($dom[$key]['value']{1})) * 2;
  20622. $dom[$key]['fontsize'] = $dom[0]['fontsize'] + $headsize;
  20623. }
  20624. if (!isset($dom[$key]['style']['font-weight'])) {
  20625. $dom[$key]['fontstyle'] .= 'B';
  20626. }
  20627. }
  20628. if (($dom[$key]['value'] == 'table')) {
  20629. $dom[$key]['rows'] = 0; // number of rows
  20630. $dom[$key]['trids'] = array(); // IDs of TR elements
  20631. $dom[$key]['thead'] = ''; // table header rows
  20632. }
  20633. if (($dom[$key]['value'] == 'tr')) {
  20634. $dom[$key]['cols'] = 0;
  20635. if ($thead) {
  20636. $dom[$key]['thead'] = true;
  20637. // rows on thead block are printed as a separate table
  20638. } else {
  20639. $dom[$key]['thead'] = false;
  20640. // store the number of rows on table element
  20641. ++$dom[($dom[$key]['parent'])]['rows'];
  20642. // store the TR elements IDs on table element
  20643. array_push($dom[($dom[$key]['parent'])]['trids'], $key);
  20644. }
  20645. }
  20646. if (($dom[$key]['value'] == 'th') OR ($dom[$key]['value'] == 'td')) {
  20647. if (isset($dom[$key]['attribute']['colspan'])) {
  20648. $colspan = intval($dom[$key]['attribute']['colspan']);
  20649. } else {
  20650. $colspan = 1;
  20651. }
  20652. $dom[$key]['attribute']['colspan'] = $colspan;
  20653. $dom[($dom[$key]['parent'])]['cols'] += $colspan;
  20654. }
  20655. // text direction
  20656. if (isset($dom[$key]['attribute']['dir'])) {
  20657. $dom[$key]['dir'] = $dom[$key]['attribute']['dir'];
  20658. }
  20659. // set foreground color attribute
  20660. if (isset($dom[$key]['attribute']['color']) AND (!$this->empty_string($dom[$key]['attribute']['color']))) {
  20661. $dom[$key]['fgcolor'] = $this->convertHTMLColorToDec($dom[$key]['attribute']['color']);
  20662. } elseif (!isset($dom[$key]['style']['color']) AND ($dom[$key]['value'] == 'a')) {
  20663. $dom[$key]['fgcolor'] = $this->htmlLinkColorArray;
  20664. }
  20665. // set background color attribute
  20666. if (isset($dom[$key]['attribute']['bgcolor']) AND (!$this->empty_string($dom[$key]['attribute']['bgcolor']))) {
  20667. $dom[$key]['bgcolor'] = $this->convertHTMLColorToDec($dom[$key]['attribute']['bgcolor']);
  20668. }
  20669. // set stroke color attribute
  20670. if (isset($dom[$key]['attribute']['strokecolor']) AND (!$this->empty_string($dom[$key]['attribute']['strokecolor']))) {
  20671. $dom[$key]['strokecolor'] = $this->convertHTMLColorToDec($dom[$key]['attribute']['strokecolor']);
  20672. }
  20673. // check for width attribute
  20674. if (isset($dom[$key]['attribute']['width'])) {
  20675. $dom[$key]['width'] = $dom[$key]['attribute']['width'];
  20676. }
  20677. // check for height attribute
  20678. if (isset($dom[$key]['attribute']['height'])) {
  20679. $dom[$key]['height'] = $dom[$key]['attribute']['height'];
  20680. }
  20681. // check for text alignment
  20682. if (isset($dom[$key]['attribute']['align']) AND (!$this->empty_string($dom[$key]['attribute']['align'])) AND ($dom[$key]['value'] !== 'img')) {
  20683. $dom[$key]['align'] = strtoupper($dom[$key]['attribute']['align']{0});
  20684. }
  20685. // check for text rendering mode (the following attributes do not exist in HTML)
  20686. if (isset($dom[$key]['attribute']['stroke'])) {
  20687. // font stroke width
  20688. $dom[$key]['stroke'] = $this->getHTMLUnitToUnits($dom[$key]['attribute']['stroke'], $dom[$key]['fontsize'], 'pt', true);
  20689. }
  20690. if (isset($dom[$key]['attribute']['fill'])) {
  20691. // font fill
  20692. if ($dom[$key]['attribute']['fill'] == 'true') {
  20693. $dom[$key]['fill'] = true;
  20694. } else {
  20695. $dom[$key]['fill'] = false;
  20696. }
  20697. }
  20698. if (isset($dom[$key]['attribute']['clip'])) {
  20699. // clipping mode
  20700. if ($dom[$key]['attribute']['clip'] == 'true') {
  20701. $dom[$key]['clip'] = true;
  20702. } else {
  20703. $dom[$key]['clip'] = false;
  20704. }
  20705. }
  20706. } // end opening tag
  20707. } else {
  20708. // text
  20709. $dom[$key]['tag'] = false;
  20710. $dom[$key]['block'] = false;
  20711. //$element = str_replace('&nbsp;', $this->unichr(160), $element);
  20712. $dom[$key]['value'] = stripslashes($this->unhtmlentities($element));
  20713. $dom[$key]['parent'] = end($level);
  20714. $dom[$key]['dir'] = $dom[$dom[$key]['parent']]['dir'];
  20715. }
  20716. ++$elkey;
  20717. ++$key;
  20718. }
  20719. return $dom;
  20720. }
  20721. /**
  20722. * Returns the string used to find spaces
  20723. * @return string
  20724. * @protected
  20725. * @author Nicola Asuni
  20726. * @since 4.8.024 (2010-01-15)
  20727. */
  20728. protected function getSpaceString() {
  20729. $spacestr = chr(32);
  20730. if ($this->isUnicodeFont()) {
  20731. $spacestr = chr(0).chr(32);
  20732. }
  20733. return $spacestr;
  20734. }
  20735. /**
  20736. * Prints a cell (rectangular area) with optional borders, background color and html text string.
  20737. * 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 />
  20738. * If automatic page breaking is enabled and the cell goes beyond the limit, a page break is done before outputting.
  20739. * @param $w (float) Cell width. If 0, the cell extends up to the right margin.
  20740. * @param $h (float) Cell minimum height. The cell extends automatically if needed.
  20741. * @param $x (float) upper-left corner X coordinate
  20742. * @param $y (float) upper-left corner Y coordinate
  20743. * @param $html (string) html text to print. Default value: empty string.
  20744. * @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)))
  20745. * @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>
  20746. Putting 1 is equivalent to putting 0 and calling Ln() just after. Default value: 0.
  20747. * @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
  20748. * @param $reseth (boolean) if true reset the last cell height (default true).
  20749. * @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>
  20750. * @param $autopadding (boolean) if true, uses internal padding and automatically adjust it to account for line width.
  20751. * @see Multicell(), writeHTML()
  20752. * @public
  20753. */
  20754. public function writeHTMLCell($w, $h, $x, $y, $html='', $border=0, $ln=0, $fill=false, $reseth=true, $align='', $autopadding=true) {
  20755. return $this->MultiCell($w, $h, $html, $border, $align, $fill, $ln, $x, $y, $reseth, 0, true, $autopadding, 0, 'T', false);
  20756. }
  20757. /**
  20758. * Allows to preserve some HTML formatting (limited support).<br />
  20759. * IMPORTANT: The HTML must be well formatted - try to clean-up it using an application like HTML-Tidy before submitting.
  20760. * 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
  20761. * @param $html (string) text to display
  20762. * @param $ln (boolean) if true add a new line after text (default = true)
  20763. * @param $fill (boolean) Indicates if the background must be painted (true) or transparent (false).
  20764. * @param $reseth (boolean) if true reset the last cell height (default false).
  20765. * @param $cell (boolean) if true add the current left (or right for RTL) padding to each Write (default false).
  20766. * @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>
  20767. * @public
  20768. */
  20769. public function writeHTML($html, $ln=true, $fill=false, $reseth=false, $cell=false, $align='') {
  20770. $gvars = $this->getGraphicVars();
  20771. // store current values
  20772. $prev_cell_margin = $this->cell_margin;
  20773. $prev_cell_padding = $this->cell_padding;
  20774. $prevPage = $this->page;
  20775. $prevlMargin = $this->lMargin;
  20776. $prevrMargin = $this->rMargin;
  20777. $curfontname = $this->FontFamily;
  20778. $curfontstyle = $this->FontStyle;
  20779. $curfontsize = $this->FontSizePt;
  20780. $curfontascent = $this->getFontAscent($curfontname, $curfontstyle, $curfontsize);
  20781. $curfontdescent = $this->getFontDescent($curfontname, $curfontstyle, $curfontsize);
  20782. $curfontstretcing = $this->font_stretching;
  20783. $curfontkerning = $this->font_spacing;
  20784. $this->newline = true;
  20785. $newline = true;
  20786. $startlinepage = $this->page;
  20787. $minstartliney = $this->y;
  20788. $maxbottomliney = 0;
  20789. $startlinex = $this->x;
  20790. $startliney = $this->y;
  20791. $yshift = 0;
  20792. $loop = 0;
  20793. $curpos = 0;
  20794. $this_method_vars = array();
  20795. $undo = false;
  20796. $fontaligned = false;
  20797. $reverse_dir = false; // true when the text direction is reversed
  20798. $this->premode = false;
  20799. if ($this->inxobj) {
  20800. // we are inside an XObject template
  20801. $pask = count($this->xobjects[$this->xobjid]['annotations']);
  20802. } elseif (isset($this->PageAnnots[$this->page])) {
  20803. $pask = count($this->PageAnnots[$this->page]);
  20804. } else {
  20805. $pask = 0;
  20806. }
  20807. if ($this->inxobj) {
  20808. // we are inside an XObject template
  20809. $startlinepos = strlen($this->xobjects[$this->xobjid]['outdata']);
  20810. } elseif (!$this->InFooter) {
  20811. if (isset($this->footerlen[$this->page])) {
  20812. $this->footerpos[$this->page] = $this->pagelen[$this->page] - $this->footerlen[$this->page];
  20813. } else {
  20814. $this->footerpos[$this->page] = $this->pagelen[$this->page];
  20815. }
  20816. $startlinepos = $this->footerpos[$this->page];
  20817. } else {
  20818. // we are inside the footer
  20819. $startlinepos = $this->pagelen[$this->page];
  20820. }
  20821. $lalign = $align;
  20822. $plalign = $align;
  20823. if ($this->rtl) {
  20824. $w = $this->x - $this->lMargin;
  20825. } else {
  20826. $w = $this->w - $this->rMargin - $this->x;
  20827. }
  20828. $w -= ($this->cell_padding['L'] + $this->cell_padding['R']);
  20829. if ($cell) {
  20830. if ($this->rtl) {
  20831. $this->x -= $this->cell_padding['R'];
  20832. $this->lMargin += $this->cell_padding['R'];
  20833. } else {
  20834. $this->x += $this->cell_padding['L'];
  20835. $this->rMargin += $this->cell_padding['L'];
  20836. }
  20837. }
  20838. if ($this->customlistindent >= 0) {
  20839. $this->listindent = $this->customlistindent;
  20840. } else {
  20841. $this->listindent = $this->GetStringWidth('000000');
  20842. }
  20843. $this->listindentlevel = 0;
  20844. // save previous states
  20845. $prev_cell_height_ratio = $this->cell_height_ratio;
  20846. $prev_listnum = $this->listnum;
  20847. $prev_listordered = $this->listordered;
  20848. $prev_listcount = $this->listcount;
  20849. $prev_lispacer = $this->lispacer;
  20850. $this->listnum = 0;
  20851. $this->listordered = array();
  20852. $this->listcount = array();
  20853. $this->lispacer = '';
  20854. if (($this->empty_string($this->lasth)) OR ($reseth)) {
  20855. // reset row height
  20856. $this->resetLastH();
  20857. }
  20858. $dom = $this->getHtmlDomArray($html);
  20859. $maxel = count($dom);
  20860. $key = 0;
  20861. $hidden_node_key = -1;
  20862. while ($key < $maxel) {
  20863. if ($dom[$key]['tag']) {
  20864. if ($dom[$key]['opening']) {
  20865. if (($hidden_node_key <= 0) AND $dom[$key]['hide']) {
  20866. // store the node key
  20867. $hidden_node_key = $key;
  20868. }
  20869. } elseif (($hidden_node_key > 0) AND ($dom[$key]['parent'] == $hidden_node_key)) {
  20870. // we have reached the closing tag of the hidden node
  20871. $hidden_node_key = 0;
  20872. }
  20873. }
  20874. if ($hidden_node_key >= 0) {
  20875. // skip this node
  20876. ++$key;
  20877. if ($hidden_node_key == 0) {
  20878. // reset hidden mode
  20879. $hidden_node_key = -1;
  20880. }
  20881. continue;
  20882. }
  20883. if ($dom[$key]['tag'] AND isset($dom[$key]['attribute']['pagebreak'])) {
  20884. // check for pagebreak
  20885. if (($dom[$key]['attribute']['pagebreak'] == 'true') OR ($dom[$key]['attribute']['pagebreak'] == 'left') OR ($dom[$key]['attribute']['pagebreak'] == 'right')) {
  20886. // add a page (or trig AcceptPageBreak() for multicolumn mode)
  20887. $this->checkPageBreak($this->PageBreakTrigger + 1);
  20888. }
  20889. if ((($dom[$key]['attribute']['pagebreak'] == 'left') AND (((!$this->rtl) AND (($this->page % 2) == 0)) OR (($this->rtl) AND (($this->page % 2) != 0))))
  20890. OR (($dom[$key]['attribute']['pagebreak'] == 'right') AND (((!$this->rtl) AND (($this->page % 2) != 0)) OR (($this->rtl) AND (($this->page % 2) == 0))))) {
  20891. // add a page (or trig AcceptPageBreak() for multicolumn mode)
  20892. $this->checkPageBreak($this->PageBreakTrigger + 1);
  20893. }
  20894. }
  20895. if ($dom[$key]['tag'] AND $dom[$key]['opening'] AND isset($dom[$key]['attribute']['nobr']) AND ($dom[$key]['attribute']['nobr'] == 'true')) {
  20896. if (isset($dom[($dom[$key]['parent'])]['attribute']['nobr']) AND ($dom[($dom[$key]['parent'])]['attribute']['nobr'] == 'true')) {
  20897. $dom[$key]['attribute']['nobr'] = false;
  20898. } else {
  20899. // store current object
  20900. $this->startTransaction();
  20901. // save this method vars
  20902. $this_method_vars['html'] = $html;
  20903. $this_method_vars['ln'] = $ln;
  20904. $this_method_vars['fill'] = $fill;
  20905. $this_method_vars['reseth'] = $reseth;
  20906. $this_method_vars['cell'] = $cell;
  20907. $this_method_vars['align'] = $align;
  20908. $this_method_vars['gvars'] = $gvars;
  20909. $this_method_vars['prevPage'] = $prevPage;
  20910. $this_method_vars['prev_cell_margin'] = $prev_cell_margin;
  20911. $this_method_vars['prev_cell_padding'] = $prev_cell_padding;
  20912. $this_method_vars['prevlMargin'] = $prevlMargin;
  20913. $this_method_vars['prevrMargin'] = $prevrMargin;
  20914. $this_method_vars['curfontname'] = $curfontname;
  20915. $this_method_vars['curfontstyle'] = $curfontstyle;
  20916. $this_method_vars['curfontsize'] = $curfontsize;
  20917. $this_method_vars['curfontascent'] = $curfontascent;
  20918. $this_method_vars['curfontdescent'] = $curfontdescent;
  20919. $this_method_vars['curfontstretcing'] = $curfontstretcing;
  20920. $this_method_vars['curfontkerning'] = $curfontkerning;
  20921. $this_method_vars['minstartliney'] = $minstartliney;
  20922. $this_method_vars['maxbottomliney'] = $maxbottomliney;
  20923. $this_method_vars['yshift'] = $yshift;
  20924. $this_method_vars['startlinepage'] = $startlinepage;
  20925. $this_method_vars['startlinepos'] = $startlinepos;
  20926. $this_method_vars['startlinex'] = $startlinex;
  20927. $this_method_vars['startliney'] = $startliney;
  20928. $this_method_vars['newline'] = $newline;
  20929. $this_method_vars['loop'] = $loop;
  20930. $this_method_vars['curpos'] = $curpos;
  20931. $this_method_vars['pask'] = $pask;
  20932. $this_method_vars['lalign'] = $lalign;
  20933. $this_method_vars['plalign'] = $plalign;
  20934. $this_method_vars['w'] = $w;
  20935. $this_method_vars['prev_cell_height_ratio'] = $prev_cell_height_ratio;
  20936. $this_method_vars['prev_listnum'] = $prev_listnum;
  20937. $this_method_vars['prev_listordered'] = $prev_listordered;
  20938. $this_method_vars['prev_listcount'] = $prev_listcount;
  20939. $this_method_vars['prev_lispacer'] = $prev_lispacer;
  20940. $this_method_vars['fontaligned'] = $fontaligned;
  20941. $this_method_vars['key'] = $key;
  20942. $this_method_vars['dom'] = $dom;
  20943. }
  20944. }
  20945. // print THEAD block
  20946. if (($dom[$key]['value'] == 'tr') AND isset($dom[$key]['thead']) AND $dom[$key]['thead']) {
  20947. if (isset($dom[$key]['parent']) AND isset($dom[$dom[$key]['parent']]['thead']) AND !$this->empty_string($dom[$dom[$key]['parent']]['thead'])) {
  20948. $this->inthead = true;
  20949. // print table header (thead)
  20950. $this->writeHTML($this->thead, false, false, false, false, '');
  20951. // check if we are on a new page or on a new column
  20952. if (($this->y < $this->start_transaction_y) OR ($this->checkPageBreak($this->lasth, '', false))) {
  20953. // we are on a new page or on a new column and the total object height is less than the available vertical space.
  20954. // restore previous object
  20955. $this->rollbackTransaction(true);
  20956. // restore previous values
  20957. foreach ($this_method_vars as $vkey => $vval) {
  20958. $$vkey = $vval;
  20959. }
  20960. // disable table header
  20961. $tmp_thead = $this->thead;
  20962. $this->thead = '';
  20963. // add a page (or trig AcceptPageBreak() for multicolumn mode)
  20964. $pre_y = $this->y;
  20965. if ((!$this->checkPageBreak($this->PageBreakTrigger + 1)) AND ($this->y < $pre_y)) {
  20966. // fix for multicolumn mode
  20967. $startliney = $this->y;
  20968. }
  20969. $this->start_transaction_page = $this->page;
  20970. $this->start_transaction_y = $this->y;
  20971. // restore table header
  20972. $this->thead = $tmp_thead;
  20973. // fix table border properties
  20974. if (isset($dom[$dom[$key]['parent']]['attribute']['cellspacing'])) {
  20975. $tmp_cellspacing = $this->getHTMLUnitToUnits($dom[$dom[$key]['parent']]['attribute']['cellspacing'], 1, 'px');
  20976. } elseif (isset($dom[$dom[$key]['parent']]['border-spacing'])) {
  20977. $tmp_cellspacing = $dom[$dom[$key]['parent']]['border-spacing']['V'];
  20978. } else {
  20979. $tmp_cellspacing = 0;
  20980. }
  20981. $dom[$dom[$key]['parent']]['borderposition']['page'] = $this->page;
  20982. $dom[$dom[$key]['parent']]['borderposition']['column'] = $this->current_column;
  20983. $dom[$dom[$key]['parent']]['borderposition']['y'] = $this->y + $tmp_cellspacing;
  20984. $xoffset = ($this->x - $dom[$dom[$key]['parent']]['borderposition']['x']);
  20985. $dom[$dom[$key]['parent']]['borderposition']['x'] += $xoffset;
  20986. $dom[$dom[$key]['parent']]['borderposition']['xmax'] += $xoffset;
  20987. // print table header (thead)
  20988. $this->writeHTML($this->thead, false, false, false, false, '');
  20989. }
  20990. }
  20991. // move $key index forward to skip THEAD block
  20992. while ( ($key < $maxel) AND (!(
  20993. ($dom[$key]['tag'] AND $dom[$key]['opening'] AND ($dom[$key]['value'] == 'tr') AND (!isset($dom[$key]['thead']) OR !$dom[$key]['thead']))
  20994. OR ($dom[$key]['tag'] AND (!$dom[$key]['opening']) AND ($dom[$key]['value'] == 'table'))) )) {
  20995. ++$key;
  20996. }
  20997. }
  20998. if ($dom[$key]['tag'] OR ($key == 0)) {
  20999. if ((($dom[$key]['value'] == 'table') OR ($dom[$key]['value'] == 'tr')) AND (isset($dom[$key]['align']))) {
  21000. $dom[$key]['align'] = ($this->rtl) ? 'R' : 'L';
  21001. }
  21002. // vertically align image in line
  21003. if ((!$this->newline) AND ($dom[$key]['value'] == 'img') AND (isset($dom[$key]['height'])) AND ($dom[$key]['height'] > 0)) {
  21004. // get image height
  21005. $imgh = $this->getHTMLUnitToUnits($dom[$key]['height'], $this->lasth, 'px');
  21006. $autolinebreak = false;
  21007. if (isset($dom[$key]['width']) AND ($dom[$key]['width'] > 0)) {
  21008. $imgw = $this->getHTMLUnitToUnits($dom[$key]['width'], 1, 'px', false);
  21009. if (($imgw <= ($this->w - $this->lMargin - $this->rMargin - $this->cell_padding['L'] - $this->cell_padding['R']))
  21010. AND ((($this->rtl) AND (($this->x - $imgw) < ($this->lMargin + $this->cell_padding['L'])))
  21011. OR ((!$this->rtl) AND (($this->x + $imgw) > ($this->w - $this->rMargin - $this->cell_padding['R']))))) {
  21012. // add automatic line break
  21013. $autolinebreak = true;
  21014. $this->Ln('', $cell);
  21015. if ((!$dom[($key-1)]['tag']) AND ($dom[($key-1)]['value'] == ' ')) {
  21016. // go back to evaluate this line break
  21017. --$key;
  21018. }
  21019. }
  21020. }
  21021. if (!$autolinebreak) {
  21022. if ($this->inPageBody()) {
  21023. $pre_y = $this->y;
  21024. // check for page break
  21025. if ((!$this->checkPageBreak($imgh)) AND ($this->y < $pre_y)) {
  21026. // fix for multicolumn mode
  21027. $startliney = $this->y;
  21028. }
  21029. }
  21030. if ($this->page > $startlinepage) {
  21031. // fix line splitted over two pages
  21032. if (isset($this->footerlen[$startlinepage])) {
  21033. $curpos = $this->pagelen[$startlinepage] - $this->footerlen[$startlinepage];
  21034. }
  21035. // line to be moved one page forward
  21036. $pagebuff = $this->getPageBuffer($startlinepage);
  21037. $linebeg = substr($pagebuff, $startlinepos, ($curpos - $startlinepos));
  21038. $tstart = substr($pagebuff, 0, $startlinepos);
  21039. $tend = substr($this->getPageBuffer($startlinepage), $curpos);
  21040. // remove line from previous page
  21041. $this->setPageBuffer($startlinepage, $tstart.''.$tend);
  21042. $pagebuff = $this->getPageBuffer($this->page);
  21043. $tstart = substr($pagebuff, 0, $this->cntmrk[$this->page]);
  21044. $tend = substr($pagebuff, $this->cntmrk[$this->page]);
  21045. // add line start to current page
  21046. $yshift = ($minstartliney - $this->y);
  21047. if ($fontaligned) {
  21048. $yshift += ($curfontsize / $this->k);
  21049. }
  21050. $try = sprintf('1 0 0 1 0 %.3F cm', ($yshift * $this->k));
  21051. $this->setPageBuffer($this->page, $tstart."\nq\n".$try."\n".$linebeg."\nQ\n".$tend);
  21052. // shift the annotations and links
  21053. if (isset($this->PageAnnots[$this->page])) {
  21054. $next_pask = count($this->PageAnnots[$this->page]);
  21055. } else {
  21056. $next_pask = 0;
  21057. }
  21058. if (isset($this->PageAnnots[$startlinepage])) {
  21059. foreach ($this->PageAnnots[$startlinepage] as $pak => $pac) {
  21060. if ($pak >= $pask) {
  21061. $this->PageAnnots[$this->page][] = $pac;
  21062. unset($this->PageAnnots[$startlinepage][$pak]);
  21063. $npak = count($this->PageAnnots[$this->page]) - 1;
  21064. $this->PageAnnots[$this->page][$npak]['y'] -= $yshift;
  21065. }
  21066. }
  21067. }
  21068. $pask = $next_pask;
  21069. $startlinepos = $this->cntmrk[$this->page];
  21070. $startlinepage = $this->page;
  21071. $startliney = $this->y;
  21072. $this->newline = false;
  21073. }
  21074. $this->y += ((($curfontsize * $this->cell_height_ratio / $this->k) + $curfontascent - $curfontdescent) / 2) - $imgh;
  21075. $minstartliney = min($this->y, $minstartliney);
  21076. $maxbottomliney = ($startliney + ($this->FontSize * $this->cell_height_ratio));
  21077. }
  21078. } elseif (isset($dom[$key]['fontname']) OR isset($dom[$key]['fontstyle']) OR isset($dom[$key]['fontsize']) OR isset($dom[$key]['line-height'])) {
  21079. // account for different font size
  21080. $pfontname = $curfontname;
  21081. $pfontstyle = $curfontstyle;
  21082. $pfontsize = $curfontsize;
  21083. $fontname = isset($dom[$key]['fontname']) ? $dom[$key]['fontname'] : $curfontname;
  21084. $fontstyle = isset($dom[$key]['fontstyle']) ? $dom[$key]['fontstyle'] : $curfontstyle;
  21085. $fontsize = isset($dom[$key]['fontsize']) ? $dom[$key]['fontsize'] : $curfontsize;
  21086. $fontascent = $this->getFontAscent($fontname, $fontstyle, $fontsize);
  21087. $fontdescent = $this->getFontDescent($fontname, $fontstyle, $fontsize);
  21088. if (($fontname != $curfontname) OR ($fontstyle != $curfontstyle) OR ($fontsize != $curfontsize)
  21089. OR ($this->cell_height_ratio != $dom[$key]['line-height'])
  21090. OR ($dom[$key]['tag'] AND $dom[$key]['opening'] AND ($dom[$key]['value'] == 'li')) ) {
  21091. if (($key < ($maxel - 1)) AND (
  21092. ($dom[$key]['tag'] AND $dom[$key]['opening'] AND ($dom[$key]['value'] == 'li'))
  21093. OR ($this->cell_height_ratio != $dom[$key]['line-height'])
  21094. OR (!$this->newline AND is_numeric($fontsize) AND is_numeric($curfontsize) AND ($fontsize >= 0) AND ($curfontsize >= 0) AND ($fontsize != $curfontsize))
  21095. )) {
  21096. if ($this->page > $startlinepage) {
  21097. // fix lines splitted over two pages
  21098. if (isset($this->footerlen[$startlinepage])) {
  21099. $curpos = $this->pagelen[$startlinepage] - $this->footerlen[$startlinepage];
  21100. }
  21101. // line to be moved one page forward
  21102. $pagebuff = $this->getPageBuffer($startlinepage);
  21103. $linebeg = substr($pagebuff, $startlinepos, ($curpos - $startlinepos));
  21104. $tstart = substr($pagebuff, 0, $startlinepos);
  21105. $tend = substr($this->getPageBuffer($startlinepage), $curpos);
  21106. // remove line start from previous page
  21107. $this->setPageBuffer($startlinepage, $tstart.''.$tend);
  21108. $pagebuff = $this->getPageBuffer($this->page);
  21109. $tstart = substr($pagebuff, 0, $this->cntmrk[$this->page]);
  21110. $tend = substr($pagebuff, $this->cntmrk[$this->page]);
  21111. // add line start to current page
  21112. $yshift = ($minstartliney - $this->y);
  21113. $try = sprintf('1 0 0 1 0 %.3F cm', ($yshift * $this->k));
  21114. $this->setPageBuffer($this->page, $tstart."\nq\n".$try."\n".$linebeg."\nQ\n".$tend);
  21115. // shift the annotations and links
  21116. if (isset($this->PageAnnots[$this->page])) {
  21117. $next_pask = count($this->PageAnnots[$this->page]);
  21118. } else {
  21119. $next_pask = 0;
  21120. }
  21121. if (isset($this->PageAnnots[$startlinepage])) {
  21122. foreach ($this->PageAnnots[$startlinepage] as $pak => $pac) {
  21123. if ($pak >= $pask) {
  21124. $this->PageAnnots[$this->page][] = $pac;
  21125. unset($this->PageAnnots[$startlinepage][$pak]);
  21126. $npak = count($this->PageAnnots[$this->page]) - 1;
  21127. $this->PageAnnots[$this->page][$npak]['y'] -= $yshift;
  21128. }
  21129. }
  21130. }
  21131. $pask = $next_pask;
  21132. $startlinepos = $this->cntmrk[$this->page];
  21133. $startlinepage = $this->page;
  21134. $startliney = $this->y;
  21135. }
  21136. if (!isset($dom[$key]['line-height'])) {
  21137. $dom[$key]['line-height'] = $this->cell_height_ratio;
  21138. }
  21139. if (!$dom[$key]['block']) {
  21140. 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']))) {
  21141. $this->y += (((($curfontsize * $this->cell_height_ratio) - ($fontsize * $dom[$key]['line-height'])) / $this->k) + $curfontascent - $fontascent - $curfontdescent + $fontdescent) / 2;
  21142. }
  21143. if (($dom[$key]['value'] != 'sup') AND ($dom[$key]['value'] != 'sub')) {
  21144. $current_line_align_data = array($key, $minstartliney, $maxbottomliney);
  21145. 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)))) {
  21146. $minstartliney = min($this->y, $line_align_data[1]);
  21147. $maxbottomliney = max(($this->y + (($fontsize * $this->cell_height_ratio) / $this->k)), $line_align_data[2]);
  21148. } else {
  21149. $minstartliney = min($this->y, $minstartliney);
  21150. $maxbottomliney = max(($this->y + (($fontsize * $this->cell_height_ratio) / $this->k)), $maxbottomliney);
  21151. }
  21152. $line_align_data = $current_line_align_data;
  21153. }
  21154. }
  21155. $this->cell_height_ratio = $dom[$key]['line-height'];
  21156. $fontaligned = true;
  21157. }
  21158. $this->SetFont($fontname, $fontstyle, $fontsize);
  21159. // reset row height
  21160. $this->resetLastH();
  21161. $curfontname = $fontname;
  21162. $curfontstyle = $fontstyle;
  21163. $curfontsize = $fontsize;
  21164. $curfontascent = $fontascent;
  21165. $curfontdescent = $fontdescent;
  21166. }
  21167. }
  21168. // set text rendering mode
  21169. $textstroke = isset($dom[$key]['stroke']) ? $dom[$key]['stroke'] : $this->textstrokewidth;
  21170. $textfill = isset($dom[$key]['fill']) ? $dom[$key]['fill'] : (($this->textrendermode % 2) == 0);
  21171. $textclip = isset($dom[$key]['clip']) ? $dom[$key]['clip'] : ($this->textrendermode > 3);
  21172. $this->setTextRenderingMode($textstroke, $textfill, $textclip);
  21173. if (isset($dom[$key]['font-stretch']) AND ($dom[$key]['font-stretch'] !== false)) {
  21174. $this->setFontStretching($dom[$key]['font-stretch']);
  21175. }
  21176. if (isset($dom[$key]['letter-spacing']) AND ($dom[$key]['letter-spacing'] !== false)) {
  21177. $this->setFontSpacing($dom[$key]['letter-spacing']);
  21178. }
  21179. if (($plalign == 'J') AND $dom[$key]['block']) {
  21180. $plalign = '';
  21181. }
  21182. // get current position on page buffer
  21183. $curpos = $this->pagelen[$startlinepage];
  21184. if (isset($dom[$key]['bgcolor']) AND ($dom[$key]['bgcolor'] !== false)) {
  21185. $this->SetFillColorArray($dom[$key]['bgcolor']);
  21186. $wfill = true;
  21187. } else {
  21188. $wfill = $fill | false;
  21189. }
  21190. if (isset($dom[$key]['fgcolor']) AND ($dom[$key]['fgcolor'] !== false)) {
  21191. $this->SetTextColorArray($dom[$key]['fgcolor']);
  21192. }
  21193. if (isset($dom[$key]['strokecolor']) AND ($dom[$key]['strokecolor'] !== false)) {
  21194. $this->SetDrawColorArray($dom[$key]['strokecolor']);
  21195. }
  21196. if (isset($dom[$key]['align'])) {
  21197. $lalign = $dom[$key]['align'];
  21198. }
  21199. if ($this->empty_string($lalign)) {
  21200. $lalign = $align;
  21201. }
  21202. }
  21203. // align lines
  21204. if ($this->newline AND (strlen($dom[$key]['value']) > 0) AND ($dom[$key]['value'] != 'td') AND ($dom[$key]['value'] != 'th')) {
  21205. $newline = true;
  21206. $fontaligned = false;
  21207. // we are at the beginning of a new line
  21208. if (isset($startlinex)) {
  21209. $yshift = ($minstartliney - $startliney);
  21210. if (($yshift > 0) OR ($this->page > $startlinepage)) {
  21211. $yshift = 0;
  21212. }
  21213. $t_x = 0;
  21214. // the last line must be shifted to be aligned as requested
  21215. $linew = abs($this->endlinex - $startlinex);
  21216. if ($this->inxobj) {
  21217. // we are inside an XObject template
  21218. $pstart = substr($this->xobjects[$this->xobjid]['outdata'], 0, $startlinepos);
  21219. if (isset($opentagpos)) {
  21220. $midpos = $opentagpos;
  21221. } else {
  21222. $midpos = 0;
  21223. }
  21224. if ($midpos > 0) {
  21225. $pmid = substr($this->xobjects[$this->xobjid]['outdata'], $startlinepos, ($midpos - $startlinepos));
  21226. $pend = substr($this->xobjects[$this->xobjid]['outdata'], $midpos);
  21227. } else {
  21228. $pmid = substr($this->xobjects[$this->xobjid]['outdata'], $startlinepos);
  21229. $pend = '';
  21230. }
  21231. } else {
  21232. $pstart = substr($this->getPageBuffer($startlinepage), 0, $startlinepos);
  21233. if (isset($opentagpos) AND isset($this->footerlen[$startlinepage]) AND (!$this->InFooter)) {
  21234. $this->footerpos[$startlinepage] = $this->pagelen[$startlinepage] - $this->footerlen[$startlinepage];
  21235. $midpos = min($opentagpos, $this->footerpos[$startlinepage]);
  21236. } elseif (isset($opentagpos)) {
  21237. $midpos = $opentagpos;
  21238. } elseif (isset($this->footerlen[$startlinepage]) AND (!$this->InFooter)) {
  21239. $this->footerpos[$startlinepage] = $this->pagelen[$startlinepage] - $this->footerlen[$startlinepage];
  21240. $midpos = $this->footerpos[$startlinepage];
  21241. } else {
  21242. $midpos = 0;
  21243. }
  21244. if ($midpos > 0) {
  21245. $pmid = substr($this->getPageBuffer($startlinepage), $startlinepos, ($midpos - $startlinepos));
  21246. $pend = substr($this->getPageBuffer($startlinepage), $midpos);
  21247. } else {
  21248. $pmid = substr($this->getPageBuffer($startlinepage), $startlinepos);
  21249. $pend = '';
  21250. }
  21251. }
  21252. if ((isset($plalign) AND ((($plalign == 'C') OR ($plalign == 'J') OR (($plalign == 'R') AND (!$this->rtl)) OR (($plalign == 'L') AND ($this->rtl)))))) {
  21253. // calculate shifting amount
  21254. $tw = $w;
  21255. if (($plalign == 'J') AND $this->isRTLTextDir() AND ($this->num_columns > 1)) {
  21256. $tw += $this->cell_padding['R'];
  21257. }
  21258. if ($this->lMargin != $prevlMargin) {
  21259. $tw += ($prevlMargin - $this->lMargin);
  21260. }
  21261. if ($this->rMargin != $prevrMargin) {
  21262. $tw += ($prevrMargin - $this->rMargin);
  21263. }
  21264. $one_space_width = $this->GetStringWidth(chr(32));
  21265. $no = 0; // number of spaces on a line contained on a single block
  21266. if ($this->isRTLTextDir()) { // RTL
  21267. // remove left space if exist
  21268. $pos1 = $this->revstrpos($pmid, '[(');
  21269. if ($pos1 > 0) {
  21270. $pos1 = intval($pos1);
  21271. if ($this->isUnicodeFont()) {
  21272. $pos2 = intval($this->revstrpos($pmid, '[('.chr(0).chr(32)));
  21273. $spacelen = 2;
  21274. } else {
  21275. $pos2 = intval($this->revstrpos($pmid, '[('.chr(32)));
  21276. $spacelen = 1;
  21277. }
  21278. if ($pos1 == $pos2) {
  21279. $pmid = substr($pmid, 0, ($pos1 + 2)).substr($pmid, ($pos1 + 2 + $spacelen));
  21280. if (substr($pmid, $pos1, 4) == '[()]') {
  21281. $linew -= $one_space_width;
  21282. } elseif ($pos1 == strpos($pmid, '[(')) {
  21283. $no = 1;
  21284. }
  21285. }
  21286. }
  21287. } else { // LTR
  21288. // remove right space if exist
  21289. $pos1 = $this->revstrpos($pmid, ')]');
  21290. if ($pos1 > 0) {
  21291. $pos1 = intval($pos1);
  21292. if ($this->isUnicodeFont()) {
  21293. $pos2 = intval($this->revstrpos($pmid, chr(0).chr(32).')]')) + 2;
  21294. $spacelen = 2;
  21295. } else {
  21296. $pos2 = intval($this->revstrpos($pmid, chr(32).')]')) + 1;
  21297. $spacelen = 1;
  21298. }
  21299. if ($pos1 == $pos2) {
  21300. $pmid = substr($pmid, 0, ($pos1 - $spacelen)).substr($pmid, $pos1);
  21301. $linew -= $one_space_width;
  21302. }
  21303. }
  21304. }
  21305. $mdiff = ($tw - $linew);
  21306. if ($plalign == 'C') {
  21307. if ($this->rtl) {
  21308. $t_x = -($mdiff / 2);
  21309. } else {
  21310. $t_x = ($mdiff / 2);
  21311. }
  21312. } elseif ($plalign == 'R') {
  21313. // right alignment on LTR document
  21314. $t_x = $mdiff;
  21315. } elseif ($plalign == 'L') {
  21316. // left alignment on RTL document
  21317. $t_x = -$mdiff;
  21318. } elseif (($plalign == 'J') AND ($plalign == $lalign)) {
  21319. // Justification
  21320. if ($this->isRTLTextDir()) {
  21321. // align text on the left
  21322. $t_x = -$mdiff;
  21323. }
  21324. $ns = 0; // number of spaces
  21325. $pmidtemp = $pmid;
  21326. // escape special characters
  21327. $pmidtemp = preg_replace('/[\\\][\(]/x', '\\#!#OP#!#', $pmidtemp);
  21328. $pmidtemp = preg_replace('/[\\\][\)]/x', '\\#!#CP#!#', $pmidtemp);
  21329. // search spaces
  21330. if (preg_match_all('/\[\(([^\)]*)\)\]/x', $pmidtemp, $lnstring, PREG_PATTERN_ORDER)) {
  21331. $spacestr = $this->getSpaceString();
  21332. $maxkk = count($lnstring[1]) - 1;
  21333. for ($kk=0; $kk <= $maxkk; ++$kk) {
  21334. // restore special characters
  21335. $lnstring[1][$kk] = str_replace('#!#OP#!#', '(', $lnstring[1][$kk]);
  21336. $lnstring[1][$kk] = str_replace('#!#CP#!#', ')', $lnstring[1][$kk]);
  21337. // store number of spaces on the strings
  21338. $lnstring[2][$kk] = substr_count($lnstring[1][$kk], $spacestr);
  21339. // count total spaces on line
  21340. $ns += $lnstring[2][$kk];
  21341. $lnstring[3][$kk] = $ns;
  21342. }
  21343. if ($ns == 0) {
  21344. $ns = 1;
  21345. }
  21346. // calculate additional space to add to each existing space
  21347. $spacewidth = ($mdiff / ($ns - $no)) * $this->k;
  21348. $spacewidthu = -1000 * ($mdiff + (($ns + $no) * $one_space_width)) / $ns / $this->FontSize;
  21349. if ($this->font_spacing != 0) {
  21350. // fixed spacing mode
  21351. $osw = -1000 * $this->font_spacing / $this->FontSize;
  21352. $spacewidthu += $osw;
  21353. }
  21354. $nsmax = $ns;
  21355. $ns = 0;
  21356. reset($lnstring);
  21357. $offset = 0;
  21358. $strcount = 0;
  21359. $prev_epsposbeg = 0;
  21360. $textpos = 0;
  21361. if ($this->isRTLTextDir()) {
  21362. $textpos = $this->wPt;
  21363. }
  21364. global $spacew;
  21365. while (preg_match('/([0-9\.\+\-]*)[\s](Td|cm|m|l|c|re)[\s]/x', $pmid, $strpiece, PREG_OFFSET_CAPTURE, $offset) == 1) {
  21366. // check if we are inside a string section '[( ... )]'
  21367. $stroffset = strpos($pmid, '[(', $offset);
  21368. if (($stroffset !== false) AND ($stroffset <= $strpiece[2][1])) {
  21369. // set offset to the end of string section
  21370. $offset = strpos($pmid, ')]', $stroffset);
  21371. while (($offset !== false) AND ($pmid[($offset - 1)] == '\\')) {
  21372. $offset = strpos($pmid, ')]', ($offset + 1));
  21373. }
  21374. if ($offset === false) {
  21375. $this->Error('HTML Justification: malformed PDF code.');
  21376. }
  21377. continue;
  21378. }
  21379. if ($this->isRTLTextDir()) {
  21380. $spacew = ($spacewidth * ($nsmax - $ns));
  21381. } else {
  21382. $spacew = ($spacewidth * $ns);
  21383. }
  21384. $offset = $strpiece[2][1] + strlen($strpiece[2][0]);
  21385. $epsposbeg = strpos($pmid, 'q'.$this->epsmarker, $offset);
  21386. $epsposend = strpos($pmid, $this->epsmarker.'Q', $offset) + strlen($this->epsmarker.'Q');
  21387. if ((($epsposbeg > 0) AND ($epsposend > 0) AND ($offset > $epsposbeg) AND ($offset < $epsposend))
  21388. OR (($epsposbeg === false) AND ($epsposend > 0) AND ($offset < $epsposend))) {
  21389. // shift EPS images
  21390. $trx = sprintf('1 0 0 1 %.3F 0 cm', $spacew);
  21391. $epsposbeg = strpos($pmid, 'q'.$this->epsmarker, ($prev_epsposbeg - 6));
  21392. $pmid_b = substr($pmid, 0, $epsposbeg);
  21393. $pmid_m = substr($pmid, $epsposbeg, ($epsposend - $epsposbeg));
  21394. $pmid_e = substr($pmid, $epsposend);
  21395. $pmid = $pmid_b."\nq\n".$trx."\n".$pmid_m."\nQ\n".$pmid_e;
  21396. $offset = $epsposend;
  21397. continue;
  21398. }
  21399. $prev_epsposbeg = $epsposbeg;
  21400. $currentxpos = 0;
  21401. // shift blocks of code
  21402. switch ($strpiece[2][0]) {
  21403. case 'Td':
  21404. case 'cm':
  21405. case 'm':
  21406. case 'l': {
  21407. // get current X position
  21408. preg_match('/([0-9\.\+\-]*)[\s]('.$strpiece[1][0].')[\s]('.$strpiece[2][0].')([\s]*)/x', $pmid, $xmatches);
  21409. $currentxpos = $xmatches[1];
  21410. $textpos = $currentxpos;
  21411. if (($strcount <= $maxkk) AND ($strpiece[2][0] == 'Td')) {
  21412. $ns = $lnstring[3][$strcount];
  21413. if ($this->isRTLTextDir()) {
  21414. $spacew = ($spacewidth * ($nsmax - $ns));
  21415. }
  21416. ++$strcount;
  21417. }
  21418. // justify block
  21419. $pmid = preg_replace_callback('/([0-9\.\+\-]*)[\s]('.$strpiece[1][0].')[\s]('.$strpiece[2][0].')([\s]*)/x',
  21420. create_function('$matches', 'global $spacew;
  21421. $newx = sprintf("%.2F",(floatval($matches[1]) + $spacew));
  21422. return "".$newx." ".$matches[2]." x*#!#*x".$matches[3].$matches[4];'), $pmid, 1);
  21423. break;
  21424. }
  21425. case 're': {
  21426. // justify block
  21427. if (!$this->empty_string($this->lispacer)) {
  21428. $this->lispacer = '';
  21429. continue;
  21430. }
  21431. preg_match('/([0-9\.\+\-]*)[\s]([0-9\.\+\-]*)[\s]([0-9\.\+\-]*)[\s]('.$strpiece[1][0].')[\s](re)([\s]*)/x', $pmid, $xmatches);
  21432. $currentxpos = $xmatches[1];
  21433. global $x_diff, $w_diff;
  21434. $x_diff = 0;
  21435. $w_diff = 0;
  21436. if ($this->isRTLTextDir()) { // RTL
  21437. if ($currentxpos < $textpos) {
  21438. $x_diff = ($spacewidth * ($nsmax - $lnstring[3][$strcount]));
  21439. $w_diff = ($spacewidth * $lnstring[2][$strcount]);
  21440. } else {
  21441. if ($strcount > 0) {
  21442. $x_diff = ($spacewidth * ($nsmax - $lnstring[3][($strcount - 1)]));
  21443. $w_diff = ($spacewidth * $lnstring[2][($strcount - 1)]);
  21444. }
  21445. }
  21446. } else { // LTR
  21447. if ($currentxpos > $textpos) {
  21448. if ($strcount > 0) {
  21449. $x_diff = ($spacewidth * $lnstring[3][($strcount - 1)]);
  21450. }
  21451. $w_diff = ($spacewidth * $lnstring[2][$strcount]);
  21452. } else {
  21453. if ($strcount > 1) {
  21454. $x_diff = ($spacewidth * $lnstring[3][($strcount - 2)]);
  21455. }
  21456. if ($strcount > 0) {
  21457. $w_diff = ($spacewidth * $lnstring[2][($strcount - 1)]);
  21458. }
  21459. }
  21460. }
  21461. $pmid = preg_replace_callback('/('.$xmatches[1].')[\s]('.$xmatches[2].')[\s]('.$xmatches[3].')[\s]('.$strpiece[1][0].')[\s](re)([\s]*)/x',
  21462. create_function('$matches', 'global $x_diff, $w_diff;
  21463. $newx = sprintf("%.2F",(floatval($matches[1]) + $x_diff));
  21464. $neww = sprintf("%.2F",(floatval($matches[3]) + $w_diff));
  21465. return "".$newx." ".$matches[2]." ".$neww." ".$matches[4]." x*#!#*x".$matches[5].$matches[6];'), $pmid, 1);
  21466. break;
  21467. }
  21468. case 'c': {
  21469. // get current X position
  21470. 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);
  21471. $currentxpos = $xmatches[1];
  21472. // justify block
  21473. $pmid = preg_replace_callback('/('.$xmatches[1].')[\s]('.$xmatches[2].')[\s]('.$xmatches[3].')[\s]('.$xmatches[4].')[\s]('.$xmatches[5].')[\s]('.$strpiece[1][0].')[\s](c)([\s]*)/x',
  21474. create_function('$matches', 'global $spacew;
  21475. $newx1 = sprintf("%.3F",(floatval($matches[1]) + $spacew));
  21476. $newx2 = sprintf("%.3F",(floatval($matches[3]) + $spacew));
  21477. $newx3 = sprintf("%.3F",(floatval($matches[5]) + $spacew));
  21478. return "".$newx1." ".$matches[2]." ".$newx2." ".$matches[4]." ".$newx3." ".$matches[6]." x*#!#*x".$matches[7].$matches[8];'), $pmid, 1);
  21479. break;
  21480. }
  21481. }
  21482. // shift the annotations and links
  21483. $cxpos = ($currentxpos / $this->k);
  21484. $lmpos = ($this->lMargin + $this->cell_padding['L'] + $this->feps);
  21485. if ($this->inxobj) {
  21486. // we are inside an XObject template
  21487. foreach ($this->xobjects[$this->xobjid]['annotations'] as $pak => $pac) {
  21488. if (($pac['y'] >= $minstartliney) AND (($pac['x'] * $this->k) >= ($currentxpos - $this->feps)) AND (($pac['x'] * $this->k) <= ($currentxpos + $this->feps))) {
  21489. if ($cxpos > $lmpos) {
  21490. $this->xobjects[$this->xobjid]['annotations'][$pak]['x'] += ($spacew / $this->k);
  21491. $this->xobjects[$this->xobjid]['annotations'][$pak]['w'] += (($spacewidth * $pac['numspaces']) / $this->k);
  21492. } else {
  21493. $this->xobjects[$this->xobjid]['annotations'][$pak]['w'] += (($spacewidth * $pac['numspaces']) / $this->k);
  21494. }
  21495. break;
  21496. }
  21497. }
  21498. } elseif (isset($this->PageAnnots[$this->page])) {
  21499. foreach ($this->PageAnnots[$this->page] as $pak => $pac) {
  21500. if (($pac['y'] >= $minstartliney) AND (($pac['x'] * $this->k) >= ($currentxpos - $this->feps)) AND (($pac['x'] * $this->k) <= ($currentxpos + $this->feps))) {
  21501. if ($cxpos > $lmpos) {
  21502. $this->PageAnnots[$this->page][$pak]['x'] += ($spacew / $this->k);
  21503. $this->PageAnnots[$this->page][$pak]['w'] += (($spacewidth * $pac['numspaces']) / $this->k);
  21504. } else {
  21505. $this->PageAnnots[$this->page][$pak]['w'] += (($spacewidth * $pac['numspaces']) / $this->k);
  21506. }
  21507. break;
  21508. }
  21509. }
  21510. }
  21511. } // end of while
  21512. // remove markers
  21513. $pmid = str_replace('x*#!#*x', '', $pmid);
  21514. if ($this->isUnicodeFont()) {
  21515. // multibyte characters
  21516. $spacew = $spacewidthu;
  21517. if ($this->font_stretching != 100) {
  21518. // word spacing is affected by stretching
  21519. $spacew /= ($this->font_stretching / 100);
  21520. }
  21521. $pmidtemp = $pmid;
  21522. // escape special characters
  21523. $pmidtemp = preg_replace('/[\\\][\(]/x', '\\#!#OP#!#', $pmidtemp);
  21524. $pmidtemp = preg_replace('/[\\\][\)]/x', '\\#!#CP#!#', $pmidtemp);
  21525. $pmid = preg_replace_callback("/\[\(([^\)]*)\)\]/x",
  21526. create_function('$matches', 'global $spacew;
  21527. $matches[1] = str_replace("#!#OP#!#", "(", $matches[1]);
  21528. $matches[1] = str_replace("#!#CP#!#", ")", $matches[1]);
  21529. return "[(".str_replace(chr(0).chr(32), ") ".sprintf("%.3F", $spacew)." (", $matches[1]).")]";'), $pmidtemp);
  21530. if ($this->inxobj) {
  21531. // we are inside an XObject template
  21532. $this->xobjects[$this->xobjid]['outdata'] = $pstart."\n".$pmid."\n".$pend;
  21533. } else {
  21534. $this->setPageBuffer($startlinepage, $pstart."\n".$pmid."\n".$pend);
  21535. }
  21536. $endlinepos = strlen($pstart."\n".$pmid."\n");
  21537. } else {
  21538. // non-unicode (single-byte characters)
  21539. if ($this->font_stretching != 100) {
  21540. // word spacing (Tw) is affected by stretching
  21541. $spacewidth /= ($this->font_stretching / 100);
  21542. }
  21543. $rs = sprintf('%.3F Tw', $spacewidth);
  21544. $pmid = preg_replace("/\[\(/x", $rs.' [(', $pmid);
  21545. if ($this->inxobj) {
  21546. // we are inside an XObject template
  21547. $this->xobjects[$this->xobjid]['outdata'] = $pstart."\n".$pmid."\nBT 0 Tw ET\n".$pend;
  21548. } else {
  21549. $this->setPageBuffer($startlinepage, $pstart."\n".$pmid."\nBT 0 Tw ET\n".$pend);
  21550. }
  21551. $endlinepos = strlen($pstart."\n".$pmid."\nBT 0 Tw ET\n");
  21552. }
  21553. }
  21554. } // end of J
  21555. } // end if $startlinex
  21556. if (($t_x != 0) OR ($yshift < 0)) {
  21557. // shift the line
  21558. $trx = sprintf('1 0 0 1 %.3F %.3F cm', ($t_x * $this->k), ($yshift * $this->k));
  21559. $pstart .= "\nq\n".$trx."\n".$pmid."\nQ\n";
  21560. $endlinepos = strlen($pstart);
  21561. if ($this->inxobj) {
  21562. // we are inside an XObject template
  21563. $this->xobjects[$this->xobjid]['outdata'] = $pstart.$pend;
  21564. foreach ($this->xobjects[$this->xobjid]['annotations'] as $pak => $pac) {
  21565. if ($pak >= $pask) {
  21566. $this->xobjects[$this->xobjid]['annotations'][$pak]['x'] += $t_x;
  21567. $this->xobjects[$this->xobjid]['annotations'][$pak]['y'] -= $yshift;
  21568. }
  21569. }
  21570. } else {
  21571. $this->setPageBuffer($startlinepage, $pstart.$pend);
  21572. // shift the annotations and links
  21573. if (isset($this->PageAnnots[$this->page])) {
  21574. foreach ($this->PageAnnots[$this->page] as $pak => $pac) {
  21575. if ($pak >= $pask) {
  21576. $this->PageAnnots[$this->page][$pak]['x'] += $t_x;
  21577. $this->PageAnnots[$this->page][$pak]['y'] -= $yshift;
  21578. }
  21579. }
  21580. }
  21581. }
  21582. $this->y -= $yshift;
  21583. }
  21584. }
  21585. $pbrk = $this->checkPageBreak($this->lasth);
  21586. $this->newline = false;
  21587. $startlinex = $this->x;
  21588. $startliney = $this->y;
  21589. if ($dom[$dom[$key]['parent']]['value'] == 'sup') {
  21590. $startliney -= ((0.3 * $this->FontSizePt) / $this->k);
  21591. } elseif ($dom[$dom[$key]['parent']]['value'] == 'sub') {
  21592. $startliney -= (($this->FontSizePt / 0.7) / $this->k);
  21593. } else {
  21594. $minstartliney = $startliney;
  21595. $maxbottomliney = ($this->y + (($fontsize * $this->cell_height_ratio) / $this->k));
  21596. }
  21597. $startlinepage = $this->page;
  21598. if (isset($endlinepos) AND (!$pbrk)) {
  21599. $startlinepos = $endlinepos;
  21600. } else {
  21601. if ($this->inxobj) {
  21602. // we are inside an XObject template
  21603. $startlinepos = strlen($this->xobjects[$this->xobjid]['outdata']);
  21604. } elseif (!$this->InFooter) {
  21605. if (isset($this->footerlen[$this->page])) {
  21606. $this->footerpos[$this->page] = $this->pagelen[$this->page] - $this->footerlen[$this->page];
  21607. } else {
  21608. $this->footerpos[$this->page] = $this->pagelen[$this->page];
  21609. }
  21610. $startlinepos = $this->footerpos[$this->page];
  21611. } else {
  21612. $startlinepos = $this->pagelen[$this->page];
  21613. }
  21614. }
  21615. unset($endlinepos);
  21616. $plalign = $lalign;
  21617. if (isset($this->PageAnnots[$this->page])) {
  21618. $pask = count($this->PageAnnots[$this->page]);
  21619. } else {
  21620. $pask = 0;
  21621. }
  21622. if (!($dom[$key]['tag'] AND !$dom[$key]['opening'] AND ($dom[$key]['value'] == 'table')
  21623. AND (isset($this->emptypagemrk[$this->page]))
  21624. AND ($this->emptypagemrk[$this->page] == $this->pagelen[$this->page]))) {
  21625. $this->SetFont($fontname, $fontstyle, $fontsize);
  21626. if ($wfill) {
  21627. $this->SetFillColorArray($this->bgcolor);
  21628. }
  21629. }
  21630. } // end newline
  21631. if (isset($opentagpos)) {
  21632. unset($opentagpos);
  21633. }
  21634. if ($dom[$key]['tag']) {
  21635. if ($dom[$key]['opening']) {
  21636. // get text indentation (if any)
  21637. if (isset($dom[$key]['text-indent']) AND $dom[$key]['block']) {
  21638. $this->textindent = $dom[$key]['text-indent'];
  21639. $this->newline = true;
  21640. }
  21641. // table
  21642. if ($dom[$key]['value'] == 'table') {
  21643. // available page width
  21644. if ($this->rtl) {
  21645. $wtmp = $this->x - $this->lMargin;
  21646. } else {
  21647. $wtmp = $this->w - $this->rMargin - $this->x;
  21648. }
  21649. // get cell spacing
  21650. if (isset($dom[$key]['attribute']['cellspacing'])) {
  21651. $clsp = $this->getHTMLUnitToUnits($dom[$key]['attribute']['cellspacing'], 1, 'px');
  21652. $cellspacing = array('H' => $clsp, 'V' => $clsp);
  21653. } elseif (isset($dom[$key]['border-spacing'])) {
  21654. $cellspacing = $dom[$key]['border-spacing'];
  21655. } else {
  21656. $cellspacing = array('H' => 0, 'V' => 0);
  21657. }
  21658. // table width
  21659. if (isset($dom[$key]['width'])) {
  21660. $table_width = $this->getHTMLUnitToUnits($dom[$key]['width'], $wtmp, 'px');
  21661. } else {
  21662. $table_width = $wtmp;
  21663. }
  21664. $table_width -= (2 * $cellspacing['H']);
  21665. if (!$this->inthead) {
  21666. $this->y += $cellspacing['V'];
  21667. }
  21668. if ($this->rtl) {
  21669. $cellspacingx = -$cellspacing['H'];
  21670. } else {
  21671. $cellspacingx = $cellspacing['H'];
  21672. }
  21673. // total table width without cellspaces
  21674. $table_columns_width = ($table_width - ($cellspacing['H'] * ($dom[$key]['cols'] - 1)));
  21675. // minimum column width
  21676. $table_min_column_width = ($table_columns_width / $dom[$key]['cols']);
  21677. // array of custom column widths
  21678. $table_colwidths = array_fill(0, $dom[$key]['cols'], $table_min_column_width);
  21679. }
  21680. // table row
  21681. if ($dom[$key]['value'] == 'tr') {
  21682. // reset column counter
  21683. $colid = 0;
  21684. }
  21685. // table cell
  21686. if (($dom[$key]['value'] == 'td') OR ($dom[$key]['value'] == 'th')) {
  21687. $trid = $dom[$key]['parent'];
  21688. $table_el = $dom[$trid]['parent'];
  21689. if (!isset($dom[$table_el]['cols'])) {
  21690. $dom[$table_el]['cols'] = $dom[$trid]['cols'];
  21691. }
  21692. // store border info
  21693. $tdborder = 0;
  21694. if (isset($dom[$key]['border']) AND !empty($dom[$key]['border'])) {
  21695. $tdborder = $dom[$key]['border'];
  21696. }
  21697. $colspan = $dom[$key]['attribute']['colspan'];
  21698. $old_cell_padding = $this->cell_padding;
  21699. if (isset($dom[($dom[$trid]['parent'])]['attribute']['cellpadding'])) {
  21700. $crclpd = $this->getHTMLUnitToUnits($dom[($dom[$trid]['parent'])]['attribute']['cellpadding'], 1, 'px');
  21701. $current_cell_padding = array('L' => $crclpd, 'T' => $crclpd, 'R' => $crclpd, 'B' => $crclpd);
  21702. } elseif (isset($dom[($dom[$trid]['parent'])]['padding'])) {
  21703. $current_cell_padding = $dom[($dom[$trid]['parent'])]['padding'];
  21704. } else {
  21705. $current_cell_padding = array('L' => 0, 'T' => 0, 'R' => 0, 'B' => 0);
  21706. }
  21707. $this->cell_padding = $current_cell_padding;
  21708. if (isset($dom[$key]['height'])) {
  21709. // minimum cell height
  21710. $cellh = $this->getHTMLUnitToUnits($dom[$key]['height'], 0, 'px');
  21711. } else {
  21712. $cellh = 0;
  21713. }
  21714. if (isset($dom[$key]['content'])) {
  21715. $cell_content = stripslashes($dom[$key]['content']);
  21716. } else {
  21717. $cell_content = '&nbsp;';
  21718. }
  21719. $tagtype = $dom[$key]['value'];
  21720. $parentid = $key;
  21721. while (($key < $maxel) AND (!(($dom[$key]['tag']) AND (!$dom[$key]['opening']) AND ($dom[$key]['value'] == $tagtype) AND ($dom[$key]['parent'] == $parentid)))) {
  21722. // move $key index forward
  21723. ++$key;
  21724. }
  21725. if (!isset($dom[$trid]['startpage'])) {
  21726. $dom[$trid]['startpage'] = $this->page;
  21727. } else {
  21728. $this->setPage($dom[$trid]['startpage']);
  21729. }
  21730. if (!isset($dom[$trid]['startcolumn'])) {
  21731. $dom[$trid]['startcolumn'] = $this->current_column;
  21732. } elseif ($this->current_column != $dom[$trid]['startcolumn']) {
  21733. $tmpx = $this->x;
  21734. $this->selectColumn($dom[$trid]['startcolumn']);
  21735. $this->x = $tmpx;
  21736. }
  21737. if (!isset($dom[$trid]['starty'])) {
  21738. $dom[$trid]['starty'] = $this->y;
  21739. } else {
  21740. $this->y = $dom[$trid]['starty'];
  21741. }
  21742. if (!isset($dom[$trid]['startx'])) {
  21743. $dom[$trid]['startx'] = $this->x;
  21744. $this->x += $cellspacingx;
  21745. } else {
  21746. $this->x += ($cellspacingx / 2);
  21747. }
  21748. if (isset($dom[$parentid]['attribute']['rowspan'])) {
  21749. $rowspan = intval($dom[$parentid]['attribute']['rowspan']);
  21750. } else {
  21751. $rowspan = 1;
  21752. }
  21753. // skip row-spanned cells started on the previous rows
  21754. if (isset($dom[$table_el]['rowspans'])) {
  21755. $rsk = 0;
  21756. $rskmax = count($dom[$table_el]['rowspans']);
  21757. while ($rsk < $rskmax) {
  21758. $trwsp = $dom[$table_el]['rowspans'][$rsk];
  21759. $rsstartx = $trwsp['startx'];
  21760. $rsendx = $trwsp['endx'];
  21761. // account for margin changes
  21762. if ($trwsp['startpage'] < $this->page) {
  21763. if (($this->rtl) AND ($this->pagedim[$this->page]['orm'] != $this->pagedim[$trwsp['startpage']]['orm'])) {
  21764. $dl = ($this->pagedim[$this->page]['orm'] - $this->pagedim[$trwsp['startpage']]['orm']);
  21765. $rsstartx -= $dl;
  21766. $rsendx -= $dl;
  21767. } elseif ((!$this->rtl) AND ($this->pagedim[$this->page]['olm'] != $this->pagedim[$trwsp['startpage']]['olm'])) {
  21768. $dl = ($this->pagedim[$this->page]['olm'] - $this->pagedim[$trwsp['startpage']]['olm']);
  21769. $rsstartx += $dl;
  21770. $rsendx += $dl;
  21771. }
  21772. }
  21773. if (($trwsp['rowspan'] > 0)
  21774. AND ($rsstartx > ($this->x - $cellspacing['H'] - $current_cell_padding['L'] - $this->feps))
  21775. AND ($rsstartx < ($this->x + $cellspacing['H'] + $current_cell_padding['R'] + $this->feps))
  21776. AND (($trwsp['starty'] < ($this->y - $this->feps)) OR ($trwsp['startpage'] < $this->page) OR ($trwsp['startcolumn'] < $this->current_column))) {
  21777. // set the starting X position of the current cell
  21778. $this->x = $rsendx + $cellspacingx;
  21779. // increment column indicator
  21780. $colid += $trwsp['colspan'];
  21781. if (($trwsp['rowspan'] == 1)
  21782. AND (isset($dom[$trid]['endy']))
  21783. AND (isset($dom[$trid]['endpage']))
  21784. AND (isset($dom[$trid]['endcolumn']))
  21785. AND ($trwsp['endpage'] == $dom[$trid]['endpage'])
  21786. AND ($trwsp['endcolumn'] == $dom[$trid]['endcolumn'])) {
  21787. // set ending Y position for row
  21788. $dom[$table_el]['rowspans'][$rsk]['endy'] = max($dom[$trid]['endy'], $trwsp['endy']);
  21789. $dom[$trid]['endy'] = $dom[$table_el]['rowspans'][$rsk]['endy'];
  21790. }
  21791. $rsk = 0;
  21792. } else {
  21793. ++$rsk;
  21794. }
  21795. }
  21796. }
  21797. if (isset($dom[$parentid]['width'])) {
  21798. // user specified width
  21799. $cellw = $this->getHTMLUnitToUnits($dom[$parentid]['width'], $table_columns_width, 'px');
  21800. $tmpcw = ($cellw / $colspan);
  21801. for ($i = 0; $i < $colspan; ++$i) {
  21802. $table_colwidths[($colid + $i)] = $tmpcw;
  21803. }
  21804. } else {
  21805. // inherit column width
  21806. $cellw = 0;
  21807. for ($i = 0; $i < $colspan; ++$i) {
  21808. $cellw += $table_colwidths[($colid + $i)];
  21809. }
  21810. }
  21811. $cellw += (($colspan - 1) * $cellspacing['H']);
  21812. // increment column indicator
  21813. $colid += $colspan;
  21814. // add rowspan information to table element
  21815. if ($rowspan > 1) {
  21816. $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));
  21817. }
  21818. $cellid = array_push($dom[$trid]['cellpos'], array('startx' => $this->x));
  21819. if ($rowspan > 1) {
  21820. $dom[$trid]['cellpos'][($cellid - 1)]['rowspanid'] = ($trsid - 1);
  21821. }
  21822. // push background colors
  21823. if (isset($dom[$parentid]['bgcolor']) AND ($dom[$parentid]['bgcolor'] !== false)) {
  21824. $dom[$trid]['cellpos'][($cellid - 1)]['bgcolor'] = $dom[$parentid]['bgcolor'];
  21825. }
  21826. // store border info
  21827. if (isset($tdborder) AND !empty($tdborder)) {
  21828. $dom[$trid]['cellpos'][($cellid - 1)]['border'] = $tdborder;
  21829. }
  21830. $prevLastH = $this->lasth;
  21831. // store some info for multicolumn mode
  21832. if ($this->rtl) {
  21833. $this->colxshift['x'] = $this->w - $this->x - $this->rMargin;
  21834. } else {
  21835. $this->colxshift['x'] = $this->x - $this->lMargin;
  21836. }
  21837. $this->colxshift['s'] = $cellspacing;
  21838. $this->colxshift['p'] = $current_cell_padding;
  21839. // ****** write the cell content ******
  21840. $this->MultiCell($cellw, $cellh, $cell_content, false, $lalign, false, 2, '', '', true, 0, true, true, 0, 'T', false);
  21841. // restore some values
  21842. $this->colxshift = array('x' => 0, 's' => array('H' => 0, 'V' => 0), 'p' => array('L' => 0, 'T' => 0, 'R' => 0, 'B' => 0));
  21843. $this->lasth = $prevLastH;
  21844. $this->cell_padding = $old_cell_padding;
  21845. $dom[$trid]['cellpos'][($cellid - 1)]['endx'] = $this->x;
  21846. // update the end of row position
  21847. if ($rowspan <= 1) {
  21848. if (isset($dom[$trid]['endy'])) {
  21849. if (($this->page == $dom[$trid]['endpage']) AND ($this->current_column == $dom[$trid]['endcolumn'])) {
  21850. $dom[$trid]['endy'] = max($this->y, $dom[$trid]['endy']);
  21851. } elseif (($this->page > $dom[$trid]['endpage']) OR ($this->current_column > $dom[$trid]['endcolumn'])) {
  21852. $dom[$trid]['endy'] = $this->y;
  21853. }
  21854. } else {
  21855. $dom[$trid]['endy'] = $this->y;
  21856. }
  21857. if (isset($dom[$trid]['endpage'])) {
  21858. $dom[$trid]['endpage'] = max($this->page, $dom[$trid]['endpage']);
  21859. } else {
  21860. $dom[$trid]['endpage'] = $this->page;
  21861. }
  21862. if (isset($dom[$trid]['endcolumn'])) {
  21863. $dom[$trid]['endcolumn'] = max($this->current_column, $dom[$trid]['endcolumn']);
  21864. } else {
  21865. $dom[$trid]['endcolumn'] = $this->current_column;
  21866. }
  21867. } else {
  21868. // account for row-spanned cells
  21869. $dom[$table_el]['rowspans'][($trsid - 1)]['endx'] = $this->x;
  21870. $dom[$table_el]['rowspans'][($trsid - 1)]['endy'] = $this->y;
  21871. $dom[$table_el]['rowspans'][($trsid - 1)]['endpage'] = $this->page;
  21872. $dom[$table_el]['rowspans'][($trsid - 1)]['endcolumn'] = $this->current_column;
  21873. }
  21874. if (isset($dom[$table_el]['rowspans'])) {
  21875. // update endy and endpage on rowspanned cells
  21876. foreach ($dom[$table_el]['rowspans'] as $k => $trwsp) {
  21877. if ($trwsp['rowspan'] > 0) {
  21878. if (isset($dom[$trid]['endpage'])) {
  21879. if (($trwsp['endpage'] == $dom[$trid]['endpage']) AND ($trwsp['endcolumn'] == $dom[$trid]['endcolumn'])) {
  21880. $dom[$table_el]['rowspans'][$k]['endy'] = max($dom[$trid]['endy'], $trwsp['endy']);
  21881. } elseif (($trwsp['endpage'] < $dom[$trid]['endpage']) OR ($trwsp['endcolumn'] < $dom[$trid]['endcolumn'])) {
  21882. $dom[$table_el]['rowspans'][$k]['endy'] = $dom[$trid]['endy'];
  21883. $dom[$table_el]['rowspans'][$k]['endpage'] = $dom[$trid]['endpage'];
  21884. $dom[$table_el]['rowspans'][$k]['endcolumn'] = $dom[$trid]['endcolumn'];
  21885. } else {
  21886. $dom[$trid]['endy'] = $this->pagedim[$dom[$trid]['endpage']]['hk'] - $this->pagedim[$dom[$trid]['endpage']]['bm'];
  21887. }
  21888. }
  21889. }
  21890. }
  21891. }
  21892. $this->x += ($cellspacingx / 2);
  21893. } else {
  21894. // opening tag (or self-closing tag)
  21895. if (!isset($opentagpos)) {
  21896. if ($this->inxobj) {
  21897. // we are inside an XObject template
  21898. $opentagpos = strlen($this->xobjects[$this->xobjid]['outdata']);
  21899. } elseif (!$this->InFooter) {
  21900. if (isset($this->footerlen[$this->page])) {
  21901. $this->footerpos[$this->page] = $this->pagelen[$this->page] - $this->footerlen[$this->page];
  21902. } else {
  21903. $this->footerpos[$this->page] = $this->pagelen[$this->page];
  21904. }
  21905. $opentagpos = $this->footerpos[$this->page];
  21906. }
  21907. }
  21908. $dom = $this->openHTMLTagHandler($dom, $key, $cell);
  21909. }
  21910. } else { // closing tag
  21911. $prev_numpages = $this->numpages;
  21912. $old_bordermrk = $this->bordermrk[$this->page];
  21913. $dom = $this->closeHTMLTagHandler($dom, $key, $cell, $maxbottomliney);
  21914. if ($this->bordermrk[$this->page] > $old_bordermrk) {
  21915. $startlinepos += ($this->bordermrk[$this->page] - $old_bordermrk);
  21916. }
  21917. if ($prev_numpages > $this->numpages) {
  21918. $startlinepage = $this->page;
  21919. }
  21920. }
  21921. } elseif (strlen($dom[$key]['value']) > 0) {
  21922. // print list-item
  21923. if (!$this->empty_string($this->lispacer) AND ($this->lispacer != '^')) {
  21924. $this->SetFont($pfontname, $pfontstyle, $pfontsize);
  21925. $this->resetLastH();
  21926. $minstartliney = $this->y;
  21927. $maxbottomliney = ($startliney + ($this->FontSize * $this->cell_height_ratio));
  21928. $this->putHtmlListBullet($this->listnum, $this->lispacer, $pfontsize);
  21929. $this->SetFont($curfontname, $curfontstyle, $curfontsize);
  21930. $this->resetLastH();
  21931. if (is_numeric($pfontsize) AND ($pfontsize > 0) AND is_numeric($curfontsize) AND ($curfontsize > 0) AND ($pfontsize != $curfontsize)) {
  21932. $pfontascent = $this->getFontAscent($pfontname, $pfontstyle, $pfontsize);
  21933. $pfontdescent = $this->getFontDescent($pfontname, $pfontstyle, $pfontsize);
  21934. $this->y += ((($pfontsize - $curfontsize) * $this->cell_height_ratio / $this->k) + $pfontascent - $curfontascent - $pfontdescent + $curfontdescent) / 2;
  21935. $minstartliney = min($this->y, $minstartliney);
  21936. $maxbottomliney = max(($this->y + (($pfontsize * $this->cell_height_ratio) / $this->k)), $maxbottomliney);
  21937. }
  21938. }
  21939. // text
  21940. $this->htmlvspace = 0;
  21941. if ((!$this->premode) AND $this->isRTLTextDir()) {
  21942. // reverse spaces order
  21943. $lsp = ''; // left spaces
  21944. $rsp = ''; // right spaces
  21945. if (preg_match('/^('.$this->re_space['p'].'+)/'.$this->re_space['m'], $dom[$key]['value'], $matches)) {
  21946. $lsp = $matches[1];
  21947. }
  21948. if (preg_match('/('.$this->re_space['p'].'+)$/'.$this->re_space['m'], $dom[$key]['value'], $matches)) {
  21949. $rsp = $matches[1];
  21950. }
  21951. $dom[$key]['value'] = $rsp.$this->stringTrim($dom[$key]['value']).$lsp;
  21952. }
  21953. if ($newline) {
  21954. if (!$this->premode) {
  21955. $prelen = strlen($dom[$key]['value']);
  21956. if ($this->isRTLTextDir()) {
  21957. // right trim except non-breaking space
  21958. $dom[$key]['value'] = $this->stringRightTrim($dom[$key]['value']);
  21959. } else {
  21960. // left trim except non-breaking space
  21961. $dom[$key]['value'] = $this->stringLeftTrim($dom[$key]['value']);
  21962. }
  21963. $postlen = strlen($dom[$key]['value']);
  21964. if (($postlen == 0) AND ($prelen > 0)) {
  21965. $dom[$key]['trimmed_space'] = true;
  21966. }
  21967. }
  21968. $newline = false;
  21969. $firstblock = true;
  21970. } else {
  21971. $firstblock = false;
  21972. // replace empty multiple spaces string with a single space
  21973. $dom[$key]['value'] = preg_replace('/^'.$this->re_space['p'].'+$/'.$this->re_space['m'], chr(32), $dom[$key]['value']);
  21974. }
  21975. $strrest = '';
  21976. if ($this->rtl) {
  21977. $this->x -= $this->textindent;
  21978. } else {
  21979. $this->x += $this->textindent;
  21980. }
  21981. if (!isset($dom[$key]['trimmed_space']) OR !$dom[$key]['trimmed_space']) {
  21982. $strlinelen = $this->GetStringWidth($dom[$key]['value']);
  21983. if (!empty($this->HREF) AND (isset($this->HREF['url']))) {
  21984. // HTML <a> Link
  21985. $hrefcolor = '';
  21986. if (isset($dom[($dom[$key]['parent'])]['fgcolor']) AND ($dom[($dom[$key]['parent'])]['fgcolor'] !== false)) {
  21987. $hrefcolor = $dom[($dom[$key]['parent'])]['fgcolor'];
  21988. }
  21989. $hrefstyle = -1;
  21990. if (isset($dom[($dom[$key]['parent'])]['fontstyle']) AND ($dom[($dom[$key]['parent'])]['fontstyle'] !== false)) {
  21991. $hrefstyle = $dom[($dom[$key]['parent'])]['fontstyle'];
  21992. }
  21993. $strrest = $this->addHtmlLink($this->HREF['url'], $dom[$key]['value'], $wfill, true, $hrefcolor, $hrefstyle, true);
  21994. } else {
  21995. $wadj = 0; // space to leave for block continuity
  21996. if ($this->rtl) {
  21997. $cwa = $this->x - $this->lMargin;
  21998. } else {
  21999. $cwa = $this->w - $this->rMargin - $this->x;
  22000. }
  22001. if (($strlinelen < $cwa) AND (isset($dom[($key + 1)])) AND ($dom[($key + 1)]['tag']) AND (!$dom[($key + 1)]['block'])) {
  22002. // check the next text blocks for continuity
  22003. $nkey = ($key + 1);
  22004. $write_block = true;
  22005. $same_textdir = true;
  22006. $tmp_fontname = $this->FontFamily;
  22007. $tmp_fontstyle = $this->FontStyle;
  22008. $tmp_fontsize = $this->FontSizePt;
  22009. while ($write_block AND isset($dom[$nkey])) {
  22010. if ($dom[$nkey]['tag']) {
  22011. if ($dom[$nkey]['block']) {
  22012. // end of block
  22013. $write_block = false;
  22014. }
  22015. $tmp_fontname = isset($dom[$nkey]['fontname']) ? $dom[$nkey]['fontname'] : $this->FontFamily;
  22016. $tmp_fontstyle = isset($dom[$nkey]['fontstyle']) ? $dom[$nkey]['fontstyle'] : $this->FontStyle;
  22017. $tmp_fontsize = isset($dom[$nkey]['fontsize']) ? $dom[$nkey]['fontsize'] : $this->FontSizePt;
  22018. $same_textdir = ($dom[$nkey]['dir'] == $dom[$key]['dir']);
  22019. } else {
  22020. $nextstr = preg_split('/'.$this->re_space['p'].'+/'.$this->re_space['m'], $dom[$nkey]['value']);
  22021. if (isset($nextstr[0]) AND $same_textdir) {
  22022. $wadj += $this->GetStringWidth($nextstr[0], $tmp_fontname, $tmp_fontstyle, $tmp_fontsize);
  22023. }
  22024. if (isset($nextstr[1])) {
  22025. $write_block = false;
  22026. }
  22027. }
  22028. ++$nkey;
  22029. }
  22030. }
  22031. if (($wadj > 0) AND (($strlinelen + $wadj) >= $cwa)) {
  22032. $wadj = 0;
  22033. $nextstr = preg_split('/'.$this->re_space['p'].'/'.$this->re_space['m'], $dom[$key]['value']);
  22034. $numblks = count($nextstr);
  22035. if ($numblks > 1) {
  22036. // try to split on blank spaces
  22037. $wadj = ($cwa - $strlinelen + $this->GetStringWidth($nextstr[($numblks - 1)]));
  22038. }
  22039. }
  22040. // check for reversed text direction
  22041. if (($wadj > 0) AND (($this->rtl AND ($this->tmprtl === 'L')) OR (!$this->rtl AND ($this->tmprtl === 'R')))) {
  22042. // LTR text on RTL direction or RTL text on LTR direction
  22043. $reverse_dir = true;
  22044. $this->rtl = !$this->rtl;
  22045. $revshift = ($strlinelen + $wadj + 0.000001); // add little quantity for rounding problems
  22046. if ($this->rtl) {
  22047. $this->x += $revshift;
  22048. } else {
  22049. $this->x -= $revshift;
  22050. }
  22051. $xws = $this->x;
  22052. }
  22053. // ****** write only until the end of the line and get the rest ******
  22054. $strrest = $this->Write($this->lasth, $dom[$key]['value'], '', $wfill, '', false, 0, true, $firstblock, 0, $wadj);
  22055. // restore default direction
  22056. if ($reverse_dir AND ($wadj == 0)) {
  22057. $this->x = $xws;
  22058. $this->rtl = !$this->rtl;
  22059. $reverse_dir = false;
  22060. }
  22061. }
  22062. }
  22063. $this->textindent = 0;
  22064. if (strlen($strrest) > 0) {
  22065. // store the remaining string on the previous $key position
  22066. $this->newline = true;
  22067. if ($strrest == $dom[$key]['value']) {
  22068. // used to avoid infinite loop
  22069. ++$loop;
  22070. } else {
  22071. $loop = 0;
  22072. }
  22073. $dom[$key]['value'] = $strrest;
  22074. if ($cell) {
  22075. if ($this->rtl) {
  22076. $this->x -= $this->cell_padding['R'];
  22077. } else {
  22078. $this->x += $this->cell_padding['L'];
  22079. }
  22080. }
  22081. if ($loop < 3) {
  22082. --$key;
  22083. }
  22084. } else {
  22085. $loop = 0;
  22086. }
  22087. }
  22088. ++$key;
  22089. 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')) {
  22090. // check if we are on a new page or on a new column
  22091. if ((!$undo) AND (($this->y < $this->start_transaction_y) OR (($dom[$key]['value'] == 'tr') AND ($dom[($dom[$key]['parent'])]['endy'] < $this->start_transaction_y)))) {
  22092. // we are on a new page or on a new column and the total object height is less than the available vertical space.
  22093. // restore previous object
  22094. $this->rollbackTransaction(true);
  22095. // restore previous values
  22096. foreach ($this_method_vars as $vkey => $vval) {
  22097. $$vkey = $vval;
  22098. }
  22099. // add a page (or trig AcceptPageBreak() for multicolumn mode)
  22100. $pre_y = $this->y;
  22101. if ((!$this->checkPageBreak($this->PageBreakTrigger + 1)) AND ($this->y < $pre_y)) {
  22102. $startliney = $this->y;
  22103. }
  22104. $undo = true; // avoid infinite loop
  22105. } else {
  22106. $undo = false;
  22107. }
  22108. }
  22109. } // end for each $key
  22110. // align the last line
  22111. if (isset($startlinex)) {
  22112. $yshift = ($minstartliney - $startliney);
  22113. if (($yshift > 0) OR ($this->page > $startlinepage)) {
  22114. $yshift = 0;
  22115. }
  22116. $t_x = 0;
  22117. // the last line must be shifted to be aligned as requested
  22118. $linew = abs($this->endlinex - $startlinex);
  22119. if ($this->inxobj) {
  22120. // we are inside an XObject template
  22121. $pstart = substr($this->xobjects[$this->xobjid]['outdata'], 0, $startlinepos);
  22122. if (isset($opentagpos)) {
  22123. $midpos = $opentagpos;
  22124. } else {
  22125. $midpos = 0;
  22126. }
  22127. if ($midpos > 0) {
  22128. $pmid = substr($this->xobjects[$this->xobjid]['outdata'], $startlinepos, ($midpos - $startlinepos));
  22129. $pend = substr($this->xobjects[$this->xobjid]['outdata'], $midpos);
  22130. } else {
  22131. $pmid = substr($this->xobjects[$this->xobjid]['outdata'], $startlinepos);
  22132. $pend = '';
  22133. }
  22134. } else {
  22135. $pstart = substr($this->getPageBuffer($startlinepage), 0, $startlinepos);
  22136. if (isset($opentagpos) AND isset($this->footerlen[$startlinepage]) AND (!$this->InFooter)) {
  22137. $this->footerpos[$startlinepage] = $this->pagelen[$startlinepage] - $this->footerlen[$startlinepage];
  22138. $midpos = min($opentagpos, $this->footerpos[$startlinepage]);
  22139. } elseif (isset($opentagpos)) {
  22140. $midpos = $opentagpos;
  22141. } elseif (isset($this->footerlen[$startlinepage]) AND (!$this->InFooter)) {
  22142. $this->footerpos[$startlinepage] = $this->pagelen[$startlinepage] - $this->footerlen[$startlinepage];
  22143. $midpos = $this->footerpos[$startlinepage];
  22144. } else {
  22145. $midpos = 0;
  22146. }
  22147. if ($midpos > 0) {
  22148. $pmid = substr($this->getPageBuffer($startlinepage), $startlinepos, ($midpos - $startlinepos));
  22149. $pend = substr($this->getPageBuffer($startlinepage), $midpos);
  22150. } else {
  22151. $pmid = substr($this->getPageBuffer($startlinepage), $startlinepos);
  22152. $pend = '';
  22153. }
  22154. }
  22155. if ((isset($plalign) AND ((($plalign == 'C') OR (($plalign == 'R') AND (!$this->rtl)) OR (($plalign == 'L') AND ($this->rtl)))))) {
  22156. // calculate shifting amount
  22157. $tw = $w;
  22158. if ($this->lMargin != $prevlMargin) {
  22159. $tw += ($prevlMargin - $this->lMargin);
  22160. }
  22161. if ($this->rMargin != $prevrMargin) {
  22162. $tw += ($prevrMargin - $this->rMargin);
  22163. }
  22164. $one_space_width = $this->GetStringWidth(chr(32));
  22165. $no = 0; // number of spaces on a line contained on a single block
  22166. if ($this->isRTLTextDir()) { // RTL
  22167. // remove left space if exist
  22168. $pos1 = $this->revstrpos($pmid, '[(');
  22169. if ($pos1 > 0) {
  22170. $pos1 = intval($pos1);
  22171. if ($this->isUnicodeFont()) {
  22172. $pos2 = intval($this->revstrpos($pmid, '[('.chr(0).chr(32)));
  22173. $spacelen = 2;
  22174. } else {
  22175. $pos2 = intval($this->revstrpos($pmid, '[('.chr(32)));
  22176. $spacelen = 1;
  22177. }
  22178. if ($pos1 == $pos2) {
  22179. $pmid = substr($pmid, 0, ($pos1 + 2)).substr($pmid, ($pos1 + 2 + $spacelen));
  22180. if (substr($pmid, $pos1, 4) == '[()]') {
  22181. $linew -= $one_space_width;
  22182. } elseif ($pos1 == strpos($pmid, '[(')) {
  22183. $no = 1;
  22184. }
  22185. }
  22186. }
  22187. } else { // LTR
  22188. // remove right space if exist
  22189. $pos1 = $this->revstrpos($pmid, ')]');
  22190. if ($pos1 > 0) {
  22191. $pos1 = intval($pos1);
  22192. if ($this->isUnicodeFont()) {
  22193. $pos2 = intval($this->revstrpos($pmid, chr(0).chr(32).')]')) + 2;
  22194. $spacelen = 2;
  22195. } else {
  22196. $pos2 = intval($this->revstrpos($pmid, chr(32).')]')) + 1;
  22197. $spacelen = 1;
  22198. }
  22199. if ($pos1 == $pos2) {
  22200. $pmid = substr($pmid, 0, ($pos1 - $spacelen)).substr($pmid, $pos1);
  22201. $linew -= $one_space_width;
  22202. }
  22203. }
  22204. }
  22205. $mdiff = ($tw - $linew);
  22206. if ($plalign == 'C') {
  22207. if ($this->rtl) {
  22208. $t_x = -($mdiff / 2);
  22209. } else {
  22210. $t_x = ($mdiff / 2);
  22211. }
  22212. } elseif ($plalign == 'R') {
  22213. // right alignment on LTR document
  22214. $t_x = $mdiff;
  22215. } elseif ($plalign == 'L') {
  22216. // left alignment on RTL document
  22217. $t_x = -$mdiff;
  22218. }
  22219. } // end if startlinex
  22220. if (($t_x != 0) OR ($yshift < 0)) {
  22221. // shift the line
  22222. $trx = sprintf('1 0 0 1 %.3F %.3F cm', ($t_x * $this->k), ($yshift * $this->k));
  22223. $pstart .= "\nq\n".$trx."\n".$pmid."\nQ\n";
  22224. $endlinepos = strlen($pstart);
  22225. if ($this->inxobj) {
  22226. // we are inside an XObject template
  22227. $this->xobjects[$this->xobjid]['outdata'] = $pstart.$pend;
  22228. foreach ($this->xobjects[$this->xobjid]['annotations'] as $pak => $pac) {
  22229. if ($pak >= $pask) {
  22230. $this->xobjects[$this->xobjid]['annotations'][$pak]['x'] += $t_x;
  22231. $this->xobjects[$this->xobjid]['annotations'][$pak]['y'] -= $yshift;
  22232. }
  22233. }
  22234. } else {
  22235. $this->setPageBuffer($startlinepage, $pstart.$pend);
  22236. // shift the annotations and links
  22237. if (isset($this->PageAnnots[$this->page])) {
  22238. foreach ($this->PageAnnots[$this->page] as $pak => $pac) {
  22239. if ($pak >= $pask) {
  22240. $this->PageAnnots[$this->page][$pak]['x'] += $t_x;
  22241. $this->PageAnnots[$this->page][$pak]['y'] -= $yshift;
  22242. }
  22243. }
  22244. }
  22245. }
  22246. $this->y -= $yshift;
  22247. $yshift = 0;
  22248. }
  22249. }
  22250. // restore previous values
  22251. $this->setGraphicVars($gvars);
  22252. if ($this->num_columns > 1) {
  22253. $this->selectColumn();
  22254. } elseif ($this->page > $prevPage) {
  22255. $this->lMargin = $this->pagedim[$this->page]['olm'];
  22256. $this->rMargin = $this->pagedim[$this->page]['orm'];
  22257. }
  22258. // restore previous list state
  22259. $this->cell_height_ratio = $prev_cell_height_ratio;
  22260. $this->listnum = $prev_listnum;
  22261. $this->listordered = $prev_listordered;
  22262. $this->listcount = $prev_listcount;
  22263. $this->lispacer = $prev_lispacer;
  22264. if ($ln AND (!($cell AND ($dom[$key-1]['value'] == 'table')))) {
  22265. $this->Ln($this->lasth);
  22266. if ($this->y < $maxbottomliney) {
  22267. $this->y = $maxbottomliney;
  22268. }
  22269. }
  22270. unset($dom);
  22271. }
  22272. /**
  22273. * Process opening tags.
  22274. * @param $dom (array) html dom array
  22275. * @param $key (int) current element id
  22276. * @param $cell (boolean) if true add the default left (or right if RTL) padding to each new line (default false).
  22277. * @return $dom array
  22278. * @protected
  22279. */
  22280. protected function openHTMLTagHandler($dom, $key, $cell) {
  22281. $tag = $dom[$key];
  22282. $parent = $dom[($dom[$key]['parent'])];
  22283. $firsttag = ($key == 1);
  22284. // check for text direction attribute
  22285. if (isset($tag['dir'])) {
  22286. $this->setTempRTL($tag['dir']);
  22287. } else {
  22288. $this->tmprtl = false;
  22289. }
  22290. if ($tag['block']) {
  22291. $hbz = 0; // distance from y to line bottom
  22292. $hb = 0; // vertical space between block tags
  22293. // calculate vertical space for block tags
  22294. if (isset($this->tagvspaces[$tag['value']][0]['h']) AND ($this->tagvspaces[$tag['value']][0]['h'] >= 0)) {
  22295. $cur_h = $this->tagvspaces[$tag['value']][0]['h'];
  22296. } elseif (isset($tag['fontsize'])) {
  22297. $cur_h = ($tag['fontsize'] / $this->k) * $this->cell_height_ratio;
  22298. } else {
  22299. $cur_h = $this->FontSize * $this->cell_height_ratio;
  22300. }
  22301. if (isset($this->tagvspaces[$tag['value']][0]['n'])) {
  22302. $n = $this->tagvspaces[$tag['value']][0]['n'];
  22303. } elseif (preg_match('/[h][0-9]/', $tag['value']) > 0) {
  22304. $n = 0.6;
  22305. } else {
  22306. $n = 1;
  22307. }
  22308. if ((!isset($this->tagvspaces[$tag['value']])) AND (in_array($tag['value'], array('div', 'dt', 'dd', 'li', 'br')))) {
  22309. $hb = 0;
  22310. } else {
  22311. $hb = ($n * $cur_h);
  22312. }
  22313. if (($this->htmlvspace <= 0) AND ($n > 0)) {
  22314. if (isset($parent['fontsize'])) {
  22315. $hbz = (($parent['fontsize'] / $this->k) * $this->cell_height_ratio);
  22316. } else {
  22317. $hbz = $this->FontSize * $this->cell_height_ratio;
  22318. }
  22319. }
  22320. }
  22321. // Opening tag
  22322. switch($tag['value']) {
  22323. case 'table': {
  22324. $cp = 0;
  22325. $cs = 0;
  22326. $dom[$key]['rowspans'] = array();
  22327. if (!isset($dom[$key]['attribute']['nested']) OR ($dom[$key]['attribute']['nested'] != 'true')) {
  22328. // set table header
  22329. if (!$this->empty_string($dom[$key]['thead'])) {
  22330. // set table header
  22331. $this->thead = $dom[$key]['thead'];
  22332. if (!isset($this->theadMargins) OR (empty($this->theadMargins))) {
  22333. $this->theadMargins = array();
  22334. $this->theadMargins['cell_padding'] = $this->cell_padding;
  22335. $this->theadMargins['lmargin'] = $this->lMargin;
  22336. $this->theadMargins['rmargin'] = $this->rMargin;
  22337. $this->theadMargins['page'] = $this->page;
  22338. $this->theadMargins['cell'] = $cell;
  22339. }
  22340. }
  22341. }
  22342. // store current margins and page
  22343. $dom[$key]['old_cell_padding'] = $this->cell_padding;
  22344. if (isset($tag['attribute']['cellpadding'])) {
  22345. $pad = $this->getHTMLUnitToUnits($tag['attribute']['cellpadding'], 1, 'px');
  22346. $this->SetCellPadding($pad);
  22347. } elseif (isset($tag['padding'])) {
  22348. $this->cell_padding = $tag['padding'];
  22349. }
  22350. if (isset($tag['attribute']['cellspacing'])) {
  22351. $cs = $this->getHTMLUnitToUnits($tag['attribute']['cellspacing'], 1, 'px');
  22352. } elseif (isset($tag['border-spacing'])) {
  22353. $cs = $tag['border-spacing']['V'];
  22354. }
  22355. $prev_y = $this->y;
  22356. if ($this->checkPageBreak(((2 * $cp) + (2 * $cs) + $this->lasth), '', false) OR ($this->y < $prev_y)) {
  22357. $this->inthead = true;
  22358. // add a page (or trig AcceptPageBreak() for multicolumn mode)
  22359. $this->checkPageBreak($this->PageBreakTrigger + 1);
  22360. }
  22361. break;
  22362. }
  22363. case 'tr': {
  22364. // array of columns positions
  22365. $dom[$key]['cellpos'] = array();
  22366. break;
  22367. }
  22368. case 'hr': {
  22369. if ((isset($tag['height'])) AND ($tag['height'] != '')) {
  22370. $hrHeight = $this->getHTMLUnitToUnits($tag['height'], 1, 'px');
  22371. } else {
  22372. $hrHeight = $this->GetLineWidth();
  22373. }
  22374. $this->addHTMLVertSpace($hbz, ($hrHeight / 2), $cell, $firsttag);
  22375. $x = $this->GetX();
  22376. $y = $this->GetY();
  22377. $wtmp = $this->w - $this->lMargin - $this->rMargin;
  22378. if ($cell) {
  22379. $wtmp -= ($this->cell_padding['L'] + $this->cell_padding['R']);
  22380. }
  22381. if ((isset($tag['width'])) AND ($tag['width'] != '')) {
  22382. $hrWidth = $this->getHTMLUnitToUnits($tag['width'], $wtmp, 'px');
  22383. } else {
  22384. $hrWidth = $wtmp;
  22385. }
  22386. $prevlinewidth = $this->GetLineWidth();
  22387. $this->SetLineWidth($hrHeight);
  22388. $this->Line($x, $y, $x + $hrWidth, $y);
  22389. $this->SetLineWidth($prevlinewidth);
  22390. $this->addHTMLVertSpace(($hrHeight / 2), 0, $cell, !isset($dom[($key + 1)]));
  22391. break;
  22392. }
  22393. case 'a': {
  22394. if (array_key_exists('href', $tag['attribute'])) {
  22395. $this->HREF['url'] = $tag['attribute']['href'];
  22396. }
  22397. break;
  22398. }
  22399. case 'img': {
  22400. if (isset($tag['attribute']['src'])) {
  22401. if ($tag['attribute']['src']{0} === '@') {
  22402. // data stream
  22403. $tag['attribute']['src'] = '@'.base64_decode(substr($tag['attribute']['src'], 1));
  22404. $type = '';
  22405. } else {
  22406. // check for images without protocol
  22407. if (preg_match('%^/{2}%', $tag['attribute']['src'])) {
  22408. $tag['attribute']['src'] = 'http:'.$tag['attribute']['src'];
  22409. }
  22410. // replace relative path with real server path
  22411. if (($tag['attribute']['src'][0] == '/') AND !empty($_SERVER['DOCUMENT_ROOT']) AND ($_SERVER['DOCUMENT_ROOT'] != '/')) {
  22412. $findroot = strpos($tag['attribute']['src'], $_SERVER['DOCUMENT_ROOT']);
  22413. if (($findroot === false) OR ($findroot > 1)) {
  22414. if (substr($_SERVER['DOCUMENT_ROOT'], -1) == '/') {
  22415. $tag['attribute']['src'] = substr($_SERVER['DOCUMENT_ROOT'], 0, -1).$tag['attribute']['src'];
  22416. } else {
  22417. $tag['attribute']['src'] = $_SERVER['DOCUMENT_ROOT'].$tag['attribute']['src'];
  22418. }
  22419. }
  22420. }
  22421. $tag['attribute']['src'] = htmlspecialchars_decode(urldecode($tag['attribute']['src']));
  22422. $type = $this->getImageFileType($tag['attribute']['src']);
  22423. $testscrtype = @parse_url($tag['attribute']['src']);
  22424. if (!isset($testscrtype['query']) OR empty($testscrtype['query'])) {
  22425. // convert URL to server path
  22426. $tag['attribute']['src'] = str_replace(K_PATH_URL, K_PATH_MAIN, $tag['attribute']['src']);
  22427. }
  22428. }
  22429. if (!isset($tag['width'])) {
  22430. $tag['width'] = 0;
  22431. }
  22432. if (!isset($tag['height'])) {
  22433. $tag['height'] = 0;
  22434. }
  22435. //if (!isset($tag['attribute']['align'])) {
  22436. // the only alignment supported is "bottom"
  22437. // further development is required for other modes.
  22438. $tag['attribute']['align'] = 'bottom';
  22439. //}
  22440. switch($tag['attribute']['align']) {
  22441. case 'top': {
  22442. $align = 'T';
  22443. break;
  22444. }
  22445. case 'middle': {
  22446. $align = 'M';
  22447. break;
  22448. }
  22449. case 'bottom': {
  22450. $align = 'B';
  22451. break;
  22452. }
  22453. default: {
  22454. $align = 'B';
  22455. break;
  22456. }
  22457. }
  22458. $prevy = $this->y;
  22459. $xpos = $this->x;
  22460. $imglink = '';
  22461. if (isset($this->HREF['url']) AND !$this->empty_string($this->HREF['url'])) {
  22462. $imglink = $this->HREF['url'];
  22463. if ($imglink{0} == '#') {
  22464. // convert url to internal link
  22465. $lnkdata = explode(',', $imglink);
  22466. if (isset($lnkdata[0])) {
  22467. $page = intval(substr($lnkdata[0], 1));
  22468. if (empty($page) OR ($page <= 0)) {
  22469. $page = $this->page;
  22470. }
  22471. if (isset($lnkdata[1]) AND (strlen($lnkdata[1]) > 0)) {
  22472. $lnky = floatval($lnkdata[1]);
  22473. } else {
  22474. $lnky = 0;
  22475. }
  22476. $imglink = $this->AddLink();
  22477. $this->SetLink($imglink, $lnky, $page);
  22478. }
  22479. }
  22480. }
  22481. $border = 0;
  22482. if (isset($tag['border']) AND !empty($tag['border'])) {
  22483. // currently only support 1 (frame) or a combination of 'LTRB'
  22484. $border = $tag['border'];
  22485. }
  22486. $iw = '';
  22487. if (isset($tag['width'])) {
  22488. $iw = $this->getHTMLUnitToUnits($tag['width'], 1, 'px', false);
  22489. }
  22490. $ih = '';
  22491. if (isset($tag['height'])) {
  22492. $ih = $this->getHTMLUnitToUnits($tag['height'], 1, 'px', false);
  22493. }
  22494. if (($type == 'eps') OR ($type == 'ai')) {
  22495. $this->ImageEps($tag['attribute']['src'], $xpos, $this->y, $iw, $ih, $imglink, true, $align, '', $border, true);
  22496. } elseif ($type == 'svg') {
  22497. $this->ImageSVG($tag['attribute']['src'], $xpos, $this->y, $iw, $ih, $imglink, $align, '', $border, true);
  22498. } else {
  22499. $this->Image($tag['attribute']['src'], $xpos, $this->y, $iw, $ih, '', $imglink, $align, false, 300, '', false, false, $border, false, false, true);
  22500. }
  22501. switch($align) {
  22502. case 'T': {
  22503. $this->y = $prevy;
  22504. break;
  22505. }
  22506. case 'M': {
  22507. $this->y = (($this->img_rb_y + $prevy - ($tag['fontsize'] / $this->k)) / 2) ;
  22508. break;
  22509. }
  22510. case 'B': {
  22511. $this->y = $this->img_rb_y - ($tag['fontsize'] / $this->k);
  22512. break;
  22513. }
  22514. }
  22515. }
  22516. break;
  22517. }
  22518. case 'dl': {
  22519. ++$this->listnum;
  22520. if ($this->listnum == 1) {
  22521. $this->addHTMLVertSpace($hbz, $hb, $cell, $firsttag);
  22522. } else {
  22523. $this->addHTMLVertSpace(0, 0, $cell, $firsttag);
  22524. }
  22525. break;
  22526. }
  22527. case 'dt': {
  22528. $this->addHTMLVertSpace($hbz, $hb, $cell, $firsttag);
  22529. break;
  22530. }
  22531. case 'dd': {
  22532. if ($this->rtl) {
  22533. $this->rMargin += $this->listindent;
  22534. } else {
  22535. $this->lMargin += $this->listindent;
  22536. }
  22537. ++$this->listindentlevel;
  22538. $this->addHTMLVertSpace($hbz, $hb, $cell, $firsttag);
  22539. break;
  22540. }
  22541. case 'ul':
  22542. case 'ol': {
  22543. ++$this->listnum;
  22544. if ($tag['value'] == 'ol') {
  22545. $this->listordered[$this->listnum] = true;
  22546. } else {
  22547. $this->listordered[$this->listnum] = false;
  22548. }
  22549. if (isset($tag['attribute']['start'])) {
  22550. $this->listcount[$this->listnum] = intval($tag['attribute']['start']) - 1;
  22551. } else {
  22552. $this->listcount[$this->listnum] = 0;
  22553. }
  22554. if ($this->rtl) {
  22555. $this->rMargin += $this->listindent;
  22556. $this->x -= $this->listindent;
  22557. } else {
  22558. $this->lMargin += $this->listindent;
  22559. $this->x += $this->listindent;
  22560. }
  22561. ++$this->listindentlevel;
  22562. if ($this->listnum == 1) {
  22563. if ($key > 1) {
  22564. $this->addHTMLVertSpace($hbz, $hb, $cell, $firsttag);
  22565. }
  22566. } else {
  22567. $this->addHTMLVertSpace(0, 0, $cell, $firsttag);
  22568. }
  22569. break;
  22570. }
  22571. case 'li': {
  22572. if ($key > 2) {
  22573. $this->addHTMLVertSpace($hbz, $hb, $cell, $firsttag);
  22574. }
  22575. if ($this->listordered[$this->listnum]) {
  22576. // ordered item
  22577. if (isset($parent['attribute']['type']) AND !$this->empty_string($parent['attribute']['type'])) {
  22578. $this->lispacer = $parent['attribute']['type'];
  22579. } elseif (isset($parent['listtype']) AND !$this->empty_string($parent['listtype'])) {
  22580. $this->lispacer = $parent['listtype'];
  22581. } elseif (isset($this->lisymbol) AND !$this->empty_string($this->lisymbol)) {
  22582. $this->lispacer = $this->lisymbol;
  22583. } else {
  22584. $this->lispacer = '#';
  22585. }
  22586. ++$this->listcount[$this->listnum];
  22587. if (isset($tag['attribute']['value'])) {
  22588. $this->listcount[$this->listnum] = intval($tag['attribute']['value']);
  22589. }
  22590. } else {
  22591. // unordered item
  22592. if (isset($parent['attribute']['type']) AND !$this->empty_string($parent['attribute']['type'])) {
  22593. $this->lispacer = $parent['attribute']['type'];
  22594. } elseif (isset($parent['listtype']) AND !$this->empty_string($parent['listtype'])) {
  22595. $this->lispacer = $parent['listtype'];
  22596. } elseif (isset($this->lisymbol) AND !$this->empty_string($this->lisymbol)) {
  22597. $this->lispacer = $this->lisymbol;
  22598. } else {
  22599. $this->lispacer = '!';
  22600. }
  22601. }
  22602. break;
  22603. }
  22604. case 'blockquote': {
  22605. if ($this->rtl) {
  22606. $this->rMargin += $this->listindent;
  22607. } else {
  22608. $this->lMargin += $this->listindent;
  22609. }
  22610. ++$this->listindentlevel;
  22611. $this->addHTMLVertSpace($hbz, $hb, $cell, $firsttag);
  22612. break;
  22613. }
  22614. case 'br': {
  22615. $this->addHTMLVertSpace($hbz, $hb, $cell, $firsttag);
  22616. break;
  22617. }
  22618. case 'div': {
  22619. $this->addHTMLVertSpace($hbz, $hb, $cell, $firsttag);
  22620. break;
  22621. }
  22622. case 'p': {
  22623. $this->addHTMLVertSpace($hbz, $hb, $cell, $firsttag);
  22624. break;
  22625. }
  22626. case 'pre': {
  22627. $this->addHTMLVertSpace($hbz, $hb, $cell, $firsttag);
  22628. $this->premode = true;
  22629. break;
  22630. }
  22631. case 'sup': {
  22632. $this->SetXY($this->GetX(), $this->GetY() - ((0.7 * $this->FontSizePt) / $this->k));
  22633. break;
  22634. }
  22635. case 'sub': {
  22636. $this->SetXY($this->GetX(), $this->GetY() + ((0.3 * $this->FontSizePt) / $this->k));
  22637. break;
  22638. }
  22639. case 'h1':
  22640. case 'h2':
  22641. case 'h3':
  22642. case 'h4':
  22643. case 'h5':
  22644. case 'h6': {
  22645. $this->addHTMLVertSpace($hbz, $hb, $cell, $firsttag);
  22646. break;
  22647. }
  22648. // Form fields (since 4.8.000 - 2009-09-07)
  22649. case 'form': {
  22650. if (isset($tag['attribute']['action'])) {
  22651. $this->form_action = $tag['attribute']['action'];
  22652. } else {
  22653. $this->form_action = K_PATH_URL.$_SERVER['SCRIPT_NAME'];
  22654. }
  22655. if (isset($tag['attribute']['enctype'])) {
  22656. $this->form_enctype = $tag['attribute']['enctype'];
  22657. } else {
  22658. $this->form_enctype = 'application/x-www-form-urlencoded';
  22659. }
  22660. if (isset($tag['attribute']['method'])) {
  22661. $this->form_mode = $tag['attribute']['method'];
  22662. } else {
  22663. $this->form_mode = 'post';
  22664. }
  22665. break;
  22666. }
  22667. case 'input': {
  22668. if (isset($tag['attribute']['name']) AND !$this->empty_string($tag['attribute']['name'])) {
  22669. $name = $tag['attribute']['name'];
  22670. } else {
  22671. break;
  22672. }
  22673. $prop = array();
  22674. $opt = array();
  22675. if (isset($tag['attribute']['readonly']) AND !$this->empty_string($tag['attribute']['readonly'])) {
  22676. $prop['readonly'] = true;
  22677. }
  22678. if (isset($tag['attribute']['value']) AND !$this->empty_string($tag['attribute']['value'])) {
  22679. $value = $tag['attribute']['value'];
  22680. }
  22681. if (isset($tag['attribute']['maxlength']) AND !$this->empty_string($tag['attribute']['maxlength'])) {
  22682. $opt['maxlen'] = intval($tag['attribute']['value']);
  22683. }
  22684. $h = $this->FontSize * $this->cell_height_ratio;
  22685. if (isset($tag['attribute']['size']) AND !$this->empty_string($tag['attribute']['size'])) {
  22686. $w = intval($tag['attribute']['size']) * $this->GetStringWidth(chr(32)) * 2;
  22687. } else {
  22688. $w = $h;
  22689. }
  22690. if (isset($tag['attribute']['checked']) AND (($tag['attribute']['checked'] == 'checked') OR ($tag['attribute']['checked'] == 'true'))) {
  22691. $checked = true;
  22692. } else {
  22693. $checked = false;
  22694. }
  22695. if (isset($tag['align'])) {
  22696. switch ($tag['align']) {
  22697. case 'C': {
  22698. $opt['q'] = 1;
  22699. break;
  22700. }
  22701. case 'R': {
  22702. $opt['q'] = 2;
  22703. break;
  22704. }
  22705. case 'L':
  22706. default: {
  22707. break;
  22708. }
  22709. }
  22710. }
  22711. switch ($tag['attribute']['type']) {
  22712. case 'text': {
  22713. if (isset($value)) {
  22714. $opt['v'] = $value;
  22715. }
  22716. $this->TextField($name, $w, $h, $prop, $opt, '', '', false);
  22717. break;
  22718. }
  22719. case 'password': {
  22720. if (isset($value)) {
  22721. $opt['v'] = $value;
  22722. }
  22723. $prop['password'] = 'true';
  22724. $this->TextField($name, $w, $h, $prop, $opt, '', '', false);
  22725. break;
  22726. }
  22727. case 'checkbox': {
  22728. $this->CheckBox($name, $w, $checked, $prop, $opt, $value, '', '', false);
  22729. break;
  22730. }
  22731. case 'radio': {
  22732. $this->RadioButton($name, $w, $prop, $opt, $value, $checked, '', '', false);
  22733. break;
  22734. }
  22735. case 'submit': {
  22736. $w = $this->GetStringWidth($value) * 1.5;
  22737. $h *= 1.6;
  22738. $prop = array('lineWidth'=>1, 'borderStyle'=>'beveled', 'fillColor'=>array(196, 196, 196), 'strokeColor'=>array(255, 255, 255));
  22739. $action = array();
  22740. $action['S'] = 'SubmitForm';
  22741. $action['F'] = $this->form_action;
  22742. if ($this->form_enctype != 'FDF') {
  22743. $action['Flags'] = array('ExportFormat');
  22744. }
  22745. if ($this->form_mode == 'get') {
  22746. $action['Flags'] = array('GetMethod');
  22747. }
  22748. $this->Button($name, $w, $h, $value, $action, $prop, $opt, '', '', false);
  22749. break;
  22750. }
  22751. case 'reset': {
  22752. $w = $this->GetStringWidth($value) * 1.5;
  22753. $h *= 1.6;
  22754. $prop = array('lineWidth'=>1, 'borderStyle'=>'beveled', 'fillColor'=>array(196, 196, 196), 'strokeColor'=>array(255, 255, 255));
  22755. $this->Button($name, $w, $h, $value, array('S'=>'ResetForm'), $prop, $opt, '', '', false);
  22756. break;
  22757. }
  22758. case 'file': {
  22759. $prop['fileSelect'] = 'true';
  22760. $this->TextField($name, $w, $h, $prop, $opt, '', '', false);
  22761. if (!isset($value)) {
  22762. $value = '*';
  22763. }
  22764. $w = $this->GetStringWidth($value) * 2;
  22765. $h *= 1.2;
  22766. $prop = array('lineWidth'=>1, 'borderStyle'=>'beveled', 'fillColor'=>array(196, 196, 196), 'strokeColor'=>array(255, 255, 255));
  22767. $jsaction = 'var f=this.getField(\''.$name.'\'); f.browseForFileToSubmit();';
  22768. $this->Button('FB_'.$name, $w, $h, $value, $jsaction, $prop, $opt, '', '', false);
  22769. break;
  22770. }
  22771. case 'hidden': {
  22772. if (isset($value)) {
  22773. $opt['v'] = $value;
  22774. }
  22775. $opt['f'] = array('invisible', 'hidden');
  22776. $this->TextField($name, 0, 0, $prop, $opt, '', '', false);
  22777. break;
  22778. }
  22779. case 'image': {
  22780. // THIS TYPE MUST BE FIXED
  22781. if (isset($tag['attribute']['src']) AND !$this->empty_string($tag['attribute']['src'])) {
  22782. $img = $tag['attribute']['src'];
  22783. } else {
  22784. break;
  22785. }
  22786. $value = 'img';
  22787. //$opt['mk'] = array('i'=>$img, 'tp'=>1, 'if'=>array('sw'=>'A', 's'=>'A', 'fb'=>false));
  22788. if (isset($tag['attribute']['onclick']) AND !empty($tag['attribute']['onclick'])) {
  22789. $jsaction = $tag['attribute']['onclick'];
  22790. } else {
  22791. $jsaction = '';
  22792. }
  22793. $this->Button($name, $w, $h, $value, $jsaction, $prop, $opt, '', '', false);
  22794. break;
  22795. }
  22796. case 'button': {
  22797. $w = $this->GetStringWidth($value) * 1.5;
  22798. $h *= 1.6;
  22799. $prop = array('lineWidth'=>1, 'borderStyle'=>'beveled', 'fillColor'=>array(196, 196, 196), 'strokeColor'=>array(255, 255, 255));
  22800. if (isset($tag['attribute']['onclick']) AND !empty($tag['attribute']['onclick'])) {
  22801. $jsaction = $tag['attribute']['onclick'];
  22802. } else {
  22803. $jsaction = '';
  22804. }
  22805. $this->Button($name, $w, $h, $value, $jsaction, $prop, $opt, '', '', false);
  22806. break;
  22807. }
  22808. }
  22809. break;
  22810. }
  22811. case 'textarea': {
  22812. $prop = array();
  22813. $opt = array();
  22814. if (isset($tag['attribute']['readonly']) AND !$this->empty_string($tag['attribute']['readonly'])) {
  22815. $prop['readonly'] = true;
  22816. }
  22817. if (isset($tag['attribute']['name']) AND !$this->empty_string($tag['attribute']['name'])) {
  22818. $name = $tag['attribute']['name'];
  22819. } else {
  22820. break;
  22821. }
  22822. if (isset($tag['attribute']['value']) AND !$this->empty_string($tag['attribute']['value'])) {
  22823. $opt['v'] = $tag['attribute']['value'];
  22824. }
  22825. if (isset($tag['attribute']['cols']) AND !$this->empty_string($tag['attribute']['cols'])) {
  22826. $w = intval($tag['attribute']['cols']) * $this->GetStringWidth(chr(32)) * 2;
  22827. } else {
  22828. $w = 40;
  22829. }
  22830. if (isset($tag['attribute']['rows']) AND !$this->empty_string($tag['attribute']['rows'])) {
  22831. $h = intval($tag['attribute']['rows']) * $this->FontSize * $this->cell_height_ratio;
  22832. } else {
  22833. $h = 10;
  22834. }
  22835. $prop['multiline'] = 'true';
  22836. $this->TextField($name, $w, $h, $prop, $opt, '', '', false);
  22837. break;
  22838. }
  22839. case 'select': {
  22840. $h = $this->FontSize * $this->cell_height_ratio;
  22841. if (isset($tag['attribute']['size']) AND !$this->empty_string($tag['attribute']['size'])) {
  22842. $h *= ($tag['attribute']['size'] + 1);
  22843. }
  22844. $prop = array();
  22845. $opt = array();
  22846. if (isset($tag['attribute']['name']) AND !$this->empty_string($tag['attribute']['name'])) {
  22847. $name = $tag['attribute']['name'];
  22848. } else {
  22849. break;
  22850. }
  22851. $w = 0;
  22852. if (isset($tag['attribute']['opt']) AND !$this->empty_string($tag['attribute']['opt'])) {
  22853. $options = explode('#!NwL!#', $tag['attribute']['opt']);
  22854. $values = array();
  22855. foreach ($options as $val) {
  22856. if (strpos($val, '#!TaB!#') !== false) {
  22857. $opts = explode('#!TaB!#', $val);
  22858. $values[] = $opts;
  22859. $w = max($w, $this->GetStringWidth($opts[1]));
  22860. } else {
  22861. $values[] = $val;
  22862. $w = max($w, $this->GetStringWidth($val));
  22863. }
  22864. }
  22865. } else {
  22866. break;
  22867. }
  22868. $w *= 2;
  22869. if (isset($tag['attribute']['multiple']) AND ($tag['attribute']['multiple']='multiple')) {
  22870. $prop['multipleSelection'] = 'true';
  22871. $this->ListBox($name, $w, $h, $values, $prop, $opt, '', '', false);
  22872. } else {
  22873. $this->ComboBox($name, $w, $h, $values, $prop, $opt, '', '', false);
  22874. }
  22875. break;
  22876. }
  22877. case 'tcpdf': {
  22878. if (defined('K_TCPDF_CALLS_IN_HTML') AND (K_TCPDF_CALLS_IN_HTML === true)) {
  22879. // Special tag used to call TCPDF methods
  22880. if (isset($tag['attribute']['method'])) {
  22881. $tcpdf_method = $tag['attribute']['method'];
  22882. if (method_exists($this, $tcpdf_method)) {
  22883. if (isset($tag['attribute']['params']) AND (!empty($tag['attribute']['params']))) {
  22884. $params = unserialize(urldecode($tag['attribute']['params']));
  22885. call_user_func_array(array($this, $tcpdf_method), $params);
  22886. } else {
  22887. $this->$tcpdf_method();
  22888. }
  22889. $this->newline = true;
  22890. }
  22891. }
  22892. }
  22893. break;
  22894. }
  22895. default: {
  22896. break;
  22897. }
  22898. }
  22899. // define tags that support borders and background colors
  22900. $bordertags = array('blockquote','br','dd','dl','div','dt','h1','h2','h3','h4','h5','h6','hr','li','ol','p','pre','ul','tcpdf','table');
  22901. if (in_array($tag['value'], $bordertags)) {
  22902. // set border
  22903. $dom[$key]['borderposition'] = $this->getBorderStartPosition();
  22904. }
  22905. if ($dom[$key]['self'] AND isset($dom[$key]['attribute']['pagebreakafter'])) {
  22906. $pba = $dom[$key]['attribute']['pagebreakafter'];
  22907. // check for pagebreak
  22908. if (($pba == 'true') OR ($pba == 'left') OR ($pba == 'right')) {
  22909. // add a page (or trig AcceptPageBreak() for multicolumn mode)
  22910. $this->checkPageBreak($this->PageBreakTrigger + 1);
  22911. }
  22912. if ((($pba == 'left') AND (((!$this->rtl) AND (($this->page % 2) == 0)) OR (($this->rtl) AND (($this->page % 2) != 0))))
  22913. OR (($pba == 'right') AND (((!$this->rtl) AND (($this->page % 2) != 0)) OR (($this->rtl) AND (($this->page % 2) == 0))))) {
  22914. // add a page (or trig AcceptPageBreak() for multicolumn mode)
  22915. $this->checkPageBreak($this->PageBreakTrigger + 1);
  22916. }
  22917. }
  22918. return $dom;
  22919. }
  22920. /**
  22921. * Process closing tags.
  22922. * @param $dom (array) html dom array
  22923. * @param $key (int) current element id
  22924. * @param $cell (boolean) if true add the default left (or right if RTL) padding to each new line (default false).
  22925. * @param $maxbottomliney (int) maximum y value of current line
  22926. * @return $dom array
  22927. * @protected
  22928. */
  22929. protected function closeHTMLTagHandler($dom, $key, $cell, $maxbottomliney=0) {
  22930. $tag = $dom[$key];
  22931. $parent = $dom[($dom[$key]['parent'])];
  22932. $lasttag = ((!isset($dom[($key + 1)])) OR ((!isset($dom[($key + 2)])) AND ($dom[($key + 1)]['value'] == 'marker')));
  22933. $in_table_head = false;
  22934. // maximum x position (used to draw borders)
  22935. if ($this->rtl) {
  22936. $xmax = $this->w;
  22937. } else {
  22938. $xmax = 0;
  22939. }
  22940. if ($tag['block']) {
  22941. $hbz = 0; // distance from y to line bottom
  22942. $hb = 0; // vertical space between block tags
  22943. // calculate vertical space for block tags
  22944. if (isset($this->tagvspaces[$tag['value']][1]['h']) AND ($this->tagvspaces[$tag['value']][1]['h'] >= 0)) {
  22945. $pre_h = $this->tagvspaces[$tag['value']][1]['h'];
  22946. } elseif (isset($parent['fontsize'])) {
  22947. $pre_h = (($parent['fontsize'] / $this->k) * $this->cell_height_ratio);
  22948. } else {
  22949. $pre_h = $this->FontSize * $this->cell_height_ratio;
  22950. }
  22951. if (isset($this->tagvspaces[$tag['value']][1]['n'])) {
  22952. $n = $this->tagvspaces[$tag['value']][1]['n'];
  22953. } elseif (preg_match('/[h][0-9]/', $tag['value']) > 0) {
  22954. $n = 0.6;
  22955. } else {
  22956. $n = 1;
  22957. }
  22958. if ((!isset($this->tagvspaces[$tag['value']])) AND ($tag['value'] == 'div')) {
  22959. $hb = 0;
  22960. } else {
  22961. $hb = ($n * $pre_h);
  22962. }
  22963. if ($maxbottomliney > $this->PageBreakTrigger) {
  22964. $hbz = ($this->FontSize * $this->cell_height_ratio);
  22965. } elseif ($this->y < $maxbottomliney) {
  22966. $hbz = ($maxbottomliney - $this->y);
  22967. }
  22968. }
  22969. // Closing tag
  22970. switch($tag['value']) {
  22971. case 'tr': {
  22972. $table_el = $dom[($dom[$key]['parent'])]['parent'];
  22973. if (!isset($parent['endy'])) {
  22974. $dom[($dom[$key]['parent'])]['endy'] = $this->y;
  22975. $parent['endy'] = $this->y;
  22976. }
  22977. if (!isset($parent['endpage'])) {
  22978. $dom[($dom[$key]['parent'])]['endpage'] = $this->page;
  22979. $parent['endpage'] = $this->page;
  22980. }
  22981. if (!isset($parent['endcolumn'])) {
  22982. $dom[($dom[$key]['parent'])]['endcolumn'] = $this->current_column;
  22983. $parent['endcolumn'] = $this->current_column;
  22984. }
  22985. // update row-spanned cells
  22986. if (isset($dom[$table_el]['rowspans'])) {
  22987. foreach ($dom[$table_el]['rowspans'] as $k => $trwsp) {
  22988. $dom[$table_el]['rowspans'][$k]['rowspan'] -= 1;
  22989. if ($dom[$table_el]['rowspans'][$k]['rowspan'] == 0) {
  22990. if (($dom[$table_el]['rowspans'][$k]['endpage'] == $parent['endpage']) AND ($dom[$table_el]['rowspans'][$k]['endcolumn'] == $parent['endcolumn'])) {
  22991. $dom[($dom[$key]['parent'])]['endy'] = max($dom[$table_el]['rowspans'][$k]['endy'], $parent['endy']);
  22992. } elseif (($dom[$table_el]['rowspans'][$k]['endpage'] > $parent['endpage']) OR ($dom[$table_el]['rowspans'][$k]['endcolumn'] > $parent['endcolumn'])) {
  22993. $dom[($dom[$key]['parent'])]['endy'] = $dom[$table_el]['rowspans'][$k]['endy'];
  22994. $dom[($dom[$key]['parent'])]['endpage'] = $dom[$table_el]['rowspans'][$k]['endpage'];
  22995. $dom[($dom[$key]['parent'])]['endcolumn'] = $dom[$table_el]['rowspans'][$k]['endcolumn'];
  22996. }
  22997. }
  22998. }
  22999. // report new endy and endpage to the rowspanned cells
  23000. foreach ($dom[$table_el]['rowspans'] as $k => $trwsp) {
  23001. if ($dom[$table_el]['rowspans'][$k]['rowspan'] == 0) {
  23002. $dom[$table_el]['rowspans'][$k]['endpage'] = max($dom[$table_el]['rowspans'][$k]['endpage'], $dom[($dom[$key]['parent'])]['endpage']);
  23003. $dom[($dom[$key]['parent'])]['endpage'] = $dom[$table_el]['rowspans'][$k]['endpage'];
  23004. $dom[$table_el]['rowspans'][$k]['endcolumn'] = max($dom[$table_el]['rowspans'][$k]['endcolumn'], $dom[($dom[$key]['parent'])]['endcolumn']);
  23005. $dom[($dom[$key]['parent'])]['endcolumn'] = $dom[$table_el]['rowspans'][$k]['endcolumn'];
  23006. $dom[$table_el]['rowspans'][$k]['endy'] = max($dom[$table_el]['rowspans'][$k]['endy'], $dom[($dom[$key]['parent'])]['endy']);
  23007. $dom[($dom[$key]['parent'])]['endy'] = $dom[$table_el]['rowspans'][$k]['endy'];
  23008. }
  23009. }
  23010. // update remaining rowspanned cells
  23011. foreach ($dom[$table_el]['rowspans'] as $k => $trwsp) {
  23012. if ($dom[$table_el]['rowspans'][$k]['rowspan'] == 0) {
  23013. $dom[$table_el]['rowspans'][$k]['endpage'] = $dom[($dom[$key]['parent'])]['endpage'];
  23014. $dom[$table_el]['rowspans'][$k]['endcolumn'] = $dom[($dom[$key]['parent'])]['endcolumn'];
  23015. $dom[$table_el]['rowspans'][$k]['endy'] = $dom[($dom[$key]['parent'])]['endy'];
  23016. }
  23017. }
  23018. }
  23019. $this->setPage($dom[($dom[$key]['parent'])]['endpage']);
  23020. if ($this->num_columns > 1) {
  23021. $this->selectColumn($dom[($dom[$key]['parent'])]['endcolumn']);
  23022. }
  23023. $this->y = $dom[($dom[$key]['parent'])]['endy'];
  23024. if (isset($dom[$table_el]['attribute']['cellspacing'])) {
  23025. $this->y += $this->getHTMLUnitToUnits($dom[$table_el]['attribute']['cellspacing'], 1, 'px');
  23026. } elseif (isset($dom[$table_el]['border-spacing'])) {
  23027. $this->y += $dom[$table_el]['border-spacing']['V'];
  23028. }
  23029. $this->Ln(0, $cell);
  23030. if ($this->current_column == $parent['startcolumn']) {
  23031. $this->x = $parent['startx'];
  23032. }
  23033. // account for booklet mode
  23034. if ($this->page > $parent['startpage']) {
  23035. if (($this->rtl) AND ($this->pagedim[$this->page]['orm'] != $this->pagedim[$parent['startpage']]['orm'])) {
  23036. $this->x -= ($this->pagedim[$this->page]['orm'] - $this->pagedim[$parent['startpage']]['orm']);
  23037. } elseif ((!$this->rtl) AND ($this->pagedim[$this->page]['olm'] != $this->pagedim[$parent['startpage']]['olm'])) {
  23038. $this->x += ($this->pagedim[$this->page]['olm'] - $this->pagedim[$parent['startpage']]['olm']);
  23039. }
  23040. }
  23041. break;
  23042. }
  23043. case 'tablehead':
  23044. // closing tag used for the thead part
  23045. $in_table_head = true;
  23046. $this->inthead = false;
  23047. case 'table': {
  23048. $table_el = $parent;
  23049. // set default border
  23050. if (isset($table_el['attribute']['border']) AND ($table_el['attribute']['border'] > 0)) {
  23051. // set default border
  23052. $border = array('LTRB' => array('width' => $this->getCSSBorderWidth($table_el['attribute']['border']), 'cap'=>'square', 'join'=>'miter', 'dash'=> 0, 'color'=>array(0,0,0)));
  23053. } else {
  23054. $border = 0;
  23055. }
  23056. $default_border = $border;
  23057. // fix bottom line alignment of last line before page break
  23058. foreach ($dom[($dom[$key]['parent'])]['trids'] as $j => $trkey) {
  23059. // update row-spanned cells
  23060. if (isset($dom[($dom[$key]['parent'])]['rowspans'])) {
  23061. foreach ($dom[($dom[$key]['parent'])]['rowspans'] as $k => $trwsp) {
  23062. if ($trwsp['trid'] == $trkey) {
  23063. $dom[($dom[$key]['parent'])]['rowspans'][$k]['mrowspan'] -= 1;
  23064. }
  23065. if (isset($prevtrkey) AND ($trwsp['trid'] == $prevtrkey) AND ($trwsp['mrowspan'] >= 0)) {
  23066. $dom[($dom[$key]['parent'])]['rowspans'][$k]['trid'] = $trkey;
  23067. }
  23068. }
  23069. }
  23070. if (isset($prevtrkey) AND ($dom[$trkey]['startpage'] > $dom[$prevtrkey]['endpage'])) {
  23071. $pgendy = $this->pagedim[$dom[$prevtrkey]['endpage']]['hk'] - $this->pagedim[$dom[$prevtrkey]['endpage']]['bm'];
  23072. $dom[$prevtrkey]['endy'] = $pgendy;
  23073. // update row-spanned cells
  23074. if (isset($dom[($dom[$key]['parent'])]['rowspans'])) {
  23075. foreach ($dom[($dom[$key]['parent'])]['rowspans'] as $k => $trwsp) {
  23076. if (($trwsp['trid'] == $trkey) AND ($trwsp['mrowspan'] > 1) AND ($trwsp['endpage'] == $dom[$prevtrkey]['endpage'])) {
  23077. $dom[($dom[$key]['parent'])]['rowspans'][$k]['endy'] = $pgendy;
  23078. $dom[($dom[$key]['parent'])]['rowspans'][$k]['mrowspan'] = -1;
  23079. }
  23080. }
  23081. }
  23082. }
  23083. $prevtrkey = $trkey;
  23084. $table_el = $dom[($dom[$key]['parent'])];
  23085. }
  23086. // for each row
  23087. if (count($table_el['trids']) > 0) {
  23088. unset($xmax);
  23089. }
  23090. foreach ($table_el['trids'] as $j => $trkey) {
  23091. $parent = $dom[$trkey];
  23092. if (!isset($xmax)) {
  23093. $xmax = $parent['cellpos'][(count($parent['cellpos']) - 1)]['endx'];
  23094. }
  23095. // for each cell on the row
  23096. foreach ($parent['cellpos'] as $k => $cellpos) {
  23097. if (isset($cellpos['rowspanid']) AND ($cellpos['rowspanid'] >= 0)) {
  23098. $cellpos['startx'] = $table_el['rowspans'][($cellpos['rowspanid'])]['startx'];
  23099. $cellpos['endx'] = $table_el['rowspans'][($cellpos['rowspanid'])]['endx'];
  23100. $endy = $table_el['rowspans'][($cellpos['rowspanid'])]['endy'];
  23101. $startpage = $table_el['rowspans'][($cellpos['rowspanid'])]['startpage'];
  23102. $endpage = $table_el['rowspans'][($cellpos['rowspanid'])]['endpage'];
  23103. $startcolumn = $table_el['rowspans'][($cellpos['rowspanid'])]['startcolumn'];
  23104. $endcolumn = $table_el['rowspans'][($cellpos['rowspanid'])]['endcolumn'];
  23105. } else {
  23106. $endy = $parent['endy'];
  23107. $startpage = $parent['startpage'];
  23108. $endpage = $parent['endpage'];
  23109. $startcolumn = $parent['startcolumn'];
  23110. $endcolumn = $parent['endcolumn'];
  23111. }
  23112. if ($this->num_columns == 0) {
  23113. $this->num_columns = 1;
  23114. }
  23115. if (isset($cellpos['border'])) {
  23116. $border = $cellpos['border'];
  23117. }
  23118. if (isset($cellpos['bgcolor']) AND ($cellpos['bgcolor']) !== false) {
  23119. $this->SetFillColorArray($cellpos['bgcolor']);
  23120. $fill = true;
  23121. } else {
  23122. $fill = false;
  23123. }
  23124. $x = $cellpos['startx'];
  23125. $y = $parent['starty'];
  23126. $starty = $y;
  23127. $w = abs($cellpos['endx'] - $cellpos['startx']);
  23128. // get border modes
  23129. $border_start = $this->getBorderMode($border, $position='start');
  23130. $border_end = $this->getBorderMode($border, $position='end');
  23131. $border_middle = $this->getBorderMode($border, $position='middle');
  23132. // design borders around HTML cells.
  23133. for ($page = $startpage; $page <= $endpage; ++$page) { // for each page
  23134. $ccode = '';
  23135. $this->setPage($page);
  23136. if ($this->num_columns < 2) {
  23137. // single-column mode
  23138. $this->x = $x;
  23139. $this->y = $this->tMargin;
  23140. }
  23141. // account for margin changes
  23142. if ($page > $startpage) {
  23143. if (($this->rtl) AND ($this->pagedim[$page]['orm'] != $this->pagedim[$startpage]['orm'])) {
  23144. $this->x -= ($this->pagedim[$page]['orm'] - $this->pagedim[$startpage]['orm']);
  23145. } elseif ((!$this->rtl) AND ($this->pagedim[$page]['olm'] != $this->pagedim[$startpage]['olm'])) {
  23146. $this->x += ($this->pagedim[$page]['olm'] - $this->pagedim[$startpage]['olm']);
  23147. }
  23148. }
  23149. if ($startpage == $endpage) { // single page
  23150. $deltacol = 0;
  23151. $deltath = 0;
  23152. for ($column = $startcolumn; $column <= $endcolumn; ++$column) { // for each column
  23153. $this->selectColumn($column);
  23154. if ($startcolumn == $endcolumn) { // single column
  23155. $cborder = $border;
  23156. $h = $endy - $parent['starty'];
  23157. $this->y = $y;
  23158. $this->x = $x;
  23159. } elseif ($column == $startcolumn) { // first column
  23160. $cborder = $border_start;
  23161. $this->y = $starty;
  23162. $this->x = $x;
  23163. $h = $this->h - $this->y - $this->bMargin;
  23164. if ($this->rtl) {
  23165. $deltacol = $this->x + $this->rMargin - $this->w;
  23166. } else {
  23167. $deltacol = $this->x - $this->lMargin;
  23168. }
  23169. } elseif ($column == $endcolumn) { // end column
  23170. $cborder = $border_end;
  23171. if (isset($this->columns[$column]['th']['\''.$page.'\''])) {
  23172. $this->y = $this->columns[$column]['th']['\''.$page.'\''];
  23173. }
  23174. $this->x += $deltacol;
  23175. $h = $endy - $this->y;
  23176. } else { // middle column
  23177. $cborder = $border_middle;
  23178. if (isset($this->columns[$column]['th']['\''.$page.'\''])) {
  23179. $this->y = $this->columns[$column]['th']['\''.$page.'\''];
  23180. }
  23181. $this->x += $deltacol;
  23182. $h = $this->h - $this->y - $this->bMargin;
  23183. }
  23184. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
  23185. } // end for each column
  23186. } elseif ($page == $startpage) { // first page
  23187. $deltacol = 0;
  23188. $deltath = 0;
  23189. for ($column = $startcolumn; $column < $this->num_columns; ++$column) { // for each column
  23190. $this->selectColumn($column);
  23191. if ($column == $startcolumn) { // first column
  23192. $cborder = $border_start;
  23193. $this->y = $starty;
  23194. $this->x = $x;
  23195. $h = $this->h - $this->y - $this->bMargin;
  23196. if ($this->rtl) {
  23197. $deltacol = $this->x + $this->rMargin - $this->w;
  23198. } else {
  23199. $deltacol = $this->x - $this->lMargin;
  23200. }
  23201. } else { // middle column
  23202. $cborder = $border_middle;
  23203. if (isset($this->columns[$column]['th']['\''.$page.'\''])) {
  23204. $this->y = $this->columns[$column]['th']['\''.$page.'\''];
  23205. }
  23206. $this->x += $deltacol;
  23207. $h = $this->h - $this->y - $this->bMargin;
  23208. }
  23209. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
  23210. } // end for each column
  23211. } elseif ($page == $endpage) { // last page
  23212. $deltacol = 0;
  23213. $deltath = 0;
  23214. for ($column = 0; $column <= $endcolumn; ++$column) { // for each column
  23215. $this->selectColumn($column);
  23216. if ($column == $endcolumn) { // end column
  23217. $cborder = $border_end;
  23218. if (isset($this->columns[$column]['th']['\''.$page.'\''])) {
  23219. $this->y = $this->columns[$column]['th']['\''.$page.'\''];
  23220. }
  23221. $this->x += $deltacol;
  23222. $h = $endy - $this->y;
  23223. } else { // middle column
  23224. $cborder = $border_middle;
  23225. if (isset($this->columns[$column]['th']['\''.$page.'\''])) {
  23226. $this->y = $this->columns[$column]['th']['\''.$page.'\''];
  23227. }
  23228. $this->x += $deltacol;
  23229. $h = $this->h - $this->y - $this->bMargin;
  23230. }
  23231. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
  23232. } // end for each column
  23233. } else { // middle page
  23234. $deltacol = 0;
  23235. $deltath = 0;
  23236. for ($column = 0; $column < $this->num_columns; ++$column) { // for each column
  23237. $this->selectColumn($column);
  23238. $cborder = $border_middle;
  23239. if (isset($this->columns[$column]['th']['\''.$page.'\''])) {
  23240. $this->y = $this->columns[$column]['th']['\''.$page.'\''];
  23241. }
  23242. $this->x += $deltacol;
  23243. $h = $this->h - $this->y - $this->bMargin;
  23244. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
  23245. } // end for each column
  23246. }
  23247. if ($cborder OR $fill) {
  23248. $offsetlen = strlen($ccode);
  23249. // draw border and fill
  23250. if ($this->inxobj) {
  23251. // we are inside an XObject template
  23252. if (end($this->xobjects[$this->xobjid]['transfmrk']) !== false) {
  23253. $pagemarkkey = key($this->xobjects[$this->xobjid]['transfmrk']);
  23254. $pagemark = $this->xobjects[$this->xobjid]['transfmrk'][$pagemarkkey];
  23255. $this->xobjects[$this->xobjid]['transfmrk'][$pagemarkkey] += $offsetlen;
  23256. } else {
  23257. $pagemark = $this->xobjects[$this->xobjid]['intmrk'];
  23258. $this->xobjects[$this->xobjid]['intmrk'] += $offsetlen;
  23259. }
  23260. $pagebuff = $this->xobjects[$this->xobjid]['outdata'];
  23261. $pstart = substr($pagebuff, 0, $pagemark);
  23262. $pend = substr($pagebuff, $pagemark);
  23263. $this->xobjects[$this->xobjid]['outdata'] = $pstart.$ccode.$pend;
  23264. } else {
  23265. // draw border and fill
  23266. if (end($this->transfmrk[$this->page]) !== false) {
  23267. $pagemarkkey = key($this->transfmrk[$this->page]);
  23268. $pagemark = $this->transfmrk[$this->page][$pagemarkkey];
  23269. $this->transfmrk[$this->page][$pagemarkkey] += $offsetlen;
  23270. } elseif ($this->InFooter) {
  23271. $pagemark = $this->footerpos[$this->page];
  23272. $this->footerpos[$this->page] += $offsetlen;
  23273. } else {
  23274. $pagemark = $this->intmrk[$this->page];
  23275. $this->intmrk[$this->page] += $offsetlen;
  23276. }
  23277. $pagebuff = $this->getPageBuffer($this->page);
  23278. $pstart = substr($pagebuff, 0, $pagemark);
  23279. $pend = substr($pagebuff, $pagemark);
  23280. $this->setPageBuffer($this->page, $pstart.$ccode.$pend);
  23281. }
  23282. }
  23283. } // end for each page
  23284. // restore default border
  23285. $border = $default_border;
  23286. } // end for each cell on the row
  23287. if (isset($table_el['attribute']['cellspacing'])) {
  23288. $this->y += $this->getHTMLUnitToUnits($table_el['attribute']['cellspacing'], 1, 'px');
  23289. } elseif (isset($table_el['border-spacing'])) {
  23290. $this->y += $table_el['border-spacing']['V'];
  23291. }
  23292. $this->Ln(0, $cell);
  23293. $this->x = $parent['startx'];
  23294. if ($endpage > $startpage) {
  23295. if (($this->rtl) AND ($this->pagedim[$endpage]['orm'] != $this->pagedim[$startpage]['orm'])) {
  23296. $this->x += ($this->pagedim[$endpage]['orm'] - $this->pagedim[$startpage]['orm']);
  23297. } elseif ((!$this->rtl) AND ($this->pagedim[$endpage]['olm'] != $this->pagedim[$startpage]['olm'])) {
  23298. $this->x += ($this->pagedim[$endpage]['olm'] - $this->pagedim[$startpage]['olm']);
  23299. }
  23300. }
  23301. }
  23302. if (!$in_table_head) { // we are not inside a thead section
  23303. $this->cell_padding = $table_el['old_cell_padding'];
  23304. // reset row height
  23305. $this->resetLastH();
  23306. if (($this->page == ($this->numpages - 1)) AND ($this->pageopen[$this->numpages])) {
  23307. $plendiff = ($this->pagelen[$this->numpages] - $this->emptypagemrk[$this->numpages]);
  23308. if (($plendiff > 0) AND ($plendiff < 60)) {
  23309. $pagediff = substr($this->getPageBuffer($this->numpages), $this->emptypagemrk[$this->numpages], $plendiff);
  23310. if (substr($pagediff, 0, 5) == 'BT /F') {
  23311. // the difference is only a font setting
  23312. $plendiff = 0;
  23313. }
  23314. }
  23315. if ($plendiff == 0) {
  23316. // remove last blank page
  23317. $this->deletePage($this->numpages);
  23318. }
  23319. }
  23320. if (isset($this->theadMargins['top'])) {
  23321. // restore top margin
  23322. $this->tMargin = $this->theadMargins['top'];
  23323. }
  23324. if (!isset($table_el['attribute']['nested']) OR ($table_el['attribute']['nested'] != 'true')) {
  23325. // reset main table header
  23326. $this->thead = '';
  23327. $this->theadMargins = array();
  23328. $this->pagedim[$this->page]['tm'] = $this->tMargin;
  23329. }
  23330. }
  23331. $parent = $table_el;
  23332. break;
  23333. }
  23334. case 'a': {
  23335. $this->HREF = '';
  23336. break;
  23337. }
  23338. case 'sup': {
  23339. $this->SetXY($this->GetX(), $this->GetY() + ((0.7 * $parent['fontsize']) / $this->k));
  23340. break;
  23341. }
  23342. case 'sub': {
  23343. $this->SetXY($this->GetX(), $this->GetY() - ((0.3 * $parent['fontsize'])/$this->k));
  23344. break;
  23345. }
  23346. case 'div': {
  23347. $this->addHTMLVertSpace($hbz, $hb, $cell, false, $lasttag);
  23348. break;
  23349. }
  23350. case 'blockquote': {
  23351. if ($this->rtl) {
  23352. $this->rMargin -= $this->listindent;
  23353. } else {
  23354. $this->lMargin -= $this->listindent;
  23355. }
  23356. --$this->listindentlevel;
  23357. $this->addHTMLVertSpace($hbz, $hb, $cell, false, $lasttag);
  23358. break;
  23359. }
  23360. case 'p': {
  23361. $this->addHTMLVertSpace($hbz, $hb, $cell, false, $lasttag);
  23362. break;
  23363. }
  23364. case 'pre': {
  23365. $this->addHTMLVertSpace($hbz, $hb, $cell, false, $lasttag);
  23366. $this->premode = false;
  23367. break;
  23368. }
  23369. case 'dl': {
  23370. --$this->listnum;
  23371. if ($this->listnum <= 0) {
  23372. $this->listnum = 0;
  23373. $this->addHTMLVertSpace($hbz, $hb, $cell, false, $lasttag);
  23374. } else {
  23375. $this->addHTMLVertSpace(0, 0, $cell, false, $lasttag);
  23376. }
  23377. $this->resetLastH();
  23378. break;
  23379. }
  23380. case 'dt': {
  23381. $this->lispacer = '';
  23382. $this->addHTMLVertSpace(0, 0, $cell, false, $lasttag);
  23383. break;
  23384. }
  23385. case 'dd': {
  23386. $this->lispacer = '';
  23387. if ($this->rtl) {
  23388. $this->rMargin -= $this->listindent;
  23389. } else {
  23390. $this->lMargin -= $this->listindent;
  23391. }
  23392. --$this->listindentlevel;
  23393. $this->addHTMLVertSpace(0, 0, $cell, false, $lasttag);
  23394. break;
  23395. }
  23396. case 'ul':
  23397. case 'ol': {
  23398. --$this->listnum;
  23399. $this->lispacer = '';
  23400. if ($this->rtl) {
  23401. $this->rMargin -= $this->listindent;
  23402. } else {
  23403. $this->lMargin -= $this->listindent;
  23404. }
  23405. --$this->listindentlevel;
  23406. if ($this->listnum <= 0) {
  23407. $this->listnum = 0;
  23408. $this->addHTMLVertSpace($hbz, $hb, $cell, false, $lasttag);
  23409. } else {
  23410. $this->addHTMLVertSpace(0, 0, $cell, false, $lasttag);
  23411. }
  23412. $this->resetLastH();
  23413. break;
  23414. }
  23415. case 'li': {
  23416. $this->lispacer = '';
  23417. $this->addHTMLVertSpace(0, 0, $cell, false, $lasttag);
  23418. break;
  23419. }
  23420. case 'h1':
  23421. case 'h2':
  23422. case 'h3':
  23423. case 'h4':
  23424. case 'h5':
  23425. case 'h6': {
  23426. $this->addHTMLVertSpace($hbz, $hb, $cell, false, $lasttag);
  23427. break;
  23428. }
  23429. // Form fields (since 4.8.000 - 2009-09-07)
  23430. case 'form': {
  23431. $this->form_action = '';
  23432. $this->form_enctype = 'application/x-www-form-urlencoded';
  23433. break;
  23434. }
  23435. default : {
  23436. break;
  23437. }
  23438. }
  23439. // draw border and background (if any)
  23440. $this->drawHTMLTagBorder($parent, $xmax);
  23441. if (isset($dom[($dom[$key]['parent'])]['attribute']['pagebreakafter'])) {
  23442. $pba = $dom[($dom[$key]['parent'])]['attribute']['pagebreakafter'];
  23443. // check for pagebreak
  23444. if (($pba == 'true') OR ($pba == 'left') OR ($pba == 'right')) {
  23445. // add a page (or trig AcceptPageBreak() for multicolumn mode)
  23446. $this->checkPageBreak($this->PageBreakTrigger + 1);
  23447. }
  23448. if ((($pba == 'left') AND (((!$this->rtl) AND (($this->page % 2) == 0)) OR (($this->rtl) AND (($this->page % 2) != 0))))
  23449. OR (($pba == 'right') AND (((!$this->rtl) AND (($this->page % 2) != 0)) OR (($this->rtl) AND (($this->page % 2) == 0))))) {
  23450. // add a page (or trig AcceptPageBreak() for multicolumn mode)
  23451. $this->checkPageBreak($this->PageBreakTrigger + 1);
  23452. }
  23453. }
  23454. $this->tmprtl = false;
  23455. return $dom;
  23456. }
  23457. /**
  23458. * Add vertical spaces if needed.
  23459. * @param $hbz (string) Distance between current y and line bottom.
  23460. * @param $hb (string) The height of the break.
  23461. * @param $cell (boolean) if true add the default left (or right if RTL) padding to each new line (default false).
  23462. * @param $firsttag (boolean) set to true when the tag is the first.
  23463. * @param $lasttag (boolean) set to true when the tag is the last.
  23464. * @protected
  23465. */
  23466. protected function addHTMLVertSpace($hbz=0, $hb=0, $cell=false, $firsttag=false, $lasttag=false) {
  23467. if ($firsttag) {
  23468. $this->Ln(0, $cell);
  23469. $this->htmlvspace = 0;
  23470. return;
  23471. }
  23472. if ($lasttag) {
  23473. $this->Ln($hbz, $cell);
  23474. $this->htmlvspace = 0;
  23475. return;
  23476. }
  23477. if ($hb < $this->htmlvspace) {
  23478. $hd = 0;
  23479. } else {
  23480. $hd = $hb - $this->htmlvspace;
  23481. $this->htmlvspace = $hb;
  23482. }
  23483. $this->Ln(($hbz + $hd), $cell);
  23484. }
  23485. /**
  23486. * Return the starting coordinates to draw an html border
  23487. * @return array containing top-left border coordinates
  23488. * @protected
  23489. * @since 5.7.000 (2010-08-03)
  23490. */
  23491. protected function getBorderStartPosition() {
  23492. if ($this->rtl) {
  23493. $xmax = $this->lMargin;
  23494. } else {
  23495. $xmax = $this->w - $this->rMargin;
  23496. }
  23497. return array('page' => $this->page, 'column' => $this->current_column, 'x' => $this->x, 'y' => $this->y, 'xmax' => $xmax);
  23498. }
  23499. /**
  23500. * Draw an HTML block border and fill
  23501. * @param $tag (array) array of tag properties.
  23502. * @param $xmax (int) end X coordinate for border.
  23503. * @protected
  23504. * @since 5.7.000 (2010-08-03)
  23505. */
  23506. protected function drawHTMLTagBorder($tag, $xmax) {
  23507. if (!isset($tag['borderposition'])) {
  23508. // nothing to draw
  23509. return;
  23510. }
  23511. $prev_x = $this->x;
  23512. $prev_y = $this->y;
  23513. $prev_lasth = $this->lasth;
  23514. $border = 0;
  23515. $fill = false;
  23516. $this->lasth = 0;
  23517. if (isset($tag['border']) AND !empty($tag['border'])) {
  23518. // get border style
  23519. $border = $tag['border'];
  23520. if (!$this->empty_string($this->thead) AND (!$this->inthead)) {
  23521. // border for table header
  23522. $border = $this->getBorderMode($border, $position='middle');
  23523. }
  23524. }
  23525. if (isset($tag['bgcolor']) AND ($tag['bgcolor'] !== false)) {
  23526. // get background color
  23527. $old_bgcolor = $this->bgcolor;
  23528. $this->SetFillColorArray($tag['bgcolor']);
  23529. $fill = true;
  23530. }
  23531. if (!$border AND !$fill) {
  23532. // nothing to draw
  23533. return;
  23534. }
  23535. if (isset($tag['attribute']['cellspacing'])) {
  23536. $clsp = $this->getHTMLUnitToUnits($tag['attribute']['cellspacing'], 1, 'px');
  23537. $cellspacing = array('H' => $clsp, 'V' => $clsp);
  23538. } elseif (isset($tag['border-spacing'])) {
  23539. $cellspacing = $tag['border-spacing'];
  23540. } else {
  23541. $cellspacing = array('H' => 0, 'V' => 0);
  23542. }
  23543. if (($tag['value'] != 'table') AND (is_array($border)) AND (!empty($border))) {
  23544. // draw the border externally respect the sqare edge.
  23545. $border['mode'] = 'ext';
  23546. }
  23547. if ($this->rtl) {
  23548. if ($xmax >= $tag['borderposition']['x']) {
  23549. $xmax = $tag['borderposition']['xmax'];
  23550. }
  23551. $w = ($tag['borderposition']['x'] - $xmax);
  23552. } else {
  23553. if ($xmax <= $tag['borderposition']['x']) {
  23554. $xmax = $tag['borderposition']['xmax'];
  23555. }
  23556. $w = ($xmax - $tag['borderposition']['x']);
  23557. }
  23558. if ($w <= 0) {
  23559. return;
  23560. }
  23561. $w += $cellspacing['H'];
  23562. $startpage = $tag['borderposition']['page'];
  23563. $startcolumn = $tag['borderposition']['column'];
  23564. $x = $tag['borderposition']['x'];
  23565. $y = $tag['borderposition']['y'];
  23566. $endpage = $this->page;
  23567. $starty = $tag['borderposition']['y'] - $cellspacing['V'];
  23568. $currentY = $this->y;
  23569. $this->x = $x;
  23570. // get latest column
  23571. $endcolumn = $this->current_column;
  23572. if ($this->num_columns == 0) {
  23573. $this->num_columns = 1;
  23574. }
  23575. // get border modes
  23576. $border_start = $this->getBorderMode($border, $position='start');
  23577. $border_end = $this->getBorderMode($border, $position='end');
  23578. $border_middle = $this->getBorderMode($border, $position='middle');
  23579. // temporary disable page regions
  23580. $temp_page_regions = $this->page_regions;
  23581. $this->page_regions = array();
  23582. // design borders around HTML cells.
  23583. for ($page = $startpage; $page <= $endpage; ++$page) { // for each page
  23584. $ccode = '';
  23585. $this->setPage($page);
  23586. if ($this->num_columns < 2) {
  23587. // single-column mode
  23588. $this->x = $x;
  23589. $this->y = $this->tMargin;
  23590. }
  23591. // account for margin changes
  23592. if ($page > $startpage) {
  23593. if (($this->rtl) AND ($this->pagedim[$page]['orm'] != $this->pagedim[$startpage]['orm'])) {
  23594. $this->x -= ($this->pagedim[$page]['orm'] - $this->pagedim[$startpage]['orm']);
  23595. } elseif ((!$this->rtl) AND ($this->pagedim[$page]['olm'] != $this->pagedim[$startpage]['olm'])) {
  23596. $this->x += ($this->pagedim[$page]['olm'] - $this->pagedim[$startpage]['olm']);
  23597. }
  23598. }
  23599. if ($startpage == $endpage) {
  23600. // single page
  23601. for ($column = $startcolumn; $column <= $endcolumn; ++$column) { // for each column
  23602. $this->selectColumn($column);
  23603. if ($startcolumn == $endcolumn) { // single column
  23604. $cborder = $border;
  23605. $h = ($currentY - $y) + $cellspacing['V'];
  23606. $this->y = $starty;
  23607. } elseif ($column == $startcolumn) { // first column
  23608. $cborder = $border_start;
  23609. $this->y = $starty;
  23610. $h = $this->h - $this->y - $this->bMargin;
  23611. } elseif ($column == $endcolumn) { // end column
  23612. $cborder = $border_end;
  23613. $h = $currentY - $this->y;
  23614. } else { // middle column
  23615. $cborder = $border_middle;
  23616. $h = $this->h - $this->y - $this->bMargin;
  23617. }
  23618. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
  23619. } // end for each column
  23620. } elseif ($page == $startpage) { // first page
  23621. for ($column = $startcolumn; $column < $this->num_columns; ++$column) { // for each column
  23622. $this->selectColumn($column);
  23623. if ($column == $startcolumn) { // first column
  23624. $cborder = $border_start;
  23625. $this->y = $starty;
  23626. $h = $this->h - $this->y - $this->bMargin;
  23627. } else { // middle column
  23628. $cborder = $border_middle;
  23629. $h = $this->h - $this->y - $this->bMargin;
  23630. }
  23631. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
  23632. } // end for each column
  23633. } elseif ($page == $endpage) { // last page
  23634. for ($column = 0; $column <= $endcolumn; ++$column) { // for each column
  23635. $this->selectColumn($column);
  23636. if ($column == $endcolumn) {
  23637. // end column
  23638. $cborder = $border_end;
  23639. $h = $currentY - $this->y;
  23640. } else {
  23641. // middle column
  23642. $cborder = $border_middle;
  23643. $h = $this->h - $this->y - $this->bMargin;
  23644. }
  23645. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
  23646. } // end for each column
  23647. } else { // middle page
  23648. for ($column = 0; $column < $this->num_columns; ++$column) { // for each column
  23649. $this->selectColumn($column);
  23650. $cborder = $border_middle;
  23651. $h = $this->h - $this->y - $this->bMargin;
  23652. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
  23653. } // end for each column
  23654. }
  23655. if ($cborder OR $fill) {
  23656. $offsetlen = strlen($ccode);
  23657. // draw border and fill
  23658. if ($this->inxobj) {
  23659. // we are inside an XObject template
  23660. if (end($this->xobjects[$this->xobjid]['transfmrk']) !== false) {
  23661. $pagemarkkey = key($this->xobjects[$this->xobjid]['transfmrk']);
  23662. $pagemark = $this->xobjects[$this->xobjid]['transfmrk'][$pagemarkkey];
  23663. $this->xobjects[$this->xobjid]['transfmrk'][$pagemarkkey] += $offsetlen;
  23664. } else {
  23665. $pagemark = $this->xobjects[$this->xobjid]['intmrk'];
  23666. $this->xobjects[$this->xobjid]['intmrk'] += $offsetlen;
  23667. }
  23668. $pagebuff = $this->xobjects[$this->xobjid]['outdata'];
  23669. $pstart = substr($pagebuff, 0, $pagemark);
  23670. $pend = substr($pagebuff, $pagemark);
  23671. $this->xobjects[$this->xobjid]['outdata'] = $pstart.$ccode.$pend;
  23672. } else {
  23673. if (end($this->transfmrk[$this->page]) !== false) {
  23674. $pagemarkkey = key($this->transfmrk[$this->page]);
  23675. $pagemark = $this->transfmrk[$this->page][$pagemarkkey];
  23676. $this->transfmrk[$this->page][$pagemarkkey] += $offsetlen;
  23677. } elseif ($this->InFooter) {
  23678. $pagemark = $this->footerpos[$this->page];
  23679. $this->footerpos[$this->page] += $offsetlen;
  23680. } else {
  23681. $pagemark = $this->intmrk[$this->page];
  23682. $this->intmrk[$this->page] += $offsetlen;
  23683. }
  23684. $pagebuff = $this->getPageBuffer($this->page);
  23685. $pstart = substr($pagebuff, 0, $this->bordermrk[$this->page]);
  23686. $pend = substr($pagebuff, $this->bordermrk[$this->page]);
  23687. $this->setPageBuffer($this->page, $pstart.$ccode.$pend);
  23688. $this->bordermrk[$this->page] += $offsetlen;
  23689. $this->cntmrk[$this->page] += $offsetlen;
  23690. }
  23691. }
  23692. } // end for each page
  23693. // restore page regions
  23694. $this->page_regions = $temp_page_regions;
  23695. if (isset($old_bgcolor)) {
  23696. // restore background color
  23697. $this->SetFillColorArray($old_bgcolor);
  23698. }
  23699. // restore pointer position
  23700. $this->x = $prev_x;
  23701. $this->y = $prev_y;
  23702. $this->lasth = $prev_lasth;
  23703. }
  23704. /**
  23705. * Set the default bullet to be used as LI bullet symbol
  23706. * @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')
  23707. * @public
  23708. * @since 4.0.028 (2008-09-26)
  23709. */
  23710. public function setLIsymbol($symbol='!') {
  23711. // check for custom image symbol
  23712. if (substr($symbol, 0, 4) == 'img|') {
  23713. $this->lisymbol = $symbol;
  23714. return;
  23715. }
  23716. $symbol = strtolower($symbol);
  23717. switch ($symbol) {
  23718. case '!' :
  23719. case '#' :
  23720. case 'disc' :
  23721. case 'circle' :
  23722. case 'square' :
  23723. case '1':
  23724. case 'decimal':
  23725. case 'decimal-leading-zero':
  23726. case 'i':
  23727. case 'lower-roman':
  23728. case 'I':
  23729. case 'upper-roman':
  23730. case 'a':
  23731. case 'lower-alpha':
  23732. case 'lower-latin':
  23733. case 'A':
  23734. case 'upper-alpha':
  23735. case 'upper-latin':
  23736. case 'lower-greek': {
  23737. $this->lisymbol = $symbol;
  23738. break;
  23739. }
  23740. default : {
  23741. $this->lisymbol = '';
  23742. }
  23743. }
  23744. }
  23745. /**
  23746. * Set the booklet mode for double-sided pages.
  23747. * @param $booklet (boolean) true set the booklet mode on, false otherwise.
  23748. * @param $inner (float) Inner page margin.
  23749. * @param $outer (float) Outer page margin.
  23750. * @public
  23751. * @since 4.2.000 (2008-10-29)
  23752. */
  23753. public function SetBooklet($booklet=true, $inner=-1, $outer=-1) {
  23754. $this->booklet = $booklet;
  23755. if ($inner >= 0) {
  23756. $this->lMargin = $inner;
  23757. }
  23758. if ($outer >= 0) {
  23759. $this->rMargin = $outer;
  23760. }
  23761. }
  23762. /**
  23763. * Swap the left and right margins.
  23764. * @param $reverse (boolean) if true swap left and right margins.
  23765. * @protected
  23766. * @since 4.2.000 (2008-10-29)
  23767. */
  23768. protected function swapMargins($reverse=true) {
  23769. if ($reverse) {
  23770. // swap left and right margins
  23771. $mtemp = $this->original_lMargin;
  23772. $this->original_lMargin = $this->original_rMargin;
  23773. $this->original_rMargin = $mtemp;
  23774. $deltam = $this->original_lMargin - $this->original_rMargin;
  23775. $this->lMargin += $deltam;
  23776. $this->rMargin -= $deltam;
  23777. }
  23778. }
  23779. /**
  23780. * Set the vertical spaces for HTML tags.
  23781. * The array must have the following structure (example):
  23782. * $tagvs = array('h1' => array(0 => array('h' => '', 'n' => 2), 1 => array('h' => 1.3, 'n' => 1)));
  23783. * The first array level contains the tag names,
  23784. * the second level contains 0 for opening tags or 1 for closing tags,
  23785. * the third level contains the vertical space unit (h) and the number spaces to add (n).
  23786. * If the h parameter is not specified, default values are used.
  23787. * @param $tagvs (array) array of tags and relative vertical spaces.
  23788. * @public
  23789. * @since 4.2.001 (2008-10-30)
  23790. */
  23791. public function setHtmlVSpace($tagvs) {
  23792. $this->tagvspaces = $tagvs;
  23793. }
  23794. /**
  23795. * Set custom width for list indentation.
  23796. * @param $width (float) width of the indentation. Use negative value to disable it.
  23797. * @public
  23798. * @since 4.2.007 (2008-11-12)
  23799. */
  23800. public function setListIndentWidth($width) {
  23801. return $this->customlistindent = floatval($width);
  23802. }
  23803. /**
  23804. * Set the top/bottom cell sides to be open or closed when the cell cross the page.
  23805. * @param $isopen (boolean) if true keeps the top/bottom border open for the cell sides that cross the page.
  23806. * @public
  23807. * @since 4.2.010 (2008-11-14)
  23808. */
  23809. public function setOpenCell($isopen) {
  23810. $this->opencell = $isopen;
  23811. }
  23812. /**
  23813. * Set the color and font style for HTML links.
  23814. * @param $color (array) RGB array of colors
  23815. * @param $fontstyle (string) additional font styles to add
  23816. * @public
  23817. * @since 4.4.003 (2008-12-09)
  23818. */
  23819. public function setHtmlLinksStyle($color=array(0,0,255), $fontstyle='U') {
  23820. $this->htmlLinkColorArray = $color;
  23821. $this->htmlLinkFontStyle = $fontstyle;
  23822. }
  23823. /**
  23824. * Convert HTML string containing value and unit of measure to user's units or points.
  23825. * @param $htmlval (string) string containing values and unit
  23826. * @param $refsize (string) reference value in points
  23827. * @param $defaultunit (string) default unit (can be one of the following: %, em, ex, px, in, mm, pc, pt).
  23828. * @param $points (boolean) if true returns points, otherwise returns value in user's units
  23829. * @return float value in user's unit or point if $points=true
  23830. * @public
  23831. * @since 4.4.004 (2008-12-10)
  23832. */
  23833. public function getHTMLUnitToUnits($htmlval, $refsize=1, $defaultunit='px', $points=false) {
  23834. $supportedunits = array('%', 'em', 'ex', 'px', 'in', 'cm', 'mm', 'pc', 'pt');
  23835. $retval = 0;
  23836. $value = 0;
  23837. $unit = 'px';
  23838. $k = $this->k;
  23839. if ($points) {
  23840. $k = 1;
  23841. }
  23842. if (in_array($defaultunit, $supportedunits)) {
  23843. $unit = $defaultunit;
  23844. }
  23845. if (is_numeric($htmlval)) {
  23846. $value = floatval($htmlval);
  23847. } elseif (preg_match('/([0-9\.\-\+]+)/', $htmlval, $mnum)) {
  23848. $value = floatval($mnum[1]);
  23849. if (preg_match('/([a-z%]+)/', $htmlval, $munit)) {
  23850. if (in_array($munit[1], $supportedunits)) {
  23851. $unit = $munit[1];
  23852. }
  23853. }
  23854. }
  23855. switch ($unit) {
  23856. // percentage
  23857. case '%': {
  23858. $retval = (($value * $refsize) / 100);
  23859. break;
  23860. }
  23861. // relative-size
  23862. case 'em': {
  23863. $retval = ($value * $refsize);
  23864. break;
  23865. }
  23866. // height of lower case 'x' (about half the font-size)
  23867. case 'ex': {
  23868. $retval = $value * ($refsize / 2);
  23869. break;
  23870. }
  23871. // absolute-size
  23872. case 'in': {
  23873. $retval = ($value * $this->dpi) / $k;
  23874. break;
  23875. }
  23876. // centimeters
  23877. case 'cm': {
  23878. $retval = ($value / 2.54 * $this->dpi) / $k;
  23879. break;
  23880. }
  23881. // millimeters
  23882. case 'mm': {
  23883. $retval = ($value / 25.4 * $this->dpi) / $k;
  23884. break;
  23885. }
  23886. // one pica is 12 points
  23887. case 'pc': {
  23888. $retval = ($value * 12) / $k;
  23889. break;
  23890. }
  23891. // points
  23892. case 'pt': {
  23893. $retval = $value / $k;
  23894. break;
  23895. }
  23896. // pixels
  23897. case 'px': {
  23898. $retval = $this->pixelsToUnits($value);
  23899. break;
  23900. }
  23901. }
  23902. return $retval;
  23903. }
  23904. /**
  23905. * Returns the Roman representation of an integer number
  23906. * @param $number (int) number to convert
  23907. * @return string roman representation of the specified number
  23908. * @since 4.4.004 (2008-12-10)
  23909. * @public
  23910. */
  23911. public function intToRoman($number) {
  23912. $roman = '';
  23913. while ($number >= 1000) {
  23914. $roman .= 'M';
  23915. $number -= 1000;
  23916. }
  23917. while ($number >= 900) {
  23918. $roman .= 'CM';
  23919. $number -= 900;
  23920. }
  23921. while ($number >= 500) {
  23922. $roman .= 'D';
  23923. $number -= 500;
  23924. }
  23925. while ($number >= 400) {
  23926. $roman .= 'CD';
  23927. $number -= 400;
  23928. }
  23929. while ($number >= 100) {
  23930. $roman .= 'C';
  23931. $number -= 100;
  23932. }
  23933. while ($number >= 90) {
  23934. $roman .= 'XC';
  23935. $number -= 90;
  23936. }
  23937. while ($number >= 50) {
  23938. $roman .= 'L';
  23939. $number -= 50;
  23940. }
  23941. while ($number >= 40) {
  23942. $roman .= 'XL';
  23943. $number -= 40;
  23944. }
  23945. while ($number >= 10) {
  23946. $roman .= 'X';
  23947. $number -= 10;
  23948. }
  23949. while ($number >= 9) {
  23950. $roman .= 'IX';
  23951. $number -= 9;
  23952. }
  23953. while ($number >= 5) {
  23954. $roman .= 'V';
  23955. $number -= 5;
  23956. }
  23957. while ($number >= 4) {
  23958. $roman .= 'IV';
  23959. $number -= 4;
  23960. }
  23961. while ($number >= 1) {
  23962. $roman .= 'I';
  23963. --$number;
  23964. }
  23965. return $roman;
  23966. }
  23967. /**
  23968. * Output an HTML list bullet or ordered item symbol
  23969. * @param $listdepth (int) list nesting level
  23970. * @param $listtype (string) type of list
  23971. * @param $size (float) current font size
  23972. * @protected
  23973. * @since 4.4.004 (2008-12-10)
  23974. */
  23975. protected function putHtmlListBullet($listdepth, $listtype='', $size=10) {
  23976. $size /= $this->k;
  23977. $fill = '';
  23978. $bgcolor = $this->bgcolor;
  23979. $color = $this->fgcolor;
  23980. $strokecolor = $this->strokecolor;
  23981. $width = 0;
  23982. $textitem = '';
  23983. $tmpx = $this->x;
  23984. $lspace = $this->GetStringWidth(' ');
  23985. if ($listtype == '^') {
  23986. // special symbol used for avoid justification of rect bullet
  23987. $this->lispacer = '';
  23988. return;
  23989. } elseif ($listtype == '!') {
  23990. // set default list type for unordered list
  23991. $deftypes = array('disc', 'circle', 'square');
  23992. $listtype = $deftypes[($listdepth - 1) % 3];
  23993. } elseif ($listtype == '#') {
  23994. // set default list type for ordered list
  23995. $listtype = 'decimal';
  23996. } elseif (substr($listtype, 0, 4) == 'img|') {
  23997. // custom image type ('img|type|width|height|image.ext')
  23998. $img = explode('|', $listtype);
  23999. $listtype = 'img';
  24000. }
  24001. switch ($listtype) {
  24002. // unordered types
  24003. case 'none': {
  24004. break;
  24005. }
  24006. case 'disc': {
  24007. $r = $size / 6;
  24008. $lspace += (2 * $r);
  24009. if ($this->rtl) {
  24010. $this->x += $lspace;
  24011. } else {
  24012. $this->x -= $lspace;
  24013. }
  24014. $this->Circle(($this->x + $r), ($this->y + ($this->lasth / 2)), $r, 0, 360, 'F', array(), $color, 8);
  24015. break;
  24016. }
  24017. case 'circle': {
  24018. $r = $size / 6;
  24019. $lspace += (2 * $r);
  24020. if ($this->rtl) {
  24021. $this->x += $lspace;
  24022. } else {
  24023. $this->x -= $lspace;
  24024. }
  24025. $prev_line_style = $this->linestyleWidth.' '.$this->linestyleCap.' '.$this->linestyleJoin.' '.$this->linestyleDash.' '.$this->DrawColor;
  24026. $new_line_style = array('width' => ($r / 3), 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'phase' => 0, 'color'=>$color);
  24027. $this->Circle(($this->x + $r), ($this->y + ($this->lasth / 2)), ($r * (1 - (1/6))), 0, 360, 'D', $new_line_style, array(), 8);
  24028. $this->_out($prev_line_style); // restore line settings
  24029. break;
  24030. }
  24031. case 'square': {
  24032. $l = $size / 3;
  24033. $lspace += $l;
  24034. if ($this->rtl) {;
  24035. $this->x += $lspace;
  24036. } else {
  24037. $this->x -= $lspace;
  24038. }
  24039. $this->Rect($this->x, ($this->y + (($this->lasth - $l) / 2)), $l, $l, 'F', array(), $color);
  24040. break;
  24041. }
  24042. case 'img': {
  24043. // 1=>type, 2=>width, 3=>height, 4=>image.ext
  24044. $lspace += $img[2];
  24045. if ($this->rtl) {;
  24046. $this->x += $lspace;
  24047. } else {
  24048. $this->x -= $lspace;
  24049. }
  24050. $imgtype = strtolower($img[1]);
  24051. $prev_y = $this->y;
  24052. switch ($imgtype) {
  24053. case 'svg': {
  24054. $this->ImageSVG($img[4], $this->x, ($this->y + (($this->lasth - $img[3]) / 2)), $img[2], $img[3], '', 'T', '', 0, false);
  24055. break;
  24056. }
  24057. case 'ai':
  24058. case 'eps': {
  24059. $this->ImageEps($img[4], $this->x, ($this->y + (($this->lasth - $img[3]) / 2)), $img[2], $img[3], '', true, 'T', '', 0, false);
  24060. break;
  24061. }
  24062. default: {
  24063. $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);
  24064. break;
  24065. }
  24066. }
  24067. $this->y = $prev_y;
  24068. break;
  24069. }
  24070. // ordered types
  24071. // $this->listcount[$this->listnum];
  24072. // $textitem
  24073. case '1':
  24074. case 'decimal': {
  24075. $textitem = $this->listcount[$this->listnum];
  24076. break;
  24077. }
  24078. case 'decimal-leading-zero': {
  24079. $textitem = sprintf('%02d', $this->listcount[$this->listnum]);
  24080. break;
  24081. }
  24082. case 'i':
  24083. case 'lower-roman': {
  24084. $textitem = strtolower($this->intToRoman($this->listcount[$this->listnum]));
  24085. break;
  24086. }
  24087. case 'I':
  24088. case 'upper-roman': {
  24089. $textitem = $this->intToRoman($this->listcount[$this->listnum]);
  24090. break;
  24091. }
  24092. case 'a':
  24093. case 'lower-alpha':
  24094. case 'lower-latin': {
  24095. $textitem = chr(97 + $this->listcount[$this->listnum] - 1);
  24096. break;
  24097. }
  24098. case 'A':
  24099. case 'upper-alpha':
  24100. case 'upper-latin': {
  24101. $textitem = chr(65 + $this->listcount[$this->listnum] - 1);
  24102. break;
  24103. }
  24104. case 'lower-greek': {
  24105. $textitem = $this->unichr(945 + $this->listcount[$this->listnum] - 1);
  24106. break;
  24107. }
  24108. /*
  24109. // Types to be implemented (special handling)
  24110. case 'hebrew': {
  24111. break;
  24112. }
  24113. case 'armenian': {
  24114. break;
  24115. }
  24116. case 'georgian': {
  24117. break;
  24118. }
  24119. case 'cjk-ideographic': {
  24120. break;
  24121. }
  24122. case 'hiragana': {
  24123. break;
  24124. }
  24125. case 'katakana': {
  24126. break;
  24127. }
  24128. case 'hiragana-iroha': {
  24129. break;
  24130. }
  24131. case 'katakana-iroha': {
  24132. break;
  24133. }
  24134. */
  24135. default: {
  24136. $textitem = $this->listcount[$this->listnum];
  24137. }
  24138. }
  24139. if (!$this->empty_string($textitem)) {
  24140. // Check whether we need a new page or new column
  24141. $prev_y = $this->y;
  24142. $h = ($this->FontSize * $this->cell_height_ratio) + $this->cell_padding['T'] + $this->cell_padding['B'];
  24143. if ($this->checkPageBreak($h) OR ($this->y < $prev_y)) {
  24144. $tmpx = $this->x;
  24145. }
  24146. // print ordered item
  24147. if ($this->rtl) {
  24148. $textitem = '.'.$textitem;
  24149. } else {
  24150. $textitem = $textitem.'.';
  24151. }
  24152. $lspace += $this->GetStringWidth($textitem);
  24153. if ($this->rtl) {
  24154. $this->x += $lspace;
  24155. } else {
  24156. $this->x -= $lspace;
  24157. }
  24158. $this->Write($this->lasth, $textitem, '', false, '', false, 0, false);
  24159. }
  24160. $this->x = $tmpx;
  24161. $this->lispacer = '^';
  24162. // restore colors
  24163. $this->SetFillColorArray($bgcolor);
  24164. $this->SetDrawColorArray($strokecolor);
  24165. $this->SettextColorArray($color);
  24166. }
  24167. /**
  24168. * Returns current graphic variables as array.
  24169. * @return array of graphic variables
  24170. * @protected
  24171. * @since 4.2.010 (2008-11-14)
  24172. */
  24173. protected function getGraphicVars() {
  24174. $grapvars = array(
  24175. 'FontFamily' => $this->FontFamily,
  24176. 'FontStyle' => $this->FontStyle,
  24177. 'FontSizePt' => $this->FontSizePt,
  24178. 'rMargin' => $this->rMargin,
  24179. 'lMargin' => $this->lMargin,
  24180. 'cell_padding' => $this->cell_padding,
  24181. 'cell_margin' => $this->cell_margin,
  24182. 'LineWidth' => $this->LineWidth,
  24183. 'linestyleWidth' => $this->linestyleWidth,
  24184. 'linestyleCap' => $this->linestyleCap,
  24185. 'linestyleJoin' => $this->linestyleJoin,
  24186. 'linestyleDash' => $this->linestyleDash,
  24187. 'textrendermode' => $this->textrendermode,
  24188. 'textstrokewidth' => $this->textstrokewidth,
  24189. 'DrawColor' => $this->DrawColor,
  24190. 'FillColor' => $this->FillColor,
  24191. 'TextColor' => $this->TextColor,
  24192. 'ColorFlag' => $this->ColorFlag,
  24193. 'bgcolor' => $this->bgcolor,
  24194. 'fgcolor' => $this->fgcolor,
  24195. 'htmlvspace' => $this->htmlvspace,
  24196. 'listindent' => $this->listindent,
  24197. 'listindentlevel' => $this->listindentlevel,
  24198. 'listnum' => $this->listnum,
  24199. 'listordered' => $this->listordered,
  24200. 'listcount' => $this->listcount,
  24201. 'lispacer' => $this->lispacer,
  24202. 'cell_height_ratio' => $this->cell_height_ratio,
  24203. 'font_stretching' => $this->font_stretching,
  24204. 'font_spacing' => $this->font_spacing,
  24205. // extended
  24206. 'lasth' => $this->lasth,
  24207. 'tMargin' => $this->tMargin,
  24208. 'bMargin' => $this->bMargin,
  24209. 'AutoPageBreak' => $this->AutoPageBreak,
  24210. 'PageBreakTrigger' => $this->PageBreakTrigger,
  24211. 'x' => $this->x,
  24212. 'y' => $this->y,
  24213. 'w' => $this->w,
  24214. 'h' => $this->h,
  24215. 'wPt' => $this->wPt,
  24216. 'hPt' => $this->hPt,
  24217. 'fwPt' => $this->fwPt,
  24218. 'fhPt' => $this->fhPt,
  24219. 'page' => $this->page,
  24220. 'current_column' => $this->current_column,
  24221. 'num_columns' => $this->num_columns
  24222. );
  24223. return $grapvars;
  24224. }
  24225. /**
  24226. * Set graphic variables.
  24227. * @param $gvars (array) array of graphic variablesto restore
  24228. * @param $extended (boolean) if true restore extended graphic variables
  24229. * @protected
  24230. * @since 4.2.010 (2008-11-14)
  24231. */
  24232. protected function setGraphicVars($gvars, $extended=false) {
  24233. $this->FontFamily = $gvars['FontFamily'];
  24234. $this->FontStyle = $gvars['FontStyle'];
  24235. $this->FontSizePt = $gvars['FontSizePt'];
  24236. $this->rMargin = $gvars['rMargin'];
  24237. $this->lMargin = $gvars['lMargin'];
  24238. $this->cell_padding = $gvars['cell_padding'];
  24239. $this->cell_margin = $gvars['cell_margin'];
  24240. $this->LineWidth = $gvars['LineWidth'];
  24241. $this->linestyleWidth = $gvars['linestyleWidth'];
  24242. $this->linestyleCap = $gvars['linestyleCap'];
  24243. $this->linestyleJoin = $gvars['linestyleJoin'];
  24244. $this->linestyleDash = $gvars['linestyleDash'];
  24245. $this->textrendermode = $gvars['textrendermode'];
  24246. $this->textstrokewidth = $gvars['textstrokewidth'];
  24247. $this->DrawColor = $gvars['DrawColor'];
  24248. $this->FillColor = $gvars['FillColor'];
  24249. $this->TextColor = $gvars['TextColor'];
  24250. $this->ColorFlag = $gvars['ColorFlag'];
  24251. $this->bgcolor = $gvars['bgcolor'];
  24252. $this->fgcolor = $gvars['fgcolor'];
  24253. $this->htmlvspace = $gvars['htmlvspace'];
  24254. $this->listindent = $gvars['listindent'];
  24255. $this->listindentlevel = $gvars['listindentlevel'];
  24256. $this->listnum = $gvars['listnum'];
  24257. $this->listordered = $gvars['listordered'];
  24258. $this->listcount = $gvars['listcount'];
  24259. $this->lispacer = $gvars['lispacer'];
  24260. $this->cell_height_ratio = $gvars['cell_height_ratio'];
  24261. $this->font_stretching = $gvars['font_stretching'];
  24262. $this->font_spacing = $gvars['font_spacing'];
  24263. if ($extended) {
  24264. // restore extended values
  24265. $this->lasth = $gvars['lasth'];
  24266. $this->tMargin = $gvars['tMargin'];
  24267. $this->bMargin = $gvars['bMargin'];
  24268. $this->AutoPageBreak = $gvars['AutoPageBreak'];
  24269. $this->PageBreakTrigger = $gvars['PageBreakTrigger'];
  24270. $this->x = $gvars['x'];
  24271. $this->y = $gvars['y'];
  24272. $this->w = $gvars['w'];
  24273. $this->h = $gvars['h'];
  24274. $this->wPt = $gvars['wPt'];
  24275. $this->hPt = $gvars['hPt'];
  24276. $this->fwPt = $gvars['fwPt'];
  24277. $this->fhPt = $gvars['fhPt'];
  24278. $this->page = $gvars['page'];
  24279. $this->current_column = $gvars['current_column'];
  24280. $this->num_columns = $gvars['num_columns'];
  24281. }
  24282. $this->_out(''.$this->linestyleWidth.' '.$this->linestyleCap.' '.$this->linestyleJoin.' '.$this->linestyleDash.' '.$this->DrawColor.' '.$this->FillColor.'');
  24283. if (!$this->empty_string($this->FontFamily)) {
  24284. $this->SetFont($this->FontFamily, $this->FontStyle, $this->FontSizePt);
  24285. }
  24286. }
  24287. /**
  24288. * Returns a temporary filename for caching object on filesystem.
  24289. * @param $name (string) prefix to add to filename
  24290. * @return string filename.
  24291. * @since 4.5.000 (2008-12-31)
  24292. * @protected
  24293. */
  24294. protected function getObjFilename($name) {
  24295. return tempnam(K_PATH_CACHE, $name.'_');
  24296. }
  24297. /**
  24298. * Writes data to a temporary file on filesystem.
  24299. * @param $filename (string) file name
  24300. * @param $data (mixed) data to write on file
  24301. * @param $append (boolean) if true append data, false replace.
  24302. * @since 4.5.000 (2008-12-31)
  24303. * @protected
  24304. */
  24305. protected function writeDiskCache($filename, $data, $append=false) {
  24306. if ($append) {
  24307. $fmode = 'ab+';
  24308. } else {
  24309. $fmode = 'wb+';
  24310. }
  24311. $f = @fopen($filename, $fmode);
  24312. if (!$f) {
  24313. $this->Error('Unable to write cache file: '.$filename);
  24314. } else {
  24315. fwrite($f, $data);
  24316. fclose($f);
  24317. }
  24318. // update file length (needed for transactions)
  24319. if (!isset($this->cache_file_length['_'.$filename])) {
  24320. $this->cache_file_length['_'.$filename] = strlen($data);
  24321. } else {
  24322. $this->cache_file_length['_'.$filename] += strlen($data);
  24323. }
  24324. }
  24325. /**
  24326. * Read data from a temporary file on filesystem.
  24327. * @param $filename (string) file name
  24328. * @return mixed retrieved data
  24329. * @since 4.5.000 (2008-12-31)
  24330. * @protected
  24331. */
  24332. protected function readDiskCache($filename) {
  24333. return file_get_contents($filename);
  24334. }
  24335. /**
  24336. * Set buffer content (always append data).
  24337. * @param $data (string) data
  24338. * @protected
  24339. * @since 4.5.000 (2009-01-02)
  24340. */
  24341. protected function setBuffer($data) {
  24342. $this->bufferlen += strlen($data);
  24343. if ($this->diskcache) {
  24344. if (!isset($this->buffer) OR $this->empty_string($this->buffer)) {
  24345. $this->buffer = $this->getObjFilename('buffer');
  24346. }
  24347. $this->writeDiskCache($this->buffer, $data, true);
  24348. } else {
  24349. $this->buffer .= $data;
  24350. }
  24351. }
  24352. /**
  24353. * Replace the buffer content
  24354. * @param $data (string) data
  24355. * @protected
  24356. * @since 5.5.000 (2010-06-22)
  24357. */
  24358. protected function replaceBuffer($data) {
  24359. $this->bufferlen = strlen($data);
  24360. if ($this->diskcache) {
  24361. if (!isset($this->buffer) OR $this->empty_string($this->buffer)) {
  24362. $this->buffer = $this->getObjFilename('buffer');
  24363. }
  24364. $this->writeDiskCache($this->buffer, $data, false);
  24365. } else {
  24366. $this->buffer = $data;
  24367. }
  24368. }
  24369. /**
  24370. * Get buffer content.
  24371. * @return string buffer content
  24372. * @protected
  24373. * @since 4.5.000 (2009-01-02)
  24374. */
  24375. protected function getBuffer() {
  24376. if ($this->diskcache) {
  24377. return $this->readDiskCache($this->buffer);
  24378. } else {
  24379. return $this->buffer;
  24380. }
  24381. }
  24382. /**
  24383. * Set page buffer content.
  24384. * @param $page (int) page number
  24385. * @param $data (string) page data
  24386. * @param $append (boolean) if true append data, false replace.
  24387. * @protected
  24388. * @since 4.5.000 (2008-12-31)
  24389. */
  24390. protected function setPageBuffer($page, $data, $append=false) {
  24391. if ($this->diskcache) {
  24392. if (!isset($this->pages[$page])) {
  24393. $this->pages[$page] = $this->getObjFilename('page'.$page);
  24394. }
  24395. $this->writeDiskCache($this->pages[$page], $data, $append);
  24396. } else {
  24397. if ($append) {
  24398. $this->pages[$page] .= $data;
  24399. } else {
  24400. $this->pages[$page] = $data;
  24401. }
  24402. }
  24403. if ($append AND isset($this->pagelen[$page])) {
  24404. $this->pagelen[$page] += strlen($data);
  24405. } else {
  24406. $this->pagelen[$page] = strlen($data);
  24407. }
  24408. }
  24409. /**
  24410. * Get page buffer content.
  24411. * @param $page (int) page number
  24412. * @return string page buffer content or false in case of error
  24413. * @protected
  24414. * @since 4.5.000 (2008-12-31)
  24415. */
  24416. protected function getPageBuffer($page) {
  24417. if ($this->diskcache) {
  24418. return $this->readDiskCache($this->pages[$page]);
  24419. } elseif (isset($this->pages[$page])) {
  24420. return $this->pages[$page];
  24421. }
  24422. return false;
  24423. }
  24424. /**
  24425. * Set image buffer content.
  24426. * @param $image (string) image key
  24427. * @param $data (array) image data
  24428. * @protected
  24429. * @since 4.5.000 (2008-12-31)
  24430. */
  24431. protected function setImageBuffer($image, $data) {
  24432. if ($this->diskcache) {
  24433. if (!isset($this->images[$image])) {
  24434. $this->images[$image] = $this->getObjFilename('image'.$image);
  24435. }
  24436. $this->writeDiskCache($this->images[$image], serialize($data));
  24437. } else {
  24438. $this->images[$image] = $data;
  24439. }
  24440. if (!in_array($image, $this->imagekeys)) {
  24441. $this->imagekeys[] = $image;
  24442. ++$this->numimages;
  24443. }
  24444. }
  24445. /**
  24446. * Set image buffer content for a specified sub-key.
  24447. * @param $image (string) image key
  24448. * @param $key (string) image sub-key
  24449. * @param $data (array) image data
  24450. * @protected
  24451. * @since 4.5.000 (2008-12-31)
  24452. */
  24453. protected function setImageSubBuffer($image, $key, $data) {
  24454. if (!isset($this->images[$image])) {
  24455. $this->setImageBuffer($image, array());
  24456. }
  24457. if ($this->diskcache) {
  24458. $tmpimg = $this->getImageBuffer($image);
  24459. $tmpimg[$key] = $data;
  24460. $this->writeDiskCache($this->images[$image], serialize($tmpimg));
  24461. } else {
  24462. $this->images[$image][$key] = $data;
  24463. }
  24464. }
  24465. /**
  24466. * Get image buffer content.
  24467. * @param $image (string) image key
  24468. * @return string image buffer content or false in case of error
  24469. * @protected
  24470. * @since 4.5.000 (2008-12-31)
  24471. */
  24472. protected function getImageBuffer($image) {
  24473. if ($this->diskcache AND isset($this->images[$image])) {
  24474. return unserialize($this->readDiskCache($this->images[$image]));
  24475. } elseif (isset($this->images[$image])) {
  24476. return $this->images[$image];
  24477. }
  24478. return false;
  24479. }
  24480. /**
  24481. * Set font buffer content.
  24482. * @param $font (string) font key
  24483. * @param $data (array) font data
  24484. * @protected
  24485. * @since 4.5.000 (2009-01-02)
  24486. */
  24487. protected function setFontBuffer($font, $data) {
  24488. if ($this->diskcache) {
  24489. if (!isset($this->fonts[$font])) {
  24490. $this->fonts[$font] = $this->getObjFilename('font');
  24491. }
  24492. $this->writeDiskCache($this->fonts[$font], serialize($data));
  24493. } else {
  24494. $this->fonts[$font] = $data;
  24495. }
  24496. if (!in_array($font, $this->fontkeys)) {
  24497. $this->fontkeys[] = $font;
  24498. // store object ID for current font
  24499. ++$this->n;
  24500. $this->font_obj_ids[$font] = $this->n;
  24501. $this->setFontSubBuffer($font, 'n', $this->n);
  24502. }
  24503. }
  24504. /**
  24505. * Set font buffer content.
  24506. * @param $font (string) font key
  24507. * @param $key (string) font sub-key
  24508. * @param $data (array) font data
  24509. * @protected
  24510. * @since 4.5.000 (2009-01-02)
  24511. */
  24512. protected function setFontSubBuffer($font, $key, $data) {
  24513. if (!isset($this->fonts[$font])) {
  24514. $this->setFontBuffer($font, array());
  24515. }
  24516. if ($this->diskcache) {
  24517. $tmpfont = $this->getFontBuffer($font);
  24518. $tmpfont[$key] = $data;
  24519. $this->writeDiskCache($this->fonts[$font], serialize($tmpfont));
  24520. } else {
  24521. $this->fonts[$font][$key] = $data;
  24522. }
  24523. }
  24524. /**
  24525. * Get font buffer content.
  24526. * @param $font (string) font key
  24527. * @return string font buffer content or false in case of error
  24528. * @protected
  24529. * @since 4.5.000 (2009-01-02)
  24530. */
  24531. protected function getFontBuffer($font) {
  24532. if ($this->diskcache AND isset($this->fonts[$font])) {
  24533. return unserialize($this->readDiskCache($this->fonts[$font]));
  24534. } elseif (isset($this->fonts[$font])) {
  24535. return $this->fonts[$font];
  24536. }
  24537. return false;
  24538. }
  24539. /**
  24540. * Move a page to a previous position.
  24541. * @param $frompage (int) number of the source page
  24542. * @param $topage (int) number of the destination page (must be less than $frompage)
  24543. * @return true in case of success, false in case of error.
  24544. * @public
  24545. * @since 4.5.000 (2009-01-02)
  24546. */
  24547. public function movePage($frompage, $topage) {
  24548. if (($frompage > $this->numpages) OR ($frompage <= $topage)) {
  24549. return false;
  24550. }
  24551. if ($frompage == $this->page) {
  24552. // close the page before moving it
  24553. $this->endPage();
  24554. }
  24555. // move all page-related states
  24556. $tmppage = $this->getPageBuffer($frompage);
  24557. $tmppagedim = $this->pagedim[$frompage];
  24558. $tmppagelen = $this->pagelen[$frompage];
  24559. $tmpintmrk = $this->intmrk[$frompage];
  24560. $tmpbordermrk = $this->bordermrk[$frompage];
  24561. $tmpcntmrk = $this->cntmrk[$frompage];
  24562. if (isset($this->footerpos[$frompage])) {
  24563. $tmpfooterpos = $this->footerpos[$frompage];
  24564. }
  24565. if (isset($this->footerlen[$frompage])) {
  24566. $tmpfooterlen = $this->footerlen[$frompage];
  24567. }
  24568. if (isset($this->transfmrk[$frompage])) {
  24569. $tmptransfmrk = $this->transfmrk[$frompage];
  24570. }
  24571. if (isset($this->PageAnnots[$frompage])) {
  24572. $tmpannots = $this->PageAnnots[$frompage];
  24573. }
  24574. if (isset($this->newpagegroup) AND !empty($this->newpagegroup)) {
  24575. for ($i = $frompage; $i > $topage; --$i) {
  24576. if (isset($this->newpagegroup[$i]) AND (($i + $this->pagegroups[$this->newpagegroup[$i]]) > $frompage)) {
  24577. --$this->pagegroups[$this->newpagegroup[$i]];
  24578. break;
  24579. }
  24580. }
  24581. for ($i = $topage; $i > 0; --$i) {
  24582. if (isset($this->newpagegroup[$i]) AND (($i + $this->pagegroups[$this->newpagegroup[$i]]) > $topage)) {
  24583. ++$this->pagegroups[$this->newpagegroup[$i]];
  24584. break;
  24585. }
  24586. }
  24587. }
  24588. for ($i = $frompage; $i > $topage; --$i) {
  24589. $j = $i - 1;
  24590. // shift pages down
  24591. $this->setPageBuffer($i, $this->getPageBuffer($j));
  24592. $this->pagedim[$i] = $this->pagedim[$j];
  24593. $this->pagelen[$i] = $this->pagelen[$j];
  24594. $this->intmrk[$i] = $this->intmrk[$j];
  24595. $this->bordermrk[$i] = $this->bordermrk[$j];
  24596. $this->cntmrk[$i] = $this->cntmrk[$j];
  24597. if (isset($this->footerpos[$j])) {
  24598. $this->footerpos[$i] = $this->footerpos[$j];
  24599. } elseif (isset($this->footerpos[$i])) {
  24600. unset($this->footerpos[$i]);
  24601. }
  24602. if (isset($this->footerlen[$j])) {
  24603. $this->footerlen[$i] = $this->footerlen[$j];
  24604. } elseif (isset($this->footerlen[$i])) {
  24605. unset($this->footerlen[$i]);
  24606. }
  24607. if (isset($this->transfmrk[$j])) {
  24608. $this->transfmrk[$i] = $this->transfmrk[$j];
  24609. } elseif (isset($this->transfmrk[$i])) {
  24610. unset($this->transfmrk[$i]);
  24611. }
  24612. if (isset($this->PageAnnots[$j])) {
  24613. $this->PageAnnots[$i] = $this->PageAnnots[$j];
  24614. } elseif (isset($this->PageAnnots[$i])) {
  24615. unset($this->PageAnnots[$i]);
  24616. }
  24617. if (isset($this->newpagegroup[$j])) {
  24618. $this->newpagegroup[$i] = $this->newpagegroup[$j];
  24619. unset($this->newpagegroup[$j]);
  24620. }
  24621. if ($this->currpagegroup == $j) {
  24622. $this->currpagegroup = $i;
  24623. }
  24624. }
  24625. $this->setPageBuffer($topage, $tmppage);
  24626. $this->pagedim[$topage] = $tmppagedim;
  24627. $this->pagelen[$topage] = $tmppagelen;
  24628. $this->intmrk[$topage] = $tmpintmrk;
  24629. $this->bordermrk[$topage] = $tmpbordermrk;
  24630. $this->cntmrk[$topage] = $tmpcntmrk;
  24631. if (isset($tmpfooterpos)) {
  24632. $this->footerpos[$topage] = $tmpfooterpos;
  24633. } elseif (isset($this->footerpos[$topage])) {
  24634. unset($this->footerpos[$topage]);
  24635. }
  24636. if (isset($tmpfooterlen)) {
  24637. $this->footerlen[$topage] = $tmpfooterlen;
  24638. } elseif (isset($this->footerlen[$topage])) {
  24639. unset($this->footerlen[$topage]);
  24640. }
  24641. if (isset($tmptransfmrk)) {
  24642. $this->transfmrk[$topage] = $tmptransfmrk;
  24643. } elseif (isset($this->transfmrk[$topage])) {
  24644. unset($this->transfmrk[$topage]);
  24645. }
  24646. if (isset($tmpannots)) {
  24647. $this->PageAnnots[$topage] = $tmpannots;
  24648. } elseif (isset($this->PageAnnots[$topage])) {
  24649. unset($this->PageAnnots[$topage]);
  24650. }
  24651. // adjust outlines
  24652. $tmpoutlines = $this->outlines;
  24653. foreach ($tmpoutlines as $key => $outline) {
  24654. if (($outline['p'] >= $topage) AND ($outline['p'] < $frompage)) {
  24655. $this->outlines[$key]['p'] = ($outline['p'] + 1);
  24656. } elseif ($outline['p'] == $frompage) {
  24657. $this->outlines[$key]['p'] = $topage;
  24658. }
  24659. }
  24660. // adjust dests
  24661. $tmpdests = $this->dests;
  24662. foreach ($tmpdests as $key => $dest) {
  24663. if (($dest['p'] >= $topage) AND ($dest['p'] < $frompage)) {
  24664. $this->dests[$key]['p'] = ($dest['p'] + 1);
  24665. } elseif ($dest['p'] == $frompage) {
  24666. $this->dests[$key]['p'] = $topage;
  24667. }
  24668. }
  24669. // adjust links
  24670. $tmplinks = $this->links;
  24671. foreach ($tmplinks as $key => $link) {
  24672. if (($link[0] >= $topage) AND ($link[0] < $frompage)) {
  24673. $this->links[$key][0] = ($link[0] + 1);
  24674. } elseif ($link[0] == $frompage) {
  24675. $this->links[$key][0] = $topage;
  24676. }
  24677. }
  24678. // adjust javascript
  24679. $tmpjavascript = $this->javascript;
  24680. global $jfrompage, $jtopage;
  24681. $jfrompage = $frompage;
  24682. $jtopage = $topage;
  24683. $this->javascript = preg_replace_callback('/this\.addField\(\'([^\']*)\',\'([^\']*)\',([0-9]+)/',
  24684. create_function('$matches', 'global $jfrompage, $jtopage;
  24685. $pagenum = intval($matches[3]) + 1;
  24686. if (($pagenum >= $jtopage) AND ($pagenum < $jfrompage)) {
  24687. $newpage = ($pagenum + 1);
  24688. } elseif ($pagenum == $jfrompage) {
  24689. $newpage = $jtopage;
  24690. } else {
  24691. $newpage = $pagenum;
  24692. }
  24693. --$newpage;
  24694. return "this.addField(\'".$matches[1]."\',\'".$matches[2]."\',".$newpage."";'), $tmpjavascript);
  24695. // return to last page
  24696. $this->lastPage(true);
  24697. return true;
  24698. }
  24699. /**
  24700. * Remove the specified page.
  24701. * @param $page (int) page to remove
  24702. * @return true in case of success, false in case of error.
  24703. * @public
  24704. * @since 4.6.004 (2009-04-23)
  24705. */
  24706. public function deletePage($page) {
  24707. if (($page < 1) OR ($page > $this->numpages)) {
  24708. return false;
  24709. }
  24710. // delete current page
  24711. unset($this->pages[$page]);
  24712. unset($this->pagedim[$page]);
  24713. unset($this->pagelen[$page]);
  24714. unset($this->intmrk[$page]);
  24715. unset($this->bordermrk[$page]);
  24716. unset($this->cntmrk[$page]);
  24717. if (isset($this->footerpos[$page])) {
  24718. unset($this->footerpos[$page]);
  24719. }
  24720. if (isset($this->footerlen[$page])) {
  24721. unset($this->footerlen[$page]);
  24722. }
  24723. if (isset($this->transfmrk[$page])) {
  24724. unset($this->transfmrk[$page]);
  24725. }
  24726. if (isset($this->PageAnnots[$page])) {
  24727. unset($this->PageAnnots[$page]);
  24728. }
  24729. if (isset($this->newpagegroup) AND !empty($this->newpagegroup)) {
  24730. for ($i = $page; $i > 0; --$i) {
  24731. if (isset($this->newpagegroup[$i]) AND (($i + $this->pagegroups[$this->newpagegroup[$i]]) > $page)) {
  24732. --$this->pagegroups[$this->newpagegroup[$i]];
  24733. break;
  24734. }
  24735. }
  24736. }
  24737. if (isset($this->pageopen[$page])) {
  24738. unset($this->pageopen[$page]);
  24739. }
  24740. if ($page < $this->numpages) {
  24741. // update remaining pages
  24742. for ($i = $page; $i < $this->numpages; ++$i) {
  24743. $j = $i + 1;
  24744. // shift pages
  24745. $this->setPageBuffer($i, $this->getPageBuffer($j));
  24746. $this->pagedim[$i] = $this->pagedim[$j];
  24747. $this->pagelen[$i] = $this->pagelen[$j];
  24748. $this->intmrk[$i] = $this->intmrk[$j];
  24749. $this->bordermrk[$i] = $this->bordermrk[$j];
  24750. $this->cntmrk[$i] = $this->cntmrk[$j];
  24751. if (isset($this->footerpos[$j])) {
  24752. $this->footerpos[$i] = $this->footerpos[$j];
  24753. } elseif (isset($this->footerpos[$i])) {
  24754. unset($this->footerpos[$i]);
  24755. }
  24756. if (isset($this->footerlen[$j])) {
  24757. $this->footerlen[$i] = $this->footerlen[$j];
  24758. } elseif (isset($this->footerlen[$i])) {
  24759. unset($this->footerlen[$i]);
  24760. }
  24761. if (isset($this->transfmrk[$j])) {
  24762. $this->transfmrk[$i] = $this->transfmrk[$j];
  24763. } elseif (isset($this->transfmrk[$i])) {
  24764. unset($this->transfmrk[$i]);
  24765. }
  24766. if (isset($this->PageAnnots[$j])) {
  24767. $this->PageAnnots[$i] = $this->PageAnnots[$j];
  24768. } elseif (isset($this->PageAnnots[$i])) {
  24769. unset($this->PageAnnots[$i]);
  24770. }
  24771. if (isset($this->newpagegroup[$j])) {
  24772. $this->newpagegroup[$i] = $this->newpagegroup[$j];
  24773. unset($this->newpagegroup[$j]);
  24774. }
  24775. if ($this->currpagegroup == $j) {
  24776. $this->currpagegroup = $i;
  24777. }
  24778. if (isset($this->pageopen[$j])) {
  24779. $this->pageopen[$i] = $this->pageopen[$j];
  24780. } elseif (isset($this->pageopen[$i])) {
  24781. unset($this->pageopen[$i]);
  24782. }
  24783. }
  24784. // remove last page
  24785. unset($this->pages[$this->numpages]);
  24786. unset($this->pagedim[$this->numpages]);
  24787. unset($this->pagelen[$this->numpages]);
  24788. unset($this->intmrk[$this->numpages]);
  24789. unset($this->bordermrk[$this->numpages]);
  24790. unset($this->cntmrk[$this->numpages]);
  24791. if (isset($this->footerpos[$this->numpages])) {
  24792. unset($this->footerpos[$this->numpages]);
  24793. }
  24794. if (isset($this->footerlen[$this->numpages])) {
  24795. unset($this->footerlen[$this->numpages]);
  24796. }
  24797. if (isset($this->transfmrk[$this->numpages])) {
  24798. unset($this->transfmrk[$this->numpages]);
  24799. }
  24800. if (isset($this->PageAnnots[$this->numpages])) {
  24801. unset($this->PageAnnots[$this->numpages]);
  24802. }
  24803. if (isset($this->newpagegroup[$this->numpages])) {
  24804. unset($this->newpagegroup[$this->numpages]);
  24805. }
  24806. if ($this->currpagegroup == $this->numpages) {
  24807. $this->currpagegroup = ($this->numpages - 1);
  24808. }
  24809. if (isset($this->pagegroups[$this->numpages])) {
  24810. unset($this->pagegroups[$this->numpages]);
  24811. }
  24812. if (isset($this->pageopen[$this->numpages])) {
  24813. unset($this->pageopen[$this->numpages]);
  24814. }
  24815. }
  24816. --$this->numpages;
  24817. $this->page = $this->numpages;
  24818. // adjust outlines
  24819. $tmpoutlines = $this->outlines;
  24820. foreach ($tmpoutlines as $key => $outline) {
  24821. if ($outline['p'] > $page) {
  24822. $this->outlines[$key]['p'] = $outline['p'] - 1;
  24823. } elseif ($outline['p'] == $page) {
  24824. unset($this->outlines[$key]);
  24825. }
  24826. }
  24827. // adjust dests
  24828. $tmpdests = $this->dests;
  24829. foreach ($tmpdests as $key => $dest) {
  24830. if ($dest['p'] > $page) {
  24831. $this->dests[$key]['p'] = $dest['p'] - 1;
  24832. } elseif ($dest['p'] == $page) {
  24833. unset($this->dests[$key]);
  24834. }
  24835. }
  24836. // adjust links
  24837. $tmplinks = $this->links;
  24838. foreach ($tmplinks as $key => $link) {
  24839. if ($link[0] > $page) {
  24840. $this->links[$key][0] = $link[0] - 1;
  24841. } elseif ($link[0] == $page) {
  24842. unset($this->links[$key]);
  24843. }
  24844. }
  24845. // adjust javascript
  24846. $tmpjavascript = $this->javascript;
  24847. global $jpage;
  24848. $jpage = $page;
  24849. $this->javascript = preg_replace_callback('/this\.addField\(\'([^\']*)\',\'([^\']*)\',([0-9]+)/',
  24850. create_function('$matches', 'global $jpage;
  24851. $pagenum = intval($matches[3]) + 1;
  24852. if ($pagenum >= $jpage) {
  24853. $newpage = ($pagenum - 1);
  24854. } elseif ($pagenum == $jpage) {
  24855. $newpage = 1;
  24856. } else {
  24857. $newpage = $pagenum;
  24858. }
  24859. --$newpage;
  24860. return "this.addField(\'".$matches[1]."\',\'".$matches[2]."\',".$newpage."";'), $tmpjavascript);
  24861. // return to last page
  24862. $this->lastPage(true);
  24863. return true;
  24864. }
  24865. /**
  24866. * Clone the specified page to a new page.
  24867. * @param $page (int) number of page to copy (0 = current page)
  24868. * @return true in case of success, false in case of error.
  24869. * @public
  24870. * @since 4.9.015 (2010-04-20)
  24871. */
  24872. public function copyPage($page=0) {
  24873. if ($page == 0) {
  24874. // default value
  24875. $page = $this->page;
  24876. }
  24877. if (($page < 1) OR ($page > $this->numpages)) {
  24878. return false;
  24879. }
  24880. // close the last page
  24881. $this->endPage();
  24882. // copy all page-related states
  24883. ++$this->numpages;
  24884. $this->page = $this->numpages;
  24885. $this->setPageBuffer($this->page, $this->getPageBuffer($page));
  24886. $this->pagedim[$this->page] = $this->pagedim[$page];
  24887. $this->pagelen[$this->page] = $this->pagelen[$page];
  24888. $this->intmrk[$this->page] = $this->intmrk[$page];
  24889. $this->bordermrk[$this->page] = $this->bordermrk[$page];
  24890. $this->cntmrk[$this->page] = $this->cntmrk[$page];
  24891. $this->pageopen[$this->page] = false;
  24892. if (isset($this->footerpos[$page])) {
  24893. $this->footerpos[$this->page] = $this->footerpos[$page];
  24894. }
  24895. if (isset($this->footerlen[$page])) {
  24896. $this->footerlen[$this->page] = $this->footerlen[$page];
  24897. }
  24898. if (isset($this->transfmrk[$page])) {
  24899. $this->transfmrk[$this->page] = $this->transfmrk[$page];
  24900. }
  24901. if (isset($this->PageAnnots[$page])) {
  24902. $this->PageAnnots[$this->page] = $this->PageAnnots[$page];
  24903. }
  24904. if (isset($this->newpagegroup[$page])) {
  24905. // start a new group
  24906. $this->newpagegroup[$this->page] = sizeof($this->newpagegroup) + 1;
  24907. $this->currpagegroup = $this->newpagegroup[$this->page];
  24908. $this->pagegroups[$this->currpagegroup] = 1;
  24909. } elseif (isset($this->currpagegroup) AND ($this->currpagegroup > 0)) {
  24910. ++$this->pagegroups[$this->currpagegroup];
  24911. }
  24912. // copy outlines
  24913. $tmpoutlines = $this->outlines;
  24914. foreach ($tmpoutlines as $key => $outline) {
  24915. if ($outline['p'] == $page) {
  24916. $this->outlines[] = array('t' => $outline['t'], 'l' => $outline['l'], 'y' => $outline['y'], 'p' => $this->page, 's' => $outline['s'], 'c' => $outline['c']);
  24917. }
  24918. }
  24919. // copy links
  24920. $tmplinks = $this->links;
  24921. foreach ($tmplinks as $key => $link) {
  24922. if ($link[0] == $page) {
  24923. $this->links[] = array($this->page, $link[1]);
  24924. }
  24925. }
  24926. // return to last page
  24927. $this->lastPage(true);
  24928. return true;
  24929. }
  24930. /**
  24931. * Output a Table of Content Index (TOC).
  24932. * This method must be called after all Bookmarks were set.
  24933. * Before calling this method you have to open the page using the addTOCPage() method.
  24934. * After calling this method you have to call endTOCPage() to close the TOC page.
  24935. * You can override this method to achieve different styles.
  24936. * @param $page (int) page number where this TOC should be inserted (leave empty for current page).
  24937. * @param $numbersfont (string) set the font for page numbers (please use monospaced font for better alignment).
  24938. * @param $filler (string) string used to fill the space between text and page number.
  24939. * @param $toc_name (string) name to use for TOC bookmark.
  24940. * @param $style (string) Font style for title: B = Bold, I = Italic, BI = Bold + Italic.
  24941. * @param $color (array) RGB color array for bookmark title (values from 0 to 255).
  24942. * @public
  24943. * @author Nicola Asuni
  24944. * @since 4.5.000 (2009-01-02)
  24945. * @see addTOCPage(), endTOCPage(), addHTMLTOC()
  24946. */
  24947. public function addTOC($page='', $numbersfont='', $filler='.', $toc_name='TOC', $style='', $color=array(0,0,0)) {
  24948. $fontsize = $this->FontSizePt;
  24949. $fontfamily = $this->FontFamily;
  24950. $fontstyle = $this->FontStyle;
  24951. $w = $this->w - $this->lMargin - $this->rMargin;
  24952. $spacer = $this->GetStringWidth(chr(32)) * 4;
  24953. $page_first = $this->getPage();
  24954. $lmargin = $this->lMargin;
  24955. $rmargin = $this->rMargin;
  24956. $x_start = $this->GetX();
  24957. $current_page = $this->page;
  24958. $current_column = $this->current_column;
  24959. if ($this->empty_string($numbersfont)) {
  24960. $numbersfont = $this->default_monospaced_font;
  24961. }
  24962. if ($this->empty_string($filler)) {
  24963. $filler = ' ';
  24964. }
  24965. if ($this->empty_string($page)) {
  24966. $gap = ' ';
  24967. } else {
  24968. $gap = '';
  24969. if ($page < 1) {
  24970. $page = 1;
  24971. }
  24972. }
  24973. $this->SetFont($numbersfont, $fontstyle, $fontsize);
  24974. $numwidth = $this->GetStringWidth('00000');
  24975. $maxpage = 0; //used for pages on attached documents
  24976. foreach ($this->outlines as $key => $outline) {
  24977. // check for extra pages (used for attachments)
  24978. if (($this->page > $page_first) AND ($outline['p'] >= $this->numpages)) {
  24979. $outline['p'] += ($this->page - $page_first);
  24980. }
  24981. if ($this->rtl) {
  24982. $aligntext = 'R';
  24983. $alignnum = 'L';
  24984. } else {
  24985. $aligntext = 'L';
  24986. $alignnum = 'R';
  24987. }
  24988. if ($outline['l'] == 0) {
  24989. $this->SetFont($fontfamily, $fontstyle.'B', $fontsize);
  24990. } else {
  24991. $this->SetFont($fontfamily, $fontstyle, $fontsize - $outline['l']);
  24992. }
  24993. // check for page break
  24994. $this->checkPageBreak((2 * $this->FontSize * $this->cell_height_ratio));
  24995. // set margins and X position
  24996. if (($this->page == $current_page) AND ($this->current_column == $current_column)) {
  24997. $this->lMargin = $lmargin;
  24998. $this->rMargin = $rmargin;
  24999. } else {
  25000. if ($this->current_column != $current_column) {
  25001. if ($this->rtl) {
  25002. $x_start = $this->w - $this->columns[$this->current_column]['x'];
  25003. } else {
  25004. $x_start = $this->columns[$this->current_column]['x'];
  25005. }
  25006. }
  25007. $lmargin = $this->lMargin;
  25008. $rmargin = $this->rMargin;
  25009. $current_page = $this->page;
  25010. $current_column = $this->current_column;
  25011. }
  25012. $this->SetX($x_start);
  25013. $indent = ($spacer * $outline['l']);
  25014. if ($this->rtl) {
  25015. $this->x -= $indent;
  25016. $this->rMargin = $this->w - $this->x;
  25017. } else {
  25018. $this->x += $indent;
  25019. $this->lMargin = $this->x;
  25020. }
  25021. $link = $this->AddLink();
  25022. $this->SetLink($link, $outline['y'], $outline['p']);
  25023. // write the text
  25024. if ($this->rtl) {
  25025. $txt = ' '.$outline['t'];
  25026. } else {
  25027. $txt = $outline['t'].' ';
  25028. }
  25029. $this->Write(0, $txt, $link, false, $aligntext, false, 0, false, false, 0, $numwidth, '');
  25030. if ($this->rtl) {
  25031. $tw = $this->x - $this->lMargin;
  25032. } else {
  25033. $tw = $this->w - $this->rMargin - $this->x;
  25034. }
  25035. $this->SetFont($numbersfont, $fontstyle, $fontsize);
  25036. if ($this->empty_string($page)) {
  25037. $pagenum = $outline['p'];
  25038. } else {
  25039. // placemark to be replaced with the correct number
  25040. $pagenum = '{#'.($outline['p']).'}';
  25041. if ($this->isUnicodeFont()) {
  25042. $pagenum = '{'.$pagenum.'}';
  25043. }
  25044. $maxpage = max($maxpage, $outline['p']);
  25045. }
  25046. $fw = ($tw - $this->GetStringWidth($pagenum.$filler));
  25047. $numfills = floor($fw / $this->GetStringWidth($filler));
  25048. if ($numfills > 0) {
  25049. $rowfill = str_repeat($filler, $numfills);
  25050. } else {
  25051. $rowfill = '';
  25052. }
  25053. if ($this->rtl) {
  25054. $pagenum = $pagenum.$gap.$rowfill;
  25055. } else {
  25056. $pagenum = $rowfill.$gap.$pagenum;
  25057. }
  25058. // write the number
  25059. $this->Cell($tw, 0, $pagenum, 0, 1, $alignnum, 0, $link, 0);
  25060. }
  25061. $page_last = $this->getPage();
  25062. $maxpage = max($maxpage, $page_last);
  25063. $numpages = $page_last - $page_first + 1;
  25064. if (!$this->empty_string($page)) {
  25065. for ($p = $page_first; $p <= $page_last; ++$p) {
  25066. // get page data
  25067. $temppage = $this->getPageBuffer($p);
  25068. for ($n = 1; $n <= $maxpage; ++$n) {
  25069. // update page numbers
  25070. $a = '{#'.$n.'}';
  25071. // get page number aliases
  25072. $pnalias = $this->getInternalPageNumberAliases($a);
  25073. // calculate replacement number
  25074. if (($n >= $page) AND ($n <= $this->numpages)) {
  25075. $np = $n + $numpages;
  25076. } else {
  25077. $np = $n;
  25078. }
  25079. $na = $this->formatTOCPageNumber(($this->starting_page_number + $np - 1));
  25080. $nu = $this->UTF8ToUTF16BE($na, false);
  25081. // replace aliases with numbers
  25082. foreach ($pnalias['u'] as $u) {
  25083. $sfill = str_repeat($filler, max(0, (strlen($u) - strlen($nu.' '))));
  25084. if ($this->rtl) {
  25085. $nr = $nu.$this->UTF8ToUTF16BE(' '.$sfill);
  25086. } else {
  25087. $nr = $this->UTF8ToUTF16BE($sfill.' ').$nu;
  25088. }
  25089. $temppage = str_replace($u, $nr, $temppage);
  25090. }
  25091. foreach ($pnalias['a'] as $a) {
  25092. $sfill = str_repeat($filler, max(0, (strlen($a) - strlen($na.' '))));
  25093. if ($this->rtl) {
  25094. $nr = $na.' '.$sfill;
  25095. } else {
  25096. $nr = $sfill.' '.$na;
  25097. }
  25098. $temppage = str_replace($a, $nr, $temppage);
  25099. }
  25100. }
  25101. // save changes
  25102. $this->setPageBuffer($p, $temppage);
  25103. }
  25104. // move pages
  25105. $this->Bookmark($toc_name, 0, 0, $page_first, $style, $color);
  25106. for ($i = 0; $i < $numpages; ++$i) {
  25107. $this->movePage($page_last, $page);
  25108. }
  25109. }
  25110. }
  25111. /**
  25112. * Output a Table Of Content Index (TOC) using HTML templates.
  25113. * This method must be called after all Bookmarks were set.
  25114. * Before calling this method you have to open the page using the addTOCPage() method.
  25115. * After calling this method you have to call endTOCPage() to close the TOC page.
  25116. * @param $page (int) page number where this TOC should be inserted (leave empty for current page).
  25117. * @param $toc_name (string) name to use for TOC bookmark.
  25118. * @param $templates (array) array of html templates. Use: "#TOC_DESCRIPTION#" for bookmark title, "#TOC_PAGE_NUMBER#" for page number.
  25119. * @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)
  25120. * @param $style (string) Font style for title: B = Bold, I = Italic, BI = Bold + Italic.
  25121. * @param $color (array) RGB color array for title (values from 0 to 255).
  25122. * @public
  25123. * @author Nicola Asuni
  25124. * @since 5.0.001 (2010-05-06)
  25125. * @see addTOCPage(), endTOCPage(), addTOC()
  25126. */
  25127. public function addHTMLTOC($page='', $toc_name='TOC', $templates=array(), $correct_align=true, $style='', $color=array(0,0,0)) {
  25128. $filler = ' ';
  25129. $prev_htmlLinkColorArray = $this->htmlLinkColorArray;
  25130. $prev_htmlLinkFontStyle = $this->htmlLinkFontStyle;
  25131. // set new style for link
  25132. $this->htmlLinkColorArray = array();
  25133. $this->htmlLinkFontStyle = '';
  25134. $page_first = $this->getPage();
  25135. // get the font type used for numbers in each template
  25136. $current_font = $this->FontFamily;
  25137. foreach ($templates as $level => $html) {
  25138. $dom = $this->getHtmlDomArray($html);
  25139. foreach ($dom as $key => $value) {
  25140. if ($value['value'] == '#TOC_PAGE_NUMBER#') {
  25141. $this->SetFont($dom[($key - 1)]['fontname']);
  25142. $templates['F'.$level] = $this->isUnicodeFont();
  25143. }
  25144. }
  25145. }
  25146. $this->SetFont($current_font);
  25147. $maxpage = 0; //used for pages on attached documents
  25148. foreach ($this->outlines as $key => $outline) {
  25149. // get HTML template
  25150. $row = $templates[$outline['l']];
  25151. if ($this->empty_string($page)) {
  25152. $pagenum = $outline['p'];
  25153. } else {
  25154. // placemark to be replaced with the correct number
  25155. $pagenum = '{#'.($outline['p']).'}';
  25156. if ($templates['F'.$outline['l']]) {
  25157. $pagenum = '{'.$pagenum.'}';
  25158. }
  25159. $maxpage = max($maxpage, $outline['p']);
  25160. }
  25161. // replace templates with current values
  25162. $row = str_replace('#TOC_DESCRIPTION#', $outline['t'], $row);
  25163. $row = str_replace('#TOC_PAGE_NUMBER#', $pagenum, $row);
  25164. // add link to page
  25165. $row = '<a href="#'.$outline['p'].','.$outline['y'].'">'.$row.'</a>';
  25166. // write bookmark entry
  25167. $this->writeHTML($row, false, false, true, false, '');
  25168. }
  25169. // restore link styles
  25170. $this->htmlLinkColorArray = $prev_htmlLinkColorArray;
  25171. $this->htmlLinkFontStyle = $prev_htmlLinkFontStyle;
  25172. // move TOC page and replace numbers
  25173. $page_last = $this->getPage();
  25174. $maxpage = max($maxpage, $page_last);
  25175. $numpages = $page_last - $page_first + 1;
  25176. if (!$this->empty_string($page)) {
  25177. for ($p = $page_first; $p <= $page_last; ++$p) {
  25178. // get page data
  25179. $temppage = $this->getPageBuffer($p);
  25180. for ($n = 1; $n <= $maxpage; ++$n) {
  25181. // update page numbers
  25182. $a = '{#'.$n.'}';
  25183. // get page number aliases
  25184. $pnalias = $this->getInternalPageNumberAliases($a);
  25185. // calculate replacement number
  25186. if ($n >= $page) {
  25187. $np = $n + $numpages;
  25188. } else {
  25189. $np = $n;
  25190. }
  25191. $na = $this->formatTOCPageNumber(($this->starting_page_number + $np - 1));
  25192. $nu = $this->UTF8ToUTF16BE($na, false);
  25193. // replace aliases with numbers
  25194. foreach ($pnalias['u'] as $u) {
  25195. if ($correct_align) {
  25196. $sfill = str_repeat($filler, (strlen($u) - strlen($nu.' ')));
  25197. if ($this->rtl) {
  25198. $nr = $nu.$this->UTF8ToUTF16BE(' '.$sfill);
  25199. } else {
  25200. $nr = $this->UTF8ToUTF16BE($sfill.' ').$nu;
  25201. }
  25202. } else {
  25203. $nr = $nu;
  25204. }
  25205. $temppage = str_replace($u, $nr, $temppage);
  25206. }
  25207. foreach ($pnalias['a'] as $a) {
  25208. if ($correct_align) {
  25209. $sfill = str_repeat($filler, (strlen($a) - strlen($na.' ')));
  25210. if ($this->rtl) {
  25211. $nr = $na.' '.$sfill;
  25212. } else {
  25213. $nr = $sfill.' '.$na;
  25214. }
  25215. } else {
  25216. $nr = $na;
  25217. }
  25218. $temppage = str_replace($a, $nr, $temppage);
  25219. }
  25220. }
  25221. // save changes
  25222. $this->setPageBuffer($p, $temppage);
  25223. }
  25224. // move pages
  25225. $this->Bookmark($toc_name, 0, 0, $page_first, $style, $color);
  25226. for ($i = 0; $i < $numpages; ++$i) {
  25227. $this->movePage($page_last, $page);
  25228. }
  25229. }
  25230. }
  25231. /**
  25232. * Stores a copy of the current TCPDF object used for undo operation.
  25233. * @public
  25234. * @since 4.5.029 (2009-03-19)
  25235. */
  25236. public function startTransaction() {
  25237. if (isset($this->objcopy)) {
  25238. // remove previous copy
  25239. $this->commitTransaction();
  25240. }
  25241. // record current page number and Y position
  25242. $this->start_transaction_page = $this->page;
  25243. $this->start_transaction_y = $this->y;
  25244. // clone current object
  25245. $this->objcopy = $this->objclone($this);
  25246. }
  25247. /**
  25248. * Delete the copy of the current TCPDF object used for undo operation.
  25249. * @public
  25250. * @since 4.5.029 (2009-03-19)
  25251. */
  25252. public function commitTransaction() {
  25253. if (isset($this->objcopy)) {
  25254. $this->objcopy->_destroy(true, true);
  25255. unset($this->objcopy);
  25256. }
  25257. }
  25258. /**
  25259. * This method allows to undo the latest transaction by returning the latest saved TCPDF object with startTransaction().
  25260. * @param $self (boolean) if true restores current class object to previous state without the need of reassignment via the returned value.
  25261. * @return TCPDF object.
  25262. * @public
  25263. * @since 4.5.029 (2009-03-19)
  25264. */
  25265. public function rollbackTransaction($self=false) {
  25266. if (isset($this->objcopy)) {
  25267. if (isset($this->objcopy->diskcache) AND $this->objcopy->diskcache) {
  25268. // truncate files to previous values
  25269. foreach ($this->objcopy->cache_file_length as $file => $length) {
  25270. $file = substr($file, 1);
  25271. $handle = fopen($file, 'r+');
  25272. ftruncate($handle, $length);
  25273. }
  25274. }
  25275. $this->_destroy(true, true);
  25276. if ($self) {
  25277. $objvars = get_object_vars($this->objcopy);
  25278. foreach ($objvars as $key => $value) {
  25279. $this->$key = $value;
  25280. }
  25281. }
  25282. return $this->objcopy;
  25283. }
  25284. return $this;
  25285. }
  25286. /**
  25287. * Creates a copy of a class object
  25288. * @param $object (object) class object to be cloned
  25289. * @return cloned object
  25290. * @public
  25291. * @since 4.5.029 (2009-03-19)
  25292. */
  25293. public function objclone($object) {
  25294. return @clone($object);
  25295. }
  25296. /**
  25297. * Determine whether a string is empty.
  25298. * @param $str (string) string to be checked
  25299. * @return boolean true if string is empty
  25300. * @public
  25301. * @since 4.5.044 (2009-04-16)
  25302. */
  25303. public function empty_string($str) {
  25304. return (is_null($str) OR (is_string($str) AND (strlen($str) == 0)));
  25305. }
  25306. /**
  25307. * Find position of last occurrence of a substring in a string
  25308. * @param $haystack (string) The string to search in.
  25309. * @param $needle (string) substring to search.
  25310. * @param $offset (int) May be specified to begin searching an arbitrary number of characters into the string.
  25311. * @return Returns the position where the needle exists. Returns FALSE if the needle was not found.
  25312. * @public
  25313. * @since 4.8.038 (2010-03-13)
  25314. */
  25315. public function revstrpos($haystack, $needle, $offset = 0) {
  25316. $length = strlen($haystack);
  25317. $offset = ($offset > 0)?($length - $offset):abs($offset);
  25318. $pos = strpos(strrev($haystack), strrev($needle), $offset);
  25319. return ($pos === false)?false:($length - $pos - strlen($needle));
  25320. }
  25321. // --- MULTI COLUMNS METHODS -----------------------
  25322. /**
  25323. * Set multiple columns of the same size
  25324. * @param $numcols (int) number of columns (set to zero to disable columns mode)
  25325. * @param $width (int) column width
  25326. * @param $y (int) column starting Y position (leave empty for current Y position)
  25327. * @public
  25328. * @since 4.9.001 (2010-03-28)
  25329. */
  25330. public function setEqualColumns($numcols=0, $width=0, $y='') {
  25331. $this->columns = array();
  25332. if ($numcols < 2) {
  25333. $numcols = 0;
  25334. $this->columns = array();
  25335. } else {
  25336. // maximum column width
  25337. $maxwidth = ($this->w - $this->original_lMargin - $this->original_rMargin) / $numcols;
  25338. if (($width == 0) OR ($width > $maxwidth)) {
  25339. $width = $maxwidth;
  25340. }
  25341. if ($this->empty_string($y)) {
  25342. $y = $this->y;
  25343. }
  25344. // space between columns
  25345. $space = (($this->w - $this->original_lMargin - $this->original_rMargin - ($numcols * $width)) / ($numcols - 1));
  25346. // fill the columns array (with, space, starting Y position)
  25347. for ($i = 0; $i < $numcols; ++$i) {
  25348. $this->columns[$i] = array('w' => $width, 's' => $space, 'y' => $y);
  25349. }
  25350. }
  25351. $this->num_columns = $numcols;
  25352. $this->current_column = 0;
  25353. $this->column_start_page = $this->page;
  25354. $this->selectColumn(0);
  25355. }
  25356. /**
  25357. * Remove columns and reset page margins.
  25358. * @public
  25359. * @since 5.9.072 (2011-04-26)
  25360. */
  25361. public function resetColumns() {
  25362. $this->lMargin = $this->original_lMargin;
  25363. $this->rMargin = $this->original_rMargin;
  25364. $this->setEqualColumns();
  25365. }
  25366. /**
  25367. * Set columns array.
  25368. * Each column is represented by an array of arrays with the following keys: (w = width, s = space between columns, y = column top position).
  25369. * @param $columns (array)
  25370. * @public
  25371. * @since 4.9.001 (2010-03-28)
  25372. */
  25373. public function setColumnsArray($columns) {
  25374. $this->columns = $columns;
  25375. $this->num_columns = count($columns);
  25376. $this->current_column = 0;
  25377. $this->column_start_page = $this->page;
  25378. $this->selectColumn(0);
  25379. }
  25380. /**
  25381. * Set position at a given column
  25382. * @param $col (int) column number (from 0 to getNumberOfColumns()-1); empty string = current column.
  25383. * @public
  25384. * @since 4.9.001 (2010-03-28)
  25385. */
  25386. public function selectColumn($col='') {
  25387. if (is_string($col)) {
  25388. $col = $this->current_column;
  25389. } elseif ($col >= $this->num_columns) {
  25390. $col = 0;
  25391. }
  25392. $xshift = array('x' => 0, 's' => array('H' => 0, 'V' => 0), 'p' => array('L' => 0, 'T' => 0, 'R' => 0, 'B' => 0));
  25393. $enable_thead = false;
  25394. if ($this->num_columns > 1) {
  25395. if ($col != $this->current_column) {
  25396. // move Y pointer at the top of the column
  25397. if ($this->column_start_page == $this->page) {
  25398. $this->y = $this->columns[$col]['y'];
  25399. } else {
  25400. $this->y = $this->tMargin;
  25401. }
  25402. // Avoid to write table headers more than once
  25403. if (($this->page > $this->maxselcol['page']) OR (($this->page == $this->maxselcol['page']) AND ($col > $this->maxselcol['column']))) {
  25404. $enable_thead = true;
  25405. $this->maxselcol['page'] = $this->page;
  25406. $this->maxselcol['column'] = $col;
  25407. }
  25408. }
  25409. $xshift = $this->colxshift;
  25410. // set X position of the current column by case
  25411. $listindent = ($this->listindentlevel * $this->listindent);
  25412. // calculate column X position
  25413. $colpos = 0;
  25414. for ($i = 0; $i < $col; ++$i) {
  25415. $colpos += ($this->columns[$i]['w'] + $this->columns[$i]['s']);
  25416. }
  25417. if ($this->rtl) {
  25418. $x = $this->w - $this->original_rMargin - $colpos;
  25419. $this->rMargin = ($this->w - $x + $listindent);
  25420. $this->lMargin = ($x - $this->columns[$col]['w']);
  25421. $this->x = $x - $listindent;
  25422. } else {
  25423. $x = $this->original_lMargin + $colpos;
  25424. $this->lMargin = ($x + $listindent);
  25425. $this->rMargin = ($this->w - $x - $this->columns[$col]['w']);
  25426. $this->x = $x + $listindent;
  25427. }
  25428. $this->columns[$col]['x'] = $x;
  25429. }
  25430. $this->current_column = $col;
  25431. // fix for HTML mode
  25432. $this->newline = true;
  25433. // print HTML table header (if any)
  25434. if ((!$this->empty_string($this->thead)) AND (!$this->inthead)) {
  25435. if ($enable_thead) {
  25436. // print table header
  25437. $this->writeHTML($this->thead, false, false, false, false, '');
  25438. $this->y += $xshift['s']['V'];
  25439. // store end of header position
  25440. if (!isset($this->columns[$col]['th'])) {
  25441. $this->columns[$col]['th'] = array();
  25442. }
  25443. $this->columns[$col]['th']['\''.$this->page.'\''] = $this->y;
  25444. $this->lasth = 0;
  25445. } elseif (isset($this->columns[$col]['th']['\''.$this->page.'\''])) {
  25446. $this->y = $this->columns[$col]['th']['\''.$this->page.'\''];
  25447. }
  25448. }
  25449. // account for an html table cell over multiple columns
  25450. if ($this->rtl) {
  25451. $this->rMargin += $xshift['x'];
  25452. $this->x -= ($xshift['x'] + $xshift['p']['R']);
  25453. } else {
  25454. $this->lMargin += $xshift['x'];
  25455. $this->x += $xshift['x'] + $xshift['p']['L'];
  25456. }
  25457. }
  25458. /**
  25459. * Return the current column number
  25460. * @return int current column number
  25461. * @public
  25462. * @since 5.5.011 (2010-07-08)
  25463. */
  25464. public function getColumn() {
  25465. return $this->current_column;
  25466. }
  25467. /**
  25468. * Return the current number of columns.
  25469. * @return int number of columns
  25470. * @public
  25471. * @since 5.8.018 (2010-08-25)
  25472. */
  25473. public function getNumberOfColumns() {
  25474. return $this->num_columns;
  25475. }
  25476. /**
  25477. * Serialize an array of parameters to be used with TCPDF tag in HTML code.
  25478. * @param $pararray (array) parameters array
  25479. * @return sting containing serialized data
  25480. * @public
  25481. * @since 4.9.006 (2010-04-02)
  25482. */
  25483. public function serializeTCPDFtagParameters($pararray) {
  25484. return urlencode(serialize($pararray));
  25485. }
  25486. /**
  25487. * Set Text rendering mode.
  25488. * @param $stroke (int) outline size in user units (0 = disable).
  25489. * @param $fill (boolean) if true fills the text (default).
  25490. * @param $clip (boolean) if true activate clipping mode
  25491. * @public
  25492. * @since 4.9.008 (2009-04-02)
  25493. */
  25494. public function setTextRenderingMode($stroke=0, $fill=true, $clip=false) {
  25495. // Ref.: PDF 32000-1:2008 - 9.3.6 Text Rendering Mode
  25496. // convert text rendering parameters
  25497. if ($stroke < 0) {
  25498. $stroke = 0;
  25499. }
  25500. if ($fill === true) {
  25501. if ($stroke > 0) {
  25502. if ($clip === true) {
  25503. // Fill, then stroke text and add to path for clipping
  25504. $textrendermode = 6;
  25505. } else {
  25506. // Fill, then stroke text
  25507. $textrendermode = 2;
  25508. }
  25509. $textstrokewidth = $stroke;
  25510. } else {
  25511. if ($clip === true) {
  25512. // Fill text and add to path for clipping
  25513. $textrendermode = 4;
  25514. } else {
  25515. // Fill text
  25516. $textrendermode = 0;
  25517. }
  25518. }
  25519. } else {
  25520. if ($stroke > 0) {
  25521. if ($clip === true) {
  25522. // Stroke text and add to path for clipping
  25523. $textrendermode = 5;
  25524. } else {
  25525. // Stroke text
  25526. $textrendermode = 1;
  25527. }
  25528. $textstrokewidth = $stroke;
  25529. } else {
  25530. if ($clip === true) {
  25531. // Add text to path for clipping
  25532. $textrendermode = 7;
  25533. } else {
  25534. // Neither fill nor stroke text (invisible)
  25535. $textrendermode = 3;
  25536. }
  25537. }
  25538. }
  25539. $this->textrendermode = $textrendermode;
  25540. $this->textstrokewidth = $stroke * $this->k;
  25541. }
  25542. /**
  25543. * Returns an array of chars containing soft hyphens.
  25544. * @param $word (array) array of chars
  25545. * @param $patterns (array) Array of hypenation patterns.
  25546. * @param $dictionary (array) Array of words to be returned without applying the hyphenation algoritm.
  25547. * @param $leftmin (int) Minimum number of character to leave on the left of the word without applying the hyphens.
  25548. * @param $rightmin (int) Minimum number of character to leave on the right of the word without applying the hyphens.
  25549. * @param $charmin (int) Minimum word length to apply the hyphenation algoritm.
  25550. * @param $charmax (int) Maximum length of broken piece of word.
  25551. * @return array text with soft hyphens
  25552. * @author Nicola Asuni
  25553. * @since 4.9.012 (2010-04-12)
  25554. * @protected
  25555. */
  25556. protected function hyphenateWord($word, $patterns, $dictionary=array(), $leftmin=1, $rightmin=2, $charmin=1, $charmax=8) {
  25557. $hyphenword = array(); // hyphens positions
  25558. $numchars = count($word);
  25559. if ($numchars <= $charmin) {
  25560. return $word;
  25561. }
  25562. $word_string = $this->UTF8ArrSubString($word);
  25563. // some words will be returned as-is
  25564. $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})(\]?)$/';
  25565. if (preg_match($pattern, $word_string) > 0) {
  25566. // email
  25567. return $word;
  25568. }
  25569. $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})(\]?)$/';
  25570. if (preg_match($pattern, $word_string) > 0) {
  25571. // URL
  25572. return $word;
  25573. }
  25574. if (isset($dictionary[$word_string])) {
  25575. return $this->UTF8StringToArray($dictionary[$word_string]);
  25576. }
  25577. // suround word with '_' characters
  25578. $tmpword = array_merge(array(95), $word, array(95));
  25579. $tmpnumchars = $numchars + 2;
  25580. $maxpos = $tmpnumchars - $charmin;
  25581. for ($pos = 0; $pos < $maxpos; ++$pos) {
  25582. $imax = min(($tmpnumchars - $pos), $charmax);
  25583. for ($i = $charmin; $i <= $imax; ++$i) {
  25584. $subword = strtolower($this->UTF8ArrSubString($tmpword, $pos, $pos + $i));
  25585. if (isset($patterns[$subword])) {
  25586. $pattern = $this->UTF8StringToArray($patterns[$subword]);
  25587. $pattern_length = count($pattern);
  25588. $digits = 1;
  25589. for ($j = 0; $j < $pattern_length; ++$j) {
  25590. // check if $pattern[$j] is a number
  25591. if (($pattern[$j] >= 48) AND ($pattern[$j] <= 57)) {
  25592. if ($j == 0) {
  25593. $zero = $pos - 1;
  25594. } else {
  25595. $zero = $pos + $j - $digits;
  25596. }
  25597. if (!isset($hyphenword[$zero]) OR ($hyphenword[$zero] != $pattern[$j])) {
  25598. $hyphenword[$zero] = $this->unichr($pattern[$j]);
  25599. }
  25600. ++$digits;
  25601. }
  25602. }
  25603. }
  25604. }
  25605. }
  25606. $inserted = 0;
  25607. $maxpos = $numchars - $rightmin;
  25608. for ($i = $leftmin; $i <= $maxpos; ++$i) {
  25609. if (isset($hyphenword[$i]) AND (($hyphenword[$i] % 2) != 0)) {
  25610. // 173 = soft hyphen character
  25611. array_splice($word, $i + $inserted, 0, 173);
  25612. ++$inserted;
  25613. }
  25614. }
  25615. return $word;
  25616. }
  25617. /**
  25618. * Returns an array of hyphenation patterns.
  25619. * @param $file (string) TEX file containing hypenation patterns. TEX pattrns can be downloaded from http://www.ctan.org/tex-archive/language/hyph-utf8/tex/generic/hyph-utf8/patterns/
  25620. * @return array of hyphenation patterns
  25621. * @author Nicola Asuni
  25622. * @since 4.9.012 (2010-04-12)
  25623. * @public
  25624. */
  25625. public function getHyphenPatternsFromTEX($file) {
  25626. // TEX patterns are available at:
  25627. // http://www.ctan.org/tex-archive/language/hyph-utf8/tex/generic/hyph-utf8/patterns/
  25628. $data = file_get_contents($file);
  25629. $patterns = array();
  25630. // remove comments
  25631. $data = preg_replace('/\%[^\n]*/', '', $data);
  25632. // extract the patterns part
  25633. preg_match('/\\\\patterns\{([^\}]*)\}/i', $data, $matches);
  25634. $data = trim(substr($matches[0], 10, -1));
  25635. // extract each pattern
  25636. $patterns_array = preg_split('/[\s]+/', $data);
  25637. // create new language array of patterns
  25638. $patterns = array();
  25639. foreach($patterns_array as $val) {
  25640. if (!$this->empty_string($val)) {
  25641. $val = trim($val);
  25642. $val = str_replace('\'', '\\\'', $val);
  25643. $key = preg_replace('/[0-9]+/', '', $val);
  25644. $patterns[$key] = $val;
  25645. }
  25646. }
  25647. return $patterns;
  25648. }
  25649. /**
  25650. * Returns text with soft hyphens.
  25651. * @param $text (string) text to process
  25652. * @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/
  25653. * @param $dictionary (array) Array of words to be returned without applying the hyphenation algoritm.
  25654. * @param $leftmin (int) Minimum number of character to leave on the left of the word without applying the hyphens.
  25655. * @param $rightmin (int) Minimum number of character to leave on the right of the word without applying the hyphens.
  25656. * @param $charmin (int) Minimum word length to apply the hyphenation algoritm.
  25657. * @param $charmax (int) Maximum length of broken piece of word.
  25658. * @return array text with soft hyphens
  25659. * @author Nicola Asuni
  25660. * @since 4.9.012 (2010-04-12)
  25661. * @public
  25662. */
  25663. public function hyphenateText($text, $patterns, $dictionary=array(), $leftmin=1, $rightmin=2, $charmin=1, $charmax=8) {
  25664. $text = $this->unhtmlentities($text);
  25665. $word = array(); // last word
  25666. $txtarr = array(); // text to be returned
  25667. $intag = false; // true if we are inside an HTML tag
  25668. if (!is_array($patterns)) {
  25669. $patterns = $this->getHyphenPatternsFromTEX($patterns);
  25670. }
  25671. // get array of characters
  25672. $unichars = $this->UTF8StringToArray($text);
  25673. // for each char
  25674. foreach ($unichars as $char) {
  25675. if ((!$intag) AND $this->unicode->uni_type[$char] == 'L') {
  25676. // letter character
  25677. $word[] = $char;
  25678. } else {
  25679. // other type of character
  25680. if (!$this->empty_string($word)) {
  25681. // hypenate the word
  25682. $txtarr = array_merge($txtarr, $this->hyphenateWord($word, $patterns, $dictionary, $leftmin, $rightmin, $charmin, $charmax));
  25683. $word = array();
  25684. }
  25685. $txtarr[] = $char;
  25686. if (chr($char) == '<') {
  25687. // we are inside an HTML tag
  25688. $intag = true;
  25689. } elseif ($intag AND (chr($char) == '>')) {
  25690. // end of HTML tag
  25691. $intag = false;
  25692. }
  25693. }
  25694. }
  25695. if (!$this->empty_string($word)) {
  25696. // hypenate the word
  25697. $txtarr = array_merge($txtarr, $this->hyphenateWord($word, $patterns, $dictionary, $leftmin, $rightmin, $charmin, $charmax));
  25698. }
  25699. // convert char array to string and return
  25700. return $this->UTF8ArrSubString($txtarr);
  25701. }
  25702. /**
  25703. * Enable/disable rasterization of vector images using ImageMagick library.
  25704. * @param $mode (boolean) if true enable rasterization, false otherwise.
  25705. * @public
  25706. * @since 5.0.000 (2010-04-27)
  25707. */
  25708. public function setRasterizeVectorImages($mode) {
  25709. $this->rasterize_vector_images = $mode;
  25710. }
  25711. /**
  25712. * Get the Path-Painting Operators.
  25713. * @param $style (string) Style of rendering. Possible values are:
  25714. * <ul>
  25715. * <li>S or D: Stroke the path.</li>
  25716. * <li>s or d: Close and stroke the path.</li>
  25717. * <li>f or F: Fill the path, using the nonzero winding number rule to determine the region to fill.</li>
  25718. * <li>f* or F*: Fill the path, using the even-odd rule to determine the region to fill.</li>
  25719. * <li>B or FD or DF: Fill and then stroke the path, using the nonzero winding number rule to determine the region to fill.</li>
  25720. * <li>B* or F*D or DF*: Fill and then stroke the path, using the even-odd rule to determine the region to fill.</li>
  25721. * <li>b or fd or df: Close, fill, and then stroke the path, using the nonzero winding number rule to determine the region to fill.</li>
  25722. * <li>b or f*d or df*: Close, fill, and then stroke the path, using the even-odd rule to determine the region to fill.</li>
  25723. * <li>CNZ: Clipping mode using the even-odd rule to determine which regions lie inside the clipping path.</li>
  25724. * <li>CEO: Clipping mode using the nonzero winding number rule to determine which regions lie inside the clipping path</li>
  25725. * <li>n: End the path object without filling or stroking it.</li>
  25726. * </ul>
  25727. * @param $default (string) default style
  25728. * @author Nicola Asuni
  25729. * @since 5.0.000 (2010-04-30)
  25730. * @protected
  25731. */
  25732. protected function getPathPaintOperator($style, $default='S') {
  25733. $op = '';
  25734. switch($style) {
  25735. case 'S':
  25736. case 'D': {
  25737. $op = 'S';
  25738. break;
  25739. }
  25740. case 's':
  25741. case 'd': {
  25742. $op = 's';
  25743. break;
  25744. }
  25745. case 'f':
  25746. case 'F': {
  25747. $op = 'f';
  25748. break;
  25749. }
  25750. case 'f*':
  25751. case 'F*': {
  25752. $op = 'f*';
  25753. break;
  25754. }
  25755. case 'B':
  25756. case 'FD':
  25757. case 'DF': {
  25758. $op = 'B';
  25759. break;
  25760. }
  25761. case 'B*':
  25762. case 'F*D':
  25763. case 'DF*': {
  25764. $op = 'B*';
  25765. break;
  25766. }
  25767. case 'b':
  25768. case 'fd':
  25769. case 'df': {
  25770. $op = 'b';
  25771. break;
  25772. }
  25773. case 'b*':
  25774. case 'f*d':
  25775. case 'df*': {
  25776. $op = 'b*';
  25777. break;
  25778. }
  25779. case 'CNZ': {
  25780. $op = 'W n';
  25781. break;
  25782. }
  25783. case 'CEO': {
  25784. $op = 'W* n';
  25785. break;
  25786. }
  25787. case 'n': {
  25788. $op = 'n';
  25789. break;
  25790. }
  25791. default: {
  25792. if (!empty($default)) {
  25793. $op = $this->getPathPaintOperator($default, '');
  25794. } else {
  25795. $op = '';
  25796. }
  25797. }
  25798. }
  25799. return $op;
  25800. }
  25801. /**
  25802. * Enable or disable default option for font subsetting.
  25803. * @param $enable (boolean) if true enable font subsetting by default.
  25804. * @author Nicola Asuni
  25805. * @public
  25806. * @since 5.3.002 (2010-06-07)
  25807. */
  25808. public function setFontSubsetting($enable=true) {
  25809. if ($this->pdfa_mode) {
  25810. $this->font_subsetting = false;
  25811. } else {
  25812. $this->font_subsetting = $enable ? true : false;
  25813. }
  25814. }
  25815. /**
  25816. * Return the default option for font subsetting.
  25817. * @return boolean default font subsetting state.
  25818. * @author Nicola Asuni
  25819. * @public
  25820. * @since 5.3.002 (2010-06-07)
  25821. */
  25822. public function getFontSubsetting() {
  25823. return $this->font_subsetting;
  25824. }
  25825. /**
  25826. * Left trim the input string
  25827. * @param $str (string) string to trim
  25828. * @param $replace (string) string that replace spaces.
  25829. * @return left trimmed string
  25830. * @author Nicola Asuni
  25831. * @public
  25832. * @since 5.8.000 (2010-08-11)
  25833. */
  25834. public function stringLeftTrim($str, $replace='') {
  25835. return preg_replace('/^'.$this->re_space['p'].'+/'.$this->re_space['m'], $replace, $str);
  25836. }
  25837. /**
  25838. * Right trim the input string
  25839. * @param $str (string) string to trim
  25840. * @param $replace (string) string that replace spaces.
  25841. * @return right trimmed string
  25842. * @author Nicola Asuni
  25843. * @public
  25844. * @since 5.8.000 (2010-08-11)
  25845. */
  25846. public function stringRightTrim($str, $replace='') {
  25847. return preg_replace('/'.$this->re_space['p'].'+$/'.$this->re_space['m'], $replace, $str);
  25848. }
  25849. /**
  25850. * Trim the input string
  25851. * @param $str (string) string to trim
  25852. * @param $replace (string) string that replace spaces.
  25853. * @return trimmed string
  25854. * @author Nicola Asuni
  25855. * @public
  25856. * @since 5.8.000 (2010-08-11)
  25857. */
  25858. public function stringTrim($str, $replace='') {
  25859. $str = $this->stringLeftTrim($str, $replace);
  25860. $str = $this->stringRightTrim($str, $replace);
  25861. return $str;
  25862. }
  25863. /**
  25864. * Return true if the current font is unicode type.
  25865. * @return true for unicode font, false otherwise.
  25866. * @author Nicola Asuni
  25867. * @public
  25868. * @since 5.8.002 (2010-08-14)
  25869. */
  25870. public function isUnicodeFont() {
  25871. return (($this->CurrentFont['type'] == 'TrueTypeUnicode') OR ($this->CurrentFont['type'] == 'cidfont0'));
  25872. }
  25873. /**
  25874. * Return normalized font name
  25875. * @param $fontfamily (string) property string containing font family names
  25876. * @return string normalized font name
  25877. * @author Nicola Asuni
  25878. * @public
  25879. * @since 5.8.004 (2010-08-17)
  25880. */
  25881. public function getFontFamilyName($fontfamily) {
  25882. // remove spaces and symbols
  25883. $fontfamily = preg_replace('/[^a-z0-9\,]/', '', strtolower($fontfamily));
  25884. // extract all font names
  25885. $fontslist = preg_split('/[,]/', $fontfamily);
  25886. // find first valid font name
  25887. foreach ($fontslist as $font) {
  25888. // replace font variations
  25889. $font = preg_replace('/italic$/', 'I', $font);
  25890. $font = preg_replace('/oblique$/', 'I', $font);
  25891. $font = preg_replace('/bold([I]?)$/', 'B\\1', $font);
  25892. // replace common family names and core fonts
  25893. $pattern = array();
  25894. $replacement = array();
  25895. $pattern[] = '/^serif|^cursive|^fantasy|^timesnewroman/';
  25896. $replacement[] = 'times';
  25897. $pattern[] = '/^sansserif/';
  25898. $replacement[] = 'helvetica';
  25899. $pattern[] = '/^monospace/';
  25900. $replacement[] = 'courier';
  25901. $font = preg_replace($pattern, $replacement, $font);
  25902. if (in_array(strtolower($font), $this->fontlist) OR in_array($font, $this->fontkeys)) {
  25903. return $font;
  25904. }
  25905. }
  25906. // return current font as default
  25907. return $this->CurrentFont['fontkey'];
  25908. }
  25909. /**
  25910. * Start a new XObject Template.
  25911. * 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).
  25912. * 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.
  25913. * Note: X,Y coordinates will be reset to 0,0.
  25914. * @param $w (int) Template width in user units (empty string or zero = page width less margins).
  25915. * @param $h (int) Template height in user units (empty string or zero = page height less margins).
  25916. * @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).
  25917. * @return int the XObject Template ID in case of success or false in case of error.
  25918. * @author Nicola Asuni
  25919. * @public
  25920. * @since 5.8.017 (2010-08-24)
  25921. * @see endTemplate(), printTemplate()
  25922. */
  25923. public function startTemplate($w=0, $h=0, $group=false) {
  25924. if ($this->inxobj) {
  25925. // we are already inside an XObject template
  25926. return false;
  25927. }
  25928. $this->inxobj = true;
  25929. ++$this->n;
  25930. // XObject ID
  25931. $this->xobjid = 'XT'.$this->n;
  25932. // object ID
  25933. $this->xobjects[$this->xobjid] = array('n' => $this->n);
  25934. // store current graphic state
  25935. $this->xobjects[$this->xobjid]['gvars'] = $this->getGraphicVars();
  25936. // initialize data
  25937. $this->xobjects[$this->xobjid]['intmrk'] = 0;
  25938. $this->xobjects[$this->xobjid]['transfmrk'] = array();
  25939. $this->xobjects[$this->xobjid]['outdata'] = '';
  25940. $this->xobjects[$this->xobjid]['xobjects'] = array();
  25941. $this->xobjects[$this->xobjid]['images'] = array();
  25942. $this->xobjects[$this->xobjid]['fonts'] = array();
  25943. $this->xobjects[$this->xobjid]['annotations'] = array();
  25944. $this->xobjects[$this->xobjid]['extgstates'] = array();
  25945. $this->xobjects[$this->xobjid]['gradients'] = array();
  25946. $this->xobjects[$this->xobjid]['spot_colors'] = array();
  25947. // set new environment
  25948. $this->num_columns = 1;
  25949. $this->current_column = 0;
  25950. $this->SetAutoPageBreak(false);
  25951. if (($w === '') OR ($w <= 0)) {
  25952. $w = $this->w - $this->lMargin - $this->rMargin;
  25953. }
  25954. if (($h === '') OR ($h <= 0)) {
  25955. $h = $this->h - $this->tMargin - $this->bMargin;
  25956. }
  25957. $this->xobjects[$this->xobjid]['x'] = 0;
  25958. $this->xobjects[$this->xobjid]['y'] = 0;
  25959. $this->xobjects[$this->xobjid]['w'] = $w;
  25960. $this->xobjects[$this->xobjid]['h'] = $h;
  25961. $this->w = $w;
  25962. $this->h = $h;
  25963. $this->wPt = $this->w * $this->k;
  25964. $this->hPt = $this->h * $this->k;
  25965. $this->fwPt = $this->wPt;
  25966. $this->fhPt = $this->hPt;
  25967. $this->x = 0;
  25968. $this->y = 0;
  25969. $this->lMargin = 0;
  25970. $this->rMargin = 0;
  25971. $this->tMargin = 0;
  25972. $this->bMargin = 0;
  25973. // set group mode
  25974. $this->xobjects[$this->xobjid]['group'] = $group;
  25975. return $this->xobjid;
  25976. }
  25977. /**
  25978. * End the current XObject Template started with startTemplate() and restore the previous graphic state.
  25979. * 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).
  25980. * 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.
  25981. * @return int the XObject Template ID in case of success or false in case of error.
  25982. * @author Nicola Asuni
  25983. * @public
  25984. * @since 5.8.017 (2010-08-24)
  25985. * @see startTemplate(), printTemplate()
  25986. */
  25987. public function endTemplate() {
  25988. if (!$this->inxobj) {
  25989. // we are not inside a template
  25990. return false;
  25991. }
  25992. $this->inxobj = false;
  25993. // restore previous graphic state
  25994. $this->setGraphicVars($this->xobjects[$this->xobjid]['gvars'], true);
  25995. return $this->xobjid;
  25996. }
  25997. /**
  25998. * Print an XObject Template.
  25999. * You can print an XObject Template inside the currently opened Template.
  26000. * 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).
  26001. * 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.
  26002. * @param $id (string) The ID of XObject Template to print.
  26003. * @param $x (int) X position in user units (empty string = current x position)
  26004. * @param $y (int) Y position in user units (empty string = current y position)
  26005. * @param $w (int) Width in user units (zero = remaining page width)
  26006. * @param $h (int) Height in user units (zero = remaining page height)
  26007. * @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>
  26008. * @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>
  26009. * @param $fitonpage (boolean) If true the template is resized to not exceed page dimensions.
  26010. * @author Nicola Asuni
  26011. * @public
  26012. * @since 5.8.017 (2010-08-24)
  26013. * @see startTemplate(), endTemplate()
  26014. */
  26015. public function printTemplate($id, $x='', $y='', $w=0, $h=0, $align='', $palign='', $fitonpage=false) {
  26016. if (!isset($this->xobjects[$id])) {
  26017. $this->Error('The XObject Template \''.$id.'\' doesn\'t exist!');
  26018. }
  26019. if ($this->inxobj) {
  26020. if ($id == $this->xobjid) {
  26021. // close current template
  26022. $this->endTemplate();
  26023. } else {
  26024. // use the template as resource for the template currently opened
  26025. $this->xobjects[$this->xobjid]['xobjects'][$id] = $this->xobjects[$id];
  26026. }
  26027. }
  26028. // set default values
  26029. if ($x === '') {
  26030. $x = $this->x;
  26031. }
  26032. if ($y === '') {
  26033. $y = $this->y;
  26034. }
  26035. // check page for no-write regions and adapt page margins if necessary
  26036. list($x, $y) = $this->checkPageRegions($h, $x, $y);
  26037. $ow = $this->xobjects[$id]['w'];
  26038. $oh = $this->xobjects[$id]['h'];
  26039. // calculate template width and height on document
  26040. if (($w <= 0) AND ($h <= 0)) {
  26041. $w = $ow;
  26042. $h = $oh;
  26043. } elseif ($w <= 0) {
  26044. $w = $h * $ow / $oh;
  26045. } elseif ($h <= 0) {
  26046. $h = $w * $oh / $ow;
  26047. }
  26048. // fit the template on available space
  26049. list($w, $h, $x, $y) = $this->fitBlock($w, $h, $x, $y, $fitonpage);
  26050. // set page alignment
  26051. $rb_y = $y + $h;
  26052. // set alignment
  26053. if ($this->rtl) {
  26054. if ($palign == 'L') {
  26055. $xt = $this->lMargin;
  26056. } elseif ($palign == 'C') {
  26057. $xt = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
  26058. } elseif ($palign == 'R') {
  26059. $xt = $this->w - $this->rMargin - $w;
  26060. } else {
  26061. $xt = $x - $w;
  26062. }
  26063. $rb_x = $xt;
  26064. } else {
  26065. if ($palign == 'L') {
  26066. $xt = $this->lMargin;
  26067. } elseif ($palign == 'C') {
  26068. $xt = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
  26069. } elseif ($palign == 'R') {
  26070. $xt = $this->w - $this->rMargin - $w;
  26071. } else {
  26072. $xt = $x;
  26073. }
  26074. $rb_x = $xt + $w;
  26075. }
  26076. // print XObject Template + Transformation matrix
  26077. $this->StartTransform();
  26078. // translate and scale
  26079. $sx = ($w / $this->xobjects[$id]['w']);
  26080. $sy = ($h / $this->xobjects[$id]['h']);
  26081. $tm = array();
  26082. $tm[0] = $sx;
  26083. $tm[1] = 0;
  26084. $tm[2] = 0;
  26085. $tm[3] = $sy;
  26086. $tm[4] = $xt * $this->k;
  26087. $tm[5] = ($this->h - $h - $y) * $this->k;
  26088. $this->Transform($tm);
  26089. // set object
  26090. $this->_out('/'.$id.' Do');
  26091. $this->StopTransform();
  26092. // add annotations
  26093. if (!empty($this->xobjects[$id]['annotations'])) {
  26094. foreach ($this->xobjects[$id]['annotations'] as $annot) {
  26095. // transform original coordinates
  26096. $coordlt = $this->getTransformationMatrixProduct($tm, array(1, 0, 0, 1, ($annot['x'] * $this->k), (-$annot['y'] * $this->k)));
  26097. $ax = ($coordlt[4] / $this->k);
  26098. $ay = ($this->h - $h - ($coordlt[5] / $this->k));
  26099. $coordrb = $this->getTransformationMatrixProduct($tm, array(1, 0, 0, 1, (($annot['x'] + $annot['w']) * $this->k), ((-$annot['y'] - $annot['h']) * $this->k)));
  26100. $aw = ($coordrb[4] / $this->k) - $ax;
  26101. $ah = ($this->h - $h - ($coordrb[5] / $this->k)) - $ay;
  26102. $this->Annotation($ax, $ay, $aw, $ah, $annot['text'], $annot['opt'], $annot['spaces']);
  26103. }
  26104. }
  26105. // set pointer to align the next text/objects
  26106. switch($align) {
  26107. case 'T': {
  26108. $this->y = $y;
  26109. $this->x = $rb_x;
  26110. break;
  26111. }
  26112. case 'M': {
  26113. $this->y = $y + round($h/2);
  26114. $this->x = $rb_x;
  26115. break;
  26116. }
  26117. case 'B': {
  26118. $this->y = $rb_y;
  26119. $this->x = $rb_x;
  26120. break;
  26121. }
  26122. case 'N': {
  26123. $this->SetY($rb_y);
  26124. break;
  26125. }
  26126. default:{
  26127. break;
  26128. }
  26129. }
  26130. }
  26131. /**
  26132. * Set the percentage of character stretching.
  26133. * @param $perc (int) percentage of stretching (100 = no stretching)
  26134. * @author Nicola Asuni
  26135. * @public
  26136. * @since 5.9.000 (2010-09-29)
  26137. */
  26138. public function setFontStretching($perc=100) {
  26139. $this->font_stretching = $perc;
  26140. }
  26141. /**
  26142. * Get the percentage of character stretching.
  26143. * @return float stretching value
  26144. * @author Nicola Asuni
  26145. * @public
  26146. * @since 5.9.000 (2010-09-29)
  26147. */
  26148. public function getFontStretching() {
  26149. return $this->font_stretching;
  26150. }
  26151. /**
  26152. * Set the amount to increase or decrease the space between characters in a text.
  26153. * @param $spacing (float) amount to increase or decrease the space between characters in a text (0 = default spacing)
  26154. * @author Nicola Asuni
  26155. * @public
  26156. * @since 5.9.000 (2010-09-29)
  26157. */
  26158. public function setFontSpacing($spacing=0) {
  26159. $this->font_spacing = $spacing;
  26160. }
  26161. /**
  26162. * Get the amount to increase or decrease the space between characters in a text.
  26163. * @return int font spacing (tracking/kerning) value
  26164. * @author Nicola Asuni
  26165. * @public
  26166. * @since 5.9.000 (2010-09-29)
  26167. */
  26168. public function getFontSpacing() {
  26169. return $this->font_spacing;
  26170. }
  26171. /**
  26172. * Return an array of no-write page regions
  26173. * @return array of no-write page regions
  26174. * @author Nicola Asuni
  26175. * @public
  26176. * @since 5.9.003 (2010-10-13)
  26177. * @see setPageRegions(), addPageRegion()
  26178. */
  26179. public function getPageRegions() {
  26180. return $this->page_regions;
  26181. }
  26182. /**
  26183. * Set no-write regions on page.
  26184. * 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.
  26185. * A region is always aligned on the left or right side of the page ad is defined using a vertical segment.
  26186. * You can set multiple regions for the same page.
  26187. * @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.
  26188. * @author Nicola Asuni
  26189. * @public
  26190. * @since 5.9.003 (2010-10-13)
  26191. * @see addPageRegion(), getPageRegions()
  26192. */
  26193. public function setPageRegions($regions=array()) {
  26194. // empty current regions array
  26195. $this->page_regions = array();
  26196. // add regions
  26197. foreach ($regions as $data) {
  26198. $this->addPageRegion($data);
  26199. }
  26200. }
  26201. /**
  26202. * Add a single no-write region on selected page.
  26203. * 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.
  26204. * A region is always aligned on the left or right side of the page ad is defined using a vertical segment.
  26205. * You can set multiple regions for the same page.
  26206. * @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).
  26207. * @author Nicola Asuni
  26208. * @public
  26209. * @since 5.9.003 (2010-10-13)
  26210. * @see setPageRegions(), getPageRegions()
  26211. */
  26212. public function addPageRegion($region) {
  26213. if (!isset($region['page']) OR empty($region['page'])) {
  26214. $region['page'] = $this->page;
  26215. }
  26216. if (isset($region['xt']) AND isset($region['xb']) AND ($region['xt'] > 0) AND ($region['xb'] > 0)
  26217. AND isset($region['yt']) AND isset($region['yb']) AND ($region['yt'] >= 0) AND ($region['yt'] < $region['yb'])
  26218. AND isset($region['side']) AND (($region['side'] == 'L') OR ($region['side'] == 'R'))) {
  26219. $this->page_regions[] = $region;
  26220. }
  26221. }
  26222. /**
  26223. * Remove a single no-write region.
  26224. * @param $key (int) region key
  26225. * @author Nicola Asuni
  26226. * @public
  26227. * @since 5.9.003 (2010-10-13)
  26228. * @see setPageRegions(), getPageRegions()
  26229. */
  26230. public function removePageRegion($key) {
  26231. if (isset($this->page_regions[$key])) {
  26232. unset($this->page_regions[$key]);
  26233. }
  26234. }
  26235. /**
  26236. * Check page for no-write regions and adapt current coordinates and page margins if necessary.
  26237. * 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.
  26238. * A region is always aligned on the left or right side of the page ad is defined using a vertical segment.
  26239. * @param $h (float) height of the text/image/object to print in user units
  26240. * @param $x (float) current X coordinate in user units
  26241. * @param $y (float) current Y coordinate in user units
  26242. * @return array($x, $y)
  26243. * @author Nicola Asuni
  26244. * @protected
  26245. * @since 5.9.003 (2010-10-13)
  26246. */
  26247. protected function checkPageRegions($h, $x, $y) {
  26248. // set default values
  26249. if ($x === '') {
  26250. $x = $this->x;
  26251. }
  26252. if ($y === '') {
  26253. $y = $this->y;
  26254. }
  26255. if (empty($this->page_regions)) {
  26256. // no page regions defined
  26257. return array($x, $y);
  26258. }
  26259. if (empty($h)) {
  26260. $h = ($this->FontSize * $this->cell_height_ratio) + $this->cell_padding['T'] + $this->cell_padding['B'];
  26261. }
  26262. // check for page break
  26263. if ($this->checkPageBreak($h, $y)) {
  26264. // the content will be printed on a new page
  26265. $x = $this->x;
  26266. $y = $this->y;
  26267. }
  26268. if ($this->num_columns > 1) {
  26269. if ($this->rtl) {
  26270. $this->lMargin = $this->columns[$this->current_column]['x'] - $this->columns[$this->current_column]['w'];
  26271. } else {
  26272. $this->rMargin = $this->w - $this->columns[$this->current_column]['x'] - $this->columns[$this->current_column]['w'];
  26273. }
  26274. } else {
  26275. if ($this->rtl) {
  26276. $this->lMargin = $this->original_lMargin;
  26277. } else {
  26278. $this->rMargin = $this->original_rMargin;
  26279. }
  26280. }
  26281. // adjust coordinates and page margins
  26282. foreach ($this->page_regions as $regid => $regdata) {
  26283. if ($regdata['page'] == $this->page) {
  26284. // check region boundaries
  26285. if (($y > ($regdata['yt'] - $h)) AND ($y <= $regdata['yb'])) {
  26286. // Y is inside the region
  26287. $minv = ($regdata['xb'] - $regdata['xt']) / ($regdata['yb'] - $regdata['yt']); // inverse of angular coefficient
  26288. $yt = max($y, $regdata['yt']);
  26289. $yb = min(($yt + $h), $regdata['yb']);
  26290. $xt = (($yt - $regdata['yt']) * $minv) + $regdata['xt'];
  26291. $xb = (($yb - $regdata['yt']) * $minv) + $regdata['xt'];
  26292. if ($regdata['side'] == 'L') { // left side
  26293. $new_margin = max($xt, $xb);
  26294. if ($this->lMargin < $new_margin) {
  26295. if ($this->rtl) {
  26296. // adjust left page margin
  26297. $this->lMargin = $new_margin;
  26298. }
  26299. if ($x < $new_margin) {
  26300. // adjust x position
  26301. $x = $new_margin;
  26302. if ($new_margin > ($this->w - $this->rMargin)) {
  26303. // adjust y position
  26304. $y = $regdata['yb'] - $h;
  26305. }
  26306. }
  26307. }
  26308. } elseif ($regdata['side'] == 'R') { // right side
  26309. $new_margin = min($xt, $xb);
  26310. if (($this->w - $this->rMargin) > $new_margin) {
  26311. if (!$this->rtl) {
  26312. // adjust right page margin
  26313. $this->rMargin = ($this->w - $new_margin);
  26314. }
  26315. if ($x > $new_margin) {
  26316. // adjust x position
  26317. $x = $new_margin;
  26318. if ($new_margin > $this->lMargin) {
  26319. // adjust y position
  26320. $y = $regdata['yb'] - $h;
  26321. }
  26322. }
  26323. }
  26324. }
  26325. }
  26326. }
  26327. }
  26328. return array($x, $y);
  26329. }
  26330. // --- SVG METHODS ---------------------------------------------------------
  26331. /**
  26332. * Embedd a Scalable Vector Graphics (SVG) image.
  26333. * NOTE: SVG standard is not yet fully implemented, use the setRasterizeVectorImages() method to enable/disable rasterization of vector images using ImageMagick library.
  26334. * @param $file (string) Name of the SVG file or a '@' character followed by the SVG data string.
  26335. * @param $x (float) Abscissa of the upper-left corner.
  26336. * @param $y (float) Ordinate of the upper-left corner.
  26337. * @param $w (float) Width of the image in the page. If not specified or equal to zero, it is automatically calculated.
  26338. * @param $h (float) Height of the image in the page. If not specified or equal to zero, it is automatically calculated.
  26339. * @param $link (mixed) URL or identifier returned by AddLink().
  26340. * @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.
  26341. * @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>
  26342. * @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)))
  26343. * @param $fitonpage (boolean) if true the image is resized to not exceed page dimensions.
  26344. * @author Nicola Asuni
  26345. * @since 5.0.000 (2010-05-02)
  26346. * @public
  26347. */
  26348. public function ImageSVG($file, $x='', $y='', $w=0, $h=0, $link='', $align='', $palign='', $border=0, $fitonpage=false) {
  26349. if ($this->rasterize_vector_images AND ($w > 0) AND ($h > 0)) {
  26350. // convert SVG to raster image using GD or ImageMagick libraries
  26351. return $this->Image($file, $x, $y, $w, $h, 'SVG', $link, $align, true, 300, $palign, false, false, $border, false, false, false);
  26352. }
  26353. if ($file{0} === '@') { // image from string
  26354. $this->svgdir = '';
  26355. $svgdata = substr($file, 1);
  26356. } else { // SVG file
  26357. $this->svgdir = dirname($file);
  26358. $svgdata = file_get_contents($file);
  26359. }
  26360. if ($svgdata === false) {
  26361. $this->Error('SVG file not found: '.$file);
  26362. }
  26363. if ($x === '') {
  26364. $x = $this->x;
  26365. }
  26366. if ($y === '') {
  26367. $y = $this->y;
  26368. }
  26369. // check page for no-write regions and adapt page margins if necessary
  26370. list($x, $y) = $this->checkPageRegions($h, $x, $y);
  26371. $k = $this->k;
  26372. $ox = 0;
  26373. $oy = 0;
  26374. $ow = $w;
  26375. $oh = $h;
  26376. $aspect_ratio_align = 'xMidYMid';
  26377. $aspect_ratio_ms = 'meet';
  26378. $regs = array();
  26379. // get original image width and height
  26380. preg_match('/<svg([^\>]*)>/si', $svgdata, $regs);
  26381. if (isset($regs[1]) AND !empty($regs[1])) {
  26382. $tmp = array();
  26383. if (preg_match('/[\s]+x[\s]*=[\s]*"([^"]*)"/si', $regs[1], $tmp)) {
  26384. $ox = $this->getHTMLUnitToUnits($tmp[1], 0, $this->svgunit, false);
  26385. }
  26386. $tmp = array();
  26387. if (preg_match('/[\s]+y[\s]*=[\s]*"([^"]*)"/si', $regs[1], $tmp)) {
  26388. $oy = $this->getHTMLUnitToUnits($tmp[1], 0, $this->svgunit, false);
  26389. }
  26390. $tmp = array();
  26391. if (preg_match('/[\s]+width[\s]*=[\s]*"([^"]*)"/si', $regs[1], $tmp)) {
  26392. $ow = $this->getHTMLUnitToUnits($tmp[1], 1, $this->svgunit, false);
  26393. }
  26394. $tmp = array();
  26395. if (preg_match('/[\s]+height[\s]*=[\s]*"([^"]*)"/si', $regs[1], $tmp)) {
  26396. $oh = $this->getHTMLUnitToUnits($tmp[1], 1, $this->svgunit, false);
  26397. }
  26398. $tmp = array();
  26399. $view_box = array();
  26400. if (preg_match('/[\s]+viewBox[\s]*=[\s]*"[\s]*([0-9\.\-]+)[\s]+([0-9\.\-]+)[\s]+([0-9\.]+)[\s]+([0-9\.]+)[\s]*"/si', $regs[1], $tmp)) {
  26401. if (count($tmp) == 5) {
  26402. array_shift($tmp);
  26403. foreach ($tmp as $key => $val) {
  26404. $view_box[$key] = $this->getHTMLUnitToUnits($val, 0, $this->svgunit, false);
  26405. }
  26406. $ox = $view_box[0];
  26407. $oy = $view_box[1];
  26408. }
  26409. // get aspect ratio
  26410. $tmp = array();
  26411. if (preg_match('/[\s]+preserveAspectRatio[\s]*=[\s]*"([^"]*)"/si', $regs[1], $tmp)) {
  26412. $aspect_ratio = preg_split('/[\s]+/si', $tmp[1]);
  26413. switch (count($aspect_ratio)) {
  26414. case 3: {
  26415. $aspect_ratio_align = $aspect_ratio[1];
  26416. $aspect_ratio_ms = $aspect_ratio[2];
  26417. break;
  26418. }
  26419. case 2: {
  26420. $aspect_ratio_align = $aspect_ratio[0];
  26421. $aspect_ratio_ms = $aspect_ratio[1];
  26422. break;
  26423. }
  26424. case 1: {
  26425. $aspect_ratio_align = $aspect_ratio[0];
  26426. $aspect_ratio_ms = 'meet';
  26427. break;
  26428. }
  26429. }
  26430. }
  26431. }
  26432. }
  26433. // calculate image width and height on document
  26434. if (($w <= 0) AND ($h <= 0)) {
  26435. // convert image size to document unit
  26436. $w = $ow;
  26437. $h = $oh;
  26438. } elseif ($w <= 0) {
  26439. $w = $h * $ow / $oh;
  26440. } elseif ($h <= 0) {
  26441. $h = $w * $oh / $ow;
  26442. }
  26443. // fit the image on available space
  26444. list($w, $h, $x, $y) = $this->fitBlock($w, $h, $x, $y, $fitonpage);
  26445. if ($this->rasterize_vector_images) {
  26446. // convert SVG to raster image using GD or ImageMagick libraries
  26447. return $this->Image($file, $x, $y, $w, $h, 'SVG', $link, $align, true, 300, $palign, false, false, $border, false, false, false);
  26448. }
  26449. // set alignment
  26450. $this->img_rb_y = $y + $h;
  26451. // set alignment
  26452. if ($this->rtl) {
  26453. if ($palign == 'L') {
  26454. $ximg = $this->lMargin;
  26455. } elseif ($palign == 'C') {
  26456. $ximg = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
  26457. } elseif ($palign == 'R') {
  26458. $ximg = $this->w - $this->rMargin - $w;
  26459. } else {
  26460. $ximg = $x - $w;
  26461. }
  26462. $this->img_rb_x = $ximg;
  26463. } else {
  26464. if ($palign == 'L') {
  26465. $ximg = $this->lMargin;
  26466. } elseif ($palign == 'C') {
  26467. $ximg = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
  26468. } elseif ($palign == 'R') {
  26469. $ximg = $this->w - $this->rMargin - $w;
  26470. } else {
  26471. $ximg = $x;
  26472. }
  26473. $this->img_rb_x = $ximg + $w;
  26474. }
  26475. // store current graphic vars
  26476. $gvars = $this->getGraphicVars();
  26477. // store SVG position and scale factors
  26478. $svgoffset_x = ($ximg - $ox) * $this->k;
  26479. $svgoffset_y = -($y - $oy) * $this->k;
  26480. if (isset($view_box[2]) AND ($view_box[2] > 0) AND ($view_box[3] > 0)) {
  26481. $ow = $view_box[2];
  26482. $oh = $view_box[3];
  26483. } else {
  26484. if ($ow <= 0) {
  26485. $ow = $w;
  26486. }
  26487. if ($oh <= 0) {
  26488. $oh = $h;
  26489. }
  26490. }
  26491. $svgscale_x = $w / $ow;
  26492. $svgscale_y = $h / $oh;
  26493. // scaling and alignment
  26494. if ($aspect_ratio_align != 'none') {
  26495. // store current scaling values
  26496. $svgscale_old_x = $svgscale_x;
  26497. $svgscale_old_y = $svgscale_y;
  26498. // force uniform scaling
  26499. if ($aspect_ratio_ms == 'slice') {
  26500. // the entire viewport is covered by the viewBox
  26501. if ($svgscale_x > $svgscale_y) {
  26502. $svgscale_y = $svgscale_x;
  26503. } elseif ($svgscale_x < $svgscale_y) {
  26504. $svgscale_x = $svgscale_y;
  26505. }
  26506. } else { // meet
  26507. // the entire viewBox is visible within the viewport
  26508. if ($svgscale_x < $svgscale_y) {
  26509. $svgscale_y = $svgscale_x;
  26510. } elseif ($svgscale_x > $svgscale_y) {
  26511. $svgscale_x = $svgscale_y;
  26512. }
  26513. }
  26514. // correct X alignment
  26515. switch (substr($aspect_ratio_align, 1, 3)) {
  26516. case 'Min': {
  26517. // do nothing
  26518. break;
  26519. }
  26520. case 'Max': {
  26521. $svgoffset_x += (($w * $this->k) - ($ow * $this->k * $svgscale_x));
  26522. break;
  26523. }
  26524. default:
  26525. case 'Mid': {
  26526. $svgoffset_x += ((($w * $this->k) - ($ow * $this->k * $svgscale_x)) / 2);
  26527. break;
  26528. }
  26529. }
  26530. // correct Y alignment
  26531. switch (substr($aspect_ratio_align, 5)) {
  26532. case 'Min': {
  26533. // do nothing
  26534. break;
  26535. }
  26536. case 'Max': {
  26537. $svgoffset_y -= (($h * $this->k) - ($oh * $this->k * $svgscale_y));
  26538. break;
  26539. }
  26540. default:
  26541. case 'Mid': {
  26542. $svgoffset_y -= ((($h * $this->k) - ($oh * $this->k * $svgscale_y)) / 2);
  26543. break;
  26544. }
  26545. }
  26546. }
  26547. // store current page break mode
  26548. $page_break_mode = $this->AutoPageBreak;
  26549. $page_break_margin = $this->getBreakMargin();
  26550. $cell_padding = $this->cell_padding;
  26551. $this->SetCellPadding(0);
  26552. $this->SetAutoPageBreak(false);
  26553. // save the current graphic state
  26554. $this->_out('q'.$this->epsmarker);
  26555. // set initial clipping mask
  26556. $this->Rect($x, $y, $w, $h, 'CNZ', array(), array());
  26557. // scale and translate
  26558. $e = $ox * $this->k * (1 - $svgscale_x);
  26559. $f = ($this->h - $oy) * $this->k * (1 - $svgscale_y);
  26560. $this->_out(sprintf('%.3F %.3F %.3F %.3F %.3F %.3F cm', $svgscale_x, 0, 0, $svgscale_y, $e + $svgoffset_x, $f + $svgoffset_y));
  26561. // creates a new XML parser to be used by the other XML functions
  26562. $this->parser = xml_parser_create('UTF-8');
  26563. // the following function allows to use parser inside object
  26564. xml_set_object($this->parser, $this);
  26565. // disable case-folding for this XML parser
  26566. xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, 0);
  26567. // sets the element handler functions for the XML parser
  26568. xml_set_element_handler($this->parser, 'startSVGElementHandler', 'endSVGElementHandler');
  26569. // sets the character data handler function for the XML parser
  26570. xml_set_character_data_handler($this->parser, 'segSVGContentHandler');
  26571. // start parsing an XML document
  26572. if (!xml_parse($this->parser, $svgdata)) {
  26573. $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));
  26574. $this->Error($error_message);
  26575. }
  26576. // free this XML parser
  26577. xml_parser_free($this->parser);
  26578. // restore previous graphic state
  26579. $this->_out($this->epsmarker.'Q');
  26580. // restore graphic vars
  26581. $this->setGraphicVars($gvars);
  26582. $this->lasth = $gvars['lasth'];
  26583. if (!empty($border)) {
  26584. $bx = $this->x;
  26585. $by = $this->y;
  26586. $this->x = $ximg;
  26587. if ($this->rtl) {
  26588. $this->x += $w;
  26589. }
  26590. $this->y = $y;
  26591. $this->Cell($w, $h, '', $border, 0, '', 0, '', 0, true);
  26592. $this->x = $bx;
  26593. $this->y = $by;
  26594. }
  26595. if ($link) {
  26596. $this->Link($ximg, $y, $w, $h, $link, 0);
  26597. }
  26598. // set pointer to align the next text/objects
  26599. switch($align) {
  26600. case 'T':{
  26601. $this->y = $y;
  26602. $this->x = $this->img_rb_x;
  26603. break;
  26604. }
  26605. case 'M':{
  26606. $this->y = $y + round($h/2);
  26607. $this->x = $this->img_rb_x;
  26608. break;
  26609. }
  26610. case 'B':{
  26611. $this->y = $this->img_rb_y;
  26612. $this->x = $this->img_rb_x;
  26613. break;
  26614. }
  26615. case 'N':{
  26616. $this->SetY($this->img_rb_y);
  26617. break;
  26618. }
  26619. default:{
  26620. // restore pointer to starting position
  26621. $this->x = $gvars['x'];
  26622. $this->y = $gvars['y'];
  26623. $this->page = $gvars['page'];
  26624. $this->current_column = $gvars['current_column'];
  26625. $this->tMargin = $gvars['tMargin'];
  26626. $this->bMargin = $gvars['bMargin'];
  26627. $this->w = $gvars['w'];
  26628. $this->h = $gvars['h'];
  26629. $this->wPt = $gvars['wPt'];
  26630. $this->hPt = $gvars['hPt'];
  26631. $this->fwPt = $gvars['fwPt'];
  26632. $this->fhPt = $gvars['fhPt'];
  26633. break;
  26634. }
  26635. }
  26636. $this->endlinex = $this->img_rb_x;
  26637. // restore page break
  26638. $this->SetAutoPageBreak($page_break_mode, $page_break_margin);
  26639. $this->cell_padding = $cell_padding;
  26640. }
  26641. /**
  26642. * Get the tranformation matrix from SVG transform attribute
  26643. * @param $attribute (string) transformation
  26644. * @return array of transformations
  26645. * @author Nicola Asuni
  26646. * @since 5.0.000 (2010-05-02)
  26647. * @protected
  26648. */
  26649. protected function getSVGTransformMatrix($attribute) {
  26650. // identity matrix
  26651. $tm = array(1, 0, 0, 1, 0, 0);
  26652. $transform = array();
  26653. if (preg_match_all('/(matrix|translate|scale|rotate|skewX|skewY)[\s]*\(([^\)]+)\)/si', $attribute, $transform, PREG_SET_ORDER) > 0) {
  26654. foreach ($transform as $key => $data) {
  26655. if (!empty($data[2])) {
  26656. $a = 1;
  26657. $b = 0;
  26658. $c = 0;
  26659. $d = 1;
  26660. $e = 0;
  26661. $f = 0;
  26662. $regs = array();
  26663. switch ($data[1]) {
  26664. case 'matrix': {
  26665. if (preg_match('/([a-z0-9\-\.]+)[\,\s]+([a-z0-9\-\.]+)[\,\s]+([a-z0-9\-\.]+)[\,\s]+([a-z0-9\-\.]+)[\,\s]+([a-z0-9\-\.]+)[\,\s]+([a-z0-9\-\.]+)/si', $data[2], $regs)) {
  26666. $a = $regs[1];
  26667. $b = $regs[2];
  26668. $c = $regs[3];
  26669. $d = $regs[4];
  26670. $e = $regs[5];
  26671. $f = $regs[6];
  26672. }
  26673. break;
  26674. }
  26675. case 'translate': {
  26676. if (preg_match('/([a-z0-9\-\.]+)[\,\s]+([a-z0-9\-\.]+)/si', $data[2], $regs)) {
  26677. $e = $regs[1];
  26678. $f = $regs[2];
  26679. } elseif (preg_match('/([a-z0-9\-\.]+)/si', $data[2], $regs)) {
  26680. $e = $regs[1];
  26681. }
  26682. break;
  26683. }
  26684. case 'scale': {
  26685. if (preg_match('/([a-z0-9\-\.]+)[\,\s]+([a-z0-9\-\.]+)/si', $data[2], $regs)) {
  26686. $a = $regs[1];
  26687. $d = $regs[2];
  26688. } elseif (preg_match('/([a-z0-9\-\.]+)/si', $data[2], $regs)) {
  26689. $a = $regs[1];
  26690. $d = $a;
  26691. }
  26692. break;
  26693. }
  26694. case 'rotate': {
  26695. if (preg_match('/([0-9\-\.]+)[\,\s]+([a-z0-9\-\.]+)[\,\s]+([a-z0-9\-\.]+)/si', $data[2], $regs)) {
  26696. $ang = deg2rad($regs[1]);
  26697. $x = $regs[2];
  26698. $y = $regs[3];
  26699. $a = cos($ang);
  26700. $b = sin($ang);
  26701. $c = -$b;
  26702. $d = $a;
  26703. $e = ($x * (1 - $a)) - ($y * $c);
  26704. $f = ($y * (1 - $d)) - ($x * $b);
  26705. } elseif (preg_match('/([0-9\-\.]+)/si', $data[2], $regs)) {
  26706. $ang = deg2rad($regs[1]);
  26707. $a = cos($ang);
  26708. $b = sin($ang);
  26709. $c = -$b;
  26710. $d = $a;
  26711. $e = 0;
  26712. $f = 0;
  26713. }
  26714. break;
  26715. }
  26716. case 'skewX': {
  26717. if (preg_match('/([0-9\-\.]+)/si', $data[2], $regs)) {
  26718. $c = tan(deg2rad($regs[1]));
  26719. }
  26720. break;
  26721. }
  26722. case 'skewY': {
  26723. if (preg_match('/([0-9\-\.]+)/si', $data[2], $regs)) {
  26724. $b = tan(deg2rad($regs[1]));
  26725. }
  26726. break;
  26727. }
  26728. }
  26729. $tm = $this->getTransformationMatrixProduct($tm, array($a, $b, $c, $d, $e, $f));
  26730. }
  26731. }
  26732. }
  26733. return $tm;
  26734. }
  26735. /**
  26736. * Get the product of two SVG tranformation matrices
  26737. * @param $ta (array) first SVG tranformation matrix
  26738. * @param $tb (array) second SVG tranformation matrix
  26739. * @return transformation array
  26740. * @author Nicola Asuni
  26741. * @since 5.0.000 (2010-05-02)
  26742. * @protected
  26743. */
  26744. protected function getTransformationMatrixProduct($ta, $tb) {
  26745. $tm = array();
  26746. $tm[0] = ($ta[0] * $tb[0]) + ($ta[2] * $tb[1]);
  26747. $tm[1] = ($ta[1] * $tb[0]) + ($ta[3] * $tb[1]);
  26748. $tm[2] = ($ta[0] * $tb[2]) + ($ta[2] * $tb[3]);
  26749. $tm[3] = ($ta[1] * $tb[2]) + ($ta[3] * $tb[3]);
  26750. $tm[4] = ($ta[0] * $tb[4]) + ($ta[2] * $tb[5]) + $ta[4];
  26751. $tm[5] = ($ta[1] * $tb[4]) + ($ta[3] * $tb[5]) + $ta[5];
  26752. return $tm;
  26753. }
  26754. /**
  26755. * Convert SVG transformation matrix to PDF.
  26756. * @param $tm (array) original SVG transformation matrix
  26757. * @return array transformation matrix
  26758. * @protected
  26759. * @since 5.0.000 (2010-05-02)
  26760. */
  26761. protected function convertSVGtMatrix($tm) {
  26762. $a = $tm[0];
  26763. $b = -$tm[1];
  26764. $c = -$tm[2];
  26765. $d = $tm[3];
  26766. $e = $this->getHTMLUnitToUnits($tm[4], 1, $this->svgunit, false) * $this->k;
  26767. $f = -$this->getHTMLUnitToUnits($tm[5], 1, $this->svgunit, false) * $this->k;
  26768. $x = 0;
  26769. $y = $this->h * $this->k;
  26770. $e = ($x * (1 - $a)) - ($y * $c) + $e;
  26771. $f = ($y * (1 - $d)) - ($x * $b) + $f;
  26772. return array($a, $b, $c, $d, $e, $f);
  26773. }
  26774. /**
  26775. * Apply SVG graphic transformation matrix.
  26776. * @param $tm (array) original SVG transformation matrix
  26777. * @protected
  26778. * @since 5.0.000 (2010-05-02)
  26779. */
  26780. protected function SVGTransform($tm) {
  26781. $this->Transform($this->convertSVGtMatrix($tm));
  26782. }
  26783. /**
  26784. * Apply the requested SVG styles (*** TO BE COMPLETED ***)
  26785. * @param $svgstyle (array) array of SVG styles to apply
  26786. * @param $prevsvgstyle (array) array of previous SVG style
  26787. * @param $x (int) X origin of the bounding box
  26788. * @param $y (int) Y origin of the bounding box
  26789. * @param $w (int) width of the bounding box
  26790. * @param $h (int) height of the bounding box
  26791. * @param $clip_function (string) clip function
  26792. * @param $clip_params (array) array of parameters for clipping function
  26793. * @return object style
  26794. * @author Nicola Asuni
  26795. * @since 5.0.000 (2010-05-02)
  26796. * @protected
  26797. */
  26798. protected function setSVGStyles($svgstyle, $prevsvgstyle, $x=0, $y=0, $w=1, $h=1, $clip_function='', $clip_params=array()) {
  26799. $objstyle = '';
  26800. $minlen = (0.01 / $this->k); // minimum acceptable length (3 point)
  26801. if (!isset($svgstyle['opacity'])) {
  26802. return $objstyle;
  26803. }
  26804. // clip-path
  26805. $regs = array();
  26806. if (preg_match('/url\([\s]*\#([^\)]*)\)/si', $svgstyle['clip-path'], $regs)) {
  26807. $clip_path = $this->svgclippaths[$regs[1]];
  26808. foreach ($clip_path as $cp) {
  26809. $this->startSVGElementHandler('clip-path', $cp['name'], $cp['attribs'], $cp['tm']);
  26810. }
  26811. }
  26812. // opacity
  26813. if ($svgstyle['opacity'] != 1) {
  26814. $this->SetAlpha($svgstyle['opacity']);
  26815. }
  26816. // color
  26817. $fill_color = $this->convertHTMLColorToDec($svgstyle['color']);
  26818. $this->SetFillColorArray($fill_color);
  26819. // text color
  26820. $text_color = $this->convertHTMLColorToDec($svgstyle['text-color']);
  26821. $this->SetTextColorArray($text_color);
  26822. // clip
  26823. if (preg_match('/rect\(([a-z0-9\-\.]*)[\s]*([a-z0-9\-\.]*)[\s]*([a-z0-9\-\.]*)[\s]*([a-z0-9\-\.]*)\)/si', $svgstyle['clip'], $regs)) {
  26824. $top = (isset($regs[1])?$this->getHTMLUnitToUnits($regs[1], 0, $this->svgunit, false):0);
  26825. $right = (isset($regs[2])?$this->getHTMLUnitToUnits($regs[2], 0, $this->svgunit, false):0);
  26826. $bottom = (isset($regs[3])?$this->getHTMLUnitToUnits($regs[3], 0, $this->svgunit, false):0);
  26827. $left = (isset($regs[4])?$this->getHTMLUnitToUnits($regs[4], 0, $this->svgunit, false):0);
  26828. $cx = $x + $left;
  26829. $cy = $y + $top;
  26830. $cw = $w - $left - $right;
  26831. $ch = $h - $top - $bottom;
  26832. if ($svgstyle['clip-rule'] == 'evenodd') {
  26833. $clip_rule = 'CNZ';
  26834. } else {
  26835. $clip_rule = 'CEO';
  26836. }
  26837. $this->Rect($cx, $cy, $cw, $ch, $clip_rule, array(), array());
  26838. }
  26839. // fill
  26840. $regs = array();
  26841. if (preg_match('/url\([\s]*\#([^\)]*)\)/si', $svgstyle['fill'], $regs)) {
  26842. // gradient
  26843. $gradient = $this->svggradients[$regs[1]];
  26844. if (isset($gradient['xref'])) {
  26845. // reference to another gradient definition
  26846. $newgradient = $this->svggradients[$gradient['xref']];
  26847. $newgradient['coords'] = $gradient['coords'];
  26848. $newgradient['mode'] = $gradient['mode'];
  26849. $newgradient['gradientUnits'] = $gradient['gradientUnits'];
  26850. if (isset($gradient['gradientTransform'])) {
  26851. $newgradient['gradientTransform'] = $gradient['gradientTransform'];
  26852. }
  26853. $gradient = $newgradient;
  26854. }
  26855. //save current Graphic State
  26856. $this->_out('q');
  26857. //set clipping area
  26858. if (!empty($clip_function) AND method_exists($this, $clip_function)) {
  26859. $bbox = call_user_func_array(array($this, $clip_function), $clip_params);
  26860. if (is_array($bbox) AND (count($bbox) == 4)) {
  26861. list($x, $y, $w, $h) = $bbox;
  26862. }
  26863. }
  26864. if ($gradient['mode'] == 'measure') {
  26865. if (isset($gradient['gradientTransform']) AND !empty($gradient['gradientTransform'])) {
  26866. $gtm = $gradient['gradientTransform'];
  26867. // apply transformation matrix
  26868. $xa = ($gtm[0] * $gradient['coords'][0]) + ($gtm[2] * $gradient['coords'][1]) + $gtm[4];
  26869. $ya = ($gtm[1] * $gradient['coords'][0]) + ($gtm[3] * $gradient['coords'][1]) + $gtm[5];
  26870. $xb = ($gtm[0] * $gradient['coords'][2]) + ($gtm[2] * $gradient['coords'][3]) + $gtm[4];
  26871. $yb = ($gtm[1] * $gradient['coords'][2]) + ($gtm[3] * $gradient['coords'][3]) + $gtm[5];
  26872. if (isset($gradient['coords'][4])) {
  26873. $gradient['coords'][4] = sqrt(pow(($gtm[0] * $gradient['coords'][4]), 2) + pow(($gtm[1] * $gradient['coords'][4]), 2));
  26874. }
  26875. $gradient['coords'][0] = $xa;
  26876. $gradient['coords'][1] = $ya;
  26877. $gradient['coords'][2] = $xb;
  26878. $gradient['coords'][3] = $yb;
  26879. }
  26880. // convert SVG coordinates to user units
  26881. $gradient['coords'][0] = $this->getHTMLUnitToUnits($gradient['coords'][0], 0, $this->svgunit, false);
  26882. $gradient['coords'][1] = $this->getHTMLUnitToUnits($gradient['coords'][1], 0, $this->svgunit, false);
  26883. $gradient['coords'][2] = $this->getHTMLUnitToUnits($gradient['coords'][2], 0, $this->svgunit, false);
  26884. $gradient['coords'][3] = $this->getHTMLUnitToUnits($gradient['coords'][3], 0, $this->svgunit, false);
  26885. if (isset($gradient['coords'][4])) {
  26886. $gradient['coords'][4] = $this->getHTMLUnitToUnits($gradient['coords'][4], 0, $this->svgunit, false);
  26887. }
  26888. // shift units
  26889. if ($gradient['gradientUnits'] == 'objectBoundingBox') {
  26890. // convert to SVG coordinate system
  26891. $gradient['coords'][0] += $x;
  26892. $gradient['coords'][1] += $y;
  26893. $gradient['coords'][2] += $x;
  26894. $gradient['coords'][3] += $y;
  26895. }
  26896. if ($w <= $minlen) {
  26897. $w = $minlen;
  26898. }
  26899. if ($h <= $minlen) {
  26900. $h = $minlen;
  26901. }
  26902. // calculate percentages
  26903. $gradient['coords'][0] = ($gradient['coords'][0] - $x) / $w;
  26904. $gradient['coords'][1] = ($gradient['coords'][1] - $y) / $h;
  26905. $gradient['coords'][2] = ($gradient['coords'][2] - $x) / $w;
  26906. $gradient['coords'][3] = ($gradient['coords'][3] - $y) / $h;
  26907. if (isset($gradient['coords'][4])) {
  26908. $gradient['coords'][4] /= $w;
  26909. }
  26910. } elseif ($gradient['mode'] == 'percentage') {
  26911. foreach($gradient['coords'] as $key => $val) {
  26912. $gradient['coords'][$key] = (intval($val) / 100);
  26913. }
  26914. }
  26915. // fix values
  26916. foreach($gradient['coords'] as $key => $val) {
  26917. if ($val < 0) {
  26918. $gradient['coords'][$key] = 0;
  26919. } elseif ($val > 1) {
  26920. $gradient['coords'][$key] = 1;
  26921. }
  26922. }
  26923. if (($gradient['type'] == 2) AND ($gradient['coords'][0] == $gradient['coords'][2]) AND ($gradient['coords'][1] == $gradient['coords'][3])) {
  26924. // single color (no shading)
  26925. $gradient['coords'][0] = 1;
  26926. $gradient['coords'][1] = 0;
  26927. $gradient['coords'][2] = 0.999;
  26928. $gradient['coords'][3] = 0;
  26929. }
  26930. // swap Y coordinates
  26931. $tmp = $gradient['coords'][1];
  26932. $gradient['coords'][1] = $gradient['coords'][3];
  26933. $gradient['coords'][3] = $tmp;
  26934. // set transformation map for gradient
  26935. if ($gradient['type'] == 3) {
  26936. // gradient is always circular
  26937. $cy = $this->h - $y - ($gradient['coords'][1] * ($w + $h));
  26938. $this->_out(sprintf('%.3F 0 0 %.3F %.3F %.3F cm', $w*$this->k, $w*$this->k, $x*$this->k, $cy*$this->k));
  26939. } else {
  26940. $this->_out(sprintf('%.3F 0 0 %.3F %.3F %.3F cm', $w*$this->k, $h*$this->k, $x*$this->k, ($this->h-($y+$h))*$this->k));
  26941. }
  26942. if (count($gradient['stops']) > 1) {
  26943. $this->Gradient($gradient['type'], $gradient['coords'], $gradient['stops'], array(), false);
  26944. }
  26945. } elseif ($svgstyle['fill'] != 'none') {
  26946. $fill_color = $this->convertHTMLColorToDec($svgstyle['fill']);
  26947. if ($svgstyle['fill-opacity'] != 1) {
  26948. $this->SetAlpha($svgstyle['fill-opacity']);
  26949. }
  26950. $this->SetFillColorArray($fill_color);
  26951. if ($svgstyle['fill-rule'] == 'evenodd') {
  26952. $objstyle .= 'F*';
  26953. } else {
  26954. $objstyle .= 'F';
  26955. }
  26956. }
  26957. // stroke
  26958. if ($svgstyle['stroke'] != 'none') {
  26959. $stroke_style = array(
  26960. 'color' => $this->convertHTMLColorToDec($svgstyle['stroke']),
  26961. 'width' => $this->getHTMLUnitToUnits($svgstyle['stroke-width'], 0, $this->svgunit, false),
  26962. 'cap' => $svgstyle['stroke-linecap'],
  26963. 'join' => $svgstyle['stroke-linejoin']
  26964. );
  26965. if (isset($svgstyle['stroke-dasharray']) AND !empty($svgstyle['stroke-dasharray']) AND ($svgstyle['stroke-dasharray'] != 'none')) {
  26966. $stroke_style['dash'] = $svgstyle['stroke-dasharray'];
  26967. }
  26968. $this->SetLineStyle($stroke_style);
  26969. $objstyle .= 'D';
  26970. }
  26971. // font
  26972. $regs = array();
  26973. if (!empty($svgstyle['font'])) {
  26974. if (preg_match('/font-family[\s]*:[\s]*([^\;\"]*)/si', $svgstyle['font'], $regs)) {
  26975. $font_family = $this->getFontFamilyName($regs[1]);
  26976. } else {
  26977. $font_family = $svgstyle['font-family'];
  26978. }
  26979. if (preg_match('/font-size[\s]*:[\s]*([^\s\;\"]*)/si', $svgstyle['font'], $regs)) {
  26980. $font_size = trim($regs[1]);
  26981. } else {
  26982. $font_size = $svgstyle['font-size'];
  26983. }
  26984. if (preg_match('/font-style[\s]*:[\s]*([^\s\;\"]*)/si', $svgstyle['font'], $regs)) {
  26985. $font_style = trim($regs[1]);
  26986. } else {
  26987. $font_style = $svgstyle['font-style'];
  26988. }
  26989. if (preg_match('/font-weight[\s]*:[\s]*([^\s\;\"]*)/si', $svgstyle['font'], $regs)) {
  26990. $font_weight = trim($regs[1]);
  26991. } else {
  26992. $font_weight = $svgstyle['font-weight'];
  26993. }
  26994. if (preg_match('/font-stretch[\s]*:[\s]*([^\s\;\"]*)/si', $svgstyle['font'], $regs)) {
  26995. $font_stretch = trim($regs[1]);
  26996. } else {
  26997. $font_stretch = $svgstyle['font-stretch'];
  26998. }
  26999. if (preg_match('/letter-spacing[\s]*:[\s]*([^\s\;\"]*)/si', $svgstyle['font'], $regs)) {
  27000. $font_spacing = trim($regs[1]);
  27001. } else {
  27002. $font_spacing = $svgstyle['letter-spacing'];
  27003. }
  27004. } else {
  27005. $font_family = $this->getFontFamilyName($svgstyle['font-family']);
  27006. $font_size = $svgstyle['font-size'];
  27007. $font_style = $svgstyle['font-style'];
  27008. $font_weight = $svgstyle['font-weight'];
  27009. $font_stretch = $svgstyle['font-stretch'];
  27010. $font_spacing = $svgstyle['letter-spacing'];
  27011. }
  27012. $font_size = $this->getHTMLUnitToUnits($font_size, $prevsvgstyle['font-size'], $this->svgunit, false) * $this->k;
  27013. $font_stretch = $this->getCSSFontStretching($font_stretch, $svgstyle['font-stretch']);
  27014. $font_spacing = $this->getCSSFontSpacing($font_spacing, $svgstyle['letter-spacing']);
  27015. switch ($font_style) {
  27016. case 'italic': {
  27017. $font_style = 'I';
  27018. break;
  27019. }
  27020. case 'oblique': {
  27021. $font_style = 'I';
  27022. break;
  27023. }
  27024. default:
  27025. case 'normal': {
  27026. $font_style = '';
  27027. break;
  27028. }
  27029. }
  27030. switch ($font_weight) {
  27031. case 'bold':
  27032. case 'bolder': {
  27033. $font_style .= 'B';
  27034. break;
  27035. }
  27036. }
  27037. switch ($svgstyle['text-decoration']) {
  27038. case 'underline': {
  27039. $font_style .= 'U';
  27040. break;
  27041. }
  27042. case 'overline': {
  27043. $font_style .= 'O';
  27044. break;
  27045. }
  27046. case 'line-through': {
  27047. $font_style .= 'D';
  27048. break;
  27049. }
  27050. default:
  27051. case 'none': {
  27052. break;
  27053. }
  27054. }
  27055. $this->SetFont($font_family, $font_style, $font_size);
  27056. $this->setFontStretching($font_stretch);
  27057. $this->setFontSpacing($font_spacing);
  27058. return $objstyle;
  27059. }
  27060. /**
  27061. * Draws an SVG path
  27062. * @param $d (string) attribute d of the path SVG element
  27063. * @param $style (string) Style of rendering. Possible values are:
  27064. * <ul>
  27065. * <li>D or empty string: Draw (default).</li>
  27066. * <li>F: Fill.</li>
  27067. * <li>F*: Fill using the even-odd rule to determine which regions lie inside the clipping path.</li>
  27068. * <li>DF or FD: Draw and fill.</li>
  27069. * <li>DF* or FD*: Draw and fill using the even-odd rule to determine which regions lie inside the clipping path.</li>
  27070. * <li>CNZ: Clipping mode (using the even-odd rule to determine which regions lie inside the clipping path).</li>
  27071. * <li>CEO: Clipping mode (using the nonzero winding number rule to determine which regions lie inside the clipping path).</li>
  27072. * </ul>
  27073. * @return array of container box measures (x, y, w, h)
  27074. * @author Nicola Asuni
  27075. * @since 5.0.000 (2010-05-02)
  27076. * @protected
  27077. */
  27078. protected function SVGPath($d, $style='') {
  27079. // set fill/stroke style
  27080. $op = $this->getPathPaintOperator($style, '');
  27081. if (empty($op)) {
  27082. return;
  27083. }
  27084. $paths = array();
  27085. $d = preg_replace('/([0-9ACHLMQSTVZ])([\-\+])/si', '\\1 \\2', $d);
  27086. preg_match_all('/([ACHLMQSTVZ])[\s]*([^ACHLMQSTVZ\"]*)/si', $d, $paths, PREG_SET_ORDER);
  27087. $x = 0;
  27088. $y = 0;
  27089. $x1 = 0;
  27090. $y1 = 0;
  27091. $x2 = 0;
  27092. $y2 = 0;
  27093. $xmin = 2147483647;
  27094. $xmax = 0;
  27095. $ymin = 2147483647;
  27096. $ymax = 0;
  27097. $relcoord = false;
  27098. $minlen = (0.01 / $this->k); // minimum acceptable length (3 point)
  27099. $firstcmd = true; // used to print first point
  27100. // draw curve pieces
  27101. foreach ($paths as $key => $val) {
  27102. // get curve type
  27103. $cmd = trim($val[1]);
  27104. if (strtolower($cmd) == $cmd) {
  27105. // use relative coordinated instead of absolute
  27106. $relcoord = true;
  27107. $xoffset = $x;
  27108. $yoffset = $y;
  27109. } else {
  27110. $relcoord = false;
  27111. $xoffset = 0;
  27112. $yoffset = 0;
  27113. }
  27114. $params = array();
  27115. if (isset($val[2])) {
  27116. // get curve parameters
  27117. $rawparams = preg_split('/([\,\s]+)/si', trim($val[2]));
  27118. $params = array();
  27119. foreach ($rawparams as $ck => $cp) {
  27120. $params[$ck] = $this->getHTMLUnitToUnits($cp, 0, $this->svgunit, false);
  27121. if (abs($params[$ck]) < $minlen) {
  27122. // aproximate little values to zero
  27123. $params[$ck] = 0;
  27124. }
  27125. }
  27126. }
  27127. // store current origin point
  27128. $x0 = $x;
  27129. $y0 = $y;
  27130. switch (strtoupper($cmd)) {
  27131. case 'M': { // moveto
  27132. foreach ($params as $ck => $cp) {
  27133. if (($ck % 2) == 0) {
  27134. $x = $cp + $xoffset;
  27135. } else {
  27136. $y = $cp + $yoffset;
  27137. if ($firstcmd OR (abs($x0 - $x) >= $minlen) OR (abs($y0 - $y) >= $minlen)) {
  27138. if ($ck == 1) {
  27139. $this->_outPoint($x, $y);
  27140. $firstcmd = false;
  27141. } else {
  27142. $this->_outLine($x, $y);
  27143. }
  27144. }
  27145. $xmin = min($xmin, $x);
  27146. $ymin = min($ymin, $y);
  27147. $xmax = max($xmax, $x);
  27148. $ymax = max($ymax, $y);
  27149. if ($relcoord) {
  27150. $xoffset = $x;
  27151. $yoffset = $y;
  27152. }
  27153. }
  27154. }
  27155. break;
  27156. }
  27157. case 'L': { // lineto
  27158. foreach ($params as $ck => $cp) {
  27159. if (($ck % 2) == 0) {
  27160. $x = $cp + $xoffset;
  27161. } else {
  27162. $y = $cp + $yoffset;
  27163. if ((abs($x0 - $x) >= $minlen) OR (abs($y0 - $y) >= $minlen)) {
  27164. $this->_outLine($x, $y);
  27165. }
  27166. $xmin = min($xmin, $x);
  27167. $ymin = min($ymin, $y);
  27168. $xmax = max($xmax, $x);
  27169. $ymax = max($ymax, $y);
  27170. if ($relcoord) {
  27171. $xoffset = $x;
  27172. $yoffset = $y;
  27173. }
  27174. }
  27175. }
  27176. break;
  27177. }
  27178. case 'H': { // horizontal lineto
  27179. foreach ($params as $ck => $cp) {
  27180. $x = $cp + $xoffset;
  27181. if ((abs($x0 - $x) >= $minlen) OR (abs($y0 - $y) >= $minlen)) {
  27182. $this->_outLine($x, $y);
  27183. }
  27184. $xmin = min($xmin, $x);
  27185. $xmax = max($xmax, $x);
  27186. if ($relcoord) {
  27187. $xoffset = $x;
  27188. }
  27189. }
  27190. break;
  27191. }
  27192. case 'V': { // vertical lineto
  27193. foreach ($params as $ck => $cp) {
  27194. $y = $cp + $yoffset;
  27195. if ((abs($x0 - $x) >= $minlen) OR (abs($y0 - $y) >= $minlen)) {
  27196. $this->_outLine($x, $y);
  27197. }
  27198. $ymin = min($ymin, $y);
  27199. $ymax = max($ymax, $y);
  27200. if ($relcoord) {
  27201. $yoffset = $y;
  27202. }
  27203. }
  27204. break;
  27205. }
  27206. case 'C': { // curveto
  27207. foreach ($params as $ck => $cp) {
  27208. $params[$ck] = $cp;
  27209. if ((($ck + 1) % 6) == 0) {
  27210. $x1 = $params[($ck - 5)] + $xoffset;
  27211. $y1 = $params[($ck - 4)] + $yoffset;
  27212. $x2 = $params[($ck - 3)] + $xoffset;
  27213. $y2 = $params[($ck - 2)] + $yoffset;
  27214. $x = $params[($ck - 1)] + $xoffset;
  27215. $y = $params[($ck)] + $yoffset;
  27216. $this->_outCurve($x1, $y1, $x2, $y2, $x, $y);
  27217. $xmin = min($xmin, $x, $x1, $x2);
  27218. $ymin = min($ymin, $y, $y1, $y2);
  27219. $xmax = max($xmax, $x, $x1, $x2);
  27220. $ymax = max($ymax, $y, $y1, $y2);
  27221. if ($relcoord) {
  27222. $xoffset = $x;
  27223. $yoffset = $y;
  27224. }
  27225. }
  27226. }
  27227. break;
  27228. }
  27229. case 'S': { // shorthand/smooth curveto
  27230. foreach ($params as $ck => $cp) {
  27231. $params[$ck] = $cp;
  27232. if ((($ck + 1) % 4) == 0) {
  27233. if (($key > 0) AND ((strtoupper($paths[($key - 1)][1]) == 'C') OR (strtoupper($paths[($key - 1)][1]) == 'S'))) {
  27234. $x1 = (2 * $x) - $x2;
  27235. $y1 = (2 * $y) - $y2;
  27236. } else {
  27237. $x1 = $x;
  27238. $y1 = $y;
  27239. }
  27240. $x2 = $params[($ck - 3)] + $xoffset;
  27241. $y2 = $params[($ck - 2)] + $yoffset;
  27242. $x = $params[($ck - 1)] + $xoffset;
  27243. $y = $params[($ck)] + $yoffset;
  27244. $this->_outCurve($x1, $y1, $x2, $y2, $x, $y);
  27245. $xmin = min($xmin, $x, $x1, $x2);
  27246. $ymin = min($ymin, $y, $y1, $y2);
  27247. $xmax = max($xmax, $x, $x1, $x2);
  27248. $ymax = max($ymax, $y, $y1, $y2);
  27249. if ($relcoord) {
  27250. $xoffset = $x;
  27251. $yoffset = $y;
  27252. }
  27253. }
  27254. }
  27255. break;
  27256. }
  27257. case 'Q': { // quadratic Bézier curveto
  27258. foreach ($params as $ck => $cp) {
  27259. $params[$ck] = $cp;
  27260. if ((($ck + 1) % 4) == 0) {
  27261. // convert quadratic points to cubic points
  27262. $x1 = $params[($ck - 3)] + $xoffset;
  27263. $y1 = $params[($ck - 2)] + $yoffset;
  27264. $xa = ($x + (2 * $x1)) / 3;
  27265. $ya = ($y + (2 * $y1)) / 3;
  27266. $x = $params[($ck - 1)] + $xoffset;
  27267. $y = $params[($ck)] + $yoffset;
  27268. $xb = ($x + (2 * $x1)) / 3;
  27269. $yb = ($y + (2 * $y1)) / 3;
  27270. $this->_outCurve($xa, $ya, $xb, $yb, $x, $y);
  27271. $xmin = min($xmin, $x, $xa, $xb);
  27272. $ymin = min($ymin, $y, $ya, $yb);
  27273. $xmax = max($xmax, $x, $xa, $xb);
  27274. $ymax = max($ymax, $y, $ya, $yb);
  27275. if ($relcoord) {
  27276. $xoffset = $x;
  27277. $yoffset = $y;
  27278. }
  27279. }
  27280. }
  27281. break;
  27282. }
  27283. case 'T': { // shorthand/smooth quadratic Bézier curveto
  27284. foreach ($params as $ck => $cp) {
  27285. $params[$ck] = $cp;
  27286. if (($ck % 2) != 0) {
  27287. if (($key > 0) AND ((strtoupper($paths[($key - 1)][1]) == 'Q') OR (strtoupper($paths[($key - 1)][1]) == 'T'))) {
  27288. $x1 = (2 * $x) - $x1;
  27289. $y1 = (2 * $y) - $y1;
  27290. } else {
  27291. $x1 = $x;
  27292. $y1 = $y;
  27293. }
  27294. // convert quadratic points to cubic points
  27295. $xa = ($x + (2 * $x1)) / 3;
  27296. $ya = ($y + (2 * $y1)) / 3;
  27297. $x = $params[($ck - 1)] + $xoffset;
  27298. $y = $params[($ck)] + $yoffset;
  27299. $xb = ($x + (2 * $x1)) / 3;
  27300. $yb = ($y + (2 * $y1)) / 3;
  27301. $this->_outCurve($xa, $ya, $xb, $yb, $x, $y);
  27302. $xmin = min($xmin, $x, $xa, $xb);
  27303. $ymin = min($ymin, $y, $ya, $yb);
  27304. $xmax = max($xmax, $x, $xa, $xb);
  27305. $ymax = max($ymax, $y, $ya, $yb);
  27306. if ($relcoord) {
  27307. $xoffset = $x;
  27308. $yoffset = $y;
  27309. }
  27310. }
  27311. }
  27312. break;
  27313. }
  27314. case 'A': { // elliptical arc
  27315. foreach ($params as $ck => $cp) {
  27316. $params[$ck] = $cp;
  27317. if ((($ck + 1) % 7) == 0) {
  27318. $x0 = $x;
  27319. $y0 = $y;
  27320. $rx = abs($params[($ck - 6)]);
  27321. $ry = abs($params[($ck - 5)]);
  27322. $ang = -$rawparams[($ck - 4)];
  27323. $angle = deg2rad($ang);
  27324. $fa = $rawparams[($ck - 3)]; // large-arc-flag
  27325. $fs = $rawparams[($ck - 2)]; // sweep-flag
  27326. $x = $params[($ck - 1)] + $xoffset;
  27327. $y = $params[$ck] + $yoffset;
  27328. if ((abs($x0 - $x) < $minlen) AND (abs($y0 - $y) < $minlen)) {
  27329. // endpoints are almost identical
  27330. $xmin = min($xmin, $x);
  27331. $ymin = min($ymin, $y);
  27332. $xmax = max($xmax, $x);
  27333. $ymax = max($ymax, $y);
  27334. } else {
  27335. $cos_ang = cos($angle);
  27336. $sin_ang = sin($angle);
  27337. $a = (($x0 - $x) / 2);
  27338. $b = (($y0 - $y) / 2);
  27339. $xa = ($a * $cos_ang) - ($b * $sin_ang);
  27340. $ya = ($a * $sin_ang) + ($b * $cos_ang);
  27341. $rx2 = $rx * $rx;
  27342. $ry2 = $ry * $ry;
  27343. $xa2 = $xa * $xa;
  27344. $ya2 = $ya * $ya;
  27345. $delta = ($xa2 / $rx2) + ($ya2 / $ry2);
  27346. if ($delta > 1) {
  27347. $rx *= sqrt($delta);
  27348. $ry *= sqrt($delta);
  27349. $rx2 = $rx * $rx;
  27350. $ry2 = $ry * $ry;
  27351. }
  27352. $numerator = (($rx2 * $ry2) - ($rx2 * $ya2) - ($ry2 * $xa2));
  27353. if ($numerator < 0) {
  27354. $root = 0;
  27355. } else {
  27356. $root = sqrt($numerator / (($rx2 * $ya2) + ($ry2 * $xa2)));
  27357. }
  27358. if ($fa == $fs){
  27359. $root *= -1;
  27360. }
  27361. $cax = $root * (($rx * $ya) / $ry);
  27362. $cay = -$root * (($ry * $xa) / $rx);
  27363. // coordinates of ellipse center
  27364. $cx = ($cax * $cos_ang) - ($cay * $sin_ang) + (($x0 + $x) / 2);
  27365. $cy = ($cax * $sin_ang) + ($cay * $cos_ang) + (($y0 + $y) / 2);
  27366. // get angles
  27367. $angs = $this->getVectorsAngle(1, 0, (($xa - $cax) / $rx), (($cay - $ya) / $ry));
  27368. $dang = $this->getVectorsAngle((($xa - $cax) / $rx), (($ya - $cay) / $ry), ((-$xa - $cax) / $rx), ((-$ya - $cay) / $ry));
  27369. if (($fs == 0) AND ($dang > 0)) {
  27370. $dang -= (2 * M_PI);
  27371. } elseif (($fs == 1) AND ($dang < 0)) {
  27372. $dang += (2 * M_PI);
  27373. }
  27374. $angf = $angs - $dang;
  27375. if ((($fs == 0) AND ($angs > $angf)) OR (($fs == 1) AND ($angs < $angf))) {
  27376. // reverse angles
  27377. $tmp = $angs;
  27378. $angs = $angf;
  27379. $angf = $tmp;
  27380. }
  27381. $angs = round(rad2deg($angs), 6);
  27382. $angf = round(rad2deg($angf), 6);
  27383. // covent angles to positive values
  27384. if (($angs < 0) AND ($angf < 0)) {
  27385. $angs += 360;
  27386. $angf += 360;
  27387. }
  27388. $pie = false;
  27389. if (($key == 0) AND (isset($paths[($key + 1)][1])) AND (trim($paths[($key + 1)][1]) == 'z')) {
  27390. $pie = true;
  27391. }
  27392. list($axmin, $aymin, $axmax, $aymax) = $this->_outellipticalarc($cx, $cy, $rx, $ry, $ang, $angs, $angf, $pie, 2, false, ($fs == 0), true);
  27393. $xmin = min($xmin, $x, $axmin);
  27394. $ymin = min($ymin, $y, $aymin);
  27395. $xmax = max($xmax, $x, $axmax);
  27396. $ymax = max($ymax, $y, $aymax);
  27397. }
  27398. if ($relcoord) {
  27399. $xoffset = $x;
  27400. $yoffset = $y;
  27401. }
  27402. }
  27403. }
  27404. break;
  27405. }
  27406. case 'Z': {
  27407. $this->_out('h');
  27408. break;
  27409. }
  27410. }
  27411. $firstcmd = false;
  27412. } // end foreach
  27413. if (!empty($op)) {
  27414. $this->_out($op);
  27415. }
  27416. return array($xmin, $ymin, ($xmax - $xmin), ($ymax - $ymin));
  27417. }
  27418. /**
  27419. * Returns the angle in radiants between two vectors
  27420. * @param $x1 (int) X coordinate of first vector point
  27421. * @param $y1 (int) Y coordinate of first vector point
  27422. * @param $x2 (int) X coordinate of second vector point
  27423. * @param $y2 (int) Y coordinate of second vector point
  27424. * @author Nicola Asuni
  27425. * @since 5.0.000 (2010-05-04)
  27426. * @protected
  27427. */
  27428. protected function getVectorsAngle($x1, $y1, $x2, $y2) {
  27429. $dprod = ($x1 * $x2) + ($y1 * $y2);
  27430. $dist1 = sqrt(($x1 * $x1) + ($y1 * $y1));
  27431. $dist2 = sqrt(($x2 * $x2) + ($y2 * $y2));
  27432. $angle = acos($dprod / ($dist1 * $dist2));
  27433. if (is_nan($angle)) {
  27434. $angle = M_PI;
  27435. }
  27436. if ((($x1 * $y2) - ($x2 * $y1)) < 0) {
  27437. $angle *= -1;
  27438. }
  27439. return $angle;
  27440. }
  27441. /**
  27442. * Sets the opening SVG element handler function for the XML parser. (*** TO BE COMPLETED ***)
  27443. * @param $parser (resource) The first parameter, parser, is a reference to the XML parser calling the handler.
  27444. * @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.
  27445. * @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.
  27446. * @param $ctm (array) tranformation matrix for clipping mode (starting transformation matrix).
  27447. * @author Nicola Asuni
  27448. * @since 5.0.000 (2010-05-02)
  27449. * @protected
  27450. */
  27451. protected function startSVGElementHandler($parser, $name, $attribs, $ctm=array()) {
  27452. // check if we are in clip mode
  27453. if ($this->svgclipmode) {
  27454. $this->svgclippaths[$this->svgclipid][] = array('name' => $name, 'attribs' => $attribs, 'tm' => $this->svgcliptm[$this->svgclipid]);
  27455. return;
  27456. }
  27457. if ($this->svgdefsmode AND !in_array($name, array('clipPath', 'linearGradient', 'radialGradient', 'stop'))) {
  27458. if (!isset($attribs['id'])) {
  27459. $attribs['id'] = 'DF_'.(count($this->svgdefs) + 1);
  27460. }
  27461. $this->svgdefs[$attribs['id']] = array('name' => $name, 'attribs' => $attribs);
  27462. return;
  27463. }
  27464. $clipping = false;
  27465. if ($parser == 'clip-path') {
  27466. // set clipping mode
  27467. $clipping = true;
  27468. }
  27469. // get styling properties
  27470. $prev_svgstyle = $this->svgstyles[(count($this->svgstyles) - 1)]; // previous style
  27471. $svgstyle = $this->svgstyles[0]; // set default style
  27472. if (isset($attribs['style']) AND !$this->empty_string($attribs['style'])) {
  27473. // fix style for regular expression
  27474. $attribs['style'] = ';'.$attribs['style'];
  27475. }
  27476. foreach ($prev_svgstyle as $key => $val) {
  27477. if (in_array($key, $this->svginheritprop)) {
  27478. // inherit previous value
  27479. $svgstyle[$key] = $val;
  27480. }
  27481. if (isset($attribs[$key]) AND !$this->empty_string($attribs[$key])) {
  27482. // specific attribute settings
  27483. if ($attribs[$key] == 'inherit') {
  27484. $svgstyle[$key] = $val;
  27485. } else {
  27486. $svgstyle[$key] = $attribs[$key];
  27487. }
  27488. } elseif (isset($attribs['style']) AND !$this->empty_string($attribs['style'])) {
  27489. // CSS style syntax
  27490. $attrval = array();
  27491. if (preg_match('/[;\"\s]{1}'.$key.'[\s]*:[\s]*([^;\"]*)/si', $attribs['style'], $attrval) AND isset($attrval[1])) {
  27492. if ($attrval[1] == 'inherit') {
  27493. $svgstyle[$key] = $val;
  27494. } else {
  27495. $svgstyle[$key] = $attrval[1];
  27496. }
  27497. }
  27498. }
  27499. }
  27500. // transformation matrix
  27501. if (!empty($ctm)) {
  27502. $tm = $ctm;
  27503. } else {
  27504. $tm = $this->svgstyles[(count($this->svgstyles) - 1)]['transfmatrix'];
  27505. }
  27506. if (isset($attribs['transform']) AND !empty($attribs['transform'])) {
  27507. $tm = $this->getTransformationMatrixProduct($tm, $this->getSVGTransformMatrix($attribs['transform']));
  27508. }
  27509. $svgstyle['transfmatrix'] = $tm;
  27510. $invisible = false;
  27511. if (($svgstyle['visibility'] == 'hidden') OR ($svgstyle['visibility'] == 'collapse') OR ($svgstyle['display'] == 'none')) {
  27512. // the current graphics element is invisible (nothing is painted)
  27513. $invisible = true;
  27514. }
  27515. // process tag
  27516. switch($name) {
  27517. case 'defs': {
  27518. $this->svgdefsmode = true;
  27519. break;
  27520. }
  27521. // clipPath
  27522. case 'clipPath': {
  27523. if ($invisible) {
  27524. break;
  27525. }
  27526. $this->svgclipmode = true;
  27527. if (!isset($attribs['id'])) {
  27528. $attribs['id'] = 'CP_'.(count($this->svgcliptm) + 1);
  27529. }
  27530. $this->svgclipid = $attribs['id'];
  27531. $this->svgclippaths[$this->svgclipid] = array();
  27532. $this->svgcliptm[$this->svgclipid] = $tm;
  27533. break;
  27534. }
  27535. case 'svg': {
  27536. // start of SVG object
  27537. break;
  27538. }
  27539. case 'g': {
  27540. // group together related graphics elements
  27541. array_push($this->svgstyles, $svgstyle);
  27542. $this->StartTransform();
  27543. $this->setSVGStyles($svgstyle, $prev_svgstyle);
  27544. break;
  27545. }
  27546. case 'linearGradient': {
  27547. if ($this->pdfa_mode) {
  27548. break;
  27549. }
  27550. if (!isset($attribs['id'])) {
  27551. $attribs['id'] = 'GR_'.(count($this->svggradients) + 1);
  27552. }
  27553. $this->svggradientid = $attribs['id'];
  27554. $this->svggradients[$this->svggradientid] = array();
  27555. $this->svggradients[$this->svggradientid]['type'] = 2;
  27556. $this->svggradients[$this->svggradientid]['stops'] = array();
  27557. if (isset($attribs['gradientUnits'])) {
  27558. $this->svggradients[$this->svggradientid]['gradientUnits'] = $attribs['gradientUnits'];
  27559. } else {
  27560. $this->svggradients[$this->svggradientid]['gradientUnits'] = 'objectBoundingBox';
  27561. }
  27562. //$attribs['spreadMethod']
  27563. $x1 = (isset($attribs['x1'])?$attribs['x1']:'0%');
  27564. $y1 = (isset($attribs['y1'])?$attribs['y1']:'0%');
  27565. $x2 = (isset($attribs['x2'])?$attribs['x2']:'100%');
  27566. $y2 = (isset($attribs['y2'])?$attribs['y2']:'0%');
  27567. if (substr($x1, -1) != '%') {
  27568. $this->svggradients[$this->svggradientid]['mode'] = 'measure';
  27569. } else {
  27570. $this->svggradients[$this->svggradientid]['mode'] = 'percentage';
  27571. }
  27572. if (isset($attribs['gradientTransform'])) {
  27573. $this->svggradients[$this->svggradientid]['gradientTransform'] = $this->getSVGTransformMatrix($attribs['gradientTransform']);
  27574. }
  27575. $this->svggradients[$this->svggradientid]['coords'] = array($x1, $y1, $x2, $y2);
  27576. if (isset($attribs['xlink:href']) AND !empty($attribs['xlink:href'])) {
  27577. // gradient is defined on another place
  27578. $this->svggradients[$this->svggradientid]['xref'] = substr($attribs['xlink:href'], 1);
  27579. }
  27580. break;
  27581. }
  27582. case 'radialGradient': {
  27583. if ($this->pdfa_mode) {
  27584. break;
  27585. }
  27586. if (!isset($attribs['id'])) {
  27587. $attribs['id'] = 'GR_'.(count($this->svggradients) + 1);
  27588. }
  27589. $this->svggradientid = $attribs['id'];
  27590. $this->svggradients[$this->svggradientid] = array();
  27591. $this->svggradients[$this->svggradientid]['type'] = 3;
  27592. $this->svggradients[$this->svggradientid]['stops'] = array();
  27593. if (isset($attribs['gradientUnits'])) {
  27594. $this->svggradients[$this->svggradientid]['gradientUnits'] = $attribs['gradientUnits'];
  27595. } else {
  27596. $this->svggradients[$this->svggradientid]['gradientUnits'] = 'objectBoundingBox';
  27597. }
  27598. //$attribs['spreadMethod']
  27599. $cx = (isset($attribs['cx'])?$attribs['cx']:0.5);
  27600. $cy = (isset($attribs['cy'])?$attribs['cy']:0.5);
  27601. $fx = (isset($attribs['fx'])?$attribs['fx']:$cx);
  27602. $fy = (isset($attribs['fy'])?$attribs['fy']:$cy);
  27603. $r = (isset($attribs['r'])?$attribs['r']:0.5);
  27604. if (isset($attribs['cx']) AND (substr($attribs['cx'], -1) != '%')) {
  27605. $this->svggradients[$this->svggradientid]['mode'] = 'measure';
  27606. } else {
  27607. $this->svggradients[$this->svggradientid]['mode'] = 'percentage';
  27608. }
  27609. if (isset($attribs['gradientTransform'])) {
  27610. $this->svggradients[$this->svggradientid]['gradientTransform'] = $this->getSVGTransformMatrix($attribs['gradientTransform']);
  27611. }
  27612. $this->svggradients[$this->svggradientid]['coords'] = array($cx, $cy, $fx, $fy, $r);
  27613. if (isset($attribs['xlink:href']) AND !empty($attribs['xlink:href'])) {
  27614. // gradient is defined on another place
  27615. $this->svggradients[$this->svggradientid]['xref'] = substr($attribs['xlink:href'], 1);
  27616. }
  27617. break;
  27618. }
  27619. case 'stop': {
  27620. // gradient stops
  27621. if (substr($attribs['offset'], -1) == '%') {
  27622. $offset = floatval(substr($attribs['offset'], -1)) / 100;
  27623. } else {
  27624. $offset = floatval($attribs['offset']);
  27625. if ($offset > 1) {
  27626. $offset /= 100;
  27627. }
  27628. }
  27629. $stop_color = isset($svgstyle['stop-color'])?$this->convertHTMLColorToDec($svgstyle['stop-color']):'black';
  27630. $opacity = isset($svgstyle['stop-opacity'])?$svgstyle['stop-opacity']:1;
  27631. $this->svggradients[$this->svggradientid]['stops'][] = array('offset' => $offset, 'color' => $stop_color, 'opacity' => $opacity);
  27632. break;
  27633. }
  27634. // paths
  27635. case 'path': {
  27636. if ($invisible) {
  27637. break;
  27638. }
  27639. if (isset($attribs['d'])) {
  27640. $d = trim($attribs['d']);
  27641. if (!empty($d)) {
  27642. if ($clipping) {
  27643. $this->SVGTransform($tm);
  27644. $this->SVGPath($d, 'CNZ');
  27645. } else {
  27646. $this->StartTransform();
  27647. $this->SVGTransform($tm);
  27648. $obstyle = $this->setSVGStyles($svgstyle, $prev_svgstyle, 0, 0, 1, 1, 'SVGPath', array($d, 'CNZ'));
  27649. if (!empty($obstyle)) {
  27650. $this->SVGPath($d, $obstyle);
  27651. }
  27652. $this->StopTransform();
  27653. }
  27654. }
  27655. }
  27656. break;
  27657. }
  27658. // shapes
  27659. case 'rect': {
  27660. if ($invisible) {
  27661. break;
  27662. }
  27663. $x = (isset($attribs['x'])?$this->getHTMLUnitToUnits($attribs['x'], 0, $this->svgunit, false):0);
  27664. $y = (isset($attribs['y'])?$this->getHTMLUnitToUnits($attribs['y'], 0, $this->svgunit, false):0);
  27665. $w = (isset($attribs['width'])?$this->getHTMLUnitToUnits($attribs['width'], 0, $this->svgunit, false):0);
  27666. $h = (isset($attribs['height'])?$this->getHTMLUnitToUnits($attribs['height'], 0, $this->svgunit, false):0);
  27667. $rx = (isset($attribs['rx'])?$this->getHTMLUnitToUnits($attribs['rx'], 0, $this->svgunit, false):0);
  27668. $ry = (isset($attribs['ry'])?$this->getHTMLUnitToUnits($attribs['ry'], 0, $this->svgunit, false):$rx);
  27669. if ($clipping) {
  27670. $this->SVGTransform($tm);
  27671. $this->RoundedRectXY($x, $y, $w, $h, $rx, $ry, '1111', 'CNZ', array(), array());
  27672. } else {
  27673. $this->StartTransform();
  27674. $this->SVGTransform($tm);
  27675. $obstyle = $this->setSVGStyles($svgstyle, $prev_svgstyle, $x, $y, $w, $h, 'RoundedRectXY', array($x, $y, $w, $h, $rx, $ry, '1111', 'CNZ'));
  27676. if (!empty($obstyle)) {
  27677. $this->RoundedRectXY($x, $y, $w, $h, $rx, $ry, '1111', $obstyle, array(), array());
  27678. }
  27679. $this->StopTransform();
  27680. }
  27681. break;
  27682. }
  27683. case 'circle': {
  27684. if ($invisible) {
  27685. break;
  27686. }
  27687. $cx = (isset($attribs['cx'])?$this->getHTMLUnitToUnits($attribs['cx'], 0, $this->svgunit, false):0);
  27688. $cy = (isset($attribs['cy'])?$this->getHTMLUnitToUnits($attribs['cy'], 0, $this->svgunit, false):0);
  27689. $r = (isset($attribs['r'])?$this->getHTMLUnitToUnits($attribs['r'], 0, $this->svgunit, false):0);
  27690. $x = $cx - $r;
  27691. $y = $cy - $r;
  27692. $w = 2 * $r;
  27693. $h = $w;
  27694. if ($clipping) {
  27695. $this->SVGTransform($tm);
  27696. $this->Circle($cx, $cy, $r, 0, 360, 'CNZ', array(), array(), 8);
  27697. } else {
  27698. $this->StartTransform();
  27699. $this->SVGTransform($tm);
  27700. $obstyle = $this->setSVGStyles($svgstyle, $prev_svgstyle, $x, $y, $w, $h, 'Circle', array($cx, $cy, $r, 0, 360, 'CNZ'));
  27701. if (!empty($obstyle)) {
  27702. $this->Circle($cx, $cy, $r, 0, 360, $obstyle, array(), array(), 8);
  27703. }
  27704. $this->StopTransform();
  27705. }
  27706. break;
  27707. }
  27708. case 'ellipse': {
  27709. if ($invisible) {
  27710. break;
  27711. }
  27712. $cx = (isset($attribs['cx'])?$this->getHTMLUnitToUnits($attribs['cx'], 0, $this->svgunit, false):0);
  27713. $cy = (isset($attribs['cy'])?$this->getHTMLUnitToUnits($attribs['cy'], 0, $this->svgunit, false):0);
  27714. $rx = (isset($attribs['rx'])?$this->getHTMLUnitToUnits($attribs['rx'], 0, $this->svgunit, false):0);
  27715. $ry = (isset($attribs['ry'])?$this->getHTMLUnitToUnits($attribs['ry'], 0, $this->svgunit, false):0);
  27716. $x = $cx - $rx;
  27717. $y = $cy - $ry;
  27718. $w = 2 * $rx;
  27719. $h = 2 * $ry;
  27720. if ($clipping) {
  27721. $this->SVGTransform($tm);
  27722. $this->Ellipse($cx, $cy, $rx, $ry, 0, 0, 360, 'CNZ', array(), array(), 8);
  27723. } else {
  27724. $this->StartTransform();
  27725. $this->SVGTransform($tm);
  27726. $obstyle = $this->setSVGStyles($svgstyle, $prev_svgstyle, $x, $y, $w, $h, 'Ellipse', array($cx, $cy, $rx, $ry, 0, 0, 360, 'CNZ'));
  27727. if (!empty($obstyle)) {
  27728. $this->Ellipse($cx, $cy, $rx, $ry, 0, 0, 360, $obstyle, array(), array(), 8);
  27729. }
  27730. $this->StopTransform();
  27731. }
  27732. break;
  27733. }
  27734. case 'line': {
  27735. if ($invisible) {
  27736. break;
  27737. }
  27738. $x1 = (isset($attribs['x1'])?$this->getHTMLUnitToUnits($attribs['x1'], 0, $this->svgunit, false):0);
  27739. $y1 = (isset($attribs['y1'])?$this->getHTMLUnitToUnits($attribs['y1'], 0, $this->svgunit, false):0);
  27740. $x2 = (isset($attribs['x2'])?$this->getHTMLUnitToUnits($attribs['x2'], 0, $this->svgunit, false):0);
  27741. $y2 = (isset($attribs['y2'])?$this->getHTMLUnitToUnits($attribs['y2'], 0, $this->svgunit, false):0);
  27742. $x = $x1;
  27743. $y = $y1;
  27744. $w = abs($x2 - $x1);
  27745. $h = abs($y2 - $y1);
  27746. if (!$clipping) {
  27747. $this->StartTransform();
  27748. $this->SVGTransform($tm);
  27749. $obstyle = $this->setSVGStyles($svgstyle, $prev_svgstyle, $x, $y, $w, $h, 'Line', array($x1, $y1, $x2, $y2));
  27750. $this->Line($x1, $y1, $x2, $y2);
  27751. $this->StopTransform();
  27752. }
  27753. break;
  27754. }
  27755. case 'polyline':
  27756. case 'polygon': {
  27757. if ($invisible) {
  27758. break;
  27759. }
  27760. $points = (isset($attribs['points'])?$attribs['points']:'0 0');
  27761. $points = trim($points);
  27762. // note that point may use a complex syntax not covered here
  27763. $points = preg_split('/[\,\s]+/si', $points);
  27764. if (count($points) < 4) {
  27765. break;
  27766. }
  27767. $p = array();
  27768. $xmin = 2147483647;
  27769. $xmax = 0;
  27770. $ymin = 2147483647;
  27771. $ymax = 0;
  27772. foreach ($points as $key => $val) {
  27773. $p[$key] = $this->getHTMLUnitToUnits($val, 0, $this->svgunit, false);
  27774. if (($key % 2) == 0) {
  27775. // X coordinate
  27776. $xmin = min($xmin, $p[$key]);
  27777. $xmax = max($xmax, $p[$key]);
  27778. } else {
  27779. // Y coordinate
  27780. $ymin = min($ymin, $p[$key]);
  27781. $ymax = max($ymax, $p[$key]);
  27782. }
  27783. }
  27784. $x = $xmin;
  27785. $y = $ymin;
  27786. $w = ($xmax - $xmin);
  27787. $h = ($ymax - $ymin);
  27788. if ($name == 'polyline') {
  27789. $this->StartTransform();
  27790. $this->SVGTransform($tm);
  27791. $obstyle = $this->setSVGStyles($svgstyle, $prev_svgstyle, $x, $y, $w, $h, 'PolyLine', array($p, 'CNZ'));
  27792. $this->PolyLine($p, 'D', array(), array());
  27793. $this->StopTransform();
  27794. } else { // polygon
  27795. if ($clipping) {
  27796. $this->SVGTransform($tm);
  27797. $this->Polygon($p, 'CNZ', array(), array(), true);
  27798. } else {
  27799. $this->StartTransform();
  27800. $this->SVGTransform($tm);
  27801. $obstyle = $this->setSVGStyles($svgstyle, $prev_svgstyle, $x, $y, $w, $h, 'Polygon', array($p, 'CNZ'));
  27802. if (!empty($obstyle)) {
  27803. $this->Polygon($p, $obstyle, array(), array(), true);
  27804. }
  27805. $this->StopTransform();
  27806. }
  27807. }
  27808. break;
  27809. }
  27810. // image
  27811. case 'image': {
  27812. if ($invisible) {
  27813. break;
  27814. }
  27815. if (!isset($attribs['xlink:href']) OR empty($attribs['xlink:href'])) {
  27816. break;
  27817. }
  27818. $x = (isset($attribs['x'])?$this->getHTMLUnitToUnits($attribs['x'], 0, $this->svgunit, false):0);
  27819. $y = (isset($attribs['y'])?$this->getHTMLUnitToUnits($attribs['y'], 0, $this->svgunit, false):0);
  27820. $w = (isset($attribs['width'])?$this->getHTMLUnitToUnits($attribs['width'], 0, $this->svgunit, false):0);
  27821. $h = (isset($attribs['height'])?$this->getHTMLUnitToUnits($attribs['height'], 0, $this->svgunit, false):0);
  27822. $img = $attribs['xlink:href'];
  27823. if (!$clipping) {
  27824. $this->StartTransform();
  27825. $this->SVGTransform($tm);
  27826. $obstyle = $this->setSVGStyles($svgstyle, $prev_svgstyle, $x, $y, $w, $h);
  27827. // fix image path
  27828. if (!$this->empty_string($this->svgdir) AND (($img{0} == '.') OR (basename($img) == $img))) {
  27829. // replace relative path with full server path
  27830. $img = $this->svgdir.'/'.$img;
  27831. }
  27832. if (($img[0] == '/') AND !empty($_SERVER['DOCUMENT_ROOT']) AND ($_SERVER['DOCUMENT_ROOT'] != '/')) {
  27833. $findroot = strpos($img, $_SERVER['DOCUMENT_ROOT']);
  27834. if (($findroot === false) OR ($findroot > 1)) {
  27835. if (substr($_SERVER['DOCUMENT_ROOT'], -1) == '/') {
  27836. $img = substr($_SERVER['DOCUMENT_ROOT'], 0, -1).$img;
  27837. } else {
  27838. $img = $_SERVER['DOCUMENT_ROOT'].$img;
  27839. }
  27840. }
  27841. }
  27842. $img = urldecode($img);
  27843. $testscrtype = @parse_url($img);
  27844. if (!isset($testscrtype['query']) OR empty($testscrtype['query'])) {
  27845. // convert URL to server path
  27846. $img = str_replace(K_PATH_URL, K_PATH_MAIN, $img);
  27847. }
  27848. $this->Image($img, $x, $y, $w, $h);
  27849. $this->StopTransform();
  27850. }
  27851. break;
  27852. }
  27853. // text
  27854. case 'text':
  27855. case 'tspan': {
  27856. $this->svgtextmode['invisible'] = $invisible;
  27857. if ($invisible) {
  27858. break;
  27859. }
  27860. array_push($this->svgstyles, $svgstyle);
  27861. // only basic support - advanced features must be implemented
  27862. $x = (isset($attribs['x'])?$this->getHTMLUnitToUnits($attribs['x'], 0, $this->svgunit, false):$this->x);
  27863. $y = (isset($attribs['y'])?$this->getHTMLUnitToUnits($attribs['y'], 0, $this->svgunit, false):$this->y);
  27864. $svgstyle['text-color'] = $svgstyle['fill'];
  27865. $this->svgtext = '';
  27866. if (isset($svgstyle['text-anchor'])) {
  27867. $this->svgtextmode['text-anchor'] = $svgstyle['text-anchor'];
  27868. } else {
  27869. $this->svgtextmode['text-anchor'] = 'start';
  27870. }
  27871. if (isset($svgstyle['direction'])) {
  27872. if ($svgstyle['direction'] == 'rtl') {
  27873. $this->svgtextmode['rtl'] = true;
  27874. } else {
  27875. $this->svgtextmode['rtl'] = false;
  27876. }
  27877. } else {
  27878. $this->svgtextmode['rtl'] = false;
  27879. }
  27880. if (isset($svgstyle['stroke']) AND ($svgstyle['stroke'] != 'none') AND isset($svgstyle['stroke-width']) AND ($svgstyle['stroke-width'] > 0)) {
  27881. $this->svgtextmode['stroke'] = $this->getHTMLUnitToUnits($svgstyle['stroke-width'], 0, $this->svgunit, false);
  27882. } else {
  27883. $this->svgtextmode['stroke'] = false;
  27884. }
  27885. $this->StartTransform();
  27886. $this->SVGTransform($tm);
  27887. $obstyle = $this->setSVGStyles($svgstyle, $prev_svgstyle, $x, $y, 1, 1);
  27888. $this->x = $x;
  27889. $this->y = $y;
  27890. break;
  27891. }
  27892. // use
  27893. case 'use': {
  27894. if (isset($attribs['xlink:href'])) {
  27895. $use = $this->svgdefs[substr($attribs['xlink:href'], 1)];
  27896. if (isset($attribs['xlink:href'])) {
  27897. unset($attribs['xlink:href']);
  27898. }
  27899. if (isset($attribs['id'])) {
  27900. unset($attribs['id']);
  27901. }
  27902. $attribs = array_merge($use['attribs'], $attribs);
  27903. $this->startSVGElementHandler($parser, $use['name'], $use['attribs']);
  27904. }
  27905. break;
  27906. }
  27907. default: {
  27908. break;
  27909. }
  27910. } // end of switch
  27911. }
  27912. /**
  27913. * Sets the closing SVG element handler function for the XML parser.
  27914. * @param $parser (resource) The first parameter, parser, is a reference to the XML parser calling the handler.
  27915. * @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.
  27916. * @author Nicola Asuni
  27917. * @since 5.0.000 (2010-05-02)
  27918. * @protected
  27919. */
  27920. protected function endSVGElementHandler($parser, $name) {
  27921. switch($name) {
  27922. case 'defs': {
  27923. $this->svgdefsmode = false;
  27924. break;
  27925. }
  27926. // clipPath
  27927. case 'clipPath': {
  27928. $this->svgclipmode = false;
  27929. break;
  27930. }
  27931. case 'g': {
  27932. // ungroup: remove last style from array
  27933. array_pop($this->svgstyles);
  27934. $this->StopTransform();
  27935. break;
  27936. }
  27937. case 'text':
  27938. case 'tspan': {
  27939. if ($this->svgtextmode['invisible']) {
  27940. // This implementation must be fixed to following the rule:
  27941. // 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.
  27942. break;
  27943. }
  27944. // print text
  27945. $text = $this->stringTrim($this->svgtext);
  27946. if ($this->svgtextmode['text-anchor'] != 'start') {
  27947. $textlen = $this->GetStringWidth($text);
  27948. // check if string is RTL text
  27949. if ($this->svgtextmode['text-anchor'] == 'end') {
  27950. if ($this->svgtextmode['rtl']) {
  27951. $this->x += $textlen;
  27952. } else {
  27953. $this->x -= $textlen;
  27954. }
  27955. } elseif ($this->svgtextmode['text-anchor'] == 'middle') {
  27956. if ($this->svgtextmode['rtl']) {
  27957. $this->x += ($textlen / 2);
  27958. } else {
  27959. $this->x -= ($textlen / 2);
  27960. }
  27961. }
  27962. }
  27963. $textrendermode = $this->textrendermode;
  27964. $textstrokewidth = $this->textstrokewidth;
  27965. $this->setTextRenderingMode($this->svgtextmode['stroke'], true, false);
  27966. $this->Cell(0, 0, $text, 0, 0, '', false, '', 0, false, 'L', 'T');
  27967. // restore previous rendering mode
  27968. $this->textrendermode = $textrendermode;
  27969. $this->textstrokewidth = $textstrokewidth;
  27970. $this->svgtext = '';
  27971. $this->StopTransform();
  27972. array_pop($this->svgstyles);
  27973. break;
  27974. }
  27975. default: {
  27976. break;
  27977. }
  27978. }
  27979. }
  27980. /**
  27981. * Sets the character data handler function for the XML parser.
  27982. * @param $parser (resource) The first parameter, parser, is a reference to the XML parser calling the handler.
  27983. * @param $data (string) The second parameter, data, contains the character data as a string.
  27984. * @author Nicola Asuni
  27985. * @since 5.0.000 (2010-05-02)
  27986. * @protected
  27987. */
  27988. protected function segSVGContentHandler($parser, $data) {
  27989. $this->svgtext .= $data;
  27990. }
  27991. // --- END SVG METHODS -----------------------------------------------------
  27992. } // END OF TCPDF CLASS
  27993. //============================================================+
  27994. // END OF FILE
  27995. //============================================================+