tcpdf.php 1.0 MB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765247662476724768247692477024771247722477324774247752477624777247782477924780247812478224783247842478524786247872478824789247902479124792247932479424795247962479724798247992480024801248022480324804248052480624807248082480924810248112481224813248142481524816248172481824819248202482124822248232482424825248262482724828248292483024831248322483324834248352483624837248382483924840248412484224843248442484524846248472484824849248502485124852248532485424855248562485724858248592486024861248622486324864248652486624867248682486924870248712487224873248742487524876248772487824879248802488124882248832488424885248862488724888248892489024891248922489324894248952489624897248982489924900249012490224903249042490524906249072490824909249102491124912249132491424915249162491724918249192492024921249222492324924249252492624927249282492924930249312493224933249342493524936249372493824939249402494124942249432494424945249462494724948249492495024951249522495324954249552495624957249582495924960249612496224963249642496524966249672496824969249702497124972249732497424975249762497724978249792498024981249822498324984249852498624987249882498924990249912499224993249942499524996249972499824999250002500125002250032500425005250062500725008250092501025011250122501325014250152501625017250182501925020250212502225023250242502525026250272502825029250302503125032250332503425035250362503725038250392504025041250422504325044250452504625047250482504925050250512505225053250542505525056250572505825059250602506125062250632506425065250662506725068250692507025071250722507325074250752507625077250782507925080250812508225083250842508525086250872508825089250902509125092250932509425095250962509725098250992510025101251022510325104251052510625107251082510925110251112511225113251142511525116251172511825119251202512125122251232512425125251262512725128251292513025131251322513325134251352513625137251382513925140251412514225143251442514525146251472514825149251502515125152251532515425155251562515725158251592516025161251622516325164251652516625167251682516925170251712517225173251742517525176251772517825179251802518125182251832518425185251862518725188251892519025191251922519325194251952519625197251982519925200252012520225203252042520525206252072520825209252102521125212252132521425215252162521725218252192522025221252222522325224252252522625227252282522925230252312523225233252342523525236252372523825239252402524125242252432524425245252462524725248252492525025251252522525325254252552525625257252582525925260252612526225263252642526525266252672526825269252702527125272252732527425275252762527725278252792528025281252822528325284252852528625287252882528925290252912529225293252942529525296252972529825299253002530125302253032530425305253062530725308253092531025311253122531325314253152531625317253182531925320253212532225323253242532525326253272532825329253302533125332253332533425335253362533725338253392534025341253422534325344253452534625347253482534925350253512535225353253542535525356253572535825359253602536125362253632536425365253662536725368253692537025371253722537325374253752537625377253782537925380253812538225383253842538525386253872538825389253902539125392253932539425395253962539725398253992540025401254022540325404254052540625407254082540925410254112541225413254142541525416254172541825419254202542125422254232542425425254262542725428254292543025431254322543325434254352543625437254382543925440254412544225443254442544525446254472544825449254502545125452254532545425455254562545725458254592546025461254622546325464254652546625467254682546925470254712547225473254742547525476254772547825479254802548125482254832548425485254862548725488254892549025491254922549325494254952549625497254982549925500255012550225503255042550525506255072550825509255102551125512255132551425515255162551725518255192552025521255222552325524255252552625527255282552925530255312553225533255342553525536255372553825539255402554125542255432554425545255462554725548255492555025551255522555325554255552555625557255582555925560255612556225563255642556525566255672556825569255702557125572255732557425575255762557725578255792558025581255822558325584255852558625587255882558925590255912559225593255942559525596255972559825599256002560125602256032560425605256062560725608256092561025611256122561325614256152561625617256182561925620256212562225623256242562525626256272562825629256302563125632256332563425635256362563725638256392564025641256422564325644256452564625647256482564925650256512565225653256542565525656256572565825659256602566125662256632566425665256662566725668256692567025671256722567325674256752567625677256782567925680256812568225683256842568525686256872568825689256902569125692256932569425695256962569725698256992570025701257022570325704257052570625707257082570925710257112571225713257142571525716257172571825719257202572125722257232572425725257262572725728257292573025731257322573325734257352573625737257382573925740257412574225743257442574525746257472574825749257502575125752257532575425755257562575725758257592576025761257622576325764257652576625767257682576925770257712577225773257742577525776257772577825779257802578125782257832578425785257862578725788257892579025791257922579325794257952579625797257982579925800258012580225803258042580525806258072580825809258102581125812258132581425815258162581725818258192582025821258222582325824258252582625827258282582925830258312583225833258342583525836258372583825839258402584125842258432584425845258462584725848258492585025851258522585325854258552585625857258582585925860258612586225863258642586525866258672586825869258702587125872258732587425875258762587725878258792588025881258822588325884258852588625887258882588925890258912589225893258942589525896258972589825899259002590125902259032590425905259062590725908259092591025911259122591325914259152591625917259182591925920259212592225923259242592525926259272592825929259302593125932259332593425935259362593725938259392594025941259422594325944259452594625947259482594925950259512595225953259542595525956259572595825959259602596125962259632596425965259662596725968259692597025971259722597325974259752597625977259782597925980259812598225983259842598525986259872598825989259902599125992259932599425995259962599725998259992600026001260022600326004260052600626007260082600926010260112601226013260142601526016260172601826019260202602126022260232602426025260262602726028260292603026031260322603326034260352603626037260382603926040260412604226043260442604526046260472604826049260502605126052260532605426055260562605726058260592606026061260622606326064260652606626067260682606926070260712607226073260742607526076260772607826079260802608126082260832608426085260862608726088260892609026091260922609326094260952609626097260982609926100261012610226103261042610526106261072610826109261102611126112261132611426115261162611726118261192612026121261222612326124261252612626127261282612926130261312613226133261342613526136261372613826139261402614126142261432614426145261462614726148261492615026151261522615326154261552615626157261582615926160261612616226163261642616526166261672616826169261702617126172261732617426175261762617726178261792618026181261822618326184261852618626187261882618926190261912619226193261942619526196261972619826199262002620126202262032620426205262062620726208262092621026211262122621326214262152621626217262182621926220262212622226223262242622526226262272622826229262302623126232262332623426235262362623726238262392624026241262422624326244262452624626247262482624926250262512625226253262542625526256262572625826259262602626126262262632626426265262662626726268262692627026271262722627326274262752627626277262782627926280262812628226283262842628526286262872628826289262902629126292262932629426295262962629726298262992630026301263022630326304263052630626307263082630926310263112631226313263142631526316263172631826319263202632126322263232632426325263262632726328263292633026331263322633326334263352633626337263382633926340263412634226343263442634526346263472634826349263502635126352263532635426355263562635726358263592636026361263622636326364263652636626367263682636926370263712637226373263742637526376263772637826379263802638126382263832638426385263862638726388263892639026391263922639326394263952639626397263982639926400264012640226403264042640526406264072640826409264102641126412264132641426415264162641726418264192642026421264222642326424264252642626427264282642926430264312643226433264342643526436264372643826439264402644126442264432644426445264462644726448264492645026451264522645326454264552645626457264582645926460264612646226463264642646526466264672646826469264702647126472264732647426475264762647726478264792648026481264822648326484264852648626487264882648926490264912649226493264942649526496264972649826499265002650126502265032650426505265062650726508265092651026511265122651326514265152651626517265182651926520265212652226523265242652526526265272652826529265302653126532265332653426535265362653726538265392654026541265422654326544265452654626547265482654926550265512655226553265542655526556265572655826559265602656126562265632656426565265662656726568265692657026571265722657326574265752657626577265782657926580265812658226583265842658526586265872658826589265902659126592265932659426595265962659726598265992660026601266022660326604266052660626607266082660926610266112661226613266142661526616266172661826619266202662126622266232662426625266262662726628266292663026631266322663326634266352663626637266382663926640266412664226643266442664526646266472664826649266502665126652266532665426655266562665726658266592666026661266622666326664266652666626667266682666926670266712667226673266742667526676266772667826679266802668126682266832668426685266862668726688266892669026691266922669326694266952669626697266982669926700267012670226703267042670526706267072670826709267102671126712267132671426715267162671726718267192672026721267222672326724267252672626727267282672926730267312673226733267342673526736267372673826739267402674126742267432674426745267462674726748267492675026751267522675326754267552675626757267582675926760267612676226763267642676526766267672676826769267702677126772267732677426775267762677726778267792678026781267822678326784267852678626787267882678926790267912679226793267942679526796267972679826799268002680126802268032680426805268062680726808268092681026811268122681326814268152681626817268182681926820268212682226823268242682526826268272682826829268302683126832268332683426835268362683726838268392684026841268422684326844268452684626847268482684926850268512685226853268542685526856268572685826859268602686126862268632686426865268662686726868268692687026871268722687326874268752687626877268782687926880268812688226883268842688526886268872688826889268902689126892268932689426895268962689726898268992690026901269022690326904269052690626907269082690926910269112691226913269142691526916269172691826919269202692126922269232692426925269262692726928269292693026931269322693326934269352693626937269382693926940269412694226943269442694526946269472694826949269502695126952269532695426955269562695726958269592696026961269622696326964269652696626967269682696926970269712697226973269742697526976269772697826979269802698126982269832698426985269862698726988269892699026991269922699326994269952699626997269982699927000270012700227003270042700527006270072700827009270102701127012270132701427015270162701727018270192702027021270222702327024270252702627027270282702927030270312703227033270342703527036270372703827039270402704127042270432704427045270462704727048270492705027051270522705327054270552705627057270582705927060270612706227063270642706527066270672706827069270702707127072270732707427075270762707727078270792708027081270822708327084270852708627087270882708927090270912709227093270942709527096270972709827099271002710127102271032710427105271062710727108271092711027111271122711327114271152711627117271182711927120271212712227123271242712527126271272712827129271302713127132271332713427135271362713727138271392714027141271422714327144271452714627147271482714927150271512715227153271542715527156271572715827159271602716127162271632716427165271662716727168271692717027171271722717327174271752717627177271782717927180271812718227183271842718527186271872718827189271902719127192271932719427195271962719727198271992720027201272022720327204272052720627207272082720927210272112721227213272142721527216272172721827219272202722127222272232722427225272262722727228272292723027231272322723327234272352723627237272382723927240272412724227243272442724527246272472724827249272502725127252272532725427255272562725727258272592726027261272622726327264272652726627267272682726927270272712727227273272742727527276272772727827279272802728127282272832728427285272862728727288272892729027291272922729327294272952729627297272982729927300273012730227303273042730527306273072730827309273102731127312273132731427315273162731727318273192732027321273222732327324273252732627327273282732927330273312733227333273342733527336273372733827339273402734127342273432734427345273462734727348273492735027351273522735327354273552735627357273582735927360273612736227363273642736527366273672736827369273702737127372273732737427375273762737727378273792738027381273822738327384273852738627387273882738927390273912739227393273942739527396273972739827399274002740127402274032740427405274062740727408274092741027411274122741327414274152741627417274182741927420274212742227423274242742527426274272742827429274302743127432274332743427435274362743727438274392744027441274422744327444274452744627447274482744927450274512745227453274542745527456274572745827459274602746127462274632746427465274662746727468274692747027471274722747327474274752747627477274782747927480274812748227483274842748527486274872748827489274902749127492274932749427495274962749727498274992750027501275022750327504275052750627507275082750927510275112751227513275142751527516275172751827519275202752127522275232752427525275262752727528275292753027531275322753327534275352753627537275382753927540275412754227543275442754527546275472754827549275502755127552275532755427555275562755727558275592756027561275622756327564275652756627567275682756927570275712757227573275742757527576275772757827579275802758127582275832758427585275862758727588275892759027591275922759327594275952759627597275982759927600276012760227603276042760527606276072760827609276102761127612276132761427615276162761727618276192762027621276222762327624276252762627627276282762927630276312763227633276342763527636276372763827639276402764127642276432764427645276462764727648276492765027651276522765327654276552765627657276582765927660276612766227663276642766527666276672766827669276702767127672276732767427675276762767727678276792768027681276822768327684276852768627687276882768927690276912769227693276942769527696276972769827699277002770127702277032770427705277062770727708277092771027711277122771327714277152771627717277182771927720277212772227723277242772527726277272772827729277302773127732277332773427735277362773727738277392774027741277422774327744277452774627747277482774927750277512775227753277542775527756277572775827759277602776127762277632776427765277662776727768277692777027771277722777327774277752777627777277782777927780277812778227783277842778527786277872778827789277902779127792277932779427795277962779727798277992780027801278022780327804278052780627807278082780927810278112781227813278142781527816278172781827819278202782127822278232782427825278262782727828278292783027831278322783327834278352783627837278382783927840278412784227843278442784527846278472784827849278502785127852278532785427855278562785727858278592786027861278622786327864278652786627867278682786927870278712787227873278742787527876278772787827879278802788127882278832788427885278862788727888278892789027891278922789327894278952789627897278982789927900279012790227903279042790527906279072790827909279102791127912279132791427915279162791727918279192792027921279222792327924279252792627927279282792927930279312793227933279342793527936279372793827939279402794127942279432794427945279462794727948279492795027951279522795327954279552795627957279582795927960279612796227963279642796527966279672796827969279702797127972279732797427975279762797727978279792798027981279822798327984279852798627987279882798927990279912799227993279942799527996279972799827999280002800128002280032800428005280062800728008280092801028011280122801328014280152801628017280182801928020280212802228023280242802528026280272802828029280302803128032280332803428035280362803728038280392804028041280422804328044280452804628047280482804928050280512805228053280542805528056280572805828059280602806128062280632806428065280662806728068280692807028071280722807328074280752807628077280782807928080280812808228083280842808528086280872808828089280902809128092280932809428095280962809728098280992810028101281022810328104281052810628107281082810928110281112811228113281142811528116281172811828119281202812128122281232812428125281262812728128281292813028131281322813328134281352813628137281382813928140281412814228143281442814528146281472814828149281502815128152281532815428155281562815728158281592816028161281622816328164281652816628167281682816928170281712817228173281742817528176281772817828179281802818128182281832818428185281862818728188281892819028191281922819328194281952819628197281982819928200282012820228203282042820528206282072820828209282102821128212282132821428215282162821728218282192822028221282222822328224282252822628227282282822928230282312823228233282342823528236282372823828239282402824128242282432824428245282462824728248282492825028251282522825328254282552825628257282582825928260282612826228263282642826528266282672826828269282702827128272282732827428275282762827728278282792828028281282822828328284282852828628287282882828928290282912829228293282942829528296282972829828299283002830128302283032830428305283062830728308283092831028311283122831328314283152831628317283182831928320283212832228323283242832528326283272832828329283302833128332283332833428335283362833728338283392834028341283422834328344283452834628347283482834928350283512835228353283542835528356283572835828359283602836128362283632836428365283662836728368283692837028371283722837328374283752837628377283782837928380283812838228383283842838528386283872838828389283902839128392283932839428395283962839728398283992840028401284022840328404284052840628407284082840928410284112841228413284142841528416284172841828419284202842128422284232842428425284262842728428284292843028431284322843328434284352843628437284382843928440284412844228443284442844528446284472844828449284502845128452284532845428455284562845728458284592846028461284622846328464284652846628467284682846928470284712847228473284742847528476284772847828479284802848128482284832848428485284862848728488284892849028491284922849328494284952849628497284982849928500285012850228503285042850528506285072850828509285102851128512285132851428515285162851728518285192852028521285222852328524285252852628527285282852928530285312853228533285342853528536285372853828539285402854128542285432854428545285462854728548285492855028551285522855328554285552855628557285582855928560285612856228563285642856528566285672856828569285702857128572285732857428575285762857728578285792858028581285822858328584285852858628587285882858928590285912859228593285942859528596285972859828599286002860128602286032860428605286062860728608286092861028611286122861328614286152861628617286182861928620286212862228623286242862528626286272862828629286302863128632286332863428635286362863728638286392864028641286422864328644286452864628647286482864928650286512865228653286542865528656286572865828659286602866128662286632866428665286662866728668286692867028671286722867328674286752867628677286782867928680286812868228683286842868528686286872868828689286902869128692286932869428695286962869728698286992870028701287022870328704287052870628707287082870928710287112871228713287142871528716287172871828719287202872128722287232872428725287262872728728287292873028731287322873328734287352873628737287382873928740287412874228743287442874528746287472874828749287502875128752287532875428755287562875728758287592876028761287622876328764287652876628767287682876928770287712877228773287742877528776287772877828779287802878128782287832878428785287862878728788287892879028791287922879328794287952879628797287982879928800288012880228803288042880528806288072880828809
  1. <?php
  2. //============================================================+
  3. // File name : tcpdf.php
  4. // Version : 5.9.144
  5. // Begin : 2002-08-03
  6. // Last Update : 2012-01-12
  7. // Author : Nicola Asuni - Tecnick.com LTD - Manor Coach House, Church Hill, Aldershot, Hants, GU12 4RQ, UK - 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. // Copyright (C) 2002-2012 Nicola Asuni - Tecnick.com LTD
  11. //
  12. // This file is part of TCPDF software library.
  13. //
  14. // TCPDF is free software: you can redistribute it and/or modify it
  15. // under the terms of the GNU Lesser General Public License as
  16. // published by the Free Software Foundation, either version 3 of the
  17. // License, or (at your option) any later version. Additionally,
  18. // YOU CAN'T REMOVE ANY TCPDF COPYRIGHT NOTICE OR LINK FROM THE
  19. // GENERATED PDF DOCUMENTS.
  20. //
  21. // TCPDF is distributed in the hope that it will be useful, but
  22. // WITHOUT ANY WARRANTY; without even the implied warranty of
  23. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  24. // See the GNU Lesser General Public License for more details.
  25. //
  26. // You should have received a copy of the License
  27. // along with TCPDF. If not, see
  28. // <http://www.tecnick.com/pagefiles/tcpdf/LICENSE.TXT>.
  29. //
  30. // See LICENSE.TXT file for more information.
  31. // -------------------------------------------------------------------
  32. //
  33. // Description :
  34. // This is a PHP class for generating PDF documents without requiring external extensions.
  35. //
  36. // NOTE:
  37. // This class was originally derived in 2002 from the Public
  38. // Domain FPDF class by Olivier Plathey (http://www.fpdf.org),
  39. // but now is almost entirely rewritten and contains thousands of
  40. // new lines of code and hundreds new features.
  41. //
  42. // Main features:
  43. // * no external libraries are required for the basic functions;
  44. // * all standard page formats, custom page formats, custom margins and units of measure;
  45. // * UTF-8 Unicode and Right-To-Left languages;
  46. // * TrueTypeUnicode, TrueType, Type1 and CID-0 fonts;
  47. // * font subsetting;
  48. // * methods to publish some XHTML + CSS code, Javascript and Forms;
  49. // * images, graphic (geometric figures) and transformation methods;
  50. // * 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)
  51. // * 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;
  52. // * JPEG and PNG ICC profiles, Grayscale, RGB, CMYK, Spot Colors and Transparencies;
  53. // * automatic page header and footer management;
  54. // * document encryption up to 256 bit and digital signature certifications;
  55. // * transactions to UNDO commands;
  56. // * PDF annotations, including links, text and file attachments;
  57. // * text rendering modes (fill, stroke and clipping);
  58. // * multiple columns mode;
  59. // * no-write page regions;
  60. // * bookmarks, named destinations and table of content;
  61. // * text hyphenation;
  62. // * text stretching and spacing (tracking/kerning);
  63. // * automatic page break, line break and text alignments including justification;
  64. // * automatic page numbering and page groups;
  65. // * move and delete pages;
  66. // * page compression (requires php-zlib extension);
  67. // * XOBject Templates;
  68. // * Layers and object visibility.
  69. // * PDF/A-1b support.
  70. //
  71. // -----------------------------------------------------------
  72. // THANKS TO:
  73. //
  74. // Olivier Plathey (http://www.fpdf.org) for original FPDF.
  75. // Efthimios Mavrogeorgiadis (emavro@yahoo.com) for suggestions on RTL language support.
  76. // Klemen Vodopivec (http://www.fpdf.de/downloads/addons/37/) for Encryption algorithm.
  77. // Warren Sherliker (wsherliker@gmail.com) for better image handling.
  78. // dullus for text Justification.
  79. // Bob Vincent (pillarsdotnet@users.sourceforge.net) for <li> value attribute.
  80. // Patrick Benny for text stretch suggestion on Cell().
  81. // Johannes Güntert for JavaScript support.
  82. // Denis Van Nuffelen for Dynamic Form.
  83. // Jacek Czekaj for multibyte justification
  84. // Anthony Ferrara for the reintroduction of legacy image methods.
  85. // Sourceforge user 1707880 (hucste) for line-trough mode.
  86. // Larry Stanbery for page groups.
  87. // Martin Hall-May for transparency.
  88. // Aaron C. Spike for Polycurve method.
  89. // Mohamad Ali Golkar, Saleh AlMatrafe, Charles Abbott for Arabic and Persian support.
  90. // Moritz Wagner and Andreas Wurmser for graphic functions.
  91. // Andrew Whitehead for core fonts support.
  92. // Esteban Joël Marín for OpenType font conversion.
  93. // Teus Hagen for several suggestions and fixes.
  94. // Yukihiro Nakadaira for CID-0 CJK fonts fixes.
  95. // Kosmas Papachristos for some CSS improvements.
  96. // Marcel Partap for some fixes.
  97. // Won Kyu Park for several suggestions, fixes and patches.
  98. // Dominik Dzienia for QR-code support.
  99. // Laurent Minguet for some suggestions.
  100. // Christian Deligant for some suggestions and fixes.
  101. // Travis Harris for crop mark suggestion.
  102. // Anyone that has reported a bug or sent a suggestion.
  103. //============================================================+
  104. /**
  105. * @file
  106. * This is a PHP class for generating PDF documents without requiring external extensions.<br>
  107. * 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>
  108. * <h3>TCPDF main features are:</h3>
  109. * <ul>
  110. * <li>no external libraries are required for the basic functions;</li>
  111. * <li>all standard page formats, custom page formats, custom margins and units of measure;</li>
  112. * <li>UTF-8 Unicode and Right-To-Left languages;</li>
  113. * <li>TrueTypeUnicode, TrueType, Type1 and CID-0 fonts;</li>
  114. * <li>font subsetting;</li>
  115. * <li>methods to publish some XHTML + CSS code, Javascript and Forms;</li>
  116. * <li>images, graphic (geometric figures) and transformation methods;
  117. * <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>
  118. * <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>
  119. * <li>JPEG and PNG ICC profiles, Grayscale, RGB, CMYK, Spot Colors and Transparencies;</li>
  120. * <li>automatic page header and footer management;</li>
  121. * <li>document encryption up to 256 bit and digital signature certifications;</li>
  122. * <li>transactions to UNDO commands;</li>
  123. * <li>PDF annotations, including links, text and file attachments;</li>
  124. * <li>text rendering modes (fill, stroke and clipping);</li>
  125. * <li>multiple columns mode;</li>
  126. * <li>no-write page regions;</li>
  127. * <li>bookmarks, named destinations and table of content;</li>
  128. * <li>text hyphenation;</li>
  129. * <li>text stretching and spacing (tracking/kerning);</li>
  130. * <li>automatic page break, line break and text alignments including justification;</li>
  131. * <li>automatic page numbering and page groups;</li>
  132. * <li>move and delete pages;</li>
  133. * <li>page compression (requires php-zlib extension);</li>
  134. * <li>XOBject Templates;</li>
  135. * <li>Layers and object visibility;</li>
  136. * <li>PDF/A-1b support.</li>
  137. * </ul>
  138. * Tools to encode your unicode fonts are on fonts/utils directory.</p>
  139. * @package com.tecnick.tcpdf
  140. * @author Nicola Asuni
  141. * @version 5.9.144
  142. */
  143. // Main configuration file. Define the K_TCPDF_EXTERNAL_CONFIG constant to skip this file.
  144. require_once(dirname(__FILE__).'/config/tcpdf_config.php');
  145. /**
  146. * @class TCPDF
  147. * PHP class for generating PDF documents without requiring external extensions.
  148. * 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>
  149. * @package com.tecnick.tcpdf
  150. * @brief PHP class for generating PDF documents without requiring external extensions.
  151. * @version 5.9.144
  152. * @author Nicola Asuni - info@tecnick.com
  153. */
  154. class TCPDF {
  155. // private properties
  156. /**
  157. * Current TCPDF version.
  158. * @private
  159. */
  160. private $tcpdf_version = '5.9.144';
  161. // Protected properties
  162. /**
  163. * Current page number.
  164. * @protected
  165. */
  166. protected $page;
  167. /**
  168. * Current object number.
  169. * @protected
  170. */
  171. protected $n;
  172. /**
  173. * Array of object offsets.
  174. * @protected
  175. */
  176. protected $offsets;
  177. /**
  178. * Buffer holding in-memory PDF.
  179. * @protected
  180. */
  181. protected $buffer;
  182. /**
  183. * Array containing pages.
  184. * @protected
  185. */
  186. protected $pages = array();
  187. /**
  188. * Current document state.
  189. * @protected
  190. */
  191. protected $state;
  192. /**
  193. * Compression flag.
  194. * @protected
  195. */
  196. protected $compress;
  197. /**
  198. * Current page orientation (P = Portrait, L = Landscape).
  199. * @protected
  200. */
  201. protected $CurOrientation;
  202. /**
  203. * Page dimensions.
  204. * @protected
  205. */
  206. protected $pagedim = array();
  207. /**
  208. * Scale factor (number of points in user unit).
  209. * @protected
  210. */
  211. protected $k;
  212. /**
  213. * Width of page format in points.
  214. * @protected
  215. */
  216. protected $fwPt;
  217. /**
  218. * Height of page format in points.
  219. * @protected
  220. */
  221. protected $fhPt;
  222. /**
  223. * Current width of page in points.
  224. * @protected
  225. */
  226. protected $wPt;
  227. /**
  228. * Current height of page in points.
  229. * @protected
  230. */
  231. protected $hPt;
  232. /**
  233. * Current width of page in user unit.
  234. * @protected
  235. */
  236. protected $w;
  237. /**
  238. * Current height of page in user unit.
  239. * @protected
  240. */
  241. protected $h;
  242. /**
  243. * Left margin.
  244. * @protected
  245. */
  246. protected $lMargin;
  247. /**
  248. * Top margin.
  249. * @protected
  250. */
  251. protected $tMargin;
  252. /**
  253. * Right margin.
  254. * @protected
  255. */
  256. protected $rMargin;
  257. /**
  258. * Page break margin.
  259. * @protected
  260. */
  261. protected $bMargin;
  262. /**
  263. * Array of cell internal paddings ('T' => top, 'R' => right, 'B' => bottom, 'L' => left).
  264. * @since 5.9.000 (2010-10-03)
  265. * @protected
  266. */
  267. protected $cell_padding = array('T' => 0, 'R' => 0, 'B' => 0, 'L' => 0);
  268. /**
  269. * Array of cell margins ('T' => top, 'R' => right, 'B' => bottom, 'L' => left).
  270. * @since 5.9.000 (2010-10-04)
  271. * @protected
  272. */
  273. protected $cell_margin = array('T' => 0, 'R' => 0, 'B' => 0, 'L' => 0);
  274. /**
  275. * Current horizontal position in user unit for cell positioning.
  276. * @protected
  277. */
  278. protected $x;
  279. /**
  280. * Current vertical position in user unit for cell positioning.
  281. * @protected
  282. */
  283. protected $y;
  284. /**
  285. * Height of last cell printed.
  286. * @protected
  287. */
  288. protected $lasth;
  289. /**
  290. * Line width in user unit.
  291. * @protected
  292. */
  293. protected $LineWidth;
  294. /**
  295. * Array of standard font names.
  296. * @protected
  297. */
  298. protected $CoreFonts;
  299. /**
  300. * Array of used fonts.
  301. * @protected
  302. */
  303. protected $fonts = array();
  304. /**
  305. * Array of font files.
  306. * @protected
  307. */
  308. protected $FontFiles = array();
  309. /**
  310. * Array of encoding differences.
  311. * @protected
  312. */
  313. protected $diffs = array();
  314. /**
  315. * Array of used images.
  316. * @protected
  317. */
  318. protected $images = array();
  319. /**
  320. * Array of Annotations in pages.
  321. * @protected
  322. */
  323. protected $PageAnnots = array();
  324. /**
  325. * Array of internal links.
  326. * @protected
  327. */
  328. protected $links = array();
  329. /**
  330. * Current font family.
  331. * @protected
  332. */
  333. protected $FontFamily;
  334. /**
  335. * Current font style.
  336. * @protected
  337. */
  338. protected $FontStyle;
  339. /**
  340. * Current font ascent (distance between font top and baseline).
  341. * @protected
  342. * @since 2.8.000 (2007-03-29)
  343. */
  344. protected $FontAscent;
  345. /**
  346. * Current font descent (distance between font bottom and baseline).
  347. * @protected
  348. * @since 2.8.000 (2007-03-29)
  349. */
  350. protected $FontDescent;
  351. /**
  352. * Underlining flag.
  353. * @protected
  354. */
  355. protected $underline;
  356. /**
  357. * Overlining flag.
  358. * @protected
  359. */
  360. protected $overline;
  361. /**
  362. * Current font info.
  363. * @protected
  364. */
  365. protected $CurrentFont;
  366. /**
  367. * Current font size in points.
  368. * @protected
  369. */
  370. protected $FontSizePt;
  371. /**
  372. * Current font size in user unit.
  373. * @protected
  374. */
  375. protected $FontSize;
  376. /**
  377. * Commands for drawing color.
  378. * @protected
  379. */
  380. protected $DrawColor;
  381. /**
  382. * Commands for filling color.
  383. * @protected
  384. */
  385. protected $FillColor;
  386. /**
  387. * Commands for text color.
  388. * @protected
  389. */
  390. protected $TextColor;
  391. /**
  392. * Indicates whether fill and text colors are different.
  393. * @protected
  394. */
  395. protected $ColorFlag;
  396. /**
  397. * Automatic page breaking.
  398. * @protected
  399. */
  400. protected $AutoPageBreak;
  401. /**
  402. * Threshold used to trigger page breaks.
  403. * @protected
  404. */
  405. protected $PageBreakTrigger;
  406. /**
  407. * Flag set when processing page header.
  408. * @protected
  409. */
  410. protected $InHeader = false;
  411. /**
  412. * Flag set when processing page footer.
  413. * @protected
  414. */
  415. protected $InFooter = false;
  416. /**
  417. * Zoom display mode.
  418. * @protected
  419. */
  420. protected $ZoomMode;
  421. /**
  422. * Layout display mode.
  423. * @protected
  424. */
  425. protected $LayoutMode;
  426. /**
  427. * If true set the document information dictionary in Unicode.
  428. * @protected
  429. */
  430. protected $docinfounicode = true;
  431. /**
  432. * Document title.
  433. * @protected
  434. */
  435. protected $title = '';
  436. /**
  437. * Document subject.
  438. * @protected
  439. */
  440. protected $subject = '';
  441. /**
  442. * Document author.
  443. * @protected
  444. */
  445. protected $author = '';
  446. /**
  447. * Document keywords.
  448. * @protected
  449. */
  450. protected $keywords = '';
  451. /**
  452. * Document creator.
  453. * @protected
  454. */
  455. protected $creator = '';
  456. /**
  457. * Starting page number.
  458. * @protected
  459. */
  460. protected $starting_page_number = 1;
  461. /**
  462. * String alias for total number of pages.
  463. * @protected
  464. */
  465. protected $alias_tot_pages = '{:ptp:}';
  466. /**
  467. * String alias for page number.
  468. * @protected
  469. */
  470. protected $alias_num_page = '{:pnp:}';
  471. /**
  472. * String alias for total number of pages in a single group.
  473. * @protected
  474. */
  475. protected $alias_group_tot_pages = '{:ptg:}';
  476. /**
  477. * String alias for group page number.
  478. * @protected
  479. */
  480. protected $alias_group_num_page = '{:png:}';
  481. /**
  482. * String alias for right shift compensation used to correctly align page numbers on the right.
  483. * @protected
  484. */
  485. protected $alias_right_shift = '{rsc:';
  486. /**
  487. * The right-bottom (or left-bottom for RTL) corner X coordinate of last inserted image.
  488. * @since 2002-07-31
  489. * @author Nicola Asuni
  490. * @protected
  491. */
  492. protected $img_rb_x;
  493. /**
  494. * The right-bottom corner Y coordinate of last inserted image.
  495. * @since 2002-07-31
  496. * @author Nicola Asuni
  497. * @protected
  498. */
  499. protected $img_rb_y;
  500. /**
  501. * Adjusting factor to convert pixels to user units.
  502. * @since 2004-06-14
  503. * @author Nicola Asuni
  504. * @protected
  505. */
  506. protected $imgscale = 1;
  507. /**
  508. * Boolean flag set to true when the input text is unicode (require unicode fonts).
  509. * @since 2005-01-02
  510. * @author Nicola Asuni
  511. * @protected
  512. */
  513. protected $isunicode = false;
  514. /**
  515. * Object containing unicode data.
  516. * @since 5.9.004 (2010-10-18)
  517. * @author Nicola Asuni
  518. * @protected
  519. */
  520. protected $unicode;
  521. /**
  522. * Object containing font encoding maps.
  523. * @since 5.9.123 (2011-10-01)
  524. * @author Nicola Asuni
  525. * @protected
  526. */
  527. protected $encmaps;
  528. /**
  529. * PDF version.
  530. * @since 1.5.3
  531. * @protected
  532. */
  533. protected $PDFVersion = '1.7';
  534. /**
  535. * ID of the stored default header template (-1 = not set).
  536. * @protected
  537. */
  538. protected $header_xobjid = -1;
  539. /**
  540. * If true reset the Header Xobject template at each page
  541. * @protected
  542. */
  543. protected $header_xobj_autoreset = false;
  544. /**
  545. * Minimum distance between header and top page margin.
  546. * @protected
  547. */
  548. protected $header_margin;
  549. /**
  550. * Minimum distance between footer and bottom page margin.
  551. * @protected
  552. */
  553. protected $footer_margin;
  554. /**
  555. * Original left margin value.
  556. * @protected
  557. * @since 1.53.0.TC013
  558. */
  559. protected $original_lMargin;
  560. /**
  561. * Original right margin value.
  562. * @protected
  563. * @since 1.53.0.TC013
  564. */
  565. protected $original_rMargin;
  566. /**
  567. * Default font used on page header.
  568. * @protected
  569. */
  570. protected $header_font;
  571. /**
  572. * Default font used on page footer.
  573. * @protected
  574. */
  575. protected $footer_font;
  576. /**
  577. * Language templates.
  578. * @protected
  579. */
  580. protected $l;
  581. /**
  582. * Barcode to print on page footer (only if set).
  583. * @protected
  584. */
  585. protected $barcode = false;
  586. /**
  587. * Boolean flag to print/hide page header.
  588. * @protected
  589. */
  590. protected $print_header = true;
  591. /**
  592. * Boolean flag to print/hide page footer.
  593. * @protected
  594. */
  595. protected $print_footer = true;
  596. /**
  597. * Header image logo.
  598. * @protected
  599. */
  600. protected $header_logo = '';
  601. /**
  602. * Width of header image logo in user units.
  603. * @protected
  604. */
  605. protected $header_logo_width = 30;
  606. /**
  607. * Title to be printed on default page header.
  608. * @protected
  609. */
  610. protected $header_title = '';
  611. /**
  612. * String to pring on page header after title.
  613. * @protected
  614. */
  615. protected $header_string = '';
  616. /**
  617. * Default number of columns for html table.
  618. * @protected
  619. */
  620. protected $default_table_columns = 4;
  621. // variables for html parser
  622. /**
  623. * HTML PARSER: array to store current link and rendering styles.
  624. * @protected
  625. */
  626. protected $HREF = array();
  627. /**
  628. * List of available fonts on filesystem.
  629. * @protected
  630. */
  631. protected $fontlist = array();
  632. /**
  633. * Current foreground color.
  634. * @protected
  635. */
  636. protected $fgcolor;
  637. /**
  638. * HTML PARSER: array of boolean values, true in case of ordered list (OL), false otherwise.
  639. * @protected
  640. */
  641. protected $listordered = array();
  642. /**
  643. * HTML PARSER: array count list items on nested lists.
  644. * @protected
  645. */
  646. protected $listcount = array();
  647. /**
  648. * HTML PARSER: current list nesting level.
  649. * @protected
  650. */
  651. protected $listnum = 0;
  652. /**
  653. * HTML PARSER: indent amount for lists.
  654. * @protected
  655. */
  656. protected $listindent = 0;
  657. /**
  658. * HTML PARSER: current list indententation level.
  659. * @protected
  660. */
  661. protected $listindentlevel = 0;
  662. /**
  663. * Current background color.
  664. * @protected
  665. */
  666. protected $bgcolor;
  667. /**
  668. * Temporary font size in points.
  669. * @protected
  670. */
  671. protected $tempfontsize = 10;
  672. /**
  673. * Spacer string for LI tags.
  674. * @protected
  675. */
  676. protected $lispacer = '';
  677. /**
  678. * Default encoding.
  679. * @protected
  680. * @since 1.53.0.TC010
  681. */
  682. protected $encoding = 'UTF-8';
  683. /**
  684. * PHP internal encoding.
  685. * @protected
  686. * @since 1.53.0.TC016
  687. */
  688. protected $internal_encoding;
  689. /**
  690. * Boolean flag to indicate if the document language is Right-To-Left.
  691. * @protected
  692. * @since 2.0.000
  693. */
  694. protected $rtl = false;
  695. /**
  696. * Boolean flag used to force RTL or LTR string direction.
  697. * @protected
  698. * @since 2.0.000
  699. */
  700. protected $tmprtl = false;
  701. // --- Variables used for document encryption:
  702. /**
  703. * IBoolean flag indicating whether document is protected.
  704. * @protected
  705. * @since 2.0.000 (2008-01-02)
  706. */
  707. protected $encrypted;
  708. /**
  709. * Array containing encryption settings.
  710. * @protected
  711. * @since 5.0.005 (2010-05-11)
  712. */
  713. protected $encryptdata = array();
  714. /**
  715. * Last RC4 key encrypted (cached for optimisation).
  716. * @protected
  717. * @since 2.0.000 (2008-01-02)
  718. */
  719. protected $last_enc_key;
  720. /**
  721. * Last RC4 computed key.
  722. * @protected
  723. * @since 2.0.000 (2008-01-02)
  724. */
  725. protected $last_enc_key_c;
  726. /**
  727. * Encryption padding string.
  728. * @protected
  729. */
  730. 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";
  731. /**
  732. * File ID (used on document trailer).
  733. * @protected
  734. * @since 5.0.005 (2010-05-12)
  735. */
  736. protected $file_id;
  737. // --- bookmark ---
  738. /**
  739. * Outlines for bookmark.
  740. * @protected
  741. * @since 2.1.002 (2008-02-12)
  742. */
  743. protected $outlines = array();
  744. /**
  745. * Outline root for bookmark.
  746. * @protected
  747. * @since 2.1.002 (2008-02-12)
  748. */
  749. protected $OutlineRoot;
  750. // --- javascript and form ---
  751. /**
  752. * Javascript code.
  753. * @protected
  754. * @since 2.1.002 (2008-02-12)
  755. */
  756. protected $javascript = '';
  757. /**
  758. * Javascript counter.
  759. * @protected
  760. * @since 2.1.002 (2008-02-12)
  761. */
  762. protected $n_js;
  763. /**
  764. * line trough state
  765. * @protected
  766. * @since 2.8.000 (2008-03-19)
  767. */
  768. protected $linethrough;
  769. /**
  770. * Array with additional document-wide usage rights for the document.
  771. * @protected
  772. * @since 5.8.014 (2010-08-23)
  773. */
  774. protected $ur = array();
  775. /**
  776. * DPI (Dot Per Inch) Document Resolution (do not change).
  777. * @protected
  778. * @since 3.0.000 (2008-03-27)
  779. */
  780. protected $dpi = 72;
  781. /**
  782. * Array of page numbers were a new page group was started (the page numbers are the keys of the array).
  783. * @protected
  784. * @since 3.0.000 (2008-03-27)
  785. */
  786. protected $newpagegroup = array();
  787. /**
  788. * Array that contains the number of pages in each page group.
  789. * @protected
  790. * @since 3.0.000 (2008-03-27)
  791. */
  792. protected $pagegroups = array();
  793. /**
  794. * Current page group number.
  795. * @protected
  796. * @since 3.0.000 (2008-03-27)
  797. */
  798. protected $currpagegroup = 0;
  799. /**
  800. * Array of transparency objects and parameters.
  801. * @protected
  802. * @since 3.0.000 (2008-03-27)
  803. */
  804. protected $extgstates;
  805. /**
  806. * Set the default JPEG compression quality (1-100).
  807. * @protected
  808. * @since 3.0.000 (2008-03-27)
  809. */
  810. protected $jpeg_quality;
  811. /**
  812. * Default cell height ratio.
  813. * @protected
  814. * @since 3.0.014 (2008-05-23)
  815. */
  816. protected $cell_height_ratio = K_CELL_HEIGHT_RATIO;
  817. /**
  818. * PDF viewer preferences.
  819. * @protected
  820. * @since 3.1.000 (2008-06-09)
  821. */
  822. protected $viewer_preferences;
  823. /**
  824. * A name object specifying how the document should be displayed when opened.
  825. * @protected
  826. * @since 3.1.000 (2008-06-09)
  827. */
  828. protected $PageMode;
  829. /**
  830. * Array for storing gradient information.
  831. * @protected
  832. * @since 3.1.000 (2008-06-09)
  833. */
  834. protected $gradients = array();
  835. /**
  836. * Array used to store positions inside the pages buffer (keys are the page numbers).
  837. * @protected
  838. * @since 3.2.000 (2008-06-26)
  839. */
  840. protected $intmrk = array();
  841. /**
  842. * Array used to store positions inside the pages buffer (keys are the page numbers).
  843. * @protected
  844. * @since 5.7.000 (2010-08-03)
  845. */
  846. protected $bordermrk = array();
  847. /**
  848. * Array used to store page positions to track empty pages (keys are the page numbers).
  849. * @protected
  850. * @since 5.8.007 (2010-08-18)
  851. */
  852. protected $emptypagemrk = array();
  853. /**
  854. * Array used to store content positions inside the pages buffer (keys are the page numbers).
  855. * @protected
  856. * @since 4.6.021 (2009-07-20)
  857. */
  858. protected $cntmrk = array();
  859. /**
  860. * Array used to store footer positions of each page.
  861. * @protected
  862. * @since 3.2.000 (2008-07-01)
  863. */
  864. protected $footerpos = array();
  865. /**
  866. * Array used to store footer length of each page.
  867. * @protected
  868. * @since 4.0.014 (2008-07-29)
  869. */
  870. protected $footerlen = array();
  871. /**
  872. * Boolean flag to indicate if a new line is created.
  873. * @protected
  874. * @since 3.2.000 (2008-07-01)
  875. */
  876. protected $newline = true;
  877. /**
  878. * End position of the latest inserted line.
  879. * @protected
  880. * @since 3.2.000 (2008-07-01)
  881. */
  882. protected $endlinex = 0;
  883. /**
  884. * PDF string for width value of the last line.
  885. * @protected
  886. * @since 4.0.006 (2008-07-16)
  887. */
  888. protected $linestyleWidth = '';
  889. /**
  890. * PDF string for CAP value of the last line.
  891. * @protected
  892. * @since 4.0.006 (2008-07-16)
  893. */
  894. protected $linestyleCap = '0 J';
  895. /**
  896. * PDF string for join value of the last line.
  897. * @protected
  898. * @since 4.0.006 (2008-07-16)
  899. */
  900. protected $linestyleJoin = '0 j';
  901. /**
  902. * PDF string for dash value of the last line.
  903. * @protected
  904. * @since 4.0.006 (2008-07-16)
  905. */
  906. protected $linestyleDash = '[] 0 d';
  907. /**
  908. * Boolean flag to indicate if marked-content sequence is open.
  909. * @protected
  910. * @since 4.0.013 (2008-07-28)
  911. */
  912. protected $openMarkedContent = false;
  913. /**
  914. * Count the latest inserted vertical spaces on HTML.
  915. * @protected
  916. * @since 4.0.021 (2008-08-24)
  917. */
  918. protected $htmlvspace = 0;
  919. /**
  920. * Array of Spot colors.
  921. * @protected
  922. * @since 4.0.024 (2008-09-12)
  923. */
  924. protected $spot_colors = array();
  925. /**
  926. * Symbol used for HTML unordered list items.
  927. * @protected
  928. * @since 4.0.028 (2008-09-26)
  929. */
  930. protected $lisymbol = '';
  931. /**
  932. * String used to mark the beginning and end of EPS image blocks.
  933. * @protected
  934. * @since 4.1.000 (2008-10-18)
  935. */
  936. protected $epsmarker = 'x#!#EPS#!#x';
  937. /**
  938. * Array of transformation matrix.
  939. * @protected
  940. * @since 4.2.000 (2008-10-29)
  941. */
  942. protected $transfmatrix = array();
  943. /**
  944. * Current key for transformation matrix.
  945. * @protected
  946. * @since 4.8.005 (2009-09-17)
  947. */
  948. protected $transfmatrix_key = 0;
  949. /**
  950. * Booklet mode for double-sided pages.
  951. * @protected
  952. * @since 4.2.000 (2008-10-29)
  953. */
  954. protected $booklet = false;
  955. /**
  956. * Epsilon value used for float calculations.
  957. * @protected
  958. * @since 4.2.000 (2008-10-29)
  959. */
  960. protected $feps = 0.005;
  961. /**
  962. * Array used for custom vertical spaces for HTML tags.
  963. * @protected
  964. * @since 4.2.001 (2008-10-30)
  965. */
  966. protected $tagvspaces = array();
  967. /**
  968. * HTML PARSER: custom indent amount for lists. Negative value means disabled.
  969. * @protected
  970. * @since 4.2.007 (2008-11-12)
  971. */
  972. protected $customlistindent = -1;
  973. /**
  974. * Boolean flag to indicate if the border of the cell sides that cross the page should be removed.
  975. * @protected
  976. * @since 4.2.010 (2008-11-14)
  977. */
  978. protected $opencell = true;
  979. /**
  980. * Array of files to embedd.
  981. * @protected
  982. * @since 4.4.000 (2008-12-07)
  983. */
  984. protected $embeddedfiles = array();
  985. /**
  986. * Boolean flag to indicate if we are inside a PRE tag.
  987. * @protected
  988. * @since 4.4.001 (2008-12-08)
  989. */
  990. protected $premode = false;
  991. /**
  992. * Array used to store positions of graphics transformation blocks inside the page buffer.
  993. * keys are the page numbers
  994. * @protected
  995. * @since 4.4.002 (2008-12-09)
  996. */
  997. protected $transfmrk = array();
  998. /**
  999. * Default color for html links.
  1000. * @protected
  1001. * @since 4.4.003 (2008-12-09)
  1002. */
  1003. protected $htmlLinkColorArray = array(0, 0, 255);
  1004. /**
  1005. * Default font style to add to html links.
  1006. * @protected
  1007. * @since 4.4.003 (2008-12-09)
  1008. */
  1009. protected $htmlLinkFontStyle = 'U';
  1010. /**
  1011. * Counts the number of pages.
  1012. * @protected
  1013. * @since 4.5.000 (2008-12-31)
  1014. */
  1015. protected $numpages = 0;
  1016. /**
  1017. * Array containing page lengths in bytes.
  1018. * @protected
  1019. * @since 4.5.000 (2008-12-31)
  1020. */
  1021. protected $pagelen = array();
  1022. /**
  1023. * Counts the number of pages.
  1024. * @protected
  1025. * @since 4.5.000 (2008-12-31)
  1026. */
  1027. protected $numimages = 0;
  1028. /**
  1029. * Store the image keys.
  1030. * @protected
  1031. * @since 4.5.000 (2008-12-31)
  1032. */
  1033. protected $imagekeys = array();
  1034. /**
  1035. * Length of the buffer in bytes.
  1036. * @protected
  1037. * @since 4.5.000 (2008-12-31)
  1038. */
  1039. protected $bufferlen = 0;
  1040. /**
  1041. * If true enables disk caching.
  1042. * @protected
  1043. * @since 4.5.000 (2008-12-31)
  1044. */
  1045. protected $diskcache = false;
  1046. /**
  1047. * Counts the number of fonts.
  1048. * @protected
  1049. * @since 4.5.000 (2009-01-02)
  1050. */
  1051. protected $numfonts = 0;
  1052. /**
  1053. * Store the font keys.
  1054. * @protected
  1055. * @since 4.5.000 (2009-01-02)
  1056. */
  1057. protected $fontkeys = array();
  1058. /**
  1059. * Store the font object IDs.
  1060. * @protected
  1061. * @since 4.8.001 (2009-09-09)
  1062. */
  1063. protected $font_obj_ids = array();
  1064. /**
  1065. * Store the fage status (true when opened, false when closed).
  1066. * @protected
  1067. * @since 4.5.000 (2009-01-02)
  1068. */
  1069. protected $pageopen = array();
  1070. /**
  1071. * Default monospace font.
  1072. * @protected
  1073. * @since 4.5.025 (2009-03-10)
  1074. */
  1075. protected $default_monospaced_font = 'courier';
  1076. /**
  1077. * Cloned copy of the current class object.
  1078. * @protected
  1079. * @since 4.5.029 (2009-03-19)
  1080. */
  1081. protected $objcopy;
  1082. /**
  1083. * Array used to store the lengths of cache files.
  1084. * @protected
  1085. * @since 4.5.029 (2009-03-19)
  1086. */
  1087. protected $cache_file_length = array();
  1088. /**
  1089. * Table header content to be repeated on each new page.
  1090. * @protected
  1091. * @since 4.5.030 (2009-03-20)
  1092. */
  1093. protected $thead = '';
  1094. /**
  1095. * Margins used for table header.
  1096. * @protected
  1097. * @since 4.5.030 (2009-03-20)
  1098. */
  1099. protected $theadMargins = array();
  1100. /**
  1101. * Cache array for UTF8StringToArray() method.
  1102. * @protected
  1103. * @since 4.5.037 (2009-04-07)
  1104. */
  1105. protected $cache_UTF8StringToArray = array();
  1106. /**
  1107. * Maximum size of cache array used for UTF8StringToArray() method.
  1108. * @protected
  1109. * @since 4.5.037 (2009-04-07)
  1110. */
  1111. protected $cache_maxsize_UTF8StringToArray = 8;
  1112. /**
  1113. * Current size of cache array used for UTF8StringToArray() method.
  1114. * @protected
  1115. * @since 4.5.037 (2009-04-07)
  1116. */
  1117. protected $cache_size_UTF8StringToArray = 0;
  1118. /**
  1119. * Boolean flag to enable document digital signature.
  1120. * @protected
  1121. * @since 4.6.005 (2009-04-24)
  1122. */
  1123. protected $sign = false;
  1124. /**
  1125. * Digital signature data.
  1126. * @protected
  1127. * @since 4.6.005 (2009-04-24)
  1128. */
  1129. protected $signature_data = array();
  1130. /**
  1131. * Digital signature max length.
  1132. * @protected
  1133. * @since 4.6.005 (2009-04-24)
  1134. */
  1135. protected $signature_max_length = 11742;
  1136. /**
  1137. * Data for digital signature appearance.
  1138. * @protected
  1139. * @since 5.3.011 (2010-06-16)
  1140. */
  1141. protected $signature_appearance = array('page' => 1, 'rect' => '0 0 0 0');
  1142. /**
  1143. * Array of empty digital signature appearances.
  1144. * @protected
  1145. * @since 5.9.101 (2011-07-06)
  1146. */
  1147. protected $empty_signature_appearance = array();
  1148. /**
  1149. * Regular expression used to find blank characters (required for word-wrapping).
  1150. * @protected
  1151. * @since 4.6.006 (2009-04-28)
  1152. */
  1153. protected $re_spaces = '/[^\S\xa0]/';
  1154. /**
  1155. * Array of $re_spaces parts.
  1156. * @protected
  1157. * @since 5.5.011 (2010-07-09)
  1158. */
  1159. protected $re_space = array('p' => '[^\S\xa0]', 'm' => '');
  1160. /**
  1161. * Digital signature object ID.
  1162. * @protected
  1163. * @since 4.6.022 (2009-06-23)
  1164. */
  1165. protected $sig_obj_id = 0;
  1166. /**
  1167. * ByteRange placemark used during digital signature process.
  1168. * @protected
  1169. * @since 4.6.028 (2009-08-25)
  1170. */
  1171. protected $byterange_string = '/ByteRange[0 ********** ********** **********]';
  1172. /**
  1173. * Placemark used during digital signature process.
  1174. * @protected
  1175. * @since 4.6.028 (2009-08-25)
  1176. */
  1177. protected $sig_annot_ref = '***SIGANNREF*** 0 R';
  1178. /**
  1179. * ID of page objects.
  1180. * @protected
  1181. * @since 4.7.000 (2009-08-29)
  1182. */
  1183. protected $page_obj_id = array();
  1184. /**
  1185. * List of form annotations IDs.
  1186. * @protected
  1187. * @since 4.8.000 (2009-09-07)
  1188. */
  1189. protected $form_obj_id = array();
  1190. /**
  1191. * 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.
  1192. * @protected
  1193. * @since 4.8.000 (2009-09-07)
  1194. */
  1195. protected $default_form_prop = array('lineWidth'=>1, 'borderStyle'=>'solid', 'fillColor'=>array(255, 255, 255), 'strokeColor'=>array(128, 128, 128));
  1196. /**
  1197. * Javascript objects array.
  1198. * @protected
  1199. * @since 4.8.000 (2009-09-07)
  1200. */
  1201. protected $js_objects = array();
  1202. /**
  1203. * Current form action (used during XHTML rendering).
  1204. * @protected
  1205. * @since 4.8.000 (2009-09-07)
  1206. */
  1207. protected $form_action = '';
  1208. /**
  1209. * Current form encryption type (used during XHTML rendering).
  1210. * @protected
  1211. * @since 4.8.000 (2009-09-07)
  1212. */
  1213. protected $form_enctype = 'application/x-www-form-urlencoded';
  1214. /**
  1215. * Current method to submit forms.
  1216. * @protected
  1217. * @since 4.8.000 (2009-09-07)
  1218. */
  1219. protected $form_mode = 'post';
  1220. /**
  1221. * List of fonts used on form fields (fontname => fontkey).
  1222. * @protected
  1223. * @since 4.8.001 (2009-09-09)
  1224. */
  1225. protected $annotation_fonts = array();
  1226. /**
  1227. * List of radio buttons parent objects.
  1228. * @protected
  1229. * @since 4.8.001 (2009-09-09)
  1230. */
  1231. protected $radiobutton_groups = array();
  1232. /**
  1233. * List of radio group objects IDs.
  1234. * @protected
  1235. * @since 4.8.001 (2009-09-09)
  1236. */
  1237. protected $radio_groups = array();
  1238. /**
  1239. * Text indentation value (used for text-indent CSS attribute).
  1240. * @protected
  1241. * @since 4.8.006 (2009-09-23)
  1242. */
  1243. protected $textindent = 0;
  1244. /**
  1245. * Store page number when startTransaction() is called.
  1246. * @protected
  1247. * @since 4.8.006 (2009-09-23)
  1248. */
  1249. protected $start_transaction_page = 0;
  1250. /**
  1251. * Store Y position when startTransaction() is called.
  1252. * @protected
  1253. * @since 4.9.001 (2010-03-28)
  1254. */
  1255. protected $start_transaction_y = 0;
  1256. /**
  1257. * True when we are printing the thead section on a new page.
  1258. * @protected
  1259. * @since 4.8.027 (2010-01-25)
  1260. */
  1261. protected $inthead = false;
  1262. /**
  1263. * Array of column measures (width, space, starting Y position).
  1264. * @protected
  1265. * @since 4.9.001 (2010-03-28)
  1266. */
  1267. protected $columns = array();
  1268. /**
  1269. * Number of colums.
  1270. * @protected
  1271. * @since 4.9.001 (2010-03-28)
  1272. */
  1273. protected $num_columns = 1;
  1274. /**
  1275. * Current column number.
  1276. * @protected
  1277. * @since 4.9.001 (2010-03-28)
  1278. */
  1279. protected $current_column = 0;
  1280. /**
  1281. * Starting page for columns.
  1282. * @protected
  1283. * @since 4.9.001 (2010-03-28)
  1284. */
  1285. protected $column_start_page = 0;
  1286. /**
  1287. * Maximum page and column selected.
  1288. * @protected
  1289. * @since 5.8.000 (2010-08-11)
  1290. */
  1291. protected $maxselcol = array('page' => 0, 'column' => 0);
  1292. /**
  1293. * Array of: X difference between table cell x start and starting page margin, cellspacing, cellpadding.
  1294. * @protected
  1295. * @since 5.8.000 (2010-08-11)
  1296. */
  1297. protected $colxshift = array('x' => 0, 's' => array('H' => 0, 'V' => 0), 'p' => array('L' => 0, 'T' => 0, 'R' => 0, 'B' => 0));
  1298. /**
  1299. * 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.
  1300. * @protected
  1301. * @since 4.9.008 (2010-04-03)
  1302. */
  1303. protected $textrendermode = 0;
  1304. /**
  1305. * Text stroke width in doc units.
  1306. * @protected
  1307. * @since 4.9.008 (2010-04-03)
  1308. */
  1309. protected $textstrokewidth = 0;
  1310. /**
  1311. * Current stroke color.
  1312. * @protected
  1313. * @since 4.9.008 (2010-04-03)
  1314. */
  1315. protected $strokecolor;
  1316. /**
  1317. * Default unit of measure for document.
  1318. * @protected
  1319. * @since 5.0.000 (2010-04-22)
  1320. */
  1321. protected $pdfunit = 'mm';
  1322. /**
  1323. * Boolean flag true when we are on TOC (Table Of Content) page.
  1324. * @protected
  1325. */
  1326. protected $tocpage = false;
  1327. /**
  1328. * Boolean flag: if true convert vector images (SVG, EPS) to raster image using GD or ImageMagick library.
  1329. * @protected
  1330. * @since 5.0.000 (2010-04-26)
  1331. */
  1332. protected $rasterize_vector_images = false;
  1333. /**
  1334. * Boolean flag: if true enables font subsetting by default.
  1335. * @protected
  1336. * @since 5.3.002 (2010-06-07)
  1337. */
  1338. protected $font_subsetting = true;
  1339. /**
  1340. * Array of default graphic settings.
  1341. * @protected
  1342. * @since 5.5.008 (2010-07-02)
  1343. */
  1344. protected $default_graphic_vars = array();
  1345. /**
  1346. * Array of XObjects.
  1347. * @protected
  1348. * @since 5.8.014 (2010-08-23)
  1349. */
  1350. protected $xobjects = array();
  1351. /**
  1352. * Boolean value true when we are inside an XObject.
  1353. * @protected
  1354. * @since 5.8.017 (2010-08-24)
  1355. */
  1356. protected $inxobj = false;
  1357. /**
  1358. * Current XObject ID.
  1359. * @protected
  1360. * @since 5.8.017 (2010-08-24)
  1361. */
  1362. protected $xobjid = '';
  1363. /**
  1364. * Percentage of character stretching.
  1365. * @protected
  1366. * @since 5.9.000 (2010-09-29)
  1367. */
  1368. protected $font_stretching = 100;
  1369. /**
  1370. * Increases or decreases the space between characters in a text by the specified amount (tracking/kerning).
  1371. * @protected
  1372. * @since 5.9.000 (2010-09-29)
  1373. */
  1374. protected $font_spacing = 0;
  1375. /**
  1376. * Array of no-write regions.
  1377. * ('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)
  1378. * @protected
  1379. * @since 5.9.003 (2010-10-14)
  1380. */
  1381. protected $page_regions = array();
  1382. /**
  1383. * Array containing HTML color names and values.
  1384. * @protected
  1385. * @since 5.9.004 (2010-10-18)
  1386. */
  1387. protected $webcolor = array();
  1388. /**
  1389. * Array containing spot color names and values.
  1390. * @protected
  1391. * @since 5.9.012 (2010-11-11)
  1392. */
  1393. protected $spotcolor = array();
  1394. /**
  1395. * Array of PDF layers data.
  1396. * @protected
  1397. * @since 5.9.102 (2011-07-13)
  1398. */
  1399. protected $pdflayers = array();
  1400. /**
  1401. * A dictionary of names and corresponding destinations (Dests key on document Catalog).
  1402. * @protected
  1403. * @since 5.9.097 (2011-06-23)
  1404. */
  1405. protected $dests = array();
  1406. /**
  1407. * Object ID for Named Destinations
  1408. * @protected
  1409. * @since 5.9.097 (2011-06-23)
  1410. */
  1411. protected $n_dests;
  1412. /**
  1413. * Directory used for the last SVG image.
  1414. * @protected
  1415. * @since 5.0.000 (2010-05-05)
  1416. */
  1417. protected $svgdir = '';
  1418. /**
  1419. * Deafult unit of measure for SVG.
  1420. * @protected
  1421. * @since 5.0.000 (2010-05-02)
  1422. */
  1423. protected $svgunit = 'px';
  1424. /**
  1425. * Array of SVG gradients.
  1426. * @protected
  1427. * @since 5.0.000 (2010-05-02)
  1428. */
  1429. protected $svggradients = array();
  1430. /**
  1431. * ID of last SVG gradient.
  1432. * @protected
  1433. * @since 5.0.000 (2010-05-02)
  1434. */
  1435. protected $svggradientid = 0;
  1436. /**
  1437. * Boolean value true when in SVG defs group.
  1438. * @protected
  1439. * @since 5.0.000 (2010-05-02)
  1440. */
  1441. protected $svgdefsmode = false;
  1442. /**
  1443. * Array of SVG defs.
  1444. * @protected
  1445. * @since 5.0.000 (2010-05-02)
  1446. */
  1447. protected $svgdefs = array();
  1448. /**
  1449. * Boolean value true when in SVG clipPath tag.
  1450. * @protected
  1451. * @since 5.0.000 (2010-04-26)
  1452. */
  1453. protected $svgclipmode = false;
  1454. /**
  1455. * Array of SVG clipPath commands.
  1456. * @protected
  1457. * @since 5.0.000 (2010-05-02)
  1458. */
  1459. protected $svgclippaths = array();
  1460. /**
  1461. * Array of SVG clipPath tranformation matrix.
  1462. * @protected
  1463. * @since 5.8.022 (2010-08-31)
  1464. */
  1465. protected $svgcliptm = array();
  1466. /**
  1467. * ID of last SVG clipPath.
  1468. * @protected
  1469. * @since 5.0.000 (2010-05-02)
  1470. */
  1471. protected $svgclipid = 0;
  1472. /**
  1473. * SVG text.
  1474. * @protected
  1475. * @since 5.0.000 (2010-05-02)
  1476. */
  1477. protected $svgtext = '';
  1478. /**
  1479. * SVG text properties.
  1480. * @protected
  1481. * @since 5.8.013 (2010-08-23)
  1482. */
  1483. protected $svgtextmode = array();
  1484. /**
  1485. * Array of hinheritable SVG properties.
  1486. * @protected
  1487. * @since 5.0.000 (2010-05-02)
  1488. */
  1489. 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');
  1490. /**
  1491. * Array of SVG properties.
  1492. * @protected
  1493. * @since 5.0.000 (2010-05-02)
  1494. */
  1495. protected $svgstyles = array(array(
  1496. 'alignment-baseline' => 'auto',
  1497. 'baseline-shift' => 'baseline',
  1498. 'clip' => 'auto',
  1499. 'clip-path' => 'none',
  1500. 'clip-rule' => 'nonzero',
  1501. 'color' => 'black',
  1502. 'color-interpolation' => 'sRGB',
  1503. 'color-interpolation-filters' => 'linearRGB',
  1504. 'color-profile' => 'auto',
  1505. 'color-rendering' => 'auto',
  1506. 'cursor' => 'auto',
  1507. 'direction' => 'ltr',
  1508. 'display' => 'inline',
  1509. 'dominant-baseline' => 'auto',
  1510. 'enable-background' => 'accumulate',
  1511. 'fill' => 'black',
  1512. 'fill-opacity' => 1,
  1513. 'fill-rule' => 'nonzero',
  1514. 'filter' => 'none',
  1515. 'flood-color' => 'black',
  1516. 'flood-opacity' => 1,
  1517. 'font' => '',
  1518. 'font-family' => 'helvetica',
  1519. 'font-size' => 'medium',
  1520. 'font-size-adjust' => 'none',
  1521. 'font-stretch' => 'normal',
  1522. 'font-style' => 'normal',
  1523. 'font-variant' => 'normal',
  1524. 'font-weight' => 'normal',
  1525. 'glyph-orientation-horizontal' => '0deg',
  1526. 'glyph-orientation-vertical' => 'auto',
  1527. 'image-rendering' => 'auto',
  1528. 'kerning' => 'auto',
  1529. 'letter-spacing' => 'normal',
  1530. 'lighting-color' => 'white',
  1531. 'marker' => '',
  1532. 'marker-end' => 'none',
  1533. 'marker-mid' => 'none',
  1534. 'marker-start' => 'none',
  1535. 'mask' => 'none',
  1536. 'opacity' => 1,
  1537. 'overflow' => 'auto',
  1538. 'pointer-events' => 'visiblePainted',
  1539. 'shape-rendering' => 'auto',
  1540. 'stop-color' => 'black',
  1541. 'stop-opacity' => 1,
  1542. 'stroke' => 'none',
  1543. 'stroke-dasharray' => 'none',
  1544. 'stroke-dashoffset' => 0,
  1545. 'stroke-linecap' => 'butt',
  1546. 'stroke-linejoin' => 'miter',
  1547. 'stroke-miterlimit' => 4,
  1548. 'stroke-opacity' => 1,
  1549. 'stroke-width' => 1,
  1550. 'text-anchor' => 'start',
  1551. 'text-decoration' => 'none',
  1552. 'text-rendering' => 'auto',
  1553. 'unicode-bidi' => 'normal',
  1554. 'visibility' => 'visible',
  1555. 'word-spacing' => 'normal',
  1556. 'writing-mode' => 'lr-tb',
  1557. 'text-color' => 'black',
  1558. 'transfmatrix' => array(1, 0, 0, 1, 0, 0)
  1559. ));
  1560. /**
  1561. * If true force sRGB color profile for all document.
  1562. * @protected
  1563. * @since 5.9.121 (2011-09-28)
  1564. */
  1565. protected $force_srgb = false;
  1566. /**
  1567. * If true set the document to PDF/A mode.
  1568. * @protected
  1569. * @since 5.9.121 (2011-09-27)
  1570. */
  1571. protected $pdfa_mode = false;
  1572. /**
  1573. * Document creation date
  1574. * @protected
  1575. * @since 5.9.121 (2011-09-28)
  1576. */
  1577. protected $doc_date;
  1578. /**
  1579. * Custom XMP data.
  1580. * @protected
  1581. * @since 5.9.128 (2011-10-06)
  1582. */
  1583. protected $custom_xmp = '';
  1584. //------------------------------------------------------------
  1585. // METHODS
  1586. //------------------------------------------------------------
  1587. /**
  1588. * This is the class constructor.
  1589. * It allows to set up the page format, the orientation and the measure unit used in all the methods (except for the font sizes).
  1590. * @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>
  1591. * @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.
  1592. * @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().
  1593. * @param $unicode (boolean) TRUE means that the input text is unicode (default = true)
  1594. * @param $encoding (string) Charset encoding; default is UTF-8.
  1595. * @param $diskcache (boolean) If TRUE reduce the RAM memory usage by caching temporary data on filesystem (slower).
  1596. * @param $pdfa (boolean) If TRUE set the document to PDF/A mode.
  1597. * @public
  1598. * @see getPageSizeFromFormat(), setPageFormat()
  1599. */
  1600. public function __construct($orientation='P', $unit='mm', $format='A4', $unicode=true, $encoding='UTF-8', $diskcache=false, $pdfa=false) {
  1601. /* Set internal character encoding to ASCII */
  1602. if (function_exists('mb_internal_encoding') AND mb_internal_encoding()) {
  1603. $this->internal_encoding = mb_internal_encoding();
  1604. mb_internal_encoding('ASCII');
  1605. }
  1606. // get array of HTML colors
  1607. require(dirname(__FILE__).'/htmlcolors.php');
  1608. $this->webcolor = $webcolor;
  1609. // get array of custom spot colors
  1610. if (file_exists(dirname(__FILE__).'/spotcolors.php')) {
  1611. require(dirname(__FILE__).'/spotcolors.php');
  1612. $this->spotcolor = $spotcolor;
  1613. } else {
  1614. $this->spotcolor = array();
  1615. }
  1616. require_once(dirname(__FILE__).'/unicode_data.php');
  1617. $this->unicode = new TCPDF_UNICODE_DATA();
  1618. require_once(dirname(__FILE__).'/encodings_maps.php');
  1619. $this->encmaps = new TCPDF_ENCODING_MAPS();
  1620. $this->font_obj_ids = array();
  1621. $this->page_obj_id = array();
  1622. $this->form_obj_id = array();
  1623. // set pdf/a mode
  1624. $this->pdfa_mode = $pdfa;
  1625. $this->force_srgb = false;
  1626. // set disk caching
  1627. $this->diskcache = $diskcache ? true : false;
  1628. // set language direction
  1629. $this->rtl = false;
  1630. $this->tmprtl = false;
  1631. // some checks
  1632. $this->_dochecks();
  1633. // initialization of properties
  1634. $this->isunicode = $unicode;
  1635. $this->page = 0;
  1636. $this->transfmrk[0] = array();
  1637. $this->pagedim = array();
  1638. $this->n = 2;
  1639. $this->buffer = '';
  1640. $this->pages = array();
  1641. $this->state = 0;
  1642. $this->fonts = array();
  1643. $this->FontFiles = array();
  1644. $this->diffs = array();
  1645. $this->images = array();
  1646. $this->links = array();
  1647. $this->gradients = array();
  1648. $this->InFooter = false;
  1649. $this->lasth = 0;
  1650. $this->FontFamily = 'helvetica';
  1651. $this->FontStyle = '';
  1652. $this->FontSizePt = 12;
  1653. $this->underline = false;
  1654. $this->overline = false;
  1655. $this->linethrough = false;
  1656. $this->DrawColor = '0 G';
  1657. $this->FillColor = '0 g';
  1658. $this->TextColor = '0 g';
  1659. $this->ColorFlag = false;
  1660. $this->pdflayers = array();
  1661. // encryption values
  1662. $this->encrypted = false;
  1663. $this->last_enc_key = '';
  1664. // standard Unicode fonts
  1665. $this->CoreFonts = array(
  1666. 'courier'=>'Courier',
  1667. 'courierB'=>'Courier-Bold',
  1668. 'courierI'=>'Courier-Oblique',
  1669. 'courierBI'=>'Courier-BoldOblique',
  1670. 'helvetica'=>'Helvetica',
  1671. 'helveticaB'=>'Helvetica-Bold',
  1672. 'helveticaI'=>'Helvetica-Oblique',
  1673. 'helveticaBI'=>'Helvetica-BoldOblique',
  1674. 'times'=>'Times-Roman',
  1675. 'timesB'=>'Times-Bold',
  1676. 'timesI'=>'Times-Italic',
  1677. 'timesBI'=>'Times-BoldItalic',
  1678. 'symbol'=>'Symbol',
  1679. 'zapfdingbats'=>'ZapfDingbats'
  1680. );
  1681. // set scale factor
  1682. $this->setPageUnit($unit);
  1683. // set page format and orientation
  1684. $this->setPageFormat($format, $orientation);
  1685. // page margins (1 cm)
  1686. $margin = 28.35 / $this->k;
  1687. $this->SetMargins($margin, $margin);
  1688. // internal cell padding
  1689. $cpadding = $margin / 10;
  1690. $this->setCellPaddings($cpadding, 0, $cpadding, 0);
  1691. // cell margins
  1692. $this->setCellMargins(0, 0, 0, 0);
  1693. // line width (0.2 mm)
  1694. $this->LineWidth = 0.57 / $this->k;
  1695. $this->linestyleWidth = sprintf('%.2F w', ($this->LineWidth * $this->k));
  1696. $this->linestyleCap = '0 J';
  1697. $this->linestyleJoin = '0 j';
  1698. $this->linestyleDash = '[] 0 d';
  1699. // automatic page break
  1700. $this->SetAutoPageBreak(true, (2 * $margin));
  1701. // full width display mode
  1702. $this->SetDisplayMode('fullwidth');
  1703. // compression
  1704. $this->SetCompression();
  1705. // set default PDF version number
  1706. $this->setPDFVersion();
  1707. $this->encoding = $encoding;
  1708. $this->HREF = array();
  1709. $this->getFontsList();
  1710. $this->fgcolor = array('R' => 0, 'G' => 0, 'B' => 0);
  1711. $this->strokecolor = array('R' => 0, 'G' => 0, 'B' => 0);
  1712. $this->bgcolor = array('R' => 255, 'G' => 255, 'B' => 255);
  1713. $this->extgstates = array();
  1714. // user's rights
  1715. $this->sign = false;
  1716. $this->ur['enabled'] = false;
  1717. $this->ur['document'] = '/FullSave';
  1718. $this->ur['annots'] = '/Create/Delete/Modify/Copy/Import/Export';
  1719. $this->ur['form'] = '/Add/Delete/FillIn/Import/Export/SubmitStandalone/SpawnTemplate';
  1720. $this->ur['signature'] = '/Modify';
  1721. $this->ur['ef'] = '/Create/Delete/Modify/Import';
  1722. $this->ur['formex'] = '';
  1723. $this->signature_appearance = array('page' => 1, 'rect' => '0 0 0 0');
  1724. $this->empty_signature_appearance = array();
  1725. // set default JPEG quality
  1726. $this->jpeg_quality = 75;
  1727. // initialize some settings
  1728. $this->utf8Bidi(array(''), '');
  1729. // set default font
  1730. $this->SetFont($this->FontFamily, $this->FontStyle, $this->FontSizePt);
  1731. // check if PCRE Unicode support is enabled
  1732. if ($this->isunicode AND (@preg_match('/\pL/u', 'a') == 1)) {
  1733. // PCRE unicode support is turned ON
  1734. // \p{Z} or \p{Separator}: any kind of Unicode whitespace or invisible separator.
  1735. // \p{Lo} or \p{Other_Letter}: a Unicode letter or ideograph that does not have lowercase and uppercase variants.
  1736. // \p{Lo} is needed because Chinese characters are packed next to each other without spaces in between.
  1737. //$this->setSpacesRE('/[^\S\P{Z}\P{Lo}\xa0]/u');
  1738. $this->setSpacesRE('/[^\S\P{Z}\xa0]/u');
  1739. } else {
  1740. // PCRE unicode support is turned OFF
  1741. $this->setSpacesRE('/[^\S\xa0]/');
  1742. }
  1743. $this->default_form_prop = array('lineWidth'=>1, 'borderStyle'=>'solid', 'fillColor'=>array(255, 255, 255), 'strokeColor'=>array(128, 128, 128));
  1744. // set file ID for trailer
  1745. $this->file_id = md5($this->getRandomSeed('TCPDF'.$orientation.$unit.$format.$encoding));
  1746. // set document date
  1747. $this->doc_date = substr_replace(date('YmdHisO'), '\'', (0 - 2), 0).'\'';
  1748. // get default graphic vars
  1749. $this->default_graphic_vars = $this->getGraphicVars();
  1750. $this->header_xobj_autoreset = false;
  1751. $this->custom_xmp = '';
  1752. }
  1753. /**
  1754. * Default destructor.
  1755. * @public
  1756. * @since 1.53.0.TC016
  1757. */
  1758. public function __destruct() {
  1759. // restore internal encoding
  1760. if (isset($this->internal_encoding) AND !empty($this->internal_encoding)) {
  1761. mb_internal_encoding($this->internal_encoding);
  1762. }
  1763. // unset all class variables
  1764. $this->_destroy(true);
  1765. }
  1766. /**
  1767. * Return the current TCPDF version.
  1768. * @return TCPDF version string
  1769. * @public
  1770. * @since 5.9.012 (2010-11-10)
  1771. */
  1772. public function getTCPDFVersion() {
  1773. return $this->tcpdf_version;
  1774. }
  1775. /**
  1776. * Set the units of measure for the document.
  1777. * @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.
  1778. * @public
  1779. * @since 3.0.015 (2008-06-06)
  1780. */
  1781. public function setPageUnit($unit) {
  1782. $unit = strtolower($unit);
  1783. //Set scale factor
  1784. switch ($unit) {
  1785. // points
  1786. case 'px':
  1787. case 'pt': {
  1788. $this->k = 1;
  1789. break;
  1790. }
  1791. // millimeters
  1792. case 'mm': {
  1793. $this->k = $this->dpi / 25.4;
  1794. break;
  1795. }
  1796. // centimeters
  1797. case 'cm': {
  1798. $this->k = $this->dpi / 2.54;
  1799. break;
  1800. }
  1801. // inches
  1802. case 'in': {
  1803. $this->k = $this->dpi;
  1804. break;
  1805. }
  1806. // unsupported unit
  1807. default : {
  1808. $this->Error('Incorrect unit: '.$unit);
  1809. break;
  1810. }
  1811. }
  1812. $this->pdfunit = $unit;
  1813. if (isset($this->CurOrientation)) {
  1814. $this->setPageOrientation($this->CurOrientation);
  1815. }
  1816. }
  1817. /**
  1818. * Get page dimensions from format name.
  1819. * @param $format (mixed) The format name. It can be: <ul>
  1820. * <li><b>ISO 216 A Series + 2 SIS 014711 extensions</b></li>
  1821. * <li>A0 (841x1189 mm ; 33.11x46.81 in)</li>
  1822. * <li>A1 (594x841 mm ; 23.39x33.11 in)</li>
  1823. * <li>A2 (420x594 mm ; 16.54x23.39 in)</li>
  1824. * <li>A3 (297x420 mm ; 11.69x16.54 in)</li>
  1825. * <li>A4 (210x297 mm ; 8.27x11.69 in)</li>
  1826. * <li>A5 (148x210 mm ; 5.83x8.27 in)</li>
  1827. * <li>A6 (105x148 mm ; 4.13x5.83 in)</li>
  1828. * <li>A7 (74x105 mm ; 2.91x4.13 in)</li>
  1829. * <li>A8 (52x74 mm ; 2.05x2.91 in)</li>
  1830. * <li>A9 (37x52 mm ; 1.46x2.05 in)</li>
  1831. * <li>A10 (26x37 mm ; 1.02x1.46 in)</li>
  1832. * <li>A11 (18x26 mm ; 0.71x1.02 in)</li>
  1833. * <li>A12 (13x18 mm ; 0.51x0.71 in)</li>
  1834. * <li><b>ISO 216 B Series + 2 SIS 014711 extensions</b></li>
  1835. * <li>B0 (1000x1414 mm ; 39.37x55.67 in)</li>
  1836. * <li>B1 (707x1000 mm ; 27.83x39.37 in)</li>
  1837. * <li>B2 (500x707 mm ; 19.69x27.83 in)</li>
  1838. * <li>B3 (353x500 mm ; 13.90x19.69 in)</li>
  1839. * <li>B4 (250x353 mm ; 9.84x13.90 in)</li>
  1840. * <li>B5 (176x250 mm ; 6.93x9.84 in)</li>
  1841. * <li>B6 (125x176 mm ; 4.92x6.93 in)</li>
  1842. * <li>B7 (88x125 mm ; 3.46x4.92 in)</li>
  1843. * <li>B8 (62x88 mm ; 2.44x3.46 in)</li>
  1844. * <li>B9 (44x62 mm ; 1.73x2.44 in)</li>
  1845. * <li>B10 (31x44 mm ; 1.22x1.73 in)</li>
  1846. * <li>B11 (22x31 mm ; 0.87x1.22 in)</li>
  1847. * <li>B12 (15x22 mm ; 0.59x0.87 in)</li>
  1848. * <li><b>ISO 216 C Series + 2 SIS 014711 extensions + 2 EXTENSION</b></li>
  1849. * <li>C0 (917x1297 mm ; 36.10x51.06 in)</li>
  1850. * <li>C1 (648x917 mm ; 25.51x36.10 in)</li>
  1851. * <li>C2 (458x648 mm ; 18.03x25.51 in)</li>
  1852. * <li>C3 (324x458 mm ; 12.76x18.03 in)</li>
  1853. * <li>C4 (229x324 mm ; 9.02x12.76 in)</li>
  1854. * <li>C5 (162x229 mm ; 6.38x9.02 in)</li>
  1855. * <li>C6 (114x162 mm ; 4.49x6.38 in)</li>
  1856. * <li>C7 (81x114 mm ; 3.19x4.49 in)</li>
  1857. * <li>C8 (57x81 mm ; 2.24x3.19 in)</li>
  1858. * <li>C9 (40x57 mm ; 1.57x2.24 in)</li>
  1859. * <li>C10 (28x40 mm ; 1.10x1.57 in)</li>
  1860. * <li>C11 (20x28 mm ; 0.79x1.10 in)</li>
  1861. * <li>C12 (14x20 mm ; 0.55x0.79 in)</li>
  1862. * <li>C76 (81x162 mm ; 3.19x6.38 in)</li>
  1863. * <li>DL (110x220 mm ; 4.33x8.66 in)</li>
  1864. * <li><b>SIS 014711 E Series</b></li>
  1865. * <li>E0 (879x1241 mm ; 34.61x48.86 in)</li>
  1866. * <li>E1 (620x879 mm ; 24.41x34.61 in)</li>
  1867. * <li>E2 (440x620 mm ; 17.32x24.41 in)</li>
  1868. * <li>E3 (310x440 mm ; 12.20x17.32 in)</li>
  1869. * <li>E4 (220x310 mm ; 8.66x12.20 in)</li>
  1870. * <li>E5 (155x220 mm ; 6.10x8.66 in)</li>
  1871. * <li>E6 (110x155 mm ; 4.33x6.10 in)</li>
  1872. * <li>E7 (78x110 mm ; 3.07x4.33 in)</li>
  1873. * <li>E8 (55x78 mm ; 2.17x3.07 in)</li>
  1874. * <li>E9 (39x55 mm ; 1.54x2.17 in)</li>
  1875. * <li>E10 (27x39 mm ; 1.06x1.54 in)</li>
  1876. * <li>E11 (19x27 mm ; 0.75x1.06 in)</li>
  1877. * <li>E12 (13x19 mm ; 0.51x0.75 in)</li>
  1878. * <li><b>SIS 014711 G Series</b></li>
  1879. * <li>G0 (958x1354 mm ; 37.72x53.31 in)</li>
  1880. * <li>G1 (677x958 mm ; 26.65x37.72 in)</li>
  1881. * <li>G2 (479x677 mm ; 18.86x26.65 in)</li>
  1882. * <li>G3 (338x479 mm ; 13.31x18.86 in)</li>
  1883. * <li>G4 (239x338 mm ; 9.41x13.31 in)</li>
  1884. * <li>G5 (169x239 mm ; 6.65x9.41 in)</li>
  1885. * <li>G6 (119x169 mm ; 4.69x6.65 in)</li>
  1886. * <li>G7 (84x119 mm ; 3.31x4.69 in)</li>
  1887. * <li>G8 (59x84 mm ; 2.32x3.31 in)</li>
  1888. * <li>G9 (42x59 mm ; 1.65x2.32 in)</li>
  1889. * <li>G10 (29x42 mm ; 1.14x1.65 in)</li>
  1890. * <li>G11 (21x29 mm ; 0.83x1.14 in)</li>
  1891. * <li>G12 (14x21 mm ; 0.55x0.83 in)</li>
  1892. * <li><b>ISO Press</b></li>
  1893. * <li>RA0 (860x1220 mm ; 33.86x48.03 in)</li>
  1894. * <li>RA1 (610x860 mm ; 24.02x33.86 in)</li>
  1895. * <li>RA2 (430x610 mm ; 16.93x24.02 in)</li>
  1896. * <li>RA3 (305x430 mm ; 12.01x16.93 in)</li>
  1897. * <li>RA4 (215x305 mm ; 8.46x12.01 in)</li>
  1898. * <li>SRA0 (900x1280 mm ; 35.43x50.39 in)</li>
  1899. * <li>SRA1 (640x900 mm ; 25.20x35.43 in)</li>
  1900. * <li>SRA2 (450x640 mm ; 17.72x25.20 in)</li>
  1901. * <li>SRA3 (320x450 mm ; 12.60x17.72 in)</li>
  1902. * <li>SRA4 (225x320 mm ; 8.86x12.60 in)</li>
  1903. * <li><b>German DIN 476</b></li>
  1904. * <li>4A0 (1682x2378 mm ; 66.22x93.62 in)</li>
  1905. * <li>2A0 (1189x1682 mm ; 46.81x66.22 in)</li>
  1906. * <li><b>Variations on the ISO Standard</b></li>
  1907. * <li>A2_EXTRA (445x619 mm ; 17.52x24.37 in)</li>
  1908. * <li>A3+ (329x483 mm ; 12.95x19.02 in)</li>
  1909. * <li>A3_EXTRA (322x445 mm ; 12.68x17.52 in)</li>
  1910. * <li>A3_SUPER (305x508 mm ; 12.01x20.00 in)</li>
  1911. * <li>SUPER_A3 (305x487 mm ; 12.01x19.17 in)</li>
  1912. * <li>A4_EXTRA (235x322 mm ; 9.25x12.68 in)</li>
  1913. * <li>A4_SUPER (229x322 mm ; 9.02x12.68 in)</li>
  1914. * <li>SUPER_A4 (227x356 mm ; 8.94x14.02 in)</li>
  1915. * <li>A4_LONG (210x348 mm ; 8.27x13.70 in)</li>
  1916. * <li>F4 (210x330 mm ; 8.27x12.99 in)</li>
  1917. * <li>SO_B5_EXTRA (202x276 mm ; 7.95x10.87 in)</li>
  1918. * <li>A5_EXTRA (173x235 mm ; 6.81x9.25 in)</li>
  1919. * <li><b>ANSI Series</b></li>
  1920. * <li>ANSI_E (864x1118 mm ; 34.00x44.00 in)</li>
  1921. * <li>ANSI_D (559x864 mm ; 22.00x34.00 in)</li>
  1922. * <li>ANSI_C (432x559 mm ; 17.00x22.00 in)</li>
  1923. * <li>ANSI_B (279x432 mm ; 11.00x17.00 in)</li>
  1924. * <li>ANSI_A (216x279 mm ; 8.50x11.00 in)</li>
  1925. * <li><b>Traditional 'Loose' North American Paper Sizes</b></li>
  1926. * <li>LEDGER, USLEDGER (432x279 mm ; 17.00x11.00 in)</li>
  1927. * <li>TABLOID, USTABLOID, BIBLE, ORGANIZERK (279x432 mm ; 11.00x17.00 in)</li>
  1928. * <li>LETTER, USLETTER, ORGANIZERM (216x279 mm ; 8.50x11.00 in)</li>
  1929. * <li>LEGAL, USLEGAL (216x356 mm ; 8.50x14.00 in)</li>
  1930. * <li>GLETTER, GOVERNMENTLETTER (203x267 mm ; 8.00x10.50 in)</li>
  1931. * <li>JLEGAL, JUNIORLEGAL (203x127 mm ; 8.00x5.00 in)</li>
  1932. * <li><b>Other North American Paper Sizes</b></li>
  1933. * <li>QUADDEMY (889x1143 mm ; 35.00x45.00 in)</li>
  1934. * <li>SUPER_B (330x483 mm ; 13.00x19.00 in)</li>
  1935. * <li>QUARTO (229x279 mm ; 9.00x11.00 in)</li>
  1936. * <li>FOLIO, GOVERNMENTLEGAL (216x330 mm ; 8.50x13.00 in)</li>
  1937. * <li>EXECUTIVE, MONARCH (184x267 mm ; 7.25x10.50 in)</li>
  1938. * <li>MEMO, STATEMENT, ORGANIZERL (140x216 mm ; 5.50x8.50 in)</li>
  1939. * <li>FOOLSCAP (210x330 mm ; 8.27x13.00 in)</li>
  1940. * <li>COMPACT (108x171 mm ; 4.25x6.75 in)</li>
  1941. * <li>ORGANIZERJ (70x127 mm ; 2.75x5.00 in)</li>
  1942. * <li><b>Canadian standard CAN 2-9.60M</b></li>
  1943. * <li>P1 (560x860 mm ; 22.05x33.86 in)</li>
  1944. * <li>P2 (430x560 mm ; 16.93x22.05 in)</li>
  1945. * <li>P3 (280x430 mm ; 11.02x16.93 in)</li>
  1946. * <li>P4 (215x280 mm ; 8.46x11.02 in)</li>
  1947. * <li>P5 (140x215 mm ; 5.51x8.46 in)</li>
  1948. * <li>P6 (107x140 mm ; 4.21x5.51 in)</li>
  1949. * <li><b>North American Architectural Sizes</b></li>
  1950. * <li>ARCH_E (914x1219 mm ; 36.00x48.00 in)</li>
  1951. * <li>ARCH_E1 (762x1067 mm ; 30.00x42.00 in)</li>
  1952. * <li>ARCH_D (610x914 mm ; 24.00x36.00 in)</li>
  1953. * <li>ARCH_C, BROADSHEET (457x610 mm ; 18.00x24.00 in)</li>
  1954. * <li>ARCH_B (305x457 mm ; 12.00x18.00 in)</li>
  1955. * <li>ARCH_A (229x305 mm ; 9.00x12.00 in)</li>
  1956. * <li><b>Announcement Envelopes</b></li>
  1957. * <li>ANNENV_A2 (111x146 mm ; 4.37x5.75 in)</li>
  1958. * <li>ANNENV_A6 (121x165 mm ; 4.75x6.50 in)</li>
  1959. * <li>ANNENV_A7 (133x184 mm ; 5.25x7.25 in)</li>
  1960. * <li>ANNENV_A8 (140x206 mm ; 5.50x8.12 in)</li>
  1961. * <li>ANNENV_A10 (159x244 mm ; 6.25x9.62 in)</li>
  1962. * <li>ANNENV_SLIM (98x225 mm ; 3.87x8.87 in)</li>
  1963. * <li><b>Commercial Envelopes</b></li>
  1964. * <li>COMMENV_N6_1/4 (89x152 mm ; 3.50x6.00 in)</li>
  1965. * <li>COMMENV_N6_3/4 (92x165 mm ; 3.62x6.50 in)</li>
  1966. * <li>COMMENV_N8 (98x191 mm ; 3.87x7.50 in)</li>
  1967. * <li>COMMENV_N9 (98x225 mm ; 3.87x8.87 in)</li>
  1968. * <li>COMMENV_N10 (105x241 mm ; 4.12x9.50 in)</li>
  1969. * <li>COMMENV_N11 (114x263 mm ; 4.50x10.37 in)</li>
  1970. * <li>COMMENV_N12 (121x279 mm ; 4.75x11.00 in)</li>
  1971. * <li>COMMENV_N14 (127x292 mm ; 5.00x11.50 in)</li>
  1972. * <li><b>Catalogue Envelopes</b></li>
  1973. * <li>CATENV_N1 (152x229 mm ; 6.00x9.00 in)</li>
  1974. * <li>CATENV_N1_3/4 (165x241 mm ; 6.50x9.50 in)</li>
  1975. * <li>CATENV_N2 (165x254 mm ; 6.50x10.00 in)</li>
  1976. * <li>CATENV_N3 (178x254 mm ; 7.00x10.00 in)</li>
  1977. * <li>CATENV_N6 (191x267 mm ; 7.50x10.50 in)</li>
  1978. * <li>CATENV_N7 (203x279 mm ; 8.00x11.00 in)</li>
  1979. * <li>CATENV_N8 (210x286 mm ; 8.25x11.25 in)</li>
  1980. * <li>CATENV_N9_1/2 (216x267 mm ; 8.50x10.50 in)</li>
  1981. * <li>CATENV_N9_3/4 (222x286 mm ; 8.75x11.25 in)</li>
  1982. * <li>CATENV_N10_1/2 (229x305 mm ; 9.00x12.00 in)</li>
  1983. * <li>CATENV_N12_1/2 (241x318 mm ; 9.50x12.50 in)</li>
  1984. * <li>CATENV_N13_1/2 (254x330 mm ; 10.00x13.00 in)</li>
  1985. * <li>CATENV_N14_1/4 (286x311 mm ; 11.25x12.25 in)</li>
  1986. * <li>CATENV_N14_1/2 (292x368 mm ; 11.50x14.50 in)</li>
  1987. * <li><b>Japanese (JIS P 0138-61) Standard B-Series</b></li>
  1988. * <li>JIS_B0 (1030x1456 mm ; 40.55x57.32 in)</li>
  1989. * <li>JIS_B1 (728x1030 mm ; 28.66x40.55 in)</li>
  1990. * <li>JIS_B2 (515x728 mm ; 20.28x28.66 in)</li>
  1991. * <li>JIS_B3 (364x515 mm ; 14.33x20.28 in)</li>
  1992. * <li>JIS_B4 (257x364 mm ; 10.12x14.33 in)</li>
  1993. * <li>JIS_B5 (182x257 mm ; 7.17x10.12 in)</li>
  1994. * <li>JIS_B6 (128x182 mm ; 5.04x7.17 in)</li>
  1995. * <li>JIS_B7 (91x128 mm ; 3.58x5.04 in)</li>
  1996. * <li>JIS_B8 (64x91 mm ; 2.52x3.58 in)</li>
  1997. * <li>JIS_B9 (45x64 mm ; 1.77x2.52 in)</li>
  1998. * <li>JIS_B10 (32x45 mm ; 1.26x1.77 in)</li>
  1999. * <li>JIS_B11 (22x32 mm ; 0.87x1.26 in)</li>
  2000. * <li>JIS_B12 (16x22 mm ; 0.63x0.87 in)</li>
  2001. * <li><b>PA Series</b></li>
  2002. * <li>PA0 (840x1120 mm ; 33.07x44.09 in)</li>
  2003. * <li>PA1 (560x840 mm ; 22.05x33.07 in)</li>
  2004. * <li>PA2 (420x560 mm ; 16.54x22.05 in)</li>
  2005. * <li>PA3 (280x420 mm ; 11.02x16.54 in)</li>
  2006. * <li>PA4 (210x280 mm ; 8.27x11.02 in)</li>
  2007. * <li>PA5 (140x210 mm ; 5.51x8.27 in)</li>
  2008. * <li>PA6 (105x140 mm ; 4.13x5.51 in)</li>
  2009. * <li>PA7 (70x105 mm ; 2.76x4.13 in)</li>
  2010. * <li>PA8 (52x70 mm ; 2.05x2.76 in)</li>
  2011. * <li>PA9 (35x52 mm ; 1.38x2.05 in)</li>
  2012. * <li>PA10 (26x35 mm ; 1.02x1.38 in)</li>
  2013. * <li><b>Standard Photographic Print Sizes</b></li>
  2014. * <li>PASSPORT_PHOTO (35x45 mm ; 1.38x1.77 in)</li>
  2015. * <li>E (82x120 mm ; 3.25x4.72 in)</li>
  2016. * <li>3R, L (89x127 mm ; 3.50x5.00 in)</li>
  2017. * <li>4R, KG (102x152 mm ; 4.02x5.98 in)</li>
  2018. * <li>4D (120x152 mm ; 4.72x5.98 in)</li>
  2019. * <li>5R, 2L (127x178 mm ; 5.00x7.01 in)</li>
  2020. * <li>6R, 8P (152x203 mm ; 5.98x7.99 in)</li>
  2021. * <li>8R, 6P (203x254 mm ; 7.99x10.00 in)</li>
  2022. * <li>S8R, 6PW (203x305 mm ; 7.99x12.01 in)</li>
  2023. * <li>10R, 4P (254x305 mm ; 10.00x12.01 in)</li>
  2024. * <li>S10R, 4PW (254x381 mm ; 10.00x15.00 in)</li>
  2025. * <li>11R (279x356 mm ; 10.98x14.02 in)</li>
  2026. * <li>S11R (279x432 mm ; 10.98x17.01 in)</li>
  2027. * <li>12R (305x381 mm ; 12.01x15.00 in)</li>
  2028. * <li>S12R (305x456 mm ; 12.01x17.95 in)</li>
  2029. * <li><b>Common Newspaper Sizes</b></li>
  2030. * <li>NEWSPAPER_BROADSHEET (750x600 mm ; 29.53x23.62 in)</li>
  2031. * <li>NEWSPAPER_BERLINER (470x315 mm ; 18.50x12.40 in)</li>
  2032. * <li>NEWSPAPER_COMPACT, NEWSPAPER_TABLOID (430x280 mm ; 16.93x11.02 in)</li>
  2033. * <li><b>Business Cards</b></li>
  2034. * <li>CREDIT_CARD, BUSINESS_CARD, BUSINESS_CARD_ISO7810 (54x86 mm ; 2.13x3.37 in)</li>
  2035. * <li>BUSINESS_CARD_ISO216 (52x74 mm ; 2.05x2.91 in)</li>
  2036. * <li>BUSINESS_CARD_IT, BUSINESS_CARD_UK, BUSINESS_CARD_FR, BUSINESS_CARD_DE, BUSINESS_CARD_ES (55x85 mm ; 2.17x3.35 in)</li>
  2037. * <li>BUSINESS_CARD_US, BUSINESS_CARD_CA (51x89 mm ; 2.01x3.50 in)</li>
  2038. * <li>BUSINESS_CARD_JP (55x91 mm ; 2.17x3.58 in)</li>
  2039. * <li>BUSINESS_CARD_HK (54x90 mm ; 2.13x3.54 in)</li>
  2040. * <li>BUSINESS_CARD_AU, BUSINESS_CARD_DK, BUSINESS_CARD_SE (55x90 mm ; 2.17x3.54 in)</li>
  2041. * <li>BUSINESS_CARD_RU, BUSINESS_CARD_CZ, BUSINESS_CARD_FI, BUSINESS_CARD_HU, BUSINESS_CARD_IL (50x90 mm ; 1.97x3.54 in)</li>
  2042. * <li><b>Billboards</b></li>
  2043. * <li>4SHEET (1016x1524 mm ; 40.00x60.00 in)</li>
  2044. * <li>6SHEET (1200x1800 mm ; 47.24x70.87 in)</li>
  2045. * <li>12SHEET (3048x1524 mm ; 120.00x60.00 in)</li>
  2046. * <li>16SHEET (2032x3048 mm ; 80.00x120.00 in)</li>
  2047. * <li>32SHEET (4064x3048 mm ; 160.00x120.00 in)</li>
  2048. * <li>48SHEET (6096x3048 mm ; 240.00x120.00 in)</li>
  2049. * <li>64SHEET (8128x3048 mm ; 320.00x120.00 in)</li>
  2050. * <li>96SHEET (12192x3048 mm ; 480.00x120.00 in)</li>
  2051. * <li><b>Old Imperial English (some are still used in USA)</b></li>
  2052. * <li>EN_EMPEROR (1219x1829 mm ; 48.00x72.00 in)</li>
  2053. * <li>EN_ANTIQUARIAN (787x1346 mm ; 31.00x53.00 in)</li>
  2054. * <li>EN_GRAND_EAGLE (730x1067 mm ; 28.75x42.00 in)</li>
  2055. * <li>EN_DOUBLE_ELEPHANT (679x1016 mm ; 26.75x40.00 in)</li>
  2056. * <li>EN_ATLAS (660x864 mm ; 26.00x34.00 in)</li>
  2057. * <li>EN_COLOMBIER (597x876 mm ; 23.50x34.50 in)</li>
  2058. * <li>EN_ELEPHANT (584x711 mm ; 23.00x28.00 in)</li>
  2059. * <li>EN_DOUBLE_DEMY (572x902 mm ; 22.50x35.50 in)</li>
  2060. * <li>EN_IMPERIAL (559x762 mm ; 22.00x30.00 in)</li>
  2061. * <li>EN_PRINCESS (546x711 mm ; 21.50x28.00 in)</li>
  2062. * <li>EN_CARTRIDGE (533x660 mm ; 21.00x26.00 in)</li>
  2063. * <li>EN_DOUBLE_LARGE_POST (533x838 mm ; 21.00x33.00 in)</li>
  2064. * <li>EN_ROYAL (508x635 mm ; 20.00x25.00 in)</li>
  2065. * <li>EN_SHEET, EN_HALF_POST (495x597 mm ; 19.50x23.50 in)</li>
  2066. * <li>EN_SUPER_ROYAL (483x686 mm ; 19.00x27.00 in)</li>
  2067. * <li>EN_DOUBLE_POST (483x775 mm ; 19.00x30.50 in)</li>
  2068. * <li>EN_MEDIUM (445x584 mm ; 17.50x23.00 in)</li>
  2069. * <li>EN_DEMY (445x572 mm ; 17.50x22.50 in)</li>
  2070. * <li>EN_LARGE_POST (419x533 mm ; 16.50x21.00 in)</li>
  2071. * <li>EN_COPY_DRAUGHT (406x508 mm ; 16.00x20.00 in)</li>
  2072. * <li>EN_POST (394x489 mm ; 15.50x19.25 in)</li>
  2073. * <li>EN_CROWN (381x508 mm ; 15.00x20.00 in)</li>
  2074. * <li>EN_PINCHED_POST (375x470 mm ; 14.75x18.50 in)</li>
  2075. * <li>EN_BRIEF (343x406 mm ; 13.50x16.00 in)</li>
  2076. * <li>EN_FOOLSCAP (343x432 mm ; 13.50x17.00 in)</li>
  2077. * <li>EN_SMALL_FOOLSCAP (337x419 mm ; 13.25x16.50 in)</li>
  2078. * <li>EN_POTT (318x381 mm ; 12.50x15.00 in)</li>
  2079. * <li><b>Old Imperial Belgian</b></li>
  2080. * <li>BE_GRAND_AIGLE (700x1040 mm ; 27.56x40.94 in)</li>
  2081. * <li>BE_COLOMBIER (620x850 mm ; 24.41x33.46 in)</li>
  2082. * <li>BE_DOUBLE_CARRE (620x920 mm ; 24.41x36.22 in)</li>
  2083. * <li>BE_ELEPHANT (616x770 mm ; 24.25x30.31 in)</li>
  2084. * <li>BE_PETIT_AIGLE (600x840 mm ; 23.62x33.07 in)</li>
  2085. * <li>BE_GRAND_JESUS (550x730 mm ; 21.65x28.74 in)</li>
  2086. * <li>BE_JESUS (540x730 mm ; 21.26x28.74 in)</li>
  2087. * <li>BE_RAISIN (500x650 mm ; 19.69x25.59 in)</li>
  2088. * <li>BE_GRAND_MEDIAN (460x605 mm ; 18.11x23.82 in)</li>
  2089. * <li>BE_DOUBLE_POSTE (435x565 mm ; 17.13x22.24 in)</li>
  2090. * <li>BE_COQUILLE (430x560 mm ; 16.93x22.05 in)</li>
  2091. * <li>BE_PETIT_MEDIAN (415x530 mm ; 16.34x20.87 in)</li>
  2092. * <li>BE_RUCHE (360x460 mm ; 14.17x18.11 in)</li>
  2093. * <li>BE_PROPATRIA (345x430 mm ; 13.58x16.93 in)</li>
  2094. * <li>BE_LYS (317x397 mm ; 12.48x15.63 in)</li>
  2095. * <li>BE_POT (307x384 mm ; 12.09x15.12 in)</li>
  2096. * <li>BE_ROSETTE (270x347 mm ; 10.63x13.66 in)</li>
  2097. * <li><b>Old Imperial French</b></li>
  2098. * <li>FR_UNIVERS (1000x1300 mm ; 39.37x51.18 in)</li>
  2099. * <li>FR_DOUBLE_COLOMBIER (900x1260 mm ; 35.43x49.61 in)</li>
  2100. * <li>FR_GRANDE_MONDE (900x1260 mm ; 35.43x49.61 in)</li>
  2101. * <li>FR_DOUBLE_SOLEIL (800x1200 mm ; 31.50x47.24 in)</li>
  2102. * <li>FR_DOUBLE_JESUS (760x1120 mm ; 29.92x44.09 in)</li>
  2103. * <li>FR_GRAND_AIGLE (750x1060 mm ; 29.53x41.73 in)</li>
  2104. * <li>FR_PETIT_AIGLE (700x940 mm ; 27.56x37.01 in)</li>
  2105. * <li>FR_DOUBLE_RAISIN (650x1000 mm ; 25.59x39.37 in)</li>
  2106. * <li>FR_JOURNAL (650x940 mm ; 25.59x37.01 in)</li>
  2107. * <li>FR_COLOMBIER_AFFICHE (630x900 mm ; 24.80x35.43 in)</li>
  2108. * <li>FR_DOUBLE_CAVALIER (620x920 mm ; 24.41x36.22 in)</li>
  2109. * <li>FR_CLOCHE (600x800 mm ; 23.62x31.50 in)</li>
  2110. * <li>FR_SOLEIL (600x800 mm ; 23.62x31.50 in)</li>
  2111. * <li>FR_DOUBLE_CARRE (560x900 mm ; 22.05x35.43 in)</li>
  2112. * <li>FR_DOUBLE_COQUILLE (560x880 mm ; 22.05x34.65 in)</li>
  2113. * <li>FR_JESUS (560x760 mm ; 22.05x29.92 in)</li>
  2114. * <li>FR_RAISIN (500x650 mm ; 19.69x25.59 in)</li>
  2115. * <li>FR_CAVALIER (460x620 mm ; 18.11x24.41 in)</li>
  2116. * <li>FR_DOUBLE_COURONNE (460x720 mm ; 18.11x28.35 in)</li>
  2117. * <li>FR_CARRE (450x560 mm ; 17.72x22.05 in)</li>
  2118. * <li>FR_COQUILLE (440x560 mm ; 17.32x22.05 in)</li>
  2119. * <li>FR_DOUBLE_TELLIERE (440x680 mm ; 17.32x26.77 in)</li>
  2120. * <li>FR_DOUBLE_CLOCHE (400x600 mm ; 15.75x23.62 in)</li>
  2121. * <li>FR_DOUBLE_POT (400x620 mm ; 15.75x24.41 in)</li>
  2122. * <li>FR_ECU (400x520 mm ; 15.75x20.47 in)</li>
  2123. * <li>FR_COURONNE (360x460 mm ; 14.17x18.11 in)</li>
  2124. * <li>FR_TELLIERE (340x440 mm ; 13.39x17.32 in)</li>
  2125. * <li>FR_POT (310x400 mm ; 12.20x15.75 in)</li>
  2126. * </ul>
  2127. * @return array containing page width and height in points
  2128. * @public
  2129. * @since 5.0.010 (2010-05-17)
  2130. */
  2131. public function getPageSizeFromFormat($format) {
  2132. // Paper cordinates are calculated in this way: (inches * 72) where (1 inch = 25.4 mm)
  2133. switch (strtoupper($format)) {
  2134. // ISO 216 A Series + 2 SIS 014711 extensions
  2135. case 'A0' : {$pf = array( 2383.937, 3370.394); break;}
  2136. case 'A1' : {$pf = array( 1683.780, 2383.937); break;}
  2137. case 'A2' : {$pf = array( 1190.551, 1683.780); break;}
  2138. case 'A3' : {$pf = array( 841.890, 1190.551); break;}
  2139. case 'A4' : {$pf = array( 595.276, 841.890); break;}
  2140. case 'A5' : {$pf = array( 419.528, 595.276); break;}
  2141. case 'A6' : {$pf = array( 297.638, 419.528); break;}
  2142. case 'A7' : {$pf = array( 209.764, 297.638); break;}
  2143. case 'A8' : {$pf = array( 147.402, 209.764); break;}
  2144. case 'A9' : {$pf = array( 104.882, 147.402); break;}
  2145. case 'A10': {$pf = array( 73.701, 104.882); break;}
  2146. case 'A11': {$pf = array( 51.024, 73.701); break;}
  2147. case 'A12': {$pf = array( 36.850, 51.024); break;}
  2148. // ISO 216 B Series + 2 SIS 014711 extensions
  2149. case 'B0' : {$pf = array( 2834.646, 4008.189); break;}
  2150. case 'B1' : {$pf = array( 2004.094, 2834.646); break;}
  2151. case 'B2' : {$pf = array( 1417.323, 2004.094); break;}
  2152. case 'B3' : {$pf = array( 1000.630, 1417.323); break;}
  2153. case 'B4' : {$pf = array( 708.661, 1000.630); break;}
  2154. case 'B5' : {$pf = array( 498.898, 708.661); break;}
  2155. case 'B6' : {$pf = array( 354.331, 498.898); break;}
  2156. case 'B7' : {$pf = array( 249.449, 354.331); break;}
  2157. case 'B8' : {$pf = array( 175.748, 249.449); break;}
  2158. case 'B9' : {$pf = array( 124.724, 175.748); break;}
  2159. case 'B10': {$pf = array( 87.874, 124.724); break;}
  2160. case 'B11': {$pf = array( 62.362, 87.874); break;}
  2161. case 'B12': {$pf = array( 42.520, 62.362); break;}
  2162. // ISO 216 C Series + 2 SIS 014711 extensions + 2 EXTENSION
  2163. case 'C0' : {$pf = array( 2599.370, 3676.535); break;}
  2164. case 'C1' : {$pf = array( 1836.850, 2599.370); break;}
  2165. case 'C2' : {$pf = array( 1298.268, 1836.850); break;}
  2166. case 'C3' : {$pf = array( 918.425, 1298.268); break;}
  2167. case 'C4' : {$pf = array( 649.134, 918.425); break;}
  2168. case 'C5' : {$pf = array( 459.213, 649.134); break;}
  2169. case 'C6' : {$pf = array( 323.150, 459.213); break;}
  2170. case 'C7' : {$pf = array( 229.606, 323.150); break;}
  2171. case 'C8' : {$pf = array( 161.575, 229.606); break;}
  2172. case 'C9' : {$pf = array( 113.386, 161.575); break;}
  2173. case 'C10': {$pf = array( 79.370, 113.386); break;}
  2174. case 'C11': {$pf = array( 56.693, 79.370); break;}
  2175. case 'C12': {$pf = array( 39.685, 56.693); break;}
  2176. case 'C76': {$pf = array( 229.606, 459.213); break;}
  2177. case 'DL' : {$pf = array( 311.811, 623.622); break;}
  2178. // SIS 014711 E Series
  2179. case 'E0' : {$pf = array( 2491.654, 3517.795); break;}
  2180. case 'E1' : {$pf = array( 1757.480, 2491.654); break;}
  2181. case 'E2' : {$pf = array( 1247.244, 1757.480); break;}
  2182. case 'E3' : {$pf = array( 878.740, 1247.244); break;}
  2183. case 'E4' : {$pf = array( 623.622, 878.740); break;}
  2184. case 'E5' : {$pf = array( 439.370, 623.622); break;}
  2185. case 'E6' : {$pf = array( 311.811, 439.370); break;}
  2186. case 'E7' : {$pf = array( 221.102, 311.811); break;}
  2187. case 'E8' : {$pf = array( 155.906, 221.102); break;}
  2188. case 'E9' : {$pf = array( 110.551, 155.906); break;}
  2189. case 'E10': {$pf = array( 76.535, 110.551); break;}
  2190. case 'E11': {$pf = array( 53.858, 76.535); break;}
  2191. case 'E12': {$pf = array( 36.850, 53.858); break;}
  2192. // SIS 014711 G Series
  2193. case 'G0' : {$pf = array( 2715.591, 3838.110); break;}
  2194. case 'G1' : {$pf = array( 1919.055, 2715.591); break;}
  2195. case 'G2' : {$pf = array( 1357.795, 1919.055); break;}
  2196. case 'G3' : {$pf = array( 958.110, 1357.795); break;}
  2197. case 'G4' : {$pf = array( 677.480, 958.110); break;}
  2198. case 'G5' : {$pf = array( 479.055, 677.480); break;}
  2199. case 'G6' : {$pf = array( 337.323, 479.055); break;}
  2200. case 'G7' : {$pf = array( 238.110, 337.323); break;}
  2201. case 'G8' : {$pf = array( 167.244, 238.110); break;}
  2202. case 'G9' : {$pf = array( 119.055, 167.244); break;}
  2203. case 'G10': {$pf = array( 82.205, 119.055); break;}
  2204. case 'G11': {$pf = array( 59.528, 82.205); break;}
  2205. case 'G12': {$pf = array( 39.685, 59.528); break;}
  2206. // ISO Press
  2207. case 'RA0': {$pf = array( 2437.795, 3458.268); break;}
  2208. case 'RA1': {$pf = array( 1729.134, 2437.795); break;}
  2209. case 'RA2': {$pf = array( 1218.898, 1729.134); break;}
  2210. case 'RA3': {$pf = array( 864.567, 1218.898); break;}
  2211. case 'RA4': {$pf = array( 609.449, 864.567); break;}
  2212. case 'SRA0': {$pf = array( 2551.181, 3628.346); break;}
  2213. case 'SRA1': {$pf = array( 1814.173, 2551.181); break;}
  2214. case 'SRA2': {$pf = array( 1275.591, 1814.173); break;}
  2215. case 'SRA3': {$pf = array( 907.087, 1275.591); break;}
  2216. case 'SRA4': {$pf = array( 637.795, 907.087); break;}
  2217. // German DIN 476
  2218. case '4A0': {$pf = array( 4767.874, 6740.787); break;}
  2219. case '2A0': {$pf = array( 3370.394, 4767.874); break;}
  2220. // Variations on the ISO Standard
  2221. case 'A2_EXTRA' : {$pf = array( 1261.417, 1754.646); break;}
  2222. case 'A3+' : {$pf = array( 932.598, 1369.134); break;}
  2223. case 'A3_EXTRA' : {$pf = array( 912.756, 1261.417); break;}
  2224. case 'A3_SUPER' : {$pf = array( 864.567, 1440.000); break;}
  2225. case 'SUPER_A3' : {$pf = array( 864.567, 1380.472); break;}
  2226. case 'A4_EXTRA' : {$pf = array( 666.142, 912.756); break;}
  2227. case 'A4_SUPER' : {$pf = array( 649.134, 912.756); break;}
  2228. case 'SUPER_A4' : {$pf = array( 643.465, 1009.134); break;}
  2229. case 'A4_LONG' : {$pf = array( 595.276, 986.457); break;}
  2230. case 'F4' : {$pf = array( 595.276, 935.433); break;}
  2231. case 'SO_B5_EXTRA': {$pf = array( 572.598, 782.362); break;}
  2232. case 'A5_EXTRA' : {$pf = array( 490.394, 666.142); break;}
  2233. // ANSI Series
  2234. case 'ANSI_E': {$pf = array( 2448.000, 3168.000); break;}
  2235. case 'ANSI_D': {$pf = array( 1584.000, 2448.000); break;}
  2236. case 'ANSI_C': {$pf = array( 1224.000, 1584.000); break;}
  2237. case 'ANSI_B': {$pf = array( 792.000, 1224.000); break;}
  2238. case 'ANSI_A': {$pf = array( 612.000, 792.000); break;}
  2239. // Traditional 'Loose' North American Paper Sizes
  2240. case 'USLEDGER':
  2241. case 'LEDGER' : {$pf = array( 1224.000, 792.000); break;}
  2242. case 'ORGANIZERK':
  2243. case 'BIBLE':
  2244. case 'USTABLOID':
  2245. case 'TABLOID': {$pf = array( 792.000, 1224.000); break;}
  2246. case 'ORGANIZERM':
  2247. case 'USLETTER':
  2248. case 'LETTER' : {$pf = array( 612.000, 792.000); break;}
  2249. case 'USLEGAL':
  2250. case 'LEGAL' : {$pf = array( 612.000, 1008.000); break;}
  2251. case 'GOVERNMENTLETTER':
  2252. case 'GLETTER': {$pf = array( 576.000, 756.000); break;}
  2253. case 'JUNIORLEGAL':
  2254. case 'JLEGAL' : {$pf = array( 576.000, 360.000); break;}
  2255. // Other North American Paper Sizes
  2256. case 'QUADDEMY': {$pf = array( 2520.000, 3240.000); break;}
  2257. case 'SUPER_B': {$pf = array( 936.000, 1368.000); break;}
  2258. case 'QUARTO': {$pf = array( 648.000, 792.000); break;}
  2259. case 'GOVERNMENTLEGAL':
  2260. case 'FOLIO': {$pf = array( 612.000, 936.000); break;}
  2261. case 'MONARCH':
  2262. case 'EXECUTIVE': {$pf = array( 522.000, 756.000); break;}
  2263. case 'ORGANIZERL':
  2264. case 'STATEMENT':
  2265. case 'MEMO': {$pf = array( 396.000, 612.000); break;}
  2266. case 'FOOLSCAP': {$pf = array( 595.440, 936.000); break;}
  2267. case 'COMPACT': {$pf = array( 306.000, 486.000); break;}
  2268. case 'ORGANIZERJ': {$pf = array( 198.000, 360.000); break;}
  2269. // Canadian standard CAN 2-9.60M
  2270. case 'P1': {$pf = array( 1587.402, 2437.795); break;}
  2271. case 'P2': {$pf = array( 1218.898, 1587.402); break;}
  2272. case 'P3': {$pf = array( 793.701, 1218.898); break;}
  2273. case 'P4': {$pf = array( 609.449, 793.701); break;}
  2274. case 'P5': {$pf = array( 396.850, 609.449); break;}
  2275. case 'P6': {$pf = array( 303.307, 396.850); break;}
  2276. // North American Architectural Sizes
  2277. case 'ARCH_E' : {$pf = array( 2592.000, 3456.000); break;}
  2278. case 'ARCH_E1': {$pf = array( 2160.000, 3024.000); break;}
  2279. case 'ARCH_D' : {$pf = array( 1728.000, 2592.000); break;}
  2280. case 'BROADSHEET':
  2281. case 'ARCH_C' : {$pf = array( 1296.000, 1728.000); break;}
  2282. case 'ARCH_B' : {$pf = array( 864.000, 1296.000); break;}
  2283. case 'ARCH_A' : {$pf = array( 648.000, 864.000); break;}
  2284. // --- North American Envelope Sizes ---
  2285. // - Announcement Envelopes
  2286. case 'ANNENV_A2' : {$pf = array( 314.640, 414.000); break;}
  2287. case 'ANNENV_A6' : {$pf = array( 342.000, 468.000); break;}
  2288. case 'ANNENV_A7' : {$pf = array( 378.000, 522.000); break;}
  2289. case 'ANNENV_A8' : {$pf = array( 396.000, 584.640); break;}
  2290. case 'ANNENV_A10' : {$pf = array( 450.000, 692.640); break;}
  2291. case 'ANNENV_SLIM': {$pf = array( 278.640, 638.640); break;}
  2292. // - Commercial Envelopes
  2293. case 'COMMENV_N6_1/4': {$pf = array( 252.000, 432.000); break;}
  2294. case 'COMMENV_N6_3/4': {$pf = array( 260.640, 468.000); break;}
  2295. case 'COMMENV_N8' : {$pf = array( 278.640, 540.000); break;}
  2296. case 'COMMENV_N9' : {$pf = array( 278.640, 638.640); break;}
  2297. case 'COMMENV_N10' : {$pf = array( 296.640, 684.000); break;}
  2298. case 'COMMENV_N11' : {$pf = array( 324.000, 746.640); break;}
  2299. case 'COMMENV_N12' : {$pf = array( 342.000, 792.000); break;}
  2300. case 'COMMENV_N14' : {$pf = array( 360.000, 828.000); break;}
  2301. // - Catalogue Envelopes
  2302. case 'CATENV_N1' : {$pf = array( 432.000, 648.000); break;}
  2303. case 'CATENV_N1_3/4' : {$pf = array( 468.000, 684.000); break;}
  2304. case 'CATENV_N2' : {$pf = array( 468.000, 720.000); break;}
  2305. case 'CATENV_N3' : {$pf = array( 504.000, 720.000); break;}
  2306. case 'CATENV_N6' : {$pf = array( 540.000, 756.000); break;}
  2307. case 'CATENV_N7' : {$pf = array( 576.000, 792.000); break;}
  2308. case 'CATENV_N8' : {$pf = array( 594.000, 810.000); break;}
  2309. case 'CATENV_N9_1/2' : {$pf = array( 612.000, 756.000); break;}
  2310. case 'CATENV_N9_3/4' : {$pf = array( 630.000, 810.000); break;}
  2311. case 'CATENV_N10_1/2': {$pf = array( 648.000, 864.000); break;}
  2312. case 'CATENV_N12_1/2': {$pf = array( 684.000, 900.000); break;}
  2313. case 'CATENV_N13_1/2': {$pf = array( 720.000, 936.000); break;}
  2314. case 'CATENV_N14_1/4': {$pf = array( 810.000, 882.000); break;}
  2315. case 'CATENV_N14_1/2': {$pf = array( 828.000, 1044.000); break;}
  2316. // Japanese (JIS P 0138-61) Standard B-Series
  2317. case 'JIS_B0' : {$pf = array( 2919.685, 4127.244); break;}
  2318. case 'JIS_B1' : {$pf = array( 2063.622, 2919.685); break;}
  2319. case 'JIS_B2' : {$pf = array( 1459.843, 2063.622); break;}
  2320. case 'JIS_B3' : {$pf = array( 1031.811, 1459.843); break;}
  2321. case 'JIS_B4' : {$pf = array( 728.504, 1031.811); break;}
  2322. case 'JIS_B5' : {$pf = array( 515.906, 728.504); break;}
  2323. case 'JIS_B6' : {$pf = array( 362.835, 515.906); break;}
  2324. case 'JIS_B7' : {$pf = array( 257.953, 362.835); break;}
  2325. case 'JIS_B8' : {$pf = array( 181.417, 257.953); break;}
  2326. case 'JIS_B9' : {$pf = array( 127.559, 181.417); break;}
  2327. case 'JIS_B10': {$pf = array( 90.709, 127.559); break;}
  2328. case 'JIS_B11': {$pf = array( 62.362, 90.709); break;}
  2329. case 'JIS_B12': {$pf = array( 45.354, 62.362); break;}
  2330. // PA Series
  2331. case 'PA0' : {$pf = array( 2381.102, 3174.803,); break;}
  2332. case 'PA1' : {$pf = array( 1587.402, 2381.102); break;}
  2333. case 'PA2' : {$pf = array( 1190.551, 1587.402); break;}
  2334. case 'PA3' : {$pf = array( 793.701, 1190.551); break;}
  2335. case 'PA4' : {$pf = array( 595.276, 793.701); break;}
  2336. case 'PA5' : {$pf = array( 396.850, 595.276); break;}
  2337. case 'PA6' : {$pf = array( 297.638, 396.850); break;}
  2338. case 'PA7' : {$pf = array( 198.425, 297.638); break;}
  2339. case 'PA8' : {$pf = array( 147.402, 198.425); break;}
  2340. case 'PA9' : {$pf = array( 99.213, 147.402); break;}
  2341. case 'PA10': {$pf = array( 73.701, 99.213); break;}
  2342. // Standard Photographic Print Sizes
  2343. case 'PASSPORT_PHOTO': {$pf = array( 99.213, 127.559); break;}
  2344. case 'E' : {$pf = array( 233.858, 340.157); break;}
  2345. case 'L':
  2346. case '3R' : {$pf = array( 252.283, 360.000); break;}
  2347. case 'KG':
  2348. case '4R' : {$pf = array( 289.134, 430.866); break;}
  2349. case '4D' : {$pf = array( 340.157, 430.866); break;}
  2350. case '2L':
  2351. case '5R' : {$pf = array( 360.000, 504.567); break;}
  2352. case '8P':
  2353. case '6R' : {$pf = array( 430.866, 575.433); break;}
  2354. case '6P':
  2355. case '8R' : {$pf = array( 575.433, 720.000); break;}
  2356. case '6PW':
  2357. case 'S8R' : {$pf = array( 575.433, 864.567); break;}
  2358. case '4P':
  2359. case '10R' : {$pf = array( 720.000, 864.567); break;}
  2360. case '4PW':
  2361. case 'S10R': {$pf = array( 720.000, 1080.000); break;}
  2362. case '11R' : {$pf = array( 790.866, 1009.134); break;}
  2363. case 'S11R': {$pf = array( 790.866, 1224.567); break;}
  2364. case '12R' : {$pf = array( 864.567, 1080.000); break;}
  2365. case 'S12R': {$pf = array( 864.567, 1292.598); break;}
  2366. // Common Newspaper Sizes
  2367. case 'NEWSPAPER_BROADSHEET': {$pf = array( 2125.984, 1700.787); break;}
  2368. case 'NEWSPAPER_BERLINER' : {$pf = array( 1332.283, 892.913); break;}
  2369. case 'NEWSPAPER_TABLOID':
  2370. case 'NEWSPAPER_COMPACT' : {$pf = array( 1218.898, 793.701); break;}
  2371. // Business Cards
  2372. case 'CREDIT_CARD':
  2373. case 'BUSINESS_CARD':
  2374. case 'BUSINESS_CARD_ISO7810': {$pf = array( 153.014, 242.646); break;}
  2375. case 'BUSINESS_CARD_ISO216' : {$pf = array( 147.402, 209.764); break;}
  2376. case 'BUSINESS_CARD_IT':
  2377. case 'BUSINESS_CARD_UK':
  2378. case 'BUSINESS_CARD_FR':
  2379. case 'BUSINESS_CARD_DE':
  2380. case 'BUSINESS_CARD_ES' : {$pf = array( 155.906, 240.945); break;}
  2381. case 'BUSINESS_CARD_CA':
  2382. case 'BUSINESS_CARD_US' : {$pf = array( 144.567, 252.283); break;}
  2383. case 'BUSINESS_CARD_JP' : {$pf = array( 155.906, 257.953); break;}
  2384. case 'BUSINESS_CARD_HK' : {$pf = array( 153.071, 255.118); break;}
  2385. case 'BUSINESS_CARD_AU':
  2386. case 'BUSINESS_CARD_DK':
  2387. case 'BUSINESS_CARD_SE' : {$pf = array( 155.906, 255.118); break;}
  2388. case 'BUSINESS_CARD_RU':
  2389. case 'BUSINESS_CARD_CZ':
  2390. case 'BUSINESS_CARD_FI':
  2391. case 'BUSINESS_CARD_HU':
  2392. case 'BUSINESS_CARD_IL' : {$pf = array( 141.732, 255.118); break;}
  2393. // Billboards
  2394. case '4SHEET' : {$pf = array( 2880.000, 4320.000); break;}
  2395. case '6SHEET' : {$pf = array( 3401.575, 5102.362); break;}
  2396. case '12SHEET': {$pf = array( 8640.000, 4320.000); break;}
  2397. case '16SHEET': {$pf = array( 5760.000, 8640.000); break;}
  2398. case '32SHEET': {$pf = array(11520.000, 8640.000); break;}
  2399. case '48SHEET': {$pf = array(17280.000, 8640.000); break;}
  2400. case '64SHEET': {$pf = array(23040.000, 8640.000); break;}
  2401. case '96SHEET': {$pf = array(34560.000, 8640.000); break;}
  2402. // Old European Sizes
  2403. // - Old Imperial English Sizes
  2404. case 'EN_EMPEROR' : {$pf = array( 3456.000, 5184.000); break;}
  2405. case 'EN_ANTIQUARIAN' : {$pf = array( 2232.000, 3816.000); break;}
  2406. case 'EN_GRAND_EAGLE' : {$pf = array( 2070.000, 3024.000); break;}
  2407. case 'EN_DOUBLE_ELEPHANT' : {$pf = array( 1926.000, 2880.000); break;}
  2408. case 'EN_ATLAS' : {$pf = array( 1872.000, 2448.000); break;}
  2409. case 'EN_COLOMBIER' : {$pf = array( 1692.000, 2484.000); break;}
  2410. case 'EN_ELEPHANT' : {$pf = array( 1656.000, 2016.000); break;}
  2411. case 'EN_DOUBLE_DEMY' : {$pf = array( 1620.000, 2556.000); break;}
  2412. case 'EN_IMPERIAL' : {$pf = array( 1584.000, 2160.000); break;}
  2413. case 'EN_PRINCESS' : {$pf = array( 1548.000, 2016.000); break;}
  2414. case 'EN_CARTRIDGE' : {$pf = array( 1512.000, 1872.000); break;}
  2415. case 'EN_DOUBLE_LARGE_POST': {$pf = array( 1512.000, 2376.000); break;}
  2416. case 'EN_ROYAL' : {$pf = array( 1440.000, 1800.000); break;}
  2417. case 'EN_SHEET':
  2418. case 'EN_HALF_POST' : {$pf = array( 1404.000, 1692.000); break;}
  2419. case 'EN_SUPER_ROYAL' : {$pf = array( 1368.000, 1944.000); break;}
  2420. case 'EN_DOUBLE_POST' : {$pf = array( 1368.000, 2196.000); break;}
  2421. case 'EN_MEDIUM' : {$pf = array( 1260.000, 1656.000); break;}
  2422. case 'EN_DEMY' : {$pf = array( 1260.000, 1620.000); break;}
  2423. case 'EN_LARGE_POST' : {$pf = array( 1188.000, 1512.000); break;}
  2424. case 'EN_COPY_DRAUGHT' : {$pf = array( 1152.000, 1440.000); break;}
  2425. case 'EN_POST' : {$pf = array( 1116.000, 1386.000); break;}
  2426. case 'EN_CROWN' : {$pf = array( 1080.000, 1440.000); break;}
  2427. case 'EN_PINCHED_POST' : {$pf = array( 1062.000, 1332.000); break;}
  2428. case 'EN_BRIEF' : {$pf = array( 972.000, 1152.000); break;}
  2429. case 'EN_FOOLSCAP' : {$pf = array( 972.000, 1224.000); break;}
  2430. case 'EN_SMALL_FOOLSCAP' : {$pf = array( 954.000, 1188.000); break;}
  2431. case 'EN_POTT' : {$pf = array( 900.000, 1080.000); break;}
  2432. // - Old Imperial Belgian Sizes
  2433. case 'BE_GRAND_AIGLE' : {$pf = array( 1984.252, 2948.031); break;}
  2434. case 'BE_COLOMBIER' : {$pf = array( 1757.480, 2409.449); break;}
  2435. case 'BE_DOUBLE_CARRE': {$pf = array( 1757.480, 2607.874); break;}
  2436. case 'BE_ELEPHANT' : {$pf = array( 1746.142, 2182.677); break;}
  2437. case 'BE_PETIT_AIGLE' : {$pf = array( 1700.787, 2381.102); break;}
  2438. case 'BE_GRAND_JESUS' : {$pf = array( 1559.055, 2069.291); break;}
  2439. case 'BE_JESUS' : {$pf = array( 1530.709, 2069.291); break;}
  2440. case 'BE_RAISIN' : {$pf = array( 1417.323, 1842.520); break;}
  2441. case 'BE_GRAND_MEDIAN': {$pf = array( 1303.937, 1714.961); break;}
  2442. case 'BE_DOUBLE_POSTE': {$pf = array( 1233.071, 1601.575); break;}
  2443. case 'BE_COQUILLE' : {$pf = array( 1218.898, 1587.402); break;}
  2444. case 'BE_PETIT_MEDIAN': {$pf = array( 1176.378, 1502.362); break;}
  2445. case 'BE_RUCHE' : {$pf = array( 1020.472, 1303.937); break;}
  2446. case 'BE_PROPATRIA' : {$pf = array( 977.953, 1218.898); break;}
  2447. case 'BE_LYS' : {$pf = array( 898.583, 1125.354); break;}
  2448. case 'BE_POT' : {$pf = array( 870.236, 1088.504); break;}
  2449. case 'BE_ROSETTE' : {$pf = array( 765.354, 983.622); break;}
  2450. // - Old Imperial French Sizes
  2451. case 'FR_UNIVERS' : {$pf = array( 2834.646, 3685.039); break;}
  2452. case 'FR_DOUBLE_COLOMBIER' : {$pf = array( 2551.181, 3571.654); break;}
  2453. case 'FR_GRANDE_MONDE' : {$pf = array( 2551.181, 3571.654); break;}
  2454. case 'FR_DOUBLE_SOLEIL' : {$pf = array( 2267.717, 3401.575); break;}
  2455. case 'FR_DOUBLE_JESUS' : {$pf = array( 2154.331, 3174.803); break;}
  2456. case 'FR_GRAND_AIGLE' : {$pf = array( 2125.984, 3004.724); break;}
  2457. case 'FR_PETIT_AIGLE' : {$pf = array( 1984.252, 2664.567); break;}
  2458. case 'FR_DOUBLE_RAISIN' : {$pf = array( 1842.520, 2834.646); break;}
  2459. case 'FR_JOURNAL' : {$pf = array( 1842.520, 2664.567); break;}
  2460. case 'FR_COLOMBIER_AFFICHE': {$pf = array( 1785.827, 2551.181); break;}
  2461. case 'FR_DOUBLE_CAVALIER' : {$pf = array( 1757.480, 2607.874); break;}
  2462. case 'FR_CLOCHE' : {$pf = array( 1700.787, 2267.717); break;}
  2463. case 'FR_SOLEIL' : {$pf = array( 1700.787, 2267.717); break;}
  2464. case 'FR_DOUBLE_CARRE' : {$pf = array( 1587.402, 2551.181); break;}
  2465. case 'FR_DOUBLE_COQUILLE' : {$pf = array( 1587.402, 2494.488); break;}
  2466. case 'FR_JESUS' : {$pf = array( 1587.402, 2154.331); break;}
  2467. case 'FR_RAISIN' : {$pf = array( 1417.323, 1842.520); break;}
  2468. case 'FR_CAVALIER' : {$pf = array( 1303.937, 1757.480); break;}
  2469. case 'FR_DOUBLE_COURONNE' : {$pf = array( 1303.937, 2040.945); break;}
  2470. case 'FR_CARRE' : {$pf = array( 1275.591, 1587.402); break;}
  2471. case 'FR_COQUILLE' : {$pf = array( 1247.244, 1587.402); break;}
  2472. case 'FR_DOUBLE_TELLIERE' : {$pf = array( 1247.244, 1927.559); break;}
  2473. case 'FR_DOUBLE_CLOCHE' : {$pf = array( 1133.858, 1700.787); break;}
  2474. case 'FR_DOUBLE_POT' : {$pf = array( 1133.858, 1757.480); break;}
  2475. case 'FR_ECU' : {$pf = array( 1133.858, 1474.016); break;}
  2476. case 'FR_COURONNE' : {$pf = array( 1020.472, 1303.937); break;}
  2477. case 'FR_TELLIERE' : {$pf = array( 963.780, 1247.244); break;}
  2478. case 'FR_POT' : {$pf = array( 878.740, 1133.858); break;}
  2479. // DEFAULT ISO A4
  2480. default: {$pf = array( 595.276, 841.890); break;}
  2481. }
  2482. return $pf;
  2483. }
  2484. /**
  2485. * Change the format of the current page
  2486. * @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>
  2487. * <li>['format'] = page format name (one of the above);</li>
  2488. * <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>
  2489. * <li>['PZ'] : The page's preferred zoom (magnification) factor.</li>
  2490. * <li>['MediaBox'] : the boundaries of the physical medium on which the page shall be displayed or printed:</li>
  2491. * <li>['MediaBox']['llx'] : lower-left x coordinate in points</li>
  2492. * <li>['MediaBox']['lly'] : lower-left y coordinate in points</li>
  2493. * <li>['MediaBox']['urx'] : upper-right x coordinate in points</li>
  2494. * <li>['MediaBox']['ury'] : upper-right y coordinate in points</li>
  2495. * <li>['CropBox'] : the visible region of default user space:</li>
  2496. * <li>['CropBox']['llx'] : lower-left x coordinate in points</li>
  2497. * <li>['CropBox']['lly'] : lower-left y coordinate in points</li>
  2498. * <li>['CropBox']['urx'] : upper-right x coordinate in points</li>
  2499. * <li>['CropBox']['ury'] : upper-right y coordinate in points</li>
  2500. * <li>['BleedBox'] : the region to which the contents of the page shall be clipped when output in a production environment:</li>
  2501. * <li>['BleedBox']['llx'] : lower-left x coordinate in points</li>
  2502. * <li>['BleedBox']['lly'] : lower-left y coordinate in points</li>
  2503. * <li>['BleedBox']['urx'] : upper-right x coordinate in points</li>
  2504. * <li>['BleedBox']['ury'] : upper-right y coordinate in points</li>
  2505. * <li>['TrimBox'] : the intended dimensions of the finished page after trimming:</li>
  2506. * <li>['TrimBox']['llx'] : lower-left x coordinate in points</li>
  2507. * <li>['TrimBox']['lly'] : lower-left y coordinate in points</li>
  2508. * <li>['TrimBox']['urx'] : upper-right x coordinate in points</li>
  2509. * <li>['TrimBox']['ury'] : upper-right y coordinate in points</li>
  2510. * <li>['ArtBox'] : the extent of the page's meaningful content:</li>
  2511. * <li>['ArtBox']['llx'] : lower-left x coordinate in points</li>
  2512. * <li>['ArtBox']['lly'] : lower-left y coordinate in points</li>
  2513. * <li>['ArtBox']['urx'] : upper-right x coordinate in points</li>
  2514. * <li>['ArtBox']['ury'] : upper-right y coordinate in points</li>
  2515. * <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>
  2516. * <li>['BoxColorInfo'][BOXTYPE]['C'] : an array of three numbers in the range 0-255, representing the components in the DeviceRGB colour space.</li>
  2517. * <li>['BoxColorInfo'][BOXTYPE]['W'] : the guideline width in default user units</li>
  2518. * <li>['BoxColorInfo'][BOXTYPE]['S'] : the guideline style: S = Solid; D = Dashed</li>
  2519. * <li>['BoxColorInfo'][BOXTYPE]['D'] : dash array defining a pattern of dashes and gaps to be used in drawing dashed guidelines</li>
  2520. * <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>
  2521. * <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>
  2522. * <li>['trans']['S'] : transition style : Split, Blinds, Box, Wipe, Dissolve, Glitter, R, Fly, Push, Cover, Uncover, Fade</li>
  2523. * <li>['trans']['D'] : The duration of the transition effect, in seconds.</li>
  2524. * <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>
  2525. * <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>
  2526. * <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>
  2527. * <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>
  2528. * <li>['trans']['B'] : (Fly transition style only) If true, the area that shall be flown in is rectangular and opaque. Default: false.</li>
  2529. * </ul>
  2530. * @param $orientation (string) page orientation. Possible values are (case insensitive):<ul>
  2531. * <li>P or Portrait (default)</li>
  2532. * <li>L or Landscape</li>
  2533. * <li>'' (empty string) for automatic orientation</li>
  2534. * </ul>
  2535. * @protected
  2536. * @since 3.0.015 (2008-06-06)
  2537. * @see getPageSizeFromFormat()
  2538. */
  2539. protected function setPageFormat($format, $orientation='P') {
  2540. if (!empty($format) AND isset($this->pagedim[$this->page])) {
  2541. // remove inherited values
  2542. unset($this->pagedim[$this->page]);
  2543. }
  2544. if (is_string($format)) {
  2545. // get page measures from format name
  2546. $pf = $this->getPageSizeFromFormat($format);
  2547. $this->fwPt = $pf[0];
  2548. $this->fhPt = $pf[1];
  2549. } else {
  2550. // the boundaries of the physical medium on which the page shall be displayed or printed
  2551. if (isset($format['MediaBox'])) {
  2552. $this->setPageBoxes($this->page, 'MediaBox', $format['MediaBox']['llx'], $format['MediaBox']['lly'], $format['MediaBox']['urx'], $format['MediaBox']['ury'], false);
  2553. $this->fwPt = (($format['MediaBox']['urx'] - $format['MediaBox']['llx']) * $this->k);
  2554. $this->fhPt = (($format['MediaBox']['ury'] - $format['MediaBox']['lly']) * $this->k);
  2555. } else {
  2556. if (isset($format[0]) AND is_numeric($format[0]) AND isset($format[1]) AND is_numeric($format[1])) {
  2557. $pf = array(($format[0] * $this->k), ($format[1] * $this->k));
  2558. } else {
  2559. if (!isset($format['format'])) {
  2560. // default value
  2561. $format['format'] = 'A4';
  2562. }
  2563. $pf = $this->getPageSizeFromFormat($format['format']);
  2564. }
  2565. $this->fwPt = $pf[0];
  2566. $this->fhPt = $pf[1];
  2567. $this->setPageBoxes($this->page, 'MediaBox', 0, 0, $this->fwPt, $this->fhPt, true);
  2568. }
  2569. // the visible region of default user space
  2570. if (isset($format['CropBox'])) {
  2571. $this->setPageBoxes($this->page, 'CropBox', $format['CropBox']['llx'], $format['CropBox']['lly'], $format['CropBox']['urx'], $format['CropBox']['ury'], false);
  2572. }
  2573. // the region to which the contents of the page shall be clipped when output in a production environment
  2574. if (isset($format['BleedBox'])) {
  2575. $this->setPageBoxes($this->page, 'BleedBox', $format['BleedBox']['llx'], $format['BleedBox']['lly'], $format['BleedBox']['urx'], $format['BleedBox']['ury'], false);
  2576. }
  2577. // the intended dimensions of the finished page after trimming
  2578. if (isset($format['TrimBox'])) {
  2579. $this->setPageBoxes($this->page, 'TrimBox', $format['TrimBox']['llx'], $format['TrimBox']['lly'], $format['TrimBox']['urx'], $format['TrimBox']['ury'], false);
  2580. }
  2581. // the page's meaningful content (including potential white space)
  2582. if (isset($format['ArtBox'])) {
  2583. $this->setPageBoxes($this->page, 'ArtBox', $format['ArtBox']['llx'], $format['ArtBox']['lly'], $format['ArtBox']['urx'], $format['ArtBox']['ury'], false);
  2584. }
  2585. // specify the colours and other visual characteristics that should be used in displaying guidelines on the screen for the various page boundaries
  2586. if (isset($format['BoxColorInfo'])) {
  2587. $this->pagedim[$this->page]['BoxColorInfo'] = $format['BoxColorInfo'];
  2588. }
  2589. if (isset($format['Rotate']) AND (($format['Rotate'] % 90) == 0)) {
  2590. // The number of degrees by which the page shall be rotated clockwise when displayed or printed. The value shall be a multiple of 90.
  2591. $this->pagedim[$this->page]['Rotate'] = intval($format['Rotate']);
  2592. }
  2593. if (isset($format['PZ'])) {
  2594. // The page's preferred zoom (magnification) factor
  2595. $this->pagedim[$this->page]['PZ'] = floatval($format['PZ']);
  2596. }
  2597. if (isset($format['trans'])) {
  2598. // The style and duration of the visual transition to use when moving from another page to the given page during a presentation
  2599. if (isset($format['trans']['Dur'])) {
  2600. // The page's display duration
  2601. $this->pagedim[$this->page]['trans']['Dur'] = floatval($format['trans']['Dur']);
  2602. }
  2603. $stansition_styles = array('Split', 'Blinds', 'Box', 'Wipe', 'Dissolve', 'Glitter', 'R', 'Fly', 'Push', 'Cover', 'Uncover', 'Fade');
  2604. if (isset($format['trans']['S']) AND in_array($format['trans']['S'], $stansition_styles)) {
  2605. // The transition style that shall be used when moving to this page from another during a presentation
  2606. $this->pagedim[$this->page]['trans']['S'] = $format['trans']['S'];
  2607. $valid_effect = array('Split', 'Blinds');
  2608. $valid_vals = array('H', 'V');
  2609. if (isset($format['trans']['Dm']) AND in_array($format['trans']['S'], $valid_effect) AND in_array($format['trans']['Dm'], $valid_vals)) {
  2610. $this->pagedim[$this->page]['trans']['Dm'] = $format['trans']['Dm'];
  2611. }
  2612. $valid_effect = array('Split', 'Box', 'Fly');
  2613. $valid_vals = array('I', 'O');
  2614. if (isset($format['trans']['M']) AND in_array($format['trans']['S'], $valid_effect) AND in_array($format['trans']['M'], $valid_vals)) {
  2615. $this->pagedim[$this->page]['trans']['M'] = $format['trans']['M'];
  2616. }
  2617. $valid_effect = array('Wipe', 'Glitter', 'Fly', 'Cover', 'Uncover', 'Push');
  2618. if (isset($format['trans']['Di']) AND in_array($format['trans']['S'], $valid_effect)) {
  2619. if (((($format['trans']['Di'] == 90) OR ($format['trans']['Di'] == 180)) AND ($format['trans']['S'] == 'Wipe'))
  2620. OR (($format['trans']['Di'] == 315) AND ($format['trans']['S'] == 'Glitter'))
  2621. OR (($format['trans']['Di'] == 0) OR ($format['trans']['Di'] == 270))) {
  2622. $this->pagedim[$this->page]['trans']['Di'] = intval($format['trans']['Di']);
  2623. }
  2624. }
  2625. if (isset($format['trans']['SS']) AND ($format['trans']['S'] == 'Fly')) {
  2626. $this->pagedim[$this->page]['trans']['SS'] = floatval($format['trans']['SS']);
  2627. }
  2628. if (isset($format['trans']['B']) AND ($format['trans']['B'] === true) AND ($format['trans']['S'] == 'Fly')) {
  2629. $this->pagedim[$this->page]['trans']['B'] = 'true';
  2630. }
  2631. } else {
  2632. $this->pagedim[$this->page]['trans']['S'] = 'R';
  2633. }
  2634. if (isset($format['trans']['D'])) {
  2635. // The duration of the transition effect, in seconds
  2636. $this->pagedim[$this->page]['trans']['D'] = floatval($format['trans']['D']);
  2637. } else {
  2638. $this->pagedim[$this->page]['trans']['D'] = 1;
  2639. }
  2640. }
  2641. }
  2642. $this->setPageOrientation($orientation);
  2643. }
  2644. /**
  2645. * Set page boundaries.
  2646. * @param $page (int) page number
  2647. * @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>
  2648. * @param $llx (float) lower-left x coordinate in user units
  2649. * @param $lly (float) lower-left y coordinate in user units
  2650. * @param $urx (float) upper-right x coordinate in user units
  2651. * @param $ury (float) upper-right y coordinate in user units
  2652. * @param $points (boolean) if true uses user units as unit of measure, otherwise uses PDF points
  2653. * @public
  2654. * @since 5.0.010 (2010-05-17)
  2655. */
  2656. public function setPageBoxes($page, $type, $llx, $lly, $urx, $ury, $points=false) {
  2657. if (!isset($this->pagedim[$page])) {
  2658. // initialize array
  2659. $this->pagedim[$page] = array();
  2660. }
  2661. $pageboxes = array('MediaBox', 'CropBox', 'BleedBox', 'TrimBox', 'ArtBox');
  2662. if (!in_array($type, $pageboxes)) {
  2663. return;
  2664. }
  2665. if ($points) {
  2666. $k = 1;
  2667. } else {
  2668. $k = $this->k;
  2669. }
  2670. $this->pagedim[$page][$type]['llx'] = ($llx * $k);
  2671. $this->pagedim[$page][$type]['lly'] = ($lly * $k);
  2672. $this->pagedim[$page][$type]['urx'] = ($urx * $k);
  2673. $this->pagedim[$page][$type]['ury'] = ($ury * $k);
  2674. }
  2675. /**
  2676. * Swap X and Y coordinates of page boxes (change page boxes orientation).
  2677. * @param $page (int) page number
  2678. * @protected
  2679. * @since 5.0.010 (2010-05-17)
  2680. */
  2681. protected function swapPageBoxCoordinates($page) {
  2682. $pageboxes = array('MediaBox', 'CropBox', 'BleedBox', 'TrimBox', 'ArtBox');
  2683. foreach ($pageboxes as $type) {
  2684. // swap X and Y coordinates
  2685. if (isset($this->pagedim[$page][$type])) {
  2686. $tmp = $this->pagedim[$page][$type]['llx'];
  2687. $this->pagedim[$page][$type]['llx'] = $this->pagedim[$page][$type]['lly'];
  2688. $this->pagedim[$page][$type]['lly'] = $tmp;
  2689. $tmp = $this->pagedim[$page][$type]['urx'];
  2690. $this->pagedim[$page][$type]['urx'] = $this->pagedim[$page][$type]['ury'];
  2691. $this->pagedim[$page][$type]['ury'] = $tmp;
  2692. }
  2693. }
  2694. }
  2695. /**
  2696. * Set page orientation.
  2697. * @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>
  2698. * @param $autopagebreak (boolean) Boolean indicating if auto-page-break mode should be on or off.
  2699. * @param $bottommargin (float) bottom margin of the page.
  2700. * @public
  2701. * @since 3.0.015 (2008-06-06)
  2702. */
  2703. public function setPageOrientation($orientation, $autopagebreak='', $bottommargin='') {
  2704. if (!isset($this->pagedim[$this->page]['MediaBox'])) {
  2705. // the boundaries of the physical medium on which the page shall be displayed or printed
  2706. $this->setPageBoxes($this->page, 'MediaBox', 0, 0, $this->fwPt, $this->fhPt, true);
  2707. }
  2708. if (!isset($this->pagedim[$this->page]['CropBox'])) {
  2709. // the visible region of default user space
  2710. $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);
  2711. }
  2712. if (!isset($this->pagedim[$this->page]['BleedBox'])) {
  2713. // the region to which the contents of the page shall be clipped when output in a production environment
  2714. $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);
  2715. }
  2716. if (!isset($this->pagedim[$this->page]['TrimBox'])) {
  2717. // the intended dimensions of the finished page after trimming
  2718. $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);
  2719. }
  2720. if (!isset($this->pagedim[$this->page]['ArtBox'])) {
  2721. // the page's meaningful content (including potential white space)
  2722. $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);
  2723. }
  2724. if (!isset($this->pagedim[$this->page]['Rotate'])) {
  2725. // The number of degrees by which the page shall be rotated clockwise when displayed or printed. The value shall be a multiple of 90.
  2726. $this->pagedim[$this->page]['Rotate'] = 0;
  2727. }
  2728. if (!isset($this->pagedim[$this->page]['PZ'])) {
  2729. // The page's preferred zoom (magnification) factor
  2730. $this->pagedim[$this->page]['PZ'] = 1;
  2731. }
  2732. if ($this->fwPt > $this->fhPt) {
  2733. // landscape
  2734. $default_orientation = 'L';
  2735. } else {
  2736. // portrait
  2737. $default_orientation = 'P';
  2738. }
  2739. $valid_orientations = array('P', 'L');
  2740. if (empty($orientation)) {
  2741. $orientation = $default_orientation;
  2742. } else {
  2743. $orientation = strtoupper($orientation{0});
  2744. }
  2745. if (in_array($orientation, $valid_orientations) AND ($orientation != $default_orientation)) {
  2746. $this->CurOrientation = $orientation;
  2747. $this->wPt = $this->fhPt;
  2748. $this->hPt = $this->fwPt;
  2749. } else {
  2750. $this->CurOrientation = $default_orientation;
  2751. $this->wPt = $this->fwPt;
  2752. $this->hPt = $this->fhPt;
  2753. }
  2754. if ((abs($this->pagedim[$this->page]['MediaBox']['urx'] - $this->hPt) < $this->feps) AND (abs($this->pagedim[$this->page]['MediaBox']['ury'] - $this->wPt) < $this->feps)){
  2755. // swap X and Y coordinates (change page orientation)
  2756. $this->swapPageBoxCoordinates($this->page);
  2757. }
  2758. $this->w = $this->wPt / $this->k;
  2759. $this->h = $this->hPt / $this->k;
  2760. if ($this->empty_string($autopagebreak)) {
  2761. if (isset($this->AutoPageBreak)) {
  2762. $autopagebreak = $this->AutoPageBreak;
  2763. } else {
  2764. $autopagebreak = true;
  2765. }
  2766. }
  2767. if ($this->empty_string($bottommargin)) {
  2768. if (isset($this->bMargin)) {
  2769. $bottommargin = $this->bMargin;
  2770. } else {
  2771. // default value = 2 cm
  2772. $bottommargin = 2 * 28.35 / $this->k;
  2773. }
  2774. }
  2775. $this->SetAutoPageBreak($autopagebreak, $bottommargin);
  2776. // store page dimensions
  2777. $this->pagedim[$this->page]['w'] = $this->wPt;
  2778. $this->pagedim[$this->page]['h'] = $this->hPt;
  2779. $this->pagedim[$this->page]['wk'] = $this->w;
  2780. $this->pagedim[$this->page]['hk'] = $this->h;
  2781. $this->pagedim[$this->page]['tm'] = $this->tMargin;
  2782. $this->pagedim[$this->page]['bm'] = $bottommargin;
  2783. $this->pagedim[$this->page]['lm'] = $this->lMargin;
  2784. $this->pagedim[$this->page]['rm'] = $this->rMargin;
  2785. $this->pagedim[$this->page]['pb'] = $autopagebreak;
  2786. $this->pagedim[$this->page]['or'] = $this->CurOrientation;
  2787. $this->pagedim[$this->page]['olm'] = $this->original_lMargin;
  2788. $this->pagedim[$this->page]['orm'] = $this->original_rMargin;
  2789. }
  2790. /**
  2791. * Set regular expression to detect withespaces or word separators.
  2792. * The pattern delimiter must be the forward-slash character "/".
  2793. * Some example patterns are:
  2794. * <pre>
  2795. * Non-Unicode or missing PCRE unicode support: "/[^\S\xa0]/"
  2796. * Unicode and PCRE unicode support: "/[^\S\P{Z}\xa0]/u"
  2797. * Unicode and PCRE unicode support in Chinese mode: "/[^\S\P{Z}\P{Lo}\xa0]/u"
  2798. * if PCRE unicode support is turned ON ("\P" is the negate class of "\p"):
  2799. * "\p{Z}" or "\p{Separator}": any kind of Unicode whitespace or invisible separator.
  2800. * "\p{Lo}" or "\p{Other_Letter}": a Unicode letter or ideograph that does not have lowercase and uppercase variants.
  2801. * "\p{Lo}" is needed for Chinese characters because are packed next to each other without spaces in between.
  2802. * </pre>
  2803. * @param $re (string) regular expression (leave empty for default).
  2804. * @public
  2805. * @since 4.6.016 (2009-06-15)
  2806. */
  2807. public function setSpacesRE($re='/[^\S\xa0]/') {
  2808. $this->re_spaces = $re;
  2809. $re_parts = explode('/', $re);
  2810. // get pattern parts
  2811. $this->re_space = array();
  2812. if (isset($re_parts[1]) AND !empty($re_parts[1])) {
  2813. $this->re_space['p'] = $re_parts[1];
  2814. } else {
  2815. $this->re_space['p'] = '[\s]';
  2816. }
  2817. // set pattern modifiers
  2818. if (isset($re_parts[2]) AND !empty($re_parts[2])) {
  2819. $this->re_space['m'] = $re_parts[2];
  2820. } else {
  2821. $this->re_space['m'] = '';
  2822. }
  2823. }
  2824. /**
  2825. * Enable or disable Right-To-Left language mode
  2826. * @param $enable (Boolean) if true enable Right-To-Left language mode.
  2827. * @param $resetx (Boolean) if true reset the X position on direction change.
  2828. * @public
  2829. * @since 2.0.000 (2008-01-03)
  2830. */
  2831. public function setRTL($enable, $resetx=true) {
  2832. $enable = $enable ? true : false;
  2833. $resetx = ($resetx AND ($enable != $this->rtl));
  2834. $this->rtl = $enable;
  2835. $this->tmprtl = false;
  2836. if ($resetx) {
  2837. $this->Ln(0);
  2838. }
  2839. }
  2840. /**
  2841. * Return the RTL status
  2842. * @return boolean
  2843. * @public
  2844. * @since 4.0.012 (2008-07-24)
  2845. */
  2846. public function getRTL() {
  2847. return $this->rtl;
  2848. }
  2849. /**
  2850. * Force temporary RTL language direction
  2851. * @param $mode (mixed) can be false, 'L' for LTR or 'R' for RTL
  2852. * @public
  2853. * @since 2.1.000 (2008-01-09)
  2854. */
  2855. public function setTempRTL($mode) {
  2856. $newmode = false;
  2857. switch (strtoupper($mode)) {
  2858. case 'LTR':
  2859. case 'L': {
  2860. if ($this->rtl) {
  2861. $newmode = 'L';
  2862. }
  2863. break;
  2864. }
  2865. case 'RTL':
  2866. case 'R': {
  2867. if (!$this->rtl) {
  2868. $newmode = 'R';
  2869. }
  2870. break;
  2871. }
  2872. case false:
  2873. default: {
  2874. $newmode = false;
  2875. break;
  2876. }
  2877. }
  2878. $this->tmprtl = $newmode;
  2879. }
  2880. /**
  2881. * Return the current temporary RTL status
  2882. * @return boolean
  2883. * @public
  2884. * @since 4.8.014 (2009-11-04)
  2885. */
  2886. public function isRTLTextDir() {
  2887. return ($this->rtl OR ($this->tmprtl == 'R'));
  2888. }
  2889. /**
  2890. * Set the last cell height.
  2891. * @param $h (float) cell height.
  2892. * @author Nicola Asuni
  2893. * @public
  2894. * @since 1.53.0.TC034
  2895. */
  2896. public function setLastH($h) {
  2897. $this->lasth = $h;
  2898. }
  2899. /**
  2900. * Reset the last cell height.
  2901. * @public
  2902. * @since 5.9.000 (2010-10-03)
  2903. */
  2904. public function resetLastH() {
  2905. $this->lasth = ($this->FontSize * $this->cell_height_ratio) + $this->cell_padding['T'] + $this->cell_padding['B'];
  2906. }
  2907. /**
  2908. * Get the last cell height.
  2909. * @return last cell height
  2910. * @public
  2911. * @since 4.0.017 (2008-08-05)
  2912. */
  2913. public function getLastH() {
  2914. return $this->lasth;
  2915. }
  2916. /**
  2917. * Set the adjusting factor to convert pixels to user units.
  2918. * @param $scale (float) adjusting factor to convert pixels to user units.
  2919. * @author Nicola Asuni
  2920. * @public
  2921. * @since 1.5.2
  2922. */
  2923. public function setImageScale($scale) {
  2924. $this->imgscale = $scale;
  2925. }
  2926. /**
  2927. * Returns the adjusting factor to convert pixels to user units.
  2928. * @return float adjusting factor to convert pixels to user units.
  2929. * @author Nicola Asuni
  2930. * @public
  2931. * @since 1.5.2
  2932. */
  2933. public function getImageScale() {
  2934. return $this->imgscale;
  2935. }
  2936. /**
  2937. * Returns an array of page dimensions:
  2938. * <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>
  2939. * @param $pagenum (int) page number (empty = current page)
  2940. * @return array of page dimensions.
  2941. * @author Nicola Asuni
  2942. * @public
  2943. * @since 4.5.027 (2009-03-16)
  2944. */
  2945. public function getPageDimensions($pagenum='') {
  2946. if (empty($pagenum)) {
  2947. $pagenum = $this->page;
  2948. }
  2949. return $this->pagedim[$pagenum];
  2950. }
  2951. /**
  2952. * Returns the page width in units.
  2953. * @param $pagenum (int) page number (empty = current page)
  2954. * @return int page width.
  2955. * @author Nicola Asuni
  2956. * @public
  2957. * @since 1.5.2
  2958. * @see getPageDimensions()
  2959. */
  2960. public function getPageWidth($pagenum='') {
  2961. if (empty($pagenum)) {
  2962. return $this->w;
  2963. }
  2964. return $this->pagedim[$pagenum]['w'];
  2965. }
  2966. /**
  2967. * Returns the page height in units.
  2968. * @param $pagenum (int) page number (empty = current page)
  2969. * @return int page height.
  2970. * @author Nicola Asuni
  2971. * @public
  2972. * @since 1.5.2
  2973. * @see getPageDimensions()
  2974. */
  2975. public function getPageHeight($pagenum='') {
  2976. if (empty($pagenum)) {
  2977. return $this->h;
  2978. }
  2979. return $this->pagedim[$pagenum]['h'];
  2980. }
  2981. /**
  2982. * Returns the page break margin.
  2983. * @param $pagenum (int) page number (empty = current page)
  2984. * @return int page break margin.
  2985. * @author Nicola Asuni
  2986. * @public
  2987. * @since 1.5.2
  2988. * @see getPageDimensions()
  2989. */
  2990. public function getBreakMargin($pagenum='') {
  2991. if (empty($pagenum)) {
  2992. return $this->bMargin;
  2993. }
  2994. return $this->pagedim[$pagenum]['bm'];
  2995. }
  2996. /**
  2997. * Returns the scale factor (number of points in user unit).
  2998. * @return int scale factor.
  2999. * @author Nicola Asuni
  3000. * @public
  3001. * @since 1.5.2
  3002. */
  3003. public function getScaleFactor() {
  3004. return $this->k;
  3005. }
  3006. /**
  3007. * Defines the left, top and right margins.
  3008. * @param $left (float) Left margin.
  3009. * @param $top (float) Top margin.
  3010. * @param $right (float) Right margin. Default value is the left one.
  3011. * @param $keepmargins (boolean) if true overwrites the default page margins
  3012. * @public
  3013. * @since 1.0
  3014. * @see SetLeftMargin(), SetTopMargin(), SetRightMargin(), SetAutoPageBreak()
  3015. */
  3016. public function SetMargins($left, $top, $right=-1, $keepmargins=false) {
  3017. //Set left, top and right margins
  3018. $this->lMargin = $left;
  3019. $this->tMargin = $top;
  3020. if ($right == -1) {
  3021. $right = $left;
  3022. }
  3023. $this->rMargin = $right;
  3024. if ($keepmargins) {
  3025. // overwrite original values
  3026. $this->original_lMargin = $this->lMargin;
  3027. $this->original_rMargin = $this->rMargin;
  3028. }
  3029. }
  3030. /**
  3031. * 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.
  3032. * @param $margin (float) The margin.
  3033. * @public
  3034. * @since 1.4
  3035. * @see SetTopMargin(), SetRightMargin(), SetAutoPageBreak(), SetMargins()
  3036. */
  3037. public function SetLeftMargin($margin) {
  3038. //Set left margin
  3039. $this->lMargin = $margin;
  3040. if (($this->page > 0) AND ($this->x < $margin)) {
  3041. $this->x = $margin;
  3042. }
  3043. }
  3044. /**
  3045. * Defines the top margin. The method can be called before creating the first page.
  3046. * @param $margin (float) The margin.
  3047. * @public
  3048. * @since 1.5
  3049. * @see SetLeftMargin(), SetRightMargin(), SetAutoPageBreak(), SetMargins()
  3050. */
  3051. public function SetTopMargin($margin) {
  3052. //Set top margin
  3053. $this->tMargin = $margin;
  3054. if (($this->page > 0) AND ($this->y < $margin)) {
  3055. $this->y = $margin;
  3056. }
  3057. }
  3058. /**
  3059. * Defines the right margin. The method can be called before creating the first page.
  3060. * @param $margin (float) The margin.
  3061. * @public
  3062. * @since 1.5
  3063. * @see SetLeftMargin(), SetTopMargin(), SetAutoPageBreak(), SetMargins()
  3064. */
  3065. public function SetRightMargin($margin) {
  3066. $this->rMargin = $margin;
  3067. if (($this->page > 0) AND ($this->x > ($this->w - $margin))) {
  3068. $this->x = $this->w - $margin;
  3069. }
  3070. }
  3071. /**
  3072. * Set the same internal Cell padding for top, right, bottom, left-
  3073. * @param $pad (float) internal padding.
  3074. * @public
  3075. * @since 2.1.000 (2008-01-09)
  3076. * @see getCellPaddings(), setCellPaddings()
  3077. */
  3078. public function SetCellPadding($pad) {
  3079. if ($pad >= 0) {
  3080. $this->cell_padding['L'] = $pad;
  3081. $this->cell_padding['T'] = $pad;
  3082. $this->cell_padding['R'] = $pad;
  3083. $this->cell_padding['B'] = $pad;
  3084. }
  3085. }
  3086. /**
  3087. * Set the internal Cell paddings.
  3088. * @param $left (float) left padding
  3089. * @param $top (float) top padding
  3090. * @param $right (float) right padding
  3091. * @param $bottom (float) bottom padding
  3092. * @public
  3093. * @since 5.9.000 (2010-10-03)
  3094. * @see getCellPaddings(), SetCellPadding()
  3095. */
  3096. public function setCellPaddings($left='', $top='', $right='', $bottom='') {
  3097. if (($left !== '') AND ($left >= 0)) {
  3098. $this->cell_padding['L'] = $left;
  3099. }
  3100. if (($top !== '') AND ($top >= 0)) {
  3101. $this->cell_padding['T'] = $top;
  3102. }
  3103. if (($right !== '') AND ($right >= 0)) {
  3104. $this->cell_padding['R'] = $right;
  3105. }
  3106. if (($bottom !== '') AND ($bottom >= 0)) {
  3107. $this->cell_padding['B'] = $bottom;
  3108. }
  3109. }
  3110. /**
  3111. * Get the internal Cell padding array.
  3112. * @return array of padding values
  3113. * @public
  3114. * @since 5.9.000 (2010-10-03)
  3115. * @see setCellPaddings(), SetCellPadding()
  3116. */
  3117. public function getCellPaddings() {
  3118. return $this->cell_padding;
  3119. }
  3120. /**
  3121. * Set the internal Cell margins.
  3122. * @param $left (float) left margin
  3123. * @param $top (float) top margin
  3124. * @param $right (float) right margin
  3125. * @param $bottom (float) bottom margin
  3126. * @public
  3127. * @since 5.9.000 (2010-10-03)
  3128. * @see getCellMargins()
  3129. */
  3130. public function setCellMargins($left='', $top='', $right='', $bottom='') {
  3131. if (($left !== '') AND ($left >= 0)) {
  3132. $this->cell_margin['L'] = $left;
  3133. }
  3134. if (($top !== '') AND ($top >= 0)) {
  3135. $this->cell_margin['T'] = $top;
  3136. }
  3137. if (($right !== '') AND ($right >= 0)) {
  3138. $this->cell_margin['R'] = $right;
  3139. }
  3140. if (($bottom !== '') AND ($bottom >= 0)) {
  3141. $this->cell_margin['B'] = $bottom;
  3142. }
  3143. }
  3144. /**
  3145. * Get the internal Cell margin array.
  3146. * @return array of margin values
  3147. * @public
  3148. * @since 5.9.000 (2010-10-03)
  3149. * @see setCellMargins()
  3150. */
  3151. public function getCellMargins() {
  3152. return $this->cell_margin;
  3153. }
  3154. /**
  3155. * Adjust the internal Cell padding array to take account of the line width.
  3156. * @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)))
  3157. * @return array of adjustments
  3158. * @public
  3159. * @since 5.9.000 (2010-10-03)
  3160. */
  3161. protected function adjustCellPadding($brd=0) {
  3162. if (empty($brd)) {
  3163. return;
  3164. }
  3165. if (is_string($brd)) {
  3166. // convert string to array
  3167. $slen = strlen($brd);
  3168. $newbrd = array();
  3169. for ($i = 0; $i < $slen; ++$i) {
  3170. $newbrd[$brd[$i]] = true;
  3171. }
  3172. $brd = $newbrd;
  3173. } elseif (($brd === 1) OR ($brd === true) OR (is_numeric($brd) AND (intval($brd) > 0))) {
  3174. $brd = array('LRTB' => true);
  3175. }
  3176. if (!is_array($brd)) {
  3177. return;
  3178. }
  3179. // store current cell padding
  3180. $cp = $this->cell_padding;
  3181. // select border mode
  3182. if (isset($brd['mode'])) {
  3183. $mode = $brd['mode'];
  3184. unset($brd['mode']);
  3185. } else {
  3186. $mode = 'normal';
  3187. }
  3188. // process borders
  3189. foreach ($brd as $border => $style) {
  3190. $line_width = $this->LineWidth;
  3191. if (is_array($style) AND isset($style['width'])) {
  3192. // get border width
  3193. $line_width = $style['width'];
  3194. }
  3195. $adj = 0; // line width inside the cell
  3196. switch ($mode) {
  3197. case 'ext': {
  3198. $adj = 0;
  3199. break;
  3200. }
  3201. case 'int': {
  3202. $adj = $line_width;
  3203. break;
  3204. }
  3205. case 'normal':
  3206. default: {
  3207. $adj = ($line_width / 2);
  3208. break;
  3209. }
  3210. }
  3211. // correct internal cell padding if required to avoid overlap between text and lines
  3212. if ((strpos($border,'T') !== false) AND ($this->cell_padding['T'] < $adj)) {
  3213. $this->cell_padding['T'] = $adj;
  3214. }
  3215. if ((strpos($border,'R') !== false) AND ($this->cell_padding['R'] < $adj)) {
  3216. $this->cell_padding['R'] = $adj;
  3217. }
  3218. if ((strpos($border,'B') !== false) AND ($this->cell_padding['B'] < $adj)) {
  3219. $this->cell_padding['B'] = $adj;
  3220. }
  3221. if ((strpos($border,'L') !== false) AND ($this->cell_padding['L'] < $adj)) {
  3222. $this->cell_padding['L'] = $adj;
  3223. }
  3224. }
  3225. 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']));
  3226. }
  3227. /**
  3228. * 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.
  3229. * @param $auto (boolean) Boolean indicating if mode should be on or off.
  3230. * @param $margin (float) Distance from the bottom of the page.
  3231. * @public
  3232. * @since 1.0
  3233. * @see Cell(), MultiCell(), AcceptPageBreak()
  3234. */
  3235. public function SetAutoPageBreak($auto, $margin=0) {
  3236. $this->AutoPageBreak = $auto ? true : false;
  3237. $this->bMargin = $margin;
  3238. $this->PageBreakTrigger = $this->h - $margin;
  3239. }
  3240. /**
  3241. * Return the auto-page-break mode (true or false).
  3242. * @return boolean auto-page-break mode
  3243. * @public
  3244. * @since 5.9.088
  3245. */
  3246. public function getAutoPageBreak() {
  3247. return $this->AutoPageBreak;
  3248. }
  3249. /**
  3250. * Defines the way the document is to be displayed by the viewer.
  3251. * @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>
  3252. * @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>
  3253. * @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>
  3254. * @public
  3255. * @since 1.2
  3256. */
  3257. public function SetDisplayMode($zoom, $layout='SinglePage', $mode='UseNone') {
  3258. if (($zoom == 'fullpage') OR ($zoom == 'fullwidth') OR ($zoom == 'real') OR ($zoom == 'default') OR (!is_string($zoom))) {
  3259. $this->ZoomMode = $zoom;
  3260. } else {
  3261. $this->Error('Incorrect zoom display mode: '.$zoom);
  3262. }
  3263. switch ($layout) {
  3264. case 'default':
  3265. case 'single':
  3266. case 'SinglePage': {
  3267. $this->LayoutMode = 'SinglePage';
  3268. break;
  3269. }
  3270. case 'continuous':
  3271. case 'OneColumn': {
  3272. $this->LayoutMode = 'OneColumn';
  3273. break;
  3274. }
  3275. case 'two':
  3276. case 'TwoColumnLeft': {
  3277. $this->LayoutMode = 'TwoColumnLeft';
  3278. break;
  3279. }
  3280. case 'TwoColumnRight': {
  3281. $this->LayoutMode = 'TwoColumnRight';
  3282. break;
  3283. }
  3284. case 'TwoPageLeft': {
  3285. $this->LayoutMode = 'TwoPageLeft';
  3286. break;
  3287. }
  3288. case 'TwoPageRight': {
  3289. $this->LayoutMode = 'TwoPageRight';
  3290. break;
  3291. }
  3292. default: {
  3293. $this->LayoutMode = 'SinglePage';
  3294. }
  3295. }
  3296. switch ($mode) {
  3297. case 'UseNone': {
  3298. $this->PageMode = 'UseNone';
  3299. break;
  3300. }
  3301. case 'UseOutlines': {
  3302. $this->PageMode = 'UseOutlines';
  3303. break;
  3304. }
  3305. case 'UseThumbs': {
  3306. $this->PageMode = 'UseThumbs';
  3307. break;
  3308. }
  3309. case 'FullScreen': {
  3310. $this->PageMode = 'FullScreen';
  3311. break;
  3312. }
  3313. case 'UseOC': {
  3314. $this->PageMode = 'UseOC';
  3315. break;
  3316. }
  3317. case '': {
  3318. $this->PageMode = 'UseAttachments';
  3319. break;
  3320. }
  3321. default: {
  3322. $this->PageMode = 'UseNone';
  3323. }
  3324. }
  3325. }
  3326. /**
  3327. * 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.
  3328. * Note: the Zlib extension is required for this feature. If not present, compression will be turned off.
  3329. * @param $compress (boolean) Boolean indicating if compression must be enabled.
  3330. * @public
  3331. * @since 1.4
  3332. */
  3333. public function SetCompression($compress=true) {
  3334. if (function_exists('gzcompress')) {
  3335. $this->compress = $compress ? true : false;
  3336. } else {
  3337. $this->compress = false;
  3338. }
  3339. }
  3340. /**
  3341. * Set flag to force sRGB_IEC61966-2.1 black scaled ICC color profile for the whole document.
  3342. * @param $mode (boolean) If true force sRGB output intent.
  3343. * @public
  3344. * @since 5.9.121 (2011-09-28)
  3345. */
  3346. public function setSRGBmode($mode=false) {
  3347. $this->force_srgb = $mode ? true : false;
  3348. }
  3349. /**
  3350. * Turn on/off Unicode mode for document information dictionary (meta tags).
  3351. * This has effect only when unicode mode is set to false.
  3352. * @param $unicode (boolean) if true set the meta information in Unicode
  3353. * @since 5.9.027 (2010-12-01)
  3354. * @public
  3355. */
  3356. public function SetDocInfoUnicode($unicode=true) {
  3357. $this->docinfounicode = $unicode ? true : false;
  3358. }
  3359. /**
  3360. * Defines the title of the document.
  3361. * @param $title (string) The title.
  3362. * @public
  3363. * @since 1.2
  3364. * @see SetAuthor(), SetCreator(), SetKeywords(), SetSubject()
  3365. */
  3366. public function SetTitle($title) {
  3367. $this->title = $title;
  3368. }
  3369. /**
  3370. * Defines the subject of the document.
  3371. * @param $subject (string) The subject.
  3372. * @public
  3373. * @since 1.2
  3374. * @see SetAuthor(), SetCreator(), SetKeywords(), SetTitle()
  3375. */
  3376. public function SetSubject($subject) {
  3377. $this->subject = $subject;
  3378. }
  3379. /**
  3380. * Defines the author of the document.
  3381. * @param $author (string) The name of the author.
  3382. * @public
  3383. * @since 1.2
  3384. * @see SetCreator(), SetKeywords(), SetSubject(), SetTitle()
  3385. */
  3386. public function SetAuthor($author) {
  3387. $this->author = $author;
  3388. }
  3389. /**
  3390. * Associates keywords with the document, generally in the form 'keyword1 keyword2 ...'.
  3391. * @param $keywords (string) The list of keywords.
  3392. * @public
  3393. * @since 1.2
  3394. * @see SetAuthor(), SetCreator(), SetSubject(), SetTitle()
  3395. */
  3396. public function SetKeywords($keywords) {
  3397. $this->keywords = $keywords;
  3398. }
  3399. /**
  3400. * Defines the creator of the document. This is typically the name of the application that generates the PDF.
  3401. * @param $creator (string) The name of the creator.
  3402. * @public
  3403. * @since 1.2
  3404. * @see SetAuthor(), SetKeywords(), SetSubject(), SetTitle()
  3405. */
  3406. public function SetCreator($creator) {
  3407. $this->creator = $creator;
  3408. }
  3409. /**
  3410. * 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.
  3411. * 2004-06-11 :: Nicola Asuni : changed bold tag with strong
  3412. * @param $msg (string) The error message
  3413. * @public
  3414. * @since 1.0
  3415. */
  3416. public function Error($msg) {
  3417. // unset all class variables
  3418. $this->_destroy(true);
  3419. // exit program and print error
  3420. die('<strong>TCPDF ERROR: </strong>'.$msg);
  3421. }
  3422. /**
  3423. * This method begins the generation of the PDF document.
  3424. * It is not necessary to call it explicitly because AddPage() does it automatically.
  3425. * Note: no page is created by this method
  3426. * @public
  3427. * @since 1.0
  3428. * @see AddPage(), Close()
  3429. */
  3430. public function Open() {
  3431. $this->state = 1;
  3432. }
  3433. /**
  3434. * Terminates the PDF document.
  3435. * It is not necessary to call this method explicitly because Output() does it automatically.
  3436. * If the document contains no page, AddPage() is called to prevent from getting an invalid document.
  3437. * @public
  3438. * @since 1.0
  3439. * @see Open(), Output()
  3440. */
  3441. public function Close() {
  3442. if ($this->state == 3) {
  3443. return;
  3444. }
  3445. if ($this->page == 0) {
  3446. $this->AddPage();
  3447. }
  3448. $this->endLayer();
  3449. // save current graphic settings
  3450. $gvars = $this->getGraphicVars();
  3451. $this->setEqualColumns();
  3452. $this->lastpage(true);
  3453. $this->SetAutoPageBreak(false);
  3454. $this->x = 0;
  3455. $this->y = $this->h - (1 / $this->k);
  3456. $this->lMargin = 0;
  3457. $this->_out('q');
  3458. $this->SetFont('helvetica', '', 1);
  3459. $this->setTextRenderingMode(0, false, false);
  3460. $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";
  3461. $lnk = "\x68\x74\x74\x70\x3a\x2f\x2f\x77\x77\x77\x2e\x74\x63\x70\x64\x66\x2e\x6f\x72\x67";
  3462. $this->Cell(0, 0, $msg, 0, 0, 'L', 0, $lnk, 0, false, 'D', 'B');
  3463. $this->_out('Q');
  3464. // restore graphic settings
  3465. $this->setGraphicVars($gvars);
  3466. // close page
  3467. $this->endPage();
  3468. // close document
  3469. $this->_enddoc();
  3470. // unset all class variables (except critical ones)
  3471. $this->_destroy(false);
  3472. }
  3473. /**
  3474. * Move pointer at the specified document page and update page dimensions.
  3475. * @param $pnum (int) page number (1 ... numpages)
  3476. * @param $resetmargins (boolean) if true reset left, right, top margins and Y position.
  3477. * @public
  3478. * @since 2.1.000 (2008-01-07)
  3479. * @see getPage(), lastpage(), getNumPages()
  3480. */
  3481. public function setPage($pnum, $resetmargins=false) {
  3482. if (($pnum == $this->page) AND ($this->state == 2)) {
  3483. return;
  3484. }
  3485. if (($pnum > 0) AND ($pnum <= $this->numpages)) {
  3486. $this->state = 2;
  3487. // save current graphic settings
  3488. //$gvars = $this->getGraphicVars();
  3489. $oldpage = $this->page;
  3490. $this->page = $pnum;
  3491. $this->wPt = $this->pagedim[$this->page]['w'];
  3492. $this->hPt = $this->pagedim[$this->page]['h'];
  3493. $this->w = $this->pagedim[$this->page]['wk'];
  3494. $this->h = $this->pagedim[$this->page]['hk'];
  3495. $this->tMargin = $this->pagedim[$this->page]['tm'];
  3496. $this->bMargin = $this->pagedim[$this->page]['bm'];
  3497. $this->original_lMargin = $this->pagedim[$this->page]['olm'];
  3498. $this->original_rMargin = $this->pagedim[$this->page]['orm'];
  3499. $this->AutoPageBreak = $this->pagedim[$this->page]['pb'];
  3500. $this->CurOrientation = $this->pagedim[$this->page]['or'];
  3501. $this->SetAutoPageBreak($this->AutoPageBreak, $this->bMargin);
  3502. // restore graphic settings
  3503. //$this->setGraphicVars($gvars);
  3504. if ($resetmargins) {
  3505. $this->lMargin = $this->pagedim[$this->page]['olm'];
  3506. $this->rMargin = $this->pagedim[$this->page]['orm'];
  3507. $this->SetY($this->tMargin);
  3508. } else {
  3509. // account for booklet mode
  3510. if ($this->pagedim[$this->page]['olm'] != $this->pagedim[$oldpage]['olm']) {
  3511. $deltam = $this->pagedim[$this->page]['olm'] - $this->pagedim[$this->page]['orm'];
  3512. $this->lMargin += $deltam;
  3513. $this->rMargin -= $deltam;
  3514. }
  3515. }
  3516. } else {
  3517. $this->Error('Wrong page number on setPage() function: '.$pnum);
  3518. }
  3519. }
  3520. /**
  3521. * Reset pointer to the last document page.
  3522. * @param $resetmargins (boolean) if true reset left, right, top margins and Y position.
  3523. * @public
  3524. * @since 2.0.000 (2008-01-04)
  3525. * @see setPage(), getPage(), getNumPages()
  3526. */
  3527. public function lastPage($resetmargins=false) {
  3528. $this->setPage($this->getNumPages(), $resetmargins);
  3529. }
  3530. /**
  3531. * Get current document page number.
  3532. * @return int page number
  3533. * @public
  3534. * @since 2.1.000 (2008-01-07)
  3535. * @see setPage(), lastpage(), getNumPages()
  3536. */
  3537. public function getPage() {
  3538. return $this->page;
  3539. }
  3540. /**
  3541. * Get the total number of insered pages.
  3542. * @return int number of pages
  3543. * @public
  3544. * @since 2.1.000 (2008-01-07)
  3545. * @see setPage(), getPage(), lastpage()
  3546. */
  3547. public function getNumPages() {
  3548. return $this->numpages;
  3549. }
  3550. /**
  3551. * Adds a new TOC (Table Of Content) page to the document.
  3552. * @param $orientation (string) page orientation.
  3553. * @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().
  3554. * @param $keepmargins (boolean) if true overwrites the default page margins with the current margins
  3555. * @public
  3556. * @since 5.0.001 (2010-05-06)
  3557. * @see AddPage(), startPage(), endPage(), endTOCPage()
  3558. */
  3559. public function addTOCPage($orientation='', $format='', $keepmargins=false) {
  3560. $this->AddPage($orientation, $format, $keepmargins, true);
  3561. }
  3562. /**
  3563. * Terminate the current TOC (Table Of Content) page
  3564. * @public
  3565. * @since 5.0.001 (2010-05-06)
  3566. * @see AddPage(), startPage(), endPage(), addTOCPage()
  3567. */
  3568. public function endTOCPage() {
  3569. $this->endPage(true);
  3570. }
  3571. /**
  3572. * 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).
  3573. * The origin of the coordinate system is at the top-left corner (or top-right for RTL) and increasing ordinates go downwards.
  3574. * @param $orientation (string) page orientation. Possible values are (case insensitive):<ul><li>P or PORTRAIT (default)</li><li>L or LANDSCAPE</li></ul>
  3575. * @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().
  3576. * @param $keepmargins (boolean) if true overwrites the default page margins with the current margins
  3577. * @param $tocpage (boolean) if true set the tocpage state to true (the added page will be used to display Table Of Content).
  3578. * @public
  3579. * @since 1.0
  3580. * @see startPage(), endPage(), addTOCPage(), endTOCPage(), getPageSizeFromFormat(), setPageFormat()
  3581. */
  3582. public function AddPage($orientation='', $format='', $keepmargins=false, $tocpage=false) {
  3583. if ($this->inxobj) {
  3584. // we are inside an XObject template
  3585. return;
  3586. }
  3587. if (!isset($this->original_lMargin) OR $keepmargins) {
  3588. $this->original_lMargin = $this->lMargin;
  3589. }
  3590. if (!isset($this->original_rMargin) OR $keepmargins) {
  3591. $this->original_rMargin = $this->rMargin;
  3592. }
  3593. // terminate previous page
  3594. $this->endPage();
  3595. // start new page
  3596. $this->startPage($orientation, $format, $tocpage);
  3597. }
  3598. /**
  3599. * Terminate the current page
  3600. * @param $tocpage (boolean) if true set the tocpage state to false (end the page used to display Table Of Content).
  3601. * @public
  3602. * @since 4.2.010 (2008-11-14)
  3603. * @see AddPage(), startPage(), addTOCPage(), endTOCPage()
  3604. */
  3605. public function endPage($tocpage=false) {
  3606. // check if page is already closed
  3607. if (($this->page == 0) OR ($this->numpages > $this->page) OR (!$this->pageopen[$this->page])) {
  3608. return;
  3609. }
  3610. // print page footer
  3611. $this->setFooter();
  3612. // close page
  3613. $this->_endpage();
  3614. // mark page as closed
  3615. $this->pageopen[$this->page] = false;
  3616. if ($tocpage) {
  3617. $this->tocpage = false;
  3618. }
  3619. }
  3620. /**
  3621. * Starts a new page to the document. The page must be closed using the endPage() function.
  3622. * The origin of the coordinate system is at the top-left corner and increasing ordinates go downwards.
  3623. * @param $orientation (string) page orientation. Possible values are (case insensitive):<ul><li>P or PORTRAIT (default)</li><li>L or LANDSCAPE</li></ul>
  3624. * @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().
  3625. * @param $tocpage (boolean) if true the page is designated to contain the Table-Of-Content.
  3626. * @since 4.2.010 (2008-11-14)
  3627. * @see AddPage(), endPage(), addTOCPage(), endTOCPage(), getPageSizeFromFormat(), setPageFormat()
  3628. * @public
  3629. */
  3630. public function startPage($orientation='', $format='', $tocpage=false) {
  3631. if ($tocpage) {
  3632. $this->tocpage = true;
  3633. }
  3634. // move page numbers of documents to be attached
  3635. if ($this->tocpage) {
  3636. // move reference to unexistent pages (used for page attachments)
  3637. // adjust outlines
  3638. $tmpoutlines = $this->outlines;
  3639. foreach ($tmpoutlines as $key => $outline) {
  3640. if ($outline['p'] > $this->numpages) {
  3641. $this->outlines[$key]['p'] = ($outline['p'] + 1);
  3642. }
  3643. }
  3644. // adjust dests
  3645. $tmpdests = $this->dests;
  3646. foreach ($tmpdests as $key => $dest) {
  3647. if ($dest['p'] > $this->numpages) {
  3648. $this->dests[$key]['p'] = ($dest['p'] + 1);
  3649. }
  3650. }
  3651. // adjust links
  3652. $tmplinks = $this->links;
  3653. foreach ($tmplinks as $key => $link) {
  3654. if ($link[0] > $this->numpages) {
  3655. $this->links[$key][0] = ($link[0] + 1);
  3656. }
  3657. }
  3658. }
  3659. if ($this->numpages > $this->page) {
  3660. // this page has been already added
  3661. $this->setPage($this->page + 1);
  3662. $this->SetY($this->tMargin);
  3663. return;
  3664. }
  3665. // start a new page
  3666. if ($this->state == 0) {
  3667. $this->Open();
  3668. }
  3669. ++$this->numpages;
  3670. $this->swapMargins($this->booklet);
  3671. // save current graphic settings
  3672. $gvars = $this->getGraphicVars();
  3673. // start new page
  3674. $this->_beginpage($orientation, $format);
  3675. // mark page as open
  3676. $this->pageopen[$this->page] = true;
  3677. // restore graphic settings
  3678. $this->setGraphicVars($gvars);
  3679. // mark this point
  3680. $this->setPageMark();
  3681. // print page header
  3682. $this->setHeader();
  3683. // restore graphic settings
  3684. $this->setGraphicVars($gvars);
  3685. // mark this point
  3686. $this->setPageMark();
  3687. // print table header (if any)
  3688. $this->setTableHeader();
  3689. // set mark for empty page check
  3690. $this->emptypagemrk[$this->page]= $this->pagelen[$this->page];
  3691. }
  3692. /**
  3693. * Set start-writing mark on current page stream used to put borders and fills.
  3694. * Borders and fills are always created after content and inserted on the position marked by this method.
  3695. * This function must be called after calling Image() function for a background image.
  3696. * Background images must be always inserted before calling Multicell() or WriteHTMLCell() or WriteHTML() functions.
  3697. * @public
  3698. * @since 4.0.016 (2008-07-30)
  3699. */
  3700. public function setPageMark() {
  3701. $this->intmrk[$this->page] = $this->pagelen[$this->page];
  3702. $this->bordermrk[$this->page] = $this->intmrk[$this->page];
  3703. $this->setContentMark();
  3704. }
  3705. /**
  3706. * Set start-writing mark on selected page.
  3707. * Borders and fills are always created after content and inserted on the position marked by this method.
  3708. * @param $page (int) page number (default is the current page)
  3709. * @protected
  3710. * @since 4.6.021 (2009-07-20)
  3711. */
  3712. protected function setContentMark($page=0) {
  3713. if ($page <= 0) {
  3714. $page = $this->page;
  3715. }
  3716. if (isset($this->footerlen[$page])) {
  3717. $this->cntmrk[$page] = $this->pagelen[$page] - $this->footerlen[$page];
  3718. } else {
  3719. $this->cntmrk[$page] = $this->pagelen[$page];
  3720. }
  3721. }
  3722. /**
  3723. * Set header data.
  3724. * @param $ln (string) header image logo
  3725. * @param $lw (string) header image logo width in mm
  3726. * @param $ht (string) string to print as title on document header
  3727. * @param $hs (string) string to print on document header
  3728. * @public
  3729. */
  3730. public function setHeaderData($ln='', $lw=0, $ht='', $hs='') {
  3731. $this->header_logo = $ln;
  3732. $this->header_logo_width = $lw;
  3733. $this->header_title = $ht;
  3734. $this->header_string = $hs;
  3735. }
  3736. /**
  3737. * Returns header data:
  3738. * <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>
  3739. * @return array()
  3740. * @public
  3741. * @since 4.0.012 (2008-07-24)
  3742. */
  3743. public function getHeaderData() {
  3744. $ret = array();
  3745. $ret['logo'] = $this->header_logo;
  3746. $ret['logo_width'] = $this->header_logo_width;
  3747. $ret['title'] = $this->header_title;
  3748. $ret['string'] = $this->header_string;
  3749. return $ret;
  3750. }
  3751. /**
  3752. * Set header margin.
  3753. * (minimum distance between header and top page margin)
  3754. * @param $hm (int) distance in user units
  3755. * @public
  3756. */
  3757. public function setHeaderMargin($hm=10) {
  3758. $this->header_margin = $hm;
  3759. }
  3760. /**
  3761. * Returns header margin in user units.
  3762. * @return float
  3763. * @since 4.0.012 (2008-07-24)
  3764. * @public
  3765. */
  3766. public function getHeaderMargin() {
  3767. return $this->header_margin;
  3768. }
  3769. /**
  3770. * Set footer margin.
  3771. * (minimum distance between footer and bottom page margin)
  3772. * @param $fm (int) distance in user units
  3773. * @public
  3774. */
  3775. public function setFooterMargin($fm=10) {
  3776. $this->footer_margin = $fm;
  3777. }
  3778. /**
  3779. * Returns footer margin in user units.
  3780. * @return float
  3781. * @since 4.0.012 (2008-07-24)
  3782. * @public
  3783. */
  3784. public function getFooterMargin() {
  3785. return $this->footer_margin;
  3786. }
  3787. /**
  3788. * Set a flag to print page header.
  3789. * @param $val (boolean) set to true to print the page header (default), false otherwise.
  3790. * @public
  3791. */
  3792. public function setPrintHeader($val=true) {
  3793. $this->print_header = $val ? true : false;
  3794. }
  3795. /**
  3796. * Set a flag to print page footer.
  3797. * @param $val (boolean) set to true to print the page footer (default), false otherwise.
  3798. * @public
  3799. */
  3800. public function setPrintFooter($val=true) {
  3801. $this->print_footer = $val ? true : false;
  3802. }
  3803. /**
  3804. * Return the right-bottom (or left-bottom for RTL) corner X coordinate of last inserted image
  3805. * @return float
  3806. * @public
  3807. */
  3808. public function getImageRBX() {
  3809. return $this->img_rb_x;
  3810. }
  3811. /**
  3812. * Return the right-bottom (or left-bottom for RTL) corner Y coordinate of last inserted image
  3813. * @return float
  3814. * @public
  3815. */
  3816. public function getImageRBY() {
  3817. return $this->img_rb_y;
  3818. }
  3819. /**
  3820. * Reset the xobject template used by Header() method.
  3821. * @public
  3822. */
  3823. public function resetHeaderTemplate() {
  3824. $this->header_xobjid = -1;
  3825. }
  3826. /**
  3827. * Set a flag to automatically reset the xobject template used by Header() method at each page.
  3828. * @param $val (boolean) set to true to reset Header xobject template at each page, false otherwise.
  3829. * @public
  3830. */
  3831. public function setHeaderTemplateAutoreset($val=true) {
  3832. $this->header_xobj_autoreset = $val ? true : false;
  3833. }
  3834. /**
  3835. * This method is used to render the page header.
  3836. * It is automatically called by AddPage() and could be overwritten in your own inherited class.
  3837. * @public
  3838. */
  3839. public function Header() {
  3840. if ($this->header_xobjid < 0) {
  3841. // start a new XObject Template
  3842. $this->header_xobjid = $this->startTemplate($this->w, $this->tMargin);
  3843. $headerfont = $this->getHeaderFont();
  3844. $headerdata = $this->getHeaderData();
  3845. $this->y = $this->header_margin;
  3846. if ($this->rtl) {
  3847. $this->x = $this->w - $this->original_rMargin;
  3848. } else {
  3849. $this->x = $this->original_lMargin;
  3850. }
  3851. if (($headerdata['logo']) AND ($headerdata['logo'] != K_BLANK_IMAGE)) {
  3852. $imgtype = $this->getImageFileType(K_PATH_IMAGES.$headerdata['logo']);
  3853. if (($imgtype == 'eps') OR ($imgtype == 'ai')) {
  3854. $this->ImageEps(K_PATH_IMAGES.$headerdata['logo'], '', '', $headerdata['logo_width']);
  3855. } elseif ($imgtype == 'svg') {
  3856. $this->ImageSVG(K_PATH_IMAGES.$headerdata['logo'], '', '', $headerdata['logo_width']);
  3857. } else {
  3858. $this->Image(K_PATH_IMAGES.$headerdata['logo'], '', '', $headerdata['logo_width']);
  3859. }
  3860. $imgy = $this->getImageRBY();
  3861. } else {
  3862. $imgy = $this->y;
  3863. }
  3864. $cell_height = round(($this->cell_height_ratio * $headerfont[2]) / $this->k, 2);
  3865. // set starting margin for text data cell
  3866. if ($this->getRTL()) {
  3867. $header_x = $this->original_rMargin + ($headerdata['logo_width'] * 1.1);
  3868. } else {
  3869. $header_x = $this->original_lMargin + ($headerdata['logo_width'] * 1.1);
  3870. }
  3871. $cw = $this->w - $this->original_lMargin - $this->original_rMargin - ($headerdata['logo_width'] * 1.1);
  3872. $this->SetTextColor(0, 0, 0);
  3873. // header title
  3874. $this->SetFont($headerfont[0], 'B', $headerfont[2] + 1);
  3875. $this->SetX($header_x);
  3876. $this->Cell($cw, $cell_height, $headerdata['title'], 0, 1, '', 0, '', 0);
  3877. // header string
  3878. $this->SetFont($headerfont[0], $headerfont[1], $headerfont[2]);
  3879. $this->SetX($header_x);
  3880. $this->MultiCell($cw, $cell_height, $headerdata['string'], 0, '', 0, 1, '', '', true, 0, false, true, 0, 'T', false);
  3881. // print an ending header line
  3882. $this->SetLineStyle(array('width' => 0.85 / $this->k, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)));
  3883. $this->SetY((2.835 / $this->k) + max($imgy, $this->y));
  3884. if ($this->rtl) {
  3885. $this->SetX($this->original_rMargin);
  3886. } else {
  3887. $this->SetX($this->original_lMargin);
  3888. }
  3889. $this->Cell(($this->w - $this->original_lMargin - $this->original_rMargin), 0, '', 'T', 0, 'C');
  3890. $this->endTemplate();
  3891. }
  3892. // print header template
  3893. $x = 0;
  3894. $dx = 0;
  3895. if ($this->booklet AND (($this->page % 2) == 0)) {
  3896. // adjust margins for booklet mode
  3897. $dx = ($this->original_lMargin - $this->original_rMargin);
  3898. }
  3899. if ($this->rtl) {
  3900. $x = $this->w + $dx;
  3901. } else {
  3902. $x = 0 + $dx;
  3903. }
  3904. $this->printTemplate($this->header_xobjid, $x, 0, 0, 0, '', '', false);
  3905. if ($this->header_xobj_autoreset) {
  3906. // reset header xobject template at each page
  3907. $this->header_xobjid = -1;
  3908. }
  3909. }
  3910. /**
  3911. * This method is used to render the page footer.
  3912. * It is automatically called by AddPage() and could be overwritten in your own inherited class.
  3913. * @public
  3914. */
  3915. public function Footer() {
  3916. $cur_y = $this->y;
  3917. $this->SetTextColor(0, 0, 0);
  3918. //set style for cell border
  3919. $line_width = 0.85 / $this->k;
  3920. $this->SetLineStyle(array('width' => $line_width, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)));
  3921. //print document barcode
  3922. $barcode = $this->getBarcode();
  3923. if (!empty($barcode)) {
  3924. $this->Ln($line_width);
  3925. $barcode_width = round(($this->w - $this->original_lMargin - $this->original_rMargin) / 3);
  3926. $style = array(
  3927. 'position' => $this->rtl?'R':'L',
  3928. 'align' => $this->rtl?'R':'L',
  3929. 'stretch' => false,
  3930. 'fitwidth' => true,
  3931. 'cellfitalign' => '',
  3932. 'border' => false,
  3933. 'padding' => 0,
  3934. 'fgcolor' => array(0,0,0),
  3935. 'bgcolor' => false,
  3936. 'text' => false
  3937. );
  3938. $this->write1DBarcode($barcode, 'C128', '', $cur_y + $line_width, '', (($this->footer_margin / 3) - $line_width), 0.3, $style, '');
  3939. }
  3940. if (empty($this->pagegroups)) {
  3941. $pagenumtxt = $this->l['w_page'].' '.$this->getAliasNumPage().' / '.$this->getAliasNbPages();
  3942. } else {
  3943. $pagenumtxt = $this->l['w_page'].' '.$this->getPageNumGroupAlias().' / '.$this->getPageGroupAlias();
  3944. }
  3945. $this->SetY($cur_y);
  3946. //Print page number
  3947. if ($this->getRTL()) {
  3948. $this->SetX($this->original_rMargin);
  3949. $this->Cell(0, 0, $pagenumtxt, 'T', 0, 'L');
  3950. } else {
  3951. $this->SetX($this->original_lMargin);
  3952. $this->Cell(0, 0, $this->getAliasRightShift().$pagenumtxt, 'T', 0, 'R');
  3953. }
  3954. }
  3955. /**
  3956. * This method is used to render the page header.
  3957. * @protected
  3958. * @since 4.0.012 (2008-07-24)
  3959. */
  3960. protected function setHeader() {
  3961. if (!$this->print_header) {
  3962. return;
  3963. }
  3964. $this->InHeader = true;
  3965. $this->setGraphicVars($this->default_graphic_vars);
  3966. $temp_thead = $this->thead;
  3967. $temp_theadMargins = $this->theadMargins;
  3968. $lasth = $this->lasth;
  3969. $this->_out('q');
  3970. $this->rMargin = $this->original_rMargin;
  3971. $this->lMargin = $this->original_lMargin;
  3972. $this->SetCellPadding(0);
  3973. //set current position
  3974. if ($this->rtl) {
  3975. $this->SetXY($this->original_rMargin, $this->header_margin);
  3976. } else {
  3977. $this->SetXY($this->original_lMargin, $this->header_margin);
  3978. }
  3979. $this->SetFont($this->header_font[0], $this->header_font[1], $this->header_font[2]);
  3980. $this->Header();
  3981. //restore position
  3982. if ($this->rtl) {
  3983. $this->SetXY($this->original_rMargin, $this->tMargin);
  3984. } else {
  3985. $this->SetXY($this->original_lMargin, $this->tMargin);
  3986. }
  3987. $this->_out('Q');
  3988. $this->lasth = $lasth;
  3989. $this->thead = $temp_thead;
  3990. $this->theadMargins = $temp_theadMargins;
  3991. $this->newline = false;
  3992. $this->InHeader = false;
  3993. }
  3994. /**
  3995. * This method is used to render the page footer.
  3996. * @protected
  3997. * @since 4.0.012 (2008-07-24)
  3998. */
  3999. protected function setFooter() {
  4000. //Page footer
  4001. $this->InFooter = true;
  4002. // save current graphic settings
  4003. $gvars = $this->getGraphicVars();
  4004. // mark this point
  4005. $this->footerpos[$this->page] = $this->pagelen[$this->page];
  4006. $this->_out("\n");
  4007. if ($this->print_footer) {
  4008. $this->setGraphicVars($this->default_graphic_vars);
  4009. $this->current_column = 0;
  4010. $this->num_columns = 1;
  4011. $temp_thead = $this->thead;
  4012. $temp_theadMargins = $this->theadMargins;
  4013. $lasth = $this->lasth;
  4014. $this->_out('q');
  4015. $this->rMargin = $this->original_rMargin;
  4016. $this->lMargin = $this->original_lMargin;
  4017. $this->SetCellPadding(0);
  4018. //set current position
  4019. $footer_y = $this->h - $this->footer_margin;
  4020. if ($this->rtl) {
  4021. $this->SetXY($this->original_rMargin, $footer_y);
  4022. } else {
  4023. $this->SetXY($this->original_lMargin, $footer_y);
  4024. }
  4025. $this->SetFont($this->footer_font[0], $this->footer_font[1], $this->footer_font[2]);
  4026. $this->Footer();
  4027. //restore position
  4028. if ($this->rtl) {
  4029. $this->SetXY($this->original_rMargin, $this->tMargin);
  4030. } else {
  4031. $this->SetXY($this->original_lMargin, $this->tMargin);
  4032. }
  4033. $this->_out('Q');
  4034. $this->lasth = $lasth;
  4035. $this->thead = $temp_thead;
  4036. $this->theadMargins = $temp_theadMargins;
  4037. }
  4038. // restore graphic settings
  4039. $this->setGraphicVars($gvars);
  4040. $this->current_column = $gvars['current_column'];
  4041. $this->num_columns = $gvars['num_columns'];
  4042. // calculate footer length
  4043. $this->footerlen[$this->page] = $this->pagelen[$this->page] - $this->footerpos[$this->page] + 1;
  4044. $this->InFooter = false;
  4045. }
  4046. /**
  4047. * Check if we are on the page body (excluding page header and footer).
  4048. * @return true if we are not in page header nor in page footer, false otherwise.
  4049. * @protected
  4050. * @since 5.9.091 (2011-06-15)
  4051. */
  4052. protected function inPageBody() {
  4053. return (($this->InHeader === false) AND ($this->InFooter === false));
  4054. }
  4055. /**
  4056. * This method is used to render the table header on new page (if any).
  4057. * @protected
  4058. * @since 4.5.030 (2009-03-25)
  4059. */
  4060. protected function setTableHeader() {
  4061. if ($this->num_columns > 1) {
  4062. // multi column mode
  4063. return;
  4064. }
  4065. if (isset($this->theadMargins['top'])) {
  4066. // restore the original top-margin
  4067. $this->tMargin = $this->theadMargins['top'];
  4068. $this->pagedim[$this->page]['tm'] = $this->tMargin;
  4069. $this->y = $this->tMargin;
  4070. }
  4071. if (!$this->empty_string($this->thead) AND (!$this->inthead)) {
  4072. // set margins
  4073. $prev_lMargin = $this->lMargin;
  4074. $prev_rMargin = $this->rMargin;
  4075. $prev_cell_padding = $this->cell_padding;
  4076. $this->lMargin = $this->theadMargins['lmargin'] + ($this->pagedim[$this->page]['olm'] - $this->pagedim[$this->theadMargins['page']]['olm']);
  4077. $this->rMargin = $this->theadMargins['rmargin'] + ($this->pagedim[$this->page]['orm'] - $this->pagedim[$this->theadMargins['page']]['orm']);
  4078. $this->cell_padding = $this->theadMargins['cell_padding'];
  4079. if ($this->rtl) {
  4080. $this->x = $this->w - $this->rMargin;
  4081. } else {
  4082. $this->x = $this->lMargin;
  4083. }
  4084. // account for special "cell" mode
  4085. if ($this->theadMargins['cell']) {
  4086. if ($this->rtl) {
  4087. $this->x -= $this->cell_padding['R'];
  4088. } else {
  4089. $this->x += $this->cell_padding['L'];
  4090. }
  4091. }
  4092. // print table header
  4093. $this->writeHTML($this->thead, false, false, false, false, '');
  4094. // set new top margin to skip the table headers
  4095. if (!isset($this->theadMargins['top'])) {
  4096. $this->theadMargins['top'] = $this->tMargin;
  4097. }
  4098. // store end of header position
  4099. if (!isset($this->columns[0]['th'])) {
  4100. $this->columns[0]['th'] = array();
  4101. }
  4102. $this->columns[0]['th']['\''.$this->page.'\''] = $this->y;
  4103. $this->tMargin = $this->y;
  4104. $this->pagedim[$this->page]['tm'] = $this->tMargin;
  4105. $this->lasth = 0;
  4106. $this->lMargin = $prev_lMargin;
  4107. $this->rMargin = $prev_rMargin;
  4108. $this->cell_padding = $prev_cell_padding;
  4109. }
  4110. }
  4111. /**
  4112. * Returns the current page number.
  4113. * @return int page number
  4114. * @public
  4115. * @since 1.0
  4116. * @see getAliasNbPages()
  4117. */
  4118. public function PageNo() {
  4119. return $this->page;
  4120. }
  4121. /**
  4122. * Defines a new spot color.
  4123. * It can be expressed in RGB components or gray scale.
  4124. * The method can be called before the first page is created and the value is retained from page to page.
  4125. * @param $name (string) Full name of the spot color.
  4126. * @param $c (float) Cyan color for CMYK. Value between 0 and 100.
  4127. * @param $m (float) Magenta color for CMYK. Value between 0 and 100.
  4128. * @param $y (float) Yellow color for CMYK. Value between 0 and 100.
  4129. * @param $k (float) Key (Black) color for CMYK. Value between 0 and 100.
  4130. * @public
  4131. * @since 4.0.024 (2008-09-12)
  4132. * @see SetDrawSpotColor(), SetFillSpotColor(), SetTextSpotColor()
  4133. */
  4134. public function AddSpotColor($name, $c, $m, $y, $k) {
  4135. if (!isset($this->spot_colors[$name])) {
  4136. $i = (1 + count($this->spot_colors));
  4137. $this->spot_colors[$name] = array('C' => $c, 'M' => $m, 'Y' => $y, 'K' => $k, 'name' => $name, 'i' => $i);
  4138. }
  4139. }
  4140. /**
  4141. * Return the Spot color array.
  4142. * @param $name (string) Name of the spot color.
  4143. * @return (array) Spot color array or false if not defined.
  4144. * @public
  4145. * @since 5.9.125 (2011-10-03)
  4146. */
  4147. public function getSpotColor($name) {
  4148. if (isset($this->spot_colors[$name])) {
  4149. return $this->spot_colors[$name];
  4150. }
  4151. $color = preg_replace('/[\s]*/', '', $name); // remove extra spaces
  4152. $color = strtolower($color);
  4153. if (isset($this->spotcolor[$color])) {
  4154. $this->AddSpotColor($this->spotcolor[$color][4], $this->spotcolor[$color][0], $this->spotcolor[$color][1], $this->spotcolor[$color][2], $this->spotcolor[$color][3]);
  4155. return $this->spot_colors[$this->spotcolor[$color][4]];
  4156. }
  4157. return false;
  4158. }
  4159. /**
  4160. * Set the spot color for the specified type ('draw', 'fill', 'text').
  4161. * @param $type (string) Type of object affected by this color: ('draw', 'fill', 'text').
  4162. * @param $name (string) Name of the spot color.
  4163. * @param $tint (float) Intensity of the color (from 0 to 100 ; 100 = full intensity by default).
  4164. * @return (string) PDF color command.
  4165. * @public
  4166. * @since 5.9.125 (2011-10-03)
  4167. */
  4168. public function setSpotColor($type, $name, $tint=100) {
  4169. $spotcolor = $this->getSpotColor($name);
  4170. if ($spotcolor === false) {
  4171. $this->Error('Undefined spot color: '.$name.', you must add it on the spotcolors.php file.');
  4172. }
  4173. $tint = (max(0, min(100, $tint)) / 100);
  4174. $pdfcolor = sprintf('/CS%d ', $this->spot_colors[$name]['i']);
  4175. switch ($type) {
  4176. case 'draw': {
  4177. $pdfcolor .= sprintf('CS %.3F SCN', $tint);
  4178. $this->DrawColor = $pdfcolor;
  4179. $this->strokecolor = $spotcolor;
  4180. break;
  4181. }
  4182. case 'fill': {
  4183. $pdfcolor .= sprintf('cs %.3F scn', $tint);
  4184. $this->FillColor = $pdfcolor;
  4185. $this->bgcolor = $spotcolor;
  4186. break;
  4187. }
  4188. case 'text': {
  4189. $pdfcolor .= sprintf('cs %.3F scn', $tint);
  4190. $this->TextColor = $pdfcolor;
  4191. $this->fgcolor = $spotcolor;
  4192. break;
  4193. }
  4194. }
  4195. $this->ColorFlag = ($this->FillColor != $this->TextColor);
  4196. if ($this->page > 0) {
  4197. $this->_out($pdfcolor);
  4198. }
  4199. if ($this->inxobj) {
  4200. // we are inside an XObject template
  4201. $this->xobjects[$this->xobjid]['spot_colors'][$name] = $this->spot_colors[$name];
  4202. }
  4203. return $pdfcolor;
  4204. }
  4205. /**
  4206. * Defines the spot color used for all drawing operations (lines, rectangles and cell borders).
  4207. * @param $name (string) Name of the spot color.
  4208. * @param $tint (float) Intensity of the color (from 0 to 100 ; 100 = full intensity by default).
  4209. * @public
  4210. * @since 4.0.024 (2008-09-12)
  4211. * @see AddSpotColor(), SetFillSpotColor(), SetTextSpotColor()
  4212. */
  4213. public function SetDrawSpotColor($name, $tint=100) {
  4214. $this->setSpotColor('draw', $name, $tint);
  4215. }
  4216. /**
  4217. * Defines the spot color used for all filling operations (filled rectangles and cell backgrounds).
  4218. * @param $name (string) Name of the spot color.
  4219. * @param $tint (float) Intensity of the color (from 0 to 100 ; 100 = full intensity by default).
  4220. * @public
  4221. * @since 4.0.024 (2008-09-12)
  4222. * @see AddSpotColor(), SetDrawSpotColor(), SetTextSpotColor()
  4223. */
  4224. public function SetFillSpotColor($name, $tint=100) {
  4225. $this->setSpotColor('fill', $name, $tint);
  4226. }
  4227. /**
  4228. * Defines the spot color used for text.
  4229. * @param $name (string) Name of the spot color.
  4230. * @param $tint (int) Intensity of the color (from 0 to 100 ; 100 = full intensity by default).
  4231. * @public
  4232. * @since 4.0.024 (2008-09-12)
  4233. * @see AddSpotColor(), SetDrawSpotColor(), SetFillSpotColor()
  4234. */
  4235. public function SetTextSpotColor($name, $tint=100) {
  4236. $this->setSpotColor('text', $name, $tint);
  4237. }
  4238. /**
  4239. * Set the color array for the specified type ('draw', 'fill', 'text').
  4240. * It can be expressed in RGB, CMYK or GRAY SCALE components.
  4241. * The method can be called before the first page is created and the value is retained from page to page.
  4242. * @param $type (string) Type of object affected by this color: ('draw', 'fill', 'text').
  4243. * @param $color (array) Array of colors (1=gray, 3=RGB, 4=CMYK or 5=spotcolor=CMYK+name values).
  4244. * @param $ret (boolean) If true do not send the PDF command.
  4245. * @return (string) The PDF command or empty string.
  4246. * @public
  4247. * @since 3.1.000 (2008-06-11)
  4248. */
  4249. public function setColorArray($type, $color, $ret=false) {
  4250. if (is_array($color)) {
  4251. $color = array_values($color);
  4252. // component: grey, RGB red or CMYK cyan
  4253. $c = isset($color[0]) ? $color[0] : -1;
  4254. // component: RGB green or CMYK magenta
  4255. $m = isset($color[1]) ? $color[1] : -1;
  4256. // component: RGB blue or CMYK yellow
  4257. $y = isset($color[2]) ? $color[2] : -1;
  4258. // component: CMYK black
  4259. $k = isset($color[3]) ? $color[3] : -1;
  4260. // color name
  4261. $name = isset($color[4]) ? $color[4] : '';
  4262. if ($c >= 0) {
  4263. return $this->setColor($type, $c, $m, $y, $k, $ret, $name);
  4264. }
  4265. }
  4266. return '';
  4267. }
  4268. /**
  4269. * Defines the color used for all drawing operations (lines, rectangles and cell borders).
  4270. * It can be expressed in RGB, CMYK or GRAY SCALE components.
  4271. * The method can be called before the first page is created and the value is retained from page to page.
  4272. * @param $color (array) Array of colors (1, 3 or 4 values).
  4273. * @param $ret (boolean) If true do not send the PDF command.
  4274. * @return string the PDF command
  4275. * @public
  4276. * @since 3.1.000 (2008-06-11)
  4277. * @see SetDrawColor()
  4278. */
  4279. public function SetDrawColorArray($color, $ret=false) {
  4280. return $this->setColorArray('draw', $color, $ret);
  4281. }
  4282. /**
  4283. * Defines the color used for all filling operations (filled rectangles and cell backgrounds).
  4284. * It can be expressed in RGB, CMYK or GRAY SCALE components.
  4285. * The method can be called before the first page is created and the value is retained from page to page.
  4286. * @param $color (array) Array of colors (1, 3 or 4 values).
  4287. * @param $ret (boolean) If true do not send the PDF command.
  4288. * @public
  4289. * @since 3.1.000 (2008-6-11)
  4290. * @see SetFillColor()
  4291. */
  4292. public function SetFillColorArray($color, $ret=false) {
  4293. return $this->setColorArray('fill', $color, $ret);
  4294. }
  4295. /**
  4296. * Defines the color used for text. It can be expressed in RGB components or gray scale.
  4297. * The method can be called before the first page is created and the value is retained from page to page.
  4298. * @param $color (array) Array of colors (1, 3 or 4 values).
  4299. * @param $ret (boolean) If true do not send the PDF command.
  4300. * @public
  4301. * @since 3.1.000 (2008-6-11)
  4302. * @see SetFillColor()
  4303. */
  4304. public function SetTextColorArray($color, $ret=false) {
  4305. return $this->setColorArray('text', $color, $ret);
  4306. }
  4307. /**
  4308. * Defines the color used by the specified type ('draw', 'fill', 'text').
  4309. * @param $type (string) Type of object affected by this color: ('draw', 'fill', 'text').
  4310. * @param $col1 (float) GRAY level for single color, or Red color for RGB (0-255), or CYAN color for CMYK (0-100).
  4311. * @param $col2 (float) GREEN color for RGB (0-255), or MAGENTA color for CMYK (0-100).
  4312. * @param $col3 (float) BLUE color for RGB (0-255), or YELLOW color for CMYK (0-100).
  4313. * @param $col4 (float) KEY (BLACK) color for CMYK (0-100).
  4314. * @param $ret (boolean) If true do not send the command.
  4315. * @param $name (string) spot color name (if any)
  4316. * @return (string) The PDF command or empty string.
  4317. * @public
  4318. * @since 5.9.125 (2011-10-03)
  4319. */
  4320. public function setColor($type, $col1=0, $col2=-1, $col3=-1, $col4=-1, $ret=false, $name='') {
  4321. // set default values
  4322. if (!is_numeric($col1)) {
  4323. $col1 = 0;
  4324. }
  4325. if (!is_numeric($col2)) {
  4326. $col2 = -1;
  4327. }
  4328. if (!is_numeric($col3)) {
  4329. $col3 = -1;
  4330. }
  4331. if (!is_numeric($col4)) {
  4332. $col4 = -1;
  4333. }
  4334. // set color by case
  4335. $suffix = '';
  4336. if (($col2 == -1) AND ($col3 == -1) AND ($col4 == -1)) {
  4337. // Grey scale
  4338. $col1 = max(0, min(255, $col1));
  4339. $intcolor = array('G' => $col1);
  4340. $pdfcolor = sprintf('%.3F ', ($col1 / 255));
  4341. $suffix = 'g';
  4342. } elseif ($col4 == -1) {
  4343. // RGB
  4344. $col1 = max(0, min(255, $col1));
  4345. $col2 = max(0, min(255, $col2));
  4346. $col3 = max(0, min(255, $col3));
  4347. $intcolor = array('R' => $col1, 'G' => $col2, 'B' => $col3);
  4348. $pdfcolor = sprintf('%.3F %.3F %.3F ', ($col1 / 255), ($col2 / 255), ($col3 / 255));
  4349. $suffix = 'rg';
  4350. } else {
  4351. $col1 = max(0, min(100, $col1));
  4352. $col2 = max(0, min(100, $col2));
  4353. $col3 = max(0, min(100, $col3));
  4354. $col4 = max(0, min(100, $col4));
  4355. if (empty($name)) {
  4356. // CMYK
  4357. $intcolor = array('C' => $col1, 'M' => $col2, 'Y' => $col3, 'K' => $col4);
  4358. $pdfcolor = sprintf('%.3F %.3F %.3F %.3F ', ($col1 / 100), ($col2 / 100), ($col3 / 100), ($col4 / 100));
  4359. $suffix = 'k';
  4360. } else {
  4361. // SPOT COLOR
  4362. $intcolor = array('C' => $col1, 'M' => $col2, 'Y' => $col3, 'K' => $col4, 'name' => $name);
  4363. $this->AddSpotColor($name, $col1, $col2, $col3, $col4);
  4364. $pdfcolor = $this->setSpotColor($type, $name, 100);
  4365. }
  4366. }
  4367. switch ($type) {
  4368. case 'draw': {
  4369. $pdfcolor .= strtoupper($suffix);
  4370. $this->DrawColor = $pdfcolor;
  4371. $this->strokecolor = $intcolor;
  4372. break;
  4373. }
  4374. case 'fill': {
  4375. $pdfcolor .= $suffix;
  4376. $this->FillColor = $pdfcolor;
  4377. $this->bgcolor = $intcolor;
  4378. break;
  4379. }
  4380. case 'text': {
  4381. $pdfcolor .= $suffix;
  4382. $this->TextColor = $pdfcolor;
  4383. $this->fgcolor = $intcolor;
  4384. break;
  4385. }
  4386. }
  4387. $this->ColorFlag = ($this->FillColor != $this->TextColor);
  4388. if (($type != 'text') AND ($this->page > 0)) {
  4389. if (!$ret) {
  4390. $this->_out($pdfcolor);
  4391. }
  4392. return $pdfcolor;
  4393. }
  4394. return '';
  4395. }
  4396. /**
  4397. * Convert a color array into a string representation.
  4398. * @param $c (array) Array of colors.
  4399. * @return (string) The color array representation.
  4400. * @protected
  4401. * @since 5.9.137 (2011-12-01)
  4402. */
  4403. protected function getColorStringFromArray($c) {
  4404. $c = array_values($c);
  4405. $color = '[';
  4406. switch (count($c)) {
  4407. case 4: {
  4408. // CMYK
  4409. $color .= sprintf('%.3F %.3F %.3F %.3F', (max(0, min(100, floatval($c[0]))) / 100), (max(0, min(100, floatval($c[1]))) / 100), (max(0, min(100, floatval($c[2]))) / 100), (max(0, min(100, floatval($c[3]))) / 100));
  4410. break;
  4411. }
  4412. case 3: {
  4413. // RGB
  4414. $color .= sprintf('%.3F %.3F %.3F', (max(0, min(255, floatval($c[0]))) / 255), (max(0, min(255, floatval($c[1]))) / 255), (max(0, min(255, floatval($c[2]))) / 255));
  4415. break;
  4416. }
  4417. case 1: {
  4418. // grayscale
  4419. $color .= sprintf('%.3F', (max(0, min(255, floatval($c[0]))) / 255));
  4420. break;
  4421. }
  4422. }
  4423. $color .= ']';
  4424. return $color;
  4425. }
  4426. /**
  4427. * 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.
  4428. * @param $col1 (float) GRAY level for single color, or Red color for RGB (0-255), or CYAN color for CMYK (0-100).
  4429. * @param $col2 (float) GREEN color for RGB (0-255), or MAGENTA color for CMYK (0-100).
  4430. * @param $col3 (float) BLUE color for RGB (0-255), or YELLOW color for CMYK (0-100).
  4431. * @param $col4 (float) KEY (BLACK) color for CMYK (0-100).
  4432. * @param $ret (boolean) If true do not send the command.
  4433. * @param $name (string) spot color name (if any)
  4434. * @return string the PDF command
  4435. * @public
  4436. * @since 1.3
  4437. * @see SetDrawColorArray(), SetFillColor(), SetTextColor(), Line(), Rect(), Cell(), MultiCell()
  4438. */
  4439. public function SetDrawColor($col1=0, $col2=-1, $col3=-1, $col4=-1, $ret=false, $name='') {
  4440. return $this->setColor('draw', $col1, $col2, $col3, $col4, $ret, $name);
  4441. }
  4442. /**
  4443. * 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.
  4444. * @param $col1 (float) GRAY level for single color, or Red color for RGB (0-255), or CYAN color for CMYK (0-100).
  4445. * @param $col2 (float) GREEN color for RGB (0-255), or MAGENTA color for CMYK (0-100).
  4446. * @param $col3 (float) BLUE color for RGB (0-255), or YELLOW color for CMYK (0-100).
  4447. * @param $col4 (float) KEY (BLACK) color for CMYK (0-100).
  4448. * @param $ret (boolean) If true do not send the command.
  4449. * @param $name (string) Spot color name (if any).
  4450. * @return (string) The PDF command.
  4451. * @public
  4452. * @since 1.3
  4453. * @see SetFillColorArray(), SetDrawColor(), SetTextColor(), Rect(), Cell(), MultiCell()
  4454. */
  4455. public function SetFillColor($col1=0, $col2=-1, $col3=-1, $col4=-1, $ret=false, $name='') {
  4456. return $this->setColor('fill', $col1, $col2, $col3, $col4, $ret, $name);
  4457. }
  4458. /**
  4459. * 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.
  4460. * @param $col1 (float) GRAY level for single color, or Red color for RGB (0-255), or CYAN color for CMYK (0-100).
  4461. * @param $col2 (float) GREEN color for RGB (0-255), or MAGENTA color for CMYK (0-100).
  4462. * @param $col3 (float) BLUE color for RGB (0-255), or YELLOW color for CMYK (0-100).
  4463. * @param $col4 (float) KEY (BLACK) color for CMYK (0-100).
  4464. * @param $ret (boolean) If true do not send the command.
  4465. * @param $name (string) Spot color name (if any).
  4466. * @return (string) Empty string.
  4467. * @public
  4468. * @since 1.3
  4469. * @see SetTextColorArray(), SetDrawColor(), SetFillColor(), Text(), Cell(), MultiCell()
  4470. */
  4471. public function SetTextColor($col1=0, $col2=-1, $col3=-1, $col4=-1, $ret=false, $name='') {
  4472. return $this->setColor('text', $col1, $col2, $col3, $col4, $ret, $name);
  4473. }
  4474. /**
  4475. * Returns the length of a string in user unit. A font must be selected.<br>
  4476. * @param $s (string) The string whose length is to be computed
  4477. * @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.
  4478. * @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.
  4479. * @param $fontsize (float) Font size in points. The default value is the current size.
  4480. * @param $getarray (boolean) if true returns an array of characters widths, if false returns the total length.
  4481. * @return mixed int total string length or array of characted widths
  4482. * @author Nicola Asuni
  4483. * @public
  4484. * @since 1.2
  4485. */
  4486. public function GetStringWidth($s, $fontname='', $fontstyle='', $fontsize=0, $getarray=false) {
  4487. return $this->GetArrStringWidth($this->utf8Bidi($this->UTF8StringToArray($s), $s, $this->tmprtl), $fontname, $fontstyle, $fontsize, $getarray);
  4488. }
  4489. /**
  4490. * Returns the string length of an array of chars in user unit or an array of characters widths. A font must be selected.<br>
  4491. * @param $sa (string) The array of chars whose total length is to be computed
  4492. * @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.
  4493. * @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.
  4494. * @param $fontsize (float) Font size in points. The default value is the current size.
  4495. * @param $getarray (boolean) if true returns an array of characters widths, if false returns the total length.
  4496. * @return mixed int total string length or array of characted widths
  4497. * @author Nicola Asuni
  4498. * @public
  4499. * @since 2.4.000 (2008-03-06)
  4500. */
  4501. public function GetArrStringWidth($sa, $fontname='', $fontstyle='', $fontsize=0, $getarray=false) {
  4502. // store current values
  4503. if (!$this->empty_string($fontname)) {
  4504. $prev_FontFamily = $this->FontFamily;
  4505. $prev_FontStyle = $this->FontStyle;
  4506. $prev_FontSizePt = $this->FontSizePt;
  4507. $this->SetFont($fontname, $fontstyle, $fontsize, '', 'default', false);
  4508. }
  4509. // convert UTF-8 array to Latin1 if required
  4510. $sa = $this->UTF8ArrToLatin1($sa);
  4511. $w = 0; // total width
  4512. $wa = array(); // array of characters widths
  4513. foreach ($sa as $ck => $char) {
  4514. // character width
  4515. $cw = $this->GetCharWidth($char, isset($sa[($ck + 1)]));
  4516. $wa[] = $cw;
  4517. $w += $cw;
  4518. }
  4519. // restore previous values
  4520. if (!$this->empty_string($fontname)) {
  4521. $this->SetFont($prev_FontFamily, $prev_FontStyle, $prev_FontSizePt, '', 'default', false);
  4522. }
  4523. if ($getarray) {
  4524. return $wa;
  4525. }
  4526. return $w;
  4527. }
  4528. /**
  4529. * Returns the length of the char in user unit for the current font considering current stretching and spacing (tracking/kerning).
  4530. * @param $char (int) The char code whose length is to be returned
  4531. * @param $notlast (boolean) set to false for the latest character on string, true otherwise (default)
  4532. * @return float char width
  4533. * @author Nicola Asuni
  4534. * @public
  4535. * @since 2.4.000 (2008-03-06)
  4536. */
  4537. public function GetCharWidth($char, $notlast=true) {
  4538. // get raw width
  4539. $chw = $this->getRawCharWidth($char);
  4540. if (($this->font_spacing != 0) AND $notlast) {
  4541. // increase/decrease font spacing
  4542. $chw += $this->font_spacing;
  4543. }
  4544. if ($this->font_stretching != 100) {
  4545. // fixed stretching mode
  4546. $chw *= ($this->font_stretching / 100);
  4547. }
  4548. return $chw;
  4549. }
  4550. /**
  4551. * Returns the length of the char in user unit for the current font.
  4552. * @param $char (int) The char code whose length is to be returned
  4553. * @return float char width
  4554. * @author Nicola Asuni
  4555. * @public
  4556. * @since 5.9.000 (2010-09-28)
  4557. */
  4558. public function getRawCharWidth($char) {
  4559. if ($char == 173) {
  4560. // SHY character will not be printed
  4561. return (0);
  4562. }
  4563. if (isset($this->CurrentFont['cw'][$char])) {
  4564. $w = $this->CurrentFont['cw'][$char];
  4565. } elseif (isset($this->CurrentFont['dw'])) {
  4566. // default width
  4567. $w = $this->CurrentFont['dw'];
  4568. } elseif (isset($this->CurrentFont['cw'][32])) {
  4569. // default width
  4570. $w = $this->CurrentFont['cw'][32];
  4571. } else {
  4572. $w = 600;
  4573. }
  4574. return ($w * $this->FontSize / 1000);
  4575. }
  4576. /**
  4577. * Returns the numbero of characters in a string.
  4578. * @param $s (string) The input string.
  4579. * @return int number of characters
  4580. * @public
  4581. * @since 2.0.0001 (2008-01-07)
  4582. */
  4583. public function GetNumChars($s) {
  4584. if ($this->isUnicodeFont()) {
  4585. return count($this->UTF8StringToArray($s));
  4586. }
  4587. return strlen($s);
  4588. }
  4589. /**
  4590. * Fill the list of available fonts ($this->fontlist).
  4591. * @protected
  4592. * @since 4.0.013 (2008-07-28)
  4593. */
  4594. protected function getFontsList() {
  4595. $fontsdir = opendir($this->_getfontpath());
  4596. while (($file = readdir($fontsdir)) !== false) {
  4597. if (substr($file, -4) == '.php') {
  4598. array_push($this->fontlist, strtolower(basename($file, '.php')));
  4599. }
  4600. }
  4601. closedir($fontsdir);
  4602. }
  4603. /**
  4604. * Imports a TrueType, Type1, core, or CID0 font and makes it available.
  4605. * It is necessary to generate a font definition file first (read /fonts/utils/README.TXT).
  4606. * 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.
  4607. * @param $family (string) Font family. The name can be chosen arbitrarily. If it is a standard family name, it will override the corresponding font.
  4608. * @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>
  4609. * @param $fontfile (string) The font definition file. By default, the name is built from the family and style, in lower case with no spaces.
  4610. * @return array containing the font data, or false in case of error.
  4611. * @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.
  4612. * @public
  4613. * @since 1.5
  4614. * @see SetFont(), setFontSubsetting()
  4615. */
  4616. public function AddFont($family, $style='', $fontfile='', $subset='default') {
  4617. if ($subset === 'default') {
  4618. $subset = $this->font_subsetting;
  4619. }
  4620. if ($this->pdfa_mode) {
  4621. $subset = false;
  4622. }
  4623. if ($this->empty_string($family)) {
  4624. if (!$this->empty_string($this->FontFamily)) {
  4625. $family = $this->FontFamily;
  4626. } else {
  4627. $this->Error('Empty font family');
  4628. }
  4629. }
  4630. // move embedded styles on $style
  4631. if (substr($family, -1) == 'I') {
  4632. $style .= 'I';
  4633. $family = substr($family, 0, -1);
  4634. }
  4635. if (substr($family, -1) == 'B') {
  4636. $style .= 'B';
  4637. $family = substr($family, 0, -1);
  4638. }
  4639. // normalize family name
  4640. $family = strtolower($family);
  4641. if ((!$this->isunicode) AND ($family == 'arial')) {
  4642. $family = 'helvetica';
  4643. }
  4644. if (($family == 'symbol') OR ($family == 'zapfdingbats')) {
  4645. $style = '';
  4646. }
  4647. if ($this->pdfa_mode AND (isset($this->CoreFonts[$family]))) {
  4648. // all fonts must be embedded
  4649. $family = 'pdfa'.$family;
  4650. }
  4651. $tempstyle = strtoupper($style);
  4652. $style = '';
  4653. // underline
  4654. if (strpos($tempstyle, 'U') !== false) {
  4655. $this->underline = true;
  4656. } else {
  4657. $this->underline = false;
  4658. }
  4659. // line-through (deleted)
  4660. if (strpos($tempstyle, 'D') !== false) {
  4661. $this->linethrough = true;
  4662. } else {
  4663. $this->linethrough = false;
  4664. }
  4665. // overline
  4666. if (strpos($tempstyle, 'O') !== false) {
  4667. $this->overline = true;
  4668. } else {
  4669. $this->overline = false;
  4670. }
  4671. // bold
  4672. if (strpos($tempstyle, 'B') !== false) {
  4673. $style .= 'B';
  4674. }
  4675. // oblique
  4676. if (strpos($tempstyle, 'I') !== false) {
  4677. $style .= 'I';
  4678. }
  4679. $bistyle = $style;
  4680. $fontkey = $family.$style;
  4681. $font_style = $style.($this->underline ? 'U' : '').($this->linethrough ? 'D' : '').($this->overline ? 'O' : '');
  4682. $fontdata = array('fontkey' => $fontkey, 'family' => $family, 'style' => $font_style);
  4683. // check if the font has been already added
  4684. $fb = $this->getFontBuffer($fontkey);
  4685. if ($fb !== false) {
  4686. if ($this->inxobj) {
  4687. // we are inside an XObject template
  4688. $this->xobjects[$this->xobjid]['fonts'][$fontkey] = $fb['i'];
  4689. }
  4690. return $fontdata;
  4691. }
  4692. if (isset($type)) {
  4693. unset($type);
  4694. }
  4695. if (isset($cw)) {
  4696. unset($cw);
  4697. }
  4698. // get specified font directory (if any)
  4699. $fontdir = false;
  4700. if (!$this->empty_string($fontfile)) {
  4701. $fontdir = dirname($fontfile);
  4702. if ($this->empty_string($fontdir) OR ($fontdir == '.')) {
  4703. $fontdir = '';
  4704. } else {
  4705. $fontdir .= '/';
  4706. }
  4707. }
  4708. $missing_style = false; // true when the font style variation is missing
  4709. // search and include font file
  4710. if ($this->empty_string($fontfile) OR (!file_exists($fontfile))) {
  4711. // build a standard filenames for specified font
  4712. $tmp_fontfile = str_replace(' ', '', $family).strtolower($style).'.php';
  4713. // search files on various directories
  4714. if (($fontdir !== false) AND file_exists($fontdir.$tmp_fontfile)) {
  4715. $fontfile = $fontdir.$tmp_fontfile;
  4716. } elseif (file_exists($this->_getfontpath().$tmp_fontfile)) {
  4717. $fontfile = $this->_getfontpath().$tmp_fontfile;
  4718. } elseif (file_exists($tmp_fontfile)) {
  4719. $fontfile = $tmp_fontfile;
  4720. } elseif (!$this->empty_string($style)) {
  4721. $missing_style = true;
  4722. // try to remove the style part
  4723. $tmp_fontfile = str_replace(' ', '', $family).'.php';
  4724. if (($fontdir !== false) AND file_exists($fontdir.$tmp_fontfile)) {
  4725. $fontfile = $fontdir.$tmp_fontfile;
  4726. } elseif (file_exists($this->_getfontpath().$tmp_fontfile)) {
  4727. $fontfile = $this->_getfontpath().$tmp_fontfile;
  4728. } else {
  4729. $fontfile = $tmp_fontfile;
  4730. }
  4731. }
  4732. }
  4733. // include font file
  4734. if (file_exists($fontfile)) {
  4735. include($fontfile);
  4736. } else {
  4737. $this->Error('Could not include font definition file: '.$family.'');
  4738. }
  4739. // check font parameters
  4740. if ((!isset($type)) OR (!isset($cw))) {
  4741. $this->Error('The font definition file has a bad format: '.$fontfile.'');
  4742. }
  4743. // SET default parameters
  4744. if (!isset($file) OR $this->empty_string($file)) {
  4745. $file = '';
  4746. }
  4747. if (!isset($enc) OR $this->empty_string($enc)) {
  4748. $enc = '';
  4749. }
  4750. if (!isset($cidinfo) OR $this->empty_string($cidinfo)) {
  4751. $cidinfo = array('Registry'=>'Adobe', 'Ordering'=>'Identity', 'Supplement'=>0);
  4752. $cidinfo['uni2cid'] = array();
  4753. }
  4754. if (!isset($ctg) OR $this->empty_string($ctg)) {
  4755. $ctg = '';
  4756. }
  4757. if (!isset($desc) OR $this->empty_string($desc)) {
  4758. $desc = array();
  4759. }
  4760. if (!isset($up) OR $this->empty_string($up)) {
  4761. $up = -100;
  4762. }
  4763. if (!isset($ut) OR $this->empty_string($ut)) {
  4764. $ut = 50;
  4765. }
  4766. if (!isset($cw) OR $this->empty_string($cw)) {
  4767. $cw = array();
  4768. }
  4769. if (!isset($dw) OR $this->empty_string($dw)) {
  4770. // set default width
  4771. if (isset($desc['MissingWidth']) AND ($desc['MissingWidth'] > 0)) {
  4772. $dw = $desc['MissingWidth'];
  4773. } elseif (isset($cw[32])) {
  4774. $dw = $cw[32];
  4775. } else {
  4776. $dw = 600;
  4777. }
  4778. }
  4779. ++$this->numfonts;
  4780. if ($type == 'core') {
  4781. $name = $this->CoreFonts[$fontkey];
  4782. $subset = false;
  4783. } elseif (($type == 'TrueType') OR ($type == 'Type1')) {
  4784. $subset = false;
  4785. } elseif ($type == 'TrueTypeUnicode') {
  4786. $enc = 'Identity-H';
  4787. } elseif ($type == 'cidfont0') {
  4788. if ($this->pdfa_mode) {
  4789. $this->Error('All fonts must be embedded in PDF/A mode!');
  4790. }
  4791. } else {
  4792. $this->Error('Unknow font type: '.$type.'');
  4793. }
  4794. // set name if unset
  4795. if (!isset($name) OR empty($name)) {
  4796. $name = $fontkey;
  4797. }
  4798. // create artificial font style variations if missing (only works with non-embedded fonts)
  4799. if (($type != 'core') AND $missing_style) {
  4800. // style variations
  4801. $styles = array('' => '', 'B' => ',Bold', 'I' => ',Italic', 'BI' => ',BoldItalic');
  4802. $name .= $styles[$bistyle];
  4803. // artificial bold
  4804. if (strpos($bistyle, 'B') !== false) {
  4805. if (isset($desc['StemV'])) {
  4806. // from normal to bold
  4807. $desc['StemV'] = round($desc['StemV'] * 1.75);
  4808. } else {
  4809. // bold
  4810. $desc['StemV'] = 123;
  4811. }
  4812. }
  4813. // artificial italic
  4814. if (strpos($bistyle, 'I') !== false) {
  4815. if (isset($desc['ItalicAngle'])) {
  4816. $desc['ItalicAngle'] -= 11;
  4817. } else {
  4818. $desc['ItalicAngle'] = -11;
  4819. }
  4820. if (isset($desc['Flags'])) {
  4821. $desc['Flags'] |= 64; //bit 7
  4822. } else {
  4823. $desc['Flags'] = 64;
  4824. }
  4825. }
  4826. }
  4827. // initialize subsetchars to contain default ASCII values (0-255)
  4828. $subsetchars = array_fill(0, 256, true);
  4829. $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));
  4830. if ($this->inxobj) {
  4831. // we are inside an XObject template
  4832. $this->xobjects[$this->xobjid]['fonts'][$fontkey] = $this->numfonts;
  4833. }
  4834. if (isset($diff) AND (!empty($diff))) {
  4835. //Search existing encodings
  4836. $d = 0;
  4837. $nb = count($this->diffs);
  4838. for ($i=1; $i <= $nb; ++$i) {
  4839. if ($this->diffs[$i] == $diff) {
  4840. $d = $i;
  4841. break;
  4842. }
  4843. }
  4844. if ($d == 0) {
  4845. $d = $nb + 1;
  4846. $this->diffs[$d] = $diff;
  4847. }
  4848. $this->setFontSubBuffer($fontkey, 'diff', $d);
  4849. }
  4850. if (!$this->empty_string($file)) {
  4851. if (!isset($this->FontFiles[$file])) {
  4852. if ((strcasecmp($type,'TrueType') == 0) OR (strcasecmp($type, 'TrueTypeUnicode') == 0)) {
  4853. $this->FontFiles[$file] = array('length1' => $originalsize, 'fontdir' => $fontdir, 'subset' => $subset, 'fontkeys' => array($fontkey));
  4854. } elseif ($type != 'core') {
  4855. $this->FontFiles[$file] = array('length1' => $size1, 'length2' => $size2, 'fontdir' => $fontdir, 'subset' => $subset, 'fontkeys' => array($fontkey));
  4856. }
  4857. } else {
  4858. // update fontkeys that are sharing this font file
  4859. $this->FontFiles[$file]['subset'] = ($this->FontFiles[$file]['subset'] AND $subset);
  4860. if (!in_array($fontkey, $this->FontFiles[$file]['fontkeys'])) {
  4861. $this->FontFiles[$file]['fontkeys'][] = $fontkey;
  4862. }
  4863. }
  4864. }
  4865. return $fontdata;
  4866. }
  4867. /**
  4868. * Sets the font used to print character strings.
  4869. * The font can be either a standard one or a font added via the AddFont() method. Standard fonts use Windows encoding cp1252 (Western Europe).
  4870. * The method can be called before the first page is created and the font is retained from page to page.
  4871. * If you just wish to change the current font size, it is simpler to call SetFontSize().
  4872. * 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 />
  4873. * @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.
  4874. * @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.
  4875. * @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
  4876. * @param $fontfile (string) The font definition file. By default, the name is built from the family and style, in lower case with no spaces.
  4877. * @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.
  4878. * @param $out (boolean) if true output the font size command, otherwise only set the font properties.
  4879. * @author Nicola Asuni
  4880. * @public
  4881. * @since 1.0
  4882. * @see AddFont(), SetFontSize()
  4883. */
  4884. public function SetFont($family, $style='', $size=0, $fontfile='', $subset='default', $out=true) {
  4885. //Select a font; size given in points
  4886. if ($size == 0) {
  4887. $size = $this->FontSizePt;
  4888. }
  4889. // try to add font (if not already added)
  4890. $fontdata = $this->AddFont($family, $style, $fontfile, $subset);
  4891. $this->FontFamily = $fontdata['family'];
  4892. $this->FontStyle = $fontdata['style'];
  4893. $this->CurrentFont = $this->getFontBuffer($fontdata['fontkey']);
  4894. $this->SetFontSize($size, $out);
  4895. }
  4896. /**
  4897. * Defines the size of the current font.
  4898. * @param $size (float) The font size in points.
  4899. * @param $out (boolean) if true output the font size command, otherwise only set the font properties.
  4900. * @public
  4901. * @since 1.0
  4902. * @see SetFont()
  4903. */
  4904. public function SetFontSize($size, $out=true) {
  4905. // font size in points
  4906. $this->FontSizePt = $size;
  4907. // font size in user units
  4908. $this->FontSize = $size / $this->k;
  4909. // calculate some font metrics
  4910. if (isset($this->CurrentFont['desc']['FontBBox'])) {
  4911. $bbox = explode(' ', substr($this->CurrentFont['desc']['FontBBox'], 1, -1));
  4912. $font_height = ((intval($bbox[3]) - intval($bbox[1])) * $size / 1000);
  4913. } else {
  4914. $font_height = $size * 1.219;
  4915. }
  4916. if (isset($this->CurrentFont['desc']['Ascent']) AND ($this->CurrentFont['desc']['Ascent'] > 0)) {
  4917. $font_ascent = ($this->CurrentFont['desc']['Ascent'] * $size / 1000);
  4918. }
  4919. if (isset($this->CurrentFont['desc']['Descent']) AND ($this->CurrentFont['desc']['Descent'] <= 0)) {
  4920. $font_descent = (- $this->CurrentFont['desc']['Descent'] * $size / 1000);
  4921. }
  4922. if (!isset($font_ascent) AND !isset($font_descent)) {
  4923. // core font
  4924. $font_ascent = 0.76 * $font_height;
  4925. $font_descent = $font_height - $font_ascent;
  4926. } elseif (!isset($font_descent)) {
  4927. $font_descent = $font_height - $font_ascent;
  4928. } elseif (!isset($font_ascent)) {
  4929. $font_ascent = $font_height - $font_descent;
  4930. }
  4931. $this->FontAscent = ($font_ascent / $this->k);
  4932. $this->FontDescent = ($font_descent / $this->k);
  4933. if ($out AND ($this->page > 0) AND (isset($this->CurrentFont['i']))) {
  4934. $this->_out(sprintf('BT /F%d %.2F Tf ET', $this->CurrentFont['i'], $this->FontSizePt));
  4935. }
  4936. }
  4937. /**
  4938. * Return the font descent value
  4939. * @param $font (string) font name
  4940. * @param $style (string) font style
  4941. * @param $size (float) The size (in points)
  4942. * @return int font descent
  4943. * @public
  4944. * @author Nicola Asuni
  4945. * @since 4.9.003 (2010-03-30)
  4946. */
  4947. public function getFontDescent($font, $style='', $size=0) {
  4948. $fontdata = $this->AddFont($font, $style);
  4949. $fontinfo = $this->getFontBuffer($fontdata['fontkey']);
  4950. if (isset($fontinfo['desc']['Descent']) AND ($fontinfo['desc']['Descent'] <= 0)) {
  4951. $descent = (- $fontinfo['desc']['Descent'] * $size / 1000);
  4952. } else {
  4953. $descent = 1.219 * 0.24 * $size;
  4954. }
  4955. return ($descent / $this->k);
  4956. }
  4957. /**
  4958. * Return the font ascent value
  4959. * @param $font (string) font name
  4960. * @param $style (string) font style
  4961. * @param $size (float) The size (in points)
  4962. * @return int font ascent
  4963. * @public
  4964. * @author Nicola Asuni
  4965. * @since 4.9.003 (2010-03-30)
  4966. */
  4967. public function getFontAscent($font, $style='', $size=0) {
  4968. $fontdata = $this->AddFont($font, $style);
  4969. $fontinfo = $this->getFontBuffer($fontdata['fontkey']);
  4970. if (isset($fontinfo['desc']['Ascent']) AND ($fontinfo['desc']['Ascent'] > 0)) {
  4971. $ascent = ($fontinfo['desc']['Ascent'] * $size / 1000);
  4972. } else {
  4973. $ascent = 1.219 * 0.76 * $size;
  4974. }
  4975. return ($ascent / $this->k);
  4976. }
  4977. /**
  4978. * Defines the default monospaced font.
  4979. * @param $font (string) Font name.
  4980. * @public
  4981. * @since 4.5.025
  4982. */
  4983. public function SetDefaultMonospacedFont($font) {
  4984. $this->default_monospaced_font = $font;
  4985. }
  4986. /**
  4987. * 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 />
  4988. * The identifier can then be passed to Cell(), Write(), Image() or Link(). The destination is defined with SetLink().
  4989. * @public
  4990. * @since 1.5
  4991. * @see Cell(), Write(), Image(), Link(), SetLink()
  4992. */
  4993. public function AddLink() {
  4994. //Create a new internal link
  4995. $n = count($this->links) + 1;
  4996. $this->links[$n] = array(0, 0);
  4997. return $n;
  4998. }
  4999. /**
  5000. * Defines the page and position a link points to.
  5001. * @param $link (int) The link identifier returned by AddLink()
  5002. * @param $y (float) Ordinate of target position; -1 indicates the current position. The default value is 0 (top of page)
  5003. * @param $page (int) Number of target page; -1 indicates the current page. This is the default value
  5004. * @public
  5005. * @since 1.5
  5006. * @see AddLink()
  5007. */
  5008. public function SetLink($link, $y=0, $page=-1) {
  5009. if ($y == -1) {
  5010. $y = $this->y;
  5011. }
  5012. if ($page == -1) {
  5013. $page = $this->page;
  5014. }
  5015. $this->links[$link] = array($page, $y);
  5016. }
  5017. /**
  5018. * Puts a link on a rectangular area of the page.
  5019. * 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.
  5020. * @param $x (float) Abscissa of the upper-left corner of the rectangle
  5021. * @param $y (float) Ordinate of the upper-left corner of the rectangle
  5022. * @param $w (float) Width of the rectangle
  5023. * @param $h (float) Height of the rectangle
  5024. * @param $link (mixed) URL or identifier returned by AddLink()
  5025. * @param $spaces (int) number of spaces on the text to link
  5026. * @public
  5027. * @since 1.5
  5028. * @see AddLink(), Annotation(), Cell(), Write(), Image()
  5029. */
  5030. public function Link($x, $y, $w, $h, $link, $spaces=0) {
  5031. $this->Annotation($x, $y, $w, $h, $link, array('Subtype'=>'Link'), $spaces);
  5032. }
  5033. /**
  5034. * Puts a markup annotation on a rectangular area of the page.
  5035. * !!!!THE ANNOTATION SUPPORT IS NOT YET FULLY IMPLEMENTED !!!!
  5036. * @param $x (float) Abscissa of the upper-left corner of the rectangle
  5037. * @param $y (float) Ordinate of the upper-left corner of the rectangle
  5038. * @param $w (float) Width of the rectangle
  5039. * @param $h (float) Height of the rectangle
  5040. * @param $text (string) annotation text or alternate content
  5041. * @param $opt (array) array of options (see section 8.4 of PDF reference 1.7).
  5042. * @param $spaces (int) number of spaces on the text to link
  5043. * @public
  5044. * @since 4.0.018 (2008-08-06)
  5045. */
  5046. public function Annotation($x, $y, $w, $h, $text, $opt=array('Subtype'=>'Text'), $spaces=0) {
  5047. if ($this->inxobj) {
  5048. // store parameters for later use on template
  5049. $this->xobjects[$this->xobjid]['annotations'][] = array('x' => $x, 'y' => $y, 'w' => $w, 'h' => $h, 'text' => $text, 'opt' => $opt, 'spaces' => $spaces);
  5050. return;
  5051. }
  5052. if ($x === '') {
  5053. $x = $this->x;
  5054. }
  5055. if ($y === '') {
  5056. $y = $this->y;
  5057. }
  5058. // check page for no-write regions and adapt page margins if necessary
  5059. list($x, $y) = $this->checkPageRegions($h, $x, $y);
  5060. // recalculate coordinates to account for graphic transformations
  5061. if (isset($this->transfmatrix) AND !empty($this->transfmatrix)) {
  5062. for ($i=$this->transfmatrix_key; $i > 0; --$i) {
  5063. $maxid = count($this->transfmatrix[$i]) - 1;
  5064. for ($j=$maxid; $j >= 0; --$j) {
  5065. $ctm = $this->transfmatrix[$i][$j];
  5066. if (isset($ctm['a'])) {
  5067. $x = $x * $this->k;
  5068. $y = ($this->h - $y) * $this->k;
  5069. $w = $w * $this->k;
  5070. $h = $h * $this->k;
  5071. // top left
  5072. $xt = $x;
  5073. $yt = $y;
  5074. $x1 = ($ctm['a'] * $xt) + ($ctm['c'] * $yt) + $ctm['e'];
  5075. $y1 = ($ctm['b'] * $xt) + ($ctm['d'] * $yt) + $ctm['f'];
  5076. // top right
  5077. $xt = $x + $w;
  5078. $yt = $y;
  5079. $x2 = ($ctm['a'] * $xt) + ($ctm['c'] * $yt) + $ctm['e'];
  5080. $y2 = ($ctm['b'] * $xt) + ($ctm['d'] * $yt) + $ctm['f'];
  5081. // bottom left
  5082. $xt = $x;
  5083. $yt = $y - $h;
  5084. $x3 = ($ctm['a'] * $xt) + ($ctm['c'] * $yt) + $ctm['e'];
  5085. $y3 = ($ctm['b'] * $xt) + ($ctm['d'] * $yt) + $ctm['f'];
  5086. // bottom right
  5087. $xt = $x + $w;
  5088. $yt = $y - $h;
  5089. $x4 = ($ctm['a'] * $xt) + ($ctm['c'] * $yt) + $ctm['e'];
  5090. $y4 = ($ctm['b'] * $xt) + ($ctm['d'] * $yt) + $ctm['f'];
  5091. // new coordinates (rectangle area)
  5092. $x = min($x1, $x2, $x3, $x4);
  5093. $y = max($y1, $y2, $y3, $y4);
  5094. $w = (max($x1, $x2, $x3, $x4) - $x) / $this->k;
  5095. $h = ($y - min($y1, $y2, $y3, $y4)) / $this->k;
  5096. $x = $x / $this->k;
  5097. $y = $this->h - ($y / $this->k);
  5098. }
  5099. }
  5100. }
  5101. }
  5102. if ($this->page <= 0) {
  5103. $page = 1;
  5104. } else {
  5105. $page = $this->page;
  5106. }
  5107. if (!isset($this->PageAnnots[$page])) {
  5108. $this->PageAnnots[$page] = array();
  5109. }
  5110. ++$this->n;
  5111. $this->PageAnnots[$page][] = array('n' => $this->n, 'x' => $x, 'y' => $y, 'w' => $w, 'h' => $h, 'txt' => $text, 'opt' => $opt, 'numspaces' => $spaces);
  5112. if (!$this->pdfa_mode) {
  5113. 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'])]))) {
  5114. ++$this->n;
  5115. $this->embeddedfiles[basename($opt['FS'])] = array('n' => $this->n, 'file' => $opt['FS']);
  5116. }
  5117. }
  5118. // Add widgets annotation's icons
  5119. if (isset($opt['mk']['i']) AND file_exists($opt['mk']['i'])) {
  5120. $this->Image($opt['mk']['i'], '', '', 10, 10, '', '', '', false, 300, '', false, false, 0, false, true);
  5121. }
  5122. if (isset($opt['mk']['ri']) AND file_exists($opt['mk']['ri'])) {
  5123. $this->Image($opt['mk']['ri'], '', '', 0, 0, '', '', '', false, 300, '', false, false, 0, false, true);
  5124. }
  5125. if (isset($opt['mk']['ix']) AND file_exists($opt['mk']['ix'])) {
  5126. $this->Image($opt['mk']['ix'], '', '', 0, 0, '', '', '', false, 300, '', false, false, 0, false, true);
  5127. }
  5128. }
  5129. /**
  5130. * Embedd the attached files.
  5131. * @since 4.4.000 (2008-12-07)
  5132. * @protected
  5133. * @see Annotation()
  5134. */
  5135. protected function _putEmbeddedFiles() {
  5136. if ($this->pdfa_mode) {
  5137. // embedded files are not allowed in PDF/A mode
  5138. return;
  5139. }
  5140. reset($this->embeddedfiles);
  5141. foreach ($this->embeddedfiles as $filename => $filedata) {
  5142. $data = file_get_contents($filedata['file']);
  5143. $filter = '';
  5144. if ($this->compress) {
  5145. $data = gzcompress($data);
  5146. $filter = ' /Filter /FlateDecode';
  5147. }
  5148. $stream = $this->_getrawstream($data, $filedata['n']);
  5149. $out = $this->_getobj($filedata['n'])."\n";
  5150. $out .= '<< /Type /EmbeddedFile'.$filter.' /Length '.strlen($stream).' >>';
  5151. $out .= ' stream'."\n".$stream."\n".'endstream';
  5152. $out .= "\n".'endobj';
  5153. $this->_out($out);
  5154. }
  5155. }
  5156. /**
  5157. * Prints a text cell at the specified position.
  5158. * This method allows to place a string precisely on the page.
  5159. * @param $x (float) Abscissa of the cell origin
  5160. * @param $y (float) Ordinate of the cell origin
  5161. * @param $txt (string) String to print
  5162. * @param $fstroke (int) outline size in user units (false = disable)
  5163. * @param $fclip (boolean) if true activate clipping mode (you must call StartTransform() before this function and StopTransform() to stop the clipping tranformation).
  5164. * @param $ffill (boolean) if true fills the text
  5165. * @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)))
  5166. * @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.
  5167. * @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>
  5168. * @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
  5169. * @param $link (mixed) URL or identifier returned by AddLink().
  5170. * @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.
  5171. * @param $ignore_min_height (boolean) if true ignore automatic minimum height value.
  5172. * @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>
  5173. * @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>
  5174. * @param $rtloff (boolean) if true uses the page top-left corner as origin of axis for $x and $y initial position.
  5175. * @public
  5176. * @since 1.0
  5177. * @see Cell(), Write(), MultiCell(), WriteHTML(), WriteHTMLCell()
  5178. */
  5179. 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) {
  5180. $textrendermode = $this->textrendermode;
  5181. $textstrokewidth = $this->textstrokewidth;
  5182. $this->setTextRenderingMode($fstroke, $ffill, $fclip);
  5183. $this->SetXY($x, $y, $rtloff);
  5184. $this->Cell(0, 0, $txt, $border, $ln, $align, $fill, $link, $stretch, $ignore_min_height, $calign, $valign);
  5185. // restore previous rendering mode
  5186. $this->textrendermode = $textrendermode;
  5187. $this->textstrokewidth = $textstrokewidth;
  5188. }
  5189. /**
  5190. * Whenever a page break condition is met, the method is called, and the break is issued or not depending on the returned value.
  5191. * The default implementation returns a value according to the mode selected by SetAutoPageBreak().<br />
  5192. * This method is called automatically and should not be called directly by the application.
  5193. * @return boolean
  5194. * @public
  5195. * @since 1.4
  5196. * @see SetAutoPageBreak()
  5197. */
  5198. public function AcceptPageBreak() {
  5199. if ($this->num_columns > 1) {
  5200. // multi column mode
  5201. if ($this->current_column < ($this->num_columns - 1)) {
  5202. // go to next column
  5203. $this->selectColumn($this->current_column + 1);
  5204. } else {
  5205. // add a new page
  5206. $this->AddPage();
  5207. // set first column
  5208. $this->selectColumn(0);
  5209. }
  5210. // avoid page breaking from checkPageBreak()
  5211. return false;
  5212. }
  5213. return $this->AutoPageBreak;
  5214. }
  5215. /**
  5216. * Add page if needed.
  5217. * @param $h (float) Cell height. Default value: 0.
  5218. * @param $y (mixed) starting y position, leave empty for current position.
  5219. * @param $addpage (boolean) if true add a page, otherwise only return the true/false state
  5220. * @return boolean true in case of page break, false otherwise.
  5221. * @since 3.2.000 (2008-07-01)
  5222. * @protected
  5223. */
  5224. protected function checkPageBreak($h=0, $y='', $addpage=true) {
  5225. if ($this->empty_string($y)) {
  5226. $y = $this->y;
  5227. }
  5228. $current_page = $this->page;
  5229. if ((($y + $h) > $this->PageBreakTrigger) AND ($this->inPageBody()) AND ($this->AcceptPageBreak())) {
  5230. if ($addpage) {
  5231. //Automatic page break
  5232. $x = $this->x;
  5233. $this->AddPage($this->CurOrientation);
  5234. $this->y = $this->tMargin;
  5235. $oldpage = $this->page - 1;
  5236. if ($this->rtl) {
  5237. if ($this->pagedim[$this->page]['orm'] != $this->pagedim[$oldpage]['orm']) {
  5238. $this->x = $x - ($this->pagedim[$this->page]['orm'] - $this->pagedim[$oldpage]['orm']);
  5239. } else {
  5240. $this->x = $x;
  5241. }
  5242. } else {
  5243. if ($this->pagedim[$this->page]['olm'] != $this->pagedim[$oldpage]['olm']) {
  5244. $this->x = $x + ($this->pagedim[$this->page]['olm'] - $this->pagedim[$oldpage]['olm']);
  5245. } else {
  5246. $this->x = $x;
  5247. }
  5248. }
  5249. }
  5250. return true;
  5251. }
  5252. if ($current_page != $this->page) {
  5253. // account for columns mode
  5254. return true;
  5255. }
  5256. return false;
  5257. }
  5258. /**
  5259. * Removes SHY characters from text.
  5260. * Unicode Data:<ul>
  5261. * <li>Name : SOFT HYPHEN, commonly abbreviated as SHY</li>
  5262. * <li>HTML Entity (decimal): "&amp;#173;"</li>
  5263. * <li>HTML Entity (hex): "&amp;#xad;"</li>
  5264. * <li>HTML Entity (named): "&amp;shy;"</li>
  5265. * <li>How to type in Microsoft Windows: [Alt +00AD] or [Alt 0173]</li>
  5266. * <li>UTF-8 (hex): 0xC2 0xAD (c2ad)</li>
  5267. * <li>UTF-8 character: chr(194).chr(173)</li>
  5268. * </ul>
  5269. * @param $txt (string) input string
  5270. * @return string without SHY characters.
  5271. * @public
  5272. * @since (4.5.019) 2009-02-28
  5273. */
  5274. public function removeSHY($txt='') {
  5275. $txt = preg_replace('/([\\xc2]{1}[\\xad]{1})/', '', $txt);
  5276. if (!$this->isunicode) {
  5277. $txt = preg_replace('/([\\xad]{1})/', '', $txt);
  5278. }
  5279. return $txt;
  5280. }
  5281. /**
  5282. * 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 />
  5283. * If automatic page breaking is enabled and the cell goes beyond the limit, a page break is done before outputting.
  5284. * @param $w (float) Cell width. If 0, the cell extends up to the right margin.
  5285. * @param $h (float) Cell height. Default value: 0.
  5286. * @param $txt (string) String to print. Default value: empty string.
  5287. * @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)))
  5288. * @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.
  5289. * @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>
  5290. * @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
  5291. * @param $link (mixed) URL or identifier returned by AddLink().
  5292. * @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.
  5293. * @param $ignore_min_height (boolean) if true ignore automatic minimum height value.
  5294. * @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>
  5295. * @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>
  5296. * @public
  5297. * @since 1.0
  5298. * @see SetFont(), SetDrawColor(), SetFillColor(), SetTextColor(), SetLineWidth(), AddLink(), Ln(), MultiCell(), Write(), SetAutoPageBreak()
  5299. */
  5300. 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') {
  5301. $prev_cell_margin = $this->cell_margin;
  5302. $prev_cell_padding = $this->cell_padding;
  5303. $this->adjustCellPadding($border);
  5304. if (!$ignore_min_height) {
  5305. $min_cell_height = ($this->FontSize * $this->cell_height_ratio) + $this->cell_padding['T'] + $this->cell_padding['B'];
  5306. if ($h < $min_cell_height) {
  5307. $h = $min_cell_height;
  5308. }
  5309. }
  5310. $this->checkPageBreak($h + $this->cell_margin['T'] + $this->cell_margin['B']);
  5311. $this->_out($this->getCellCode($w, $h, $txt, $border, $ln, $align, $fill, $link, $stretch, true, $calign, $valign));
  5312. $this->cell_padding = $prev_cell_padding;
  5313. $this->cell_margin = $prev_cell_margin;
  5314. }
  5315. /**
  5316. * 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 />
  5317. * If automatic page breaking is enabled and the cell goes beyond the limit, a page break is done before outputting.
  5318. * @param $w (float) Cell width. If 0, the cell extends up to the right margin.
  5319. * @param $h (float) Cell height. Default value: 0.
  5320. * @param $txt (string) String to print. Default value: empty string.
  5321. * @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)))
  5322. * @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.
  5323. * @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>
  5324. * @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
  5325. * @param $link (mixed) URL or identifier returned by AddLink().
  5326. * @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.
  5327. * @param $ignore_min_height (boolean) if true ignore automatic minimum height value.
  5328. * @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>
  5329. * @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>
  5330. * @return string containing cell code
  5331. * @protected
  5332. * @since 1.0
  5333. * @see Cell()
  5334. */
  5335. 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') {
  5336. // replace 'NO-BREAK SPACE' (U+00A0) character with a simple space
  5337. $txt = str_replace($this->unichr(160), ' ', $txt);
  5338. $prev_cell_margin = $this->cell_margin;
  5339. $prev_cell_padding = $this->cell_padding;
  5340. $txt = $this->removeSHY($txt);
  5341. $rs = ''; //string to be returned
  5342. $this->adjustCellPadding($border);
  5343. if (!$ignore_min_height) {
  5344. $min_cell_height = ($this->FontSize * $this->cell_height_ratio) + $this->cell_padding['T'] + $this->cell_padding['B'];
  5345. if ($h < $min_cell_height) {
  5346. $h = $min_cell_height;
  5347. }
  5348. }
  5349. $k = $this->k;
  5350. // check page for no-write regions and adapt page margins if necessary
  5351. list($this->x, $this->y) = $this->checkPageRegions($h, $this->x, $this->y);
  5352. if ($this->rtl) {
  5353. $x = $this->x - $this->cell_margin['R'];
  5354. } else {
  5355. $x = $this->x + $this->cell_margin['L'];
  5356. }
  5357. $y = $this->y + $this->cell_margin['T'];
  5358. $prev_font_stretching = $this->font_stretching;
  5359. $prev_font_spacing = $this->font_spacing;
  5360. // cell vertical alignment
  5361. switch ($calign) {
  5362. case 'A': {
  5363. // font top
  5364. switch ($valign) {
  5365. case 'T': {
  5366. // top
  5367. $y -= $this->cell_padding['T'];
  5368. break;
  5369. }
  5370. case 'B': {
  5371. // bottom
  5372. $y -= ($h - $this->cell_padding['B'] - $this->FontAscent - $this->FontDescent);
  5373. break;
  5374. }
  5375. default:
  5376. case 'C':
  5377. case 'M': {
  5378. // center
  5379. $y -= (($h - $this->FontAscent - $this->FontDescent) / 2);
  5380. break;
  5381. }
  5382. }
  5383. break;
  5384. }
  5385. case 'L': {
  5386. // font baseline
  5387. switch ($valign) {
  5388. case 'T': {
  5389. // top
  5390. $y -= ($this->cell_padding['T'] + $this->FontAscent);
  5391. break;
  5392. }
  5393. case 'B': {
  5394. // bottom
  5395. $y -= ($h - $this->cell_padding['B'] - $this->FontDescent);
  5396. break;
  5397. }
  5398. default:
  5399. case 'C':
  5400. case 'M': {
  5401. // center
  5402. $y -= (($h + $this->FontAscent - $this->FontDescent) / 2);
  5403. break;
  5404. }
  5405. }
  5406. break;
  5407. }
  5408. case 'D': {
  5409. // font bottom
  5410. switch ($valign) {
  5411. case 'T': {
  5412. // top
  5413. $y -= ($this->cell_padding['T'] + $this->FontAscent + $this->FontDescent);
  5414. break;
  5415. }
  5416. case 'B': {
  5417. // bottom
  5418. $y -= ($h - $this->cell_padding['B']);
  5419. break;
  5420. }
  5421. default:
  5422. case 'C':
  5423. case 'M': {
  5424. // center
  5425. $y -= (($h + $this->FontAscent + $this->FontDescent) / 2);
  5426. break;
  5427. }
  5428. }
  5429. break;
  5430. }
  5431. case 'B': {
  5432. // cell bottom
  5433. $y -= $h;
  5434. break;
  5435. }
  5436. case 'C':
  5437. case 'M': {
  5438. // cell center
  5439. $y -= ($h / 2);
  5440. break;
  5441. }
  5442. default:
  5443. case 'T': {
  5444. // cell top
  5445. break;
  5446. }
  5447. }
  5448. // text vertical alignment
  5449. switch ($valign) {
  5450. case 'T': {
  5451. // top
  5452. $yt = $y + $this->cell_padding['T'];
  5453. break;
  5454. }
  5455. case 'B': {
  5456. // bottom
  5457. $yt = $y + $h - $this->cell_padding['B'] - $this->FontAscent - $this->FontDescent;
  5458. break;
  5459. }
  5460. default:
  5461. case 'C':
  5462. case 'M': {
  5463. // center
  5464. $yt = $y + (($h - $this->FontAscent - $this->FontDescent) / 2);
  5465. break;
  5466. }
  5467. }
  5468. $basefonty = $yt + $this->FontAscent;
  5469. if ($this->empty_string($w) OR ($w <= 0)) {
  5470. if ($this->rtl) {
  5471. $w = $x - $this->lMargin;
  5472. } else {
  5473. $w = $this->w - $this->rMargin - $x;
  5474. }
  5475. }
  5476. $s = '';
  5477. // fill and borders
  5478. if (is_string($border) AND (strlen($border) == 4)) {
  5479. // full border
  5480. $border = 1;
  5481. }
  5482. if ($fill OR ($border == 1)) {
  5483. if ($fill) {
  5484. $op = ($border == 1) ? 'B' : 'f';
  5485. } else {
  5486. $op = 'S';
  5487. }
  5488. if ($this->rtl) {
  5489. $xk = (($x - $w) * $k);
  5490. } else {
  5491. $xk = ($x * $k);
  5492. }
  5493. $s .= sprintf('%.2F %.2F %.2F %.2F re %s ', $xk, (($this->h - $y) * $k), ($w * $k), (-$h * $k), $op);
  5494. }
  5495. // draw borders
  5496. $s .= $this->getCellBorder($x, $y, $w, $h, $border);
  5497. if ($txt != '') {
  5498. $txt2 = $txt;
  5499. if ($this->isunicode) {
  5500. if (($this->CurrentFont['type'] == 'core') OR ($this->CurrentFont['type'] == 'TrueType') OR ($this->CurrentFont['type'] == 'Type1')) {
  5501. $txt2 = $this->UTF8ToLatin1($txt2);
  5502. } else {
  5503. $unicode = $this->UTF8StringToArray($txt); // array of UTF-8 unicode values
  5504. $unicode = $this->utf8Bidi($unicode, '', $this->tmprtl);
  5505. if (defined('K_THAI_TOPCHARS') AND (K_THAI_TOPCHARS == true)) {
  5506. // ---- Fix for bug #2977340 "Incorrect Thai characters position arrangement" ----
  5507. // NOTE: this doesn't work with HTML justification
  5508. // Symbols that could overlap on the font top (only works in LTR)
  5509. $topchar = array(3611, 3613, 3615, 3650, 3651, 3652); // chars that extends on top
  5510. $topsym = array(3633, 3636, 3637, 3638, 3639, 3655, 3656, 3657, 3658, 3659, 3660, 3661, 3662); // symbols with top position
  5511. $numchars = count($unicode); // number of chars
  5512. $unik = 0;
  5513. $uniblock = array();
  5514. $uniblock[$unik] = array();
  5515. $uniblock[$unik][] = $unicode[0];
  5516. // resolve overlapping conflicts by splitting the string in several parts
  5517. for ($i = 1; $i < $numchars; ++$i) {
  5518. // check if symbols overlaps at top
  5519. if (in_array($unicode[$i], $topsym) AND (in_array($unicode[($i - 1)], $topsym) OR in_array($unicode[($i - 1)], $topchar))) {
  5520. // move symbols to another array
  5521. ++$unik;
  5522. $uniblock[$unik] = array();
  5523. $uniblock[$unik][] = $unicode[$i];
  5524. ++$unik;
  5525. $uniblock[$unik] = array();
  5526. $unicode[$i] = 0x200b; // Unicode Character 'ZERO WIDTH SPACE' (DEC:8203, U+200B)
  5527. } else {
  5528. $uniblock[$unik][] = $unicode[$i];
  5529. }
  5530. }
  5531. // ---- END OF Fix for bug #2977340
  5532. }
  5533. $txt2 = $this->arrUTF8ToUTF16BE($unicode, false);
  5534. }
  5535. }
  5536. $txt2 = $this->_escape($txt2);
  5537. // get current text width (considering general font stretching and spacing)
  5538. $txwidth = $this->GetStringWidth($txt);
  5539. $width = $txwidth;
  5540. // check for stretch mode
  5541. if ($stretch > 0) {
  5542. // calculate ratio between cell width and text width
  5543. if ($width <= 0) {
  5544. $ratio = 1;
  5545. } else {
  5546. $ratio = (($w - $this->cell_padding['L'] - $this->cell_padding['R']) / $width);
  5547. }
  5548. // check if stretching is required
  5549. if (($ratio < 1) OR (($ratio > 1) AND (($stretch % 2) == 0))) {
  5550. // the text will be stretched to fit cell width
  5551. if ($stretch > 2) {
  5552. // set new character spacing
  5553. $this->font_spacing += ($w - $this->cell_padding['L'] - $this->cell_padding['R'] - $width) / (max(($this->GetNumChars($txt) - 1), 1) * ($this->font_stretching / 100));
  5554. } else {
  5555. // set new horizontal stretching
  5556. $this->font_stretching *= $ratio;
  5557. }
  5558. // recalculate text width (the text fills the entire cell)
  5559. $width = $w - $this->cell_padding['L'] - $this->cell_padding['R'];
  5560. // reset alignment
  5561. $align = '';
  5562. }
  5563. }
  5564. if ($this->font_stretching != 100) {
  5565. // apply font stretching
  5566. $rs .= sprintf('BT %.2F Tz ET ', $this->font_stretching);
  5567. }
  5568. if ($this->font_spacing != 0) {
  5569. // increase/decrease font spacing
  5570. $rs .= sprintf('BT %.2F Tc ET ', ($this->font_spacing * $this->k));
  5571. }
  5572. if ($this->ColorFlag AND ($this->textrendermode < 4)) {
  5573. $s .= 'q '.$this->TextColor.' ';
  5574. }
  5575. // rendering mode
  5576. $s .= sprintf('BT %d Tr %.2F w ET ', $this->textrendermode, $this->textstrokewidth);
  5577. // count number of spaces
  5578. $ns = substr_count($txt, chr(32));
  5579. // Justification
  5580. $spacewidth = 0;
  5581. if (($align == 'J') AND ($ns > 0)) {
  5582. if ($this->isUnicodeFont()) {
  5583. // get string width without spaces
  5584. $width = $this->GetStringWidth(str_replace(' ', '', $txt));
  5585. // calculate average space width
  5586. $spacewidth = -1000 * ($w - $width - $this->cell_padding['L'] - $this->cell_padding['R']) / ($ns?$ns:1) / $this->FontSize;
  5587. if ($this->font_stretching != 100) {
  5588. // word spacing is affected by stretching
  5589. $spacewidth /= ($this->font_stretching / 100);
  5590. }
  5591. // set word position to be used with TJ operator
  5592. $txt2 = str_replace(chr(0).chr(32), ') '.sprintf('%.3F', $spacewidth).' (', $txt2);
  5593. $unicode_justification = true;
  5594. } else {
  5595. // get string width
  5596. $width = $txwidth;
  5597. // new space width
  5598. $spacewidth = (($w - $width - $this->cell_padding['L'] - $this->cell_padding['R']) / ($ns?$ns:1)) * $this->k;
  5599. if ($this->font_stretching != 100) {
  5600. // word spacing (Tw) is affected by stretching
  5601. $spacewidth /= ($this->font_stretching / 100);
  5602. }
  5603. // set word spacing
  5604. $rs .= sprintf('BT %.3F Tw ET ', $spacewidth);
  5605. }
  5606. $width = $w - $this->cell_padding['L'] - $this->cell_padding['R'];
  5607. }
  5608. // replace carriage return characters
  5609. $txt2 = str_replace("\r", ' ', $txt2);
  5610. switch ($align) {
  5611. case 'C': {
  5612. $dx = ($w - $width) / 2;
  5613. break;
  5614. }
  5615. case 'R': {
  5616. if ($this->rtl) {
  5617. $dx = $this->cell_padding['R'];
  5618. } else {
  5619. $dx = $w - $width - $this->cell_padding['R'];
  5620. }
  5621. break;
  5622. }
  5623. case 'L': {
  5624. if ($this->rtl) {
  5625. $dx = $w - $width - $this->cell_padding['L'];
  5626. } else {
  5627. $dx = $this->cell_padding['L'];
  5628. }
  5629. break;
  5630. }
  5631. case 'J':
  5632. default: {
  5633. if ($this->rtl) {
  5634. $dx = $this->cell_padding['R'];
  5635. } else {
  5636. $dx = $this->cell_padding['L'];
  5637. }
  5638. break;
  5639. }
  5640. }
  5641. if ($this->rtl) {
  5642. $xdx = $x - $dx - $width;
  5643. } else {
  5644. $xdx = $x + $dx;
  5645. }
  5646. $xdk = $xdx * $k;
  5647. // print text
  5648. $s .= sprintf('BT %.2F %.2F Td [(%s)] TJ ET', $xdk, (($this->h - $basefonty) * $k), $txt2);
  5649. if (isset($uniblock)) {
  5650. // print overlapping characters as separate string
  5651. $xshift = 0; // horizontal shift
  5652. $ty = (($this->h - $basefonty + (0.2 * $this->FontSize)) * $k);
  5653. $spw = (($w - $txwidth - $this->cell_padding['L'] - $this->cell_padding['R']) / ($ns?$ns:1));
  5654. foreach ($uniblock as $uk => $uniarr) {
  5655. if (($uk % 2) == 0) {
  5656. // x space to skip
  5657. if ($spacewidth != 0) {
  5658. // justification shift
  5659. $xshift += (count(array_keys($uniarr, 32)) * $spw);
  5660. }
  5661. $xshift += $this->GetArrStringWidth($uniarr); // + shift justification
  5662. } else {
  5663. // character to print
  5664. $topchr = $this->arrUTF8ToUTF16BE($uniarr, false);
  5665. $topchr = $this->_escape($topchr);
  5666. $s .= sprintf(' BT %.2F %.2F Td [(%s)] TJ ET', ($xdk + ($xshift * $k)), $ty, $topchr);
  5667. }
  5668. }
  5669. }
  5670. if ($this->underline) {
  5671. $s .= ' '.$this->_dounderlinew($xdx, $basefonty, $width);
  5672. }
  5673. if ($this->linethrough) {
  5674. $s .= ' '.$this->_dolinethroughw($xdx, $basefonty, $width);
  5675. }
  5676. if ($this->overline) {
  5677. $s .= ' '.$this->_dooverlinew($xdx, $basefonty, $width);
  5678. }
  5679. if ($this->ColorFlag AND ($this->textrendermode < 4)) {
  5680. $s .= ' Q';
  5681. }
  5682. if ($link) {
  5683. $this->Link($xdx, $yt, $width, ($this->FontAscent + $this->FontDescent), $link, $ns);
  5684. }
  5685. }
  5686. // output cell
  5687. if ($s) {
  5688. // output cell
  5689. $rs .= $s;
  5690. if ($this->font_spacing != 0) {
  5691. // reset font spacing mode
  5692. $rs .= ' BT 0 Tc ET';
  5693. }
  5694. if ($this->font_stretching != 100) {
  5695. // reset font stretching mode
  5696. $rs .= ' BT 100 Tz ET';
  5697. }
  5698. }
  5699. // reset word spacing
  5700. if (!$this->isUnicodeFont() AND ($align == 'J')) {
  5701. $rs .= ' BT 0 Tw ET';
  5702. }
  5703. // reset stretching and spacing
  5704. $this->font_stretching = $prev_font_stretching;
  5705. $this->font_spacing = $prev_font_spacing;
  5706. $this->lasth = $h;
  5707. if ($ln > 0) {
  5708. //Go to the beginning of the next line
  5709. $this->y = $y + $h + $this->cell_margin['B'];
  5710. if ($ln == 1) {
  5711. if ($this->rtl) {
  5712. $this->x = $this->w - $this->rMargin;
  5713. } else {
  5714. $this->x = $this->lMargin;
  5715. }
  5716. }
  5717. } else {
  5718. // go left or right by case
  5719. if ($this->rtl) {
  5720. $this->x = $x - $w - $this->cell_margin['L'];
  5721. } else {
  5722. $this->x = $x + $w + $this->cell_margin['R'];
  5723. }
  5724. }
  5725. $gstyles = ''.$this->linestyleWidth.' '.$this->linestyleCap.' '.$this->linestyleJoin.' '.$this->linestyleDash.' '.$this->DrawColor.' '.$this->FillColor."\n";
  5726. $rs = $gstyles.$rs;
  5727. $this->cell_padding = $prev_cell_padding;
  5728. $this->cell_margin = $prev_cell_margin;
  5729. return $rs;
  5730. }
  5731. /**
  5732. * Returns the code to draw the cell border
  5733. * @param $x (float) X coordinate.
  5734. * @param $y (float) Y coordinate.
  5735. * @param $w (float) Cell width.
  5736. * @param $h (float) Cell height.
  5737. * @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)))
  5738. * @return string containing cell border code
  5739. * @protected
  5740. * @see SetLineStyle()
  5741. * @since 5.7.000 (2010-08-02)
  5742. */
  5743. protected function getCellBorder($x, $y, $w, $h, $brd) {
  5744. $s = ''; // string to be returned
  5745. if (empty($brd)) {
  5746. return $s;
  5747. }
  5748. if ($brd == 1) {
  5749. $brd = array('LRTB' => true);
  5750. }
  5751. // calculate coordinates for border
  5752. $k = $this->k;
  5753. if ($this->rtl) {
  5754. $xeL = ($x - $w) * $k;
  5755. $xeR = $x * $k;
  5756. } else {
  5757. $xeL = $x * $k;
  5758. $xeR = ($x + $w) * $k;
  5759. }
  5760. $yeL = (($this->h - ($y + $h)) * $k);
  5761. $yeT = (($this->h - $y) * $k);
  5762. $xeT = $xeL;
  5763. $xeB = $xeR;
  5764. $yeR = $yeT;
  5765. $yeB = $yeL;
  5766. if (is_string($brd)) {
  5767. // convert string to array
  5768. $slen = strlen($brd);
  5769. $newbrd = array();
  5770. for ($i = 0; $i < $slen; ++$i) {
  5771. $newbrd[$brd[$i]] = array('cap' => 'square', 'join' => 'miter');
  5772. }
  5773. $brd = $newbrd;
  5774. }
  5775. if (isset($brd['mode'])) {
  5776. $mode = $brd['mode'];
  5777. unset($brd['mode']);
  5778. } else {
  5779. $mode = 'normal';
  5780. }
  5781. foreach ($brd as $border => $style) {
  5782. if (is_array($style) AND !empty($style)) {
  5783. // apply border style
  5784. $prev_style = $this->linestyleWidth.' '.$this->linestyleCap.' '.$this->linestyleJoin.' '.$this->linestyleDash.' '.$this->DrawColor.' ';
  5785. $s .= $this->SetLineStyle($style, true)."\n";
  5786. }
  5787. switch ($mode) {
  5788. case 'ext': {
  5789. $off = (($this->LineWidth / 2) * $k);
  5790. $xL = $xeL - $off;
  5791. $xR = $xeR + $off;
  5792. $yT = $yeT + $off;
  5793. $yL = $yeL - $off;
  5794. $xT = $xL;
  5795. $xB = $xR;
  5796. $yR = $yT;
  5797. $yB = $yL;
  5798. $w += $this->LineWidth;
  5799. $h += $this->LineWidth;
  5800. break;
  5801. }
  5802. case 'int': {
  5803. $off = ($this->LineWidth / 2) * $k;
  5804. $xL = $xeL + $off;
  5805. $xR = $xeR - $off;
  5806. $yT = $yeT - $off;
  5807. $yL = $yeL + $off;
  5808. $xT = $xL;
  5809. $xB = $xR;
  5810. $yR = $yT;
  5811. $yB = $yL;
  5812. $w -= $this->LineWidth;
  5813. $h -= $this->LineWidth;
  5814. break;
  5815. }
  5816. case 'normal':
  5817. default: {
  5818. $xL = $xeL;
  5819. $xT = $xeT;
  5820. $xB = $xeB;
  5821. $xR = $xeR;
  5822. $yL = $yeL;
  5823. $yT = $yeT;
  5824. $yB = $yeB;
  5825. $yR = $yeR;
  5826. break;
  5827. }
  5828. }
  5829. // draw borders by case
  5830. if (strlen($border) == 4) {
  5831. $s .= sprintf('%.2F %.2F %.2F %.2F re S ', $xT, $yT, ($w * $k), (-$h * $k));
  5832. } elseif (strlen($border) == 3) {
  5833. if (strpos($border,'B') === false) { // LTR
  5834. $s .= sprintf('%.2F %.2F m ', $xL, $yL);
  5835. $s .= sprintf('%.2F %.2F l ', $xT, $yT);
  5836. $s .= sprintf('%.2F %.2F l ', $xR, $yR);
  5837. $s .= sprintf('%.2F %.2F l ', $xB, $yB);
  5838. $s .= 'S ';
  5839. } elseif (strpos($border,'L') === false) { // TRB
  5840. $s .= sprintf('%.2F %.2F m ', $xT, $yT);
  5841. $s .= sprintf('%.2F %.2F l ', $xR, $yR);
  5842. $s .= sprintf('%.2F %.2F l ', $xB, $yB);
  5843. $s .= sprintf('%.2F %.2F l ', $xL, $yL);
  5844. $s .= 'S ';
  5845. } elseif (strpos($border,'T') === false) { // RBL
  5846. $s .= sprintf('%.2F %.2F m ', $xR, $yR);
  5847. $s .= sprintf('%.2F %.2F l ', $xB, $yB);
  5848. $s .= sprintf('%.2F %.2F l ', $xL, $yL);
  5849. $s .= sprintf('%.2F %.2F l ', $xT, $yT);
  5850. $s .= 'S ';
  5851. } elseif (strpos($border,'R') === false) { // BLT
  5852. $s .= sprintf('%.2F %.2F m ', $xB, $yB);
  5853. $s .= sprintf('%.2F %.2F l ', $xL, $yL);
  5854. $s .= sprintf('%.2F %.2F l ', $xT, $yT);
  5855. $s .= sprintf('%.2F %.2F l ', $xR, $yR);
  5856. $s .= 'S ';
  5857. }
  5858. } elseif (strlen($border) == 2) {
  5859. if ((strpos($border,'L') !== false) AND (strpos($border,'T') !== false)) { // LT
  5860. $s .= sprintf('%.2F %.2F m ', $xL, $yL);
  5861. $s .= sprintf('%.2F %.2F l ', $xT, $yT);
  5862. $s .= sprintf('%.2F %.2F l ', $xR, $yR);
  5863. $s .= 'S ';
  5864. } elseif ((strpos($border,'T') !== false) AND (strpos($border,'R') !== false)) { // TR
  5865. $s .= sprintf('%.2F %.2F m ', $xT, $yT);
  5866. $s .= sprintf('%.2F %.2F l ', $xR, $yR);
  5867. $s .= sprintf('%.2F %.2F l ', $xB, $yB);
  5868. $s .= 'S ';
  5869. } elseif ((strpos($border,'R') !== false) AND (strpos($border,'B') !== false)) { // RB
  5870. $s .= sprintf('%.2F %.2F m ', $xR, $yR);
  5871. $s .= sprintf('%.2F %.2F l ', $xB, $yB);
  5872. $s .= sprintf('%.2F %.2F l ', $xL, $yL);
  5873. $s .= 'S ';
  5874. } elseif ((strpos($border,'B') !== false) AND (strpos($border,'L') !== false)) { // BL
  5875. $s .= sprintf('%.2F %.2F m ', $xB, $yB);
  5876. $s .= sprintf('%.2F %.2F l ', $xL, $yL);
  5877. $s .= sprintf('%.2F %.2F l ', $xT, $yT);
  5878. $s .= 'S ';
  5879. } elseif ((strpos($border,'L') !== false) AND (strpos($border,'R') !== false)) { // LR
  5880. $s .= sprintf('%.2F %.2F m ', $xL, $yL);
  5881. $s .= sprintf('%.2F %.2F l ', $xT, $yT);
  5882. $s .= 'S ';
  5883. $s .= sprintf('%.2F %.2F m ', $xR, $yR);
  5884. $s .= sprintf('%.2F %.2F l ', $xB, $yB);
  5885. $s .= 'S ';
  5886. } elseif ((strpos($border,'T') !== false) AND (strpos($border,'B') !== false)) { // TB
  5887. $s .= sprintf('%.2F %.2F m ', $xT, $yT);
  5888. $s .= sprintf('%.2F %.2F l ', $xR, $yR);
  5889. $s .= 'S ';
  5890. $s .= sprintf('%.2F %.2F m ', $xB, $yB);
  5891. $s .= sprintf('%.2F %.2F l ', $xL, $yL);
  5892. $s .= 'S ';
  5893. }
  5894. } else { // strlen($border) == 1
  5895. if (strpos($border,'L') !== false) { // L
  5896. $s .= sprintf('%.2F %.2F m ', $xL, $yL);
  5897. $s .= sprintf('%.2F %.2F l ', $xT, $yT);
  5898. $s .= 'S ';
  5899. } elseif (strpos($border,'T') !== false) { // T
  5900. $s .= sprintf('%.2F %.2F m ', $xT, $yT);
  5901. $s .= sprintf('%.2F %.2F l ', $xR, $yR);
  5902. $s .= 'S ';
  5903. } elseif (strpos($border,'R') !== false) { // R
  5904. $s .= sprintf('%.2F %.2F m ', $xR, $yR);
  5905. $s .= sprintf('%.2F %.2F l ', $xB, $yB);
  5906. $s .= 'S ';
  5907. } elseif (strpos($border,'B') !== false) { // B
  5908. $s .= sprintf('%.2F %.2F m ', $xB, $yB);
  5909. $s .= sprintf('%.2F %.2F l ', $xL, $yL);
  5910. $s .= 'S ';
  5911. }
  5912. }
  5913. if (is_array($style) AND !empty($style)) {
  5914. // reset border style to previous value
  5915. $s .= "\n".$this->linestyleWidth.' '.$this->linestyleCap.' '.$this->linestyleJoin.' '.$this->linestyleDash.' '.$this->DrawColor."\n";
  5916. }
  5917. }
  5918. return $s;
  5919. }
  5920. /**
  5921. * This method allows printing text with line breaks.
  5922. * 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 />
  5923. * Text can be aligned, centered or justified. The cell block can be framed and the background painted.
  5924. * @param $w (float) Width of cells. If 0, they extend up to the right margin of the page.
  5925. * @param $h (float) Cell minimum height. The cell extends automatically if needed.
  5926. * @param $txt (string) String to print
  5927. * @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)))
  5928. * @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>
  5929. * @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
  5930. * @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>
  5931. * @param $x (float) x position in user units
  5932. * @param $y (float) y position in user units
  5933. * @param $reseth (boolean) if true reset the last cell height (default true).
  5934. * @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.
  5935. * @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.
  5936. * @param $autopadding (boolean) if true, uses internal padding and automatically adjust it to account for line width.
  5937. * @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.
  5938. * @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.
  5939. * @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).
  5940. * @return int Return the number of cells or 1 for html mode.
  5941. * @public
  5942. * @since 1.3
  5943. * @see SetFont(), SetDrawColor(), SetFillColor(), SetTextColor(), SetLineWidth(), Cell(), Write(), SetAutoPageBreak()
  5944. */
  5945. 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) {
  5946. $prev_cell_margin = $this->cell_margin;
  5947. $prev_cell_padding = $this->cell_padding;
  5948. // adjust internal padding
  5949. $this->adjustCellPadding($border);
  5950. $mc_padding = $this->cell_padding;
  5951. $mc_margin = $this->cell_margin;
  5952. $this->cell_padding['T'] = 0;
  5953. $this->cell_padding['B'] = 0;
  5954. $this->setCellMargins(0, 0, 0, 0);
  5955. if ($this->empty_string($this->lasth) OR $reseth) {
  5956. // reset row height
  5957. $this->resetLastH();
  5958. }
  5959. if (!$this->empty_string($y)) {
  5960. $this->SetY($y);
  5961. } else {
  5962. $y = $this->GetY();
  5963. }
  5964. $resth = 0;
  5965. if (($h > 0) AND $this->inPageBody() AND (($y + $h + $mc_margin['T'] + $mc_margin['B']) > $this->PageBreakTrigger)) {
  5966. // spit cell in more pages/columns
  5967. $newh = ($this->PageBreakTrigger - $y);
  5968. $resth = ($h - $newh); // cell to be printed on the next page/column
  5969. $h = $newh;
  5970. }
  5971. // get current page number
  5972. $startpage = $this->page;
  5973. // get current column
  5974. $startcolumn = $this->current_column;
  5975. if (!$this->empty_string($x)) {
  5976. $this->SetX($x);
  5977. } else {
  5978. $x = $this->GetX();
  5979. }
  5980. // check page for no-write regions and adapt page margins if necessary
  5981. list($x, $y) = $this->checkPageRegions(0, $x, $y);
  5982. // apply margins
  5983. $oy = $y + $mc_margin['T'];
  5984. if ($this->rtl) {
  5985. $ox = $this->w - $x - $mc_margin['R'];
  5986. } else {
  5987. $ox = $x + $mc_margin['L'];
  5988. }
  5989. $this->x = $ox;
  5990. $this->y = $oy;
  5991. // set width
  5992. if ($this->empty_string($w) OR ($w <= 0)) {
  5993. if ($this->rtl) {
  5994. $w = $this->x - $this->lMargin - $mc_margin['L'];
  5995. } else {
  5996. $w = $this->w - $this->x - $this->rMargin - $mc_margin['R'];
  5997. }
  5998. }
  5999. // store original margin values
  6000. $lMargin = $this->lMargin;
  6001. $rMargin = $this->rMargin;
  6002. if ($this->rtl) {
  6003. $this->rMargin = $this->w - $this->x;
  6004. $this->lMargin = $this->x - $w;
  6005. } else {
  6006. $this->lMargin = $this->x;
  6007. $this->rMargin = $this->w - $this->x - $w;
  6008. }
  6009. if ($autopadding) {
  6010. // add top padding
  6011. $this->y += $mc_padding['T'];
  6012. }
  6013. if ($ishtml) { // ******* Write HTML text
  6014. $this->writeHTML($txt, true, false, $reseth, true, $align);
  6015. $nl = 1;
  6016. } else { // ******* Write simple text
  6017. $prev_FontSizePt = $this->FontSizePt;
  6018. // vertical alignment
  6019. if ($maxh > 0) {
  6020. // get text height
  6021. $text_height = $this->getStringHeight($w, $txt, $reseth, $autopadding, $mc_padding, $border);
  6022. if ($fitcell) {
  6023. // try to reduce font size to fit text on cell (use a quick search algorithm)
  6024. $fmin = 1;
  6025. $fmax = $this->FontSizePt;
  6026. $prev_text_height = $text_height;
  6027. $maxit = 100; // max number of iterations
  6028. while ($maxit > 0) {
  6029. $fmid = (($fmax + $fmin) / 2);
  6030. $this->SetFontSize($fmid, false);
  6031. $this->resetLastH();
  6032. $text_height = $this->getStringHeight($w, $txt, $reseth, $autopadding, $mc_padding, $border);
  6033. if (($text_height == $maxh) OR (($text_height < $maxh) AND ($fmin >= ($fmax - 0.01)))) {
  6034. break;
  6035. } elseif ($text_height < $maxh) {
  6036. $fmin = $fmid;
  6037. } else {
  6038. $fmax = $fmid;
  6039. }
  6040. --$maxit;
  6041. }
  6042. $this->SetFontSize($this->FontSizePt);
  6043. }
  6044. if ($text_height < $maxh) {
  6045. if ($valign == 'M') {
  6046. // text vertically centered
  6047. $this->y += (($maxh - $text_height) / 2);
  6048. } elseif ($valign == 'B') {
  6049. // text vertically aligned on bottom
  6050. $this->y += ($maxh - $text_height);
  6051. }
  6052. }
  6053. }
  6054. $nl = $this->Write($this->lasth, $txt, '', 0, $align, true, $stretch, false, true, $maxh, 0, $mc_margin);
  6055. if ($fitcell) {
  6056. // restore font size
  6057. $this->SetFontSize($prev_FontSizePt);
  6058. }
  6059. }
  6060. if ($autopadding) {
  6061. // add bottom padding
  6062. $this->y += $mc_padding['B'];
  6063. }
  6064. // Get end-of-text Y position
  6065. $currentY = $this->y;
  6066. // get latest page number
  6067. $endpage = $this->page;
  6068. if ($resth > 0) {
  6069. $skip = ($endpage - $startpage);
  6070. $tmpresth = $resth;
  6071. while ($tmpresth > 0) {
  6072. if ($skip <= 0) {
  6073. // add a page (or trig AcceptPageBreak() for multicolumn mode)
  6074. $this->checkPageBreak($this->PageBreakTrigger + 1);
  6075. }
  6076. if ($this->num_columns > 1) {
  6077. $tmpresth -= ($this->h - $this->y - $this->bMargin);
  6078. } else {
  6079. $tmpresth -= ($this->h - $this->tMargin - $this->bMargin);
  6080. }
  6081. --$skip;
  6082. }
  6083. $currentY = $this->y;
  6084. $endpage = $this->page;
  6085. }
  6086. // get latest column
  6087. $endcolumn = $this->current_column;
  6088. if ($this->num_columns == 0) {
  6089. $this->num_columns = 1;
  6090. }
  6091. // get border modes
  6092. $border_start = $this->getBorderMode($border, $position='start');
  6093. $border_end = $this->getBorderMode($border, $position='end');
  6094. $border_middle = $this->getBorderMode($border, $position='middle');
  6095. // design borders around HTML cells.
  6096. for ($page = $startpage; $page <= $endpage; ++$page) { // for each page
  6097. $ccode = '';
  6098. $this->setPage($page);
  6099. if ($this->num_columns < 2) {
  6100. // single-column mode
  6101. $this->SetX($x);
  6102. $this->y = $this->tMargin;
  6103. }
  6104. // account for margin changes
  6105. if ($page > $startpage) {
  6106. if (($this->rtl) AND ($this->pagedim[$page]['orm'] != $this->pagedim[$startpage]['orm'])) {
  6107. $this->x -= ($this->pagedim[$page]['orm'] - $this->pagedim[$startpage]['orm']);
  6108. } elseif ((!$this->rtl) AND ($this->pagedim[$page]['olm'] != $this->pagedim[$startpage]['olm'])) {
  6109. $this->x += ($this->pagedim[$page]['olm'] - $this->pagedim[$startpage]['olm']);
  6110. }
  6111. }
  6112. if ($startpage == $endpage) {
  6113. // single page
  6114. for ($column = $startcolumn; $column <= $endcolumn; ++$column) { // for each column
  6115. $this->selectColumn($column);
  6116. if ($this->rtl) {
  6117. $this->x -= $mc_margin['R'];
  6118. } else {
  6119. $this->x += $mc_margin['L'];
  6120. }
  6121. if ($startcolumn == $endcolumn) { // single column
  6122. $cborder = $border;
  6123. $h = max($h, ($currentY - $oy));
  6124. $this->y = $oy;
  6125. } elseif ($column == $startcolumn) { // first column
  6126. $cborder = $border_start;
  6127. $this->y = $oy;
  6128. $h = $this->h - $this->y - $this->bMargin;
  6129. } elseif ($column == $endcolumn) { // end column
  6130. $cborder = $border_end;
  6131. $h = $currentY - $this->y;
  6132. if ($resth > $h) {
  6133. $h = $resth;
  6134. }
  6135. } else { // middle column
  6136. $cborder = $border_middle;
  6137. $h = $this->h - $this->y - $this->bMargin;
  6138. $resth -= $h;
  6139. }
  6140. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
  6141. } // end for each column
  6142. } elseif ($page == $startpage) { // first page
  6143. for ($column = $startcolumn; $column < $this->num_columns; ++$column) { // for each column
  6144. $this->selectColumn($column);
  6145. if ($this->rtl) {
  6146. $this->x -= $mc_margin['R'];
  6147. } else {
  6148. $this->x += $mc_margin['L'];
  6149. }
  6150. if ($column == $startcolumn) { // first column
  6151. $cborder = $border_start;
  6152. $this->y = $oy;
  6153. $h = $this->h - $this->y - $this->bMargin;
  6154. } else { // middle column
  6155. $cborder = $border_middle;
  6156. $h = $this->h - $this->y - $this->bMargin;
  6157. $resth -= $h;
  6158. }
  6159. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
  6160. } // end for each column
  6161. } elseif ($page == $endpage) { // last page
  6162. for ($column = 0; $column <= $endcolumn; ++$column) { // for each column
  6163. $this->selectColumn($column);
  6164. if ($this->rtl) {
  6165. $this->x -= $mc_margin['R'];
  6166. } else {
  6167. $this->x += $mc_margin['L'];
  6168. }
  6169. if ($column == $endcolumn) {
  6170. // end column
  6171. $cborder = $border_end;
  6172. $h = $currentY - $this->y;
  6173. if ($resth > $h) {
  6174. $h = $resth;
  6175. }
  6176. } else {
  6177. // middle column
  6178. $cborder = $border_middle;
  6179. $h = $this->h - $this->y - $this->bMargin;
  6180. $resth -= $h;
  6181. }
  6182. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
  6183. } // end for each column
  6184. } else { // middle page
  6185. for ($column = 0; $column < $this->num_columns; ++$column) { // for each column
  6186. $this->selectColumn($column);
  6187. if ($this->rtl) {
  6188. $this->x -= $mc_margin['R'];
  6189. } else {
  6190. $this->x += $mc_margin['L'];
  6191. }
  6192. $cborder = $border_middle;
  6193. $h = $this->h - $this->y - $this->bMargin;
  6194. $resth -= $h;
  6195. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
  6196. } // end for each column
  6197. }
  6198. if ($cborder OR $fill) {
  6199. $offsetlen = strlen($ccode);
  6200. // draw border and fill
  6201. if ($this->inxobj) {
  6202. // we are inside an XObject template
  6203. if (end($this->xobjects[$this->xobjid]['transfmrk']) !== false) {
  6204. $pagemarkkey = key($this->xobjects[$this->xobjid]['transfmrk']);
  6205. $pagemark = $this->xobjects[$this->xobjid]['transfmrk'][$pagemarkkey];
  6206. $this->xobjects[$this->xobjid]['transfmrk'][$pagemarkkey] += $offsetlen;
  6207. } else {
  6208. $pagemark = $this->xobjects[$this->xobjid]['intmrk'];
  6209. $this->xobjects[$this->xobjid]['intmrk'] += $offsetlen;
  6210. }
  6211. $pagebuff = $this->xobjects[$this->xobjid]['outdata'];
  6212. $pstart = substr($pagebuff, 0, $pagemark);
  6213. $pend = substr($pagebuff, $pagemark);
  6214. $this->xobjects[$this->xobjid]['outdata'] = $pstart.$ccode.$pend;
  6215. } else {
  6216. if (end($this->transfmrk[$this->page]) !== false) {
  6217. $pagemarkkey = key($this->transfmrk[$this->page]);
  6218. $pagemark = $this->transfmrk[$this->page][$pagemarkkey];
  6219. $this->transfmrk[$this->page][$pagemarkkey] += $offsetlen;
  6220. } elseif ($this->InFooter) {
  6221. $pagemark = $this->footerpos[$this->page];
  6222. $this->footerpos[$this->page] += $offsetlen;
  6223. } else {
  6224. $pagemark = $this->intmrk[$this->page];
  6225. $this->intmrk[$this->page] += $offsetlen;
  6226. }
  6227. $pagebuff = $this->getPageBuffer($this->page);
  6228. $pstart = substr($pagebuff, 0, $pagemark);
  6229. $pend = substr($pagebuff, $pagemark);
  6230. $this->setPageBuffer($this->page, $pstart.$ccode.$pend);
  6231. }
  6232. }
  6233. } // end for each page
  6234. // Get end-of-cell Y position
  6235. $currentY = $this->GetY();
  6236. // restore previous values
  6237. if ($this->num_columns > 1) {
  6238. $this->selectColumn();
  6239. } else {
  6240. // restore original margins
  6241. $this->lMargin = $lMargin;
  6242. $this->rMargin = $rMargin;
  6243. if ($this->page > $startpage) {
  6244. // check for margin variations between pages (i.e. booklet mode)
  6245. $dl = ($this->pagedim[$this->page]['olm'] - $this->pagedim[$startpage]['olm']);
  6246. $dr = ($this->pagedim[$this->page]['orm'] - $this->pagedim[$startpage]['orm']);
  6247. if (($dl != 0) OR ($dr != 0)) {
  6248. $this->lMargin += $dl;
  6249. $this->rMargin += $dr;
  6250. }
  6251. }
  6252. }
  6253. if ($ln > 0) {
  6254. //Go to the beginning of the next line
  6255. $this->SetY($currentY + $mc_margin['B']);
  6256. if ($ln == 2) {
  6257. $this->SetX($x + $w + $mc_margin['L'] + $mc_margin['R']);
  6258. }
  6259. } else {
  6260. // go left or right by case
  6261. $this->setPage($startpage);
  6262. $this->y = $y;
  6263. $this->SetX($x + $w + $mc_margin['L'] + $mc_margin['R']);
  6264. }
  6265. $this->setContentMark();
  6266. $this->cell_padding = $prev_cell_padding;
  6267. $this->cell_margin = $prev_cell_margin;
  6268. return $nl;
  6269. }
  6270. /**
  6271. * Get the border mode accounting for multicell position (opens bottom side of multicell crossing pages)
  6272. * @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)))
  6273. * @param $position (string) multicell position: 'start', 'middle', 'end'
  6274. * @return border mode array
  6275. * @protected
  6276. * @since 4.4.002 (2008-12-09)
  6277. */
  6278. protected function getBorderMode($brd, $position='start') {
  6279. if ((!$this->opencell) OR empty($brd)) {
  6280. return $brd;
  6281. }
  6282. if ($brd == 1) {
  6283. $brd = 'LTRB';
  6284. }
  6285. if (is_string($brd)) {
  6286. // convert string to array
  6287. $slen = strlen($brd);
  6288. $newbrd = array();
  6289. for ($i = 0; $i < $slen; ++$i) {
  6290. $newbrd[$brd[$i]] = array('cap' => 'square', 'join' => 'miter');
  6291. }
  6292. $brd = $newbrd;
  6293. }
  6294. foreach ($brd as $border => $style) {
  6295. switch ($position) {
  6296. case 'start': {
  6297. if (strpos($border, 'B') !== false) {
  6298. // remove bottom line
  6299. $newkey = str_replace('B', '', $border);
  6300. if (strlen($newkey) > 0) {
  6301. $brd[$newkey] = $style;
  6302. }
  6303. unset($brd[$border]);
  6304. }
  6305. break;
  6306. }
  6307. case 'middle': {
  6308. if (strpos($border, 'B') !== false) {
  6309. // remove bottom line
  6310. $newkey = str_replace('B', '', $border);
  6311. if (strlen($newkey) > 0) {
  6312. $brd[$newkey] = $style;
  6313. }
  6314. unset($brd[$border]);
  6315. $border = $newkey;
  6316. }
  6317. if (strpos($border, 'T') !== false) {
  6318. // remove bottom line
  6319. $newkey = str_replace('T', '', $border);
  6320. if (strlen($newkey) > 0) {
  6321. $brd[$newkey] = $style;
  6322. }
  6323. unset($brd[$border]);
  6324. }
  6325. break;
  6326. }
  6327. case 'end': {
  6328. if (strpos($border, 'T') !== false) {
  6329. // remove bottom line
  6330. $newkey = str_replace('T', '', $border);
  6331. if (strlen($newkey) > 0) {
  6332. $brd[$newkey] = $style;
  6333. }
  6334. unset($brd[$border]);
  6335. }
  6336. break;
  6337. }
  6338. }
  6339. }
  6340. return $brd;
  6341. }
  6342. /**
  6343. * This method return the estimated number of lines for print a simple text string using Multicell() method.
  6344. * @param $txt (string) String for calculating his height
  6345. * @param $w (float) Width of cells. If 0, they extend up to the right margin of the page.
  6346. * @param $reseth (boolean) if true reset the last cell height (default false).
  6347. * @param $autopadding (boolean) if true, uses internal padding and automatically adjust it to account for line width (default true).
  6348. * @param $cellpadding (float) Internal cell padding, if empty uses default cell padding.
  6349. * @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)))
  6350. * @return float Return the minimal height needed for multicell method for printing the $txt param.
  6351. * @author Alexander Escalona Fernández, Nicola Asuni
  6352. * @public
  6353. * @since 4.5.011
  6354. */
  6355. public function getNumLines($txt, $w=0, $reseth=false, $autopadding=true, $cellpadding='', $border=0) {
  6356. if ($txt === '') {
  6357. // empty string
  6358. return 1;
  6359. }
  6360. // adjust internal padding
  6361. $prev_cell_padding = $this->cell_padding;
  6362. $prev_lasth = $this->lasth;
  6363. if (is_array($cellpadding)) {
  6364. $this->cell_padding = $cellpadding;
  6365. }
  6366. $this->adjustCellPadding($border);
  6367. if ($this->empty_string($w) OR ($w <= 0)) {
  6368. if ($this->rtl) {
  6369. $w = $this->x - $this->lMargin;
  6370. } else {
  6371. $w = $this->w - $this->rMargin - $this->x;
  6372. }
  6373. }
  6374. $wmax = $w - $this->cell_padding['L'] - $this->cell_padding['R'];
  6375. if ($reseth) {
  6376. // reset row height
  6377. $this->resetLastH();
  6378. }
  6379. $lines = 1;
  6380. $sum = 0;
  6381. $chars = $this->utf8Bidi($this->UTF8StringToArray($txt), $txt, $this->tmprtl);
  6382. $charsWidth = $this->GetArrStringWidth($chars, '', '', 0, true);
  6383. $length = count($chars);
  6384. $lastSeparator = -1;
  6385. for ($i = 0; $i < $length; ++$i) {
  6386. $charWidth = $charsWidth[$i];
  6387. if (preg_match($this->re_spaces, $this->unichr($chars[$i]))) {
  6388. $lastSeparator = $i;
  6389. }
  6390. if ((($sum + $charWidth) > $wmax) OR ($chars[$i] == 10)) {
  6391. ++$lines;
  6392. if ($chars[$i] == 10) {
  6393. $lastSeparator = -1;
  6394. $sum = 0;
  6395. } elseif ($lastSeparator != -1) {
  6396. $i = $lastSeparator;
  6397. $lastSeparator = -1;
  6398. $sum = 0;
  6399. } else {
  6400. $sum = $charWidth;
  6401. }
  6402. } else {
  6403. $sum += $charWidth;
  6404. }
  6405. }
  6406. if ($chars[($length - 1)] == 10) {
  6407. --$lines;
  6408. }
  6409. $this->cell_padding = $prev_cell_padding;
  6410. $this->lasth = $prev_lasth;
  6411. return $lines;
  6412. }
  6413. /**
  6414. * This method return the estimated height needed for printing a simple text string using the Multicell() method.
  6415. * Generally, if you want to know the exact height for a block of content you can use the following alternative technique:
  6416. * @pre
  6417. * // store current object
  6418. * $pdf->startTransaction();
  6419. * // store starting values
  6420. * $start_y = $pdf->GetY();
  6421. * $start_page = $pdf->getPage();
  6422. * // call your printing functions with your parameters
  6423. * // - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  6424. * $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);
  6425. * // - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  6426. * // get the new Y
  6427. * $end_y = $pdf->GetY();
  6428. * $end_page = $pdf->getPage();
  6429. * // calculate height
  6430. * $height = 0;
  6431. * if ($end_page == $start_page) {
  6432. * $height = $end_y - $start_y;
  6433. * } else {
  6434. * for ($page=$start_page; $page <= $end_page; ++$page) {
  6435. * $this->setPage($page);
  6436. * if ($page == $start_page) {
  6437. * // first page
  6438. * $height = $this->h - $start_y - $this->bMargin;
  6439. * } elseif ($page == $end_page) {
  6440. * // last page
  6441. * $height = $end_y - $this->tMargin;
  6442. * } else {
  6443. * $height = $this->h - $this->tMargin - $this->bMargin;
  6444. * }
  6445. * }
  6446. * }
  6447. * // restore previous object
  6448. * $pdf = $pdf->rollbackTransaction();
  6449. *
  6450. * @param $w (float) Width of cells. If 0, they extend up to the right margin of the page.
  6451. * @param $txt (string) String for calculating his height
  6452. * @param $reseth (boolean) if true reset the last cell height (default false).
  6453. * @param $autopadding (boolean) if true, uses internal padding and automatically adjust it to account for line width (default true).
  6454. * @param $cellpadding (float) Internal cell padding, if empty uses default cell padding.
  6455. * @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)))
  6456. * @return float Return the minimal height needed for multicell method for printing the $txt param.
  6457. * @author Nicola Asuni, Alexander Escalona Fernández
  6458. * @public
  6459. */
  6460. public function getStringHeight($w, $txt, $reseth=false, $autopadding=true, $cellpadding='', $border=0) {
  6461. // adjust internal padding
  6462. $prev_cell_padding = $this->cell_padding;
  6463. $prev_lasth = $this->lasth;
  6464. if (is_array($cellpadding)) {
  6465. $this->cell_padding = $cellpadding;
  6466. }
  6467. $this->adjustCellPadding($border);
  6468. $lines = $this->getNumLines($txt, $w, $reseth, $autopadding, $cellpadding, $border);
  6469. $height = $lines * ($this->FontSize * $this->cell_height_ratio);
  6470. if ($autopadding) {
  6471. // add top and bottom padding
  6472. $height += ($this->cell_padding['T'] + $this->cell_padding['B']);
  6473. }
  6474. $this->cell_padding = $prev_cell_padding;
  6475. $this->lasth = $prev_lasth;
  6476. return $height;
  6477. }
  6478. /**
  6479. * This method prints text from the current position.<br />
  6480. * @param $h (float) Line height
  6481. * @param $txt (string) String to print
  6482. * @param $link (mixed) URL or identifier returned by AddLink()
  6483. * @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
  6484. * @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>
  6485. * @param $ln (boolean) if true set cursor at the bottom of the line, otherwise set cursor at the top of the line.
  6486. * @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.
  6487. * @param $firstline (boolean) if true prints only the first line and return the remaining string.
  6488. * @param $firstblock (boolean) if true the string is the starting of a line.
  6489. * @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.
  6490. * @param $wadj (float) first line width will be reduced by this amount (used in HTML mode).
  6491. * @param $margin (array) margin array of the parent container
  6492. * @return mixed Return the number of cells or the remaining string if $firstline = true.
  6493. * @public
  6494. * @since 1.5
  6495. */
  6496. public function Write($h, $txt, $link='', $fill=false, $align='', $ln=false, $stretch=0, $firstline=false, $firstblock=false, $maxh=0, $wadj=0, $margin='') {
  6497. // check page for no-write regions and adapt page margins if necessary
  6498. list($this->x, $this->y) = $this->checkPageRegions($h, $this->x, $this->y);
  6499. if (strlen($txt) == 0) {
  6500. // fix empty text
  6501. $txt = ' ';
  6502. }
  6503. if ($margin === '') {
  6504. // set default margins
  6505. $margin = $this->cell_margin;
  6506. }
  6507. // remove carriage returns
  6508. $s = str_replace("\r", '', $txt);
  6509. // check if string contains arabic text
  6510. if (preg_match($this->unicode->uni_RE_PATTERN_ARABIC, $s)) {
  6511. $arabic = true;
  6512. } else {
  6513. $arabic = false;
  6514. }
  6515. // check if string contains RTL text
  6516. if ($arabic OR ($this->tmprtl == 'R') OR preg_match($this->unicode->uni_RE_PATTERN_RTL, $s)) {
  6517. $rtlmode = true;
  6518. } else {
  6519. $rtlmode = false;
  6520. }
  6521. // get a char width
  6522. $chrwidth = $this->GetCharWidth(46); // dot character
  6523. // get array of unicode values
  6524. $chars = $this->UTF8StringToArray($s);
  6525. // get array of chars
  6526. $uchars = $this->UTF8ArrayToUniArray($chars);
  6527. // get the number of characters
  6528. $nb = count($chars);
  6529. // replacement for SHY character (minus symbol)
  6530. $shy_replacement = 45;
  6531. $shy_replacement_char = $this->unichr($shy_replacement);
  6532. // widht for SHY replacement
  6533. $shy_replacement_width = $this->GetCharWidth($shy_replacement);
  6534. // max Y
  6535. $maxy = $this->y + $maxh - $h - $this->cell_padding['T'] - $this->cell_padding['B'];
  6536. // page width
  6537. $pw = $w = $this->w - $this->lMargin - $this->rMargin;
  6538. // calculate remaining line width ($w)
  6539. if ($this->rtl) {
  6540. $w = $this->x - $this->lMargin;
  6541. } else {
  6542. $w = $this->w - $this->rMargin - $this->x;
  6543. }
  6544. // max column width
  6545. $wmax = $w - $wadj;
  6546. if (!$firstline) {
  6547. $wmax -= ($this->cell_padding['L'] + $this->cell_padding['R']);
  6548. }
  6549. if ((!$firstline) AND (($chrwidth > $wmax) OR ($this->GetCharWidth($chars[0]) > $wmax))) {
  6550. // a single character do not fit on column
  6551. return '';
  6552. }
  6553. // minimum row height
  6554. $row_height = max($h, $this->FontSize * $this->cell_height_ratio);
  6555. $start_page = $this->page;
  6556. $i = 0; // character position
  6557. $j = 0; // current starting position
  6558. $sep = -1; // position of the last blank space
  6559. $shy = false; // true if the last blank is a soft hypen (SHY)
  6560. $l = 0; // current string length
  6561. $nl = 0; //number of lines
  6562. $linebreak = false;
  6563. $pc = 0; // previous character
  6564. // for each character
  6565. while ($i < $nb) {
  6566. if (($maxh > 0) AND ($this->y >= $maxy) ) {
  6567. break;
  6568. }
  6569. //Get the current character
  6570. $c = $chars[$i];
  6571. if ($c == 10) { // 10 = "\n" = new line
  6572. //Explicit line break
  6573. if ($align == 'J') {
  6574. if ($this->rtl) {
  6575. $talign = 'R';
  6576. } else {
  6577. $talign = 'L';
  6578. }
  6579. } else {
  6580. $talign = $align;
  6581. }
  6582. $tmpstr = $this->UniArrSubString($uchars, $j, $i);
  6583. if ($firstline) {
  6584. $startx = $this->x;
  6585. $tmparr = array_slice($chars, $j, ($i - $j));
  6586. if ($rtlmode) {
  6587. $tmparr = $this->utf8Bidi($tmparr, $tmpstr, $this->tmprtl);
  6588. }
  6589. $linew = $this->GetArrStringWidth($tmparr);
  6590. unset($tmparr);
  6591. if ($this->rtl) {
  6592. $this->endlinex = $startx - $linew;
  6593. } else {
  6594. $this->endlinex = $startx + $linew;
  6595. }
  6596. $w = $linew;
  6597. $tmpcellpadding = $this->cell_padding;
  6598. if ($maxh == 0) {
  6599. $this->SetCellPadding(0);
  6600. }
  6601. }
  6602. if ($firstblock AND $this->isRTLTextDir()) {
  6603. $tmpstr = $this->stringRightTrim($tmpstr);
  6604. }
  6605. // Skip newlines at the begining of a page or column
  6606. if (!empty($tmpstr) OR ($this->y < ($this->PageBreakTrigger - $row_height))) {
  6607. $this->Cell($w, $h, $tmpstr, 0, 1, $talign, $fill, $link, $stretch);
  6608. }
  6609. unset($tmpstr);
  6610. if ($firstline) {
  6611. $this->cell_padding = $tmpcellpadding;
  6612. return ($this->UniArrSubString($uchars, $i));
  6613. }
  6614. ++$nl;
  6615. $j = $i + 1;
  6616. $l = 0;
  6617. $sep = -1;
  6618. $shy = false;
  6619. // account for margin changes
  6620. if ((($this->y + $this->lasth) > $this->PageBreakTrigger) AND ($this->inPageBody())) {
  6621. $this->AcceptPageBreak();
  6622. if ($this->rtl) {
  6623. $this->x -= $margin['R'];
  6624. } else {
  6625. $this->x += $margin['L'];
  6626. }
  6627. $this->lMargin += $margin['L'];
  6628. $this->rMargin += $margin['R'];
  6629. }
  6630. $w = $this->getRemainingWidth();
  6631. $wmax = $w - $this->cell_padding['L'] - $this->cell_padding['R'];
  6632. } else {
  6633. // 160 is the non-breaking space.
  6634. // 173 is SHY (Soft Hypen).
  6635. // \p{Z} or \p{Separator}: any kind of Unicode whitespace or invisible separator.
  6636. // \p{Lo} or \p{Other_Letter}: a Unicode letter or ideograph that does not have lowercase and uppercase variants.
  6637. // \p{Lo} is needed because Chinese characters are packed next to each other without spaces in between.
  6638. if (($c != 160) AND (($c == 173) OR preg_match($this->re_spaces, $this->unichr($c)))) {
  6639. // update last blank space position
  6640. $sep = $i;
  6641. // check if is a SHY
  6642. if ($c == 173) {
  6643. $shy = true;
  6644. if ($pc == 45) {
  6645. $tmp_shy_replacement_width = 0;
  6646. $tmp_shy_replacement_char = '';
  6647. } else {
  6648. $tmp_shy_replacement_width = $shy_replacement_width;
  6649. $tmp_shy_replacement_char = $shy_replacement_char;
  6650. }
  6651. } else {
  6652. $shy = false;
  6653. }
  6654. }
  6655. // update string length
  6656. if ($this->isUnicodeFont() AND ($arabic)) {
  6657. // with bidirectional algorithm some chars may be changed affecting the line length
  6658. // *** very slow ***
  6659. $l = $this->GetArrStringWidth($this->utf8Bidi(array_slice($chars, $j, ($i - $j)), '', $this->tmprtl));
  6660. } else {
  6661. $l += $this->GetCharWidth($c);
  6662. }
  6663. if (($l > $wmax) OR (($c == 173) AND (($l + $tmp_shy_replacement_width) > $wmax)) ) {
  6664. // we have reached the end of column
  6665. if ($sep == -1) {
  6666. // check if the line was already started
  6667. if (($this->rtl AND ($this->x <= ($this->w - $this->rMargin - $chrwidth)))
  6668. OR ((!$this->rtl) AND ($this->x >= ($this->lMargin + $chrwidth)))) {
  6669. // print a void cell and go to next line
  6670. $this->Cell($w, $h, '', 0, 1);
  6671. $linebreak = true;
  6672. if ($firstline) {
  6673. return ($this->UniArrSubString($uchars, $j));
  6674. }
  6675. } else {
  6676. // truncate the word because do not fit on column
  6677. $tmpstr = $this->UniArrSubString($uchars, $j, $i);
  6678. if ($firstline) {
  6679. $startx = $this->x;
  6680. $tmparr = array_slice($chars, $j, ($i - $j));
  6681. if ($rtlmode) {
  6682. $tmparr = $this->utf8Bidi($tmparr, $tmpstr, $this->tmprtl);
  6683. }
  6684. $linew = $this->GetArrStringWidth($tmparr);
  6685. unset($tmparr);
  6686. if ($this->rtl) {
  6687. $this->endlinex = $startx - $linew;
  6688. } else {
  6689. $this->endlinex = $startx + $linew;
  6690. }
  6691. $w = $linew;
  6692. $tmpcellpadding = $this->cell_padding;
  6693. if ($maxh == 0) {
  6694. $this->SetCellPadding(0);
  6695. }
  6696. }
  6697. if ($firstblock AND $this->isRTLTextDir()) {
  6698. $tmpstr = $this->stringRightTrim($tmpstr);
  6699. }
  6700. $this->Cell($w, $h, $tmpstr, 0, 1, $align, $fill, $link, $stretch);
  6701. unset($tmpstr);
  6702. if ($firstline) {
  6703. $this->cell_padding = $tmpcellpadding;
  6704. return ($this->UniArrSubString($uchars, $i));
  6705. }
  6706. $j = $i;
  6707. --$i;
  6708. }
  6709. } else {
  6710. // word wrapping
  6711. if ($this->rtl AND (!$firstblock) AND ($sep < $i)) {
  6712. $endspace = 1;
  6713. } else {
  6714. $endspace = 0;
  6715. }
  6716. // check the length of the next string
  6717. $strrest = $this->UniArrSubString($uchars, ($sep + $endspace));
  6718. $nextstr = preg_split('/'.$this->re_space['p'].'/'.$this->re_space['m'], $this->stringTrim($strrest));
  6719. if (isset($nextstr[0]) AND ($this->GetStringWidth($nextstr[0]) > $pw)) {
  6720. // truncate the word because do not fit on a full page width
  6721. $tmpstr = $this->UniArrSubString($uchars, $j, $i);
  6722. if ($firstline) {
  6723. $startx = $this->x;
  6724. $tmparr = array_slice($chars, $j, ($i - $j));
  6725. if ($rtlmode) {
  6726. $tmparr = $this->utf8Bidi($tmparr, $tmpstr, $this->tmprtl);
  6727. }
  6728. $linew = $this->GetArrStringWidth($tmparr);
  6729. unset($tmparr);
  6730. if ($this->rtl) {
  6731. $this->endlinex = $startx - $linew;
  6732. } else {
  6733. $this->endlinex = $startx + $linew;
  6734. }
  6735. $w = $linew;
  6736. $tmpcellpadding = $this->cell_padding;
  6737. if ($maxh == 0) {
  6738. $this->SetCellPadding(0);
  6739. }
  6740. }
  6741. if ($firstblock AND $this->isRTLTextDir()) {
  6742. $tmpstr = $this->stringRightTrim($tmpstr);
  6743. }
  6744. $this->Cell($w, $h, $tmpstr, 0, 1, $align, $fill, $link, $stretch);
  6745. unset($tmpstr);
  6746. if ($firstline) {
  6747. $this->cell_padding = $tmpcellpadding;
  6748. return ($this->UniArrSubString($uchars, $i));
  6749. }
  6750. $j = $i;
  6751. --$i;
  6752. } else {
  6753. // word wrapping
  6754. if ($shy) {
  6755. // add hypen (minus symbol) at the end of the line
  6756. $shy_width = $tmp_shy_replacement_width;
  6757. if ($this->rtl) {
  6758. $shy_char_left = $tmp_shy_replacement_char;
  6759. $shy_char_right = '';
  6760. } else {
  6761. $shy_char_left = '';
  6762. $shy_char_right = $tmp_shy_replacement_char;
  6763. }
  6764. } else {
  6765. $shy_width = 0;
  6766. $shy_char_left = '';
  6767. $shy_char_right = '';
  6768. }
  6769. $tmpstr = $this->UniArrSubString($uchars, $j, ($sep + $endspace));
  6770. if ($firstline) {
  6771. $startx = $this->x;
  6772. $tmparr = array_slice($chars, $j, (($sep + $endspace) - $j));
  6773. if ($rtlmode) {
  6774. $tmparr = $this->utf8Bidi($tmparr, $tmpstr, $this->tmprtl);
  6775. }
  6776. $linew = $this->GetArrStringWidth($tmparr);
  6777. unset($tmparr);
  6778. if ($this->rtl) {
  6779. $this->endlinex = $startx - $linew - $shy_width;
  6780. } else {
  6781. $this->endlinex = $startx + $linew + $shy_width;
  6782. }
  6783. $w = $linew;
  6784. $tmpcellpadding = $this->cell_padding;
  6785. if ($maxh == 0) {
  6786. $this->SetCellPadding(0);
  6787. }
  6788. }
  6789. // print the line
  6790. if ($firstblock AND $this->isRTLTextDir()) {
  6791. $tmpstr = $this->stringRightTrim($tmpstr);
  6792. }
  6793. $this->Cell($w, $h, $shy_char_left.$tmpstr.$shy_char_right, 0, 1, $align, $fill, $link, $stretch);
  6794. unset($tmpstr);
  6795. if ($firstline) {
  6796. // return the remaining text
  6797. $this->cell_padding = $tmpcellpadding;
  6798. return ($this->UniArrSubString($uchars, ($sep + $endspace)));
  6799. }
  6800. $i = $sep;
  6801. $sep = -1;
  6802. $shy = false;
  6803. $j = ($i+1);
  6804. }
  6805. }
  6806. // account for margin changes
  6807. if ((($this->y + $this->lasth) > $this->PageBreakTrigger) AND ($this->inPageBody())) {
  6808. $this->AcceptPageBreak();
  6809. if ($this->rtl) {
  6810. $this->x -= $margin['R'];
  6811. } else {
  6812. $this->x += $margin['L'];
  6813. }
  6814. $this->lMargin += $margin['L'];
  6815. $this->rMargin += $margin['R'];
  6816. }
  6817. $w = $this->getRemainingWidth();
  6818. $wmax = $w - $this->cell_padding['L'] - $this->cell_padding['R'];
  6819. if ($linebreak) {
  6820. $linebreak = false;
  6821. } else {
  6822. ++$nl;
  6823. $l = 0;
  6824. }
  6825. }
  6826. }
  6827. // save last character
  6828. $pc = $c;
  6829. ++$i;
  6830. } // end while i < nb
  6831. // print last substring (if any)
  6832. if ($l > 0) {
  6833. switch ($align) {
  6834. case 'J':
  6835. case 'C': {
  6836. $w = $w;
  6837. break;
  6838. }
  6839. case 'L': {
  6840. if ($this->rtl) {
  6841. $w = $w;
  6842. } else {
  6843. $w = $l;
  6844. }
  6845. break;
  6846. }
  6847. case 'R': {
  6848. if ($this->rtl) {
  6849. $w = $l;
  6850. } else {
  6851. $w = $w;
  6852. }
  6853. break;
  6854. }
  6855. default: {
  6856. $w = $l;
  6857. break;
  6858. }
  6859. }
  6860. $tmpstr = $this->UniArrSubString($uchars, $j, $nb);
  6861. if ($firstline) {
  6862. $startx = $this->x;
  6863. $tmparr = array_slice($chars, $j, ($nb - $j));
  6864. if ($rtlmode) {
  6865. $tmparr = $this->utf8Bidi($tmparr, $tmpstr, $this->tmprtl);
  6866. }
  6867. $linew = $this->GetArrStringWidth($tmparr);
  6868. unset($tmparr);
  6869. if ($this->rtl) {
  6870. $this->endlinex = $startx - $linew;
  6871. } else {
  6872. $this->endlinex = $startx + $linew;
  6873. }
  6874. $w = $linew;
  6875. $tmpcellpadding = $this->cell_padding;
  6876. if ($maxh == 0) {
  6877. $this->SetCellPadding(0);
  6878. }
  6879. }
  6880. if ($firstblock AND $this->isRTLTextDir()) {
  6881. $tmpstr = $this->stringRightTrim($tmpstr);
  6882. }
  6883. $this->Cell($w, $h, $tmpstr, 0, $ln, $align, $fill, $link, $stretch);
  6884. unset($tmpstr);
  6885. if ($firstline) {
  6886. $this->cell_padding = $tmpcellpadding;
  6887. return ($this->UniArrSubString($uchars, $nb));
  6888. }
  6889. ++$nl;
  6890. }
  6891. if ($firstline) {
  6892. return '';
  6893. }
  6894. return $nl;
  6895. }
  6896. /**
  6897. * Returns the remaining width between the current position and margins.
  6898. * @return int Return the remaining width
  6899. * @protected
  6900. */
  6901. protected function getRemainingWidth() {
  6902. list($this->x, $this->y) = $this->checkPageRegions(0, $this->x, $this->y);
  6903. if ($this->rtl) {
  6904. return ($this->x - $this->lMargin);
  6905. } else {
  6906. return ($this->w - $this->rMargin - $this->x);
  6907. }
  6908. }
  6909. /**
  6910. * Extract a slice of the $strarr array and return it as string.
  6911. * @param $strarr (string) The input array of characters.
  6912. * @param $start (int) the starting element of $strarr.
  6913. * @param $end (int) first element that will not be returned.
  6914. * @return Return part of a string
  6915. * @public
  6916. */
  6917. public function UTF8ArrSubString($strarr, $start='', $end='') {
  6918. if (strlen($start) == 0) {
  6919. $start = 0;
  6920. }
  6921. if (strlen($end) == 0) {
  6922. $end = count($strarr);
  6923. }
  6924. $string = '';
  6925. for ($i=$start; $i < $end; ++$i) {
  6926. $string .= $this->unichr($strarr[$i]);
  6927. }
  6928. return $string;
  6929. }
  6930. /**
  6931. * Extract a slice of the $uniarr array and return it as string.
  6932. * @param $uniarr (string) The input array of characters.
  6933. * @param $start (int) the starting element of $strarr.
  6934. * @param $end (int) first element that will not be returned.
  6935. * @return Return part of a string
  6936. * @public
  6937. * @since 4.5.037 (2009-04-07)
  6938. */
  6939. public function UniArrSubString($uniarr, $start='', $end='') {
  6940. if (strlen($start) == 0) {
  6941. $start = 0;
  6942. }
  6943. if (strlen($end) == 0) {
  6944. $end = count($uniarr);
  6945. }
  6946. $string = '';
  6947. for ($i=$start; $i < $end; ++$i) {
  6948. $string .= $uniarr[$i];
  6949. }
  6950. return $string;
  6951. }
  6952. /**
  6953. * Convert an array of UTF8 values to array of unicode characters
  6954. * @param $ta (string) The input array of UTF8 values.
  6955. * @return Return array of unicode characters
  6956. * @public
  6957. * @since 4.5.037 (2009-04-07)
  6958. */
  6959. public function UTF8ArrayToUniArray($ta) {
  6960. return array_map(array($this, 'unichr'), $ta);
  6961. }
  6962. /**
  6963. * Returns the unicode caracter specified by UTF-8 value
  6964. * @param $c (int) UTF-8 value
  6965. * @return Returns the specified character.
  6966. * @author Miguel Perez, Nicola Asuni
  6967. * @public
  6968. * @since 2.3.000 (2008-03-05)
  6969. */
  6970. public function unichr($c) {
  6971. if (!$this->isunicode) {
  6972. return chr($c);
  6973. } elseif ($c <= 0x7F) {
  6974. // one byte
  6975. return chr($c);
  6976. } elseif ($c <= 0x7FF) {
  6977. // two bytes
  6978. return chr(0xC0 | $c >> 6).chr(0x80 | $c & 0x3F);
  6979. } elseif ($c <= 0xFFFF) {
  6980. // three bytes
  6981. return chr(0xE0 | $c >> 12).chr(0x80 | $c >> 6 & 0x3F).chr(0x80 | $c & 0x3F);
  6982. } elseif ($c <= 0x10FFFF) {
  6983. // four bytes
  6984. return chr(0xF0 | $c >> 18).chr(0x80 | $c >> 12 & 0x3F).chr(0x80 | $c >> 6 & 0x3F).chr(0x80 | $c & 0x3F);
  6985. } else {
  6986. return '';
  6987. }
  6988. }
  6989. /**
  6990. * Return the image type given the file name or array returned by getimagesize() function.
  6991. * @param $imgfile (string) image file name
  6992. * @param $iminfo (array) array of image information returned by getimagesize() function.
  6993. * @return string image type
  6994. * @since 4.8.017 (2009-11-27)
  6995. */
  6996. public function getImageFileType($imgfile, $iminfo=array()) {
  6997. $type = '';
  6998. if (isset($iminfo['mime']) AND !empty($iminfo['mime'])) {
  6999. $mime = explode('/', $iminfo['mime']);
  7000. if ((count($mime) > 1) AND ($mime[0] == 'image') AND (!empty($mime[1]))) {
  7001. $type = strtolower(trim($mime[1]));
  7002. }
  7003. }
  7004. if (empty($type)) {
  7005. $fileinfo = pathinfo($imgfile);
  7006. if (isset($fileinfo['extension']) AND (!$this->empty_string($fileinfo['extension']))) {
  7007. $type = strtolower(trim($fileinfo['extension']));
  7008. }
  7009. }
  7010. if ($type == 'jpg') {
  7011. $type = 'jpeg';
  7012. }
  7013. return $type;
  7014. }
  7015. /**
  7016. * Set the block dimensions accounting for page breaks and page/column fitting
  7017. * @param $w (float) width
  7018. * @param $h (float) height
  7019. * @param $x (float) X coordinate
  7020. * @param $y (float) Y coodiante
  7021. * @param $fitonpage (boolean) if true the block is resized to not exceed page dimensions.
  7022. * @return array($w, $h, $x, $y)
  7023. * @protected
  7024. * @since 5.5.009 (2010-07-05)
  7025. */
  7026. protected function fitBlock($w, $h, $x, $y, $fitonpage=false) {
  7027. if ($w <= 0) {
  7028. // set maximum width
  7029. $w = ($this->w - $this->lMargin - $this->rMargin);
  7030. }
  7031. if ($h <= 0) {
  7032. // set maximum height
  7033. $h = ($this->PageBreakTrigger - $this->tMargin);
  7034. }
  7035. // resize the block to be vertically contained on a single page or single column
  7036. if ($fitonpage OR $this->AutoPageBreak) {
  7037. $ratio_wh = ($w / $h);
  7038. if ($h > ($this->PageBreakTrigger - $this->tMargin)) {
  7039. $h = $this->PageBreakTrigger - $this->tMargin;
  7040. $w = ($h * $ratio_wh);
  7041. }
  7042. // resize the block to be horizontally contained on a single page or single column
  7043. if ($fitonpage) {
  7044. $maxw = ($this->w - $this->lMargin - $this->rMargin);
  7045. if ($w > $maxw) {
  7046. $w = $maxw;
  7047. $h = ($w / $ratio_wh);
  7048. }
  7049. }
  7050. }
  7051. // Check whether we need a new page or new column first as this does not fit
  7052. $prev_x = $this->x;
  7053. $prev_y = $this->y;
  7054. if ($this->checkPageBreak($h, $y) OR ($this->y < $prev_y)) {
  7055. $y = $this->y;
  7056. if ($this->rtl) {
  7057. $x += ($prev_x - $this->x);
  7058. } else {
  7059. $x += ($this->x - $prev_x);
  7060. }
  7061. $this->newline = true;
  7062. }
  7063. // resize the block to be contained on the remaining available page or column space
  7064. if ($fitonpage) {
  7065. $ratio_wh = ($w / $h);
  7066. if (($y + $h) > $this->PageBreakTrigger) {
  7067. $h = $this->PageBreakTrigger - $y;
  7068. $w = ($h * $ratio_wh);
  7069. }
  7070. if ((!$this->rtl) AND (($x + $w) > ($this->w - $this->rMargin))) {
  7071. $w = $this->w - $this->rMargin - $x;
  7072. $h = ($w / $ratio_wh);
  7073. } elseif (($this->rtl) AND (($x - $w) < ($this->lMargin))) {
  7074. $w = $x - $this->lMargin;
  7075. $h = ($w / $ratio_wh);
  7076. }
  7077. }
  7078. return array($w, $h, $x, $y);
  7079. }
  7080. /**
  7081. * Puts an image in the page.
  7082. * The upper-left corner must be given.
  7083. * The dimensions can be specified in different ways:<ul>
  7084. * <li>explicit width and height (expressed in user unit)</li>
  7085. * <li>one explicit dimension, the other being calculated automatically in order to keep the original proportions</li>
  7086. * <li>no explicit dimension, in which case the image is put at 72 dpi</li></ul>
  7087. * 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;
  7088. * The format can be specified explicitly or inferred from the file extension.<br />
  7089. * It is possible to put a link on the image.<br />
  7090. * Remark: if an image is used several times, only one copy will be embedded in the file.<br />
  7091. * @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').
  7092. * @param $x (float) Abscissa of the upper-left corner (LTR) or upper-right corner (RTL).
  7093. * @param $y (float) Ordinate of the upper-left corner (LTR) or upper-right corner (RTL).
  7094. * @param $w (float) Width of the image in the page. If not specified or equal to zero, it is automatically calculated.
  7095. * @param $h (float) Height of the image in the page. If not specified or equal to zero, it is automatically calculated.
  7096. * @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.
  7097. * @param $link (mixed) URL or identifier returned by AddLink().
  7098. * @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>
  7099. * @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).
  7100. * @param $dpi (int) dot-per-inch resolution used on resize
  7101. * @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>
  7102. * @param $ismask (boolean) true if this image is a mask, false otherwise
  7103. * @param $imgmask (mixed) image object returned by this function or false
  7104. * @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)))
  7105. * @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).
  7106. * @param $hidden (boolean) If true do not display the image.
  7107. * @param $fitonpage (boolean) If true the image is resized to not exceed page dimensions.
  7108. * @param $alt (boolean) If true the image will be added as alternative and not directly printed (the ID of the image will be returned).
  7109. * @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.
  7110. * @return image information
  7111. * @public
  7112. * @since 1.1
  7113. */
  7114. 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()) {
  7115. if ($x === '') {
  7116. $x = $this->x;
  7117. }
  7118. if ($y === '') {
  7119. $y = $this->y;
  7120. }
  7121. // check page for no-write regions and adapt page margins if necessary
  7122. list($x, $y) = $this->checkPageRegions($h, $x, $y);
  7123. $cached_file = false; // true when the file is cached
  7124. $exurl = ''; // external streams
  7125. // check if we are passing an image as file or string
  7126. if ($file[0] === '@') {
  7127. // image from string
  7128. $imgdata = substr($file, 1);
  7129. $file = K_PATH_CACHE.'img_'.md5($imgdata);
  7130. $fp = fopen($file, 'w');
  7131. fwrite($fp, $imgdata);
  7132. fclose($fp);
  7133. unset($imgdata);
  7134. $cached_file = true;
  7135. $imsize = @getimagesize($file);
  7136. if ($imsize === FALSE) {
  7137. unlink($file);
  7138. $cached_file = false;
  7139. }
  7140. } else { // image file
  7141. if ($file{0} === '*') {
  7142. // image as external stream
  7143. $file = substr($file, 1);
  7144. $exurl = $file;
  7145. }
  7146. // check if is local file
  7147. if (!@file_exists($file)) {
  7148. // encode spaces on filename (file is probably an URL)
  7149. $file = str_replace(' ', '%20', $file);
  7150. }
  7151. if (@file_exists($file)) {
  7152. // get image dimensions
  7153. $imsize = @getimagesize($file);
  7154. } else {
  7155. $imsize = false;
  7156. }
  7157. if ($imsize === FALSE) {
  7158. if (function_exists('curl_init')) {
  7159. // try to get remote file data using cURL
  7160. $cs = curl_init(); // curl session
  7161. curl_setopt($cs, CURLOPT_URL, $file);
  7162. curl_setopt($cs, CURLOPT_BINARYTRANSFER, true);
  7163. curl_setopt($cs, CURLOPT_FAILONERROR, true);
  7164. curl_setopt($cs, CURLOPT_RETURNTRANSFER, true);
  7165. curl_setopt($cs, CURLOPT_FOLLOWLOCATION, true);
  7166. curl_setopt($cs, CURLOPT_CONNECTTIMEOUT, 5);
  7167. curl_setopt($cs, CURLOPT_TIMEOUT, 30);
  7168. curl_setopt($cs, CURLOPT_SSL_VERIFYPEER, false);
  7169. curl_setopt($cs, CURLOPT_SSL_VERIFYHOST, false);
  7170. curl_setopt($cs, CURLOPT_USERAGENT, 'TCPDF');
  7171. $imgdata = curl_exec($cs);
  7172. curl_close($cs);
  7173. if ($imgdata !== FALSE) {
  7174. // copy image to cache
  7175. $file = K_PATH_CACHE.'img_'.md5($imgdata);
  7176. $fp = fopen($file, 'w');
  7177. fwrite($fp, $imgdata);
  7178. fclose($fp);
  7179. unset($imgdata);
  7180. $cached_file = true;
  7181. $imsize = @getimagesize($file);
  7182. if ($imsize === FALSE) {
  7183. unlink($file);
  7184. $cached_file = false;
  7185. }
  7186. }
  7187. } elseif (($w > 0) AND ($h > 0)) {
  7188. // get measures from specified data
  7189. $pw = $this->getHTMLUnitToUnits($w, 0, $this->pdfunit, true) * $this->imgscale * $this->k;
  7190. $ph = $this->getHTMLUnitToUnits($h, 0, $this->pdfunit, true) * $this->imgscale * $this->k;
  7191. $imsize = array($pw, $ph);
  7192. }
  7193. }
  7194. }
  7195. if ($imsize === FALSE) {
  7196. if (substr($file, 0, -34) == K_PATH_CACHE.'msk') { // mask file
  7197. // get measures from specified data
  7198. $pw = $this->getHTMLUnitToUnits($w, 0, $this->pdfunit, true) * $this->imgscale * $this->k;
  7199. $ph = $this->getHTMLUnitToUnits($h, 0, $this->pdfunit, true) * $this->imgscale * $this->k;
  7200. $imsize = array($pw, $ph);
  7201. } else {
  7202. $this->Error('[Image] Unable to get image: '.$file);
  7203. }
  7204. }
  7205. // get original image width and height in pixels
  7206. list($pixw, $pixh) = $imsize;
  7207. // calculate image width and height on document
  7208. if (($w <= 0) AND ($h <= 0)) {
  7209. // convert image size to document unit
  7210. $w = $this->pixelsToUnits($pixw);
  7211. $h = $this->pixelsToUnits($pixh);
  7212. } elseif ($w <= 0) {
  7213. $w = $h * $pixw / $pixh;
  7214. } elseif ($h <= 0) {
  7215. $h = $w * $pixh / $pixw;
  7216. } elseif (($fitbox !== false) AND ($w > 0) AND ($h > 0)) {
  7217. if (strlen($fitbox) !== 2) {
  7218. // set default alignment
  7219. $fitbox = '--';
  7220. }
  7221. // scale image dimensions proportionally to fit within the ($w, $h) box
  7222. if ((($w * $pixh) / ($h * $pixw)) < 1) {
  7223. // store current height
  7224. $oldh = $h;
  7225. // calculate new height
  7226. $h = $w * $pixh / $pixw;
  7227. // height difference
  7228. $hdiff = ($oldh - $h);
  7229. // vertical alignment
  7230. switch (strtoupper($fitbox{1})) {
  7231. case 'T': {
  7232. break;
  7233. }
  7234. case 'M': {
  7235. $y += ($hdiff / 2);
  7236. break;
  7237. }
  7238. case 'B': {
  7239. $y += $hdiff;
  7240. break;
  7241. }
  7242. }
  7243. } else {
  7244. // store current width
  7245. $oldw = $w;
  7246. // calculate new width
  7247. $w = $h * $pixw / $pixh;
  7248. // width difference
  7249. $wdiff = ($oldw - $w);
  7250. // horizontal alignment
  7251. switch (strtoupper($fitbox{0})) {
  7252. case 'L': {
  7253. if ($this->rtl) {
  7254. $x -= $wdiff;
  7255. }
  7256. break;
  7257. }
  7258. case 'C': {
  7259. if ($this->rtl) {
  7260. $x -= ($wdiff / 2);
  7261. } else {
  7262. $x += ($wdiff / 2);
  7263. }
  7264. break;
  7265. }
  7266. case 'R': {
  7267. if (!$this->rtl) {
  7268. $x += $wdiff;
  7269. }
  7270. break;
  7271. }
  7272. }
  7273. }
  7274. }
  7275. // fit the image on available space
  7276. list($w, $h, $x, $y) = $this->fitBlock($w, $h, $x, $y, $fitonpage);
  7277. // calculate new minimum dimensions in pixels
  7278. $neww = round($w * $this->k * $dpi / $this->dpi);
  7279. $newh = round($h * $this->k * $dpi / $this->dpi);
  7280. // check if resize is necessary (resize is used only to reduce the image)
  7281. $newsize = ($neww * $newh);
  7282. $pixsize = ($pixw * $pixh);
  7283. if (intval($resize) == 2) {
  7284. $resize = true;
  7285. } elseif ($newsize >= $pixsize) {
  7286. $resize = false;
  7287. }
  7288. // check if image has been already added on document
  7289. $newimage = true;
  7290. if (in_array($file, $this->imagekeys)) {
  7291. $newimage = false;
  7292. // get existing image data
  7293. $info = $this->getImageBuffer($file);
  7294. if (substr($file, 0, -34) != K_PATH_CACHE.'msk') {
  7295. // check if the newer image is larger
  7296. $oldsize = ($info['w'] * $info['h']);
  7297. if ((($oldsize < $newsize) AND ($resize)) OR (($oldsize < $pixsize) AND (!$resize))) {
  7298. $newimage = true;
  7299. }
  7300. }
  7301. } elseif (substr($file, 0, -34) != K_PATH_CACHE.'msk') {
  7302. // check for cached images with alpha channel
  7303. $filehash = md5($file);
  7304. $tempfile_plain = K_PATH_CACHE.'mskp_'.$filehash;
  7305. $tempfile_alpha = K_PATH_CACHE.'mska_'.$filehash;
  7306. if (in_array($tempfile_plain, $this->imagekeys)) {
  7307. // get existing image data
  7308. $info = $this->getImageBuffer($tempfile_plain);
  7309. // check if the newer image is larger
  7310. $oldsize = ($info['w'] * $info['h']);
  7311. if ((($oldsize < $newsize) AND ($resize)) OR (($oldsize < $pixsize) AND (!$resize))) {
  7312. $newimage = true;
  7313. } else {
  7314. $newimage = false;
  7315. // embed mask image
  7316. $imgmask = $this->Image($tempfile_alpha, $x, $y, $w, $h, 'PNG', '', '', $resize, $dpi, '', true, false);
  7317. // embed image, masked with previously embedded mask
  7318. return $this->Image($tempfile_plain, $x, $y, $w, $h, $type, $link, $align, $resize, $dpi, $palign, false, $imgmask);
  7319. }
  7320. }
  7321. }
  7322. if ($newimage) {
  7323. //First use of image, get info
  7324. $type = strtolower($type);
  7325. if ($type == '') {
  7326. $type = $this->getImageFileType($file, $imsize);
  7327. } elseif ($type == 'jpg') {
  7328. $type = 'jpeg';
  7329. }
  7330. $mqr = $this->get_mqr();
  7331. $this->set_mqr(false);
  7332. // Specific image handlers
  7333. $mtd = '_parse'.$type;
  7334. // GD image handler function
  7335. $gdfunction = 'imagecreatefrom'.$type;
  7336. $info = false;
  7337. if ((method_exists($this, $mtd)) AND (!($resize AND (function_exists($gdfunction) OR extension_loaded('imagick'))))) {
  7338. // TCPDF image functions
  7339. $info = $this->$mtd($file);
  7340. if ($info == 'pngalpha') {
  7341. return $this->ImagePngAlpha($file, $x, $y, $pixw, $pixh, $w, $h, 'PNG', $link, $align, $resize, $dpi, $palign, $filehash);
  7342. }
  7343. }
  7344. if (!$info) {
  7345. if (function_exists($gdfunction)) {
  7346. // GD library
  7347. $img = $gdfunction($file);
  7348. if ($resize) {
  7349. $imgr = imagecreatetruecolor($neww, $newh);
  7350. if (($type == 'gif') OR ($type == 'png')) {
  7351. $imgr = $this->_setGDImageTransparency($imgr, $img);
  7352. }
  7353. imagecopyresampled($imgr, $img, 0, 0, 0, 0, $neww, $newh, $pixw, $pixh);
  7354. if (($type == 'gif') OR ($type == 'png')) {
  7355. $info = $this->_toPNG($imgr);
  7356. } else {
  7357. $info = $this->_toJPEG($imgr);
  7358. }
  7359. } else {
  7360. if (($type == 'gif') OR ($type == 'png')) {
  7361. $info = $this->_toPNG($img);
  7362. } else {
  7363. $info = $this->_toJPEG($img);
  7364. }
  7365. }
  7366. } elseif (extension_loaded('imagick')) {
  7367. // ImageMagick library
  7368. $img = new Imagick();
  7369. if ($type == 'SVG') {
  7370. // get SVG file content
  7371. $svgimg = file_get_contents($file);
  7372. // get width and height
  7373. $regs = array();
  7374. if (preg_match('/<svg([^\>]*)>/si', $svgimg, $regs)) {
  7375. $svgtag = $regs[1];
  7376. $tmp = array();
  7377. if (preg_match('/[\s]+width[\s]*=[\s]*"([^"]*)"/si', $svgtag, $tmp)) {
  7378. $ow = $this->getHTMLUnitToUnits($tmp[1], 1, $this->svgunit, false);
  7379. $owu = sprintf('%.3F', ($ow * $dpi / 72)).$this->pdfunit;
  7380. $svgtag = preg_replace('/[\s]+width[\s]*=[\s]*"[^"]*"/si', ' width="'.$owu.'"', $svgtag, 1);
  7381. } else {
  7382. $ow = $w;
  7383. }
  7384. $tmp = array();
  7385. if (preg_match('/[\s]+height[\s]*=[\s]*"([^"]*)"/si', $svgtag, $tmp)) {
  7386. $oh = $this->getHTMLUnitToUnits($tmp[1], 1, $this->svgunit, false);
  7387. $ohu = sprintf('%.3F', ($oh * $dpi / 72)).$this->pdfunit;
  7388. $svgtag = preg_replace('/[\s]+height[\s]*=[\s]*"[^"]*"/si', ' height="'.$ohu.'"', $svgtag, 1);
  7389. } else {
  7390. $oh = $h;
  7391. }
  7392. $tmp = array();
  7393. if (!preg_match('/[\s]+viewBox[\s]*=[\s]*"[\s]*([0-9\.]+)[\s]+([0-9\.]+)[\s]+([0-9\.]+)[\s]+([0-9\.]+)[\s]*"/si', $svgtag, $tmp)) {
  7394. $vbw = ($ow * $this->imgscale * $this->k);
  7395. $vbh = ($oh * $this->imgscale * $this->k);
  7396. $vbox = sprintf(' viewBox="0 0 %.3F %.3F" ', $vbw, $vbh);
  7397. $svgtag = $vbox.$svgtag;
  7398. }
  7399. $svgimg = preg_replace('/<svg([^\>]*)>/si', '<svg'.$svgtag.'>', $svgimg, 1);
  7400. }
  7401. $img->readImageBlob($svgimg);
  7402. } else {
  7403. $img->readImage($file);
  7404. }
  7405. if ($resize) {
  7406. $img->resizeImage($neww, $newh, 10, 1, false);
  7407. }
  7408. $img->setCompressionQuality($this->jpeg_quality);
  7409. $img->setImageFormat('jpeg');
  7410. $tempname = tempnam(K_PATH_CACHE, 'jpg_');
  7411. $img->writeImage($tempname);
  7412. $info = $this->_parsejpeg($tempname);
  7413. unlink($tempname);
  7414. $img->destroy();
  7415. } else {
  7416. return;
  7417. }
  7418. }
  7419. if ($info === false) {
  7420. //If false, we cannot process image
  7421. return;
  7422. }
  7423. $this->set_mqr($mqr);
  7424. if ($ismask) {
  7425. // force grayscale
  7426. $info['cs'] = 'DeviceGray';
  7427. }
  7428. $info['i'] = $this->numimages;
  7429. if (!in_array($file, $this->imagekeys)) {
  7430. ++$info['i'];
  7431. }
  7432. if ($imgmask !== false) {
  7433. $info['masked'] = $imgmask;
  7434. }
  7435. if (!empty($exurl)) {
  7436. $info['exurl'] = $exurl;
  7437. }
  7438. // array of alternative images
  7439. $info['altimgs'] = $altimgs;
  7440. // add image to document
  7441. $this->setImageBuffer($file, $info);
  7442. }
  7443. if ($cached_file) {
  7444. // remove cached file
  7445. unlink($file);
  7446. }
  7447. // set alignment
  7448. $this->img_rb_y = $y + $h;
  7449. // set alignment
  7450. if ($this->rtl) {
  7451. if ($palign == 'L') {
  7452. $ximg = $this->lMargin;
  7453. } elseif ($palign == 'C') {
  7454. $ximg = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
  7455. } elseif ($palign == 'R') {
  7456. $ximg = $this->w - $this->rMargin - $w;
  7457. } else {
  7458. $ximg = $x - $w;
  7459. }
  7460. $this->img_rb_x = $ximg;
  7461. } else {
  7462. if ($palign == 'L') {
  7463. $ximg = $this->lMargin;
  7464. } elseif ($palign == 'C') {
  7465. $ximg = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
  7466. } elseif ($palign == 'R') {
  7467. $ximg = $this->w - $this->rMargin - $w;
  7468. } else {
  7469. $ximg = $x;
  7470. }
  7471. $this->img_rb_x = $ximg + $w;
  7472. }
  7473. if ($ismask OR $hidden) {
  7474. // image is not displayed
  7475. return $info['i'];
  7476. }
  7477. $xkimg = $ximg * $this->k;
  7478. if (!$alt) {
  7479. // only non-alternative immages will be set
  7480. $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']));
  7481. }
  7482. if (!empty($border)) {
  7483. $bx = $this->x;
  7484. $by = $this->y;
  7485. $this->x = $ximg;
  7486. if ($this->rtl) {
  7487. $this->x += $w;
  7488. }
  7489. $this->y = $y;
  7490. $this->Cell($w, $h, '', $border, 0, '', 0, '', 0, true);
  7491. $this->x = $bx;
  7492. $this->y = $by;
  7493. }
  7494. if ($link) {
  7495. $this->Link($ximg, $y, $w, $h, $link, 0);
  7496. }
  7497. // set pointer to align the next text/objects
  7498. switch($align) {
  7499. case 'T': {
  7500. $this->y = $y;
  7501. $this->x = $this->img_rb_x;
  7502. break;
  7503. }
  7504. case 'M': {
  7505. $this->y = $y + round($h/2);
  7506. $this->x = $this->img_rb_x;
  7507. break;
  7508. }
  7509. case 'B': {
  7510. $this->y = $this->img_rb_y;
  7511. $this->x = $this->img_rb_x;
  7512. break;
  7513. }
  7514. case 'N': {
  7515. $this->SetY($this->img_rb_y);
  7516. break;
  7517. }
  7518. default:{
  7519. break;
  7520. }
  7521. }
  7522. $this->endlinex = $this->img_rb_x;
  7523. if ($this->inxobj) {
  7524. // we are inside an XObject template
  7525. $this->xobjects[$this->xobjid]['images'][] = $info['i'];
  7526. }
  7527. return $info['i'];
  7528. }
  7529. /**
  7530. * Sets the current active configuration setting of magic_quotes_runtime (if the set_magic_quotes_runtime function exist)
  7531. * @param $mqr (boolean) FALSE for off, TRUE for on.
  7532. * @since 4.6.025 (2009-08-17)
  7533. */
  7534. public function set_mqr($mqr) {
  7535. if (!defined('PHP_VERSION_ID')) {
  7536. $version = PHP_VERSION;
  7537. define('PHP_VERSION_ID', (($version{0} * 10000) + ($version{2} * 100) + $version{4}));
  7538. }
  7539. if (PHP_VERSION_ID < 50300) {
  7540. @set_magic_quotes_runtime($mqr);
  7541. }
  7542. }
  7543. /**
  7544. * Gets the current active configuration setting of magic_quotes_runtime (if the get_magic_quotes_runtime function exist)
  7545. * @return Returns 0 if magic quotes runtime is off or get_magic_quotes_runtime doesn't exist, 1 otherwise.
  7546. * @since 4.6.025 (2009-08-17)
  7547. */
  7548. public function get_mqr() {
  7549. if (!defined('PHP_VERSION_ID')) {
  7550. $version = PHP_VERSION;
  7551. define('PHP_VERSION_ID', (($version{0} * 10000) + ($version{2} * 100) + $version{4}));
  7552. }
  7553. if (PHP_VERSION_ID < 50300) {
  7554. return @get_magic_quotes_runtime();
  7555. }
  7556. return 0;
  7557. }
  7558. /**
  7559. * Convert the loaded image to a JPEG and then return a structure for the PDF creator.
  7560. * This function requires GD library and write access to the directory defined on K_PATH_CACHE constant.
  7561. * @param $image (image) Image object.
  7562. * return image JPEG image object.
  7563. * @protected
  7564. */
  7565. protected function _toJPEG($image) {
  7566. $tempname = tempnam(K_PATH_CACHE, 'jpg_');
  7567. imagejpeg($image, $tempname, $this->jpeg_quality);
  7568. imagedestroy($image);
  7569. $retvars = $this->_parsejpeg($tempname);
  7570. // tidy up by removing temporary image
  7571. unlink($tempname);
  7572. return $retvars;
  7573. }
  7574. /**
  7575. * Convert the loaded image to a PNG and then return a structure for the PDF creator.
  7576. * This function requires GD library and write access to the directory defined on K_PATH_CACHE constant.
  7577. * @param $image (image) Image object.
  7578. * return image PNG image object.
  7579. * @protected
  7580. * @since 4.9.016 (2010-04-20)
  7581. */
  7582. protected function _toPNG($image) {
  7583. // set temporary image file name
  7584. $tempname = tempnam(K_PATH_CACHE, 'jpg_');
  7585. // turn off interlaced mode
  7586. imageinterlace($image, 0);
  7587. // create temporary PNG image
  7588. imagepng($image, $tempname);
  7589. // remove image from memory
  7590. imagedestroy($image);
  7591. // get PNG image data
  7592. $retvars = $this->_parsepng($tempname);
  7593. // tidy up by removing temporary image
  7594. unlink($tempname);
  7595. return $retvars;
  7596. }
  7597. /**
  7598. * Set the transparency for the given GD image.
  7599. * @param $new_image (image) GD image object
  7600. * @param $image (image) GD image object.
  7601. * return GD image object.
  7602. * @protected
  7603. * @since 4.9.016 (2010-04-20)
  7604. */
  7605. protected function _setGDImageTransparency($new_image, $image) {
  7606. // transparency index
  7607. $tid = imagecolortransparent($image);
  7608. // default transparency color
  7609. $tcol = array('red' => 255, 'green' => 255, 'blue' => 255);
  7610. if ($tid >= 0) {
  7611. // get the colors for the transparency index
  7612. $tcol = imagecolorsforindex($image, $tid);
  7613. }
  7614. $tid = imagecolorallocate($new_image, $tcol['red'], $tcol['green'], $tcol['blue']);
  7615. imagefill($new_image, 0, 0, $tid);
  7616. imagecolortransparent($new_image, $tid);
  7617. return $new_image;
  7618. }
  7619. /**
  7620. * Extract info from a JPEG file without using the GD library.
  7621. * @param $file (string) image file to parse
  7622. * @return array structure containing the image data
  7623. * @protected
  7624. */
  7625. protected function _parsejpeg($file) {
  7626. $a = getimagesize($file);
  7627. if (empty($a)) {
  7628. $this->Error('Missing or incorrect image file: '.$file);
  7629. }
  7630. if ($a[2] != 2) {
  7631. $this->Error('Not a JPEG file: '.$file);
  7632. }
  7633. // bits per pixel
  7634. $bpc = isset($a['bits']) ? intval($a['bits']) : 8;
  7635. // number of image channels
  7636. if (!isset($a['channels'])) {
  7637. $channels = 3;
  7638. } else {
  7639. $channels = intval($a['channels']);
  7640. }
  7641. // default colour space
  7642. switch ($channels) {
  7643. case 1: {
  7644. $colspace = 'DeviceGray';
  7645. break;
  7646. }
  7647. case 3: {
  7648. $colspace = 'DeviceRGB';
  7649. break;
  7650. }
  7651. case 4: {
  7652. $colspace = 'DeviceCMYK';
  7653. break;
  7654. }
  7655. default: {
  7656. $channels = 3;
  7657. $colspace = 'DeviceRGB';
  7658. break;
  7659. }
  7660. }
  7661. // get file content
  7662. $data = file_get_contents($file);
  7663. // check for embedded ICC profile
  7664. $icc = array();
  7665. $offset = 0;
  7666. while (($pos = strpos($data, "ICC_PROFILE\0", $offset)) !== false) {
  7667. // get ICC sequence length
  7668. $length = ($this->_getUSHORT($data, ($pos - 2)) - 16);
  7669. // marker sequence number
  7670. $msn = max(1, ord($data[($pos + 12)]));
  7671. // number of markers (total of APP2 used)
  7672. $nom = max(1, ord($data[($pos + 13)]));
  7673. // get sequence segment
  7674. $icc[($msn - 1)] = substr($data, ($pos + 14), $length);
  7675. // move forward to next sequence
  7676. $offset = ($pos + 14 + $length);
  7677. }
  7678. // order and compact ICC segments
  7679. if (count($icc) > 0) {
  7680. ksort($icc);
  7681. $icc = implode('', $icc);
  7682. if ((ord($icc{36}) != 0x61) OR (ord($icc{37}) != 0x63) OR (ord($icc{38}) != 0x73) OR (ord($icc{39}) != 0x70)) {
  7683. // invalid ICC profile
  7684. $icc = false;
  7685. }
  7686. } else {
  7687. $icc = false;
  7688. }
  7689. return array('w' => $a[0], 'h' => $a[1], 'ch' => $channels, 'icc' => $icc, 'cs' => $colspace, 'bpc' => $bpc, 'f' => 'DCTDecode', 'data' => $data);
  7690. }
  7691. /**
  7692. * Extract info from a PNG file without using the GD library.
  7693. * @param $file (string) image file to parse
  7694. * @return array structure containing the image data
  7695. * @protected
  7696. */
  7697. protected function _parsepng($file) {
  7698. $f = fopen($file, 'rb');
  7699. if ($f === false) {
  7700. $this->Error('Can\'t open image file: '.$file);
  7701. }
  7702. //Check signature
  7703. if (fread($f, 8) != chr(137).'PNG'.chr(13).chr(10).chr(26).chr(10)) {
  7704. $this->Error('Not a PNG file: '.$file);
  7705. }
  7706. //Read header chunk
  7707. fread($f, 4);
  7708. if (fread($f, 4) != 'IHDR') {
  7709. $this->Error('Incorrect PNG file: '.$file);
  7710. }
  7711. $w = $this->_freadint($f);
  7712. $h = $this->_freadint($f);
  7713. $bpc = ord(fread($f, 1));
  7714. if ($bpc > 8) {
  7715. //$this->Error('16-bit depth not supported: '.$file);
  7716. fclose($f);
  7717. return false;
  7718. }
  7719. $ct = ord(fread($f, 1));
  7720. if ($ct == 0) {
  7721. $colspace = 'DeviceGray';
  7722. } elseif ($ct == 2) {
  7723. $colspace = 'DeviceRGB';
  7724. } elseif ($ct == 3) {
  7725. $colspace = 'Indexed';
  7726. } else {
  7727. // alpha channel
  7728. fclose($f);
  7729. return 'pngalpha';
  7730. }
  7731. if (ord(fread($f, 1)) != 0) {
  7732. //$this->Error('Unknown compression method: '.$file);
  7733. fclose($f);
  7734. return false;
  7735. }
  7736. if (ord(fread($f, 1)) != 0) {
  7737. //$this->Error('Unknown filter method: '.$file);
  7738. fclose($f);
  7739. return false;
  7740. }
  7741. if (ord(fread($f, 1)) != 0) {
  7742. //$this->Error('Interlacing not supported: '.$file);
  7743. fclose($f);
  7744. return false;
  7745. }
  7746. fread($f, 4);
  7747. $channels = ($ct == 2 ? 3 : 1);
  7748. $parms = '/DecodeParms << /Predictor 15 /Colors '.$channels.' /BitsPerComponent '.$bpc.' /Columns '.$w.' >>';
  7749. //Scan chunks looking for palette, transparency and image data
  7750. $pal = '';
  7751. $trns = '';
  7752. $data = '';
  7753. $icc = false;
  7754. do {
  7755. $n = $this->_freadint($f);
  7756. $type = fread($f, 4);
  7757. if ($type == 'PLTE') {
  7758. // read palette
  7759. $pal = $this->rfread($f, $n);
  7760. fread($f, 4);
  7761. } elseif ($type == 'tRNS') {
  7762. // read transparency info
  7763. $t = $this->rfread($f, $n);
  7764. if ($ct == 0) {
  7765. $trns = array(ord($t{1}));
  7766. } elseif ($ct == 2) {
  7767. $trns = array(ord($t{1}), ord($t{3}), ord($t{5}));
  7768. } else {
  7769. $pos = strpos($t, chr(0));
  7770. if ($pos !== false) {
  7771. $trns = array($pos);
  7772. }
  7773. }
  7774. fread($f, 4);
  7775. } elseif ($type == 'IDAT') {
  7776. // read image data block
  7777. $data .= $this->rfread($f, $n);
  7778. fread($f, 4);
  7779. } elseif ($type == 'iCCP') {
  7780. // skip profile name and null separator
  7781. $len = 0;
  7782. while ((ord(fread($f, 1)) > 0) AND ($len < 80)) {
  7783. ++$len;
  7784. }
  7785. // get compression method
  7786. if (ord(fread($f, 1)) != 0) {
  7787. //$this->Error('Unknown filter method: '.$file);
  7788. fclose($f);
  7789. return false;
  7790. }
  7791. // read ICC Color Profile
  7792. $icc = $this->rfread($f, ($n - $len - 2));
  7793. // decompress profile
  7794. $icc = gzuncompress($icc);
  7795. fread($f, 4);
  7796. } elseif ($type == 'IEND') {
  7797. break;
  7798. } else {
  7799. $this->rfread($f, $n + 4);
  7800. }
  7801. } while ($n);
  7802. if (($colspace == 'Indexed') AND (empty($pal))) {
  7803. //$this->Error('Missing palette in '.$file);
  7804. fclose($f);
  7805. return false;
  7806. }
  7807. fclose($f);
  7808. return array('w' => $w, 'h' => $h, 'ch' => $channels, 'icc' => $icc, 'cs' => $colspace, 'bpc' => $bpc, 'f' => 'FlateDecode', 'parms' => $parms, 'pal' => $pal, 'trns' => $trns, 'data' => $data);
  7809. }
  7810. /**
  7811. * Binary-safe and URL-safe file read.
  7812. * 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.
  7813. * @param $handle (resource)
  7814. * @param $length (int)
  7815. * @return Returns the read string or FALSE in case of error.
  7816. * @author Nicola Asuni
  7817. * @protected
  7818. * @since 4.5.027 (2009-03-16)
  7819. */
  7820. protected function rfread($handle, $length) {
  7821. $data = fread($handle, $length);
  7822. if ($data === false) {
  7823. return false;
  7824. }
  7825. $rest = $length - strlen($data);
  7826. if ($rest > 0) {
  7827. $data .= $this->rfread($handle, $rest);
  7828. }
  7829. return $data;
  7830. }
  7831. /**
  7832. * Extract info from a PNG image with alpha channel using the GD library.
  7833. * @param $file (string) Name of the file containing the image.
  7834. * @param $x (float) Abscissa of the upper-left corner.
  7835. * @param $y (float) Ordinate of the upper-left corner.
  7836. * @param $wpx (float) Original width of the image in pixels.
  7837. * @param $hpx (float) original height of the image in pixels.
  7838. * @param $w (float) Width of the image in the page. If not specified or equal to zero, it is automatically calculated.
  7839. * @param $h (float) Height of the image in the page. If not specified or equal to zero, it is automatically calculated.
  7840. * @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.
  7841. * @param $link (mixed) URL or identifier returned by AddLink().
  7842. * @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>
  7843. * @param $resize (boolean) If true resize (reduce) the image to fit $w and $h (requires GD library).
  7844. * @param $dpi (int) dot-per-inch resolution used on resize
  7845. * @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>
  7846. * @param $filehash (string) File hash used to build unique file names.
  7847. * @author Nicola Asuni
  7848. * @protected
  7849. * @since 4.3.007 (2008-12-04)
  7850. * @see Image()
  7851. */
  7852. protected function ImagePngAlpha($file, $x, $y, $wpx, $hpx, $w, $h, $type, $link, $align, $resize, $dpi, $palign, $filehash='') {
  7853. if (empty($filehash)) {
  7854. $filehash = md5($file);
  7855. }
  7856. // create temp image file (without alpha channel)
  7857. $tempfile_plain = K_PATH_CACHE.'mskp_'.$filehash;
  7858. // create temp alpha file
  7859. $tempfile_alpha = K_PATH_CACHE.'mska_'.$filehash;
  7860. if (extension_loaded('imagick')) { // ImageMagick extension
  7861. // ImageMagick library
  7862. $img = new Imagick();
  7863. $img->readImage($file);
  7864. // clone image object
  7865. $imga = $img->clone();
  7866. // extract alpha channel
  7867. $img->separateImageChannel(8); // 8 = (imagick::CHANNEL_ALPHA | imagick::CHANNEL_OPACITY | imagick::CHANNEL_MATTE);
  7868. $img->negateImage(true);
  7869. $img->setImageFormat('png');
  7870. $img->writeImage($tempfile_alpha);
  7871. // remove alpha channel
  7872. $imga->separateImageChannel(39); // 39 = (imagick::CHANNEL_ALL & ~(imagick::CHANNEL_ALPHA | imagick::CHANNEL_OPACITY | imagick::CHANNEL_MATTE));
  7873. $imga->setImageFormat('png');
  7874. $imga->writeImage($tempfile_plain);
  7875. } elseif (function_exists('imagecreatefrompng')) { // GD extension
  7876. // generate images
  7877. $img = imagecreatefrompng($file);
  7878. $imgalpha = imagecreate($wpx, $hpx);
  7879. // generate gray scale palette (0 -> 255)
  7880. for ($c = 0; $c < 256; ++$c) {
  7881. ImageColorAllocate($imgalpha, $c, $c, $c);
  7882. }
  7883. // extract alpha channel
  7884. for ($xpx = 0; $xpx < $wpx; ++$xpx) {
  7885. for ($ypx = 0; $ypx < $hpx; ++$ypx) {
  7886. $color = imagecolorat($img, $xpx, $ypx);
  7887. $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)
  7888. $alpha = (((127 - $alpha) / 127) * 255); // GD alpha is only 7 bit (0 -> 127)
  7889. $alpha = $this->getGDgamma($alpha); // correct gamma
  7890. imagesetpixel($imgalpha, $xpx, $ypx, $alpha);
  7891. }
  7892. }
  7893. imagepng($imgalpha, $tempfile_alpha);
  7894. imagedestroy($imgalpha);
  7895. // extract image without alpha channel
  7896. $imgplain = imagecreatetruecolor($wpx, $hpx);
  7897. imagecopy($imgplain, $img, 0, 0, 0, 0, $wpx, $hpx);
  7898. imagepng($imgplain, $tempfile_plain);
  7899. imagedestroy($imgplain);
  7900. } else {
  7901. $this->Error('TCPDF requires the Imagick or GD extension to handle PNG images with alpha channel.');
  7902. }
  7903. // embed mask image
  7904. $imgmask = $this->Image($tempfile_alpha, $x, $y, $w, $h, 'PNG', '', '', $resize, $dpi, '', true, false);
  7905. // embed image, masked with previously embedded mask
  7906. $this->Image($tempfile_plain, $x, $y, $w, $h, $type, $link, $align, $resize, $dpi, $palign, false, $imgmask);
  7907. // remove temp files
  7908. unlink($tempfile_alpha);
  7909. unlink($tempfile_plain);
  7910. }
  7911. /**
  7912. * Correct the gamma value to be used with GD library
  7913. * @param $v (float) the gamma value to be corrected
  7914. * @protected
  7915. * @since 4.3.007 (2008-12-04)
  7916. */
  7917. protected function getGDgamma($v) {
  7918. return (pow(($v / 255), 2.2) * 255);
  7919. }
  7920. /**
  7921. * Performs a line break.
  7922. * The current abscissa goes back to the left margin and the ordinate increases by the amount passed in parameter.
  7923. * @param $h (float) The height of the break. By default, the value equals the height of the last printed cell.
  7924. * @param $cell (boolean) if true add the current left (or right o for RTL) padding to the X coordinate
  7925. * @public
  7926. * @since 1.0
  7927. * @see Cell()
  7928. */
  7929. public function Ln($h='', $cell=false) {
  7930. 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'])) {
  7931. // revove vertical space from the top of the column
  7932. return;
  7933. }
  7934. if ($cell) {
  7935. if ($this->rtl) {
  7936. $cellpadding = $this->cell_padding['R'];
  7937. } else {
  7938. $cellpadding = $this->cell_padding['L'];
  7939. }
  7940. } else {
  7941. $cellpadding = 0;
  7942. }
  7943. if ($this->rtl) {
  7944. $this->x = $this->w - $this->rMargin - $cellpadding;
  7945. } else {
  7946. $this->x = $this->lMargin + $cellpadding;
  7947. }
  7948. if (is_string($h)) {
  7949. $this->y += $this->lasth;
  7950. } else {
  7951. $this->y += $h;
  7952. }
  7953. $this->newline = true;
  7954. }
  7955. /**
  7956. * Returns the relative X value of current position.
  7957. * The value is relative to the left border for LTR languages and to the right border for RTL languages.
  7958. * @return float
  7959. * @public
  7960. * @since 1.2
  7961. * @see SetX(), GetY(), SetY()
  7962. */
  7963. public function GetX() {
  7964. //Get x position
  7965. if ($this->rtl) {
  7966. return ($this->w - $this->x);
  7967. } else {
  7968. return $this->x;
  7969. }
  7970. }
  7971. /**
  7972. * Returns the absolute X value of current position.
  7973. * @return float
  7974. * @public
  7975. * @since 1.2
  7976. * @see SetX(), GetY(), SetY()
  7977. */
  7978. public function GetAbsX() {
  7979. return $this->x;
  7980. }
  7981. /**
  7982. * Returns the ordinate of the current position.
  7983. * @return float
  7984. * @public
  7985. * @since 1.0
  7986. * @see SetY(), GetX(), SetX()
  7987. */
  7988. public function GetY() {
  7989. return $this->y;
  7990. }
  7991. /**
  7992. * Defines the abscissa of the current position.
  7993. * If the passed value is negative, it is relative to the right of the page (or left if language is RTL).
  7994. * @param $x (float) The value of the abscissa.
  7995. * @param $rtloff (boolean) if true always uses the page top-left corner as origin of axis.
  7996. * @public
  7997. * @since 1.2
  7998. * @see GetX(), GetY(), SetY(), SetXY()
  7999. */
  8000. public function SetX($x, $rtloff=false) {
  8001. if (!$rtloff AND $this->rtl) {
  8002. if ($x >= 0) {
  8003. $this->x = $this->w - $x;
  8004. } else {
  8005. $this->x = abs($x);
  8006. }
  8007. } else {
  8008. if ($x >= 0) {
  8009. $this->x = $x;
  8010. } else {
  8011. $this->x = $this->w + $x;
  8012. }
  8013. }
  8014. if ($this->x < 0) {
  8015. $this->x = 0;
  8016. }
  8017. if ($this->x > $this->w) {
  8018. $this->x = $this->w;
  8019. }
  8020. }
  8021. /**
  8022. * Moves the current abscissa back to the left margin and sets the ordinate.
  8023. * If the passed value is negative, it is relative to the bottom of the page.
  8024. * @param $y (float) The value of the ordinate.
  8025. * @param $resetx (bool) if true (default) reset the X position.
  8026. * @param $rtloff (boolean) if true always uses the page top-left corner as origin of axis.
  8027. * @public
  8028. * @since 1.0
  8029. * @see GetX(), GetY(), SetY(), SetXY()
  8030. */
  8031. public function SetY($y, $resetx=true, $rtloff=false) {
  8032. if ($resetx) {
  8033. //reset x
  8034. if (!$rtloff AND $this->rtl) {
  8035. $this->x = $this->w - $this->rMargin;
  8036. } else {
  8037. $this->x = $this->lMargin;
  8038. }
  8039. }
  8040. if ($y >= 0) {
  8041. $this->y = $y;
  8042. } else {
  8043. $this->y = $this->h + $y;
  8044. }
  8045. if ($this->y < 0) {
  8046. $this->y = 0;
  8047. }
  8048. if ($this->y > $this->h) {
  8049. $this->y = $this->h;
  8050. }
  8051. }
  8052. /**
  8053. * Defines the abscissa and ordinate of the current position.
  8054. * If the passed values are negative, they are relative respectively to the right and bottom of the page.
  8055. * @param $x (float) The value of the abscissa.
  8056. * @param $y (float) The value of the ordinate.
  8057. * @param $rtloff (boolean) if true always uses the page top-left corner as origin of axis.
  8058. * @public
  8059. * @since 1.2
  8060. * @see SetX(), SetY()
  8061. */
  8062. public function SetXY($x, $y, $rtloff=false) {
  8063. $this->SetY($y, false, $rtloff);
  8064. $this->SetX($x, $rtloff);
  8065. }
  8066. /**
  8067. * Ouput input data and compress it if possible.
  8068. * @param $data (string) Data to output.
  8069. * @param $length (int) Data length in bytes.
  8070. * @protected
  8071. * @since 5.9.086
  8072. */
  8073. protected function sendOutputData($data, $length) {
  8074. if (!isset($_SERVER['HTTP_ACCEPT_ENCODING']) OR empty($_SERVER['HTTP_ACCEPT_ENCODING'])) {
  8075. // the content length may vary if the server is using compression
  8076. header('Content-Length: '.$length);
  8077. }
  8078. echo $data;
  8079. }
  8080. /**
  8081. * Send the document to a given destination: string, local file or browser.
  8082. * In the last case, the plug-in may be used (if present) or a download ("Save as" dialog box) may be forced.<br />
  8083. * The method first calls Close() if necessary to terminate the document.
  8084. * @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.
  8085. * @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>
  8086. * @public
  8087. * @since 1.0
  8088. * @see Close()
  8089. */
  8090. public function Output($name='doc.pdf', $dest='I') {
  8091. //Output PDF to some destination
  8092. //Finish document if necessary
  8093. if ($this->state < 3) {
  8094. $this->Close();
  8095. }
  8096. //Normalize parameters
  8097. if (is_bool($dest)) {
  8098. $dest = $dest ? 'D' : 'F';
  8099. }
  8100. $dest = strtoupper($dest);
  8101. if ($dest{0} != 'F') {
  8102. $name = preg_replace('/[\s]+/', '_', $name);
  8103. $name = preg_replace('/[^a-zA-Z0-9_\.-]/', '', $name);
  8104. }
  8105. if ($this->sign) {
  8106. // *** apply digital signature to the document ***
  8107. // get the document content
  8108. $pdfdoc = $this->getBuffer();
  8109. // remove last newline
  8110. $pdfdoc = substr($pdfdoc, 0, -1);
  8111. // Remove the original buffer
  8112. if (isset($this->diskcache) AND $this->diskcache) {
  8113. // remove buffer file from cache
  8114. unlink($this->buffer);
  8115. }
  8116. unset($this->buffer);
  8117. // remove filler space
  8118. $byterange_string_len = strlen($this->byterange_string);
  8119. // define the ByteRange
  8120. $byte_range = array();
  8121. $byte_range[0] = 0;
  8122. $byte_range[1] = strpos($pdfdoc, $this->byterange_string) + $byterange_string_len + 10;
  8123. $byte_range[2] = $byte_range[1] + $this->signature_max_length + 2;
  8124. $byte_range[3] = strlen($pdfdoc) - $byte_range[2];
  8125. $pdfdoc = substr($pdfdoc, 0, $byte_range[1]).substr($pdfdoc, $byte_range[2]);
  8126. // replace the ByteRange
  8127. $byterange = sprintf('/ByteRange[0 %u %u %u]', $byte_range[1], $byte_range[2], $byte_range[3]);
  8128. $byterange .= str_repeat(' ', ($byterange_string_len - strlen($byterange)));
  8129. $pdfdoc = str_replace($this->byterange_string, $byterange, $pdfdoc);
  8130. // write the document to a temporary folder
  8131. $tempdoc = tempnam(K_PATH_CACHE, 'tmppdf_');
  8132. $f = fopen($tempdoc, 'wb');
  8133. if (!$f) {
  8134. $this->Error('Unable to create temporary file: '.$tempdoc);
  8135. }
  8136. $pdfdoc_length = strlen($pdfdoc);
  8137. fwrite($f, $pdfdoc, $pdfdoc_length);
  8138. fclose($f);
  8139. // get digital signature via openssl library
  8140. $tempsign = tempnam(K_PATH_CACHE, 'tmpsig_');
  8141. if (empty($this->signature_data['extracerts'])) {
  8142. openssl_pkcs7_sign($tempdoc, $tempsign, $this->signature_data['signcert'], array($this->signature_data['privkey'], $this->signature_data['password']), array(), PKCS7_BINARY | PKCS7_DETACHED);
  8143. } else {
  8144. 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']);
  8145. }
  8146. unlink($tempdoc);
  8147. // read signature
  8148. $signature = file_get_contents($tempsign);
  8149. unlink($tempsign);
  8150. // extract signature
  8151. $signature = substr($signature, $pdfdoc_length);
  8152. $signature = substr($signature, (strpos($signature, "%%EOF\n\n------") + 13));
  8153. $tmparr = explode("\n\n", $signature);
  8154. $signature = $tmparr[1];
  8155. unset($tmparr);
  8156. // decode signature
  8157. $signature = base64_decode(trim($signature));
  8158. // convert signature to hex
  8159. $signature = current(unpack('H*', $signature));
  8160. $signature = str_pad($signature, $this->signature_max_length, '0');
  8161. // disable disk caching
  8162. $this->diskcache = false;
  8163. // Add signature to the document
  8164. $this->buffer = substr($pdfdoc, 0, $byte_range[1]).'<'.$signature.'>'.substr($pdfdoc, $byte_range[1]);
  8165. $this->bufferlen = strlen($this->buffer);
  8166. }
  8167. switch($dest) {
  8168. case 'I': {
  8169. // Send PDF to the standard output
  8170. if (ob_get_contents()) {
  8171. $this->Error('Some data has already been output, can\'t send PDF file');
  8172. }
  8173. if (php_sapi_name() != 'cli') {
  8174. // send output to a browser
  8175. header('Content-Type: application/pdf');
  8176. if (headers_sent()) {
  8177. $this->Error('Some data has already been output to browser, can\'t send PDF file');
  8178. }
  8179. header('Cache-Control: private, must-revalidate, post-check=0, pre-check=0, max-age=1');
  8180. //header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
  8181. header('Pragma: public');
  8182. header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  8183. header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  8184. header('Content-Disposition: inline; filename="'.basename($name).'";');
  8185. $this->sendOutputData($this->getBuffer(), $this->bufferlen);
  8186. } else {
  8187. echo $this->getBuffer();
  8188. }
  8189. break;
  8190. }
  8191. case 'D': {
  8192. // download PDF as file
  8193. if (ob_get_contents()) {
  8194. $this->Error('Some data has already been output, can\'t send PDF file');
  8195. }
  8196. header('Content-Description: File Transfer');
  8197. if (headers_sent()) {
  8198. $this->Error('Some data has already been output to browser, can\'t send PDF file');
  8199. }
  8200. header('Cache-Control: private, must-revalidate, post-check=0, pre-check=0, max-age=1');
  8201. //header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
  8202. header('Pragma: public');
  8203. header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  8204. header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  8205. // force download dialog
  8206. if (strpos(php_sapi_name(), 'cgi') === false) {
  8207. header('Content-Type: application/force-download');
  8208. header('Content-Type: application/octet-stream', false);
  8209. header('Content-Type: application/download', false);
  8210. header('Content-Type: application/pdf', false);
  8211. } else {
  8212. header('Content-Type: application/pdf');
  8213. }
  8214. // use the Content-Disposition header to supply a recommended filename
  8215. header('Content-Disposition: attachment; filename="'.basename($name).'";');
  8216. header('Content-Transfer-Encoding: binary');
  8217. $this->sendOutputData($this->getBuffer(), $this->bufferlen);
  8218. break;
  8219. }
  8220. case 'F':
  8221. case 'FI':
  8222. case 'FD': {
  8223. // save PDF to a local file
  8224. if ($this->diskcache) {
  8225. copy($this->buffer, $name);
  8226. } else {
  8227. $f = fopen($name, 'wb');
  8228. if (!$f) {
  8229. $this->Error('Unable to create output file: '.$name);
  8230. }
  8231. fwrite($f, $this->getBuffer(), $this->bufferlen);
  8232. fclose($f);
  8233. }
  8234. if ($dest == 'FI') {
  8235. // send headers to browser
  8236. header('Content-Type: application/pdf');
  8237. header('Cache-Control: private, must-revalidate, post-check=0, pre-check=0, max-age=1');
  8238. //header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
  8239. header('Pragma: public');
  8240. header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  8241. header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  8242. header('Content-Disposition: inline; filename="'.basename($name).'";');
  8243. $this->sendOutputData(file_get_contents($name), filesize($name));
  8244. } elseif ($dest == 'FD') {
  8245. // send headers to browser
  8246. if (ob_get_contents()) {
  8247. $this->Error('Some data has already been output, can\'t send PDF file');
  8248. }
  8249. header('Content-Description: File Transfer');
  8250. if (headers_sent()) {
  8251. $this->Error('Some data has already been output to browser, can\'t send PDF file');
  8252. }
  8253. header('Cache-Control: private, must-revalidate, post-check=0, pre-check=0, max-age=1');
  8254. header('Pragma: public');
  8255. header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  8256. header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  8257. // force download dialog
  8258. if (strpos(php_sapi_name(), 'cgi') === false) {
  8259. header('Content-Type: application/force-download');
  8260. header('Content-Type: application/octet-stream', false);
  8261. header('Content-Type: application/download', false);
  8262. header('Content-Type: application/pdf', false);
  8263. } else {
  8264. header('Content-Type: application/pdf');
  8265. }
  8266. // use the Content-Disposition header to supply a recommended filename
  8267. header('Content-Disposition: attachment; filename="'.basename($name).'";');
  8268. header('Content-Transfer-Encoding: binary');
  8269. $this->sendOutputData(file_get_contents($name), filesize($name));
  8270. }
  8271. break;
  8272. }
  8273. case 'E': {
  8274. // return PDF as base64 mime multi-part email attachment (RFC 2045)
  8275. $retval = 'Content-Type: application/pdf;'."\r\n";
  8276. $retval .= ' name="'.$name.'"'."\r\n";
  8277. $retval .= 'Content-Transfer-Encoding: base64'."\r\n";
  8278. $retval .= 'Content-Disposition: attachment;'."\r\n";
  8279. $retval .= ' filename="'.$name.'"'."\r\n\r\n";
  8280. $retval .= chunk_split(base64_encode($this->getBuffer()), 76, "\r\n");
  8281. return $retval;
  8282. }
  8283. case 'S': {
  8284. // returns PDF as a string
  8285. return $this->getBuffer();
  8286. }
  8287. default: {
  8288. $this->Error('Incorrect output destination: '.$dest);
  8289. }
  8290. }
  8291. return '';
  8292. }
  8293. /**
  8294. * Unset all class variables except the following critical variables: internal_encoding, state, bufferlen, buffer and diskcache.
  8295. * @param $destroyall (boolean) if true destroys all class variables, otherwise preserves critical variables.
  8296. * @param $preserve_objcopy (boolean) if true preserves the objcopy variable
  8297. * @public
  8298. * @since 4.5.016 (2009-02-24)
  8299. */
  8300. public function _destroy($destroyall=false, $preserve_objcopy=false) {
  8301. if ($destroyall AND isset($this->diskcache) AND $this->diskcache AND (!$preserve_objcopy) AND (!$this->empty_string($this->buffer))) {
  8302. // remove buffer file from cache
  8303. unlink($this->buffer);
  8304. }
  8305. foreach (array_keys(get_object_vars($this)) as $val) {
  8306. if ($destroyall OR (
  8307. ($val != 'internal_encoding')
  8308. AND ($val != 'state')
  8309. AND ($val != 'bufferlen')
  8310. AND ($val != 'buffer')
  8311. AND ($val != 'diskcache')
  8312. AND ($val != 'sign')
  8313. AND ($val != 'signature_data')
  8314. AND ($val != 'signature_max_length')
  8315. AND ($val != 'byterange_string')
  8316. )) {
  8317. if ((!$preserve_objcopy OR ($val != 'objcopy')) AND isset($this->$val)) {
  8318. unset($this->$val);
  8319. }
  8320. }
  8321. }
  8322. }
  8323. /**
  8324. * Check for locale-related bug
  8325. * @protected
  8326. */
  8327. protected function _dochecks() {
  8328. //Check for locale-related bug
  8329. if (1.1 == 1) {
  8330. $this->Error('Don\'t alter the locale before including class file');
  8331. }
  8332. //Check for decimal separator
  8333. if (sprintf('%.1F', 1.0) != '1.0') {
  8334. setlocale(LC_NUMERIC, 'C');
  8335. }
  8336. }
  8337. /**
  8338. * Return fonts path
  8339. * @return string
  8340. * @protected
  8341. */
  8342. protected function _getfontpath() {
  8343. if (!defined('K_PATH_FONTS') AND is_dir(dirname(__FILE__).'/fonts')) {
  8344. define('K_PATH_FONTS', dirname(__FILE__).'/fonts/');
  8345. }
  8346. return defined('K_PATH_FONTS') ? K_PATH_FONTS : '';
  8347. }
  8348. /**
  8349. * Return an array containing variations for the basic page number alias.
  8350. * @param $a (string) Base alias.
  8351. * @return array of page number aliases
  8352. * @protected
  8353. */
  8354. protected function getInternalPageNumberAliases($a= '') {
  8355. $alias = array();
  8356. // build array of Unicode + ASCII variants (the order is important)
  8357. $alias = array('u' => array(), 'a' => array());
  8358. $u = '{'.$a.'}';
  8359. $alias['u'][] = $this->_escape($u);
  8360. if ($this->isunicode) {
  8361. $alias['u'][] = $this->_escape($this->UTF8ToLatin1($u));
  8362. $alias['u'][] = $this->_escape($this->utf8StrRev($u, false, $this->tmprtl));
  8363. $alias['a'][] = $this->_escape($this->UTF8ToLatin1($a));
  8364. $alias['a'][] = $this->_escape($this->utf8StrRev($a, false, $this->tmprtl));
  8365. }
  8366. $alias['a'][] = $this->_escape($a);
  8367. return $alias;
  8368. }
  8369. /**
  8370. * Return an array containing all internal page aliases.
  8371. * @return array of page number aliases
  8372. * @protected
  8373. */
  8374. protected function getAllInternalPageNumberAliases() {
  8375. $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);
  8376. $pnalias = array();
  8377. foreach($basic_alias as $k => $a) {
  8378. $pnalias[$k] = $this->getInternalPageNumberAliases($a);
  8379. }
  8380. return $pnalias;
  8381. }
  8382. /**
  8383. * Replace page number aliases with number.
  8384. * @param $page (string) Page content.
  8385. * @param $replace (array) Array of replacements (array keys are replacement strings, values are alias arrays).
  8386. * @param $diff (int) If passed, this will be set to the total char number difference between alias and replacements.
  8387. * @return replaced page content and updated $diff parameter as array.
  8388. * @protected
  8389. */
  8390. protected function replacePageNumAliases($page, $replace, $diff=0) {
  8391. foreach ($replace as $rep) {
  8392. foreach ($rep[3] as $a) {
  8393. if (strpos($page, $a) !== false) {
  8394. $page = str_replace($a, $rep[0], $page);
  8395. $diff += ($rep[2] - $rep[1]);
  8396. }
  8397. }
  8398. }
  8399. return array($page, $diff);
  8400. }
  8401. /**
  8402. * Replace right shift page number aliases with spaces to correct right alignment.
  8403. * This works perfectly only when using monospaced fonts.
  8404. * @param $page (string) Page content.
  8405. * @param $aliases (array) Array of page aliases.
  8406. * @param $diff (int) initial difference to add.
  8407. * @return replaced page content.
  8408. * @protected
  8409. */
  8410. protected function replaceRightShiftPageNumAliases($page, $aliases, $diff) {
  8411. foreach ($aliases as $type => $alias) {
  8412. foreach ($alias as $a) {
  8413. // find position of compensation factor
  8414. $startnum = (strpos($a, ':') + 1);
  8415. $a = substr($a, 0, $startnum);
  8416. if (($pos = strpos($page, $a)) !== false) {
  8417. // end of alias
  8418. $endnum = strpos($page, '}', $pos);
  8419. // string to be replaced
  8420. $aa = substr($page, $pos, ($endnum - $pos + 1));
  8421. // get compensation factor
  8422. $ratio = substr($page, ($pos + $startnum), ($endnum - $pos - $startnum));
  8423. $ratio = preg_replace('/[^0-9\.]/', '', $ratio);
  8424. $ratio = floatval($ratio);
  8425. if ($type == 'u') {
  8426. $chrdiff = floor(($diff + 12) * $ratio);
  8427. $shift = str_repeat(' ', $chrdiff);
  8428. $shift = $this->UTF8ToUTF16BE($shift, false);
  8429. } else {
  8430. $chrdiff = floor(($diff + 11) * $ratio);
  8431. $shift = str_repeat(' ', $chrdiff);
  8432. }
  8433. $page = str_replace($aa, $shift, $page);
  8434. }
  8435. }
  8436. }
  8437. return $page;
  8438. }
  8439. /**
  8440. * Output pages (and replace page number aliases).
  8441. * @protected
  8442. */
  8443. protected function _putpages() {
  8444. $filter = ($this->compress) ? '/Filter /FlateDecode ' : '';
  8445. // get internal aliases for page numbers
  8446. $pnalias = $this->getAllInternalPageNumberAliases();
  8447. $num_pages = $this->numpages;
  8448. $ptpa = $this->formatPageNumber(($this->starting_page_number + $num_pages - 1));
  8449. $ptpu = $this->UTF8ToUTF16BE($ptpa, false);
  8450. $ptp_num_chars = $this->GetNumChars($ptpa);
  8451. $pagegroupnum = 0;
  8452. $groupnum = 0;
  8453. $ptgu = 1;
  8454. $ptga = 1;
  8455. for ($n = 1; $n <= $num_pages; ++$n) {
  8456. // get current page
  8457. $temppage = $this->getPageBuffer($n);
  8458. $pagelen = strlen($temppage);
  8459. // set replacements for total pages number
  8460. $pnpa = $this->formatPageNumber(($this->starting_page_number + $n - 1));
  8461. $pnpu = $this->UTF8ToUTF16BE($pnpa, false);
  8462. $pnp_num_chars = $this->GetNumChars($pnpa);
  8463. $pdiff = 0; // difference used for right shift alignment of page numbers
  8464. $gdiff = 0; // difference used for right shift alignment of page group numbers
  8465. if (!empty($this->pagegroups)) {
  8466. if (isset($this->newpagegroup[$n])) {
  8467. $pagegroupnum = 0;
  8468. ++$groupnum;
  8469. $ptga = $this->formatPageNumber($this->pagegroups[$groupnum]);
  8470. $ptgu = $this->UTF8ToUTF16BE($ptga, false);
  8471. $ptg_num_chars = $this->GetNumChars($ptga);
  8472. }
  8473. ++$pagegroupnum;
  8474. $pnga = $this->formatPageNumber($pagegroupnum);
  8475. $pngu = $this->UTF8ToUTF16BE($pnga, false);
  8476. $png_num_chars = $this->GetNumChars($pnga);
  8477. // replace page numbers
  8478. $replace = array();
  8479. $replace[] = array($ptgu, $ptg_num_chars, 9, $pnalias[2]['u']);
  8480. $replace[] = array($ptga, $ptg_num_chars, 7, $pnalias[2]['a']);
  8481. $replace[] = array($pngu, $png_num_chars, 9, $pnalias[3]['u']);
  8482. $replace[] = array($pnga, $png_num_chars, 7, $pnalias[3]['a']);
  8483. list($temppage, $gdiff) = $this->replacePageNumAliases($temppage, $replace, $gdiff);
  8484. }
  8485. // replace page numbers
  8486. $replace = array();
  8487. $replace[] = array($ptpu, $ptp_num_chars, 9, $pnalias[0]['u']);
  8488. $replace[] = array($ptpa, $ptp_num_chars, 7, $pnalias[0]['a']);
  8489. $replace[] = array($pnpu, $pnp_num_chars, 9, $pnalias[1]['u']);
  8490. $replace[] = array($pnpa, $pnp_num_chars, 7, $pnalias[1]['a']);
  8491. list($temppage, $pdiff) = $this->replacePageNumAliases($temppage, $replace, $pdiff);
  8492. // replace right shift alias
  8493. $temppage = $this->replaceRightShiftPageNumAliases($temppage, $pnalias[4], max($pdiff, $gdiff));
  8494. // replace EPS marker
  8495. $temppage = str_replace($this->epsmarker, '', $temppage);
  8496. //Page
  8497. $this->page_obj_id[$n] = $this->_newobj();
  8498. $out = '<<';
  8499. $out .= ' /Type /Page';
  8500. $out .= ' /Parent 1 0 R';
  8501. $out .= ' /LastModified '.$this->_datestring();
  8502. $out .= ' /Resources 2 0 R';
  8503. $boxes = array('MediaBox', 'CropBox', 'BleedBox', 'TrimBox', 'ArtBox');
  8504. foreach ($boxes as $box) {
  8505. $out .= ' /'.$box;
  8506. $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']);
  8507. }
  8508. if (isset($this->pagedim[$n]['BoxColorInfo']) AND !empty($this->pagedim[$n]['BoxColorInfo'])) {
  8509. $out .= ' /BoxColorInfo <<';
  8510. foreach ($boxes as $box) {
  8511. if (isset($this->pagedim[$n]['BoxColorInfo'][$box])) {
  8512. $out .= ' /'.$box.' <<';
  8513. if (isset($this->pagedim[$n]['BoxColorInfo'][$box]['C'])) {
  8514. $color = $this->pagedim[$n]['BoxColorInfo'][$box]['C'];
  8515. $out .= ' /C [';
  8516. $out .= sprintf(' %.3F %.3F %.3F', $color[0]/255, $color[1]/255, $color[2]/255);
  8517. $out .= ' ]';
  8518. }
  8519. if (isset($this->pagedim[$n]['BoxColorInfo'][$box]['W'])) {
  8520. $out .= ' /W '.($this->pagedim[$n]['BoxColorInfo'][$box]['W'] * $this->k);
  8521. }
  8522. if (isset($this->pagedim[$n]['BoxColorInfo'][$box]['S'])) {
  8523. $out .= ' /S /'.$this->pagedim[$n]['BoxColorInfo'][$box]['S'];
  8524. }
  8525. if (isset($this->pagedim[$n]['BoxColorInfo'][$box]['D'])) {
  8526. $dashes = $this->pagedim[$n]['BoxColorInfo'][$box]['D'];
  8527. $out .= ' /D [';
  8528. foreach ($dashes as $dash) {
  8529. $out .= sprintf(' %.3F', ($dash * $this->k));
  8530. }
  8531. $out .= ' ]';
  8532. }
  8533. $out .= ' >>';
  8534. }
  8535. }
  8536. $out .= ' >>';
  8537. }
  8538. $out .= ' /Contents '.($this->n + 1).' 0 R';
  8539. $out .= ' /Rotate '.$this->pagedim[$n]['Rotate'];
  8540. if (!$this->pdfa_mode) {
  8541. $out .= ' /Group << /Type /Group /S /Transparency /CS /DeviceRGB >>';
  8542. }
  8543. if (isset($this->pagedim[$n]['trans']) AND !empty($this->pagedim[$n]['trans'])) {
  8544. // page transitions
  8545. if (isset($this->pagedim[$n]['trans']['Dur'])) {
  8546. $out .= ' /Dur '.$this->pagedim[$n]['trans']['Dur'];
  8547. }
  8548. $out .= ' /Trans <<';
  8549. $out .= ' /Type /Trans';
  8550. if (isset($this->pagedim[$n]['trans']['S'])) {
  8551. $out .= ' /S /'.$this->pagedim[$n]['trans']['S'];
  8552. }
  8553. if (isset($this->pagedim[$n]['trans']['D'])) {
  8554. $out .= ' /D '.$this->pagedim[$n]['trans']['D'];
  8555. }
  8556. if (isset($this->pagedim[$n]['trans']['Dm'])) {
  8557. $out .= ' /Dm /'.$this->pagedim[$n]['trans']['Dm'];
  8558. }
  8559. if (isset($this->pagedim[$n]['trans']['M'])) {
  8560. $out .= ' /M /'.$this->pagedim[$n]['trans']['M'];
  8561. }
  8562. if (isset($this->pagedim[$n]['trans']['Di'])) {
  8563. $out .= ' /Di '.$this->pagedim[$n]['trans']['Di'];
  8564. }
  8565. if (isset($this->pagedim[$n]['trans']['SS'])) {
  8566. $out .= ' /SS '.$this->pagedim[$n]['trans']['SS'];
  8567. }
  8568. if (isset($this->pagedim[$n]['trans']['B'])) {
  8569. $out .= ' /B '.$this->pagedim[$n]['trans']['B'];
  8570. }
  8571. $out .= ' >>';
  8572. }
  8573. $out .= $this->_getannotsrefs($n);
  8574. $out .= ' /PZ '.$this->pagedim[$n]['PZ'];
  8575. $out .= ' >>';
  8576. $out .= "\n".'endobj';
  8577. $this->_out($out);
  8578. //Page content
  8579. $p = ($this->compress) ? gzcompress($temppage) : $temppage;
  8580. $this->_newobj();
  8581. $p = $this->_getrawstream($p);
  8582. $this->_out('<<'.$filter.'/Length '.strlen($p).'>> stream'."\n".$p."\n".'endstream'."\n".'endobj');
  8583. if ($this->diskcache) {
  8584. // remove temporary files
  8585. unlink($this->pages[$n]);
  8586. }
  8587. }
  8588. //Pages root
  8589. $out = $this->_getobj(1)."\n";
  8590. $out .= '<< /Type /Pages /Kids [';
  8591. foreach($this->page_obj_id as $page_obj) {
  8592. $out .= ' '.$page_obj.' 0 R';
  8593. }
  8594. $out .= ' ] /Count '.$num_pages.' >>';
  8595. $out .= "\n".'endobj';
  8596. $this->_out($out);
  8597. }
  8598. /**
  8599. * Output references to page annotations
  8600. * @param $n (int) page number
  8601. * @protected
  8602. * @author Nicola Asuni
  8603. * @since 4.7.000 (2008-08-29)
  8604. * @deprecated
  8605. */
  8606. protected function _putannotsrefs($n) {
  8607. $this->_out($this->_getannotsrefs($n));
  8608. }
  8609. /**
  8610. * Get references to page annotations.
  8611. * @param $n (int) page number
  8612. * @return string
  8613. * @protected
  8614. * @author Nicola Asuni
  8615. * @since 5.0.010 (2010-05-17)
  8616. */
  8617. protected function _getannotsrefs($n) {
  8618. if (!(isset($this->PageAnnots[$n]) OR ($this->sign AND isset($this->signature_data['cert_type'])))) {
  8619. return '';
  8620. }
  8621. $out = ' /Annots [';
  8622. if (isset($this->PageAnnots[$n])) {
  8623. foreach ($this->PageAnnots[$n] as $key => $val) {
  8624. if (!in_array($val['n'], $this->radio_groups)) {
  8625. $out .= ' '.$val['n'].' 0 R';
  8626. }
  8627. }
  8628. // add radiobutton groups
  8629. if (isset($this->radiobutton_groups[$n])) {
  8630. foreach ($this->radiobutton_groups[$n] as $key => $data) {
  8631. if (isset($data['n'])) {
  8632. $out .= ' '.$data['n'].' 0 R';
  8633. }
  8634. }
  8635. }
  8636. }
  8637. if ($this->sign AND ($n == $this->signature_appearance['page']) AND isset($this->signature_data['cert_type'])) {
  8638. // set reference for signature object
  8639. $out .= ' '.$this->sig_obj_id.' 0 R';
  8640. }
  8641. if (!empty($this->empty_signature_appearance)) {
  8642. foreach ($this->empty_signature_appearance as $esa) {
  8643. if ($esa['page'] == $n) {
  8644. // set reference for empty signature objects
  8645. $out .= ' '.$esa['objid'].' 0 R';
  8646. }
  8647. }
  8648. }
  8649. $out .= ' ]';
  8650. return $out;
  8651. }
  8652. /**
  8653. * Output annotations objects for all pages.
  8654. * !!! THIS METHOD IS NOT YET COMPLETED !!!
  8655. * See section 12.5 of PDF 32000_2008 reference.
  8656. * @protected
  8657. * @author Nicola Asuni
  8658. * @since 4.0.018 (2008-08-06)
  8659. */
  8660. protected function _putannotsobjs() {
  8661. // reset object counter
  8662. for ($n=1; $n <= $this->numpages; ++$n) {
  8663. if (isset($this->PageAnnots[$n])) {
  8664. // set page annotations
  8665. foreach ($this->PageAnnots[$n] as $key => $pl) {
  8666. $annot_obj_id = $this->PageAnnots[$n][$key]['n'];
  8667. // create annotation object for grouping radiobuttons
  8668. if (isset($this->radiobutton_groups[$n][$pl['txt']]) AND is_array($this->radiobutton_groups[$n][$pl['txt']])) {
  8669. $radio_button_obj_id = $this->radiobutton_groups[$n][$pl['txt']]['n'];
  8670. $annots = '<<';
  8671. $annots .= ' /Type /Annot';
  8672. $annots .= ' /Subtype /Widget';
  8673. $annots .= ' /Rect [0 0 0 0]';
  8674. if ($this->radiobutton_groups[$n][$pl['txt']]['#readonly#']) {
  8675. // read only
  8676. $annots .= ' /F 68';
  8677. $annots .= ' /Ff 49153';
  8678. } else {
  8679. $annots .= ' /F 4'; // default print for PDF/A
  8680. $annots .= ' /Ff 49152';
  8681. }
  8682. $annots .= ' /T '.$this->_datastring($pl['txt'], $radio_button_obj_id);
  8683. $annots .= ' /FT /Btn';
  8684. $annots .= ' /Kids [';
  8685. foreach ($this->radiobutton_groups[$n][$pl['txt']] as $key => $data) {
  8686. if (isset($data['kid'])) {
  8687. $annots .= ' '.$data['kid'].' 0 R';
  8688. if ($data['def'] !== 'Off') {
  8689. $defval = $data['def'];
  8690. }
  8691. }
  8692. }
  8693. $annots .= ' ]';
  8694. if (isset($defval)) {
  8695. $annots .= ' /V /'.$defval;
  8696. }
  8697. $annots .= ' >>';
  8698. $this->_out($this->_getobj($radio_button_obj_id)."\n".$annots."\n".'endobj');
  8699. $this->form_obj_id[] = $radio_button_obj_id;
  8700. // store object id to be used on Parent entry of Kids
  8701. $this->radiobutton_groups[$n][$pl['txt']] = $radio_button_obj_id;
  8702. }
  8703. $formfield = false;
  8704. $pl['opt'] = array_change_key_case($pl['opt'], CASE_LOWER);
  8705. $a = $pl['x'] * $this->k;
  8706. $b = $this->pagedim[$n]['h'] - (($pl['y'] + $pl['h']) * $this->k);
  8707. $c = $pl['w'] * $this->k;
  8708. $d = $pl['h'] * $this->k;
  8709. $rect = sprintf('%.2F %.2F %.2F %.2F', $a, $b, $a+$c, $b+$d);
  8710. // create new annotation object
  8711. $annots = '<</Type /Annot';
  8712. $annots .= ' /Subtype /'.$pl['opt']['subtype'];
  8713. $annots .= ' /Rect ['.$rect.']';
  8714. $ft = array('Btn', 'Tx', 'Ch', 'Sig');
  8715. if (isset($pl['opt']['ft']) AND in_array($pl['opt']['ft'], $ft)) {
  8716. $annots .= ' /FT /'.$pl['opt']['ft'];
  8717. $formfield = true;
  8718. }
  8719. $annots .= ' /Contents '.$this->_textstring($pl['txt'], $annot_obj_id);
  8720. $annots .= ' /P '.$this->page_obj_id[$n].' 0 R';
  8721. $annots .= ' /NM '.$this->_datastring(sprintf('%04u-%04u', $n, $key), $annot_obj_id);
  8722. $annots .= ' /M '.$this->_datestring($annot_obj_id);
  8723. if (isset($pl['opt']['f'])) {
  8724. $fval = 0;
  8725. if (is_array($pl['opt']['f'])) {
  8726. foreach ($pl['opt']['f'] as $f) {
  8727. switch (strtolower($f)) {
  8728. case 'invisible': {
  8729. $fval += 1 << 0;
  8730. break;
  8731. }
  8732. case 'hidden': {
  8733. $fval += 1 << 1;
  8734. break;
  8735. }
  8736. case 'print': {
  8737. $fval += 1 << 2;
  8738. break;
  8739. }
  8740. case 'nozoom': {
  8741. $fval += 1 << 3;
  8742. break;
  8743. }
  8744. case 'norotate': {
  8745. $fval += 1 << 4;
  8746. break;
  8747. }
  8748. case 'noview': {
  8749. $fval += 1 << 5;
  8750. break;
  8751. }
  8752. case 'readonly': {
  8753. $fval += 1 << 6;
  8754. break;
  8755. }
  8756. case 'locked': {
  8757. $fval += 1 << 8;
  8758. break;
  8759. }
  8760. case 'togglenoview': {
  8761. $fval += 1 << 9;
  8762. break;
  8763. }
  8764. case 'lockedcontents': {
  8765. $fval += 1 << 10;
  8766. break;
  8767. }
  8768. default: {
  8769. break;
  8770. }
  8771. }
  8772. }
  8773. } else {
  8774. $fval = intval($pl['opt']['f']);
  8775. }
  8776. } else {
  8777. $fval = 4;
  8778. }
  8779. if ($this->pdfa_mode) {
  8780. // force print flag for PDF/A mode
  8781. $fval |= 4;
  8782. }
  8783. $annots .= ' /F '.intval($fval);
  8784. if (isset($pl['opt']['as']) AND is_string($pl['opt']['as'])) {
  8785. $annots .= ' /AS /'.$pl['opt']['as'];
  8786. }
  8787. if (isset($pl['opt']['ap'])) {
  8788. // appearance stream
  8789. $annots .= ' /AP <<';
  8790. if (is_array($pl['opt']['ap'])) {
  8791. foreach ($pl['opt']['ap'] as $apmode => $apdef) {
  8792. // $apmode can be: n = normal; r = rollover; d = down;
  8793. $annots .= ' /'.strtoupper($apmode);
  8794. if (is_array($apdef)) {
  8795. $annots .= ' <<';
  8796. foreach ($apdef as $apstate => $stream) {
  8797. // reference to XObject that define the appearance for this mode-state
  8798. $apsobjid = $this->_putAPXObject($c, $d, $stream);
  8799. $annots .= ' /'.$apstate.' '.$apsobjid.' 0 R';
  8800. }
  8801. $annots .= ' >>';
  8802. } else {
  8803. // reference to XObject that define the appearance for this mode
  8804. $apsobjid = $this->_putAPXObject($c, $d, $apdef);
  8805. $annots .= ' '.$apsobjid.' 0 R';
  8806. }
  8807. }
  8808. } else {
  8809. $annots .= $pl['opt']['ap'];
  8810. }
  8811. $annots .= ' >>';
  8812. }
  8813. if (isset($pl['opt']['bs']) AND (is_array($pl['opt']['bs']))) {
  8814. $annots .= ' /BS <<';
  8815. $annots .= ' /Type /Border';
  8816. if (isset($pl['opt']['bs']['w'])) {
  8817. $annots .= ' /W '.intval($pl['opt']['bs']['w']);
  8818. }
  8819. $bstyles = array('S', 'D', 'B', 'I', 'U');
  8820. if (isset($pl['opt']['bs']['s']) AND in_array($pl['opt']['bs']['s'], $bstyles)) {
  8821. $annots .= ' /S /'.$pl['opt']['bs']['s'];
  8822. }
  8823. if (isset($pl['opt']['bs']['d']) AND (is_array($pl['opt']['bs']['d']))) {
  8824. $annots .= ' /D [';
  8825. foreach ($pl['opt']['bs']['d'] as $cord) {
  8826. $annots .= ' '.intval($cord);
  8827. }
  8828. $annots .= ']';
  8829. }
  8830. $annots .= ' >>';
  8831. } else {
  8832. $annots .= ' /Border [';
  8833. if (isset($pl['opt']['border']) AND (count($pl['opt']['border']) >= 3)) {
  8834. $annots .= intval($pl['opt']['border'][0]).' ';
  8835. $annots .= intval($pl['opt']['border'][1]).' ';
  8836. $annots .= intval($pl['opt']['border'][2]);
  8837. if (isset($pl['opt']['border'][3]) AND is_array($pl['opt']['border'][3])) {
  8838. $annots .= ' [';
  8839. foreach ($pl['opt']['border'][3] as $dash) {
  8840. $annots .= intval($dash).' ';
  8841. }
  8842. $annots .= ']';
  8843. }
  8844. } else {
  8845. $annots .= '0 0 0';
  8846. }
  8847. $annots .= ']';
  8848. }
  8849. if (isset($pl['opt']['be']) AND (is_array($pl['opt']['be']))) {
  8850. $annots .= ' /BE <<';
  8851. $bstyles = array('S', 'C');
  8852. if (isset($pl['opt']['be']['s']) AND in_array($pl['opt']['be']['s'], $markups)) {
  8853. $annots .= ' /S /'.$pl['opt']['bs']['s'];
  8854. } else {
  8855. $annots .= ' /S /S';
  8856. }
  8857. if (isset($pl['opt']['be']['i']) AND ($pl['opt']['be']['i'] >= 0) AND ($pl['opt']['be']['i'] <= 2)) {
  8858. $annots .= ' /I '.sprintf(' %.4F', $pl['opt']['be']['i']);
  8859. }
  8860. $annots .= '>>';
  8861. }
  8862. if (isset($pl['opt']['c']) AND (is_array($pl['opt']['c'])) AND !empty($pl['opt']['c'])) {
  8863. $annots .= ' /C '.$this->getColorStringFromArray($pl['opt']['c']);
  8864. }
  8865. //$annots .= ' /StructParent ';
  8866. //$annots .= ' /OC ';
  8867. $markups = array('text', 'freetext', 'line', 'square', 'circle', 'polygon', 'polyline', 'highlight', 'underline', 'squiggly', 'strikeout', 'stamp', 'caret', 'ink', 'fileattachment', 'sound');
  8868. if (in_array(strtolower($pl['opt']['subtype']), $markups)) {
  8869. // this is a markup type
  8870. if (isset($pl['opt']['t']) AND is_string($pl['opt']['t'])) {
  8871. $annots .= ' /T '.$this->_textstring($pl['opt']['t'], $annot_obj_id);
  8872. }
  8873. //$annots .= ' /Popup ';
  8874. if (isset($pl['opt']['ca'])) {
  8875. $annots .= ' /CA '.sprintf('%.4F', floatval($pl['opt']['ca']));
  8876. }
  8877. if (isset($pl['opt']['rc'])) {
  8878. $annots .= ' /RC '.$this->_textstring($pl['opt']['rc'], $annot_obj_id);
  8879. }
  8880. $annots .= ' /CreationDate '.$this->_datestring($annot_obj_id);
  8881. //$annots .= ' /IRT ';
  8882. if (isset($pl['opt']['subj'])) {
  8883. $annots .= ' /Subj '.$this->_textstring($pl['opt']['subj'], $annot_obj_id);
  8884. }
  8885. //$annots .= ' /RT ';
  8886. //$annots .= ' /IT ';
  8887. //$annots .= ' /ExData ';
  8888. }
  8889. $lineendings = array('Square', 'Circle', 'Diamond', 'OpenArrow', 'ClosedArrow', 'None', 'Butt', 'ROpenArrow', 'RClosedArrow', 'Slash');
  8890. // Annotation types
  8891. switch (strtolower($pl['opt']['subtype'])) {
  8892. case 'text': {
  8893. if (isset($pl['opt']['open'])) {
  8894. $annots .= ' /Open '. (strtolower($pl['opt']['open']) == 'true' ? 'true' : 'false');
  8895. }
  8896. $iconsapp = array('Comment', 'Help', 'Insert', 'Key', 'NewParagraph', 'Note', 'Paragraph');
  8897. if (isset($pl['opt']['name']) AND in_array($pl['opt']['name'], $iconsapp)) {
  8898. $annots .= ' /Name /'.$pl['opt']['name'];
  8899. } else {
  8900. $annots .= ' /Name /Note';
  8901. }
  8902. $statemodels = array('Marked', 'Review');
  8903. if (isset($pl['opt']['statemodel']) AND in_array($pl['opt']['statemodel'], $statemodels)) {
  8904. $annots .= ' /StateModel /'.$pl['opt']['statemodel'];
  8905. } else {
  8906. $pl['opt']['statemodel'] = 'Marked';
  8907. $annots .= ' /StateModel /'.$pl['opt']['statemodel'];
  8908. }
  8909. if ($pl['opt']['statemodel'] == 'Marked') {
  8910. $states = array('Accepted', 'Unmarked');
  8911. } else {
  8912. $states = array('Accepted', 'Rejected', 'Cancelled', 'Completed', 'None');
  8913. }
  8914. if (isset($pl['opt']['state']) AND in_array($pl['opt']['state'], $states)) {
  8915. $annots .= ' /State /'.$pl['opt']['state'];
  8916. } else {
  8917. if ($pl['opt']['statemodel'] == 'Marked') {
  8918. $annots .= ' /State /Unmarked';
  8919. } else {
  8920. $annots .= ' /State /None';
  8921. }
  8922. }
  8923. break;
  8924. }
  8925. case 'link': {
  8926. if (is_string($pl['txt'])) {
  8927. // external URI link
  8928. $annots .= ' /A <</S /URI /URI '.$this->_datastring($this->unhtmlentities($pl['txt']), $annot_obj_id).'>>';
  8929. } else {
  8930. // internal link
  8931. $l = $this->links[$pl['txt']];
  8932. if (isset($this->page_obj_id[($l[0])])) {
  8933. $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)));
  8934. }
  8935. }
  8936. $hmodes = array('N', 'I', 'O', 'P');
  8937. if (isset($pl['opt']['h']) AND in_array($pl['opt']['h'], $hmodes)) {
  8938. $annots .= ' /H /'.$pl['opt']['h'];
  8939. } else {
  8940. $annots .= ' /H /I';
  8941. }
  8942. //$annots .= ' /PA ';
  8943. //$annots .= ' /Quadpoints ';
  8944. break;
  8945. }
  8946. case 'freetext': {
  8947. if (isset($pl['opt']['da']) AND !empty($pl['opt']['da'])) {
  8948. $annots .= ' /DA ('.$pl['opt']['da'].')';
  8949. }
  8950. if (isset($pl['opt']['q']) AND ($pl['opt']['q'] >= 0) AND ($pl['opt']['q'] <= 2)) {
  8951. $annots .= ' /Q '.intval($pl['opt']['q']);
  8952. }
  8953. if (isset($pl['opt']['rc'])) {
  8954. $annots .= ' /RC '.$this->_textstring($pl['opt']['rc'], $annot_obj_id);
  8955. }
  8956. if (isset($pl['opt']['ds'])) {
  8957. $annots .= ' /DS '.$this->_textstring($pl['opt']['ds'], $annot_obj_id);
  8958. }
  8959. if (isset($pl['opt']['cl']) AND is_array($pl['opt']['cl'])) {
  8960. $annots .= ' /CL [';
  8961. foreach ($pl['opt']['cl'] as $cl) {
  8962. $annots .= sprintf('%.4F ', $cl * $this->k);
  8963. }
  8964. $annots .= ']';
  8965. }
  8966. $tfit = array('FreeText', 'FreeTextCallout', 'FreeTextTypeWriter');
  8967. if (isset($pl['opt']['it']) AND in_array($pl['opt']['it'], $tfit)) {
  8968. $annots .= ' /IT /'.$pl['opt']['it'];
  8969. }
  8970. if (isset($pl['opt']['rd']) AND is_array($pl['opt']['rd'])) {
  8971. $l = $pl['opt']['rd'][0] * $this->k;
  8972. $r = $pl['opt']['rd'][1] * $this->k;
  8973. $t = $pl['opt']['rd'][2] * $this->k;
  8974. $b = $pl['opt']['rd'][3] * $this->k;
  8975. $annots .= ' /RD ['.sprintf('%.2F %.2F %.2F %.2F', $l, $r, $t, $b).']';
  8976. }
  8977. if (isset($pl['opt']['le']) AND in_array($pl['opt']['le'], $lineendings)) {
  8978. $annots .= ' /LE /'.$pl['opt']['le'];
  8979. }
  8980. break;
  8981. }
  8982. case 'line': {
  8983. break;
  8984. }
  8985. case 'square': {
  8986. break;
  8987. }
  8988. case 'circle': {
  8989. break;
  8990. }
  8991. case 'polygon': {
  8992. break;
  8993. }
  8994. case 'polyline': {
  8995. break;
  8996. }
  8997. case 'highlight': {
  8998. break;
  8999. }
  9000. case 'underline': {
  9001. break;
  9002. }
  9003. case 'squiggly': {
  9004. break;
  9005. }
  9006. case 'strikeout': {
  9007. break;
  9008. }
  9009. case 'stamp': {
  9010. break;
  9011. }
  9012. case 'caret': {
  9013. break;
  9014. }
  9015. case 'ink': {
  9016. break;
  9017. }
  9018. case 'popup': {
  9019. break;
  9020. }
  9021. case 'fileattachment': {
  9022. if ($this->pdfa_mode) {
  9023. // embedded files are not allowed in PDF/A mode
  9024. break;
  9025. }
  9026. if (!isset($pl['opt']['fs'])) {
  9027. break;
  9028. }
  9029. $filename = basename($pl['opt']['fs']);
  9030. if (isset($this->embeddedfiles[$filename]['n'])) {
  9031. $annots .= ' /FS <</Type /Filespec /F '.$this->_datastring($filename, $annot_obj_id).' /EF <</F '.$this->embeddedfiles[$filename]['n'].' 0 R>> >>';
  9032. $iconsapp = array('Graph', 'Paperclip', 'PushPin', 'Tag');
  9033. if (isset($pl['opt']['name']) AND in_array($pl['opt']['name'], $iconsapp)) {
  9034. $annots .= ' /Name /'.$pl['opt']['name'];
  9035. } else {
  9036. $annots .= ' /Name /PushPin';
  9037. }
  9038. }
  9039. break;
  9040. }
  9041. case 'sound': {
  9042. if (!isset($pl['opt']['fs'])) {
  9043. break;
  9044. }
  9045. $filename = basename($pl['opt']['fs']);
  9046. if (isset($this->embeddedfiles[$filename]['n'])) {
  9047. // ... TO BE COMPLETED ...
  9048. // /R /C /B /E /CO /CP
  9049. $annots .= ' /Sound <</Type /Filespec /F '.$this->_datastring($filename, $annot_obj_id).' /EF <</F '.$this->embeddedfiles[$filename]['n'].' 0 R>> >>';
  9050. $iconsapp = array('Speaker', 'Mic');
  9051. if (isset($pl['opt']['name']) AND in_array($pl['opt']['name'], $iconsapp)) {
  9052. $annots .= ' /Name /'.$pl['opt']['name'];
  9053. } else {
  9054. $annots .= ' /Name /Speaker';
  9055. }
  9056. }
  9057. break;
  9058. }
  9059. case 'movie': {
  9060. break;
  9061. }
  9062. case 'widget': {
  9063. $hmode = array('N', 'I', 'O', 'P', 'T');
  9064. if (isset($pl['opt']['h']) AND in_array($pl['opt']['h'], $hmode)) {
  9065. $annots .= ' /H /'.$pl['opt']['h'];
  9066. }
  9067. if (isset($pl['opt']['mk']) AND (is_array($pl['opt']['mk'])) AND !empty($pl['opt']['mk'])) {
  9068. $annots .= ' /MK <<';
  9069. if (isset($pl['opt']['mk']['r'])) {
  9070. $annots .= ' /R '.$pl['opt']['mk']['r'];
  9071. }
  9072. if (isset($pl['opt']['mk']['bc']) AND (is_array($pl['opt']['mk']['bc']))) {
  9073. $annots .= ' /BC '.$this->getColorStringFromArray($pl['opt']['mk']['bc']);
  9074. }
  9075. if (isset($pl['opt']['mk']['bg']) AND (is_array($pl['opt']['mk']['bg']))) {
  9076. $annots .= ' /BG '.$this->getColorStringFromArray($pl['opt']['mk']['bg']);
  9077. }
  9078. if (isset($pl['opt']['mk']['ca'])) {
  9079. $annots .= ' /CA '.$pl['opt']['mk']['ca'];
  9080. }
  9081. if (isset($pl['opt']['mk']['rc'])) {
  9082. $annots .= ' /RC '.$pl['opt']['mk']['rc'];
  9083. }
  9084. if (isset($pl['opt']['mk']['ac'])) {
  9085. $annots .= ' /AC '.$pl['opt']['mk']['ac'];
  9086. }
  9087. if (isset($pl['opt']['mk']['i'])) {
  9088. $info = $this->getImageBuffer($pl['opt']['mk']['i']);
  9089. if ($info !== false) {
  9090. $annots .= ' /I '.$info['n'].' 0 R';
  9091. }
  9092. }
  9093. if (isset($pl['opt']['mk']['ri'])) {
  9094. $info = $this->getImageBuffer($pl['opt']['mk']['ri']);
  9095. if ($info !== false) {
  9096. $annots .= ' /RI '.$info['n'].' 0 R';
  9097. }
  9098. }
  9099. if (isset($pl['opt']['mk']['ix'])) {
  9100. $info = $this->getImageBuffer($pl['opt']['mk']['ix']);
  9101. if ($info !== false) {
  9102. $annots .= ' /IX '.$info['n'].' 0 R';
  9103. }
  9104. }
  9105. if (isset($pl['opt']['mk']['if']) AND (is_array($pl['opt']['mk']['if'])) AND !empty($pl['opt']['mk']['if'])) {
  9106. $annots .= ' /IF <<';
  9107. $if_sw = array('A', 'B', 'S', 'N');
  9108. if (isset($pl['opt']['mk']['if']['sw']) AND in_array($pl['opt']['mk']['if']['sw'], $if_sw)) {
  9109. $annots .= ' /SW /'.$pl['opt']['mk']['if']['sw'];
  9110. }
  9111. $if_s = array('A', 'P');
  9112. if (isset($pl['opt']['mk']['if']['s']) AND in_array($pl['opt']['mk']['if']['s'], $if_s)) {
  9113. $annots .= ' /S /'.$pl['opt']['mk']['if']['s'];
  9114. }
  9115. if (isset($pl['opt']['mk']['if']['a']) AND (is_array($pl['opt']['mk']['if']['a'])) AND !empty($pl['opt']['mk']['if']['a'])) {
  9116. $annots .= sprintf(' /A [%.2F %.2F]', $pl['opt']['mk']['if']['a'][0], $pl['opt']['mk']['if']['a'][1]);
  9117. }
  9118. if (isset($pl['opt']['mk']['if']['fb']) AND ($pl['opt']['mk']['if']['fb'])) {
  9119. $annots .= ' /FB true';
  9120. }
  9121. $annots .= '>>';
  9122. }
  9123. if (isset($pl['opt']['mk']['tp']) AND ($pl['opt']['mk']['tp'] >= 0) AND ($pl['opt']['mk']['tp'] <= 6)) {
  9124. $annots .= ' /TP '.intval($pl['opt']['mk']['tp']);
  9125. }
  9126. $annots .= '>>';
  9127. } // end MK
  9128. // --- Entries for field dictionaries ---
  9129. if (isset($this->radiobutton_groups[$n][$pl['txt']])) {
  9130. // set parent
  9131. $annots .= ' /Parent '.$this->radiobutton_groups[$n][$pl['txt']].' 0 R';
  9132. }
  9133. if (isset($pl['opt']['t']) AND is_string($pl['opt']['t'])) {
  9134. $annots .= ' /T '.$this->_datastring($pl['opt']['t'], $annot_obj_id);
  9135. }
  9136. if (isset($pl['opt']['tu']) AND is_string($pl['opt']['tu'])) {
  9137. $annots .= ' /TU '.$this->_datastring($pl['opt']['tu'], $annot_obj_id);
  9138. }
  9139. if (isset($pl['opt']['tm']) AND is_string($pl['opt']['tm'])) {
  9140. $annots .= ' /TM '.$this->_datastring($pl['opt']['tm'], $annot_obj_id);
  9141. }
  9142. if (isset($pl['opt']['ff'])) {
  9143. if (is_array($pl['opt']['ff'])) {
  9144. // array of bit settings
  9145. $flag = 0;
  9146. foreach($pl['opt']['ff'] as $val) {
  9147. $flag += 1 << ($val - 1);
  9148. }
  9149. } else {
  9150. $flag = intval($pl['opt']['ff']);
  9151. }
  9152. $annots .= ' /Ff '.$flag;
  9153. }
  9154. if (isset($pl['opt']['maxlen'])) {
  9155. $annots .= ' /MaxLen '.intval($pl['opt']['maxlen']);
  9156. }
  9157. if (isset($pl['opt']['v'])) {
  9158. $annots .= ' /V';
  9159. if (is_array($pl['opt']['v'])) {
  9160. foreach ($pl['opt']['v'] 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']['v'], $annot_obj_id);
  9168. }
  9169. }
  9170. if (isset($pl['opt']['dv'])) {
  9171. $annots .= ' /DV';
  9172. if (is_array($pl['opt']['dv'])) {
  9173. foreach ($pl['opt']['dv'] 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']['dv'], $annot_obj_id);
  9181. }
  9182. }
  9183. if (isset($pl['opt']['rv'])) {
  9184. $annots .= ' /RV';
  9185. if (is_array($pl['opt']['rv'])) {
  9186. foreach ($pl['opt']['rv'] AS $optval) {
  9187. if (is_float($optval)) {
  9188. $optval = sprintf('%.2F', $optval);
  9189. }
  9190. $annots .= ' '.$optval;
  9191. }
  9192. } else {
  9193. $annots .= ' '.$this->_textstring($pl['opt']['rv'], $annot_obj_id);
  9194. }
  9195. }
  9196. if (isset($pl['opt']['a']) AND !empty($pl['opt']['a'])) {
  9197. $annots .= ' /A << '.$pl['opt']['a'].' >>';
  9198. }
  9199. if (isset($pl['opt']['aa']) AND !empty($pl['opt']['aa'])) {
  9200. $annots .= ' /AA << '.$pl['opt']['aa'].' >>';
  9201. }
  9202. if (isset($pl['opt']['da']) AND !empty($pl['opt']['da'])) {
  9203. $annots .= ' /DA ('.$pl['opt']['da'].')';
  9204. }
  9205. if (isset($pl['opt']['q']) AND ($pl['opt']['q'] >= 0) AND ($pl['opt']['q'] <= 2)) {
  9206. $annots .= ' /Q '.intval($pl['opt']['q']);
  9207. }
  9208. if (isset($pl['opt']['opt']) AND (is_array($pl['opt']['opt'])) AND !empty($pl['opt']['opt'])) {
  9209. $annots .= ' /Opt [';
  9210. foreach($pl['opt']['opt'] AS $copt) {
  9211. if (is_array($copt)) {
  9212. $annots .= ' ['.$this->_textstring($copt[0], $annot_obj_id).' '.$this->_textstring($copt[1], $annot_obj_id).']';
  9213. } else {
  9214. $annots .= ' '.$this->_textstring($copt, $annot_obj_id);
  9215. }
  9216. }
  9217. $annots .= ']';
  9218. }
  9219. if (isset($pl['opt']['ti'])) {
  9220. $annots .= ' /TI '.intval($pl['opt']['ti']);
  9221. }
  9222. if (isset($pl['opt']['i']) AND (is_array($pl['opt']['i'])) AND !empty($pl['opt']['i'])) {
  9223. $annots .= ' /I [';
  9224. foreach($pl['opt']['i'] AS $copt) {
  9225. $annots .= intval($copt).' ';
  9226. }
  9227. $annots .= ']';
  9228. }
  9229. break;
  9230. }
  9231. case 'screen': {
  9232. break;
  9233. }
  9234. case 'printermark': {
  9235. break;
  9236. }
  9237. case 'trapnet': {
  9238. break;
  9239. }
  9240. case 'watermark': {
  9241. break;
  9242. }
  9243. case '3d': {
  9244. break;
  9245. }
  9246. default: {
  9247. break;
  9248. }
  9249. }
  9250. $annots .= '>>';
  9251. // create new annotation object
  9252. $this->_out($this->_getobj($annot_obj_id)."\n".$annots."\n".'endobj');
  9253. if ($formfield AND !isset($this->radiobutton_groups[$n][$pl['txt']])) {
  9254. // store reference of form object
  9255. $this->form_obj_id[] = $annot_obj_id;
  9256. }
  9257. }
  9258. }
  9259. } // end for each page
  9260. }
  9261. /**
  9262. * Put appearance streams XObject used to define annotation's appearance states.
  9263. * @param $w (int) annotation width
  9264. * @param $h (int) annotation height
  9265. * @param $stream (string) appearance stream
  9266. * @return int object ID
  9267. * @protected
  9268. * @since 4.8.001 (2009-09-09)
  9269. */
  9270. protected function _putAPXObject($w=0, $h=0, $stream='') {
  9271. $stream = trim($stream);
  9272. $out = $this->_getobj()."\n";
  9273. $this->xobjects['AX'.$this->n] = array('n' => $this->n);
  9274. $out .= '<<';
  9275. $out .= ' /Type /XObject';
  9276. $out .= ' /Subtype /Form';
  9277. $out .= ' /FormType 1';
  9278. if ($this->compress) {
  9279. $stream = gzcompress($stream);
  9280. $out .= ' /Filter /FlateDecode';
  9281. }
  9282. $rect = sprintf('%.2F %.2F', $w, $h);
  9283. $out .= ' /BBox [0 0 '.$rect.']';
  9284. $out .= ' /Matrix [1 0 0 1 0 0]';
  9285. $out .= ' /Resources 2 0 R';
  9286. $stream = $this->_getrawstream($stream);
  9287. $out .= ' /Length '.strlen($stream);
  9288. $out .= ' >>';
  9289. $out .= ' stream'."\n".$stream."\n".'endstream';
  9290. $out .= "\n".'endobj';
  9291. $this->_out($out);
  9292. return $this->n;
  9293. }
  9294. /**
  9295. * Get ULONG from string (Big Endian 32-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 32 bit value
  9299. * @author Nicola Asuni
  9300. * @protected
  9301. * @since 5.2.000 (2010-06-02)
  9302. */
  9303. protected function _getULONG($str, $offset) {
  9304. $v = unpack('Ni', substr($str, $offset, 4));
  9305. return $v['i'];
  9306. }
  9307. /**
  9308. * Get USHORT from string (Big Endian 16-bit unsigned 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 _getUSHORT($str, $offset) {
  9317. $v = unpack('ni', substr($str, $offset, 2));
  9318. return $v['i'];
  9319. }
  9320. /**
  9321. * Get SHORT 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.2.000 (2010-06-02)
  9328. */
  9329. protected function _getSHORT($str, $offset) {
  9330. $v = unpack('si', substr($str, $offset, 2));
  9331. return $v['i'];
  9332. }
  9333. /**
  9334. * Get FWORD from string (Big Endian 16-bit signed integer).
  9335. * @param $str (string) string from where to extract value
  9336. * @param $offset (int) point from where to read the data
  9337. * @return int 16 bit value
  9338. * @author Nicola Asuni
  9339. * @protected
  9340. * @since 5.9.123 (2011-09-30)
  9341. */
  9342. protected function _getFWORD($str, $offset) {
  9343. $v = $this->_getUSHORT($str, $offset);
  9344. if ($v > 0x7fff) {
  9345. $v -= 0x10000;
  9346. }
  9347. return $v;
  9348. }
  9349. /**
  9350. * Get UFWORD from string (Big Endian 16-bit unsigned integer).
  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 _getUFWORD($str, $offset) {
  9359. $v = $this->_getUSHORT($str, $offset);
  9360. return $v;
  9361. }
  9362. /**
  9363. * Get FIXED from string (32-bit signed fixed-point number (16.16).
  9364. * @param $str (string) string from where to extract value
  9365. * @param $offset (int) point from where to read the data
  9366. * @return int 16 bit value
  9367. * @author Nicola Asuni
  9368. * @protected
  9369. * @since 5.9.123 (2011-09-30)
  9370. */
  9371. protected function _getFIXED($str, $offset) {
  9372. // mantissa
  9373. $m = $this->_getFWORD($str, $offset);
  9374. // fraction
  9375. $f = $this->_getUSHORT($str, ($offset + 2));
  9376. $v = floatval(''.$m.'.'.$f.'');
  9377. return $v;
  9378. }
  9379. /**
  9380. * Get BYTE from string (8-bit unsigned integer).
  9381. * @param $str (string) String from where to extract value.
  9382. * @param $offset (int) Point from where to read the data.
  9383. * @return int 8 bit value
  9384. * @author Nicola Asuni
  9385. * @protected
  9386. * @since 5.2.000 (2010-06-02)
  9387. */
  9388. protected function _getBYTE($str, $offset) {
  9389. $v = unpack('Ci', substr($str, $offset, 1));
  9390. return $v['i'];
  9391. }
  9392. /**
  9393. * Update the CIDToGIDMap string with a new value.
  9394. * @param $map (string) CIDToGIDMap.
  9395. * @param $cid (int) CID value.
  9396. * @param $gid (int) GID value.
  9397. * @return (string) CIDToGIDMap.
  9398. * @author Nicola Asuni
  9399. * @protected
  9400. * @since 5.9.123 (2011-09-29)
  9401. */
  9402. protected function updateCIDtoGIDmap($map, $cid, $gid) {
  9403. if (($cid >= 0) AND ($cid <= 0xFFFF) AND ($gid >= 0)) {
  9404. if ($gid > 0xFFFF) {
  9405. $gid -= 0x10000;
  9406. }
  9407. $map[($cid * 2)] = chr($gid >> 8);
  9408. $map[(($cid * 2) + 1)] = chr($gid & 0xFF);
  9409. }
  9410. return $map;
  9411. }
  9412. /**
  9413. * Convert and add the selected TrueType or Type1 font to the fonts folder (that must be writeable).
  9414. * @param $fontfile (string) TrueType or Type1 font file (full path).
  9415. * @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.
  9416. * @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.
  9417. * @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.
  9418. * @param $outpath (string) Output path for generated font files (must be writeable by the web server). Leave empty for default font folder.
  9419. * @return (string) TCPDF font name.
  9420. * @author Nicola Asuni
  9421. * @public
  9422. * @since 5.9.123 (2010-09-30)
  9423. */
  9424. public function addTTFfont($fontfile, $fonttype='', $enc='', $flags=32, $outpath='') {
  9425. if (!file_exists($fontfile)) {
  9426. $this->Error('Could not find file: '.$fontfile.'');
  9427. }
  9428. // font metrics
  9429. $fmetric = array();
  9430. // build new font name for TCPDF compatibility
  9431. $font_path_parts = pathinfo($fontfile);
  9432. if (!isset($font_path_parts['filename'])) {
  9433. $font_path_parts['filename'] = substr($font_path_parts['basename'], 0, -(strlen($font_path_parts['extension']) + 1));
  9434. }
  9435. $font_name = strtolower($font_path_parts['filename']);
  9436. $font_name = preg_replace('/[^a-z0-9_]/', '', $font_name);
  9437. $search = array('bold', 'oblique', 'italic', 'regular');
  9438. $replace = array('b', 'i', 'i', '');
  9439. $font_name = str_replace($search, $replace, $font_name);
  9440. if (empty($font_name)) {
  9441. // set generic name
  9442. $font_name = 'tcpdffont';
  9443. }
  9444. // set output path
  9445. if (empty($outpath)) {
  9446. $outpath = $this->_getfontpath();
  9447. }
  9448. // check if this font already exist
  9449. if (file_exists($outpath.$font_name.'.php')) {
  9450. // this font already exist (delete it from fonts folder to rebuild it)
  9451. return $font_name;
  9452. }
  9453. $fmetric['file'] = $font_name.'.z';
  9454. $fmetric['ctg'] = $font_name.'.ctg.z';
  9455. // get font data
  9456. $font = file_get_contents($fontfile);
  9457. $fmetric['originalsize'] = strlen($font);
  9458. // autodetect font type
  9459. if (empty($fonttype)) {
  9460. if ($this->_getULONG($font, 0) == 0x10000) {
  9461. // True Type (Unicode or not)
  9462. $fonttype = 'TrueTypeUnicode';
  9463. } elseif (substr($font, 0, 4) == 'OTTO') {
  9464. // Open Type (Unicode or not)
  9465. $this->Error('Unsupported font format: OpenType with CFF data.');
  9466. } else {
  9467. // Type 1
  9468. $fonttype = 'Type1';
  9469. }
  9470. }
  9471. // set font type
  9472. switch ($fonttype) {
  9473. case 'CID0CT':
  9474. case 'CID0CS':
  9475. case 'CID0KR':
  9476. case 'CID0JP': {
  9477. $fmetric['type'] = 'cidfont0';
  9478. break;
  9479. }
  9480. case 'Type1': {
  9481. $fmetric['type'] = 'Type1';
  9482. if (empty($enc) AND (($flags & 4) == 0)) {
  9483. $enc = 'cp1252';
  9484. }
  9485. break;
  9486. }
  9487. case 'TrueType': {
  9488. $fmetric['type'] = 'TrueType';
  9489. break;
  9490. }
  9491. case 'TrueTypeUnicode':
  9492. default: {
  9493. $fmetric['type'] = 'TrueTypeUnicode';
  9494. break;
  9495. }
  9496. }
  9497. // set encoding maps (if any)
  9498. $fmetric['enc'] = preg_replace('/[^A-Za-z0-9_\-]/', '', $enc);
  9499. $fmetric['diff'] = '';
  9500. if (($fmetric['type'] == 'TrueType') OR ($fmetric['type'] == 'Type1')) {
  9501. if (!empty($enc) AND ($enc != 'cp1252') AND isset($this->encmaps->encmap[$enc])) {
  9502. // build differences from reference encoding
  9503. $enc_ref = $this->encmaps->encmap['cp1252'];
  9504. $enc_target = $this->encmaps->encmap[$enc];
  9505. $last = 0;
  9506. for ($i = 32; $i <= 255; ++$i) {
  9507. if ($enc_target != $enc_ref[$i]) {
  9508. if ($i != ($last + 1)) {
  9509. $fmetric['diff'] .= $i.' ';
  9510. }
  9511. $last = $i;
  9512. $fmetric['diff'] .= '/'.$enc_target[$i].' ';
  9513. }
  9514. }
  9515. }
  9516. }
  9517. // parse the font by type
  9518. if ($fmetric['type'] == 'Type1') {
  9519. // ---------- TYPE 1 ----------
  9520. // read first segment
  9521. $a = unpack('Cmarker/Ctype/Vsize', substr($font, 0, 6));
  9522. if ($a['marker'] != 128) {
  9523. $this->Error('Font file is not a valid binary Type1');
  9524. }
  9525. $fmetric['size1'] = $a['size'];
  9526. $data = substr($font, 6, $fmetric['size1']);
  9527. // read second segment
  9528. $a = unpack('Cmarker/Ctype/Vsize', substr($font, (6 + $fmetric['size1']), 6));
  9529. if ($a['marker'] != 128) {
  9530. $this->Error('Font file is not a valid binary Type1');
  9531. }
  9532. $fmetric['size2'] = $a['size'];
  9533. $encrypted = substr($font, (12 + $fmetric['size1']), $fmetric['size2']);
  9534. $data .= $encrypted;
  9535. // store compressed font
  9536. $fp = fopen($outpath.$fmetric['file'], 'wb');
  9537. fwrite($fp, gzcompress($data));
  9538. fclose($fp);
  9539. // get font info
  9540. $fmetric['Flags'] = $flags;
  9541. preg_match ('#/FullName[\s]*\(([^\)]*)#', $font, $matches);
  9542. $fmetric['name'] = preg_replace('/[^a-zA-Z0-9_\-]/', '', $matches[1]);
  9543. preg_match('#/FontBBox[\s]*{([^}]*)#', $font, $matches);
  9544. $fmetric['bbox'] = trim($matches[1]);
  9545. $bv = explode(' ', $fmetric['bbox']);
  9546. $fmetric['Ascent'] = intval($bv[3]);
  9547. $fmetric['Descent'] = intval($bv[1]);
  9548. preg_match('#/ItalicAngle[\s]*([0-9\+\-]*)#', $font, $matches);
  9549. $fmetric['italicAngle'] = intval($matches[1]);
  9550. if ($fmetric['italicAngle'] != 0) {
  9551. $fmetric['Flags'] |= 64;
  9552. }
  9553. preg_match('#/UnderlinePosition[\s]*([0-9\+\-]*)#', $font, $matches);
  9554. $fmetric['underlinePosition'] = intval($matches[1]);
  9555. preg_match('#/UnderlineThickness[\s]*([0-9\+\-]*)#', $font, $matches);
  9556. $fmetric['underlineThickness'] = intval($matches[1]);
  9557. preg_match('#/isFixedPitch[\s]*([^\s]*)#', $font, $matches);
  9558. if ($matches[1] == 'true') {
  9559. $fmetric['Flags'] |= 1;
  9560. }
  9561. // get internal map
  9562. $imap = array();
  9563. if (preg_match_all('#dup[\s]([0-9]+)[\s]*/([^\s]*)[\s]put#sU', $font, $fmap, PREG_SET_ORDER) > 0) {
  9564. foreach ($fmap as $v) {
  9565. $imap[$v[2]] = $v[1];
  9566. }
  9567. }
  9568. // decrypt eexec encrypted part
  9569. $r = 55665; // eexec encryption constant
  9570. $c1 = 52845;
  9571. $c2 = 22719;
  9572. $elen = strlen($encrypted);
  9573. $eplain = '';
  9574. for ($i = 0; $i < $elen; ++$i) {
  9575. $chr = ord($encrypted[$i]);
  9576. $eplain .= chr($chr ^ ($r >> 8));
  9577. $r = ((($chr + $r) * $c1 + $c2) % 65536);
  9578. }
  9579. if (preg_match('#/ForceBold[\s]*([^\s]*)#', $eplain, $matches) > 0) {
  9580. if ($matches[1] == 'true') {
  9581. $fmetric['Flags'] |= 0x40000;
  9582. }
  9583. }
  9584. if (preg_match('#/StdVW[\s]*\[([^\]]*)#', $eplain, $matches) > 0) {
  9585. $fmetric['StemV'] = intval($matches[1]);
  9586. } else {
  9587. $fmetric['StemV'] = 70;
  9588. }
  9589. if (preg_match('#/StdHW[\s]*\[([^\]]*)#', $eplain, $matches) > 0) {
  9590. $fmetric['StemH'] = intval($matches[1]);
  9591. } else {
  9592. $fmetric['StemH'] = 30;
  9593. }
  9594. if (preg_match('#/BlueValues[\s]*\[([^\]]*)#', $eplain, $matches) > 0) {
  9595. $bv = explode(' ', $matches[1]);
  9596. if (count($bv) >= 6) {
  9597. $v1 = intval($bv[2]);
  9598. $v2 = intval($bv[4]);
  9599. if ($v1 <= $v2) {
  9600. $fmetric['XHeight'] = $v1;
  9601. $fmetric['CapHeight'] = $v2;
  9602. } else {
  9603. $fmetric['XHeight'] = $v2;
  9604. $fmetric['CapHeight'] = $v1;
  9605. }
  9606. } else {
  9607. $fmetric['XHeight'] = 450;
  9608. $fmetric['CapHeight'] = 700;
  9609. }
  9610. } else {
  9611. $fmetric['XHeight'] = 450;
  9612. $fmetric['CapHeight'] = 700;
  9613. }
  9614. // get the number of random bytes at the beginning of charstrings
  9615. if (preg_match('#/lenIV[\s]*([0-9]*)#', $eplain, $matches) > 0) {
  9616. $lenIV = intval($matches[1]);
  9617. } else {
  9618. $lenIV = 4;
  9619. }
  9620. $fmetric['Leading'] = 0;
  9621. // get charstring data
  9622. $eplain = substr($eplain, (strpos($eplain, '/CharStrings') + 1));
  9623. preg_match_all('#/([A-Za-z0-9\.]*)[\s][0-9]+[\s]RD[\s](.*)[\s]ND#sU', $eplain, $matches, PREG_SET_ORDER);
  9624. if (!empty($enc) AND isset($this->encmaps->encmap[$enc])) {
  9625. $enc_map = $this->encmaps->encmap[$enc];
  9626. } else {
  9627. $enc_map = false;
  9628. }
  9629. $fmetric['cw'] = '';
  9630. $fmetric['MaxWidth'] = 0;
  9631. $cwidths = array();
  9632. foreach ($matches as $k => $v) {
  9633. $cid = 0;
  9634. if (isset($imap[$v[1]])) {
  9635. $cid = $imap[$v[1]];
  9636. } elseif ($enc_map !== false) {
  9637. $cid = array_search($v[1], $enc_map);
  9638. if ($cid === false) {
  9639. $cid = 0;
  9640. } elseif ($cid > 1000) {
  9641. $cid -= 1000;
  9642. }
  9643. }
  9644. // decrypt charstring encrypted part
  9645. $r = 4330; // charstring encryption constant
  9646. $c1 = 52845;
  9647. $c2 = 22719;
  9648. $cd = $v[2];
  9649. $clen = strlen($cd);
  9650. $ccom = array();
  9651. for ($i = 0; $i < $clen; ++$i) {
  9652. $chr = ord($cd[$i]);
  9653. $ccom[] = ($chr ^ ($r >> 8));
  9654. $r = ((($chr + $r) * $c1 + $c2) % 65536);
  9655. }
  9656. // decode numbers
  9657. $cdec = array();
  9658. $ck = 0;
  9659. $i = $lenIV;
  9660. while ($i < $clen) {
  9661. if ($ccom[$i] < 32) {
  9662. $cdec[$ck] = $ccom[$i];
  9663. if (($ck > 0) AND ($cdec[$ck] == 13)) {
  9664. // hsbw command: update width
  9665. $cwidths[$cid] = $cdec[($ck - 1)];
  9666. }
  9667. ++$i;
  9668. } elseif (($ccom[$i] >= 32) AND ($ccom[$i] <= 246)) {
  9669. $cdec[$ck] = ($ccom[$i] - 139);
  9670. ++$i;
  9671. } elseif (($ccom[$i] >= 247) AND ($ccom[$i] <= 250)) {
  9672. $cdec[$ck] = ((($ccom[$i] - 247) * 256) + $ccom[($i + 1)] + 108);
  9673. $i += 2;
  9674. } elseif (($ccom[$i] >= 251) AND ($ccom[$i] <= 254)) {
  9675. $cdec[$ck] = ((-($ccom[$i] - 251) * 256) - $ccom[($i + 1)] - 108);
  9676. $i += 2;
  9677. } elseif ($ccom[$i] == 255) {
  9678. $sval = chr($ccom[($i + 1)]).chr($ccom[($i + 2)]).chr($ccom[($i + 3)]).chr($ccom[($i + 4)]);
  9679. $vsval = unpack('li', $sval);
  9680. $cdec[$ck] = $vsval['i'];
  9681. $i += 5;
  9682. }
  9683. ++$ck;
  9684. }
  9685. } // end for each matches
  9686. $fmetric['MissingWidth'] = $cwidths[0];
  9687. $fmetric['MaxWidth'] = $fmetric['MissingWidth'];
  9688. $fmetric['AvgWidth'] = 0;
  9689. // set chars widths
  9690. for ($cid = 0; $cid <= 255; ++$cid) {
  9691. if (isset($cwidths[$cid])) {
  9692. if ($cwidths[$cid] > $fmetric['MaxWidth']) {
  9693. $fmetric['MaxWidth'] = $cwidths[$cid];
  9694. }
  9695. $fmetric['AvgWidth'] += $cwidths[$cid];
  9696. $fmetric['cw'] .= ','.$cid.'=>'.$cwidths[$cid];
  9697. } else {
  9698. $fmetric['cw'] .= ','.$cid.'=>'.$fmetric['MissingWidth'];
  9699. }
  9700. }
  9701. $fmetric['AvgWidth'] = round($fmetric['AvgWidth'] / count($cwidths));
  9702. } else {
  9703. // ---------- TRUE TYPE ----------
  9704. if ($fmetric['type'] != 'cidfont0') {
  9705. // store compressed font
  9706. $fp = fopen($outpath.$fmetric['file'], 'wb');
  9707. fwrite($fp, gzcompress($font));
  9708. fclose($fp);
  9709. }
  9710. $offset = 0; // offset position of the font data
  9711. if ($this->_getULONG($font, $offset) != 0x10000) {
  9712. // sfnt version must be 0x00010000 for TrueType version 1.0.
  9713. return $font;
  9714. }
  9715. $offset += 4;
  9716. // get number of tables
  9717. $numTables = $this->_getUSHORT($font, $offset);
  9718. $offset += 2;
  9719. // skip searchRange, entrySelector and rangeShift
  9720. $offset += 6;
  9721. // tables array
  9722. $table = array();
  9723. // ---------- get tables ----------
  9724. for ($i = 0; $i < $numTables; ++$i) {
  9725. // get table info
  9726. $tag = substr($font, $offset, 4);
  9727. $offset += 4;
  9728. $table[$tag] = array();
  9729. $table[$tag]['checkSum'] = $this->_getULONG($font, $offset);
  9730. $offset += 4;
  9731. $table[$tag]['offset'] = $this->_getULONG($font, $offset);
  9732. $offset += 4;
  9733. $table[$tag]['length'] = $this->_getULONG($font, $offset);
  9734. $offset += 4;
  9735. }
  9736. // check magicNumber
  9737. $offset = $table['head']['offset'] + 12;
  9738. if ($this->_getULONG($font, $offset) != 0x5F0F3CF5) {
  9739. // magicNumber must be 0x5F0F3CF5
  9740. return $font;
  9741. }
  9742. $offset += 4;
  9743. $offset += 2; // skip flags
  9744. // get FUnits
  9745. $fmetric['unitsPerEm'] = $this->_getUSHORT($font, $offset);
  9746. $offset += 2;
  9747. // units ratio constant
  9748. $urk = (1000 / $fmetric['unitsPerEm']);
  9749. $offset += 16; // skip created, modified
  9750. $xMin = round($this->_getFWORD($font, $offset) * $urk);
  9751. $offset += 2;
  9752. $yMin = round($this->_getFWORD($font, $offset) * $urk);
  9753. $offset += 2;
  9754. $xMax = round($this->_getFWORD($font, $offset) * $urk);
  9755. $offset += 2;
  9756. $yMax = round($this->_getFWORD($font, $offset) * $urk);
  9757. $offset += 2;
  9758. $fmetric['bbox'] = ''.$xMin.' '.$yMin.' '.$xMax.' '.$yMax.'';
  9759. $macStyle = $this->_getUSHORT($font, $offset);
  9760. $offset += 2;
  9761. // PDF font flags
  9762. $fmetric['Flags'] = $flags;
  9763. if (($macStyle & 2) == 2) {
  9764. // italic flag
  9765. $fmetric['Flags'] |= 64;
  9766. }
  9767. // get offset mode (indexToLocFormat : 0 = short, 1 = long)
  9768. $offset = $table['head']['offset'] + 50;
  9769. $short_offset = ($this->_getSHORT($font, $offset) == 0);
  9770. $offset += 2;
  9771. // get the offsets to the locations of the glyphs in the font, relative to the beginning of the glyphData table
  9772. $indexToLoc = array();
  9773. $offset = $table['loca']['offset'];
  9774. if ($short_offset) {
  9775. // short version
  9776. $tot_num_glyphs = ($table['loca']['length'] / 2); // numGlyphs + 1
  9777. for ($i = 0; $i < $tot_num_glyphs; ++$i) {
  9778. $indexToLoc[$i] = $this->_getUSHORT($font, $offset) * 2;
  9779. $offset += 2;
  9780. }
  9781. } else {
  9782. // long version
  9783. $tot_num_glyphs = ($table['loca']['length'] / 4); // numGlyphs + 1
  9784. for ($i = 0; $i < $tot_num_glyphs; ++$i) {
  9785. $indexToLoc[$i] = $this->_getULONG($font, $offset);
  9786. $offset += 4;
  9787. }
  9788. }
  9789. // get glyphs indexes of chars from cmap table
  9790. $offset = $table['cmap']['offset'] + 2;
  9791. $numEncodingTables = $this->_getUSHORT($font, $offset);
  9792. $offset += 2;
  9793. $encodingTables = array();
  9794. for ($i = 0; $i < $numEncodingTables; ++$i) {
  9795. $encodingTables[$i]['platformID'] = $this->_getUSHORT($font, $offset);
  9796. $offset += 2;
  9797. $encodingTables[$i]['encodingID'] = $this->_getUSHORT($font, $offset);
  9798. $offset += 2;
  9799. $encodingTables[$i]['offset'] = $this->_getULONG($font, $offset);
  9800. $offset += 4;
  9801. }
  9802. // ---------- get os/2 metrics ----------
  9803. $offset = $table['OS/2']['offset'];
  9804. $offset += 2; // skip version
  9805. // xAvgCharWidth
  9806. $fmetric['AvgWidth'] = round($this->_getFWORD($font, $offset) * $urk);
  9807. $offset += 2;
  9808. // usWeightClass
  9809. $usWeightClass = round($this->_getUFWORD($font, $offset) * $urk);
  9810. // estimate StemV and StemH (400 = usWeightClass for Normal - Regular font)
  9811. $fmetric['StemV'] = round((70 * $usWeightClass) / 400);
  9812. $fmetric['StemH'] = round((30 * $usWeightClass) / 400);
  9813. $offset += 2;
  9814. $offset += 2; // usWidthClass
  9815. $fsType = $this->_getSHORT($font, $offset);
  9816. $offset += 2;
  9817. if ($fsType == 2) {
  9818. $this->Error('This Font cannot be modified, embedded or exchanged in any manner without first obtaining permission of the legal owner.');
  9819. }
  9820. // ---------- get font name ----------
  9821. $fmetric['name'] = '';
  9822. $offset = $table['name']['offset'];
  9823. $offset += 2; // skip Format selector (=0).
  9824. // Number of NameRecords that follow n.
  9825. $numNameRecords = $this->_getUSHORT($font, $offset);
  9826. $offset += 2;
  9827. // Offset to start of string storage (from start of table).
  9828. $stringStorageOffset = $this->_getUSHORT($font, $offset);
  9829. $offset += 2;
  9830. for ($i = 0; $i < $numNameRecords; ++$i) {
  9831. $offset += 6; // skip Platform ID, Platform-specific encoding ID, Language ID.
  9832. // Name ID.
  9833. $nameID = $this->_getUSHORT($font, $offset);
  9834. $offset += 2;
  9835. if ($nameID == 6) {
  9836. // String length (in bytes).
  9837. $stringLength = $this->_getUSHORT($font, $offset);
  9838. $offset += 2;
  9839. // String offset from start of storage area (in bytes).
  9840. $stringOffset = $this->_getUSHORT($font, $offset);
  9841. $offset += 2;
  9842. $offset = ($table['name']['offset'] + $stringStorageOffset + $stringOffset);
  9843. $fmetric['name'] = substr($font, $offset, $stringLength);
  9844. $fmetric['name'] = preg_replace('/[^a-zA-Z0-9_\-]/', '', $fmetric['name']);
  9845. break;
  9846. } else {
  9847. $offset += 4; // skip String length, String offset
  9848. }
  9849. }
  9850. if (empty($fmetric['name'])) {
  9851. $fmetric['name'] = $font_name;
  9852. }
  9853. // ---------- get post data ----------
  9854. $offset = $table['post']['offset'];
  9855. $offset += 4; // skip Format Type
  9856. $fmetric['italicAngle'] = $this->_getFIXED($font, $offset);
  9857. $offset += 4;
  9858. $fmetric['underlinePosition'] = round($this->_getFWORD($font, $offset) * $urk);
  9859. $offset += 2;
  9860. $fmetric['underlineThickness'] = round($this->_getFWORD($font, $offset) * $urk);
  9861. $offset += 2;
  9862. $isFixedPitch = ($this->_getULONG($font, $offset) == 0) ? false : true;
  9863. $offset += 2;
  9864. if ($isFixedPitch) {
  9865. $fmetric['Flags'] |= 1;
  9866. }
  9867. // ---------- get hhea data ----------
  9868. $offset = $table['hhea']['offset'];
  9869. $offset += 4; // skip Table version number
  9870. // Ascender
  9871. $fmetric['Ascent'] = round($this->_getFWORD($font, $offset) * $urk);
  9872. $offset += 2;
  9873. // Descender
  9874. $fmetric['Descent'] = round($this->_getFWORD($font, $offset) * $urk);
  9875. $offset += 2;
  9876. // LineGap
  9877. $fmetric['Leading'] = round($this->_getFWORD($font, $offset) * $urk);
  9878. $offset += 2;
  9879. // advanceWidthMax
  9880. $fmetric['MaxWidth'] = round($this->_getUFWORD($font, $offset) * $urk);
  9881. $offset += 2;
  9882. $offset += 22; // skip some values
  9883. // get the number of hMetric entries in hmtx table
  9884. $numberOfHMetrics = $this->_getUSHORT($font, $offset);
  9885. // ---------- get maxp data ----------
  9886. $offset = $table['maxp']['offset'];
  9887. $offset += 4; // skip Table version number
  9888. // get the the number of glyphs in the font.
  9889. $numGlyphs = $this->_getUSHORT($font, $offset);
  9890. // ---------- get CIDToGIDMap ----------
  9891. $ctg = array();
  9892. foreach ($encodingTables as $enctable) {
  9893. if (($enctable['platformID'] == 3) AND ($enctable['encodingID'] == 0)) {
  9894. $modesymbol = true;
  9895. } else {
  9896. $modesymbol = false;
  9897. }
  9898. $offset = $table['cmap']['offset'] + $enctable['offset'];
  9899. $format = $this->_getUSHORT($font, $offset);
  9900. $offset += 2;
  9901. switch ($format) {
  9902. case 0: { // Format 0: Byte encoding table
  9903. $offset += 4; // skip length and version/language
  9904. for ($c = 0; $c < 256; ++$c) {
  9905. $g = $this->_getBYTE($font, $offset);
  9906. $ctg[$c] = $g;
  9907. ++$offset;
  9908. }
  9909. break;
  9910. }
  9911. case 2: { // Format 2: High-byte mapping through table
  9912. $offset += 4; // skip length and version/language
  9913. $numSubHeaders = 0;
  9914. for ($i = 0; $i < 256; ++$i) {
  9915. // Array that maps high bytes to subHeaders: value is subHeader index * 8.
  9916. $subHeaderKeys[$i] = ($this->_getUSHORT($font, $offset) / 8);
  9917. $offset += 2;
  9918. if ($numSubHeaders < $subHeaderKeys[$i]) {
  9919. $numSubHeaders = $subHeaderKeys[$i];
  9920. }
  9921. }
  9922. // the number of subHeaders is equal to the max of subHeaderKeys + 1
  9923. ++$numSubHeaders;
  9924. // read subHeader structures
  9925. $subHeaders = array();
  9926. $numGlyphIndexArray = 0;
  9927. for ($k = 0; $k < $numSubHeaders; ++$k) {
  9928. $subHeaders[$k]['firstCode'] = $this->_getUSHORT($font, $offset);
  9929. $offset += 2;
  9930. $subHeaders[$k]['entryCount'] = $this->_getUSHORT($font, $offset);
  9931. $offset += 2;
  9932. $subHeaders[$k]['idDelta'] = $this->_getSHORT($font, $offset);
  9933. $offset += 2;
  9934. $subHeaders[$k]['idRangeOffset'] = $this->_getUSHORT($font, $offset);
  9935. $offset += 2;
  9936. $subHeaders[$k]['idRangeOffset'] -= (2 + (($numSubHeaders - $k - 1) * 8));
  9937. $subHeaders[$k]['idRangeOffset'] /= 2;
  9938. $numGlyphIndexArray += $subHeaders[$k]['entryCount'];
  9939. }
  9940. for ($k = 0; $k < $numGlyphIndexArray; ++$k) {
  9941. $glyphIndexArray[$k] = $this->_getUSHORT($font, $offset);
  9942. $offset += 2;
  9943. }
  9944. for ($i = 0; $i < 256; ++$i) {
  9945. $k = $subHeaderKeys[$i];
  9946. if ($k == 0) {
  9947. // one byte code
  9948. $c = $i;
  9949. $g = $glyphIndexArray[0];
  9950. $ctg[$c] = $g;
  9951. } else {
  9952. // two bytes code
  9953. $start_byte = $subHeaders[$k]['firstCode'];
  9954. $end_byte = $start_byte + $subHeaders[$k]['entryCount'];
  9955. for ($j = $start_byte; $j < $end_byte; ++$j) {
  9956. // combine high and low bytes
  9957. $c = (($i << 8) + $j);
  9958. $idRangeOffset = ($subHeaders[$k]['idRangeOffset'] + $j - $subHeaders[$k]['firstCode']);
  9959. $g = $glyphIndexArray[$idRangeOffset];
  9960. $g += ($idDelta[$k] - 65536);
  9961. if ($g < 0) {
  9962. $g = 0;
  9963. }
  9964. $ctg[$c] = $g;
  9965. }
  9966. }
  9967. }
  9968. break;
  9969. }
  9970. case 4: { // Format 4: Segment mapping to delta values
  9971. $length = $this->_getUSHORT($font, $offset);
  9972. $offset += 2;
  9973. $offset += 2; // skip version/language
  9974. $segCount = ($this->_getUSHORT($font, $offset) / 2);
  9975. $offset += 2;
  9976. $offset += 6; // skip searchRange, entrySelector, rangeShift
  9977. $endCount = array(); // array of end character codes for each segment
  9978. for ($k = 0; $k < $segCount; ++$k) {
  9979. $endCount[$k] = $this->_getUSHORT($font, $offset);
  9980. $offset += 2;
  9981. }
  9982. $offset += 2; // skip reservedPad
  9983. $startCount = array(); // array of start character codes for each segment
  9984. for ($k = 0; $k < $segCount; ++$k) {
  9985. $startCount[$k] = $this->_getUSHORT($font, $offset);
  9986. $offset += 2;
  9987. }
  9988. $idDelta = array(); // delta for all character codes in segment
  9989. for ($k = 0; $k < $segCount; ++$k) {
  9990. $idDelta[$k] = $this->_getUSHORT($font, $offset);
  9991. $offset += 2;
  9992. }
  9993. $idRangeOffset = array(); // Offsets into glyphIdArray or 0
  9994. for ($k = 0; $k < $segCount; ++$k) {
  9995. $idRangeOffset[$k] = $this->_getUSHORT($font, $offset);
  9996. $offset += 2;
  9997. }
  9998. $gidlen = ($length / 2) - 8 - (4 * $segCount);
  9999. $glyphIdArray = array(); // glyph index array
  10000. for ($k = 0; $k < $gidlen; ++$k) {
  10001. $glyphIdArray[$k] = $this->_getUSHORT($font, $offset);
  10002. $offset += 2;
  10003. }
  10004. for ($k = 0; $k < $segCount; ++$k) {
  10005. for ($c = $startCount[$k]; $c <= $endCount[$k]; ++$c) {
  10006. if ($idRangeOffset[$k] == 0) {
  10007. $g = $c;
  10008. } else {
  10009. $gid = (($idRangeOffset[$k] / 2) + ($c - $startCount[$k]) - ($segCount - $k));
  10010. $g = $glyphIdArray[$gid];
  10011. }
  10012. $g += ($idDelta[$k] - 65536);
  10013. if ($g < 0) {
  10014. $g = 0;
  10015. }
  10016. $ctg[$c] = $g;
  10017. }
  10018. }
  10019. break;
  10020. }
  10021. case 6: { // Format 6: Trimmed table mapping
  10022. $offset += 4; // skip length and version/language
  10023. $firstCode = $this->_getUSHORT($font, $offset);
  10024. $offset += 2;
  10025. $entryCount = $this->_getUSHORT($font, $offset);
  10026. $offset += 2;
  10027. for ($k = 0; $k < $entryCount; ++$k) {
  10028. $c = ($k + $firstCode);
  10029. $g = $this->_getUSHORT($font, $offset);
  10030. $ctg[$c] = $g;
  10031. $offset += 2;
  10032. }
  10033. break;
  10034. }
  10035. case 8: { // Format 8: Mixed 16-bit and 32-bit coverage
  10036. $offset += 10; // skip reserved, length and version/language
  10037. for ($k = 0; $k < 8192; ++$k) {
  10038. $is32[$k] = $this->_getBYTE($font, $offset);
  10039. ++$offset;
  10040. }
  10041. $nGroups = $this->_getULONG($font, $offset);
  10042. $offset += 4;
  10043. for ($i = 0; $i < $nGroups; ++$i) {
  10044. $startCharCode = $this->_getULONG($font, $offset);
  10045. $offset += 4;
  10046. $endCharCode = $this->_getULONG($font, $offset);
  10047. $offset += 4;
  10048. $startGlyphID = $this->_getULONG($font, $offset);
  10049. $offset += 4;
  10050. for ($k = $startCharCode; $k <= $endCharCode; ++$k) {
  10051. $is32idx = floor($c / 8);
  10052. if ((isset($is32[$is32idx])) AND (($is32[$is32idx] & (1 << (7 - ($c % 8)))) == 0)) {
  10053. $c = $k;
  10054. } else {
  10055. // 32 bit format
  10056. // convert to decimal (http://www.unicode.org/faq//utf_bom.html#utf16-4)
  10057. //LEAD_OFFSET = (0xD800 - (0x10000 >> 10)) = 55232
  10058. //SURROGATE_OFFSET = (0x10000 - (0xD800 << 10) - 0xDC00) = -56613888
  10059. $c = ((55232 + ($k >> 10)) << 10) + (0xDC00 + ($k & 0x3FF)) -56613888;
  10060. }
  10061. $ctg[$c] = $g;
  10062. ++$startGlyphID;
  10063. }
  10064. }
  10065. break;
  10066. }
  10067. case 10: { // Format 10: Trimmed array
  10068. $offset += 10; // skip reserved, length and version/language
  10069. $startCharCode = $this->_getULONG($font, $offset);
  10070. $offset += 4;
  10071. $numChars = $this->_getULONG($font, $offset);
  10072. $offset += 4;
  10073. for ($k = 0; $k < $numChars; ++$k) {
  10074. $c = ($k + $startCharCode);
  10075. $g = $this->_getUSHORT($font, $offset);
  10076. $ctg[$c] = $g;
  10077. $offset += 2;
  10078. }
  10079. break;
  10080. }
  10081. case 12: { // Format 12: Segmented coverage
  10082. $offset += 10; // skip length and version/language
  10083. $nGroups = $this->_getULONG($font, $offset);
  10084. $offset += 4;
  10085. for ($k = 0; $k < $nGroups; ++$k) {
  10086. $startCharCode = $this->_getULONG($font, $offset);
  10087. $offset += 4;
  10088. $endCharCode = $this->_getULONG($font, $offset);
  10089. $offset += 4;
  10090. $startGlyphCode = $this->_getULONG($font, $offset);
  10091. $offset += 4;
  10092. for ($c = $startCharCode; $c <= $endCharCode; ++$c) {
  10093. $ctg[$c] = $startGlyphCode;
  10094. ++$startGlyphCode;
  10095. }
  10096. }
  10097. break;
  10098. }
  10099. case 13: { // Format 13: Many-to-one range mappings
  10100. // to be implemented ...
  10101. break;
  10102. }
  10103. case 14: { // Format 14: Unicode Variation Sequences
  10104. // to be implemented ...
  10105. break;
  10106. }
  10107. }
  10108. }
  10109. if (!isset($ctg[0])) {
  10110. $ctg[0] = 0;
  10111. }
  10112. // get xHeight (height of x)
  10113. $offset = ($table['glyf']['offset'] + $indexToLoc[$ctg[120]] + 4);
  10114. $yMin = $this->_getFWORD($font, $offset);
  10115. $offset += 4;
  10116. $yMax = $this->_getFWORD($font, $offset);
  10117. $offset += 2;
  10118. $fmetric['XHeight'] = round(($yMax - $yMin) * $urk);
  10119. // get CapHeight (height of H)
  10120. $offset = ($table['glyf']['offset'] + $indexToLoc[$ctg[72]] + 4);
  10121. $yMin = $this->_getFWORD($font, $offset);
  10122. $offset += 4;
  10123. $yMax = $this->_getFWORD($font, $offset);
  10124. $offset += 2;
  10125. $fmetric['CapHeight'] = round(($yMax - $yMin) * $urk);
  10126. // ceate widths array
  10127. $cw = array();
  10128. $offset = $table['hmtx']['offset'];
  10129. for ($i = 0 ; $i < $numberOfHMetrics; ++$i) {
  10130. $cw[$i] = round($this->_getUFWORD($font, $offset) * $urk);
  10131. $offset += 4; // skip lsb
  10132. }
  10133. if ($numberOfHMetrics < $numGlyphs) {
  10134. // fill missing widths with the last value
  10135. $cw = array_pad($cw, $numGlyphs, $cw[($numberOfHMetrics - 1)]);
  10136. }
  10137. $fmetric['MissingWidth'] = $cw[0];
  10138. $fmetric['cw'] = '';
  10139. for ($cid = 0; $cid <= 65535; ++$cid) {
  10140. if (isset($ctg[$cid]) AND isset($cw[$ctg[$cid]])) {
  10141. $fmetric['cw'] .= ','.$cid.'=>'.$cw[$ctg[$cid]];
  10142. }
  10143. }
  10144. } // end of true type
  10145. if (($fmetric['type'] == 'TrueTypeUnicode') AND (count($ctg) == 256)) {
  10146. $fmetric['type'] == 'TrueType';
  10147. }
  10148. // ---------- create php font file ----------
  10149. $pfile = '<'.'?'.'php'."\n";
  10150. $pfile .= '// TCPDF FONT FILE DESCRIPTION'."\n";
  10151. $pfile .= '$type=\''.$fmetric['type'].'\';'."\n";
  10152. $pfile .= '$name=\''.$fmetric['name'].'\';'."\n";
  10153. $pfile .= '$up='.$fmetric['underlinePosition'].';'."\n";
  10154. $pfile .= '$ut='.$fmetric['underlineThickness'].';'."\n";
  10155. if ($fmetric['MissingWidth'] > 0) {
  10156. $pfile .= '$dw='.$fmetric['MissingWidth'].';'."\n";
  10157. } else {
  10158. $pfile .= '$dw='.$fmetric['AvgWidth'].';'."\n";
  10159. }
  10160. $pfile .= '$diff=\''.$fmetric['diff'].'\';'."\n";
  10161. if ($fmetric['type'] == 'Type1') {
  10162. // Type 1
  10163. $pfile .= '$enc=\''.$fmetric['enc'].'\';'."\n";
  10164. $pfile .= '$file=\''.$fmetric['file'].'\';'."\n";
  10165. $pfile .= '$size1='.$fmetric['size1'].';'."\n";
  10166. $pfile .= '$size2='.$fmetric['size2'].';'."\n";
  10167. } else {
  10168. $pfile .= '$originalsize='.$fmetric['originalsize'].';'."\n";
  10169. if ($fmetric['type'] == 'cidfont0') {
  10170. // CID-0
  10171. switch ($fonttype) {
  10172. case 'CID0JP': {
  10173. $pfile .= '// Japanese'."\n";
  10174. $pfile .= '$enc=\'UniJIS-UTF16-H\';'."\n";
  10175. $pfile .= '$cidinfo=array(\'Registry\'=>\'Adobe\', \'Ordering\'=>\'Japan1\',\'Supplement\'=>5);'."\n";
  10176. $pfile .= 'include(dirname(__FILE__).\'/uni2cid_aj16.php\');'."\n";
  10177. break;
  10178. }
  10179. case 'CID0KR': {
  10180. $pfile .= '// Korean'."\n";
  10181. $pfile .= '$enc=\'UniKS-UTF16-H\';'."\n";
  10182. $pfile .= '$cidinfo=array(\'Registry\'=>\'Adobe\', \'Ordering\'=>\'Korea1\',\'Supplement\'=>0);'."\n";
  10183. $pfile .= 'include(dirname(__FILE__).\'/uni2cid_ak12.php\');'."\n";
  10184. break;
  10185. }
  10186. case 'CID0CS': {
  10187. $pfile .= '// Chinese Simplified'."\n";
  10188. $pfile .= '$enc=\'UniGB-UTF16-H\';'."\n";
  10189. $pfile .= '$cidinfo=array(\'Registry\'=>\'Adobe\', \'Ordering\'=>\'GB1\',\'Supplement\'=>2);'."\n";
  10190. $pfile .= 'include(dirname(__FILE__).\'/uni2cid_ag15.php\');'."\n";
  10191. break;
  10192. }
  10193. case 'CID0CT':
  10194. default: {
  10195. $pfile .= '// Chinese Traditional'."\n";
  10196. $pfile .= '$enc=\'UniCNS-UTF16-H\';'."\n";
  10197. $pfile .= '$cidinfo=array(\'Registry\'=>\'Adobe\', \'Ordering\'=>\'CNS1\',\'Supplement\'=>0);'."\n";
  10198. $pfile .= 'include(dirname(__FILE__).\'/uni2cid_aj16.php\');'."\n";
  10199. break;
  10200. }
  10201. }
  10202. } else {
  10203. // TrueType
  10204. $pfile .= '$enc=\''.$fmetric['enc'].'\';'."\n";
  10205. $pfile .= '$file=\''.$fmetric['file'].'\';'."\n";
  10206. $pfile .= '$ctg=\''.$fmetric['ctg'].'\';'."\n";
  10207. // create CIDToGIDMap
  10208. $cidtogidmap = str_pad('', 131072, "\x00"); // (256 * 256 * 2) = 131072
  10209. foreach ($ctg as $cid => $gid) {
  10210. $cidtogidmap = $this->updateCIDtoGIDmap($cidtogidmap, $cid, $ctg[$cid]);
  10211. }
  10212. // store compressed CIDToGIDMap
  10213. $fp = fopen($outpath.$fmetric['ctg'], 'wb');
  10214. fwrite($fp, gzcompress($cidtogidmap));
  10215. fclose($fp);
  10216. }
  10217. }
  10218. $pfile .= '$desc=array(';
  10219. $pfile .= '\'Flags\'=>'.$fmetric['Flags'].',';
  10220. $pfile .= '\'FontBBox\'=>\'['.$fmetric['bbox'].']\',';
  10221. $pfile .= '\'ItalicAngle\'=>'.$fmetric['italicAngle'].',';
  10222. $pfile .= '\'Ascent\'=>'.$fmetric['Ascent'].',';
  10223. $pfile .= '\'Descent\'=>'.$fmetric['Descent'].',';
  10224. $pfile .= '\'Leading\'=>'.$fmetric['Leading'].',';
  10225. $pfile .= '\'CapHeight\'=>'.$fmetric['CapHeight'].',';
  10226. $pfile .= '\'XHeight\'=>'.$fmetric['XHeight'].',';
  10227. $pfile .= '\'StemV\'=>'.$fmetric['StemV'].',';
  10228. $pfile .= '\'StemH\'=>'.$fmetric['StemH'].',';
  10229. $pfile .= '\'AvgWidth\'=>'.$fmetric['AvgWidth'].',';
  10230. $pfile .= '\'MaxWidth\'=>'.$fmetric['MaxWidth'].',';
  10231. $pfile .= '\'MissingWidth\'=>'.$fmetric['MissingWidth'].'';
  10232. $pfile .= ');'."\n";
  10233. $pfile .= '$cw=array('.substr($fmetric['cw'], 1).');'."\n";
  10234. $pfile .= '// --- EOF ---'."\n";
  10235. // store file
  10236. $fp = fopen($outpath.$font_name.'.php', 'w');
  10237. fwrite($fp, $pfile);
  10238. fclose($fp);
  10239. // return TCPDF font name
  10240. return $font_name;
  10241. }
  10242. /**
  10243. * Returns a subset of the TrueType font data without the unused glyphs.
  10244. * @param $font (string) TrueType font data.
  10245. * @param $subsetchars (array) Array of used characters (the glyphs to keep).
  10246. * @return (string) A subset of TrueType font data without the unused glyphs.
  10247. * @author Nicola Asuni
  10248. * @protected
  10249. * @since 5.2.000 (2010-06-02)
  10250. */
  10251. protected function _getTrueTypeFontSubset($font, $subsetchars) {
  10252. ksort($subsetchars);
  10253. $offset = 0; // offset position of the font data
  10254. if ($this->_getULONG($font, $offset) != 0x10000) {
  10255. // sfnt version must be 0x00010000 for TrueType version 1.0.
  10256. return $font;
  10257. }
  10258. $offset += 4;
  10259. // get number of tables
  10260. $numTables = $this->_getUSHORT($font, $offset);
  10261. $offset += 2;
  10262. // skip searchRange, entrySelector and rangeShift
  10263. $offset += 6;
  10264. // tables array
  10265. $table = array();
  10266. // for each table
  10267. for ($i = 0; $i < $numTables; ++$i) {
  10268. // get table info
  10269. $tag = substr($font, $offset, 4);
  10270. $offset += 4;
  10271. $table[$tag] = array();
  10272. $table[$tag]['checkSum'] = $this->_getULONG($font, $offset);
  10273. $offset += 4;
  10274. $table[$tag]['offset'] = $this->_getULONG($font, $offset);
  10275. $offset += 4;
  10276. $table[$tag]['length'] = $this->_getULONG($font, $offset);
  10277. $offset += 4;
  10278. }
  10279. // check magicNumber
  10280. $offset = $table['head']['offset'] + 12;
  10281. if ($this->_getULONG($font, $offset) != 0x5F0F3CF5) {
  10282. // magicNumber must be 0x5F0F3CF5
  10283. return $font;
  10284. }
  10285. $offset += 4;
  10286. // get offset mode (indexToLocFormat : 0 = short, 1 = long)
  10287. $offset = $table['head']['offset'] + 50;
  10288. $short_offset = ($this->_getSHORT($font, $offset) == 0);
  10289. $offset += 2;
  10290. // get the offsets to the locations of the glyphs in the font, relative to the beginning of the glyphData table
  10291. $indexToLoc = array();
  10292. $offset = $table['loca']['offset'];
  10293. if ($short_offset) {
  10294. // short version
  10295. $tot_num_glyphs = ($table['loca']['length'] / 2); // numGlyphs + 1
  10296. for ($i = 0; $i < $tot_num_glyphs; ++$i) {
  10297. $indexToLoc[$i] = $this->_getUSHORT($font, $offset) * 2;
  10298. $offset += 2;
  10299. }
  10300. } else {
  10301. // long version
  10302. $tot_num_glyphs = ($table['loca']['length'] / 4); // numGlyphs + 1
  10303. for ($i = 0; $i < $tot_num_glyphs; ++$i) {
  10304. $indexToLoc[$i] = $this->_getULONG($font, $offset);
  10305. $offset += 4;
  10306. }
  10307. }
  10308. // get glyphs indexes of chars from cmap table
  10309. $subsetglyphs = array(); // glyph IDs on key
  10310. $subsetglyphs[0] = true; // character codes that do not correspond to any glyph in the font should be mapped to glyph index 0
  10311. $offset = $table['cmap']['offset'] + 2;
  10312. $numEncodingTables = $this->_getUSHORT($font, $offset);
  10313. $offset += 2;
  10314. $encodingTables = array();
  10315. for ($i = 0; $i < $numEncodingTables; ++$i) {
  10316. $encodingTables[$i]['platformID'] = $this->_getUSHORT($font, $offset);
  10317. $offset += 2;
  10318. $encodingTables[$i]['encodingID'] = $this->_getUSHORT($font, $offset);
  10319. $offset += 2;
  10320. $encodingTables[$i]['offset'] = $this->_getULONG($font, $offset);
  10321. $offset += 4;
  10322. }
  10323. foreach ($encodingTables as $enctable) {
  10324. if (($enctable['platformID'] == 3) AND ($enctable['encodingID'] == 0)) {
  10325. $modesymbol = true;
  10326. } else {
  10327. $modesymbol = false;
  10328. }
  10329. $offset = $table['cmap']['offset'] + $enctable['offset'];
  10330. $format = $this->_getUSHORT($font, $offset);
  10331. $offset += 2;
  10332. switch ($format) {
  10333. case 0: { // Format 0: Byte encoding table
  10334. $offset += 4; // skip length and version/language
  10335. for ($c = 0; $c < 256; ++$c) {
  10336. if (isset($subsetchars[$c])) {
  10337. $g = $this->_getBYTE($font, $offset);
  10338. $subsetglyphs[$g] = true;
  10339. }
  10340. ++$offset;
  10341. }
  10342. break;
  10343. }
  10344. case 2: { // Format 2: High-byte mapping through table
  10345. $offset += 4; // skip length and version/language
  10346. $numSubHeaders = 0;
  10347. for ($i = 0; $i < 256; ++$i) {
  10348. // Array that maps high bytes to subHeaders: value is subHeader index * 8.
  10349. $subHeaderKeys[$i] = ($this->_getUSHORT($font, $offset) / 8);
  10350. $offset += 2;
  10351. if ($numSubHeaders < $subHeaderKeys[$i]) {
  10352. $numSubHeaders = $subHeaderKeys[$i];
  10353. }
  10354. }
  10355. // the number of subHeaders is equal to the max of subHeaderKeys + 1
  10356. ++$numSubHeaders;
  10357. // read subHeader structures
  10358. $subHeaders = array();
  10359. $numGlyphIndexArray = 0;
  10360. for ($k = 0; $k < $numSubHeaders; ++$k) {
  10361. $subHeaders[$k]['firstCode'] = $this->_getUSHORT($font, $offset);
  10362. $offset += 2;
  10363. $subHeaders[$k]['entryCount'] = $this->_getUSHORT($font, $offset);
  10364. $offset += 2;
  10365. $subHeaders[$k]['idDelta'] = $this->_getSHORT($font, $offset);
  10366. $offset += 2;
  10367. $subHeaders[$k]['idRangeOffset'] = $this->_getUSHORT($font, $offset);
  10368. $offset += 2;
  10369. $subHeaders[$k]['idRangeOffset'] -= (2 + (($numSubHeaders - $k - 1) * 8));
  10370. $subHeaders[$k]['idRangeOffset'] /= 2;
  10371. $numGlyphIndexArray += $subHeaders[$k]['entryCount'];
  10372. }
  10373. for ($k = 0; $k < $numGlyphIndexArray; ++$k) {
  10374. $glyphIndexArray[$k] = $this->_getUSHORT($font, $offset);
  10375. $offset += 2;
  10376. }
  10377. for ($i = 0; $i < 256; ++$i) {
  10378. $k = $subHeaderKeys[$i];
  10379. if ($k == 0) {
  10380. // one byte code
  10381. $c = $i;
  10382. if (isset($subsetchars[$c])) {
  10383. $g = $glyphIndexArray[0];
  10384. $subsetglyphs[$g] = true;
  10385. }
  10386. } else {
  10387. // two bytes code
  10388. $start_byte = $subHeaders[$k]['firstCode'];
  10389. $end_byte = $start_byte + $subHeaders[$k]['entryCount'];
  10390. for ($j = $start_byte; $j < $end_byte; ++$j) {
  10391. // combine high and low bytes
  10392. $c = (($i << 8) + $j);
  10393. if (isset($subsetchars[$c])) {
  10394. $idRangeOffset = ($subHeaders[$k]['idRangeOffset'] + $j - $subHeaders[$k]['firstCode']);
  10395. $g = $glyphIndexArray[$idRangeOffset];
  10396. $g += ($idDelta[$k] - 65536);
  10397. if ($g < 0) {
  10398. $g = 0;
  10399. }
  10400. $subsetglyphs[$g] = true;
  10401. }
  10402. }
  10403. }
  10404. }
  10405. break;
  10406. }
  10407. case 4: { // Format 4: Segment mapping to delta values
  10408. $length = $this->_getUSHORT($font, $offset);
  10409. $offset += 2;
  10410. $offset += 2; // skip version/language
  10411. $segCount = ($this->_getUSHORT($font, $offset) / 2);
  10412. $offset += 2;
  10413. $offset += 6; // skip searchRange, entrySelector, rangeShift
  10414. $endCount = array(); // array of end character codes for each segment
  10415. for ($k = 0; $k < $segCount; ++$k) {
  10416. $endCount[$k] = $this->_getUSHORT($font, $offset);
  10417. $offset += 2;
  10418. }
  10419. $offset += 2; // skip reservedPad
  10420. $startCount = array(); // array of start character codes for each segment
  10421. for ($k = 0; $k < $segCount; ++$k) {
  10422. $startCount[$k] = $this->_getUSHORT($font, $offset);
  10423. $offset += 2;
  10424. }
  10425. $idDelta = array(); // delta for all character codes in segment
  10426. for ($k = 0; $k < $segCount; ++$k) {
  10427. $idDelta[$k] = $this->_getUSHORT($font, $offset);
  10428. $offset += 2;
  10429. }
  10430. $idRangeOffset = array(); // Offsets into glyphIdArray or 0
  10431. for ($k = 0; $k < $segCount; ++$k) {
  10432. $idRangeOffset[$k] = $this->_getUSHORT($font, $offset);
  10433. $offset += 2;
  10434. }
  10435. $gidlen = ($length / 2) - 8 - (4 * $segCount);
  10436. $glyphIdArray = array(); // glyph index array
  10437. for ($k = 0; $k < $gidlen; ++$k) {
  10438. $glyphIdArray[$k] = $this->_getUSHORT($font, $offset);
  10439. $offset += 2;
  10440. }
  10441. for ($k = 0; $k < $segCount; ++$k) {
  10442. for ($c = $startCount[$k]; $c <= $endCount[$k]; ++$c) {
  10443. if (isset($subsetchars[$c])) {
  10444. if ($idRangeOffset[$k] == 0) {
  10445. $g = $c;
  10446. } else {
  10447. $gid = (($idRangeOffset[$k] / 2) + ($c - $startCount[$k]) - ($segCount - $k));
  10448. $g = $glyphIdArray[$gid];
  10449. }
  10450. $g += ($idDelta[$k] - 65536);
  10451. if ($g < 0) {
  10452. $g = 0;
  10453. }
  10454. $subsetglyphs[$g] = true;
  10455. }
  10456. }
  10457. }
  10458. break;
  10459. }
  10460. case 6: { // Format 6: Trimmed table mapping
  10461. $offset += 4; // skip length and version/language
  10462. $firstCode = $this->_getUSHORT($font, $offset);
  10463. $offset += 2;
  10464. $entryCount = $this->_getUSHORT($font, $offset);
  10465. $offset += 2;
  10466. for ($k = 0; $k < $entryCount; ++$k) {
  10467. $c = ($k + $firstCode);
  10468. if (isset($subsetchars[$c])) {
  10469. $g = $this->_getUSHORT($font, $offset);
  10470. $subsetglyphs[$g] = true;
  10471. }
  10472. $offset += 2;
  10473. }
  10474. break;
  10475. }
  10476. case 8: { // Format 8: Mixed 16-bit and 32-bit coverage
  10477. $offset += 10; // skip reserved, length and version/language
  10478. for ($k = 0; $k < 8192; ++$k) {
  10479. $is32[$k] = $this->_getBYTE($font, $offset);
  10480. ++$offset;
  10481. }
  10482. $nGroups = $this->_getULONG($font, $offset);
  10483. $offset += 4;
  10484. for ($i = 0; $i < $nGroups; ++$i) {
  10485. $startCharCode = $this->_getULONG($font, $offset);
  10486. $offset += 4;
  10487. $endCharCode = $this->_getULONG($font, $offset);
  10488. $offset += 4;
  10489. $startGlyphID = $this->_getULONG($font, $offset);
  10490. $offset += 4;
  10491. for ($k = $startCharCode; $k <= $endCharCode; ++$k) {
  10492. $is32idx = floor($c / 8);
  10493. if ((isset($is32[$is32idx])) AND (($is32[$is32idx] & (1 << (7 - ($c % 8)))) == 0)) {
  10494. $c = $k;
  10495. } else {
  10496. // 32 bit format
  10497. // convert to decimal (http://www.unicode.org/faq//utf_bom.html#utf16-4)
  10498. //LEAD_OFFSET = (0xD800 - (0x10000 >> 10)) = 55232
  10499. //SURROGATE_OFFSET = (0x10000 - (0xD800 << 10) - 0xDC00) = -56613888
  10500. $c = ((55232 + ($k >> 10)) << 10) + (0xDC00 + ($k & 0x3FF)) -56613888;
  10501. }
  10502. if (isset($subsetchars[$c])) {
  10503. $subsetglyphs[$startGlyphID] = true;
  10504. }
  10505. ++$startGlyphID;
  10506. }
  10507. }
  10508. break;
  10509. }
  10510. case 10: { // Format 10: Trimmed array
  10511. $offset += 10; // skip reserved, length and version/language
  10512. $startCharCode = $this->_getULONG($font, $offset);
  10513. $offset += 4;
  10514. $numChars = $this->_getULONG($font, $offset);
  10515. $offset += 4;
  10516. for ($k = 0; $k < $numChars; ++$k) {
  10517. $c = ($k + $startCharCode);
  10518. if (isset($subsetchars[$c])) {
  10519. $g = $this->_getUSHORT($font, $offset);
  10520. $subsetglyphs[$g] = true;
  10521. }
  10522. $offset += 2;
  10523. }
  10524. break;
  10525. }
  10526. case 12: { // Format 12: Segmented coverage
  10527. $offset += 10; // skip length and version/language
  10528. $nGroups = $this->_getULONG($font, $offset);
  10529. $offset += 4;
  10530. for ($k = 0; $k < $nGroups; ++$k) {
  10531. $startCharCode = $this->_getULONG($font, $offset);
  10532. $offset += 4;
  10533. $endCharCode = $this->_getULONG($font, $offset);
  10534. $offset += 4;
  10535. $startGlyphCode = $this->_getULONG($font, $offset);
  10536. $offset += 4;
  10537. for ($c = $startCharCode; $c <= $endCharCode; ++$c) {
  10538. if (isset($subsetchars[$c])) {
  10539. $subsetglyphs[$startGlyphCode] = true;
  10540. }
  10541. ++$startGlyphCode;
  10542. }
  10543. }
  10544. break;
  10545. }
  10546. case 13: { // Format 13: Many-to-one range mappings
  10547. // to be implemented ...
  10548. break;
  10549. }
  10550. case 14: { // Format 14: Unicode Variation Sequences
  10551. // to be implemented ...
  10552. break;
  10553. }
  10554. }
  10555. }
  10556. // include all parts of composite glyphs
  10557. $new_sga = $subsetglyphs;
  10558. while (!empty($new_sga)) {
  10559. $sga = $new_sga;
  10560. $new_sga = array();
  10561. foreach ($sga as $key => $val) {
  10562. if (isset($indexToLoc[$key])) {
  10563. $offset = ($table['glyf']['offset'] + $indexToLoc[$key]);
  10564. $numberOfContours = $this->_getSHORT($font, $offset);
  10565. $offset += 2;
  10566. if ($numberOfContours < 0) { // composite glyph
  10567. $offset += 8; // skip xMin, yMin, xMax, yMax
  10568. do {
  10569. $flags = $this->_getUSHORT($font, $offset);
  10570. $offset += 2;
  10571. $glyphIndex = $this->_getUSHORT($font, $offset);
  10572. $offset += 2;
  10573. if (!isset($subsetglyphs[$glyphIndex])) {
  10574. // add missing glyphs
  10575. $new_sga[$glyphIndex] = true;
  10576. }
  10577. // skip some bytes by case
  10578. if ($flags & 1) {
  10579. $offset += 4;
  10580. } else {
  10581. $offset += 2;
  10582. }
  10583. if ($flags & 8) {
  10584. $offset += 2;
  10585. } elseif ($flags & 64) {
  10586. $offset += 4;
  10587. } elseif ($flags & 128) {
  10588. $offset += 8;
  10589. }
  10590. } while ($flags & 32);
  10591. }
  10592. }
  10593. }
  10594. $subsetglyphs += $new_sga;
  10595. }
  10596. // sort glyphs by key (and remove duplicates)
  10597. ksort($subsetglyphs);
  10598. // build new glyf and loca tables
  10599. $glyf = '';
  10600. $loca = '';
  10601. $offset = 0;
  10602. $glyf_offset = $table['glyf']['offset'];
  10603. for ($i = 0; $i < $tot_num_glyphs; ++$i) {
  10604. if (isset($subsetglyphs[$i])) {
  10605. $length = ($indexToLoc[($i + 1)] - $indexToLoc[$i]);
  10606. $glyf .= substr($font, ($glyf_offset + $indexToLoc[$i]), $length);
  10607. } else {
  10608. $length = 0;
  10609. }
  10610. if ($short_offset) {
  10611. $loca .= pack('n', ($offset / 2));
  10612. } else {
  10613. $loca .= pack('N', $offset);
  10614. }
  10615. $offset += $length;
  10616. }
  10617. // array of table names to preserve (loca and glyf tables will be added later)
  10618. // the cmap table is not needed and shall not be present, since the mapping from character codes to glyph descriptions is provided separately
  10619. $table_names = array ('head', 'hhea', 'hmtx', 'maxp', 'cvt ', 'fpgm', 'prep'); // minimum required table names
  10620. // get the tables to preserve
  10621. $offset = 12;
  10622. foreach ($table as $tag => $val) {
  10623. if (in_array($tag, $table_names)) {
  10624. $table[$tag]['data'] = substr($font, $table[$tag]['offset'], $table[$tag]['length']);
  10625. if ($tag == 'head') {
  10626. // set the checkSumAdjustment to 0
  10627. $table[$tag]['data'] = substr($table[$tag]['data'], 0, 8)."\x0\x0\x0\x0".substr($table[$tag]['data'], 12);
  10628. }
  10629. $pad = 4 - ($table[$tag]['length'] % 4);
  10630. if ($pad != 4) {
  10631. // the length of a table must be a multiple of four bytes
  10632. $table[$tag]['length'] += $pad;
  10633. $table[$tag]['data'] .= str_repeat("\x0", $pad);
  10634. }
  10635. $table[$tag]['offset'] = $offset;
  10636. $offset += $table[$tag]['length'];
  10637. // check sum is not changed (so keep the following line commented)
  10638. //$table[$tag]['checkSum'] = $this->_getTTFtableChecksum($table[$tag]['data'], $table[$tag]['length']);
  10639. } else {
  10640. unset($table[$tag]);
  10641. }
  10642. }
  10643. // add loca
  10644. $table['loca']['data'] = $loca;
  10645. $table['loca']['length'] = strlen($loca);
  10646. $pad = 4 - ($table['loca']['length'] % 4);
  10647. if ($pad != 4) {
  10648. // the length of a table must be a multiple of four bytes
  10649. $table['loca']['length'] += $pad;
  10650. $table['loca']['data'] .= str_repeat("\x0", $pad);
  10651. }
  10652. $table['loca']['offset'] = $offset;
  10653. $table['loca']['checkSum'] = $this->_getTTFtableChecksum($table['loca']['data'], $table['loca']['length']);
  10654. $offset += $table['loca']['length'];
  10655. // add glyf
  10656. $table['glyf']['data'] = $glyf;
  10657. $table['glyf']['length'] = strlen($glyf);
  10658. $pad = 4 - ($table['glyf']['length'] % 4);
  10659. if ($pad != 4) {
  10660. // the length of a table must be a multiple of four bytes
  10661. $table['glyf']['length'] += $pad;
  10662. $table['glyf']['data'] .= str_repeat("\x0", $pad);
  10663. }
  10664. $table['glyf']['offset'] = $offset;
  10665. $table['glyf']['checkSum'] = $this->_getTTFtableChecksum($table['glyf']['data'], $table['glyf']['length']);
  10666. // rebuild font
  10667. $font = '';
  10668. $font .= pack('N', 0x10000); // sfnt version
  10669. $numTables = count($table);
  10670. $font .= pack('n', $numTables); // numTables
  10671. $entrySelector = floor(log($numTables, 2));
  10672. $searchRange = pow(2, $entrySelector) * 16;
  10673. $rangeShift = ($numTables * 16) - $searchRange;
  10674. $font .= pack('n', $searchRange); // searchRange
  10675. $font .= pack('n', $entrySelector); // entrySelector
  10676. $font .= pack('n', $rangeShift); // rangeShift
  10677. $offset = ($numTables * 16);
  10678. foreach ($table as $tag => $data) {
  10679. $font .= $tag; // tag
  10680. $font .= pack('N', $data['checkSum']); // checkSum
  10681. $font .= pack('N', ($data['offset'] + $offset)); // offset
  10682. $font .= pack('N', $data['length']); // length
  10683. }
  10684. foreach ($table as $data) {
  10685. $font .= $data['data'];
  10686. }
  10687. // set checkSumAdjustment on head table
  10688. $checkSumAdjustment = 0xB1B0AFBA - $this->_getTTFtableChecksum($font, strlen($font));
  10689. $font = substr($font, 0, $table['head']['offset'] + 8).pack('N', $checkSumAdjustment).substr($font, $table['head']['offset'] + 12);
  10690. return $font;
  10691. }
  10692. /**
  10693. * Returs the checksum of a TTF table.
  10694. * @param $table (string) table to check
  10695. * @param $length (int) length of table in bytes
  10696. * @return int checksum
  10697. * @author Nicola Asuni
  10698. * @protected
  10699. * @since 5.2.000 (2010-06-02)
  10700. */
  10701. protected function _getTTFtableChecksum($table, $length) {
  10702. $sum = 0;
  10703. $tlen = ($length / 4);
  10704. $offset = 0;
  10705. for ($i = 0; $i < $tlen; ++$i) {
  10706. $v = unpack('Ni', substr($table, $offset, 4));
  10707. $sum += $v['i'];
  10708. $offset += 4;
  10709. }
  10710. $sum = unpack('Ni', pack('N', $sum));
  10711. return $sum['i'];
  10712. }
  10713. /**
  10714. * Outputs font widths
  10715. * @param $font (array) font data
  10716. * @param $cidoffset (int) offset for CID values
  10717. * @return PDF command string for font widths
  10718. * @author Nicola Asuni
  10719. * @protected
  10720. * @since 4.4.000 (2008-12-07)
  10721. */
  10722. protected function _putfontwidths($font, $cidoffset=0) {
  10723. ksort($font['cw']);
  10724. $rangeid = 0;
  10725. $range = array();
  10726. $prevcid = -2;
  10727. $prevwidth = -1;
  10728. $interval = false;
  10729. // for each character
  10730. foreach ($font['cw'] as $cid => $width) {
  10731. $cid -= $cidoffset;
  10732. if ($font['subset'] AND ($cid > 255) AND (!isset($font['subsetchars'][$cid]))) {
  10733. // ignore the unused characters (font subsetting)
  10734. continue;
  10735. }
  10736. if ($width != $font['dw']) {
  10737. if ($cid == ($prevcid + 1)) {
  10738. // consecutive CID
  10739. if ($width == $prevwidth) {
  10740. if ($width == $range[$rangeid][0]) {
  10741. $range[$rangeid][] = $width;
  10742. } else {
  10743. array_pop($range[$rangeid]);
  10744. // new range
  10745. $rangeid = $prevcid;
  10746. $range[$rangeid] = array();
  10747. $range[$rangeid][] = $prevwidth;
  10748. $range[$rangeid][] = $width;
  10749. }
  10750. $interval = true;
  10751. $range[$rangeid]['interval'] = true;
  10752. } else {
  10753. if ($interval) {
  10754. // new range
  10755. $rangeid = $cid;
  10756. $range[$rangeid] = array();
  10757. $range[$rangeid][] = $width;
  10758. } else {
  10759. $range[$rangeid][] = $width;
  10760. }
  10761. $interval = false;
  10762. }
  10763. } else {
  10764. // new range
  10765. $rangeid = $cid;
  10766. $range[$rangeid] = array();
  10767. $range[$rangeid][] = $width;
  10768. $interval = false;
  10769. }
  10770. $prevcid = $cid;
  10771. $prevwidth = $width;
  10772. }
  10773. }
  10774. // optimize ranges
  10775. $prevk = -1;
  10776. $nextk = -1;
  10777. $prevint = false;
  10778. foreach ($range as $k => $ws) {
  10779. $cws = count($ws);
  10780. if (($k == $nextk) AND (!$prevint) AND ((!isset($ws['interval'])) OR ($cws < 4))) {
  10781. if (isset($range[$k]['interval'])) {
  10782. unset($range[$k]['interval']);
  10783. }
  10784. $range[$prevk] = array_merge($range[$prevk], $range[$k]);
  10785. unset($range[$k]);
  10786. } else {
  10787. $prevk = $k;
  10788. }
  10789. $nextk = $k + $cws;
  10790. if (isset($ws['interval'])) {
  10791. if ($cws > 3) {
  10792. $prevint = true;
  10793. } else {
  10794. $prevint = false;
  10795. }
  10796. unset($range[$k]['interval']);
  10797. --$nextk;
  10798. } else {
  10799. $prevint = false;
  10800. }
  10801. }
  10802. // output data
  10803. $w = '';
  10804. foreach ($range as $k => $ws) {
  10805. if (count(array_count_values($ws)) == 1) {
  10806. // interval mode is more compact
  10807. $w .= ' '.$k.' '.($k + count($ws) - 1).' '.$ws[0];
  10808. } else {
  10809. // range mode
  10810. $w .= ' '.$k.' [ '.implode(' ', $ws).' ]';
  10811. }
  10812. }
  10813. return '/W ['.$w.' ]';
  10814. }
  10815. /**
  10816. * Output fonts.
  10817. * @author Nicola Asuni
  10818. * @protected
  10819. */
  10820. protected function _putfonts() {
  10821. $nf = $this->n;
  10822. foreach ($this->diffs as $diff) {
  10823. //Encodings
  10824. $this->_newobj();
  10825. $this->_out('<< /Type /Encoding /BaseEncoding /WinAnsiEncoding /Differences ['.$diff.'] >>'."\n".'endobj');
  10826. }
  10827. $mqr = $this->get_mqr();
  10828. $this->set_mqr(false);
  10829. foreach ($this->FontFiles as $file => $info) {
  10830. // search and get font file to embedd
  10831. $fontdir = $info['fontdir'];
  10832. $file = strtolower($file);
  10833. $fontfile = '';
  10834. // search files on various directories
  10835. if (($fontdir !== false) AND file_exists($fontdir.$file)) {
  10836. $fontfile = $fontdir.$file;
  10837. } elseif (file_exists($this->_getfontpath().$file)) {
  10838. $fontfile = $this->_getfontpath().$file;
  10839. } elseif (file_exists($file)) {
  10840. $fontfile = $file;
  10841. }
  10842. if (!$this->empty_string($fontfile)) {
  10843. $font = file_get_contents($fontfile);
  10844. $compressed = (substr($file, -2) == '.z');
  10845. if ((!$compressed) AND (isset($info['length2']))) {
  10846. $header = (ord($font{0}) == 128);
  10847. if ($header) {
  10848. // strip first binary header
  10849. $font = substr($font, 6);
  10850. }
  10851. if ($header AND (ord($font[$info['length1']]) == 128)) {
  10852. // strip second binary header
  10853. $font = substr($font, 0, $info['length1']).substr($font, ($info['length1'] + 6));
  10854. }
  10855. } elseif ($info['subset'] AND ((!$compressed) OR ($compressed AND function_exists('gzcompress')))) {
  10856. if ($compressed) {
  10857. // uncompress font
  10858. $font = gzuncompress($font);
  10859. }
  10860. // merge subset characters
  10861. $subsetchars = array(); // used chars
  10862. foreach ($info['fontkeys'] as $fontkey) {
  10863. $fontinfo = $this->getFontBuffer($fontkey);
  10864. $subsetchars += $fontinfo['subsetchars'];
  10865. }
  10866. // rebuild a font subset
  10867. $font = $this->_getTrueTypeFontSubset($font, $subsetchars);
  10868. // calculate new font length
  10869. $info['length1'] = strlen($font);
  10870. if ($compressed) {
  10871. // recompress font
  10872. $font = gzcompress($font);
  10873. }
  10874. }
  10875. $this->_newobj();
  10876. $this->FontFiles[$file]['n'] = $this->n;
  10877. $stream = $this->_getrawstream($font);
  10878. $out = '<< /Length '.strlen($stream);
  10879. if ($compressed) {
  10880. $out .= ' /Filter /FlateDecode';
  10881. }
  10882. $out .= ' /Length1 '.$info['length1'];
  10883. if (isset($info['length2'])) {
  10884. $out .= ' /Length2 '.$info['length2'].' /Length3 0';
  10885. }
  10886. $out .= ' >>';
  10887. $out .= ' stream'."\n".$stream."\n".'endstream';
  10888. $out .= "\n".'endobj';
  10889. $this->_out($out);
  10890. }
  10891. }
  10892. $this->set_mqr($mqr);
  10893. foreach ($this->fontkeys as $k) {
  10894. //Font objects
  10895. $font = $this->getFontBuffer($k);
  10896. $type = $font['type'];
  10897. $name = $font['name'];
  10898. if ($type == 'core') {
  10899. // standard core font
  10900. $out = $this->_getobj($this->font_obj_ids[$k])."\n";
  10901. $out .= '<</Type /Font';
  10902. $out .= ' /Subtype /Type1';
  10903. $out .= ' /BaseFont /'.$name;
  10904. $out .= ' /Name /F'.$font['i'];
  10905. if ((strtolower($name) != 'symbol') AND (strtolower($name) != 'zapfdingbats')) {
  10906. $out .= ' /Encoding /WinAnsiEncoding';
  10907. }
  10908. if ($k == 'helvetica') {
  10909. // add default font for annotations
  10910. $this->annotation_fonts[$k] = $font['i'];
  10911. }
  10912. $out .= ' >>';
  10913. $out .= "\n".'endobj';
  10914. $this->_out($out);
  10915. } elseif (($type == 'Type1') OR ($type == 'TrueType')) {
  10916. // additional Type1 or TrueType font
  10917. $out = $this->_getobj($this->font_obj_ids[$k])."\n";
  10918. $out .= '<</Type /Font';
  10919. $out .= ' /Subtype /'.$type;
  10920. $out .= ' /BaseFont /'.$name;
  10921. $out .= ' /Name /F'.$font['i'];
  10922. $out .= ' /FirstChar 32 /LastChar 255';
  10923. $out .= ' /Widths '.($this->n + 1).' 0 R';
  10924. $out .= ' /FontDescriptor '.($this->n + 2).' 0 R';
  10925. if ($font['enc']) {
  10926. if (isset($font['diff'])) {
  10927. $out .= ' /Encoding '.($nf + $font['diff']).' 0 R';
  10928. } else {
  10929. $out .= ' /Encoding /WinAnsiEncoding';
  10930. }
  10931. }
  10932. $out .= ' >>';
  10933. $out .= "\n".'endobj';
  10934. $this->_out($out);
  10935. // Widths
  10936. $this->_newobj();
  10937. $s = '[';
  10938. for ($i = 32; $i < 256; ++$i) {
  10939. $s .= $font['cw'][$i].' ';
  10940. }
  10941. $s .= ']';
  10942. $s .= "\n".'endobj';
  10943. $this->_out($s);
  10944. //Descriptor
  10945. $this->_newobj();
  10946. $s = '<</Type /FontDescriptor /FontName /'.$name;
  10947. foreach ($font['desc'] as $fdk => $fdv) {
  10948. if (is_float($fdv)) {
  10949. $fdv = sprintf('%.3F', $fdv);
  10950. }
  10951. $s .= ' /'.$fdk.' '.$fdv.'';
  10952. }
  10953. if (!$this->empty_string($font['file'])) {
  10954. $s .= ' /FontFile'.($type == 'Type1' ? '' : '2').' '.$this->FontFiles[$font['file']]['n'].' 0 R';
  10955. }
  10956. $s .= '>>';
  10957. $s .= "\n".'endobj';
  10958. $this->_out($s);
  10959. } else {
  10960. // additional types
  10961. $mtd = '_put'.strtolower($type);
  10962. if (!method_exists($this, $mtd)) {
  10963. $this->Error('Unsupported font type: '.$type);
  10964. }
  10965. $this->$mtd($font);
  10966. }
  10967. }
  10968. }
  10969. /**
  10970. * Adds unicode fonts.<br>
  10971. * Based on PDF Reference 1.3 (section 5)
  10972. * @param $font (array) font data
  10973. * @protected
  10974. * @author Nicola Asuni
  10975. * @since 1.52.0.TC005 (2005-01-05)
  10976. */
  10977. protected function _puttruetypeunicode($font) {
  10978. $fontname = '';
  10979. if ($font['subset']) {
  10980. // change name for font subsetting
  10981. $subtag = sprintf('%06u', $font['i']);
  10982. $subtag = strtr($subtag, '0123456789', 'ABCDEFGHIJ');
  10983. $fontname .= $subtag.'+';
  10984. }
  10985. $fontname .= $font['name'];
  10986. // Type0 Font
  10987. // A composite font composed of other fonts, organized hierarchically
  10988. $out = $this->_getobj($this->font_obj_ids[$font['fontkey']])."\n";
  10989. $out .= '<< /Type /Font';
  10990. $out .= ' /Subtype /Type0';
  10991. $out .= ' /BaseFont /'.$fontname;
  10992. $out .= ' /Name /F'.$font['i'];
  10993. $out .= ' /Encoding /'.$font['enc'];
  10994. $out .= ' /ToUnicode '.($this->n + 1).' 0 R';
  10995. $out .= ' /DescendantFonts ['.($this->n + 2).' 0 R]';
  10996. $out .= ' >>';
  10997. $out .= "\n".'endobj';
  10998. $this->_out($out);
  10999. // ToUnicode map for Identity-H
  11000. $stream = "/CIDInit /ProcSet findresource begin\n";
  11001. $stream .= "12 dict begin\n";
  11002. $stream .= "begincmap\n";
  11003. $stream .= "/CIDSystemInfo << /Registry (Adobe) /Ordering (UCS) /Supplement 0 >> def\n";
  11004. $stream .= "/CMapName /Adobe-Identity-UCS def\n";
  11005. $stream .= "/CMapType 2 def\n";
  11006. $stream .= "/WMode 0 def\n";
  11007. $stream .= "1 begincodespacerange\n";
  11008. $stream .= "<0000> <FFFF>\n";
  11009. $stream .= "endcodespacerange\n";
  11010. $stream .= "100 beginbfrange\n";
  11011. $stream .= "<0000> <00ff> <0000>\n";
  11012. $stream .= "<0100> <01ff> <0100>\n";
  11013. $stream .= "<0200> <02ff> <0200>\n";
  11014. $stream .= "<0300> <03ff> <0300>\n";
  11015. $stream .= "<0400> <04ff> <0400>\n";
  11016. $stream .= "<0500> <05ff> <0500>\n";
  11017. $stream .= "<0600> <06ff> <0600>\n";
  11018. $stream .= "<0700> <07ff> <0700>\n";
  11019. $stream .= "<0800> <08ff> <0800>\n";
  11020. $stream .= "<0900> <09ff> <0900>\n";
  11021. $stream .= "<0a00> <0aff> <0a00>\n";
  11022. $stream .= "<0b00> <0bff> <0b00>\n";
  11023. $stream .= "<0c00> <0cff> <0c00>\n";
  11024. $stream .= "<0d00> <0dff> <0d00>\n";
  11025. $stream .= "<0e00> <0eff> <0e00>\n";
  11026. $stream .= "<0f00> <0fff> <0f00>\n";
  11027. $stream .= "<1000> <10ff> <1000>\n";
  11028. $stream .= "<1100> <11ff> <1100>\n";
  11029. $stream .= "<1200> <12ff> <1200>\n";
  11030. $stream .= "<1300> <13ff> <1300>\n";
  11031. $stream .= "<1400> <14ff> <1400>\n";
  11032. $stream .= "<1500> <15ff> <1500>\n";
  11033. $stream .= "<1600> <16ff> <1600>\n";
  11034. $stream .= "<1700> <17ff> <1700>\n";
  11035. $stream .= "<1800> <18ff> <1800>\n";
  11036. $stream .= "<1900> <19ff> <1900>\n";
  11037. $stream .= "<1a00> <1aff> <1a00>\n";
  11038. $stream .= "<1b00> <1bff> <1b00>\n";
  11039. $stream .= "<1c00> <1cff> <1c00>\n";
  11040. $stream .= "<1d00> <1dff> <1d00>\n";
  11041. $stream .= "<1e00> <1eff> <1e00>\n";
  11042. $stream .= "<1f00> <1fff> <1f00>\n";
  11043. $stream .= "<2000> <20ff> <2000>\n";
  11044. $stream .= "<2100> <21ff> <2100>\n";
  11045. $stream .= "<2200> <22ff> <2200>\n";
  11046. $stream .= "<2300> <23ff> <2300>\n";
  11047. $stream .= "<2400> <24ff> <2400>\n";
  11048. $stream .= "<2500> <25ff> <2500>\n";
  11049. $stream .= "<2600> <26ff> <2600>\n";
  11050. $stream .= "<2700> <27ff> <2700>\n";
  11051. $stream .= "<2800> <28ff> <2800>\n";
  11052. $stream .= "<2900> <29ff> <2900>\n";
  11053. $stream .= "<2a00> <2aff> <2a00>\n";
  11054. $stream .= "<2b00> <2bff> <2b00>\n";
  11055. $stream .= "<2c00> <2cff> <2c00>\n";
  11056. $stream .= "<2d00> <2dff> <2d00>\n";
  11057. $stream .= "<2e00> <2eff> <2e00>\n";
  11058. $stream .= "<2f00> <2fff> <2f00>\n";
  11059. $stream .= "<3000> <30ff> <3000>\n";
  11060. $stream .= "<3100> <31ff> <3100>\n";
  11061. $stream .= "<3200> <32ff> <3200>\n";
  11062. $stream .= "<3300> <33ff> <3300>\n";
  11063. $stream .= "<3400> <34ff> <3400>\n";
  11064. $stream .= "<3500> <35ff> <3500>\n";
  11065. $stream .= "<3600> <36ff> <3600>\n";
  11066. $stream .= "<3700> <37ff> <3700>\n";
  11067. $stream .= "<3800> <38ff> <3800>\n";
  11068. $stream .= "<3900> <39ff> <3900>\n";
  11069. $stream .= "<3a00> <3aff> <3a00>\n";
  11070. $stream .= "<3b00> <3bff> <3b00>\n";
  11071. $stream .= "<3c00> <3cff> <3c00>\n";
  11072. $stream .= "<3d00> <3dff> <3d00>\n";
  11073. $stream .= "<3e00> <3eff> <3e00>\n";
  11074. $stream .= "<3f00> <3fff> <3f00>\n";
  11075. $stream .= "<4000> <40ff> <4000>\n";
  11076. $stream .= "<4100> <41ff> <4100>\n";
  11077. $stream .= "<4200> <42ff> <4200>\n";
  11078. $stream .= "<4300> <43ff> <4300>\n";
  11079. $stream .= "<4400> <44ff> <4400>\n";
  11080. $stream .= "<4500> <45ff> <4500>\n";
  11081. $stream .= "<4600> <46ff> <4600>\n";
  11082. $stream .= "<4700> <47ff> <4700>\n";
  11083. $stream .= "<4800> <48ff> <4800>\n";
  11084. $stream .= "<4900> <49ff> <4900>\n";
  11085. $stream .= "<4a00> <4aff> <4a00>\n";
  11086. $stream .= "<4b00> <4bff> <4b00>\n";
  11087. $stream .= "<4c00> <4cff> <4c00>\n";
  11088. $stream .= "<4d00> <4dff> <4d00>\n";
  11089. $stream .= "<4e00> <4eff> <4e00>\n";
  11090. $stream .= "<4f00> <4fff> <4f00>\n";
  11091. $stream .= "<5000> <50ff> <5000>\n";
  11092. $stream .= "<5100> <51ff> <5100>\n";
  11093. $stream .= "<5200> <52ff> <5200>\n";
  11094. $stream .= "<5300> <53ff> <5300>\n";
  11095. $stream .= "<5400> <54ff> <5400>\n";
  11096. $stream .= "<5500> <55ff> <5500>\n";
  11097. $stream .= "<5600> <56ff> <5600>\n";
  11098. $stream .= "<5700> <57ff> <5700>\n";
  11099. $stream .= "<5800> <58ff> <5800>\n";
  11100. $stream .= "<5900> <59ff> <5900>\n";
  11101. $stream .= "<5a00> <5aff> <5a00>\n";
  11102. $stream .= "<5b00> <5bff> <5b00>\n";
  11103. $stream .= "<5c00> <5cff> <5c00>\n";
  11104. $stream .= "<5d00> <5dff> <5d00>\n";
  11105. $stream .= "<5e00> <5eff> <5e00>\n";
  11106. $stream .= "<5f00> <5fff> <5f00>\n";
  11107. $stream .= "<6000> <60ff> <6000>\n";
  11108. $stream .= "<6100> <61ff> <6100>\n";
  11109. $stream .= "<6200> <62ff> <6200>\n";
  11110. $stream .= "<6300> <63ff> <6300>\n";
  11111. $stream .= "endbfrange\n";
  11112. $stream .= "100 beginbfrange\n";
  11113. $stream .= "<6400> <64ff> <6400>\n";
  11114. $stream .= "<6500> <65ff> <6500>\n";
  11115. $stream .= "<6600> <66ff> <6600>\n";
  11116. $stream .= "<6700> <67ff> <6700>\n";
  11117. $stream .= "<6800> <68ff> <6800>\n";
  11118. $stream .= "<6900> <69ff> <6900>\n";
  11119. $stream .= "<6a00> <6aff> <6a00>\n";
  11120. $stream .= "<6b00> <6bff> <6b00>\n";
  11121. $stream .= "<6c00> <6cff> <6c00>\n";
  11122. $stream .= "<6d00> <6dff> <6d00>\n";
  11123. $stream .= "<6e00> <6eff> <6e00>\n";
  11124. $stream .= "<6f00> <6fff> <6f00>\n";
  11125. $stream .= "<7000> <70ff> <7000>\n";
  11126. $stream .= "<7100> <71ff> <7100>\n";
  11127. $stream .= "<7200> <72ff> <7200>\n";
  11128. $stream .= "<7300> <73ff> <7300>\n";
  11129. $stream .= "<7400> <74ff> <7400>\n";
  11130. $stream .= "<7500> <75ff> <7500>\n";
  11131. $stream .= "<7600> <76ff> <7600>\n";
  11132. $stream .= "<7700> <77ff> <7700>\n";
  11133. $stream .= "<7800> <78ff> <7800>\n";
  11134. $stream .= "<7900> <79ff> <7900>\n";
  11135. $stream .= "<7a00> <7aff> <7a00>\n";
  11136. $stream .= "<7b00> <7bff> <7b00>\n";
  11137. $stream .= "<7c00> <7cff> <7c00>\n";
  11138. $stream .= "<7d00> <7dff> <7d00>\n";
  11139. $stream .= "<7e00> <7eff> <7e00>\n";
  11140. $stream .= "<7f00> <7fff> <7f00>\n";
  11141. $stream .= "<8000> <80ff> <8000>\n";
  11142. $stream .= "<8100> <81ff> <8100>\n";
  11143. $stream .= "<8200> <82ff> <8200>\n";
  11144. $stream .= "<8300> <83ff> <8300>\n";
  11145. $stream .= "<8400> <84ff> <8400>\n";
  11146. $stream .= "<8500> <85ff> <8500>\n";
  11147. $stream .= "<8600> <86ff> <8600>\n";
  11148. $stream .= "<8700> <87ff> <8700>\n";
  11149. $stream .= "<8800> <88ff> <8800>\n";
  11150. $stream .= "<8900> <89ff> <8900>\n";
  11151. $stream .= "<8a00> <8aff> <8a00>\n";
  11152. $stream .= "<8b00> <8bff> <8b00>\n";
  11153. $stream .= "<8c00> <8cff> <8c00>\n";
  11154. $stream .= "<8d00> <8dff> <8d00>\n";
  11155. $stream .= "<8e00> <8eff> <8e00>\n";
  11156. $stream .= "<8f00> <8fff> <8f00>\n";
  11157. $stream .= "<9000> <90ff> <9000>\n";
  11158. $stream .= "<9100> <91ff> <9100>\n";
  11159. $stream .= "<9200> <92ff> <9200>\n";
  11160. $stream .= "<9300> <93ff> <9300>\n";
  11161. $stream .= "<9400> <94ff> <9400>\n";
  11162. $stream .= "<9500> <95ff> <9500>\n";
  11163. $stream .= "<9600> <96ff> <9600>\n";
  11164. $stream .= "<9700> <97ff> <9700>\n";
  11165. $stream .= "<9800> <98ff> <9800>\n";
  11166. $stream .= "<9900> <99ff> <9900>\n";
  11167. $stream .= "<9a00> <9aff> <9a00>\n";
  11168. $stream .= "<9b00> <9bff> <9b00>\n";
  11169. $stream .= "<9c00> <9cff> <9c00>\n";
  11170. $stream .= "<9d00> <9dff> <9d00>\n";
  11171. $stream .= "<9e00> <9eff> <9e00>\n";
  11172. $stream .= "<9f00> <9fff> <9f00>\n";
  11173. $stream .= "<a000> <a0ff> <a000>\n";
  11174. $stream .= "<a100> <a1ff> <a100>\n";
  11175. $stream .= "<a200> <a2ff> <a200>\n";
  11176. $stream .= "<a300> <a3ff> <a300>\n";
  11177. $stream .= "<a400> <a4ff> <a400>\n";
  11178. $stream .= "<a500> <a5ff> <a500>\n";
  11179. $stream .= "<a600> <a6ff> <a600>\n";
  11180. $stream .= "<a700> <a7ff> <a700>\n";
  11181. $stream .= "<a800> <a8ff> <a800>\n";
  11182. $stream .= "<a900> <a9ff> <a900>\n";
  11183. $stream .= "<aa00> <aaff> <aa00>\n";
  11184. $stream .= "<ab00> <abff> <ab00>\n";
  11185. $stream .= "<ac00> <acff> <ac00>\n";
  11186. $stream .= "<ad00> <adff> <ad00>\n";
  11187. $stream .= "<ae00> <aeff> <ae00>\n";
  11188. $stream .= "<af00> <afff> <af00>\n";
  11189. $stream .= "<b000> <b0ff> <b000>\n";
  11190. $stream .= "<b100> <b1ff> <b100>\n";
  11191. $stream .= "<b200> <b2ff> <b200>\n";
  11192. $stream .= "<b300> <b3ff> <b300>\n";
  11193. $stream .= "<b400> <b4ff> <b400>\n";
  11194. $stream .= "<b500> <b5ff> <b500>\n";
  11195. $stream .= "<b600> <b6ff> <b600>\n";
  11196. $stream .= "<b700> <b7ff> <b700>\n";
  11197. $stream .= "<b800> <b8ff> <b800>\n";
  11198. $stream .= "<b900> <b9ff> <b900>\n";
  11199. $stream .= "<ba00> <baff> <ba00>\n";
  11200. $stream .= "<bb00> <bbff> <bb00>\n";
  11201. $stream .= "<bc00> <bcff> <bc00>\n";
  11202. $stream .= "<bd00> <bdff> <bd00>\n";
  11203. $stream .= "<be00> <beff> <be00>\n";
  11204. $stream .= "<bf00> <bfff> <bf00>\n";
  11205. $stream .= "<c000> <c0ff> <c000>\n";
  11206. $stream .= "<c100> <c1ff> <c100>\n";
  11207. $stream .= "<c200> <c2ff> <c200>\n";
  11208. $stream .= "<c300> <c3ff> <c300>\n";
  11209. $stream .= "<c400> <c4ff> <c400>\n";
  11210. $stream .= "<c500> <c5ff> <c500>\n";
  11211. $stream .= "<c600> <c6ff> <c600>\n";
  11212. $stream .= "<c700> <c7ff> <c700>\n";
  11213. $stream .= "endbfrange\n";
  11214. $stream .= "56 beginbfrange\n";
  11215. $stream .= "<c800> <c8ff> <c800>\n";
  11216. $stream .= "<c900> <c9ff> <c900>\n";
  11217. $stream .= "<ca00> <caff> <ca00>\n";
  11218. $stream .= "<cb00> <cbff> <cb00>\n";
  11219. $stream .= "<cc00> <ccff> <cc00>\n";
  11220. $stream .= "<cd00> <cdff> <cd00>\n";
  11221. $stream .= "<ce00> <ceff> <ce00>\n";
  11222. $stream .= "<cf00> <cfff> <cf00>\n";
  11223. $stream .= "<d000> <d0ff> <d000>\n";
  11224. $stream .= "<d100> <d1ff> <d100>\n";
  11225. $stream .= "<d200> <d2ff> <d200>\n";
  11226. $stream .= "<d300> <d3ff> <d300>\n";
  11227. $stream .= "<d400> <d4ff> <d400>\n";
  11228. $stream .= "<d500> <d5ff> <d500>\n";
  11229. $stream .= "<d600> <d6ff> <d600>\n";
  11230. $stream .= "<d700> <d7ff> <d700>\n";
  11231. $stream .= "<d800> <d8ff> <d800>\n";
  11232. $stream .= "<d900> <d9ff> <d900>\n";
  11233. $stream .= "<da00> <daff> <da00>\n";
  11234. $stream .= "<db00> <dbff> <db00>\n";
  11235. $stream .= "<dc00> <dcff> <dc00>\n";
  11236. $stream .= "<dd00> <ddff> <dd00>\n";
  11237. $stream .= "<de00> <deff> <de00>\n";
  11238. $stream .= "<df00> <dfff> <df00>\n";
  11239. $stream .= "<e000> <e0ff> <e000>\n";
  11240. $stream .= "<e100> <e1ff> <e100>\n";
  11241. $stream .= "<e200> <e2ff> <e200>\n";
  11242. $stream .= "<e300> <e3ff> <e300>\n";
  11243. $stream .= "<e400> <e4ff> <e400>\n";
  11244. $stream .= "<e500> <e5ff> <e500>\n";
  11245. $stream .= "<e600> <e6ff> <e600>\n";
  11246. $stream .= "<e700> <e7ff> <e700>\n";
  11247. $stream .= "<e800> <e8ff> <e800>\n";
  11248. $stream .= "<e900> <e9ff> <e900>\n";
  11249. $stream .= "<ea00> <eaff> <ea00>\n";
  11250. $stream .= "<eb00> <ebff> <eb00>\n";
  11251. $stream .= "<ec00> <ecff> <ec00>\n";
  11252. $stream .= "<ed00> <edff> <ed00>\n";
  11253. $stream .= "<ee00> <eeff> <ee00>\n";
  11254. $stream .= "<ef00> <efff> <ef00>\n";
  11255. $stream .= "<f000> <f0ff> <f000>\n";
  11256. $stream .= "<f100> <f1ff> <f100>\n";
  11257. $stream .= "<f200> <f2ff> <f200>\n";
  11258. $stream .= "<f300> <f3ff> <f300>\n";
  11259. $stream .= "<f400> <f4ff> <f400>\n";
  11260. $stream .= "<f500> <f5ff> <f500>\n";
  11261. $stream .= "<f600> <f6ff> <f600>\n";
  11262. $stream .= "<f700> <f7ff> <f700>\n";
  11263. $stream .= "<f800> <f8ff> <f800>\n";
  11264. $stream .= "<f900> <f9ff> <f900>\n";
  11265. $stream .= "<fa00> <faff> <fa00>\n";
  11266. $stream .= "<fb00> <fbff> <fb00>\n";
  11267. $stream .= "<fc00> <fcff> <fc00>\n";
  11268. $stream .= "<fd00> <fdff> <fd00>\n";
  11269. $stream .= "<fe00> <feff> <fe00>\n";
  11270. $stream .= "<ff00> <ffff> <ff00>\n";
  11271. $stream .= "endbfrange\n";
  11272. $stream .= "endcmap\n";
  11273. $stream .= "CMapName currentdict /CMap defineresource pop\n";
  11274. $stream .= "end\n";
  11275. $stream .= "end";
  11276. // ToUnicode Object
  11277. $this->_newobj();
  11278. $stream = ($this->compress) ? gzcompress($stream) : $stream;
  11279. $filter = ($this->compress) ? '/Filter /FlateDecode ' : '';
  11280. $stream = $this->_getrawstream($stream);
  11281. $this->_out('<<'.$filter.'/Length '.strlen($stream).'>> stream'."\n".$stream."\n".'endstream'."\n".'endobj');
  11282. // CIDFontType2
  11283. // A CIDFont whose glyph descriptions are based on TrueType font technology
  11284. $oid = $this->_newobj();
  11285. $out = '<< /Type /Font';
  11286. $out .= ' /Subtype /CIDFontType2';
  11287. $out .= ' /BaseFont /'.$fontname;
  11288. // A dictionary containing entries that define the character collection of the CIDFont.
  11289. $cidinfo = '/Registry '.$this->_datastring($font['cidinfo']['Registry'], $oid);
  11290. $cidinfo .= ' /Ordering '.$this->_datastring($font['cidinfo']['Ordering'], $oid);
  11291. $cidinfo .= ' /Supplement '.$font['cidinfo']['Supplement'];
  11292. $out .= ' /CIDSystemInfo << '.$cidinfo.' >>';
  11293. $out .= ' /FontDescriptor '.($this->n + 1).' 0 R';
  11294. $out .= ' /DW '.$font['dw']; // default width
  11295. $out .= "\n".$this->_putfontwidths($font, 0);
  11296. if (isset($font['ctg']) AND (!$this->empty_string($font['ctg']))) {
  11297. $out .= "\n".'/CIDToGIDMap '.($this->n + 2).' 0 R';
  11298. }
  11299. $out .= ' >>';
  11300. $out .= "\n".'endobj';
  11301. $this->_out($out);
  11302. // Font descriptor
  11303. // A font descriptor describing the CIDFont default metrics other than its glyph widths
  11304. $this->_newobj();
  11305. $out = '<< /Type /FontDescriptor';
  11306. $out .= ' /FontName /'.$fontname;
  11307. foreach ($font['desc'] as $key => $value) {
  11308. if (is_float($value)) {
  11309. $value = sprintf('%.3F', $value);
  11310. }
  11311. $out .= ' /'.$key.' '.$value;
  11312. }
  11313. $fontdir = false;
  11314. if (!$this->empty_string($font['file'])) {
  11315. // A stream containing a TrueType font
  11316. $out .= ' /FontFile2 '.$this->FontFiles[$font['file']]['n'].' 0 R';
  11317. $fontdir = $this->FontFiles[$font['file']]['fontdir'];
  11318. }
  11319. $out .= ' >>';
  11320. $out .= "\n".'endobj';
  11321. $this->_out($out);
  11322. if (isset($font['ctg']) AND (!$this->empty_string($font['ctg']))) {
  11323. $this->_newobj();
  11324. // Embed CIDToGIDMap
  11325. // A specification of the mapping from CIDs to glyph indices
  11326. // search and get CTG font file to embedd
  11327. $ctgfile = strtolower($font['ctg']);
  11328. // search and get ctg font file to embedd
  11329. $fontfile = '';
  11330. // search files on various directories
  11331. if (($fontdir !== false) AND file_exists($fontdir.$ctgfile)) {
  11332. $fontfile = $fontdir.$ctgfile;
  11333. } elseif (file_exists($this->_getfontpath().$ctgfile)) {
  11334. $fontfile = $this->_getfontpath().$ctgfile;
  11335. } elseif (file_exists($ctgfile)) {
  11336. $fontfile = $ctgfile;
  11337. }
  11338. if ($this->empty_string($fontfile)) {
  11339. $this->Error('Font file not found: '.$ctgfile);
  11340. }
  11341. $stream = $this->_getrawstream(file_get_contents($fontfile));
  11342. $out = '<< /Length '.strlen($stream).'';
  11343. if (substr($fontfile, -2) == '.z') { // check file extension
  11344. // Decompresses data encoded using the public-domain
  11345. // zlib/deflate compression method, reproducing the
  11346. // original text or binary data
  11347. $out .= ' /Filter /FlateDecode';
  11348. }
  11349. $out .= ' >>';
  11350. $out .= ' stream'."\n".$stream."\n".'endstream';
  11351. $out .= "\n".'endobj';
  11352. $this->_out($out);
  11353. }
  11354. }
  11355. /**
  11356. * Output CID-0 fonts.
  11357. * A Type 0 CIDFont contains glyph descriptions based on the Adobe Type 1 font format
  11358. * @param $font (array) font data
  11359. * @protected
  11360. * @author Andrew Whitehead, Nicola Asuni, Yukihiro Nakadaira
  11361. * @since 3.2.000 (2008-06-23)
  11362. */
  11363. protected function _putcidfont0($font) {
  11364. $cidoffset = 0;
  11365. if (!isset($font['cw'][1])) {
  11366. $cidoffset = 31;
  11367. }
  11368. if (isset($font['cidinfo']['uni2cid'])) {
  11369. // convert unicode to cid.
  11370. $uni2cid = $font['cidinfo']['uni2cid'];
  11371. $cw = array();
  11372. foreach ($font['cw'] as $uni => $width) {
  11373. if (isset($uni2cid[$uni])) {
  11374. $cw[($uni2cid[$uni] + $cidoffset)] = $width;
  11375. } elseif ($uni < 256) {
  11376. $cw[$uni] = $width;
  11377. } // else unknown character
  11378. }
  11379. $font = array_merge($font, array('cw' => $cw));
  11380. }
  11381. $name = $font['name'];
  11382. $enc = $font['enc'];
  11383. if ($enc) {
  11384. $longname = $name.'-'.$enc;
  11385. } else {
  11386. $longname = $name;
  11387. }
  11388. $out = $this->_getobj($this->font_obj_ids[$font['fontkey']])."\n";
  11389. $out .= '<</Type /Font';
  11390. $out .= ' /Subtype /Type0';
  11391. $out .= ' /BaseFont /'.$longname;
  11392. $out .= ' /Name /F'.$font['i'];
  11393. if ($enc) {
  11394. $out .= ' /Encoding /'.$enc;
  11395. }
  11396. $out .= ' /DescendantFonts ['.($this->n + 1).' 0 R]';
  11397. $out .= ' >>';
  11398. $out .= "\n".'endobj';
  11399. $this->_out($out);
  11400. $oid = $this->_newobj();
  11401. $out = '<</Type /Font';
  11402. $out .= ' /Subtype /CIDFontType0';
  11403. $out .= ' /BaseFont /'.$name;
  11404. $cidinfo = '/Registry '.$this->_datastring($font['cidinfo']['Registry'], $oid);
  11405. $cidinfo .= ' /Ordering '.$this->_datastring($font['cidinfo']['Ordering'], $oid);
  11406. $cidinfo .= ' /Supplement '.$font['cidinfo']['Supplement'];
  11407. $out .= ' /CIDSystemInfo <<'.$cidinfo.'>>';
  11408. $out .= ' /FontDescriptor '.($this->n + 1).' 0 R';
  11409. $out .= ' /DW '.$font['dw'];
  11410. $out .= "\n".$this->_putfontwidths($font, $cidoffset);
  11411. $out .= ' >>';
  11412. $out .= "\n".'endobj';
  11413. $this->_out($out);
  11414. $this->_newobj();
  11415. $s = '<</Type /FontDescriptor /FontName /'.$name;
  11416. foreach ($font['desc'] as $k => $v) {
  11417. if ($k != 'Style') {
  11418. if (is_float($v)) {
  11419. $v = sprintf('%.3F', $v);
  11420. }
  11421. $s .= ' /'.$k.' '.$v.'';
  11422. }
  11423. }
  11424. $s .= '>>';
  11425. $s .= "\n".'endobj';
  11426. $this->_out($s);
  11427. }
  11428. /**
  11429. * Output images.
  11430. * @protected
  11431. */
  11432. protected function _putimages() {
  11433. $filter = ($this->compress) ? '/Filter /FlateDecode ' : '';
  11434. foreach ($this->imagekeys as $file) {
  11435. $info = $this->getImageBuffer($file);
  11436. // set object for alternate images array
  11437. if ((!$this->pdfa_mode) AND isset($info['altimgs']) AND !empty($info['altimgs'])) {
  11438. $altoid = $this->_newobj();
  11439. $out = '[';
  11440. foreach ($info['altimgs'] as $altimage) {
  11441. if (isset($this->xobjects['I'.$altimage[0]]['n'])) {
  11442. $out .= ' << /Image '.$this->xobjects['I'.$altimage[0]]['n'].' 0 R';
  11443. $out .= ' /DefaultForPrinting';
  11444. if ($altimage[1] === true) {
  11445. $out .= ' true';
  11446. } else {
  11447. $out .= ' false';
  11448. }
  11449. $out .= ' >>';
  11450. }
  11451. }
  11452. $out .= ' ]';
  11453. $out .= "\n".'endobj';
  11454. $this->_out($out);
  11455. }
  11456. // set image object
  11457. $oid = $this->_newobj();
  11458. $this->xobjects['I'.$info['i']] = array('n' => $oid);
  11459. $this->setImageSubBuffer($file, 'n', $this->n);
  11460. $out = '<</Type /XObject';
  11461. $out .= ' /Subtype /Image';
  11462. $out .= ' /Width '.$info['w'];
  11463. $out .= ' /Height '.$info['h'];
  11464. if (array_key_exists('masked', $info)) {
  11465. $out .= ' /SMask '.($this->n - 1).' 0 R';
  11466. }
  11467. // set color space
  11468. $icc = false;
  11469. if (isset($info['icc']) AND ($info['icc'] !== false)) {
  11470. // ICC Colour Space
  11471. $icc = true;
  11472. $out .= ' /ColorSpace [/ICCBased '.($this->n + 1).' 0 R]';
  11473. } elseif ($info['cs'] == 'Indexed') {
  11474. // Indexed Colour Space
  11475. $out .= ' /ColorSpace [/Indexed /DeviceRGB '.((strlen($info['pal']) / 3) - 1).' '.($this->n + 1).' 0 R]';
  11476. } else {
  11477. // Device Colour Space
  11478. $out .= ' /ColorSpace /'.$info['cs'];
  11479. }
  11480. if ($info['cs'] == 'DeviceCMYK') {
  11481. $out .= ' /Decode [1 0 1 0 1 0 1 0]';
  11482. }
  11483. $out .= ' /BitsPerComponent '.$info['bpc'];
  11484. if (isset($altoid) AND ($altoid > 0)) {
  11485. // reference to alternate images dictionary
  11486. $out .= ' /Alternates '.$altoid.' 0 R';
  11487. }
  11488. if (isset($info['exurl']) AND !empty($info['exurl'])) {
  11489. // external stream
  11490. $out .= ' /Length 0';
  11491. $out .= ' /F << /FS /URL /F '.$this->_datastring($info['exurl'], $oid).' >>';
  11492. if (isset($info['f'])) {
  11493. $out .= ' /FFilter /'.$info['f'];
  11494. }
  11495. $out .= ' >>';
  11496. $out .= ' stream'."\n".'endstream';
  11497. } else {
  11498. if (isset($info['f'])) {
  11499. $out .= ' /Filter /'.$info['f'];
  11500. }
  11501. if (isset($info['parms'])) {
  11502. $out .= ' '.$info['parms'];
  11503. }
  11504. if (isset($info['trns']) AND is_array($info['trns'])) {
  11505. $trns = '';
  11506. $count_info = count($info['trns']);
  11507. for ($i=0; $i < $count_info; ++$i) {
  11508. $trns .= $info['trns'][$i].' '.$info['trns'][$i].' ';
  11509. }
  11510. $out .= ' /Mask ['.$trns.']';
  11511. }
  11512. $stream = $this->_getrawstream($info['data']);
  11513. $out .= ' /Length '.strlen($stream).' >>';
  11514. $out .= ' stream'."\n".$stream."\n".'endstream';
  11515. }
  11516. $out .= "\n".'endobj';
  11517. $this->_out($out);
  11518. if ($icc) {
  11519. // ICC colour profile
  11520. $this->_newobj();
  11521. $icc = ($this->compress) ? gzcompress($info['icc']) : $info['icc'];
  11522. $icc = $this->_getrawstream($icc);
  11523. $this->_out('<</N '.$info['ch'].' /Alternate /'.$info['cs'].' '.$filter.'/Length '.strlen($icc).'>> stream'."\n".$icc."\n".'endstream'."\n".'endobj');
  11524. } elseif ($info['cs'] == 'Indexed') {
  11525. // colour palette
  11526. $this->_newobj();
  11527. $pal = ($this->compress) ? gzcompress($info['pal']) : $info['pal'];
  11528. $pal = $this->_getrawstream($pal);
  11529. $this->_out('<<'.$filter.'/Length '.strlen($pal).'>> stream'."\n".$pal."\n".'endstream'."\n".'endobj');
  11530. }
  11531. }
  11532. }
  11533. /**
  11534. * Output Form XObjects Templates.
  11535. * @author Nicola Asuni
  11536. * @since 5.8.017 (2010-08-24)
  11537. * @protected
  11538. * @see startTemplate(), endTemplate(), printTemplate()
  11539. */
  11540. protected function _putxobjects() {
  11541. foreach ($this->xobjects as $key => $data) {
  11542. if (isset($data['outdata'])) {
  11543. $stream = trim($data['outdata']);
  11544. $out = $this->_getobj($data['n'])."\n";
  11545. $out .= '<<';
  11546. $out .= ' /Type /XObject';
  11547. $out .= ' /Subtype /Form';
  11548. $out .= ' /FormType 1';
  11549. if ($this->compress) {
  11550. $stream = gzcompress($stream);
  11551. $out .= ' /Filter /FlateDecode';
  11552. }
  11553. $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));
  11554. $out .= ' /Matrix [1 0 0 1 0 0]';
  11555. $out .= ' /Resources <<';
  11556. $out .= ' /ProcSet [/PDF /Text /ImageB /ImageC /ImageI]';
  11557. if (!$this->pdfa_mode) {
  11558. // transparency
  11559. if (isset($data['extgstates']) AND !empty($data['extgstates'])) {
  11560. $out .= ' /ExtGState <<';
  11561. foreach ($data['extgstates'] as $k => $extgstate) {
  11562. if (isset($this->extgstates[$k]['name'])) {
  11563. $out .= ' /'.$this->extgstates[$k]['name'];
  11564. } else {
  11565. $out .= ' /GS'.$k;
  11566. }
  11567. $out .= ' '.$this->extgstates[$k]['n'].' 0 R';
  11568. }
  11569. $out .= ' >>';
  11570. }
  11571. if (isset($data['gradients']) AND !empty($data['gradients'])) {
  11572. $gp = '';
  11573. $gs = '';
  11574. foreach ($data['gradients'] as $id => $grad) {
  11575. // gradient patterns
  11576. $gp .= ' /p'.$id.' '.$this->gradients[$id]['pattern'].' 0 R';
  11577. // gradient shadings
  11578. $gs .= ' /Sh'.$id.' '.$this->gradients[$id]['id'].' 0 R';
  11579. }
  11580. $out .= ' /Pattern <<'.$gp.' >>';
  11581. $out .= ' /Shading <<'.$gs.' >>';
  11582. }
  11583. }
  11584. // spot colors
  11585. if (isset($data['spot_colors']) AND !empty($data['spot_colors'])) {
  11586. $out .= ' /ColorSpace <<';
  11587. foreach ($data['spot_colors'] as $name => $color) {
  11588. $out .= ' /CS'.$color['i'].' '.$this->spot_colors[$name]['n'].' 0 R';
  11589. }
  11590. $out .= ' >>';
  11591. }
  11592. // fonts
  11593. if (!empty($data['fonts'])) {
  11594. $out .= ' /Font <<';
  11595. foreach ($data['fonts'] as $fontkey => $fontid) {
  11596. $out .= ' /F'.$fontid.' '.$this->font_obj_ids[$fontkey].' 0 R';
  11597. }
  11598. $out .= ' >>';
  11599. }
  11600. // images or nested xobjects
  11601. if (!empty($data['images']) OR !empty($data['xobjects'])) {
  11602. $out .= ' /XObject <<';
  11603. foreach ($data['images'] as $imgid) {
  11604. $out .= ' /I'.$imgid.' '.$this->xobjects['I'.$imgid]['n'].' 0 R';
  11605. }
  11606. foreach ($data['xobjects'] as $sub_id => $sub_objid) {
  11607. $out .= ' /'.$sub_id.' '.$sub_objid['n'].' 0 R';
  11608. }
  11609. $out .= ' >>';
  11610. }
  11611. $out .= ' >>'; //end resources
  11612. if (isset($data['group']) AND ($data['group'] !== false)) {
  11613. // set transparency group
  11614. $out .= ' /Group << /Type /Group /S /Transparency';
  11615. if (is_array($data['group'])) {
  11616. if (isset($data['group']['CS']) AND !empty($data['group']['CS'])) {
  11617. $out .= ' /CS /'.$data['group']['CS'];
  11618. }
  11619. if (isset($data['group']['I'])) {
  11620. $out .= ' /I /'.($data['group']['I']===true?'true':'false');
  11621. }
  11622. if (isset($data['group']['K'])) {
  11623. $out .= ' /K /'.($data['group']['K']===true?'true':'false');
  11624. }
  11625. }
  11626. $out .= ' >>';
  11627. }
  11628. $stream = $this->_getrawstream($stream, $data['n']);
  11629. $out .= ' /Length '.strlen($stream);
  11630. $out .= ' >>';
  11631. $out .= ' stream'."\n".$stream."\n".'endstream';
  11632. $out .= "\n".'endobj';
  11633. $this->_out($out);
  11634. }
  11635. }
  11636. }
  11637. /**
  11638. * Output Spot Colors Resources.
  11639. * @protected
  11640. * @since 4.0.024 (2008-09-12)
  11641. */
  11642. protected function _putspotcolors() {
  11643. foreach ($this->spot_colors as $name => $color) {
  11644. $this->_newobj();
  11645. $this->spot_colors[$name]['n'] = $this->n;
  11646. $out = '[/Separation /'.str_replace(' ', '#20', $name);
  11647. $out .= ' /DeviceCMYK <<';
  11648. $out .= ' /Range [0 1 0 1 0 1 0 1] /C0 [0 0 0 0]';
  11649. $out .= ' '.sprintf('/C1 [%.4F %.4F %.4F %.4F] ', ($color['C'] / 100), ($color['M'] / 100), ($color['Y'] / 100), ($color['K'] / 100));
  11650. $out .= ' /FunctionType 2 /Domain [0 1] /N 1>>]';
  11651. $out .= "\n".'endobj';
  11652. $this->_out($out);
  11653. }
  11654. }
  11655. /**
  11656. * Return XObjects Dictionary.
  11657. * @return string XObjects dictionary
  11658. * @protected
  11659. * @since 5.8.014 (2010-08-23)
  11660. */
  11661. protected function _getxobjectdict() {
  11662. $out = '';
  11663. foreach ($this->xobjects as $id => $objid) {
  11664. $out .= ' /'.$id.' '.$objid['n'].' 0 R';
  11665. }
  11666. return $out;
  11667. }
  11668. /**
  11669. * Output Resources Dictionary.
  11670. * @protected
  11671. */
  11672. protected function _putresourcedict() {
  11673. $out = $this->_getobj(2)."\n";
  11674. $out .= '<< /ProcSet [/PDF /Text /ImageB /ImageC /ImageI]';
  11675. $out .= ' /Font <<';
  11676. foreach ($this->fontkeys as $fontkey) {
  11677. $font = $this->getFontBuffer($fontkey);
  11678. $out .= ' /F'.$font['i'].' '.$font['n'].' 0 R';
  11679. }
  11680. $out .= ' >>';
  11681. $out .= ' /XObject <<';
  11682. $out .= $this->_getxobjectdict();
  11683. $out .= ' >>';
  11684. // layers
  11685. if (!empty($this->pdflayers)) {
  11686. $out .= ' /Properties <<';
  11687. foreach ($this->pdflayers as $layer) {
  11688. $out .= ' /'.$layer['layer'].' '.$layer['objid'].' 0 R';
  11689. }
  11690. $out .= ' >>';
  11691. }
  11692. if (!$this->pdfa_mode) {
  11693. // transparency
  11694. if (isset($this->extgstates) AND !empty($this->extgstates)) {
  11695. $out .= ' /ExtGState <<';
  11696. foreach ($this->extgstates as $k => $extgstate) {
  11697. if (isset($extgstate['name'])) {
  11698. $out .= ' /'.$extgstate['name'];
  11699. } else {
  11700. $out .= ' /GS'.$k;
  11701. }
  11702. $out .= ' '.$extgstate['n'].' 0 R';
  11703. }
  11704. $out .= ' >>';
  11705. }
  11706. if (isset($this->gradients) AND !empty($this->gradients)) {
  11707. $gp = '';
  11708. $gs = '';
  11709. foreach ($this->gradients as $id => $grad) {
  11710. // gradient patterns
  11711. $gp .= ' /p'.$id.' '.$grad['pattern'].' 0 R';
  11712. // gradient shadings
  11713. $gs .= ' /Sh'.$id.' '.$grad['id'].' 0 R';
  11714. }
  11715. $out .= ' /Pattern <<'.$gp.' >>';
  11716. $out .= ' /Shading <<'.$gs.' >>';
  11717. }
  11718. }
  11719. // spot colors
  11720. if (isset($this->spot_colors) AND !empty($this->spot_colors)) {
  11721. $out .= ' /ColorSpace <<';
  11722. foreach ($this->spot_colors as $color) {
  11723. $out .= ' /CS'.$color['i'].' '.$color['n'].' 0 R';
  11724. }
  11725. $out .= ' >>';
  11726. }
  11727. $out .= ' >>';
  11728. $out .= "\n".'endobj';
  11729. $this->_out($out);
  11730. }
  11731. /**
  11732. * Output Resources.
  11733. * @protected
  11734. */
  11735. protected function _putresources() {
  11736. $this->_putextgstates();
  11737. $this->_putocg();
  11738. $this->_putfonts();
  11739. $this->_putimages();
  11740. $this->_putspotcolors();
  11741. $this->_putshaders();
  11742. $this->_putxobjects();
  11743. $this->_putresourcedict();
  11744. $this->_putdests();
  11745. $this->_putbookmarks();
  11746. $this->_putEmbeddedFiles();
  11747. $this->_putannotsobjs();
  11748. $this->_putjavascript();
  11749. $this->_putencryption();
  11750. }
  11751. /**
  11752. * Adds some Metadata information (Document Information Dictionary)
  11753. * (see Chapter 14.3.3 Document Information Dictionary of PDF32000_2008.pdf Reference)
  11754. * @return int object id
  11755. * @protected
  11756. */
  11757. protected function _putinfo() {
  11758. $oid = $this->_newobj();
  11759. $out = '<<';
  11760. // store current isunicode value
  11761. $prev_isunicode = $this->isunicode;
  11762. if ($this->docinfounicode) {
  11763. $this->isunicode = true;
  11764. }
  11765. if (!$this->empty_string($this->title)) {
  11766. // The document's title.
  11767. $out .= ' /Title '.$this->_textstring($this->title, $oid);
  11768. }
  11769. if (!$this->empty_string($this->author)) {
  11770. // The name of the person who created the document.
  11771. $out .= ' /Author '.$this->_textstring($this->author, $oid);
  11772. }
  11773. if (!$this->empty_string($this->subject)) {
  11774. // The subject of the document.
  11775. $out .= ' /Subject '.$this->_textstring($this->subject, $oid);
  11776. }
  11777. if (!$this->empty_string($this->keywords)) {
  11778. // Keywords associated with the document.
  11779. $out .= ' /Keywords '.$this->_textstring($this->keywords.' TCPDF', $oid);
  11780. }
  11781. if (!$this->empty_string($this->creator)) {
  11782. // 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.
  11783. $out .= ' /Creator '.$this->_textstring($this->creator, $oid);
  11784. }
  11785. // restore previous isunicode value
  11786. $this->isunicode = $prev_isunicode;
  11787. // default producer
  11788. $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);
  11789. // The date and time the document was created, in human-readable form
  11790. $out .= ' /CreationDate '.$this->_datestring();
  11791. // The date and time the document was most recently modified, in human-readable form
  11792. $out .= ' /ModDate '.$this->_datestring();
  11793. // A name object indicating whether the document has been modified to include trapping information
  11794. $out .= ' /Trapped /False';
  11795. $out .= ' >>';
  11796. $out .= "\n".'endobj';
  11797. $this->_out($out);
  11798. return $oid;
  11799. }
  11800. /**
  11801. * Set additional XMP data to be added on the default XMP data just before the end of "x:xmpmeta" tag.
  11802. * IMPORTANT: This data is added as-is without controls, so you have to validate your data before using this method!
  11803. * @param $xmp (string) Custom XMP data.
  11804. * @since 5.9.128 (2011-10-06)
  11805. * @public
  11806. */
  11807. public function setExtraXMP($xmp) {
  11808. $this->custom_xmp = $xmp;
  11809. }
  11810. /**
  11811. * Put XMP data object and return ID.
  11812. * @return (int) The object ID.
  11813. * @since 5.9.121 (2011-09-28)
  11814. * @protected
  11815. */
  11816. protected function _putXMP() {
  11817. $oid = $this->_newobj();
  11818. // store current isunicode value
  11819. $prev_isunicode = $this->isunicode;
  11820. $this->isunicode = true;
  11821. $prev_encrypted = $this->encrypted;
  11822. $this->encrypted = false;
  11823. // set XMP data
  11824. $xmp = '<?xpacket begin="'.$this->unichr(0xfeff).'" id="W5M0MpCehiHzreSzNTczkc9d"?>'."\n";
  11825. $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";
  11826. $xmp .= "\t".'<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">'."\n";
  11827. $xmp .= "\t\t".'<rdf:Description rdf:about="" xmlns:dc="http://purl.org/dc/elements/1.1/">'."\n";
  11828. $xmp .= "\t\t\t".'<dc:format>application/pdf</dc:format>'."\n";
  11829. $xmp .= "\t\t\t".'<dc:title>'."\n";
  11830. $xmp .= "\t\t\t\t".'<rdf:Alt>'."\n";
  11831. $xmp .= "\t\t\t\t\t".'<rdf:li xml:lang="x-default">'.$this->_escapeXML($this->title).'</rdf:li>'."\n";
  11832. $xmp .= "\t\t\t\t".'</rdf:Alt>'."\n";
  11833. $xmp .= "\t\t\t".'</dc:title>'."\n";
  11834. $xmp .= "\t\t\t".'<dc:creator>'."\n";
  11835. $xmp .= "\t\t\t\t".'<rdf:Seq>'."\n";
  11836. $xmp .= "\t\t\t\t\t".'<rdf:li>'.$this->_escapeXML($this->author).'</rdf:li>'."\n";
  11837. $xmp .= "\t\t\t\t".'</rdf:Seq>'."\n";
  11838. $xmp .= "\t\t\t".'</dc:creator>'."\n";
  11839. $xmp .= "\t\t\t".'<dc:description>'."\n";
  11840. $xmp .= "\t\t\t\t".'<rdf:Alt>'."\n";
  11841. $xmp .= "\t\t\t\t\t".'<rdf:li xml:lang="x-default">'.$this->_escapeXML($this->subject).'</rdf:li>'."\n";
  11842. $xmp .= "\t\t\t\t".'</rdf:Alt>'."\n";
  11843. $xmp .= "\t\t\t".'</dc:description>'."\n";
  11844. $xmp .= "\t\t\t".'<dc:subject>'."\n";
  11845. $xmp .= "\t\t\t\t".'<rdf:Bag>'."\n";
  11846. $xmp .= "\t\t\t\t\t".'<rdf:li>'.$this->_escapeXML($this->keywords).'</rdf:li>'."\n";
  11847. $xmp .= "\t\t\t\t".'</rdf:Bag>'."\n";
  11848. $xmp .= "\t\t\t".'</dc:subject>'."\n";
  11849. $xmp .= "\t\t".'</rdf:Description>'."\n";
  11850. // convert date format
  11851. $docdate = substr($this->doc_date, 0, 4).'-'.substr($this->doc_date, 4, 2).'-'.substr($this->doc_date, 6, 2);
  11852. $docdate .= 'T'.substr($this->doc_date, 8, 2).':'.substr($this->doc_date, 10, 2).':'.substr($this->doc_date, 12, 2);
  11853. $docdate .= '+'.substr($this->doc_date, 15, 2).':'.substr($this->doc_date, 18, 2);
  11854. $docdate = $this->_escapeXML($docdate);
  11855. $xmp .= "\t\t".'<rdf:Description rdf:about="" xmlns:xmp="http://ns.adobe.com/xap/1.0/">'."\n";
  11856. $xmp .= "\t\t\t".'<xmp:CreateDate>'.$docdate.'</xmp:CreateDate>'."\n";
  11857. $xmp .= "\t\t\t".'<xmp:CreatorTool>'.$this->creator.'</xmp:CreatorTool>'."\n";
  11858. $xmp .= "\t\t\t".'<xmp:ModifyDate>'.$docdate.'</xmp:ModifyDate>'."\n";
  11859. $xmp .= "\t\t\t".'<xmp:MetadataDate>'.$docdate.'</xmp:MetadataDate>'."\n";
  11860. $xmp .= "\t\t".'</rdf:Description>'."\n";
  11861. $xmp .= "\t\t".'<rdf:Description rdf:about="" xmlns:pdf="http://ns.adobe.com/pdf/1.3/">'."\n";
  11862. $xmp .= "\t\t\t".'<pdf:Keywords>'.$this->_escapeXML($this->keywords).' TCPDF</pdf:Keywords>'."\n";
  11863. $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";
  11864. $xmp .= "\t\t".'</rdf:Description>'."\n";
  11865. $xmp .= "\t\t".'<rdf:Description rdf:about="" xmlns:xmpMM="http://ns.adobe.com/xap/1.0/mm/">'."\n";
  11866. $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);
  11867. $xmp .= "\t\t\t".'<xmpMM:DocumentID>'.$uuid.'</xmpMM:DocumentID>'."\n";
  11868. $xmp .= "\t\t\t".'<xmpMM:InstanceID>'.$uuid.'</xmpMM:InstanceID>'."\n";
  11869. $xmp .= "\t\t".'</rdf:Description>'."\n";
  11870. if ($this->pdfa_mode) {
  11871. $xmp .= "\t\t".'<rdf:Description rdf:about="" xmlns:pdfaid="http://www.aiim.org/pdfa/ns/id/">'."\n";
  11872. $xmp .= "\t\t\t".'<pdfaid:part>1</pdfaid:part>'."\n";
  11873. $xmp .= "\t\t\t".'<pdfaid:conformance>B</pdfaid:conformance>'."\n";
  11874. $xmp .= "\t\t".'</rdf:Description>'."\n";
  11875. }
  11876. // XMP extension schemas
  11877. $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";
  11878. $xmp .= "\t\t\t".'<pdfaExtension:schemas>'."\n";
  11879. $xmp .= "\t\t\t\t".'<rdf:Bag>'."\n";
  11880. $xmp .= "\t\t\t\t\t".'<rdf:li rdf:parseType="Resource">'."\n";
  11881. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:namespaceURI>http://ns.adobe.com/pdf/1.3/</pdfaSchema:namespaceURI>'."\n";
  11882. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:prefix>pdf</pdfaSchema:prefix>'."\n";
  11883. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:schema>Adobe PDF Schema</pdfaSchema:schema>'."\n";
  11884. $xmp .= "\t\t\t\t\t".'</rdf:li>'."\n";
  11885. $xmp .= "\t\t\t\t\t".'<rdf:li rdf:parseType="Resource">'."\n";
  11886. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:namespaceURI>http://ns.adobe.com/xap/1.0/mm/</pdfaSchema:namespaceURI>'."\n";
  11887. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:prefix>xmpMM</pdfaSchema:prefix>'."\n";
  11888. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:schema>XMP Media Management Schema</pdfaSchema:schema>'."\n";
  11889. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:property>'."\n";
  11890. $xmp .= "\t\t\t\t\t\t\t".'<rdf:Seq>'."\n";
  11891. $xmp .= "\t\t\t\t\t\t\t\t".'<rdf:li rdf:parseType="Resource">'."\n";
  11892. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:category>internal</pdfaProperty:category>'."\n";
  11893. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:description>UUID based identifier for specific incarnation of a document</pdfaProperty:description>'."\n";
  11894. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:name>InstanceID</pdfaProperty:name>'."\n";
  11895. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:valueType>URI</pdfaProperty:valueType>'."\n";
  11896. $xmp .= "\t\t\t\t\t\t\t\t".'</rdf:li>'."\n";
  11897. $xmp .= "\t\t\t\t\t\t\t".'</rdf:Seq>'."\n";
  11898. $xmp .= "\t\t\t\t\t\t".'</pdfaSchema:property>'."\n";
  11899. $xmp .= "\t\t\t\t\t".'</rdf:li>'."\n";
  11900. $xmp .= "\t\t\t\t\t".'<rdf:li rdf:parseType="Resource">'."\n";
  11901. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:namespaceURI>http://www.aiim.org/pdfa/ns/id/</pdfaSchema:namespaceURI>'."\n";
  11902. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:prefix>pdfaid</pdfaSchema:prefix>'."\n";
  11903. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:schema>PDF/A ID Schema</pdfaSchema:schema>'."\n";
  11904. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:property>'."\n";
  11905. $xmp .= "\t\t\t\t\t\t\t".'<rdf:Seq>'."\n";
  11906. $xmp .= "\t\t\t\t\t\t\t\t".'<rdf:li rdf:parseType="Resource">'."\n";
  11907. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:category>internal</pdfaProperty:category>'."\n";
  11908. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:description>Part of PDF/A standard</pdfaProperty:description>'."\n";
  11909. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:name>part</pdfaProperty:name>'."\n";
  11910. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:valueType>Integer</pdfaProperty:valueType>'."\n";
  11911. $xmp .= "\t\t\t\t\t\t\t\t".'</rdf:li>'."\n";
  11912. $xmp .= "\t\t\t\t\t\t\t\t".'<rdf:li rdf:parseType="Resource">'."\n";
  11913. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:category>internal</pdfaProperty:category>'."\n";
  11914. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:description>Amendment of PDF/A standard</pdfaProperty:description>'."\n";
  11915. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:name>amd</pdfaProperty:name>'."\n";
  11916. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:valueType>Text</pdfaProperty:valueType>'."\n";
  11917. $xmp .= "\t\t\t\t\t\t\t\t".'</rdf:li>'."\n";
  11918. $xmp .= "\t\t\t\t\t\t\t\t".'<rdf:li rdf:parseType="Resource">'."\n";
  11919. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:category>internal</pdfaProperty:category>'."\n";
  11920. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:description>Conformance level of PDF/A standard</pdfaProperty:description>'."\n";
  11921. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:name>conformance</pdfaProperty:name>'."\n";
  11922. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:valueType>Text</pdfaProperty:valueType>'."\n";
  11923. $xmp .= "\t\t\t\t\t\t\t\t".'</rdf:li>'."\n";
  11924. $xmp .= "\t\t\t\t\t\t\t".'</rdf:Seq>'."\n";
  11925. $xmp .= "\t\t\t\t\t\t".'</pdfaSchema:property>'."\n";
  11926. $xmp .= "\t\t\t\t\t".'</rdf:li>'."\n";
  11927. $xmp .= "\t\t\t\t".'</rdf:Bag>'."\n";
  11928. $xmp .= "\t\t\t".'</pdfaExtension:schemas>'."\n";
  11929. $xmp .= "\t\t".'</rdf:Description>'."\n";
  11930. $xmp .= "\t".'</rdf:RDF>'."\n";
  11931. $xmp .= $this->custom_xmp;
  11932. $xmp .= '</x:xmpmeta>'."\n";
  11933. $xmp .= '<?xpacket end="w"?>';
  11934. $out = '<< /Type /Metadata /Subtype /XML /Length '.strlen($xmp).' >> stream'."\n".$xmp."\n".'endstream'."\n".'endobj';
  11935. // restore previous isunicode value
  11936. $this->isunicode = $prev_isunicode;
  11937. $this->encrypted = $prev_encrypted;
  11938. $this->_out($out);
  11939. return $oid;
  11940. }
  11941. /**
  11942. * Output Catalog.
  11943. * @return int object id
  11944. * @protected
  11945. */
  11946. protected function _putcatalog() {
  11947. // put XMP
  11948. $xmpobj = $this->_putXMP();
  11949. // if required, add standard sRGB_IEC61966-2.1 blackscaled ICC colour profile
  11950. if ($this->pdfa_mode OR $this->force_srgb) {
  11951. $iccobj = $this->_newobj();
  11952. $icc = file_get_contents(dirname(__FILE__).'/sRGB.icc');
  11953. $filter = '';
  11954. if ($this->compress) {
  11955. $filter = ' /Filter /FlateDecode';
  11956. $icc = gzcompress($icc);
  11957. }
  11958. $icc = $this->_getrawstream($icc);
  11959. $this->_out('<</N 3 '.$filter.'/Length '.strlen($icc).'>> stream'."\n".$icc."\n".'endstream'."\n".'endobj');
  11960. }
  11961. // start catalog
  11962. $oid = $this->_newobj();
  11963. $out = '<< /Type /Catalog';
  11964. $out .= ' /Version /'.$this->PDFVersion;
  11965. //$out .= ' /Extensions <<>>';
  11966. $out .= ' /Pages 1 0 R';
  11967. //$out .= ' /PageLabels ' //...;
  11968. $out .= ' /Names <<';
  11969. if ((!$this->pdfa_mode) AND ((!empty($this->javascript)) OR (!empty($this->js_objects)))) {
  11970. $out .= ' /JavaScript '.($this->n_js).' 0 R';
  11971. }
  11972. $out .= ' >>';
  11973. if (!empty($this->dests)) {
  11974. $out .= ' /Dests '.$this->n_dests.' 0 R';
  11975. }
  11976. $out .= $this->_putviewerpreferences();
  11977. if (isset($this->LayoutMode) AND (!$this->empty_string($this->LayoutMode))) {
  11978. $out .= ' /PageLayout /'.$this->LayoutMode;
  11979. }
  11980. if (isset($this->PageMode) AND (!$this->empty_string($this->PageMode))) {
  11981. $out .= ' /PageMode /'.$this->PageMode;
  11982. }
  11983. if (count($this->outlines) > 0) {
  11984. $out .= ' /Outlines '.$this->OutlineRoot.' 0 R';
  11985. $out .= ' /PageMode /UseOutlines';
  11986. }
  11987. //$out .= ' /Threads []';
  11988. if ($this->ZoomMode == 'fullpage') {
  11989. $out .= ' /OpenAction ['.$this->page_obj_id[1].' 0 R /Fit]';
  11990. } elseif ($this->ZoomMode == 'fullwidth') {
  11991. $out .= ' /OpenAction ['.$this->page_obj_id[1].' 0 R /FitH null]';
  11992. } elseif ($this->ZoomMode == 'real') {
  11993. $out .= ' /OpenAction ['.$this->page_obj_id[1].' 0 R /XYZ null null 1]';
  11994. } elseif (!is_string($this->ZoomMode)) {
  11995. $out .= sprintf(' /OpenAction ['.$this->page_obj_id[1].' 0 R /XYZ null null %.2F]', ($this->ZoomMode / 100));
  11996. }
  11997. //$out .= ' /AA <<>>';
  11998. //$out .= ' /URI <<>>';
  11999. $out .= ' /Metadata '.$xmpobj.' 0 R';
  12000. //$out .= ' /StructTreeRoot <<>>';
  12001. //$out .= ' /MarkInfo <<>>';
  12002. if (isset($this->l['a_meta_language'])) {
  12003. $out .= ' /Lang '.$this->_textstring($this->l['a_meta_language'], $oid);
  12004. }
  12005. //$out .= ' /SpiderInfo <<>>';
  12006. // set OutputIntent to sRGB IEC61966-2.1 if required
  12007. if ($this->pdfa_mode OR $this->force_srgb) {
  12008. $out .= ' /OutputIntents [<<';
  12009. $out .= ' /Type /OutputIntent';
  12010. $out .= ' /S /GTS_PDFA1';
  12011. $out .= ' /OutputCondition '.$this->_textstring('sRGB IEC61966-2.1', $oid);
  12012. $out .= ' /OutputConditionIdentifier '.$this->_textstring('sRGB IEC61966-2.1', $oid);
  12013. $out .= ' /RegistryName '.$this->_textstring('http://www.color.org', $oid);
  12014. $out .= ' /Info '.$this->_textstring('sRGB IEC61966-2.1', $oid);
  12015. $out .= ' /DestOutputProfile '.$iccobj.' 0 R';
  12016. $out .= ' >>]';
  12017. }
  12018. //$out .= ' /PieceInfo <<>>';
  12019. if (!empty($this->pdflayers)) {
  12020. $lyrobjs = '';
  12021. $lyrobjs_print = '';
  12022. $lyrobjs_view = '';
  12023. foreach ($this->pdflayers as $layer) {
  12024. $lyrobjs .= ' '.$layer['objid'].' 0 R';
  12025. if ($layer['print']) {
  12026. $lyrobjs_print .= ' '.$layer['objid'].' 0 R';
  12027. }
  12028. if ($layer['view']) {
  12029. $lyrobjs_view .= ' '.$layer['objid'].' 0 R';
  12030. }
  12031. }
  12032. $out .= ' /OCProperties << /OCGs ['.$lyrobjs.']';
  12033. $out .= ' /D <<';
  12034. $out .= ' /Name '.$this->_textstring('Layers', $oid);
  12035. $out .= ' /Creator '.$this->_textstring('TCPDF', $oid);
  12036. $out .= ' /BaseState /ON';
  12037. $out .= ' /ON ['.$lyrobjs_print.']';
  12038. $out .= ' /OFF ['.$lyrobjs_view.']';
  12039. $out .= ' /Intent /View';
  12040. $out .= ' /AS [';
  12041. $out .= ' << /Event /Print /OCGs ['.$lyrobjs.'] /Category [/Print] >>';
  12042. $out .= ' << /Event /View /OCGs ['.$lyrobjs.'] /Category [/View] >>';
  12043. $out .= ' ]';
  12044. $out .= ' /Order ['.$lyrobjs.']';
  12045. $out .= ' /ListMode /AllPages';
  12046. //$out .= ' /RBGroups ['..']';
  12047. //$out .= ' /Locked ['..']';
  12048. $out .= ' >>';
  12049. $out .= ' >>';
  12050. }
  12051. // AcroForm
  12052. if (!empty($this->form_obj_id) OR ($this->sign AND isset($this->signature_data['cert_type']))) {
  12053. $out .= ' /AcroForm <<';
  12054. $objrefs = '';
  12055. if ($this->sign AND isset($this->signature_data['cert_type'])) {
  12056. // set reference for signature object
  12057. $objrefs .= $this->sig_obj_id.' 0 R';
  12058. }
  12059. if (!empty($this->empty_signature_appearance)) {
  12060. foreach ($this->empty_signature_appearance as $esa) {
  12061. // set reference for empty signature objects
  12062. $objrefs .= ' '.$esa['objid'].' 0 R';
  12063. }
  12064. }
  12065. if (!empty($this->form_obj_id)) {
  12066. foreach($this->form_obj_id as $objid) {
  12067. $objrefs .= ' '.$objid.' 0 R';
  12068. }
  12069. }
  12070. $out .= ' /Fields ['.$objrefs.']';
  12071. // It's better to turn off this value and set the appearance stream for each annotation (/AP) to avoid conflicts with signature fields.
  12072. $out .= ' /NeedAppearances false';
  12073. if ($this->sign AND isset($this->signature_data['cert_type'])) {
  12074. if ($this->signature_data['cert_type'] > 0) {
  12075. $out .= ' /SigFlags 3';
  12076. } else {
  12077. $out .= ' /SigFlags 1';
  12078. }
  12079. }
  12080. //$out .= ' /CO ';
  12081. if (isset($this->annotation_fonts) AND !empty($this->annotation_fonts)) {
  12082. $out .= ' /DR <<';
  12083. $out .= ' /Font <<';
  12084. foreach ($this->annotation_fonts as $fontkey => $fontid) {
  12085. $out .= ' /F'.$fontid.' '.$this->font_obj_ids[$fontkey].' 0 R';
  12086. }
  12087. $out .= ' >> >>';
  12088. }
  12089. $font = $this->getFontBuffer('helvetica');
  12090. $out .= ' /DA (/F'.$font['i'].' 0 Tf 0 g)';
  12091. $out .= ' /Q '.(($this->rtl)?'2':'0');
  12092. //$out .= ' /XFA ';
  12093. $out .= ' >>';
  12094. // signatures
  12095. if ($this->sign AND isset($this->signature_data['cert_type'])) {
  12096. if ($this->signature_data['cert_type'] > 0) {
  12097. $out .= ' /Perms << /DocMDP '.($this->sig_obj_id + 1).' 0 R >>';
  12098. } else {
  12099. $out .= ' /Perms << /UR3 '.($this->sig_obj_id + 1).' 0 R >>';
  12100. }
  12101. }
  12102. }
  12103. //$out .= ' /Legal <<>>';
  12104. //$out .= ' /Requirements []';
  12105. //$out .= ' /Collection <<>>';
  12106. //$out .= ' /NeedsRendering true';
  12107. $out .= ' >>';
  12108. $out .= "\n".'endobj';
  12109. $this->_out($out);
  12110. return $oid;
  12111. }
  12112. /**
  12113. * Output viewer preferences.
  12114. * @return string for viewer preferences
  12115. * @author Nicola asuni
  12116. * @since 3.1.000 (2008-06-09)
  12117. * @protected
  12118. */
  12119. protected function _putviewerpreferences() {
  12120. $out = ' /ViewerPreferences <<';
  12121. if ($this->rtl) {
  12122. $out .= ' /Direction /R2L';
  12123. } else {
  12124. $out .= ' /Direction /L2R';
  12125. }
  12126. if (isset($this->viewer_preferences['HideToolbar']) AND ($this->viewer_preferences['HideToolbar'])) {
  12127. $out .= ' /HideToolbar true';
  12128. }
  12129. if (isset($this->viewer_preferences['HideMenubar']) AND ($this->viewer_preferences['HideMenubar'])) {
  12130. $out .= ' /HideMenubar true';
  12131. }
  12132. if (isset($this->viewer_preferences['HideWindowUI']) AND ($this->viewer_preferences['HideWindowUI'])) {
  12133. $out .= ' /HideWindowUI true';
  12134. }
  12135. if (isset($this->viewer_preferences['FitWindow']) AND ($this->viewer_preferences['FitWindow'])) {
  12136. $out .= ' /FitWindow true';
  12137. }
  12138. if (isset($this->viewer_preferences['CenterWindow']) AND ($this->viewer_preferences['CenterWindow'])) {
  12139. $out .= ' /CenterWindow true';
  12140. }
  12141. if (isset($this->viewer_preferences['DisplayDocTitle']) AND ($this->viewer_preferences['DisplayDocTitle'])) {
  12142. $out .= ' /DisplayDocTitle true';
  12143. }
  12144. if (isset($this->viewer_preferences['NonFullScreenPageMode'])) {
  12145. $out .= ' /NonFullScreenPageMode /'.$this->viewer_preferences['NonFullScreenPageMode'];
  12146. }
  12147. if (isset($this->viewer_preferences['ViewArea'])) {
  12148. $out .= ' /ViewArea /'.$this->viewer_preferences['ViewArea'];
  12149. }
  12150. if (isset($this->viewer_preferences['ViewClip'])) {
  12151. $out .= ' /ViewClip /'.$this->viewer_preferences['ViewClip'];
  12152. }
  12153. if (isset($this->viewer_preferences['PrintArea'])) {
  12154. $out .= ' /PrintArea /'.$this->viewer_preferences['PrintArea'];
  12155. }
  12156. if (isset($this->viewer_preferences['PrintClip'])) {
  12157. $out .= ' /PrintClip /'.$this->viewer_preferences['PrintClip'];
  12158. }
  12159. if (isset($this->viewer_preferences['PrintScaling'])) {
  12160. $out .= ' /PrintScaling /'.$this->viewer_preferences['PrintScaling'];
  12161. }
  12162. if (isset($this->viewer_preferences['Duplex']) AND (!$this->empty_string($this->viewer_preferences['Duplex']))) {
  12163. $out .= ' /Duplex /'.$this->viewer_preferences['Duplex'];
  12164. }
  12165. if (isset($this->viewer_preferences['PickTrayByPDFSize'])) {
  12166. if ($this->viewer_preferences['PickTrayByPDFSize']) {
  12167. $out .= ' /PickTrayByPDFSize true';
  12168. } else {
  12169. $out .= ' /PickTrayByPDFSize false';
  12170. }
  12171. }
  12172. if (isset($this->viewer_preferences['PrintPageRange'])) {
  12173. $PrintPageRangeNum = '';
  12174. foreach ($this->viewer_preferences['PrintPageRange'] as $k => $v) {
  12175. $PrintPageRangeNum .= ' '.($v - 1).'';
  12176. }
  12177. $out .= ' /PrintPageRange ['.substr($PrintPageRangeNum,1).']';
  12178. }
  12179. if (isset($this->viewer_preferences['NumCopies'])) {
  12180. $out .= ' /NumCopies '.intval($this->viewer_preferences['NumCopies']);
  12181. }
  12182. $out .= ' >>';
  12183. return $out;
  12184. }
  12185. /**
  12186. * Output PDF File Header (7.5.2).
  12187. * @protected
  12188. */
  12189. protected function _putheader() {
  12190. $this->_out('%PDF-'.$this->PDFVersion);
  12191. $this->_out('%'.chr(0xe2).chr(0xe3).chr(0xcf).chr(0xd3));
  12192. }
  12193. /**
  12194. * Output end of document (EOF).
  12195. * @protected
  12196. */
  12197. protected function _enddoc() {
  12198. $this->state = 1;
  12199. $this->_putheader();
  12200. $this->_putpages();
  12201. $this->_putresources();
  12202. // empty signature fields
  12203. if (!empty($this->empty_signature_appearance)) {
  12204. foreach ($this->empty_signature_appearance as $key => $esa) {
  12205. // widget annotation for empty signature
  12206. $out = $this->_getobj($esa['objid'])."\n";
  12207. $out .= '<< /Type /Annot';
  12208. $out .= ' /Subtype /Widget';
  12209. $out .= ' /Rect ['.$esa['rect'].']';
  12210. $out .= ' /P '.$this->page_obj_id[($esa['page'])].' 0 R'; // link to signature appearance page
  12211. $out .= ' /F 4';
  12212. $out .= ' /FT /Sig';
  12213. $signame = sprintf('Signature_%03d', ($key + 1));
  12214. $out .= ' /T '.$this->_textstring($signame, $esa['objid']);
  12215. $out .= ' /Ff 0';
  12216. $out .= ' >>';
  12217. $out .= "\n".'endobj';
  12218. $this->_out($out);
  12219. }
  12220. }
  12221. // Signature
  12222. if ($this->sign AND isset($this->signature_data['cert_type'])) {
  12223. // widget annotation for signature
  12224. $out = $this->_getobj($this->sig_obj_id)."\n";
  12225. $out .= '<< /Type /Annot';
  12226. $out .= ' /Subtype /Widget';
  12227. $out .= ' /Rect ['.$this->signature_appearance['rect'].']';
  12228. $out .= ' /P '.$this->page_obj_id[($this->signature_appearance['page'])].' 0 R'; // link to signature appearance page
  12229. $out .= ' /F 4';
  12230. $out .= ' /FT /Sig';
  12231. $out .= ' /T '.$this->_textstring('Signature_000', $this->sig_obj_id);
  12232. $out .= ' /Ff 0';
  12233. $out .= ' /V '.($this->sig_obj_id + 1).' 0 R';
  12234. $out .= ' >>';
  12235. $out .= "\n".'endobj';
  12236. $this->_out($out);
  12237. // signature
  12238. $this->_putsignature();
  12239. }
  12240. // Info
  12241. $objid_info = $this->_putinfo();
  12242. // Catalog
  12243. $objid_catalog = $this->_putcatalog();
  12244. // Cross-ref
  12245. $o = $this->bufferlen;
  12246. // XREF section
  12247. $this->_out('xref');
  12248. $this->_out('0 '.($this->n + 1));
  12249. $this->_out('0000000000 65535 f ');
  12250. for ($i=1; $i <= $this->n; ++$i) {
  12251. if (!isset($this->offsets[$i]) AND ($i > 1)) {
  12252. $this->offsets[$i] = $this->offsets[($i - 1)];
  12253. }
  12254. $this->_out(sprintf('%010d 00000 n ', $this->offsets[$i]));
  12255. }
  12256. // TRAILER
  12257. $out = 'trailer'."\n";
  12258. $out .= '<<';
  12259. $out .= ' /Size '.($this->n + 1);
  12260. $out .= ' /Root '.$objid_catalog.' 0 R';
  12261. $out .= ' /Info '.$objid_info.' 0 R';
  12262. if ($this->encrypted) {
  12263. $out .= ' /Encrypt '.$this->encryptdata['objid'].' 0 R';
  12264. }
  12265. $out .= ' /ID [ <'.$this->file_id.'> <'.$this->file_id.'> ]';
  12266. $out .= ' >>';
  12267. $this->_out($out);
  12268. $this->_out('startxref');
  12269. $this->_out($o);
  12270. $this->_out('%%EOF');
  12271. $this->state = 3; // end-of-doc
  12272. if ($this->diskcache) {
  12273. // remove temporary files used for images
  12274. foreach ($this->imagekeys as $key) {
  12275. // remove temporary files
  12276. unlink($this->images[$key]);
  12277. }
  12278. foreach ($this->fontkeys as $key) {
  12279. // remove temporary files
  12280. unlink($this->fonts[$key]);
  12281. }
  12282. }
  12283. }
  12284. /**
  12285. * Initialize a new page.
  12286. * @param $orientation (string) page orientation. Possible values are (case insensitive):<ul><li>P or PORTRAIT (default)</li><li>L or LANDSCAPE</li></ul>
  12287. * @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().
  12288. * @protected
  12289. * @see getPageSizeFromFormat(), setPageFormat()
  12290. */
  12291. protected function _beginpage($orientation='', $format='') {
  12292. ++$this->page;
  12293. $this->setPageBuffer($this->page, '');
  12294. // initialize array for graphics tranformation positions inside a page buffer
  12295. $this->transfmrk[$this->page] = array();
  12296. $this->state = 2;
  12297. if ($this->empty_string($orientation)) {
  12298. if (isset($this->CurOrientation)) {
  12299. $orientation = $this->CurOrientation;
  12300. } elseif ($this->fwPt > $this->fhPt) {
  12301. // landscape
  12302. $orientation = 'L';
  12303. } else {
  12304. // portrait
  12305. $orientation = 'P';
  12306. }
  12307. }
  12308. if ($this->empty_string($format)) {
  12309. $this->pagedim[$this->page] = $this->pagedim[($this->page - 1)];
  12310. $this->setPageOrientation($orientation);
  12311. } else {
  12312. $this->setPageFormat($format, $orientation);
  12313. }
  12314. if ($this->rtl) {
  12315. $this->x = $this->w - $this->rMargin;
  12316. } else {
  12317. $this->x = $this->lMargin;
  12318. }
  12319. $this->y = $this->tMargin;
  12320. if (isset($this->newpagegroup[$this->page])) {
  12321. // start a new group
  12322. $this->currpagegroup = $this->newpagegroup[$this->page];
  12323. $this->pagegroups[$this->currpagegroup] = 1;
  12324. } elseif (isset($this->currpagegroup) AND ($this->currpagegroup > 0)) {
  12325. ++$this->pagegroups[$this->currpagegroup];
  12326. }
  12327. }
  12328. /**
  12329. * Mark end of page.
  12330. * @protected
  12331. */
  12332. protected function _endpage() {
  12333. $this->setVisibility('all');
  12334. $this->state = 1;
  12335. }
  12336. /**
  12337. * Begin a new object and return the object number.
  12338. * @return int object number
  12339. * @protected
  12340. */
  12341. protected function _newobj() {
  12342. $this->_out($this->_getobj());
  12343. return $this->n;
  12344. }
  12345. /**
  12346. * Return the starting object string for the selected object ID.
  12347. * @param $objid (int) Object ID (leave empty to get a new ID).
  12348. * @return string the starting object string
  12349. * @protected
  12350. * @since 5.8.009 (2010-08-20)
  12351. */
  12352. protected function _getobj($objid='') {
  12353. if ($objid === '') {
  12354. ++$this->n;
  12355. $objid = $this->n;
  12356. }
  12357. $this->offsets[$objid] = $this->bufferlen;
  12358. return $objid.' 0 obj';
  12359. }
  12360. /**
  12361. * Underline text.
  12362. * @param $x (int) X coordinate
  12363. * @param $y (int) Y coordinate
  12364. * @param $txt (string) text to underline
  12365. * @protected
  12366. */
  12367. protected function _dounderline($x, $y, $txt) {
  12368. $w = $this->GetStringWidth($txt);
  12369. return $this->_dounderlinew($x, $y, $w);
  12370. }
  12371. /**
  12372. * Underline for rectangular text area.
  12373. * @param $x (int) X coordinate
  12374. * @param $y (int) Y coordinate
  12375. * @param $w (int) width to underline
  12376. * @protected
  12377. * @since 4.8.008 (2009-09-29)
  12378. */
  12379. protected function _dounderlinew($x, $y, $w) {
  12380. $linew = - $this->CurrentFont['ut'] / 1000 * $this->FontSizePt;
  12381. return sprintf('%.2F %.2F %.2F %.2F re f', $x * $this->k, ((($this->h - $y) * $this->k) + $linew), $w * $this->k, $linew);
  12382. }
  12383. /**
  12384. * Line through text.
  12385. * @param $x (int) X coordinate
  12386. * @param $y (int) Y coordinate
  12387. * @param $txt (string) text to linethrough
  12388. * @protected
  12389. */
  12390. protected function _dolinethrough($x, $y, $txt) {
  12391. $w = $this->GetStringWidth($txt);
  12392. return $this->_dolinethroughw($x, $y, $w);
  12393. }
  12394. /**
  12395. * Line through for rectangular text area.
  12396. * @param $x (int) X coordinate
  12397. * @param $y (int) Y coordinate
  12398. * @param $w (int) line length (width)
  12399. * @protected
  12400. * @since 4.9.008 (2009-09-29)
  12401. */
  12402. protected function _dolinethroughw($x, $y, $w) {
  12403. $linew = - $this->CurrentFont['ut'] / 1000 * $this->FontSizePt;
  12404. return sprintf('%.2F %.2F %.2F %.2F re f', $x * $this->k, ((($this->h - $y) * $this->k) + $linew + ($this->FontSizePt / 3)), $w * $this->k, $linew);
  12405. }
  12406. /**
  12407. * Overline text.
  12408. * @param $x (int) X coordinate
  12409. * @param $y (int) Y coordinate
  12410. * @param $txt (string) text to overline
  12411. * @protected
  12412. * @since 4.9.015 (2010-04-19)
  12413. */
  12414. protected function _dooverline($x, $y, $txt) {
  12415. $w = $this->GetStringWidth($txt);
  12416. return $this->_dooverlinew($x, $y, $w);
  12417. }
  12418. /**
  12419. * Overline for rectangular text area.
  12420. * @param $x (int) X coordinate
  12421. * @param $y (int) Y coordinate
  12422. * @param $w (int) width to overline
  12423. * @protected
  12424. * @since 4.9.015 (2010-04-19)
  12425. */
  12426. protected function _dooverlinew($x, $y, $w) {
  12427. $linew = - $this->CurrentFont['ut'] / 1000 * $this->FontSizePt;
  12428. return sprintf('%.2F %.2F %.2F %.2F re f', $x * $this->k, (($this->h - $y + $this->FontAscent) * $this->k) - $linew, $w * $this->k, $linew);
  12429. }
  12430. /**
  12431. * Read a 4-byte (32 bit) integer from file.
  12432. * @param $f (string) file name.
  12433. * @return 4-byte integer
  12434. * @protected
  12435. */
  12436. protected function _freadint($f) {
  12437. $a = unpack('Ni', fread($f, 4));
  12438. return $a['i'];
  12439. }
  12440. /**
  12441. * Add "\" before "\", "(" and ")"
  12442. * @param $s (string) string to escape.
  12443. * @return string escaped string.
  12444. * @protected
  12445. */
  12446. protected function _escape($s) {
  12447. // the chr(13) substitution fixes the Bugs item #1421290.
  12448. return strtr($s, array(')' => '\\)', '(' => '\\(', '\\' => '\\\\', chr(13) => '\r'));
  12449. }
  12450. /**
  12451. * Format a data string for meta information
  12452. * @param $s (string) data string to escape.
  12453. * @param $n (int) object ID
  12454. * @return string escaped string.
  12455. * @protected
  12456. */
  12457. protected function _datastring($s, $n=0) {
  12458. if ($n == 0) {
  12459. $n = $this->n;
  12460. }
  12461. $s = $this->_encrypt_data($n, $s);
  12462. return '('. $this->_escape($s).')';
  12463. }
  12464. /**
  12465. * Returns a formatted date for meta information
  12466. * @param $n (int) object ID
  12467. * @return string escaped date string.
  12468. * @protected
  12469. * @since 4.6.028 (2009-08-25)
  12470. */
  12471. protected function _datestring($n=0) {
  12472. return $this->_datastring('D:'.$this->doc_date, $n);
  12473. }
  12474. /**
  12475. * Format a text string for meta information
  12476. * @param $s (string) string to escape.
  12477. * @param $n (int) object ID
  12478. * @return string escaped string.
  12479. * @protected
  12480. */
  12481. protected function _textstring($s, $n=0) {
  12482. if ($this->isunicode) {
  12483. //Convert string to UTF-16BE
  12484. $s = $this->UTF8ToUTF16BE($s, true);
  12485. }
  12486. return $this->_datastring($s, $n);
  12487. }
  12488. /**
  12489. * THIS METHOD IS DEPRECATED
  12490. * Format a text string
  12491. * @param $s (string) string to escape.
  12492. * @return string escaped string.
  12493. * @protected
  12494. * @deprecated
  12495. */
  12496. protected function _escapetext($s) {
  12497. if ($this->isunicode) {
  12498. if (($this->CurrentFont['type'] == 'core') OR ($this->CurrentFont['type'] == 'TrueType') OR ($this->CurrentFont['type'] == 'Type1')) {
  12499. $s = $this->UTF8ToLatin1($s);
  12500. } else {
  12501. //Convert string to UTF-16BE and reverse RTL language
  12502. $s = $this->utf8StrRev($s, false, $this->tmprtl);
  12503. }
  12504. }
  12505. return $this->_escape($s);
  12506. }
  12507. /**
  12508. * Escape some special characters (&lt; &gt; &amp;) for XML output.
  12509. * @param $str (string) Input string to convert.
  12510. * @return converted string
  12511. * @since 5.9.121 (2011-09-28)
  12512. * @protected
  12513. */
  12514. protected function _escapeXML($str) {
  12515. $replaceTable = array("\0" => '', '&' => '&amp;', '<' => '&lt;', '>' => '&gt;');
  12516. $str = strtr($str, $replaceTable);
  12517. return $str;
  12518. }
  12519. /**
  12520. * get raw output stream.
  12521. * @param $s (string) string to output.
  12522. * @param $n (int) object reference for encryption mode
  12523. * @protected
  12524. * @author Nicola Asuni
  12525. * @since 5.5.000 (2010-06-22)
  12526. */
  12527. protected function _getrawstream($s, $n=0) {
  12528. if ($n <= 0) {
  12529. // default to current object
  12530. $n = $this->n;
  12531. }
  12532. return $this->_encrypt_data($n, $s);
  12533. }
  12534. /**
  12535. * Format output stream (DEPRECATED).
  12536. * @param $s (string) string to output.
  12537. * @param $n (int) object reference for encryption mode
  12538. * @protected
  12539. * @deprecated
  12540. */
  12541. protected function _getstream($s, $n=0) {
  12542. return 'stream'."\n".$this->_getrawstream($s, $n)."\n".'endstream';
  12543. }
  12544. /**
  12545. * Output a stream (DEPRECATED).
  12546. * @param $s (string) string to output.
  12547. * @param $n (int) object reference for encryption mode
  12548. * @protected
  12549. * @deprecated
  12550. */
  12551. protected function _putstream($s, $n=0) {
  12552. $this->_out($this->_getstream($s, $n));
  12553. }
  12554. /**
  12555. * Output a string to the document.
  12556. * @param $s (string) string to output.
  12557. * @protected
  12558. */
  12559. protected function _out($s) {
  12560. if ($this->state == 2) {
  12561. if ($this->inxobj) {
  12562. // we are inside an XObject template
  12563. $this->xobjects[$this->xobjid]['outdata'] .= $s."\n";
  12564. } elseif ((!$this->InFooter) AND isset($this->footerlen[$this->page]) AND ($this->footerlen[$this->page] > 0)) {
  12565. // puts data before page footer
  12566. $pagebuff = $this->getPageBuffer($this->page);
  12567. $page = substr($pagebuff, 0, -$this->footerlen[$this->page]);
  12568. $footer = substr($pagebuff, -$this->footerlen[$this->page]);
  12569. $this->setPageBuffer($this->page, $page.$s."\n".$footer);
  12570. // update footer position
  12571. $this->footerpos[$this->page] += strlen($s."\n");
  12572. } else {
  12573. $this->setPageBuffer($this->page, $s."\n", true);
  12574. }
  12575. } else {
  12576. $this->setBuffer($s."\n");
  12577. }
  12578. }
  12579. /**
  12580. * Converts UTF-8 strings to codepoints array.<br>
  12581. * Invalid byte sequences will be replaced with 0xFFFD (replacement character)<br>
  12582. * Based on: http://www.faqs.org/rfcs/rfc3629.html
  12583. * <pre>
  12584. * Char. number range | UTF-8 octet sequence
  12585. * (hexadecimal) | (binary)
  12586. * --------------------+-----------------------------------------------
  12587. * 0000 0000-0000 007F | 0xxxxxxx
  12588. * 0000 0080-0000 07FF | 110xxxxx 10xxxxxx
  12589. * 0000 0800-0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx
  12590. * 0001 0000-0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
  12591. * ---------------------------------------------------------------------
  12592. *
  12593. * ABFN notation:
  12594. * ---------------------------------------------------------------------
  12595. * UTF8-octets = *( UTF8-char )
  12596. * UTF8-char = UTF8-1 / UTF8-2 / UTF8-3 / UTF8-4
  12597. * UTF8-1 = %x00-7F
  12598. * UTF8-2 = %xC2-DF UTF8-tail
  12599. *
  12600. * UTF8-3 = %xE0 %xA0-BF UTF8-tail / %xE1-EC 2( UTF8-tail ) /
  12601. * %xED %x80-9F UTF8-tail / %xEE-EF 2( UTF8-tail )
  12602. * UTF8-4 = %xF0 %x90-BF 2( UTF8-tail ) / %xF1-F3 3( UTF8-tail ) /
  12603. * %xF4 %x80-8F 2( UTF8-tail )
  12604. * UTF8-tail = %x80-BF
  12605. * ---------------------------------------------------------------------
  12606. * </pre>
  12607. * @param $str (string) string to process.
  12608. * @return array containing codepoints (UTF-8 characters values)
  12609. * @protected
  12610. * @author Nicola Asuni
  12611. * @since 1.53.0.TC005 (2005-01-05)
  12612. */
  12613. protected function UTF8StringToArray($str) {
  12614. // build a unique string key
  12615. $strkey = md5($str);
  12616. if (isset($this->cache_UTF8StringToArray[$strkey])) {
  12617. // return cached value
  12618. $chrarray = $this->cache_UTF8StringToArray[$strkey]['s'];
  12619. if (!isset($this->cache_UTF8StringToArray[$strkey]['f'][$this->CurrentFont['fontkey']])) {
  12620. if ($this->isunicode) {
  12621. foreach ($chrarray as $chr) {
  12622. // store this char for font subsetting
  12623. $this->CurrentFont['subsetchars'][$chr] = true;
  12624. }
  12625. // update font subsetchars
  12626. $this->setFontSubBuffer($this->CurrentFont['fontkey'], 'subsetchars', $this->CurrentFont['subsetchars']);
  12627. }
  12628. $this->cache_UTF8StringToArray[$strkey]['f'][$this->CurrentFont['fontkey']] = true;
  12629. }
  12630. return $chrarray;
  12631. }
  12632. // check cache size
  12633. if ($this->cache_size_UTF8StringToArray >= $this->cache_maxsize_UTF8StringToArray) {
  12634. // remove first element
  12635. array_shift($this->cache_UTF8StringToArray);
  12636. }
  12637. // new cache array for selected string
  12638. $this->cache_UTF8StringToArray[$strkey] = array('s' => array(), 'f' => array());
  12639. ++$this->cache_size_UTF8StringToArray;
  12640. if (!$this->isunicode) {
  12641. // split string into array of equivalent codes
  12642. $strarr = array();
  12643. $strlen = strlen($str);
  12644. for ($i=0; $i < $strlen; ++$i) {
  12645. $strarr[] = ord($str[$i]);
  12646. }
  12647. // insert new value on cache
  12648. $this->cache_UTF8StringToArray[$strkey]['s'] = $strarr;
  12649. $this->cache_UTF8StringToArray[$strkey]['f'][$this->CurrentFont['fontkey']] = true;
  12650. return $strarr;
  12651. }
  12652. $unichar = -1; // last unicode char
  12653. $unicode = array(); // array containing unicode values
  12654. $bytes = array(); // array containing single character byte sequences
  12655. $numbytes = 1; // number of octetc needed to represent the UTF-8 character
  12656. $str .= ''; // force $str to be a string
  12657. $length = strlen($str);
  12658. for ($i = 0; $i < $length; ++$i) {
  12659. $char = ord($str[$i]); // get one string character at time
  12660. if (count($bytes) == 0) { // get starting octect
  12661. if ($char <= 0x7F) {
  12662. $unichar = $char; // use the character "as is" because is ASCII
  12663. $numbytes = 1;
  12664. } elseif (($char >> 0x05) == 0x06) { // 2 bytes character (0x06 = 110 BIN)
  12665. $bytes[] = ($char - 0xC0) << 0x06;
  12666. $numbytes = 2;
  12667. } elseif (($char >> 0x04) == 0x0E) { // 3 bytes character (0x0E = 1110 BIN)
  12668. $bytes[] = ($char - 0xE0) << 0x0C;
  12669. $numbytes = 3;
  12670. } elseif (($char >> 0x03) == 0x1E) { // 4 bytes character (0x1E = 11110 BIN)
  12671. $bytes[] = ($char - 0xF0) << 0x12;
  12672. $numbytes = 4;
  12673. } else {
  12674. // use replacement character for other invalid sequences
  12675. $unichar = 0xFFFD;
  12676. $bytes = array();
  12677. $numbytes = 1;
  12678. }
  12679. } elseif (($char >> 0x06) == 0x02) { // bytes 2, 3 and 4 must start with 0x02 = 10 BIN
  12680. $bytes[] = $char - 0x80;
  12681. if (count($bytes) == $numbytes) {
  12682. // compose UTF-8 bytes to a single unicode value
  12683. $char = $bytes[0];
  12684. for ($j = 1; $j < $numbytes; ++$j) {
  12685. $char += ($bytes[$j] << (($numbytes - $j - 1) * 0x06));
  12686. }
  12687. if ((($char >= 0xD800) AND ($char <= 0xDFFF)) OR ($char >= 0x10FFFF)) {
  12688. /* The definition of UTF-8 prohibits encoding character numbers between
  12689. U+D800 and U+DFFF, which are reserved for use with the UTF-16
  12690. encoding form (as surrogate pairs) and do not directly represent
  12691. characters. */
  12692. $unichar = 0xFFFD; // use replacement character
  12693. } else {
  12694. $unichar = $char; // add char to array
  12695. }
  12696. // reset data for next char
  12697. $bytes = array();
  12698. $numbytes = 1;
  12699. }
  12700. } else {
  12701. // use replacement character for other invalid sequences
  12702. $unichar = 0xFFFD;
  12703. $bytes = array();
  12704. $numbytes = 1;
  12705. }
  12706. if ($unichar >= 0) {
  12707. // insert unicode value into array
  12708. $unicode[] = $unichar;
  12709. // store this char for font subsetting
  12710. $this->CurrentFont['subsetchars'][$unichar] = true;
  12711. $unichar = -1;
  12712. }
  12713. }
  12714. // update font subsetchars
  12715. $this->setFontSubBuffer($this->CurrentFont['fontkey'], 'subsetchars', $this->CurrentFont['subsetchars']);
  12716. // insert new value on cache
  12717. $this->cache_UTF8StringToArray[$strkey]['s'] = $unicode;
  12718. $this->cache_UTF8StringToArray[$strkey]['f'][$this->CurrentFont['fontkey']] = true;
  12719. return $unicode;
  12720. }
  12721. /**
  12722. * Converts UTF-8 strings to UTF16-BE.<br>
  12723. * @param $str (string) string to process.
  12724. * @param $setbom (boolean) if true set the Byte Order Mark (BOM = 0xFEFF)
  12725. * @return string
  12726. * @author Nicola Asuni
  12727. * @since 1.53.0.TC005 (2005-01-05)
  12728. * @see UTF8StringToArray(), arrUTF8ToUTF16BE()
  12729. * @protected
  12730. */
  12731. protected function UTF8ToUTF16BE($str, $setbom=false) {
  12732. if (!$this->isunicode) {
  12733. return $str; // string is not in unicode
  12734. }
  12735. $unicode = $this->UTF8StringToArray($str); // array containing UTF-8 unicode values
  12736. return $this->arrUTF8ToUTF16BE($unicode, $setbom);
  12737. }
  12738. /**
  12739. * Converts UTF-8 strings to Latin1 when using the standard 14 core fonts.<br>
  12740. * @param $str (string) string to process.
  12741. * @return string
  12742. * @author Andrew Whitehead, Nicola Asuni
  12743. * @protected
  12744. * @since 3.2.000 (2008-06-23)
  12745. */
  12746. protected function UTF8ToLatin1($str) {
  12747. if (!$this->isunicode) {
  12748. return $str; // string is not in unicode
  12749. }
  12750. $outstr = ''; // string to be returned
  12751. $unicode = $this->UTF8StringToArray($str); // array containing UTF-8 unicode values
  12752. foreach ($unicode as $char) {
  12753. if ($char < 256) {
  12754. $outstr .= chr($char);
  12755. } elseif (array_key_exists($char, $this->unicode->uni_utf8tolatin)) {
  12756. // map from UTF-8
  12757. $outstr .= chr($this->unicode->uni_utf8tolatin[$char]);
  12758. } elseif ($char == 0xFFFD) {
  12759. // skip
  12760. } else {
  12761. $outstr .= '?';
  12762. }
  12763. }
  12764. return $outstr;
  12765. }
  12766. /**
  12767. * Converts UTF-8 characters array to array of Latin1 characters<br>
  12768. * @param $unicode (array) array containing UTF-8 unicode values
  12769. * @return array
  12770. * @author Nicola Asuni
  12771. * @protected
  12772. * @since 4.8.023 (2010-01-15)
  12773. */
  12774. protected function UTF8ArrToLatin1($unicode) {
  12775. if ((!$this->isunicode) OR $this->isUnicodeFont()) {
  12776. return $unicode;
  12777. }
  12778. $outarr = array(); // array to be returned
  12779. foreach ($unicode as $char) {
  12780. if ($char < 256) {
  12781. $outarr[] = $char;
  12782. } elseif (array_key_exists($char, $this->unicode->uni_utf8tolatin)) {
  12783. // map from UTF-8
  12784. $outarr[] = $this->unicode->uni_utf8tolatin[$char];
  12785. } elseif ($char == 0xFFFD) {
  12786. // skip
  12787. } else {
  12788. $outarr[] = 63; // '?' character
  12789. }
  12790. }
  12791. return $outarr;
  12792. }
  12793. /**
  12794. * Converts array of UTF-8 characters to UTF16-BE string.<br>
  12795. * Based on: http://www.faqs.org/rfcs/rfc2781.html
  12796. * <pre>
  12797. * Encoding UTF-16:
  12798. *
  12799. * Encoding of a single character from an ISO 10646 character value to
  12800. * UTF-16 proceeds as follows. Let U be the character number, no greater
  12801. * than 0x10FFFF.
  12802. *
  12803. * 1) If U < 0x10000, encode U as a 16-bit unsigned integer and
  12804. * terminate.
  12805. *
  12806. * 2) Let U' = U - 0x10000. Because U is less than or equal to 0x10FFFF,
  12807. * U' must be less than or equal to 0xFFFFF. That is, U' can be
  12808. * represented in 20 bits.
  12809. *
  12810. * 3) Initialize two 16-bit unsigned integers, W1 and W2, to 0xD800 and
  12811. * 0xDC00, respectively. These integers each have 10 bits free to
  12812. * encode the character value, for a total of 20 bits.
  12813. *
  12814. * 4) Assign the 10 high-order bits of the 20-bit U' to the 10 low-order
  12815. * bits of W1 and the 10 low-order bits of U' to the 10 low-order
  12816. * bits of W2. Terminate.
  12817. *
  12818. * Graphically, steps 2 through 4 look like:
  12819. * U' = yyyyyyyyyyxxxxxxxxxx
  12820. * W1 = 110110yyyyyyyyyy
  12821. * W2 = 110111xxxxxxxxxx
  12822. * </pre>
  12823. * @param $unicode (array) array containing UTF-8 unicode values
  12824. * @param $setbom (boolean) if true set the Byte Order Mark (BOM = 0xFEFF)
  12825. * @return string
  12826. * @protected
  12827. * @author Nicola Asuni
  12828. * @since 2.1.000 (2008-01-08)
  12829. * @see UTF8ToUTF16BE()
  12830. */
  12831. protected function arrUTF8ToUTF16BE($unicode, $setbom=false) {
  12832. $outstr = ''; // string to be returned
  12833. if ($setbom) {
  12834. $outstr .= "\xFE\xFF"; // Byte Order Mark (BOM)
  12835. }
  12836. foreach ($unicode as $char) {
  12837. if ($char == 0x200b) {
  12838. // skip Unicode Character 'ZERO WIDTH SPACE' (DEC:8203, U+200B)
  12839. } elseif ($char == 0xFFFD) {
  12840. $outstr .= "\xFF\xFD"; // replacement character
  12841. } elseif ($char < 0x10000) {
  12842. $outstr .= chr($char >> 0x08);
  12843. $outstr .= chr($char & 0xFF);
  12844. } else {
  12845. $char -= 0x10000;
  12846. $w1 = 0xD800 | ($char >> 0x0a);
  12847. $w2 = 0xDC00 | ($char & 0x3FF);
  12848. $outstr .= chr($w1 >> 0x08);
  12849. $outstr .= chr($w1 & 0xFF);
  12850. $outstr .= chr($w2 >> 0x08);
  12851. $outstr .= chr($w2 & 0xFF);
  12852. }
  12853. }
  12854. return $outstr;
  12855. }
  12856. // ====================================================
  12857. /**
  12858. * Set header font.
  12859. * @param $font (array) font
  12860. * @public
  12861. * @since 1.1
  12862. */
  12863. public function setHeaderFont($font) {
  12864. $this->header_font = $font;
  12865. }
  12866. /**
  12867. * Get header font.
  12868. * @return array()
  12869. * @public
  12870. * @since 4.0.012 (2008-07-24)
  12871. */
  12872. public function getHeaderFont() {
  12873. return $this->header_font;
  12874. }
  12875. /**
  12876. * Set footer font.
  12877. * @param $font (array) font
  12878. * @public
  12879. * @since 1.1
  12880. */
  12881. public function setFooterFont($font) {
  12882. $this->footer_font = $font;
  12883. }
  12884. /**
  12885. * Get Footer font.
  12886. * @return array()
  12887. * @public
  12888. * @since 4.0.012 (2008-07-24)
  12889. */
  12890. public function getFooterFont() {
  12891. return $this->footer_font;
  12892. }
  12893. /**
  12894. * Set language array.
  12895. * @param $language (array)
  12896. * @public
  12897. * @since 1.1
  12898. */
  12899. public function setLanguageArray($language) {
  12900. $this->l = $language;
  12901. if (isset($this->l['a_meta_dir'])) {
  12902. $this->rtl = $this->l['a_meta_dir']=='rtl' ? true : false;
  12903. } else {
  12904. $this->rtl = false;
  12905. }
  12906. }
  12907. /**
  12908. * Returns the PDF data.
  12909. * @public
  12910. */
  12911. public function getPDFData() {
  12912. if ($this->state < 3) {
  12913. $this->Close();
  12914. }
  12915. return $this->buffer;
  12916. }
  12917. /**
  12918. * Output anchor link.
  12919. * @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;)
  12920. * @param $name (string) link name
  12921. * @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
  12922. * @param $firstline (boolean) if true prints only the first line and return the remaining string.
  12923. * @param $color (array) array of RGB text color
  12924. * @param $style (string) font style (U, D, B, I)
  12925. * @param $firstblock (boolean) if true the string is the starting of a line.
  12926. * @return the number of cells used or the remaining text if $firstline = true;
  12927. * @public
  12928. */
  12929. public function addHtmlLink($url, $name, $fill=false, $firstline=false, $color='', $style=-1, $firstblock=false) {
  12930. if (!$this->empty_string($url) AND ($url{0} == '#')) {
  12931. // convert url to internal link
  12932. $lnkdata = explode(',', $url);
  12933. if (isset($lnkdata[0])) {
  12934. $page = intval(substr($lnkdata[0], 1));
  12935. if (empty($page) OR ($page <= 0)) {
  12936. $page = $this->page;
  12937. }
  12938. if (isset($lnkdata[1]) AND (strlen($lnkdata[1]) > 0)) {
  12939. $lnky = floatval($lnkdata[1]);
  12940. } else {
  12941. $lnky = 0;
  12942. }
  12943. $url = $this->AddLink();
  12944. $this->SetLink($url, $lnky, $page);
  12945. }
  12946. }
  12947. // store current settings
  12948. $prevcolor = $this->fgcolor;
  12949. $prevstyle = $this->FontStyle;
  12950. if (empty($color)) {
  12951. $this->SetTextColorArray($this->htmlLinkColorArray);
  12952. } else {
  12953. $this->SetTextColorArray($color);
  12954. }
  12955. if ($style == -1) {
  12956. $this->SetFont('', $this->FontStyle.$this->htmlLinkFontStyle);
  12957. } else {
  12958. $this->SetFont('', $this->FontStyle.$style);
  12959. }
  12960. $ret = $this->Write($this->lasth, $name, $url, $fill, '', false, 0, $firstline, $firstblock, 0);
  12961. // restore settings
  12962. $this->SetFont('', $prevstyle);
  12963. $this->SetTextColorArray($prevcolor);
  12964. return $ret;
  12965. }
  12966. /**
  12967. * 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, or a javascript color array, or javascript color name.
  12968. * @param $hcolor (string) HTML color.
  12969. * @param $defcol (array) Color to return in case of error.
  12970. * @return array RGB or CMYK color, or false in case of error.
  12971. * @public
  12972. */
  12973. public function convertHTMLColorToDec($hcolor='#FFFFFF', $defcol=array('R'=>128,'G'=>128,'B'=>128)) {
  12974. $color = preg_replace('/[\s]*/', '', $hcolor); // remove extra spaces
  12975. $color = strtolower($color);
  12976. // check for javascript color array syntax
  12977. if (strpos($color, '[') !== false) {
  12978. if (preg_match('/[\[][\"\'](t|g|rgb|cmyk)[\"\'][\,]?([0-9\.]*)[\,]?([0-9\.]*)[\,]?([0-9\.]*)[\,]?([0-9\.]*)[\]]/', $color, $m) > 0) {
  12979. $returncolor = array();
  12980. switch ($m[1]) {
  12981. case 'cmyk': {
  12982. // RGB
  12983. $returncolor['C'] = max(0, min(100, (floatval($m[2]) * 100)));
  12984. $returncolor['M'] = max(0, min(100, (floatval($m[3]) * 100)));
  12985. $returncolor['Y'] = max(0, min(100, (floatval($m[4]) * 100)));
  12986. $returncolor['K'] = max(0, min(100, (floatval($m[5]) * 100)));
  12987. break;
  12988. }
  12989. case 'rgb': {
  12990. // RGB
  12991. $returncolor['R'] = max(0, min(255, (floatval($m[2]) * 255)));
  12992. $returncolor['G'] = max(0, min(255, (floatval($m[3]) * 255)));
  12993. $returncolor['B'] = max(0, min(255, (floatval($m[4]) * 255)));
  12994. break;
  12995. }
  12996. case 'g': {
  12997. // grayscale
  12998. $returncolor['G'] = max(0, min(255, (floatval($m[2]) * 255)));
  12999. break;
  13000. }
  13001. case 't':
  13002. default: {
  13003. // transparent (empty array)
  13004. break;
  13005. }
  13006. }
  13007. return $returncolor;
  13008. }
  13009. } elseif (($dotpos = strpos($color, '.')) !== false) {
  13010. // remove class parent (i.e.: color.red)
  13011. $color = substr($color, ($dotpos + 1));
  13012. if ($color == 'transparent') {
  13013. // transparent (empty array)
  13014. return array();
  13015. }
  13016. }
  13017. if (strlen($color) == 0) {
  13018. return $defcol;
  13019. }
  13020. // RGB ARRAY
  13021. if (substr($color, 0, 3) == 'rgb') {
  13022. $codes = substr($color, 4);
  13023. $codes = str_replace(')', '', $codes);
  13024. $returncolor = explode(',', $codes);
  13025. foreach ($returncolor as $key => $val) {
  13026. if (strpos($val, '%') > 0) {
  13027. // percentage
  13028. $returncolor[$key] = (255 * intval($val) / 100);
  13029. } else {
  13030. $returncolor[$key] = intval($val);
  13031. }
  13032. // normalize value
  13033. $returncolor[$key] = max(0, min(255, $returncolor[$key]));
  13034. }
  13035. return $returncolor;
  13036. }
  13037. // CMYK ARRAY
  13038. if (substr($color, 0, 4) == 'cmyk') {
  13039. $codes = substr($color, 5);
  13040. $codes = str_replace(')', '', $codes);
  13041. $returncolor = explode(',', $codes);
  13042. foreach ($returncolor as $key => $val) {
  13043. if (strpos($val, '%') !== false) {
  13044. // percentage
  13045. $returncolor[$key] = (100 * intval($val) / 100);
  13046. } else {
  13047. $returncolor[$key] = intval($val);
  13048. }
  13049. // normalize value
  13050. $returncolor[$key] = max(0, min(100, $returncolor[$key]));
  13051. }
  13052. return $returncolor;
  13053. }
  13054. if ($color{0} != '#') {
  13055. // COLOR NAME
  13056. if (isset($this->webcolor[$color])) {
  13057. // web color
  13058. $color_code = $this->webcolor[$color];
  13059. } else {
  13060. // spot color
  13061. $returncolor = $this->getSpotColor($color);
  13062. if ($returncolor === false) {
  13063. $returncolor = $defcol;
  13064. }
  13065. return $returncolor;
  13066. }
  13067. } else {
  13068. $color_code = substr($color, 1);
  13069. }
  13070. // HEXADECIMAL REPRESENTATION
  13071. switch (strlen($color_code)) {
  13072. case 3: {
  13073. // 3-digit RGB hexadecimal representation
  13074. $r = substr($color_code, 0, 1);
  13075. $g = substr($color_code, 1, 1);
  13076. $b = substr($color_code, 2, 1);
  13077. $returncolor = array();
  13078. $returncolor['R'] = max(0, min(255, hexdec($r.$r)));
  13079. $returncolor['G'] = max(0, min(255, hexdec($g.$g)));
  13080. $returncolor['B'] = max(0, min(255, hexdec($b.$b)));
  13081. break;
  13082. }
  13083. case 6: {
  13084. // 6-digit RGB hexadecimal representation
  13085. $returncolor = array();
  13086. $returncolor['R'] = max(0, min(255, hexdec(substr($color_code, 0, 2))));
  13087. $returncolor['G'] = max(0, min(255, hexdec(substr($color_code, 2, 2))));
  13088. $returncolor['B'] = max(0, min(255, hexdec(substr($color_code, 4, 2))));
  13089. break;
  13090. }
  13091. case 8: {
  13092. // 8-digit CMYK hexadecimal representation
  13093. $returncolor = array();
  13094. $returncolor['C'] = max(0, min(100, round(hexdec(substr($color_code, 0, 2)) / 2.55)));
  13095. $returncolor['M'] = max(0, min(100, round(hexdec(substr($color_code, 2, 2)) / 2.55)));
  13096. $returncolor['Y'] = max(0, min(100, round(hexdec(substr($color_code, 4, 2)) / 2.55)));
  13097. $returncolor['K'] = max(0, min(100, round(hexdec(substr($color_code, 6, 2)) / 2.55)));
  13098. break;
  13099. }
  13100. default: {
  13101. $returncolor = $defcol;
  13102. break;
  13103. }
  13104. }
  13105. return $returncolor;
  13106. }
  13107. /**
  13108. * Converts pixels to User's Units.
  13109. * @param $px (int) pixels
  13110. * @return float value in user's unit
  13111. * @public
  13112. * @see setImageScale(), getImageScale()
  13113. */
  13114. public function pixelsToUnits($px) {
  13115. return ($px / ($this->imgscale * $this->k));
  13116. }
  13117. /**
  13118. * Reverse function for htmlentities.
  13119. * Convert entities in UTF-8.
  13120. * @param $text_to_convert (string) Text to convert.
  13121. * @return string converted text string
  13122. * @public
  13123. */
  13124. public function unhtmlentities($text_to_convert) {
  13125. return @html_entity_decode($text_to_convert, ENT_QUOTES, $this->encoding);
  13126. }
  13127. // ENCRYPTION METHODS ----------------------------------
  13128. /**
  13129. * Returns a string containing random data to be used as a seed for encryption methods.
  13130. * @param $seed (string) starting seed value
  13131. * @return string containing random data
  13132. * @author Nicola Asuni
  13133. * @since 5.9.006 (2010-10-19)
  13134. * @protected
  13135. */
  13136. protected function getRandomSeed($seed='') {
  13137. $seed .= microtime();
  13138. if (function_exists('openssl_random_pseudo_bytes') AND (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN')) {
  13139. // this is not used on windows systems because it is very slow for a know bug
  13140. $seed .= openssl_random_pseudo_bytes(512);
  13141. } else {
  13142. for ($i = 0; $i < 23; ++$i) {
  13143. $seed .= uniqid('', true);
  13144. }
  13145. }
  13146. $seed .= uniqid('', true);
  13147. $seed .= rand();
  13148. $seed .= getmypid();
  13149. $seed .= __FILE__;
  13150. $seed .= $this->bufferlen;
  13151. if (isset($_SERVER['REMOTE_ADDR'])) {
  13152. $seed .= $_SERVER['REMOTE_ADDR'];
  13153. }
  13154. if (isset($_SERVER['HTTP_USER_AGENT'])) {
  13155. $seed .= $_SERVER['HTTP_USER_AGENT'];
  13156. }
  13157. if (isset($_SERVER['HTTP_ACCEPT'])) {
  13158. $seed .= $_SERVER['HTTP_ACCEPT'];
  13159. }
  13160. if (isset($_SERVER['HTTP_ACCEPT_ENCODING'])) {
  13161. $seed .= $_SERVER['HTTP_ACCEPT_ENCODING'];
  13162. }
  13163. if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
  13164. $seed .= $_SERVER['HTTP_ACCEPT_LANGUAGE'];
  13165. }
  13166. if (isset($_SERVER['HTTP_ACCEPT_CHARSET'])) {
  13167. $seed .= $_SERVER['HTTP_ACCEPT_CHARSET'];
  13168. }
  13169. $seed .= rand();
  13170. $seed .= uniqid('', true);
  13171. $seed .= microtime();
  13172. return $seed;
  13173. }
  13174. /**
  13175. * Compute encryption key depending on object number where the encrypted data is stored.
  13176. * This is used for all strings and streams without crypt filter specifier.
  13177. * @param $n (int) object number
  13178. * @return int object key
  13179. * @protected
  13180. * @author Nicola Asuni
  13181. * @since 2.0.000 (2008-01-02)
  13182. */
  13183. protected function _objectkey($n) {
  13184. $objkey = $this->encryptdata['key'].pack('VXxx', $n);
  13185. if ($this->encryptdata['mode'] == 2) { // AES-128
  13186. // AES padding
  13187. $objkey .= "\x73\x41\x6C\x54"; // sAlT
  13188. }
  13189. $objkey = substr($this->_md5_16($objkey), 0, (($this->encryptdata['Length'] / 8) + 5));
  13190. $objkey = substr($objkey, 0, 16);
  13191. return $objkey;
  13192. }
  13193. /**
  13194. * Encrypt the input string.
  13195. * @param $n (int) object number
  13196. * @param $s (string) data string to encrypt
  13197. * @return encrypted string
  13198. * @protected
  13199. * @author Nicola Asuni
  13200. * @since 5.0.005 (2010-05-11)
  13201. */
  13202. protected function _encrypt_data($n, $s) {
  13203. if (!$this->encrypted) {
  13204. return $s;
  13205. }
  13206. switch ($this->encryptdata['mode']) {
  13207. case 0: // RC4-40
  13208. case 1: { // RC4-128
  13209. $s = $this->_RC4($this->_objectkey($n), $s);
  13210. break;
  13211. }
  13212. case 2: { // AES-128
  13213. $s = $this->_AES($this->_objectkey($n), $s);
  13214. break;
  13215. }
  13216. case 3: { // AES-256
  13217. $s = $this->_AES($this->encryptdata['key'], $s);
  13218. break;
  13219. }
  13220. }
  13221. return $s;
  13222. }
  13223. /**
  13224. * Put encryption on PDF document.
  13225. * @protected
  13226. * @author Nicola Asuni
  13227. * @since 2.0.000 (2008-01-02)
  13228. */
  13229. protected function _putencryption() {
  13230. if (!$this->encrypted) {
  13231. return;
  13232. }
  13233. $this->encryptdata['objid'] = $this->_newobj();
  13234. $out = '<<';
  13235. if (!isset($this->encryptdata['Filter']) OR empty($this->encryptdata['Filter'])) {
  13236. $this->encryptdata['Filter'] = 'Standard';
  13237. }
  13238. $out .= ' /Filter /'.$this->encryptdata['Filter'];
  13239. if (isset($this->encryptdata['SubFilter']) AND !empty($this->encryptdata['SubFilter'])) {
  13240. $out .= ' /SubFilter /'.$this->encryptdata['SubFilter'];
  13241. }
  13242. if (!isset($this->encryptdata['V']) OR empty($this->encryptdata['V'])) {
  13243. $this->encryptdata['V'] = 1;
  13244. }
  13245. // V is a code specifying the algorithm to be used in encrypting and decrypting the document
  13246. $out .= ' /V '.$this->encryptdata['V'];
  13247. if (isset($this->encryptdata['Length']) AND !empty($this->encryptdata['Length'])) {
  13248. // The length of the encryption key, in bits. The value shall be a multiple of 8, in the range 40 to 256
  13249. $out .= ' /Length '.$this->encryptdata['Length'];
  13250. } else {
  13251. $out .= ' /Length 40';
  13252. }
  13253. if ($this->encryptdata['V'] >= 4) {
  13254. if (!isset($this->encryptdata['StmF']) OR empty($this->encryptdata['StmF'])) {
  13255. $this->encryptdata['StmF'] = 'Identity';
  13256. }
  13257. if (!isset($this->encryptdata['StrF']) OR empty($this->encryptdata['StrF'])) {
  13258. // The name of the crypt filter that shall be used when decrypting all strings in the document.
  13259. $this->encryptdata['StrF'] = 'Identity';
  13260. }
  13261. // A dictionary whose keys shall be crypt filter names and whose values shall be the corresponding crypt filter dictionaries.
  13262. if (isset($this->encryptdata['CF']) AND !empty($this->encryptdata['CF'])) {
  13263. $out .= ' /CF <<';
  13264. $out .= ' /'.$this->encryptdata['StmF'].' <<';
  13265. $out .= ' /Type /CryptFilter';
  13266. if (isset($this->encryptdata['CF']['CFM']) AND !empty($this->encryptdata['CF']['CFM'])) {
  13267. // The method used
  13268. $out .= ' /CFM /'.$this->encryptdata['CF']['CFM'];
  13269. if ($this->encryptdata['pubkey']) {
  13270. $out .= ' /Recipients [';
  13271. foreach ($this->encryptdata['Recipients'] as $rec) {
  13272. $out .= ' <'.$rec.'>';
  13273. }
  13274. $out .= ' ]';
  13275. if (isset($this->encryptdata['CF']['EncryptMetadata']) AND (!$this->encryptdata['CF']['EncryptMetadata'])) {
  13276. $out .= ' /EncryptMetadata false';
  13277. } else {
  13278. $out .= ' /EncryptMetadata true';
  13279. }
  13280. }
  13281. } else {
  13282. $out .= ' /CFM /None';
  13283. }
  13284. if (isset($this->encryptdata['CF']['AuthEvent']) AND !empty($this->encryptdata['CF']['AuthEvent'])) {
  13285. // The event to be used to trigger the authorization that is required to access encryption keys used by this filter.
  13286. $out .= ' /AuthEvent /'.$this->encryptdata['CF']['AuthEvent'];
  13287. } else {
  13288. $out .= ' /AuthEvent /DocOpen';
  13289. }
  13290. if (isset($this->encryptdata['CF']['Length']) AND !empty($this->encryptdata['CF']['Length'])) {
  13291. // The bit length of the encryption key.
  13292. $out .= ' /Length '.$this->encryptdata['CF']['Length'];
  13293. }
  13294. $out .= ' >> >>';
  13295. }
  13296. // The name of the crypt filter that shall be used by default when decrypting streams.
  13297. $out .= ' /StmF /'.$this->encryptdata['StmF'];
  13298. // The name of the crypt filter that shall be used when decrypting all strings in the document.
  13299. $out .= ' /StrF /'.$this->encryptdata['StrF'];
  13300. if (isset($this->encryptdata['EFF']) AND !empty($this->encryptdata['EFF'])) {
  13301. // The name of the crypt filter that shall be used when encrypting embedded file streams that do not have their own crypt filter specifier.
  13302. $out .= ' /EFF /'.$this->encryptdata[''];
  13303. }
  13304. }
  13305. // Additional encryption dictionary entries for the standard security handler
  13306. if ($this->encryptdata['pubkey']) {
  13307. if (($this->encryptdata['V'] < 4) AND isset($this->encryptdata['Recipients']) AND !empty($this->encryptdata['Recipients'])) {
  13308. $out .= ' /Recipients [';
  13309. foreach ($this->encryptdata['Recipients'] as $rec) {
  13310. $out .= ' <'.$rec.'>';
  13311. }
  13312. $out .= ' ]';
  13313. }
  13314. } else {
  13315. $out .= ' /R';
  13316. if ($this->encryptdata['V'] == 5) { // AES-256
  13317. $out .= ' 5';
  13318. $out .= ' /OE ('.$this->_escape($this->encryptdata['OE']).')';
  13319. $out .= ' /UE ('.$this->_escape($this->encryptdata['UE']).')';
  13320. $out .= ' /Perms ('.$this->_escape($this->encryptdata['perms']).')';
  13321. } elseif ($this->encryptdata['V'] == 4) { // AES-128
  13322. $out .= ' 4';
  13323. } elseif ($this->encryptdata['V'] < 2) { // RC-40
  13324. $out .= ' 2';
  13325. } else { // RC-128
  13326. $out .= ' 3';
  13327. }
  13328. $out .= ' /O ('.$this->_escape($this->encryptdata['O']).')';
  13329. $out .= ' /U ('.$this->_escape($this->encryptdata['U']).')';
  13330. $out .= ' /P '.$this->encryptdata['P'];
  13331. if (isset($this->encryptdata['EncryptMetadata']) AND (!$this->encryptdata['EncryptMetadata'])) {
  13332. $out .= ' /EncryptMetadata false';
  13333. } else {
  13334. $out .= ' /EncryptMetadata true';
  13335. }
  13336. }
  13337. $out .= ' >>';
  13338. $out .= "\n".'endobj';
  13339. $this->_out($out);
  13340. }
  13341. /**
  13342. * Returns the input text encrypted using RC4 algorithm and the specified key.
  13343. * RC4 is the standard encryption algorithm used in PDF format
  13344. * @param $key (string) encryption key
  13345. * @param $text (String) input text to be encrypted
  13346. * @return String encrypted text
  13347. * @protected
  13348. * @since 2.0.000 (2008-01-02)
  13349. * @author Klemen Vodopivec, Nicola Asuni
  13350. */
  13351. protected function _RC4($key, $text) {
  13352. if (function_exists('mcrypt_decrypt') AND ($out = @mcrypt_decrypt(MCRYPT_ARCFOUR, $key, $text, MCRYPT_MODE_STREAM, ''))) {
  13353. // try to use mcrypt function if exist
  13354. return $out;
  13355. }
  13356. if ($this->last_enc_key != $key) {
  13357. $k = str_repeat($key, ((256 / strlen($key)) + 1));
  13358. $rc4 = range(0, 255);
  13359. $j = 0;
  13360. for ($i = 0; $i < 256; ++$i) {
  13361. $t = $rc4[$i];
  13362. $j = ($j + $t + ord($k[$i])) % 256;
  13363. $rc4[$i] = $rc4[$j];
  13364. $rc4[$j] = $t;
  13365. }
  13366. $this->last_enc_key = $key;
  13367. $this->last_enc_key_c = $rc4;
  13368. } else {
  13369. $rc4 = $this->last_enc_key_c;
  13370. }
  13371. $len = strlen($text);
  13372. $a = 0;
  13373. $b = 0;
  13374. $out = '';
  13375. for ($i = 0; $i < $len; ++$i) {
  13376. $a = ($a + 1) % 256;
  13377. $t = $rc4[$a];
  13378. $b = ($b + $t) % 256;
  13379. $rc4[$a] = $rc4[$b];
  13380. $rc4[$b] = $t;
  13381. $k = $rc4[($rc4[$a] + $rc4[$b]) % 256];
  13382. $out .= chr(ord($text[$i]) ^ $k);
  13383. }
  13384. return $out;
  13385. }
  13386. /**
  13387. * Returns the input text exrypted using AES algorithm and the specified key.
  13388. * This method requires mcrypt.
  13389. * @param $key (string) encryption key
  13390. * @param $text (String) input text to be encrypted
  13391. * @return String encrypted text
  13392. * @protected
  13393. * @author Nicola Asuni
  13394. * @since 5.0.005 (2010-05-11)
  13395. */
  13396. protected function _AES($key, $text) {
  13397. // padding (RFC 2898, PKCS #5: Password-Based Cryptography Specification Version 2.0)
  13398. $padding = 16 - (strlen($text) % 16);
  13399. $text .= str_repeat(chr($padding), $padding);
  13400. $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC), MCRYPT_RAND);
  13401. $text = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $text, MCRYPT_MODE_CBC, $iv);
  13402. $text = $iv.$text;
  13403. return $text;
  13404. }
  13405. /**
  13406. * Encrypts a string using MD5 and returns it's value as a binary string.
  13407. * @param $str (string) input string
  13408. * @return String MD5 encrypted binary string
  13409. * @protected
  13410. * @since 2.0.000 (2008-01-02)
  13411. * @author Klemen Vodopivec
  13412. */
  13413. protected function _md5_16($str) {
  13414. return pack('H*', md5($str));
  13415. }
  13416. /**
  13417. * Compute U value (used for encryption)
  13418. * @return string U value
  13419. * @protected
  13420. * @since 2.0.000 (2008-01-02)
  13421. * @author Nicola Asuni
  13422. */
  13423. protected function _Uvalue() {
  13424. if ($this->encryptdata['mode'] == 0) { // RC4-40
  13425. return $this->_RC4($this->encryptdata['key'], $this->enc_padding);
  13426. } elseif ($this->encryptdata['mode'] < 3) { // RC4-128, AES-128
  13427. $tmp = $this->_md5_16($this->enc_padding.$this->encryptdata['fileid']);
  13428. $enc = $this->_RC4($this->encryptdata['key'], $tmp);
  13429. $len = strlen($tmp);
  13430. for ($i = 1; $i <= 19; ++$i) {
  13431. $ek = '';
  13432. for ($j = 0; $j < $len; ++$j) {
  13433. $ek .= chr(ord($this->encryptdata['key'][$j]) ^ $i);
  13434. }
  13435. $enc = $this->_RC4($ek, $enc);
  13436. }
  13437. $enc .= str_repeat("\x00", 16);
  13438. return substr($enc, 0, 32);
  13439. } elseif ($this->encryptdata['mode'] == 3) { // AES-256
  13440. $seed = $this->_md5_16($this->getRandomSeed());
  13441. // User Validation Salt
  13442. $this->encryptdata['UVS'] = substr($seed, 0, 8);
  13443. // User Key Salt
  13444. $this->encryptdata['UKS'] = substr($seed, 8, 16);
  13445. return hash('sha256', $this->encryptdata['user_password'].$this->encryptdata['UVS'], true).$this->encryptdata['UVS'].$this->encryptdata['UKS'];
  13446. }
  13447. }
  13448. /**
  13449. * Compute UE value (used for encryption)
  13450. * @return string UE value
  13451. * @protected
  13452. * @since 5.9.006 (2010-10-19)
  13453. * @author Nicola Asuni
  13454. */
  13455. protected function _UEvalue() {
  13456. $hashkey = hash('sha256', $this->encryptdata['user_password'].$this->encryptdata['UKS'], true);
  13457. $iv = str_repeat("\x00", mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC));
  13458. return mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $hashkey, $this->encryptdata['key'], MCRYPT_MODE_CBC, $iv);
  13459. }
  13460. /**
  13461. * Compute O value (used for encryption)
  13462. * @return string O value
  13463. * @protected
  13464. * @since 2.0.000 (2008-01-02)
  13465. * @author Nicola Asuni
  13466. */
  13467. protected function _Ovalue() {
  13468. if ($this->encryptdata['mode'] < 3) { // RC4-40, RC4-128, AES-128
  13469. $tmp = $this->_md5_16($this->encryptdata['owner_password']);
  13470. if ($this->encryptdata['mode'] > 0) {
  13471. for ($i = 0; $i < 50; ++$i) {
  13472. $tmp = $this->_md5_16($tmp);
  13473. }
  13474. }
  13475. $owner_key = substr($tmp, 0, ($this->encryptdata['Length'] / 8));
  13476. $enc = $this->_RC4($owner_key, $this->encryptdata['user_password']);
  13477. if ($this->encryptdata['mode'] > 0) {
  13478. $len = strlen($owner_key);
  13479. for ($i = 1; $i <= 19; ++$i) {
  13480. $ek = '';
  13481. for ($j = 0; $j < $len; ++$j) {
  13482. $ek .= chr(ord($owner_key[$j]) ^ $i);
  13483. }
  13484. $enc = $this->_RC4($ek, $enc);
  13485. }
  13486. }
  13487. return $enc;
  13488. } elseif ($this->encryptdata['mode'] == 3) { // AES-256
  13489. $seed = $this->_md5_16($this->getRandomSeed());
  13490. // Owner Validation Salt
  13491. $this->encryptdata['OVS'] = substr($seed, 0, 8);
  13492. // Owner Key Salt
  13493. $this->encryptdata['OKS'] = substr($seed, 8, 16);
  13494. return hash('sha256', $this->encryptdata['owner_password'].$this->encryptdata['OVS'].$this->encryptdata['U'], true).$this->encryptdata['OVS'].$this->encryptdata['OKS'];
  13495. }
  13496. }
  13497. /**
  13498. * Compute OE value (used for encryption)
  13499. * @return string OE value
  13500. * @protected
  13501. * @since 5.9.006 (2010-10-19)
  13502. * @author Nicola Asuni
  13503. */
  13504. protected function _OEvalue() {
  13505. $hashkey = hash('sha256', $this->encryptdata['owner_password'].$this->encryptdata['OKS'].$this->encryptdata['U'], true);
  13506. $iv = str_repeat("\x00", mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC));
  13507. return mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $hashkey, $this->encryptdata['key'], MCRYPT_MODE_CBC, $iv);
  13508. }
  13509. /**
  13510. * Convert password for AES-256 encryption mode
  13511. * @param $password (string) password
  13512. * @return string password
  13513. * @protected
  13514. * @since 5.9.006 (2010-10-19)
  13515. * @author Nicola Asuni
  13516. */
  13517. protected function _fixAES256Password($password) {
  13518. $psw = ''; // password to be returned
  13519. $psw_array = $this->utf8Bidi($this->UTF8StringToArray($password), $password, $this->rtl);
  13520. foreach ($psw_array as $c) {
  13521. $psw .= $this->unichr($c);
  13522. }
  13523. return substr($psw, 0, 127);
  13524. }
  13525. /**
  13526. * Compute encryption key
  13527. * @protected
  13528. * @since 2.0.000 (2008-01-02)
  13529. * @author Nicola Asuni
  13530. */
  13531. protected function _generateencryptionkey() {
  13532. $keybytelen = ($this->encryptdata['Length'] / 8);
  13533. if (!$this->encryptdata['pubkey']) { // standard mode
  13534. if ($this->encryptdata['mode'] == 3) { // AES-256
  13535. // generate 256 bit random key
  13536. $this->encryptdata['key'] = substr(hash('sha256', $this->getRandomSeed(), true), 0, $keybytelen);
  13537. // truncate passwords
  13538. $this->encryptdata['user_password'] = $this->_fixAES256Password($this->encryptdata['user_password']);
  13539. $this->encryptdata['owner_password'] = $this->_fixAES256Password($this->encryptdata['owner_password']);
  13540. // Compute U value
  13541. $this->encryptdata['U'] = $this->_Uvalue();
  13542. // Compute UE value
  13543. $this->encryptdata['UE'] = $this->_UEvalue();
  13544. // Compute O value
  13545. $this->encryptdata['O'] = $this->_Ovalue();
  13546. // Compute OE value
  13547. $this->encryptdata['OE'] = $this->_OEvalue();
  13548. // Compute P value
  13549. $this->encryptdata['P'] = $this->encryptdata['protection'];
  13550. // Computing the encryption dictionary's Perms (permissions) value
  13551. $perms = $this->getEncPermissionsString($this->encryptdata['protection']); // bytes 0-3
  13552. $perms .= chr(255).chr(255).chr(255).chr(255); // bytes 4-7
  13553. if (isset($this->encryptdata['CF']['EncryptMetadata']) AND (!$this->encryptdata['CF']['EncryptMetadata'])) { // byte 8
  13554. $perms .= 'F';
  13555. } else {
  13556. $perms .= 'T';
  13557. }
  13558. $perms .= 'adb'; // bytes 9-11
  13559. $perms .= 'nick'; // bytes 12-15
  13560. $iv = str_repeat("\x00", mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB));
  13561. $this->encryptdata['perms'] = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $this->encryptdata['key'], $perms, MCRYPT_MODE_ECB, $iv);
  13562. } else { // RC4-40, RC4-128, AES-128
  13563. // Pad passwords
  13564. $this->encryptdata['user_password'] = substr($this->encryptdata['user_password'].$this->enc_padding, 0, 32);
  13565. $this->encryptdata['owner_password'] = substr($this->encryptdata['owner_password'].$this->enc_padding, 0, 32);
  13566. // Compute O value
  13567. $this->encryptdata['O'] = $this->_Ovalue();
  13568. // get default permissions (reverse byte order)
  13569. $permissions = $this->getEncPermissionsString($this->encryptdata['protection']);
  13570. // Compute encryption key
  13571. $tmp = $this->_md5_16($this->encryptdata['user_password'].$this->encryptdata['O'].$permissions.$this->encryptdata['fileid']);
  13572. if ($this->encryptdata['mode'] > 0) {
  13573. for ($i = 0; $i < 50; ++$i) {
  13574. $tmp = $this->_md5_16(substr($tmp, 0, $keybytelen));
  13575. }
  13576. }
  13577. $this->encryptdata['key'] = substr($tmp, 0, $keybytelen);
  13578. // Compute U value
  13579. $this->encryptdata['U'] = $this->_Uvalue();
  13580. // Compute P value
  13581. $this->encryptdata['P'] = $this->encryptdata['protection'];
  13582. }
  13583. } else { // Public-Key mode
  13584. // random 20-byte seed
  13585. $seed = sha1($this->getRandomSeed(), true);
  13586. $recipient_bytes = '';
  13587. foreach ($this->encryptdata['pubkeys'] as $pubkey) {
  13588. // for each public certificate
  13589. if (isset($pubkey['p'])) {
  13590. $pkprotection = $this->getUserPermissionCode($pubkey['p'], $this->encryptdata['mode']);
  13591. } else {
  13592. $pkprotection = $this->encryptdata['protection'];
  13593. }
  13594. // get default permissions (reverse byte order)
  13595. $pkpermissions = $this->getEncPermissionsString($pkprotection);
  13596. // envelope data
  13597. $envelope = $seed.$pkpermissions;
  13598. // write the envelope data to a temporary file
  13599. $tempkeyfile = tempnam(K_PATH_CACHE, 'tmpkey_');
  13600. $f = fopen($tempkeyfile, 'wb');
  13601. if (!$f) {
  13602. $this->Error('Unable to create temporary key file: '.$tempkeyfile);
  13603. }
  13604. $envelope_length = strlen($envelope);
  13605. fwrite($f, $envelope, $envelope_length);
  13606. fclose($f);
  13607. $tempencfile = tempnam(K_PATH_CACHE, 'tmpenc_');
  13608. if (!openssl_pkcs7_encrypt($tempkeyfile, $tempencfile, $pubkey['c'], array(), PKCS7_BINARY | PKCS7_DETACHED)) {
  13609. $this->Error('Unable to encrypt the file: '.$tempkeyfile);
  13610. }
  13611. unlink($tempkeyfile);
  13612. // read encryption signature
  13613. $signature = file_get_contents($tempencfile, false, null, $envelope_length);
  13614. unlink($tempencfile);
  13615. // extract signature
  13616. $signature = substr($signature, strpos($signature, 'Content-Disposition'));
  13617. $tmparr = explode("\n\n", $signature);
  13618. $signature = trim($tmparr[1]);
  13619. unset($tmparr);
  13620. // decode signature
  13621. $signature = base64_decode($signature);
  13622. // convert signature to hex
  13623. $hexsignature = current(unpack('H*', $signature));
  13624. // store signature on recipients array
  13625. $this->encryptdata['Recipients'][] = $hexsignature;
  13626. // The bytes of each item in the Recipients array of PKCS#7 objects in the order in which they appear in the array
  13627. $recipient_bytes .= $signature;
  13628. }
  13629. // calculate encryption key
  13630. if ($this->encryptdata['mode'] == 3) { // AES-256
  13631. $this->encryptdata['key'] = substr(hash('sha256', $seed.$recipient_bytes, true), 0, $keybytelen);
  13632. } else { // RC4-40, RC4-128, AES-128
  13633. $this->encryptdata['key'] = substr(sha1($seed.$recipient_bytes, true), 0, $keybytelen);
  13634. }
  13635. }
  13636. }
  13637. /**
  13638. * Return the premission code used on encryption (P value).
  13639. * @param $permissions (Array) the set of permissions (specify the ones you want to block).
  13640. * @param $mode (int) encryption strength: 0 = RC4 40 bit; 1 = RC4 128 bit; 2 = AES 128 bit; 3 = AES 256 bit.
  13641. * @protected
  13642. * @since 5.0.005 (2010-05-12)
  13643. * @author Nicola Asuni
  13644. */
  13645. protected function getUserPermissionCode($permissions, $mode=0) {
  13646. $options = array(
  13647. 'owner' => 2, // bit 2 -- inverted logic: cleared by default
  13648. 'print' => 4, // bit 3
  13649. 'modify' => 8, // bit 4
  13650. 'copy' => 16, // bit 5
  13651. 'annot-forms' => 32, // bit 6
  13652. 'fill-forms' => 256, // bit 9
  13653. 'extract' => 512, // bit 10
  13654. 'assemble' => 1024,// bit 11
  13655. 'print-high' => 2048 // bit 12
  13656. );
  13657. $protection = 2147422012; // 32 bit: (01111111 11111111 00001111 00111100)
  13658. foreach ($permissions as $permission) {
  13659. if (!isset($options[$permission])) {
  13660. $this->Error('Incorrect permission: '.$permission);
  13661. }
  13662. if (($mode > 0) OR ($options[$permission] <= 32)) {
  13663. // set only valid permissions
  13664. if ($options[$permission] == 2) {
  13665. // the logic for bit 2 is inverted (cleared by default)
  13666. $protection += $options[$permission];
  13667. } else {
  13668. $protection -= $options[$permission];
  13669. }
  13670. }
  13671. }
  13672. return $protection;
  13673. }
  13674. /**
  13675. * Set document protection
  13676. * Remark: the protection against modification is for people who have the full Acrobat product.
  13677. * 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.
  13678. * 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.
  13679. * @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>
  13680. * @param $user_pass (String) user password. Empty by default.
  13681. * @param $owner_pass (String) owner password. If not specified, a random value is used.
  13682. * @param $mode (int) encryption strength: 0 = RC4 40 bit; 1 = RC4 128 bit; 2 = AES 128 bit; 3 = AES 256 bit.
  13683. * @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')))
  13684. * @public
  13685. * @since 2.0.000 (2008-01-02)
  13686. * @author Nicola Asuni
  13687. */
  13688. 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) {
  13689. if ($this->pdfa_mode) {
  13690. // encryption is not allowed in PDF/A mode
  13691. return;
  13692. }
  13693. $this->encryptdata['protection'] = $this->getUserPermissionCode($permissions, $mode);
  13694. if (($pubkeys !== null) AND (is_array($pubkeys))) {
  13695. // public-key mode
  13696. $this->encryptdata['pubkeys'] = $pubkeys;
  13697. if ($mode == 0) {
  13698. // public-Key Security requires at least 128 bit
  13699. $mode = 1;
  13700. }
  13701. if (!function_exists('openssl_pkcs7_encrypt')) {
  13702. $this->Error('Public-Key Security requires openssl library.');
  13703. }
  13704. // Set Public-Key filter (availabe are: Entrust.PPKEF, Adobe.PPKLite, Adobe.PubSec)
  13705. $this->encryptdata['pubkey'] = true;
  13706. $this->encryptdata['Filter'] = 'Adobe.PubSec';
  13707. $this->encryptdata['StmF'] = 'DefaultCryptFilter';
  13708. $this->encryptdata['StrF'] = 'DefaultCryptFilter';
  13709. } else {
  13710. // standard mode (password mode)
  13711. $this->encryptdata['pubkey'] = false;
  13712. $this->encryptdata['Filter'] = 'Standard';
  13713. $this->encryptdata['StmF'] = 'StdCF';
  13714. $this->encryptdata['StrF'] = 'StdCF';
  13715. }
  13716. if ($mode > 1) { // AES
  13717. if (!extension_loaded('mcrypt')) {
  13718. $this->Error('AES encryption requires mcrypt library (http://www.php.net/manual/en/mcrypt.requirements.php).');
  13719. }
  13720. if (mcrypt_get_cipher_name(MCRYPT_RIJNDAEL_128) === false) {
  13721. $this->Error('AES encryption requires MCRYPT_RIJNDAEL_128 cypher.');
  13722. }
  13723. if (($mode == 3) AND !function_exists('hash')) {
  13724. // the Hash extension requires no external libraries and is enabled by default as of PHP 5.1.2.
  13725. $this->Error('AES 256 encryption requires HASH Message Digest Framework (http://www.php.net/manual/en/book.hash.php).');
  13726. }
  13727. }
  13728. if ($owner_pass === null) {
  13729. $owner_pass = md5($this->getRandomSeed());
  13730. }
  13731. $this->encryptdata['user_password'] = $user_pass;
  13732. $this->encryptdata['owner_password'] = $owner_pass;
  13733. $this->encryptdata['mode'] = $mode;
  13734. switch ($mode) {
  13735. case 0: { // RC4 40 bit
  13736. $this->encryptdata['V'] = 1;
  13737. $this->encryptdata['Length'] = 40;
  13738. $this->encryptdata['CF']['CFM'] = 'V2';
  13739. break;
  13740. }
  13741. case 1: { // RC4 128 bit
  13742. $this->encryptdata['V'] = 2;
  13743. $this->encryptdata['Length'] = 128;
  13744. $this->encryptdata['CF']['CFM'] = 'V2';
  13745. if ($this->encryptdata['pubkey']) {
  13746. $this->encryptdata['SubFilter'] = 'adbe.pkcs7.s4';
  13747. $this->encryptdata['Recipients'] = array();
  13748. }
  13749. break;
  13750. }
  13751. case 2: { // AES 128 bit
  13752. $this->encryptdata['V'] = 4;
  13753. $this->encryptdata['Length'] = 128;
  13754. $this->encryptdata['CF']['CFM'] = 'AESV2';
  13755. $this->encryptdata['CF']['Length'] = 128;
  13756. if ($this->encryptdata['pubkey']) {
  13757. $this->encryptdata['SubFilter'] = 'adbe.pkcs7.s5';
  13758. $this->encryptdata['Recipients'] = array();
  13759. }
  13760. break;
  13761. }
  13762. case 3: { // AES 256 bit
  13763. $this->encryptdata['V'] = 5;
  13764. $this->encryptdata['Length'] = 256;
  13765. $this->encryptdata['CF']['CFM'] = 'AESV3';
  13766. $this->encryptdata['CF']['Length'] = 256;
  13767. if ($this->encryptdata['pubkey']) {
  13768. $this->encryptdata['SubFilter'] = 'adbe.pkcs7.s5';
  13769. $this->encryptdata['Recipients'] = array();
  13770. }
  13771. break;
  13772. }
  13773. }
  13774. $this->encrypted = true;
  13775. $this->encryptdata['fileid'] = $this->convertHexStringToString($this->file_id);
  13776. $this->_generateencryptionkey();
  13777. }
  13778. /**
  13779. * Convert hexadecimal string to string
  13780. * @param $bs (string) byte-string to convert
  13781. * @return String
  13782. * @protected
  13783. * @since 5.0.005 (2010-05-12)
  13784. * @author Nicola Asuni
  13785. */
  13786. protected function convertHexStringToString($bs) {
  13787. $string = ''; // string to be returned
  13788. $bslength = strlen($bs);
  13789. if (($bslength % 2) != 0) {
  13790. // padding
  13791. $bs .= '0';
  13792. ++$bslength;
  13793. }
  13794. for ($i = 0; $i < $bslength; $i += 2) {
  13795. $string .= chr(hexdec($bs[$i].$bs[($i + 1)]));
  13796. }
  13797. return $string;
  13798. }
  13799. /**
  13800. * Convert string to hexadecimal string (byte string)
  13801. * @param $s (string) string to convert
  13802. * @return byte string
  13803. * @protected
  13804. * @since 5.0.010 (2010-05-17)
  13805. * @author Nicola Asuni
  13806. */
  13807. protected function convertStringToHexString($s) {
  13808. $bs = '';
  13809. $chars = preg_split('//', $s, -1, PREG_SPLIT_NO_EMPTY);
  13810. foreach ($chars as $c) {
  13811. $bs .= sprintf('%02s', dechex(ord($c)));
  13812. }
  13813. return $bs;
  13814. }
  13815. /**
  13816. * Convert encryption P value to a string of bytes, low-order byte first.
  13817. * @param $protection (string) 32bit encryption permission value (P value)
  13818. * @return String
  13819. * @protected
  13820. * @since 5.0.005 (2010-05-12)
  13821. * @author Nicola Asuni
  13822. */
  13823. protected function getEncPermissionsString($protection) {
  13824. $binprot = sprintf('%032b', $protection);
  13825. $str = chr(bindec(substr($binprot, 24, 8)));
  13826. $str .= chr(bindec(substr($binprot, 16, 8)));
  13827. $str .= chr(bindec(substr($binprot, 8, 8)));
  13828. $str .= chr(bindec(substr($binprot, 0, 8)));
  13829. return $str;
  13830. }
  13831. // END OF ENCRYPTION FUNCTIONS -------------------------
  13832. // START TRANSFORMATIONS SECTION -----------------------
  13833. /**
  13834. * Starts a 2D tranformation saving current graphic state.
  13835. * This function must be called before scaling, mirroring, translation, rotation and skewing.
  13836. * Use StartTransform() before, and StopTransform() after the transformations to restore the normal behavior.
  13837. * @public
  13838. * @since 2.1.000 (2008-01-07)
  13839. * @see StartTransform(), StopTransform()
  13840. */
  13841. public function StartTransform() {
  13842. $this->_out('q');
  13843. if ($this->inxobj) {
  13844. // we are inside an XObject template
  13845. $this->xobjects[$this->xobjid]['transfmrk'][] = strlen($this->xobjects[$this->xobjid]['outdata']);
  13846. } else {
  13847. $this->transfmrk[$this->page][] = $this->pagelen[$this->page];
  13848. }
  13849. ++$this->transfmatrix_key;
  13850. $this->transfmatrix[$this->transfmatrix_key] = array();
  13851. }
  13852. /**
  13853. * Stops a 2D tranformation restoring previous graphic state.
  13854. * This function must be called after scaling, mirroring, translation, rotation and skewing.
  13855. * Use StartTransform() before, and StopTransform() after the transformations to restore the normal behavior.
  13856. * @public
  13857. * @since 2.1.000 (2008-01-07)
  13858. * @see StartTransform(), StopTransform()
  13859. */
  13860. public function StopTransform() {
  13861. $this->_out('Q');
  13862. if (isset($this->transfmatrix[$this->transfmatrix_key])) {
  13863. array_pop($this->transfmatrix[$this->transfmatrix_key]);
  13864. --$this->transfmatrix_key;
  13865. }
  13866. if ($this->inxobj) {
  13867. // we are inside an XObject template
  13868. array_pop($this->xobjects[$this->xobjid]['transfmrk']);
  13869. } else {
  13870. array_pop($this->transfmrk[$this->page]);
  13871. }
  13872. }
  13873. /**
  13874. * Horizontal Scaling.
  13875. * @param $s_x (float) scaling factor for width as percent. 0 is not allowed.
  13876. * @param $x (int) abscissa of the scaling center. Default is current x position
  13877. * @param $y (int) ordinate of the scaling center. Default is current y position
  13878. * @public
  13879. * @since 2.1.000 (2008-01-07)
  13880. * @see StartTransform(), StopTransform()
  13881. */
  13882. public function ScaleX($s_x, $x='', $y='') {
  13883. $this->Scale($s_x, 100, $x, $y);
  13884. }
  13885. /**
  13886. * Vertical Scaling.
  13887. * @param $s_y (float) scaling factor for height as percent. 0 is not allowed.
  13888. * @param $x (int) abscissa of the scaling center. Default is current x position
  13889. * @param $y (int) ordinate of the scaling center. Default is current y position
  13890. * @public
  13891. * @since 2.1.000 (2008-01-07)
  13892. * @see StartTransform(), StopTransform()
  13893. */
  13894. public function ScaleY($s_y, $x='', $y='') {
  13895. $this->Scale(100, $s_y, $x, $y);
  13896. }
  13897. /**
  13898. * Vertical and horizontal proportional Scaling.
  13899. * @param $s (float) scaling factor for width and height as percent. 0 is not allowed.
  13900. * @param $x (int) abscissa of the scaling center. Default is current x position
  13901. * @param $y (int) ordinate of the scaling center. Default is current y position
  13902. * @public
  13903. * @since 2.1.000 (2008-01-07)
  13904. * @see StartTransform(), StopTransform()
  13905. */
  13906. public function ScaleXY($s, $x='', $y='') {
  13907. $this->Scale($s, $s, $x, $y);
  13908. }
  13909. /**
  13910. * Vertical and horizontal non-proportional Scaling.
  13911. * @param $s_x (float) scaling factor for width as percent. 0 is not allowed.
  13912. * @param $s_y (float) scaling factor for height as percent. 0 is not allowed.
  13913. * @param $x (int) abscissa of the scaling center. Default is current x position
  13914. * @param $y (int) ordinate of the scaling center. Default is current y position
  13915. * @public
  13916. * @since 2.1.000 (2008-01-07)
  13917. * @see StartTransform(), StopTransform()
  13918. */
  13919. public function Scale($s_x, $s_y, $x='', $y='') {
  13920. if ($x === '') {
  13921. $x = $this->x;
  13922. }
  13923. if ($y === '') {
  13924. $y = $this->y;
  13925. }
  13926. if (($s_x == 0) OR ($s_y == 0)) {
  13927. $this->Error('Please do not use values equal to zero for scaling');
  13928. }
  13929. $y = ($this->h - $y) * $this->k;
  13930. $x *= $this->k;
  13931. //calculate elements of transformation matrix
  13932. $s_x /= 100;
  13933. $s_y /= 100;
  13934. $tm = array();
  13935. $tm[0] = $s_x;
  13936. $tm[1] = 0;
  13937. $tm[2] = 0;
  13938. $tm[3] = $s_y;
  13939. $tm[4] = $x * (1 - $s_x);
  13940. $tm[5] = $y * (1 - $s_y);
  13941. //scale the coordinate system
  13942. $this->Transform($tm);
  13943. }
  13944. /**
  13945. * Horizontal Mirroring.
  13946. * @param $x (int) abscissa of the point. Default is current x position
  13947. * @public
  13948. * @since 2.1.000 (2008-01-07)
  13949. * @see StartTransform(), StopTransform()
  13950. */
  13951. public function MirrorH($x='') {
  13952. $this->Scale(-100, 100, $x);
  13953. }
  13954. /**
  13955. * Verical Mirroring.
  13956. * @param $y (int) ordinate of the point. Default is current y position
  13957. * @public
  13958. * @since 2.1.000 (2008-01-07)
  13959. * @see StartTransform(), StopTransform()
  13960. */
  13961. public function MirrorV($y='') {
  13962. $this->Scale(100, -100, '', $y);
  13963. }
  13964. /**
  13965. * Point reflection mirroring.
  13966. * @param $x (int) abscissa of the point. Default is current x position
  13967. * @param $y (int) ordinate of the point. Default is current y position
  13968. * @public
  13969. * @since 2.1.000 (2008-01-07)
  13970. * @see StartTransform(), StopTransform()
  13971. */
  13972. public function MirrorP($x='',$y='') {
  13973. $this->Scale(-100, -100, $x, $y);
  13974. }
  13975. /**
  13976. * Reflection against a straight line through point (x, y) with the gradient angle (angle).
  13977. * @param $angle (float) gradient angle of the straight line. Default is 0 (horizontal line).
  13978. * @param $x (int) abscissa of the point. Default is current x position
  13979. * @param $y (int) ordinate of the point. Default is current y position
  13980. * @public
  13981. * @since 2.1.000 (2008-01-07)
  13982. * @see StartTransform(), StopTransform()
  13983. */
  13984. public function MirrorL($angle=0, $x='',$y='') {
  13985. $this->Scale(-100, 100, $x, $y);
  13986. $this->Rotate(-2*($angle-90), $x, $y);
  13987. }
  13988. /**
  13989. * Translate graphic object horizontally.
  13990. * @param $t_x (int) movement to the right (or left for RTL)
  13991. * @public
  13992. * @since 2.1.000 (2008-01-07)
  13993. * @see StartTransform(), StopTransform()
  13994. */
  13995. public function TranslateX($t_x) {
  13996. $this->Translate($t_x, 0);
  13997. }
  13998. /**
  13999. * Translate graphic object vertically.
  14000. * @param $t_y (int) movement to the bottom
  14001. * @public
  14002. * @since 2.1.000 (2008-01-07)
  14003. * @see StartTransform(), StopTransform()
  14004. */
  14005. public function TranslateY($t_y) {
  14006. $this->Translate(0, $t_y);
  14007. }
  14008. /**
  14009. * Translate graphic object horizontally and vertically.
  14010. * @param $t_x (int) movement to the right
  14011. * @param $t_y (int) movement to the bottom
  14012. * @public
  14013. * @since 2.1.000 (2008-01-07)
  14014. * @see StartTransform(), StopTransform()
  14015. */
  14016. public function Translate($t_x, $t_y) {
  14017. //calculate elements of transformation matrix
  14018. $tm = array();
  14019. $tm[0] = 1;
  14020. $tm[1] = 0;
  14021. $tm[2] = 0;
  14022. $tm[3] = 1;
  14023. $tm[4] = $t_x * $this->k;
  14024. $tm[5] = -$t_y * $this->k;
  14025. //translate the coordinate system
  14026. $this->Transform($tm);
  14027. }
  14028. /**
  14029. * Rotate object.
  14030. * @param $angle (float) angle in degrees for counter-clockwise rotation
  14031. * @param $x (int) abscissa of the rotation center. Default is current x position
  14032. * @param $y (int) ordinate of the rotation center. Default is current y position
  14033. * @public
  14034. * @since 2.1.000 (2008-01-07)
  14035. * @see StartTransform(), StopTransform()
  14036. */
  14037. public function Rotate($angle, $x='', $y='') {
  14038. if ($x === '') {
  14039. $x = $this->x;
  14040. }
  14041. if ($y === '') {
  14042. $y = $this->y;
  14043. }
  14044. $y = ($this->h - $y) * $this->k;
  14045. $x *= $this->k;
  14046. //calculate elements of transformation matrix
  14047. $tm = array();
  14048. $tm[0] = cos(deg2rad($angle));
  14049. $tm[1] = sin(deg2rad($angle));
  14050. $tm[2] = -$tm[1];
  14051. $tm[3] = $tm[0];
  14052. $tm[4] = $x + ($tm[1] * $y) - ($tm[0] * $x);
  14053. $tm[5] = $y - ($tm[0] * $y) - ($tm[1] * $x);
  14054. //rotate the coordinate system around ($x,$y)
  14055. $this->Transform($tm);
  14056. }
  14057. /**
  14058. * Skew horizontally.
  14059. * @param $angle_x (float) angle in degrees between -90 (skew to the left) and 90 (skew to the right)
  14060. * @param $x (int) abscissa of the skewing center. default is current x position
  14061. * @param $y (int) ordinate of the skewing center. default is current y position
  14062. * @public
  14063. * @since 2.1.000 (2008-01-07)
  14064. * @see StartTransform(), StopTransform()
  14065. */
  14066. public function SkewX($angle_x, $x='', $y='') {
  14067. $this->Skew($angle_x, 0, $x, $y);
  14068. }
  14069. /**
  14070. * Skew vertically.
  14071. * @param $angle_y (float) angle in degrees between -90 (skew to the bottom) and 90 (skew to the top)
  14072. * @param $x (int) abscissa of the skewing center. default is current x position
  14073. * @param $y (int) ordinate of the skewing center. default is current y position
  14074. * @public
  14075. * @since 2.1.000 (2008-01-07)
  14076. * @see StartTransform(), StopTransform()
  14077. */
  14078. public function SkewY($angle_y, $x='', $y='') {
  14079. $this->Skew(0, $angle_y, $x, $y);
  14080. }
  14081. /**
  14082. * Skew.
  14083. * @param $angle_x (float) angle in degrees between -90 (skew to the left) and 90 (skew to the right)
  14084. * @param $angle_y (float) angle in degrees between -90 (skew to the bottom) and 90 (skew to the top)
  14085. * @param $x (int) abscissa of the skewing center. default is current x position
  14086. * @param $y (int) ordinate of the skewing center. default is current y position
  14087. * @public
  14088. * @since 2.1.000 (2008-01-07)
  14089. * @see StartTransform(), StopTransform()
  14090. */
  14091. public function Skew($angle_x, $angle_y, $x='', $y='') {
  14092. if ($x === '') {
  14093. $x = $this->x;
  14094. }
  14095. if ($y === '') {
  14096. $y = $this->y;
  14097. }
  14098. if (($angle_x <= -90) OR ($angle_x >= 90) OR ($angle_y <= -90) OR ($angle_y >= 90)) {
  14099. $this->Error('Please use values between -90 and +90 degrees for Skewing.');
  14100. }
  14101. $x *= $this->k;
  14102. $y = ($this->h - $y) * $this->k;
  14103. //calculate elements of transformation matrix
  14104. $tm = array();
  14105. $tm[0] = 1;
  14106. $tm[1] = tan(deg2rad($angle_y));
  14107. $tm[2] = tan(deg2rad($angle_x));
  14108. $tm[3] = 1;
  14109. $tm[4] = -$tm[2] * $y;
  14110. $tm[5] = -$tm[1] * $x;
  14111. //skew the coordinate system
  14112. $this->Transform($tm);
  14113. }
  14114. /**
  14115. * Apply graphic transformations.
  14116. * @param $tm (array) transformation matrix
  14117. * @protected
  14118. * @since 2.1.000 (2008-01-07)
  14119. * @see StartTransform(), StopTransform()
  14120. */
  14121. protected function Transform($tm) {
  14122. $this->_out(sprintf('%.3F %.3F %.3F %.3F %.3F %.3F cm', $tm[0], $tm[1], $tm[2], $tm[3], $tm[4], $tm[5]));
  14123. // add tranformation matrix
  14124. $this->transfmatrix[$this->transfmatrix_key][] = array('a' => $tm[0], 'b' => $tm[1], 'c' => $tm[2], 'd' => $tm[3], 'e' => $tm[4], 'f' => $tm[5]);
  14125. // update transformation mark
  14126. if ($this->inxobj) {
  14127. // we are inside an XObject template
  14128. if (end($this->xobjects[$this->xobjid]['transfmrk']) !== false) {
  14129. $key = key($this->xobjects[$this->xobjid]['transfmrk']);
  14130. $this->xobjects[$this->xobjid]['transfmrk'][$key] = strlen($this->xobjects[$this->xobjid]['outdata']);
  14131. }
  14132. } elseif (end($this->transfmrk[$this->page]) !== false) {
  14133. $key = key($this->transfmrk[$this->page]);
  14134. $this->transfmrk[$this->page][$key] = $this->pagelen[$this->page];
  14135. }
  14136. }
  14137. // END TRANSFORMATIONS SECTION -------------------------
  14138. // START GRAPHIC FUNCTIONS SECTION ---------------------
  14139. // The following section is based on the code provided by David Hernandez Sanz
  14140. /**
  14141. * 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.
  14142. * @param $width (float) The width.
  14143. * @public
  14144. * @since 1.0
  14145. * @see Line(), Rect(), Cell(), MultiCell()
  14146. */
  14147. public function SetLineWidth($width) {
  14148. //Set line width
  14149. $this->LineWidth = $width;
  14150. $this->linestyleWidth = sprintf('%.2F w', ($width * $this->k));
  14151. if ($this->page > 0) {
  14152. $this->_out($this->linestyleWidth);
  14153. }
  14154. }
  14155. /**
  14156. * Returns the current the line width.
  14157. * @return int Line width
  14158. * @public
  14159. * @since 2.1.000 (2008-01-07)
  14160. * @see Line(), SetLineWidth()
  14161. */
  14162. public function GetLineWidth() {
  14163. return $this->LineWidth;
  14164. }
  14165. /**
  14166. * Set line style.
  14167. * @param $style (array) Line style. Array with keys among the following:
  14168. * <ul>
  14169. * <li>width (float): Width of the line in user units.</li>
  14170. * <li>cap (string): Type of cap to put on the line. Possible values are:
  14171. * butt, round, square. The difference between "square" and "butt" is that
  14172. * "square" projects a flat end past the end of the line.</li>
  14173. * <li>join (string): Type of join. Possible values are: miter, round,
  14174. * bevel.</li>
  14175. * <li>dash (mixed): Dash pattern. Is 0 (without dash) or string with
  14176. * series of length values, which are the lengths of the on and off dashes.
  14177. * For example: "2" represents 2 on, 2 off, 2 on, 2 off, ...; "2,1" is 2 on,
  14178. * 1 off, 2 on, 1 off, ...</li>
  14179. * <li>phase (integer): Modifier on the dash pattern which is used to shift
  14180. * the point at which the pattern starts.</li>
  14181. * <li>color (array): Draw color. Format: array(GREY) or array(R,G,B) or array(C,M,Y,K).</li>
  14182. * </ul>
  14183. * @param $ret (boolean) if true do not send the command.
  14184. * @return string the PDF command
  14185. * @public
  14186. * @since 2.1.000 (2008-01-08)
  14187. */
  14188. public function SetLineStyle($style, $ret=false) {
  14189. $s = ''; // string to be returned
  14190. if (!is_array($style)) {
  14191. return;
  14192. }
  14193. if (isset($style['width'])) {
  14194. $this->LineWidth = $style['width'];
  14195. $this->linestyleWidth = sprintf('%.2F w', ($style['width'] * $this->k));
  14196. $s .= $this->linestyleWidth.' ';
  14197. }
  14198. if (isset($style['cap'])) {
  14199. $ca = array('butt' => 0, 'round'=> 1, 'square' => 2);
  14200. if (isset($ca[$style['cap']])) {
  14201. $this->linestyleCap = $ca[$style['cap']].' J';
  14202. $s .= $this->linestyleCap.' ';
  14203. }
  14204. }
  14205. if (isset($style['join'])) {
  14206. $ja = array('miter' => 0, 'round' => 1, 'bevel' => 2);
  14207. if (isset($ja[$style['join']])) {
  14208. $this->linestyleJoin = $ja[$style['join']].' j';
  14209. $s .= $this->linestyleJoin.' ';
  14210. }
  14211. }
  14212. if (isset($style['dash'])) {
  14213. $dash_string = '';
  14214. if ($style['dash']) {
  14215. if (preg_match('/^.+,/', $style['dash']) > 0) {
  14216. $tab = explode(',', $style['dash']);
  14217. } else {
  14218. $tab = array($style['dash']);
  14219. }
  14220. $dash_string = '';
  14221. foreach ($tab as $i => $v) {
  14222. if ($i) {
  14223. $dash_string .= ' ';
  14224. }
  14225. $dash_string .= sprintf('%.2F', $v);
  14226. }
  14227. }
  14228. if (!isset($style['phase']) OR !$style['dash']) {
  14229. $style['phase'] = 0;
  14230. }
  14231. $this->linestyleDash = sprintf('[%s] %.2F d', $dash_string, $style['phase']);
  14232. $s .= $this->linestyleDash.' ';
  14233. }
  14234. if (isset($style['color'])) {
  14235. $s .= $this->SetDrawColorArray($style['color'], true).' ';
  14236. }
  14237. if (!$ret) {
  14238. $this->_out($s);
  14239. }
  14240. return $s;
  14241. }
  14242. /**
  14243. * Begin a new subpath by moving the current point to coordinates (x, y), omitting any connecting line segment.
  14244. * @param $x (float) Abscissa of point.
  14245. * @param $y (float) Ordinate of point.
  14246. * @protected
  14247. * @since 2.1.000 (2008-01-08)
  14248. */
  14249. protected function _outPoint($x, $y) {
  14250. $this->_out(sprintf('%.2F %.2F m', $x * $this->k, ($this->h - $y) * $this->k));
  14251. }
  14252. /**
  14253. * Append a straight line segment from the current point to the point (x, y).
  14254. * The new current point shall be (x, y).
  14255. * @param $x (float) Abscissa of end point.
  14256. * @param $y (float) Ordinate of end point.
  14257. * @protected
  14258. * @since 2.1.000 (2008-01-08)
  14259. */
  14260. protected function _outLine($x, $y) {
  14261. $this->_out(sprintf('%.2F %.2F l', $x * $this->k, ($this->h - $y) * $this->k));
  14262. }
  14263. /**
  14264. * Append a rectangle to the current path as a complete subpath, with lower-left corner (x, y) and dimensions widthand height in user space.
  14265. * @param $x (float) Abscissa of upper-left corner.
  14266. * @param $y (float) Ordinate of upper-left corner.
  14267. * @param $w (float) Width.
  14268. * @param $h (float) Height.
  14269. * @param $op (string) options
  14270. * @protected
  14271. * @since 2.1.000 (2008-01-08)
  14272. */
  14273. protected function _outRect($x, $y, $w, $h, $op) {
  14274. $this->_out(sprintf('%.2F %.2F %.2F %.2F re %s', $x * $this->k, ($this->h - $y) * $this->k, $w * $this->k, -$h * $this->k, $op));
  14275. }
  14276. /**
  14277. * 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.
  14278. * The new current point shall be (x3, y3).
  14279. * @param $x1 (float) Abscissa of control point 1.
  14280. * @param $y1 (float) Ordinate of control point 1.
  14281. * @param $x2 (float) Abscissa of control point 2.
  14282. * @param $y2 (float) Ordinate of control point 2.
  14283. * @param $x3 (float) Abscissa of end point.
  14284. * @param $y3 (float) Ordinate of end point.
  14285. * @protected
  14286. * @since 2.1.000 (2008-01-08)
  14287. */
  14288. protected function _outCurve($x1, $y1, $x2, $y2, $x3, $y3) {
  14289. $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));
  14290. }
  14291. /**
  14292. * 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.
  14293. * The new current point shall be (x3, y3).
  14294. * @param $x2 (float) Abscissa of control point 2.
  14295. * @param $y2 (float) Ordinate of control point 2.
  14296. * @param $x3 (float) Abscissa of end point.
  14297. * @param $y3 (float) Ordinate of end point.
  14298. * @protected
  14299. * @since 4.9.019 (2010-04-26)
  14300. */
  14301. protected function _outCurveV($x2, $y2, $x3, $y3) {
  14302. $this->_out(sprintf('%.2F %.2F %.2F %.2F v', $x2 * $this->k, ($this->h - $y2) * $this->k, $x3 * $this->k, ($this->h - $y3) * $this->k));
  14303. }
  14304. /**
  14305. * 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.
  14306. * The new current point shall be (x3, y3).
  14307. * @param $x1 (float) Abscissa of control point 1.
  14308. * @param $y1 (float) Ordinate of control point 1.
  14309. * @param $x3 (float) Abscissa of end point.
  14310. * @param $y3 (float) Ordinate of end point.
  14311. * @protected
  14312. * @since 2.1.000 (2008-01-08)
  14313. */
  14314. protected function _outCurveY($x1, $y1, $x3, $y3) {
  14315. $this->_out(sprintf('%.2F %.2F %.2F %.2F y', $x1 * $this->k, ($this->h - $y1) * $this->k, $x3 * $this->k, ($this->h - $y3) * $this->k));
  14316. }
  14317. /**
  14318. * Draws a line between two points.
  14319. * @param $x1 (float) Abscissa of first point.
  14320. * @param $y1 (float) Ordinate of first point.
  14321. * @param $x2 (float) Abscissa of second point.
  14322. * @param $y2 (float) Ordinate of second point.
  14323. * @param $style (array) Line style. Array like for SetLineStyle(). Default value: default line style (empty array).
  14324. * @public
  14325. * @since 1.0
  14326. * @see SetLineWidth(), SetDrawColor(), SetLineStyle()
  14327. */
  14328. public function Line($x1, $y1, $x2, $y2, $style=array()) {
  14329. if (is_array($style)) {
  14330. $this->SetLineStyle($style);
  14331. }
  14332. $this->_outPoint($x1, $y1);
  14333. $this->_outLine($x2, $y2);
  14334. $this->_out('S');
  14335. }
  14336. /**
  14337. * Draws a rectangle.
  14338. * @param $x (float) Abscissa of upper-left corner.
  14339. * @param $y (float) Ordinate of upper-left corner.
  14340. * @param $w (float) Width.
  14341. * @param $h (float) Height.
  14342. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  14343. * @param $border_style (array) Border style of rectangle. Array with keys among the following:
  14344. * <ul>
  14345. * <li>all: Line style of all borders. Array like for SetLineStyle().</li>
  14346. * <li>L, T, R, B or combinations: Line style of left, top, right or bottom border. Array like for SetLineStyle().</li>
  14347. * </ul>
  14348. * If a key is not present or is null, not draws the border. Default value: default line style (empty array).
  14349. * @param $border_style (array) Border style of rectangle. Array like for SetLineStyle(). Default value: default line style (empty array).
  14350. * @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).
  14351. * @public
  14352. * @since 1.0
  14353. * @see SetLineStyle()
  14354. */
  14355. public function Rect($x, $y, $w, $h, $style='', $border_style=array(), $fill_color=array()) {
  14356. if (!(false === strpos($style, 'F')) AND !empty($fill_color)) {
  14357. $this->SetFillColorArray($fill_color);
  14358. }
  14359. $op = $this->getPathPaintOperator($style);
  14360. if ((!$border_style) OR (isset($border_style['all']))) {
  14361. if (isset($border_style['all']) AND $border_style['all']) {
  14362. $this->SetLineStyle($border_style['all']);
  14363. $border_style = array();
  14364. }
  14365. }
  14366. $this->_outRect($x, $y, $w, $h, $op);
  14367. if ($border_style) {
  14368. $border_style2 = array();
  14369. foreach ($border_style as $line => $value) {
  14370. $length = strlen($line);
  14371. for ($i = 0; $i < $length; ++$i) {
  14372. $border_style2[$line[$i]] = $value;
  14373. }
  14374. }
  14375. $border_style = $border_style2;
  14376. if (isset($border_style['L']) AND $border_style['L']) {
  14377. $this->Line($x, $y, $x, $y + $h, $border_style['L']);
  14378. }
  14379. if (isset($border_style['T']) AND $border_style['T']) {
  14380. $this->Line($x, $y, $x + $w, $y, $border_style['T']);
  14381. }
  14382. if (isset($border_style['R']) AND $border_style['R']) {
  14383. $this->Line($x + $w, $y, $x + $w, $y + $h, $border_style['R']);
  14384. }
  14385. if (isset($border_style['B']) AND $border_style['B']) {
  14386. $this->Line($x, $y + $h, $x + $w, $y + $h, $border_style['B']);
  14387. }
  14388. }
  14389. }
  14390. /**
  14391. * Draws a Bezier curve.
  14392. * The Bezier curve is a tangent to the line between the control points at
  14393. * either end of the curve.
  14394. * @param $x0 (float) Abscissa of start point.
  14395. * @param $y0 (float) Ordinate of start point.
  14396. * @param $x1 (float) Abscissa of control point 1.
  14397. * @param $y1 (float) Ordinate of control point 1.
  14398. * @param $x2 (float) Abscissa of control point 2.
  14399. * @param $y2 (float) Ordinate of control point 2.
  14400. * @param $x3 (float) Abscissa of end point.
  14401. * @param $y3 (float) Ordinate of end point.
  14402. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  14403. * @param $line_style (array) Line style of curve. Array like for SetLineStyle(). Default value: default line style (empty array).
  14404. * @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).
  14405. * @public
  14406. * @see SetLineStyle()
  14407. * @since 2.1.000 (2008-01-08)
  14408. */
  14409. public function Curve($x0, $y0, $x1, $y1, $x2, $y2, $x3, $y3, $style='', $line_style=array(), $fill_color=array()) {
  14410. if (!(false === strpos($style, 'F')) AND isset($fill_color)) {
  14411. $this->SetFillColorArray($fill_color);
  14412. }
  14413. $op = $this->getPathPaintOperator($style);
  14414. if ($line_style) {
  14415. $this->SetLineStyle($line_style);
  14416. }
  14417. $this->_outPoint($x0, $y0);
  14418. $this->_outCurve($x1, $y1, $x2, $y2, $x3, $y3);
  14419. $this->_out($op);
  14420. }
  14421. /**
  14422. * Draws a poly-Bezier curve.
  14423. * Each Bezier curve segment is a tangent to the line between the control points at
  14424. * either end of the curve.
  14425. * @param $x0 (float) Abscissa of start point.
  14426. * @param $y0 (float) Ordinate of start point.
  14427. * @param $segments (float) An array of bezier descriptions. Format: array(x1, y1, x2, y2, x3, y3).
  14428. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  14429. * @param $line_style (array) Line style of curve. Array like for SetLineStyle(). Default value: default line style (empty array).
  14430. * @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).
  14431. * @public
  14432. * @see SetLineStyle()
  14433. * @since 3.0008 (2008-05-12)
  14434. */
  14435. public function Polycurve($x0, $y0, $segments, $style='', $line_style=array(), $fill_color=array()) {
  14436. if (!(false === strpos($style, 'F')) AND isset($fill_color)) {
  14437. $this->SetFillColorArray($fill_color);
  14438. }
  14439. $op = $this->getPathPaintOperator($style);
  14440. if ($op == 'f') {
  14441. $line_style = array();
  14442. }
  14443. if ($line_style) {
  14444. $this->SetLineStyle($line_style);
  14445. }
  14446. $this->_outPoint($x0, $y0);
  14447. foreach ($segments as $segment) {
  14448. list($x1, $y1, $x2, $y2, $x3, $y3) = $segment;
  14449. $this->_outCurve($x1, $y1, $x2, $y2, $x3, $y3);
  14450. }
  14451. $this->_out($op);
  14452. }
  14453. /**
  14454. * Draws an ellipse.
  14455. * An ellipse is formed from n Bezier curves.
  14456. * @param $x0 (float) Abscissa of center point.
  14457. * @param $y0 (float) Ordinate of center point.
  14458. * @param $rx (float) Horizontal radius.
  14459. * @param $ry (float) Vertical radius (if ry = 0 then is a circle, see Circle()). Default value: 0.
  14460. * @param $angle: (float) Angle oriented (anti-clockwise). Default value: 0.
  14461. * @param $astart: (float) Angle start of draw line. Default value: 0.
  14462. * @param $afinish: (float) Angle finish of draw line. Default value: 360.
  14463. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  14464. * @param $line_style (array) Line style of ellipse. Array like for SetLineStyle(). Default value: default line style (empty array).
  14465. * @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).
  14466. * @param $nc (integer) Number of curves used to draw a 90 degrees portion of ellipse.
  14467. * @author Nicola Asuni
  14468. * @public
  14469. * @since 2.1.000 (2008-01-08)
  14470. */
  14471. public function Ellipse($x0, $y0, $rx, $ry='', $angle=0, $astart=0, $afinish=360, $style='', $line_style=array(), $fill_color=array(), $nc=2) {
  14472. if ($this->empty_string($ry) OR ($ry == 0)) {
  14473. $ry = $rx;
  14474. }
  14475. if (!(false === strpos($style, 'F')) AND isset($fill_color)) {
  14476. $this->SetFillColorArray($fill_color);
  14477. }
  14478. $op = $this->getPathPaintOperator($style);
  14479. if ($op == 'f') {
  14480. $line_style = array();
  14481. }
  14482. if ($line_style) {
  14483. $this->SetLineStyle($line_style);
  14484. }
  14485. $this->_outellipticalarc($x0, $y0, $rx, $ry, $angle, $astart, $afinish, false, $nc, true, true, false);
  14486. $this->_out($op);
  14487. }
  14488. /**
  14489. * Append an elliptical arc to the current path.
  14490. * An ellipse is formed from n Bezier curves.
  14491. * @param $xc (float) Abscissa of center point.
  14492. * @param $yc (float) Ordinate of center point.
  14493. * @param $rx (float) Horizontal radius.
  14494. * @param $ry (float) Vertical radius (if ry = 0 then is a circle, see Circle()). Default value: 0.
  14495. * @param $xang: (float) Angle between the X-axis and the major axis of the ellipse. Default value: 0.
  14496. * @param $angs: (float) Angle start of draw line. Default value: 0.
  14497. * @param $angf: (float) Angle finish of draw line. Default value: 360.
  14498. * @param $pie (boolean) if true do not mark the border point (used to draw pie sectors).
  14499. * @param $nc (integer) Number of curves used to draw a 90 degrees portion of ellipse.
  14500. * @param $startpoint (boolean) if true output a starting point.
  14501. * @param $ccw (boolean) if true draws in counter-clockwise.
  14502. * @param $svg (boolean) if true the angles are in svg mode (already calculated).
  14503. * @return array bounding box coordinates (x min, y min, x max, y max)
  14504. * @author Nicola Asuni
  14505. * @protected
  14506. * @since 4.9.019 (2010-04-26)
  14507. */
  14508. protected function _outellipticalarc($xc, $yc, $rx, $ry, $xang=0, $angs=0, $angf=360, $pie=false, $nc=2, $startpoint=true, $ccw=true, $svg=false) {
  14509. $k = $this->k;
  14510. if ($nc < 2) {
  14511. $nc = 2;
  14512. }
  14513. $xmin = 2147483647;
  14514. $ymin = 2147483647;
  14515. $xmax = 0;
  14516. $ymax = 0;
  14517. if ($pie) {
  14518. // center of the arc
  14519. $this->_outPoint($xc, $yc);
  14520. }
  14521. $xang = deg2rad((float) $xang);
  14522. $angs = deg2rad((float) $angs);
  14523. $angf = deg2rad((float) $angf);
  14524. if ($svg) {
  14525. $as = $angs;
  14526. $af = $angf;
  14527. } else {
  14528. $as = atan2((sin($angs) / $ry), (cos($angs) / $rx));
  14529. $af = atan2((sin($angf) / $ry), (cos($angf) / $rx));
  14530. }
  14531. if ($as < 0) {
  14532. $as += (2 * M_PI);
  14533. }
  14534. if ($af < 0) {
  14535. $af += (2 * M_PI);
  14536. }
  14537. if ($ccw AND ($as > $af)) {
  14538. // reverse rotation
  14539. $as -= (2 * M_PI);
  14540. } elseif (!$ccw AND ($as < $af)) {
  14541. // reverse rotation
  14542. $af -= (2 * M_PI);
  14543. }
  14544. $total_angle = ($af - $as);
  14545. if ($nc < 2) {
  14546. $nc = 2;
  14547. }
  14548. // total arcs to draw
  14549. $nc *= (2 * abs($total_angle) / M_PI);
  14550. $nc = round($nc) + 1;
  14551. // angle of each arc
  14552. $arcang = ($total_angle / $nc);
  14553. // center point in PDF coordinates
  14554. $x0 = $xc;
  14555. $y0 = ($this->h - $yc);
  14556. // starting angle
  14557. $ang = $as;
  14558. $alpha = sin($arcang) * ((sqrt(4 + (3 * pow(tan(($arcang) / 2), 2))) - 1) / 3);
  14559. $cos_xang = cos($xang);
  14560. $sin_xang = sin($xang);
  14561. $cos_ang = cos($ang);
  14562. $sin_ang = sin($ang);
  14563. // first arc point
  14564. $px1 = $x0 + ($rx * $cos_xang * $cos_ang) - ($ry * $sin_xang * $sin_ang);
  14565. $py1 = $y0 + ($rx * $sin_xang * $cos_ang) + ($ry * $cos_xang * $sin_ang);
  14566. // first Bezier control point
  14567. $qx1 = ($alpha * ((-$rx * $cos_xang * $sin_ang) - ($ry * $sin_xang * $cos_ang)));
  14568. $qy1 = ($alpha * ((-$rx * $sin_xang * $sin_ang) + ($ry * $cos_xang * $cos_ang)));
  14569. if ($pie) {
  14570. // line from center to arc starting point
  14571. $this->_outLine($px1, $this->h - $py1);
  14572. } elseif ($startpoint) {
  14573. // arc starting point
  14574. $this->_outPoint($px1, $this->h - $py1);
  14575. }
  14576. // draw arcs
  14577. for ($i = 1; $i <= $nc; ++$i) {
  14578. // starting angle
  14579. $ang = $as + ($i * $arcang);
  14580. if ($i == $nc) {
  14581. $ang = $af;
  14582. }
  14583. $cos_ang = cos($ang);
  14584. $sin_ang = sin($ang);
  14585. // second arc point
  14586. $px2 = $x0 + ($rx * $cos_xang * $cos_ang) - ($ry * $sin_xang * $sin_ang);
  14587. $py2 = $y0 + ($rx * $sin_xang * $cos_ang) + ($ry * $cos_xang * $sin_ang);
  14588. // second Bezier control point
  14589. $qx2 = ($alpha * ((-$rx * $cos_xang * $sin_ang) - ($ry * $sin_xang * $cos_ang)));
  14590. $qy2 = ($alpha * ((-$rx * $sin_xang * $sin_ang) + ($ry * $cos_xang * $cos_ang)));
  14591. // draw arc
  14592. $cx1 = ($px1 + $qx1);
  14593. $cy1 = ($this->h - ($py1 + $qy1));
  14594. $cx2 = ($px2 - $qx2);
  14595. $cy2 = ($this->h - ($py2 - $qy2));
  14596. $cx3 = $px2;
  14597. $cy3 = ($this->h - $py2);
  14598. $this->_outCurve($cx1, $cy1, $cx2, $cy2, $cx3, $cy3);
  14599. // get bounding box coordinates
  14600. $xmin = min($xmin, $cx1, $cx2, $cx3);
  14601. $ymin = min($ymin, $cy1, $cy2, $cy3);
  14602. $xmax = max($xmax, $cx1, $cx2, $cx3);
  14603. $ymax = max($ymax, $cy1, $cy2, $cy3);
  14604. // move to next point
  14605. $px1 = $px2;
  14606. $py1 = $py2;
  14607. $qx1 = $qx2;
  14608. $qy1 = $qy2;
  14609. }
  14610. if ($pie) {
  14611. $this->_outLine($xc, $yc);
  14612. // get bounding box coordinates
  14613. $xmin = min($xmin, $xc);
  14614. $ymin = min($ymin, $yc);
  14615. $xmax = max($xmax, $xc);
  14616. $ymax = max($ymax, $yc);
  14617. }
  14618. return array($xmin, $ymin, $xmax, $ymax);
  14619. }
  14620. /**
  14621. * Draws a circle.
  14622. * A circle is formed from n Bezier curves.
  14623. * @param $x0 (float) Abscissa of center point.
  14624. * @param $y0 (float) Ordinate of center point.
  14625. * @param $r (float) Radius.
  14626. * @param $angstr: (float) Angle start of draw line. Default value: 0.
  14627. * @param $angend: (float) Angle finish of draw line. Default value: 360.
  14628. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  14629. * @param $line_style (array) Line style of circle. Array like for SetLineStyle(). Default value: default line style (empty array).
  14630. * @param $fill_color (array) Fill color. Format: array(red, green, blue). Default value: default color (empty array).
  14631. * @param $nc (integer) Number of curves used to draw a 90 degrees portion of circle.
  14632. * @public
  14633. * @since 2.1.000 (2008-01-08)
  14634. */
  14635. public function Circle($x0, $y0, $r, $angstr=0, $angend=360, $style='', $line_style=array(), $fill_color=array(), $nc=2) {
  14636. $this->Ellipse($x0, $y0, $r, $r, 0, $angstr, $angend, $style, $line_style, $fill_color, $nc);
  14637. }
  14638. /**
  14639. * Draws a polygonal line
  14640. * @param $p (array) Points 0 to ($np - 1). Array with values (x0, y0, x1, y1,..., x(np-1), y(np - 1))
  14641. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  14642. * @param $line_style (array) Line style of polygon. Array with keys among the following:
  14643. * <ul>
  14644. * <li>all: Line style of all lines. Array like for SetLineStyle().</li>
  14645. * <li>0 to ($np - 1): Line style of each line. Array like for SetLineStyle().</li>
  14646. * </ul>
  14647. * If a key is not present or is null, not draws the line. Default value is default line style (empty array).
  14648. * @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).
  14649. * @since 4.8.003 (2009-09-15)
  14650. * @public
  14651. */
  14652. public function PolyLine($p, $style='', $line_style=array(), $fill_color=array()) {
  14653. $this->Polygon($p, $style, $line_style, $fill_color, false);
  14654. }
  14655. /**
  14656. * Draws a polygon.
  14657. * @param $p (array) Points 0 to ($np - 1). Array with values (x0, y0, x1, y1,..., x(np-1), y(np - 1))
  14658. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  14659. * @param $line_style (array) Line style of polygon. Array with keys among the following:
  14660. * <ul>
  14661. * <li>all: Line style of all lines. Array like for SetLineStyle().</li>
  14662. * <li>0 to ($np - 1): Line style of each line. Array like for SetLineStyle().</li>
  14663. * </ul>
  14664. * If a key is not present or is null, not draws the line. Default value is default line style (empty array).
  14665. * @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).
  14666. * @param $closed (boolean) if true the polygon is closes, otherwise will remain open
  14667. * @public
  14668. * @since 2.1.000 (2008-01-08)
  14669. */
  14670. public function Polygon($p, $style='', $line_style=array(), $fill_color=array(), $closed=true) {
  14671. $nc = count($p); // number of coordinates
  14672. $np = $nc / 2; // number of points
  14673. if ($closed) {
  14674. // close polygon by adding the first 2 points at the end (one line)
  14675. for ($i = 0; $i < 4; ++$i) {
  14676. $p[$nc + $i] = $p[$i];
  14677. }
  14678. // copy style for the last added line
  14679. if (isset($line_style[0])) {
  14680. $line_style[$np] = $line_style[0];
  14681. }
  14682. $nc += 4;
  14683. }
  14684. if (!(false === strpos($style, 'F')) AND isset($fill_color)) {
  14685. $this->SetFillColorArray($fill_color);
  14686. }
  14687. $op = $this->getPathPaintOperator($style);
  14688. if ($op == 'f') {
  14689. $line_style = array();
  14690. }
  14691. $draw = true;
  14692. if ($line_style) {
  14693. if (isset($line_style['all'])) {
  14694. $this->SetLineStyle($line_style['all']);
  14695. } else {
  14696. $draw = false;
  14697. if ($op == 'B') {
  14698. // draw fill
  14699. $op = 'f';
  14700. $this->_outPoint($p[0], $p[1]);
  14701. for ($i = 2; $i < $nc; $i = $i + 2) {
  14702. $this->_outLine($p[$i], $p[$i + 1]);
  14703. }
  14704. $this->_out($op);
  14705. }
  14706. // draw outline
  14707. $this->_outPoint($p[0], $p[1]);
  14708. for ($i = 2; $i < $nc; $i = $i + 2) {
  14709. $line_num = ($i / 2) - 1;
  14710. if (isset($line_style[$line_num])) {
  14711. if ($line_style[$line_num] != 0) {
  14712. if (is_array($line_style[$line_num])) {
  14713. $this->_out('S');
  14714. $this->SetLineStyle($line_style[$line_num]);
  14715. $this->_outPoint($p[$i - 2], $p[$i - 1]);
  14716. $this->_outLine($p[$i], $p[$i + 1]);
  14717. $this->_out('S');
  14718. $this->_outPoint($p[$i], $p[$i + 1]);
  14719. } else {
  14720. $this->_outLine($p[$i], $p[$i + 1]);
  14721. }
  14722. }
  14723. } else {
  14724. $this->_outLine($p[$i], $p[$i + 1]);
  14725. }
  14726. }
  14727. $this->_out($op);
  14728. }
  14729. }
  14730. if ($draw) {
  14731. $this->_outPoint($p[0], $p[1]);
  14732. for ($i = 2; $i < $nc; $i = $i + 2) {
  14733. $this->_outLine($p[$i], $p[$i + 1]);
  14734. }
  14735. $this->_out($op);
  14736. }
  14737. }
  14738. /**
  14739. * Draws a regular polygon.
  14740. * @param $x0 (float) Abscissa of center point.
  14741. * @param $y0 (float) Ordinate of center point.
  14742. * @param $r: (float) Radius of inscribed circle.
  14743. * @param $ns (integer) Number of sides.
  14744. * @param $angle (float) Angle oriented (anti-clockwise). Default value: 0.
  14745. * @param $draw_circle (boolean) Draw inscribed circle or not. Default value: false.
  14746. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  14747. * @param $line_style (array) Line style of polygon sides. Array with keys among the following:
  14748. * <ul>
  14749. * <li>all: Line style of all sides. Array like for SetLineStyle().</li>
  14750. * <li>0 to ($ns - 1): Line style of each side. Array like for SetLineStyle().</li>
  14751. * </ul>
  14752. * If a key is not present or is null, not draws the side. Default value is default line style (empty array).
  14753. * @param $fill_color (array) Fill color. Format: array(red, green, blue). Default value: default color (empty array).
  14754. * @param $circle_style (string) Style of rendering of inscribed circle (if draws). Possible values are:
  14755. * <ul>
  14756. * <li>D or empty string: Draw (default).</li>
  14757. * <li>F: Fill.</li>
  14758. * <li>DF or FD: Draw and fill.</li>
  14759. * <li>CNZ: Clipping mode (using the even-odd rule to determine which regions lie inside the clipping path).</li>
  14760. * <li>CEO: Clipping mode (using the nonzero winding number rule to determine which regions lie inside the clipping path).</li>
  14761. * </ul>
  14762. * @param $circle_outLine_style (array) Line style of inscribed circle (if draws). Array like for SetLineStyle(). Default value: default line style (empty array).
  14763. * @param $circle_fill_color (array) Fill color of inscribed circle (if draws). Format: array(red, green, blue). Default value: default color (empty array).
  14764. * @public
  14765. * @since 2.1.000 (2008-01-08)
  14766. */
  14767. 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()) {
  14768. if (3 > $ns) {
  14769. $ns = 3;
  14770. }
  14771. if ($draw_circle) {
  14772. $this->Circle($x0, $y0, $r, 0, 360, $circle_style, $circle_outLine_style, $circle_fill_color);
  14773. }
  14774. $p = array();
  14775. for ($i = 0; $i < $ns; ++$i) {
  14776. $a = $angle + ($i * 360 / $ns);
  14777. $a_rad = deg2rad((float) $a);
  14778. $p[] = $x0 + ($r * sin($a_rad));
  14779. $p[] = $y0 + ($r * cos($a_rad));
  14780. }
  14781. $this->Polygon($p, $style, $line_style, $fill_color);
  14782. }
  14783. /**
  14784. * Draws a star polygon
  14785. * @param $x0 (float) Abscissa of center point.
  14786. * @param $y0 (float) Ordinate of center point.
  14787. * @param $r (float) Radius of inscribed circle.
  14788. * @param $nv (integer) Number of vertices.
  14789. * @param $ng (integer) Number of gap (if ($ng % $nv = 1) then is a regular polygon).
  14790. * @param $angle: (float) Angle oriented (anti-clockwise). Default value: 0.
  14791. * @param $draw_circle: (boolean) Draw inscribed circle or not. Default value is false.
  14792. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  14793. * @param $line_style (array) Line style of polygon sides. Array with keys among the following:
  14794. * <ul>
  14795. * <li>all: Line style of all sides. Array like for
  14796. * SetLineStyle().</li>
  14797. * <li>0 to (n - 1): Line style of each side. Array like for SetLineStyle().</li>
  14798. * </ul>
  14799. * If a key is not present or is null, not draws the side. Default value is default line style (empty array).
  14800. * @param $fill_color (array) Fill color. Format: array(red, green, blue). Default value: default color (empty array).
  14801. * @param $circle_style (string) Style of rendering of inscribed circle (if draws). Possible values are:
  14802. * <ul>
  14803. * <li>D or empty string: Draw (default).</li>
  14804. * <li>F: Fill.</li>
  14805. * <li>DF or FD: Draw and fill.</li>
  14806. * <li>CNZ: Clipping mode (using the even-odd rule to determine which regions lie inside the clipping path).</li>
  14807. * <li>CEO: Clipping mode (using the nonzero winding number rule to determine which regions lie inside the clipping path).</li>
  14808. * </ul>
  14809. * @param $circle_outLine_style (array) Line style of inscribed circle (if draws). Array like for SetLineStyle(). Default value: default line style (empty array).
  14810. * @param $circle_fill_color (array) Fill color of inscribed circle (if draws). Format: array(red, green, blue). Default value: default color (empty array).
  14811. * @public
  14812. * @since 2.1.000 (2008-01-08)
  14813. */
  14814. 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()) {
  14815. if ($nv < 2) {
  14816. $nv = 2;
  14817. }
  14818. if ($draw_circle) {
  14819. $this->Circle($x0, $y0, $r, 0, 360, $circle_style, $circle_outLine_style, $circle_fill_color);
  14820. }
  14821. $p2 = array();
  14822. $visited = array();
  14823. for ($i = 0; $i < $nv; ++$i) {
  14824. $a = $angle + ($i * 360 / $nv);
  14825. $a_rad = deg2rad((float) $a);
  14826. $p2[] = $x0 + ($r * sin($a_rad));
  14827. $p2[] = $y0 + ($r * cos($a_rad));
  14828. $visited[] = false;
  14829. }
  14830. $p = array();
  14831. $i = 0;
  14832. do {
  14833. $p[] = $p2[$i * 2];
  14834. $p[] = $p2[($i * 2) + 1];
  14835. $visited[$i] = true;
  14836. $i += $ng;
  14837. $i %= $nv;
  14838. } while (!$visited[$i]);
  14839. $this->Polygon($p, $style, $line_style, $fill_color);
  14840. }
  14841. /**
  14842. * Draws a rounded rectangle.
  14843. * @param $x (float) Abscissa of upper-left corner.
  14844. * @param $y (float) Ordinate of upper-left corner.
  14845. * @param $w (float) Width.
  14846. * @param $h (float) Height.
  14847. * @param $r (float) the radius of the circle used to round off the corners of the rectangle.
  14848. * @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").
  14849. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  14850. * @param $border_style (array) Border style of rectangle. Array like for SetLineStyle(). Default value: default line style (empty array).
  14851. * @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).
  14852. * @public
  14853. * @since 2.1.000 (2008-01-08)
  14854. */
  14855. public function RoundedRect($x, $y, $w, $h, $r, $round_corner='1111', $style='', $border_style=array(), $fill_color=array()) {
  14856. $this->RoundedRectXY($x, $y, $w, $h, $r, $r, $round_corner, $style, $border_style, $fill_color);
  14857. }
  14858. /**
  14859. * Draws a rounded rectangle.
  14860. * @param $x (float) Abscissa of upper-left corner.
  14861. * @param $y (float) Ordinate of upper-left corner.
  14862. * @param $w (float) Width.
  14863. * @param $h (float) Height.
  14864. * @param $rx (float) the x-axis radius of the ellipse used to round off the corners of the rectangle.
  14865. * @param $ry (float) the y-axis radius of the ellipse used to round off the corners of the rectangle.
  14866. * @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").
  14867. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  14868. * @param $border_style (array) Border style of rectangle. Array like for SetLineStyle(). Default value: default line style (empty array).
  14869. * @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).
  14870. * @public
  14871. * @since 4.9.019 (2010-04-22)
  14872. */
  14873. public function RoundedRectXY($x, $y, $w, $h, $rx, $ry, $round_corner='1111', $style='', $border_style=array(), $fill_color=array()) {
  14874. if (($round_corner == '0000') OR (($rx == $ry) AND ($rx == 0))) {
  14875. // Not rounded
  14876. $this->Rect($x, $y, $w, $h, $style, $border_style, $fill_color);
  14877. return;
  14878. }
  14879. // Rounded
  14880. if (!(false === strpos($style, 'F')) AND isset($fill_color)) {
  14881. $this->SetFillColorArray($fill_color);
  14882. }
  14883. $op = $this->getPathPaintOperator($style);
  14884. if ($op == 'f') {
  14885. $border_style = array();
  14886. }
  14887. if ($border_style) {
  14888. $this->SetLineStyle($border_style);
  14889. }
  14890. $MyArc = 4 / 3 * (sqrt(2) - 1);
  14891. $this->_outPoint($x + $rx, $y);
  14892. $xc = $x + $w - $rx;
  14893. $yc = $y + $ry;
  14894. $this->_outLine($xc, $y);
  14895. if ($round_corner[0]) {
  14896. $this->_outCurve($xc + ($rx * $MyArc), $yc - $ry, $xc + $rx, $yc - ($ry * $MyArc), $xc + $rx, $yc);
  14897. } else {
  14898. $this->_outLine($x + $w, $y);
  14899. }
  14900. $xc = $x + $w - $rx;
  14901. $yc = $y + $h - $ry;
  14902. $this->_outLine($x + $w, $yc);
  14903. if ($round_corner[1]) {
  14904. $this->_outCurve($xc + $rx, $yc + ($ry * $MyArc), $xc + ($rx * $MyArc), $yc + $ry, $xc, $yc + $ry);
  14905. } else {
  14906. $this->_outLine($x + $w, $y + $h);
  14907. }
  14908. $xc = $x + $rx;
  14909. $yc = $y + $h - $ry;
  14910. $this->_outLine($xc, $y + $h);
  14911. if ($round_corner[2]) {
  14912. $this->_outCurve($xc - ($rx * $MyArc), $yc + $ry, $xc - $rx, $yc + ($ry * $MyArc), $xc - $rx, $yc);
  14913. } else {
  14914. $this->_outLine($x, $y + $h);
  14915. }
  14916. $xc = $x + $rx;
  14917. $yc = $y + $ry;
  14918. $this->_outLine($x, $yc);
  14919. if ($round_corner[3]) {
  14920. $this->_outCurve($xc - $rx, $yc - ($ry * $MyArc), $xc - ($rx * $MyArc), $yc - $ry, $xc, $yc - $ry);
  14921. } else {
  14922. $this->_outLine($x, $y);
  14923. $this->_outLine($x + $rx, $y);
  14924. }
  14925. $this->_out($op);
  14926. }
  14927. /**
  14928. * Draws a grahic arrow.
  14929. * @param $x0 (float) Abscissa of first point.
  14930. * @param $y0 (float) Ordinate of first point.
  14931. * @param $x1 (float) Abscissa of second point.
  14932. * @param $y1 (float) Ordinate of second point.
  14933. * @param $head_style (int) (0 = draw only arrowhead arms, 1 = draw closed arrowhead, but no fill, 2 = closed and filled arrowhead, 3 = filled arrowhead)
  14934. * @param $arm_size (float) length of arrowhead arms
  14935. * @param $arm_angle (int) angle between an arm and the shaft
  14936. * @author Piotr Galecki, Nicola Asuni, Andy Meier
  14937. * @since 4.6.018 (2009-07-10)
  14938. */
  14939. public function Arrow($x0, $y0, $x1, $y1, $head_style=0, $arm_size=5, $arm_angle=15) {
  14940. // getting arrow direction angle
  14941. // 0 deg angle is when both arms go along X axis. angle grows clockwise.
  14942. $dir_angle = atan2(($y0 - $y1), ($x0 - $x1));
  14943. if ($dir_angle < 0) {
  14944. $dir_angle += (2 * M_PI);
  14945. }
  14946. $arm_angle = deg2rad($arm_angle);
  14947. $sx1 = $x1;
  14948. $sy1 = $y1;
  14949. if ($head_style > 0) {
  14950. // calculate the stopping point for the arrow shaft
  14951. $sx1 = $x1 + (($arm_size - $this->LineWidth) * cos($dir_angle));
  14952. $sy1 = $y1 + (($arm_size - $this->LineWidth) * sin($dir_angle));
  14953. }
  14954. // main arrow line / shaft
  14955. $this->Line($x0, $y0, $sx1, $sy1);
  14956. // left arrowhead arm tip
  14957. $x2L = $x1 + ($arm_size * cos($dir_angle + $arm_angle));
  14958. $y2L = $y1 + ($arm_size * sin($dir_angle + $arm_angle));
  14959. // right arrowhead arm tip
  14960. $x2R = $x1 + ($arm_size * cos($dir_angle - $arm_angle));
  14961. $y2R = $y1 + ($arm_size * sin($dir_angle - $arm_angle));
  14962. $mode = 'D';
  14963. $style = array();
  14964. switch ($head_style) {
  14965. case 0: {
  14966. // draw only arrowhead arms
  14967. $mode = 'D';
  14968. $style = array(1, 1, 0);
  14969. break;
  14970. }
  14971. case 1: {
  14972. // draw closed arrowhead, but no fill
  14973. $mode = 'D';
  14974. break;
  14975. }
  14976. case 2: {
  14977. // closed and filled arrowhead
  14978. $mode = 'DF';
  14979. break;
  14980. }
  14981. case 3: {
  14982. // filled arrowhead
  14983. $mode = 'F';
  14984. break;
  14985. }
  14986. }
  14987. $this->Polygon(array($x2L, $y2L, $x1, $y1, $x2R, $y2R), $mode, $style, array());
  14988. }
  14989. // END GRAPHIC FUNCTIONS SECTION -----------------------
  14990. // BIDIRECTIONAL TEXT SECTION --------------------------
  14991. /**
  14992. * Reverse the RLT substrings using the Bidirectional Algorithm (http://unicode.org/reports/tr9/).
  14993. * @param $str (string) string to manipulate.
  14994. * @param $setbom (bool) if true set the Byte Order Mark (BOM = 0xFEFF)
  14995. * @param $forcertl (bool) if true forces RTL text direction
  14996. * @return string
  14997. * @protected
  14998. * @author Nicola Asuni
  14999. * @since 2.1.000 (2008-01-08)
  15000. */
  15001. protected function utf8StrRev($str, $setbom=false, $forcertl=false) {
  15002. return $this->utf8StrArrRev($this->UTF8StringToArray($str), $str, $setbom, $forcertl);
  15003. }
  15004. /**
  15005. * Reverse the RLT substrings array using the Bidirectional Algorithm (http://unicode.org/reports/tr9/).
  15006. * @param $arr (array) array of unicode values.
  15007. * @param $str (string) string to manipulate (or empty value).
  15008. * @param $setbom (bool) if true set the Byte Order Mark (BOM = 0xFEFF)
  15009. * @param $forcertl (bool) if true forces RTL text direction
  15010. * @return string
  15011. * @protected
  15012. * @author Nicola Asuni
  15013. * @since 4.9.000 (2010-03-27)
  15014. */
  15015. protected function utf8StrArrRev($arr, $str='', $setbom=false, $forcertl=false) {
  15016. return $this->arrUTF8ToUTF16BE($this->utf8Bidi($arr, $str, $forcertl), $setbom);
  15017. }
  15018. /**
  15019. * Reverse the RLT substrings using the Bidirectional Algorithm (http://unicode.org/reports/tr9/).
  15020. * @param $ta (array) array of characters composing the string.
  15021. * @param $str (string) string to process
  15022. * @param $forcertl (bool) if 'R' forces RTL, if 'L' forces LTR
  15023. * @return array of unicode chars
  15024. * @author Nicola Asuni
  15025. * @protected
  15026. * @since 2.4.000 (2008-03-06)
  15027. */
  15028. protected function utf8Bidi($ta, $str='', $forcertl=false) {
  15029. // paragraph embedding level
  15030. $pel = 0;
  15031. // max level
  15032. $maxlevel = 0;
  15033. if ($this->empty_string($str)) {
  15034. // create string from array
  15035. $str = $this->UTF8ArrSubString($ta);
  15036. }
  15037. // check if string contains arabic text
  15038. if (preg_match($this->unicode->uni_RE_PATTERN_ARABIC, $str)) {
  15039. $arabic = true;
  15040. } else {
  15041. $arabic = false;
  15042. }
  15043. // check if string contains RTL text
  15044. if (!($forcertl OR $arabic OR preg_match($this->unicode->uni_RE_PATTERN_RTL, $str))) {
  15045. return $ta;
  15046. }
  15047. // get number of chars
  15048. $numchars = count($ta);
  15049. if ($forcertl == 'R') {
  15050. $pel = 1;
  15051. } elseif ($forcertl == 'L') {
  15052. $pel = 0;
  15053. } else {
  15054. // P2. In each paragraph, find the first character of type L, AL, or R.
  15055. // 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.
  15056. for ($i=0; $i < $numchars; ++$i) {
  15057. $type = $this->unicode->uni_type[$ta[$i]];
  15058. if ($type == 'L') {
  15059. $pel = 0;
  15060. break;
  15061. } elseif (($type == 'AL') OR ($type == 'R')) {
  15062. $pel = 1;
  15063. break;
  15064. }
  15065. }
  15066. }
  15067. // Current Embedding Level
  15068. $cel = $pel;
  15069. // directional override status
  15070. $dos = 'N';
  15071. $remember = array();
  15072. // start-of-level-run
  15073. $sor = $pel % 2 ? 'R' : 'L';
  15074. $eor = $sor;
  15075. // Array of characters data
  15076. $chardata = Array();
  15077. // 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.
  15078. // In the resolution of levels in rules I1 and I2, the maximum embedding level of 62 can be reached.
  15079. for ($i=0; $i < $numchars; ++$i) {
  15080. if ($ta[$i] == $this->unicode->uni_RLE) {
  15081. // X2. With each RLE, compute the least greater odd embedding level.
  15082. // 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.
  15083. // b. If the new level would not be valid, then this code is invalid. Do not change the current level or override status.
  15084. $next_level = $cel + ($cel % 2) + 1;
  15085. if ($next_level < 62) {
  15086. $remember[] = array('num' => $this->unicode->uni_RLE, 'cel' => $cel, 'dos' => $dos);
  15087. $cel = $next_level;
  15088. $dos = 'N';
  15089. $sor = $eor;
  15090. $eor = $cel % 2 ? 'R' : 'L';
  15091. }
  15092. } elseif ($ta[$i] == $this->unicode->uni_LRE) {
  15093. // X3. With each LRE, compute the least greater even embedding level.
  15094. // 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.
  15095. // b. If the new level would not be valid, then this code is invalid. Do not change the current level or override status.
  15096. $next_level = $cel + 2 - ($cel % 2);
  15097. if ( $next_level < 62 ) {
  15098. $remember[] = array('num' => $this->unicode->uni_LRE, 'cel' => $cel, 'dos' => $dos);
  15099. $cel = $next_level;
  15100. $dos = 'N';
  15101. $sor = $eor;
  15102. $eor = $cel % 2 ? 'R' : 'L';
  15103. }
  15104. } elseif ($ta[$i] == $this->unicode->uni_RLO) {
  15105. // X4. With each RLO, compute the least greater odd embedding level.
  15106. // 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.
  15107. // b. If the new level would not be valid, then this code is invalid. Do not change the current level or override status.
  15108. $next_level = $cel + ($cel % 2) + 1;
  15109. if ($next_level < 62) {
  15110. $remember[] = array('num' => $this->unicode->uni_RLO, 'cel' => $cel, 'dos' => $dos);
  15111. $cel = $next_level;
  15112. $dos = 'R';
  15113. $sor = $eor;
  15114. $eor = $cel % 2 ? 'R' : 'L';
  15115. }
  15116. } elseif ($ta[$i] == $this->unicode->uni_LRO) {
  15117. // X5. With each LRO, compute the least greater even embedding level.
  15118. // 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.
  15119. // b. If the new level would not be valid, then this code is invalid. Do not change the current level or override status.
  15120. $next_level = $cel + 2 - ($cel % 2);
  15121. if ( $next_level < 62 ) {
  15122. $remember[] = array('num' => $this->unicode->uni_LRO, 'cel' => $cel, 'dos' => $dos);
  15123. $cel = $next_level;
  15124. $dos = 'L';
  15125. $sor = $eor;
  15126. $eor = $cel % 2 ? 'R' : 'L';
  15127. }
  15128. } elseif ($ta[$i] == $this->unicode->uni_PDF) {
  15129. // 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.
  15130. if (count($remember)) {
  15131. $last = count($remember ) - 1;
  15132. if (($remember[$last]['num'] == $this->unicode->uni_RLE) OR
  15133. ($remember[$last]['num'] == $this->unicode->uni_LRE) OR
  15134. ($remember[$last]['num'] == $this->unicode->uni_RLO) OR
  15135. ($remember[$last]['num'] == $this->unicode->uni_LRO)) {
  15136. $match = array_pop($remember);
  15137. $cel = $match['cel'];
  15138. $dos = $match['dos'];
  15139. $sor = $eor;
  15140. $eor = ($cel > $match['cel'] ? $cel : $match['cel']) % 2 ? 'R' : 'L';
  15141. }
  15142. }
  15143. } elseif (($ta[$i] != $this->unicode->uni_RLE) AND
  15144. ($ta[$i] != $this->unicode->uni_LRE) AND
  15145. ($ta[$i] != $this->unicode->uni_RLO) AND
  15146. ($ta[$i] != $this->unicode->uni_LRO) AND
  15147. ($ta[$i] != $this->unicode->uni_PDF)) {
  15148. // X6. For all types besides RLE, LRE, RLO, LRO, and PDF:
  15149. // a. Set the level of the current character to the current embedding level.
  15150. // b. Whenever the directional override status is not neutral, reset the current character type to the directional override status.
  15151. if ($dos != 'N') {
  15152. $chardir = $dos;
  15153. } else {
  15154. if (isset($this->unicode->uni_type[$ta[$i]])) {
  15155. $chardir = $this->unicode->uni_type[$ta[$i]];
  15156. } else {
  15157. $chardir = 'L';
  15158. }
  15159. }
  15160. // stores string characters and other information
  15161. $chardata[] = array('char' => $ta[$i], 'level' => $cel, 'type' => $chardir, 'sor' => $sor, 'eor' => $eor);
  15162. }
  15163. } // end for each char
  15164. // X8. All explicit directional embeddings and overrides are completely terminated at the end of each paragraph. Paragraph separators are not included in the embedding.
  15165. // X9. Remove all RLE, LRE, RLO, LRO, PDF, and BN codes.
  15166. // 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.
  15167. // 3.3.3 Resolving Weak Types
  15168. // 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.
  15169. // Nonspacing marks are now resolved based on the previous characters.
  15170. $numchars = count($chardata);
  15171. // 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.
  15172. $prevlevel = -1; // track level changes
  15173. $levcount = 0; // counts consecutive chars at the same level
  15174. for ($i=0; $i < $numchars; ++$i) {
  15175. if ($chardata[$i]['type'] == 'NSM') {
  15176. if ($levcount) {
  15177. $chardata[$i]['type'] = $chardata[$i]['sor'];
  15178. } elseif ($i > 0) {
  15179. $chardata[$i]['type'] = $chardata[($i-1)]['type'];
  15180. }
  15181. }
  15182. if ($chardata[$i]['level'] != $prevlevel) {
  15183. $levcount = 0;
  15184. } else {
  15185. ++$levcount;
  15186. }
  15187. $prevlevel = $chardata[$i]['level'];
  15188. }
  15189. // 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.
  15190. $prevlevel = -1;
  15191. $levcount = 0;
  15192. for ($i=0; $i < $numchars; ++$i) {
  15193. if ($chardata[$i]['char'] == 'EN') {
  15194. for ($j=$levcount; $j >= 0; $j--) {
  15195. if ($chardata[$j]['type'] == 'AL') {
  15196. $chardata[$i]['type'] = 'AN';
  15197. } elseif (($chardata[$j]['type'] == 'L') OR ($chardata[$j]['type'] == 'R')) {
  15198. break;
  15199. }
  15200. }
  15201. }
  15202. if ($chardata[$i]['level'] != $prevlevel) {
  15203. $levcount = 0;
  15204. } else {
  15205. ++$levcount;
  15206. }
  15207. $prevlevel = $chardata[$i]['level'];
  15208. }
  15209. // W3. Change all ALs to R.
  15210. for ($i=0; $i < $numchars; ++$i) {
  15211. if ($chardata[$i]['type'] == 'AL') {
  15212. $chardata[$i]['type'] = 'R';
  15213. }
  15214. }
  15215. // 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.
  15216. $prevlevel = -1;
  15217. $levcount = 0;
  15218. for ($i=0; $i < $numchars; ++$i) {
  15219. if (($levcount > 0) AND (($i+1) < $numchars) AND ($chardata[($i+1)]['level'] == $prevlevel)) {
  15220. if (($chardata[$i]['type'] == 'ES') AND ($chardata[($i-1)]['type'] == 'EN') AND ($chardata[($i+1)]['type'] == 'EN')) {
  15221. $chardata[$i]['type'] = 'EN';
  15222. } elseif (($chardata[$i]['type'] == 'CS') AND ($chardata[($i-1)]['type'] == 'EN') AND ($chardata[($i+1)]['type'] == 'EN')) {
  15223. $chardata[$i]['type'] = 'EN';
  15224. } elseif (($chardata[$i]['type'] == 'CS') AND ($chardata[($i-1)]['type'] == 'AN') AND ($chardata[($i+1)]['type'] == 'AN')) {
  15225. $chardata[$i]['type'] = 'AN';
  15226. }
  15227. }
  15228. if ($chardata[$i]['level'] != $prevlevel) {
  15229. $levcount = 0;
  15230. } else {
  15231. ++$levcount;
  15232. }
  15233. $prevlevel = $chardata[$i]['level'];
  15234. }
  15235. // W5. A sequence of European terminators adjacent to European numbers changes to all European numbers.
  15236. $prevlevel = -1;
  15237. $levcount = 0;
  15238. for ($i=0; $i < $numchars; ++$i) {
  15239. if ($chardata[$i]['type'] == 'ET') {
  15240. if (($levcount > 0) AND ($chardata[($i-1)]['type'] == 'EN')) {
  15241. $chardata[$i]['type'] = 'EN';
  15242. } else {
  15243. $j = $i+1;
  15244. while (($j < $numchars) AND ($chardata[$j]['level'] == $prevlevel)) {
  15245. if ($chardata[$j]['type'] == 'EN') {
  15246. $chardata[$i]['type'] = 'EN';
  15247. break;
  15248. } elseif ($chardata[$j]['type'] != 'ET') {
  15249. break;
  15250. }
  15251. ++$j;
  15252. }
  15253. }
  15254. }
  15255. if ($chardata[$i]['level'] != $prevlevel) {
  15256. $levcount = 0;
  15257. } else {
  15258. ++$levcount;
  15259. }
  15260. $prevlevel = $chardata[$i]['level'];
  15261. }
  15262. // W6. Otherwise, separators and terminators change to Other Neutral.
  15263. $prevlevel = -1;
  15264. $levcount = 0;
  15265. for ($i=0; $i < $numchars; ++$i) {
  15266. if (($chardata[$i]['type'] == 'ET') OR ($chardata[$i]['type'] == 'ES') OR ($chardata[$i]['type'] == 'CS')) {
  15267. $chardata[$i]['type'] = 'ON';
  15268. }
  15269. if ($chardata[$i]['level'] != $prevlevel) {
  15270. $levcount = 0;
  15271. } else {
  15272. ++$levcount;
  15273. }
  15274. $prevlevel = $chardata[$i]['level'];
  15275. }
  15276. //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.
  15277. $prevlevel = -1;
  15278. $levcount = 0;
  15279. for ($i=0; $i < $numchars; ++$i) {
  15280. if ($chardata[$i]['char'] == 'EN') {
  15281. for ($j=$levcount; $j >= 0; $j--) {
  15282. if ($chardata[$j]['type'] == 'L') {
  15283. $chardata[$i]['type'] = 'L';
  15284. } elseif ($chardata[$j]['type'] == 'R') {
  15285. break;
  15286. }
  15287. }
  15288. }
  15289. if ($chardata[$i]['level'] != $prevlevel) {
  15290. $levcount = 0;
  15291. } else {
  15292. ++$levcount;
  15293. }
  15294. $prevlevel = $chardata[$i]['level'];
  15295. }
  15296. // 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.
  15297. $prevlevel = -1;
  15298. $levcount = 0;
  15299. for ($i=0; $i < $numchars; ++$i) {
  15300. if (($levcount > 0) AND (($i+1) < $numchars) AND ($chardata[($i+1)]['level'] == $prevlevel)) {
  15301. if (($chardata[$i]['type'] == 'N') AND ($chardata[($i-1)]['type'] == 'L') AND ($chardata[($i+1)]['type'] == 'L')) {
  15302. $chardata[$i]['type'] = 'L';
  15303. } elseif (($chardata[$i]['type'] == 'N') AND
  15304. (($chardata[($i-1)]['type'] == 'R') OR ($chardata[($i-1)]['type'] == 'EN') OR ($chardata[($i-1)]['type'] == 'AN')) AND
  15305. (($chardata[($i+1)]['type'] == 'R') OR ($chardata[($i+1)]['type'] == 'EN') OR ($chardata[($i+1)]['type'] == 'AN'))) {
  15306. $chardata[$i]['type'] = 'R';
  15307. } elseif ($chardata[$i]['type'] == 'N') {
  15308. // N2. Any remaining neutrals take the embedding direction
  15309. $chardata[$i]['type'] = $chardata[$i]['sor'];
  15310. }
  15311. } elseif (($levcount == 0) AND (($i+1) < $numchars) AND ($chardata[($i+1)]['level'] == $prevlevel)) {
  15312. // first char
  15313. if (($chardata[$i]['type'] == 'N') AND ($chardata[$i]['sor'] == 'L') AND ($chardata[($i+1)]['type'] == 'L')) {
  15314. $chardata[$i]['type'] = 'L';
  15315. } elseif (($chardata[$i]['type'] == 'N') AND
  15316. (($chardata[$i]['sor'] == 'R') OR ($chardata[$i]['sor'] == 'EN') OR ($chardata[$i]['sor'] == 'AN')) AND
  15317. (($chardata[($i+1)]['type'] == 'R') OR ($chardata[($i+1)]['type'] == 'EN') OR ($chardata[($i+1)]['type'] == 'AN'))) {
  15318. $chardata[$i]['type'] = 'R';
  15319. } elseif ($chardata[$i]['type'] == 'N') {
  15320. // N2. Any remaining neutrals take the embedding direction
  15321. $chardata[$i]['type'] = $chardata[$i]['sor'];
  15322. }
  15323. } elseif (($levcount > 0) AND ((($i+1) == $numchars) OR (($i+1) < $numchars) AND ($chardata[($i+1)]['level'] != $prevlevel))) {
  15324. //last char
  15325. if (($chardata[$i]['type'] == 'N') AND ($chardata[($i-1)]['type'] == 'L') AND ($chardata[$i]['eor'] == 'L')) {
  15326. $chardata[$i]['type'] = 'L';
  15327. } elseif (($chardata[$i]['type'] == 'N') AND
  15328. (($chardata[($i-1)]['type'] == 'R') OR ($chardata[($i-1)]['type'] == 'EN') OR ($chardata[($i-1)]['type'] == 'AN')) AND
  15329. (($chardata[$i]['eor'] == 'R') OR ($chardata[$i]['eor'] == 'EN') OR ($chardata[$i]['eor'] == 'AN'))) {
  15330. $chardata[$i]['type'] = 'R';
  15331. } elseif ($chardata[$i]['type'] == 'N') {
  15332. // N2. Any remaining neutrals take the embedding direction
  15333. $chardata[$i]['type'] = $chardata[$i]['sor'];
  15334. }
  15335. } elseif ($chardata[$i]['type'] == 'N') {
  15336. // N2. Any remaining neutrals take the embedding direction
  15337. $chardata[$i]['type'] = $chardata[$i]['sor'];
  15338. }
  15339. if ($chardata[$i]['level'] != $prevlevel) {
  15340. $levcount = 0;
  15341. } else {
  15342. ++$levcount;
  15343. }
  15344. $prevlevel = $chardata[$i]['level'];
  15345. }
  15346. // 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.
  15347. // I2. For all characters with an odd (right-to-left) embedding direction, those of type L, EN or AN go up one level.
  15348. for ($i=0; $i < $numchars; ++$i) {
  15349. $odd = $chardata[$i]['level'] % 2;
  15350. if ($odd) {
  15351. if (($chardata[$i]['type'] == 'L') OR ($chardata[$i]['type'] == 'AN') OR ($chardata[$i]['type'] == 'EN')) {
  15352. $chardata[$i]['level'] += 1;
  15353. }
  15354. } else {
  15355. if ($chardata[$i]['type'] == 'R') {
  15356. $chardata[$i]['level'] += 1;
  15357. } elseif (($chardata[$i]['type'] == 'AN') OR ($chardata[$i]['type'] == 'EN')) {
  15358. $chardata[$i]['level'] += 2;
  15359. }
  15360. }
  15361. $maxlevel = max($chardata[$i]['level'],$maxlevel);
  15362. }
  15363. // L1. On each line, reset the embedding level of the following characters to the paragraph embedding level:
  15364. // 1. Segment separators,
  15365. // 2. Paragraph separators,
  15366. // 3. Any sequence of whitespace characters preceding a segment separator or paragraph separator, and
  15367. // 4. Any sequence of white space characters at the end of the line.
  15368. for ($i=0; $i < $numchars; ++$i) {
  15369. if (($chardata[$i]['type'] == 'B') OR ($chardata[$i]['type'] == 'S')) {
  15370. $chardata[$i]['level'] = $pel;
  15371. } elseif ($chardata[$i]['type'] == 'WS') {
  15372. $j = $i+1;
  15373. while ($j < $numchars) {
  15374. if ((($chardata[$j]['type'] == 'B') OR ($chardata[$j]['type'] == 'S')) OR
  15375. (($j == ($numchars-1)) AND ($chardata[$j]['type'] == 'WS'))) {
  15376. $chardata[$i]['level'] = $pel;
  15377. break;
  15378. } elseif ($chardata[$j]['type'] != 'WS') {
  15379. break;
  15380. }
  15381. ++$j;
  15382. }
  15383. }
  15384. }
  15385. // Arabic Shaping
  15386. // 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.
  15387. if ($arabic) {
  15388. $endedletter = array(1569,1570,1571,1572,1573,1575,1577,1583,1584,1585,1586,1608,1688);
  15389. $alfletter = array(1570,1571,1573,1575);
  15390. $chardata2 = $chardata;
  15391. $laaletter = false;
  15392. $charAL = array();
  15393. $x = 0;
  15394. for ($i=0; $i < $numchars; ++$i) {
  15395. if (($this->unicode->uni_type[$chardata[$i]['char']] == 'AL') OR ($chardata[$i]['char'] == 32) OR ($chardata[$i]['char'] == 8204)) {
  15396. $charAL[$x] = $chardata[$i];
  15397. $charAL[$x]['i'] = $i;
  15398. $chardata[$i]['x'] = $x;
  15399. ++$x;
  15400. }
  15401. }
  15402. $numAL = $x;
  15403. for ($i=0; $i < $numchars; ++$i) {
  15404. $thischar = $chardata[$i];
  15405. if ($i > 0) {
  15406. $prevchar = $chardata[($i-1)];
  15407. } else {
  15408. $prevchar = false;
  15409. }
  15410. if (($i+1) < $numchars) {
  15411. $nextchar = $chardata[($i+1)];
  15412. } else {
  15413. $nextchar = false;
  15414. }
  15415. if ($this->unicode->uni_type[$thischar['char']] == 'AL') {
  15416. $x = $thischar['x'];
  15417. if ($x > 0) {
  15418. $prevchar = $charAL[($x-1)];
  15419. } else {
  15420. $prevchar = false;
  15421. }
  15422. if (($x+1) < $numAL) {
  15423. $nextchar = $charAL[($x+1)];
  15424. } else {
  15425. $nextchar = false;
  15426. }
  15427. // if laa letter
  15428. if (($prevchar !== false) AND ($prevchar['char'] == 1604) AND (in_array($thischar['char'], $alfletter))) {
  15429. $arabicarr = $this->unicode->uni_laa_array;
  15430. $laaletter = true;
  15431. if ($x > 1) {
  15432. $prevchar = $charAL[($x-2)];
  15433. } else {
  15434. $prevchar = false;
  15435. }
  15436. } else {
  15437. $arabicarr = $this->unicode->uni_arabicsubst;
  15438. $laaletter = false;
  15439. }
  15440. if (($prevchar !== false) AND ($nextchar !== false) AND
  15441. (($this->unicode->uni_type[$prevchar['char']] == 'AL') OR ($this->unicode->uni_type[$prevchar['char']] == 'NSM')) AND
  15442. (($this->unicode->uni_type[$nextchar['char']] == 'AL') OR ($this->unicode->uni_type[$nextchar['char']] == 'NSM')) AND
  15443. ($prevchar['type'] == $thischar['type']) AND
  15444. ($nextchar['type'] == $thischar['type']) AND
  15445. ($nextchar['char'] != 1567)) {
  15446. if (in_array($prevchar['char'], $endedletter)) {
  15447. if (isset($arabicarr[$thischar['char']][2])) {
  15448. // initial
  15449. $chardata2[$i]['char'] = $arabicarr[$thischar['char']][2];
  15450. }
  15451. } else {
  15452. if (isset($arabicarr[$thischar['char']][3])) {
  15453. // medial
  15454. $chardata2[$i]['char'] = $arabicarr[$thischar['char']][3];
  15455. }
  15456. }
  15457. } elseif (($nextchar !== false) AND
  15458. (($this->unicode->uni_type[$nextchar['char']] == 'AL') OR ($this->unicode->uni_type[$nextchar['char']] == 'NSM')) AND
  15459. ($nextchar['type'] == $thischar['type']) AND
  15460. ($nextchar['char'] != 1567)) {
  15461. if (isset($arabicarr[$chardata[$i]['char']][2])) {
  15462. // initial
  15463. $chardata2[$i]['char'] = $arabicarr[$thischar['char']][2];
  15464. }
  15465. } elseif ((($prevchar !== false) AND
  15466. (($this->unicode->uni_type[$prevchar['char']] == 'AL') OR ($this->unicode->uni_type[$prevchar['char']] == 'NSM')) AND
  15467. ($prevchar['type'] == $thischar['type'])) OR
  15468. (($nextchar !== false) AND ($nextchar['char'] == 1567))) {
  15469. // final
  15470. if (($i > 1) AND ($thischar['char'] == 1607) AND
  15471. ($chardata[$i-1]['char'] == 1604) AND
  15472. ($chardata[$i-2]['char'] == 1604)) {
  15473. //Allah Word
  15474. // mark characters to delete with false
  15475. $chardata2[$i-2]['char'] = false;
  15476. $chardata2[$i-1]['char'] = false;
  15477. $chardata2[$i]['char'] = 65010;
  15478. } else {
  15479. if (($prevchar !== false) AND in_array($prevchar['char'], $endedletter)) {
  15480. if (isset($arabicarr[$thischar['char']][0])) {
  15481. // isolated
  15482. $chardata2[$i]['char'] = $arabicarr[$thischar['char']][0];
  15483. }
  15484. } else {
  15485. if (isset($arabicarr[$thischar['char']][1])) {
  15486. // final
  15487. $chardata2[$i]['char'] = $arabicarr[$thischar['char']][1];
  15488. }
  15489. }
  15490. }
  15491. } elseif (isset($arabicarr[$thischar['char']][0])) {
  15492. // isolated
  15493. $chardata2[$i]['char'] = $arabicarr[$thischar['char']][0];
  15494. }
  15495. // if laa letter
  15496. if ($laaletter) {
  15497. // mark characters to delete with false
  15498. $chardata2[($charAL[($x-1)]['i'])]['char'] = false;
  15499. }
  15500. } // end if AL (Arabic Letter)
  15501. } // end for each char
  15502. /*
  15503. * Combining characters that can occur with Arabic Shadda (0651 HEX, 1617 DEC) are replaced.
  15504. * Putting the combining mark and shadda in the same glyph allows us to avoid the two marks overlapping each other in an illegible manner.
  15505. */
  15506. for ($i = 0; $i < ($numchars-1); ++$i) {
  15507. if (($chardata2[$i]['char'] == 1617) AND (isset($this->unicode->uni_diacritics[($chardata2[$i+1]['char'])]))) {
  15508. // check if the subtitution font is defined on current font
  15509. if (isset($this->CurrentFont['cw'][($this->unicode->uni_diacritics[($chardata2[$i+1]['char'])])])) {
  15510. $chardata2[$i]['char'] = false;
  15511. $chardata2[$i+1]['char'] = $this->unicode->uni_diacritics[($chardata2[$i+1]['char'])];
  15512. }
  15513. }
  15514. }
  15515. // remove marked characters
  15516. foreach ($chardata2 as $key => $value) {
  15517. if ($value['char'] === false) {
  15518. unset($chardata2[$key]);
  15519. }
  15520. }
  15521. $chardata = array_values($chardata2);
  15522. $numchars = count($chardata);
  15523. unset($chardata2);
  15524. unset($arabicarr);
  15525. unset($laaletter);
  15526. unset($charAL);
  15527. }
  15528. // 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.
  15529. for ($j=$maxlevel; $j > 0; $j--) {
  15530. $ordarray = Array();
  15531. $revarr = Array();
  15532. $onlevel = false;
  15533. for ($i=0; $i < $numchars; ++$i) {
  15534. if ($chardata[$i]['level'] >= $j) {
  15535. $onlevel = true;
  15536. if (isset($this->unicode->uni_mirror[$chardata[$i]['char']])) {
  15537. // 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.
  15538. $chardata[$i]['char'] = $this->unicode->uni_mirror[$chardata[$i]['char']];
  15539. }
  15540. $revarr[] = $chardata[$i];
  15541. } else {
  15542. if ($onlevel) {
  15543. $revarr = array_reverse($revarr);
  15544. $ordarray = array_merge($ordarray, $revarr);
  15545. $revarr = Array();
  15546. $onlevel = false;
  15547. }
  15548. $ordarray[] = $chardata[$i];
  15549. }
  15550. }
  15551. if ($onlevel) {
  15552. $revarr = array_reverse($revarr);
  15553. $ordarray = array_merge($ordarray, $revarr);
  15554. }
  15555. $chardata = $ordarray;
  15556. }
  15557. $ordarray = array();
  15558. for ($i=0; $i < $numchars; ++$i) {
  15559. $ordarray[] = $chardata[$i]['char'];
  15560. // store char values for subsetting
  15561. $this->CurrentFont['subsetchars'][$chardata[$i]['char']] = true;
  15562. }
  15563. // update font subsetchars
  15564. $this->setFontSubBuffer($this->CurrentFont['fontkey'], 'subsetchars', $this->CurrentFont['subsetchars']);
  15565. return $ordarray;
  15566. }
  15567. // END OF BIDIRECTIONAL TEXT SECTION -------------------
  15568. /**
  15569. * Encode a name object.
  15570. * @param $name (string) Name object to encode.
  15571. * @return (string) Encoded name object.
  15572. * @protected
  15573. * @author Nicola Asuni
  15574. * @since 5.9.097 (2011-06-23)
  15575. */
  15576. protected function encodeNameObject($name) {
  15577. $escname = '';
  15578. $length = strlen($name);
  15579. for ($i = 0; $i < $length; ++$i) {
  15580. $chr = $name[$i];
  15581. if (preg_match('/[0-9a-zA-Z]/', $chr) == 1) {
  15582. $escname .= $chr;
  15583. } else {
  15584. $escname .= sprintf('#%02X', ord($chr));
  15585. }
  15586. }
  15587. return $escname;
  15588. }
  15589. /**
  15590. * Add a Named Destination.
  15591. * NOTE: destination names are unique, so only last entry will be saved.
  15592. * @param $name (string) Destination name.
  15593. * @param $y (float) Y position in user units of the destiantion on the selected page (default = -1 = current position; 0 = page start;).
  15594. * @param $page (int) Target page number (leave empty for current page).
  15595. * @return (string) Stripped named destination identifier or false in case of error.
  15596. * @public
  15597. * @author Christian Deligant, Nicola Asuni
  15598. * @since 5.9.097 (2011-06-23)
  15599. */
  15600. public function setDestination($name, $y=-1, $page='') {
  15601. // remove unsupported characters
  15602. $name = $this->encodeNameObject($name);
  15603. if ($this->empty_string($name)) {
  15604. return false;
  15605. }
  15606. if ($y == -1) {
  15607. $y = $this->GetY();
  15608. }
  15609. if (empty($page)) {
  15610. $page = $this->PageNo();
  15611. if (empty($page)) {
  15612. return;
  15613. }
  15614. }
  15615. $this->dests[$name] = array('y' => $y, 'p' => $page);
  15616. return $name;
  15617. }
  15618. /**
  15619. * Return the Named Destination array.
  15620. * @return (array) Named Destination array.
  15621. * @public
  15622. * @author Nicola Asuni
  15623. * @since 5.9.097 (2011-06-23)
  15624. */
  15625. public function getDestination() {
  15626. return $this->dests;
  15627. }
  15628. /**
  15629. * Create a javascript PDF string.
  15630. * @protected
  15631. * @author Johannes Güntert, Nicola Asuni
  15632. * @since 5.9.098 (2011-06-23)
  15633. */
  15634. protected function _putdests() {
  15635. if (empty($this->dests)) {
  15636. return;
  15637. }
  15638. $this->n_dests = $this->_newobj();
  15639. $out = ' <<';
  15640. foreach($this->dests as $name => $o) {
  15641. $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)));
  15642. }
  15643. $out .= ' >>';
  15644. $out .= "\n".'endobj';
  15645. $this->_out($out);
  15646. }
  15647. /**
  15648. * Adds a bookmark - alias for Bookmark().
  15649. * @param $txt (string) Bookmark description.
  15650. * @param $level (int) Bookmark level (minimum value is 0).
  15651. * @param $y (float) Y position in user units of the bookmark on the selected page (default = -1 = current position; 0 = page start;).
  15652. * @param $page (int) Target page number (leave empty for current page).
  15653. * @param $style (string) Font style: B = Bold, I = Italic, BI = Bold + Italic.
  15654. * @param $color (array) RGB color array (values from 0 to 255).
  15655. * @public
  15656. */
  15657. public function setBookmark($txt, $level=0, $y=-1, $page='', $style='', $color=array(0,0,0)) {
  15658. $this->Bookmark($txt, $level, $y, $page, $style, $color);
  15659. }
  15660. /**
  15661. * Adds a bookmark.
  15662. * @param $txt (string) Bookmark description.
  15663. * @param $level (int) Bookmark level (minimum value is 0).
  15664. * @param $y (float) Y position in user units of the bookmark on the selected page (default = -1 = current position; 0 = page start;).
  15665. * @param $page (int) Target page number (leave empty for current page).
  15666. * @param $style (string) Font style: B = Bold, I = Italic, BI = Bold + Italic.
  15667. * @param $color (array) RGB color array (values from 0 to 255).
  15668. * @public
  15669. * @author Olivier Plathey, Nicola Asuni
  15670. * @since 2.1.002 (2008-02-12)
  15671. */
  15672. public function Bookmark($txt, $level=0, $y=-1, $page='', $style='', $color=array(0,0,0)) {
  15673. if ($level < 0) {
  15674. $level = 0;
  15675. }
  15676. if (isset($this->outlines[0])) {
  15677. $lastoutline = end($this->outlines);
  15678. $maxlevel = $lastoutline['l'] + 1;
  15679. } else {
  15680. $maxlevel = 0;
  15681. }
  15682. if ($level > $maxlevel) {
  15683. $level = $maxlevel;
  15684. }
  15685. if ($y == -1) {
  15686. $y = $this->GetY();
  15687. }
  15688. if (empty($page)) {
  15689. $page = $this->PageNo();
  15690. if (empty($page)) {
  15691. return;
  15692. }
  15693. }
  15694. $this->outlines[] = array('t' => $txt, 'l' => $level, 'y' => $y, 'p' => $page, 's' => strtoupper($style), 'c' => $color);
  15695. }
  15696. /**
  15697. * Sort bookmarks for page and key.
  15698. * @protected
  15699. * @since 5.9.119 (2011-09-19)
  15700. */
  15701. protected function sortBookmarks() {
  15702. // get sorting columns
  15703. $outline_p = array();
  15704. $outline_y = array();
  15705. foreach ($this->outlines as $key => $row) {
  15706. $outline_p[$key] = $row['p'];
  15707. $outline_k[$key] = $key;
  15708. }
  15709. // sort outlines by page and original position
  15710. array_multisort($outline_p, SORT_NUMERIC, SORT_ASC, $outline_k, SORT_NUMERIC, SORT_ASC, $this->outlines);
  15711. }
  15712. /**
  15713. * Create a bookmark PDF string.
  15714. * @protected
  15715. * @author Olivier Plathey, Nicola Asuni
  15716. * @since 2.1.002 (2008-02-12)
  15717. */
  15718. protected function _putbookmarks() {
  15719. $nb = count($this->outlines);
  15720. if ($nb == 0) {
  15721. return;
  15722. }
  15723. // sort bookmarks
  15724. $this->sortBookmarks();
  15725. $lru = array();
  15726. $level = 0;
  15727. foreach ($this->outlines as $i => $o) {
  15728. if ($o['l'] > 0) {
  15729. $parent = $lru[($o['l'] - 1)];
  15730. //Set parent and last pointers
  15731. $this->outlines[$i]['parent'] = $parent;
  15732. $this->outlines[$parent]['last'] = $i;
  15733. if ($o['l'] > $level) {
  15734. //Level increasing: set first pointer
  15735. $this->outlines[$parent]['first'] = $i;
  15736. }
  15737. } else {
  15738. $this->outlines[$i]['parent'] = $nb;
  15739. }
  15740. if (($o['l'] <= $level) AND ($i > 0)) {
  15741. //Set prev and next pointers
  15742. $prev = $lru[$o['l']];
  15743. $this->outlines[$prev]['next'] = $i;
  15744. $this->outlines[$i]['prev'] = $prev;
  15745. }
  15746. $lru[$o['l']] = $i;
  15747. $level = $o['l'];
  15748. }
  15749. //Outline items
  15750. $n = $this->n + 1;
  15751. $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';
  15752. foreach ($this->outlines as $i => $o) {
  15753. $oid = $this->_newobj();
  15754. // covert HTML title to string
  15755. $title = preg_replace($nltags, "\n", $o['t']);
  15756. $title = preg_replace("/[\r]+/si", '', $title);
  15757. $title = preg_replace("/[\n]+/si", "\n", $title);
  15758. $title = strip_tags($title);
  15759. $title = $this->stringTrim($title);
  15760. $out = '<</Title '.$this->_textstring($title, $oid);
  15761. $out .= ' /Parent '.($n + $o['parent']).' 0 R';
  15762. if (isset($o['prev'])) {
  15763. $out .= ' /Prev '.($n + $o['prev']).' 0 R';
  15764. }
  15765. if (isset($o['next'])) {
  15766. $out .= ' /Next '.($n + $o['next']).' 0 R';
  15767. }
  15768. if (isset($o['first'])) {
  15769. $out .= ' /First '.($n + $o['first']).' 0 R';
  15770. }
  15771. if (isset($o['last'])) {
  15772. $out .= ' /Last '.($n + $o['last']).' 0 R';
  15773. }
  15774. if (isset($this->page_obj_id[($o['p'])])) {
  15775. $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)));
  15776. }
  15777. // set font style
  15778. $style = 0;
  15779. if (!empty($o['s'])) {
  15780. // bold
  15781. if (strpos($o['s'], 'B') !== false) {
  15782. $style |= 2;
  15783. }
  15784. // oblique
  15785. if (strpos($o['s'], 'I') !== false) {
  15786. $style |= 1;
  15787. }
  15788. }
  15789. $out .= sprintf(' /F %d', $style);
  15790. // set bookmark color
  15791. if (isset($o['c']) AND is_array($o['c']) AND (count($o['c']) == 3)) {
  15792. $color = array_values($o['c']);
  15793. $out .= sprintf(' /C [%.3F %.3F %.3F]', ($color[0] / 255), ($color[1] / 255), ($color[2] / 255));
  15794. } else {
  15795. // black
  15796. $out .= ' /C [0.0 0.0 0.0]';
  15797. }
  15798. $out .= ' /Count 0'; // normally closed item
  15799. $out .= ' >>';
  15800. $out .= "\n".'endobj';
  15801. $this->_out($out);
  15802. }
  15803. //Outline root
  15804. $this->OutlineRoot = $this->_newobj();
  15805. $this->_out('<< /Type /Outlines /First '.$n.' 0 R /Last '.($n + $lru[0]).' 0 R >>'."\n".'endobj');
  15806. }
  15807. // --- JAVASCRIPT ------------------------------------------------------
  15808. /**
  15809. * Adds a javascript
  15810. * @param $script (string) Javascript code
  15811. * @public
  15812. * @author Johannes Güntert, Nicola Asuni
  15813. * @since 2.1.002 (2008-02-12)
  15814. */
  15815. public function IncludeJS($script) {
  15816. $this->javascript .= $script;
  15817. }
  15818. /**
  15819. * Adds a javascript object and return object ID
  15820. * @param $script (string) Javascript code
  15821. * @param $onload (boolean) if true executes this object when opening the document
  15822. * @return int internal object ID
  15823. * @public
  15824. * @author Nicola Asuni
  15825. * @since 4.8.000 (2009-09-07)
  15826. */
  15827. public function addJavascriptObject($script, $onload=false) {
  15828. if ($this->pdfa_mode) {
  15829. // javascript is not allowed in PDF/A mode
  15830. return false;
  15831. }
  15832. ++$this->n;
  15833. $this->js_objects[$this->n] = array('n' => $this->n, 'js' => $script, 'onload' => $onload);
  15834. return $this->n;
  15835. }
  15836. /**
  15837. * Create a javascript PDF string.
  15838. * @protected
  15839. * @author Johannes Güntert, Nicola Asuni
  15840. * @since 2.1.002 (2008-02-12)
  15841. */
  15842. protected function _putjavascript() {
  15843. if ($this->pdfa_mode OR (empty($this->javascript) AND empty($this->js_objects))) {
  15844. return;
  15845. }
  15846. if (strpos($this->javascript, 'this.addField') > 0) {
  15847. if (!$this->ur['enabled']) {
  15848. //$this->setUserRights();
  15849. }
  15850. // the following two lines are used to avoid form fields duplication after saving
  15851. // The addField method only works when releasing user rights (UR3)
  15852. $jsa = sprintf("ftcpdfdocsaved=this.addField('%s','%s',%d,[%.2F,%.2F,%.2F,%.2F]);", 'tcpdfdocsaved', 'text', 0, 0, 1, 0, 1);
  15853. $jsb = "getField('tcpdfdocsaved').value='saved';";
  15854. $this->javascript = $jsa."\n".$this->javascript."\n".$jsb;
  15855. }
  15856. $this->n_js = $this->_newobj();
  15857. $out = ' << /Names [';
  15858. if (!empty($this->javascript)) {
  15859. $out .= ' (EmbeddedJS) '.($this->n + 1).' 0 R';
  15860. }
  15861. if (!empty($this->js_objects)) {
  15862. foreach ($this->js_objects as $key => $val) {
  15863. if ($val['onload']) {
  15864. $out .= ' (JS'.$key.') '.$key.' 0 R';
  15865. }
  15866. }
  15867. }
  15868. $out .= ' ] >>';
  15869. $out .= "\n".'endobj';
  15870. $this->_out($out);
  15871. // default Javascript object
  15872. if (!empty($this->javascript)) {
  15873. $obj_id = $this->_newobj();
  15874. $out = '<< /S /JavaScript';
  15875. $out .= ' /JS '.$this->_textstring($this->javascript, $obj_id);
  15876. $out .= ' >>';
  15877. $out .= "\n".'endobj';
  15878. $this->_out($out);
  15879. }
  15880. // additional Javascript objects
  15881. if (!empty($this->js_objects)) {
  15882. foreach ($this->js_objects as $key => $val) {
  15883. $out = $this->_getobj($key)."\n".' << /S /JavaScript /JS '.$this->_textstring($val['js'], $key).' >>'."\n".'endobj';
  15884. $this->_out($out);
  15885. }
  15886. }
  15887. }
  15888. /**
  15889. * Convert color to javascript color.
  15890. * @param $color (string) color name or "#RRGGBB"
  15891. * @protected
  15892. * @author Denis Van Nuffelen, Nicola Asuni
  15893. * @since 2.1.002 (2008-02-12)
  15894. */
  15895. protected function _JScolor($color) {
  15896. static $aColors = array('transparent', 'black', 'white', 'red', 'green', 'blue', 'cyan', 'magenta', 'yellow', 'dkGray', 'gray', 'ltGray');
  15897. if (substr($color,0,1) == '#') {
  15898. return sprintf("['RGB',%.3F,%.3F,%.3F]", hexdec(substr($color,1,2))/255, hexdec(substr($color,3,2))/255, hexdec(substr($color,5,2))/255);
  15899. }
  15900. if (!in_array($color,$aColors)) {
  15901. $this->Error('Invalid color: '.$color);
  15902. }
  15903. return 'color.'.$color;
  15904. }
  15905. /**
  15906. * Adds a javascript form field.
  15907. * @param $type (string) field type
  15908. * @param $name (string) field name
  15909. * @param $x (int) horizontal position
  15910. * @param $y (int) vertical position
  15911. * @param $w (int) width
  15912. * @param $h (int) height
  15913. * @param $prop (array) javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  15914. * @protected
  15915. * @author Denis Van Nuffelen, Nicola Asuni
  15916. * @since 2.1.002 (2008-02-12)
  15917. */
  15918. protected function _addfield($type, $name, $x, $y, $w, $h, $prop) {
  15919. if ($this->rtl) {
  15920. $x = $x - $w;
  15921. }
  15922. // the followind avoid fields duplication after saving the document
  15923. $this->javascript .= "if (getField('tcpdfdocsaved').value != 'saved') {";
  15924. $k = $this->k;
  15925. $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";
  15926. $this->javascript .= 'f'.$name.'.textSize='.$this->FontSizePt.";\n";
  15927. while (list($key, $val) = each($prop)) {
  15928. if (strcmp(substr($key, -5), 'Color') == 0) {
  15929. $val = $this->_JScolor($val);
  15930. } else {
  15931. $val = "'".$val."'";
  15932. }
  15933. $this->javascript .= 'f'.$name.'.'.$key.'='.$val.";\n";
  15934. }
  15935. if ($this->rtl) {
  15936. $this->x -= $w;
  15937. } else {
  15938. $this->x += $w;
  15939. }
  15940. $this->javascript .= '}';
  15941. }
  15942. // --- FORM FIELDS -----------------------------------------------------
  15943. /**
  15944. * Convert JavaScript form fields properties array to Annotation Properties array.
  15945. * @param $prop (array) javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  15946. * @return array of annotation properties
  15947. * @protected
  15948. * @author Nicola Asuni
  15949. * @since 4.8.000 (2009-09-06)
  15950. */
  15951. protected function getAnnotOptFromJSProp($prop) {
  15952. if (isset($prop['aopt']) AND is_array($prop['aopt'])) {
  15953. // the annotation options area lready defined
  15954. return $prop['aopt'];
  15955. }
  15956. $opt = array(); // value to be returned
  15957. // alignment: Controls how the text is laid out within the text field.
  15958. if (isset($prop['alignment'])) {
  15959. switch ($prop['alignment']) {
  15960. case 'left': {
  15961. $opt['q'] = 0;
  15962. break;
  15963. }
  15964. case 'center': {
  15965. $opt['q'] = 1;
  15966. break;
  15967. }
  15968. case 'right': {
  15969. $opt['q'] = 2;
  15970. break;
  15971. }
  15972. default: {
  15973. $opt['q'] = ($this->rtl)?2:0;
  15974. break;
  15975. }
  15976. }
  15977. }
  15978. // lineWidth: Specifies the thickness of the border when stroking the perimeter of a field's rectangle.
  15979. if (isset($prop['lineWidth'])) {
  15980. $linewidth = intval($prop['lineWidth']);
  15981. } else {
  15982. $linewidth = 1;
  15983. }
  15984. // borderStyle: The border style for a field.
  15985. if (isset($prop['borderStyle'])) {
  15986. switch ($prop['borderStyle']) {
  15987. case 'border.d':
  15988. case 'dashed': {
  15989. $opt['border'] = array(0, 0, $linewidth, array(3, 2));
  15990. $opt['bs'] = array('w'=>$linewidth, 's'=>'D', 'd'=>array(3, 2));
  15991. break;
  15992. }
  15993. case 'border.b':
  15994. case 'beveled': {
  15995. $opt['border'] = array(0, 0, $linewidth);
  15996. $opt['bs'] = array('w'=>$linewidth, 's'=>'B');
  15997. break;
  15998. }
  15999. case 'border.i':
  16000. case 'inset': {
  16001. $opt['border'] = array(0, 0, $linewidth);
  16002. $opt['bs'] = array('w'=>$linewidth, 's'=>'I');
  16003. break;
  16004. }
  16005. case 'border.u':
  16006. case 'underline': {
  16007. $opt['border'] = array(0, 0, $linewidth);
  16008. $opt['bs'] = array('w'=>$linewidth, 's'=>'U');
  16009. break;
  16010. }
  16011. case 'border.s':
  16012. case 'solid': {
  16013. $opt['border'] = array(0, 0, $linewidth);
  16014. $opt['bs'] = array('w'=>$linewidth, 's'=>'S');
  16015. break;
  16016. }
  16017. default: {
  16018. break;
  16019. }
  16020. }
  16021. }
  16022. if (isset($prop['border']) AND is_array($prop['border'])) {
  16023. $opt['border'] = $prop['border'];
  16024. }
  16025. if (!isset($opt['mk'])) {
  16026. $opt['mk'] = array();
  16027. }
  16028. if (!isset($opt['mk']['if'])) {
  16029. $opt['mk']['if'] = array();
  16030. }
  16031. $opt['mk']['if']['a'] = array(0.5, 0.5);
  16032. // buttonAlignX: Controls how space is distributed from the left of the button face with respect to the icon.
  16033. if (isset($prop['buttonAlignX'])) {
  16034. $opt['mk']['if']['a'][0] = $prop['buttonAlignX'];
  16035. }
  16036. // buttonAlignY: Controls how unused space is distributed from the bottom of the button face with respect to the icon.
  16037. if (isset($prop['buttonAlignY'])) {
  16038. $opt['mk']['if']['a'][1] = $prop['buttonAlignY'];
  16039. }
  16040. // buttonFitBounds: If true, the extent to which the icon may be scaled is set to the bounds of the button field.
  16041. if (isset($prop['buttonFitBounds']) AND ($prop['buttonFitBounds'] == 'true')) {
  16042. $opt['mk']['if']['fb'] = true;
  16043. }
  16044. // buttonScaleHow: Controls how the icon is scaled (if necessary) to fit inside the button face.
  16045. if (isset($prop['buttonScaleHow'])) {
  16046. switch ($prop['buttonScaleHow']) {
  16047. case 'scaleHow.proportional': {
  16048. $opt['mk']['if']['s'] = 'P';
  16049. break;
  16050. }
  16051. case 'scaleHow.anamorphic': {
  16052. $opt['mk']['if']['s'] = 'A';
  16053. break;
  16054. }
  16055. }
  16056. }
  16057. // buttonScaleWhen: Controls when an icon is scaled to fit inside the button face.
  16058. if (isset($prop['buttonScaleWhen'])) {
  16059. switch ($prop['buttonScaleWhen']) {
  16060. case 'scaleWhen.always': {
  16061. $opt['mk']['if']['sw'] = 'A';
  16062. break;
  16063. }
  16064. case 'scaleWhen.never': {
  16065. $opt['mk']['if']['sw'] = 'N';
  16066. break;
  16067. }
  16068. case 'scaleWhen.tooBig': {
  16069. $opt['mk']['if']['sw'] = 'B';
  16070. break;
  16071. }
  16072. case 'scaleWhen.tooSmall': {
  16073. $opt['mk']['if']['sw'] = 'S';
  16074. break;
  16075. }
  16076. }
  16077. }
  16078. // buttonPosition: Controls how the text and the icon of the button are positioned with respect to each other within the button face.
  16079. if (isset($prop['buttonPosition'])) {
  16080. switch ($prop['buttonPosition']) {
  16081. case 0:
  16082. case 'position.textOnly': {
  16083. $opt['mk']['tp'] = 0;
  16084. break;
  16085. }
  16086. case 1:
  16087. case 'position.iconOnly': {
  16088. $opt['mk']['tp'] = 1;
  16089. break;
  16090. }
  16091. case 2:
  16092. case 'position.iconTextV': {
  16093. $opt['mk']['tp'] = 2;
  16094. break;
  16095. }
  16096. case 3:
  16097. case 'position.textIconV': {
  16098. $opt['mk']['tp'] = 3;
  16099. break;
  16100. }
  16101. case 4:
  16102. case 'position.iconTextH': {
  16103. $opt['mk']['tp'] = 4;
  16104. break;
  16105. }
  16106. case 5:
  16107. case 'position.textIconH': {
  16108. $opt['mk']['tp'] = 5;
  16109. break;
  16110. }
  16111. case 6:
  16112. case 'position.overlay': {
  16113. $opt['mk']['tp'] = 6;
  16114. break;
  16115. }
  16116. }
  16117. }
  16118. // fillColor: Specifies the background color for a field.
  16119. if (isset($prop['fillColor'])) {
  16120. if (is_array($prop['fillColor'])) {
  16121. $opt['mk']['bg'] = $prop['fillColor'];
  16122. } else {
  16123. $opt['mk']['bg'] = $this->convertHTMLColorToDec($prop['fillColor']);
  16124. }
  16125. }
  16126. // 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.
  16127. if (isset($prop['strokeColor'])) {
  16128. if (is_array($prop['strokeColor'])) {
  16129. $opt['mk']['bc'] = $prop['strokeColor'];
  16130. } else {
  16131. $opt['mk']['bc'] = $this->convertHTMLColorToDec($prop['strokeColor']);
  16132. }
  16133. }
  16134. // rotation: The rotation of a widget in counterclockwise increments.
  16135. if (isset($prop['rotation'])) {
  16136. $opt['mk']['r'] = $prop['rotation'];
  16137. }
  16138. // charLimit: Limits the number of characters that a user can type into a text field.
  16139. if (isset($prop['charLimit'])) {
  16140. $opt['maxlen'] = intval($prop['charLimit']);
  16141. }
  16142. if (!isset($ff)) {
  16143. $ff = 0; // default value
  16144. }
  16145. // readonly: The read-only characteristic of a field. If a field is read-only, the user can see the field but cannot change it.
  16146. if (isset($prop['readonly']) AND ($prop['readonly'] == 'true')) {
  16147. $ff += 1 << 0;
  16148. }
  16149. // required: Specifies whether a field requires a value.
  16150. if (isset($prop['required']) AND ($prop['required'] == 'true')) {
  16151. $ff += 1 << 1;
  16152. }
  16153. // multiline: Controls how text is wrapped within the field.
  16154. if (isset($prop['multiline']) AND ($prop['multiline'] == 'true')) {
  16155. $ff += 1 << 12;
  16156. }
  16157. // password: Specifies whether the field should display asterisks when data is entered in the field.
  16158. if (isset($prop['password']) AND ($prop['password'] == 'true')) {
  16159. $ff += 1 << 13;
  16160. }
  16161. // NoToggleToOff: If set, exactly one radio button shall be selected at all times; selecting the currently selected button has no effect.
  16162. if (isset($prop['NoToggleToOff']) AND ($prop['NoToggleToOff'] == 'true')) {
  16163. $ff += 1 << 14;
  16164. }
  16165. // Radio: If set, the field is a set of radio buttons.
  16166. if (isset($prop['Radio']) AND ($prop['Radio'] == 'true')) {
  16167. $ff += 1 << 15;
  16168. }
  16169. // Pushbutton: If set, the field is a pushbutton that does not retain a permanent value.
  16170. if (isset($prop['Pushbutton']) AND ($prop['Pushbutton'] == 'true')) {
  16171. $ff += 1 << 16;
  16172. }
  16173. // Combo: If set, the field is a combo box; if clear, the field is a list box.
  16174. if (isset($prop['Combo']) AND ($prop['Combo'] == 'true')) {
  16175. $ff += 1 << 17;
  16176. }
  16177. // editable: Controls whether a combo box is editable.
  16178. if (isset($prop['editable']) AND ($prop['editable'] == 'true')) {
  16179. $ff += 1 << 18;
  16180. }
  16181. // Sort: If set, the field's option items shall be sorted alphabetically.
  16182. if (isset($prop['Sort']) AND ($prop['Sort'] == 'true')) {
  16183. $ff += 1 << 19;
  16184. }
  16185. // fileSelect: If true, sets the file-select flag in the Options tab of the text field (Field is Used for File Selection).
  16186. if (isset($prop['fileSelect']) AND ($prop['fileSelect'] == 'true')) {
  16187. $ff += 1 << 20;
  16188. }
  16189. // multipleSelection: If true, indicates that a list box allows a multiple selection of items.
  16190. if (isset($prop['multipleSelection']) AND ($prop['multipleSelection'] == 'true')) {
  16191. $ff += 1 << 21;
  16192. }
  16193. // doNotSpellCheck: If true, spell checking is not performed on this editable text field.
  16194. if (isset($prop['doNotSpellCheck']) AND ($prop['doNotSpellCheck'] == 'true')) {
  16195. $ff += 1 << 22;
  16196. }
  16197. // doNotScroll: If true, the text field does not scroll and the user, therefore, is limited by the rectangular region designed for the field.
  16198. if (isset($prop['doNotScroll']) AND ($prop['doNotScroll'] == 'true')) {
  16199. $ff += 1 << 23;
  16200. }
  16201. // 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.
  16202. if (isset($prop['comb']) AND ($prop['comb'] == 'true')) {
  16203. $ff += 1 << 24;
  16204. }
  16205. // 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.
  16206. if (isset($prop['radiosInUnison']) AND ($prop['radiosInUnison'] == 'true')) {
  16207. $ff += 1 << 25;
  16208. }
  16209. // richText: If true, the field allows rich text formatting.
  16210. if (isset($prop['richText']) AND ($prop['richText'] == 'true')) {
  16211. $ff += 1 << 25;
  16212. }
  16213. // commitOnSelChange: Controls whether a field value is committed after a selection change.
  16214. if (isset($prop['commitOnSelChange']) AND ($prop['commitOnSelChange'] == 'true')) {
  16215. $ff += 1 << 26;
  16216. }
  16217. $opt['ff'] = $ff;
  16218. // defaultValue: The default value of a field - that is, the value that the field is set to when the form is reset.
  16219. if (isset($prop['defaultValue'])) {
  16220. $opt['dv'] = $prop['defaultValue'];
  16221. }
  16222. $f = 4; // default value for annotation flags
  16223. // readonly: The read-only characteristic of a field. If a field is read-only, the user can see the field but cannot change it.
  16224. if (isset($prop['readonly']) AND ($prop['readonly'] == 'true')) {
  16225. $f += 1 << 6;
  16226. }
  16227. // display: Controls whether the field is hidden or visible on screen and in print.
  16228. if (isset($prop['display'])) {
  16229. if ($prop['display'] == 'display.visible') {
  16230. //
  16231. } elseif ($prop['display'] == 'display.hidden') {
  16232. $f += 1 << 1;
  16233. } elseif ($prop['display'] == 'display.noPrint') {
  16234. $f -= 1 << 2;
  16235. } elseif ($prop['display'] == 'display.noView') {
  16236. $f += 1 << 5;
  16237. }
  16238. }
  16239. $opt['f'] = $f;
  16240. // currentValueIndices: Reads and writes single or multiple values of a list box or combo box.
  16241. if (isset($prop['currentValueIndices']) AND is_array($prop['currentValueIndices'])) {
  16242. $opt['i'] = $prop['currentValueIndices'];
  16243. }
  16244. // value: The value of the field data that the user has entered.
  16245. if (isset($prop['value'])) {
  16246. if (is_array($prop['value'])) {
  16247. $opt['opt'] = array();
  16248. foreach ($prop['value'] AS $key => $optval) {
  16249. // exportValues: An array of strings representing the export values for the field.
  16250. if (isset($prop['exportValues'][$key])) {
  16251. $opt['opt'][$key] = array($prop['exportValues'][$key], $prop['value'][$key]);
  16252. } else {
  16253. $opt['opt'][$key] = $prop['value'][$key];
  16254. }
  16255. }
  16256. } else {
  16257. $opt['v'] = $prop['value'];
  16258. }
  16259. }
  16260. // richValue: This property specifies the text contents and formatting of a rich text field.
  16261. if (isset($prop['richValue'])) {
  16262. $opt['rv'] = $prop['richValue'];
  16263. }
  16264. // submitName: If nonempty, used during form submission instead of name. Only applicable if submitting in HTML format (that is, URL-encoded).
  16265. if (isset($prop['submitName'])) {
  16266. $opt['tm'] = $prop['submitName'];
  16267. }
  16268. // name: Fully qualified field name.
  16269. if (isset($prop['name'])) {
  16270. $opt['t'] = $prop['name'];
  16271. }
  16272. // userName: The user name (short description string) of the field.
  16273. if (isset($prop['userName'])) {
  16274. $opt['tu'] = $prop['userName'];
  16275. }
  16276. // highlight: Defines how a button reacts when a user clicks it.
  16277. if (isset($prop['highlight'])) {
  16278. switch ($prop['highlight']) {
  16279. case 'none':
  16280. case 'highlight.n': {
  16281. $opt['h'] = 'N';
  16282. break;
  16283. }
  16284. case 'invert':
  16285. case 'highlight.i': {
  16286. $opt['h'] = 'i';
  16287. break;
  16288. }
  16289. case 'push':
  16290. case 'highlight.p': {
  16291. $opt['h'] = 'P';
  16292. break;
  16293. }
  16294. case 'outline':
  16295. case 'highlight.o': {
  16296. $opt['h'] = 'O';
  16297. break;
  16298. }
  16299. }
  16300. }
  16301. // Unsupported options:
  16302. // - calcOrderIndex: Changes the calculation order of fields in the document.
  16303. // - delay: Delays the redrawing of a field's appearance.
  16304. // - defaultStyle: This property defines the default style attributes for the form field.
  16305. // - style: Allows the user to set the glyph style of a check box or radio button.
  16306. // - textColor, textFont, textSize
  16307. return $opt;
  16308. }
  16309. /**
  16310. * Set default properties for form fields.
  16311. * @param $prop (array) javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  16312. * @public
  16313. * @author Nicola Asuni
  16314. * @since 4.8.000 (2009-09-06)
  16315. */
  16316. public function setFormDefaultProp($prop=array()) {
  16317. $this->default_form_prop = $prop;
  16318. }
  16319. /**
  16320. * Return the default properties for form fields.
  16321. * @return array $prop javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  16322. * @public
  16323. * @author Nicola Asuni
  16324. * @since 4.8.000 (2009-09-06)
  16325. */
  16326. public function getFormDefaultProp() {
  16327. return $this->default_form_prop;
  16328. }
  16329. /**
  16330. * Creates a text field
  16331. * @param $name (string) field name
  16332. * @param $w (float) Width of the rectangle
  16333. * @param $h (float) Height of the rectangle
  16334. * @param $prop (array) javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  16335. * @param $opt (array) annotation parameters. Possible values are described on official PDF32000_2008 reference.
  16336. * @param $x (float) Abscissa of the upper-left corner of the rectangle
  16337. * @param $y (float) Ordinate of the upper-left corner of the rectangle
  16338. * @param $js (boolean) if true put the field using JavaScript (requires Acrobat Writer to be rendered).
  16339. * @public
  16340. * @author Nicola Asuni
  16341. * @since 4.8.000 (2009-09-07)
  16342. */
  16343. public function TextField($name, $w, $h, $prop=array(), $opt=array(), $x='', $y='', $js=false) {
  16344. if ($x === '') {
  16345. $x = $this->x;
  16346. }
  16347. if ($y === '') {
  16348. $y = $this->y;
  16349. }
  16350. // check page for no-write regions and adapt page margins if necessary
  16351. list($x, $y) = $this->checkPageRegions($h, $x, $y);
  16352. if ($js) {
  16353. $this->_addfield('text', $name, $x, $y, $w, $h, $prop);
  16354. return;
  16355. }
  16356. // get default style
  16357. $prop = array_merge($this->getFormDefaultProp(), $prop);
  16358. // get annotation data
  16359. $popt = $this->getAnnotOptFromJSProp($prop);
  16360. // set default appearance stream
  16361. $this->annotation_fonts[$this->CurrentFont['fontkey']] = $this->CurrentFont['i'];
  16362. $fontstyle = sprintf('/F%d %.2F Tf %s', $this->CurrentFont['i'], $this->FontSizePt, $this->TextColor);
  16363. $popt['da'] = $fontstyle;
  16364. // build appearance stream
  16365. $popt['ap'] = array();
  16366. $popt['ap']['n'] = '/Tx BMC q '.$fontstyle.' ';
  16367. $text = '';
  16368. if (isset($prop['value']) AND !empty($prop['value'])) {
  16369. $text = $prop['value'];
  16370. } elseif (isset($opt['v']) AND !empty($opt['v'])) {
  16371. $text = $opt['v'];
  16372. }
  16373. $tmpid = $this->startTemplate($w, $h, false);
  16374. $this->MultiCell($w, $h, $text, 0, '', false, 0, 0, 0, true, 0, false, true, 0, 'T', false);
  16375. $this->endTemplate();
  16376. --$this->n;
  16377. $popt['ap']['n'] .= $this->xobjects[$tmpid]['outdata'];
  16378. unset($this->xobjects[$tmpid]);
  16379. $popt['ap']['n'] .= 'Q EMC';
  16380. // merge options
  16381. $opt = array_merge($popt, $opt);
  16382. // remove some conflicting options
  16383. unset($opt['bs']);
  16384. // set remaining annotation data
  16385. $opt['Subtype'] = 'Widget';
  16386. $opt['ft'] = 'Tx';
  16387. $opt['t'] = $name;
  16388. // Additional annotation's parameters (check _putannotsobj() method):
  16389. //$opt['f']
  16390. //$opt['as']
  16391. //$opt['bs']
  16392. //$opt['be']
  16393. //$opt['c']
  16394. //$opt['border']
  16395. //$opt['h']
  16396. //$opt['mk'];
  16397. //$opt['mk']['r']
  16398. //$opt['mk']['bc'];
  16399. //$opt['mk']['bg'];
  16400. unset($opt['mk']['ca']);
  16401. unset($opt['mk']['rc']);
  16402. unset($opt['mk']['ac']);
  16403. unset($opt['mk']['i']);
  16404. unset($opt['mk']['ri']);
  16405. unset($opt['mk']['ix']);
  16406. unset($opt['mk']['if']);
  16407. //$opt['mk']['if']['sw'];
  16408. //$opt['mk']['if']['s'];
  16409. //$opt['mk']['if']['a'];
  16410. //$opt['mk']['if']['fb'];
  16411. unset($opt['mk']['tp']);
  16412. //$opt['tu']
  16413. //$opt['tm']
  16414. //$opt['ff']
  16415. //$opt['v']
  16416. //$opt['dv']
  16417. //$opt['a']
  16418. //$opt['aa']
  16419. //$opt['q']
  16420. $this->Annotation($x, $y, $w, $h, $name, $opt, 0);
  16421. if ($this->rtl) {
  16422. $this->x -= $w;
  16423. } else {
  16424. $this->x += $w;
  16425. }
  16426. }
  16427. /**
  16428. * Creates a RadioButton field.
  16429. * @param $name (string) Field name.
  16430. * @param $w (int) Width or the radio button.
  16431. * @param $prop (array) Javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  16432. * @param $opt (array) Annotation parameters. Possible values are described on official PDF32000_2008 reference.
  16433. * @param $onvalue (string) Value to be returned if selected.
  16434. * @param $checked (boolean) Define the initial state.
  16435. * @param $x (float) Abscissa of the upper-left corner of the rectangle
  16436. * @param $y (float) Ordinate of the upper-left corner of the rectangle
  16437. * @param $js (boolean) If true put the field using JavaScript (requires Acrobat Writer to be rendered).
  16438. * @public
  16439. * @author Nicola Asuni
  16440. * @since 4.8.000 (2009-09-07)
  16441. */
  16442. public function RadioButton($name, $w, $prop=array(), $opt=array(), $onvalue='On', $checked=false, $x='', $y='', $js=false) {
  16443. if ($x === '') {
  16444. $x = $this->x;
  16445. }
  16446. if ($y === '') {
  16447. $y = $this->y;
  16448. }
  16449. // check page for no-write regions and adapt page margins if necessary
  16450. list($x, $y) = $this->checkPageRegions($w, $x, $y);
  16451. if ($js) {
  16452. $this->_addfield('radiobutton', $name, $x, $y, $w, $w, $prop);
  16453. return;
  16454. }
  16455. if ($this->empty_string($onvalue)) {
  16456. $onvalue = 'On';
  16457. }
  16458. if ($checked) {
  16459. $defval = $onvalue;
  16460. } else {
  16461. $defval = 'Off';
  16462. }
  16463. // set font
  16464. $font = 'zapfdingbats';
  16465. $this->AddFont($font);
  16466. $tmpfont = $this->getFontBuffer($font);
  16467. // set data for parent group
  16468. if (!isset($this->radiobutton_groups[$this->page])) {
  16469. $this->radiobutton_groups[$this->page] = array();
  16470. }
  16471. if (!isset($this->radiobutton_groups[$this->page][$name])) {
  16472. $this->radiobutton_groups[$this->page][$name] = array();
  16473. ++$this->n;
  16474. $this->radiobutton_groups[$this->page][$name]['n'] = $this->n;
  16475. $this->radio_groups[] = $this->n;
  16476. }
  16477. $kid = ($this->n + 1);
  16478. // save object ID to be added on Kids entry on parent object
  16479. $this->radiobutton_groups[$this->page][$name][] = array('kid' => $kid, 'def' => $defval);
  16480. // get default style
  16481. $prop = array_merge($this->getFormDefaultProp(), $prop);
  16482. $prop['NoToggleToOff'] = 'true';
  16483. $prop['Radio'] = 'true';
  16484. $prop['borderStyle'] = 'inset';
  16485. // get annotation data
  16486. $popt = $this->getAnnotOptFromJSProp($prop);
  16487. // set additional default options
  16488. $this->annotation_fonts[$tmpfont['fontkey']] = $tmpfont['i'];
  16489. $fontstyle = sprintf('/F%d %.2F Tf %s', $tmpfont['i'], $this->FontSizePt, $this->TextColor);
  16490. $popt['da'] = $fontstyle;
  16491. // build appearance stream
  16492. $popt['ap'] = array();
  16493. $popt['ap']['n'] = array();
  16494. $fy = (($w - ((($tmpfont['desc']['Ascent'] - $tmpfont['desc']['Descent']) * $this->FontSizePt / 1000) / $this->k)) * $this->k);
  16495. $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);
  16496. $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);
  16497. if (!isset($popt['mk'])) {
  16498. $popt['mk'] = array();
  16499. }
  16500. $popt['mk']['ca'] = '(l)';
  16501. // merge options
  16502. $opt = array_merge($popt, $opt);
  16503. // set remaining annotation data
  16504. $opt['Subtype'] = 'Widget';
  16505. $opt['ft'] = 'Btn';
  16506. if ($checked) {
  16507. $opt['v'] = array('/'.$onvalue);
  16508. $opt['as'] = $onvalue;
  16509. } else {
  16510. $opt['as'] = 'Off';
  16511. }
  16512. // store readonly flag
  16513. if (!isset($this->radiobutton_groups[$this->page][$name]['#readonly#'])) {
  16514. $this->radiobutton_groups[$this->page][$name]['#readonly#'] = false;
  16515. }
  16516. $this->radiobutton_groups[$this->page][$name]['#readonly#'] |= ($opt['f'] & 64);
  16517. $this->Annotation($x, $y, $w, $w, $name, $opt, 0);
  16518. if ($this->rtl) {
  16519. $this->x -= $w;
  16520. } else {
  16521. $this->x += $w;
  16522. }
  16523. }
  16524. /**
  16525. * Creates a List-box field
  16526. * @param $name (string) field name
  16527. * @param $w (int) width
  16528. * @param $h (int) height
  16529. * @param $values (array) array containing the list of values.
  16530. * @param $prop (array) javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  16531. * @param $opt (array) annotation parameters. Possible values are described on official PDF32000_2008 reference.
  16532. * @param $x (float) Abscissa of the upper-left corner of the rectangle
  16533. * @param $y (float) Ordinate of the upper-left corner of the rectangle
  16534. * @param $js (boolean) if true put the field using JavaScript (requires Acrobat Writer to be rendered).
  16535. * @public
  16536. * @author Nicola Asuni
  16537. * @since 4.8.000 (2009-09-07)
  16538. */
  16539. public function ListBox($name, $w, $h, $values, $prop=array(), $opt=array(), $x='', $y='', $js=false) {
  16540. if ($x === '') {
  16541. $x = $this->x;
  16542. }
  16543. if ($y === '') {
  16544. $y = $this->y;
  16545. }
  16546. // check page for no-write regions and adapt page margins if necessary
  16547. list($x, $y) = $this->checkPageRegions($h, $x, $y);
  16548. if ($js) {
  16549. $this->_addfield('listbox', $name, $x, $y, $w, $h, $prop);
  16550. $s = '';
  16551. foreach ($values as $value) {
  16552. $s .= '\''.addslashes($value).'\',';
  16553. }
  16554. $this->javascript .= 'f'.$name.'.setItems(['.substr($s, 0, -1)."]);\n";
  16555. return;
  16556. }
  16557. // get default style
  16558. $prop = array_merge($this->getFormDefaultProp(), $prop);
  16559. // get annotation data
  16560. $popt = $this->getAnnotOptFromJSProp($prop);
  16561. // set additional default values
  16562. $this->annotation_fonts[$this->CurrentFont['fontkey']] = $this->CurrentFont['i'];
  16563. $fontstyle = sprintf('/F%d %.2F Tf %s', $this->CurrentFont['i'], $this->FontSizePt, $this->TextColor);
  16564. $popt['da'] = $fontstyle;
  16565. // build appearance stream
  16566. $popt['ap'] = array();
  16567. $popt['ap']['n'] = '/Tx BMC q '.$fontstyle.' ';
  16568. $text = '';
  16569. foreach($values as $item) {
  16570. $text .= $item."\n";
  16571. }
  16572. $tmpid = $this->startTemplate($w, $h, false);
  16573. $this->MultiCell($w, $h, $text, 0, '', false, 0, 0, 0, true, 0, false, true, 0, 'T', false);
  16574. $this->endTemplate();
  16575. --$this->n;
  16576. $popt['ap']['n'] .= $this->xobjects[$tmpid]['outdata'];
  16577. unset($this->xobjects[$tmpid]);
  16578. $popt['ap']['n'] .= 'Q EMC';
  16579. // merge options
  16580. $opt = array_merge($popt, $opt);
  16581. // set remaining annotation data
  16582. $opt['Subtype'] = 'Widget';
  16583. $opt['ft'] = 'Ch';
  16584. $opt['t'] = $name;
  16585. $opt['opt'] = $values;
  16586. unset($opt['mk']['ca']);
  16587. unset($opt['mk']['rc']);
  16588. unset($opt['mk']['ac']);
  16589. unset($opt['mk']['i']);
  16590. unset($opt['mk']['ri']);
  16591. unset($opt['mk']['ix']);
  16592. unset($opt['mk']['if']);
  16593. unset($opt['mk']['tp']);
  16594. $this->Annotation($x, $y, $w, $h, $name, $opt, 0);
  16595. if ($this->rtl) {
  16596. $this->x -= $w;
  16597. } else {
  16598. $this->x += $w;
  16599. }
  16600. }
  16601. /**
  16602. * Creates a Combo-box field
  16603. * @param $name (string) field name
  16604. * @param $w (int) width
  16605. * @param $h (int) height
  16606. * @param $values (array) array containing the list of values.
  16607. * @param $prop (array) javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  16608. * @param $opt (array) annotation parameters. Possible values are described on official PDF32000_2008 reference.
  16609. * @param $x (float) Abscissa of the upper-left corner of the rectangle
  16610. * @param $y (float) Ordinate of the upper-left corner of the rectangle
  16611. * @param $js (boolean) if true put the field using JavaScript (requires Acrobat Writer to be rendered).
  16612. * @public
  16613. * @author Nicola Asuni
  16614. * @since 4.8.000 (2009-09-07)
  16615. */
  16616. public function ComboBox($name, $w, $h, $values, $prop=array(), $opt=array(), $x='', $y='', $js=false) {
  16617. if ($x === '') {
  16618. $x = $this->x;
  16619. }
  16620. if ($y === '') {
  16621. $y = $this->y;
  16622. }
  16623. // check page for no-write regions and adapt page margins if necessary
  16624. list($x, $y) = $this->checkPageRegions($h, $x, $y);
  16625. if ($js) {
  16626. $this->_addfield('combobox', $name, $x, $y, $w, $h, $prop);
  16627. $s = '';
  16628. foreach ($values as $value) {
  16629. $s .= "'".addslashes($value)."',";
  16630. }
  16631. $this->javascript .= 'f'.$name.'.setItems(['.substr($s, 0, -1)."]);\n";
  16632. return;
  16633. }
  16634. // get default style
  16635. $prop = array_merge($this->getFormDefaultProp(), $prop);
  16636. $prop['Combo'] = true;
  16637. // get annotation data
  16638. $popt = $this->getAnnotOptFromJSProp($prop);
  16639. // set additional default options
  16640. $this->annotation_fonts[$this->CurrentFont['fontkey']] = $this->CurrentFont['i'];
  16641. $fontstyle = sprintf('/F%d %.2F Tf %s', $this->CurrentFont['i'], $this->FontSizePt, $this->TextColor);
  16642. $popt['da'] = $fontstyle;
  16643. // build appearance stream
  16644. $popt['ap'] = array();
  16645. $popt['ap']['n'] = '/Tx BMC q '.$fontstyle.' ';
  16646. $text = '';
  16647. foreach($values as $item) {
  16648. $text .= $item[1]."\n";
  16649. }
  16650. $tmpid = $this->startTemplate($w, $h, false);
  16651. $this->MultiCell($w, $h, $text, 0, '', false, 0, 0, 0, true, 0, false, true, 0, 'T', false);
  16652. $this->endTemplate();
  16653. --$this->n;
  16654. $popt['ap']['n'] .= $this->xobjects[$tmpid]['outdata'];
  16655. unset($this->xobjects[$tmpid]);
  16656. $popt['ap']['n'] .= 'Q EMC';
  16657. // merge options
  16658. $opt = array_merge($popt, $opt);
  16659. // set remaining annotation data
  16660. $opt['Subtype'] = 'Widget';
  16661. $opt['ft'] = 'Ch';
  16662. $opt['t'] = $name;
  16663. $opt['opt'] = $values;
  16664. unset($opt['mk']['ca']);
  16665. unset($opt['mk']['rc']);
  16666. unset($opt['mk']['ac']);
  16667. unset($opt['mk']['i']);
  16668. unset($opt['mk']['ri']);
  16669. unset($opt['mk']['ix']);
  16670. unset($opt['mk']['if']);
  16671. unset($opt['mk']['tp']);
  16672. $this->Annotation($x, $y, $w, $h, $name, $opt, 0);
  16673. if ($this->rtl) {
  16674. $this->x -= $w;
  16675. } else {
  16676. $this->x += $w;
  16677. }
  16678. }
  16679. /**
  16680. * Creates a CheckBox field
  16681. * @param $name (string) field name
  16682. * @param $w (int) width
  16683. * @param $checked (boolean) define the initial state.
  16684. * @param $prop (array) javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  16685. * @param $opt (array) annotation parameters. Possible values are described on official PDF32000_2008 reference.
  16686. * @param $onvalue (string) value to be returned if selected.
  16687. * @param $x (float) Abscissa of the upper-left corner of the rectangle
  16688. * @param $y (float) Ordinate of the upper-left corner of the rectangle
  16689. * @param $js (boolean) if true put the field using JavaScript (requires Acrobat Writer to be rendered).
  16690. * @public
  16691. * @author Nicola Asuni
  16692. * @since 4.8.000 (2009-09-07)
  16693. */
  16694. public function CheckBox($name, $w, $checked=false, $prop=array(), $opt=array(), $onvalue='Yes', $x='', $y='', $js=false) {
  16695. if ($x === '') {
  16696. $x = $this->x;
  16697. }
  16698. if ($y === '') {
  16699. $y = $this->y;
  16700. }
  16701. // check page for no-write regions and adapt page margins if necessary
  16702. list($x, $y) = $this->checkPageRegions($w, $x, $y);
  16703. if ($js) {
  16704. $this->_addfield('checkbox', $name, $x, $y, $w, $w, $prop);
  16705. return;
  16706. }
  16707. if (!isset($prop['value'])) {
  16708. $prop['value'] = array('Yes');
  16709. }
  16710. // get default style
  16711. $prop = array_merge($this->getFormDefaultProp(), $prop);
  16712. $prop['borderStyle'] = 'inset';
  16713. // get annotation data
  16714. $popt = $this->getAnnotOptFromJSProp($prop);
  16715. // set additional default options
  16716. $font = 'zapfdingbats';
  16717. $this->AddFont($font);
  16718. $tmpfont = $this->getFontBuffer($font);
  16719. $this->annotation_fonts[$tmpfont['fontkey']] = $tmpfont['i'];
  16720. $fontstyle = sprintf('/F%d %.2F Tf %s', $tmpfont['i'], $this->FontSizePt, $this->TextColor);
  16721. $popt['da'] = $fontstyle;
  16722. // build appearance stream
  16723. $popt['ap'] = array();
  16724. $popt['ap']['n'] = array();
  16725. $fy = ((($tmpfont['desc']['Ascent'] + $tmpfont['desc']['Descent']) * $this->FontSizePt) / (1000 * $this->k));
  16726. $fy = (($w - ((($tmpfont['desc']['Ascent'] - $tmpfont['desc']['Descent']) * $this->FontSizePt / 1000) / $this->k)) * $this->k);
  16727. $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);
  16728. $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);
  16729. // merge options
  16730. $opt = array_merge($popt, $opt);
  16731. // set remaining annotation data
  16732. $opt['Subtype'] = 'Widget';
  16733. $opt['ft'] = 'Btn';
  16734. $opt['t'] = $name;
  16735. if ($this->empty_string($onvalue)) {
  16736. $onvalue = 'Yes';
  16737. }
  16738. $opt['opt'] = array($onvalue);
  16739. if ($checked) {
  16740. $opt['v'] = array('/Yes');
  16741. $opt['as'] = 'Yes';
  16742. } else {
  16743. $opt['v'] = array('/Off');
  16744. $opt['as'] = 'Off';
  16745. }
  16746. $this->Annotation($x, $y, $w, $w, $name, $opt, 0);
  16747. if ($this->rtl) {
  16748. $this->x -= $w;
  16749. } else {
  16750. $this->x += $w;
  16751. }
  16752. }
  16753. /**
  16754. * Creates a button field
  16755. * @param $name (string) field name
  16756. * @param $w (int) width
  16757. * @param $h (int) height
  16758. * @param $caption (string) caption.
  16759. * @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.
  16760. * @param $prop (array) javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  16761. * @param $opt (array) annotation parameters. Possible values are described on official PDF32000_2008 reference.
  16762. * @param $x (float) Abscissa of the upper-left corner of the rectangle
  16763. * @param $y (float) Ordinate of the upper-left corner of the rectangle
  16764. * @param $js (boolean) if true put the field using JavaScript (requires Acrobat Writer to be rendered).
  16765. * @public
  16766. * @author Nicola Asuni
  16767. * @since 4.8.000 (2009-09-07)
  16768. */
  16769. public function Button($name, $w, $h, $caption, $action, $prop=array(), $opt=array(), $x='', $y='', $js=false) {
  16770. if ($x === '') {
  16771. $x = $this->x;
  16772. }
  16773. if ($y === '') {
  16774. $y = $this->y;
  16775. }
  16776. // check page for no-write regions and adapt page margins if necessary
  16777. list($x, $y) = $this->checkPageRegions($h, $x, $y);
  16778. if ($js) {
  16779. $this->_addfield('button', $name, $this->x, $this->y, $w, $h, $prop);
  16780. $this->javascript .= 'f'.$name.".buttonSetCaption('".addslashes($caption)."');\n";
  16781. $this->javascript .= 'f'.$name.".setAction('MouseUp','".addslashes($action)."');\n";
  16782. $this->javascript .= 'f'.$name.".highlight='push';\n";
  16783. $this->javascript .= 'f'.$name.".print=false;\n";
  16784. return;
  16785. }
  16786. // get default style
  16787. $prop = array_merge($this->getFormDefaultProp(), $prop);
  16788. $prop['Pushbutton'] = 'true';
  16789. $prop['highlight'] = 'push';
  16790. $prop['display'] = 'display.noPrint';
  16791. // get annotation data
  16792. $popt = $this->getAnnotOptFromJSProp($prop);
  16793. $this->annotation_fonts[$this->CurrentFont['fontkey']] = $this->CurrentFont['i'];
  16794. $fontstyle = sprintf('/F%d %.2F Tf %s', $this->CurrentFont['i'], $this->FontSizePt, $this->TextColor);
  16795. $popt['da'] = $fontstyle;
  16796. // build appearance stream
  16797. $popt['ap'] = array();
  16798. $popt['ap']['n'] = '/Tx BMC q '.$fontstyle.' ';
  16799. $tmpid = $this->startTemplate($w, $h, false);
  16800. $bw = (2 / $this->k); // border width
  16801. $border = array(
  16802. 'L' => array('width' => $bw, 'cap' => 'square', 'join' => 'miter', 'dash' => 0, 'color' => array(231)),
  16803. 'R' => array('width' => $bw, 'cap' => 'square', 'join' => 'miter', 'dash' => 0, 'color' => array(51)),
  16804. 'T' => array('width' => $bw, 'cap' => 'square', 'join' => 'miter', 'dash' => 0, 'color' => array(231)),
  16805. 'B' => array('width' => $bw, 'cap' => 'square', 'join' => 'miter', 'dash' => 0, 'color' => array(51)));
  16806. $this->SetFillColor(204);
  16807. $this->Cell($w, $h, $caption, $border, 0, 'C', true, '', 1, false, 'T', 'M');
  16808. $this->endTemplate();
  16809. --$this->n;
  16810. $popt['ap']['n'] .= $this->xobjects[$tmpid]['outdata'];
  16811. unset($this->xobjects[$tmpid]);
  16812. $popt['ap']['n'] .= 'Q EMC';
  16813. // set additional default options
  16814. if (!isset($popt['mk'])) {
  16815. $popt['mk'] = array();
  16816. }
  16817. $ann_obj_id = ($this->n + 1);
  16818. if (!empty($action) AND !is_array($action)) {
  16819. $ann_obj_id = ($this->n + 2);
  16820. }
  16821. $popt['mk']['ca'] = $this->_textstring($caption, $ann_obj_id);
  16822. $popt['mk']['rc'] = $this->_textstring($caption, $ann_obj_id);
  16823. $popt['mk']['ac'] = $this->_textstring($caption, $ann_obj_id);
  16824. // merge options
  16825. $opt = array_merge($popt, $opt);
  16826. // set remaining annotation data
  16827. $opt['Subtype'] = 'Widget';
  16828. $opt['ft'] = 'Btn';
  16829. $opt['t'] = $caption;
  16830. $opt['v'] = $name;
  16831. if (!empty($action)) {
  16832. if (is_array($action)) {
  16833. // form action options as on section 12.7.5 of PDF32000_2008.
  16834. $opt['aa'] = '/D <<';
  16835. $bmode = array('SubmitForm', 'ResetForm', 'ImportData');
  16836. foreach ($action AS $key => $val) {
  16837. if (($key == 'S') AND in_array($val, $bmode)) {
  16838. $opt['aa'] .= ' /S /'.$val;
  16839. } elseif (($key == 'F') AND (!empty($val))) {
  16840. $opt['aa'] .= ' /F '.$this->_datastring($val, $ann_obj_id);
  16841. } elseif (($key == 'Fields') AND is_array($val) AND !empty($val)) {
  16842. $opt['aa'] .= ' /Fields [';
  16843. foreach ($val AS $field) {
  16844. $opt['aa'] .= ' '.$this->_textstring($field, $ann_obj_id);
  16845. }
  16846. $opt['aa'] .= ']';
  16847. } elseif (($key == 'Flags')) {
  16848. $ff = 0;
  16849. if (is_array($val)) {
  16850. foreach ($val AS $flag) {
  16851. switch ($flag) {
  16852. case 'Include/Exclude': {
  16853. $ff += 1 << 0;
  16854. break;
  16855. }
  16856. case 'IncludeNoValueFields': {
  16857. $ff += 1 << 1;
  16858. break;
  16859. }
  16860. case 'ExportFormat': {
  16861. $ff += 1 << 2;
  16862. break;
  16863. }
  16864. case 'GetMethod': {
  16865. $ff += 1 << 3;
  16866. break;
  16867. }
  16868. case 'SubmitCoordinates': {
  16869. $ff += 1 << 4;
  16870. break;
  16871. }
  16872. case 'XFDF': {
  16873. $ff += 1 << 5;
  16874. break;
  16875. }
  16876. case 'IncludeAppendSaves': {
  16877. $ff += 1 << 6;
  16878. break;
  16879. }
  16880. case 'IncludeAnnotations': {
  16881. $ff += 1 << 7;
  16882. break;
  16883. }
  16884. case 'SubmitPDF': {
  16885. $ff += 1 << 8;
  16886. break;
  16887. }
  16888. case 'CanonicalFormat': {
  16889. $ff += 1 << 9;
  16890. break;
  16891. }
  16892. case 'ExclNonUserAnnots': {
  16893. $ff += 1 << 10;
  16894. break;
  16895. }
  16896. case 'ExclFKey': {
  16897. $ff += 1 << 11;
  16898. break;
  16899. }
  16900. case 'EmbedForm': {
  16901. $ff += 1 << 13;
  16902. break;
  16903. }
  16904. }
  16905. }
  16906. } else {
  16907. $ff = intval($val);
  16908. }
  16909. $opt['aa'] .= ' /Flags '.$ff;
  16910. }
  16911. }
  16912. $opt['aa'] .= ' >>';
  16913. } else {
  16914. // Javascript action or raw action command
  16915. $js_obj_id = $this->addJavascriptObject($action);
  16916. $opt['aa'] = '/D '.$js_obj_id.' 0 R';
  16917. }
  16918. }
  16919. $this->Annotation($x, $y, $w, $h, $name, $opt, 0);
  16920. if ($this->rtl) {
  16921. $this->x -= $w;
  16922. } else {
  16923. $this->x += $w;
  16924. }
  16925. }
  16926. // --- END FORMS FIELDS ------------------------------------------------
  16927. /**
  16928. * Add certification signature (DocMDP or UR3)
  16929. * You can set only one signature type
  16930. * @protected
  16931. * @author Nicola Asuni
  16932. * @since 4.6.008 (2009-05-07)
  16933. */
  16934. protected function _putsignature() {
  16935. if ((!$this->sign) OR (!isset($this->signature_data['cert_type']))) {
  16936. return;
  16937. }
  16938. $sigobjid = ($this->sig_obj_id + 1);
  16939. $out = $this->_getobj($sigobjid)."\n";
  16940. $out .= '<< /Type /Sig';
  16941. $out .= ' /Filter /Adobe.PPKLite';
  16942. $out .= ' /SubFilter /adbe.pkcs7.detached';
  16943. $out .= ' '.$this->byterange_string;
  16944. $out .= ' /Contents<'.str_repeat('0', $this->signature_max_length).'>';
  16945. $out .= ' /Reference ['; // array of signature reference dictionaries
  16946. $out .= ' << /Type /SigRef';
  16947. if ($this->signature_data['cert_type'] > 0) {
  16948. $out .= ' /TransformMethod /DocMDP';
  16949. $out .= ' /TransformParams <<';
  16950. $out .= ' /Type /TransformParams';
  16951. $out .= ' /P '.$this->signature_data['cert_type'];
  16952. $out .= ' /V /1.2';
  16953. } else {
  16954. $out .= ' /TransformMethod /UR3';
  16955. $out .= ' /TransformParams <<';
  16956. $out .= ' /Type /TransformParams';
  16957. $out .= ' /V /2.2';
  16958. if (!$this->empty_string($this->ur['document'])) {
  16959. $out .= ' /Document['.$this->ur['document'].']';
  16960. }
  16961. if (!$this->empty_string($this->ur['form'])) {
  16962. $out .= ' /Form['.$this->ur['form'].']';
  16963. }
  16964. if (!$this->empty_string($this->ur['signature'])) {
  16965. $out .= ' /Signature['.$this->ur['signature'].']';
  16966. }
  16967. if (!$this->empty_string($this->ur['annots'])) {
  16968. $out .= ' /Annots['.$this->ur['annots'].']';
  16969. }
  16970. if (!$this->empty_string($this->ur['ef'])) {
  16971. $out .= ' /EF['.$this->ur['ef'].']';
  16972. }
  16973. if (!$this->empty_string($this->ur['formex'])) {
  16974. $out .= ' /FormEX['.$this->ur['formex'].']';
  16975. }
  16976. }
  16977. $out .= ' >>'; // close TransformParams
  16978. // optional digest data (values must be calculated and replaced later)
  16979. //$out .= ' /Data ********** 0 R';
  16980. //$out .= ' /DigestMethod/MD5';
  16981. //$out .= ' /DigestLocation[********** 34]';
  16982. //$out .= ' /DigestValue<********************************>';
  16983. $out .= ' >>';
  16984. $out .= ' ]'; // end of reference
  16985. if (isset($this->signature_data['info']['Name']) AND !$this->empty_string($this->signature_data['info']['Name'])) {
  16986. $out .= ' /Name '.$this->_textstring($this->signature_data['info']['Name'], $sigobjid);
  16987. }
  16988. if (isset($this->signature_data['info']['Location']) AND !$this->empty_string($this->signature_data['info']['Location'])) {
  16989. $out .= ' /Location '.$this->_textstring($this->signature_data['info']['Location'], $sigobjid);
  16990. }
  16991. if (isset($this->signature_data['info']['Reason']) AND !$this->empty_string($this->signature_data['info']['Reason'])) {
  16992. $out .= ' /Reason '.$this->_textstring($this->signature_data['info']['Reason'], $sigobjid);
  16993. }
  16994. if (isset($this->signature_data['info']['ContactInfo']) AND !$this->empty_string($this->signature_data['info']['ContactInfo'])) {
  16995. $out .= ' /ContactInfo '.$this->_textstring($this->signature_data['info']['ContactInfo'], $sigobjid);
  16996. }
  16997. $out .= ' /M '.$this->_datestring($sigobjid);
  16998. $out .= ' >>';
  16999. $out .= "\n".'endobj';
  17000. $this->_out($out);
  17001. }
  17002. /**
  17003. * Set User's Rights for PDF Reader
  17004. * WARNING: This is experimental and currently do not work.
  17005. * Check the PDF Reference 8.7.1 Transform Methods,
  17006. * Table 8.105 Entries in the UR transform parameters dictionary
  17007. * @param $enable (boolean) if true enable user's rights on PDF reader
  17008. * @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.
  17009. * @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.
  17010. * @param $form (string) Names specifying additional form-field-related usage rights for the document. Valid names are: /Add/Delete/FillIn/Import/Export/SubmitStandalone/SpawnTemplate
  17011. * @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.
  17012. * @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
  17013. Names specifying additional embedded-files-related usage rights for the document.
  17014. * @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.
  17015. * @public
  17016. * @author Nicola Asuni
  17017. * @since 2.9.000 (2008-03-26)
  17018. */
  17019. public function setUserRights(
  17020. $enable=true,
  17021. $document='/FullSave',
  17022. $annots='/Create/Delete/Modify/Copy/Import/Export',
  17023. $form='/Add/Delete/FillIn/Import/Export/SubmitStandalone/SpawnTemplate',
  17024. $signature='/Modify',
  17025. $ef='/Create/Delete/Modify/Import',
  17026. $formex='') {
  17027. $this->ur['enabled'] = $enable;
  17028. $this->ur['document'] = $document;
  17029. $this->ur['annots'] = $annots;
  17030. $this->ur['form'] = $form;
  17031. $this->ur['signature'] = $signature;
  17032. $this->ur['ef'] = $ef;
  17033. $this->ur['formex'] = $formex;
  17034. if (!$this->sign) {
  17035. $this->setSignature('', '', '', '', 0, array());
  17036. }
  17037. }
  17038. /**
  17039. * Enable document signature (requires the OpenSSL Library).
  17040. * The digital signature improve document authenticity and integrity and allows o enable extra features on Acrobat Reader.
  17041. * To create self-signed signature: openssl req -x509 -nodes -days 365000 -newkey rsa:1024 -keyout tcpdf.crt -out tcpdf.crt
  17042. * To export crt to p12: openssl pkcs12 -export -in tcpdf.crt -out tcpdf.p12
  17043. * To convert pfx certificate to pem: openssl pkcs12 -in tcpdf.pfx -out tcpdf.crt -nodes
  17044. * @param $signing_cert (mixed) signing certificate (string or filename prefixed with 'file://')
  17045. * @param $private_key (mixed) private key (string or filename prefixed with 'file://')
  17046. * @param $private_key_password (string) password
  17047. * @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.
  17048. * @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.
  17049. * @param $info (array) array of option information: Name, Location, Reason, ContactInfo.
  17050. * @public
  17051. * @author Nicola Asuni
  17052. * @since 4.6.005 (2009-04-24)
  17053. */
  17054. public function setSignature($signing_cert='', $private_key='', $private_key_password='', $extracerts='', $cert_type=2, $info=array()) {
  17055. // to create self-signed signature: openssl req -x509 -nodes -days 365000 -newkey rsa:1024 -keyout tcpdf.crt -out tcpdf.crt
  17056. // to export crt to p12: openssl pkcs12 -export -in tcpdf.crt -out tcpdf.p12
  17057. // to convert pfx certificate to pem: openssl
  17058. // OpenSSL> pkcs12 -in <cert.pfx> -out <cert.crt> -nodes
  17059. $this->sign = true;
  17060. ++$this->n;
  17061. $this->sig_obj_id = $this->n; // signature widget
  17062. ++$this->n; // signature object ($this->sig_obj_id + 1)
  17063. $this->signature_data = array();
  17064. if (strlen($signing_cert) == 0) {
  17065. $signing_cert = 'file://'.dirname(__FILE__).'/tcpdf.crt';
  17066. $private_key_password = 'tcpdfdemo';
  17067. }
  17068. if (strlen($private_key) == 0) {
  17069. $private_key = $signing_cert;
  17070. }
  17071. $this->signature_data['signcert'] = $signing_cert;
  17072. $this->signature_data['privkey'] = $private_key;
  17073. $this->signature_data['password'] = $private_key_password;
  17074. $this->signature_data['extracerts'] = $extracerts;
  17075. $this->signature_data['cert_type'] = $cert_type;
  17076. $this->signature_data['info'] = $info;
  17077. }
  17078. /**
  17079. * Set the digital signature appearance (a cliccable rectangle area to get signature properties)
  17080. * @param $x (float) Abscissa of the upper-left corner.
  17081. * @param $y (float) Ordinate of the upper-left corner.
  17082. * @param $w (float) Width of the signature area.
  17083. * @param $h (float) Height of the signature area.
  17084. * @param $page (int) option page number (if < 0 the current page is used).
  17085. * @public
  17086. * @author Nicola Asuni
  17087. * @since 5.3.011 (2010-06-17)
  17088. */
  17089. public function setSignatureAppearance($x=0, $y=0, $w=0, $h=0, $page=-1) {
  17090. $this->signature_appearance = $this->getSignatureAppearanceArray($x, $y, $w, $h, $page);
  17091. }
  17092. /**
  17093. * Add an empty digital signature appearance (a cliccable rectangle area to get signature properties)
  17094. * @param $x (float) Abscissa of the upper-left corner.
  17095. * @param $y (float) Ordinate of the upper-left corner.
  17096. * @param $w (float) Width of the signature area.
  17097. * @param $h (float) Height of the signature area.
  17098. * @param $page (int) option page number (if < 0 the current page is used).
  17099. * @public
  17100. * @author Nicola Asuni
  17101. * @since 5.9.101 (2011-07-06)
  17102. */
  17103. public function addEmptySignatureAppearance($x=0, $y=0, $w=0, $h=0, $page=-1) {
  17104. ++$this->n;
  17105. $this->empty_signature_appearance[] = array('objid' => $this->n) + $this->getSignatureAppearanceArray($x, $y, $w, $h, $page);
  17106. }
  17107. /**
  17108. * Get the array that defines the signature appearance (page and rectangle coordinates).
  17109. * @param $x (float) Abscissa of the upper-left corner.
  17110. * @param $y (float) Ordinate of the upper-left corner.
  17111. * @param $w (float) Width of the signature area.
  17112. * @param $h (float) Height of the signature area.
  17113. * @param $page (int) option page number (if < 0 the current page is used).
  17114. * @return (array) Array defining page and rectangle coordinates of signature appearance.
  17115. * @protected
  17116. * @author Nicola Asuni
  17117. * @since 5.9.101 (2011-07-06)
  17118. */
  17119. protected function getSignatureAppearanceArray($x=0, $y=0, $w=0, $h=0, $page=-1) {
  17120. $sigapp = array();
  17121. if (($page < 1) OR ($page > $this->numpages)) {
  17122. $sigapp['page'] = $this->page;
  17123. } else {
  17124. $sigapp['page'] = intval($page);
  17125. }
  17126. $a = $x * $this->k;
  17127. $b = $this->pagedim[($sigapp['page'])]['h'] - (($y + $h) * $this->k);
  17128. $c = $w * $this->k;
  17129. $d = $h * $this->k;
  17130. $sigapp['rect'] = sprintf('%.2F %.2F %.2F %.2F', $a, $b, ($a + $c), ($b + $d));
  17131. return $sigapp;
  17132. }
  17133. /**
  17134. * Create a new page group.
  17135. * NOTE: call this function before calling AddPage()
  17136. * @param $page (int) starting group page (leave empty for next page).
  17137. * @public
  17138. * @since 3.0.000 (2008-03-27)
  17139. */
  17140. public function startPageGroup($page='') {
  17141. if (empty($page)) {
  17142. $page = $this->page + 1;
  17143. }
  17144. $this->newpagegroup[$page] = sizeof($this->newpagegroup) + 1;
  17145. }
  17146. /**
  17147. * This method is DEPRECATED and doesn't have any effect.
  17148. * Please remove any reference to this method.
  17149. * @param $s (string) Empty parameter.
  17150. * @deprecated deprecated since version 5.9.089 (2011-06-13)
  17151. * @public
  17152. */
  17153. public function AliasNbPages($s='') {}
  17154. /**
  17155. * This method is DEPRECATED and doesn't have any effect.
  17156. * Please remove any reference to this method.
  17157. * @param $s (string) Empty parameter.
  17158. * @deprecated deprecated since version 5.9.089 (2011-06-13)
  17159. * @public
  17160. */
  17161. public function AliasNumPage($s='') {}
  17162. /**
  17163. * Set the starting page number.
  17164. * @param $num (int) Starting page number.
  17165. * @since 5.9.093 (2011-06-16)
  17166. * @public
  17167. */
  17168. public function setStartingPageNumber($num=1) {
  17169. $this->starting_page_number = max(0, intval($num));
  17170. }
  17171. /**
  17172. * Returns the string alias used right align page numbers.
  17173. * If the current font is unicode type, the returned string wil contain an additional open curly brace.
  17174. * @return string
  17175. * @since 5.9.099 (2011-06-27)
  17176. * @public
  17177. */
  17178. public function getAliasRightShift() {
  17179. // calculate aproximatively the ratio between widths of aliases and replacements.
  17180. $ref = '{'.$this->alias_right_shift.'}{'.$this->alias_tot_pages.'}{'.$this->alias_num_page.'}';
  17181. $rep = str_repeat(' ', $this->GetNumChars($ref));
  17182. $wdiff = max(1, ($this->GetStringWidth($ref) / $this->GetStringWidth($rep)));
  17183. $sdiff = sprintf('%.3F', $wdiff);
  17184. $alias = $this->alias_right_shift.$sdiff.'}';
  17185. if ($this->isUnicodeFont()) {
  17186. $alias = '{'.$alias;
  17187. }
  17188. return $alias;
  17189. }
  17190. /**
  17191. * Returns the string alias used for the total number of pages.
  17192. * If the current font is unicode type, the returned string is surrounded by additional curly braces.
  17193. * This alias will be replaced by the total number of pages in the document.
  17194. * @return string
  17195. * @since 4.0.018 (2008-08-08)
  17196. * @public
  17197. */
  17198. public function getAliasNbPages() {
  17199. if ($this->isUnicodeFont()) {
  17200. return '{'.$this->alias_tot_pages.'}';
  17201. }
  17202. return $this->alias_tot_pages;
  17203. }
  17204. /**
  17205. * Returns the string alias used for the page number.
  17206. * If the current font is unicode type, the returned string is surrounded by additional curly braces.
  17207. * This alias will be replaced by the page number.
  17208. * @return string
  17209. * @since 4.5.000 (2009-01-02)
  17210. * @public
  17211. */
  17212. public function getAliasNumPage() {
  17213. if ($this->isUnicodeFont()) {
  17214. return '{'.$this->alias_num_page.'}';
  17215. }
  17216. return $this->alias_num_page;
  17217. }
  17218. /**
  17219. * Return the alias for the total number of pages in the current page group.
  17220. * If the current font is unicode type, the returned string is surrounded by additional curly braces.
  17221. * This alias will be replaced by the total number of pages in this group.
  17222. * @return alias of the current page group
  17223. * @public
  17224. * @since 3.0.000 (2008-03-27)
  17225. */
  17226. public function getPageGroupAlias() {
  17227. if ($this->isUnicodeFont()) {
  17228. return '{'.$this->alias_group_tot_pages.'}';
  17229. }
  17230. return $this->alias_group_tot_pages;
  17231. }
  17232. /**
  17233. * Return the alias for the page number on the current page group.
  17234. * If the current font is unicode type, the returned string is surrounded by additional curly braces.
  17235. * This alias will be replaced by the page number (relative to the belonging group).
  17236. * @return alias of the current page group
  17237. * @public
  17238. * @since 4.5.000 (2009-01-02)
  17239. */
  17240. public function getPageNumGroupAlias() {
  17241. if ($this->isUnicodeFont()) {
  17242. return '{'.$this->alias_group_num_page.'}';
  17243. }
  17244. return $this->alias_group_num_page;
  17245. }
  17246. /**
  17247. * Return the current page in the group.
  17248. * @return current page in the group
  17249. * @public
  17250. * @since 3.0.000 (2008-03-27)
  17251. */
  17252. public function getGroupPageNo() {
  17253. return $this->pagegroups[$this->currpagegroup];
  17254. }
  17255. /**
  17256. * Returns the current group page number formatted as a string.
  17257. * @public
  17258. * @since 4.3.003 (2008-11-18)
  17259. * @see PaneNo(), formatPageNumber()
  17260. */
  17261. public function getGroupPageNoFormatted() {
  17262. return $this->formatPageNumber($this->getGroupPageNo());
  17263. }
  17264. /**
  17265. * Format the page numbers.
  17266. * This method can be overriden for custom formats.
  17267. * @param $num (int) page number
  17268. * @protected
  17269. * @since 4.2.005 (2008-11-06)
  17270. */
  17271. protected function formatPageNumber($num) {
  17272. return number_format((float)$num, 0, '', '.');
  17273. }
  17274. /**
  17275. * Format the page numbers on the Table Of Content.
  17276. * This method can be overriden for custom formats.
  17277. * @param $num (int) page number
  17278. * @protected
  17279. * @since 4.5.001 (2009-01-04)
  17280. * @see addTOC(), addHTMLTOC()
  17281. */
  17282. protected function formatTOCPageNumber($num) {
  17283. return number_format((float)$num, 0, '', '.');
  17284. }
  17285. /**
  17286. * Returns the current page number formatted as a string.
  17287. * @public
  17288. * @since 4.2.005 (2008-11-06)
  17289. * @see PaneNo(), formatPageNumber()
  17290. */
  17291. public function PageNoFormatted() {
  17292. return $this->formatPageNumber($this->PageNo());
  17293. }
  17294. /**
  17295. * Put pdf layers.
  17296. * @protected
  17297. * @since 3.0.000 (2008-03-27)
  17298. */
  17299. protected function _putocg() {
  17300. if (empty($this->pdflayers)) {
  17301. return;
  17302. }
  17303. foreach ($this->pdflayers as $key => $layer) {
  17304. $this->pdflayers[$key]['objid'] = $this->_newobj();
  17305. $out = '<< /Type /OCG';
  17306. $out .= ' /Name '.$this->_textstring($layer['name'], $this->pdflayers[$key]['objid']);
  17307. $out .= ' /Usage <<';
  17308. $out .= ' /Print <</PrintState /'.($layer['print']?'ON':'OFF').'>>';
  17309. $out .= ' /View <</ViewState /'.($layer['view']?'ON':'OFF').'>>';
  17310. $out .= ' >> >>';
  17311. $out .= "\n".'endobj';
  17312. $this->_out($out);
  17313. }
  17314. }
  17315. /**
  17316. * Start a new pdf layer.
  17317. * @param $name (string) Layer name (only a-z letters and numbers). Leave empty for automatic name.
  17318. * @param $print (boolean) Set to true to print this layer.
  17319. * @param $view (boolean) Set to true to view this layer.
  17320. * @public
  17321. * @since 5.9.102 (2011-07-13)
  17322. */
  17323. public function startLayer($name='', $print=true, $view=true) {
  17324. $layer = sprintf('LYR%03d', (count($this->pdflayers) + 1));
  17325. if (empty($name)) {
  17326. $name = $layer;
  17327. } else {
  17328. $name = preg_replace('/[^a-zA-Z0-9_\-]/', '', $name);
  17329. }
  17330. $this->pdflayers[] = array('layer' => $layer, 'name' => $name, 'print' => $print, 'view' => $view);
  17331. $this->openMarkedContent = true;
  17332. $this->_out('/OC /'.$layer.' BDC');
  17333. }
  17334. /**
  17335. * End the current PDF layer.
  17336. * @public
  17337. * @since 5.9.102 (2011-07-13)
  17338. */
  17339. public function endLayer() {
  17340. if ($this->openMarkedContent) {
  17341. // close existing open marked-content layer
  17342. $this->_out('EMC');
  17343. $this->openMarkedContent = false;
  17344. }
  17345. }
  17346. /**
  17347. * Set the visibility of the successive elements.
  17348. * This can be useful, for instance, to put a background
  17349. * image or color that will show on screen but won't print.
  17350. * @param $v (string) visibility mode. Legal values are: all, print, screen or view.
  17351. * @public
  17352. * @since 3.0.000 (2008-03-27)
  17353. */
  17354. public function setVisibility($v) {
  17355. $this->endLayer();
  17356. switch($v) {
  17357. case 'print': {
  17358. $this->startLayer('Print', true, false);
  17359. break;
  17360. }
  17361. case 'view':
  17362. case 'screen': {
  17363. $this->startLayer('View', false, true);
  17364. break;
  17365. }
  17366. case 'all': {
  17367. $this->_out('');
  17368. break;
  17369. }
  17370. default: {
  17371. $this->Error('Incorrect visibility: '.$v);
  17372. break;
  17373. }
  17374. }
  17375. }
  17376. /**
  17377. * Add transparency parameters to the current extgstate
  17378. * @param $parms (array) parameters
  17379. * @return the number of extgstates
  17380. * @protected
  17381. * @since 3.0.000 (2008-03-27)
  17382. */
  17383. protected function addExtGState($parms) {
  17384. if ($this->pdfa_mode) {
  17385. // transparencies are not allowed in PDF/A mode
  17386. return;
  17387. }
  17388. // check if this ExtGState already exist
  17389. foreach ($this->extgstates as $i => $ext) {
  17390. if ($ext['parms'] == $parms) {
  17391. if ($this->inxobj) {
  17392. // we are inside an XObject template
  17393. $this->xobjects[$this->xobjid]['extgstates'][$i] = $ext;
  17394. }
  17395. // return reference to existing ExtGState
  17396. return $i;
  17397. }
  17398. }
  17399. $n = (count($this->extgstates) + 1);
  17400. $this->extgstates[$n] = array('parms' => $parms);
  17401. if ($this->inxobj) {
  17402. // we are inside an XObject template
  17403. $this->xobjects[$this->xobjid]['extgstates'][$n] = $this->extgstates[$n];
  17404. }
  17405. return $n;
  17406. }
  17407. /**
  17408. * Add an extgstate
  17409. * @param $gs (array) extgstate
  17410. * @protected
  17411. * @since 3.0.000 (2008-03-27)
  17412. */
  17413. protected function setExtGState($gs) {
  17414. if ($this->pdfa_mode) {
  17415. // transparency is not allowed in PDF/A mode
  17416. return;
  17417. }
  17418. $this->_out(sprintf('/GS%d gs', $gs));
  17419. }
  17420. /**
  17421. * Put extgstates for object transparency
  17422. * @protected
  17423. * @since 3.0.000 (2008-03-27)
  17424. */
  17425. protected function _putextgstates() {
  17426. if ($this->pdfa_mode) {
  17427. // transparencies are not allowed in PDF/A mode
  17428. return;
  17429. }
  17430. foreach ($this->extgstates as $i => $ext) {
  17431. $this->extgstates[$i]['n'] = $this->_newobj();
  17432. $out = '<< /Type /ExtGState';
  17433. foreach ($ext['parms'] as $k => $v) {
  17434. if (is_float($v)) {
  17435. $v = sprintf('%.2F', $v);
  17436. }
  17437. $out .= ' /'.$k.' '.$v;
  17438. }
  17439. $out .= ' >>';
  17440. $out .= "\n".'endobj';
  17441. $this->_out($out);
  17442. }
  17443. }
  17444. /**
  17445. * Set alpha for stroking (CA) and non-stroking (ca) operations.
  17446. * @param $alpha (float) real value from 0 (transparent) to 1 (opaque)
  17447. * @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
  17448. * @public
  17449. * @since 3.0.000 (2008-03-27)
  17450. */
  17451. public function setAlpha($alpha, $bm='Normal') {
  17452. if ($this->pdfa_mode) {
  17453. // transparency is not allowed in PDF/A mode
  17454. return;
  17455. }
  17456. $gs = $this->addExtGState(array('ca' => $alpha, 'CA' => $alpha, 'BM' => '/'.$bm, 'AIS' => 'false'));
  17457. $this->setExtGState($gs);
  17458. }
  17459. /**
  17460. * Set the default JPEG compression quality (1-100)
  17461. * @param $quality (int) JPEG quality, integer between 1 and 100
  17462. * @public
  17463. * @since 3.0.000 (2008-03-27)
  17464. */
  17465. public function setJPEGQuality($quality) {
  17466. if (($quality < 1) OR ($quality > 100)) {
  17467. $quality = 75;
  17468. }
  17469. $this->jpeg_quality = intval($quality);
  17470. }
  17471. /**
  17472. * Set the default number of columns in a row for HTML tables.
  17473. * @param $cols (int) number of columns
  17474. * @public
  17475. * @since 3.0.014 (2008-06-04)
  17476. */
  17477. public function setDefaultTableColumns($cols=4) {
  17478. $this->default_table_columns = intval($cols);
  17479. }
  17480. /**
  17481. * Set the height of the cell (line height) respect the font height.
  17482. * @param $h (int) cell proportion respect font height (typical value = 1.25).
  17483. * @public
  17484. * @since 3.0.014 (2008-06-04)
  17485. */
  17486. public function setCellHeightRatio($h) {
  17487. $this->cell_height_ratio = $h;
  17488. }
  17489. /**
  17490. * return the height of cell repect font height.
  17491. * @public
  17492. * @since 4.0.012 (2008-07-24)
  17493. */
  17494. public function getCellHeightRatio() {
  17495. return $this->cell_height_ratio;
  17496. }
  17497. /**
  17498. * Set the PDF version (check PDF reference for valid values).
  17499. * @param $version (string) PDF document version.
  17500. * @public
  17501. * @since 3.1.000 (2008-06-09)
  17502. */
  17503. public function setPDFVersion($version='1.7') {
  17504. if ($this->pdfa_mode) {
  17505. // PDF/A mode
  17506. $this->PDFVersion = '1.4';
  17507. } else {
  17508. $this->PDFVersion = $version;
  17509. }
  17510. }
  17511. /**
  17512. * Set the viewer preferences dictionary controlling the way the document is to be presented on the screen or in print.
  17513. * (see Section 8.1 of PDF reference, "Viewer Preferences").
  17514. * <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>
  17515. * @param $preferences (array) array of options.
  17516. * @author Nicola Asuni
  17517. * @public
  17518. * @since 3.1.000 (2008-06-09)
  17519. */
  17520. public function setViewerPreferences($preferences) {
  17521. $this->viewer_preferences = $preferences;
  17522. }
  17523. /**
  17524. * Paints color transition registration bars
  17525. * @param $x (float) abscissa of the top left corner of the rectangle.
  17526. * @param $y (float) ordinate of the top left corner of the rectangle.
  17527. * @param $w (float) width of the rectangle.
  17528. * @param $h (float) height of the rectangle.
  17529. * @param $transition (boolean) if true prints tcolor transitions to white.
  17530. * @param $vertical (boolean) if true prints bar vertically.
  17531. * @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.
  17532. * @author Nicola Asuni
  17533. * @since 4.9.000 (2010-03-26)
  17534. * @public
  17535. */
  17536. public function colorRegistrationBar($x, $y, $w, $h, $transition=true, $vertical=false, $colors='A,R,G,B,C,M,Y,K') {
  17537. $bars = explode(',', $colors);
  17538. $numbars = count($bars); // number of bars to print
  17539. // set bar measures
  17540. if ($vertical) {
  17541. $coords = array(0, 0, 0, 1);
  17542. $wb = $w / $numbars; // bar width
  17543. $hb = $h; // bar height
  17544. $xd = $wb; // delta x
  17545. $yd = 0; // delta y
  17546. } else {
  17547. $coords = array(1, 0, 0, 0);
  17548. $wb = $w; // bar width
  17549. $hb = $h / $numbars; // bar height
  17550. $xd = 0; // delta x
  17551. $yd = $hb; // delta y
  17552. }
  17553. $xb = $x;
  17554. $yb = $y;
  17555. foreach ($bars as $col) {
  17556. switch ($col) {
  17557. // set transition colors
  17558. case 'A': { // BLACK
  17559. $col_a = array(255);
  17560. $col_b = array(0);
  17561. break;
  17562. }
  17563. case 'W': { // WHITE
  17564. $col_a = array(0);
  17565. $col_b = array(255);
  17566. break;
  17567. }
  17568. case 'R': { // R
  17569. $col_a = array(255,255,255);
  17570. $col_b = array(255,0,0);
  17571. break;
  17572. }
  17573. case 'G': { // G
  17574. $col_a = array(255,255,255);
  17575. $col_b = array(0,255,0);
  17576. break;
  17577. }
  17578. case 'B': { // B
  17579. $col_a = array(255,255,255);
  17580. $col_b = array(0,0,255);
  17581. break;
  17582. }
  17583. case 'C': { // C
  17584. $col_a = array(0,0,0,0);
  17585. $col_b = array(100,0,0,0);
  17586. break;
  17587. }
  17588. case 'M': { // M
  17589. $col_a = array(0,0,0,0);
  17590. $col_b = array(0,100,0,0);
  17591. break;
  17592. }
  17593. case 'Y': { // Y
  17594. $col_a = array(0,0,0,0);
  17595. $col_b = array(0,0,100,0);
  17596. break;
  17597. }
  17598. case 'K': { // K
  17599. $col_a = array(0,0,0,0);
  17600. $col_b = array(0,0,0,100);
  17601. break;
  17602. }
  17603. default: { // GRAY
  17604. $col_a = array(255);
  17605. $col_b = array(0);
  17606. break;
  17607. }
  17608. }
  17609. if ($transition) {
  17610. // color gradient
  17611. $this->LinearGradient($xb, $yb, $wb, $hb, $col_a, $col_b, $coords);
  17612. } else {
  17613. // color rectangle
  17614. $this->SetFillColorArray($col_b);
  17615. $this->Rect($xb, $yb, $wb, $hb, 'F', array());
  17616. }
  17617. $xb += $xd;
  17618. $yb += $yd;
  17619. }
  17620. }
  17621. /**
  17622. * Paints crop marks.
  17623. * @param $x (float) abscissa of the crop mark center.
  17624. * @param $y (float) ordinate of the crop mark center.
  17625. * @param $w (float) width of the crop mark.
  17626. * @param $h (float) height of the crop mark.
  17627. * @param $type (string) type of crop mark, one symbol per type separated by comma: T = TOP, F = BOTTOM, L = LEFT, R = RIGHT, TL = A = TOP-LEFT, TR = B = TOP-RIGHT, BL = C = BOTTOM-LEFT, BR = D = BOTTOM-RIGHT.
  17628. * @param $color (array) crop mark color (default black).
  17629. * @author Nicola Asuni
  17630. * @since 4.9.000 (2010-03-26)
  17631. * @public
  17632. */
  17633. public function cropMark($x, $y, $w, $h, $type='T,R,B,L', $color=array(0,0,0)) {
  17634. $this->SetLineStyle(array('width' => (0.5 / $this->k), 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => $color));
  17635. $type = strtoupper($type);
  17636. $type = preg_replace('/[^A-Z\-\,]*/', '', $type);
  17637. // split type in single components
  17638. $type = str_replace('-', ',', $type);
  17639. $type = str_replace('TL', 'T,L', $type);
  17640. $type = str_replace('TR', 'T,R', $type);
  17641. $type = str_replace('BL', 'F,L', $type);
  17642. $type = str_replace('BR', 'F,R', $type);
  17643. $type = str_replace('A', 'T,L', $type);
  17644. $type = str_replace('B', 'T,R', $type);
  17645. $type = str_replace('T,RO', 'BO', $type);
  17646. $type = str_replace('C', 'F,L', $type);
  17647. $type = str_replace('D', 'F,R', $type);
  17648. $crops = explode(',', strtoupper($type));
  17649. // remove duplicates
  17650. $crops = array_unique($crops);
  17651. $dw = ($w / 4); // horizontal space to leave before the intersection point
  17652. $dh = ($h / 4); // vertical space to leave before the intersection point
  17653. foreach ($crops as $crop) {
  17654. switch ($crop) {
  17655. case 'T':
  17656. case 'TOP': {
  17657. $x1 = $x;
  17658. $y1 = ($y - $h);
  17659. $x2 = $x;
  17660. $y2 = ($y - $dh);
  17661. break;
  17662. }
  17663. case 'F':
  17664. case 'BOTTOM': {
  17665. $x1 = $x;
  17666. $y1 = ($y + $dh);
  17667. $x2 = $x;
  17668. $y2 = ($y + $h);
  17669. break;
  17670. }
  17671. case 'L':
  17672. case 'LEFT': {
  17673. $x1 = ($x - $w);
  17674. $y1 = $y;
  17675. $x2 = ($x - $dw);
  17676. $y2 = $y;
  17677. break;
  17678. }
  17679. case 'R':
  17680. case 'RIGHT': {
  17681. $x1 = ($x + $dw);
  17682. $y1 = $y;
  17683. $x2 = ($x + $w);
  17684. $y2 = $y;
  17685. break;
  17686. }
  17687. }
  17688. $this->Line($x1, $y1, $x2, $y2);
  17689. }
  17690. }
  17691. /**
  17692. * Paints a registration mark
  17693. * @param $x (float) abscissa of the registration mark center.
  17694. * @param $y (float) ordinate of the registration mark center.
  17695. * @param $r (float) radius of the crop mark.
  17696. * @param $double (boolean) if true print two concentric crop marks.
  17697. * @param $cola (array) crop mark color (default black).
  17698. * @param $colb (array) second crop mark color.
  17699. * @author Nicola Asuni
  17700. * @since 4.9.000 (2010-03-26)
  17701. * @public
  17702. */
  17703. public function registrationMark($x, $y, $r, $double=false, $cola=array(0,0,0), $colb=array(255,255,255)) {
  17704. $line_style = array('width' => (0.5 / $this->k), 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => $cola);
  17705. $this->SetFillColorArray($cola);
  17706. $this->PieSector($x, $y, $r, 90, 180, 'F');
  17707. $this->PieSector($x, $y, $r, 270, 360, 'F');
  17708. $this->Circle($x, $y, $r, 0, 360, 'C', $line_style, array(), 8);
  17709. if ($double) {
  17710. $r2 = $r * 0.5;
  17711. $this->SetFillColorArray($colb);
  17712. $this->PieSector($x, $y, $r2, 90, 180, 'F');
  17713. $this->PieSector($x, $y, $r2, 270, 360, 'F');
  17714. $this->SetFillColorArray($cola);
  17715. $this->PieSector($x, $y, $r2, 0, 90, 'F');
  17716. $this->PieSector($x, $y, $r2, 180, 270, 'F');
  17717. $this->Circle($x, $y, $r2, 0, 360, 'C', $line_style, array(), 8);
  17718. }
  17719. }
  17720. /**
  17721. * Paints a linear colour gradient.
  17722. * @param $x (float) abscissa of the top left corner of the rectangle.
  17723. * @param $y (float) ordinate of the top left corner of the rectangle.
  17724. * @param $w (float) width of the rectangle.
  17725. * @param $h (float) height of the rectangle.
  17726. * @param $col1 (array) first color (Grayscale, RGB or CMYK components).
  17727. * @param $col2 (array) second color (Grayscale, RGB or CMYK components).
  17728. * @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).
  17729. * @author Andreas Würmser, Nicola Asuni
  17730. * @since 3.1.000 (2008-06-09)
  17731. * @public
  17732. */
  17733. public function LinearGradient($x, $y, $w, $h, $col1=array(), $col2=array(), $coords=array(0,0,1,0)) {
  17734. $this->Clip($x, $y, $w, $h);
  17735. $this->Gradient(2, $coords, array(array('color' => $col1, 'offset' => 0, 'exponent' => 1), array('color' => $col2, 'offset' => 1, 'exponent' => 1)), array(), false);
  17736. }
  17737. /**
  17738. * Paints a radial colour gradient.
  17739. * @param $x (float) abscissa of the top left corner of the rectangle.
  17740. * @param $y (float) ordinate of the top left corner of the rectangle.
  17741. * @param $w (float) width of the rectangle.
  17742. * @param $h (float) height of the rectangle.
  17743. * @param $col1 (array) first color (Grayscale, RGB or CMYK components).
  17744. * @param $col2 (array) second color (Grayscale, RGB or CMYK components).
  17745. * @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.
  17746. * @author Andreas Würmser, Nicola Asuni
  17747. * @since 3.1.000 (2008-06-09)
  17748. * @public
  17749. */
  17750. public function RadialGradient($x, $y, $w, $h, $col1=array(), $col2=array(), $coords=array(0.5,0.5,0.5,0.5,1)) {
  17751. $this->Clip($x, $y, $w, $h);
  17752. $this->Gradient(3, $coords, array(array('color' => $col1, 'offset' => 0, 'exponent' => 1), array('color' => $col2, 'offset' => 1, 'exponent' => 1)), array(), false);
  17753. }
  17754. /**
  17755. * Paints a coons patch mesh.
  17756. * @param $x (float) abscissa of the top left corner of the rectangle.
  17757. * @param $y (float) ordinate of the top left corner of the rectangle.
  17758. * @param $w (float) width of the rectangle.
  17759. * @param $h (float) height of the rectangle.
  17760. * @param $col1 (array) first color (lower left corner) (RGB components).
  17761. * @param $col2 (array) second color (lower right corner) (RGB components).
  17762. * @param $col3 (array) third color (upper right corner) (RGB components).
  17763. * @param $col4 (array) fourth color (upper left corner) (RGB components).
  17764. * @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>
  17765. * @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
  17766. * @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
  17767. * @param $antialias (boolean) A flag indicating whether to filter the shading function to prevent aliasing artifacts.
  17768. * @author Andreas Würmser, Nicola Asuni
  17769. * @since 3.1.000 (2008-06-09)
  17770. * @public
  17771. */
  17772. 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) {
  17773. if ($this->pdfa_mode) {
  17774. return;
  17775. }
  17776. $this->Clip($x, $y, $w, $h);
  17777. $n = count($this->gradients) + 1;
  17778. $this->gradients[$n] = array();
  17779. $this->gradients[$n]['type'] = 6; //coons patch mesh
  17780. $this->gradients[$n]['coords'] = array();
  17781. $this->gradients[$n]['antialias'] = $antialias;
  17782. $this->gradients[$n]['colors'] = array();
  17783. $this->gradients[$n]['transparency'] = false;
  17784. //check the coords array if it is the simple array or the multi patch array
  17785. if (!isset($coords[0]['f'])) {
  17786. //simple array -> convert to multi patch array
  17787. if (!isset($col1[1])) {
  17788. $col1[1] = $col1[2] = $col1[0];
  17789. }
  17790. if (!isset($col2[1])) {
  17791. $col2[1] = $col2[2] = $col2[0];
  17792. }
  17793. if (!isset($col3[1])) {
  17794. $col3[1] = $col3[2] = $col3[0];
  17795. }
  17796. if (!isset($col4[1])) {
  17797. $col4[1] = $col4[2] = $col4[0];
  17798. }
  17799. $patch_array[0]['f'] = 0;
  17800. $patch_array[0]['points'] = $coords;
  17801. $patch_array[0]['colors'][0]['r'] = $col1[0];
  17802. $patch_array[0]['colors'][0]['g'] = $col1[1];
  17803. $patch_array[0]['colors'][0]['b'] = $col1[2];
  17804. $patch_array[0]['colors'][1]['r'] = $col2[0];
  17805. $patch_array[0]['colors'][1]['g'] = $col2[1];
  17806. $patch_array[0]['colors'][1]['b'] = $col2[2];
  17807. $patch_array[0]['colors'][2]['r'] = $col3[0];
  17808. $patch_array[0]['colors'][2]['g'] = $col3[1];
  17809. $patch_array[0]['colors'][2]['b'] = $col3[2];
  17810. $patch_array[0]['colors'][3]['r'] = $col4[0];
  17811. $patch_array[0]['colors'][3]['g'] = $col4[1];
  17812. $patch_array[0]['colors'][3]['b'] = $col4[2];
  17813. } else {
  17814. //multi patch array
  17815. $patch_array = $coords;
  17816. }
  17817. $bpcd = 65535; //16 bits per coordinate
  17818. //build the data stream
  17819. $this->gradients[$n]['stream'] = '';
  17820. $count_patch = count($patch_array);
  17821. for ($i=0; $i < $count_patch; ++$i) {
  17822. $this->gradients[$n]['stream'] .= chr($patch_array[$i]['f']); //start with the edge flag as 8 bit
  17823. $count_points = count($patch_array[$i]['points']);
  17824. for ($j=0; $j < $count_points; ++$j) {
  17825. //each point as 16 bit
  17826. $patch_array[$i]['points'][$j] = (($patch_array[$i]['points'][$j] - $coords_min) / ($coords_max - $coords_min)) * $bpcd;
  17827. if ($patch_array[$i]['points'][$j] < 0) {
  17828. $patch_array[$i]['points'][$j] = 0;
  17829. }
  17830. if ($patch_array[$i]['points'][$j] > $bpcd) {
  17831. $patch_array[$i]['points'][$j] = $bpcd;
  17832. }
  17833. $this->gradients[$n]['stream'] .= chr(floor($patch_array[$i]['points'][$j] / 256));
  17834. $this->gradients[$n]['stream'] .= chr(floor($patch_array[$i]['points'][$j] % 256));
  17835. }
  17836. $count_cols = count($patch_array[$i]['colors']);
  17837. for ($j=0; $j < $count_cols; ++$j) {
  17838. //each color component as 8 bit
  17839. $this->gradients[$n]['stream'] .= chr($patch_array[$i]['colors'][$j]['r']);
  17840. $this->gradients[$n]['stream'] .= chr($patch_array[$i]['colors'][$j]['g']);
  17841. $this->gradients[$n]['stream'] .= chr($patch_array[$i]['colors'][$j]['b']);
  17842. }
  17843. }
  17844. //paint the gradient
  17845. $this->_out('/Sh'.$n.' sh');
  17846. //restore previous Graphic State
  17847. $this->_out('Q');
  17848. if ($this->inxobj) {
  17849. // we are inside an XObject template
  17850. $this->xobjects[$this->xobjid]['gradients'][$n] = $this->gradients[$n];
  17851. }
  17852. }
  17853. /**
  17854. * Set a rectangular clipping area.
  17855. * @param $x (float) abscissa of the top left corner of the rectangle (or top right corner for RTL mode).
  17856. * @param $y (float) ordinate of the top left corner of the rectangle.
  17857. * @param $w (float) width of the rectangle.
  17858. * @param $h (float) height of the rectangle.
  17859. * @author Andreas Würmser, Nicola Asuni
  17860. * @since 3.1.000 (2008-06-09)
  17861. * @protected
  17862. */
  17863. protected function Clip($x, $y, $w, $h) {
  17864. if ($this->rtl) {
  17865. $x = $this->w - $x - $w;
  17866. }
  17867. //save current Graphic State
  17868. $s = 'q';
  17869. //set clipping area
  17870. $s .= sprintf(' %.2F %.2F %.2F %.2F re W n', $x*$this->k, ($this->h-$y)*$this->k, $w*$this->k, -$h*$this->k);
  17871. //set up transformation matrix for gradient
  17872. $s .= sprintf(' %.3F 0 0 %.3F %.3F %.3F cm', $w*$this->k, $h*$this->k, $x*$this->k, ($this->h-($y+$h))*$this->k);
  17873. $this->_out($s);
  17874. }
  17875. /**
  17876. * Output gradient.
  17877. * @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)
  17878. * @param $coords (array) array of coordinates.
  17879. * @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).
  17880. * @param $background (array) An array of colour components appropriate to the colour space, specifying a single background colour value.
  17881. * @param $antialias (boolean) A flag indicating whether to filter the shading function to prevent aliasing artifacts.
  17882. * @author Nicola Asuni
  17883. * @since 3.1.000 (2008-06-09)
  17884. * @public
  17885. */
  17886. public function Gradient($type, $coords, $stops, $background=array(), $antialias=false) {
  17887. if ($this->pdfa_mode) {
  17888. return;
  17889. }
  17890. $n = count($this->gradients) + 1;
  17891. $this->gradients[$n] = array();
  17892. $this->gradients[$n]['type'] = $type;
  17893. $this->gradients[$n]['coords'] = $coords;
  17894. $this->gradients[$n]['antialias'] = $antialias;
  17895. $this->gradients[$n]['colors'] = array();
  17896. $this->gradients[$n]['transparency'] = false;
  17897. // color space
  17898. $numcolspace = count($stops[0]['color']);
  17899. $bcolor = array_values($background);
  17900. switch($numcolspace) {
  17901. case 4: { // CMYK
  17902. $this->gradients[$n]['colspace'] = 'DeviceCMYK';
  17903. if (!empty($background)) {
  17904. $this->gradients[$n]['background'] = sprintf('%.3F %.3F %.3F %.3F', $bcolor[0]/100, $bcolor[1]/100, $bcolor[2]/100, $bcolor[3]/100);
  17905. }
  17906. break;
  17907. }
  17908. case 3: { // RGB
  17909. $this->gradients[$n]['colspace'] = 'DeviceRGB';
  17910. if (!empty($background)) {
  17911. $this->gradients[$n]['background'] = sprintf('%.3F %.3F %.3F', $bcolor[0]/255, $bcolor[1]/255, $bcolor[2]/255);
  17912. }
  17913. break;
  17914. }
  17915. case 1: { // Gray scale
  17916. $this->gradients[$n]['colspace'] = 'DeviceGray';
  17917. if (!empty($background)) {
  17918. $this->gradients[$n]['background'] = sprintf('%.3F', $bcolor[0]/255);
  17919. }
  17920. break;
  17921. }
  17922. }
  17923. $num_stops = count($stops);
  17924. $last_stop_id = $num_stops - 1;
  17925. foreach ($stops as $key => $stop) {
  17926. $this->gradients[$n]['colors'][$key] = array();
  17927. // offset represents a location along the gradient vector
  17928. if (isset($stop['offset'])) {
  17929. $this->gradients[$n]['colors'][$key]['offset'] = $stop['offset'];
  17930. } else {
  17931. if ($key == 0) {
  17932. $this->gradients[$n]['colors'][$key]['offset'] = 0;
  17933. } elseif ($key == $last_stop_id) {
  17934. $this->gradients[$n]['colors'][$key]['offset'] = 1;
  17935. } else {
  17936. $offsetstep = (1 - $this->gradients[$n]['colors'][($key - 1)]['offset']) / ($num_stops - $key);
  17937. $this->gradients[$n]['colors'][$key]['offset'] = $this->gradients[$n]['colors'][($key - 1)]['offset'] + $offsetstep;
  17938. }
  17939. }
  17940. if (isset($stop['opacity'])) {
  17941. $this->gradients[$n]['colors'][$key]['opacity'] = $stop['opacity'];
  17942. if ((!$this->pdfa_mode) AND ($stop['opacity'] < 1)) {
  17943. $this->gradients[$n]['transparency'] = true;
  17944. }
  17945. } else {
  17946. $this->gradients[$n]['colors'][$key]['opacity'] = 1;
  17947. }
  17948. // exponent for the exponential interpolation function
  17949. if (isset($stop['exponent'])) {
  17950. $this->gradients[$n]['colors'][$key]['exponent'] = $stop['exponent'];
  17951. } else {
  17952. $this->gradients[$n]['colors'][$key]['exponent'] = 1;
  17953. }
  17954. // set colors
  17955. $color = array_values($stop['color']);
  17956. switch($numcolspace) {
  17957. case 4: { // CMYK
  17958. $this->gradients[$n]['colors'][$key]['color'] = sprintf('%.3F %.3F %.3F %.3F', $color[0]/100, $color[1]/100, $color[2]/100, $color[3]/100);
  17959. break;
  17960. }
  17961. case 3: { // RGB
  17962. $this->gradients[$n]['colors'][$key]['color'] = sprintf('%.3F %.3F %.3F', $color[0]/255, $color[1]/255, $color[2]/255);
  17963. break;
  17964. }
  17965. case 1: { // Gray scale
  17966. $this->gradients[$n]['colors'][$key]['color'] = sprintf('%.3F', $color[0]/255);
  17967. break;
  17968. }
  17969. }
  17970. }
  17971. if ($this->gradients[$n]['transparency']) {
  17972. // paint luminosity gradient
  17973. $this->_out('/TGS'.$n.' gs');
  17974. }
  17975. //paint the gradient
  17976. $this->_out('/Sh'.$n.' sh');
  17977. //restore previous Graphic State
  17978. $this->_out('Q');
  17979. if ($this->inxobj) {
  17980. // we are inside an XObject template
  17981. $this->xobjects[$this->xobjid]['gradients'][$n] = $this->gradients[$n];
  17982. }
  17983. }
  17984. /**
  17985. * Output gradient shaders.
  17986. * @author Nicola Asuni
  17987. * @since 3.1.000 (2008-06-09)
  17988. * @protected
  17989. */
  17990. function _putshaders() {
  17991. if ($this->pdfa_mode) {
  17992. return;
  17993. }
  17994. $idt = count($this->gradients); //index for transparency gradients
  17995. foreach ($this->gradients as $id => $grad) {
  17996. if (($grad['type'] == 2) OR ($grad['type'] == 3)) {
  17997. $fc = $this->_newobj();
  17998. $out = '<<';
  17999. $out .= ' /FunctionType 3';
  18000. $out .= ' /Domain [0 1]';
  18001. $functions = '';
  18002. $bounds = '';
  18003. $encode = '';
  18004. $i = 1;
  18005. $num_cols = count($grad['colors']);
  18006. $lastcols = $num_cols - 1;
  18007. for ($i = 1; $i < $num_cols; ++$i) {
  18008. $functions .= ($fc + $i).' 0 R ';
  18009. if ($i < $lastcols) {
  18010. $bounds .= sprintf('%.3F ', $grad['colors'][$i]['offset']);
  18011. }
  18012. $encode .= '0 1 ';
  18013. }
  18014. $out .= ' /Functions ['.trim($functions).']';
  18015. $out .= ' /Bounds ['.trim($bounds).']';
  18016. $out .= ' /Encode ['.trim($encode).']';
  18017. $out .= ' >>';
  18018. $out .= "\n".'endobj';
  18019. $this->_out($out);
  18020. for ($i = 1; $i < $num_cols; ++$i) {
  18021. $this->_newobj();
  18022. $out = '<<';
  18023. $out .= ' /FunctionType 2';
  18024. $out .= ' /Domain [0 1]';
  18025. $out .= ' /C0 ['.$grad['colors'][($i - 1)]['color'].']';
  18026. $out .= ' /C1 ['.$grad['colors'][$i]['color'].']';
  18027. $out .= ' /N '.$grad['colors'][$i]['exponent'];
  18028. $out .= ' >>';
  18029. $out .= "\n".'endobj';
  18030. $this->_out($out);
  18031. }
  18032. // set transparency fuctions
  18033. if ($grad['transparency']) {
  18034. $ft = $this->_newobj();
  18035. $out = '<<';
  18036. $out .= ' /FunctionType 3';
  18037. $out .= ' /Domain [0 1]';
  18038. $functions = '';
  18039. $i = 1;
  18040. $num_cols = count($grad['colors']);
  18041. for ($i = 1; $i < $num_cols; ++$i) {
  18042. $functions .= ($ft + $i).' 0 R ';
  18043. }
  18044. $out .= ' /Functions ['.trim($functions).']';
  18045. $out .= ' /Bounds ['.trim($bounds).']';
  18046. $out .= ' /Encode ['.trim($encode).']';
  18047. $out .= ' >>';
  18048. $out .= "\n".'endobj';
  18049. $this->_out($out);
  18050. for ($i = 1; $i < $num_cols; ++$i) {
  18051. $this->_newobj();
  18052. $out = '<<';
  18053. $out .= ' /FunctionType 2';
  18054. $out .= ' /Domain [0 1]';
  18055. $out .= ' /C0 ['.$grad['colors'][($i - 1)]['opacity'].']';
  18056. $out .= ' /C1 ['.$grad['colors'][$i]['opacity'].']';
  18057. $out .= ' /N '.$grad['colors'][$i]['exponent'];
  18058. $out .= ' >>';
  18059. $out .= "\n".'endobj';
  18060. $this->_out($out);
  18061. }
  18062. }
  18063. }
  18064. // set shading object
  18065. $this->_newobj();
  18066. $out = '<< /ShadingType '.$grad['type'];
  18067. if (isset($grad['colspace'])) {
  18068. $out .= ' /ColorSpace /'.$grad['colspace'];
  18069. } else {
  18070. $out .= ' /ColorSpace /DeviceRGB';
  18071. }
  18072. if (isset($grad['background']) AND !empty($grad['background'])) {
  18073. $out .= ' /Background ['.$grad['background'].']';
  18074. }
  18075. if (isset($grad['antialias']) AND ($grad['antialias'] === true)) {
  18076. $out .= ' /AntiAlias true';
  18077. }
  18078. if ($grad['type'] == 2) {
  18079. $out .= ' '.sprintf('/Coords [%.3F %.3F %.3F %.3F]', $grad['coords'][0], $grad['coords'][1], $grad['coords'][2], $grad['coords'][3]);
  18080. $out .= ' /Domain [0 1]';
  18081. $out .= ' /Function '.$fc.' 0 R';
  18082. $out .= ' /Extend [true true]';
  18083. $out .= ' >>';
  18084. } elseif ($grad['type'] == 3) {
  18085. //x0, y0, r0, x1, y1, r1
  18086. //at this this time radius of inner circle is 0
  18087. $out .= ' '.sprintf('/Coords [%.3F %.3F 0 %.3F %.3F %.3F]', $grad['coords'][0], $grad['coords'][1], $grad['coords'][2], $grad['coords'][3], $grad['coords'][4]);
  18088. $out .= ' /Domain [0 1]';
  18089. $out .= ' /Function '.$fc.' 0 R';
  18090. $out .= ' /Extend [true true]';
  18091. $out .= ' >>';
  18092. } elseif ($grad['type'] == 6) {
  18093. $out .= ' /BitsPerCoordinate 16';
  18094. $out .= ' /BitsPerComponent 8';
  18095. $out .= ' /Decode[0 1 0 1 0 1 0 1 0 1]';
  18096. $out .= ' /BitsPerFlag 8';
  18097. $stream = $this->_getrawstream($grad['stream']);
  18098. $out .= ' /Length '.strlen($stream);
  18099. $out .= ' >>';
  18100. $out .= ' stream'."\n".$stream."\n".'endstream';
  18101. }
  18102. $out .= "\n".'endobj';
  18103. $this->_out($out);
  18104. if ($grad['transparency']) {
  18105. $shading_transparency = preg_replace('/\/ColorSpace \/[^\s]+/si', '/ColorSpace /DeviceGray', $out);
  18106. $shading_transparency = preg_replace('/\/Function [0-9]+ /si', '/Function '.$ft.' ', $shading_transparency);
  18107. }
  18108. $this->gradients[$id]['id'] = $this->n;
  18109. // set pattern object
  18110. $this->_newobj();
  18111. $out = '<< /Type /Pattern /PatternType 2';
  18112. $out .= ' /Shading '.$this->gradients[$id]['id'].' 0 R';
  18113. $out .= ' >>';
  18114. $out .= "\n".'endobj';
  18115. $this->_out($out);
  18116. $this->gradients[$id]['pattern'] = $this->n;
  18117. // set shading and pattern for transparency mask
  18118. if ($grad['transparency']) {
  18119. // luminosity pattern
  18120. $idgs = $id + $idt;
  18121. $this->_newobj();
  18122. $this->_out($shading_transparency);
  18123. $this->gradients[$idgs]['id'] = $this->n;
  18124. $this->_newobj();
  18125. $out = '<< /Type /Pattern /PatternType 2';
  18126. $out .= ' /Shading '.$this->gradients[$idgs]['id'].' 0 R';
  18127. $out .= ' >>';
  18128. $out .= "\n".'endobj';
  18129. $this->_out($out);
  18130. $this->gradients[$idgs]['pattern'] = $this->n;
  18131. // luminosity XObject
  18132. $oid = $this->_newobj();
  18133. $this->xobjects['LX'.$oid] = array('n' => $oid);
  18134. $filter = '';
  18135. $stream = 'q /a0 gs /Pattern cs /p'.$idgs.' scn 0 0 '.$this->wPt.' '.$this->hPt.' re f Q';
  18136. if ($this->compress) {
  18137. $filter = ' /Filter /FlateDecode';
  18138. $stream = gzcompress($stream);
  18139. }
  18140. $stream = $this->_getrawstream($stream);
  18141. $out = '<< /Type /XObject /Subtype /Form /FormType 1'.$filter;
  18142. $out .= ' /Length '.strlen($stream);
  18143. $rect = sprintf('%.2F %.2F', $this->wPt, $this->hPt);
  18144. $out .= ' /BBox [0 0 '.$rect.']';
  18145. $out .= ' /Group << /Type /Group /S /Transparency /CS /DeviceGray >>';
  18146. $out .= ' /Resources <<';
  18147. $out .= ' /ExtGState << /a0 << /ca 1 /CA 1 >> >>';
  18148. $out .= ' /Pattern << /p'.$idgs.' '.$this->gradients[$idgs]['pattern'].' 0 R >>';
  18149. $out .= ' >>';
  18150. $out .= ' >> ';
  18151. $out .= ' stream'."\n".$stream."\n".'endstream';
  18152. $out .= "\n".'endobj';
  18153. $this->_out($out);
  18154. // SMask
  18155. $this->_newobj();
  18156. $out = '<< /Type /Mask /S /Luminosity /G '.($this->n - 1).' 0 R >>'."\n".'endobj';
  18157. $this->_out($out);
  18158. // ExtGState
  18159. $this->_newobj();
  18160. $out = '<< /Type /ExtGState /SMask '.($this->n - 1).' 0 R /AIS false >>'."\n".'endobj';
  18161. $this->_out($out);
  18162. $this->extgstates[] = array('n' => $this->n, 'name' => 'TGS'.$id);
  18163. }
  18164. }
  18165. }
  18166. /**
  18167. * Draw the sector of a circle.
  18168. * It can be used for instance to render pie charts.
  18169. * @param $xc (float) abscissa of the center.
  18170. * @param $yc (float) ordinate of the center.
  18171. * @param $r (float) radius.
  18172. * @param $a (float) start angle (in degrees).
  18173. * @param $b (float) end angle (in degrees).
  18174. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  18175. * @param $cw: (float) indicates whether to go clockwise (default: true).
  18176. * @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.
  18177. * @author Maxime Delorme, Nicola Asuni
  18178. * @since 3.1.000 (2008-06-09)
  18179. * @public
  18180. */
  18181. public function PieSector($xc, $yc, $r, $a, $b, $style='FD', $cw=true, $o=90) {
  18182. $this->PieSectorXY($xc, $yc, $r, $r, $a, $b, $style, $cw, $o);
  18183. }
  18184. /**
  18185. * Draw the sector of an ellipse.
  18186. * It can be used for instance to render pie charts.
  18187. * @param $xc (float) abscissa of the center.
  18188. * @param $yc (float) ordinate of the center.
  18189. * @param $rx (float) the x-axis radius.
  18190. * @param $ry (float) the y-axis radius.
  18191. * @param $a (float) start angle (in degrees).
  18192. * @param $b (float) end angle (in degrees).
  18193. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  18194. * @param $cw: (float) indicates whether to go clockwise.
  18195. * @param $o: (float) origin of angles (0 for 3 o'clock, 90 for noon, 180 for 9 o'clock, 270 for 6 o'clock).
  18196. * @param $nc (integer) Number of curves used to draw a 90 degrees portion of arc.
  18197. * @author Maxime Delorme, Nicola Asuni
  18198. * @since 3.1.000 (2008-06-09)
  18199. * @public
  18200. */
  18201. public function PieSectorXY($xc, $yc, $rx, $ry, $a, $b, $style='FD', $cw=false, $o=0, $nc=2) {
  18202. if ($this->rtl) {
  18203. $xc = $this->w - $xc;
  18204. }
  18205. $op = $this->getPathPaintOperator($style);
  18206. if ($op == 'f') {
  18207. $line_style = array();
  18208. }
  18209. if ($cw) {
  18210. $d = $b;
  18211. $b = 360 - $a + $o;
  18212. $a = 360 - $d + $o;
  18213. } else {
  18214. $b += $o;
  18215. $a += $o;
  18216. }
  18217. $this->_outellipticalarc($xc, $yc, $rx, $ry, 0, $a, $b, true, $nc);
  18218. $this->_out($op);
  18219. }
  18220. /**
  18221. * Embed vector-based Adobe Illustrator (AI) or AI-compatible EPS files.
  18222. * NOTE: EPS is not yet fully implemented, use the setRasterizeVectorImages() method to enable/disable rasterization of vector images using ImageMagick library.
  18223. * Only vector drawing is supported, not text or bitmap.
  18224. * 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).
  18225. * @param $file (string) Name of the file containing the image or a '@' character followed by the EPS/AI data string.
  18226. * @param $x (float) Abscissa of the upper-left corner.
  18227. * @param $y (float) Ordinate of the upper-left corner.
  18228. * @param $w (float) Width of the image in the page. If not specified or equal to zero, it is automatically calculated.
  18229. * @param $h (float) Height of the image in the page. If not specified or equal to zero, it is automatically calculated.
  18230. * @param $link (mixed) URL or identifier returned by AddLink().
  18231. * @param $useBoundingBox (boolean) specifies whether to position the bounding box (true) or the complete canvas (false) at location (x,y). Default value is true.
  18232. * @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>
  18233. * @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>
  18234. * @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)))
  18235. * @param $fitonpage (boolean) if true the image is resized to not exceed page dimensions.
  18236. * @param $fixoutvals (boolean) if true remove values outside the bounding box.
  18237. * @author Valentin Schmidt, Nicola Asuni
  18238. * @since 3.1.000 (2008-06-09)
  18239. * @public
  18240. */
  18241. public function ImageEps($file, $x='', $y='', $w=0, $h=0, $link='', $useBoundingBox=true, $align='', $palign='', $border=0, $fitonpage=false, $fixoutvals=false) {
  18242. if ($this->rasterize_vector_images AND ($w > 0) AND ($h > 0)) {
  18243. // convert EPS to raster image using GD or ImageMagick libraries
  18244. return $this->Image($file, $x, $y, $w, $h, 'EPS', $link, $align, true, 300, $palign, false, false, $border, false, false, $fitonpage);
  18245. }
  18246. if ($x === '') {
  18247. $x = $this->x;
  18248. }
  18249. if ($y === '') {
  18250. $y = $this->y;
  18251. }
  18252. // check page for no-write regions and adapt page margins if necessary
  18253. list($x, $y) = $this->checkPageRegions($h, $x, $y);
  18254. $k = $this->k;
  18255. if ($file{0} === '@') { // image from string
  18256. $data = substr($file, 1);
  18257. } else { // EPS/AI file
  18258. $data = file_get_contents($file);
  18259. }
  18260. if ($data === false) {
  18261. $this->Error('EPS file not found: '.$file);
  18262. }
  18263. $regs = array();
  18264. // EPS/AI compatibility check (only checks files created by Adobe Illustrator!)
  18265. preg_match("/%%Creator:([^\r\n]+)/", $data, $regs); # find Creator
  18266. if (count($regs) > 1) {
  18267. $version_str = trim($regs[1]); # e.g. "Adobe Illustrator(R) 8.0"
  18268. if (strpos($version_str, 'Adobe Illustrator') !== false) {
  18269. $versexp = explode(' ', $version_str);
  18270. $version = (float)array_pop($versexp);
  18271. if ($version >= 9) {
  18272. $this->Error('This version of Adobe Illustrator file is not supported: '.$file);
  18273. }
  18274. }
  18275. }
  18276. // strip binary bytes in front of PS-header
  18277. $start = strpos($data, '%!PS-Adobe');
  18278. if ($start > 0) {
  18279. $data = substr($data, $start);
  18280. }
  18281. // find BoundingBox params
  18282. preg_match("/%%BoundingBox:([^\r\n]+)/", $data, $regs);
  18283. if (count($regs) > 1) {
  18284. list($x1, $y1, $x2, $y2) = explode(' ', trim($regs[1]));
  18285. } else {
  18286. $this->Error('No BoundingBox found in EPS/AI file: '.$file);
  18287. }
  18288. $start = strpos($data, '%%EndSetup');
  18289. if ($start === false) {
  18290. $start = strpos($data, '%%EndProlog');
  18291. }
  18292. if ($start === false) {
  18293. $start = strpos($data, '%%BoundingBox');
  18294. }
  18295. $data = substr($data, $start);
  18296. $end = strpos($data, '%%PageTrailer');
  18297. if ($end===false) {
  18298. $end = strpos($data, 'showpage');
  18299. }
  18300. if ($end) {
  18301. $data = substr($data, 0, $end);
  18302. }
  18303. // calculate image width and height on document
  18304. if (($w <= 0) AND ($h <= 0)) {
  18305. $w = ($x2 - $x1) / $k;
  18306. $h = ($y2 - $y1) / $k;
  18307. } elseif ($w <= 0) {
  18308. $w = ($x2-$x1) / $k * ($h / (($y2 - $y1) / $k));
  18309. } elseif ($h <= 0) {
  18310. $h = ($y2 - $y1) / $k * ($w / (($x2 - $x1) / $k));
  18311. }
  18312. // fit the image on available space
  18313. list($w, $h, $x, $y) = $this->fitBlock($w, $h, $x, $y, $fitonpage);
  18314. if ($this->rasterize_vector_images) {
  18315. // convert EPS to raster image using GD or ImageMagick libraries
  18316. return $this->Image($file, $x, $y, $w, $h, 'EPS', $link, $align, true, 300, $palign, false, false, $border, false, false, $fitonpage);
  18317. }
  18318. // set scaling factors
  18319. $scale_x = $w / (($x2 - $x1) / $k);
  18320. $scale_y = $h / (($y2 - $y1) / $k);
  18321. // set alignment
  18322. $this->img_rb_y = $y + $h;
  18323. // set alignment
  18324. if ($this->rtl) {
  18325. if ($palign == 'L') {
  18326. $ximg = $this->lMargin;
  18327. } elseif ($palign == 'C') {
  18328. $ximg = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
  18329. } elseif ($palign == 'R') {
  18330. $ximg = $this->w - $this->rMargin - $w;
  18331. } else {
  18332. $ximg = $x - $w;
  18333. }
  18334. $this->img_rb_x = $ximg;
  18335. } else {
  18336. if ($palign == 'L') {
  18337. $ximg = $this->lMargin;
  18338. } elseif ($palign == 'C') {
  18339. $ximg = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
  18340. } elseif ($palign == 'R') {
  18341. $ximg = $this->w - $this->rMargin - $w;
  18342. } else {
  18343. $ximg = $x;
  18344. }
  18345. $this->img_rb_x = $ximg + $w;
  18346. }
  18347. if ($useBoundingBox) {
  18348. $dx = $ximg * $k - $x1;
  18349. $dy = $y * $k - $y1;
  18350. } else {
  18351. $dx = $ximg * $k;
  18352. $dy = $y * $k;
  18353. }
  18354. // save the current graphic state
  18355. $this->_out('q'.$this->epsmarker);
  18356. // translate
  18357. $this->_out(sprintf('%.3F %.3F %.3F %.3F %.3F %.3F cm', 1, 0, 0, 1, $dx, $dy + ($this->hPt - (2 * $y * $k) - ($y2 - $y1))));
  18358. // scale
  18359. if (isset($scale_x)) {
  18360. $this->_out(sprintf('%.3F %.3F %.3F %.3F %.3F %.3F cm', $scale_x, 0, 0, $scale_y, $x1 * (1 - $scale_x), $y2 * (1 - $scale_y)));
  18361. }
  18362. // handle pc/unix/mac line endings
  18363. $lines = preg_split('/[\r\n]+/si', $data, -1, PREG_SPLIT_NO_EMPTY);
  18364. $u=0;
  18365. $cnt = count($lines);
  18366. for ($i=0; $i < $cnt; ++$i) {
  18367. $line = $lines[$i];
  18368. if (($line == '') OR ($line{0} == '%')) {
  18369. continue;
  18370. }
  18371. $len = strlen($line);
  18372. // check for spot color names
  18373. $color_name = '';
  18374. if (strcasecmp('x', substr(trim($line), -1)) == 0) {
  18375. if (preg_match('/\([^\)]*\)/', $line, $matches) > 0) {
  18376. // extract spot color name
  18377. $color_name = $matches[0];
  18378. // remove color name from string
  18379. $line = str_replace(' '.$color_name, '', $line);
  18380. // remove pharentesis from color name
  18381. $color_name = substr($color_name, 1, -1);
  18382. }
  18383. }
  18384. $chunks = explode(' ', $line);
  18385. $cmd = trim(array_pop($chunks));
  18386. // RGB
  18387. if (($cmd == 'Xa') OR ($cmd == 'XA')) {
  18388. $b = array_pop($chunks);
  18389. $g = array_pop($chunks);
  18390. $r = array_pop($chunks);
  18391. $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!
  18392. continue;
  18393. }
  18394. $skip = false;
  18395. if ($fixoutvals) {
  18396. // check for values outside the bounding box
  18397. switch ($cmd) {
  18398. case 'm':
  18399. case 'l':
  18400. case 'L': {
  18401. // skip values outside bounding box
  18402. foreach ($chunks as $key => $val) {
  18403. if ((($key % 2) == 0) AND (($val < $x1) OR ($val > $x2))) {
  18404. $skip = true;
  18405. } elseif ((($key % 2) != 0) AND (($val < $y1) OR ($val > $y2))) {
  18406. $skip = true;
  18407. }
  18408. }
  18409. }
  18410. }
  18411. }
  18412. switch ($cmd) {
  18413. case 'm':
  18414. case 'l':
  18415. case 'v':
  18416. case 'y':
  18417. case 'c':
  18418. case 'k':
  18419. case 'K':
  18420. case 'g':
  18421. case 'G':
  18422. case 's':
  18423. case 'S':
  18424. case 'J':
  18425. case 'j':
  18426. case 'w':
  18427. case 'M':
  18428. case 'd':
  18429. case 'n': {
  18430. if ($skip) {
  18431. break;
  18432. }
  18433. $this->_out($line);
  18434. break;
  18435. }
  18436. case 'x': {// custom fill color
  18437. if (empty($color_name)) {
  18438. // CMYK color
  18439. list($col_c, $col_m, $col_y, $col_k) = $chunks;
  18440. $this->_out(''.$col_c.' '.$col_m.' '.$col_y.' '.$col_k.' k');
  18441. } else {
  18442. // Spot Color (CMYK + tint)
  18443. list($col_c, $col_m, $col_y, $col_k, $col_t) = $chunks;
  18444. $this->AddSpotColor($color_name, ($col_c * 100), ($col_m * 100), ($col_y * 100), ($col_k * 100));
  18445. $color_cmd = sprintf('/CS%d cs %.3F scn', $this->spot_colors[$color_name]['i'], (1 - $col_t));
  18446. $this->_out($color_cmd);
  18447. }
  18448. break;
  18449. }
  18450. case 'X': { // custom stroke color
  18451. if (empty($color_name)) {
  18452. // CMYK color
  18453. list($col_c, $col_m, $col_y, $col_k) = $chunks;
  18454. $this->_out(''.$col_c.' '.$col_m.' '.$col_y.' '.$col_k.' K');
  18455. } else {
  18456. // Spot Color (CMYK + tint)
  18457. list($col_c, $col_m, $col_y, $col_k, $col_t) = $chunks;
  18458. $this->AddSpotColor($color_name, ($col_c * 100), ($col_m * 100), ($col_y * 100), ($col_k * 100));
  18459. $color_cmd = sprintf('/CS%d CS %.3F SCN', $this->spot_colors[$color_name]['i'], (1 - $col_t));
  18460. $this->_out($color_cmd);
  18461. }
  18462. break;
  18463. }
  18464. case 'Y':
  18465. case 'N':
  18466. case 'V':
  18467. case 'L':
  18468. case 'C': {
  18469. if ($skip) {
  18470. break;
  18471. }
  18472. $line[($len - 1)] = strtolower($cmd);
  18473. $this->_out($line);
  18474. break;
  18475. }
  18476. case 'b':
  18477. case 'B': {
  18478. $this->_out($cmd . '*');
  18479. break;
  18480. }
  18481. case 'f':
  18482. case 'F': {
  18483. if ($u > 0) {
  18484. $isU = false;
  18485. $max = min(($i + 5), $cnt);
  18486. for ($j = ($i + 1); $j < $max; ++$j) {
  18487. $isU = ($isU OR (($lines[$j] == 'U') OR ($lines[$j] == '*U')));
  18488. }
  18489. if ($isU) {
  18490. $this->_out('f*');
  18491. }
  18492. } else {
  18493. $this->_out('f*');
  18494. }
  18495. break;
  18496. }
  18497. case '*u': {
  18498. ++$u;
  18499. break;
  18500. }
  18501. case '*U': {
  18502. --$u;
  18503. break;
  18504. }
  18505. }
  18506. }
  18507. // restore previous graphic state
  18508. $this->_out($this->epsmarker.'Q');
  18509. if (!empty($border)) {
  18510. $bx = $this->x;
  18511. $by = $this->y;
  18512. $this->x = $ximg;
  18513. if ($this->rtl) {
  18514. $this->x += $w;
  18515. }
  18516. $this->y = $y;
  18517. $this->Cell($w, $h, '', $border, 0, '', 0, '', 0, true);
  18518. $this->x = $bx;
  18519. $this->y = $by;
  18520. }
  18521. if ($link) {
  18522. $this->Link($ximg, $y, $w, $h, $link, 0);
  18523. }
  18524. // set pointer to align the next text/objects
  18525. switch($align) {
  18526. case 'T':{
  18527. $this->y = $y;
  18528. $this->x = $this->img_rb_x;
  18529. break;
  18530. }
  18531. case 'M':{
  18532. $this->y = $y + round($h/2);
  18533. $this->x = $this->img_rb_x;
  18534. break;
  18535. }
  18536. case 'B':{
  18537. $this->y = $this->img_rb_y;
  18538. $this->x = $this->img_rb_x;
  18539. break;
  18540. }
  18541. case 'N':{
  18542. $this->SetY($this->img_rb_y);
  18543. break;
  18544. }
  18545. default:{
  18546. break;
  18547. }
  18548. }
  18549. $this->endlinex = $this->img_rb_x;
  18550. }
  18551. /**
  18552. * Set document barcode.
  18553. * @param $bc (string) barcode
  18554. * @public
  18555. */
  18556. public function setBarcode($bc='') {
  18557. $this->barcode = $bc;
  18558. }
  18559. /**
  18560. * Get current barcode.
  18561. * @return string
  18562. * @public
  18563. * @since 4.0.012 (2008-07-24)
  18564. */
  18565. public function getBarcode() {
  18566. return $this->barcode;
  18567. }
  18568. /**
  18569. * Print a Linear Barcode.
  18570. * @param $code (string) code to print
  18571. * @param $type (string) type of barcode (see barcodes.php for supported formats).
  18572. * @param $x (int) x position in user units (empty string = current x position)
  18573. * @param $y (int) y position in user units (empty string = current y position)
  18574. * @param $w (int) width in user units (empty string = remaining page width)
  18575. * @param $h (int) height in user units (empty string = remaining page height)
  18576. * @param $xres (float) width of the smallest bar in user units (empty string = default value = 0.4mm)
  18577. * @param $style (array) array of options:<ul>
  18578. * <li>boolean $style['border'] if true prints a border</li>
  18579. * <li>int $style['padding'] padding to leave around the barcode in user units (set to 'auto' for automatic padding)</li>
  18580. * <li>int $style['hpadding'] horizontal padding in user units (set to 'auto' for automatic padding)</li>
  18581. * <li>int $style['vpadding'] vertical padding in user units (set to 'auto' for automatic padding)</li>
  18582. * <li>array $style['fgcolor'] color array for bars and text</li>
  18583. * <li>mixed $style['bgcolor'] color array for background (set to false for transparent)</li>
  18584. * <li>boolean $style['text'] if true prints text below the barcode</li>
  18585. * <li>string $style['label'] override default label</li>
  18586. * <li>string $style['font'] font name for text</li><li>int $style['fontsize'] font size for text</li>
  18587. * <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>
  18588. * <li>string $style['position'] horizontal position of the containing barcode cell on the page: L = left margin; C = center; R = right margin.</li>
  18589. * <li>string $style['align'] horizontal position of the barcode on the containing rectangle: L = left; C = center; R = right.</li>
  18590. * <li>string $style['stretch'] if true stretch the barcode to best fit the available width, otherwise uses $xres resolution for a single bar.</li>
  18591. * <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>
  18592. * <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>
  18593. * @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>
  18594. * @author Nicola Asuni
  18595. * @since 3.1.000 (2008-06-09)
  18596. * @public
  18597. */
  18598. public function write1DBarcode($code, $type, $x='', $y='', $w='', $h='', $xres='', $style='', $align='') {
  18599. if ($this->empty_string(trim($code))) {
  18600. return;
  18601. }
  18602. require_once(dirname(__FILE__).'/barcodes.php');
  18603. // save current graphic settings
  18604. $gvars = $this->getGraphicVars();
  18605. // create new barcode object
  18606. $barcodeobj = new TCPDFBarcode($code, $type);
  18607. $arrcode = $barcodeobj->getBarcodeArray();
  18608. if ($arrcode === false) {
  18609. $this->Error('Error in 1D barcode string');
  18610. }
  18611. // set default values
  18612. if (!isset($style['position'])) {
  18613. $style['position'] = '';
  18614. } elseif ($style['position'] == 'S') {
  18615. // keep this for backward compatibility
  18616. $style['position'] = '';
  18617. $style['stretch'] = true;
  18618. }
  18619. if (!isset($style['fitwidth'])) {
  18620. if (!isset($style['stretch'])) {
  18621. $style['fitwidth'] = true;
  18622. } else {
  18623. $style['fitwidth'] = false;
  18624. }
  18625. }
  18626. if ($style['fitwidth']) {
  18627. // disable stretch
  18628. $style['stretch'] = false;
  18629. }
  18630. if (!isset($style['stretch'])) {
  18631. if (($w === '') OR ($w <= 0)) {
  18632. $style['stretch'] = false;
  18633. } else {
  18634. $style['stretch'] = true;
  18635. }
  18636. }
  18637. if (!isset($style['fgcolor'])) {
  18638. $style['fgcolor'] = array(0,0,0); // default black
  18639. }
  18640. if (!isset($style['bgcolor'])) {
  18641. $style['bgcolor'] = false; // default transparent
  18642. }
  18643. if (!isset($style['border'])) {
  18644. $style['border'] = false;
  18645. }
  18646. $fontsize = 0;
  18647. if (!isset($style['text'])) {
  18648. $style['text'] = false;
  18649. }
  18650. if ($style['text'] AND isset($style['font'])) {
  18651. if (isset($style['fontsize'])) {
  18652. $fontsize = $style['fontsize'];
  18653. }
  18654. $this->SetFont($style['font'], '', $fontsize);
  18655. }
  18656. if (!isset($style['stretchtext'])) {
  18657. $style['stretchtext'] = 4;
  18658. }
  18659. if ($x === '') {
  18660. $x = $this->x;
  18661. }
  18662. if ($y === '') {
  18663. $y = $this->y;
  18664. }
  18665. // check page for no-write regions and adapt page margins if necessary
  18666. list($x, $y) = $this->checkPageRegions($h, $x, $y);
  18667. if (($w === '') OR ($w <= 0)) {
  18668. if ($this->rtl) {
  18669. $w = $x - $this->lMargin;
  18670. } else {
  18671. $w = $this->w - $this->rMargin - $x;
  18672. }
  18673. }
  18674. // padding
  18675. if (!isset($style['padding'])) {
  18676. $padding = 0;
  18677. } elseif ($style['padding'] === 'auto') {
  18678. $padding = 10 * ($w / ($arrcode['maxw'] + 20));
  18679. } else {
  18680. $padding = floatval($style['padding']);
  18681. }
  18682. // horizontal padding
  18683. if (!isset($style['hpadding'])) {
  18684. $hpadding = $padding;
  18685. } elseif ($style['hpadding'] === 'auto') {
  18686. $hpadding = 10 * ($w / ($arrcode['maxw'] + 20));
  18687. } else {
  18688. $hpadding = floatval($style['hpadding']);
  18689. }
  18690. // vertical padding
  18691. if (!isset($style['vpadding'])) {
  18692. $vpadding = $padding;
  18693. } elseif ($style['vpadding'] === 'auto') {
  18694. $vpadding = ($hpadding / 2);
  18695. } else {
  18696. $vpadding = floatval($style['vpadding']);
  18697. }
  18698. // calculate xres (single bar width)
  18699. $max_xres = ($w - (2 * $hpadding)) / $arrcode['maxw'];
  18700. if ($style['stretch']) {
  18701. $xres = $max_xres;
  18702. } else {
  18703. if ($this->empty_string($xres)) {
  18704. $xres = (0.141 * $this->k); // default bar width = 0.4 mm
  18705. }
  18706. if ($xres > $max_xres) {
  18707. // correct xres to fit on $w
  18708. $xres = $max_xres;
  18709. }
  18710. if ((isset($style['padding']) AND ($style['padding'] === 'auto'))
  18711. OR (isset($style['hpadding']) AND ($style['hpadding'] === 'auto'))) {
  18712. $hpadding = 10 * $xres;
  18713. if (isset($style['vpadding']) AND ($style['vpadding'] === 'auto')) {
  18714. $vpadding = ($hpadding / 2);
  18715. }
  18716. }
  18717. }
  18718. if ($style['fitwidth']) {
  18719. $wold = $w;
  18720. $w = (($arrcode['maxw'] * $xres) + (2 * $hpadding));
  18721. if (isset($style['cellfitalign'])) {
  18722. switch ($style['cellfitalign']) {
  18723. case 'L': {
  18724. if ($this->rtl) {
  18725. $x -= ($wold - $w);
  18726. }
  18727. break;
  18728. }
  18729. case 'R': {
  18730. if (!$this->rtl) {
  18731. $x += ($wold - $w);
  18732. }
  18733. break;
  18734. }
  18735. case 'C': {
  18736. if ($this->rtl) {
  18737. $x -= (($wold - $w) / 2);
  18738. } else {
  18739. $x += (($wold - $w) / 2);
  18740. }
  18741. break;
  18742. }
  18743. default : {
  18744. break;
  18745. }
  18746. }
  18747. }
  18748. }
  18749. $text_height = ($this->cell_height_ratio * $fontsize / $this->k);
  18750. // height
  18751. if (($h === '') OR ($h <= 0)) {
  18752. // set default height
  18753. $h = (($arrcode['maxw'] * $xres) / 3) + (2 * $vpadding) + $text_height;
  18754. }
  18755. $barh = $h - $text_height - (2 * $vpadding);
  18756. if ($barh <=0) {
  18757. // try to reduce font or padding to fit barcode on available height
  18758. if ($text_height > $h) {
  18759. $fontsize = (($h * $this->k) / (4 * $this->cell_height_ratio));
  18760. $text_height = ($this->cell_height_ratio * $fontsize / $this->k);
  18761. $this->SetFont($style['font'], '', $fontsize);
  18762. }
  18763. if ($vpadding > 0) {
  18764. $vpadding = (($h - $text_height) / 4);
  18765. }
  18766. $barh = $h - $text_height - (2 * $vpadding);
  18767. }
  18768. // fit the barcode on available space
  18769. list($w, $h, $x, $y) = $this->fitBlock($w, $h, $x, $y, false);
  18770. // set alignment
  18771. $this->img_rb_y = $y + $h;
  18772. // set alignment
  18773. if ($this->rtl) {
  18774. if ($style['position'] == 'L') {
  18775. $xpos = $this->lMargin;
  18776. } elseif ($style['position'] == 'C') {
  18777. $xpos = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
  18778. } elseif ($style['position'] == 'R') {
  18779. $xpos = $this->w - $this->rMargin - $w;
  18780. } else {
  18781. $xpos = $x - $w;
  18782. }
  18783. $this->img_rb_x = $xpos;
  18784. } else {
  18785. if ($style['position'] == 'L') {
  18786. $xpos = $this->lMargin;
  18787. } elseif ($style['position'] == 'C') {
  18788. $xpos = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
  18789. } elseif ($style['position'] == 'R') {
  18790. $xpos = $this->w - $this->rMargin - $w;
  18791. } else {
  18792. $xpos = $x;
  18793. }
  18794. $this->img_rb_x = $xpos + $w;
  18795. }
  18796. $xpos_rect = $xpos;
  18797. if (!isset($style['align'])) {
  18798. $style['align'] = 'C';
  18799. }
  18800. switch ($style['align']) {
  18801. case 'L': {
  18802. $xpos = $xpos_rect + $hpadding;
  18803. break;
  18804. }
  18805. case 'R': {
  18806. $xpos = $xpos_rect + ($w - ($arrcode['maxw'] * $xres)) - $hpadding;
  18807. break;
  18808. }
  18809. case 'C':
  18810. default : {
  18811. $xpos = $xpos_rect + (($w - ($arrcode['maxw'] * $xres)) / 2);
  18812. break;
  18813. }
  18814. }
  18815. $xpos_text = $xpos;
  18816. // barcode is always printed in LTR direction
  18817. $tempRTL = $this->rtl;
  18818. $this->rtl = false;
  18819. // print background color
  18820. if ($style['bgcolor']) {
  18821. $this->Rect($xpos_rect, $y, $w, $h, $style['border'] ? 'DF' : 'F', '', $style['bgcolor']);
  18822. } elseif ($style['border']) {
  18823. $this->Rect($xpos_rect, $y, $w, $h, 'D');
  18824. }
  18825. // set foreground color
  18826. $this->SetDrawColorArray($style['fgcolor']);
  18827. $this->SetTextColorArray($style['fgcolor']);
  18828. // print bars
  18829. foreach ($arrcode['bcode'] as $k => $v) {
  18830. $bw = ($v['w'] * $xres);
  18831. if ($v['t']) {
  18832. // draw a vertical bar
  18833. $ypos = $y + $vpadding + ($v['p'] * $barh / $arrcode['maxh']);
  18834. $this->Rect($xpos, $ypos, $bw, ($v['h'] * $barh / $arrcode['maxh']), 'F', array(), $style['fgcolor']);
  18835. }
  18836. $xpos += $bw;
  18837. }
  18838. // print text
  18839. if ($style['text']) {
  18840. if (isset($style['label']) AND !$this->empty_string($style['label'])) {
  18841. $label = $style['label'];
  18842. } else {
  18843. $label = $code;
  18844. }
  18845. $txtwidth = ($arrcode['maxw'] * $xres);
  18846. if ($this->GetStringWidth($label) > $txtwidth) {
  18847. $style['stretchtext'] = 2;
  18848. }
  18849. // print text
  18850. $this->x = $xpos_text;
  18851. $this->y = $y + $vpadding + $barh;
  18852. $cellpadding = $this->cell_padding;
  18853. $this->SetCellPadding(0);
  18854. $this->Cell($txtwidth, '', $label, 0, 0, 'C', false, '', $style['stretchtext'], false, 'T', 'T');
  18855. $this->cell_padding = $cellpadding;
  18856. }
  18857. // restore original direction
  18858. $this->rtl = $tempRTL;
  18859. // restore previous settings
  18860. $this->setGraphicVars($gvars);
  18861. // set pointer to align the next text/objects
  18862. switch($align) {
  18863. case 'T':{
  18864. $this->y = $y;
  18865. $this->x = $this->img_rb_x;
  18866. break;
  18867. }
  18868. case 'M':{
  18869. $this->y = $y + round($h / 2);
  18870. $this->x = $this->img_rb_x;
  18871. break;
  18872. }
  18873. case 'B':{
  18874. $this->y = $this->img_rb_y;
  18875. $this->x = $this->img_rb_x;
  18876. break;
  18877. }
  18878. case 'N':{
  18879. $this->SetY($this->img_rb_y);
  18880. break;
  18881. }
  18882. default:{
  18883. break;
  18884. }
  18885. }
  18886. $this->endlinex = $this->img_rb_x;
  18887. }
  18888. /**
  18889. * This function is DEPRECATED, please use the new write1DBarcode() function.
  18890. * @param $x (int) x position in user units
  18891. * @param $y (int) y position in user units
  18892. * @param $w (int) width in user units
  18893. * @param $h (int) height position in user units
  18894. * @param $type (string) type of barcode
  18895. * @param $style (string) barcode style
  18896. * @param $font (string) font for text
  18897. * @param $xres (int) x resolution
  18898. * @param $code (string) code to print
  18899. * @deprecated deprecated since version 3.1.000 (2008-06-10)
  18900. * @public
  18901. * @see write1DBarcode()
  18902. */
  18903. public function writeBarcode($x, $y, $w, $h, $type, $style, $font, $xres, $code) {
  18904. // convert old settings for the new write1DBarcode() function.
  18905. $xres = 1 / $xres;
  18906. $newstyle = array(
  18907. 'position' => '',
  18908. 'align' => '',
  18909. 'stretch' => false,
  18910. 'fitwidth' => false,
  18911. 'cellfitalign' => '',
  18912. 'border' => false,
  18913. 'padding' => 0,
  18914. 'fgcolor' => array(0,0,0),
  18915. 'bgcolor' => false,
  18916. 'text' => true,
  18917. 'font' => $font,
  18918. 'fontsize' => 8,
  18919. 'stretchtext' => 4
  18920. );
  18921. if ($style & 1) {
  18922. $newstyle['border'] = true;
  18923. }
  18924. if ($style & 2) {
  18925. $newstyle['bgcolor'] = false;
  18926. }
  18927. if ($style & 4) {
  18928. $newstyle['position'] = 'C';
  18929. } elseif ($style & 8) {
  18930. $newstyle['position'] = 'L';
  18931. } elseif ($style & 16) {
  18932. $newstyle['position'] = 'R';
  18933. }
  18934. if ($style & 128) {
  18935. $newstyle['text'] = true;
  18936. }
  18937. if ($style & 256) {
  18938. $newstyle['stretchtext'] = 4;
  18939. }
  18940. $this->write1DBarcode($code, $type, $x, $y, $w, $h, $xres, $newstyle, '');
  18941. }
  18942. /**
  18943. * Print 2D Barcode.
  18944. * @param $code (string) code to print
  18945. * @param $type (string) type of barcode (see 2dbarcodes.php for supported formats).
  18946. * @param $x (int) x position in user units
  18947. * @param $y (int) y position in user units
  18948. * @param $w (int) width in user units
  18949. * @param $h (int) height in user units
  18950. * @param $style (array) array of options:<ul>
  18951. * <li>boolean $style['border'] if true prints a border around the barcode</li>
  18952. * <li>int $style['padding'] padding to leave around the barcode in barcode units (set to 'auto' for automatic padding)</li>
  18953. * <li>int $style['hpadding'] horizontal padding in barcode units (set to 'auto' for automatic padding)</li>
  18954. * <li>int $style['vpadding'] vertical padding in barcode units (set to 'auto' for automatic padding)</li>
  18955. * <li>int $style['module_width'] width of a single module in points</li>
  18956. * <li>int $style['module_height'] height of a single module in points</li>
  18957. * <li>array $style['fgcolor'] color array for bars and text</li>
  18958. * <li>mixed $style['bgcolor'] color array for background or false for transparent</li>
  18959. * <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>
  18960. * <li>$style['module_height'] height of a single module in points</li></ul>
  18961. * @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>
  18962. * @param $distort (boolean) if true distort the barcode to fit width and height, otherwise preserve aspect ratio
  18963. * @author Nicola Asuni
  18964. * @since 4.5.037 (2009-04-07)
  18965. * @public
  18966. */
  18967. public function write2DBarcode($code, $type, $x='', $y='', $w='', $h='', $style='', $align='', $distort=false) {
  18968. if ($this->empty_string(trim($code))) {
  18969. return;
  18970. }
  18971. require_once(dirname(__FILE__).'/2dbarcodes.php');
  18972. // save current graphic settings
  18973. $gvars = $this->getGraphicVars();
  18974. // create new barcode object
  18975. $barcodeobj = new TCPDF2DBarcode($code, $type);
  18976. $arrcode = $barcodeobj->getBarcodeArray();
  18977. if (($arrcode === false) OR empty($arrcode)) {
  18978. $this->Error('Error in 2D barcode string');
  18979. }
  18980. // set default values
  18981. if (!isset($style['position'])) {
  18982. $style['position'] = '';
  18983. }
  18984. if (!isset($style['fgcolor'])) {
  18985. $style['fgcolor'] = array(0,0,0); // default black
  18986. }
  18987. if (!isset($style['bgcolor'])) {
  18988. $style['bgcolor'] = false; // default transparent
  18989. }
  18990. if (!isset($style['border'])) {
  18991. $style['border'] = false;
  18992. }
  18993. // padding
  18994. if (!isset($style['padding'])) {
  18995. $style['padding'] = 0;
  18996. } elseif ($style['padding'] === 'auto') {
  18997. $style['padding'] = 4;
  18998. }
  18999. if (!isset($style['hpadding'])) {
  19000. $style['hpadding'] = $style['padding'];
  19001. } elseif ($style['hpadding'] === 'auto') {
  19002. $style['hpadding'] = 4;
  19003. }
  19004. if (!isset($style['vpadding'])) {
  19005. $style['vpadding'] = $style['padding'];
  19006. } elseif ($style['vpadding'] === 'auto') {
  19007. $style['vpadding'] = 4;
  19008. }
  19009. $hpad = (2 * $style['hpadding']);
  19010. $vpad = (2 * $style['vpadding']);
  19011. // cell (module) dimension
  19012. if (!isset($style['module_width'])) {
  19013. $style['module_width'] = 1; // width of a single module in points
  19014. }
  19015. if (!isset($style['module_height'])) {
  19016. $style['module_height'] = 1; // height of a single module in points
  19017. }
  19018. if ($x === '') {
  19019. $x = $this->x;
  19020. }
  19021. if ($y === '') {
  19022. $y = $this->y;
  19023. }
  19024. // check page for no-write regions and adapt page margins if necessary
  19025. list($x, $y) = $this->checkPageRegions($h, $x, $y);
  19026. // number of barcode columns and rows
  19027. $rows = $arrcode['num_rows'];
  19028. $cols = $arrcode['num_cols'];
  19029. // module width and height
  19030. $mw = $style['module_width'];
  19031. $mh = $style['module_height'];
  19032. // get max dimensions
  19033. if ($this->rtl) {
  19034. $maxw = $x - $this->lMargin;
  19035. } else {
  19036. $maxw = $this->w - $this->rMargin - $x;
  19037. }
  19038. $maxh = ($this->h - $this->tMargin - $this->bMargin);
  19039. $ratioHW = ((($rows * $mh) + $hpad) / (($cols * $mw) + $vpad));
  19040. $ratioWH = ((($cols * $mw) + $vpad) / (($rows * $mh) + $hpad));
  19041. if (!$distort) {
  19042. if (($maxw * $ratioHW) > $maxh) {
  19043. $maxw = $maxh * $ratioWH;
  19044. }
  19045. if (($maxh * $ratioWH) > $maxw) {
  19046. $maxh = $maxw * $ratioHW;
  19047. }
  19048. }
  19049. // set maximum dimesions
  19050. if ($w > $maxw) {
  19051. $w = $maxw;
  19052. }
  19053. if ($h > $maxh) {
  19054. $h = $maxh;
  19055. }
  19056. // set dimensions
  19057. if ((($w === '') OR ($w <= 0)) AND (($h === '') OR ($h <= 0))) {
  19058. $w = ($cols + $hpad) * ($mw / $this->k);
  19059. $h = ($rows + $vpad) * ($mh / $this->k);
  19060. } elseif (($w === '') OR ($w <= 0)) {
  19061. $w = $h * $ratioWH;
  19062. } elseif (($h === '') OR ($h <= 0)) {
  19063. $h = $w * $ratioHW;
  19064. }
  19065. // barcode size (excluding padding)
  19066. $bw = ($w * $cols) / ($cols + $hpad);
  19067. $bh = ($h * $rows) / ($rows + $vpad);
  19068. // dimension of single barcode cell unit
  19069. $cw = $bw / $cols;
  19070. $ch = $bh / $rows;
  19071. if (!$distort) {
  19072. if (($cw / $ch) > ($mw / $mh)) {
  19073. // correct horizontal distortion
  19074. $cw = $ch * $mw / $mh;
  19075. $bw = $cw * $cols;
  19076. $style['hpadding'] = ($w - $bw) / (2 * $cw);
  19077. } else {
  19078. // correct vertical distortion
  19079. $ch = $cw * $mh / $mw;
  19080. $bh = $ch * $rows;
  19081. $style['vpadding'] = ($h - $bh) / (2 * $ch);
  19082. }
  19083. }
  19084. // fit the barcode on available space
  19085. list($w, $h, $x, $y) = $this->fitBlock($w, $h, $x, $y, false);
  19086. // set alignment
  19087. $this->img_rb_y = $y + $h;
  19088. // set alignment
  19089. if ($this->rtl) {
  19090. if ($style['position'] == 'L') {
  19091. $xpos = $this->lMargin;
  19092. } elseif ($style['position'] == 'C') {
  19093. $xpos = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
  19094. } elseif ($style['position'] == 'R') {
  19095. $xpos = $this->w - $this->rMargin - $w;
  19096. } else {
  19097. $xpos = $x - $w;
  19098. }
  19099. $this->img_rb_x = $xpos;
  19100. } else {
  19101. if ($style['position'] == 'L') {
  19102. $xpos = $this->lMargin;
  19103. } elseif ($style['position'] == 'C') {
  19104. $xpos = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
  19105. } elseif ($style['position'] == 'R') {
  19106. $xpos = $this->w - $this->rMargin - $w;
  19107. } else {
  19108. $xpos = $x;
  19109. }
  19110. $this->img_rb_x = $xpos + $w;
  19111. }
  19112. $xstart = $xpos + ($style['hpadding'] * $cw);
  19113. $ystart = $y + ($style['vpadding'] * $ch);
  19114. // barcode is always printed in LTR direction
  19115. $tempRTL = $this->rtl;
  19116. $this->rtl = false;
  19117. // print background color
  19118. if ($style['bgcolor']) {
  19119. $this->Rect($xpos, $y, $w, $h, $style['border'] ? 'DF' : 'F', '', $style['bgcolor']);
  19120. } elseif ($style['border']) {
  19121. $this->Rect($xpos, $y, $w, $h, 'D');
  19122. }
  19123. // set foreground color
  19124. $this->SetDrawColorArray($style['fgcolor']);
  19125. // print barcode cells
  19126. // for each row
  19127. for ($r = 0; $r < $rows; ++$r) {
  19128. $xr = $xstart;
  19129. // for each column
  19130. for ($c = 0; $c < $cols; ++$c) {
  19131. if ($arrcode['bcode'][$r][$c] == 1) {
  19132. // draw a single barcode cell
  19133. $this->Rect($xr, $ystart, $cw, $ch, 'F', array(), $style['fgcolor']);
  19134. }
  19135. $xr += $cw;
  19136. }
  19137. $ystart += $ch;
  19138. }
  19139. // restore original direction
  19140. $this->rtl = $tempRTL;
  19141. // restore previous settings
  19142. $this->setGraphicVars($gvars);
  19143. // set pointer to align the next text/objects
  19144. switch($align) {
  19145. case 'T':{
  19146. $this->y = $y;
  19147. $this->x = $this->img_rb_x;
  19148. break;
  19149. }
  19150. case 'M':{
  19151. $this->y = $y + round($h/2);
  19152. $this->x = $this->img_rb_x;
  19153. break;
  19154. }
  19155. case 'B':{
  19156. $this->y = $this->img_rb_y;
  19157. $this->x = $this->img_rb_x;
  19158. break;
  19159. }
  19160. case 'N':{
  19161. $this->SetY($this->img_rb_y);
  19162. break;
  19163. }
  19164. default:{
  19165. break;
  19166. }
  19167. }
  19168. $this->endlinex = $this->img_rb_x;
  19169. }
  19170. /**
  19171. * Returns an array containing current margins:
  19172. * <ul>
  19173. <li>$ret['left'] = left margin</li>
  19174. <li>$ret['right'] = right margin</li>
  19175. <li>$ret['top'] = top margin</li>
  19176. <li>$ret['bottom'] = bottom margin</li>
  19177. <li>$ret['header'] = header margin</li>
  19178. <li>$ret['footer'] = footer margin</li>
  19179. <li>$ret['cell'] = cell padding array</li>
  19180. <li>$ret['padding_left'] = cell left padding</li>
  19181. <li>$ret['padding_top'] = cell top padding</li>
  19182. <li>$ret['padding_right'] = cell right padding</li>
  19183. <li>$ret['padding_bottom'] = cell bottom padding</li>
  19184. * </ul>
  19185. * @return array containing all margins measures
  19186. * @public
  19187. * @since 3.2.000 (2008-06-23)
  19188. */
  19189. public function getMargins() {
  19190. $ret = array(
  19191. 'left' => $this->lMargin,
  19192. 'right' => $this->rMargin,
  19193. 'top' => $this->tMargin,
  19194. 'bottom' => $this->bMargin,
  19195. 'header' => $this->header_margin,
  19196. 'footer' => $this->footer_margin,
  19197. 'cell' => $this->cell_padding,
  19198. 'padding_left' => $this->cell_padding['L'],
  19199. 'padding_top' => $this->cell_padding['T'],
  19200. 'padding_right' => $this->cell_padding['R'],
  19201. 'padding_bottom' => $this->cell_padding['B']
  19202. );
  19203. return $ret;
  19204. }
  19205. /**
  19206. * Returns an array containing original margins:
  19207. * <ul>
  19208. <li>$ret['left'] = left margin</li>
  19209. <li>$ret['right'] = right margin</li>
  19210. * </ul>
  19211. * @return array containing all margins measures
  19212. * @public
  19213. * @since 4.0.012 (2008-07-24)
  19214. */
  19215. public function getOriginalMargins() {
  19216. $ret = array(
  19217. 'left' => $this->original_lMargin,
  19218. 'right' => $this->original_rMargin
  19219. );
  19220. return $ret;
  19221. }
  19222. /**
  19223. * Returns the current font size.
  19224. * @return current font size
  19225. * @public
  19226. * @since 3.2.000 (2008-06-23)
  19227. */
  19228. public function getFontSize() {
  19229. return $this->FontSize;
  19230. }
  19231. /**
  19232. * Returns the current font size in points unit.
  19233. * @return current font size in points unit
  19234. * @public
  19235. * @since 3.2.000 (2008-06-23)
  19236. */
  19237. public function getFontSizePt() {
  19238. return $this->FontSizePt;
  19239. }
  19240. /**
  19241. * Returns the current font family name.
  19242. * @return string current font family name
  19243. * @public
  19244. * @since 4.3.008 (2008-12-05)
  19245. */
  19246. public function getFontFamily() {
  19247. return $this->FontFamily;
  19248. }
  19249. /**
  19250. * Returns the current font style.
  19251. * @return string current font style
  19252. * @public
  19253. * @since 4.3.008 (2008-12-05)
  19254. */
  19255. public function getFontStyle() {
  19256. return $this->FontStyle;
  19257. }
  19258. /**
  19259. * Cleanup HTML code (requires HTML Tidy library).
  19260. * @param $html (string) htmlcode to fix
  19261. * @param $default_css (string) CSS commands to add
  19262. * @param $tagvs (array) parameters for setHtmlVSpace method
  19263. * @param $tidy_options (array) options for tidy_parse_string function
  19264. * @return string XHTML code cleaned up
  19265. * @author Nicola Asuni
  19266. * @public
  19267. * @since 5.9.017 (2010-11-16)
  19268. * @see setHtmlVSpace()
  19269. */
  19270. public function fixHTMLCode($html, $default_css='', $tagvs='', $tidy_options='') {
  19271. // configure parameters for HTML Tidy
  19272. if ($tidy_options === '') {
  19273. $tidy_options = array (
  19274. 'clean' => 1,
  19275. 'drop-empty-paras' => 0,
  19276. 'drop-proprietary-attributes' => 1,
  19277. 'fix-backslash' => 1,
  19278. 'hide-comments' => 1,
  19279. 'join-styles' => 1,
  19280. 'lower-literals' => 1,
  19281. 'merge-divs' => 1,
  19282. 'merge-spans' => 1,
  19283. 'output-xhtml' => 1,
  19284. 'word-2000' => 1,
  19285. 'wrap' => 0,
  19286. 'output-bom' => 0,
  19287. //'char-encoding' => 'utf8',
  19288. //'input-encoding' => 'utf8',
  19289. //'output-encoding' => 'utf8'
  19290. );
  19291. }
  19292. // clean up the HTML code
  19293. $tidy = tidy_parse_string($html, $tidy_options);
  19294. // fix the HTML
  19295. $tidy->cleanRepair();
  19296. // get the CSS part
  19297. $tidy_head = tidy_get_head($tidy);
  19298. $css = $tidy_head->value;
  19299. $css = preg_replace('/<style([^>]+)>/ims', '<style>', $css);
  19300. $css = preg_replace('/<\/style>(.*)<style>/ims', "\n", $css);
  19301. $css = str_replace('/*<![CDATA[*/', '', $css);
  19302. $css = str_replace('/*]]>*/', '', $css);
  19303. preg_match('/<style>(.*)<\/style>/ims', $css, $matches);
  19304. if (isset($matches[1])) {
  19305. $css = strtolower($matches[1]);
  19306. } else {
  19307. $css = '';
  19308. }
  19309. // include default css
  19310. $css = '<style>'.$default_css.$css.'</style>';
  19311. // get the body part
  19312. $tidy_body = tidy_get_body($tidy);
  19313. $html = $tidy_body->value;
  19314. // fix some self-closing tags
  19315. $html = str_replace('<br>', '<br />', $html);
  19316. // remove some empty tag blocks
  19317. $html = preg_replace('/<div([^\>]*)><\/div>/', '', $html);
  19318. $html = preg_replace('/<p([^\>]*)><\/p>/', '', $html);
  19319. if ($tagvs !== '') {
  19320. // set vertical space for some XHTML tags
  19321. $this->setHtmlVSpace($tagvs);
  19322. }
  19323. // return the cleaned XHTML code + CSS
  19324. return $css.$html;
  19325. }
  19326. /**
  19327. * Extracts the CSS properties from a CSS string.
  19328. * @param $cssdata (string) string containing CSS definitions.
  19329. * @return An array where the keys are the CSS selectors and the values are the CSS properties.
  19330. * @author Nicola Asuni
  19331. * @since 5.1.000 (2010-05-25)
  19332. * @protected
  19333. */
  19334. protected function extractCSSproperties($cssdata) {
  19335. if (empty($cssdata)) {
  19336. return array();
  19337. }
  19338. // remove comments
  19339. $cssdata = preg_replace('/\/\*[^\*]*\*\//', '', $cssdata);
  19340. // remove newlines and multiple spaces
  19341. $cssdata = preg_replace('/[\s]+/', ' ', $cssdata);
  19342. // remove some spaces
  19343. $cssdata = preg_replace('/[\s]*([;:\{\}]{1})[\s]*/', '\\1', $cssdata);
  19344. // remove empty blocks
  19345. $cssdata = preg_replace('/([^\}\{]+)\{\}/', '', $cssdata);
  19346. // replace media type parenthesis
  19347. $cssdata = preg_replace('/@media[\s]+([^\{]*)\{/i', '@media \\1§', $cssdata);
  19348. $cssdata = preg_replace('/\}\}/si', '}§', $cssdata);
  19349. // trim string
  19350. $cssdata = trim($cssdata);
  19351. // find media blocks (all, braille, embossed, handheld, print, projection, screen, speech, tty, tv)
  19352. $cssblocks = array();
  19353. $matches = array();
  19354. if (preg_match_all('/@media[\s]+([^\§]*)§([^§]*)§/i', $cssdata, $matches) > 0) {
  19355. foreach ($matches[1] as $key => $type) {
  19356. $cssblocks[$type] = $matches[2][$key];
  19357. }
  19358. // remove media blocks
  19359. $cssdata = preg_replace('/@media[\s]+([^\§]*)§([^§]*)§/i', '', $cssdata);
  19360. }
  19361. // keep 'all' and 'print' media, other media types are discarded
  19362. if (isset($cssblocks['all']) AND !empty($cssblocks['all'])) {
  19363. $cssdata .= $cssblocks['all'];
  19364. }
  19365. if (isset($cssblocks['print']) AND !empty($cssblocks['print'])) {
  19366. $cssdata .= $cssblocks['print'];
  19367. }
  19368. // reset css blocks array
  19369. $cssblocks = array();
  19370. $matches = array();
  19371. // explode css data string into array
  19372. if (substr($cssdata, -1) == '}') {
  19373. // remove last parethesis
  19374. $cssdata = substr($cssdata, 0, -1);
  19375. }
  19376. $matches = explode('}', $cssdata);
  19377. foreach ($matches as $key => $block) {
  19378. // index 0 contains the CSS selector, index 1 contains CSS properties
  19379. $cssblocks[$key] = explode('{', $block);
  19380. if (!isset($cssblocks[$key][1])) {
  19381. // remove empty definitions
  19382. unset($cssblocks[$key]);
  19383. }
  19384. }
  19385. // split groups of selectors (comma-separated list of selectors)
  19386. foreach ($cssblocks as $key => $block) {
  19387. if (strpos($block[0], ',') > 0) {
  19388. $selectors = explode(',', $block[0]);
  19389. foreach ($selectors as $sel) {
  19390. $cssblocks[] = array(0 => trim($sel), 1 => $block[1]);
  19391. }
  19392. unset($cssblocks[$key]);
  19393. }
  19394. }
  19395. // covert array to selector => properties
  19396. $cssdata = array();
  19397. foreach ($cssblocks as $block) {
  19398. $selector = $block[0];
  19399. // calculate selector's specificity
  19400. $matches = array();
  19401. $a = 0; // the declaration is not from is a 'style' attribute
  19402. $b = intval(preg_match_all('/[\#]/', $selector, $matches)); // number of ID attributes
  19403. $c = intval(preg_match_all('/[\[\.]/', $selector, $matches)); // number of other attributes
  19404. $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
  19405. $d = intval(preg_match_all('/[\>\+\~\s]{1}[a-zA-Z0-9]+/', ' '.$selector, $matches)); // number of element names
  19406. $d += intval(preg_match_all('/[\:][\:]/', $selector, $matches)); // number of pseudo-elements
  19407. $specificity = $a.$b.$c.$d;
  19408. // add specificity to the beginning of the selector
  19409. $cssdata[$specificity.' '.$selector] = $block[1];
  19410. }
  19411. // sort selectors alphabetically to account for specificity
  19412. ksort($cssdata, SORT_STRING);
  19413. // return array
  19414. return $cssdata;
  19415. }
  19416. /**
  19417. * Returns true if the CSS selector is valid for the selected HTML tag
  19418. * @param $dom (array) array of HTML tags and properties
  19419. * @param $key (int) key of the current HTML tag
  19420. * @param $selector (string) CSS selector string
  19421. * @return true if the selector is valid, false otherwise
  19422. * @protected
  19423. * @since 5.1.000 (2010-05-25)
  19424. */
  19425. protected function isValidCSSSelectorForTag($dom, $key, $selector) {
  19426. $valid = false; // value to be returned
  19427. $tag = $dom[$key]['value'];
  19428. $class = array();
  19429. if (isset($dom[$key]['attribute']['class']) AND !empty($dom[$key]['attribute']['class'])) {
  19430. $class = explode(' ', strtolower($dom[$key]['attribute']['class']));
  19431. }
  19432. $id = '';
  19433. if (isset($dom[$key]['attribute']['id']) AND !empty($dom[$key]['attribute']['id'])) {
  19434. $id = strtolower($dom[$key]['attribute']['id']);
  19435. }
  19436. $selector = preg_replace('/([\>\+\~\s]{1})([\.]{1})([^\>\+\~\s]*)/si', '\\1*.\\3', $selector);
  19437. $matches = array();
  19438. if (preg_match_all('/([\>\+\~\s]{1})([a-zA-Z0-9\*]+)([^\>\+\~\s]*)/si', $selector, $matches, PREG_PATTERN_ORDER | PREG_OFFSET_CAPTURE) > 0) {
  19439. $parentop = array_pop($matches[1]);
  19440. $operator = $parentop[0];
  19441. $offset = $parentop[1];
  19442. $lasttag = array_pop($matches[2]);
  19443. $lasttag = strtolower(trim($lasttag[0]));
  19444. if (($lasttag == '*') OR ($lasttag == $tag)) {
  19445. // the last element on selector is our tag or 'any tag'
  19446. $attrib = array_pop($matches[3]);
  19447. $attrib = strtolower(trim($attrib[0]));
  19448. if (!empty($attrib)) {
  19449. // check if matches class, id, attribute, pseudo-class or pseudo-element
  19450. switch ($attrib{0}) {
  19451. case '.': { // class
  19452. if (in_array(substr($attrib, 1), $class)) {
  19453. $valid = true;
  19454. }
  19455. break;
  19456. }
  19457. case '#': { // ID
  19458. if (substr($attrib, 1) == $id) {
  19459. $valid = true;
  19460. }
  19461. break;
  19462. }
  19463. case '[': { // attribute
  19464. $attrmatch = array();
  19465. if (preg_match('/\[([a-zA-Z0-9]*)[\s]*([\~\^\$\*\|\=]*)[\s]*["]?([^"\]]*)["]?\]/i', $attrib, $attrmatch) > 0) {
  19466. $att = strtolower($attrmatch[1]);
  19467. $val = $attrmatch[3];
  19468. if (isset($dom[$key]['attribute'][$att])) {
  19469. switch ($attrmatch[2]) {
  19470. case '=': {
  19471. if ($dom[$key]['attribute'][$att] == $val) {
  19472. $valid = true;
  19473. }
  19474. break;
  19475. }
  19476. case '~=': {
  19477. if (in_array($val, explode(' ', $dom[$key]['attribute'][$att]))) {
  19478. $valid = true;
  19479. }
  19480. break;
  19481. }
  19482. case '^=': {
  19483. if ($val == substr($dom[$key]['attribute'][$att], 0, strlen($val))) {
  19484. $valid = true;
  19485. }
  19486. break;
  19487. }
  19488. case '$=': {
  19489. if ($val == substr($dom[$key]['attribute'][$att], -strlen($val))) {
  19490. $valid = true;
  19491. }
  19492. break;
  19493. }
  19494. case '*=': {
  19495. if (strpos($dom[$key]['attribute'][$att], $val) !== false) {
  19496. $valid = true;
  19497. }
  19498. break;
  19499. }
  19500. case '|=': {
  19501. if ($dom[$key]['attribute'][$att] == $val) {
  19502. $valid = true;
  19503. } elseif (preg_match('/'.$val.'[\-]{1}/i', $dom[$key]['attribute'][$att]) > 0) {
  19504. $valid = true;
  19505. }
  19506. break;
  19507. }
  19508. default: {
  19509. $valid = true;
  19510. }
  19511. }
  19512. }
  19513. }
  19514. break;
  19515. }
  19516. case ':': { // pseudo-class or pseudo-element
  19517. if ($attrib{1} == ':') { // pseudo-element
  19518. // pseudo-elements are not supported!
  19519. // (::first-line, ::first-letter, ::before, ::after)
  19520. } else { // pseudo-class
  19521. // pseudo-classes are not supported!
  19522. // (: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)
  19523. }
  19524. break;
  19525. }
  19526. } // end of switch
  19527. } else {
  19528. $valid = true;
  19529. }
  19530. if ($valid AND ($offset > 0)) {
  19531. $valid = false;
  19532. // check remaining selector part
  19533. $selector = substr($selector, 0, $offset);
  19534. switch ($operator) {
  19535. case ' ': { // descendant of an element
  19536. while ($dom[$key]['parent'] > 0) {
  19537. if ($this->isValidCSSSelectorForTag($dom, $dom[$key]['parent'], $selector)) {
  19538. $valid = true;
  19539. break;
  19540. } else {
  19541. $key = $dom[$key]['parent'];
  19542. }
  19543. }
  19544. break;
  19545. }
  19546. case '>': { // child of an element
  19547. $valid = $this->isValidCSSSelectorForTag($dom, $dom[$key]['parent'], $selector);
  19548. break;
  19549. }
  19550. case '+': { // immediately preceded by an element
  19551. for ($i = ($key - 1); $i > $dom[$key]['parent']; --$i) {
  19552. if ($dom[$i]['tag'] AND $dom[$i]['opening']) {
  19553. $valid = $this->isValidCSSSelectorForTag($dom, $i, $selector);
  19554. break;
  19555. }
  19556. }
  19557. break;
  19558. }
  19559. case '~': { // preceded by an element
  19560. for ($i = ($key - 1); $i > $dom[$key]['parent']; --$i) {
  19561. if ($dom[$i]['tag'] AND $dom[$i]['opening']) {
  19562. if ($this->isValidCSSSelectorForTag($dom, $i, $selector)) {
  19563. break;
  19564. }
  19565. }
  19566. }
  19567. break;
  19568. }
  19569. }
  19570. }
  19571. }
  19572. }
  19573. return $valid;
  19574. }
  19575. /**
  19576. * Returns the styles array that apply for the selected HTML tag.
  19577. * @param $dom (array) array of HTML tags and properties
  19578. * @param $key (int) key of the current HTML tag
  19579. * @param $css (array) array of CSS properties
  19580. * @return array containing CSS properties
  19581. * @protected
  19582. * @since 5.1.000 (2010-05-25)
  19583. */
  19584. protected function getCSSdataArray($dom, $key, $css) {
  19585. $cssarray = array(); // style to be returned
  19586. // get parent CSS selectors
  19587. $selectors = array();
  19588. if (isset($dom[($dom[$key]['parent'])]['csssel'])) {
  19589. $selectors = $dom[($dom[$key]['parent'])]['csssel'];
  19590. }
  19591. // get all styles that apply
  19592. foreach($css as $selector => $style) {
  19593. $pos = strpos($selector, ' ');
  19594. // get specificity
  19595. $specificity = substr($selector, 0, $pos);
  19596. // remove specificity
  19597. $selector = substr($selector, $pos);
  19598. // check if this selector apply to current tag
  19599. if ($this->isValidCSSSelectorForTag($dom, $key, $selector)) {
  19600. if (!in_array($selector, $selectors)) {
  19601. // add style if not already added on parent selector
  19602. $cssarray[] = array('k' => $selector, 's' => $specificity, 'c' => $style);
  19603. $selectors[] = $selector;
  19604. }
  19605. }
  19606. }
  19607. if (isset($dom[$key]['attribute']['style'])) {
  19608. // attach inline style (latest properties have high priority)
  19609. $cssarray[] = array('k' => '', 's' => '1000', 'c' => $dom[$key]['attribute']['style']);
  19610. }
  19611. // order the css array to account for specificity
  19612. $cssordered = array();
  19613. foreach ($cssarray as $key => $val) {
  19614. $skey = sprintf('%04d', $key);
  19615. $cssordered[$val['s'].'_'.$skey] = $val;
  19616. }
  19617. // sort selectors alphabetically to account for specificity
  19618. ksort($cssordered, SORT_STRING);
  19619. return array($selectors, $cssordered);
  19620. }
  19621. /**
  19622. * Compact CSS data array into single string.
  19623. * @param $css (array) array of CSS properties
  19624. * @return string containing merged CSS properties
  19625. * @protected
  19626. * @since 5.9.070 (2011-04-19)
  19627. */
  19628. protected function getTagStyleFromCSSarray($css) {
  19629. $tagstyle = ''; // value to be returned
  19630. foreach ($css as $style) {
  19631. // split single css commands
  19632. $csscmds = explode(';', $style['c']);
  19633. foreach ($csscmds as $cmd) {
  19634. if (!empty($cmd)) {
  19635. $pos = strpos($cmd, ':');
  19636. if ($pos !== false) {
  19637. $cmd = substr($cmd, 0, ($pos + 1));
  19638. if (strpos($tagstyle, $cmd) !== false) {
  19639. // remove duplicate commands (last commands have high priority)
  19640. $tagstyle = preg_replace('/'.$cmd.'[^;]+/i', '', $tagstyle);
  19641. }
  19642. }
  19643. }
  19644. }
  19645. $tagstyle .= ';'.$style['c'];
  19646. }
  19647. // remove multiple semicolons
  19648. $tagstyle = preg_replace('/[;]+/', ';', $tagstyle);
  19649. return $tagstyle;
  19650. }
  19651. /**
  19652. * Returns the border width from CSS property
  19653. * @param $width (string) border width
  19654. * @return int with in user units
  19655. * @protected
  19656. * @since 5.7.000 (2010-08-02)
  19657. */
  19658. protected function getCSSBorderWidth($width) {
  19659. if ($width == 'thin') {
  19660. $width = (2 / $this->k);
  19661. } elseif ($width == 'medium') {
  19662. $width = (4 / $this->k);
  19663. } elseif ($width == 'thick') {
  19664. $width = (6 / $this->k);
  19665. } else {
  19666. $width = $this->getHTMLUnitToUnits($width, 1, 'px', false);
  19667. }
  19668. return $width;
  19669. }
  19670. /**
  19671. * Returns the border dash style from CSS property
  19672. * @param $style (string) border style to convert
  19673. * @return int sash style (return -1 in case of none or hidden border)
  19674. * @protected
  19675. * @since 5.7.000 (2010-08-02)
  19676. */
  19677. protected function getCSSBorderDashStyle($style) {
  19678. switch (strtolower($style)) {
  19679. case 'none':
  19680. case 'hidden': {
  19681. $dash = -1;
  19682. break;
  19683. }
  19684. case 'dotted': {
  19685. $dash = 1;
  19686. break;
  19687. }
  19688. case 'dashed': {
  19689. $dash = 3;
  19690. break;
  19691. }
  19692. case 'double':
  19693. case 'groove':
  19694. case 'ridge':
  19695. case 'inset':
  19696. case 'outset':
  19697. case 'solid':
  19698. default: {
  19699. $dash = 0;
  19700. break;
  19701. }
  19702. }
  19703. return $dash;
  19704. }
  19705. /**
  19706. * Returns the border style array from CSS border properties
  19707. * @param $cssborder (string) border properties
  19708. * @return array containing border properties
  19709. * @protected
  19710. * @since 5.7.000 (2010-08-02)
  19711. */
  19712. protected function getCSSBorderStyle($cssborder) {
  19713. $bprop = preg_split('/[\s]+/', trim($cssborder));
  19714. $border = array(); // value to be returned
  19715. switch (count($bprop)) {
  19716. case 3: {
  19717. $width = $bprop[0];
  19718. $style = $bprop[1];
  19719. $color = $bprop[2];
  19720. break;
  19721. }
  19722. case 2: {
  19723. $width = 'medium';
  19724. $style = $bprop[0];
  19725. $color = $bprop[1];
  19726. break;
  19727. }
  19728. case 1: {
  19729. $width = 'medium';
  19730. $style = $bprop[0];
  19731. $color = 'black';
  19732. break;
  19733. }
  19734. default: {
  19735. $width = 'medium';
  19736. $style = 'solid';
  19737. $color = 'black';
  19738. break;
  19739. }
  19740. }
  19741. if ($style == 'none') {
  19742. return array();
  19743. }
  19744. $border['cap'] = 'square';
  19745. $border['join'] = 'miter';
  19746. $border['dash'] = $this->getCSSBorderDashStyle($style);
  19747. if ($border['dash'] < 0) {
  19748. return array();
  19749. }
  19750. $border['width'] = $this->getCSSBorderWidth($width);
  19751. $border['color'] = $this->convertHTMLColorToDec($color);
  19752. return $border;
  19753. }
  19754. /**
  19755. * Get the internal Cell padding from CSS attribute.
  19756. * @param $csspadding (string) padding properties
  19757. * @param $width (float) width of the containing element
  19758. * @return array of cell paddings
  19759. * @public
  19760. * @since 5.9.000 (2010-10-04)
  19761. */
  19762. public function getCSSPadding($csspadding, $width=0) {
  19763. $padding = preg_split('/[\s]+/', trim($csspadding));
  19764. $cell_padding = array(); // value to be returned
  19765. switch (count($padding)) {
  19766. case 4: {
  19767. $cell_padding['T'] = $padding[0];
  19768. $cell_padding['R'] = $padding[1];
  19769. $cell_padding['B'] = $padding[2];
  19770. $cell_padding['L'] = $padding[3];
  19771. break;
  19772. }
  19773. case 3: {
  19774. $cell_padding['T'] = $padding[0];
  19775. $cell_padding['R'] = $padding[1];
  19776. $cell_padding['B'] = $padding[2];
  19777. $cell_padding['L'] = $padding[1];
  19778. break;
  19779. }
  19780. case 2: {
  19781. $cell_padding['T'] = $padding[0];
  19782. $cell_padding['R'] = $padding[1];
  19783. $cell_padding['B'] = $padding[0];
  19784. $cell_padding['L'] = $padding[1];
  19785. break;
  19786. }
  19787. case 1: {
  19788. $cell_padding['T'] = $padding[0];
  19789. $cell_padding['R'] = $padding[0];
  19790. $cell_padding['B'] = $padding[0];
  19791. $cell_padding['L'] = $padding[0];
  19792. break;
  19793. }
  19794. default: {
  19795. return $this->cell_padding;
  19796. }
  19797. }
  19798. if ($width == 0) {
  19799. $width = $this->w - $this->lMargin - $this->rMargin;
  19800. }
  19801. $cell_padding['T'] = $this->getHTMLUnitToUnits($cell_padding['T'], $width, 'px', false);
  19802. $cell_padding['R'] = $this->getHTMLUnitToUnits($cell_padding['R'], $width, 'px', false);
  19803. $cell_padding['B'] = $this->getHTMLUnitToUnits($cell_padding['B'], $width, 'px', false);
  19804. $cell_padding['L'] = $this->getHTMLUnitToUnits($cell_padding['L'], $width, 'px', false);
  19805. return $cell_padding;
  19806. }
  19807. /**
  19808. * Get the internal Cell margin from CSS attribute.
  19809. * @param $cssmargin (string) margin properties
  19810. * @param $width (float) width of the containing element
  19811. * @return array of cell margins
  19812. * @public
  19813. * @since 5.9.000 (2010-10-04)
  19814. */
  19815. public function getCSSMargin($cssmargin, $width=0) {
  19816. $margin = preg_split('/[\s]+/', trim($cssmargin));
  19817. $cell_margin = array(); // value to be returned
  19818. switch (count($margin)) {
  19819. case 4: {
  19820. $cell_margin['T'] = $margin[0];
  19821. $cell_margin['R'] = $margin[1];
  19822. $cell_margin['B'] = $margin[2];
  19823. $cell_margin['L'] = $margin[3];
  19824. break;
  19825. }
  19826. case 3: {
  19827. $cell_margin['T'] = $margin[0];
  19828. $cell_margin['R'] = $margin[1];
  19829. $cell_margin['B'] = $margin[2];
  19830. $cell_margin['L'] = $margin[1];
  19831. break;
  19832. }
  19833. case 2: {
  19834. $cell_margin['T'] = $margin[0];
  19835. $cell_margin['R'] = $margin[1];
  19836. $cell_margin['B'] = $margin[0];
  19837. $cell_margin['L'] = $margin[1];
  19838. break;
  19839. }
  19840. case 1: {
  19841. $cell_margin['T'] = $margin[0];
  19842. $cell_margin['R'] = $margin[0];
  19843. $cell_margin['B'] = $margin[0];
  19844. $cell_margin['L'] = $margin[0];
  19845. break;
  19846. }
  19847. default: {
  19848. return $this->cell_margin;
  19849. }
  19850. }
  19851. if ($width == 0) {
  19852. $width = $this->w - $this->lMargin - $this->rMargin;
  19853. }
  19854. $cell_margin['T'] = $this->getHTMLUnitToUnits(str_replace('auto', '0', $cell_margin['T']), $width, 'px', false);
  19855. $cell_margin['R'] = $this->getHTMLUnitToUnits(str_replace('auto', '0', $cell_margin['R']), $width, 'px', false);
  19856. $cell_margin['B'] = $this->getHTMLUnitToUnits(str_replace('auto', '0', $cell_margin['B']), $width, 'px', false);
  19857. $cell_margin['L'] = $this->getHTMLUnitToUnits(str_replace('auto', '0', $cell_margin['L']), $width, 'px', false);
  19858. return $cell_margin;
  19859. }
  19860. /**
  19861. * Get the border-spacing from CSS attribute.
  19862. * @param $cssbspace (string) border-spacing CSS properties
  19863. * @param $width (float) width of the containing element
  19864. * @return array of border spacings
  19865. * @public
  19866. * @since 5.9.010 (2010-10-27)
  19867. */
  19868. public function getCSSBorderMargin($cssbspace, $width=0) {
  19869. $space = preg_split('/[\s]+/', trim($cssbspace));
  19870. $border_spacing = array(); // value to be returned
  19871. switch (count($space)) {
  19872. case 2: {
  19873. $border_spacing['H'] = $space[0];
  19874. $border_spacing['V'] = $space[1];
  19875. break;
  19876. }
  19877. case 1: {
  19878. $border_spacing['H'] = $space[0];
  19879. $border_spacing['V'] = $space[0];
  19880. break;
  19881. }
  19882. default: {
  19883. return array('H' => 0, 'V' => 0);
  19884. }
  19885. }
  19886. if ($width == 0) {
  19887. $width = $this->w - $this->lMargin - $this->rMargin;
  19888. }
  19889. $border_spacing['H'] = $this->getHTMLUnitToUnits($border_spacing['H'], $width, 'px', false);
  19890. $border_spacing['V'] = $this->getHTMLUnitToUnits($border_spacing['V'], $width, 'px', false);
  19891. return $border_spacing;
  19892. }
  19893. /**
  19894. * Returns the letter-spacing value from CSS value
  19895. * @param $spacing (string) letter-spacing value
  19896. * @param $parent (float) font spacing (tracking/kerning) value of the parent element
  19897. * @return float quantity to increases or decreases the space between characters in a text.
  19898. * @protected
  19899. * @since 5.9.000 (2010-10-02)
  19900. */
  19901. protected function getCSSFontSpacing($spacing, $parent=0) {
  19902. $val = 0; // value to be returned
  19903. $spacing = trim($spacing);
  19904. switch ($spacing) {
  19905. case 'normal': {
  19906. $val = 0;
  19907. break;
  19908. }
  19909. case 'inherit': {
  19910. if ($parent == 'normal') {
  19911. $val = 0;
  19912. } else {
  19913. $val = $parent;
  19914. }
  19915. break;
  19916. }
  19917. default: {
  19918. $val = $this->getHTMLUnitToUnits($spacing, 0, 'px', false);
  19919. }
  19920. }
  19921. return $val;
  19922. }
  19923. /**
  19924. * Returns the percentage of font stretching from CSS value
  19925. * @param $stretch (string) stretch mode
  19926. * @param $parent (float) stretch value of the parent element
  19927. * @return float font stretching percentage
  19928. * @protected
  19929. * @since 5.9.000 (2010-10-02)
  19930. */
  19931. protected function getCSSFontStretching($stretch, $parent=100) {
  19932. $val = 100; // value to be returned
  19933. $stretch = trim($stretch);
  19934. switch ($stretch) {
  19935. case 'ultra-condensed': {
  19936. $val = 40;
  19937. break;
  19938. }
  19939. case 'extra-condensed': {
  19940. $val = 55;
  19941. break;
  19942. }
  19943. case 'condensed': {
  19944. $val = 70;
  19945. break;
  19946. }
  19947. case 'semi-condensed': {
  19948. $val = 85;
  19949. break;
  19950. }
  19951. case 'normal': {
  19952. $val = 100;
  19953. break;
  19954. }
  19955. case 'semi-expanded': {
  19956. $val = 115;
  19957. break;
  19958. }
  19959. case 'expanded': {
  19960. $val = 130;
  19961. break;
  19962. }
  19963. case 'extra-expanded': {
  19964. $val = 145;
  19965. break;
  19966. }
  19967. case 'ultra-expanded': {
  19968. $val = 160;
  19969. break;
  19970. }
  19971. case 'wider': {
  19972. $val = $parent + 10;
  19973. break;
  19974. }
  19975. case 'narrower': {
  19976. $val = $parent - 10;
  19977. break;
  19978. }
  19979. case 'inherit': {
  19980. if ($parent == 'normal') {
  19981. $val = 100;
  19982. } else {
  19983. $val = $parent;
  19984. }
  19985. break;
  19986. }
  19987. default: {
  19988. $val = $this->getHTMLUnitToUnits($stretch, 100, '%', false);
  19989. }
  19990. }
  19991. return $val;
  19992. }
  19993. /**
  19994. * Returns the HTML DOM array.
  19995. * @param $html (string) html code
  19996. * @return array
  19997. * @protected
  19998. * @since 3.2.000 (2008-06-20)
  19999. */
  20000. protected function getHtmlDomArray($html) {
  20001. // array of CSS styles ( selector => properties).
  20002. $css = array();
  20003. // get CSS array defined at previous call
  20004. $matches = array();
  20005. if (preg_match_all('/<cssarray>([^\<]*)<\/cssarray>/isU', $html, $matches) > 0) {
  20006. if (isset($matches[1][0])) {
  20007. $css = array_merge($css, unserialize($this->unhtmlentities($matches[1][0])));
  20008. }
  20009. $html = preg_replace('/<cssarray>(.*?)<\/cssarray>/isU', '', $html);
  20010. }
  20011. // extract external CSS files
  20012. $matches = array();
  20013. if (preg_match_all('/<link([^\>]*)>/isU', $html, $matches) > 0) {
  20014. foreach ($matches[1] as $key => $link) {
  20015. $type = array();
  20016. if (preg_match('/type[\s]*=[\s]*"text\/css"/', $link, $type)) {
  20017. $type = array();
  20018. preg_match('/media[\s]*=[\s]*"([^"]*)"/', $link, $type);
  20019. // get 'all' and 'print' media, other media types are discarded
  20020. // (all, braille, embossed, handheld, print, projection, screen, speech, tty, tv)
  20021. if (empty($type) OR (isset($type[1]) AND (($type[1] == 'all') OR ($type[1] == 'print')))) {
  20022. $type = array();
  20023. if (preg_match('/href[\s]*=[\s]*"([^"]*)"/', $link, $type) > 0) {
  20024. // read CSS data file
  20025. $cssdata = file_get_contents(trim($type[1]));
  20026. $css = array_merge($css, $this->extractCSSproperties($cssdata));
  20027. }
  20028. }
  20029. }
  20030. }
  20031. }
  20032. // extract style tags
  20033. $matches = array();
  20034. if (preg_match_all('/<style([^\>]*)>([^\<]*)<\/style>/isU', $html, $matches) > 0) {
  20035. foreach ($matches[1] as $key => $media) {
  20036. $type = array();
  20037. preg_match('/media[\s]*=[\s]*"([^"]*)"/', $media, $type);
  20038. // get 'all' and 'print' media, other media types are discarded
  20039. // (all, braille, embossed, handheld, print, projection, screen, speech, tty, tv)
  20040. if (empty($type) OR (isset($type[1]) AND (($type[1] == 'all') OR ($type[1] == 'print')))) {
  20041. $cssdata = $matches[2][$key];
  20042. $css = array_merge($css, $this->extractCSSproperties($cssdata));
  20043. }
  20044. }
  20045. }
  20046. // create a special tag to contain the CSS array (used for table content)
  20047. $csstagarray = '<cssarray>'.htmlentities(serialize($css)).'</cssarray>';
  20048. // remove head and style blocks
  20049. $html = preg_replace('/<head([^\>]*)>(.*?)<\/head>/siU', '', $html);
  20050. $html = preg_replace('/<style([^\>]*)>([^\<]*)<\/style>/isU', '', $html);
  20051. // define block tags
  20052. $blocktags = array('blockquote','br','dd','dl','div','dt','h1','h2','h3','h4','h5','h6','hr','li','ol','p','pre','ul','tcpdf','table','tr','td');
  20053. // define self-closing tags
  20054. $selfclosingtags = array('area','base','basefont','br','hr','input','img','link','meta');
  20055. // remove all unsupported tags (the line below lists all supported tags)
  20056. $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>');
  20057. //replace some blank characters
  20058. $html = preg_replace('/<pre/', '<xre', $html); // preserve pre tag
  20059. $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);
  20060. $html = preg_replace('@(\r\n|\r)@', "\n", $html);
  20061. $repTable = array("\t" => ' ', "\0" => ' ', "\x0B" => ' ', "\\" => "\\\\");
  20062. $html = strtr($html, $repTable);
  20063. $offset = 0;
  20064. while (($offset < strlen($html)) AND ($pos = strpos($html, '</pre>', $offset)) !== false) {
  20065. $html_a = substr($html, 0, $offset);
  20066. $html_b = substr($html, $offset, ($pos - $offset + 6));
  20067. while (preg_match("'<xre([^\>]*)>(.*?)\n(.*?)</pre>'si", $html_b)) {
  20068. // preserve newlines on <pre> tag
  20069. $html_b = preg_replace("'<xre([^\>]*)>(.*?)\n(.*?)</pre>'si", "<xre\\1>\\2<br />\\3</pre>", $html_b);
  20070. }
  20071. while (preg_match("'<xre([^\>]*)>(.*?)".$this->re_space['p']."(.*?)</pre>'".$this->re_space['m'], $html_b)) {
  20072. // preserve spaces on <pre> tag
  20073. $html_b = preg_replace("'<xre([^\>]*)>(.*?)".$this->re_space['p']."(.*?)</pre>'".$this->re_space['m'], "<xre\\1>\\2&nbsp;\\3</pre>", $html_b);
  20074. }
  20075. $html = $html_a.$html_b.substr($html, $pos + 6);
  20076. $offset = strlen($html_a.$html_b);
  20077. }
  20078. $offset = 0;
  20079. while (($offset < strlen($html)) AND ($pos = strpos($html, '</textarea>', $offset)) !== false) {
  20080. $html_a = substr($html, 0, $offset);
  20081. $html_b = substr($html, $offset, ($pos - $offset + 11));
  20082. while (preg_match("'<textarea([^\>]*)>(.*?)\n(.*?)</textarea>'si", $html_b)) {
  20083. // preserve newlines on <textarea> tag
  20084. $html_b = preg_replace("'<textarea([^\>]*)>(.*?)\n(.*?)</textarea>'si", "<textarea\\1>\\2<TBR>\\3</textarea>", $html_b);
  20085. $html_b = preg_replace("'<textarea([^\>]*)>(.*?)[\"](.*?)</textarea>'si", "<textarea\\1>\\2''\\3</textarea>", $html_b);
  20086. }
  20087. $html = $html_a.$html_b.substr($html, $pos + 11);
  20088. $offset = strlen($html_a.$html_b);
  20089. }
  20090. $html = preg_replace('/([\s]*)<option/si', '<option', $html);
  20091. $html = preg_replace('/<\/option>([\s]*)/si', '</option>', $html);
  20092. $offset = 0;
  20093. while (($offset < strlen($html)) AND ($pos = strpos($html, '</option>', $offset)) !== false) {
  20094. $html_a = substr($html, 0, $offset);
  20095. $html_b = substr($html, $offset, ($pos - $offset + 9));
  20096. while (preg_match("'<option([^\>]*)>(.*?)</option>'si", $html_b)) {
  20097. $html_b = preg_replace("'<option([\s]+)value=\"([^\"]*)\"([^\>]*)>(.*?)</option>'si", "\\2#!TaB!#\\4#!NwL!#", $html_b);
  20098. $html_b = preg_replace("'<option([^\>]*)>(.*?)</option>'si", "\\2#!NwL!#", $html_b);
  20099. }
  20100. $html = $html_a.$html_b.substr($html, $pos + 9);
  20101. $offset = strlen($html_a.$html_b);
  20102. }
  20103. if (preg_match("'</select'si", $html)) {
  20104. $html = preg_replace("'<select([^\>]*)>'si", "<select\\1 opt=\"", $html);
  20105. $html = preg_replace("'#!NwL!#</select>'si", "\" />", $html);
  20106. }
  20107. $html = str_replace("\n", ' ', $html);
  20108. // restore textarea newlines
  20109. $html = str_replace('<TBR>', "\n", $html);
  20110. // remove extra spaces from code
  20111. $html = preg_replace('/[\s]+<\/(table|tr|ul|ol|dl)>/', '</\\1>', $html);
  20112. $html = preg_replace('/'.$this->re_space['p'].'+<\/(td|th|li|dt|dd)>/'.$this->re_space['m'], '</\\1>', $html);
  20113. $html = preg_replace('/[\s]+<(tr|td|th|li|dt|dd)/', '<\\1', $html);
  20114. $html = preg_replace('/'.$this->re_space['p'].'+<(ul|ol|dl|br)/'.$this->re_space['m'], '<\\1', $html);
  20115. $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);
  20116. $html = preg_replace('/<\/(td|th)>/', '<marker style="font-size:0"/></\\1>', $html);
  20117. $html = preg_replace('/<\/table>([\s]*)<marker style="font-size:0"\/>/', '</table>', $html);
  20118. $html = preg_replace('/'.$this->re_space['p'].'+<img/'.$this->re_space['m'], chr(32).'<img', $html);
  20119. $html = preg_replace('/<img([^\>]*)>[\s]+([^\<])/xi', '<img\\1>&nbsp;\\2', $html);
  20120. $html = preg_replace('/<img([^\>]*)>/xi', '<img\\1><span><marker style="font-size:0"/></span>', $html);
  20121. $html = preg_replace('/<xre/', '<pre', $html); // restore pre tag
  20122. $html = preg_replace('/<textarea([^\>]*)>([^\<]*)<\/textarea>/xi', '<textarea\\1 value="\\2" />', $html);
  20123. $html = preg_replace('/<li([^\>]*)><\/li>/', '<li\\1>&nbsp;</li>', $html);
  20124. $html = preg_replace('/<li([^\>]*)>'.$this->re_space['p'].'*<img/'.$this->re_space['m'], '<li\\1><font size="1">&nbsp;</font><img', $html);
  20125. $html = preg_replace('/<([^\>\/]*)>[\s]/', '<\\1>&nbsp;', $html); // preserve some spaces
  20126. $html = preg_replace('/[\s]<\/([^\>]*)>/', '&nbsp;</\\1>', $html); // preserve some spaces
  20127. $html = preg_replace('/'.$this->re_space['p'].'+/'.$this->re_space['m'], chr(32), $html); // replace multiple spaces with a single space
  20128. // trim string
  20129. $html = $this->stringTrim($html);
  20130. // fix first image tag alignment
  20131. $html = preg_replace('/^<img/', '<span style="font-size:0"><br /></span> <img', $html, 1);
  20132. // pattern for generic tag
  20133. $tagpattern = '/(<[^>]+>)/';
  20134. // explodes the string
  20135. $a = preg_split($tagpattern, $html, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
  20136. // count elements
  20137. $maxel = count($a);
  20138. $elkey = 0;
  20139. $key = 0;
  20140. // create an array of elements
  20141. $dom = array();
  20142. $dom[$key] = array();
  20143. // set inheritable properties fot the first void element
  20144. // 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
  20145. $dom[$key]['tag'] = false;
  20146. $dom[$key]['block'] = false;
  20147. $dom[$key]['value'] = '';
  20148. $dom[$key]['parent'] = 0;
  20149. $dom[$key]['hide'] = false;
  20150. $dom[$key]['fontname'] = $this->FontFamily;
  20151. $dom[$key]['fontstyle'] = $this->FontStyle;
  20152. $dom[$key]['fontsize'] = $this->FontSizePt;
  20153. $dom[$key]['font-stretch'] = $this->font_stretching;
  20154. $dom[$key]['letter-spacing'] = $this->font_spacing;
  20155. $dom[$key]['stroke'] = $this->textstrokewidth;
  20156. $dom[$key]['fill'] = (($this->textrendermode % 2) == 0);
  20157. $dom[$key]['clip'] = ($this->textrendermode > 3);
  20158. $dom[$key]['line-height'] = $this->cell_height_ratio;
  20159. $dom[$key]['bgcolor'] = false;
  20160. $dom[$key]['fgcolor'] = $this->fgcolor; // color
  20161. $dom[$key]['strokecolor'] = $this->strokecolor;
  20162. $dom[$key]['align'] = '';
  20163. $dom[$key]['listtype'] = '';
  20164. $dom[$key]['text-indent'] = 0;
  20165. $dom[$key]['border'] = array();
  20166. $dom[$key]['dir'] = $this->rtl?'rtl':'ltr';
  20167. $thead = false; // true when we are inside the THEAD tag
  20168. ++$key;
  20169. $level = array();
  20170. array_push($level, 0); // root
  20171. while ($elkey < $maxel) {
  20172. $dom[$key] = array();
  20173. $element = $a[$elkey];
  20174. $dom[$key]['elkey'] = $elkey;
  20175. if (preg_match($tagpattern, $element)) {
  20176. // html tag
  20177. $element = substr($element, 1, -1);
  20178. // get tag name
  20179. preg_match('/[\/]?([a-zA-Z0-9]*)/', $element, $tag);
  20180. $tagname = strtolower($tag[1]);
  20181. // check if we are inside a table header
  20182. if ($tagname == 'thead') {
  20183. if ($element{0} == '/') {
  20184. $thead = false;
  20185. } else {
  20186. $thead = true;
  20187. }
  20188. ++$elkey;
  20189. continue;
  20190. }
  20191. $dom[$key]['tag'] = true;
  20192. $dom[$key]['value'] = $tagname;
  20193. if (in_array($dom[$key]['value'], $blocktags)) {
  20194. $dom[$key]['block'] = true;
  20195. } else {
  20196. $dom[$key]['block'] = false;
  20197. }
  20198. if ($element{0} == '/') {
  20199. // *** closing html tag
  20200. $dom[$key]['opening'] = false;
  20201. $dom[$key]['parent'] = end($level);
  20202. array_pop($level);
  20203. $dom[$key]['hide'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['hide'];
  20204. $dom[$key]['fontname'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['fontname'];
  20205. $dom[$key]['fontstyle'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['fontstyle'];
  20206. $dom[$key]['fontsize'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['fontsize'];
  20207. $dom[$key]['font-stretch'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['font-stretch'];
  20208. $dom[$key]['letter-spacing'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['letter-spacing'];
  20209. $dom[$key]['stroke'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['stroke'];
  20210. $dom[$key]['fill'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['fill'];
  20211. $dom[$key]['clip'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['clip'];
  20212. $dom[$key]['line-height'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['line-height'];
  20213. $dom[$key]['bgcolor'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['bgcolor'];
  20214. $dom[$key]['fgcolor'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['fgcolor'];
  20215. $dom[$key]['strokecolor'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['strokecolor'];
  20216. $dom[$key]['align'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['align'];
  20217. $dom[$key]['dir'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['dir'];
  20218. if (isset($dom[($dom[($dom[$key]['parent'])]['parent'])]['listtype'])) {
  20219. $dom[$key]['listtype'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['listtype'];
  20220. }
  20221. // set the number of columns in table tag
  20222. if (($dom[$key]['value'] == 'tr') AND (!isset($dom[($dom[($dom[$key]['parent'])]['parent'])]['cols']))) {
  20223. $dom[($dom[($dom[$key]['parent'])]['parent'])]['cols'] = $dom[($dom[$key]['parent'])]['cols'];
  20224. }
  20225. if (($dom[$key]['value'] == 'td') OR ($dom[$key]['value'] == 'th')) {
  20226. $dom[($dom[$key]['parent'])]['content'] = $csstagarray;
  20227. for ($i = ($dom[$key]['parent'] + 1); $i < $key; ++$i) {
  20228. $dom[($dom[$key]['parent'])]['content'] .= $a[$dom[$i]['elkey']];
  20229. }
  20230. $key = $i;
  20231. // mark nested tables
  20232. $dom[($dom[$key]['parent'])]['content'] = str_replace('<table', '<table nested="true"', $dom[($dom[$key]['parent'])]['content']);
  20233. // remove thead sections from nested tables
  20234. $dom[($dom[$key]['parent'])]['content'] = str_replace('<thead>', '', $dom[($dom[$key]['parent'])]['content']);
  20235. $dom[($dom[$key]['parent'])]['content'] = str_replace('</thead>', '', $dom[($dom[$key]['parent'])]['content']);
  20236. }
  20237. // store header rows on a new table
  20238. if (($dom[$key]['value'] == 'tr') AND ($dom[($dom[$key]['parent'])]['thead'] === true)) {
  20239. if ($this->empty_string($dom[($dom[($dom[$key]['parent'])]['parent'])]['thead'])) {
  20240. $dom[($dom[($dom[$key]['parent'])]['parent'])]['thead'] = $csstagarray.$a[$dom[($dom[($dom[$key]['parent'])]['parent'])]['elkey']];
  20241. }
  20242. for ($i = $dom[$key]['parent']; $i <= $key; ++$i) {
  20243. $dom[($dom[($dom[$key]['parent'])]['parent'])]['thead'] .= $a[$dom[$i]['elkey']];
  20244. }
  20245. if (!isset($dom[($dom[$key]['parent'])]['attribute'])) {
  20246. $dom[($dom[$key]['parent'])]['attribute'] = array();
  20247. }
  20248. // header elements must be always contained in a single page
  20249. $dom[($dom[$key]['parent'])]['attribute']['nobr'] = 'true';
  20250. }
  20251. if (($dom[$key]['value'] == 'table') AND (!$this->empty_string($dom[($dom[$key]['parent'])]['thead']))) {
  20252. // remove the nobr attributes from the table header
  20253. $dom[($dom[$key]['parent'])]['thead'] = str_replace(' nobr="true"', '', $dom[($dom[$key]['parent'])]['thead']);
  20254. $dom[($dom[$key]['parent'])]['thead'] .= '</tablehead>';
  20255. }
  20256. } else {
  20257. // *** opening or self-closing html tag
  20258. $dom[$key]['opening'] = true;
  20259. $dom[$key]['parent'] = end($level);
  20260. if ((substr($element, -1, 1) == '/') OR (in_array($dom[$key]['value'], $selfclosingtags))) {
  20261. // self-closing tag
  20262. $dom[$key]['self'] = true;
  20263. } else {
  20264. // opening tag
  20265. array_push($level, $key);
  20266. $dom[$key]['self'] = false;
  20267. }
  20268. // copy some values from parent
  20269. $parentkey = 0;
  20270. if ($key > 0) {
  20271. $parentkey = $dom[$key]['parent'];
  20272. $dom[$key]['hide'] = $dom[$parentkey]['hide'];
  20273. $dom[$key]['fontname'] = $dom[$parentkey]['fontname'];
  20274. $dom[$key]['fontstyle'] = $dom[$parentkey]['fontstyle'];
  20275. $dom[$key]['fontsize'] = $dom[$parentkey]['fontsize'];
  20276. $dom[$key]['font-stretch'] = $dom[$parentkey]['font-stretch'];
  20277. $dom[$key]['letter-spacing'] = $dom[$parentkey]['letter-spacing'];
  20278. $dom[$key]['stroke'] = $dom[$parentkey]['stroke'];
  20279. $dom[$key]['fill'] = $dom[$parentkey]['fill'];
  20280. $dom[$key]['clip'] = $dom[$parentkey]['clip'];
  20281. $dom[$key]['line-height'] = $dom[$parentkey]['line-height'];
  20282. $dom[$key]['bgcolor'] = $dom[$parentkey]['bgcolor'];
  20283. $dom[$key]['fgcolor'] = $dom[$parentkey]['fgcolor'];
  20284. $dom[$key]['strokecolor'] = $dom[$parentkey]['strokecolor'];
  20285. $dom[$key]['align'] = $dom[$parentkey]['align'];
  20286. $dom[$key]['listtype'] = $dom[$parentkey]['listtype'];
  20287. $dom[$key]['text-indent'] = $dom[$parentkey]['text-indent'];
  20288. $dom[$key]['border'] = array();
  20289. $dom[$key]['dir'] = $dom[$parentkey]['dir'];
  20290. }
  20291. // get attributes
  20292. preg_match_all('/([^=\s]*)[\s]*=[\s]*"([^"]*)"/', $element, $attr_array, PREG_PATTERN_ORDER);
  20293. $dom[$key]['attribute'] = array(); // reset attribute array
  20294. while (list($id, $name) = each($attr_array[1])) {
  20295. $dom[$key]['attribute'][strtolower($name)] = $attr_array[2][$id];
  20296. }
  20297. if (!empty($css)) {
  20298. // merge CSS style to current style
  20299. list($dom[$key]['csssel'], $dom[$key]['cssdata']) = $this->getCSSdataArray($dom, $key, $css);
  20300. $dom[$key]['attribute']['style'] = $this->getTagStyleFromCSSarray($dom[$key]['cssdata']);
  20301. }
  20302. // split style attributes
  20303. if (isset($dom[$key]['attribute']['style']) AND !empty($dom[$key]['attribute']['style'])) {
  20304. // get style attributes
  20305. preg_match_all('/([^;:\s]*):([^;]*)/', $dom[$key]['attribute']['style'], $style_array, PREG_PATTERN_ORDER);
  20306. $dom[$key]['style'] = array(); // reset style attribute array
  20307. while (list($id, $name) = each($style_array[1])) {
  20308. // in case of duplicate attribute the last replace the previous
  20309. $dom[$key]['style'][strtolower($name)] = trim($style_array[2][$id]);
  20310. }
  20311. // --- get some style attributes ---
  20312. // text direction
  20313. if (isset($dom[$key]['style']['direction'])) {
  20314. $dom[$key]['dir'] = $dom[$key]['style']['direction'];
  20315. }
  20316. // display
  20317. if (isset($dom[$key]['style']['display'])) {
  20318. $dom[$key]['hide'] = (trim(strtolower($dom[$key]['style']['display'])) == 'none');
  20319. }
  20320. // font family
  20321. if (isset($dom[$key]['style']['font-family'])) {
  20322. $dom[$key]['fontname'] = $this->getFontFamilyName($dom[$key]['style']['font-family']);
  20323. }
  20324. // list-style-type
  20325. if (isset($dom[$key]['style']['list-style-type'])) {
  20326. $dom[$key]['listtype'] = trim(strtolower($dom[$key]['style']['list-style-type']));
  20327. if ($dom[$key]['listtype'] == 'inherit') {
  20328. $dom[$key]['listtype'] = $dom[$parentkey]['listtype'];
  20329. }
  20330. }
  20331. // text-indent
  20332. if (isset($dom[$key]['style']['text-indent'])) {
  20333. $dom[$key]['text-indent'] = $this->getHTMLUnitToUnits($dom[$key]['style']['text-indent']);
  20334. if ($dom[$key]['text-indent'] == 'inherit') {
  20335. $dom[$key]['text-indent'] = $dom[$parentkey]['text-indent'];
  20336. }
  20337. }
  20338. // font size
  20339. if (isset($dom[$key]['style']['font-size'])) {
  20340. $fsize = trim($dom[$key]['style']['font-size']);
  20341. switch ($fsize) {
  20342. // absolute-size
  20343. case 'xx-small': {
  20344. $dom[$key]['fontsize'] = $dom[0]['fontsize'] - 4;
  20345. break;
  20346. }
  20347. case 'x-small': {
  20348. $dom[$key]['fontsize'] = $dom[0]['fontsize'] - 3;
  20349. break;
  20350. }
  20351. case 'small': {
  20352. $dom[$key]['fontsize'] = $dom[0]['fontsize'] - 2;
  20353. break;
  20354. }
  20355. case 'medium': {
  20356. $dom[$key]['fontsize'] = $dom[0]['fontsize'];
  20357. break;
  20358. }
  20359. case 'large': {
  20360. $dom[$key]['fontsize'] = $dom[0]['fontsize'] + 2;
  20361. break;
  20362. }
  20363. case 'x-large': {
  20364. $dom[$key]['fontsize'] = $dom[0]['fontsize'] + 4;
  20365. break;
  20366. }
  20367. case 'xx-large': {
  20368. $dom[$key]['fontsize'] = $dom[0]['fontsize'] + 6;
  20369. break;
  20370. }
  20371. // relative-size
  20372. case 'smaller': {
  20373. $dom[$key]['fontsize'] = $dom[$parentkey]['fontsize'] - 3;
  20374. break;
  20375. }
  20376. case 'larger': {
  20377. $dom[$key]['fontsize'] = $dom[$parentkey]['fontsize'] + 3;
  20378. break;
  20379. }
  20380. default: {
  20381. $dom[$key]['fontsize'] = $this->getHTMLUnitToUnits($fsize, $dom[$parentkey]['fontsize'], 'pt', true);
  20382. }
  20383. }
  20384. }
  20385. // font-stretch
  20386. if (isset($dom[$key]['style']['font-stretch'])) {
  20387. $dom[$key]['font-stretch'] = $this->getCSSFontStretching($dom[$key]['style']['font-stretch'], $dom[$parentkey]['font-stretch']);
  20388. }
  20389. // letter-spacing
  20390. if (isset($dom[$key]['style']['letter-spacing'])) {
  20391. $dom[$key]['letter-spacing'] = $this->getCSSFontSpacing($dom[$key]['style']['letter-spacing'], $dom[$parentkey]['letter-spacing']);
  20392. }
  20393. // line-height
  20394. if (isset($dom[$key]['style']['line-height'])) {
  20395. $lineheight = trim($dom[$key]['style']['line-height']);
  20396. switch ($lineheight) {
  20397. // A normal line height. This is default
  20398. case 'normal': {
  20399. $dom[$key]['line-height'] = $dom[0]['line-height'];
  20400. break;
  20401. }
  20402. default: {
  20403. if (is_numeric($lineheight)) {
  20404. $lineheight = $lineheight * 100;
  20405. }
  20406. $dom[$key]['line-height'] = $this->getHTMLUnitToUnits($lineheight, 1, '%', true);
  20407. }
  20408. }
  20409. }
  20410. // font style
  20411. if (isset($dom[$key]['style']['font-weight'])) {
  20412. if (strtolower($dom[$key]['style']['font-weight']{0}) == 'n') {
  20413. if (strpos($dom[$key]['fontstyle'], 'B') !== false) {
  20414. $dom[$key]['fontstyle'] = str_replace('B', '', $dom[$key]['fontstyle']);
  20415. }
  20416. } elseif (strtolower($dom[$key]['style']['font-weight']{0}) == 'b') {
  20417. $dom[$key]['fontstyle'] .= 'B';
  20418. }
  20419. }
  20420. if (isset($dom[$key]['style']['font-style']) AND (strtolower($dom[$key]['style']['font-style']{0}) == 'i')) {
  20421. $dom[$key]['fontstyle'] .= 'I';
  20422. }
  20423. // font color
  20424. if (isset($dom[$key]['style']['color']) AND (!$this->empty_string($dom[$key]['style']['color']))) {
  20425. $dom[$key]['fgcolor'] = $this->convertHTMLColorToDec($dom[$key]['style']['color']);
  20426. } elseif ($dom[$key]['value'] == 'a') {
  20427. $dom[$key]['fgcolor'] = $this->htmlLinkColorArray;
  20428. }
  20429. // background color
  20430. if (isset($dom[$key]['style']['background-color']) AND (!$this->empty_string($dom[$key]['style']['background-color']))) {
  20431. $dom[$key]['bgcolor'] = $this->convertHTMLColorToDec($dom[$key]['style']['background-color']);
  20432. }
  20433. // text-decoration
  20434. if (isset($dom[$key]['style']['text-decoration'])) {
  20435. $decors = explode(' ', strtolower($dom[$key]['style']['text-decoration']));
  20436. foreach ($decors as $dec) {
  20437. $dec = trim($dec);
  20438. if (!$this->empty_string($dec)) {
  20439. if ($dec{0} == 'u') {
  20440. // underline
  20441. $dom[$key]['fontstyle'] .= 'U';
  20442. } elseif ($dec{0} == 'l') {
  20443. // line-trough
  20444. $dom[$key]['fontstyle'] .= 'D';
  20445. } elseif ($dec{0} == 'o') {
  20446. // overline
  20447. $dom[$key]['fontstyle'] .= 'O';
  20448. }
  20449. }
  20450. }
  20451. } elseif ($dom[$key]['value'] == 'a') {
  20452. $dom[$key]['fontstyle'] = $this->htmlLinkFontStyle;
  20453. }
  20454. // check for width attribute
  20455. if (isset($dom[$key]['style']['width'])) {
  20456. $dom[$key]['width'] = $dom[$key]['style']['width'];
  20457. }
  20458. // check for height attribute
  20459. if (isset($dom[$key]['style']['height'])) {
  20460. $dom[$key]['height'] = $dom[$key]['style']['height'];
  20461. }
  20462. // check for text alignment
  20463. if (isset($dom[$key]['style']['text-align'])) {
  20464. $dom[$key]['align'] = strtoupper($dom[$key]['style']['text-align']{0});
  20465. }
  20466. // check for CSS border properties
  20467. if (isset($dom[$key]['style']['border'])) {
  20468. $borderstyle = $this->getCSSBorderStyle($dom[$key]['style']['border']);
  20469. if (!empty($borderstyle)) {
  20470. $dom[$key]['border']['LTRB'] = $borderstyle;
  20471. }
  20472. }
  20473. if (isset($dom[$key]['style']['border-color'])) {
  20474. $brd_colors = preg_split('/[\s]+/', trim($dom[$key]['style']['border-color']));
  20475. if (isset($brd_colors[3])) {
  20476. $dom[$key]['border']['L']['color'] = $this->convertHTMLColorToDec($brd_colors[3]);
  20477. }
  20478. if (isset($brd_colors[1])) {
  20479. $dom[$key]['border']['R']['color'] = $this->convertHTMLColorToDec($brd_colors[1]);
  20480. }
  20481. if (isset($brd_colors[0])) {
  20482. $dom[$key]['border']['T']['color'] = $this->convertHTMLColorToDec($brd_colors[0]);
  20483. }
  20484. if (isset($brd_colors[2])) {
  20485. $dom[$key]['border']['B']['color'] = $this->convertHTMLColorToDec($brd_colors[2]);
  20486. }
  20487. }
  20488. if (isset($dom[$key]['style']['border-width'])) {
  20489. $brd_widths = preg_split('/[\s]+/', trim($dom[$key]['style']['border-width']));
  20490. if (isset($brd_widths[3])) {
  20491. $dom[$key]['border']['L']['width'] = $this->getCSSBorderWidth($brd_widths[3]);
  20492. }
  20493. if (isset($brd_widths[1])) {
  20494. $dom[$key]['border']['R']['width'] = $this->getCSSBorderWidth($brd_widths[1]);
  20495. }
  20496. if (isset($brd_widths[0])) {
  20497. $dom[$key]['border']['T']['width'] = $this->getCSSBorderWidth($brd_widths[0]);
  20498. }
  20499. if (isset($brd_widths[2])) {
  20500. $dom[$key]['border']['B']['width'] = $this->getCSSBorderWidth($brd_widths[2]);
  20501. }
  20502. }
  20503. if (isset($dom[$key]['style']['border-style'])) {
  20504. $brd_styles = preg_split('/[\s]+/', trim($dom[$key]['style']['border-style']));
  20505. if (isset($brd_styles[3])) {
  20506. $dom[$key]['border']['L']['cap'] = 'square';
  20507. $dom[$key]['border']['L']['join'] = 'miter';
  20508. $dom[$key]['border']['L']['dash'] = $this->getCSSBorderDashStyle($brd_styles[3]);
  20509. if ($dom[$key]['border']['L']['dash'] < 0) {
  20510. $dom[$key]['border']['L'] = array();
  20511. }
  20512. }
  20513. if (isset($brd_styles[1])) {
  20514. $dom[$key]['border']['R']['cap'] = 'square';
  20515. $dom[$key]['border']['R']['join'] = 'miter';
  20516. $dom[$key]['border']['R']['dash'] = $this->getCSSBorderDashStyle($brd_styles[1]);
  20517. if ($dom[$key]['border']['R']['dash'] < 0) {
  20518. $dom[$key]['border']['R'] = array();
  20519. }
  20520. }
  20521. if (isset($brd_styles[0])) {
  20522. $dom[$key]['border']['T']['cap'] = 'square';
  20523. $dom[$key]['border']['T']['join'] = 'miter';
  20524. $dom[$key]['border']['T']['dash'] = $this->getCSSBorderDashStyle($brd_styles[0]);
  20525. if ($dom[$key]['border']['T']['dash'] < 0) {
  20526. $dom[$key]['border']['T'] = array();
  20527. }
  20528. }
  20529. if (isset($brd_styles[2])) {
  20530. $dom[$key]['border']['B']['cap'] = 'square';
  20531. $dom[$key]['border']['B']['join'] = 'miter';
  20532. $dom[$key]['border']['B']['dash'] = $this->getCSSBorderDashStyle($brd_styles[2]);
  20533. if ($dom[$key]['border']['B']['dash'] < 0) {
  20534. $dom[$key]['border']['B'] = array();
  20535. }
  20536. }
  20537. }
  20538. $cellside = array('L' => 'left', 'R' => 'right', 'T' => 'top', 'B' => 'bottom');
  20539. foreach ($cellside as $bsk => $bsv) {
  20540. if (isset($dom[$key]['style']['border-'.$bsv])) {
  20541. $borderstyle = $this->getCSSBorderStyle($dom[$key]['style']['border-'.$bsv]);
  20542. if (!empty($borderstyle)) {
  20543. $dom[$key]['border'][$bsk] = $borderstyle;
  20544. }
  20545. }
  20546. if (isset($dom[$key]['style']['border-'.$bsv.'-color'])) {
  20547. $dom[$key]['border'][$bsk]['color'] = $this->convertHTMLColorToDec($dom[$key]['style']['border-'.$bsv.'-color']);
  20548. }
  20549. if (isset($dom[$key]['style']['border-'.$bsv.'-width'])) {
  20550. $dom[$key]['border'][$bsk]['width'] = $this->getCSSBorderWidth($dom[$key]['style']['border-'.$bsv.'-width']);
  20551. }
  20552. if (isset($dom[$key]['style']['border-'.$bsv.'-style'])) {
  20553. $dom[$key]['border'][$bsk]['dash'] = $this->getCSSBorderDashStyle($dom[$key]['style']['border-'.$bsv.'-style']);
  20554. if ($dom[$key]['border'][$bsk]['dash'] < 0) {
  20555. $dom[$key]['border'][$bsk] = array();
  20556. }
  20557. }
  20558. }
  20559. // check for CSS padding properties
  20560. if (isset($dom[$key]['style']['padding'])) {
  20561. $dom[$key]['padding'] = $this->getCSSPadding($dom[$key]['style']['padding']);
  20562. } else {
  20563. $dom[$key]['padding'] = $this->cell_padding;
  20564. }
  20565. foreach ($cellside as $psk => $psv) {
  20566. if (isset($dom[$key]['style']['padding-'.$psv])) {
  20567. $dom[$key]['padding'][$psk] = $this->getHTMLUnitToUnits($dom[$key]['style']['padding-'.$psv], 0, 'px', false);
  20568. }
  20569. }
  20570. // check for CSS margin properties
  20571. if (isset($dom[$key]['style']['margin'])) {
  20572. $dom[$key]['margin'] = $this->getCSSMargin($dom[$key]['style']['margin']);
  20573. } else {
  20574. $dom[$key]['margin'] = $this->cell_margin;
  20575. }
  20576. foreach ($cellside as $psk => $psv) {
  20577. if (isset($dom[$key]['style']['margin-'.$psv])) {
  20578. $dom[$key]['margin'][$psk] = $this->getHTMLUnitToUnits(str_replace('auto', '0', $dom[$key]['style']['margin-'.$psv]), 0, 'px', false);
  20579. }
  20580. }
  20581. // check for CSS border-spacing properties
  20582. if (isset($dom[$key]['style']['border-spacing'])) {
  20583. $dom[$key]['border-spacing'] = $this->getCSSBorderMargin($dom[$key]['style']['border-spacing']);
  20584. }
  20585. // page-break-inside
  20586. if (isset($dom[$key]['style']['page-break-inside']) AND ($dom[$key]['style']['page-break-inside'] == 'avoid')) {
  20587. $dom[$key]['attribute']['nobr'] = 'true';
  20588. }
  20589. // page-break-before
  20590. if (isset($dom[$key]['style']['page-break-before'])) {
  20591. if ($dom[$key]['style']['page-break-before'] == 'always') {
  20592. $dom[$key]['attribute']['pagebreak'] = 'true';
  20593. } elseif ($dom[$key]['style']['page-break-before'] == 'left') {
  20594. $dom[$key]['attribute']['pagebreak'] = 'left';
  20595. } elseif ($dom[$key]['style']['page-break-before'] == 'right') {
  20596. $dom[$key]['attribute']['pagebreak'] = 'right';
  20597. }
  20598. }
  20599. // page-break-after
  20600. if (isset($dom[$key]['style']['page-break-after'])) {
  20601. if ($dom[$key]['style']['page-break-after'] == 'always') {
  20602. $dom[$key]['attribute']['pagebreakafter'] = 'true';
  20603. } elseif ($dom[$key]['style']['page-break-after'] == 'left') {
  20604. $dom[$key]['attribute']['pagebreakafter'] = 'left';
  20605. } elseif ($dom[$key]['style']['page-break-after'] == 'right') {
  20606. $dom[$key]['attribute']['pagebreakafter'] = 'right';
  20607. }
  20608. }
  20609. }
  20610. if (isset($dom[$key]['attribute']['display'])) {
  20611. $dom[$key]['hide'] = (trim(strtolower($dom[$key]['attribute']['display'])) == 'none');
  20612. }
  20613. if (isset($dom[$key]['attribute']['border']) AND ($dom[$key]['attribute']['border'] != 0)) {
  20614. $borderstyle = $this->getCSSBorderStyle($dom[$key]['attribute']['border'].' solid black');
  20615. if (!empty($borderstyle)) {
  20616. $dom[$key]['border']['LTRB'] = $borderstyle;
  20617. }
  20618. }
  20619. // check for font tag
  20620. if ($dom[$key]['value'] == 'font') {
  20621. // font family
  20622. if (isset($dom[$key]['attribute']['face'])) {
  20623. $dom[$key]['fontname'] = $this->getFontFamilyName($dom[$key]['attribute']['face']);
  20624. }
  20625. // font size
  20626. if (isset($dom[$key]['attribute']['size'])) {
  20627. if ($key > 0) {
  20628. if ($dom[$key]['attribute']['size']{0} == '+') {
  20629. $dom[$key]['fontsize'] = $dom[($dom[$key]['parent'])]['fontsize'] + intval(substr($dom[$key]['attribute']['size'], 1));
  20630. } elseif ($dom[$key]['attribute']['size']{0} == '-') {
  20631. $dom[$key]['fontsize'] = $dom[($dom[$key]['parent'])]['fontsize'] - intval(substr($dom[$key]['attribute']['size'], 1));
  20632. } else {
  20633. $dom[$key]['fontsize'] = intval($dom[$key]['attribute']['size']);
  20634. }
  20635. } else {
  20636. $dom[$key]['fontsize'] = intval($dom[$key]['attribute']['size']);
  20637. }
  20638. }
  20639. }
  20640. // force natural alignment for lists
  20641. if ((($dom[$key]['value'] == 'ul') OR ($dom[$key]['value'] == 'ol') OR ($dom[$key]['value'] == 'dl'))
  20642. AND (!isset($dom[$key]['align']) OR $this->empty_string($dom[$key]['align']) OR ($dom[$key]['align'] != 'J'))) {
  20643. if ($this->rtl) {
  20644. $dom[$key]['align'] = 'R';
  20645. } else {
  20646. $dom[$key]['align'] = 'L';
  20647. }
  20648. }
  20649. if (($dom[$key]['value'] == 'small') OR ($dom[$key]['value'] == 'sup') OR ($dom[$key]['value'] == 'sub')) {
  20650. if (!isset($dom[$key]['attribute']['size']) AND !isset($dom[$key]['style']['font-size'])) {
  20651. $dom[$key]['fontsize'] = $dom[$key]['fontsize'] * K_SMALL_RATIO;
  20652. }
  20653. }
  20654. if (($dom[$key]['value'] == 'strong') OR ($dom[$key]['value'] == 'b')) {
  20655. $dom[$key]['fontstyle'] .= 'B';
  20656. }
  20657. if (($dom[$key]['value'] == 'em') OR ($dom[$key]['value'] == 'i')) {
  20658. $dom[$key]['fontstyle'] .= 'I';
  20659. }
  20660. if ($dom[$key]['value'] == 'u') {
  20661. $dom[$key]['fontstyle'] .= 'U';
  20662. }
  20663. if (($dom[$key]['value'] == 'del') OR ($dom[$key]['value'] == 's') OR ($dom[$key]['value'] == 'strike')) {
  20664. $dom[$key]['fontstyle'] .= 'D';
  20665. }
  20666. if (!isset($dom[$key]['style']['text-decoration']) AND ($dom[$key]['value'] == 'a')) {
  20667. $dom[$key]['fontstyle'] = $this->htmlLinkFontStyle;
  20668. }
  20669. if (($dom[$key]['value'] == 'pre') OR ($dom[$key]['value'] == 'tt')) {
  20670. $dom[$key]['fontname'] = $this->default_monospaced_font;
  20671. }
  20672. if (($dom[$key]['value']{0} == 'h') AND (intval($dom[$key]['value']{1}) > 0) AND (intval($dom[$key]['value']{1}) < 7)) {
  20673. // headings h1, h2, h3, h4, h5, h6
  20674. if (!isset($dom[$key]['attribute']['size']) AND !isset($dom[$key]['style']['font-size'])) {
  20675. $headsize = (4 - intval($dom[$key]['value']{1})) * 2;
  20676. $dom[$key]['fontsize'] = $dom[0]['fontsize'] + $headsize;
  20677. }
  20678. if (!isset($dom[$key]['style']['font-weight'])) {
  20679. $dom[$key]['fontstyle'] .= 'B';
  20680. }
  20681. }
  20682. if (($dom[$key]['value'] == 'table')) {
  20683. $dom[$key]['rows'] = 0; // number of rows
  20684. $dom[$key]['trids'] = array(); // IDs of TR elements
  20685. $dom[$key]['thead'] = ''; // table header rows
  20686. }
  20687. if (($dom[$key]['value'] == 'tr')) {
  20688. $dom[$key]['cols'] = 0;
  20689. if ($thead) {
  20690. $dom[$key]['thead'] = true;
  20691. // rows on thead block are printed as a separate table
  20692. } else {
  20693. $dom[$key]['thead'] = false;
  20694. // store the number of rows on table element
  20695. ++$dom[($dom[$key]['parent'])]['rows'];
  20696. // store the TR elements IDs on table element
  20697. array_push($dom[($dom[$key]['parent'])]['trids'], $key);
  20698. }
  20699. }
  20700. if (($dom[$key]['value'] == 'th') OR ($dom[$key]['value'] == 'td')) {
  20701. if (isset($dom[$key]['attribute']['colspan'])) {
  20702. $colspan = intval($dom[$key]['attribute']['colspan']);
  20703. } else {
  20704. $colspan = 1;
  20705. }
  20706. $dom[$key]['attribute']['colspan'] = $colspan;
  20707. $dom[($dom[$key]['parent'])]['cols'] += $colspan;
  20708. }
  20709. // text direction
  20710. if (isset($dom[$key]['attribute']['dir'])) {
  20711. $dom[$key]['dir'] = $dom[$key]['attribute']['dir'];
  20712. }
  20713. // set foreground color attribute
  20714. if (isset($dom[$key]['attribute']['color']) AND (!$this->empty_string($dom[$key]['attribute']['color']))) {
  20715. $dom[$key]['fgcolor'] = $this->convertHTMLColorToDec($dom[$key]['attribute']['color']);
  20716. } elseif (!isset($dom[$key]['style']['color']) AND ($dom[$key]['value'] == 'a')) {
  20717. $dom[$key]['fgcolor'] = $this->htmlLinkColorArray;
  20718. }
  20719. // set background color attribute
  20720. if (isset($dom[$key]['attribute']['bgcolor']) AND (!$this->empty_string($dom[$key]['attribute']['bgcolor']))) {
  20721. $dom[$key]['bgcolor'] = $this->convertHTMLColorToDec($dom[$key]['attribute']['bgcolor']);
  20722. }
  20723. // set stroke color attribute
  20724. if (isset($dom[$key]['attribute']['strokecolor']) AND (!$this->empty_string($dom[$key]['attribute']['strokecolor']))) {
  20725. $dom[$key]['strokecolor'] = $this->convertHTMLColorToDec($dom[$key]['attribute']['strokecolor']);
  20726. }
  20727. // check for width attribute
  20728. if (isset($dom[$key]['attribute']['width'])) {
  20729. $dom[$key]['width'] = $dom[$key]['attribute']['width'];
  20730. }
  20731. // check for height attribute
  20732. if (isset($dom[$key]['attribute']['height'])) {
  20733. $dom[$key]['height'] = $dom[$key]['attribute']['height'];
  20734. }
  20735. // check for text alignment
  20736. if (isset($dom[$key]['attribute']['align']) AND (!$this->empty_string($dom[$key]['attribute']['align'])) AND ($dom[$key]['value'] !== 'img')) {
  20737. $dom[$key]['align'] = strtoupper($dom[$key]['attribute']['align']{0});
  20738. }
  20739. // check for text rendering mode (the following attributes do not exist in HTML)
  20740. if (isset($dom[$key]['attribute']['stroke'])) {
  20741. // font stroke width
  20742. $dom[$key]['stroke'] = $this->getHTMLUnitToUnits($dom[$key]['attribute']['stroke'], $dom[$key]['fontsize'], 'pt', true);
  20743. }
  20744. if (isset($dom[$key]['attribute']['fill'])) {
  20745. // font fill
  20746. if ($dom[$key]['attribute']['fill'] == 'true') {
  20747. $dom[$key]['fill'] = true;
  20748. } else {
  20749. $dom[$key]['fill'] = false;
  20750. }
  20751. }
  20752. if (isset($dom[$key]['attribute']['clip'])) {
  20753. // clipping mode
  20754. if ($dom[$key]['attribute']['clip'] == 'true') {
  20755. $dom[$key]['clip'] = true;
  20756. } else {
  20757. $dom[$key]['clip'] = false;
  20758. }
  20759. }
  20760. } // end opening tag
  20761. } else {
  20762. // text
  20763. $dom[$key]['tag'] = false;
  20764. $dom[$key]['block'] = false;
  20765. //$element = str_replace('&nbsp;', $this->unichr(160), $element);
  20766. $dom[$key]['value'] = stripslashes($this->unhtmlentities($element));
  20767. $dom[$key]['parent'] = end($level);
  20768. $dom[$key]['dir'] = $dom[$dom[$key]['parent']]['dir'];
  20769. }
  20770. ++$elkey;
  20771. ++$key;
  20772. }
  20773. return $dom;
  20774. }
  20775. /**
  20776. * Returns the string used to find spaces
  20777. * @return string
  20778. * @protected
  20779. * @author Nicola Asuni
  20780. * @since 4.8.024 (2010-01-15)
  20781. */
  20782. protected function getSpaceString() {
  20783. $spacestr = chr(32);
  20784. if ($this->isUnicodeFont()) {
  20785. $spacestr = chr(0).chr(32);
  20786. }
  20787. return $spacestr;
  20788. }
  20789. /**
  20790. * Prints a cell (rectangular area) with optional borders, background color and html text string.
  20791. * 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 />
  20792. * If automatic page breaking is enabled and the cell goes beyond the limit, a page break is done before outputting.
  20793. * @param $w (float) Cell width. If 0, the cell extends up to the right margin.
  20794. * @param $h (float) Cell minimum height. The cell extends automatically if needed.
  20795. * @param $x (float) upper-left corner X coordinate
  20796. * @param $y (float) upper-left corner Y coordinate
  20797. * @param $html (string) html text to print. Default value: empty string.
  20798. * @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)))
  20799. * @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>
  20800. Putting 1 is equivalent to putting 0 and calling Ln() just after. Default value: 0.
  20801. * @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
  20802. * @param $reseth (boolean) if true reset the last cell height (default true).
  20803. * @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>
  20804. * @param $autopadding (boolean) if true, uses internal padding and automatically adjust it to account for line width.
  20805. * @see Multicell(), writeHTML()
  20806. * @public
  20807. */
  20808. public function writeHTMLCell($w, $h, $x, $y, $html='', $border=0, $ln=0, $fill=false, $reseth=true, $align='', $autopadding=true) {
  20809. return $this->MultiCell($w, $h, $html, $border, $align, $fill, $ln, $x, $y, $reseth, 0, true, $autopadding, 0, 'T', false);
  20810. }
  20811. /**
  20812. * Allows to preserve some HTML formatting (limited support).<br />
  20813. * IMPORTANT: The HTML must be well formatted - try to clean-up it using an application like HTML-Tidy before submitting.
  20814. * 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
  20815. * @param $html (string) text to display
  20816. * @param $ln (boolean) if true add a new line after text (default = true)
  20817. * @param $fill (boolean) Indicates if the background must be painted (true) or transparent (false).
  20818. * @param $reseth (boolean) if true reset the last cell height (default false).
  20819. * @param $cell (boolean) if true add the current left (or right for RTL) padding to each Write (default false).
  20820. * @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>
  20821. * @public
  20822. */
  20823. public function writeHTML($html, $ln=true, $fill=false, $reseth=false, $cell=false, $align='') {
  20824. $gvars = $this->getGraphicVars();
  20825. // store current values
  20826. $prev_cell_margin = $this->cell_margin;
  20827. $prev_cell_padding = $this->cell_padding;
  20828. $prevPage = $this->page;
  20829. $prevlMargin = $this->lMargin;
  20830. $prevrMargin = $this->rMargin;
  20831. $curfontname = $this->FontFamily;
  20832. $curfontstyle = $this->FontStyle;
  20833. $curfontsize = $this->FontSizePt;
  20834. $curfontascent = $this->getFontAscent($curfontname, $curfontstyle, $curfontsize);
  20835. $curfontdescent = $this->getFontDescent($curfontname, $curfontstyle, $curfontsize);
  20836. $curfontstretcing = $this->font_stretching;
  20837. $curfontkerning = $this->font_spacing;
  20838. $this->newline = true;
  20839. $newline = true;
  20840. $startlinepage = $this->page;
  20841. $minstartliney = $this->y;
  20842. $maxbottomliney = 0;
  20843. $startlinex = $this->x;
  20844. $startliney = $this->y;
  20845. $yshift = 0;
  20846. $loop = 0;
  20847. $curpos = 0;
  20848. $this_method_vars = array();
  20849. $undo = false;
  20850. $fontaligned = false;
  20851. $reverse_dir = false; // true when the text direction is reversed
  20852. $this->premode = false;
  20853. if ($this->inxobj) {
  20854. // we are inside an XObject template
  20855. $pask = count($this->xobjects[$this->xobjid]['annotations']);
  20856. } elseif (isset($this->PageAnnots[$this->page])) {
  20857. $pask = count($this->PageAnnots[$this->page]);
  20858. } else {
  20859. $pask = 0;
  20860. }
  20861. if ($this->inxobj) {
  20862. // we are inside an XObject template
  20863. $startlinepos = strlen($this->xobjects[$this->xobjid]['outdata']);
  20864. } elseif (!$this->InFooter) {
  20865. if (isset($this->footerlen[$this->page])) {
  20866. $this->footerpos[$this->page] = $this->pagelen[$this->page] - $this->footerlen[$this->page];
  20867. } else {
  20868. $this->footerpos[$this->page] = $this->pagelen[$this->page];
  20869. }
  20870. $startlinepos = $this->footerpos[$this->page];
  20871. } else {
  20872. // we are inside the footer
  20873. $startlinepos = $this->pagelen[$this->page];
  20874. }
  20875. $lalign = $align;
  20876. $plalign = $align;
  20877. if ($this->rtl) {
  20878. $w = $this->x - $this->lMargin;
  20879. } else {
  20880. $w = $this->w - $this->rMargin - $this->x;
  20881. }
  20882. $w -= ($this->cell_padding['L'] + $this->cell_padding['R']);
  20883. if ($cell) {
  20884. if ($this->rtl) {
  20885. $this->x -= $this->cell_padding['R'];
  20886. $this->lMargin += $this->cell_padding['R'];
  20887. } else {
  20888. $this->x += $this->cell_padding['L'];
  20889. $this->rMargin += $this->cell_padding['L'];
  20890. }
  20891. }
  20892. if ($this->customlistindent >= 0) {
  20893. $this->listindent = $this->customlistindent;
  20894. } else {
  20895. $this->listindent = $this->GetStringWidth('000000');
  20896. }
  20897. $this->listindentlevel = 0;
  20898. // save previous states
  20899. $prev_cell_height_ratio = $this->cell_height_ratio;
  20900. $prev_listnum = $this->listnum;
  20901. $prev_listordered = $this->listordered;
  20902. $prev_listcount = $this->listcount;
  20903. $prev_lispacer = $this->lispacer;
  20904. $this->listnum = 0;
  20905. $this->listordered = array();
  20906. $this->listcount = array();
  20907. $this->lispacer = '';
  20908. if (($this->empty_string($this->lasth)) OR ($reseth)) {
  20909. // reset row height
  20910. $this->resetLastH();
  20911. }
  20912. $dom = $this->getHtmlDomArray($html);
  20913. $maxel = count($dom);
  20914. $key = 0;
  20915. $hidden_node_key = -1;
  20916. while ($key < $maxel) {
  20917. if ($dom[$key]['tag']) {
  20918. if ($dom[$key]['opening']) {
  20919. if (($hidden_node_key <= 0) AND $dom[$key]['hide']) {
  20920. // store the node key
  20921. $hidden_node_key = $key;
  20922. }
  20923. } elseif (($hidden_node_key > 0) AND ($dom[$key]['parent'] == $hidden_node_key)) {
  20924. // we have reached the closing tag of the hidden node
  20925. $hidden_node_key = 0;
  20926. }
  20927. }
  20928. if ($hidden_node_key >= 0) {
  20929. // skip this node
  20930. ++$key;
  20931. if ($hidden_node_key == 0) {
  20932. // reset hidden mode
  20933. $hidden_node_key = -1;
  20934. }
  20935. continue;
  20936. }
  20937. if ($dom[$key]['tag'] AND isset($dom[$key]['attribute']['pagebreak'])) {
  20938. // check for pagebreak
  20939. if (($dom[$key]['attribute']['pagebreak'] == 'true') OR ($dom[$key]['attribute']['pagebreak'] == 'left') OR ($dom[$key]['attribute']['pagebreak'] == 'right')) {
  20940. // add a page (or trig AcceptPageBreak() for multicolumn mode)
  20941. $this->checkPageBreak($this->PageBreakTrigger + 1);
  20942. $this->htmlvspace = ($this->PageBreakTrigger + 1);
  20943. }
  20944. if ((($dom[$key]['attribute']['pagebreak'] == 'left') AND (((!$this->rtl) AND (($this->page % 2) == 0)) OR (($this->rtl) AND (($this->page % 2) != 0))))
  20945. OR (($dom[$key]['attribute']['pagebreak'] == 'right') AND (((!$this->rtl) AND (($this->page % 2) != 0)) OR (($this->rtl) AND (($this->page % 2) == 0))))) {
  20946. // add a page (or trig AcceptPageBreak() for multicolumn mode)
  20947. $this->checkPageBreak($this->PageBreakTrigger + 1);
  20948. $this->htmlvspace = ($this->PageBreakTrigger + 1);
  20949. }
  20950. }
  20951. if ($dom[$key]['tag'] AND $dom[$key]['opening'] AND isset($dom[$key]['attribute']['nobr']) AND ($dom[$key]['attribute']['nobr'] == 'true')) {
  20952. if (isset($dom[($dom[$key]['parent'])]['attribute']['nobr']) AND ($dom[($dom[$key]['parent'])]['attribute']['nobr'] == 'true')) {
  20953. $dom[$key]['attribute']['nobr'] = false;
  20954. } else {
  20955. // store current object
  20956. $this->startTransaction();
  20957. // save this method vars
  20958. $this_method_vars['html'] = $html;
  20959. $this_method_vars['ln'] = $ln;
  20960. $this_method_vars['fill'] = $fill;
  20961. $this_method_vars['reseth'] = $reseth;
  20962. $this_method_vars['cell'] = $cell;
  20963. $this_method_vars['align'] = $align;
  20964. $this_method_vars['gvars'] = $gvars;
  20965. $this_method_vars['prevPage'] = $prevPage;
  20966. $this_method_vars['prev_cell_margin'] = $prev_cell_margin;
  20967. $this_method_vars['prev_cell_padding'] = $prev_cell_padding;
  20968. $this_method_vars['prevlMargin'] = $prevlMargin;
  20969. $this_method_vars['prevrMargin'] = $prevrMargin;
  20970. $this_method_vars['curfontname'] = $curfontname;
  20971. $this_method_vars['curfontstyle'] = $curfontstyle;
  20972. $this_method_vars['curfontsize'] = $curfontsize;
  20973. $this_method_vars['curfontascent'] = $curfontascent;
  20974. $this_method_vars['curfontdescent'] = $curfontdescent;
  20975. $this_method_vars['curfontstretcing'] = $curfontstretcing;
  20976. $this_method_vars['curfontkerning'] = $curfontkerning;
  20977. $this_method_vars['minstartliney'] = $minstartliney;
  20978. $this_method_vars['maxbottomliney'] = $maxbottomliney;
  20979. $this_method_vars['yshift'] = $yshift;
  20980. $this_method_vars['startlinepage'] = $startlinepage;
  20981. $this_method_vars['startlinepos'] = $startlinepos;
  20982. $this_method_vars['startlinex'] = $startlinex;
  20983. $this_method_vars['startliney'] = $startliney;
  20984. $this_method_vars['newline'] = $newline;
  20985. $this_method_vars['loop'] = $loop;
  20986. $this_method_vars['curpos'] = $curpos;
  20987. $this_method_vars['pask'] = $pask;
  20988. $this_method_vars['lalign'] = $lalign;
  20989. $this_method_vars['plalign'] = $plalign;
  20990. $this_method_vars['w'] = $w;
  20991. $this_method_vars['prev_cell_height_ratio'] = $prev_cell_height_ratio;
  20992. $this_method_vars['prev_listnum'] = $prev_listnum;
  20993. $this_method_vars['prev_listordered'] = $prev_listordered;
  20994. $this_method_vars['prev_listcount'] = $prev_listcount;
  20995. $this_method_vars['prev_lispacer'] = $prev_lispacer;
  20996. $this_method_vars['fontaligned'] = $fontaligned;
  20997. $this_method_vars['key'] = $key;
  20998. $this_method_vars['dom'] = $dom;
  20999. }
  21000. }
  21001. // print THEAD block
  21002. if (($dom[$key]['value'] == 'tr') AND isset($dom[$key]['thead']) AND $dom[$key]['thead']) {
  21003. if (isset($dom[$key]['parent']) AND isset($dom[$dom[$key]['parent']]['thead']) AND !$this->empty_string($dom[$dom[$key]['parent']]['thead'])) {
  21004. $this->inthead = true;
  21005. // print table header (thead)
  21006. $this->writeHTML($this->thead, false, false, false, false, '');
  21007. // check if we are on a new page or on a new column
  21008. if (($this->y < $this->start_transaction_y) OR ($this->checkPageBreak($this->lasth, '', false))) {
  21009. // we are on a new page or on a new column and the total object height is less than the available vertical space.
  21010. // restore previous object
  21011. $this->rollbackTransaction(true);
  21012. // restore previous values
  21013. foreach ($this_method_vars as $vkey => $vval) {
  21014. $$vkey = $vval;
  21015. }
  21016. // disable table header
  21017. $tmp_thead = $this->thead;
  21018. $this->thead = '';
  21019. // add a page (or trig AcceptPageBreak() for multicolumn mode)
  21020. $pre_y = $this->y;
  21021. if ((!$this->checkPageBreak($this->PageBreakTrigger + 1)) AND ($this->y < $pre_y)) {
  21022. // fix for multicolumn mode
  21023. $startliney = $this->y;
  21024. }
  21025. $this->start_transaction_page = $this->page;
  21026. $this->start_transaction_y = $this->y;
  21027. // restore table header
  21028. $this->thead = $tmp_thead;
  21029. // fix table border properties
  21030. if (isset($dom[$dom[$key]['parent']]['attribute']['cellspacing'])) {
  21031. $tmp_cellspacing = $this->getHTMLUnitToUnits($dom[$dom[$key]['parent']]['attribute']['cellspacing'], 1, 'px');
  21032. } elseif (isset($dom[$dom[$key]['parent']]['border-spacing'])) {
  21033. $tmp_cellspacing = $dom[$dom[$key]['parent']]['border-spacing']['V'];
  21034. } else {
  21035. $tmp_cellspacing = 0;
  21036. }
  21037. $dom[$dom[$key]['parent']]['borderposition']['page'] = $this->page;
  21038. $dom[$dom[$key]['parent']]['borderposition']['column'] = $this->current_column;
  21039. $dom[$dom[$key]['parent']]['borderposition']['y'] = $this->y + $tmp_cellspacing;
  21040. $xoffset = ($this->x - $dom[$dom[$key]['parent']]['borderposition']['x']);
  21041. $dom[$dom[$key]['parent']]['borderposition']['x'] += $xoffset;
  21042. $dom[$dom[$key]['parent']]['borderposition']['xmax'] += $xoffset;
  21043. // print table header (thead)
  21044. $this->writeHTML($this->thead, false, false, false, false, '');
  21045. }
  21046. }
  21047. // move $key index forward to skip THEAD block
  21048. while ( ($key < $maxel) AND (!(
  21049. ($dom[$key]['tag'] AND $dom[$key]['opening'] AND ($dom[$key]['value'] == 'tr') AND (!isset($dom[$key]['thead']) OR !$dom[$key]['thead']))
  21050. OR ($dom[$key]['tag'] AND (!$dom[$key]['opening']) AND ($dom[$key]['value'] == 'table'))) )) {
  21051. ++$key;
  21052. }
  21053. }
  21054. if ($dom[$key]['tag'] OR ($key == 0)) {
  21055. if ((($dom[$key]['value'] == 'table') OR ($dom[$key]['value'] == 'tr')) AND (isset($dom[$key]['align']))) {
  21056. $dom[$key]['align'] = ($this->rtl) ? 'R' : 'L';
  21057. }
  21058. // vertically align image in line
  21059. if ((!$this->newline) AND ($dom[$key]['value'] == 'img') AND (isset($dom[$key]['height'])) AND ($dom[$key]['height'] > 0)) {
  21060. // get image height
  21061. $imgh = $this->getHTMLUnitToUnits($dom[$key]['height'], $this->lasth, 'px');
  21062. $autolinebreak = false;
  21063. if (isset($dom[$key]['width']) AND ($dom[$key]['width'] > 0)) {
  21064. $imgw = $this->getHTMLUnitToUnits($dom[$key]['width'], 1, 'px', false);
  21065. if (($imgw <= ($this->w - $this->lMargin - $this->rMargin - $this->cell_padding['L'] - $this->cell_padding['R']))
  21066. AND ((($this->rtl) AND (($this->x - $imgw) < ($this->lMargin + $this->cell_padding['L'])))
  21067. OR ((!$this->rtl) AND (($this->x + $imgw) > ($this->w - $this->rMargin - $this->cell_padding['R']))))) {
  21068. // add automatic line break
  21069. $autolinebreak = true;
  21070. $this->Ln('', $cell);
  21071. if ((!$dom[($key-1)]['tag']) AND ($dom[($key-1)]['value'] == ' ')) {
  21072. // go back to evaluate this line break
  21073. --$key;
  21074. }
  21075. }
  21076. }
  21077. if (!$autolinebreak) {
  21078. if ($this->inPageBody()) {
  21079. $pre_y = $this->y;
  21080. // check for page break
  21081. if ((!$this->checkPageBreak($imgh)) AND ($this->y < $pre_y)) {
  21082. // fix for multicolumn mode
  21083. $startliney = $this->y;
  21084. }
  21085. }
  21086. if ($this->page > $startlinepage) {
  21087. // fix line splitted over two pages
  21088. if (isset($this->footerlen[$startlinepage])) {
  21089. $curpos = $this->pagelen[$startlinepage] - $this->footerlen[$startlinepage];
  21090. }
  21091. // line to be moved one page forward
  21092. $pagebuff = $this->getPageBuffer($startlinepage);
  21093. $linebeg = substr($pagebuff, $startlinepos, ($curpos - $startlinepos));
  21094. $tstart = substr($pagebuff, 0, $startlinepos);
  21095. $tend = substr($this->getPageBuffer($startlinepage), $curpos);
  21096. // remove line from previous page
  21097. $this->setPageBuffer($startlinepage, $tstart.''.$tend);
  21098. $pagebuff = $this->getPageBuffer($this->page);
  21099. $tstart = substr($pagebuff, 0, $this->cntmrk[$this->page]);
  21100. $tend = substr($pagebuff, $this->cntmrk[$this->page]);
  21101. // add line start to current page
  21102. $yshift = ($minstartliney - $this->y);
  21103. if ($fontaligned) {
  21104. $yshift += ($curfontsize / $this->k);
  21105. }
  21106. $try = sprintf('1 0 0 1 0 %.3F cm', ($yshift * $this->k));
  21107. $this->setPageBuffer($this->page, $tstart."\nq\n".$try."\n".$linebeg."\nQ\n".$tend);
  21108. // shift the annotations and links
  21109. if (isset($this->PageAnnots[$this->page])) {
  21110. $next_pask = count($this->PageAnnots[$this->page]);
  21111. } else {
  21112. $next_pask = 0;
  21113. }
  21114. if (isset($this->PageAnnots[$startlinepage])) {
  21115. foreach ($this->PageAnnots[$startlinepage] as $pak => $pac) {
  21116. if ($pak >= $pask) {
  21117. $this->PageAnnots[$this->page][] = $pac;
  21118. unset($this->PageAnnots[$startlinepage][$pak]);
  21119. $npak = count($this->PageAnnots[$this->page]) - 1;
  21120. $this->PageAnnots[$this->page][$npak]['y'] -= $yshift;
  21121. }
  21122. }
  21123. }
  21124. $pask = $next_pask;
  21125. $startlinepos = $this->cntmrk[$this->page];
  21126. $startlinepage = $this->page;
  21127. $startliney = $this->y;
  21128. $this->newline = false;
  21129. }
  21130. $this->y += ((($curfontsize * $this->cell_height_ratio / $this->k) + $curfontascent - $curfontdescent) / 2) - $imgh;
  21131. $minstartliney = min($this->y, $minstartliney);
  21132. $maxbottomliney = ($startliney + ($this->FontSize * $this->cell_height_ratio));
  21133. }
  21134. } elseif (isset($dom[$key]['fontname']) OR isset($dom[$key]['fontstyle']) OR isset($dom[$key]['fontsize']) OR isset($dom[$key]['line-height'])) {
  21135. // account for different font size
  21136. $pfontname = $curfontname;
  21137. $pfontstyle = $curfontstyle;
  21138. $pfontsize = $curfontsize;
  21139. $fontname = isset($dom[$key]['fontname']) ? $dom[$key]['fontname'] : $curfontname;
  21140. $fontstyle = isset($dom[$key]['fontstyle']) ? $dom[$key]['fontstyle'] : $curfontstyle;
  21141. $fontsize = isset($dom[$key]['fontsize']) ? $dom[$key]['fontsize'] : $curfontsize;
  21142. $fontascent = $this->getFontAscent($fontname, $fontstyle, $fontsize);
  21143. $fontdescent = $this->getFontDescent($fontname, $fontstyle, $fontsize);
  21144. if (($fontname != $curfontname) OR ($fontstyle != $curfontstyle) OR ($fontsize != $curfontsize)
  21145. OR ($this->cell_height_ratio != $dom[$key]['line-height'])
  21146. OR ($dom[$key]['tag'] AND $dom[$key]['opening'] AND ($dom[$key]['value'] == 'li')) ) {
  21147. if (($key < ($maxel - 1)) AND (
  21148. ($dom[$key]['tag'] AND $dom[$key]['opening'] AND ($dom[$key]['value'] == 'li'))
  21149. OR ($this->cell_height_ratio != $dom[$key]['line-height'])
  21150. OR (!$this->newline AND is_numeric($fontsize) AND is_numeric($curfontsize) AND ($fontsize >= 0) AND ($curfontsize >= 0) AND ($fontsize != $curfontsize))
  21151. )) {
  21152. if ($this->page > $startlinepage) {
  21153. // fix lines splitted over two pages
  21154. if (isset($this->footerlen[$startlinepage])) {
  21155. $curpos = $this->pagelen[$startlinepage] - $this->footerlen[$startlinepage];
  21156. }
  21157. // line to be moved one page forward
  21158. $pagebuff = $this->getPageBuffer($startlinepage);
  21159. $linebeg = substr($pagebuff, $startlinepos, ($curpos - $startlinepos));
  21160. $tstart = substr($pagebuff, 0, $startlinepos);
  21161. $tend = substr($this->getPageBuffer($startlinepage), $curpos);
  21162. // remove line start from previous page
  21163. $this->setPageBuffer($startlinepage, $tstart.''.$tend);
  21164. $pagebuff = $this->getPageBuffer($this->page);
  21165. $tstart = substr($pagebuff, 0, $this->cntmrk[$this->page]);
  21166. $tend = substr($pagebuff, $this->cntmrk[$this->page]);
  21167. // add line start to current page
  21168. $yshift = ($minstartliney - $this->y);
  21169. $try = sprintf('1 0 0 1 0 %.3F cm', ($yshift * $this->k));
  21170. $this->setPageBuffer($this->page, $tstart."\nq\n".$try."\n".$linebeg."\nQ\n".$tend);
  21171. // shift the annotations and links
  21172. if (isset($this->PageAnnots[$this->page])) {
  21173. $next_pask = count($this->PageAnnots[$this->page]);
  21174. } else {
  21175. $next_pask = 0;
  21176. }
  21177. if (isset($this->PageAnnots[$startlinepage])) {
  21178. foreach ($this->PageAnnots[$startlinepage] as $pak => $pac) {
  21179. if ($pak >= $pask) {
  21180. $this->PageAnnots[$this->page][] = $pac;
  21181. unset($this->PageAnnots[$startlinepage][$pak]);
  21182. $npak = count($this->PageAnnots[$this->page]) - 1;
  21183. $this->PageAnnots[$this->page][$npak]['y'] -= $yshift;
  21184. }
  21185. }
  21186. }
  21187. $pask = $next_pask;
  21188. $startlinepos = $this->cntmrk[$this->page];
  21189. $startlinepage = $this->page;
  21190. $startliney = $this->y;
  21191. }
  21192. if (!isset($dom[$key]['line-height'])) {
  21193. $dom[$key]['line-height'] = $this->cell_height_ratio;
  21194. }
  21195. if (!$dom[$key]['block']) {
  21196. 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']))) {
  21197. $this->y += (((($curfontsize * $this->cell_height_ratio) - ($fontsize * $dom[$key]['line-height'])) / $this->k) + $curfontascent - $fontascent - $curfontdescent + $fontdescent) / 2;
  21198. }
  21199. if (($dom[$key]['value'] != 'sup') AND ($dom[$key]['value'] != 'sub')) {
  21200. $current_line_align_data = array($key, $minstartliney, $maxbottomliney);
  21201. 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)))) {
  21202. $minstartliney = min($this->y, $line_align_data[1]);
  21203. $maxbottomliney = max(($this->y + (($fontsize * $this->cell_height_ratio) / $this->k)), $line_align_data[2]);
  21204. } else {
  21205. $minstartliney = min($this->y, $minstartliney);
  21206. $maxbottomliney = max(($this->y + (($fontsize * $this->cell_height_ratio) / $this->k)), $maxbottomliney);
  21207. }
  21208. $line_align_data = $current_line_align_data;
  21209. }
  21210. }
  21211. $this->cell_height_ratio = $dom[$key]['line-height'];
  21212. $fontaligned = true;
  21213. }
  21214. $this->SetFont($fontname, $fontstyle, $fontsize);
  21215. // reset row height
  21216. $this->resetLastH();
  21217. $curfontname = $fontname;
  21218. $curfontstyle = $fontstyle;
  21219. $curfontsize = $fontsize;
  21220. $curfontascent = $fontascent;
  21221. $curfontdescent = $fontdescent;
  21222. }
  21223. }
  21224. // set text rendering mode
  21225. $textstroke = isset($dom[$key]['stroke']) ? $dom[$key]['stroke'] : $this->textstrokewidth;
  21226. $textfill = isset($dom[$key]['fill']) ? $dom[$key]['fill'] : (($this->textrendermode % 2) == 0);
  21227. $textclip = isset($dom[$key]['clip']) ? $dom[$key]['clip'] : ($this->textrendermode > 3);
  21228. $this->setTextRenderingMode($textstroke, $textfill, $textclip);
  21229. if (isset($dom[$key]['font-stretch']) AND ($dom[$key]['font-stretch'] !== false)) {
  21230. $this->setFontStretching($dom[$key]['font-stretch']);
  21231. }
  21232. if (isset($dom[$key]['letter-spacing']) AND ($dom[$key]['letter-spacing'] !== false)) {
  21233. $this->setFontSpacing($dom[$key]['letter-spacing']);
  21234. }
  21235. if (($plalign == 'J') AND $dom[$key]['block']) {
  21236. $plalign = '';
  21237. }
  21238. // get current position on page buffer
  21239. $curpos = $this->pagelen[$startlinepage];
  21240. if (isset($dom[$key]['bgcolor']) AND ($dom[$key]['bgcolor'] !== false)) {
  21241. $this->SetFillColorArray($dom[$key]['bgcolor']);
  21242. $wfill = true;
  21243. } else {
  21244. $wfill = $fill | false;
  21245. }
  21246. if (isset($dom[$key]['fgcolor']) AND ($dom[$key]['fgcolor'] !== false)) {
  21247. $this->SetTextColorArray($dom[$key]['fgcolor']);
  21248. }
  21249. if (isset($dom[$key]['strokecolor']) AND ($dom[$key]['strokecolor'] !== false)) {
  21250. $this->SetDrawColorArray($dom[$key]['strokecolor']);
  21251. }
  21252. if (isset($dom[$key]['align'])) {
  21253. $lalign = $dom[$key]['align'];
  21254. }
  21255. if ($this->empty_string($lalign)) {
  21256. $lalign = $align;
  21257. }
  21258. }
  21259. // align lines
  21260. if ($this->newline AND (strlen($dom[$key]['value']) > 0) AND ($dom[$key]['value'] != 'td') AND ($dom[$key]['value'] != 'th')) {
  21261. $newline = true;
  21262. $fontaligned = false;
  21263. // we are at the beginning of a new line
  21264. if (isset($startlinex)) {
  21265. $yshift = ($minstartliney - $startliney);
  21266. if (($yshift > 0) OR ($this->page > $startlinepage)) {
  21267. $yshift = 0;
  21268. }
  21269. $t_x = 0;
  21270. // the last line must be shifted to be aligned as requested
  21271. $linew = abs($this->endlinex - $startlinex);
  21272. if ($this->inxobj) {
  21273. // we are inside an XObject template
  21274. $pstart = substr($this->xobjects[$this->xobjid]['outdata'], 0, $startlinepos);
  21275. if (isset($opentagpos)) {
  21276. $midpos = $opentagpos;
  21277. } else {
  21278. $midpos = 0;
  21279. }
  21280. if ($midpos > 0) {
  21281. $pmid = substr($this->xobjects[$this->xobjid]['outdata'], $startlinepos, ($midpos - $startlinepos));
  21282. $pend = substr($this->xobjects[$this->xobjid]['outdata'], $midpos);
  21283. } else {
  21284. $pmid = substr($this->xobjects[$this->xobjid]['outdata'], $startlinepos);
  21285. $pend = '';
  21286. }
  21287. } else {
  21288. $pstart = substr($this->getPageBuffer($startlinepage), 0, $startlinepos);
  21289. if (isset($opentagpos) AND isset($this->footerlen[$startlinepage]) AND (!$this->InFooter)) {
  21290. $this->footerpos[$startlinepage] = $this->pagelen[$startlinepage] - $this->footerlen[$startlinepage];
  21291. $midpos = min($opentagpos, $this->footerpos[$startlinepage]);
  21292. } elseif (isset($opentagpos)) {
  21293. $midpos = $opentagpos;
  21294. } elseif (isset($this->footerlen[$startlinepage]) AND (!$this->InFooter)) {
  21295. $this->footerpos[$startlinepage] = $this->pagelen[$startlinepage] - $this->footerlen[$startlinepage];
  21296. $midpos = $this->footerpos[$startlinepage];
  21297. } else {
  21298. $midpos = 0;
  21299. }
  21300. if ($midpos > 0) {
  21301. $pmid = substr($this->getPageBuffer($startlinepage), $startlinepos, ($midpos - $startlinepos));
  21302. $pend = substr($this->getPageBuffer($startlinepage), $midpos);
  21303. } else {
  21304. $pmid = substr($this->getPageBuffer($startlinepage), $startlinepos);
  21305. $pend = '';
  21306. }
  21307. }
  21308. if ((isset($plalign) AND ((($plalign == 'C') OR ($plalign == 'J') OR (($plalign == 'R') AND (!$this->rtl)) OR (($plalign == 'L') AND ($this->rtl)))))) {
  21309. // calculate shifting amount
  21310. $tw = $w;
  21311. if (($plalign == 'J') AND $this->isRTLTextDir() AND ($this->num_columns > 1)) {
  21312. $tw += $this->cell_padding['R'];
  21313. }
  21314. if ($this->lMargin != $prevlMargin) {
  21315. $tw += ($prevlMargin - $this->lMargin);
  21316. }
  21317. if ($this->rMargin != $prevrMargin) {
  21318. $tw += ($prevrMargin - $this->rMargin);
  21319. }
  21320. $one_space_width = $this->GetStringWidth(chr(32));
  21321. $no = 0; // number of spaces on a line contained on a single block
  21322. if ($this->isRTLTextDir()) { // RTL
  21323. // remove left space if exist
  21324. $pos1 = $this->revstrpos($pmid, '[(');
  21325. if ($pos1 > 0) {
  21326. $pos1 = intval($pos1);
  21327. if ($this->isUnicodeFont()) {
  21328. $pos2 = intval($this->revstrpos($pmid, '[('.chr(0).chr(32)));
  21329. $spacelen = 2;
  21330. } else {
  21331. $pos2 = intval($this->revstrpos($pmid, '[('.chr(32)));
  21332. $spacelen = 1;
  21333. }
  21334. if ($pos1 == $pos2) {
  21335. $pmid = substr($pmid, 0, ($pos1 + 2)).substr($pmid, ($pos1 + 2 + $spacelen));
  21336. if (substr($pmid, $pos1, 4) == '[()]') {
  21337. $linew -= $one_space_width;
  21338. } elseif ($pos1 == strpos($pmid, '[(')) {
  21339. $no = 1;
  21340. }
  21341. }
  21342. }
  21343. } else { // LTR
  21344. // remove right space if exist
  21345. $pos1 = $this->revstrpos($pmid, ')]');
  21346. if ($pos1 > 0) {
  21347. $pos1 = intval($pos1);
  21348. if ($this->isUnicodeFont()) {
  21349. $pos2 = intval($this->revstrpos($pmid, chr(0).chr(32).')]')) + 2;
  21350. $spacelen = 2;
  21351. } else {
  21352. $pos2 = intval($this->revstrpos($pmid, chr(32).')]')) + 1;
  21353. $spacelen = 1;
  21354. }
  21355. if ($pos1 == $pos2) {
  21356. $pmid = substr($pmid, 0, ($pos1 - $spacelen)).substr($pmid, $pos1);
  21357. $linew -= $one_space_width;
  21358. }
  21359. }
  21360. }
  21361. $mdiff = ($tw - $linew);
  21362. if ($plalign == 'C') {
  21363. if ($this->rtl) {
  21364. $t_x = -($mdiff / 2);
  21365. } else {
  21366. $t_x = ($mdiff / 2);
  21367. }
  21368. } elseif ($plalign == 'R') {
  21369. // right alignment on LTR document
  21370. $t_x = $mdiff;
  21371. } elseif ($plalign == 'L') {
  21372. // left alignment on RTL document
  21373. $t_x = -$mdiff;
  21374. } elseif (($plalign == 'J') AND ($plalign == $lalign)) {
  21375. // Justification
  21376. if ($this->isRTLTextDir()) {
  21377. // align text on the left
  21378. $t_x = -$mdiff;
  21379. }
  21380. $ns = 0; // number of spaces
  21381. $pmidtemp = $pmid;
  21382. // escape special characters
  21383. $pmidtemp = preg_replace('/[\\\][\(]/x', '\\#!#OP#!#', $pmidtemp);
  21384. $pmidtemp = preg_replace('/[\\\][\)]/x', '\\#!#CP#!#', $pmidtemp);
  21385. // search spaces
  21386. if (preg_match_all('/\[\(([^\)]*)\)\]/x', $pmidtemp, $lnstring, PREG_PATTERN_ORDER)) {
  21387. $spacestr = $this->getSpaceString();
  21388. $maxkk = count($lnstring[1]) - 1;
  21389. for ($kk=0; $kk <= $maxkk; ++$kk) {
  21390. // restore special characters
  21391. $lnstring[1][$kk] = str_replace('#!#OP#!#', '(', $lnstring[1][$kk]);
  21392. $lnstring[1][$kk] = str_replace('#!#CP#!#', ')', $lnstring[1][$kk]);
  21393. // store number of spaces on the strings
  21394. $lnstring[2][$kk] = substr_count($lnstring[1][$kk], $spacestr);
  21395. // count total spaces on line
  21396. $ns += $lnstring[2][$kk];
  21397. $lnstring[3][$kk] = $ns;
  21398. }
  21399. if ($ns == 0) {
  21400. $ns = 1;
  21401. }
  21402. // calculate additional space to add to each existing space
  21403. $spacewidth = ($mdiff / ($ns - $no)) * $this->k;
  21404. $spacewidthu = -1000 * ($mdiff + (($ns + $no) * $one_space_width)) / $ns / $this->FontSize;
  21405. if ($this->font_spacing != 0) {
  21406. // fixed spacing mode
  21407. $osw = -1000 * $this->font_spacing / $this->FontSize;
  21408. $spacewidthu += $osw;
  21409. }
  21410. $nsmax = $ns;
  21411. $ns = 0;
  21412. reset($lnstring);
  21413. $offset = 0;
  21414. $strcount = 0;
  21415. $prev_epsposbeg = 0;
  21416. $textpos = 0;
  21417. if ($this->isRTLTextDir()) {
  21418. $textpos = $this->wPt;
  21419. }
  21420. global $spacew;
  21421. while (preg_match('/([0-9\.\+\-]*)[\s](Td|cm|m|l|c|re)[\s]/x', $pmid, $strpiece, PREG_OFFSET_CAPTURE, $offset) == 1) {
  21422. // check if we are inside a string section '[( ... )]'
  21423. $stroffset = strpos($pmid, '[(', $offset);
  21424. if (($stroffset !== false) AND ($stroffset <= $strpiece[2][1])) {
  21425. // set offset to the end of string section
  21426. $offset = strpos($pmid, ')]', $stroffset);
  21427. while (($offset !== false) AND ($pmid[($offset - 1)] == '\\')) {
  21428. $offset = strpos($pmid, ')]', ($offset + 1));
  21429. }
  21430. if ($offset === false) {
  21431. $this->Error('HTML Justification: malformed PDF code.');
  21432. }
  21433. continue;
  21434. }
  21435. if ($this->isRTLTextDir()) {
  21436. $spacew = ($spacewidth * ($nsmax - $ns));
  21437. } else {
  21438. $spacew = ($spacewidth * $ns);
  21439. }
  21440. $offset = $strpiece[2][1] + strlen($strpiece[2][0]);
  21441. $epsposbeg = strpos($pmid, 'q'.$this->epsmarker, $offset);
  21442. $epsposend = strpos($pmid, $this->epsmarker.'Q', $offset) + strlen($this->epsmarker.'Q');
  21443. if ((($epsposbeg > 0) AND ($epsposend > 0) AND ($offset > $epsposbeg) AND ($offset < $epsposend))
  21444. OR (($epsposbeg === false) AND ($epsposend > 0) AND ($offset < $epsposend))) {
  21445. // shift EPS images
  21446. $trx = sprintf('1 0 0 1 %.3F 0 cm', $spacew);
  21447. $epsposbeg = strpos($pmid, 'q'.$this->epsmarker, ($prev_epsposbeg - 6));
  21448. $pmid_b = substr($pmid, 0, $epsposbeg);
  21449. $pmid_m = substr($pmid, $epsposbeg, ($epsposend - $epsposbeg));
  21450. $pmid_e = substr($pmid, $epsposend);
  21451. $pmid = $pmid_b."\nq\n".$trx."\n".$pmid_m."\nQ\n".$pmid_e;
  21452. $offset = $epsposend;
  21453. continue;
  21454. }
  21455. $prev_epsposbeg = $epsposbeg;
  21456. $currentxpos = 0;
  21457. // shift blocks of code
  21458. switch ($strpiece[2][0]) {
  21459. case 'Td':
  21460. case 'cm':
  21461. case 'm':
  21462. case 'l': {
  21463. // get current X position
  21464. preg_match('/([0-9\.\+\-]*)[\s]('.$strpiece[1][0].')[\s]('.$strpiece[2][0].')([\s]*)/x', $pmid, $xmatches);
  21465. $currentxpos = $xmatches[1];
  21466. $textpos = $currentxpos;
  21467. if (($strcount <= $maxkk) AND ($strpiece[2][0] == 'Td')) {
  21468. $ns = $lnstring[3][$strcount];
  21469. if ($this->isRTLTextDir()) {
  21470. $spacew = ($spacewidth * ($nsmax - $ns));
  21471. }
  21472. ++$strcount;
  21473. }
  21474. // justify block
  21475. $pmid = preg_replace_callback('/([0-9\.\+\-]*)[\s]('.$strpiece[1][0].')[\s]('.$strpiece[2][0].')([\s]*)/x',
  21476. create_function('$matches', 'global $spacew;
  21477. $newx = sprintf("%.2F",(floatval($matches[1]) + $spacew));
  21478. return "".$newx." ".$matches[2]." x*#!#*x".$matches[3].$matches[4];'), $pmid, 1);
  21479. break;
  21480. }
  21481. case 're': {
  21482. // justify block
  21483. if (!$this->empty_string($this->lispacer)) {
  21484. $this->lispacer = '';
  21485. continue;
  21486. }
  21487. preg_match('/([0-9\.\+\-]*)[\s]([0-9\.\+\-]*)[\s]([0-9\.\+\-]*)[\s]('.$strpiece[1][0].')[\s](re)([\s]*)/x', $pmid, $xmatches);
  21488. $currentxpos = $xmatches[1];
  21489. global $x_diff, $w_diff;
  21490. $x_diff = 0;
  21491. $w_diff = 0;
  21492. if ($this->isRTLTextDir()) { // RTL
  21493. if ($currentxpos < $textpos) {
  21494. $x_diff = ($spacewidth * ($nsmax - $lnstring[3][$strcount]));
  21495. $w_diff = ($spacewidth * $lnstring[2][$strcount]);
  21496. } else {
  21497. if ($strcount > 0) {
  21498. $x_diff = ($spacewidth * ($nsmax - $lnstring[3][($strcount - 1)]));
  21499. $w_diff = ($spacewidth * $lnstring[2][($strcount - 1)]);
  21500. }
  21501. }
  21502. } else { // LTR
  21503. if ($currentxpos > $textpos) {
  21504. if ($strcount > 0) {
  21505. $x_diff = ($spacewidth * $lnstring[3][($strcount - 1)]);
  21506. }
  21507. $w_diff = ($spacewidth * $lnstring[2][$strcount]);
  21508. } else {
  21509. if ($strcount > 1) {
  21510. $x_diff = ($spacewidth * $lnstring[3][($strcount - 2)]);
  21511. }
  21512. if ($strcount > 0) {
  21513. $w_diff = ($spacewidth * $lnstring[2][($strcount - 1)]);
  21514. }
  21515. }
  21516. }
  21517. $pmid = preg_replace_callback('/('.$xmatches[1].')[\s]('.$xmatches[2].')[\s]('.$xmatches[3].')[\s]('.$strpiece[1][0].')[\s](re)([\s]*)/x',
  21518. create_function('$matches', 'global $x_diff, $w_diff;
  21519. $newx = sprintf("%.2F",(floatval($matches[1]) + $x_diff));
  21520. $neww = sprintf("%.2F",(floatval($matches[3]) + $w_diff));
  21521. return "".$newx." ".$matches[2]." ".$neww." ".$matches[4]." x*#!#*x".$matches[5].$matches[6];'), $pmid, 1);
  21522. break;
  21523. }
  21524. case 'c': {
  21525. // get current X position
  21526. 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);
  21527. $currentxpos = $xmatches[1];
  21528. // justify block
  21529. $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',
  21530. create_function('$matches', 'global $spacew;
  21531. $newx1 = sprintf("%.3F",(floatval($matches[1]) + $spacew));
  21532. $newx2 = sprintf("%.3F",(floatval($matches[3]) + $spacew));
  21533. $newx3 = sprintf("%.3F",(floatval($matches[5]) + $spacew));
  21534. return "".$newx1." ".$matches[2]." ".$newx2." ".$matches[4]." ".$newx3." ".$matches[6]." x*#!#*x".$matches[7].$matches[8];'), $pmid, 1);
  21535. break;
  21536. }
  21537. }
  21538. // shift the annotations and links
  21539. $cxpos = ($currentxpos / $this->k);
  21540. $lmpos = ($this->lMargin + $this->cell_padding['L'] + $this->feps);
  21541. if ($this->inxobj) {
  21542. // we are inside an XObject template
  21543. foreach ($this->xobjects[$this->xobjid]['annotations'] as $pak => $pac) {
  21544. if (($pac['y'] >= $minstartliney) AND (($pac['x'] * $this->k) >= ($currentxpos - $this->feps)) AND (($pac['x'] * $this->k) <= ($currentxpos + $this->feps))) {
  21545. if ($cxpos > $lmpos) {
  21546. $this->xobjects[$this->xobjid]['annotations'][$pak]['x'] += ($spacew / $this->k);
  21547. $this->xobjects[$this->xobjid]['annotations'][$pak]['w'] += (($spacewidth * $pac['numspaces']) / $this->k);
  21548. } else {
  21549. $this->xobjects[$this->xobjid]['annotations'][$pak]['w'] += (($spacewidth * $pac['numspaces']) / $this->k);
  21550. }
  21551. break;
  21552. }
  21553. }
  21554. } elseif (isset($this->PageAnnots[$this->page])) {
  21555. foreach ($this->PageAnnots[$this->page] as $pak => $pac) {
  21556. if (($pac['y'] >= $minstartliney) AND (($pac['x'] * $this->k) >= ($currentxpos - $this->feps)) AND (($pac['x'] * $this->k) <= ($currentxpos + $this->feps))) {
  21557. if ($cxpos > $lmpos) {
  21558. $this->PageAnnots[$this->page][$pak]['x'] += ($spacew / $this->k);
  21559. $this->PageAnnots[$this->page][$pak]['w'] += (($spacewidth * $pac['numspaces']) / $this->k);
  21560. } else {
  21561. $this->PageAnnots[$this->page][$pak]['w'] += (($spacewidth * $pac['numspaces']) / $this->k);
  21562. }
  21563. break;
  21564. }
  21565. }
  21566. }
  21567. } // end of while
  21568. // remove markers
  21569. $pmid = str_replace('x*#!#*x', '', $pmid);
  21570. if ($this->isUnicodeFont()) {
  21571. // multibyte characters
  21572. $spacew = $spacewidthu;
  21573. if ($this->font_stretching != 100) {
  21574. // word spacing is affected by stretching
  21575. $spacew /= ($this->font_stretching / 100);
  21576. }
  21577. $pmidtemp = $pmid;
  21578. // escape special characters
  21579. $pmidtemp = preg_replace('/[\\\][\(]/x', '\\#!#OP#!#', $pmidtemp);
  21580. $pmidtemp = preg_replace('/[\\\][\)]/x', '\\#!#CP#!#', $pmidtemp);
  21581. $pmid = preg_replace_callback("/\[\(([^\)]*)\)\]/x",
  21582. create_function('$matches', 'global $spacew;
  21583. $matches[1] = str_replace("#!#OP#!#", "(", $matches[1]);
  21584. $matches[1] = str_replace("#!#CP#!#", ")", $matches[1]);
  21585. return "[(".str_replace(chr(0).chr(32), ") ".sprintf("%.3F", $spacew)." (", $matches[1]).")]";'), $pmidtemp);
  21586. if ($this->inxobj) {
  21587. // we are inside an XObject template
  21588. $this->xobjects[$this->xobjid]['outdata'] = $pstart."\n".$pmid."\n".$pend;
  21589. } else {
  21590. $this->setPageBuffer($startlinepage, $pstart."\n".$pmid."\n".$pend);
  21591. }
  21592. $endlinepos = strlen($pstart."\n".$pmid."\n");
  21593. } else {
  21594. // non-unicode (single-byte characters)
  21595. if ($this->font_stretching != 100) {
  21596. // word spacing (Tw) is affected by stretching
  21597. $spacewidth /= ($this->font_stretching / 100);
  21598. }
  21599. $rs = sprintf('%.3F Tw', $spacewidth);
  21600. $pmid = preg_replace("/\[\(/x", $rs.' [(', $pmid);
  21601. if ($this->inxobj) {
  21602. // we are inside an XObject template
  21603. $this->xobjects[$this->xobjid]['outdata'] = $pstart."\n".$pmid."\nBT 0 Tw ET\n".$pend;
  21604. } else {
  21605. $this->setPageBuffer($startlinepage, $pstart."\n".$pmid."\nBT 0 Tw ET\n".$pend);
  21606. }
  21607. $endlinepos = strlen($pstart."\n".$pmid."\nBT 0 Tw ET\n");
  21608. }
  21609. }
  21610. } // end of J
  21611. } // end if $startlinex
  21612. if (($t_x != 0) OR ($yshift < 0)) {
  21613. // shift the line
  21614. $trx = sprintf('1 0 0 1 %.3F %.3F cm', ($t_x * $this->k), ($yshift * $this->k));
  21615. $pstart .= "\nq\n".$trx."\n".$pmid."\nQ\n";
  21616. $endlinepos = strlen($pstart);
  21617. if ($this->inxobj) {
  21618. // we are inside an XObject template
  21619. $this->xobjects[$this->xobjid]['outdata'] = $pstart.$pend;
  21620. foreach ($this->xobjects[$this->xobjid]['annotations'] as $pak => $pac) {
  21621. if ($pak >= $pask) {
  21622. $this->xobjects[$this->xobjid]['annotations'][$pak]['x'] += $t_x;
  21623. $this->xobjects[$this->xobjid]['annotations'][$pak]['y'] -= $yshift;
  21624. }
  21625. }
  21626. } else {
  21627. $this->setPageBuffer($startlinepage, $pstart.$pend);
  21628. // shift the annotations and links
  21629. if (isset($this->PageAnnots[$this->page])) {
  21630. foreach ($this->PageAnnots[$this->page] as $pak => $pac) {
  21631. if ($pak >= $pask) {
  21632. $this->PageAnnots[$this->page][$pak]['x'] += $t_x;
  21633. $this->PageAnnots[$this->page][$pak]['y'] -= $yshift;
  21634. }
  21635. }
  21636. }
  21637. }
  21638. $this->y -= $yshift;
  21639. }
  21640. }
  21641. $pbrk = $this->checkPageBreak($this->lasth);
  21642. $this->newline = false;
  21643. $startlinex = $this->x;
  21644. $startliney = $this->y;
  21645. if ($dom[$dom[$key]['parent']]['value'] == 'sup') {
  21646. $startliney -= ((0.3 * $this->FontSizePt) / $this->k);
  21647. } elseif ($dom[$dom[$key]['parent']]['value'] == 'sub') {
  21648. $startliney -= (($this->FontSizePt / 0.7) / $this->k);
  21649. } else {
  21650. $minstartliney = $startliney;
  21651. $maxbottomliney = ($this->y + (($fontsize * $this->cell_height_ratio) / $this->k));
  21652. }
  21653. $startlinepage = $this->page;
  21654. if (isset($endlinepos) AND (!$pbrk)) {
  21655. $startlinepos = $endlinepos;
  21656. } else {
  21657. if ($this->inxobj) {
  21658. // we are inside an XObject template
  21659. $startlinepos = strlen($this->xobjects[$this->xobjid]['outdata']);
  21660. } elseif (!$this->InFooter) {
  21661. if (isset($this->footerlen[$this->page])) {
  21662. $this->footerpos[$this->page] = $this->pagelen[$this->page] - $this->footerlen[$this->page];
  21663. } else {
  21664. $this->footerpos[$this->page] = $this->pagelen[$this->page];
  21665. }
  21666. $startlinepos = $this->footerpos[$this->page];
  21667. } else {
  21668. $startlinepos = $this->pagelen[$this->page];
  21669. }
  21670. }
  21671. unset($endlinepos);
  21672. $plalign = $lalign;
  21673. if (isset($this->PageAnnots[$this->page])) {
  21674. $pask = count($this->PageAnnots[$this->page]);
  21675. } else {
  21676. $pask = 0;
  21677. }
  21678. if (!($dom[$key]['tag'] AND !$dom[$key]['opening'] AND ($dom[$key]['value'] == 'table')
  21679. AND (isset($this->emptypagemrk[$this->page]))
  21680. AND ($this->emptypagemrk[$this->page] == $this->pagelen[$this->page]))) {
  21681. $this->SetFont($fontname, $fontstyle, $fontsize);
  21682. if ($wfill) {
  21683. $this->SetFillColorArray($this->bgcolor);
  21684. }
  21685. }
  21686. } // end newline
  21687. if (isset($opentagpos)) {
  21688. unset($opentagpos);
  21689. }
  21690. if ($dom[$key]['tag']) {
  21691. if ($dom[$key]['opening']) {
  21692. // get text indentation (if any)
  21693. if (isset($dom[$key]['text-indent']) AND $dom[$key]['block']) {
  21694. $this->textindent = $dom[$key]['text-indent'];
  21695. $this->newline = true;
  21696. }
  21697. // table
  21698. if ($dom[$key]['value'] == 'table') {
  21699. // available page width
  21700. if ($this->rtl) {
  21701. $wtmp = $this->x - $this->lMargin;
  21702. } else {
  21703. $wtmp = $this->w - $this->rMargin - $this->x;
  21704. }
  21705. // get cell spacing
  21706. if (isset($dom[$key]['attribute']['cellspacing'])) {
  21707. $clsp = $this->getHTMLUnitToUnits($dom[$key]['attribute']['cellspacing'], 1, 'px');
  21708. $cellspacing = array('H' => $clsp, 'V' => $clsp);
  21709. } elseif (isset($dom[$key]['border-spacing'])) {
  21710. $cellspacing = $dom[$key]['border-spacing'];
  21711. } else {
  21712. $cellspacing = array('H' => 0, 'V' => 0);
  21713. }
  21714. // table width
  21715. if (isset($dom[$key]['width'])) {
  21716. $table_width = $this->getHTMLUnitToUnits($dom[$key]['width'], $wtmp, 'px');
  21717. } else {
  21718. $table_width = $wtmp;
  21719. }
  21720. $table_width -= (2 * $cellspacing['H']);
  21721. if (!$this->inthead) {
  21722. $this->y += $cellspacing['V'];
  21723. }
  21724. if ($this->rtl) {
  21725. $cellspacingx = -$cellspacing['H'];
  21726. } else {
  21727. $cellspacingx = $cellspacing['H'];
  21728. }
  21729. // total table width without cellspaces
  21730. $table_columns_width = ($table_width - ($cellspacing['H'] * ($dom[$key]['cols'] - 1)));
  21731. // minimum column width
  21732. $table_min_column_width = ($table_columns_width / $dom[$key]['cols']);
  21733. // array of custom column widths
  21734. $table_colwidths = array_fill(0, $dom[$key]['cols'], $table_min_column_width);
  21735. }
  21736. // table row
  21737. if ($dom[$key]['value'] == 'tr') {
  21738. // reset column counter
  21739. $colid = 0;
  21740. }
  21741. // table cell
  21742. if (($dom[$key]['value'] == 'td') OR ($dom[$key]['value'] == 'th')) {
  21743. $trid = $dom[$key]['parent'];
  21744. $table_el = $dom[$trid]['parent'];
  21745. if (!isset($dom[$table_el]['cols'])) {
  21746. $dom[$table_el]['cols'] = $dom[$trid]['cols'];
  21747. }
  21748. // store border info
  21749. $tdborder = 0;
  21750. if (isset($dom[$key]['border']) AND !empty($dom[$key]['border'])) {
  21751. $tdborder = $dom[$key]['border'];
  21752. }
  21753. $colspan = $dom[$key]['attribute']['colspan'];
  21754. $old_cell_padding = $this->cell_padding;
  21755. if (isset($dom[($dom[$trid]['parent'])]['attribute']['cellpadding'])) {
  21756. $crclpd = $this->getHTMLUnitToUnits($dom[($dom[$trid]['parent'])]['attribute']['cellpadding'], 1, 'px');
  21757. $current_cell_padding = array('L' => $crclpd, 'T' => $crclpd, 'R' => $crclpd, 'B' => $crclpd);
  21758. } elseif (isset($dom[($dom[$trid]['parent'])]['padding'])) {
  21759. $current_cell_padding = $dom[($dom[$trid]['parent'])]['padding'];
  21760. } else {
  21761. $current_cell_padding = array('L' => 0, 'T' => 0, 'R' => 0, 'B' => 0);
  21762. }
  21763. $this->cell_padding = $current_cell_padding;
  21764. if (isset($dom[$key]['height'])) {
  21765. // minimum cell height
  21766. $cellh = $this->getHTMLUnitToUnits($dom[$key]['height'], 0, 'px');
  21767. } else {
  21768. $cellh = 0;
  21769. }
  21770. if (isset($dom[$key]['content'])) {
  21771. $cell_content = stripslashes($dom[$key]['content']);
  21772. } else {
  21773. $cell_content = '&nbsp;';
  21774. }
  21775. $tagtype = $dom[$key]['value'];
  21776. $parentid = $key;
  21777. while (($key < $maxel) AND (!(($dom[$key]['tag']) AND (!$dom[$key]['opening']) AND ($dom[$key]['value'] == $tagtype) AND ($dom[$key]['parent'] == $parentid)))) {
  21778. // move $key index forward
  21779. ++$key;
  21780. }
  21781. if (!isset($dom[$trid]['startpage'])) {
  21782. $dom[$trid]['startpage'] = $this->page;
  21783. } else {
  21784. $this->setPage($dom[$trid]['startpage']);
  21785. }
  21786. if (!isset($dom[$trid]['startcolumn'])) {
  21787. $dom[$trid]['startcolumn'] = $this->current_column;
  21788. } elseif ($this->current_column != $dom[$trid]['startcolumn']) {
  21789. $tmpx = $this->x;
  21790. $this->selectColumn($dom[$trid]['startcolumn']);
  21791. $this->x = $tmpx;
  21792. }
  21793. if (!isset($dom[$trid]['starty'])) {
  21794. $dom[$trid]['starty'] = $this->y;
  21795. } else {
  21796. $this->y = $dom[$trid]['starty'];
  21797. }
  21798. if (!isset($dom[$trid]['startx'])) {
  21799. $dom[$trid]['startx'] = $this->x;
  21800. $this->x += $cellspacingx;
  21801. } else {
  21802. $this->x += ($cellspacingx / 2);
  21803. }
  21804. if (isset($dom[$parentid]['attribute']['rowspan'])) {
  21805. $rowspan = intval($dom[$parentid]['attribute']['rowspan']);
  21806. } else {
  21807. $rowspan = 1;
  21808. }
  21809. // skip row-spanned cells started on the previous rows
  21810. if (isset($dom[$table_el]['rowspans'])) {
  21811. $rsk = 0;
  21812. $rskmax = count($dom[$table_el]['rowspans']);
  21813. while ($rsk < $rskmax) {
  21814. $trwsp = $dom[$table_el]['rowspans'][$rsk];
  21815. $rsstartx = $trwsp['startx'];
  21816. $rsendx = $trwsp['endx'];
  21817. // account for margin changes
  21818. if ($trwsp['startpage'] < $this->page) {
  21819. if (($this->rtl) AND ($this->pagedim[$this->page]['orm'] != $this->pagedim[$trwsp['startpage']]['orm'])) {
  21820. $dl = ($this->pagedim[$this->page]['orm'] - $this->pagedim[$trwsp['startpage']]['orm']);
  21821. $rsstartx -= $dl;
  21822. $rsendx -= $dl;
  21823. } elseif ((!$this->rtl) AND ($this->pagedim[$this->page]['olm'] != $this->pagedim[$trwsp['startpage']]['olm'])) {
  21824. $dl = ($this->pagedim[$this->page]['olm'] - $this->pagedim[$trwsp['startpage']]['olm']);
  21825. $rsstartx += $dl;
  21826. $rsendx += $dl;
  21827. }
  21828. }
  21829. if (($trwsp['rowspan'] > 0)
  21830. AND ($rsstartx > ($this->x - $cellspacing['H'] - $current_cell_padding['L'] - $this->feps))
  21831. AND ($rsstartx < ($this->x + $cellspacing['H'] + $current_cell_padding['R'] + $this->feps))
  21832. AND (($trwsp['starty'] < ($this->y - $this->feps)) OR ($trwsp['startpage'] < $this->page) OR ($trwsp['startcolumn'] < $this->current_column))) {
  21833. // set the starting X position of the current cell
  21834. $this->x = $rsendx + $cellspacingx;
  21835. // increment column indicator
  21836. $colid += $trwsp['colspan'];
  21837. if (($trwsp['rowspan'] == 1)
  21838. AND (isset($dom[$trid]['endy']))
  21839. AND (isset($dom[$trid]['endpage']))
  21840. AND (isset($dom[$trid]['endcolumn']))
  21841. AND ($trwsp['endpage'] == $dom[$trid]['endpage'])
  21842. AND ($trwsp['endcolumn'] == $dom[$trid]['endcolumn'])) {
  21843. // set ending Y position for row
  21844. $dom[$table_el]['rowspans'][$rsk]['endy'] = max($dom[$trid]['endy'], $trwsp['endy']);
  21845. $dom[$trid]['endy'] = $dom[$table_el]['rowspans'][$rsk]['endy'];
  21846. }
  21847. $rsk = 0;
  21848. } else {
  21849. ++$rsk;
  21850. }
  21851. }
  21852. }
  21853. if (isset($dom[$parentid]['width'])) {
  21854. // user specified width
  21855. $cellw = $this->getHTMLUnitToUnits($dom[$parentid]['width'], $table_columns_width, 'px');
  21856. $tmpcw = ($cellw / $colspan);
  21857. for ($i = 0; $i < $colspan; ++$i) {
  21858. $table_colwidths[($colid + $i)] = $tmpcw;
  21859. }
  21860. } else {
  21861. // inherit column width
  21862. $cellw = 0;
  21863. for ($i = 0; $i < $colspan; ++$i) {
  21864. $cellw += $table_colwidths[($colid + $i)];
  21865. }
  21866. }
  21867. $cellw += (($colspan - 1) * $cellspacing['H']);
  21868. // increment column indicator
  21869. $colid += $colspan;
  21870. // add rowspan information to table element
  21871. if ($rowspan > 1) {
  21872. $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));
  21873. }
  21874. $cellid = array_push($dom[$trid]['cellpos'], array('startx' => $this->x));
  21875. if ($rowspan > 1) {
  21876. $dom[$trid]['cellpos'][($cellid - 1)]['rowspanid'] = ($trsid - 1);
  21877. }
  21878. // push background colors
  21879. if (isset($dom[$parentid]['bgcolor']) AND ($dom[$parentid]['bgcolor'] !== false)) {
  21880. $dom[$trid]['cellpos'][($cellid - 1)]['bgcolor'] = $dom[$parentid]['bgcolor'];
  21881. }
  21882. // store border info
  21883. if (isset($tdborder) AND !empty($tdborder)) {
  21884. $dom[$trid]['cellpos'][($cellid - 1)]['border'] = $tdborder;
  21885. }
  21886. $prevLastH = $this->lasth;
  21887. // store some info for multicolumn mode
  21888. if ($this->rtl) {
  21889. $this->colxshift['x'] = $this->w - $this->x - $this->rMargin;
  21890. } else {
  21891. $this->colxshift['x'] = $this->x - $this->lMargin;
  21892. }
  21893. $this->colxshift['s'] = $cellspacing;
  21894. $this->colxshift['p'] = $current_cell_padding;
  21895. // ****** write the cell content ******
  21896. $this->MultiCell($cellw, $cellh, $cell_content, false, $lalign, false, 2, '', '', true, 0, true, true, 0, 'T', false);
  21897. // restore some values
  21898. $this->colxshift = array('x' => 0, 's' => array('H' => 0, 'V' => 0), 'p' => array('L' => 0, 'T' => 0, 'R' => 0, 'B' => 0));
  21899. $this->lasth = $prevLastH;
  21900. $this->cell_padding = $old_cell_padding;
  21901. $dom[$trid]['cellpos'][($cellid - 1)]['endx'] = $this->x;
  21902. // update the end of row position
  21903. if ($rowspan <= 1) {
  21904. if (isset($dom[$trid]['endy'])) {
  21905. if (($this->page == $dom[$trid]['endpage']) AND ($this->current_column == $dom[$trid]['endcolumn'])) {
  21906. $dom[$trid]['endy'] = max($this->y, $dom[$trid]['endy']);
  21907. } elseif (($this->page > $dom[$trid]['endpage']) OR ($this->current_column > $dom[$trid]['endcolumn'])) {
  21908. $dom[$trid]['endy'] = $this->y;
  21909. }
  21910. } else {
  21911. $dom[$trid]['endy'] = $this->y;
  21912. }
  21913. if (isset($dom[$trid]['endpage'])) {
  21914. $dom[$trid]['endpage'] = max($this->page, $dom[$trid]['endpage']);
  21915. } else {
  21916. $dom[$trid]['endpage'] = $this->page;
  21917. }
  21918. if (isset($dom[$trid]['endcolumn'])) {
  21919. $dom[$trid]['endcolumn'] = max($this->current_column, $dom[$trid]['endcolumn']);
  21920. } else {
  21921. $dom[$trid]['endcolumn'] = $this->current_column;
  21922. }
  21923. } else {
  21924. // account for row-spanned cells
  21925. $dom[$table_el]['rowspans'][($trsid - 1)]['endx'] = $this->x;
  21926. $dom[$table_el]['rowspans'][($trsid - 1)]['endy'] = $this->y;
  21927. $dom[$table_el]['rowspans'][($trsid - 1)]['endpage'] = $this->page;
  21928. $dom[$table_el]['rowspans'][($trsid - 1)]['endcolumn'] = $this->current_column;
  21929. }
  21930. if (isset($dom[$table_el]['rowspans'])) {
  21931. // update endy and endpage on rowspanned cells
  21932. foreach ($dom[$table_el]['rowspans'] as $k => $trwsp) {
  21933. if ($trwsp['rowspan'] > 0) {
  21934. if (isset($dom[$trid]['endpage'])) {
  21935. if (($trwsp['endpage'] == $dom[$trid]['endpage']) AND ($trwsp['endcolumn'] == $dom[$trid]['endcolumn'])) {
  21936. $dom[$table_el]['rowspans'][$k]['endy'] = max($dom[$trid]['endy'], $trwsp['endy']);
  21937. } elseif (($trwsp['endpage'] < $dom[$trid]['endpage']) OR ($trwsp['endcolumn'] < $dom[$trid]['endcolumn'])) {
  21938. $dom[$table_el]['rowspans'][$k]['endy'] = $dom[$trid]['endy'];
  21939. $dom[$table_el]['rowspans'][$k]['endpage'] = $dom[$trid]['endpage'];
  21940. $dom[$table_el]['rowspans'][$k]['endcolumn'] = $dom[$trid]['endcolumn'];
  21941. } else {
  21942. $dom[$trid]['endy'] = $this->pagedim[$dom[$trid]['endpage']]['hk'] - $this->pagedim[$dom[$trid]['endpage']]['bm'];
  21943. }
  21944. }
  21945. }
  21946. }
  21947. }
  21948. $this->x += ($cellspacingx / 2);
  21949. } else {
  21950. // opening tag (or self-closing tag)
  21951. if (!isset($opentagpos)) {
  21952. if ($this->inxobj) {
  21953. // we are inside an XObject template
  21954. $opentagpos = strlen($this->xobjects[$this->xobjid]['outdata']);
  21955. } elseif (!$this->InFooter) {
  21956. if (isset($this->footerlen[$this->page])) {
  21957. $this->footerpos[$this->page] = $this->pagelen[$this->page] - $this->footerlen[$this->page];
  21958. } else {
  21959. $this->footerpos[$this->page] = $this->pagelen[$this->page];
  21960. }
  21961. $opentagpos = $this->footerpos[$this->page];
  21962. }
  21963. }
  21964. $dom = $this->openHTMLTagHandler($dom, $key, $cell);
  21965. }
  21966. } else { // closing tag
  21967. $prev_numpages = $this->numpages;
  21968. $old_bordermrk = $this->bordermrk[$this->page];
  21969. $dom = $this->closeHTMLTagHandler($dom, $key, $cell, $maxbottomliney);
  21970. if ($this->bordermrk[$this->page] > $old_bordermrk) {
  21971. $startlinepos += ($this->bordermrk[$this->page] - $old_bordermrk);
  21972. }
  21973. if ($prev_numpages > $this->numpages) {
  21974. $startlinepage = $this->page;
  21975. }
  21976. }
  21977. } elseif (strlen($dom[$key]['value']) > 0) {
  21978. // print list-item
  21979. if (!$this->empty_string($this->lispacer) AND ($this->lispacer != '^')) {
  21980. $this->SetFont($pfontname, $pfontstyle, $pfontsize);
  21981. $this->resetLastH();
  21982. $minstartliney = $this->y;
  21983. $maxbottomliney = ($startliney + ($this->FontSize * $this->cell_height_ratio));
  21984. $this->putHtmlListBullet($this->listnum, $this->lispacer, $pfontsize);
  21985. $this->SetFont($curfontname, $curfontstyle, $curfontsize);
  21986. $this->resetLastH();
  21987. if (is_numeric($pfontsize) AND ($pfontsize > 0) AND is_numeric($curfontsize) AND ($curfontsize > 0) AND ($pfontsize != $curfontsize)) {
  21988. $pfontascent = $this->getFontAscent($pfontname, $pfontstyle, $pfontsize);
  21989. $pfontdescent = $this->getFontDescent($pfontname, $pfontstyle, $pfontsize);
  21990. $this->y += ((($pfontsize - $curfontsize) * $this->cell_height_ratio / $this->k) + $pfontascent - $curfontascent - $pfontdescent + $curfontdescent) / 2;
  21991. $minstartliney = min($this->y, $minstartliney);
  21992. $maxbottomliney = max(($this->y + (($pfontsize * $this->cell_height_ratio) / $this->k)), $maxbottomliney);
  21993. }
  21994. }
  21995. // text
  21996. $this->htmlvspace = 0;
  21997. if ((!$this->premode) AND $this->isRTLTextDir()) {
  21998. // reverse spaces order
  21999. $lsp = ''; // left spaces
  22000. $rsp = ''; // right spaces
  22001. if (preg_match('/^('.$this->re_space['p'].'+)/'.$this->re_space['m'], $dom[$key]['value'], $matches)) {
  22002. $lsp = $matches[1];
  22003. }
  22004. if (preg_match('/('.$this->re_space['p'].'+)$/'.$this->re_space['m'], $dom[$key]['value'], $matches)) {
  22005. $rsp = $matches[1];
  22006. }
  22007. $dom[$key]['value'] = $rsp.$this->stringTrim($dom[$key]['value']).$lsp;
  22008. }
  22009. if ($newline) {
  22010. if (!$this->premode) {
  22011. $prelen = strlen($dom[$key]['value']);
  22012. if ($this->isRTLTextDir()) {
  22013. // right trim except non-breaking space
  22014. $dom[$key]['value'] = $this->stringRightTrim($dom[$key]['value']);
  22015. } else {
  22016. // left trim except non-breaking space
  22017. $dom[$key]['value'] = $this->stringLeftTrim($dom[$key]['value']);
  22018. }
  22019. $postlen = strlen($dom[$key]['value']);
  22020. if (($postlen == 0) AND ($prelen > 0)) {
  22021. $dom[$key]['trimmed_space'] = true;
  22022. }
  22023. }
  22024. $newline = false;
  22025. $firstblock = true;
  22026. } else {
  22027. $firstblock = false;
  22028. // replace empty multiple spaces string with a single space
  22029. $dom[$key]['value'] = preg_replace('/^'.$this->re_space['p'].'+$/'.$this->re_space['m'], chr(32), $dom[$key]['value']);
  22030. }
  22031. $strrest = '';
  22032. if ($this->rtl) {
  22033. $this->x -= $this->textindent;
  22034. } else {
  22035. $this->x += $this->textindent;
  22036. }
  22037. if (!isset($dom[$key]['trimmed_space']) OR !$dom[$key]['trimmed_space']) {
  22038. $strlinelen = $this->GetStringWidth($dom[$key]['value']);
  22039. if (!empty($this->HREF) AND (isset($this->HREF['url']))) {
  22040. // HTML <a> Link
  22041. $hrefcolor = '';
  22042. if (isset($dom[($dom[$key]['parent'])]['fgcolor']) AND ($dom[($dom[$key]['parent'])]['fgcolor'] !== false)) {
  22043. $hrefcolor = $dom[($dom[$key]['parent'])]['fgcolor'];
  22044. }
  22045. $hrefstyle = -1;
  22046. if (isset($dom[($dom[$key]['parent'])]['fontstyle']) AND ($dom[($dom[$key]['parent'])]['fontstyle'] !== false)) {
  22047. $hrefstyle = $dom[($dom[$key]['parent'])]['fontstyle'];
  22048. }
  22049. $strrest = $this->addHtmlLink($this->HREF['url'], $dom[$key]['value'], $wfill, true, $hrefcolor, $hrefstyle, true);
  22050. } else {
  22051. $wadj = 0; // space to leave for block continuity
  22052. if ($this->rtl) {
  22053. $cwa = $this->x - $this->lMargin;
  22054. } else {
  22055. $cwa = $this->w - $this->rMargin - $this->x;
  22056. }
  22057. if (($strlinelen < $cwa) AND (isset($dom[($key + 1)])) AND ($dom[($key + 1)]['tag']) AND (!$dom[($key + 1)]['block'])) {
  22058. // check the next text blocks for continuity
  22059. $nkey = ($key + 1);
  22060. $write_block = true;
  22061. $same_textdir = true;
  22062. $tmp_fontname = $this->FontFamily;
  22063. $tmp_fontstyle = $this->FontStyle;
  22064. $tmp_fontsize = $this->FontSizePt;
  22065. while ($write_block AND isset($dom[$nkey])) {
  22066. if ($dom[$nkey]['tag']) {
  22067. if ($dom[$nkey]['block']) {
  22068. // end of block
  22069. $write_block = false;
  22070. }
  22071. $tmp_fontname = isset($dom[$nkey]['fontname']) ? $dom[$nkey]['fontname'] : $this->FontFamily;
  22072. $tmp_fontstyle = isset($dom[$nkey]['fontstyle']) ? $dom[$nkey]['fontstyle'] : $this->FontStyle;
  22073. $tmp_fontsize = isset($dom[$nkey]['fontsize']) ? $dom[$nkey]['fontsize'] : $this->FontSizePt;
  22074. $same_textdir = ($dom[$nkey]['dir'] == $dom[$key]['dir']);
  22075. } else {
  22076. $nextstr = preg_split('/'.$this->re_space['p'].'+/'.$this->re_space['m'], $dom[$nkey]['value']);
  22077. if (isset($nextstr[0]) AND $same_textdir) {
  22078. $wadj += $this->GetStringWidth($nextstr[0], $tmp_fontname, $tmp_fontstyle, $tmp_fontsize);
  22079. }
  22080. if (isset($nextstr[1])) {
  22081. $write_block = false;
  22082. }
  22083. }
  22084. ++$nkey;
  22085. }
  22086. }
  22087. if (($wadj > 0) AND (($strlinelen + $wadj) >= $cwa)) {
  22088. $wadj = 0;
  22089. $nextstr = preg_split('/'.$this->re_space['p'].'/'.$this->re_space['m'], $dom[$key]['value']);
  22090. $numblks = count($nextstr);
  22091. if ($numblks > 1) {
  22092. // try to split on blank spaces
  22093. $wadj = ($cwa - $strlinelen + $this->GetStringWidth($nextstr[($numblks - 1)]));
  22094. }
  22095. }
  22096. // check for reversed text direction
  22097. if (($wadj > 0) AND (($this->rtl AND ($this->tmprtl === 'L')) OR (!$this->rtl AND ($this->tmprtl === 'R')))) {
  22098. // LTR text on RTL direction or RTL text on LTR direction
  22099. $reverse_dir = true;
  22100. $this->rtl = !$this->rtl;
  22101. $revshift = ($strlinelen + $wadj + 0.000001); // add little quantity for rounding problems
  22102. if ($this->rtl) {
  22103. $this->x += $revshift;
  22104. } else {
  22105. $this->x -= $revshift;
  22106. }
  22107. $xws = $this->x;
  22108. }
  22109. // ****** write only until the end of the line and get the rest ******
  22110. $strrest = $this->Write($this->lasth, $dom[$key]['value'], '', $wfill, '', false, 0, true, $firstblock, 0, $wadj);
  22111. // restore default direction
  22112. if ($reverse_dir AND ($wadj == 0)) {
  22113. $this->x = $xws;
  22114. $this->rtl = !$this->rtl;
  22115. $reverse_dir = false;
  22116. }
  22117. }
  22118. }
  22119. $this->textindent = 0;
  22120. if (strlen($strrest) > 0) {
  22121. // store the remaining string on the previous $key position
  22122. $this->newline = true;
  22123. if ($strrest == $dom[$key]['value']) {
  22124. // used to avoid infinite loop
  22125. ++$loop;
  22126. } else {
  22127. $loop = 0;
  22128. }
  22129. $dom[$key]['value'] = $strrest;
  22130. if ($cell) {
  22131. if ($this->rtl) {
  22132. $this->x -= $this->cell_padding['R'];
  22133. } else {
  22134. $this->x += $this->cell_padding['L'];
  22135. }
  22136. }
  22137. if ($loop < 3) {
  22138. --$key;
  22139. }
  22140. } else {
  22141. $loop = 0;
  22142. }
  22143. }
  22144. ++$key;
  22145. 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')) {
  22146. // check if we are on a new page or on a new column
  22147. if ((!$undo) AND (($this->y < $this->start_transaction_y) OR (($dom[$key]['value'] == 'tr') AND ($dom[($dom[$key]['parent'])]['endy'] < $this->start_transaction_y)))) {
  22148. // we are on a new page or on a new column and the total object height is less than the available vertical space.
  22149. // restore previous object
  22150. $this->rollbackTransaction(true);
  22151. // restore previous values
  22152. foreach ($this_method_vars as $vkey => $vval) {
  22153. $$vkey = $vval;
  22154. }
  22155. // add a page (or trig AcceptPageBreak() for multicolumn mode)
  22156. $pre_y = $this->y;
  22157. if ((!$this->checkPageBreak($this->PageBreakTrigger + 1)) AND ($this->y < $pre_y)) {
  22158. $startliney = $this->y;
  22159. }
  22160. $undo = true; // avoid infinite loop
  22161. } else {
  22162. $undo = false;
  22163. }
  22164. }
  22165. } // end for each $key
  22166. // align the last line
  22167. if (isset($startlinex)) {
  22168. $yshift = ($minstartliney - $startliney);
  22169. if (($yshift > 0) OR ($this->page > $startlinepage)) {
  22170. $yshift = 0;
  22171. }
  22172. $t_x = 0;
  22173. // the last line must be shifted to be aligned as requested
  22174. $linew = abs($this->endlinex - $startlinex);
  22175. if ($this->inxobj) {
  22176. // we are inside an XObject template
  22177. $pstart = substr($this->xobjects[$this->xobjid]['outdata'], 0, $startlinepos);
  22178. if (isset($opentagpos)) {
  22179. $midpos = $opentagpos;
  22180. } else {
  22181. $midpos = 0;
  22182. }
  22183. if ($midpos > 0) {
  22184. $pmid = substr($this->xobjects[$this->xobjid]['outdata'], $startlinepos, ($midpos - $startlinepos));
  22185. $pend = substr($this->xobjects[$this->xobjid]['outdata'], $midpos);
  22186. } else {
  22187. $pmid = substr($this->xobjects[$this->xobjid]['outdata'], $startlinepos);
  22188. $pend = '';
  22189. }
  22190. } else {
  22191. $pstart = substr($this->getPageBuffer($startlinepage), 0, $startlinepos);
  22192. if (isset($opentagpos) AND isset($this->footerlen[$startlinepage]) AND (!$this->InFooter)) {
  22193. $this->footerpos[$startlinepage] = $this->pagelen[$startlinepage] - $this->footerlen[$startlinepage];
  22194. $midpos = min($opentagpos, $this->footerpos[$startlinepage]);
  22195. } elseif (isset($opentagpos)) {
  22196. $midpos = $opentagpos;
  22197. } elseif (isset($this->footerlen[$startlinepage]) AND (!$this->InFooter)) {
  22198. $this->footerpos[$startlinepage] = $this->pagelen[$startlinepage] - $this->footerlen[$startlinepage];
  22199. $midpos = $this->footerpos[$startlinepage];
  22200. } else {
  22201. $midpos = 0;
  22202. }
  22203. if ($midpos > 0) {
  22204. $pmid = substr($this->getPageBuffer($startlinepage), $startlinepos, ($midpos - $startlinepos));
  22205. $pend = substr($this->getPageBuffer($startlinepage), $midpos);
  22206. } else {
  22207. $pmid = substr($this->getPageBuffer($startlinepage), $startlinepos);
  22208. $pend = '';
  22209. }
  22210. }
  22211. if ((isset($plalign) AND ((($plalign == 'C') OR (($plalign == 'R') AND (!$this->rtl)) OR (($plalign == 'L') AND ($this->rtl)))))) {
  22212. // calculate shifting amount
  22213. $tw = $w;
  22214. if ($this->lMargin != $prevlMargin) {
  22215. $tw += ($prevlMargin - $this->lMargin);
  22216. }
  22217. if ($this->rMargin != $prevrMargin) {
  22218. $tw += ($prevrMargin - $this->rMargin);
  22219. }
  22220. $one_space_width = $this->GetStringWidth(chr(32));
  22221. $no = 0; // number of spaces on a line contained on a single block
  22222. if ($this->isRTLTextDir()) { // RTL
  22223. // remove left space if exist
  22224. $pos1 = $this->revstrpos($pmid, '[(');
  22225. if ($pos1 > 0) {
  22226. $pos1 = intval($pos1);
  22227. if ($this->isUnicodeFont()) {
  22228. $pos2 = intval($this->revstrpos($pmid, '[('.chr(0).chr(32)));
  22229. $spacelen = 2;
  22230. } else {
  22231. $pos2 = intval($this->revstrpos($pmid, '[('.chr(32)));
  22232. $spacelen = 1;
  22233. }
  22234. if ($pos1 == $pos2) {
  22235. $pmid = substr($pmid, 0, ($pos1 + 2)).substr($pmid, ($pos1 + 2 + $spacelen));
  22236. if (substr($pmid, $pos1, 4) == '[()]') {
  22237. $linew -= $one_space_width;
  22238. } elseif ($pos1 == strpos($pmid, '[(')) {
  22239. $no = 1;
  22240. }
  22241. }
  22242. }
  22243. } else { // LTR
  22244. // remove right space if exist
  22245. $pos1 = $this->revstrpos($pmid, ')]');
  22246. if ($pos1 > 0) {
  22247. $pos1 = intval($pos1);
  22248. if ($this->isUnicodeFont()) {
  22249. $pos2 = intval($this->revstrpos($pmid, chr(0).chr(32).')]')) + 2;
  22250. $spacelen = 2;
  22251. } else {
  22252. $pos2 = intval($this->revstrpos($pmid, chr(32).')]')) + 1;
  22253. $spacelen = 1;
  22254. }
  22255. if ($pos1 == $pos2) {
  22256. $pmid = substr($pmid, 0, ($pos1 - $spacelen)).substr($pmid, $pos1);
  22257. $linew -= $one_space_width;
  22258. }
  22259. }
  22260. }
  22261. $mdiff = ($tw - $linew);
  22262. if ($plalign == 'C') {
  22263. if ($this->rtl) {
  22264. $t_x = -($mdiff / 2);
  22265. } else {
  22266. $t_x = ($mdiff / 2);
  22267. }
  22268. } elseif ($plalign == 'R') {
  22269. // right alignment on LTR document
  22270. $t_x = $mdiff;
  22271. } elseif ($plalign == 'L') {
  22272. // left alignment on RTL document
  22273. $t_x = -$mdiff;
  22274. }
  22275. } // end if startlinex
  22276. if (($t_x != 0) OR ($yshift < 0)) {
  22277. // shift the line
  22278. $trx = sprintf('1 0 0 1 %.3F %.3F cm', ($t_x * $this->k), ($yshift * $this->k));
  22279. $pstart .= "\nq\n".$trx."\n".$pmid."\nQ\n";
  22280. $endlinepos = strlen($pstart);
  22281. if ($this->inxobj) {
  22282. // we are inside an XObject template
  22283. $this->xobjects[$this->xobjid]['outdata'] = $pstart.$pend;
  22284. foreach ($this->xobjects[$this->xobjid]['annotations'] as $pak => $pac) {
  22285. if ($pak >= $pask) {
  22286. $this->xobjects[$this->xobjid]['annotations'][$pak]['x'] += $t_x;
  22287. $this->xobjects[$this->xobjid]['annotations'][$pak]['y'] -= $yshift;
  22288. }
  22289. }
  22290. } else {
  22291. $this->setPageBuffer($startlinepage, $pstart.$pend);
  22292. // shift the annotations and links
  22293. if (isset($this->PageAnnots[$this->page])) {
  22294. foreach ($this->PageAnnots[$this->page] as $pak => $pac) {
  22295. if ($pak >= $pask) {
  22296. $this->PageAnnots[$this->page][$pak]['x'] += $t_x;
  22297. $this->PageAnnots[$this->page][$pak]['y'] -= $yshift;
  22298. }
  22299. }
  22300. }
  22301. }
  22302. $this->y -= $yshift;
  22303. $yshift = 0;
  22304. }
  22305. }
  22306. // restore previous values
  22307. $this->setGraphicVars($gvars);
  22308. if ($this->num_columns > 1) {
  22309. $this->selectColumn();
  22310. } elseif ($this->page > $prevPage) {
  22311. $this->lMargin = $this->pagedim[$this->page]['olm'];
  22312. $this->rMargin = $this->pagedim[$this->page]['orm'];
  22313. }
  22314. // restore previous list state
  22315. $this->cell_height_ratio = $prev_cell_height_ratio;
  22316. $this->listnum = $prev_listnum;
  22317. $this->listordered = $prev_listordered;
  22318. $this->listcount = $prev_listcount;
  22319. $this->lispacer = $prev_lispacer;
  22320. if ($ln AND (!($cell AND ($dom[$key-1]['value'] == 'table')))) {
  22321. $this->Ln($this->lasth);
  22322. if ($this->y < $maxbottomliney) {
  22323. $this->y = $maxbottomliney;
  22324. }
  22325. }
  22326. unset($dom);
  22327. }
  22328. /**
  22329. * Process opening tags.
  22330. * @param $dom (array) html dom array
  22331. * @param $key (int) current element id
  22332. * @param $cell (boolean) if true add the default left (or right if RTL) padding to each new line (default false).
  22333. * @return $dom array
  22334. * @protected
  22335. */
  22336. protected function openHTMLTagHandler($dom, $key, $cell) {
  22337. $tag = $dom[$key];
  22338. $parent = $dom[($dom[$key]['parent'])];
  22339. $firsttag = ($key == 1);
  22340. // check for text direction attribute
  22341. if (isset($tag['dir'])) {
  22342. $this->setTempRTL($tag['dir']);
  22343. } else {
  22344. $this->tmprtl = false;
  22345. }
  22346. if ($tag['block']) {
  22347. $hbz = 0; // distance from y to line bottom
  22348. $hb = 0; // vertical space between block tags
  22349. // calculate vertical space for block tags
  22350. if (isset($this->tagvspaces[$tag['value']][0]['h']) AND ($this->tagvspaces[$tag['value']][0]['h'] >= 0)) {
  22351. $cur_h = $this->tagvspaces[$tag['value']][0]['h'];
  22352. } elseif (isset($tag['fontsize'])) {
  22353. $cur_h = ($tag['fontsize'] / $this->k) * $this->cell_height_ratio;
  22354. } else {
  22355. $cur_h = $this->FontSize * $this->cell_height_ratio;
  22356. }
  22357. if (isset($this->tagvspaces[$tag['value']][0]['n'])) {
  22358. $n = $this->tagvspaces[$tag['value']][0]['n'];
  22359. } elseif (preg_match('/[h][0-9]/', $tag['value']) > 0) {
  22360. $n = 0.6;
  22361. } else {
  22362. $n = 1;
  22363. }
  22364. if ((!isset($this->tagvspaces[$tag['value']])) AND (in_array($tag['value'], array('div', 'dt', 'dd', 'li', 'br')))) {
  22365. $hb = 0;
  22366. } else {
  22367. $hb = ($n * $cur_h);
  22368. }
  22369. if (($this->htmlvspace <= 0) AND ($n > 0)) {
  22370. if (isset($parent['fontsize'])) {
  22371. $hbz = (($parent['fontsize'] / $this->k) * $this->cell_height_ratio);
  22372. } else {
  22373. $hbz = $this->FontSize * $this->cell_height_ratio;
  22374. }
  22375. }
  22376. }
  22377. // Opening tag
  22378. switch($tag['value']) {
  22379. case 'table': {
  22380. $cp = 0;
  22381. $cs = 0;
  22382. $dom[$key]['rowspans'] = array();
  22383. if (!isset($dom[$key]['attribute']['nested']) OR ($dom[$key]['attribute']['nested'] != 'true')) {
  22384. // set table header
  22385. if (!$this->empty_string($dom[$key]['thead'])) {
  22386. // set table header
  22387. $this->thead = $dom[$key]['thead'];
  22388. if (!isset($this->theadMargins) OR (empty($this->theadMargins))) {
  22389. $this->theadMargins = array();
  22390. $this->theadMargins['cell_padding'] = $this->cell_padding;
  22391. $this->theadMargins['lmargin'] = $this->lMargin;
  22392. $this->theadMargins['rmargin'] = $this->rMargin;
  22393. $this->theadMargins['page'] = $this->page;
  22394. $this->theadMargins['cell'] = $cell;
  22395. }
  22396. }
  22397. }
  22398. // store current margins and page
  22399. $dom[$key]['old_cell_padding'] = $this->cell_padding;
  22400. if (isset($tag['attribute']['cellpadding'])) {
  22401. $pad = $this->getHTMLUnitToUnits($tag['attribute']['cellpadding'], 1, 'px');
  22402. $this->SetCellPadding($pad);
  22403. } elseif (isset($tag['padding'])) {
  22404. $this->cell_padding = $tag['padding'];
  22405. }
  22406. if (isset($tag['attribute']['cellspacing'])) {
  22407. $cs = $this->getHTMLUnitToUnits($tag['attribute']['cellspacing'], 1, 'px');
  22408. } elseif (isset($tag['border-spacing'])) {
  22409. $cs = $tag['border-spacing']['V'];
  22410. }
  22411. $prev_y = $this->y;
  22412. if ($this->checkPageBreak(((2 * $cp) + (2 * $cs) + $this->lasth), '', false) OR ($this->y < $prev_y)) {
  22413. $this->inthead = true;
  22414. // add a page (or trig AcceptPageBreak() for multicolumn mode)
  22415. $this->checkPageBreak($this->PageBreakTrigger + 1);
  22416. }
  22417. break;
  22418. }
  22419. case 'tr': {
  22420. // array of columns positions
  22421. $dom[$key]['cellpos'] = array();
  22422. break;
  22423. }
  22424. case 'hr': {
  22425. if ((isset($tag['height'])) AND ($tag['height'] != '')) {
  22426. $hrHeight = $this->getHTMLUnitToUnits($tag['height'], 1, 'px');
  22427. } else {
  22428. $hrHeight = $this->GetLineWidth();
  22429. }
  22430. $this->addHTMLVertSpace($hbz, ($hrHeight / 2), $cell, $firsttag);
  22431. $x = $this->GetX();
  22432. $y = $this->GetY();
  22433. $wtmp = $this->w - $this->lMargin - $this->rMargin;
  22434. if ($cell) {
  22435. $wtmp -= ($this->cell_padding['L'] + $this->cell_padding['R']);
  22436. }
  22437. if ((isset($tag['width'])) AND ($tag['width'] != '')) {
  22438. $hrWidth = $this->getHTMLUnitToUnits($tag['width'], $wtmp, 'px');
  22439. } else {
  22440. $hrWidth = $wtmp;
  22441. }
  22442. $prevlinewidth = $this->GetLineWidth();
  22443. $this->SetLineWidth($hrHeight);
  22444. $this->Line($x, $y, $x + $hrWidth, $y);
  22445. $this->SetLineWidth($prevlinewidth);
  22446. $this->addHTMLVertSpace(($hrHeight / 2), 0, $cell, !isset($dom[($key + 1)]));
  22447. break;
  22448. }
  22449. case 'a': {
  22450. if (array_key_exists('href', $tag['attribute'])) {
  22451. $this->HREF['url'] = $tag['attribute']['href'];
  22452. }
  22453. break;
  22454. }
  22455. case 'img': {
  22456. if (isset($tag['attribute']['src'])) {
  22457. if ($tag['attribute']['src']{0} === '@') {
  22458. // data stream
  22459. $tag['attribute']['src'] = '@'.base64_decode(substr($tag['attribute']['src'], 1));
  22460. $type = '';
  22461. } else {
  22462. // check for images without protocol
  22463. if (preg_match('%^/{2}%', $tag['attribute']['src'])) {
  22464. $tag['attribute']['src'] = 'http:'.$tag['attribute']['src'];
  22465. }
  22466. // replace relative path with real server path
  22467. if (($tag['attribute']['src'][0] == '/') AND !empty($_SERVER['DOCUMENT_ROOT']) AND ($_SERVER['DOCUMENT_ROOT'] != '/')) {
  22468. $findroot = strpos($tag['attribute']['src'], $_SERVER['DOCUMENT_ROOT']);
  22469. if (($findroot === false) OR ($findroot > 1)) {
  22470. if (substr($_SERVER['DOCUMENT_ROOT'], -1) == '/') {
  22471. $tag['attribute']['src'] = substr($_SERVER['DOCUMENT_ROOT'], 0, -1).$tag['attribute']['src'];
  22472. } else {
  22473. $tag['attribute']['src'] = $_SERVER['DOCUMENT_ROOT'].$tag['attribute']['src'];
  22474. }
  22475. }
  22476. }
  22477. $tag['attribute']['src'] = htmlspecialchars_decode(urldecode($tag['attribute']['src']));
  22478. $type = $this->getImageFileType($tag['attribute']['src']);
  22479. $testscrtype = @parse_url($tag['attribute']['src']);
  22480. if (!isset($testscrtype['query']) OR empty($testscrtype['query'])) {
  22481. // convert URL to server path
  22482. $tag['attribute']['src'] = str_replace(K_PATH_URL, K_PATH_MAIN, $tag['attribute']['src']);
  22483. }
  22484. }
  22485. if (!isset($tag['width'])) {
  22486. $tag['width'] = 0;
  22487. }
  22488. if (!isset($tag['height'])) {
  22489. $tag['height'] = 0;
  22490. }
  22491. //if (!isset($tag['attribute']['align'])) {
  22492. // the only alignment supported is "bottom"
  22493. // further development is required for other modes.
  22494. $tag['attribute']['align'] = 'bottom';
  22495. //}
  22496. switch($tag['attribute']['align']) {
  22497. case 'top': {
  22498. $align = 'T';
  22499. break;
  22500. }
  22501. case 'middle': {
  22502. $align = 'M';
  22503. break;
  22504. }
  22505. case 'bottom': {
  22506. $align = 'B';
  22507. break;
  22508. }
  22509. default: {
  22510. $align = 'B';
  22511. break;
  22512. }
  22513. }
  22514. $prevy = $this->y;
  22515. $xpos = $this->x;
  22516. $imglink = '';
  22517. if (isset($this->HREF['url']) AND !$this->empty_string($this->HREF['url'])) {
  22518. $imglink = $this->HREF['url'];
  22519. if ($imglink{0} == '#') {
  22520. // convert url to internal link
  22521. $lnkdata = explode(',', $imglink);
  22522. if (isset($lnkdata[0])) {
  22523. $page = intval(substr($lnkdata[0], 1));
  22524. if (empty($page) OR ($page <= 0)) {
  22525. $page = $this->page;
  22526. }
  22527. if (isset($lnkdata[1]) AND (strlen($lnkdata[1]) > 0)) {
  22528. $lnky = floatval($lnkdata[1]);
  22529. } else {
  22530. $lnky = 0;
  22531. }
  22532. $imglink = $this->AddLink();
  22533. $this->SetLink($imglink, $lnky, $page);
  22534. }
  22535. }
  22536. }
  22537. $border = 0;
  22538. if (isset($tag['border']) AND !empty($tag['border'])) {
  22539. // currently only support 1 (frame) or a combination of 'LTRB'
  22540. $border = $tag['border'];
  22541. }
  22542. $iw = '';
  22543. if (isset($tag['width'])) {
  22544. $iw = $this->getHTMLUnitToUnits($tag['width'], 1, 'px', false);
  22545. }
  22546. $ih = '';
  22547. if (isset($tag['height'])) {
  22548. $ih = $this->getHTMLUnitToUnits($tag['height'], 1, 'px', false);
  22549. }
  22550. if (($type == 'eps') OR ($type == 'ai')) {
  22551. $this->ImageEps($tag['attribute']['src'], $xpos, $this->y, $iw, $ih, $imglink, true, $align, '', $border, true);
  22552. } elseif ($type == 'svg') {
  22553. $this->ImageSVG($tag['attribute']['src'], $xpos, $this->y, $iw, $ih, $imglink, $align, '', $border, true);
  22554. } else {
  22555. $this->Image($tag['attribute']['src'], $xpos, $this->y, $iw, $ih, '', $imglink, $align, false, 300, '', false, false, $border, false, false, true);
  22556. }
  22557. switch($align) {
  22558. case 'T': {
  22559. $this->y = $prevy;
  22560. break;
  22561. }
  22562. case 'M': {
  22563. $this->y = (($this->img_rb_y + $prevy - ($tag['fontsize'] / $this->k)) / 2) ;
  22564. break;
  22565. }
  22566. case 'B': {
  22567. $this->y = $this->img_rb_y - ($tag['fontsize'] / $this->k);
  22568. break;
  22569. }
  22570. }
  22571. }
  22572. break;
  22573. }
  22574. case 'dl': {
  22575. ++$this->listnum;
  22576. if ($this->listnum == 1) {
  22577. $this->addHTMLVertSpace($hbz, $hb, $cell, $firsttag);
  22578. } else {
  22579. $this->addHTMLVertSpace(0, 0, $cell, $firsttag);
  22580. }
  22581. break;
  22582. }
  22583. case 'dt': {
  22584. $this->addHTMLVertSpace($hbz, $hb, $cell, $firsttag);
  22585. break;
  22586. }
  22587. case 'dd': {
  22588. if ($this->rtl) {
  22589. $this->rMargin += $this->listindent;
  22590. } else {
  22591. $this->lMargin += $this->listindent;
  22592. }
  22593. ++$this->listindentlevel;
  22594. $this->addHTMLVertSpace($hbz, $hb, $cell, $firsttag);
  22595. break;
  22596. }
  22597. case 'ul':
  22598. case 'ol': {
  22599. ++$this->listnum;
  22600. if ($tag['value'] == 'ol') {
  22601. $this->listordered[$this->listnum] = true;
  22602. } else {
  22603. $this->listordered[$this->listnum] = false;
  22604. }
  22605. if (isset($tag['attribute']['start'])) {
  22606. $this->listcount[$this->listnum] = intval($tag['attribute']['start']) - 1;
  22607. } else {
  22608. $this->listcount[$this->listnum] = 0;
  22609. }
  22610. if ($this->rtl) {
  22611. $this->rMargin += $this->listindent;
  22612. $this->x -= $this->listindent;
  22613. } else {
  22614. $this->lMargin += $this->listindent;
  22615. $this->x += $this->listindent;
  22616. }
  22617. ++$this->listindentlevel;
  22618. if ($this->listnum == 1) {
  22619. if ($key > 1) {
  22620. $this->addHTMLVertSpace($hbz, $hb, $cell, $firsttag);
  22621. }
  22622. } else {
  22623. $this->addHTMLVertSpace(0, 0, $cell, $firsttag);
  22624. }
  22625. break;
  22626. }
  22627. case 'li': {
  22628. if ($key > 2) {
  22629. $this->addHTMLVertSpace($hbz, $hb, $cell, $firsttag);
  22630. }
  22631. if ($this->listordered[$this->listnum]) {
  22632. // ordered item
  22633. if (isset($parent['attribute']['type']) AND !$this->empty_string($parent['attribute']['type'])) {
  22634. $this->lispacer = $parent['attribute']['type'];
  22635. } elseif (isset($parent['listtype']) AND !$this->empty_string($parent['listtype'])) {
  22636. $this->lispacer = $parent['listtype'];
  22637. } elseif (isset($this->lisymbol) AND !$this->empty_string($this->lisymbol)) {
  22638. $this->lispacer = $this->lisymbol;
  22639. } else {
  22640. $this->lispacer = '#';
  22641. }
  22642. ++$this->listcount[$this->listnum];
  22643. if (isset($tag['attribute']['value'])) {
  22644. $this->listcount[$this->listnum] = intval($tag['attribute']['value']);
  22645. }
  22646. } else {
  22647. // unordered item
  22648. if (isset($parent['attribute']['type']) AND !$this->empty_string($parent['attribute']['type'])) {
  22649. $this->lispacer = $parent['attribute']['type'];
  22650. } elseif (isset($parent['listtype']) AND !$this->empty_string($parent['listtype'])) {
  22651. $this->lispacer = $parent['listtype'];
  22652. } elseif (isset($this->lisymbol) AND !$this->empty_string($this->lisymbol)) {
  22653. $this->lispacer = $this->lisymbol;
  22654. } else {
  22655. $this->lispacer = '!';
  22656. }
  22657. }
  22658. break;
  22659. }
  22660. case 'blockquote': {
  22661. if ($this->rtl) {
  22662. $this->rMargin += $this->listindent;
  22663. } else {
  22664. $this->lMargin += $this->listindent;
  22665. }
  22666. ++$this->listindentlevel;
  22667. $this->addHTMLVertSpace($hbz, $hb, $cell, $firsttag);
  22668. break;
  22669. }
  22670. case 'br': {
  22671. $this->addHTMLVertSpace($hbz, $hb, $cell, $firsttag);
  22672. break;
  22673. }
  22674. case 'div': {
  22675. $this->addHTMLVertSpace($hbz, $hb, $cell, $firsttag);
  22676. break;
  22677. }
  22678. case 'p': {
  22679. $this->addHTMLVertSpace($hbz, $hb, $cell, $firsttag);
  22680. break;
  22681. }
  22682. case 'pre': {
  22683. $this->addHTMLVertSpace($hbz, $hb, $cell, $firsttag);
  22684. $this->premode = true;
  22685. break;
  22686. }
  22687. case 'sup': {
  22688. $this->SetXY($this->GetX(), $this->GetY() - ((0.7 * $this->FontSizePt) / $this->k));
  22689. break;
  22690. }
  22691. case 'sub': {
  22692. $this->SetXY($this->GetX(), $this->GetY() + ((0.3 * $this->FontSizePt) / $this->k));
  22693. break;
  22694. }
  22695. case 'h1':
  22696. case 'h2':
  22697. case 'h3':
  22698. case 'h4':
  22699. case 'h5':
  22700. case 'h6': {
  22701. $this->addHTMLVertSpace($hbz, $hb, $cell, $firsttag);
  22702. break;
  22703. }
  22704. // Form fields (since 4.8.000 - 2009-09-07)
  22705. case 'form': {
  22706. if (isset($tag['attribute']['action'])) {
  22707. $this->form_action = $tag['attribute']['action'];
  22708. } else {
  22709. $this->form_action = K_PATH_URL.$_SERVER['SCRIPT_NAME'];
  22710. }
  22711. if (isset($tag['attribute']['enctype'])) {
  22712. $this->form_enctype = $tag['attribute']['enctype'];
  22713. } else {
  22714. $this->form_enctype = 'application/x-www-form-urlencoded';
  22715. }
  22716. if (isset($tag['attribute']['method'])) {
  22717. $this->form_mode = $tag['attribute']['method'];
  22718. } else {
  22719. $this->form_mode = 'post';
  22720. }
  22721. break;
  22722. }
  22723. case 'input': {
  22724. if (isset($tag['attribute']['name']) AND !$this->empty_string($tag['attribute']['name'])) {
  22725. $name = $tag['attribute']['name'];
  22726. } else {
  22727. break;
  22728. }
  22729. $prop = array();
  22730. $opt = array();
  22731. if (isset($tag['attribute']['readonly']) AND !$this->empty_string($tag['attribute']['readonly'])) {
  22732. $prop['readonly'] = true;
  22733. }
  22734. if (isset($tag['attribute']['value']) AND !$this->empty_string($tag['attribute']['value'])) {
  22735. $value = $tag['attribute']['value'];
  22736. }
  22737. if (isset($tag['attribute']['maxlength']) AND !$this->empty_string($tag['attribute']['maxlength'])) {
  22738. $opt['maxlen'] = intval($tag['attribute']['value']);
  22739. }
  22740. $h = $this->FontSize * $this->cell_height_ratio;
  22741. if (isset($tag['attribute']['size']) AND !$this->empty_string($tag['attribute']['size'])) {
  22742. $w = intval($tag['attribute']['size']) * $this->GetStringWidth(chr(32)) * 2;
  22743. } else {
  22744. $w = $h;
  22745. }
  22746. if (isset($tag['attribute']['checked']) AND (($tag['attribute']['checked'] == 'checked') OR ($tag['attribute']['checked'] == 'true'))) {
  22747. $checked = true;
  22748. } else {
  22749. $checked = false;
  22750. }
  22751. if (isset($tag['align'])) {
  22752. switch ($tag['align']) {
  22753. case 'C': {
  22754. $opt['q'] = 1;
  22755. break;
  22756. }
  22757. case 'R': {
  22758. $opt['q'] = 2;
  22759. break;
  22760. }
  22761. case 'L':
  22762. default: {
  22763. break;
  22764. }
  22765. }
  22766. }
  22767. switch ($tag['attribute']['type']) {
  22768. case 'text': {
  22769. if (isset($value)) {
  22770. $opt['v'] = $value;
  22771. }
  22772. $this->TextField($name, $w, $h, $prop, $opt, '', '', false);
  22773. break;
  22774. }
  22775. case 'password': {
  22776. if (isset($value)) {
  22777. $opt['v'] = $value;
  22778. }
  22779. $prop['password'] = 'true';
  22780. $this->TextField($name, $w, $h, $prop, $opt, '', '', false);
  22781. break;
  22782. }
  22783. case 'checkbox': {
  22784. $this->CheckBox($name, $w, $checked, $prop, $opt, $value, '', '', false);
  22785. break;
  22786. }
  22787. case 'radio': {
  22788. $this->RadioButton($name, $w, $prop, $opt, $value, $checked, '', '', false);
  22789. break;
  22790. }
  22791. case 'submit': {
  22792. $w = $this->GetStringWidth($value) * 1.5;
  22793. $h *= 1.6;
  22794. $prop = array('lineWidth'=>1, 'borderStyle'=>'beveled', 'fillColor'=>array(196, 196, 196), 'strokeColor'=>array(255, 255, 255));
  22795. $action = array();
  22796. $action['S'] = 'SubmitForm';
  22797. $action['F'] = $this->form_action;
  22798. if ($this->form_enctype != 'FDF') {
  22799. $action['Flags'] = array('ExportFormat');
  22800. }
  22801. if ($this->form_mode == 'get') {
  22802. $action['Flags'] = array('GetMethod');
  22803. }
  22804. $this->Button($name, $w, $h, $value, $action, $prop, $opt, '', '', false);
  22805. break;
  22806. }
  22807. case 'reset': {
  22808. $w = $this->GetStringWidth($value) * 1.5;
  22809. $h *= 1.6;
  22810. $prop = array('lineWidth'=>1, 'borderStyle'=>'beveled', 'fillColor'=>array(196, 196, 196), 'strokeColor'=>array(255, 255, 255));
  22811. $this->Button($name, $w, $h, $value, array('S'=>'ResetForm'), $prop, $opt, '', '', false);
  22812. break;
  22813. }
  22814. case 'file': {
  22815. $prop['fileSelect'] = 'true';
  22816. $this->TextField($name, $w, $h, $prop, $opt, '', '', false);
  22817. if (!isset($value)) {
  22818. $value = '*';
  22819. }
  22820. $w = $this->GetStringWidth($value) * 2;
  22821. $h *= 1.2;
  22822. $prop = array('lineWidth'=>1, 'borderStyle'=>'beveled', 'fillColor'=>array(196, 196, 196), 'strokeColor'=>array(255, 255, 255));
  22823. $jsaction = 'var f=this.getField(\''.$name.'\'); f.browseForFileToSubmit();';
  22824. $this->Button('FB_'.$name, $w, $h, $value, $jsaction, $prop, $opt, '', '', false);
  22825. break;
  22826. }
  22827. case 'hidden': {
  22828. if (isset($value)) {
  22829. $opt['v'] = $value;
  22830. }
  22831. $opt['f'] = array('invisible', 'hidden');
  22832. $this->TextField($name, 0, 0, $prop, $opt, '', '', false);
  22833. break;
  22834. }
  22835. case 'image': {
  22836. // THIS TYPE MUST BE FIXED
  22837. if (isset($tag['attribute']['src']) AND !$this->empty_string($tag['attribute']['src'])) {
  22838. $img = $tag['attribute']['src'];
  22839. } else {
  22840. break;
  22841. }
  22842. $value = 'img';
  22843. //$opt['mk'] = array('i'=>$img, 'tp'=>1, 'if'=>array('sw'=>'A', 's'=>'A', 'fb'=>false));
  22844. if (isset($tag['attribute']['onclick']) AND !empty($tag['attribute']['onclick'])) {
  22845. $jsaction = $tag['attribute']['onclick'];
  22846. } else {
  22847. $jsaction = '';
  22848. }
  22849. $this->Button($name, $w, $h, $value, $jsaction, $prop, $opt, '', '', false);
  22850. break;
  22851. }
  22852. case 'button': {
  22853. $w = $this->GetStringWidth($value) * 1.5;
  22854. $h *= 1.6;
  22855. $prop = array('lineWidth'=>1, 'borderStyle'=>'beveled', 'fillColor'=>array(196, 196, 196), 'strokeColor'=>array(255, 255, 255));
  22856. if (isset($tag['attribute']['onclick']) AND !empty($tag['attribute']['onclick'])) {
  22857. $jsaction = $tag['attribute']['onclick'];
  22858. } else {
  22859. $jsaction = '';
  22860. }
  22861. $this->Button($name, $w, $h, $value, $jsaction, $prop, $opt, '', '', false);
  22862. break;
  22863. }
  22864. }
  22865. break;
  22866. }
  22867. case 'textarea': {
  22868. $prop = array();
  22869. $opt = array();
  22870. if (isset($tag['attribute']['readonly']) AND !$this->empty_string($tag['attribute']['readonly'])) {
  22871. $prop['readonly'] = true;
  22872. }
  22873. if (isset($tag['attribute']['name']) AND !$this->empty_string($tag['attribute']['name'])) {
  22874. $name = $tag['attribute']['name'];
  22875. } else {
  22876. break;
  22877. }
  22878. if (isset($tag['attribute']['value']) AND !$this->empty_string($tag['attribute']['value'])) {
  22879. $opt['v'] = $tag['attribute']['value'];
  22880. }
  22881. if (isset($tag['attribute']['cols']) AND !$this->empty_string($tag['attribute']['cols'])) {
  22882. $w = intval($tag['attribute']['cols']) * $this->GetStringWidth(chr(32)) * 2;
  22883. } else {
  22884. $w = 40;
  22885. }
  22886. if (isset($tag['attribute']['rows']) AND !$this->empty_string($tag['attribute']['rows'])) {
  22887. $h = intval($tag['attribute']['rows']) * $this->FontSize * $this->cell_height_ratio;
  22888. } else {
  22889. $h = 10;
  22890. }
  22891. $prop['multiline'] = 'true';
  22892. $this->TextField($name, $w, $h, $prop, $opt, '', '', false);
  22893. break;
  22894. }
  22895. case 'select': {
  22896. $h = $this->FontSize * $this->cell_height_ratio;
  22897. if (isset($tag['attribute']['size']) AND !$this->empty_string($tag['attribute']['size'])) {
  22898. $h *= ($tag['attribute']['size'] + 1);
  22899. }
  22900. $prop = array();
  22901. $opt = array();
  22902. if (isset($tag['attribute']['name']) AND !$this->empty_string($tag['attribute']['name'])) {
  22903. $name = $tag['attribute']['name'];
  22904. } else {
  22905. break;
  22906. }
  22907. $w = 0;
  22908. if (isset($tag['attribute']['opt']) AND !$this->empty_string($tag['attribute']['opt'])) {
  22909. $options = explode('#!NwL!#', $tag['attribute']['opt']);
  22910. $values = array();
  22911. foreach ($options as $val) {
  22912. if (strpos($val, '#!TaB!#') !== false) {
  22913. $opts = explode('#!TaB!#', $val);
  22914. $values[] = $opts;
  22915. $w = max($w, $this->GetStringWidth($opts[1]));
  22916. } else {
  22917. $values[] = $val;
  22918. $w = max($w, $this->GetStringWidth($val));
  22919. }
  22920. }
  22921. } else {
  22922. break;
  22923. }
  22924. $w *= 2;
  22925. if (isset($tag['attribute']['multiple']) AND ($tag['attribute']['multiple']='multiple')) {
  22926. $prop['multipleSelection'] = 'true';
  22927. $this->ListBox($name, $w, $h, $values, $prop, $opt, '', '', false);
  22928. } else {
  22929. $this->ComboBox($name, $w, $h, $values, $prop, $opt, '', '', false);
  22930. }
  22931. break;
  22932. }
  22933. case 'tcpdf': {
  22934. if (defined('K_TCPDF_CALLS_IN_HTML') AND (K_TCPDF_CALLS_IN_HTML === true)) {
  22935. // Special tag used to call TCPDF methods
  22936. if (isset($tag['attribute']['method'])) {
  22937. $tcpdf_method = $tag['attribute']['method'];
  22938. if (method_exists($this, $tcpdf_method)) {
  22939. if (isset($tag['attribute']['params']) AND (!empty($tag['attribute']['params']))) {
  22940. $params = unserialize(urldecode($tag['attribute']['params']));
  22941. call_user_func_array(array($this, $tcpdf_method), $params);
  22942. } else {
  22943. $this->$tcpdf_method();
  22944. }
  22945. $this->newline = true;
  22946. }
  22947. }
  22948. }
  22949. break;
  22950. }
  22951. default: {
  22952. break;
  22953. }
  22954. }
  22955. // define tags that support borders and background colors
  22956. $bordertags = array('blockquote','br','dd','dl','div','dt','h1','h2','h3','h4','h5','h6','hr','li','ol','p','pre','ul','tcpdf','table');
  22957. if (in_array($tag['value'], $bordertags)) {
  22958. // set border
  22959. $dom[$key]['borderposition'] = $this->getBorderStartPosition();
  22960. }
  22961. if ($dom[$key]['self'] AND isset($dom[$key]['attribute']['pagebreakafter'])) {
  22962. $pba = $dom[$key]['attribute']['pagebreakafter'];
  22963. // check for pagebreak
  22964. if (($pba == 'true') OR ($pba == 'left') OR ($pba == 'right')) {
  22965. // add a page (or trig AcceptPageBreak() for multicolumn mode)
  22966. $this->checkPageBreak($this->PageBreakTrigger + 1);
  22967. }
  22968. if ((($pba == 'left') AND (((!$this->rtl) AND (($this->page % 2) == 0)) OR (($this->rtl) AND (($this->page % 2) != 0))))
  22969. OR (($pba == 'right') AND (((!$this->rtl) AND (($this->page % 2) != 0)) OR (($this->rtl) AND (($this->page % 2) == 0))))) {
  22970. // add a page (or trig AcceptPageBreak() for multicolumn mode)
  22971. $this->checkPageBreak($this->PageBreakTrigger + 1);
  22972. }
  22973. }
  22974. return $dom;
  22975. }
  22976. /**
  22977. * Process closing tags.
  22978. * @param $dom (array) html dom array
  22979. * @param $key (int) current element id
  22980. * @param $cell (boolean) if true add the default left (or right if RTL) padding to each new line (default false).
  22981. * @param $maxbottomliney (int) maximum y value of current line
  22982. * @return $dom array
  22983. * @protected
  22984. */
  22985. protected function closeHTMLTagHandler($dom, $key, $cell, $maxbottomliney=0) {
  22986. $tag = $dom[$key];
  22987. $parent = $dom[($dom[$key]['parent'])];
  22988. $lasttag = ((!isset($dom[($key + 1)])) OR ((!isset($dom[($key + 2)])) AND ($dom[($key + 1)]['value'] == 'marker')));
  22989. $in_table_head = false;
  22990. // maximum x position (used to draw borders)
  22991. if ($this->rtl) {
  22992. $xmax = $this->w;
  22993. } else {
  22994. $xmax = 0;
  22995. }
  22996. if ($tag['block']) {
  22997. $hbz = 0; // distance from y to line bottom
  22998. $hb = 0; // vertical space between block tags
  22999. // calculate vertical space for block tags
  23000. if (isset($this->tagvspaces[$tag['value']][1]['h']) AND ($this->tagvspaces[$tag['value']][1]['h'] >= 0)) {
  23001. $pre_h = $this->tagvspaces[$tag['value']][1]['h'];
  23002. } elseif (isset($parent['fontsize'])) {
  23003. $pre_h = (($parent['fontsize'] / $this->k) * $this->cell_height_ratio);
  23004. } else {
  23005. $pre_h = $this->FontSize * $this->cell_height_ratio;
  23006. }
  23007. if (isset($this->tagvspaces[$tag['value']][1]['n'])) {
  23008. $n = $this->tagvspaces[$tag['value']][1]['n'];
  23009. } elseif (preg_match('/[h][0-9]/', $tag['value']) > 0) {
  23010. $n = 0.6;
  23011. } else {
  23012. $n = 1;
  23013. }
  23014. if ((!isset($this->tagvspaces[$tag['value']])) AND ($tag['value'] == 'div')) {
  23015. $hb = 0;
  23016. } else {
  23017. $hb = ($n * $pre_h);
  23018. }
  23019. if ($maxbottomliney > $this->PageBreakTrigger) {
  23020. $hbz = ($this->FontSize * $this->cell_height_ratio);
  23021. } elseif ($this->y < $maxbottomliney) {
  23022. $hbz = ($maxbottomliney - $this->y);
  23023. }
  23024. }
  23025. // Closing tag
  23026. switch($tag['value']) {
  23027. case 'tr': {
  23028. $table_el = $dom[($dom[$key]['parent'])]['parent'];
  23029. if (!isset($parent['endy'])) {
  23030. $dom[($dom[$key]['parent'])]['endy'] = $this->y;
  23031. $parent['endy'] = $this->y;
  23032. }
  23033. if (!isset($parent['endpage'])) {
  23034. $dom[($dom[$key]['parent'])]['endpage'] = $this->page;
  23035. $parent['endpage'] = $this->page;
  23036. }
  23037. if (!isset($parent['endcolumn'])) {
  23038. $dom[($dom[$key]['parent'])]['endcolumn'] = $this->current_column;
  23039. $parent['endcolumn'] = $this->current_column;
  23040. }
  23041. // update row-spanned cells
  23042. if (isset($dom[$table_el]['rowspans'])) {
  23043. foreach ($dom[$table_el]['rowspans'] as $k => $trwsp) {
  23044. $dom[$table_el]['rowspans'][$k]['rowspan'] -= 1;
  23045. if ($dom[$table_el]['rowspans'][$k]['rowspan'] == 0) {
  23046. if (($dom[$table_el]['rowspans'][$k]['endpage'] == $parent['endpage']) AND ($dom[$table_el]['rowspans'][$k]['endcolumn'] == $parent['endcolumn'])) {
  23047. $dom[($dom[$key]['parent'])]['endy'] = max($dom[$table_el]['rowspans'][$k]['endy'], $parent['endy']);
  23048. } elseif (($dom[$table_el]['rowspans'][$k]['endpage'] > $parent['endpage']) OR ($dom[$table_el]['rowspans'][$k]['endcolumn'] > $parent['endcolumn'])) {
  23049. $dom[($dom[$key]['parent'])]['endy'] = $dom[$table_el]['rowspans'][$k]['endy'];
  23050. $dom[($dom[$key]['parent'])]['endpage'] = $dom[$table_el]['rowspans'][$k]['endpage'];
  23051. $dom[($dom[$key]['parent'])]['endcolumn'] = $dom[$table_el]['rowspans'][$k]['endcolumn'];
  23052. }
  23053. }
  23054. }
  23055. // report new endy and endpage to the rowspanned cells
  23056. foreach ($dom[$table_el]['rowspans'] as $k => $trwsp) {
  23057. if ($dom[$table_el]['rowspans'][$k]['rowspan'] == 0) {
  23058. $dom[$table_el]['rowspans'][$k]['endpage'] = max($dom[$table_el]['rowspans'][$k]['endpage'], $dom[($dom[$key]['parent'])]['endpage']);
  23059. $dom[($dom[$key]['parent'])]['endpage'] = $dom[$table_el]['rowspans'][$k]['endpage'];
  23060. $dom[$table_el]['rowspans'][$k]['endcolumn'] = max($dom[$table_el]['rowspans'][$k]['endcolumn'], $dom[($dom[$key]['parent'])]['endcolumn']);
  23061. $dom[($dom[$key]['parent'])]['endcolumn'] = $dom[$table_el]['rowspans'][$k]['endcolumn'];
  23062. $dom[$table_el]['rowspans'][$k]['endy'] = max($dom[$table_el]['rowspans'][$k]['endy'], $dom[($dom[$key]['parent'])]['endy']);
  23063. $dom[($dom[$key]['parent'])]['endy'] = $dom[$table_el]['rowspans'][$k]['endy'];
  23064. }
  23065. }
  23066. // update remaining rowspanned cells
  23067. foreach ($dom[$table_el]['rowspans'] as $k => $trwsp) {
  23068. if ($dom[$table_el]['rowspans'][$k]['rowspan'] == 0) {
  23069. $dom[$table_el]['rowspans'][$k]['endpage'] = $dom[($dom[$key]['parent'])]['endpage'];
  23070. $dom[$table_el]['rowspans'][$k]['endcolumn'] = $dom[($dom[$key]['parent'])]['endcolumn'];
  23071. $dom[$table_el]['rowspans'][$k]['endy'] = $dom[($dom[$key]['parent'])]['endy'];
  23072. }
  23073. }
  23074. }
  23075. $this->setPage($dom[($dom[$key]['parent'])]['endpage']);
  23076. if ($this->num_columns > 1) {
  23077. $this->selectColumn($dom[($dom[$key]['parent'])]['endcolumn']);
  23078. }
  23079. $this->y = $dom[($dom[$key]['parent'])]['endy'];
  23080. if (isset($dom[$table_el]['attribute']['cellspacing'])) {
  23081. $this->y += $this->getHTMLUnitToUnits($dom[$table_el]['attribute']['cellspacing'], 1, 'px');
  23082. } elseif (isset($dom[$table_el]['border-spacing'])) {
  23083. $this->y += $dom[$table_el]['border-spacing']['V'];
  23084. }
  23085. $this->Ln(0, $cell);
  23086. if ($this->current_column == $parent['startcolumn']) {
  23087. $this->x = $parent['startx'];
  23088. }
  23089. // account for booklet mode
  23090. if ($this->page > $parent['startpage']) {
  23091. if (($this->rtl) AND ($this->pagedim[$this->page]['orm'] != $this->pagedim[$parent['startpage']]['orm'])) {
  23092. $this->x -= ($this->pagedim[$this->page]['orm'] - $this->pagedim[$parent['startpage']]['orm']);
  23093. } elseif ((!$this->rtl) AND ($this->pagedim[$this->page]['olm'] != $this->pagedim[$parent['startpage']]['olm'])) {
  23094. $this->x += ($this->pagedim[$this->page]['olm'] - $this->pagedim[$parent['startpage']]['olm']);
  23095. }
  23096. }
  23097. break;
  23098. }
  23099. case 'tablehead':
  23100. // closing tag used for the thead part
  23101. $in_table_head = true;
  23102. $this->inthead = false;
  23103. case 'table': {
  23104. $table_el = $parent;
  23105. // set default border
  23106. if (isset($table_el['attribute']['border']) AND ($table_el['attribute']['border'] > 0)) {
  23107. // set default border
  23108. $border = array('LTRB' => array('width' => $this->getCSSBorderWidth($table_el['attribute']['border']), 'cap'=>'square', 'join'=>'miter', 'dash'=> 0, 'color'=>array(0,0,0)));
  23109. } else {
  23110. $border = 0;
  23111. }
  23112. $default_border = $border;
  23113. // fix bottom line alignment of last line before page break
  23114. foreach ($dom[($dom[$key]['parent'])]['trids'] as $j => $trkey) {
  23115. // update row-spanned cells
  23116. if (isset($dom[($dom[$key]['parent'])]['rowspans'])) {
  23117. foreach ($dom[($dom[$key]['parent'])]['rowspans'] as $k => $trwsp) {
  23118. if ($trwsp['trid'] == $trkey) {
  23119. $dom[($dom[$key]['parent'])]['rowspans'][$k]['mrowspan'] -= 1;
  23120. }
  23121. if (isset($prevtrkey) AND ($trwsp['trid'] == $prevtrkey) AND ($trwsp['mrowspan'] >= 0)) {
  23122. $dom[($dom[$key]['parent'])]['rowspans'][$k]['trid'] = $trkey;
  23123. }
  23124. }
  23125. }
  23126. if (isset($prevtrkey) AND ($dom[$trkey]['startpage'] > $dom[$prevtrkey]['endpage'])) {
  23127. $pgendy = $this->pagedim[$dom[$prevtrkey]['endpage']]['hk'] - $this->pagedim[$dom[$prevtrkey]['endpage']]['bm'];
  23128. $dom[$prevtrkey]['endy'] = $pgendy;
  23129. // update row-spanned cells
  23130. if (isset($dom[($dom[$key]['parent'])]['rowspans'])) {
  23131. foreach ($dom[($dom[$key]['parent'])]['rowspans'] as $k => $trwsp) {
  23132. if (($trwsp['trid'] == $trkey) AND ($trwsp['mrowspan'] > 1) AND ($trwsp['endpage'] == $dom[$prevtrkey]['endpage'])) {
  23133. $dom[($dom[$key]['parent'])]['rowspans'][$k]['endy'] = $pgendy;
  23134. $dom[($dom[$key]['parent'])]['rowspans'][$k]['mrowspan'] = -1;
  23135. }
  23136. }
  23137. }
  23138. }
  23139. $prevtrkey = $trkey;
  23140. $table_el = $dom[($dom[$key]['parent'])];
  23141. }
  23142. // for each row
  23143. if (count($table_el['trids']) > 0) {
  23144. unset($xmax);
  23145. }
  23146. foreach ($table_el['trids'] as $j => $trkey) {
  23147. $parent = $dom[$trkey];
  23148. if (!isset($xmax)) {
  23149. $xmax = $parent['cellpos'][(count($parent['cellpos']) - 1)]['endx'];
  23150. }
  23151. // for each cell on the row
  23152. foreach ($parent['cellpos'] as $k => $cellpos) {
  23153. if (isset($cellpos['rowspanid']) AND ($cellpos['rowspanid'] >= 0)) {
  23154. $cellpos['startx'] = $table_el['rowspans'][($cellpos['rowspanid'])]['startx'];
  23155. $cellpos['endx'] = $table_el['rowspans'][($cellpos['rowspanid'])]['endx'];
  23156. $endy = $table_el['rowspans'][($cellpos['rowspanid'])]['endy'];
  23157. $startpage = $table_el['rowspans'][($cellpos['rowspanid'])]['startpage'];
  23158. $endpage = $table_el['rowspans'][($cellpos['rowspanid'])]['endpage'];
  23159. $startcolumn = $table_el['rowspans'][($cellpos['rowspanid'])]['startcolumn'];
  23160. $endcolumn = $table_el['rowspans'][($cellpos['rowspanid'])]['endcolumn'];
  23161. } else {
  23162. $endy = $parent['endy'];
  23163. $startpage = $parent['startpage'];
  23164. $endpage = $parent['endpage'];
  23165. $startcolumn = $parent['startcolumn'];
  23166. $endcolumn = $parent['endcolumn'];
  23167. }
  23168. if ($this->num_columns == 0) {
  23169. $this->num_columns = 1;
  23170. }
  23171. if (isset($cellpos['border'])) {
  23172. $border = $cellpos['border'];
  23173. }
  23174. if (isset($cellpos['bgcolor']) AND ($cellpos['bgcolor']) !== false) {
  23175. $this->SetFillColorArray($cellpos['bgcolor']);
  23176. $fill = true;
  23177. } else {
  23178. $fill = false;
  23179. }
  23180. $x = $cellpos['startx'];
  23181. $y = $parent['starty'];
  23182. $starty = $y;
  23183. $w = abs($cellpos['endx'] - $cellpos['startx']);
  23184. // get border modes
  23185. $border_start = $this->getBorderMode($border, $position='start');
  23186. $border_end = $this->getBorderMode($border, $position='end');
  23187. $border_middle = $this->getBorderMode($border, $position='middle');
  23188. // design borders around HTML cells.
  23189. for ($page = $startpage; $page <= $endpage; ++$page) { // for each page
  23190. $ccode = '';
  23191. $this->setPage($page);
  23192. if ($this->num_columns < 2) {
  23193. // single-column mode
  23194. $this->x = $x;
  23195. $this->y = $this->tMargin;
  23196. }
  23197. // account for margin changes
  23198. if ($page > $startpage) {
  23199. if (($this->rtl) AND ($this->pagedim[$page]['orm'] != $this->pagedim[$startpage]['orm'])) {
  23200. $this->x -= ($this->pagedim[$page]['orm'] - $this->pagedim[$startpage]['orm']);
  23201. } elseif ((!$this->rtl) AND ($this->pagedim[$page]['olm'] != $this->pagedim[$startpage]['olm'])) {
  23202. $this->x += ($this->pagedim[$page]['olm'] - $this->pagedim[$startpage]['olm']);
  23203. }
  23204. }
  23205. if ($startpage == $endpage) { // single page
  23206. $deltacol = 0;
  23207. $deltath = 0;
  23208. for ($column = $startcolumn; $column <= $endcolumn; ++$column) { // for each column
  23209. $this->selectColumn($column);
  23210. if ($startcolumn == $endcolumn) { // single column
  23211. $cborder = $border;
  23212. $h = $endy - $parent['starty'];
  23213. $this->y = $y;
  23214. $this->x = $x;
  23215. } elseif ($column == $startcolumn) { // first column
  23216. $cborder = $border_start;
  23217. $this->y = $starty;
  23218. $this->x = $x;
  23219. $h = $this->h - $this->y - $this->bMargin;
  23220. if ($this->rtl) {
  23221. $deltacol = $this->x + $this->rMargin - $this->w;
  23222. } else {
  23223. $deltacol = $this->x - $this->lMargin;
  23224. }
  23225. } elseif ($column == $endcolumn) { // end column
  23226. $cborder = $border_end;
  23227. if (isset($this->columns[$column]['th']['\''.$page.'\''])) {
  23228. $this->y = $this->columns[$column]['th']['\''.$page.'\''];
  23229. }
  23230. $this->x += $deltacol;
  23231. $h = $endy - $this->y;
  23232. } else { // middle column
  23233. $cborder = $border_middle;
  23234. if (isset($this->columns[$column]['th']['\''.$page.'\''])) {
  23235. $this->y = $this->columns[$column]['th']['\''.$page.'\''];
  23236. }
  23237. $this->x += $deltacol;
  23238. $h = $this->h - $this->y - $this->bMargin;
  23239. }
  23240. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
  23241. } // end for each column
  23242. } elseif ($page == $startpage) { // first page
  23243. $deltacol = 0;
  23244. $deltath = 0;
  23245. for ($column = $startcolumn; $column < $this->num_columns; ++$column) { // for each column
  23246. $this->selectColumn($column);
  23247. if ($column == $startcolumn) { // first column
  23248. $cborder = $border_start;
  23249. $this->y = $starty;
  23250. $this->x = $x;
  23251. $h = $this->h - $this->y - $this->bMargin;
  23252. if ($this->rtl) {
  23253. $deltacol = $this->x + $this->rMargin - $this->w;
  23254. } else {
  23255. $deltacol = $this->x - $this->lMargin;
  23256. }
  23257. } else { // middle column
  23258. $cborder = $border_middle;
  23259. if (isset($this->columns[$column]['th']['\''.$page.'\''])) {
  23260. $this->y = $this->columns[$column]['th']['\''.$page.'\''];
  23261. }
  23262. $this->x += $deltacol;
  23263. $h = $this->h - $this->y - $this->bMargin;
  23264. }
  23265. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
  23266. } // end for each column
  23267. } elseif ($page == $endpage) { // last page
  23268. $deltacol = 0;
  23269. $deltath = 0;
  23270. for ($column = 0; $column <= $endcolumn; ++$column) { // for each column
  23271. $this->selectColumn($column);
  23272. if ($column == $endcolumn) { // end column
  23273. $cborder = $border_end;
  23274. if (isset($this->columns[$column]['th']['\''.$page.'\''])) {
  23275. $this->y = $this->columns[$column]['th']['\''.$page.'\''];
  23276. }
  23277. $this->x += $deltacol;
  23278. $h = $endy - $this->y;
  23279. } else { // middle column
  23280. $cborder = $border_middle;
  23281. if (isset($this->columns[$column]['th']['\''.$page.'\''])) {
  23282. $this->y = $this->columns[$column]['th']['\''.$page.'\''];
  23283. }
  23284. $this->x += $deltacol;
  23285. $h = $this->h - $this->y - $this->bMargin;
  23286. }
  23287. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
  23288. } // end for each column
  23289. } else { // middle page
  23290. $deltacol = 0;
  23291. $deltath = 0;
  23292. for ($column = 0; $column < $this->num_columns; ++$column) { // for each column
  23293. $this->selectColumn($column);
  23294. $cborder = $border_middle;
  23295. if (isset($this->columns[$column]['th']['\''.$page.'\''])) {
  23296. $this->y = $this->columns[$column]['th']['\''.$page.'\''];
  23297. }
  23298. $this->x += $deltacol;
  23299. $h = $this->h - $this->y - $this->bMargin;
  23300. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
  23301. } // end for each column
  23302. }
  23303. if ($cborder OR $fill) {
  23304. $offsetlen = strlen($ccode);
  23305. // draw border and fill
  23306. if ($this->inxobj) {
  23307. // we are inside an XObject template
  23308. if (end($this->xobjects[$this->xobjid]['transfmrk']) !== false) {
  23309. $pagemarkkey = key($this->xobjects[$this->xobjid]['transfmrk']);
  23310. $pagemark = $this->xobjects[$this->xobjid]['transfmrk'][$pagemarkkey];
  23311. $this->xobjects[$this->xobjid]['transfmrk'][$pagemarkkey] += $offsetlen;
  23312. } else {
  23313. $pagemark = $this->xobjects[$this->xobjid]['intmrk'];
  23314. $this->xobjects[$this->xobjid]['intmrk'] += $offsetlen;
  23315. }
  23316. $pagebuff = $this->xobjects[$this->xobjid]['outdata'];
  23317. $pstart = substr($pagebuff, 0, $pagemark);
  23318. $pend = substr($pagebuff, $pagemark);
  23319. $this->xobjects[$this->xobjid]['outdata'] = $pstart.$ccode.$pend;
  23320. } else {
  23321. // draw border and fill
  23322. if (end($this->transfmrk[$this->page]) !== false) {
  23323. $pagemarkkey = key($this->transfmrk[$this->page]);
  23324. $pagemark = $this->transfmrk[$this->page][$pagemarkkey];
  23325. $this->transfmrk[$this->page][$pagemarkkey] += $offsetlen;
  23326. } elseif ($this->InFooter) {
  23327. $pagemark = $this->footerpos[$this->page];
  23328. $this->footerpos[$this->page] += $offsetlen;
  23329. } else {
  23330. $pagemark = $this->intmrk[$this->page];
  23331. $this->intmrk[$this->page] += $offsetlen;
  23332. }
  23333. $pagebuff = $this->getPageBuffer($this->page);
  23334. $pstart = substr($pagebuff, 0, $pagemark);
  23335. $pend = substr($pagebuff, $pagemark);
  23336. $this->setPageBuffer($this->page, $pstart.$ccode.$pend);
  23337. }
  23338. }
  23339. } // end for each page
  23340. // restore default border
  23341. $border = $default_border;
  23342. } // end for each cell on the row
  23343. if (isset($table_el['attribute']['cellspacing'])) {
  23344. $this->y += $this->getHTMLUnitToUnits($table_el['attribute']['cellspacing'], 1, 'px');
  23345. } elseif (isset($table_el['border-spacing'])) {
  23346. $this->y += $table_el['border-spacing']['V'];
  23347. }
  23348. $this->Ln(0, $cell);
  23349. $this->x = $parent['startx'];
  23350. if ($endpage > $startpage) {
  23351. if (($this->rtl) AND ($this->pagedim[$endpage]['orm'] != $this->pagedim[$startpage]['orm'])) {
  23352. $this->x += ($this->pagedim[$endpage]['orm'] - $this->pagedim[$startpage]['orm']);
  23353. } elseif ((!$this->rtl) AND ($this->pagedim[$endpage]['olm'] != $this->pagedim[$startpage]['olm'])) {
  23354. $this->x += ($this->pagedim[$endpage]['olm'] - $this->pagedim[$startpage]['olm']);
  23355. }
  23356. }
  23357. }
  23358. if (!$in_table_head) { // we are not inside a thead section
  23359. $this->cell_padding = $table_el['old_cell_padding'];
  23360. // reset row height
  23361. $this->resetLastH();
  23362. if (($this->page == ($this->numpages - 1)) AND ($this->pageopen[$this->numpages])) {
  23363. $plendiff = ($this->pagelen[$this->numpages] - $this->emptypagemrk[$this->numpages]);
  23364. if (($plendiff > 0) AND ($plendiff < 60)) {
  23365. $pagediff = substr($this->getPageBuffer($this->numpages), $this->emptypagemrk[$this->numpages], $plendiff);
  23366. if (substr($pagediff, 0, 5) == 'BT /F') {
  23367. // the difference is only a font setting
  23368. $plendiff = 0;
  23369. }
  23370. }
  23371. if ($plendiff == 0) {
  23372. // remove last blank page
  23373. $this->deletePage($this->numpages);
  23374. }
  23375. }
  23376. if (isset($this->theadMargins['top'])) {
  23377. // restore top margin
  23378. $this->tMargin = $this->theadMargins['top'];
  23379. }
  23380. if (!isset($table_el['attribute']['nested']) OR ($table_el['attribute']['nested'] != 'true')) {
  23381. // reset main table header
  23382. $this->thead = '';
  23383. $this->theadMargins = array();
  23384. $this->pagedim[$this->page]['tm'] = $this->tMargin;
  23385. }
  23386. }
  23387. $parent = $table_el;
  23388. break;
  23389. }
  23390. case 'a': {
  23391. $this->HREF = '';
  23392. break;
  23393. }
  23394. case 'sup': {
  23395. $this->SetXY($this->GetX(), $this->GetY() + ((0.7 * $parent['fontsize']) / $this->k));
  23396. break;
  23397. }
  23398. case 'sub': {
  23399. $this->SetXY($this->GetX(), $this->GetY() - ((0.3 * $parent['fontsize'])/$this->k));
  23400. break;
  23401. }
  23402. case 'div': {
  23403. $this->addHTMLVertSpace($hbz, $hb, $cell, false, $lasttag);
  23404. break;
  23405. }
  23406. case 'blockquote': {
  23407. if ($this->rtl) {
  23408. $this->rMargin -= $this->listindent;
  23409. } else {
  23410. $this->lMargin -= $this->listindent;
  23411. }
  23412. --$this->listindentlevel;
  23413. $this->addHTMLVertSpace($hbz, $hb, $cell, false, $lasttag);
  23414. break;
  23415. }
  23416. case 'p': {
  23417. $this->addHTMLVertSpace($hbz, $hb, $cell, false, $lasttag);
  23418. break;
  23419. }
  23420. case 'pre': {
  23421. $this->addHTMLVertSpace($hbz, $hb, $cell, false, $lasttag);
  23422. $this->premode = false;
  23423. break;
  23424. }
  23425. case 'dl': {
  23426. --$this->listnum;
  23427. if ($this->listnum <= 0) {
  23428. $this->listnum = 0;
  23429. $this->addHTMLVertSpace($hbz, $hb, $cell, false, $lasttag);
  23430. } else {
  23431. $this->addHTMLVertSpace(0, 0, $cell, false, $lasttag);
  23432. }
  23433. $this->resetLastH();
  23434. break;
  23435. }
  23436. case 'dt': {
  23437. $this->lispacer = '';
  23438. $this->addHTMLVertSpace(0, 0, $cell, false, $lasttag);
  23439. break;
  23440. }
  23441. case 'dd': {
  23442. $this->lispacer = '';
  23443. if ($this->rtl) {
  23444. $this->rMargin -= $this->listindent;
  23445. } else {
  23446. $this->lMargin -= $this->listindent;
  23447. }
  23448. --$this->listindentlevel;
  23449. $this->addHTMLVertSpace(0, 0, $cell, false, $lasttag);
  23450. break;
  23451. }
  23452. case 'ul':
  23453. case 'ol': {
  23454. --$this->listnum;
  23455. $this->lispacer = '';
  23456. if ($this->rtl) {
  23457. $this->rMargin -= $this->listindent;
  23458. } else {
  23459. $this->lMargin -= $this->listindent;
  23460. }
  23461. --$this->listindentlevel;
  23462. if ($this->listnum <= 0) {
  23463. $this->listnum = 0;
  23464. $this->addHTMLVertSpace($hbz, $hb, $cell, false, $lasttag);
  23465. } else {
  23466. $this->addHTMLVertSpace(0, 0, $cell, false, $lasttag);
  23467. }
  23468. $this->resetLastH();
  23469. break;
  23470. }
  23471. case 'li': {
  23472. $this->lispacer = '';
  23473. $this->addHTMLVertSpace(0, 0, $cell, false, $lasttag);
  23474. break;
  23475. }
  23476. case 'h1':
  23477. case 'h2':
  23478. case 'h3':
  23479. case 'h4':
  23480. case 'h5':
  23481. case 'h6': {
  23482. $this->addHTMLVertSpace($hbz, $hb, $cell, false, $lasttag);
  23483. break;
  23484. }
  23485. // Form fields (since 4.8.000 - 2009-09-07)
  23486. case 'form': {
  23487. $this->form_action = '';
  23488. $this->form_enctype = 'application/x-www-form-urlencoded';
  23489. break;
  23490. }
  23491. default : {
  23492. break;
  23493. }
  23494. }
  23495. // draw border and background (if any)
  23496. $this->drawHTMLTagBorder($parent, $xmax);
  23497. if (isset($dom[($dom[$key]['parent'])]['attribute']['pagebreakafter'])) {
  23498. $pba = $dom[($dom[$key]['parent'])]['attribute']['pagebreakafter'];
  23499. // check for pagebreak
  23500. if (($pba == 'true') OR ($pba == 'left') OR ($pba == 'right')) {
  23501. // add a page (or trig AcceptPageBreak() for multicolumn mode)
  23502. $this->checkPageBreak($this->PageBreakTrigger + 1);
  23503. }
  23504. if ((($pba == 'left') AND (((!$this->rtl) AND (($this->page % 2) == 0)) OR (($this->rtl) AND (($this->page % 2) != 0))))
  23505. OR (($pba == 'right') AND (((!$this->rtl) AND (($this->page % 2) != 0)) OR (($this->rtl) AND (($this->page % 2) == 0))))) {
  23506. // add a page (or trig AcceptPageBreak() for multicolumn mode)
  23507. $this->checkPageBreak($this->PageBreakTrigger + 1);
  23508. }
  23509. }
  23510. $this->tmprtl = false;
  23511. return $dom;
  23512. }
  23513. /**
  23514. * Add vertical spaces if needed.
  23515. * @param $hbz (string) Distance between current y and line bottom.
  23516. * @param $hb (string) The height of the break.
  23517. * @param $cell (boolean) if true add the default left (or right if RTL) padding to each new line (default false).
  23518. * @param $firsttag (boolean) set to true when the tag is the first.
  23519. * @param $lasttag (boolean) set to true when the tag is the last.
  23520. * @protected
  23521. */
  23522. protected function addHTMLVertSpace($hbz=0, $hb=0, $cell=false, $firsttag=false, $lasttag=false) {
  23523. if ($firsttag) {
  23524. $this->Ln(0, $cell);
  23525. $this->htmlvspace = 0;
  23526. return;
  23527. }
  23528. if ($lasttag) {
  23529. $this->Ln($hbz, $cell);
  23530. $this->htmlvspace = 0;
  23531. return;
  23532. }
  23533. if ($hb < $this->htmlvspace) {
  23534. $hd = 0;
  23535. } else {
  23536. $hd = $hb - $this->htmlvspace;
  23537. $this->htmlvspace = $hb;
  23538. }
  23539. $this->Ln(($hbz + $hd), $cell);
  23540. }
  23541. /**
  23542. * Return the starting coordinates to draw an html border
  23543. * @return array containing top-left border coordinates
  23544. * @protected
  23545. * @since 5.7.000 (2010-08-03)
  23546. */
  23547. protected function getBorderStartPosition() {
  23548. if ($this->rtl) {
  23549. $xmax = $this->lMargin;
  23550. } else {
  23551. $xmax = $this->w - $this->rMargin;
  23552. }
  23553. return array('page' => $this->page, 'column' => $this->current_column, 'x' => $this->x, 'y' => $this->y, 'xmax' => $xmax);
  23554. }
  23555. /**
  23556. * Draw an HTML block border and fill
  23557. * @param $tag (array) array of tag properties.
  23558. * @param $xmax (int) end X coordinate for border.
  23559. * @protected
  23560. * @since 5.7.000 (2010-08-03)
  23561. */
  23562. protected function drawHTMLTagBorder($tag, $xmax) {
  23563. if (!isset($tag['borderposition'])) {
  23564. // nothing to draw
  23565. return;
  23566. }
  23567. $prev_x = $this->x;
  23568. $prev_y = $this->y;
  23569. $prev_lasth = $this->lasth;
  23570. $border = 0;
  23571. $fill = false;
  23572. $this->lasth = 0;
  23573. if (isset($tag['border']) AND !empty($tag['border'])) {
  23574. // get border style
  23575. $border = $tag['border'];
  23576. if (!$this->empty_string($this->thead) AND (!$this->inthead)) {
  23577. // border for table header
  23578. $border = $this->getBorderMode($border, $position='middle');
  23579. }
  23580. }
  23581. if (isset($tag['bgcolor']) AND ($tag['bgcolor'] !== false)) {
  23582. // get background color
  23583. $old_bgcolor = $this->bgcolor;
  23584. $this->SetFillColorArray($tag['bgcolor']);
  23585. $fill = true;
  23586. }
  23587. if (!$border AND !$fill) {
  23588. // nothing to draw
  23589. return;
  23590. }
  23591. if (isset($tag['attribute']['cellspacing'])) {
  23592. $clsp = $this->getHTMLUnitToUnits($tag['attribute']['cellspacing'], 1, 'px');
  23593. $cellspacing = array('H' => $clsp, 'V' => $clsp);
  23594. } elseif (isset($tag['border-spacing'])) {
  23595. $cellspacing = $tag['border-spacing'];
  23596. } else {
  23597. $cellspacing = array('H' => 0, 'V' => 0);
  23598. }
  23599. if (($tag['value'] != 'table') AND (is_array($border)) AND (!empty($border))) {
  23600. // draw the border externally respect the sqare edge.
  23601. $border['mode'] = 'ext';
  23602. }
  23603. if ($this->rtl) {
  23604. if ($xmax >= $tag['borderposition']['x']) {
  23605. $xmax = $tag['borderposition']['xmax'];
  23606. }
  23607. $w = ($tag['borderposition']['x'] - $xmax);
  23608. } else {
  23609. if ($xmax <= $tag['borderposition']['x']) {
  23610. $xmax = $tag['borderposition']['xmax'];
  23611. }
  23612. $w = ($xmax - $tag['borderposition']['x']);
  23613. }
  23614. if ($w <= 0) {
  23615. return;
  23616. }
  23617. $w += $cellspacing['H'];
  23618. $startpage = $tag['borderposition']['page'];
  23619. $startcolumn = $tag['borderposition']['column'];
  23620. $x = $tag['borderposition']['x'];
  23621. $y = $tag['borderposition']['y'];
  23622. $endpage = $this->page;
  23623. $starty = $tag['borderposition']['y'] - $cellspacing['V'];
  23624. $currentY = $this->y;
  23625. $this->x = $x;
  23626. // get latest column
  23627. $endcolumn = $this->current_column;
  23628. if ($this->num_columns == 0) {
  23629. $this->num_columns = 1;
  23630. }
  23631. // get border modes
  23632. $border_start = $this->getBorderMode($border, $position='start');
  23633. $border_end = $this->getBorderMode($border, $position='end');
  23634. $border_middle = $this->getBorderMode($border, $position='middle');
  23635. // temporary disable page regions
  23636. $temp_page_regions = $this->page_regions;
  23637. $this->page_regions = array();
  23638. // design borders around HTML cells.
  23639. for ($page = $startpage; $page <= $endpage; ++$page) { // for each page
  23640. $ccode = '';
  23641. $this->setPage($page);
  23642. if ($this->num_columns < 2) {
  23643. // single-column mode
  23644. $this->x = $x;
  23645. $this->y = $this->tMargin;
  23646. }
  23647. // account for margin changes
  23648. if ($page > $startpage) {
  23649. if (($this->rtl) AND ($this->pagedim[$page]['orm'] != $this->pagedim[$startpage]['orm'])) {
  23650. $this->x -= ($this->pagedim[$page]['orm'] - $this->pagedim[$startpage]['orm']);
  23651. } elseif ((!$this->rtl) AND ($this->pagedim[$page]['olm'] != $this->pagedim[$startpage]['olm'])) {
  23652. $this->x += ($this->pagedim[$page]['olm'] - $this->pagedim[$startpage]['olm']);
  23653. }
  23654. }
  23655. if ($startpage == $endpage) {
  23656. // single page
  23657. for ($column = $startcolumn; $column <= $endcolumn; ++$column) { // for each column
  23658. $this->selectColumn($column);
  23659. if ($startcolumn == $endcolumn) { // single column
  23660. $cborder = $border;
  23661. $h = ($currentY - $y) + $cellspacing['V'];
  23662. $this->y = $starty;
  23663. } elseif ($column == $startcolumn) { // first column
  23664. $cborder = $border_start;
  23665. $this->y = $starty;
  23666. $h = $this->h - $this->y - $this->bMargin;
  23667. } elseif ($column == $endcolumn) { // end column
  23668. $cborder = $border_end;
  23669. $h = $currentY - $this->y;
  23670. } else { // middle column
  23671. $cborder = $border_middle;
  23672. $h = $this->h - $this->y - $this->bMargin;
  23673. }
  23674. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
  23675. } // end for each column
  23676. } elseif ($page == $startpage) { // first page
  23677. for ($column = $startcolumn; $column < $this->num_columns; ++$column) { // for each column
  23678. $this->selectColumn($column);
  23679. if ($column == $startcolumn) { // first column
  23680. $cborder = $border_start;
  23681. $this->y = $starty;
  23682. $h = $this->h - $this->y - $this->bMargin;
  23683. } else { // middle column
  23684. $cborder = $border_middle;
  23685. $h = $this->h - $this->y - $this->bMargin;
  23686. }
  23687. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
  23688. } // end for each column
  23689. } elseif ($page == $endpage) { // last page
  23690. for ($column = 0; $column <= $endcolumn; ++$column) { // for each column
  23691. $this->selectColumn($column);
  23692. if ($column == $endcolumn) {
  23693. // end column
  23694. $cborder = $border_end;
  23695. $h = $currentY - $this->y;
  23696. } else {
  23697. // middle column
  23698. $cborder = $border_middle;
  23699. $h = $this->h - $this->y - $this->bMargin;
  23700. }
  23701. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
  23702. } // end for each column
  23703. } else { // middle page
  23704. for ($column = 0; $column < $this->num_columns; ++$column) { // for each column
  23705. $this->selectColumn($column);
  23706. $cborder = $border_middle;
  23707. $h = $this->h - $this->y - $this->bMargin;
  23708. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
  23709. } // end for each column
  23710. }
  23711. if ($cborder OR $fill) {
  23712. $offsetlen = strlen($ccode);
  23713. // draw border and fill
  23714. if ($this->inxobj) {
  23715. // we are inside an XObject template
  23716. if (end($this->xobjects[$this->xobjid]['transfmrk']) !== false) {
  23717. $pagemarkkey = key($this->xobjects[$this->xobjid]['transfmrk']);
  23718. $pagemark = $this->xobjects[$this->xobjid]['transfmrk'][$pagemarkkey];
  23719. $this->xobjects[$this->xobjid]['transfmrk'][$pagemarkkey] += $offsetlen;
  23720. } else {
  23721. $pagemark = $this->xobjects[$this->xobjid]['intmrk'];
  23722. $this->xobjects[$this->xobjid]['intmrk'] += $offsetlen;
  23723. }
  23724. $pagebuff = $this->xobjects[$this->xobjid]['outdata'];
  23725. $pstart = substr($pagebuff, 0, $pagemark);
  23726. $pend = substr($pagebuff, $pagemark);
  23727. $this->xobjects[$this->xobjid]['outdata'] = $pstart.$ccode.$pend;
  23728. } else {
  23729. if (end($this->transfmrk[$this->page]) !== false) {
  23730. $pagemarkkey = key($this->transfmrk[$this->page]);
  23731. $pagemark = $this->transfmrk[$this->page][$pagemarkkey];
  23732. $this->transfmrk[$this->page][$pagemarkkey] += $offsetlen;
  23733. } elseif ($this->InFooter) {
  23734. $pagemark = $this->footerpos[$this->page];
  23735. $this->footerpos[$this->page] += $offsetlen;
  23736. } else {
  23737. $pagemark = $this->intmrk[$this->page];
  23738. $this->intmrk[$this->page] += $offsetlen;
  23739. }
  23740. $pagebuff = $this->getPageBuffer($this->page);
  23741. $pstart = substr($pagebuff, 0, $this->bordermrk[$this->page]);
  23742. $pend = substr($pagebuff, $this->bordermrk[$this->page]);
  23743. $this->setPageBuffer($this->page, $pstart.$ccode.$pend);
  23744. $this->bordermrk[$this->page] += $offsetlen;
  23745. $this->cntmrk[$this->page] += $offsetlen;
  23746. }
  23747. }
  23748. } // end for each page
  23749. // restore page regions
  23750. $this->page_regions = $temp_page_regions;
  23751. if (isset($old_bgcolor)) {
  23752. // restore background color
  23753. $this->SetFillColorArray($old_bgcolor);
  23754. }
  23755. // restore pointer position
  23756. $this->x = $prev_x;
  23757. $this->y = $prev_y;
  23758. $this->lasth = $prev_lasth;
  23759. }
  23760. /**
  23761. * Set the default bullet to be used as LI bullet symbol
  23762. * @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')
  23763. * @public
  23764. * @since 4.0.028 (2008-09-26)
  23765. */
  23766. public function setLIsymbol($symbol='!') {
  23767. // check for custom image symbol
  23768. if (substr($symbol, 0, 4) == 'img|') {
  23769. $this->lisymbol = $symbol;
  23770. return;
  23771. }
  23772. $symbol = strtolower($symbol);
  23773. switch ($symbol) {
  23774. case '!' :
  23775. case '#' :
  23776. case 'disc' :
  23777. case 'circle' :
  23778. case 'square' :
  23779. case '1':
  23780. case 'decimal':
  23781. case 'decimal-leading-zero':
  23782. case 'i':
  23783. case 'lower-roman':
  23784. case 'I':
  23785. case 'upper-roman':
  23786. case 'a':
  23787. case 'lower-alpha':
  23788. case 'lower-latin':
  23789. case 'A':
  23790. case 'upper-alpha':
  23791. case 'upper-latin':
  23792. case 'lower-greek': {
  23793. $this->lisymbol = $symbol;
  23794. break;
  23795. }
  23796. default : {
  23797. $this->lisymbol = '';
  23798. }
  23799. }
  23800. }
  23801. /**
  23802. * Set the booklet mode for double-sided pages.
  23803. * @param $booklet (boolean) true set the booklet mode on, false otherwise.
  23804. * @param $inner (float) Inner page margin.
  23805. * @param $outer (float) Outer page margin.
  23806. * @public
  23807. * @since 4.2.000 (2008-10-29)
  23808. */
  23809. public function SetBooklet($booklet=true, $inner=-1, $outer=-1) {
  23810. $this->booklet = $booklet;
  23811. if ($inner >= 0) {
  23812. $this->lMargin = $inner;
  23813. }
  23814. if ($outer >= 0) {
  23815. $this->rMargin = $outer;
  23816. }
  23817. }
  23818. /**
  23819. * Swap the left and right margins.
  23820. * @param $reverse (boolean) if true swap left and right margins.
  23821. * @protected
  23822. * @since 4.2.000 (2008-10-29)
  23823. */
  23824. protected function swapMargins($reverse=true) {
  23825. if ($reverse) {
  23826. // swap left and right margins
  23827. $mtemp = $this->original_lMargin;
  23828. $this->original_lMargin = $this->original_rMargin;
  23829. $this->original_rMargin = $mtemp;
  23830. $deltam = $this->original_lMargin - $this->original_rMargin;
  23831. $this->lMargin += $deltam;
  23832. $this->rMargin -= $deltam;
  23833. }
  23834. }
  23835. /**
  23836. * Set the vertical spaces for HTML tags.
  23837. * The array must have the following structure (example):
  23838. * $tagvs = array('h1' => array(0 => array('h' => '', 'n' => 2), 1 => array('h' => 1.3, 'n' => 1)));
  23839. * The first array level contains the tag names,
  23840. * the second level contains 0 for opening tags or 1 for closing tags,
  23841. * the third level contains the vertical space unit (h) and the number spaces to add (n).
  23842. * If the h parameter is not specified, default values are used.
  23843. * @param $tagvs (array) array of tags and relative vertical spaces.
  23844. * @public
  23845. * @since 4.2.001 (2008-10-30)
  23846. */
  23847. public function setHtmlVSpace($tagvs) {
  23848. $this->tagvspaces = $tagvs;
  23849. }
  23850. /**
  23851. * Set custom width for list indentation.
  23852. * @param $width (float) width of the indentation. Use negative value to disable it.
  23853. * @public
  23854. * @since 4.2.007 (2008-11-12)
  23855. */
  23856. public function setListIndentWidth($width) {
  23857. return $this->customlistindent = floatval($width);
  23858. }
  23859. /**
  23860. * Set the top/bottom cell sides to be open or closed when the cell cross the page.
  23861. * @param $isopen (boolean) if true keeps the top/bottom border open for the cell sides that cross the page.
  23862. * @public
  23863. * @since 4.2.010 (2008-11-14)
  23864. */
  23865. public function setOpenCell($isopen) {
  23866. $this->opencell = $isopen;
  23867. }
  23868. /**
  23869. * Set the color and font style for HTML links.
  23870. * @param $color (array) RGB array of colors
  23871. * @param $fontstyle (string) additional font styles to add
  23872. * @public
  23873. * @since 4.4.003 (2008-12-09)
  23874. */
  23875. public function setHtmlLinksStyle($color=array(0,0,255), $fontstyle='U') {
  23876. $this->htmlLinkColorArray = $color;
  23877. $this->htmlLinkFontStyle = $fontstyle;
  23878. }
  23879. /**
  23880. * Convert HTML string containing value and unit of measure to user's units or points.
  23881. * @param $htmlval (string) string containing values and unit
  23882. * @param $refsize (string) reference value in points
  23883. * @param $defaultunit (string) default unit (can be one of the following: %, em, ex, px, in, mm, pc, pt).
  23884. * @param $points (boolean) if true returns points, otherwise returns value in user's units
  23885. * @return float value in user's unit or point if $points=true
  23886. * @public
  23887. * @since 4.4.004 (2008-12-10)
  23888. */
  23889. public function getHTMLUnitToUnits($htmlval, $refsize=1, $defaultunit='px', $points=false) {
  23890. $supportedunits = array('%', 'em', 'ex', 'px', 'in', 'cm', 'mm', 'pc', 'pt');
  23891. $retval = 0;
  23892. $value = 0;
  23893. $unit = 'px';
  23894. $k = $this->k;
  23895. if ($points) {
  23896. $k = 1;
  23897. }
  23898. if (in_array($defaultunit, $supportedunits)) {
  23899. $unit = $defaultunit;
  23900. }
  23901. if (is_numeric($htmlval)) {
  23902. $value = floatval($htmlval);
  23903. } elseif (preg_match('/([0-9\.\-\+]+)/', $htmlval, $mnum)) {
  23904. $value = floatval($mnum[1]);
  23905. if (preg_match('/([a-z%]+)/', $htmlval, $munit)) {
  23906. if (in_array($munit[1], $supportedunits)) {
  23907. $unit = $munit[1];
  23908. }
  23909. }
  23910. }
  23911. switch ($unit) {
  23912. // percentage
  23913. case '%': {
  23914. $retval = (($value * $refsize) / 100);
  23915. break;
  23916. }
  23917. // relative-size
  23918. case 'em': {
  23919. $retval = ($value * $refsize);
  23920. break;
  23921. }
  23922. // height of lower case 'x' (about half the font-size)
  23923. case 'ex': {
  23924. $retval = $value * ($refsize / 2);
  23925. break;
  23926. }
  23927. // absolute-size
  23928. case 'in': {
  23929. $retval = ($value * $this->dpi) / $k;
  23930. break;
  23931. }
  23932. // centimeters
  23933. case 'cm': {
  23934. $retval = ($value / 2.54 * $this->dpi) / $k;
  23935. break;
  23936. }
  23937. // millimeters
  23938. case 'mm': {
  23939. $retval = ($value / 25.4 * $this->dpi) / $k;
  23940. break;
  23941. }
  23942. // one pica is 12 points
  23943. case 'pc': {
  23944. $retval = ($value * 12) / $k;
  23945. break;
  23946. }
  23947. // points
  23948. case 'pt': {
  23949. $retval = $value / $k;
  23950. break;
  23951. }
  23952. // pixels
  23953. case 'px': {
  23954. $retval = $this->pixelsToUnits($value);
  23955. break;
  23956. }
  23957. }
  23958. return $retval;
  23959. }
  23960. /**
  23961. * Returns the Roman representation of an integer number
  23962. * @param $number (int) number to convert
  23963. * @return string roman representation of the specified number
  23964. * @since 4.4.004 (2008-12-10)
  23965. * @public
  23966. */
  23967. public function intToRoman($number) {
  23968. $roman = '';
  23969. while ($number >= 1000) {
  23970. $roman .= 'M';
  23971. $number -= 1000;
  23972. }
  23973. while ($number >= 900) {
  23974. $roman .= 'CM';
  23975. $number -= 900;
  23976. }
  23977. while ($number >= 500) {
  23978. $roman .= 'D';
  23979. $number -= 500;
  23980. }
  23981. while ($number >= 400) {
  23982. $roman .= 'CD';
  23983. $number -= 400;
  23984. }
  23985. while ($number >= 100) {
  23986. $roman .= 'C';
  23987. $number -= 100;
  23988. }
  23989. while ($number >= 90) {
  23990. $roman .= 'XC';
  23991. $number -= 90;
  23992. }
  23993. while ($number >= 50) {
  23994. $roman .= 'L';
  23995. $number -= 50;
  23996. }
  23997. while ($number >= 40) {
  23998. $roman .= 'XL';
  23999. $number -= 40;
  24000. }
  24001. while ($number >= 10) {
  24002. $roman .= 'X';
  24003. $number -= 10;
  24004. }
  24005. while ($number >= 9) {
  24006. $roman .= 'IX';
  24007. $number -= 9;
  24008. }
  24009. while ($number >= 5) {
  24010. $roman .= 'V';
  24011. $number -= 5;
  24012. }
  24013. while ($number >= 4) {
  24014. $roman .= 'IV';
  24015. $number -= 4;
  24016. }
  24017. while ($number >= 1) {
  24018. $roman .= 'I';
  24019. --$number;
  24020. }
  24021. return $roman;
  24022. }
  24023. /**
  24024. * Output an HTML list bullet or ordered item symbol
  24025. * @param $listdepth (int) list nesting level
  24026. * @param $listtype (string) type of list
  24027. * @param $size (float) current font size
  24028. * @protected
  24029. * @since 4.4.004 (2008-12-10)
  24030. */
  24031. protected function putHtmlListBullet($listdepth, $listtype='', $size=10) {
  24032. $size /= $this->k;
  24033. $fill = '';
  24034. $bgcolor = $this->bgcolor;
  24035. $color = $this->fgcolor;
  24036. $strokecolor = $this->strokecolor;
  24037. $width = 0;
  24038. $textitem = '';
  24039. $tmpx = $this->x;
  24040. $lspace = $this->GetStringWidth(' ');
  24041. if ($listtype == '^') {
  24042. // special symbol used for avoid justification of rect bullet
  24043. $this->lispacer = '';
  24044. return;
  24045. } elseif ($listtype == '!') {
  24046. // set default list type for unordered list
  24047. $deftypes = array('disc', 'circle', 'square');
  24048. $listtype = $deftypes[($listdepth - 1) % 3];
  24049. } elseif ($listtype == '#') {
  24050. // set default list type for ordered list
  24051. $listtype = 'decimal';
  24052. } elseif (substr($listtype, 0, 4) == 'img|') {
  24053. // custom image type ('img|type|width|height|image.ext')
  24054. $img = explode('|', $listtype);
  24055. $listtype = 'img';
  24056. }
  24057. switch ($listtype) {
  24058. // unordered types
  24059. case 'none': {
  24060. break;
  24061. }
  24062. case 'disc': {
  24063. $r = $size / 6;
  24064. $lspace += (2 * $r);
  24065. if ($this->rtl) {
  24066. $this->x += $lspace;
  24067. } else {
  24068. $this->x -= $lspace;
  24069. }
  24070. $this->Circle(($this->x + $r), ($this->y + ($this->lasth / 2)), $r, 0, 360, 'F', array(), $color, 8);
  24071. break;
  24072. }
  24073. case 'circle': {
  24074. $r = $size / 6;
  24075. $lspace += (2 * $r);
  24076. if ($this->rtl) {
  24077. $this->x += $lspace;
  24078. } else {
  24079. $this->x -= $lspace;
  24080. }
  24081. $prev_line_style = $this->linestyleWidth.' '.$this->linestyleCap.' '.$this->linestyleJoin.' '.$this->linestyleDash.' '.$this->DrawColor;
  24082. $new_line_style = array('width' => ($r / 3), 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'phase' => 0, 'color'=>$color);
  24083. $this->Circle(($this->x + $r), ($this->y + ($this->lasth / 2)), ($r * (1 - (1/6))), 0, 360, 'D', $new_line_style, array(), 8);
  24084. $this->_out($prev_line_style); // restore line settings
  24085. break;
  24086. }
  24087. case 'square': {
  24088. $l = $size / 3;
  24089. $lspace += $l;
  24090. if ($this->rtl) {;
  24091. $this->x += $lspace;
  24092. } else {
  24093. $this->x -= $lspace;
  24094. }
  24095. $this->Rect($this->x, ($this->y + (($this->lasth - $l) / 2)), $l, $l, 'F', array(), $color);
  24096. break;
  24097. }
  24098. case 'img': {
  24099. // 1=>type, 2=>width, 3=>height, 4=>image.ext
  24100. $lspace += $img[2];
  24101. if ($this->rtl) {;
  24102. $this->x += $lspace;
  24103. } else {
  24104. $this->x -= $lspace;
  24105. }
  24106. $imgtype = strtolower($img[1]);
  24107. $prev_y = $this->y;
  24108. switch ($imgtype) {
  24109. case 'svg': {
  24110. $this->ImageSVG($img[4], $this->x, ($this->y + (($this->lasth - $img[3]) / 2)), $img[2], $img[3], '', 'T', '', 0, false);
  24111. break;
  24112. }
  24113. case 'ai':
  24114. case 'eps': {
  24115. $this->ImageEps($img[4], $this->x, ($this->y + (($this->lasth - $img[3]) / 2)), $img[2], $img[3], '', true, 'T', '', 0, false);
  24116. break;
  24117. }
  24118. default: {
  24119. $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);
  24120. break;
  24121. }
  24122. }
  24123. $this->y = $prev_y;
  24124. break;
  24125. }
  24126. // ordered types
  24127. // $this->listcount[$this->listnum];
  24128. // $textitem
  24129. case '1':
  24130. case 'decimal': {
  24131. $textitem = $this->listcount[$this->listnum];
  24132. break;
  24133. }
  24134. case 'decimal-leading-zero': {
  24135. $textitem = sprintf('%02d', $this->listcount[$this->listnum]);
  24136. break;
  24137. }
  24138. case 'i':
  24139. case 'lower-roman': {
  24140. $textitem = strtolower($this->intToRoman($this->listcount[$this->listnum]));
  24141. break;
  24142. }
  24143. case 'I':
  24144. case 'upper-roman': {
  24145. $textitem = $this->intToRoman($this->listcount[$this->listnum]);
  24146. break;
  24147. }
  24148. case 'a':
  24149. case 'lower-alpha':
  24150. case 'lower-latin': {
  24151. $textitem = chr(97 + $this->listcount[$this->listnum] - 1);
  24152. break;
  24153. }
  24154. case 'A':
  24155. case 'upper-alpha':
  24156. case 'upper-latin': {
  24157. $textitem = chr(65 + $this->listcount[$this->listnum] - 1);
  24158. break;
  24159. }
  24160. case 'lower-greek': {
  24161. $textitem = $this->unichr(945 + $this->listcount[$this->listnum] - 1);
  24162. break;
  24163. }
  24164. /*
  24165. // Types to be implemented (special handling)
  24166. case 'hebrew': {
  24167. break;
  24168. }
  24169. case 'armenian': {
  24170. break;
  24171. }
  24172. case 'georgian': {
  24173. break;
  24174. }
  24175. case 'cjk-ideographic': {
  24176. break;
  24177. }
  24178. case 'hiragana': {
  24179. break;
  24180. }
  24181. case 'katakana': {
  24182. break;
  24183. }
  24184. case 'hiragana-iroha': {
  24185. break;
  24186. }
  24187. case 'katakana-iroha': {
  24188. break;
  24189. }
  24190. */
  24191. default: {
  24192. $textitem = $this->listcount[$this->listnum];
  24193. }
  24194. }
  24195. if (!$this->empty_string($textitem)) {
  24196. // Check whether we need a new page or new column
  24197. $prev_y = $this->y;
  24198. $h = ($this->FontSize * $this->cell_height_ratio) + $this->cell_padding['T'] + $this->cell_padding['B'];
  24199. if ($this->checkPageBreak($h) OR ($this->y < $prev_y)) {
  24200. $tmpx = $this->x;
  24201. }
  24202. // print ordered item
  24203. if ($this->rtl) {
  24204. $textitem = '.'.$textitem;
  24205. } else {
  24206. $textitem = $textitem.'.';
  24207. }
  24208. $lspace += $this->GetStringWidth($textitem);
  24209. if ($this->rtl) {
  24210. $this->x += $lspace;
  24211. } else {
  24212. $this->x -= $lspace;
  24213. }
  24214. $this->Write($this->lasth, $textitem, '', false, '', false, 0, false);
  24215. }
  24216. $this->x = $tmpx;
  24217. $this->lispacer = '^';
  24218. // restore colors
  24219. $this->SetFillColorArray($bgcolor);
  24220. $this->SetDrawColorArray($strokecolor);
  24221. $this->SettextColorArray($color);
  24222. }
  24223. /**
  24224. * Returns current graphic variables as array.
  24225. * @return array of graphic variables
  24226. * @protected
  24227. * @since 4.2.010 (2008-11-14)
  24228. */
  24229. protected function getGraphicVars() {
  24230. $grapvars = array(
  24231. 'FontFamily' => $this->FontFamily,
  24232. 'FontStyle' => $this->FontStyle,
  24233. 'FontSizePt' => $this->FontSizePt,
  24234. 'rMargin' => $this->rMargin,
  24235. 'lMargin' => $this->lMargin,
  24236. 'cell_padding' => $this->cell_padding,
  24237. 'cell_margin' => $this->cell_margin,
  24238. 'LineWidth' => $this->LineWidth,
  24239. 'linestyleWidth' => $this->linestyleWidth,
  24240. 'linestyleCap' => $this->linestyleCap,
  24241. 'linestyleJoin' => $this->linestyleJoin,
  24242. 'linestyleDash' => $this->linestyleDash,
  24243. 'textrendermode' => $this->textrendermode,
  24244. 'textstrokewidth' => $this->textstrokewidth,
  24245. 'DrawColor' => $this->DrawColor,
  24246. 'FillColor' => $this->FillColor,
  24247. 'TextColor' => $this->TextColor,
  24248. 'ColorFlag' => $this->ColorFlag,
  24249. 'bgcolor' => $this->bgcolor,
  24250. 'fgcolor' => $this->fgcolor,
  24251. 'htmlvspace' => $this->htmlvspace,
  24252. 'listindent' => $this->listindent,
  24253. 'listindentlevel' => $this->listindentlevel,
  24254. 'listnum' => $this->listnum,
  24255. 'listordered' => $this->listordered,
  24256. 'listcount' => $this->listcount,
  24257. 'lispacer' => $this->lispacer,
  24258. 'cell_height_ratio' => $this->cell_height_ratio,
  24259. 'font_stretching' => $this->font_stretching,
  24260. 'font_spacing' => $this->font_spacing,
  24261. // extended
  24262. 'lasth' => $this->lasth,
  24263. 'tMargin' => $this->tMargin,
  24264. 'bMargin' => $this->bMargin,
  24265. 'AutoPageBreak' => $this->AutoPageBreak,
  24266. 'PageBreakTrigger' => $this->PageBreakTrigger,
  24267. 'x' => $this->x,
  24268. 'y' => $this->y,
  24269. 'w' => $this->w,
  24270. 'h' => $this->h,
  24271. 'wPt' => $this->wPt,
  24272. 'hPt' => $this->hPt,
  24273. 'fwPt' => $this->fwPt,
  24274. 'fhPt' => $this->fhPt,
  24275. 'page' => $this->page,
  24276. 'current_column' => $this->current_column,
  24277. 'num_columns' => $this->num_columns
  24278. );
  24279. return $grapvars;
  24280. }
  24281. /**
  24282. * Set graphic variables.
  24283. * @param $gvars (array) array of graphic variablesto restore
  24284. * @param $extended (boolean) if true restore extended graphic variables
  24285. * @protected
  24286. * @since 4.2.010 (2008-11-14)
  24287. */
  24288. protected function setGraphicVars($gvars, $extended=false) {
  24289. $this->FontFamily = $gvars['FontFamily'];
  24290. $this->FontStyle = $gvars['FontStyle'];
  24291. $this->FontSizePt = $gvars['FontSizePt'];
  24292. $this->rMargin = $gvars['rMargin'];
  24293. $this->lMargin = $gvars['lMargin'];
  24294. $this->cell_padding = $gvars['cell_padding'];
  24295. $this->cell_margin = $gvars['cell_margin'];
  24296. $this->LineWidth = $gvars['LineWidth'];
  24297. $this->linestyleWidth = $gvars['linestyleWidth'];
  24298. $this->linestyleCap = $gvars['linestyleCap'];
  24299. $this->linestyleJoin = $gvars['linestyleJoin'];
  24300. $this->linestyleDash = $gvars['linestyleDash'];
  24301. $this->textrendermode = $gvars['textrendermode'];
  24302. $this->textstrokewidth = $gvars['textstrokewidth'];
  24303. $this->DrawColor = $gvars['DrawColor'];
  24304. $this->FillColor = $gvars['FillColor'];
  24305. $this->TextColor = $gvars['TextColor'];
  24306. $this->ColorFlag = $gvars['ColorFlag'];
  24307. $this->bgcolor = $gvars['bgcolor'];
  24308. $this->fgcolor = $gvars['fgcolor'];
  24309. $this->htmlvspace = $gvars['htmlvspace'];
  24310. $this->listindent = $gvars['listindent'];
  24311. $this->listindentlevel = $gvars['listindentlevel'];
  24312. $this->listnum = $gvars['listnum'];
  24313. $this->listordered = $gvars['listordered'];
  24314. $this->listcount = $gvars['listcount'];
  24315. $this->lispacer = $gvars['lispacer'];
  24316. $this->cell_height_ratio = $gvars['cell_height_ratio'];
  24317. $this->font_stretching = $gvars['font_stretching'];
  24318. $this->font_spacing = $gvars['font_spacing'];
  24319. if ($extended) {
  24320. // restore extended values
  24321. $this->lasth = $gvars['lasth'];
  24322. $this->tMargin = $gvars['tMargin'];
  24323. $this->bMargin = $gvars['bMargin'];
  24324. $this->AutoPageBreak = $gvars['AutoPageBreak'];
  24325. $this->PageBreakTrigger = $gvars['PageBreakTrigger'];
  24326. $this->x = $gvars['x'];
  24327. $this->y = $gvars['y'];
  24328. $this->w = $gvars['w'];
  24329. $this->h = $gvars['h'];
  24330. $this->wPt = $gvars['wPt'];
  24331. $this->hPt = $gvars['hPt'];
  24332. $this->fwPt = $gvars['fwPt'];
  24333. $this->fhPt = $gvars['fhPt'];
  24334. $this->page = $gvars['page'];
  24335. $this->current_column = $gvars['current_column'];
  24336. $this->num_columns = $gvars['num_columns'];
  24337. }
  24338. $this->_out(''.$this->linestyleWidth.' '.$this->linestyleCap.' '.$this->linestyleJoin.' '.$this->linestyleDash.' '.$this->DrawColor.' '.$this->FillColor.'');
  24339. if (!$this->empty_string($this->FontFamily)) {
  24340. $this->SetFont($this->FontFamily, $this->FontStyle, $this->FontSizePt);
  24341. }
  24342. }
  24343. /**
  24344. * Returns a temporary filename for caching object on filesystem.
  24345. * @param $name (string) prefix to add to filename
  24346. * @return string filename.
  24347. * @since 4.5.000 (2008-12-31)
  24348. * @protected
  24349. */
  24350. protected function getObjFilename($name) {
  24351. return tempnam(K_PATH_CACHE, $name.'_');
  24352. }
  24353. /**
  24354. * Writes data to a temporary file on filesystem.
  24355. * @param $filename (string) file name
  24356. * @param $data (mixed) data to write on file
  24357. * @param $append (boolean) if true append data, false replace.
  24358. * @since 4.5.000 (2008-12-31)
  24359. * @protected
  24360. */
  24361. protected function writeDiskCache($filename, $data, $append=false) {
  24362. if ($append) {
  24363. $fmode = 'ab+';
  24364. } else {
  24365. $fmode = 'wb+';
  24366. }
  24367. $f = @fopen($filename, $fmode);
  24368. if (!$f) {
  24369. $this->Error('Unable to write cache file: '.$filename);
  24370. } else {
  24371. fwrite($f, $data);
  24372. fclose($f);
  24373. }
  24374. // update file length (needed for transactions)
  24375. if (!isset($this->cache_file_length['_'.$filename])) {
  24376. $this->cache_file_length['_'.$filename] = strlen($data);
  24377. } else {
  24378. $this->cache_file_length['_'.$filename] += strlen($data);
  24379. }
  24380. }
  24381. /**
  24382. * Read data from a temporary file on filesystem.
  24383. * @param $filename (string) file name
  24384. * @return mixed retrieved data
  24385. * @since 4.5.000 (2008-12-31)
  24386. * @protected
  24387. */
  24388. protected function readDiskCache($filename) {
  24389. return file_get_contents($filename);
  24390. }
  24391. /**
  24392. * Set buffer content (always append data).
  24393. * @param $data (string) data
  24394. * @protected
  24395. * @since 4.5.000 (2009-01-02)
  24396. */
  24397. protected function setBuffer($data) {
  24398. $this->bufferlen += strlen($data);
  24399. if ($this->diskcache) {
  24400. if (!isset($this->buffer) OR $this->empty_string($this->buffer)) {
  24401. $this->buffer = $this->getObjFilename('buffer');
  24402. }
  24403. $this->writeDiskCache($this->buffer, $data, true);
  24404. } else {
  24405. $this->buffer .= $data;
  24406. }
  24407. }
  24408. /**
  24409. * Replace the buffer content
  24410. * @param $data (string) data
  24411. * @protected
  24412. * @since 5.5.000 (2010-06-22)
  24413. */
  24414. protected function replaceBuffer($data) {
  24415. $this->bufferlen = strlen($data);
  24416. if ($this->diskcache) {
  24417. if (!isset($this->buffer) OR $this->empty_string($this->buffer)) {
  24418. $this->buffer = $this->getObjFilename('buffer');
  24419. }
  24420. $this->writeDiskCache($this->buffer, $data, false);
  24421. } else {
  24422. $this->buffer = $data;
  24423. }
  24424. }
  24425. /**
  24426. * Get buffer content.
  24427. * @return string buffer content
  24428. * @protected
  24429. * @since 4.5.000 (2009-01-02)
  24430. */
  24431. protected function getBuffer() {
  24432. if ($this->diskcache) {
  24433. return $this->readDiskCache($this->buffer);
  24434. } else {
  24435. return $this->buffer;
  24436. }
  24437. }
  24438. /**
  24439. * Set page buffer content.
  24440. * @param $page (int) page number
  24441. * @param $data (string) page data
  24442. * @param $append (boolean) if true append data, false replace.
  24443. * @protected
  24444. * @since 4.5.000 (2008-12-31)
  24445. */
  24446. protected function setPageBuffer($page, $data, $append=false) {
  24447. if ($this->diskcache) {
  24448. if (!isset($this->pages[$page])) {
  24449. $this->pages[$page] = $this->getObjFilename('page'.$page);
  24450. }
  24451. $this->writeDiskCache($this->pages[$page], $data, $append);
  24452. } else {
  24453. if ($append) {
  24454. $this->pages[$page] .= $data;
  24455. } else {
  24456. $this->pages[$page] = $data;
  24457. }
  24458. }
  24459. if ($append AND isset($this->pagelen[$page])) {
  24460. $this->pagelen[$page] += strlen($data);
  24461. } else {
  24462. $this->pagelen[$page] = strlen($data);
  24463. }
  24464. }
  24465. /**
  24466. * Get page buffer content.
  24467. * @param $page (int) page number
  24468. * @return string page buffer content or false in case of error
  24469. * @protected
  24470. * @since 4.5.000 (2008-12-31)
  24471. */
  24472. protected function getPageBuffer($page) {
  24473. if ($this->diskcache) {
  24474. return $this->readDiskCache($this->pages[$page]);
  24475. } elseif (isset($this->pages[$page])) {
  24476. return $this->pages[$page];
  24477. }
  24478. return false;
  24479. }
  24480. /**
  24481. * Set image buffer content.
  24482. * @param $image (string) image key
  24483. * @param $data (array) image data
  24484. * @protected
  24485. * @since 4.5.000 (2008-12-31)
  24486. */
  24487. protected function setImageBuffer($image, $data) {
  24488. if ($this->diskcache) {
  24489. if (!isset($this->images[$image])) {
  24490. $this->images[$image] = $this->getObjFilename('image'.$image);
  24491. }
  24492. $this->writeDiskCache($this->images[$image], serialize($data));
  24493. } else {
  24494. $this->images[$image] = $data;
  24495. }
  24496. if (!in_array($image, $this->imagekeys)) {
  24497. $this->imagekeys[] = $image;
  24498. ++$this->numimages;
  24499. }
  24500. }
  24501. /**
  24502. * Set image buffer content for a specified sub-key.
  24503. * @param $image (string) image key
  24504. * @param $key (string) image sub-key
  24505. * @param $data (array) image data
  24506. * @protected
  24507. * @since 4.5.000 (2008-12-31)
  24508. */
  24509. protected function setImageSubBuffer($image, $key, $data) {
  24510. if (!isset($this->images[$image])) {
  24511. $this->setImageBuffer($image, array());
  24512. }
  24513. if ($this->diskcache) {
  24514. $tmpimg = $this->getImageBuffer($image);
  24515. $tmpimg[$key] = $data;
  24516. $this->writeDiskCache($this->images[$image], serialize($tmpimg));
  24517. } else {
  24518. $this->images[$image][$key] = $data;
  24519. }
  24520. }
  24521. /**
  24522. * Get image buffer content.
  24523. * @param $image (string) image key
  24524. * @return string image buffer content or false in case of error
  24525. * @protected
  24526. * @since 4.5.000 (2008-12-31)
  24527. */
  24528. protected function getImageBuffer($image) {
  24529. if ($this->diskcache AND isset($this->images[$image])) {
  24530. return unserialize($this->readDiskCache($this->images[$image]));
  24531. } elseif (isset($this->images[$image])) {
  24532. return $this->images[$image];
  24533. }
  24534. return false;
  24535. }
  24536. /**
  24537. * Set font buffer content.
  24538. * @param $font (string) font key
  24539. * @param $data (array) font data
  24540. * @protected
  24541. * @since 4.5.000 (2009-01-02)
  24542. */
  24543. protected function setFontBuffer($font, $data) {
  24544. if ($this->diskcache) {
  24545. if (!isset($this->fonts[$font])) {
  24546. $this->fonts[$font] = $this->getObjFilename('font');
  24547. }
  24548. $this->writeDiskCache($this->fonts[$font], serialize($data));
  24549. } else {
  24550. $this->fonts[$font] = $data;
  24551. }
  24552. if (!in_array($font, $this->fontkeys)) {
  24553. $this->fontkeys[] = $font;
  24554. // store object ID for current font
  24555. ++$this->n;
  24556. $this->font_obj_ids[$font] = $this->n;
  24557. $this->setFontSubBuffer($font, 'n', $this->n);
  24558. }
  24559. }
  24560. /**
  24561. * Set font buffer content.
  24562. * @param $font (string) font key
  24563. * @param $key (string) font sub-key
  24564. * @param $data (array) font data
  24565. * @protected
  24566. * @since 4.5.000 (2009-01-02)
  24567. */
  24568. protected function setFontSubBuffer($font, $key, $data) {
  24569. if (!isset($this->fonts[$font])) {
  24570. $this->setFontBuffer($font, array());
  24571. }
  24572. if ($this->diskcache) {
  24573. $tmpfont = $this->getFontBuffer($font);
  24574. $tmpfont[$key] = $data;
  24575. $this->writeDiskCache($this->fonts[$font], serialize($tmpfont));
  24576. } else {
  24577. $this->fonts[$font][$key] = $data;
  24578. }
  24579. }
  24580. /**
  24581. * Get font buffer content.
  24582. * @param $font (string) font key
  24583. * @return string font buffer content or false in case of error
  24584. * @protected
  24585. * @since 4.5.000 (2009-01-02)
  24586. */
  24587. protected function getFontBuffer($font) {
  24588. if ($this->diskcache AND isset($this->fonts[$font])) {
  24589. return unserialize($this->readDiskCache($this->fonts[$font]));
  24590. } elseif (isset($this->fonts[$font])) {
  24591. return $this->fonts[$font];
  24592. }
  24593. return false;
  24594. }
  24595. /**
  24596. * Move a page to a previous position.
  24597. * @param $frompage (int) number of the source page
  24598. * @param $topage (int) number of the destination page (must be less than $frompage)
  24599. * @return true in case of success, false in case of error.
  24600. * @public
  24601. * @since 4.5.000 (2009-01-02)
  24602. */
  24603. public function movePage($frompage, $topage) {
  24604. if (($frompage > $this->numpages) OR ($frompage <= $topage)) {
  24605. return false;
  24606. }
  24607. if ($frompage == $this->page) {
  24608. // close the page before moving it
  24609. $this->endPage();
  24610. }
  24611. // move all page-related states
  24612. $tmppage = $this->getPageBuffer($frompage);
  24613. $tmppagedim = $this->pagedim[$frompage];
  24614. $tmppagelen = $this->pagelen[$frompage];
  24615. $tmpintmrk = $this->intmrk[$frompage];
  24616. $tmpbordermrk = $this->bordermrk[$frompage];
  24617. $tmpcntmrk = $this->cntmrk[$frompage];
  24618. if (isset($this->footerpos[$frompage])) {
  24619. $tmpfooterpos = $this->footerpos[$frompage];
  24620. }
  24621. if (isset($this->footerlen[$frompage])) {
  24622. $tmpfooterlen = $this->footerlen[$frompage];
  24623. }
  24624. if (isset($this->transfmrk[$frompage])) {
  24625. $tmptransfmrk = $this->transfmrk[$frompage];
  24626. }
  24627. if (isset($this->PageAnnots[$frompage])) {
  24628. $tmpannots = $this->PageAnnots[$frompage];
  24629. }
  24630. if (isset($this->newpagegroup) AND !empty($this->newpagegroup)) {
  24631. for ($i = $frompage; $i > $topage; --$i) {
  24632. if (isset($this->newpagegroup[$i]) AND (($i + $this->pagegroups[$this->newpagegroup[$i]]) > $frompage)) {
  24633. --$this->pagegroups[$this->newpagegroup[$i]];
  24634. break;
  24635. }
  24636. }
  24637. for ($i = $topage; $i > 0; --$i) {
  24638. if (isset($this->newpagegroup[$i]) AND (($i + $this->pagegroups[$this->newpagegroup[$i]]) > $topage)) {
  24639. ++$this->pagegroups[$this->newpagegroup[$i]];
  24640. break;
  24641. }
  24642. }
  24643. }
  24644. for ($i = $frompage; $i > $topage; --$i) {
  24645. $j = $i - 1;
  24646. // shift pages down
  24647. $this->setPageBuffer($i, $this->getPageBuffer($j));
  24648. $this->pagedim[$i] = $this->pagedim[$j];
  24649. $this->pagelen[$i] = $this->pagelen[$j];
  24650. $this->intmrk[$i] = $this->intmrk[$j];
  24651. $this->bordermrk[$i] = $this->bordermrk[$j];
  24652. $this->cntmrk[$i] = $this->cntmrk[$j];
  24653. if (isset($this->footerpos[$j])) {
  24654. $this->footerpos[$i] = $this->footerpos[$j];
  24655. } elseif (isset($this->footerpos[$i])) {
  24656. unset($this->footerpos[$i]);
  24657. }
  24658. if (isset($this->footerlen[$j])) {
  24659. $this->footerlen[$i] = $this->footerlen[$j];
  24660. } elseif (isset($this->footerlen[$i])) {
  24661. unset($this->footerlen[$i]);
  24662. }
  24663. if (isset($this->transfmrk[$j])) {
  24664. $this->transfmrk[$i] = $this->transfmrk[$j];
  24665. } elseif (isset($this->transfmrk[$i])) {
  24666. unset($this->transfmrk[$i]);
  24667. }
  24668. if (isset($this->PageAnnots[$j])) {
  24669. $this->PageAnnots[$i] = $this->PageAnnots[$j];
  24670. } elseif (isset($this->PageAnnots[$i])) {
  24671. unset($this->PageAnnots[$i]);
  24672. }
  24673. if (isset($this->newpagegroup[$j])) {
  24674. $this->newpagegroup[$i] = $this->newpagegroup[$j];
  24675. unset($this->newpagegroup[$j]);
  24676. }
  24677. if ($this->currpagegroup == $j) {
  24678. $this->currpagegroup = $i;
  24679. }
  24680. }
  24681. $this->setPageBuffer($topage, $tmppage);
  24682. $this->pagedim[$topage] = $tmppagedim;
  24683. $this->pagelen[$topage] = $tmppagelen;
  24684. $this->intmrk[$topage] = $tmpintmrk;
  24685. $this->bordermrk[$topage] = $tmpbordermrk;
  24686. $this->cntmrk[$topage] = $tmpcntmrk;
  24687. if (isset($tmpfooterpos)) {
  24688. $this->footerpos[$topage] = $tmpfooterpos;
  24689. } elseif (isset($this->footerpos[$topage])) {
  24690. unset($this->footerpos[$topage]);
  24691. }
  24692. if (isset($tmpfooterlen)) {
  24693. $this->footerlen[$topage] = $tmpfooterlen;
  24694. } elseif (isset($this->footerlen[$topage])) {
  24695. unset($this->footerlen[$topage]);
  24696. }
  24697. if (isset($tmptransfmrk)) {
  24698. $this->transfmrk[$topage] = $tmptransfmrk;
  24699. } elseif (isset($this->transfmrk[$topage])) {
  24700. unset($this->transfmrk[$topage]);
  24701. }
  24702. if (isset($tmpannots)) {
  24703. $this->PageAnnots[$topage] = $tmpannots;
  24704. } elseif (isset($this->PageAnnots[$topage])) {
  24705. unset($this->PageAnnots[$topage]);
  24706. }
  24707. // adjust outlines
  24708. $tmpoutlines = $this->outlines;
  24709. foreach ($tmpoutlines as $key => $outline) {
  24710. if (($outline['p'] >= $topage) AND ($outline['p'] < $frompage)) {
  24711. $this->outlines[$key]['p'] = ($outline['p'] + 1);
  24712. } elseif ($outline['p'] == $frompage) {
  24713. $this->outlines[$key]['p'] = $topage;
  24714. }
  24715. }
  24716. // adjust dests
  24717. $tmpdests = $this->dests;
  24718. foreach ($tmpdests as $key => $dest) {
  24719. if (($dest['p'] >= $topage) AND ($dest['p'] < $frompage)) {
  24720. $this->dests[$key]['p'] = ($dest['p'] + 1);
  24721. } elseif ($dest['p'] == $frompage) {
  24722. $this->dests[$key]['p'] = $topage;
  24723. }
  24724. }
  24725. // adjust links
  24726. $tmplinks = $this->links;
  24727. foreach ($tmplinks as $key => $link) {
  24728. if (($link[0] >= $topage) AND ($link[0] < $frompage)) {
  24729. $this->links[$key][0] = ($link[0] + 1);
  24730. } elseif ($link[0] == $frompage) {
  24731. $this->links[$key][0] = $topage;
  24732. }
  24733. }
  24734. // adjust javascript
  24735. $tmpjavascript = $this->javascript;
  24736. global $jfrompage, $jtopage;
  24737. $jfrompage = $frompage;
  24738. $jtopage = $topage;
  24739. $this->javascript = preg_replace_callback('/this\.addField\(\'([^\']*)\',\'([^\']*)\',([0-9]+)/',
  24740. create_function('$matches', 'global $jfrompage, $jtopage;
  24741. $pagenum = intval($matches[3]) + 1;
  24742. if (($pagenum >= $jtopage) AND ($pagenum < $jfrompage)) {
  24743. $newpage = ($pagenum + 1);
  24744. } elseif ($pagenum == $jfrompage) {
  24745. $newpage = $jtopage;
  24746. } else {
  24747. $newpage = $pagenum;
  24748. }
  24749. --$newpage;
  24750. return "this.addField(\'".$matches[1]."\',\'".$matches[2]."\',".$newpage."";'), $tmpjavascript);
  24751. // return to last page
  24752. $this->lastPage(true);
  24753. return true;
  24754. }
  24755. /**
  24756. * Remove the specified page.
  24757. * @param $page (int) page to remove
  24758. * @return true in case of success, false in case of error.
  24759. * @public
  24760. * @since 4.6.004 (2009-04-23)
  24761. */
  24762. public function deletePage($page) {
  24763. if (($page < 1) OR ($page > $this->numpages)) {
  24764. return false;
  24765. }
  24766. // delete current page
  24767. unset($this->pages[$page]);
  24768. unset($this->pagedim[$page]);
  24769. unset($this->pagelen[$page]);
  24770. unset($this->intmrk[$page]);
  24771. unset($this->bordermrk[$page]);
  24772. unset($this->cntmrk[$page]);
  24773. if (isset($this->footerpos[$page])) {
  24774. unset($this->footerpos[$page]);
  24775. }
  24776. if (isset($this->footerlen[$page])) {
  24777. unset($this->footerlen[$page]);
  24778. }
  24779. if (isset($this->transfmrk[$page])) {
  24780. unset($this->transfmrk[$page]);
  24781. }
  24782. if (isset($this->PageAnnots[$page])) {
  24783. unset($this->PageAnnots[$page]);
  24784. }
  24785. if (isset($this->newpagegroup) AND !empty($this->newpagegroup)) {
  24786. for ($i = $page; $i > 0; --$i) {
  24787. if (isset($this->newpagegroup[$i]) AND (($i + $this->pagegroups[$this->newpagegroup[$i]]) > $page)) {
  24788. --$this->pagegroups[$this->newpagegroup[$i]];
  24789. break;
  24790. }
  24791. }
  24792. }
  24793. if (isset($this->pageopen[$page])) {
  24794. unset($this->pageopen[$page]);
  24795. }
  24796. if ($page < $this->numpages) {
  24797. // update remaining pages
  24798. for ($i = $page; $i < $this->numpages; ++$i) {
  24799. $j = $i + 1;
  24800. // shift pages
  24801. $this->setPageBuffer($i, $this->getPageBuffer($j));
  24802. $this->pagedim[$i] = $this->pagedim[$j];
  24803. $this->pagelen[$i] = $this->pagelen[$j];
  24804. $this->intmrk[$i] = $this->intmrk[$j];
  24805. $this->bordermrk[$i] = $this->bordermrk[$j];
  24806. $this->cntmrk[$i] = $this->cntmrk[$j];
  24807. if (isset($this->footerpos[$j])) {
  24808. $this->footerpos[$i] = $this->footerpos[$j];
  24809. } elseif (isset($this->footerpos[$i])) {
  24810. unset($this->footerpos[$i]);
  24811. }
  24812. if (isset($this->footerlen[$j])) {
  24813. $this->footerlen[$i] = $this->footerlen[$j];
  24814. } elseif (isset($this->footerlen[$i])) {
  24815. unset($this->footerlen[$i]);
  24816. }
  24817. if (isset($this->transfmrk[$j])) {
  24818. $this->transfmrk[$i] = $this->transfmrk[$j];
  24819. } elseif (isset($this->transfmrk[$i])) {
  24820. unset($this->transfmrk[$i]);
  24821. }
  24822. if (isset($this->PageAnnots[$j])) {
  24823. $this->PageAnnots[$i] = $this->PageAnnots[$j];
  24824. } elseif (isset($this->PageAnnots[$i])) {
  24825. unset($this->PageAnnots[$i]);
  24826. }
  24827. if (isset($this->newpagegroup[$j])) {
  24828. $this->newpagegroup[$i] = $this->newpagegroup[$j];
  24829. unset($this->newpagegroup[$j]);
  24830. }
  24831. if ($this->currpagegroup == $j) {
  24832. $this->currpagegroup = $i;
  24833. }
  24834. if (isset($this->pageopen[$j])) {
  24835. $this->pageopen[$i] = $this->pageopen[$j];
  24836. } elseif (isset($this->pageopen[$i])) {
  24837. unset($this->pageopen[$i]);
  24838. }
  24839. }
  24840. // remove last page
  24841. unset($this->pages[$this->numpages]);
  24842. unset($this->pagedim[$this->numpages]);
  24843. unset($this->pagelen[$this->numpages]);
  24844. unset($this->intmrk[$this->numpages]);
  24845. unset($this->bordermrk[$this->numpages]);
  24846. unset($this->cntmrk[$this->numpages]);
  24847. if (isset($this->footerpos[$this->numpages])) {
  24848. unset($this->footerpos[$this->numpages]);
  24849. }
  24850. if (isset($this->footerlen[$this->numpages])) {
  24851. unset($this->footerlen[$this->numpages]);
  24852. }
  24853. if (isset($this->transfmrk[$this->numpages])) {
  24854. unset($this->transfmrk[$this->numpages]);
  24855. }
  24856. if (isset($this->PageAnnots[$this->numpages])) {
  24857. unset($this->PageAnnots[$this->numpages]);
  24858. }
  24859. if (isset($this->newpagegroup[$this->numpages])) {
  24860. unset($this->newpagegroup[$this->numpages]);
  24861. }
  24862. if ($this->currpagegroup == $this->numpages) {
  24863. $this->currpagegroup = ($this->numpages - 1);
  24864. }
  24865. if (isset($this->pagegroups[$this->numpages])) {
  24866. unset($this->pagegroups[$this->numpages]);
  24867. }
  24868. if (isset($this->pageopen[$this->numpages])) {
  24869. unset($this->pageopen[$this->numpages]);
  24870. }
  24871. }
  24872. --$this->numpages;
  24873. $this->page = $this->numpages;
  24874. // adjust outlines
  24875. $tmpoutlines = $this->outlines;
  24876. foreach ($tmpoutlines as $key => $outline) {
  24877. if ($outline['p'] > $page) {
  24878. $this->outlines[$key]['p'] = $outline['p'] - 1;
  24879. } elseif ($outline['p'] == $page) {
  24880. unset($this->outlines[$key]);
  24881. }
  24882. }
  24883. // adjust dests
  24884. $tmpdests = $this->dests;
  24885. foreach ($tmpdests as $key => $dest) {
  24886. if ($dest['p'] > $page) {
  24887. $this->dests[$key]['p'] = $dest['p'] - 1;
  24888. } elseif ($dest['p'] == $page) {
  24889. unset($this->dests[$key]);
  24890. }
  24891. }
  24892. // adjust links
  24893. $tmplinks = $this->links;
  24894. foreach ($tmplinks as $key => $link) {
  24895. if ($link[0] > $page) {
  24896. $this->links[$key][0] = $link[0] - 1;
  24897. } elseif ($link[0] == $page) {
  24898. unset($this->links[$key]);
  24899. }
  24900. }
  24901. // adjust javascript
  24902. $tmpjavascript = $this->javascript;
  24903. global $jpage;
  24904. $jpage = $page;
  24905. $this->javascript = preg_replace_callback('/this\.addField\(\'([^\']*)\',\'([^\']*)\',([0-9]+)/',
  24906. create_function('$matches', 'global $jpage;
  24907. $pagenum = intval($matches[3]) + 1;
  24908. if ($pagenum >= $jpage) {
  24909. $newpage = ($pagenum - 1);
  24910. } elseif ($pagenum == $jpage) {
  24911. $newpage = 1;
  24912. } else {
  24913. $newpage = $pagenum;
  24914. }
  24915. --$newpage;
  24916. return "this.addField(\'".$matches[1]."\',\'".$matches[2]."\',".$newpage."";'), $tmpjavascript);
  24917. // return to last page
  24918. $this->lastPage(true);
  24919. return true;
  24920. }
  24921. /**
  24922. * Clone the specified page to a new page.
  24923. * @param $page (int) number of page to copy (0 = current page)
  24924. * @return true in case of success, false in case of error.
  24925. * @public
  24926. * @since 4.9.015 (2010-04-20)
  24927. */
  24928. public function copyPage($page=0) {
  24929. if ($page == 0) {
  24930. // default value
  24931. $page = $this->page;
  24932. }
  24933. if (($page < 1) OR ($page > $this->numpages)) {
  24934. return false;
  24935. }
  24936. // close the last page
  24937. $this->endPage();
  24938. // copy all page-related states
  24939. ++$this->numpages;
  24940. $this->page = $this->numpages;
  24941. $this->setPageBuffer($this->page, $this->getPageBuffer($page));
  24942. $this->pagedim[$this->page] = $this->pagedim[$page];
  24943. $this->pagelen[$this->page] = $this->pagelen[$page];
  24944. $this->intmrk[$this->page] = $this->intmrk[$page];
  24945. $this->bordermrk[$this->page] = $this->bordermrk[$page];
  24946. $this->cntmrk[$this->page] = $this->cntmrk[$page];
  24947. $this->pageopen[$this->page] = false;
  24948. if (isset($this->footerpos[$page])) {
  24949. $this->footerpos[$this->page] = $this->footerpos[$page];
  24950. }
  24951. if (isset($this->footerlen[$page])) {
  24952. $this->footerlen[$this->page] = $this->footerlen[$page];
  24953. }
  24954. if (isset($this->transfmrk[$page])) {
  24955. $this->transfmrk[$this->page] = $this->transfmrk[$page];
  24956. }
  24957. if (isset($this->PageAnnots[$page])) {
  24958. $this->PageAnnots[$this->page] = $this->PageAnnots[$page];
  24959. }
  24960. if (isset($this->newpagegroup[$page])) {
  24961. // start a new group
  24962. $this->newpagegroup[$this->page] = sizeof($this->newpagegroup) + 1;
  24963. $this->currpagegroup = $this->newpagegroup[$this->page];
  24964. $this->pagegroups[$this->currpagegroup] = 1;
  24965. } elseif (isset($this->currpagegroup) AND ($this->currpagegroup > 0)) {
  24966. ++$this->pagegroups[$this->currpagegroup];
  24967. }
  24968. // copy outlines
  24969. $tmpoutlines = $this->outlines;
  24970. foreach ($tmpoutlines as $key => $outline) {
  24971. if ($outline['p'] == $page) {
  24972. $this->outlines[] = array('t' => $outline['t'], 'l' => $outline['l'], 'y' => $outline['y'], 'p' => $this->page, 's' => $outline['s'], 'c' => $outline['c']);
  24973. }
  24974. }
  24975. // copy links
  24976. $tmplinks = $this->links;
  24977. foreach ($tmplinks as $key => $link) {
  24978. if ($link[0] == $page) {
  24979. $this->links[] = array($this->page, $link[1]);
  24980. }
  24981. }
  24982. // return to last page
  24983. $this->lastPage(true);
  24984. return true;
  24985. }
  24986. /**
  24987. * Output a Table of Content Index (TOC).
  24988. * This method must be called after all Bookmarks were set.
  24989. * Before calling this method you have to open the page using the addTOCPage() method.
  24990. * After calling this method you have to call endTOCPage() to close the TOC page.
  24991. * You can override this method to achieve different styles.
  24992. * @param $page (int) page number where this TOC should be inserted (leave empty for current page).
  24993. * @param $numbersfont (string) set the font for page numbers (please use monospaced font for better alignment).
  24994. * @param $filler (string) string used to fill the space between text and page number.
  24995. * @param $toc_name (string) name to use for TOC bookmark.
  24996. * @param $style (string) Font style for title: B = Bold, I = Italic, BI = Bold + Italic.
  24997. * @param $color (array) RGB color array for bookmark title (values from 0 to 255).
  24998. * @public
  24999. * @author Nicola Asuni
  25000. * @since 4.5.000 (2009-01-02)
  25001. * @see addTOCPage(), endTOCPage(), addHTMLTOC()
  25002. */
  25003. public function addTOC($page='', $numbersfont='', $filler='.', $toc_name='TOC', $style='', $color=array(0,0,0)) {
  25004. $fontsize = $this->FontSizePt;
  25005. $fontfamily = $this->FontFamily;
  25006. $fontstyle = $this->FontStyle;
  25007. $w = $this->w - $this->lMargin - $this->rMargin;
  25008. $spacer = $this->GetStringWidth(chr(32)) * 4;
  25009. $page_first = $this->getPage();
  25010. $lmargin = $this->lMargin;
  25011. $rmargin = $this->rMargin;
  25012. $x_start = $this->GetX();
  25013. $current_page = $this->page;
  25014. $current_column = $this->current_column;
  25015. if ($this->empty_string($numbersfont)) {
  25016. $numbersfont = $this->default_monospaced_font;
  25017. }
  25018. if ($this->empty_string($filler)) {
  25019. $filler = ' ';
  25020. }
  25021. if ($this->empty_string($page)) {
  25022. $gap = ' ';
  25023. } else {
  25024. $gap = '';
  25025. if ($page < 1) {
  25026. $page = 1;
  25027. }
  25028. }
  25029. $this->SetFont($numbersfont, $fontstyle, $fontsize);
  25030. $numwidth = $this->GetStringWidth('00000');
  25031. $maxpage = 0; //used for pages on attached documents
  25032. foreach ($this->outlines as $key => $outline) {
  25033. // check for extra pages (used for attachments)
  25034. if (($this->page > $page_first) AND ($outline['p'] >= $this->numpages)) {
  25035. $outline['p'] += ($this->page - $page_first);
  25036. }
  25037. if ($this->rtl) {
  25038. $aligntext = 'R';
  25039. $alignnum = 'L';
  25040. } else {
  25041. $aligntext = 'L';
  25042. $alignnum = 'R';
  25043. }
  25044. if ($outline['l'] == 0) {
  25045. $this->SetFont($fontfamily, $fontstyle.'B', $fontsize);
  25046. } else {
  25047. $this->SetFont($fontfamily, $fontstyle, $fontsize - $outline['l']);
  25048. }
  25049. // check for page break
  25050. $this->checkPageBreak((2 * $this->FontSize * $this->cell_height_ratio));
  25051. // set margins and X position
  25052. if (($this->page == $current_page) AND ($this->current_column == $current_column)) {
  25053. $this->lMargin = $lmargin;
  25054. $this->rMargin = $rmargin;
  25055. } else {
  25056. if ($this->current_column != $current_column) {
  25057. if ($this->rtl) {
  25058. $x_start = $this->w - $this->columns[$this->current_column]['x'];
  25059. } else {
  25060. $x_start = $this->columns[$this->current_column]['x'];
  25061. }
  25062. }
  25063. $lmargin = $this->lMargin;
  25064. $rmargin = $this->rMargin;
  25065. $current_page = $this->page;
  25066. $current_column = $this->current_column;
  25067. }
  25068. $this->SetX($x_start);
  25069. $indent = ($spacer * $outline['l']);
  25070. if ($this->rtl) {
  25071. $this->x -= $indent;
  25072. $this->rMargin = $this->w - $this->x;
  25073. } else {
  25074. $this->x += $indent;
  25075. $this->lMargin = $this->x;
  25076. }
  25077. $link = $this->AddLink();
  25078. $this->SetLink($link, $outline['y'], $outline['p']);
  25079. // write the text
  25080. if ($this->rtl) {
  25081. $txt = ' '.$outline['t'];
  25082. } else {
  25083. $txt = $outline['t'].' ';
  25084. }
  25085. $this->Write(0, $txt, $link, false, $aligntext, false, 0, false, false, 0, $numwidth, '');
  25086. if ($this->rtl) {
  25087. $tw = $this->x - $this->lMargin;
  25088. } else {
  25089. $tw = $this->w - $this->rMargin - $this->x;
  25090. }
  25091. $this->SetFont($numbersfont, $fontstyle, $fontsize);
  25092. if ($this->empty_string($page)) {
  25093. $pagenum = $outline['p'];
  25094. } else {
  25095. // placemark to be replaced with the correct number
  25096. $pagenum = '{#'.($outline['p']).'}';
  25097. if ($this->isUnicodeFont()) {
  25098. $pagenum = '{'.$pagenum.'}';
  25099. }
  25100. $maxpage = max($maxpage, $outline['p']);
  25101. }
  25102. $fw = ($tw - $this->GetStringWidth($pagenum.$filler));
  25103. $numfills = floor($fw / $this->GetStringWidth($filler));
  25104. if ($numfills > 0) {
  25105. $rowfill = str_repeat($filler, $numfills);
  25106. } else {
  25107. $rowfill = '';
  25108. }
  25109. if ($this->rtl) {
  25110. $pagenum = $pagenum.$gap.$rowfill;
  25111. } else {
  25112. $pagenum = $rowfill.$gap.$pagenum;
  25113. }
  25114. // write the number
  25115. $this->Cell($tw, 0, $pagenum, 0, 1, $alignnum, 0, $link, 0);
  25116. }
  25117. $page_last = $this->getPage();
  25118. $maxpage = max($maxpage, $page_last);
  25119. $numpages = $page_last - $page_first + 1;
  25120. if (!$this->empty_string($page)) {
  25121. for ($p = $page_first; $p <= $page_last; ++$p) {
  25122. // get page data
  25123. $temppage = $this->getPageBuffer($p);
  25124. for ($n = 1; $n <= $maxpage; ++$n) {
  25125. // update page numbers
  25126. $a = '{#'.$n.'}';
  25127. // get page number aliases
  25128. $pnalias = $this->getInternalPageNumberAliases($a);
  25129. // calculate replacement number
  25130. if (($n >= $page) AND ($n <= $this->numpages)) {
  25131. $np = $n + $numpages;
  25132. } else {
  25133. $np = $n;
  25134. }
  25135. $na = $this->formatTOCPageNumber(($this->starting_page_number + $np - 1));
  25136. $nu = $this->UTF8ToUTF16BE($na, false);
  25137. // replace aliases with numbers
  25138. foreach ($pnalias['u'] as $u) {
  25139. $sfill = str_repeat($filler, max(0, (strlen($u) - strlen($nu.' '))));
  25140. if ($this->rtl) {
  25141. $nr = $nu.$this->UTF8ToUTF16BE(' '.$sfill);
  25142. } else {
  25143. $nr = $this->UTF8ToUTF16BE($sfill.' ').$nu;
  25144. }
  25145. $temppage = str_replace($u, $nr, $temppage);
  25146. }
  25147. foreach ($pnalias['a'] as $a) {
  25148. $sfill = str_repeat($filler, max(0, (strlen($a) - strlen($na.' '))));
  25149. if ($this->rtl) {
  25150. $nr = $na.' '.$sfill;
  25151. } else {
  25152. $nr = $sfill.' '.$na;
  25153. }
  25154. $temppage = str_replace($a, $nr, $temppage);
  25155. }
  25156. }
  25157. // save changes
  25158. $this->setPageBuffer($p, $temppage);
  25159. }
  25160. // move pages
  25161. $this->Bookmark($toc_name, 0, 0, $page_first, $style, $color);
  25162. for ($i = 0; $i < $numpages; ++$i) {
  25163. $this->movePage($page_last, $page);
  25164. }
  25165. }
  25166. }
  25167. /**
  25168. * Output a Table Of Content Index (TOC) using HTML templates.
  25169. * This method must be called after all Bookmarks were set.
  25170. * Before calling this method you have to open the page using the addTOCPage() method.
  25171. * After calling this method you have to call endTOCPage() to close the TOC page.
  25172. * @param $page (int) page number where this TOC should be inserted (leave empty for current page).
  25173. * @param $toc_name (string) name to use for TOC bookmark.
  25174. * @param $templates (array) array of html templates. Use: "#TOC_DESCRIPTION#" for bookmark title, "#TOC_PAGE_NUMBER#" for page number.
  25175. * @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)
  25176. * @param $style (string) Font style for title: B = Bold, I = Italic, BI = Bold + Italic.
  25177. * @param $color (array) RGB color array for title (values from 0 to 255).
  25178. * @public
  25179. * @author Nicola Asuni
  25180. * @since 5.0.001 (2010-05-06)
  25181. * @see addTOCPage(), endTOCPage(), addTOC()
  25182. */
  25183. public function addHTMLTOC($page='', $toc_name='TOC', $templates=array(), $correct_align=true, $style='', $color=array(0,0,0)) {
  25184. $filler = ' ';
  25185. $prev_htmlLinkColorArray = $this->htmlLinkColorArray;
  25186. $prev_htmlLinkFontStyle = $this->htmlLinkFontStyle;
  25187. // set new style for link
  25188. $this->htmlLinkColorArray = array();
  25189. $this->htmlLinkFontStyle = '';
  25190. $page_first = $this->getPage();
  25191. // get the font type used for numbers in each template
  25192. $current_font = $this->FontFamily;
  25193. foreach ($templates as $level => $html) {
  25194. $dom = $this->getHtmlDomArray($html);
  25195. foreach ($dom as $key => $value) {
  25196. if ($value['value'] == '#TOC_PAGE_NUMBER#') {
  25197. $this->SetFont($dom[($key - 1)]['fontname']);
  25198. $templates['F'.$level] = $this->isUnicodeFont();
  25199. }
  25200. }
  25201. }
  25202. $this->SetFont($current_font);
  25203. $maxpage = 0; //used for pages on attached documents
  25204. foreach ($this->outlines as $key => $outline) {
  25205. // get HTML template
  25206. $row = $templates[$outline['l']];
  25207. if ($this->empty_string($page)) {
  25208. $pagenum = $outline['p'];
  25209. } else {
  25210. // placemark to be replaced with the correct number
  25211. $pagenum = '{#'.($outline['p']).'}';
  25212. if ($templates['F'.$outline['l']]) {
  25213. $pagenum = '{'.$pagenum.'}';
  25214. }
  25215. $maxpage = max($maxpage, $outline['p']);
  25216. }
  25217. // replace templates with current values
  25218. $row = str_replace('#TOC_DESCRIPTION#', $outline['t'], $row);
  25219. $row = str_replace('#TOC_PAGE_NUMBER#', $pagenum, $row);
  25220. // add link to page
  25221. $row = '<a href="#'.$outline['p'].','.$outline['y'].'">'.$row.'</a>';
  25222. // write bookmark entry
  25223. $this->writeHTML($row, false, false, true, false, '');
  25224. }
  25225. // restore link styles
  25226. $this->htmlLinkColorArray = $prev_htmlLinkColorArray;
  25227. $this->htmlLinkFontStyle = $prev_htmlLinkFontStyle;
  25228. // move TOC page and replace numbers
  25229. $page_last = $this->getPage();
  25230. $maxpage = max($maxpage, $page_last);
  25231. $numpages = $page_last - $page_first + 1;
  25232. if (!$this->empty_string($page)) {
  25233. for ($p = $page_first; $p <= $page_last; ++$p) {
  25234. // get page data
  25235. $temppage = $this->getPageBuffer($p);
  25236. for ($n = 1; $n <= $maxpage; ++$n) {
  25237. // update page numbers
  25238. $a = '{#'.$n.'}';
  25239. // get page number aliases
  25240. $pnalias = $this->getInternalPageNumberAliases($a);
  25241. // calculate replacement number
  25242. if ($n >= $page) {
  25243. $np = $n + $numpages;
  25244. } else {
  25245. $np = $n;
  25246. }
  25247. $na = $this->formatTOCPageNumber(($this->starting_page_number + $np - 1));
  25248. $nu = $this->UTF8ToUTF16BE($na, false);
  25249. // replace aliases with numbers
  25250. foreach ($pnalias['u'] as $u) {
  25251. if ($correct_align) {
  25252. $sfill = str_repeat($filler, (strlen($u) - strlen($nu.' ')));
  25253. if ($this->rtl) {
  25254. $nr = $nu.$this->UTF8ToUTF16BE(' '.$sfill);
  25255. } else {
  25256. $nr = $this->UTF8ToUTF16BE($sfill.' ').$nu;
  25257. }
  25258. } else {
  25259. $nr = $nu;
  25260. }
  25261. $temppage = str_replace($u, $nr, $temppage);
  25262. }
  25263. foreach ($pnalias['a'] as $a) {
  25264. if ($correct_align) {
  25265. $sfill = str_repeat($filler, (strlen($a) - strlen($na.' ')));
  25266. if ($this->rtl) {
  25267. $nr = $na.' '.$sfill;
  25268. } else {
  25269. $nr = $sfill.' '.$na;
  25270. }
  25271. } else {
  25272. $nr = $na;
  25273. }
  25274. $temppage = str_replace($a, $nr, $temppage);
  25275. }
  25276. }
  25277. // save changes
  25278. $this->setPageBuffer($p, $temppage);
  25279. }
  25280. // move pages
  25281. $this->Bookmark($toc_name, 0, 0, $page_first, $style, $color);
  25282. for ($i = 0; $i < $numpages; ++$i) {
  25283. $this->movePage($page_last, $page);
  25284. }
  25285. }
  25286. }
  25287. /**
  25288. * Stores a copy of the current TCPDF object used for undo operation.
  25289. * @public
  25290. * @since 4.5.029 (2009-03-19)
  25291. */
  25292. public function startTransaction() {
  25293. if (isset($this->objcopy)) {
  25294. // remove previous copy
  25295. $this->commitTransaction();
  25296. }
  25297. // record current page number and Y position
  25298. $this->start_transaction_page = $this->page;
  25299. $this->start_transaction_y = $this->y;
  25300. // clone current object
  25301. $this->objcopy = $this->objclone($this);
  25302. }
  25303. /**
  25304. * Delete the copy of the current TCPDF object used for undo operation.
  25305. * @public
  25306. * @since 4.5.029 (2009-03-19)
  25307. */
  25308. public function commitTransaction() {
  25309. if (isset($this->objcopy)) {
  25310. $this->objcopy->_destroy(true, true);
  25311. unset($this->objcopy);
  25312. }
  25313. }
  25314. /**
  25315. * This method allows to undo the latest transaction by returning the latest saved TCPDF object with startTransaction().
  25316. * @param $self (boolean) if true restores current class object to previous state without the need of reassignment via the returned value.
  25317. * @return TCPDF object.
  25318. * @public
  25319. * @since 4.5.029 (2009-03-19)
  25320. */
  25321. public function rollbackTransaction($self=false) {
  25322. if (isset($this->objcopy)) {
  25323. if (isset($this->objcopy->diskcache) AND $this->objcopy->diskcache) {
  25324. // truncate files to previous values
  25325. foreach ($this->objcopy->cache_file_length as $file => $length) {
  25326. $file = substr($file, 1);
  25327. $handle = fopen($file, 'r+');
  25328. ftruncate($handle, $length);
  25329. }
  25330. }
  25331. $this->_destroy(true, true);
  25332. if ($self) {
  25333. $objvars = get_object_vars($this->objcopy);
  25334. foreach ($objvars as $key => $value) {
  25335. $this->$key = $value;
  25336. }
  25337. }
  25338. return $this->objcopy;
  25339. }
  25340. return $this;
  25341. }
  25342. /**
  25343. * Creates a copy of a class object
  25344. * @param $object (object) class object to be cloned
  25345. * @return cloned object
  25346. * @public
  25347. * @since 4.5.029 (2009-03-19)
  25348. */
  25349. public function objclone($object) {
  25350. return @clone($object);
  25351. }
  25352. /**
  25353. * Determine whether a string is empty.
  25354. * @param $str (string) string to be checked
  25355. * @return boolean true if string is empty
  25356. * @public
  25357. * @since 4.5.044 (2009-04-16)
  25358. */
  25359. public function empty_string($str) {
  25360. return (is_null($str) OR (is_string($str) AND (strlen($str) == 0)));
  25361. }
  25362. /**
  25363. * Find position of last occurrence of a substring in a string
  25364. * @param $haystack (string) The string to search in.
  25365. * @param $needle (string) substring to search.
  25366. * @param $offset (int) May be specified to begin searching an arbitrary number of characters into the string.
  25367. * @return Returns the position where the needle exists. Returns FALSE if the needle was not found.
  25368. * @public
  25369. * @since 4.8.038 (2010-03-13)
  25370. */
  25371. public function revstrpos($haystack, $needle, $offset = 0) {
  25372. $length = strlen($haystack);
  25373. $offset = ($offset > 0)?($length - $offset):abs($offset);
  25374. $pos = strpos(strrev($haystack), strrev($needle), $offset);
  25375. return ($pos === false)?false:($length - $pos - strlen($needle));
  25376. }
  25377. // --- MULTI COLUMNS METHODS -----------------------
  25378. /**
  25379. * Set multiple columns of the same size
  25380. * @param $numcols (int) number of columns (set to zero to disable columns mode)
  25381. * @param $width (int) column width
  25382. * @param $y (int) column starting Y position (leave empty for current Y position)
  25383. * @public
  25384. * @since 4.9.001 (2010-03-28)
  25385. */
  25386. public function setEqualColumns($numcols=0, $width=0, $y='') {
  25387. $this->columns = array();
  25388. if ($numcols < 2) {
  25389. $numcols = 0;
  25390. $this->columns = array();
  25391. } else {
  25392. // maximum column width
  25393. $maxwidth = ($this->w - $this->original_lMargin - $this->original_rMargin) / $numcols;
  25394. if (($width == 0) OR ($width > $maxwidth)) {
  25395. $width = $maxwidth;
  25396. }
  25397. if ($this->empty_string($y)) {
  25398. $y = $this->y;
  25399. }
  25400. // space between columns
  25401. $space = (($this->w - $this->original_lMargin - $this->original_rMargin - ($numcols * $width)) / ($numcols - 1));
  25402. // fill the columns array (with, space, starting Y position)
  25403. for ($i = 0; $i < $numcols; ++$i) {
  25404. $this->columns[$i] = array('w' => $width, 's' => $space, 'y' => $y);
  25405. }
  25406. }
  25407. $this->num_columns = $numcols;
  25408. $this->current_column = 0;
  25409. $this->column_start_page = $this->page;
  25410. $this->selectColumn(0);
  25411. }
  25412. /**
  25413. * Remove columns and reset page margins.
  25414. * @public
  25415. * @since 5.9.072 (2011-04-26)
  25416. */
  25417. public function resetColumns() {
  25418. $this->lMargin = $this->original_lMargin;
  25419. $this->rMargin = $this->original_rMargin;
  25420. $this->setEqualColumns();
  25421. }
  25422. /**
  25423. * Set columns array.
  25424. * Each column is represented by an array of arrays with the following keys: (w = width, s = space between columns, y = column top position).
  25425. * @param $columns (array)
  25426. * @public
  25427. * @since 4.9.001 (2010-03-28)
  25428. */
  25429. public function setColumnsArray($columns) {
  25430. $this->columns = $columns;
  25431. $this->num_columns = count($columns);
  25432. $this->current_column = 0;
  25433. $this->column_start_page = $this->page;
  25434. $this->selectColumn(0);
  25435. }
  25436. /**
  25437. * Set position at a given column
  25438. * @param $col (int) column number (from 0 to getNumberOfColumns()-1); empty string = current column.
  25439. * @public
  25440. * @since 4.9.001 (2010-03-28)
  25441. */
  25442. public function selectColumn($col='') {
  25443. if (is_string($col)) {
  25444. $col = $this->current_column;
  25445. } elseif ($col >= $this->num_columns) {
  25446. $col = 0;
  25447. }
  25448. $xshift = array('x' => 0, 's' => array('H' => 0, 'V' => 0), 'p' => array('L' => 0, 'T' => 0, 'R' => 0, 'B' => 0));
  25449. $enable_thead = false;
  25450. if ($this->num_columns > 1) {
  25451. if ($col != $this->current_column) {
  25452. // move Y pointer at the top of the column
  25453. if ($this->column_start_page == $this->page) {
  25454. $this->y = $this->columns[$col]['y'];
  25455. } else {
  25456. $this->y = $this->tMargin;
  25457. }
  25458. // Avoid to write table headers more than once
  25459. if (($this->page > $this->maxselcol['page']) OR (($this->page == $this->maxselcol['page']) AND ($col > $this->maxselcol['column']))) {
  25460. $enable_thead = true;
  25461. $this->maxselcol['page'] = $this->page;
  25462. $this->maxselcol['column'] = $col;
  25463. }
  25464. }
  25465. $xshift = $this->colxshift;
  25466. // set X position of the current column by case
  25467. $listindent = ($this->listindentlevel * $this->listindent);
  25468. // calculate column X position
  25469. $colpos = 0;
  25470. for ($i = 0; $i < $col; ++$i) {
  25471. $colpos += ($this->columns[$i]['w'] + $this->columns[$i]['s']);
  25472. }
  25473. if ($this->rtl) {
  25474. $x = $this->w - $this->original_rMargin - $colpos;
  25475. $this->rMargin = ($this->w - $x + $listindent);
  25476. $this->lMargin = ($x - $this->columns[$col]['w']);
  25477. $this->x = $x - $listindent;
  25478. } else {
  25479. $x = $this->original_lMargin + $colpos;
  25480. $this->lMargin = ($x + $listindent);
  25481. $this->rMargin = ($this->w - $x - $this->columns[$col]['w']);
  25482. $this->x = $x + $listindent;
  25483. }
  25484. $this->columns[$col]['x'] = $x;
  25485. }
  25486. $this->current_column = $col;
  25487. // fix for HTML mode
  25488. $this->newline = true;
  25489. // print HTML table header (if any)
  25490. if ((!$this->empty_string($this->thead)) AND (!$this->inthead)) {
  25491. if ($enable_thead) {
  25492. // print table header
  25493. $this->writeHTML($this->thead, false, false, false, false, '');
  25494. $this->y += $xshift['s']['V'];
  25495. // store end of header position
  25496. if (!isset($this->columns[$col]['th'])) {
  25497. $this->columns[$col]['th'] = array();
  25498. }
  25499. $this->columns[$col]['th']['\''.$this->page.'\''] = $this->y;
  25500. $this->lasth = 0;
  25501. } elseif (isset($this->columns[$col]['th']['\''.$this->page.'\''])) {
  25502. $this->y = $this->columns[$col]['th']['\''.$this->page.'\''];
  25503. }
  25504. }
  25505. // account for an html table cell over multiple columns
  25506. if ($this->rtl) {
  25507. $this->rMargin += $xshift['x'];
  25508. $this->x -= ($xshift['x'] + $xshift['p']['R']);
  25509. } else {
  25510. $this->lMargin += $xshift['x'];
  25511. $this->x += $xshift['x'] + $xshift['p']['L'];
  25512. }
  25513. }
  25514. /**
  25515. * Return the current column number
  25516. * @return int current column number
  25517. * @public
  25518. * @since 5.5.011 (2010-07-08)
  25519. */
  25520. public function getColumn() {
  25521. return $this->current_column;
  25522. }
  25523. /**
  25524. * Return the current number of columns.
  25525. * @return int number of columns
  25526. * @public
  25527. * @since 5.8.018 (2010-08-25)
  25528. */
  25529. public function getNumberOfColumns() {
  25530. return $this->num_columns;
  25531. }
  25532. /**
  25533. * Serialize an array of parameters to be used with TCPDF tag in HTML code.
  25534. * @param $pararray (array) parameters array
  25535. * @return sting containing serialized data
  25536. * @public
  25537. * @since 4.9.006 (2010-04-02)
  25538. */
  25539. public function serializeTCPDFtagParameters($pararray) {
  25540. return urlencode(serialize($pararray));
  25541. }
  25542. /**
  25543. * Set Text rendering mode.
  25544. * @param $stroke (int) outline size in user units (0 = disable).
  25545. * @param $fill (boolean) if true fills the text (default).
  25546. * @param $clip (boolean) if true activate clipping mode
  25547. * @public
  25548. * @since 4.9.008 (2009-04-02)
  25549. */
  25550. public function setTextRenderingMode($stroke=0, $fill=true, $clip=false) {
  25551. // Ref.: PDF 32000-1:2008 - 9.3.6 Text Rendering Mode
  25552. // convert text rendering parameters
  25553. if ($stroke < 0) {
  25554. $stroke = 0;
  25555. }
  25556. if ($fill === true) {
  25557. if ($stroke > 0) {
  25558. if ($clip === true) {
  25559. // Fill, then stroke text and add to path for clipping
  25560. $textrendermode = 6;
  25561. } else {
  25562. // Fill, then stroke text
  25563. $textrendermode = 2;
  25564. }
  25565. $textstrokewidth = $stroke;
  25566. } else {
  25567. if ($clip === true) {
  25568. // Fill text and add to path for clipping
  25569. $textrendermode = 4;
  25570. } else {
  25571. // Fill text
  25572. $textrendermode = 0;
  25573. }
  25574. }
  25575. } else {
  25576. if ($stroke > 0) {
  25577. if ($clip === true) {
  25578. // Stroke text and add to path for clipping
  25579. $textrendermode = 5;
  25580. } else {
  25581. // Stroke text
  25582. $textrendermode = 1;
  25583. }
  25584. $textstrokewidth = $stroke;
  25585. } else {
  25586. if ($clip === true) {
  25587. // Add text to path for clipping
  25588. $textrendermode = 7;
  25589. } else {
  25590. // Neither fill nor stroke text (invisible)
  25591. $textrendermode = 3;
  25592. }
  25593. }
  25594. }
  25595. $this->textrendermode = $textrendermode;
  25596. $this->textstrokewidth = $stroke * $this->k;
  25597. }
  25598. /**
  25599. * Returns an array of chars containing soft hyphens.
  25600. * @param $word (array) array of chars
  25601. * @param $patterns (array) Array of hypenation patterns.
  25602. * @param $dictionary (array) Array of words to be returned without applying the hyphenation algoritm.
  25603. * @param $leftmin (int) Minimum number of character to leave on the left of the word without applying the hyphens.
  25604. * @param $rightmin (int) Minimum number of character to leave on the right of the word without applying the hyphens.
  25605. * @param $charmin (int) Minimum word length to apply the hyphenation algoritm.
  25606. * @param $charmax (int) Maximum length of broken piece of word.
  25607. * @return array text with soft hyphens
  25608. * @author Nicola Asuni
  25609. * @since 4.9.012 (2010-04-12)
  25610. * @protected
  25611. */
  25612. protected function hyphenateWord($word, $patterns, $dictionary=array(), $leftmin=1, $rightmin=2, $charmin=1, $charmax=8) {
  25613. $hyphenword = array(); // hyphens positions
  25614. $numchars = count($word);
  25615. if ($numchars <= $charmin) {
  25616. return $word;
  25617. }
  25618. $word_string = $this->UTF8ArrSubString($word);
  25619. // some words will be returned as-is
  25620. $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})(\]?)$/';
  25621. if (preg_match($pattern, $word_string) > 0) {
  25622. // email
  25623. return $word;
  25624. }
  25625. $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})(\]?)$/';
  25626. if (preg_match($pattern, $word_string) > 0) {
  25627. // URL
  25628. return $word;
  25629. }
  25630. if (isset($dictionary[$word_string])) {
  25631. return $this->UTF8StringToArray($dictionary[$word_string]);
  25632. }
  25633. // suround word with '_' characters
  25634. $tmpword = array_merge(array(95), $word, array(95));
  25635. $tmpnumchars = $numchars + 2;
  25636. $maxpos = $tmpnumchars - $charmin;
  25637. for ($pos = 0; $pos < $maxpos; ++$pos) {
  25638. $imax = min(($tmpnumchars - $pos), $charmax);
  25639. for ($i = $charmin; $i <= $imax; ++$i) {
  25640. $subword = strtolower($this->UTF8ArrSubString($tmpword, $pos, $pos + $i));
  25641. if (isset($patterns[$subword])) {
  25642. $pattern = $this->UTF8StringToArray($patterns[$subword]);
  25643. $pattern_length = count($pattern);
  25644. $digits = 1;
  25645. for ($j = 0; $j < $pattern_length; ++$j) {
  25646. // check if $pattern[$j] is a number
  25647. if (($pattern[$j] >= 48) AND ($pattern[$j] <= 57)) {
  25648. if ($j == 0) {
  25649. $zero = $pos - 1;
  25650. } else {
  25651. $zero = $pos + $j - $digits;
  25652. }
  25653. if (!isset($hyphenword[$zero]) OR ($hyphenword[$zero] != $pattern[$j])) {
  25654. $hyphenword[$zero] = $this->unichr($pattern[$j]);
  25655. }
  25656. ++$digits;
  25657. }
  25658. }
  25659. }
  25660. }
  25661. }
  25662. $inserted = 0;
  25663. $maxpos = $numchars - $rightmin;
  25664. for ($i = $leftmin; $i <= $maxpos; ++$i) {
  25665. if (isset($hyphenword[$i]) AND (($hyphenword[$i] % 2) != 0)) {
  25666. // 173 = soft hyphen character
  25667. array_splice($word, $i + $inserted, 0, 173);
  25668. ++$inserted;
  25669. }
  25670. }
  25671. return $word;
  25672. }
  25673. /**
  25674. * Returns an array of hyphenation patterns.
  25675. * @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/
  25676. * @return array of hyphenation patterns
  25677. * @author Nicola Asuni
  25678. * @since 4.9.012 (2010-04-12)
  25679. * @public
  25680. */
  25681. public function getHyphenPatternsFromTEX($file) {
  25682. // TEX patterns are available at:
  25683. // http://www.ctan.org/tex-archive/language/hyph-utf8/tex/generic/hyph-utf8/patterns/
  25684. $data = file_get_contents($file);
  25685. $patterns = array();
  25686. // remove comments
  25687. $data = preg_replace('/\%[^\n]*/', '', $data);
  25688. // extract the patterns part
  25689. preg_match('/\\\\patterns\{([^\}]*)\}/i', $data, $matches);
  25690. $data = trim(substr($matches[0], 10, -1));
  25691. // extract each pattern
  25692. $patterns_array = preg_split('/[\s]+/', $data);
  25693. // create new language array of patterns
  25694. $patterns = array();
  25695. foreach($patterns_array as $val) {
  25696. if (!$this->empty_string($val)) {
  25697. $val = trim($val);
  25698. $val = str_replace('\'', '\\\'', $val);
  25699. $key = preg_replace('/[0-9]+/', '', $val);
  25700. $patterns[$key] = $val;
  25701. }
  25702. }
  25703. return $patterns;
  25704. }
  25705. /**
  25706. * Returns text with soft hyphens.
  25707. * @param $text (string) text to process
  25708. * @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/
  25709. * @param $dictionary (array) Array of words to be returned without applying the hyphenation algoritm.
  25710. * @param $leftmin (int) Minimum number of character to leave on the left of the word without applying the hyphens.
  25711. * @param $rightmin (int) Minimum number of character to leave on the right of the word without applying the hyphens.
  25712. * @param $charmin (int) Minimum word length to apply the hyphenation algoritm.
  25713. * @param $charmax (int) Maximum length of broken piece of word.
  25714. * @return array text with soft hyphens
  25715. * @author Nicola Asuni
  25716. * @since 4.9.012 (2010-04-12)
  25717. * @public
  25718. */
  25719. public function hyphenateText($text, $patterns, $dictionary=array(), $leftmin=1, $rightmin=2, $charmin=1, $charmax=8) {
  25720. $text = $this->unhtmlentities($text);
  25721. $word = array(); // last word
  25722. $txtarr = array(); // text to be returned
  25723. $intag = false; // true if we are inside an HTML tag
  25724. if (!is_array($patterns)) {
  25725. $patterns = $this->getHyphenPatternsFromTEX($patterns);
  25726. }
  25727. // get array of characters
  25728. $unichars = $this->UTF8StringToArray($text);
  25729. // for each char
  25730. foreach ($unichars as $char) {
  25731. if ((!$intag) AND $this->unicode->uni_type[$char] == 'L') {
  25732. // letter character
  25733. $word[] = $char;
  25734. } else {
  25735. // other type of character
  25736. if (!$this->empty_string($word)) {
  25737. // hypenate the word
  25738. $txtarr = array_merge($txtarr, $this->hyphenateWord($word, $patterns, $dictionary, $leftmin, $rightmin, $charmin, $charmax));
  25739. $word = array();
  25740. }
  25741. $txtarr[] = $char;
  25742. if (chr($char) == '<') {
  25743. // we are inside an HTML tag
  25744. $intag = true;
  25745. } elseif ($intag AND (chr($char) == '>')) {
  25746. // end of HTML tag
  25747. $intag = false;
  25748. }
  25749. }
  25750. }
  25751. if (!$this->empty_string($word)) {
  25752. // hypenate the word
  25753. $txtarr = array_merge($txtarr, $this->hyphenateWord($word, $patterns, $dictionary, $leftmin, $rightmin, $charmin, $charmax));
  25754. }
  25755. // convert char array to string and return
  25756. return $this->UTF8ArrSubString($txtarr);
  25757. }
  25758. /**
  25759. * Enable/disable rasterization of vector images using ImageMagick library.
  25760. * @param $mode (boolean) if true enable rasterization, false otherwise.
  25761. * @public
  25762. * @since 5.0.000 (2010-04-27)
  25763. */
  25764. public function setRasterizeVectorImages($mode) {
  25765. $this->rasterize_vector_images = $mode;
  25766. }
  25767. /**
  25768. * Get the Path-Painting Operators.
  25769. * @param $style (string) Style of rendering. Possible values are:
  25770. * <ul>
  25771. * <li>S or D: Stroke the path.</li>
  25772. * <li>s or d: Close and stroke the path.</li>
  25773. * <li>f or F: Fill the path, using the nonzero winding number rule to determine the region to fill.</li>
  25774. * <li>f* or F*: Fill the path, using the even-odd rule to determine the region to fill.</li>
  25775. * <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>
  25776. * <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>
  25777. * <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>
  25778. * <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>
  25779. * <li>CNZ: Clipping mode using the even-odd rule to determine which regions lie inside the clipping path.</li>
  25780. * <li>CEO: Clipping mode using the nonzero winding number rule to determine which regions lie inside the clipping path</li>
  25781. * <li>n: End the path object without filling or stroking it.</li>
  25782. * </ul>
  25783. * @param $default (string) default style
  25784. * @author Nicola Asuni
  25785. * @since 5.0.000 (2010-04-30)
  25786. * @protected
  25787. */
  25788. protected function getPathPaintOperator($style, $default='S') {
  25789. $op = '';
  25790. switch($style) {
  25791. case 'S':
  25792. case 'D': {
  25793. $op = 'S';
  25794. break;
  25795. }
  25796. case 's':
  25797. case 'd': {
  25798. $op = 's';
  25799. break;
  25800. }
  25801. case 'f':
  25802. case 'F': {
  25803. $op = 'f';
  25804. break;
  25805. }
  25806. case 'f*':
  25807. case 'F*': {
  25808. $op = 'f*';
  25809. break;
  25810. }
  25811. case 'B':
  25812. case 'FD':
  25813. case 'DF': {
  25814. $op = 'B';
  25815. break;
  25816. }
  25817. case 'B*':
  25818. case 'F*D':
  25819. case 'DF*': {
  25820. $op = 'B*';
  25821. break;
  25822. }
  25823. case 'b':
  25824. case 'fd':
  25825. case 'df': {
  25826. $op = 'b';
  25827. break;
  25828. }
  25829. case 'b*':
  25830. case 'f*d':
  25831. case 'df*': {
  25832. $op = 'b*';
  25833. break;
  25834. }
  25835. case 'CNZ': {
  25836. $op = 'W n';
  25837. break;
  25838. }
  25839. case 'CEO': {
  25840. $op = 'W* n';
  25841. break;
  25842. }
  25843. case 'n': {
  25844. $op = 'n';
  25845. break;
  25846. }
  25847. default: {
  25848. if (!empty($default)) {
  25849. $op = $this->getPathPaintOperator($default, '');
  25850. } else {
  25851. $op = '';
  25852. }
  25853. }
  25854. }
  25855. return $op;
  25856. }
  25857. /**
  25858. * Enable or disable default option for font subsetting.
  25859. * @param $enable (boolean) if true enable font subsetting by default.
  25860. * @author Nicola Asuni
  25861. * @public
  25862. * @since 5.3.002 (2010-06-07)
  25863. */
  25864. public function setFontSubsetting($enable=true) {
  25865. if ($this->pdfa_mode) {
  25866. $this->font_subsetting = false;
  25867. } else {
  25868. $this->font_subsetting = $enable ? true : false;
  25869. }
  25870. }
  25871. /**
  25872. * Return the default option for font subsetting.
  25873. * @return boolean default font subsetting state.
  25874. * @author Nicola Asuni
  25875. * @public
  25876. * @since 5.3.002 (2010-06-07)
  25877. */
  25878. public function getFontSubsetting() {
  25879. return $this->font_subsetting;
  25880. }
  25881. /**
  25882. * Left trim the input string
  25883. * @param $str (string) string to trim
  25884. * @param $replace (string) string that replace spaces.
  25885. * @return left trimmed string
  25886. * @author Nicola Asuni
  25887. * @public
  25888. * @since 5.8.000 (2010-08-11)
  25889. */
  25890. public function stringLeftTrim($str, $replace='') {
  25891. return preg_replace('/^'.$this->re_space['p'].'+/'.$this->re_space['m'], $replace, $str);
  25892. }
  25893. /**
  25894. * Right trim the input string
  25895. * @param $str (string) string to trim
  25896. * @param $replace (string) string that replace spaces.
  25897. * @return right trimmed string
  25898. * @author Nicola Asuni
  25899. * @public
  25900. * @since 5.8.000 (2010-08-11)
  25901. */
  25902. public function stringRightTrim($str, $replace='') {
  25903. return preg_replace('/'.$this->re_space['p'].'+$/'.$this->re_space['m'], $replace, $str);
  25904. }
  25905. /**
  25906. * Trim the input string
  25907. * @param $str (string) string to trim
  25908. * @param $replace (string) string that replace spaces.
  25909. * @return trimmed string
  25910. * @author Nicola Asuni
  25911. * @public
  25912. * @since 5.8.000 (2010-08-11)
  25913. */
  25914. public function stringTrim($str, $replace='') {
  25915. $str = $this->stringLeftTrim($str, $replace);
  25916. $str = $this->stringRightTrim($str, $replace);
  25917. return $str;
  25918. }
  25919. /**
  25920. * Return true if the current font is unicode type.
  25921. * @return true for unicode font, false otherwise.
  25922. * @author Nicola Asuni
  25923. * @public
  25924. * @since 5.8.002 (2010-08-14)
  25925. */
  25926. public function isUnicodeFont() {
  25927. return (($this->CurrentFont['type'] == 'TrueTypeUnicode') OR ($this->CurrentFont['type'] == 'cidfont0'));
  25928. }
  25929. /**
  25930. * Return normalized font name
  25931. * @param $fontfamily (string) property string containing font family names
  25932. * @return string normalized font name
  25933. * @author Nicola Asuni
  25934. * @public
  25935. * @since 5.8.004 (2010-08-17)
  25936. */
  25937. public function getFontFamilyName($fontfamily) {
  25938. // remove spaces and symbols
  25939. $fontfamily = preg_replace('/[^a-z0-9\,]/', '', strtolower($fontfamily));
  25940. // extract all font names
  25941. $fontslist = preg_split('/[,]/', $fontfamily);
  25942. // find first valid font name
  25943. foreach ($fontslist as $font) {
  25944. // replace font variations
  25945. $font = preg_replace('/italic$/', 'I', $font);
  25946. $font = preg_replace('/oblique$/', 'I', $font);
  25947. $font = preg_replace('/bold([I]?)$/', 'B\\1', $font);
  25948. // replace common family names and core fonts
  25949. $pattern = array();
  25950. $replacement = array();
  25951. $pattern[] = '/^serif|^cursive|^fantasy|^timesnewroman/';
  25952. $replacement[] = 'times';
  25953. $pattern[] = '/^sansserif/';
  25954. $replacement[] = 'helvetica';
  25955. $pattern[] = '/^monospace/';
  25956. $replacement[] = 'courier';
  25957. $font = preg_replace($pattern, $replacement, $font);
  25958. if (in_array(strtolower($font), $this->fontlist) OR in_array($font, $this->fontkeys)) {
  25959. return $font;
  25960. }
  25961. }
  25962. // return current font as default
  25963. return $this->CurrentFont['fontkey'];
  25964. }
  25965. /**
  25966. * Start a new XObject Template.
  25967. * 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).
  25968. * 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.
  25969. * Note: X,Y coordinates will be reset to 0,0.
  25970. * @param $w (int) Template width in user units (empty string or zero = page width less margins).
  25971. * @param $h (int) Template height in user units (empty string or zero = page height less margins).
  25972. * @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).
  25973. * @return int the XObject Template ID in case of success or false in case of error.
  25974. * @author Nicola Asuni
  25975. * @public
  25976. * @since 5.8.017 (2010-08-24)
  25977. * @see endTemplate(), printTemplate()
  25978. */
  25979. public function startTemplate($w=0, $h=0, $group=false) {
  25980. if ($this->inxobj) {
  25981. // we are already inside an XObject template
  25982. return false;
  25983. }
  25984. $this->inxobj = true;
  25985. ++$this->n;
  25986. // XObject ID
  25987. $this->xobjid = 'XT'.$this->n;
  25988. // object ID
  25989. $this->xobjects[$this->xobjid] = array('n' => $this->n);
  25990. // store current graphic state
  25991. $this->xobjects[$this->xobjid]['gvars'] = $this->getGraphicVars();
  25992. // initialize data
  25993. $this->xobjects[$this->xobjid]['intmrk'] = 0;
  25994. $this->xobjects[$this->xobjid]['transfmrk'] = array();
  25995. $this->xobjects[$this->xobjid]['outdata'] = '';
  25996. $this->xobjects[$this->xobjid]['xobjects'] = array();
  25997. $this->xobjects[$this->xobjid]['images'] = array();
  25998. $this->xobjects[$this->xobjid]['fonts'] = array();
  25999. $this->xobjects[$this->xobjid]['annotations'] = array();
  26000. $this->xobjects[$this->xobjid]['extgstates'] = array();
  26001. $this->xobjects[$this->xobjid]['gradients'] = array();
  26002. $this->xobjects[$this->xobjid]['spot_colors'] = array();
  26003. // set new environment
  26004. $this->num_columns = 1;
  26005. $this->current_column = 0;
  26006. $this->SetAutoPageBreak(false);
  26007. if (($w === '') OR ($w <= 0)) {
  26008. $w = $this->w - $this->lMargin - $this->rMargin;
  26009. }
  26010. if (($h === '') OR ($h <= 0)) {
  26011. $h = $this->h - $this->tMargin - $this->bMargin;
  26012. }
  26013. $this->xobjects[$this->xobjid]['x'] = 0;
  26014. $this->xobjects[$this->xobjid]['y'] = 0;
  26015. $this->xobjects[$this->xobjid]['w'] = $w;
  26016. $this->xobjects[$this->xobjid]['h'] = $h;
  26017. $this->w = $w;
  26018. $this->h = $h;
  26019. $this->wPt = $this->w * $this->k;
  26020. $this->hPt = $this->h * $this->k;
  26021. $this->fwPt = $this->wPt;
  26022. $this->fhPt = $this->hPt;
  26023. $this->x = 0;
  26024. $this->y = 0;
  26025. $this->lMargin = 0;
  26026. $this->rMargin = 0;
  26027. $this->tMargin = 0;
  26028. $this->bMargin = 0;
  26029. // set group mode
  26030. $this->xobjects[$this->xobjid]['group'] = $group;
  26031. return $this->xobjid;
  26032. }
  26033. /**
  26034. * End the current XObject Template started with startTemplate() and restore the previous graphic state.
  26035. * 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).
  26036. * 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.
  26037. * @return int the XObject Template ID in case of success or false in case of error.
  26038. * @author Nicola Asuni
  26039. * @public
  26040. * @since 5.8.017 (2010-08-24)
  26041. * @see startTemplate(), printTemplate()
  26042. */
  26043. public function endTemplate() {
  26044. if (!$this->inxobj) {
  26045. // we are not inside a template
  26046. return false;
  26047. }
  26048. $this->inxobj = false;
  26049. // restore previous graphic state
  26050. $this->setGraphicVars($this->xobjects[$this->xobjid]['gvars'], true);
  26051. return $this->xobjid;
  26052. }
  26053. /**
  26054. * Print an XObject Template.
  26055. * You can print an XObject Template inside the currently opened Template.
  26056. * 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).
  26057. * 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.
  26058. * @param $id (string) The ID of XObject Template to print.
  26059. * @param $x (int) X position in user units (empty string = current x position)
  26060. * @param $y (int) Y position in user units (empty string = current y position)
  26061. * @param $w (int) Width in user units (zero = remaining page width)
  26062. * @param $h (int) Height in user units (zero = remaining page height)
  26063. * @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>
  26064. * @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>
  26065. * @param $fitonpage (boolean) If true the template is resized to not exceed page dimensions.
  26066. * @author Nicola Asuni
  26067. * @public
  26068. * @since 5.8.017 (2010-08-24)
  26069. * @see startTemplate(), endTemplate()
  26070. */
  26071. public function printTemplate($id, $x='', $y='', $w=0, $h=0, $align='', $palign='', $fitonpage=false) {
  26072. if (!isset($this->xobjects[$id])) {
  26073. $this->Error('The XObject Template \''.$id.'\' doesn\'t exist!');
  26074. }
  26075. if ($this->inxobj) {
  26076. if ($id == $this->xobjid) {
  26077. // close current template
  26078. $this->endTemplate();
  26079. } else {
  26080. // use the template as resource for the template currently opened
  26081. $this->xobjects[$this->xobjid]['xobjects'][$id] = $this->xobjects[$id];
  26082. }
  26083. }
  26084. // set default values
  26085. if ($x === '') {
  26086. $x = $this->x;
  26087. }
  26088. if ($y === '') {
  26089. $y = $this->y;
  26090. }
  26091. // check page for no-write regions and adapt page margins if necessary
  26092. list($x, $y) = $this->checkPageRegions($h, $x, $y);
  26093. $ow = $this->xobjects[$id]['w'];
  26094. $oh = $this->xobjects[$id]['h'];
  26095. // calculate template width and height on document
  26096. if (($w <= 0) AND ($h <= 0)) {
  26097. $w = $ow;
  26098. $h = $oh;
  26099. } elseif ($w <= 0) {
  26100. $w = $h * $ow / $oh;
  26101. } elseif ($h <= 0) {
  26102. $h = $w * $oh / $ow;
  26103. }
  26104. // fit the template on available space
  26105. list($w, $h, $x, $y) = $this->fitBlock($w, $h, $x, $y, $fitonpage);
  26106. // set page alignment
  26107. $rb_y = $y + $h;
  26108. // set alignment
  26109. if ($this->rtl) {
  26110. if ($palign == 'L') {
  26111. $xt = $this->lMargin;
  26112. } elseif ($palign == 'C') {
  26113. $xt = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
  26114. } elseif ($palign == 'R') {
  26115. $xt = $this->w - $this->rMargin - $w;
  26116. } else {
  26117. $xt = $x - $w;
  26118. }
  26119. $rb_x = $xt;
  26120. } else {
  26121. if ($palign == 'L') {
  26122. $xt = $this->lMargin;
  26123. } elseif ($palign == 'C') {
  26124. $xt = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
  26125. } elseif ($palign == 'R') {
  26126. $xt = $this->w - $this->rMargin - $w;
  26127. } else {
  26128. $xt = $x;
  26129. }
  26130. $rb_x = $xt + $w;
  26131. }
  26132. // print XObject Template + Transformation matrix
  26133. $this->StartTransform();
  26134. // translate and scale
  26135. $sx = ($w / $this->xobjects[$id]['w']);
  26136. $sy = ($h / $this->xobjects[$id]['h']);
  26137. $tm = array();
  26138. $tm[0] = $sx;
  26139. $tm[1] = 0;
  26140. $tm[2] = 0;
  26141. $tm[3] = $sy;
  26142. $tm[4] = $xt * $this->k;
  26143. $tm[5] = ($this->h - $h - $y) * $this->k;
  26144. $this->Transform($tm);
  26145. // set object
  26146. $this->_out('/'.$id.' Do');
  26147. $this->StopTransform();
  26148. // add annotations
  26149. if (!empty($this->xobjects[$id]['annotations'])) {
  26150. foreach ($this->xobjects[$id]['annotations'] as $annot) {
  26151. // transform original coordinates
  26152. $coordlt = $this->getTransformationMatrixProduct($tm, array(1, 0, 0, 1, ($annot['x'] * $this->k), (-$annot['y'] * $this->k)));
  26153. $ax = ($coordlt[4] / $this->k);
  26154. $ay = ($this->h - $h - ($coordlt[5] / $this->k));
  26155. $coordrb = $this->getTransformationMatrixProduct($tm, array(1, 0, 0, 1, (($annot['x'] + $annot['w']) * $this->k), ((-$annot['y'] - $annot['h']) * $this->k)));
  26156. $aw = ($coordrb[4] / $this->k) - $ax;
  26157. $ah = ($this->h - $h - ($coordrb[5] / $this->k)) - $ay;
  26158. $this->Annotation($ax, $ay, $aw, $ah, $annot['text'], $annot['opt'], $annot['spaces']);
  26159. }
  26160. }
  26161. // set pointer to align the next text/objects
  26162. switch($align) {
  26163. case 'T': {
  26164. $this->y = $y;
  26165. $this->x = $rb_x;
  26166. break;
  26167. }
  26168. case 'M': {
  26169. $this->y = $y + round($h/2);
  26170. $this->x = $rb_x;
  26171. break;
  26172. }
  26173. case 'B': {
  26174. $this->y = $rb_y;
  26175. $this->x = $rb_x;
  26176. break;
  26177. }
  26178. case 'N': {
  26179. $this->SetY($rb_y);
  26180. break;
  26181. }
  26182. default:{
  26183. break;
  26184. }
  26185. }
  26186. }
  26187. /**
  26188. * Set the percentage of character stretching.
  26189. * @param $perc (int) percentage of stretching (100 = no stretching)
  26190. * @author Nicola Asuni
  26191. * @public
  26192. * @since 5.9.000 (2010-09-29)
  26193. */
  26194. public function setFontStretching($perc=100) {
  26195. $this->font_stretching = $perc;
  26196. }
  26197. /**
  26198. * Get the percentage of character stretching.
  26199. * @return float stretching value
  26200. * @author Nicola Asuni
  26201. * @public
  26202. * @since 5.9.000 (2010-09-29)
  26203. */
  26204. public function getFontStretching() {
  26205. return $this->font_stretching;
  26206. }
  26207. /**
  26208. * Set the amount to increase or decrease the space between characters in a text.
  26209. * @param $spacing (float) amount to increase or decrease the space between characters in a text (0 = default spacing)
  26210. * @author Nicola Asuni
  26211. * @public
  26212. * @since 5.9.000 (2010-09-29)
  26213. */
  26214. public function setFontSpacing($spacing=0) {
  26215. $this->font_spacing = $spacing;
  26216. }
  26217. /**
  26218. * Get the amount to increase or decrease the space between characters in a text.
  26219. * @return int font spacing (tracking/kerning) value
  26220. * @author Nicola Asuni
  26221. * @public
  26222. * @since 5.9.000 (2010-09-29)
  26223. */
  26224. public function getFontSpacing() {
  26225. return $this->font_spacing;
  26226. }
  26227. /**
  26228. * Return an array of no-write page regions
  26229. * @return array of no-write page regions
  26230. * @author Nicola Asuni
  26231. * @public
  26232. * @since 5.9.003 (2010-10-13)
  26233. * @see setPageRegions(), addPageRegion()
  26234. */
  26235. public function getPageRegions() {
  26236. return $this->page_regions;
  26237. }
  26238. /**
  26239. * Set no-write regions on page.
  26240. * 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.
  26241. * A region is always aligned on the left or right side of the page ad is defined using a vertical segment.
  26242. * You can set multiple regions for the same page.
  26243. * @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.
  26244. * @author Nicola Asuni
  26245. * @public
  26246. * @since 5.9.003 (2010-10-13)
  26247. * @see addPageRegion(), getPageRegions()
  26248. */
  26249. public function setPageRegions($regions=array()) {
  26250. // empty current regions array
  26251. $this->page_regions = array();
  26252. // add regions
  26253. foreach ($regions as $data) {
  26254. $this->addPageRegion($data);
  26255. }
  26256. }
  26257. /**
  26258. * Add a single no-write region on selected page.
  26259. * 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.
  26260. * A region is always aligned on the left or right side of the page ad is defined using a vertical segment.
  26261. * You can set multiple regions for the same page.
  26262. * @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).
  26263. * @author Nicola Asuni
  26264. * @public
  26265. * @since 5.9.003 (2010-10-13)
  26266. * @see setPageRegions(), getPageRegions()
  26267. */
  26268. public function addPageRegion($region) {
  26269. if (!isset($region['page']) OR empty($region['page'])) {
  26270. $region['page'] = $this->page;
  26271. }
  26272. if (isset($region['xt']) AND isset($region['xb']) AND ($region['xt'] > 0) AND ($region['xb'] > 0)
  26273. AND isset($region['yt']) AND isset($region['yb']) AND ($region['yt'] >= 0) AND ($region['yt'] < $region['yb'])
  26274. AND isset($region['side']) AND (($region['side'] == 'L') OR ($region['side'] == 'R'))) {
  26275. $this->page_regions[] = $region;
  26276. }
  26277. }
  26278. /**
  26279. * Remove a single no-write region.
  26280. * @param $key (int) region key
  26281. * @author Nicola Asuni
  26282. * @public
  26283. * @since 5.9.003 (2010-10-13)
  26284. * @see setPageRegions(), getPageRegions()
  26285. */
  26286. public function removePageRegion($key) {
  26287. if (isset($this->page_regions[$key])) {
  26288. unset($this->page_regions[$key]);
  26289. }
  26290. }
  26291. /**
  26292. * Check page for no-write regions and adapt current coordinates and page margins if necessary.
  26293. * 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.
  26294. * A region is always aligned on the left or right side of the page ad is defined using a vertical segment.
  26295. * @param $h (float) height of the text/image/object to print in user units
  26296. * @param $x (float) current X coordinate in user units
  26297. * @param $y (float) current Y coordinate in user units
  26298. * @return array($x, $y)
  26299. * @author Nicola Asuni
  26300. * @protected
  26301. * @since 5.9.003 (2010-10-13)
  26302. */
  26303. protected function checkPageRegions($h, $x, $y) {
  26304. // set default values
  26305. if ($x === '') {
  26306. $x = $this->x;
  26307. }
  26308. if ($y === '') {
  26309. $y = $this->y;
  26310. }
  26311. if (empty($this->page_regions)) {
  26312. // no page regions defined
  26313. return array($x, $y);
  26314. }
  26315. if (empty($h)) {
  26316. $h = ($this->FontSize * $this->cell_height_ratio) + $this->cell_padding['T'] + $this->cell_padding['B'];
  26317. }
  26318. // check for page break
  26319. if ($this->checkPageBreak($h, $y)) {
  26320. // the content will be printed on a new page
  26321. $x = $this->x;
  26322. $y = $this->y;
  26323. }
  26324. if ($this->num_columns > 1) {
  26325. if ($this->rtl) {
  26326. $this->lMargin = $this->columns[$this->current_column]['x'] - $this->columns[$this->current_column]['w'];
  26327. } else {
  26328. $this->rMargin = $this->w - $this->columns[$this->current_column]['x'] - $this->columns[$this->current_column]['w'];
  26329. }
  26330. } else {
  26331. if ($this->rtl) {
  26332. $this->lMargin = $this->original_lMargin;
  26333. } else {
  26334. $this->rMargin = $this->original_rMargin;
  26335. }
  26336. }
  26337. // adjust coordinates and page margins
  26338. foreach ($this->page_regions as $regid => $regdata) {
  26339. if ($regdata['page'] == $this->page) {
  26340. // check region boundaries
  26341. if (($y > ($regdata['yt'] - $h)) AND ($y <= $regdata['yb'])) {
  26342. // Y is inside the region
  26343. $minv = ($regdata['xb'] - $regdata['xt']) / ($regdata['yb'] - $regdata['yt']); // inverse of angular coefficient
  26344. $yt = max($y, $regdata['yt']);
  26345. $yb = min(($yt + $h), $regdata['yb']);
  26346. $xt = (($yt - $regdata['yt']) * $minv) + $regdata['xt'];
  26347. $xb = (($yb - $regdata['yt']) * $minv) + $regdata['xt'];
  26348. if ($regdata['side'] == 'L') { // left side
  26349. $new_margin = max($xt, $xb);
  26350. if ($this->lMargin < $new_margin) {
  26351. if ($this->rtl) {
  26352. // adjust left page margin
  26353. $this->lMargin = $new_margin;
  26354. }
  26355. if ($x < $new_margin) {
  26356. // adjust x position
  26357. $x = $new_margin;
  26358. if ($new_margin > ($this->w - $this->rMargin)) {
  26359. // adjust y position
  26360. $y = $regdata['yb'] - $h;
  26361. }
  26362. }
  26363. }
  26364. } elseif ($regdata['side'] == 'R') { // right side
  26365. $new_margin = min($xt, $xb);
  26366. if (($this->w - $this->rMargin) > $new_margin) {
  26367. if (!$this->rtl) {
  26368. // adjust right page margin
  26369. $this->rMargin = ($this->w - $new_margin);
  26370. }
  26371. if ($x > $new_margin) {
  26372. // adjust x position
  26373. $x = $new_margin;
  26374. if ($new_margin > $this->lMargin) {
  26375. // adjust y position
  26376. $y = $regdata['yb'] - $h;
  26377. }
  26378. }
  26379. }
  26380. }
  26381. }
  26382. }
  26383. }
  26384. return array($x, $y);
  26385. }
  26386. // --- SVG METHODS ---------------------------------------------------------
  26387. /**
  26388. * Embedd a Scalable Vector Graphics (SVG) image.
  26389. * NOTE: SVG standard is not yet fully implemented, use the setRasterizeVectorImages() method to enable/disable rasterization of vector images using ImageMagick library.
  26390. * @param $file (string) Name of the SVG file or a '@' character followed by the SVG data string.
  26391. * @param $x (float) Abscissa of the upper-left corner.
  26392. * @param $y (float) Ordinate of the upper-left corner.
  26393. * @param $w (float) Width of the image in the page. If not specified or equal to zero, it is automatically calculated.
  26394. * @param $h (float) Height of the image in the page. If not specified or equal to zero, it is automatically calculated.
  26395. * @param $link (mixed) URL or identifier returned by AddLink().
  26396. * @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.
  26397. * @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>
  26398. * @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)))
  26399. * @param $fitonpage (boolean) if true the image is resized to not exceed page dimensions.
  26400. * @author Nicola Asuni
  26401. * @since 5.0.000 (2010-05-02)
  26402. * @public
  26403. */
  26404. public function ImageSVG($file, $x='', $y='', $w=0, $h=0, $link='', $align='', $palign='', $border=0, $fitonpage=false) {
  26405. if ($this->rasterize_vector_images AND ($w > 0) AND ($h > 0)) {
  26406. // convert SVG to raster image using GD or ImageMagick libraries
  26407. return $this->Image($file, $x, $y, $w, $h, 'SVG', $link, $align, true, 300, $palign, false, false, $border, false, false, false);
  26408. }
  26409. if ($file{0} === '@') { // image from string
  26410. $this->svgdir = '';
  26411. $svgdata = substr($file, 1);
  26412. } else { // SVG file
  26413. $this->svgdir = dirname($file);
  26414. $svgdata = file_get_contents($file);
  26415. }
  26416. if ($svgdata === false) {
  26417. $this->Error('SVG file not found: '.$file);
  26418. }
  26419. if ($x === '') {
  26420. $x = $this->x;
  26421. }
  26422. if ($y === '') {
  26423. $y = $this->y;
  26424. }
  26425. // check page for no-write regions and adapt page margins if necessary
  26426. list($x, $y) = $this->checkPageRegions($h, $x, $y);
  26427. $k = $this->k;
  26428. $ox = 0;
  26429. $oy = 0;
  26430. $ow = $w;
  26431. $oh = $h;
  26432. $aspect_ratio_align = 'xMidYMid';
  26433. $aspect_ratio_ms = 'meet';
  26434. $regs = array();
  26435. // get original image width and height
  26436. preg_match('/<svg([^\>]*)>/si', $svgdata, $regs);
  26437. if (isset($regs[1]) AND !empty($regs[1])) {
  26438. $tmp = array();
  26439. if (preg_match('/[\s]+x[\s]*=[\s]*"([^"]*)"/si', $regs[1], $tmp)) {
  26440. $ox = $this->getHTMLUnitToUnits($tmp[1], 0, $this->svgunit, false);
  26441. }
  26442. $tmp = array();
  26443. if (preg_match('/[\s]+y[\s]*=[\s]*"([^"]*)"/si', $regs[1], $tmp)) {
  26444. $oy = $this->getHTMLUnitToUnits($tmp[1], 0, $this->svgunit, false);
  26445. }
  26446. $tmp = array();
  26447. if (preg_match('/[\s]+width[\s]*=[\s]*"([^"]*)"/si', $regs[1], $tmp)) {
  26448. $ow = $this->getHTMLUnitToUnits($tmp[1], 1, $this->svgunit, false);
  26449. }
  26450. $tmp = array();
  26451. if (preg_match('/[\s]+height[\s]*=[\s]*"([^"]*)"/si', $regs[1], $tmp)) {
  26452. $oh = $this->getHTMLUnitToUnits($tmp[1], 1, $this->svgunit, false);
  26453. }
  26454. $tmp = array();
  26455. $view_box = array();
  26456. if (preg_match('/[\s]+viewBox[\s]*=[\s]*"[\s]*([0-9\.\-]+)[\s]+([0-9\.\-]+)[\s]+([0-9\.]+)[\s]+([0-9\.]+)[\s]*"/si', $regs[1], $tmp)) {
  26457. if (count($tmp) == 5) {
  26458. array_shift($tmp);
  26459. foreach ($tmp as $key => $val) {
  26460. $view_box[$key] = $this->getHTMLUnitToUnits($val, 0, $this->svgunit, false);
  26461. }
  26462. $ox = $view_box[0];
  26463. $oy = $view_box[1];
  26464. }
  26465. // get aspect ratio
  26466. $tmp = array();
  26467. if (preg_match('/[\s]+preserveAspectRatio[\s]*=[\s]*"([^"]*)"/si', $regs[1], $tmp)) {
  26468. $aspect_ratio = preg_split('/[\s]+/si', $tmp[1]);
  26469. switch (count($aspect_ratio)) {
  26470. case 3: {
  26471. $aspect_ratio_align = $aspect_ratio[1];
  26472. $aspect_ratio_ms = $aspect_ratio[2];
  26473. break;
  26474. }
  26475. case 2: {
  26476. $aspect_ratio_align = $aspect_ratio[0];
  26477. $aspect_ratio_ms = $aspect_ratio[1];
  26478. break;
  26479. }
  26480. case 1: {
  26481. $aspect_ratio_align = $aspect_ratio[0];
  26482. $aspect_ratio_ms = 'meet';
  26483. break;
  26484. }
  26485. }
  26486. }
  26487. }
  26488. }
  26489. // calculate image width and height on document
  26490. if (($w <= 0) AND ($h <= 0)) {
  26491. // convert image size to document unit
  26492. $w = $ow;
  26493. $h = $oh;
  26494. } elseif ($w <= 0) {
  26495. $w = $h * $ow / $oh;
  26496. } elseif ($h <= 0) {
  26497. $h = $w * $oh / $ow;
  26498. }
  26499. // fit the image on available space
  26500. list($w, $h, $x, $y) = $this->fitBlock($w, $h, $x, $y, $fitonpage);
  26501. if ($this->rasterize_vector_images) {
  26502. // convert SVG to raster image using GD or ImageMagick libraries
  26503. return $this->Image($file, $x, $y, $w, $h, 'SVG', $link, $align, true, 300, $palign, false, false, $border, false, false, false);
  26504. }
  26505. // set alignment
  26506. $this->img_rb_y = $y + $h;
  26507. // set alignment
  26508. if ($this->rtl) {
  26509. if ($palign == 'L') {
  26510. $ximg = $this->lMargin;
  26511. } elseif ($palign == 'C') {
  26512. $ximg = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
  26513. } elseif ($palign == 'R') {
  26514. $ximg = $this->w - $this->rMargin - $w;
  26515. } else {
  26516. $ximg = $x - $w;
  26517. }
  26518. $this->img_rb_x = $ximg;
  26519. } else {
  26520. if ($palign == 'L') {
  26521. $ximg = $this->lMargin;
  26522. } elseif ($palign == 'C') {
  26523. $ximg = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
  26524. } elseif ($palign == 'R') {
  26525. $ximg = $this->w - $this->rMargin - $w;
  26526. } else {
  26527. $ximg = $x;
  26528. }
  26529. $this->img_rb_x = $ximg + $w;
  26530. }
  26531. // store current graphic vars
  26532. $gvars = $this->getGraphicVars();
  26533. // store SVG position and scale factors
  26534. $svgoffset_x = ($ximg - $ox) * $this->k;
  26535. $svgoffset_y = -($y - $oy) * $this->k;
  26536. if (isset($view_box[2]) AND ($view_box[2] > 0) AND ($view_box[3] > 0)) {
  26537. $ow = $view_box[2];
  26538. $oh = $view_box[3];
  26539. } else {
  26540. if ($ow <= 0) {
  26541. $ow = $w;
  26542. }
  26543. if ($oh <= 0) {
  26544. $oh = $h;
  26545. }
  26546. }
  26547. $svgscale_x = $w / $ow;
  26548. $svgscale_y = $h / $oh;
  26549. // scaling and alignment
  26550. if ($aspect_ratio_align != 'none') {
  26551. // store current scaling values
  26552. $svgscale_old_x = $svgscale_x;
  26553. $svgscale_old_y = $svgscale_y;
  26554. // force uniform scaling
  26555. if ($aspect_ratio_ms == 'slice') {
  26556. // the entire viewport is covered by the viewBox
  26557. if ($svgscale_x > $svgscale_y) {
  26558. $svgscale_y = $svgscale_x;
  26559. } elseif ($svgscale_x < $svgscale_y) {
  26560. $svgscale_x = $svgscale_y;
  26561. }
  26562. } else { // meet
  26563. // the entire viewBox is visible within the viewport
  26564. if ($svgscale_x < $svgscale_y) {
  26565. $svgscale_y = $svgscale_x;
  26566. } elseif ($svgscale_x > $svgscale_y) {
  26567. $svgscale_x = $svgscale_y;
  26568. }
  26569. }
  26570. // correct X alignment
  26571. switch (substr($aspect_ratio_align, 1, 3)) {
  26572. case 'Min': {
  26573. // do nothing
  26574. break;
  26575. }
  26576. case 'Max': {
  26577. $svgoffset_x += (($w * $this->k) - ($ow * $this->k * $svgscale_x));
  26578. break;
  26579. }
  26580. default:
  26581. case 'Mid': {
  26582. $svgoffset_x += ((($w * $this->k) - ($ow * $this->k * $svgscale_x)) / 2);
  26583. break;
  26584. }
  26585. }
  26586. // correct Y alignment
  26587. switch (substr($aspect_ratio_align, 5)) {
  26588. case 'Min': {
  26589. // do nothing
  26590. break;
  26591. }
  26592. case 'Max': {
  26593. $svgoffset_y -= (($h * $this->k) - ($oh * $this->k * $svgscale_y));
  26594. break;
  26595. }
  26596. default:
  26597. case 'Mid': {
  26598. $svgoffset_y -= ((($h * $this->k) - ($oh * $this->k * $svgscale_y)) / 2);
  26599. break;
  26600. }
  26601. }
  26602. }
  26603. // store current page break mode
  26604. $page_break_mode = $this->AutoPageBreak;
  26605. $page_break_margin = $this->getBreakMargin();
  26606. $cell_padding = $this->cell_padding;
  26607. $this->SetCellPadding(0);
  26608. $this->SetAutoPageBreak(false);
  26609. // save the current graphic state
  26610. $this->_out('q'.$this->epsmarker);
  26611. // set initial clipping mask
  26612. $this->Rect($x, $y, $w, $h, 'CNZ', array(), array());
  26613. // scale and translate
  26614. $e = $ox * $this->k * (1 - $svgscale_x);
  26615. $f = ($this->h - $oy) * $this->k * (1 - $svgscale_y);
  26616. $this->_out(sprintf('%.3F %.3F %.3F %.3F %.3F %.3F cm', $svgscale_x, 0, 0, $svgscale_y, $e + $svgoffset_x, $f + $svgoffset_y));
  26617. // creates a new XML parser to be used by the other XML functions
  26618. $this->parser = xml_parser_create('UTF-8');
  26619. // the following function allows to use parser inside object
  26620. xml_set_object($this->parser, $this);
  26621. // disable case-folding for this XML parser
  26622. xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, 0);
  26623. // sets the element handler functions for the XML parser
  26624. xml_set_element_handler($this->parser, 'startSVGElementHandler', 'endSVGElementHandler');
  26625. // sets the character data handler function for the XML parser
  26626. xml_set_character_data_handler($this->parser, 'segSVGContentHandler');
  26627. // start parsing an XML document
  26628. if (!xml_parse($this->parser, $svgdata)) {
  26629. $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));
  26630. $this->Error($error_message);
  26631. }
  26632. // free this XML parser
  26633. xml_parser_free($this->parser);
  26634. // restore previous graphic state
  26635. $this->_out($this->epsmarker.'Q');
  26636. // restore graphic vars
  26637. $this->setGraphicVars($gvars);
  26638. $this->lasth = $gvars['lasth'];
  26639. if (!empty($border)) {
  26640. $bx = $this->x;
  26641. $by = $this->y;
  26642. $this->x = $ximg;
  26643. if ($this->rtl) {
  26644. $this->x += $w;
  26645. }
  26646. $this->y = $y;
  26647. $this->Cell($w, $h, '', $border, 0, '', 0, '', 0, true);
  26648. $this->x = $bx;
  26649. $this->y = $by;
  26650. }
  26651. if ($link) {
  26652. $this->Link($ximg, $y, $w, $h, $link, 0);
  26653. }
  26654. // set pointer to align the next text/objects
  26655. switch($align) {
  26656. case 'T':{
  26657. $this->y = $y;
  26658. $this->x = $this->img_rb_x;
  26659. break;
  26660. }
  26661. case 'M':{
  26662. $this->y = $y + round($h/2);
  26663. $this->x = $this->img_rb_x;
  26664. break;
  26665. }
  26666. case 'B':{
  26667. $this->y = $this->img_rb_y;
  26668. $this->x = $this->img_rb_x;
  26669. break;
  26670. }
  26671. case 'N':{
  26672. $this->SetY($this->img_rb_y);
  26673. break;
  26674. }
  26675. default:{
  26676. // restore pointer to starting position
  26677. $this->x = $gvars['x'];
  26678. $this->y = $gvars['y'];
  26679. $this->page = $gvars['page'];
  26680. $this->current_column = $gvars['current_column'];
  26681. $this->tMargin = $gvars['tMargin'];
  26682. $this->bMargin = $gvars['bMargin'];
  26683. $this->w = $gvars['w'];
  26684. $this->h = $gvars['h'];
  26685. $this->wPt = $gvars['wPt'];
  26686. $this->hPt = $gvars['hPt'];
  26687. $this->fwPt = $gvars['fwPt'];
  26688. $this->fhPt = $gvars['fhPt'];
  26689. break;
  26690. }
  26691. }
  26692. $this->endlinex = $this->img_rb_x;
  26693. // restore page break
  26694. $this->SetAutoPageBreak($page_break_mode, $page_break_margin);
  26695. $this->cell_padding = $cell_padding;
  26696. }
  26697. /**
  26698. * Get the tranformation matrix from SVG transform attribute
  26699. * @param $attribute (string) transformation
  26700. * @return array of transformations
  26701. * @author Nicola Asuni
  26702. * @since 5.0.000 (2010-05-02)
  26703. * @protected
  26704. */
  26705. protected function getSVGTransformMatrix($attribute) {
  26706. // identity matrix
  26707. $tm = array(1, 0, 0, 1, 0, 0);
  26708. $transform = array();
  26709. if (preg_match_all('/(matrix|translate|scale|rotate|skewX|skewY)[\s]*\(([^\)]+)\)/si', $attribute, $transform, PREG_SET_ORDER) > 0) {
  26710. foreach ($transform as $key => $data) {
  26711. if (!empty($data[2])) {
  26712. $a = 1;
  26713. $b = 0;
  26714. $c = 0;
  26715. $d = 1;
  26716. $e = 0;
  26717. $f = 0;
  26718. $regs = array();
  26719. switch ($data[1]) {
  26720. case 'matrix': {
  26721. 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)) {
  26722. $a = $regs[1];
  26723. $b = $regs[2];
  26724. $c = $regs[3];
  26725. $d = $regs[4];
  26726. $e = $regs[5];
  26727. $f = $regs[6];
  26728. }
  26729. break;
  26730. }
  26731. case 'translate': {
  26732. if (preg_match('/([a-z0-9\-\.]+)[\,\s]+([a-z0-9\-\.]+)/si', $data[2], $regs)) {
  26733. $e = $regs[1];
  26734. $f = $regs[2];
  26735. } elseif (preg_match('/([a-z0-9\-\.]+)/si', $data[2], $regs)) {
  26736. $e = $regs[1];
  26737. }
  26738. break;
  26739. }
  26740. case 'scale': {
  26741. if (preg_match('/([a-z0-9\-\.]+)[\,\s]+([a-z0-9\-\.]+)/si', $data[2], $regs)) {
  26742. $a = $regs[1];
  26743. $d = $regs[2];
  26744. } elseif (preg_match('/([a-z0-9\-\.]+)/si', $data[2], $regs)) {
  26745. $a = $regs[1];
  26746. $d = $a;
  26747. }
  26748. break;
  26749. }
  26750. case 'rotate': {
  26751. if (preg_match('/([0-9\-\.]+)[\,\s]+([a-z0-9\-\.]+)[\,\s]+([a-z0-9\-\.]+)/si', $data[2], $regs)) {
  26752. $ang = deg2rad($regs[1]);
  26753. $x = $regs[2];
  26754. $y = $regs[3];
  26755. $a = cos($ang);
  26756. $b = sin($ang);
  26757. $c = -$b;
  26758. $d = $a;
  26759. $e = ($x * (1 - $a)) - ($y * $c);
  26760. $f = ($y * (1 - $d)) - ($x * $b);
  26761. } elseif (preg_match('/([0-9\-\.]+)/si', $data[2], $regs)) {
  26762. $ang = deg2rad($regs[1]);
  26763. $a = cos($ang);
  26764. $b = sin($ang);
  26765. $c = -$b;
  26766. $d = $a;
  26767. $e = 0;
  26768. $f = 0;
  26769. }
  26770. break;
  26771. }
  26772. case 'skewX': {
  26773. if (preg_match('/([0-9\-\.]+)/si', $data[2], $regs)) {
  26774. $c = tan(deg2rad($regs[1]));
  26775. }
  26776. break;
  26777. }
  26778. case 'skewY': {
  26779. if (preg_match('/([0-9\-\.]+)/si', $data[2], $regs)) {
  26780. $b = tan(deg2rad($regs[1]));
  26781. }
  26782. break;
  26783. }
  26784. }
  26785. $tm = $this->getTransformationMatrixProduct($tm, array($a, $b, $c, $d, $e, $f));
  26786. }
  26787. }
  26788. }
  26789. return $tm;
  26790. }
  26791. /**
  26792. * Get the product of two SVG tranformation matrices
  26793. * @param $ta (array) first SVG tranformation matrix
  26794. * @param $tb (array) second SVG tranformation matrix
  26795. * @return transformation array
  26796. * @author Nicola Asuni
  26797. * @since 5.0.000 (2010-05-02)
  26798. * @protected
  26799. */
  26800. protected function getTransformationMatrixProduct($ta, $tb) {
  26801. $tm = array();
  26802. $tm[0] = ($ta[0] * $tb[0]) + ($ta[2] * $tb[1]);
  26803. $tm[1] = ($ta[1] * $tb[0]) + ($ta[3] * $tb[1]);
  26804. $tm[2] = ($ta[0] * $tb[2]) + ($ta[2] * $tb[3]);
  26805. $tm[3] = ($ta[1] * $tb[2]) + ($ta[3] * $tb[3]);
  26806. $tm[4] = ($ta[0] * $tb[4]) + ($ta[2] * $tb[5]) + $ta[4];
  26807. $tm[5] = ($ta[1] * $tb[4]) + ($ta[3] * $tb[5]) + $ta[5];
  26808. return $tm;
  26809. }
  26810. /**
  26811. * Convert SVG transformation matrix to PDF.
  26812. * @param $tm (array) original SVG transformation matrix
  26813. * @return array transformation matrix
  26814. * @protected
  26815. * @since 5.0.000 (2010-05-02)
  26816. */
  26817. protected function convertSVGtMatrix($tm) {
  26818. $a = $tm[0];
  26819. $b = -$tm[1];
  26820. $c = -$tm[2];
  26821. $d = $tm[3];
  26822. $e = $this->getHTMLUnitToUnits($tm[4], 1, $this->svgunit, false) * $this->k;
  26823. $f = -$this->getHTMLUnitToUnits($tm[5], 1, $this->svgunit, false) * $this->k;
  26824. $x = 0;
  26825. $y = $this->h * $this->k;
  26826. $e = ($x * (1 - $a)) - ($y * $c) + $e;
  26827. $f = ($y * (1 - $d)) - ($x * $b) + $f;
  26828. return array($a, $b, $c, $d, $e, $f);
  26829. }
  26830. /**
  26831. * Apply SVG graphic transformation matrix.
  26832. * @param $tm (array) original SVG transformation matrix
  26833. * @protected
  26834. * @since 5.0.000 (2010-05-02)
  26835. */
  26836. protected function SVGTransform($tm) {
  26837. $this->Transform($this->convertSVGtMatrix($tm));
  26838. }
  26839. /**
  26840. * Apply the requested SVG styles (*** TO BE COMPLETED ***)
  26841. * @param $svgstyle (array) array of SVG styles to apply
  26842. * @param $prevsvgstyle (array) array of previous SVG style
  26843. * @param $x (int) X origin of the bounding box
  26844. * @param $y (int) Y origin of the bounding box
  26845. * @param $w (int) width of the bounding box
  26846. * @param $h (int) height of the bounding box
  26847. * @param $clip_function (string) clip function
  26848. * @param $clip_params (array) array of parameters for clipping function
  26849. * @return object style
  26850. * @author Nicola Asuni
  26851. * @since 5.0.000 (2010-05-02)
  26852. * @protected
  26853. */
  26854. protected function setSVGStyles($svgstyle, $prevsvgstyle, $x=0, $y=0, $w=1, $h=1, $clip_function='', $clip_params=array()) {
  26855. $objstyle = '';
  26856. $minlen = (0.01 / $this->k); // minimum acceptable length (3 point)
  26857. if (!isset($svgstyle['opacity'])) {
  26858. return $objstyle;
  26859. }
  26860. // clip-path
  26861. $regs = array();
  26862. if (preg_match('/url\([\s]*\#([^\)]*)\)/si', $svgstyle['clip-path'], $regs)) {
  26863. $clip_path = $this->svgclippaths[$regs[1]];
  26864. foreach ($clip_path as $cp) {
  26865. $this->startSVGElementHandler('clip-path', $cp['name'], $cp['attribs'], $cp['tm']);
  26866. }
  26867. }
  26868. // opacity
  26869. if ($svgstyle['opacity'] != 1) {
  26870. $this->SetAlpha($svgstyle['opacity']);
  26871. }
  26872. // color
  26873. $fill_color = $this->convertHTMLColorToDec($svgstyle['color']);
  26874. $this->SetFillColorArray($fill_color);
  26875. // text color
  26876. $text_color = $this->convertHTMLColorToDec($svgstyle['text-color']);
  26877. $this->SetTextColorArray($text_color);
  26878. // clip
  26879. if (preg_match('/rect\(([a-z0-9\-\.]*)[\s]*([a-z0-9\-\.]*)[\s]*([a-z0-9\-\.]*)[\s]*([a-z0-9\-\.]*)\)/si', $svgstyle['clip'], $regs)) {
  26880. $top = (isset($regs[1])?$this->getHTMLUnitToUnits($regs[1], 0, $this->svgunit, false):0);
  26881. $right = (isset($regs[2])?$this->getHTMLUnitToUnits($regs[2], 0, $this->svgunit, false):0);
  26882. $bottom = (isset($regs[3])?$this->getHTMLUnitToUnits($regs[3], 0, $this->svgunit, false):0);
  26883. $left = (isset($regs[4])?$this->getHTMLUnitToUnits($regs[4], 0, $this->svgunit, false):0);
  26884. $cx = $x + $left;
  26885. $cy = $y + $top;
  26886. $cw = $w - $left - $right;
  26887. $ch = $h - $top - $bottom;
  26888. if ($svgstyle['clip-rule'] == 'evenodd') {
  26889. $clip_rule = 'CNZ';
  26890. } else {
  26891. $clip_rule = 'CEO';
  26892. }
  26893. $this->Rect($cx, $cy, $cw, $ch, $clip_rule, array(), array());
  26894. }
  26895. // fill
  26896. $regs = array();
  26897. if (preg_match('/url\([\s]*\#([^\)]*)\)/si', $svgstyle['fill'], $regs)) {
  26898. // gradient
  26899. $gradient = $this->svggradients[$regs[1]];
  26900. if (isset($gradient['xref'])) {
  26901. // reference to another gradient definition
  26902. $newgradient = $this->svggradients[$gradient['xref']];
  26903. $newgradient['coords'] = $gradient['coords'];
  26904. $newgradient['mode'] = $gradient['mode'];
  26905. $newgradient['gradientUnits'] = $gradient['gradientUnits'];
  26906. if (isset($gradient['gradientTransform'])) {
  26907. $newgradient['gradientTransform'] = $gradient['gradientTransform'];
  26908. }
  26909. $gradient = $newgradient;
  26910. }
  26911. //save current Graphic State
  26912. $this->_out('q');
  26913. //set clipping area
  26914. if (!empty($clip_function) AND method_exists($this, $clip_function)) {
  26915. $bbox = call_user_func_array(array($this, $clip_function), $clip_params);
  26916. if (is_array($bbox) AND (count($bbox) == 4)) {
  26917. list($x, $y, $w, $h) = $bbox;
  26918. }
  26919. }
  26920. if ($gradient['mode'] == 'measure') {
  26921. if (isset($gradient['gradientTransform']) AND !empty($gradient['gradientTransform'])) {
  26922. $gtm = $gradient['gradientTransform'];
  26923. // apply transformation matrix
  26924. $xa = ($gtm[0] * $gradient['coords'][0]) + ($gtm[2] * $gradient['coords'][1]) + $gtm[4];
  26925. $ya = ($gtm[1] * $gradient['coords'][0]) + ($gtm[3] * $gradient['coords'][1]) + $gtm[5];
  26926. $xb = ($gtm[0] * $gradient['coords'][2]) + ($gtm[2] * $gradient['coords'][3]) + $gtm[4];
  26927. $yb = ($gtm[1] * $gradient['coords'][2]) + ($gtm[3] * $gradient['coords'][3]) + $gtm[5];
  26928. if (isset($gradient['coords'][4])) {
  26929. $gradient['coords'][4] = sqrt(pow(($gtm[0] * $gradient['coords'][4]), 2) + pow(($gtm[1] * $gradient['coords'][4]), 2));
  26930. }
  26931. $gradient['coords'][0] = $xa;
  26932. $gradient['coords'][1] = $ya;
  26933. $gradient['coords'][2] = $xb;
  26934. $gradient['coords'][3] = $yb;
  26935. }
  26936. // convert SVG coordinates to user units
  26937. $gradient['coords'][0] = $this->getHTMLUnitToUnits($gradient['coords'][0], 0, $this->svgunit, false);
  26938. $gradient['coords'][1] = $this->getHTMLUnitToUnits($gradient['coords'][1], 0, $this->svgunit, false);
  26939. $gradient['coords'][2] = $this->getHTMLUnitToUnits($gradient['coords'][2], 0, $this->svgunit, false);
  26940. $gradient['coords'][3] = $this->getHTMLUnitToUnits($gradient['coords'][3], 0, $this->svgunit, false);
  26941. if (isset($gradient['coords'][4])) {
  26942. $gradient['coords'][4] = $this->getHTMLUnitToUnits($gradient['coords'][4], 0, $this->svgunit, false);
  26943. }
  26944. // shift units
  26945. if ($gradient['gradientUnits'] == 'objectBoundingBox') {
  26946. // convert to SVG coordinate system
  26947. $gradient['coords'][0] += $x;
  26948. $gradient['coords'][1] += $y;
  26949. $gradient['coords'][2] += $x;
  26950. $gradient['coords'][3] += $y;
  26951. }
  26952. if ($w <= $minlen) {
  26953. $w = $minlen;
  26954. }
  26955. if ($h <= $minlen) {
  26956. $h = $minlen;
  26957. }
  26958. // calculate percentages
  26959. $gradient['coords'][0] = ($gradient['coords'][0] - $x) / $w;
  26960. $gradient['coords'][1] = ($gradient['coords'][1] - $y) / $h;
  26961. $gradient['coords'][2] = ($gradient['coords'][2] - $x) / $w;
  26962. $gradient['coords'][3] = ($gradient['coords'][3] - $y) / $h;
  26963. if (isset($gradient['coords'][4])) {
  26964. $gradient['coords'][4] /= $w;
  26965. }
  26966. } elseif ($gradient['mode'] == 'percentage') {
  26967. foreach($gradient['coords'] as $key => $val) {
  26968. $gradient['coords'][$key] = (intval($val) / 100);
  26969. }
  26970. }
  26971. // fix values
  26972. foreach($gradient['coords'] as $key => $val) {
  26973. if ($val < 0) {
  26974. $gradient['coords'][$key] = 0;
  26975. } elseif ($val > 1) {
  26976. $gradient['coords'][$key] = 1;
  26977. }
  26978. }
  26979. if (($gradient['type'] == 2) AND ($gradient['coords'][0] == $gradient['coords'][2]) AND ($gradient['coords'][1] == $gradient['coords'][3])) {
  26980. // single color (no shading)
  26981. $gradient['coords'][0] = 1;
  26982. $gradient['coords'][1] = 0;
  26983. $gradient['coords'][2] = 0.999;
  26984. $gradient['coords'][3] = 0;
  26985. }
  26986. // swap Y coordinates
  26987. $tmp = $gradient['coords'][1];
  26988. $gradient['coords'][1] = $gradient['coords'][3];
  26989. $gradient['coords'][3] = $tmp;
  26990. // set transformation map for gradient
  26991. if ($gradient['type'] == 3) {
  26992. // gradient is always circular
  26993. $cy = $this->h - $y - ($gradient['coords'][1] * ($w + $h));
  26994. $this->_out(sprintf('%.3F 0 0 %.3F %.3F %.3F cm', $w*$this->k, $w*$this->k, $x*$this->k, $cy*$this->k));
  26995. } else {
  26996. $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));
  26997. }
  26998. if (count($gradient['stops']) > 1) {
  26999. $this->Gradient($gradient['type'], $gradient['coords'], $gradient['stops'], array(), false);
  27000. }
  27001. } elseif ($svgstyle['fill'] != 'none') {
  27002. $fill_color = $this->convertHTMLColorToDec($svgstyle['fill']);
  27003. if ($svgstyle['fill-opacity'] != 1) {
  27004. $this->SetAlpha($svgstyle['fill-opacity']);
  27005. }
  27006. $this->SetFillColorArray($fill_color);
  27007. if ($svgstyle['fill-rule'] == 'evenodd') {
  27008. $objstyle .= 'F*';
  27009. } else {
  27010. $objstyle .= 'F';
  27011. }
  27012. }
  27013. // stroke
  27014. if ($svgstyle['stroke'] != 'none') {
  27015. $stroke_style = array(
  27016. 'color' => $this->convertHTMLColorToDec($svgstyle['stroke']),
  27017. 'width' => $this->getHTMLUnitToUnits($svgstyle['stroke-width'], 0, $this->svgunit, false),
  27018. 'cap' => $svgstyle['stroke-linecap'],
  27019. 'join' => $svgstyle['stroke-linejoin']
  27020. );
  27021. if (isset($svgstyle['stroke-dasharray']) AND !empty($svgstyle['stroke-dasharray']) AND ($svgstyle['stroke-dasharray'] != 'none')) {
  27022. $stroke_style['dash'] = $svgstyle['stroke-dasharray'];
  27023. }
  27024. $this->SetLineStyle($stroke_style);
  27025. $objstyle .= 'D';
  27026. }
  27027. // font
  27028. $regs = array();
  27029. if (!empty($svgstyle['font'])) {
  27030. if (preg_match('/font-family[\s]*:[\s]*([^\;\"]*)/si', $svgstyle['font'], $regs)) {
  27031. $font_family = $this->getFontFamilyName($regs[1]);
  27032. } else {
  27033. $font_family = $svgstyle['font-family'];
  27034. }
  27035. if (preg_match('/font-size[\s]*:[\s]*([^\s\;\"]*)/si', $svgstyle['font'], $regs)) {
  27036. $font_size = trim($regs[1]);
  27037. } else {
  27038. $font_size = $svgstyle['font-size'];
  27039. }
  27040. if (preg_match('/font-style[\s]*:[\s]*([^\s\;\"]*)/si', $svgstyle['font'], $regs)) {
  27041. $font_style = trim($regs[1]);
  27042. } else {
  27043. $font_style = $svgstyle['font-style'];
  27044. }
  27045. if (preg_match('/font-weight[\s]*:[\s]*([^\s\;\"]*)/si', $svgstyle['font'], $regs)) {
  27046. $font_weight = trim($regs[1]);
  27047. } else {
  27048. $font_weight = $svgstyle['font-weight'];
  27049. }
  27050. if (preg_match('/font-stretch[\s]*:[\s]*([^\s\;\"]*)/si', $svgstyle['font'], $regs)) {
  27051. $font_stretch = trim($regs[1]);
  27052. } else {
  27053. $font_stretch = $svgstyle['font-stretch'];
  27054. }
  27055. if (preg_match('/letter-spacing[\s]*:[\s]*([^\s\;\"]*)/si', $svgstyle['font'], $regs)) {
  27056. $font_spacing = trim($regs[1]);
  27057. } else {
  27058. $font_spacing = $svgstyle['letter-spacing'];
  27059. }
  27060. } else {
  27061. $font_family = $this->getFontFamilyName($svgstyle['font-family']);
  27062. $font_size = $svgstyle['font-size'];
  27063. $font_style = $svgstyle['font-style'];
  27064. $font_weight = $svgstyle['font-weight'];
  27065. $font_stretch = $svgstyle['font-stretch'];
  27066. $font_spacing = $svgstyle['letter-spacing'];
  27067. }
  27068. $font_size = $this->getHTMLUnitToUnits($font_size, $prevsvgstyle['font-size'], $this->svgunit, false) * $this->k;
  27069. $font_stretch = $this->getCSSFontStretching($font_stretch, $svgstyle['font-stretch']);
  27070. $font_spacing = $this->getCSSFontSpacing($font_spacing, $svgstyle['letter-spacing']);
  27071. switch ($font_style) {
  27072. case 'italic': {
  27073. $font_style = 'I';
  27074. break;
  27075. }
  27076. case 'oblique': {
  27077. $font_style = 'I';
  27078. break;
  27079. }
  27080. default:
  27081. case 'normal': {
  27082. $font_style = '';
  27083. break;
  27084. }
  27085. }
  27086. switch ($font_weight) {
  27087. case 'bold':
  27088. case 'bolder': {
  27089. $font_style .= 'B';
  27090. break;
  27091. }
  27092. }
  27093. switch ($svgstyle['text-decoration']) {
  27094. case 'underline': {
  27095. $font_style .= 'U';
  27096. break;
  27097. }
  27098. case 'overline': {
  27099. $font_style .= 'O';
  27100. break;
  27101. }
  27102. case 'line-through': {
  27103. $font_style .= 'D';
  27104. break;
  27105. }
  27106. default:
  27107. case 'none': {
  27108. break;
  27109. }
  27110. }
  27111. $this->SetFont($font_family, $font_style, $font_size);
  27112. $this->setFontStretching($font_stretch);
  27113. $this->setFontSpacing($font_spacing);
  27114. return $objstyle;
  27115. }
  27116. /**
  27117. * Draws an SVG path
  27118. * @param $d (string) attribute d of the path SVG element
  27119. * @param $style (string) Style of rendering. Possible values are:
  27120. * <ul>
  27121. * <li>D or empty string: Draw (default).</li>
  27122. * <li>F: Fill.</li>
  27123. * <li>F*: Fill using the even-odd rule to determine which regions lie inside the clipping path.</li>
  27124. * <li>DF or FD: Draw and fill.</li>
  27125. * <li>DF* or FD*: Draw and fill using the even-odd rule to determine which regions lie inside the clipping path.</li>
  27126. * <li>CNZ: Clipping mode (using the even-odd rule to determine which regions lie inside the clipping path).</li>
  27127. * <li>CEO: Clipping mode (using the nonzero winding number rule to determine which regions lie inside the clipping path).</li>
  27128. * </ul>
  27129. * @return array of container box measures (x, y, w, h)
  27130. * @author Nicola Asuni
  27131. * @since 5.0.000 (2010-05-02)
  27132. * @protected
  27133. */
  27134. protected function SVGPath($d, $style='') {
  27135. // set fill/stroke style
  27136. $op = $this->getPathPaintOperator($style, '');
  27137. if (empty($op)) {
  27138. return;
  27139. }
  27140. $paths = array();
  27141. $d = preg_replace('/([0-9ACHLMQSTVZ])([\-\+])/si', '\\1 \\2', $d);
  27142. preg_match_all('/([ACHLMQSTVZ])[\s]*([^ACHLMQSTVZ\"]*)/si', $d, $paths, PREG_SET_ORDER);
  27143. $x = 0;
  27144. $y = 0;
  27145. $x1 = 0;
  27146. $y1 = 0;
  27147. $x2 = 0;
  27148. $y2 = 0;
  27149. $xmin = 2147483647;
  27150. $xmax = 0;
  27151. $ymin = 2147483647;
  27152. $ymax = 0;
  27153. $relcoord = false;
  27154. $minlen = (0.01 / $this->k); // minimum acceptable length (3 point)
  27155. $firstcmd = true; // used to print first point
  27156. // draw curve pieces
  27157. foreach ($paths as $key => $val) {
  27158. // get curve type
  27159. $cmd = trim($val[1]);
  27160. if (strtolower($cmd) == $cmd) {
  27161. // use relative coordinated instead of absolute
  27162. $relcoord = true;
  27163. $xoffset = $x;
  27164. $yoffset = $y;
  27165. } else {
  27166. $relcoord = false;
  27167. $xoffset = 0;
  27168. $yoffset = 0;
  27169. }
  27170. $params = array();
  27171. if (isset($val[2])) {
  27172. // get curve parameters
  27173. $rawparams = preg_split('/([\,\s]+)/si', trim($val[2]));
  27174. $params = array();
  27175. foreach ($rawparams as $ck => $cp) {
  27176. $params[$ck] = $this->getHTMLUnitToUnits($cp, 0, $this->svgunit, false);
  27177. if (abs($params[$ck]) < $minlen) {
  27178. // aproximate little values to zero
  27179. $params[$ck] = 0;
  27180. }
  27181. }
  27182. }
  27183. // store current origin point
  27184. $x0 = $x;
  27185. $y0 = $y;
  27186. switch (strtoupper($cmd)) {
  27187. case 'M': { // moveto
  27188. foreach ($params as $ck => $cp) {
  27189. if (($ck % 2) == 0) {
  27190. $x = $cp + $xoffset;
  27191. } else {
  27192. $y = $cp + $yoffset;
  27193. if ($firstcmd OR (abs($x0 - $x) >= $minlen) OR (abs($y0 - $y) >= $minlen)) {
  27194. if ($ck == 1) {
  27195. $this->_outPoint($x, $y);
  27196. $firstcmd = false;
  27197. } else {
  27198. $this->_outLine($x, $y);
  27199. }
  27200. }
  27201. $xmin = min($xmin, $x);
  27202. $ymin = min($ymin, $y);
  27203. $xmax = max($xmax, $x);
  27204. $ymax = max($ymax, $y);
  27205. if ($relcoord) {
  27206. $xoffset = $x;
  27207. $yoffset = $y;
  27208. }
  27209. }
  27210. }
  27211. break;
  27212. }
  27213. case 'L': { // lineto
  27214. foreach ($params as $ck => $cp) {
  27215. if (($ck % 2) == 0) {
  27216. $x = $cp + $xoffset;
  27217. } else {
  27218. $y = $cp + $yoffset;
  27219. if ((abs($x0 - $x) >= $minlen) OR (abs($y0 - $y) >= $minlen)) {
  27220. $this->_outLine($x, $y);
  27221. }
  27222. $xmin = min($xmin, $x);
  27223. $ymin = min($ymin, $y);
  27224. $xmax = max($xmax, $x);
  27225. $ymax = max($ymax, $y);
  27226. if ($relcoord) {
  27227. $xoffset = $x;
  27228. $yoffset = $y;
  27229. }
  27230. }
  27231. }
  27232. break;
  27233. }
  27234. case 'H': { // horizontal lineto
  27235. foreach ($params as $ck => $cp) {
  27236. $x = $cp + $xoffset;
  27237. if ((abs($x0 - $x) >= $minlen) OR (abs($y0 - $y) >= $minlen)) {
  27238. $this->_outLine($x, $y);
  27239. }
  27240. $xmin = min($xmin, $x);
  27241. $xmax = max($xmax, $x);
  27242. if ($relcoord) {
  27243. $xoffset = $x;
  27244. }
  27245. }
  27246. break;
  27247. }
  27248. case 'V': { // vertical lineto
  27249. foreach ($params as $ck => $cp) {
  27250. $y = $cp + $yoffset;
  27251. if ((abs($x0 - $x) >= $minlen) OR (abs($y0 - $y) >= $minlen)) {
  27252. $this->_outLine($x, $y);
  27253. }
  27254. $ymin = min($ymin, $y);
  27255. $ymax = max($ymax, $y);
  27256. if ($relcoord) {
  27257. $yoffset = $y;
  27258. }
  27259. }
  27260. break;
  27261. }
  27262. case 'C': { // curveto
  27263. foreach ($params as $ck => $cp) {
  27264. $params[$ck] = $cp;
  27265. if ((($ck + 1) % 6) == 0) {
  27266. $x1 = $params[($ck - 5)] + $xoffset;
  27267. $y1 = $params[($ck - 4)] + $yoffset;
  27268. $x2 = $params[($ck - 3)] + $xoffset;
  27269. $y2 = $params[($ck - 2)] + $yoffset;
  27270. $x = $params[($ck - 1)] + $xoffset;
  27271. $y = $params[($ck)] + $yoffset;
  27272. $this->_outCurve($x1, $y1, $x2, $y2, $x, $y);
  27273. $xmin = min($xmin, $x, $x1, $x2);
  27274. $ymin = min($ymin, $y, $y1, $y2);
  27275. $xmax = max($xmax, $x, $x1, $x2);
  27276. $ymax = max($ymax, $y, $y1, $y2);
  27277. if ($relcoord) {
  27278. $xoffset = $x;
  27279. $yoffset = $y;
  27280. }
  27281. }
  27282. }
  27283. break;
  27284. }
  27285. case 'S': { // shorthand/smooth curveto
  27286. foreach ($params as $ck => $cp) {
  27287. $params[$ck] = $cp;
  27288. if ((($ck + 1) % 4) == 0) {
  27289. if (($key > 0) AND ((strtoupper($paths[($key - 1)][1]) == 'C') OR (strtoupper($paths[($key - 1)][1]) == 'S'))) {
  27290. $x1 = (2 * $x) - $x2;
  27291. $y1 = (2 * $y) - $y2;
  27292. } else {
  27293. $x1 = $x;
  27294. $y1 = $y;
  27295. }
  27296. $x2 = $params[($ck - 3)] + $xoffset;
  27297. $y2 = $params[($ck - 2)] + $yoffset;
  27298. $x = $params[($ck - 1)] + $xoffset;
  27299. $y = $params[($ck)] + $yoffset;
  27300. $this->_outCurve($x1, $y1, $x2, $y2, $x, $y);
  27301. $xmin = min($xmin, $x, $x1, $x2);
  27302. $ymin = min($ymin, $y, $y1, $y2);
  27303. $xmax = max($xmax, $x, $x1, $x2);
  27304. $ymax = max($ymax, $y, $y1, $y2);
  27305. if ($relcoord) {
  27306. $xoffset = $x;
  27307. $yoffset = $y;
  27308. }
  27309. }
  27310. }
  27311. break;
  27312. }
  27313. case 'Q': { // quadratic Bézier curveto
  27314. foreach ($params as $ck => $cp) {
  27315. $params[$ck] = $cp;
  27316. if ((($ck + 1) % 4) == 0) {
  27317. // convert quadratic points to cubic points
  27318. $x1 = $params[($ck - 3)] + $xoffset;
  27319. $y1 = $params[($ck - 2)] + $yoffset;
  27320. $xa = ($x + (2 * $x1)) / 3;
  27321. $ya = ($y + (2 * $y1)) / 3;
  27322. $x = $params[($ck - 1)] + $xoffset;
  27323. $y = $params[($ck)] + $yoffset;
  27324. $xb = ($x + (2 * $x1)) / 3;
  27325. $yb = ($y + (2 * $y1)) / 3;
  27326. $this->_outCurve($xa, $ya, $xb, $yb, $x, $y);
  27327. $xmin = min($xmin, $x, $xa, $xb);
  27328. $ymin = min($ymin, $y, $ya, $yb);
  27329. $xmax = max($xmax, $x, $xa, $xb);
  27330. $ymax = max($ymax, $y, $ya, $yb);
  27331. if ($relcoord) {
  27332. $xoffset = $x;
  27333. $yoffset = $y;
  27334. }
  27335. }
  27336. }
  27337. break;
  27338. }
  27339. case 'T': { // shorthand/smooth quadratic Bézier curveto
  27340. foreach ($params as $ck => $cp) {
  27341. $params[$ck] = $cp;
  27342. if (($ck % 2) != 0) {
  27343. if (($key > 0) AND ((strtoupper($paths[($key - 1)][1]) == 'Q') OR (strtoupper($paths[($key - 1)][1]) == 'T'))) {
  27344. $x1 = (2 * $x) - $x1;
  27345. $y1 = (2 * $y) - $y1;
  27346. } else {
  27347. $x1 = $x;
  27348. $y1 = $y;
  27349. }
  27350. // convert quadratic points to cubic points
  27351. $xa = ($x + (2 * $x1)) / 3;
  27352. $ya = ($y + (2 * $y1)) / 3;
  27353. $x = $params[($ck - 1)] + $xoffset;
  27354. $y = $params[($ck)] + $yoffset;
  27355. $xb = ($x + (2 * $x1)) / 3;
  27356. $yb = ($y + (2 * $y1)) / 3;
  27357. $this->_outCurve($xa, $ya, $xb, $yb, $x, $y);
  27358. $xmin = min($xmin, $x, $xa, $xb);
  27359. $ymin = min($ymin, $y, $ya, $yb);
  27360. $xmax = max($xmax, $x, $xa, $xb);
  27361. $ymax = max($ymax, $y, $ya, $yb);
  27362. if ($relcoord) {
  27363. $xoffset = $x;
  27364. $yoffset = $y;
  27365. }
  27366. }
  27367. }
  27368. break;
  27369. }
  27370. case 'A': { // elliptical arc
  27371. foreach ($params as $ck => $cp) {
  27372. $params[$ck] = $cp;
  27373. if ((($ck + 1) % 7) == 0) {
  27374. $x0 = $x;
  27375. $y0 = $y;
  27376. $rx = abs($params[($ck - 6)]);
  27377. $ry = abs($params[($ck - 5)]);
  27378. $ang = -$rawparams[($ck - 4)];
  27379. $angle = deg2rad($ang);
  27380. $fa = $rawparams[($ck - 3)]; // large-arc-flag
  27381. $fs = $rawparams[($ck - 2)]; // sweep-flag
  27382. $x = $params[($ck - 1)] + $xoffset;
  27383. $y = $params[$ck] + $yoffset;
  27384. if ((abs($x0 - $x) < $minlen) AND (abs($y0 - $y) < $minlen)) {
  27385. // endpoints are almost identical
  27386. $xmin = min($xmin, $x);
  27387. $ymin = min($ymin, $y);
  27388. $xmax = max($xmax, $x);
  27389. $ymax = max($ymax, $y);
  27390. } else {
  27391. $cos_ang = cos($angle);
  27392. $sin_ang = sin($angle);
  27393. $a = (($x0 - $x) / 2);
  27394. $b = (($y0 - $y) / 2);
  27395. $xa = ($a * $cos_ang) - ($b * $sin_ang);
  27396. $ya = ($a * $sin_ang) + ($b * $cos_ang);
  27397. $rx2 = $rx * $rx;
  27398. $ry2 = $ry * $ry;
  27399. $xa2 = $xa * $xa;
  27400. $ya2 = $ya * $ya;
  27401. $delta = ($xa2 / $rx2) + ($ya2 / $ry2);
  27402. if ($delta > 1) {
  27403. $rx *= sqrt($delta);
  27404. $ry *= sqrt($delta);
  27405. $rx2 = $rx * $rx;
  27406. $ry2 = $ry * $ry;
  27407. }
  27408. $numerator = (($rx2 * $ry2) - ($rx2 * $ya2) - ($ry2 * $xa2));
  27409. if ($numerator < 0) {
  27410. $root = 0;
  27411. } else {
  27412. $root = sqrt($numerator / (($rx2 * $ya2) + ($ry2 * $xa2)));
  27413. }
  27414. if ($fa == $fs){
  27415. $root *= -1;
  27416. }
  27417. $cax = $root * (($rx * $ya) / $ry);
  27418. $cay = -$root * (($ry * $xa) / $rx);
  27419. // coordinates of ellipse center
  27420. $cx = ($cax * $cos_ang) - ($cay * $sin_ang) + (($x0 + $x) / 2);
  27421. $cy = ($cax * $sin_ang) + ($cay * $cos_ang) + (($y0 + $y) / 2);
  27422. // get angles
  27423. $angs = $this->getVectorsAngle(1, 0, (($xa - $cax) / $rx), (($cay - $ya) / $ry));
  27424. $dang = $this->getVectorsAngle((($xa - $cax) / $rx), (($ya - $cay) / $ry), ((-$xa - $cax) / $rx), ((-$ya - $cay) / $ry));
  27425. if (($fs == 0) AND ($dang > 0)) {
  27426. $dang -= (2 * M_PI);
  27427. } elseif (($fs == 1) AND ($dang < 0)) {
  27428. $dang += (2 * M_PI);
  27429. }
  27430. $angf = $angs - $dang;
  27431. if ((($fs == 0) AND ($angs > $angf)) OR (($fs == 1) AND ($angs < $angf))) {
  27432. // reverse angles
  27433. $tmp = $angs;
  27434. $angs = $angf;
  27435. $angf = $tmp;
  27436. }
  27437. $angs = round(rad2deg($angs), 6);
  27438. $angf = round(rad2deg($angf), 6);
  27439. // covent angles to positive values
  27440. if (($angs < 0) AND ($angf < 0)) {
  27441. $angs += 360;
  27442. $angf += 360;
  27443. }
  27444. $pie = false;
  27445. if (($key == 0) AND (isset($paths[($key + 1)][1])) AND (trim($paths[($key + 1)][1]) == 'z')) {
  27446. $pie = true;
  27447. }
  27448. list($axmin, $aymin, $axmax, $aymax) = $this->_outellipticalarc($cx, $cy, $rx, $ry, $ang, $angs, $angf, $pie, 2, false, ($fs == 0), true);
  27449. $xmin = min($xmin, $x, $axmin);
  27450. $ymin = min($ymin, $y, $aymin);
  27451. $xmax = max($xmax, $x, $axmax);
  27452. $ymax = max($ymax, $y, $aymax);
  27453. }
  27454. if ($relcoord) {
  27455. $xoffset = $x;
  27456. $yoffset = $y;
  27457. }
  27458. }
  27459. }
  27460. break;
  27461. }
  27462. case 'Z': {
  27463. $this->_out('h');
  27464. break;
  27465. }
  27466. }
  27467. $firstcmd = false;
  27468. } // end foreach
  27469. if (!empty($op)) {
  27470. $this->_out($op);
  27471. }
  27472. return array($xmin, $ymin, ($xmax - $xmin), ($ymax - $ymin));
  27473. }
  27474. /**
  27475. * Returns the angle in radiants between two vectors
  27476. * @param $x1 (int) X coordinate of first vector point
  27477. * @param $y1 (int) Y coordinate of first vector point
  27478. * @param $x2 (int) X coordinate of second vector point
  27479. * @param $y2 (int) Y coordinate of second vector point
  27480. * @author Nicola Asuni
  27481. * @since 5.0.000 (2010-05-04)
  27482. * @protected
  27483. */
  27484. protected function getVectorsAngle($x1, $y1, $x2, $y2) {
  27485. $dprod = ($x1 * $x2) + ($y1 * $y2);
  27486. $dist1 = sqrt(($x1 * $x1) + ($y1 * $y1));
  27487. $dist2 = sqrt(($x2 * $x2) + ($y2 * $y2));
  27488. $angle = acos($dprod / ($dist1 * $dist2));
  27489. if (is_nan($angle)) {
  27490. $angle = M_PI;
  27491. }
  27492. if ((($x1 * $y2) - ($x2 * $y1)) < 0) {
  27493. $angle *= -1;
  27494. }
  27495. return $angle;
  27496. }
  27497. /**
  27498. * Sets the opening SVG element handler function for the XML parser. (*** TO BE COMPLETED ***)
  27499. * @param $parser (resource) The first parameter, parser, is a reference to the XML parser calling the handler.
  27500. * @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.
  27501. * @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.
  27502. * @param $ctm (array) tranformation matrix for clipping mode (starting transformation matrix).
  27503. * @author Nicola Asuni
  27504. * @since 5.0.000 (2010-05-02)
  27505. * @protected
  27506. */
  27507. protected function startSVGElementHandler($parser, $name, $attribs, $ctm=array()) {
  27508. // check if we are in clip mode
  27509. if ($this->svgclipmode) {
  27510. $this->svgclippaths[$this->svgclipid][] = array('name' => $name, 'attribs' => $attribs, 'tm' => $this->svgcliptm[$this->svgclipid]);
  27511. return;
  27512. }
  27513. if ($this->svgdefsmode AND !in_array($name, array('clipPath', 'linearGradient', 'radialGradient', 'stop'))) {
  27514. if (!isset($attribs['id'])) {
  27515. $attribs['id'] = 'DF_'.(count($this->svgdefs) + 1);
  27516. }
  27517. $this->svgdefs[$attribs['id']] = array('name' => $name, 'attribs' => $attribs);
  27518. return;
  27519. }
  27520. $clipping = false;
  27521. if ($parser == 'clip-path') {
  27522. // set clipping mode
  27523. $clipping = true;
  27524. }
  27525. // get styling properties
  27526. $prev_svgstyle = $this->svgstyles[(count($this->svgstyles) - 1)]; // previous style
  27527. $svgstyle = $this->svgstyles[0]; // set default style
  27528. if (isset($attribs['style']) AND !$this->empty_string($attribs['style'])) {
  27529. // fix style for regular expression
  27530. $attribs['style'] = ';'.$attribs['style'];
  27531. }
  27532. foreach ($prev_svgstyle as $key => $val) {
  27533. if (in_array($key, $this->svginheritprop)) {
  27534. // inherit previous value
  27535. $svgstyle[$key] = $val;
  27536. }
  27537. if (isset($attribs[$key]) AND !$this->empty_string($attribs[$key])) {
  27538. // specific attribute settings
  27539. if ($attribs[$key] == 'inherit') {
  27540. $svgstyle[$key] = $val;
  27541. } else {
  27542. $svgstyle[$key] = $attribs[$key];
  27543. }
  27544. } elseif (isset($attribs['style']) AND !$this->empty_string($attribs['style'])) {
  27545. // CSS style syntax
  27546. $attrval = array();
  27547. if (preg_match('/[;\"\s]{1}'.$key.'[\s]*:[\s]*([^;\"]*)/si', $attribs['style'], $attrval) AND isset($attrval[1])) {
  27548. if ($attrval[1] == 'inherit') {
  27549. $svgstyle[$key] = $val;
  27550. } else {
  27551. $svgstyle[$key] = $attrval[1];
  27552. }
  27553. }
  27554. }
  27555. }
  27556. // transformation matrix
  27557. if (!empty($ctm)) {
  27558. $tm = $ctm;
  27559. } else {
  27560. $tm = $this->svgstyles[(count($this->svgstyles) - 1)]['transfmatrix'];
  27561. }
  27562. if (isset($attribs['transform']) AND !empty($attribs['transform'])) {
  27563. $tm = $this->getTransformationMatrixProduct($tm, $this->getSVGTransformMatrix($attribs['transform']));
  27564. }
  27565. $svgstyle['transfmatrix'] = $tm;
  27566. $invisible = false;
  27567. if (($svgstyle['visibility'] == 'hidden') OR ($svgstyle['visibility'] == 'collapse') OR ($svgstyle['display'] == 'none')) {
  27568. // the current graphics element is invisible (nothing is painted)
  27569. $invisible = true;
  27570. }
  27571. // process tag
  27572. switch($name) {
  27573. case 'defs': {
  27574. $this->svgdefsmode = true;
  27575. break;
  27576. }
  27577. // clipPath
  27578. case 'clipPath': {
  27579. if ($invisible) {
  27580. break;
  27581. }
  27582. $this->svgclipmode = true;
  27583. if (!isset($attribs['id'])) {
  27584. $attribs['id'] = 'CP_'.(count($this->svgcliptm) + 1);
  27585. }
  27586. $this->svgclipid = $attribs['id'];
  27587. $this->svgclippaths[$this->svgclipid] = array();
  27588. $this->svgcliptm[$this->svgclipid] = $tm;
  27589. break;
  27590. }
  27591. case 'svg': {
  27592. // start of SVG object
  27593. break;
  27594. }
  27595. case 'g': {
  27596. // group together related graphics elements
  27597. array_push($this->svgstyles, $svgstyle);
  27598. $this->StartTransform();
  27599. $this->setSVGStyles($svgstyle, $prev_svgstyle);
  27600. break;
  27601. }
  27602. case 'linearGradient': {
  27603. if ($this->pdfa_mode) {
  27604. break;
  27605. }
  27606. if (!isset($attribs['id'])) {
  27607. $attribs['id'] = 'GR_'.(count($this->svggradients) + 1);
  27608. }
  27609. $this->svggradientid = $attribs['id'];
  27610. $this->svggradients[$this->svggradientid] = array();
  27611. $this->svggradients[$this->svggradientid]['type'] = 2;
  27612. $this->svggradients[$this->svggradientid]['stops'] = array();
  27613. if (isset($attribs['gradientUnits'])) {
  27614. $this->svggradients[$this->svggradientid]['gradientUnits'] = $attribs['gradientUnits'];
  27615. } else {
  27616. $this->svggradients[$this->svggradientid]['gradientUnits'] = 'objectBoundingBox';
  27617. }
  27618. //$attribs['spreadMethod']
  27619. $x1 = (isset($attribs['x1'])?$attribs['x1']:'0%');
  27620. $y1 = (isset($attribs['y1'])?$attribs['y1']:'0%');
  27621. $x2 = (isset($attribs['x2'])?$attribs['x2']:'100%');
  27622. $y2 = (isset($attribs['y2'])?$attribs['y2']:'0%');
  27623. if (substr($x1, -1) != '%') {
  27624. $this->svggradients[$this->svggradientid]['mode'] = 'measure';
  27625. } else {
  27626. $this->svggradients[$this->svggradientid]['mode'] = 'percentage';
  27627. }
  27628. if (isset($attribs['gradientTransform'])) {
  27629. $this->svggradients[$this->svggradientid]['gradientTransform'] = $this->getSVGTransformMatrix($attribs['gradientTransform']);
  27630. }
  27631. $this->svggradients[$this->svggradientid]['coords'] = array($x1, $y1, $x2, $y2);
  27632. if (isset($attribs['xlink:href']) AND !empty($attribs['xlink:href'])) {
  27633. // gradient is defined on another place
  27634. $this->svggradients[$this->svggradientid]['xref'] = substr($attribs['xlink:href'], 1);
  27635. }
  27636. break;
  27637. }
  27638. case 'radialGradient': {
  27639. if ($this->pdfa_mode) {
  27640. break;
  27641. }
  27642. if (!isset($attribs['id'])) {
  27643. $attribs['id'] = 'GR_'.(count($this->svggradients) + 1);
  27644. }
  27645. $this->svggradientid = $attribs['id'];
  27646. $this->svggradients[$this->svggradientid] = array();
  27647. $this->svggradients[$this->svggradientid]['type'] = 3;
  27648. $this->svggradients[$this->svggradientid]['stops'] = array();
  27649. if (isset($attribs['gradientUnits'])) {
  27650. $this->svggradients[$this->svggradientid]['gradientUnits'] = $attribs['gradientUnits'];
  27651. } else {
  27652. $this->svggradients[$this->svggradientid]['gradientUnits'] = 'objectBoundingBox';
  27653. }
  27654. //$attribs['spreadMethod']
  27655. $cx = (isset($attribs['cx'])?$attribs['cx']:0.5);
  27656. $cy = (isset($attribs['cy'])?$attribs['cy']:0.5);
  27657. $fx = (isset($attribs['fx'])?$attribs['fx']:$cx);
  27658. $fy = (isset($attribs['fy'])?$attribs['fy']:$cy);
  27659. $r = (isset($attribs['r'])?$attribs['r']:0.5);
  27660. if (isset($attribs['cx']) AND (substr($attribs['cx'], -1) != '%')) {
  27661. $this->svggradients[$this->svggradientid]['mode'] = 'measure';
  27662. } else {
  27663. $this->svggradients[$this->svggradientid]['mode'] = 'percentage';
  27664. }
  27665. if (isset($attribs['gradientTransform'])) {
  27666. $this->svggradients[$this->svggradientid]['gradientTransform'] = $this->getSVGTransformMatrix($attribs['gradientTransform']);
  27667. }
  27668. $this->svggradients[$this->svggradientid]['coords'] = array($cx, $cy, $fx, $fy, $r);
  27669. if (isset($attribs['xlink:href']) AND !empty($attribs['xlink:href'])) {
  27670. // gradient is defined on another place
  27671. $this->svggradients[$this->svggradientid]['xref'] = substr($attribs['xlink:href'], 1);
  27672. }
  27673. break;
  27674. }
  27675. case 'stop': {
  27676. // gradient stops
  27677. if (substr($attribs['offset'], -1) == '%') {
  27678. $offset = floatval(substr($attribs['offset'], -1)) / 100;
  27679. } else {
  27680. $offset = floatval($attribs['offset']);
  27681. if ($offset > 1) {
  27682. $offset /= 100;
  27683. }
  27684. }
  27685. $stop_color = isset($svgstyle['stop-color'])?$this->convertHTMLColorToDec($svgstyle['stop-color']):'black';
  27686. $opacity = isset($svgstyle['stop-opacity'])?$svgstyle['stop-opacity']:1;
  27687. $this->svggradients[$this->svggradientid]['stops'][] = array('offset' => $offset, 'color' => $stop_color, 'opacity' => $opacity);
  27688. break;
  27689. }
  27690. // paths
  27691. case 'path': {
  27692. if ($invisible) {
  27693. break;
  27694. }
  27695. if (isset($attribs['d'])) {
  27696. $d = trim($attribs['d']);
  27697. if (!empty($d)) {
  27698. if ($clipping) {
  27699. $this->SVGTransform($tm);
  27700. $this->SVGPath($d, 'CNZ');
  27701. } else {
  27702. $this->StartTransform();
  27703. $this->SVGTransform($tm);
  27704. $obstyle = $this->setSVGStyles($svgstyle, $prev_svgstyle, 0, 0, 1, 1, 'SVGPath', array($d, 'CNZ'));
  27705. if (!empty($obstyle)) {
  27706. $this->SVGPath($d, $obstyle);
  27707. }
  27708. $this->StopTransform();
  27709. }
  27710. }
  27711. }
  27712. break;
  27713. }
  27714. // shapes
  27715. case 'rect': {
  27716. if ($invisible) {
  27717. break;
  27718. }
  27719. $x = (isset($attribs['x'])?$this->getHTMLUnitToUnits($attribs['x'], 0, $this->svgunit, false):0);
  27720. $y = (isset($attribs['y'])?$this->getHTMLUnitToUnits($attribs['y'], 0, $this->svgunit, false):0);
  27721. $w = (isset($attribs['width'])?$this->getHTMLUnitToUnits($attribs['width'], 0, $this->svgunit, false):0);
  27722. $h = (isset($attribs['height'])?$this->getHTMLUnitToUnits($attribs['height'], 0, $this->svgunit, false):0);
  27723. $rx = (isset($attribs['rx'])?$this->getHTMLUnitToUnits($attribs['rx'], 0, $this->svgunit, false):0);
  27724. $ry = (isset($attribs['ry'])?$this->getHTMLUnitToUnits($attribs['ry'], 0, $this->svgunit, false):$rx);
  27725. if ($clipping) {
  27726. $this->SVGTransform($tm);
  27727. $this->RoundedRectXY($x, $y, $w, $h, $rx, $ry, '1111', 'CNZ', array(), array());
  27728. } else {
  27729. $this->StartTransform();
  27730. $this->SVGTransform($tm);
  27731. $obstyle = $this->setSVGStyles($svgstyle, $prev_svgstyle, $x, $y, $w, $h, 'RoundedRectXY', array($x, $y, $w, $h, $rx, $ry, '1111', 'CNZ'));
  27732. if (!empty($obstyle)) {
  27733. $this->RoundedRectXY($x, $y, $w, $h, $rx, $ry, '1111', $obstyle, array(), array());
  27734. }
  27735. $this->StopTransform();
  27736. }
  27737. break;
  27738. }
  27739. case 'circle': {
  27740. if ($invisible) {
  27741. break;
  27742. }
  27743. $cx = (isset($attribs['cx'])?$this->getHTMLUnitToUnits($attribs['cx'], 0, $this->svgunit, false):0);
  27744. $cy = (isset($attribs['cy'])?$this->getHTMLUnitToUnits($attribs['cy'], 0, $this->svgunit, false):0);
  27745. $r = (isset($attribs['r'])?$this->getHTMLUnitToUnits($attribs['r'], 0, $this->svgunit, false):0);
  27746. $x = $cx - $r;
  27747. $y = $cy - $r;
  27748. $w = 2 * $r;
  27749. $h = $w;
  27750. if ($clipping) {
  27751. $this->SVGTransform($tm);
  27752. $this->Circle($cx, $cy, $r, 0, 360, 'CNZ', array(), array(), 8);
  27753. } else {
  27754. $this->StartTransform();
  27755. $this->SVGTransform($tm);
  27756. $obstyle = $this->setSVGStyles($svgstyle, $prev_svgstyle, $x, $y, $w, $h, 'Circle', array($cx, $cy, $r, 0, 360, 'CNZ'));
  27757. if (!empty($obstyle)) {
  27758. $this->Circle($cx, $cy, $r, 0, 360, $obstyle, array(), array(), 8);
  27759. }
  27760. $this->StopTransform();
  27761. }
  27762. break;
  27763. }
  27764. case 'ellipse': {
  27765. if ($invisible) {
  27766. break;
  27767. }
  27768. $cx = (isset($attribs['cx'])?$this->getHTMLUnitToUnits($attribs['cx'], 0, $this->svgunit, false):0);
  27769. $cy = (isset($attribs['cy'])?$this->getHTMLUnitToUnits($attribs['cy'], 0, $this->svgunit, false):0);
  27770. $rx = (isset($attribs['rx'])?$this->getHTMLUnitToUnits($attribs['rx'], 0, $this->svgunit, false):0);
  27771. $ry = (isset($attribs['ry'])?$this->getHTMLUnitToUnits($attribs['ry'], 0, $this->svgunit, false):0);
  27772. $x = $cx - $rx;
  27773. $y = $cy - $ry;
  27774. $w = 2 * $rx;
  27775. $h = 2 * $ry;
  27776. if ($clipping) {
  27777. $this->SVGTransform($tm);
  27778. $this->Ellipse($cx, $cy, $rx, $ry, 0, 0, 360, 'CNZ', array(), array(), 8);
  27779. } else {
  27780. $this->StartTransform();
  27781. $this->SVGTransform($tm);
  27782. $obstyle = $this->setSVGStyles($svgstyle, $prev_svgstyle, $x, $y, $w, $h, 'Ellipse', array($cx, $cy, $rx, $ry, 0, 0, 360, 'CNZ'));
  27783. if (!empty($obstyle)) {
  27784. $this->Ellipse($cx, $cy, $rx, $ry, 0, 0, 360, $obstyle, array(), array(), 8);
  27785. }
  27786. $this->StopTransform();
  27787. }
  27788. break;
  27789. }
  27790. case 'line': {
  27791. if ($invisible) {
  27792. break;
  27793. }
  27794. $x1 = (isset($attribs['x1'])?$this->getHTMLUnitToUnits($attribs['x1'], 0, $this->svgunit, false):0);
  27795. $y1 = (isset($attribs['y1'])?$this->getHTMLUnitToUnits($attribs['y1'], 0, $this->svgunit, false):0);
  27796. $x2 = (isset($attribs['x2'])?$this->getHTMLUnitToUnits($attribs['x2'], 0, $this->svgunit, false):0);
  27797. $y2 = (isset($attribs['y2'])?$this->getHTMLUnitToUnits($attribs['y2'], 0, $this->svgunit, false):0);
  27798. $x = $x1;
  27799. $y = $y1;
  27800. $w = abs($x2 - $x1);
  27801. $h = abs($y2 - $y1);
  27802. if (!$clipping) {
  27803. $this->StartTransform();
  27804. $this->SVGTransform($tm);
  27805. $obstyle = $this->setSVGStyles($svgstyle, $prev_svgstyle, $x, $y, $w, $h, 'Line', array($x1, $y1, $x2, $y2));
  27806. $this->Line($x1, $y1, $x2, $y2);
  27807. $this->StopTransform();
  27808. }
  27809. break;
  27810. }
  27811. case 'polyline':
  27812. case 'polygon': {
  27813. if ($invisible) {
  27814. break;
  27815. }
  27816. $points = (isset($attribs['points'])?$attribs['points']:'0 0');
  27817. $points = trim($points);
  27818. // note that point may use a complex syntax not covered here
  27819. $points = preg_split('/[\,\s]+/si', $points);
  27820. if (count($points) < 4) {
  27821. break;
  27822. }
  27823. $p = array();
  27824. $xmin = 2147483647;
  27825. $xmax = 0;
  27826. $ymin = 2147483647;
  27827. $ymax = 0;
  27828. foreach ($points as $key => $val) {
  27829. $p[$key] = $this->getHTMLUnitToUnits($val, 0, $this->svgunit, false);
  27830. if (($key % 2) == 0) {
  27831. // X coordinate
  27832. $xmin = min($xmin, $p[$key]);
  27833. $xmax = max($xmax, $p[$key]);
  27834. } else {
  27835. // Y coordinate
  27836. $ymin = min($ymin, $p[$key]);
  27837. $ymax = max($ymax, $p[$key]);
  27838. }
  27839. }
  27840. $x = $xmin;
  27841. $y = $ymin;
  27842. $w = ($xmax - $xmin);
  27843. $h = ($ymax - $ymin);
  27844. if ($name == 'polyline') {
  27845. $this->StartTransform();
  27846. $this->SVGTransform($tm);
  27847. $obstyle = $this->setSVGStyles($svgstyle, $prev_svgstyle, $x, $y, $w, $h, 'PolyLine', array($p, 'CNZ'));
  27848. $this->PolyLine($p, 'D', array(), array());
  27849. $this->StopTransform();
  27850. } else { // polygon
  27851. if ($clipping) {
  27852. $this->SVGTransform($tm);
  27853. $this->Polygon($p, 'CNZ', array(), array(), true);
  27854. } else {
  27855. $this->StartTransform();
  27856. $this->SVGTransform($tm);
  27857. $obstyle = $this->setSVGStyles($svgstyle, $prev_svgstyle, $x, $y, $w, $h, 'Polygon', array($p, 'CNZ'));
  27858. if (!empty($obstyle)) {
  27859. $this->Polygon($p, $obstyle, array(), array(), true);
  27860. }
  27861. $this->StopTransform();
  27862. }
  27863. }
  27864. break;
  27865. }
  27866. // image
  27867. case 'image': {
  27868. if ($invisible) {
  27869. break;
  27870. }
  27871. if (!isset($attribs['xlink:href']) OR empty($attribs['xlink:href'])) {
  27872. break;
  27873. }
  27874. $x = (isset($attribs['x'])?$this->getHTMLUnitToUnits($attribs['x'], 0, $this->svgunit, false):0);
  27875. $y = (isset($attribs['y'])?$this->getHTMLUnitToUnits($attribs['y'], 0, $this->svgunit, false):0);
  27876. $w = (isset($attribs['width'])?$this->getHTMLUnitToUnits($attribs['width'], 0, $this->svgunit, false):0);
  27877. $h = (isset($attribs['height'])?$this->getHTMLUnitToUnits($attribs['height'], 0, $this->svgunit, false):0);
  27878. $img = $attribs['xlink:href'];
  27879. if (!$clipping) {
  27880. $this->StartTransform();
  27881. $this->SVGTransform($tm);
  27882. $obstyle = $this->setSVGStyles($svgstyle, $prev_svgstyle, $x, $y, $w, $h);
  27883. if (preg_match('/^data:image\/[^;]+;base64,/', $img, $m) > 0) {
  27884. // embedded image encoded as base64
  27885. $img = '@'.base64_decode(substr($img, strlen($m[0])));
  27886. } else {
  27887. // fix image path
  27888. if (!$this->empty_string($this->svgdir) AND (($img{0} == '.') OR (basename($img) == $img))) {
  27889. // replace relative path with full server path
  27890. $img = $this->svgdir.'/'.$img;
  27891. }
  27892. if (($img[0] == '/') AND !empty($_SERVER['DOCUMENT_ROOT']) AND ($_SERVER['DOCUMENT_ROOT'] != '/')) {
  27893. $findroot = strpos($img, $_SERVER['DOCUMENT_ROOT']);
  27894. if (($findroot === false) OR ($findroot > 1)) {
  27895. if (substr($_SERVER['DOCUMENT_ROOT'], -1) == '/') {
  27896. $img = substr($_SERVER['DOCUMENT_ROOT'], 0, -1).$img;
  27897. } else {
  27898. $img = $_SERVER['DOCUMENT_ROOT'].$img;
  27899. }
  27900. }
  27901. }
  27902. $img = urldecode($img);
  27903. $testscrtype = @parse_url($img);
  27904. if (!isset($testscrtype['query']) OR empty($testscrtype['query'])) {
  27905. // convert URL to server path
  27906. $img = str_replace(K_PATH_URL, K_PATH_MAIN, $img);
  27907. }
  27908. }
  27909. $this->Image($img, $x, $y, $w, $h);
  27910. $this->StopTransform();
  27911. }
  27912. break;
  27913. }
  27914. // text
  27915. case 'text':
  27916. case 'tspan': {
  27917. $this->svgtextmode['invisible'] = $invisible;
  27918. if ($invisible) {
  27919. break;
  27920. }
  27921. array_push($this->svgstyles, $svgstyle);
  27922. // only basic support - advanced features must be implemented
  27923. $x = (isset($attribs['x'])?$this->getHTMLUnitToUnits($attribs['x'], 0, $this->svgunit, false):$this->x);
  27924. $y = (isset($attribs['y'])?$this->getHTMLUnitToUnits($attribs['y'], 0, $this->svgunit, false):$this->y);
  27925. $svgstyle['text-color'] = $svgstyle['fill'];
  27926. $this->svgtext = '';
  27927. if (isset($svgstyle['text-anchor'])) {
  27928. $this->svgtextmode['text-anchor'] = $svgstyle['text-anchor'];
  27929. } else {
  27930. $this->svgtextmode['text-anchor'] = 'start';
  27931. }
  27932. if (isset($svgstyle['direction'])) {
  27933. if ($svgstyle['direction'] == 'rtl') {
  27934. $this->svgtextmode['rtl'] = true;
  27935. } else {
  27936. $this->svgtextmode['rtl'] = false;
  27937. }
  27938. } else {
  27939. $this->svgtextmode['rtl'] = false;
  27940. }
  27941. if (isset($svgstyle['stroke']) AND ($svgstyle['stroke'] != 'none') AND isset($svgstyle['stroke-width']) AND ($svgstyle['stroke-width'] > 0)) {
  27942. $this->svgtextmode['stroke'] = $this->getHTMLUnitToUnits($svgstyle['stroke-width'], 0, $this->svgunit, false);
  27943. } else {
  27944. $this->svgtextmode['stroke'] = false;
  27945. }
  27946. $this->StartTransform();
  27947. $this->SVGTransform($tm);
  27948. $obstyle = $this->setSVGStyles($svgstyle, $prev_svgstyle, $x, $y, 1, 1);
  27949. $this->x = $x;
  27950. $this->y = $y;
  27951. break;
  27952. }
  27953. // use
  27954. case 'use': {
  27955. if (isset($attribs['xlink:href'])) {
  27956. $use = $this->svgdefs[substr($attribs['xlink:href'], 1)];
  27957. if (isset($attribs['xlink:href'])) {
  27958. unset($attribs['xlink:href']);
  27959. }
  27960. if (isset($attribs['id'])) {
  27961. unset($attribs['id']);
  27962. }
  27963. $attribs = array_merge($use['attribs'], $attribs);
  27964. $this->startSVGElementHandler($parser, $use['name'], $use['attribs']);
  27965. }
  27966. break;
  27967. }
  27968. default: {
  27969. break;
  27970. }
  27971. } // end of switch
  27972. }
  27973. /**
  27974. * Sets the closing SVG element handler function for the XML parser.
  27975. * @param $parser (resource) The first parameter, parser, is a reference to the XML parser calling the handler.
  27976. * @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.
  27977. * @author Nicola Asuni
  27978. * @since 5.0.000 (2010-05-02)
  27979. * @protected
  27980. */
  27981. protected function endSVGElementHandler($parser, $name) {
  27982. switch($name) {
  27983. case 'defs': {
  27984. $this->svgdefsmode = false;
  27985. break;
  27986. }
  27987. // clipPath
  27988. case 'clipPath': {
  27989. $this->svgclipmode = false;
  27990. break;
  27991. }
  27992. case 'g': {
  27993. // ungroup: remove last style from array
  27994. array_pop($this->svgstyles);
  27995. $this->StopTransform();
  27996. break;
  27997. }
  27998. case 'text':
  27999. case 'tspan': {
  28000. if ($this->svgtextmode['invisible']) {
  28001. // This implementation must be fixed to following the rule:
  28002. // 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.
  28003. break;
  28004. }
  28005. // print text
  28006. $text = $this->stringTrim($this->svgtext);
  28007. if ($this->svgtextmode['text-anchor'] != 'start') {
  28008. $textlen = $this->GetStringWidth($text);
  28009. // check if string is RTL text
  28010. if ($this->svgtextmode['text-anchor'] == 'end') {
  28011. if ($this->svgtextmode['rtl']) {
  28012. $this->x += $textlen;
  28013. } else {
  28014. $this->x -= $textlen;
  28015. }
  28016. } elseif ($this->svgtextmode['text-anchor'] == 'middle') {
  28017. if ($this->svgtextmode['rtl']) {
  28018. $this->x += ($textlen / 2);
  28019. } else {
  28020. $this->x -= ($textlen / 2);
  28021. }
  28022. }
  28023. }
  28024. $textrendermode = $this->textrendermode;
  28025. $textstrokewidth = $this->textstrokewidth;
  28026. $this->setTextRenderingMode($this->svgtextmode['stroke'], true, false);
  28027. $this->Cell(0, 0, $text, 0, 0, '', false, '', 0, false, 'L', 'T');
  28028. // restore previous rendering mode
  28029. $this->textrendermode = $textrendermode;
  28030. $this->textstrokewidth = $textstrokewidth;
  28031. $this->svgtext = '';
  28032. $this->StopTransform();
  28033. array_pop($this->svgstyles);
  28034. break;
  28035. }
  28036. default: {
  28037. break;
  28038. }
  28039. }
  28040. }
  28041. /**
  28042. * Sets the character data handler function for the XML parser.
  28043. * @param $parser (resource) The first parameter, parser, is a reference to the XML parser calling the handler.
  28044. * @param $data (string) The second parameter, data, contains the character data as a string.
  28045. * @author Nicola Asuni
  28046. * @since 5.0.000 (2010-05-02)
  28047. * @protected
  28048. */
  28049. protected function segSVGContentHandler($parser, $data) {
  28050. $this->svgtext .= $data;
  28051. }
  28052. // --- END SVG METHODS -----------------------------------------------------
  28053. } // END OF TCPDF CLASS
  28054. //============================================================+
  28055. // END OF FILE
  28056. //============================================================+