PageRenderTime 49ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/phppdf/class.pdf.php

https://github.com/timschofield/2.8
PHP | 3101 lines | 2195 code | 169 blank | 737 comment | 395 complexity | 089bf8e23af093132e8c162926ff2b49 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause, GPL-2.0

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * Cpdf
  4. *
  5. * http://pdf-php.sourceforge.net/
  6. * http://www.ros.co.nz/pdf
  7. *
  8. * A PHP class to provide the basic functionality to create a pdf document without
  9. * any requirement for additional modules.
  10. *
  11. * Note that they companion class CezPdf can be used to extend this class and dramatically
  12. * simplify the creation of documents.
  13. *
  14. * IMPORTANT NOTE
  15. * there is no warranty, implied or otherwise with this software.
  16. *
  17. * LICENCE
  18. * This code has been placed in the Public Domain for all to enjoy.
  19. *
  20. * @author Wayne Munro <pdf@ros.co.nz>
  21. * @version 010a
  22. * @package Cpdf
  23. */
  24. class Cpdf {
  25. /**
  26. * the current number of pdf objects in the document
  27. */
  28. var $numObj=0;
  29. /**
  30. * this array contains all of the pdf objects, ready for final assembly
  31. */
  32. var $objects = array();
  33. /**
  34. * the objectId (number within the objects array) of the document catalog
  35. */
  36. var $catalogId;
  37. /**
  38. * array carrying information about the fonts that the system currently knows about
  39. * used to ensure that a font is not loaded twice, among other things
  40. */
  41. var $fonts=array();
  42. /**
  43. * a record of the current font
  44. */
  45. var $currentFont='';
  46. /**
  47. * the current base font
  48. */
  49. var $currentBaseFont='';
  50. /**
  51. * the number of the current font within the font array
  52. */
  53. var $currentFontNum=0;
  54. /**
  55. *
  56. */
  57. var $currentNode;
  58. /**
  59. * object number of the current page
  60. */
  61. var $currentPage;
  62. /**
  63. * object number of the currently active contents block
  64. */
  65. var $currentContents;
  66. /**
  67. * number of fonts within the system
  68. */
  69. var $numFonts=0;
  70. /**
  71. * current colour for fill operations, defaults to inactive value, all three components should be between 0 and 1 inclusive when active
  72. */
  73. var $currentColour=array('r'=>-1,'g'=>-1,'b'=>-1);
  74. /**
  75. * current colour for stroke operations (lines etc.)
  76. */
  77. var $currentStrokeColour=array('r'=>-1,'g'=>-1,'b'=>-1);
  78. /**
  79. * current style that lines are drawn in
  80. */
  81. var $currentLineStyle='';
  82. /**
  83. * an array which is used to save the state of the document, mainly the colours and styles
  84. * it is used to temporarily change to another state, the change back to what it was before
  85. */
  86. var $stateStack = array();
  87. /**
  88. * number of elements within the state stack
  89. */
  90. var $nStateStack = 0;
  91. /**
  92. * number of page objects within the document
  93. */
  94. var $numPages=0;
  95. /**
  96. * object Id storage stack
  97. */
  98. var $stack=array();
  99. /**
  100. * number of elements within the object Id storage stack
  101. */
  102. var $nStack=0;
  103. /**
  104. * an array which contains information about the objects which are not firmly attached to pages
  105. * these have been added with the addObject function
  106. */
  107. var $looseObjects=array();
  108. /**
  109. * array contains infomation about how the loose objects are to be added to the document
  110. */
  111. var $addLooseObjects=array();
  112. /**
  113. * the objectId of the information object for the document
  114. * this contains authorship, title etc.
  115. */
  116. var $infoObject=0;
  117. /**
  118. * number of images being tracked within the document
  119. */
  120. var $numImages=0;
  121. /**
  122. * an array containing options about the document
  123. * it defaults to turning on the compression of the objects
  124. */
  125. var $options=array('compression'=>1);
  126. /**
  127. * the objectId of the first page of the document
  128. */
  129. var $firstPageId;
  130. /**
  131. * used to track the last used value of the inter-word spacing, this is so that it is known
  132. * when the spacing is changed.
  133. */
  134. var $wordSpaceAdjust=0;
  135. /**
  136. * the object Id of the procset object
  137. */
  138. var $procsetObjectId;
  139. /**
  140. * store the information about the relationship between font families
  141. * this used so that the code knows which font is the bold version of another font, etc.
  142. * the value of this array is initialised in the constuctor function.
  143. */
  144. var $fontFamilies = array();
  145. /**
  146. * track if the current font is bolded or italicised
  147. */
  148. var $currentTextState = '';
  149. /**
  150. * messages are stored here during processing, these can be selected afterwards to give some useful debug information
  151. */
  152. var $messages='';
  153. /**
  154. * the ancryption array for the document encryption is stored here
  155. */
  156. var $arc4='';
  157. /**
  158. * the object Id of the encryption information
  159. */
  160. var $arc4_objnum=0;
  161. /**
  162. * the file identifier, used to uniquely identify a pdf document
  163. */
  164. var $fileIdentifier='';
  165. /**
  166. * a flag to say if a document is to be encrypted or not
  167. */
  168. var $encrypted=0;
  169. /**
  170. * the ancryption key for the encryption of all the document content (structure is not encrypted)
  171. */
  172. var $encryptionKey='';
  173. /**
  174. * array which forms a stack to keep track of nested callback functions
  175. */
  176. var $callback = array();
  177. /**
  178. * the number of callback functions in the callback array
  179. */
  180. var $nCallback = 0;
  181. /**
  182. * store label->id pairs for named destinations, these will be used to replace internal links
  183. * done this way so that destinations can be defined after the location that links to them
  184. */
  185. var $destinations = array();
  186. /**
  187. * store the stack for the transaction commands, each item in here is a record of the values of all the
  188. * variables within the class, so that the user can rollback at will (from each 'start' command)
  189. * note that this includes the objects array, so these can be large.
  190. */
  191. var $checkpoint = '';
  192. /**
  193. * class constructor
  194. * this will start a new document
  195. * @var array array of 4 numbers, defining the bottom left and upper right corner of the page. first two are normally zero.
  196. */
  197. function Cpdf ($pageSize=array(0,0,612,792)){
  198. $this->newDocument($pageSize);
  199. // also initialize the font families that are known about already
  200. $this->setFontFamily('init');
  201. // $this->fileIdentifier = md5('xxxxxxxx'.time());
  202. }
  203. /**
  204. * Document object methods (internal use only)
  205. *
  206. * There is about one object method for each type of object in the pdf document
  207. * Each function has the same call list ($id,$action,$options).
  208. * $id = the object ID of the object, or what it is to be if it is being created
  209. * $action = a string specifying the action to be performed, though ALL must support:
  210. * 'new' - create the object with the id $id
  211. * 'out' - produce the output for the pdf object
  212. * $options = optional, a string or array containing the various parameters for the object
  213. *
  214. * These, in conjunction with the output function are the ONLY way for output to be produced
  215. * within the pdf 'file'.
  216. */
  217. /**
  218. *destination object, used to specify the location for the user to jump to, presently on opening
  219. */
  220. function o_destination($id,$action,$options=''){
  221. if ($action!='new'){
  222. $o =& $this->objects[$id];
  223. }
  224. switch($action){
  225. case 'new':
  226. $this->objects[$id]=array('t'=>'destination','info'=>array());
  227. $tmp = '';
  228. switch ($options['type']){
  229. case 'XYZ':
  230. case 'FitR':
  231. $tmp = ' '.$options['p3'].$tmp;
  232. case 'FitH':
  233. case 'FitV':
  234. case 'FitBH':
  235. case 'FitBV':
  236. $tmp = ' '.$options['p1'].' '.$options['p2'].$tmp;
  237. case 'Fit':
  238. case 'FitB':
  239. $tmp = $options['type'].$tmp;
  240. $this->objects[$id]['info']['string']=$tmp;
  241. $this->objects[$id]['info']['page']=$options['page'];
  242. }
  243. break;
  244. case 'out':
  245. $tmp = $o['info'];
  246. $res="\n".$id." 0 obj\n".'['.$tmp['page'].' 0 R /'.$tmp['string']."]\nendobj\n";
  247. return $res;
  248. break;
  249. }
  250. }
  251. /**
  252. * set the viewer preferences
  253. */
  254. function o_viewerPreferences($id,$action,$options=''){
  255. if ($action!='new'){
  256. $o =& $this->objects[$id];
  257. }
  258. switch ($action){
  259. case 'new':
  260. $this->objects[$id]=array('t'=>'viewerPreferences','info'=>array());
  261. break;
  262. case 'add':
  263. foreach($options as $k=>$v){
  264. switch ($k){
  265. case 'HideToolbar':
  266. case 'HideMenubar':
  267. case 'HideWindowUI':
  268. case 'FitWindow':
  269. case 'CenterWindow':
  270. case 'NonFullScreenPageMode':
  271. case 'Direction':
  272. $o['info'][$k]=$v;
  273. break;
  274. }
  275. }
  276. break;
  277. case 'out':
  278. $res="\n".$id." 0 obj\n".'<< ';
  279. foreach($o['info'] as $k=>$v){
  280. $res.="\n/".$k.' '.$v;
  281. }
  282. $res.="\n>>\n";
  283. return $res;
  284. break;
  285. }
  286. }
  287. /**
  288. * define the document catalog, the overall controller for the document
  289. */
  290. function o_catalog($id,$action,$options=''){
  291. if ($action!='new'){
  292. $o =& $this->objects[$id];
  293. }
  294. switch ($action){
  295. case 'new':
  296. $this->objects[$id]=array('t'=>'catalog','info'=>array());
  297. $this->catalogId=$id;
  298. break;
  299. case 'outlines':
  300. case 'pages':
  301. case 'openHere':
  302. $o['info'][$action]=$options;
  303. break;
  304. case 'viewerPreferences':
  305. if (!isset($o['info']['viewerPreferences'])){
  306. $this->numObj++;
  307. $this->o_viewerPreferences($this->numObj,'new');
  308. $o['info']['viewerPreferences']=$this->numObj;
  309. }
  310. $vp = $o['info']['viewerPreferences'];
  311. $this->o_viewerPreferences($vp,'add',$options);
  312. break;
  313. case 'out':
  314. $res="\n".$id." 0 obj\n".'<< /Type /Catalog';
  315. foreach($o['info'] as $k=>$v){
  316. switch($k){
  317. case 'outlines':
  318. $res.="\n".'/Outlines '.$v.' 0 R';
  319. break;
  320. case 'pages':
  321. $res.="\n".'/Pages '.$v.' 0 R';
  322. break;
  323. case 'viewerPreferences':
  324. $res.="\n".'/ViewerPreferences '.$o['info']['viewerPreferences'].' 0 R';
  325. break;
  326. case 'openHere':
  327. $res.="\n".'/OpenAction '.$o['info']['openHere'].' 0 R';
  328. break;
  329. }
  330. }
  331. $res.=" >>\nendobj";
  332. return $res;
  333. break;
  334. }
  335. }
  336. /**
  337. * object which is a parent to the pages in the document
  338. */
  339. function o_pages($id,$action,$options=''){
  340. if ($action!='new'){
  341. $o =& $this->objects[$id];
  342. }
  343. switch ($action){
  344. case 'new':
  345. $this->objects[$id]=array('t'=>'pages','info'=>array());
  346. $this->o_catalog($this->catalogId,'pages',$id);
  347. break;
  348. case 'page':
  349. if (!is_array($options)){
  350. // then it will just be the id of the new page
  351. $o['info']['pages'][]=$options;
  352. } else {
  353. // then it should be an array having 'id','rid','pos', where rid=the page to which this one will be placed relative
  354. // and pos is either 'before' or 'after', saying where this page will fit.
  355. if (isset($options['id']) && isset($options['rid']) && isset($options['pos'])){
  356. $i = array_search($options['rid'],$o['info']['pages']);
  357. if (isset($o['info']['pages'][$i]) && $o['info']['pages'][$i]==$options['rid']){
  358. // then there is a match
  359. // make a space
  360. switch ($options['pos']){
  361. case 'before':
  362. $k = $i;
  363. break;
  364. case 'after':
  365. $k=$i+1;
  366. break;
  367. default:
  368. $k=-1;
  369. break;
  370. }
  371. if ($k>=0){
  372. for ($j=count($o['info']['pages'])-1;$j>=$k;$j--){
  373. $o['info']['pages'][$j+1]=$o['info']['pages'][$j];
  374. }
  375. $o['info']['pages'][$k]=$options['id'];
  376. }
  377. }
  378. }
  379. }
  380. break;
  381. case 'procset':
  382. $o['info']['procset']=$options;
  383. break;
  384. case 'mediaBox':
  385. $o['info']['mediaBox']=$options; // which should be an array of 4 numbers
  386. break;
  387. case 'font':
  388. $o['info']['fonts'][]=array('objNum'=>$options['objNum'],'fontNum'=>$options['fontNum']);
  389. break;
  390. case 'xObject':
  391. $o['info']['xObjects'][]=array('objNum'=>$options['objNum'],'label'=>$options['label']);
  392. break;
  393. case 'out':
  394. if (count($o['info']['pages'])){
  395. $res="\n".$id." 0 obj\n<< /Type /Pages\n/Kids [";
  396. foreach($o['info']['pages'] as $k=>$v){
  397. $res.=$v." 0 R\n";
  398. }
  399. $res.="]\n/Count ".count($this->objects[$id]['info']['pages']);
  400. if ((isset($o['info']['fonts']) && count($o['info']['fonts'])) || isset($o['info']['procset'])){
  401. $res.="\n/Resources <<";
  402. if (isset($o['info']['procset'])){
  403. $res.="\n/ProcSet ".$o['info']['procset']." 0 R";
  404. }
  405. if (isset($o['info']['fonts']) && count($o['info']['fonts'])){
  406. $res.="\n/Font << ";
  407. foreach($o['info']['fonts'] as $finfo){
  408. $res.="\n/F".$finfo['fontNum']." ".$finfo['objNum']." 0 R";
  409. }
  410. $res.=" >>";
  411. }
  412. if (isset($o['info']['xObjects']) && count($o['info']['xObjects'])){
  413. $res.="\n/XObject << ";
  414. foreach($o['info']['xObjects'] as $finfo){
  415. $res.="\n/".$finfo['label']." ".$finfo['objNum']." 0 R";
  416. }
  417. $res.=" >>";
  418. }
  419. $res.="\n>>";
  420. if (isset($o['info']['mediaBox'])){
  421. $tmp=$o['info']['mediaBox'];
  422. $res.="\n/MediaBox [".$tmp[0].' '.$tmp[1].' '.$tmp[2].' '.$tmp[3].']';
  423. }
  424. }
  425. $res.="\n >>\nendobj";
  426. } else {
  427. $res="\n".$id." 0 obj\n<< /Type /Pages\n/Count 0\n>>\nendobj";
  428. }
  429. return $res;
  430. break;
  431. }
  432. }
  433. /**
  434. * Beta Redirection function
  435. */
  436. function o_redirect($id,$action,$options=''){
  437. switch($action){
  438. case 'new':
  439. $this->objects[$id]=array('t'=>'redirect','data'=>$options['data'],'info'=>array());
  440. $this->o_pages($this->currentNode,'xObject',array('label'=>$options['label'],'objNum'=>$id));
  441. break;
  442. case 'out':
  443. $o =& $this->objects[$id];
  444. $tmp=$o['data'];
  445. $res= "\n".$id." 0 obj\n<<";
  446. $res.="/R".$o['data']." ".$o['data']." 0 R>>\nendobj\n";
  447. return $res;
  448. break;
  449. }
  450. }
  451. /**
  452. * define the outlines in the doc, empty for now
  453. */
  454. function o_outlines($id,$action,$options=''){
  455. if ($action!='new'){
  456. $o =& $this->objects[$id];
  457. }
  458. switch ($action){
  459. case 'new':
  460. $this->objects[$id]=array('t'=>'outlines','info'=>array('outlines'=>array()));
  461. $this->o_catalog($this->catalogId,'outlines',$id);
  462. break;
  463. case 'outline':
  464. $o['info']['outlines'][]=$options;
  465. break;
  466. case 'out':
  467. if (count($o['info']['outlines'])){
  468. $res="\n".$id." 0 obj\n<< /Type /Outlines /Kids [";
  469. foreach($o['info']['outlines'] as $k=>$v){
  470. $res.=$v." 0 R ";
  471. }
  472. $res.="] /Count ".count($o['info']['outlines'])." >>\nendobj";
  473. } else {
  474. $res="\n".$id." 0 obj\n<< /Type /Outlines /Count 0 >>\nendobj";
  475. }
  476. return $res;
  477. break;
  478. }
  479. }
  480. /**
  481. * an object to hold the font description
  482. */
  483. function o_font($id,$action,$options=''){
  484. if ($action!='new'){
  485. $o =& $this->objects[$id];
  486. }
  487. switch ($action){
  488. case 'new':
  489. $this->objects[$id]=array('t'=>'font','info'=>array('name'=>$options['name'],'SubType'=>'Type1'));
  490. $fontNum=$this->numFonts;
  491. $this->objects[$id]['info']['fontNum']=$fontNum;
  492. // deal with the encoding and the differences
  493. if (isset($options['differences'])){
  494. // then we'll need an encoding dictionary
  495. $this->numObj++;
  496. $this->o_fontEncoding($this->numObj,'new',$options);
  497. $this->objects[$id]['info']['encodingDictionary']=$this->numObj;
  498. } else if (isset($options['encoding'])){
  499. // we can specify encoding here
  500. switch($options['encoding']){
  501. case 'WinAnsiEncoding':
  502. case 'MacRomanEncoding':
  503. case 'MacExpertEncoding':
  504. $this->objects[$id]['info']['encoding']=$options['encoding'];
  505. break;
  506. case 'none':
  507. break;
  508. default:
  509. $this->objects[$id]['info']['encoding']='WinAnsiEncoding';
  510. break;
  511. }
  512. } else {
  513. $this->objects[$id]['info']['encoding']='WinAnsiEncoding';
  514. }
  515. // also tell the pages node about the new font
  516. $this->o_pages($this->currentNode,'font',array('fontNum'=>$fontNum,'objNum'=>$id));
  517. break;
  518. case 'add':
  519. foreach ($options as $k=>$v){
  520. switch ($k){
  521. case 'BaseFont':
  522. $o['info']['name'] = $v;
  523. break;
  524. case 'FirstChar':
  525. case 'LastChar':
  526. case 'Widths':
  527. case 'FontDescriptor':
  528. case 'SubType':
  529. $this->addMessage('o_font '.$k." : ".$v);
  530. $o['info'][$k] = $v;
  531. break;
  532. }
  533. }
  534. break;
  535. case 'out':
  536. $res="\n".$id." 0 obj\n<< /Type /Font\n/Subtype /".$o['info']['SubType']."\n";
  537. $res.="/Name /F".$o['info']['fontNum']."\n";
  538. $res.="/BaseFont /".$o['info']['name']."\n";
  539. if (isset($o['info']['encodingDictionary'])){
  540. // then place a reference to the dictionary
  541. $res.="/Encoding ".$o['info']['encodingDictionary']." 0 R\n";
  542. } else if (isset($o['info']['encoding'])){
  543. // use the specified encoding
  544. $res.="/Encoding /".$o['info']['encoding']."\n";
  545. }
  546. if (isset($o['info']['FirstChar'])){
  547. $res.="/FirstChar ".$o['info']['FirstChar']."\n";
  548. }
  549. if (isset($o['info']['LastChar'])){
  550. $res.="/LastChar ".$o['info']['LastChar']."\n";
  551. }
  552. if (isset($o['info']['Widths'])){
  553. $res.="/Widths ".$o['info']['Widths']." 0 R\n";
  554. }
  555. if (isset($o['info']['FontDescriptor'])){
  556. $res.="/FontDescriptor ".$o['info']['FontDescriptor']." 0 R\n";
  557. }
  558. $res.=">>\nendobj";
  559. return $res;
  560. break;
  561. }
  562. }
  563. /**
  564. * a font descriptor, needed for including additional fonts
  565. */
  566. function o_fontDescriptor($id,$action,$options=''){
  567. if ($action!='new'){
  568. $o =& $this->objects[$id];
  569. }
  570. switch ($action){
  571. case 'new':
  572. $this->objects[$id]=array('t'=>'fontDescriptor','info'=>$options);
  573. break;
  574. case 'out':
  575. $res="\n".$id." 0 obj\n<< /Type /FontDescriptor\n";
  576. foreach ($o['info'] as $label => $value){
  577. switch ($label){
  578. case 'Ascent':
  579. case 'CapHeight':
  580. case 'Descent':
  581. case 'Flags':
  582. case 'ItalicAngle':
  583. case 'StemV':
  584. case 'AvgWidth':
  585. case 'Leading':
  586. case 'MaxWidth':
  587. case 'MissingWidth':
  588. case 'StemH':
  589. case 'XHeight':
  590. case 'CharSet':
  591. if (strlen($value)){
  592. $res.='/'.$label.' '.$value."\n";
  593. }
  594. break;
  595. case 'FontFile':
  596. case 'FontFile2':
  597. case 'FontFile3':
  598. $res.='/'.$label.' '.$value." 0 R\n";
  599. break;
  600. case 'FontBBox':
  601. $res.='/'.$label.' ['.$value[0].' '.$value[1].' '.$value[2].' '.$value[3]."]\n";
  602. break;
  603. case 'FontName':
  604. $res.='/'.$label.' /'.$value."\n";
  605. break;
  606. }
  607. }
  608. $res.=">>\nendobj";
  609. return $res;
  610. break;
  611. }
  612. }
  613. /**
  614. * the font encoding
  615. */
  616. function o_fontEncoding($id,$action,$options=''){
  617. if ($action!='new'){
  618. $o =& $this->objects[$id];
  619. }
  620. switch ($action){
  621. case 'new':
  622. // the options array should contain 'differences' and maybe 'encoding'
  623. $this->objects[$id]=array('t'=>'fontEncoding','info'=>$options);
  624. break;
  625. case 'out':
  626. $res="\n".$id." 0 obj\n<< /Type /Encoding\n";
  627. if (!isset($o['info']['encoding'])){
  628. $o['info']['encoding']='WinAnsiEncoding';
  629. }
  630. if ($o['info']['encoding']!='none'){
  631. $res.="/BaseEncoding /".$o['info']['encoding']."\n";
  632. }
  633. $res.="/Differences \n[";
  634. $onum=-100;
  635. foreach($o['info']['differences'] as $num=>$label){
  636. if ($num!=$onum+1){
  637. // we cannot make use of consecutive numbering
  638. $res.= "\n".$num." /".$label;
  639. } else {
  640. $res.= " /".$label;
  641. }
  642. $onum=$num;
  643. }
  644. $res.="\n]\n>>\nendobj";
  645. return $res;
  646. break;
  647. }
  648. }
  649. /**
  650. * the document procset, solves some problems with printing to old PS printers
  651. */
  652. function o_procset($id,$action,$options=''){
  653. if ($action!='new'){
  654. $o =& $this->objects[$id];
  655. }
  656. switch ($action){
  657. case 'new':
  658. $this->objects[$id]=array('t'=>'procset','info'=>array('PDF'=>1,'Text'=>1));
  659. $this->o_pages($this->currentNode,'procset',$id);
  660. $this->procsetObjectId=$id;
  661. break;
  662. case 'add':
  663. // this is to add new items to the procset list, despite the fact that this is considered
  664. // obselete, the items are required for printing to some postscript printers
  665. switch ($options) {
  666. case 'ImageB':
  667. case 'ImageC':
  668. case 'ImageI':
  669. $o['info'][$options]=1;
  670. break;
  671. }
  672. break;
  673. case 'out':
  674. $res="\n".$id." 0 obj\n[";
  675. foreach ($o['info'] as $label=>$val){
  676. $res.='/'.$label.' ';
  677. }
  678. $res.="]\nendobj";
  679. return $res;
  680. break;
  681. }
  682. }
  683. /**
  684. * define the document information
  685. */
  686. function o_info($id,$action,$options=''){
  687. if ($action!='new'){
  688. $o =& $this->objects[$id];
  689. }
  690. switch ($action){
  691. case 'new':
  692. $this->infoObject=$id;
  693. $date='D:'.date('Ymd');
  694. $this->objects[$id]=array('t'=>'info','info'=>array('Creator'=>'R and OS php pdf writer, http://www.ros.co.nz','CreationDate'=>$date));
  695. break;
  696. case 'Title':
  697. case 'Author':
  698. case 'Subject':
  699. case 'Keywords':
  700. case 'Creator':
  701. case 'Producer':
  702. case 'CreationDate':
  703. case 'ModDate':
  704. case 'Trapped':
  705. $o['info'][$action]=$options;
  706. break;
  707. case 'out':
  708. if ($this->encrypted){
  709. $this->encryptInit($id);
  710. }
  711. $res="\n".$id." 0 obj\n<<\n";
  712. foreach ($o['info'] as $k=>$v){
  713. $res.='/'.$k.' (';
  714. if ($this->encrypted){
  715. $res.=$this->filterText($this->ARC4($v));
  716. } else {
  717. $res.=$this->filterText($v);
  718. }
  719. $res.=")\n";
  720. }
  721. $res.=">>\nendobj";
  722. return $res;
  723. break;
  724. }
  725. }
  726. /**
  727. * an action object, used to link to URLS initially
  728. */
  729. function o_action($id,$action,$options=''){
  730. if ($action!='new'){
  731. $o =& $this->objects[$id];
  732. }
  733. switch ($action){
  734. case 'new':
  735. if (is_array($options)){
  736. $this->objects[$id]=array('t'=>'action','info'=>$options,'type'=>$options['type']);
  737. } else {
  738. // then assume a URI action
  739. $this->objects[$id]=array('t'=>'action','info'=>$options,'type'=>'URI');
  740. }
  741. break;
  742. case 'out':
  743. if ($this->encrypted){
  744. $this->encryptInit($id);
  745. }
  746. $res="\n".$id." 0 obj\n<< /Type /Action";
  747. switch($o['type']){
  748. case 'ilink':
  749. // there will be an 'label' setting, this is the name of the destination
  750. $res.="\n/S /GoTo\n/D ".$this->destinations[(string)$o['info']['label']]." 0 R";
  751. break;
  752. case 'URI':
  753. $res.="\n/S /URI\n/URI (";
  754. if ($this->encrypted){
  755. $res.=$this->filterText($this->ARC4($o['info']));
  756. } else {
  757. $res.=$this->filterText($o['info']);
  758. }
  759. $res.=")";
  760. break;
  761. }
  762. $res.="\n>>\nendobj";
  763. return $res;
  764. break;
  765. }
  766. }
  767. /**
  768. * an annotation object, this will add an annotation to the current page.
  769. * initially will support just link annotations
  770. */
  771. function o_annotation($id,$action,$options=''){
  772. if ($action!='new'){
  773. $o =& $this->objects[$id];
  774. }
  775. switch ($action){
  776. case 'new':
  777. // add the annotation to the current page
  778. $pageId = $this->currentPage;
  779. $this->o_page($pageId,'annot',$id);
  780. // and add the action object which is going to be required
  781. switch($options['type']){
  782. case 'link':
  783. $this->objects[$id]=array('t'=>'annotation','info'=>$options);
  784. $this->numObj++;
  785. $this->o_action($this->numObj,'new',$options['url']);
  786. $this->objects[$id]['info']['actionId']=$this->numObj;
  787. break;
  788. case 'ilink':
  789. // this is to a named internal link
  790. $label = $options['label'];
  791. $this->objects[$id]=array('t'=>'annotation','info'=>$options);
  792. $this->numObj++;
  793. $this->o_action($this->numObj,'new',array('type'=>'ilink','label'=>$label));
  794. $this->objects[$id]['info']['actionId']=$this->numObj;
  795. break;
  796. }
  797. break;
  798. case 'out':
  799. $res="\n".$id." 0 obj\n<< /Type /Annot";
  800. switch($o['info']['type']){
  801. case 'link':
  802. case 'ilink':
  803. $res.= "\n/Subtype /Link";
  804. break;
  805. }
  806. $res.="\n/A ".$o['info']['actionId']." 0 R";
  807. $res.="\n/Border [0 0 0]";
  808. $res.="\n/H /I";
  809. $res.="\n/Rect [ ";
  810. foreach($o['info']['rect'] as $v){
  811. $res.= sprintf("%.4F ",$v);
  812. }
  813. $res.="]";
  814. $res.="\n>>\nendobj";
  815. return $res;
  816. break;
  817. }
  818. }
  819. /**
  820. * a page object, it also creates a contents object to hold its contents
  821. */
  822. function o_page($id,$action,$options=''){
  823. if ($action!='new'){
  824. $o =& $this->objects[$id];
  825. }
  826. switch ($action){
  827. case 'new':
  828. $this->numPages++;
  829. $this->objects[$id]=array('t'=>'page','info'=>array('parent'=>$this->currentNode,'pageNum'=>$this->numPages));
  830. if (is_array($options)){
  831. // then this must be a page insertion, array shoudl contain 'rid','pos'=[before|after]
  832. $options['id']=$id;
  833. $this->o_pages($this->currentNode,'page',$options);
  834. } else {
  835. $this->o_pages($this->currentNode,'page',$id);
  836. }
  837. $this->currentPage=$id;
  838. //make a contents object to go with this page
  839. $this->numObj++;
  840. $this->o_contents($this->numObj,'new',$id);
  841. $this->currentContents=$this->numObj;
  842. $this->objects[$id]['info']['contents']=array();
  843. $this->objects[$id]['info']['contents'][]=$this->numObj;
  844. $match = ($this->numPages%2 ? 'odd' : 'even');
  845. foreach($this->addLooseObjects as $oId=>$target){
  846. if ($target=='all' || $match==$target){
  847. $this->objects[$id]['info']['contents'][]=$oId;
  848. }
  849. }
  850. break;
  851. case 'content':
  852. $o['info']['contents'][]=$options;
  853. break;
  854. case 'annot':
  855. // add an annotation to this page
  856. if (!isset($o['info']['annot'])){
  857. $o['info']['annot']=array();
  858. }
  859. // $options should contain the id of the annotation dictionary
  860. $o['info']['annot'][]=$options;
  861. break;
  862. case 'out':
  863. $res="\n".$id." 0 obj\n<< /Type /Page";
  864. $res.="\n/Parent ".$o['info']['parent']." 0 R";
  865. if (isset($o['info']['annot'])){
  866. $res.="\n/Annots [";
  867. foreach($o['info']['annot'] as $aId){
  868. $res.=" ".$aId." 0 R";
  869. }
  870. $res.=" ]";
  871. }
  872. $count = count($o['info']['contents']);
  873. if ($count==1){
  874. $res.="\n/Contents ".$o['info']['contents'][0]." 0 R";
  875. } else if ($count>1){
  876. $res.="\n/Contents [\n";
  877. foreach ($o['info']['contents'] as $cId){
  878. $res.=$cId." 0 R\n";
  879. }
  880. $res.="]";
  881. }
  882. $res.="\n>>\nendobj";
  883. return $res;
  884. break;
  885. }
  886. }
  887. /**
  888. * the contents objects hold all of the content which appears on pages
  889. */
  890. function o_contents($id,$action,$options=''){
  891. if ($action!='new'){
  892. $o =& $this->objects[$id];
  893. }
  894. switch ($action){
  895. case 'new':
  896. $this->objects[$id]=array('t'=>'contents','c'=>'','info'=>array());
  897. if (strlen($options) && intval($options)){
  898. // then this contents is the primary for a page
  899. $this->objects[$id]['onPage']=$options;
  900. } else if ($options=='raw'){
  901. // then this page contains some other type of system object
  902. $this->objects[$id]['raw']=1;
  903. }
  904. break;
  905. case 'add':
  906. // add more options to the decleration
  907. foreach ($options as $k=>$v){
  908. $o['info'][$k]=$v;
  909. }
  910. case 'out':
  911. $tmp=$o['c'];
  912. $res= "\n".$id." 0 obj\n";
  913. if (isset($this->objects[$id]['raw'])){
  914. $res.=$tmp;
  915. } else {
  916. $res.= "<<";
  917. if (function_exists('gzcompress') && $this->options['compression']){
  918. // then implement ZLIB based compression on this content stream
  919. $res.=" /Filter /FlateDecode";
  920. $tmp = gzcompress($tmp);
  921. }
  922. if ($this->encrypted){
  923. $this->encryptInit($id);
  924. $tmp = $this->ARC4($tmp);
  925. }
  926. foreach($o['info'] as $k=>$v){
  927. $res .= "\n/".$k.' '.$v;
  928. }
  929. $res.="\n/Length ".strlen($tmp)." >>\nstream\n".$tmp."\nendstream";
  930. }
  931. $res.="\nendobj\n";
  932. return $res;
  933. break;
  934. }
  935. }
  936. /**
  937. * an image object, will be an XObject in the document, includes description and data
  938. */
  939. function o_image($id,$action,$options=''){
  940. if ($action!='new'){
  941. $o =& $this->objects[$id];
  942. }
  943. switch($action){
  944. case 'new':
  945. // make the new object
  946. $this->objects[$id]=array('t'=>'image','data'=>$options['data'],'info'=>array());
  947. $this->objects[$id]['info']['Type']='/XObject';
  948. $this->objects[$id]['info']['Subtype']='/Image';
  949. $this->objects[$id]['info']['Width']=$options['iw'];
  950. $this->objects[$id]['info']['Height']=$options['ih'];
  951. if (!isset($options['type']) || $options['type']=='jpg'){
  952. if (!isset($options['channels'])){
  953. $options['channels']=3;
  954. }
  955. switch($options['channels']){
  956. case 1:
  957. $this->objects[$id]['info']['ColorSpace']='/DeviceGray';
  958. break;
  959. default:
  960. $this->objects[$id]['info']['ColorSpace']='/DeviceRGB';
  961. break;
  962. }
  963. $this->objects[$id]['info']['Filter']='/DCTDecode';
  964. $this->objects[$id]['info']['BitsPerComponent']=8;
  965. } else if ($options['type']=='png'){
  966. $this->objects[$id]['info']['Filter']='/FlateDecode';
  967. $this->objects[$id]['info']['DecodeParms']='<< /Predictor 15 /Colors '.$options['ncolor'].' /Columns '.$options['iw'].' /BitsPerComponent '.$options['bitsPerComponent'].'>>';
  968. if (strlen($options['pdata'])){
  969. $tmp = ' [ /Indexed /DeviceRGB '.(strlen($options['pdata'])/3-1).' ';
  970. $this->numObj++;
  971. $this->o_contents($this->numObj,'new');
  972. $this->objects[$this->numObj]['c']=$options['pdata'];
  973. $tmp.=$this->numObj.' 0 R';
  974. $tmp .=' ]';
  975. $this->objects[$id]['info']['ColorSpace'] = $tmp;
  976. if (isset($options['transparency'])){
  977. switch($options['transparency']['type']){
  978. case 'indexed':
  979. $tmp=' [ '.$options['transparency']['data'].' '.$options['transparency']['data'].'] ';
  980. $this->objects[$id]['info']['Mask'] = $tmp;
  981. break;
  982. }
  983. }
  984. } else {
  985. $this->objects[$id]['info']['ColorSpace']='/'.$options['color'];
  986. }
  987. $this->objects[$id]['info']['BitsPerComponent']=$options['bitsPerComponent'];
  988. }
  989. // assign it a place in the named resource dictionary as an external object, according to
  990. // the label passed in with it.
  991. $this->o_pages($this->currentNode,'xObject',array('label'=>$options['label'],'objNum'=>$id));
  992. // also make sure that we have the right procset object for it.
  993. $this->o_procset($this->procsetObjectId,'add','ImageC');
  994. break;
  995. case 'out':
  996. $tmp=$o['data'];
  997. $res= "\n".$id." 0 obj\n<<";
  998. foreach($o['info'] as $k=>$v){
  999. $res.="\n/".$k.' '.$v;
  1000. }
  1001. if ($this->encrypted){
  1002. $this->encryptInit($id);
  1003. $tmp = $this->ARC4($tmp);
  1004. }
  1005. $res.="\n/Length ".strlen($tmp)." >>\nstream\n".$tmp."\nendstream\nendobj\n";
  1006. return $res;
  1007. break;
  1008. }
  1009. }
  1010. /**
  1011. * encryption object.
  1012. */
  1013. function o_encryption($id,$action,$options=''){
  1014. if ($action!='new'){
  1015. $o =& $this->objects[$id];
  1016. }
  1017. switch($action){
  1018. case 'new':
  1019. // make the new object
  1020. $this->objects[$id]=array('t'=>'encryption','info'=>$options);
  1021. $this->arc4_objnum=$id;
  1022. // figure out the additional paramaters required
  1023. $pad = chr(0x28).chr(0xBF).chr(0x4E).chr(0x5E).chr(0x4E).chr(0x75).chr(0x8A).chr(0x41).chr(0x64).chr(0x00).chr(0x4E).chr(0x56).chr(0xFF).chr(0xFA).chr(0x01).chr(0x08).chr(0x2E).chr(0x2E).chr(0x00).chr(0xB6).chr(0xD0).chr(0x68).chr(0x3E).chr(0x80).chr(0x2F).chr(0x0C).chr(0xA9).chr(0xFE).chr(0x64).chr(0x53).chr(0x69).chr(0x7A);
  1024. $len = strlen($options['owner']);
  1025. if ($len>32){
  1026. $owner = substr($options['owner'],0,32);
  1027. } else if ($len<32){
  1028. $owner = $options['owner'].substr($pad,0,32-$len);
  1029. } else {
  1030. $owner = $options['owner'];
  1031. }
  1032. $len = strlen($options['user']);
  1033. if ($len>32){
  1034. $user = substr($options['user'],0,32);
  1035. } else if ($len<32){
  1036. $user = $options['user'].substr($pad,0,32-$len);
  1037. } else {
  1038. $user = $options['user'];
  1039. }
  1040. $tmp = $this->md5_16($owner);
  1041. $okey = substr($tmp,0,5);
  1042. $this->ARC4_init($okey);
  1043. $ovalue=$this->ARC4($user);
  1044. $this->objects[$id]['info']['O']=$ovalue;
  1045. // now make the u value, phew.
  1046. $tmp = $this->md5_16($user.$ovalue.chr($options['p']).chr(255).chr(255).chr(255).$this->fileIdentifier);
  1047. $ukey = substr($tmp,0,5);
  1048. $this->ARC4_init($ukey);
  1049. $this->encryptionKey = $ukey;
  1050. $this->encrypted=1;
  1051. $uvalue=$this->ARC4($pad);
  1052. $this->objects[$id]['info']['U']=$uvalue;
  1053. $this->encryptionKey=$ukey;
  1054. // initialize the arc4 array
  1055. break;
  1056. case 'out':
  1057. $res= "\n".$id." 0 obj\n<<";
  1058. $res.="\n/Filter /Standard";
  1059. $res.="\n/V 1";
  1060. $res.="\n/R 2";
  1061. $res.="\n/O (".$this->filterText($o['info']['O']).')';
  1062. $res.="\n/U (".$this->filterText($o['info']['U']).')';
  1063. // and the p-value needs to be converted to account for the twos-complement approach
  1064. $o['info']['p'] = (($o['info']['p']^255)+1)*-1;
  1065. $res.="\n/P ".($o['info']['p']);
  1066. $res.="\n>>\nendobj\n";
  1067. return $res;
  1068. break;
  1069. }
  1070. }
  1071. /**
  1072. * ARC4 functions
  1073. * A series of function to implement ARC4 encoding in PHP
  1074. */
  1075. /**
  1076. * calculate the 16 byte version of the 128 bit md5 digest of the string
  1077. */
  1078. function md5_16($string){
  1079. $tmp = md5($string);
  1080. $out='';
  1081. for ($i=0;$i<=30;$i=$i+2){
  1082. $out.=chr(hexdec(substr($tmp,$i,2)));
  1083. }
  1084. return $out;
  1085. }
  1086. /**
  1087. * initialize the encryption for processing a particular object
  1088. */
  1089. function encryptInit($id){
  1090. $tmp = $this->encryptionKey;
  1091. $hex = dechex($id);
  1092. if (strlen($hex)<6){
  1093. $hex = substr('000000',0,6-strlen($hex)).$hex;
  1094. }
  1095. $tmp.= chr(hexdec(substr($hex,4,2))).chr(hexdec(substr($hex,2,2))).chr(hexdec(substr($hex,0,2))).chr(0).chr(0);
  1096. $key = $this->md5_16($tmp);
  1097. $this->ARC4_init(substr($key,0,10));
  1098. }
  1099. /**
  1100. * initialize the ARC4 encryption
  1101. */
  1102. function ARC4_init($key=''){
  1103. $this->arc4 = '';
  1104. // setup the control array
  1105. if (strlen($key)==0){
  1106. return;
  1107. }
  1108. $k = '';
  1109. while(strlen($k)<256){
  1110. $k.=$key;
  1111. }
  1112. $k=substr($k,0,256);
  1113. for ($i=0;$i<256;$i++){
  1114. $this->arc4 .= chr($i);
  1115. }
  1116. $j=0;
  1117. for ($i=0;$i<256;$i++){
  1118. $t = $this->arc4[$i];
  1119. $j = ($j + ord($t) + ord($k[$i]))%256;
  1120. $this->arc4[$i]=$this->arc4[$j];
  1121. $this->arc4[$j]=$t;
  1122. }
  1123. }
  1124. /**
  1125. * ARC4 encrypt a text string
  1126. */
  1127. function ARC4($text){
  1128. $len=strlen($text);
  1129. $a=0;
  1130. $b=0;
  1131. $c = $this->arc4;
  1132. $out='';
  1133. for ($i=0;$i<$len;$i++){
  1134. $a = ($a+1)%256;
  1135. $t= $c[$a];
  1136. $b = ($b+ord($t))%256;
  1137. $c[$a]=$c[$b];
  1138. $c[$b]=$t;
  1139. $k = ord($c[(ord($c[$a])+ord($c[$b]))%256]);
  1140. $out.=chr(ord($text[$i]) ^ $k);
  1141. }
  1142. return $out;
  1143. }
  1144. /**
  1145. * functions which can be called to adjust or add to the document
  1146. */
  1147. /**
  1148. * add a link in the document to an external URL
  1149. */
  1150. function addLink($url,$x0,$y0,$x1,$y1){
  1151. $this->numObj++;
  1152. $info = array('type'=>'link','url'=>$url,'rect'=>array($x0,$y0,$x1,$y1));
  1153. $this->o_annotation($this->numObj,'new',$info);
  1154. }
  1155. /**
  1156. * add a link in the document to an internal destination (ie. within the document)
  1157. */
  1158. function addInternalLink($label,$x0,$y0,$x1,$y1){
  1159. $this->numObj++;
  1160. $info = array('type'=>'ilink','label'=>$label,'rect'=>array($x0,$y0,$x1,$y1));
  1161. $this->o_annotation($this->numObj,'new',$info);
  1162. }
  1163. /**
  1164. * set the encryption of the document
  1165. * can be used to turn it on and/or set the passwords which it will have.
  1166. * also the functions that the user will have are set here, such as print, modify, add
  1167. */
  1168. function setEncryption($userPass='',$ownerPass='',$pc=array()){
  1169. $p=bindec(11000000);
  1170. $options = array(
  1171. 'print'=>4
  1172. ,'modify'=>8
  1173. ,'copy'=>16
  1174. ,'add'=>32
  1175. );
  1176. foreach($pc as $k=>$v){
  1177. if ($v && isset($options[$k])){
  1178. $p+=$options[$k];
  1179. } else if (isset($options[$v])){
  1180. $p+=$options[$v];
  1181. }
  1182. }
  1183. // implement encryption on the document
  1184. if ($this->arc4_objnum == 0){
  1185. // then the block does not exist already, add it.
  1186. $this->numObj++;
  1187. if (strlen($ownerPass)==0){
  1188. $ownerPass=$userPass;
  1189. }
  1190. $this->o_encryption($this->numObj,'new',array('user'=>$userPass,'owner'=>$ownerPass,'p'=>$p));
  1191. }
  1192. }
  1193. /**
  1194. * should be used for internal checks, not implemented as yet
  1195. */
  1196. function checkAllHere(){
  1197. }
  1198. /**
  1199. * return the pdf stream as a string returned from the function
  1200. */
  1201. function output($debug=0){
  1202. if ($debug){
  1203. // turn compression off
  1204. $this->options['compression']=0;
  1205. }
  1206. if ($this->arc4_objnum){
  1207. $this->ARC4_init($this->encryptionKey);
  1208. }
  1209. $this->checkAllHere();
  1210. $xref=array();
  1211. // $content="%PDF-1.3\n%????\n";
  1212. $content="%PDF-1.3\n";
  1213. $pos=strlen($content);
  1214. foreach($this->objects as $k=>$v){
  1215. $tmp='o_'.$v['t'];
  1216. $cont=$this->$tmp($k,'out');
  1217. $content.=$cont;
  1218. $xref[]=$pos;
  1219. $pos+=strlen($cont);
  1220. }
  1221. $content.="\nxref\n0 ".(count($xref)+1)."\n0000000000 65535 f \n";
  1222. foreach($xref as $p){
  1223. $content.=substr('0000000000',0,10-strlen($p)).$p." 00000 n \n";
  1224. }
  1225. $content.="\ntrailer\n << /Size ".(count($xref)+1)."\n /Root 1 0 R\n /Info ".$this->infoObject." 0 R\n";
  1226. // if encryption has been applied to this document then add the marker for this dictionary
  1227. if ($this->arc4_objnum > 0){
  1228. $content .= "/Encrypt ".$this->arc4_objnum." 0 R\n";
  1229. }
  1230. if (strlen($this->fileIdentifier)){
  1231. $content .= "/ID[<".$this->fileIdentifier."><".$this->fileIdentifier.">]\n";
  1232. }
  1233. $content .= " >>\nstartxref\n".$pos."\n%%EOF\n";
  1234. return $content;
  1235. }
  1236. /**
  1237. * intialize a new document
  1238. * if this is called on an existing document results may be unpredictable, but the existing document would be lost at minimum
  1239. * this function is called automatically by the constructor function
  1240. *
  1241. * @access private
  1242. */
  1243. function newDocument($pageSize=array(0,0,612,792)){
  1244. $this->numObj=0;
  1245. $this->objects = array();
  1246. $this->numObj++;
  1247. $this->o_catalog($this->numObj,'new');
  1248. $this->numObj++;
  1249. $this->o_outlines($this->numObj,'new');
  1250. $this->numObj++;
  1251. $this->o_pages($this->numObj,'new');
  1252. $this->o_pages($this->numObj,'mediaBox',$pageSize);
  1253. $this->currentNode = 3;
  1254. $this->numObj++;
  1255. $this->o_procset($this->numObj,'new');
  1256. $this->numObj++;
  1257. $this->o_info($this->numObj,'new');
  1258. $this->numObj++;
  1259. $this->o_page($this->numObj,'new');
  1260. // need to store the first page id as there is no way to get it to the user during
  1261. // startup
  1262. $this->firstPageId = $this->currentContents;
  1263. }
  1264. /**
  1265. * open the font file and return a php structure containing it.
  1266. * first check if this one has been done before and saved in a form more suited to php
  1267. * note that if a php serialized version does not exist it will try and make one, but will
  1268. * require write access to the directory to do it... it is MUCH faster to have these serialized
  1269. * files.
  1270. *
  1271. * @access private
  1272. */
  1273. function openFont($font){
  1274. // assume that $font contains both the path and perhaps the extension to the file, split them
  1275. $pos=strrpos($font,'/');
  1276. if ($pos===false){
  1277. $dir = './';
  1278. $name = $font;
  1279. } else {
  1280. $dir=substr($font,0,$pos+1);
  1281. $name=substr($font,$pos+1);
  1282. }
  1283. if (substr($name,-4)=='.afm'){
  1284. $name=substr($name,0,strlen($name)-4);
  1285. }
  1286. $this->addMessage('openFont: '.$font.' - '.$name);
  1287. if (file_exists($dir.'php_'.$name.'.afm')){
  1288. $this->addMessage('openFont: php file exists '.$dir.'php_'.$name.'.afm');
  1289. $tmp = file($dir.'php_'.$name.'.afm');
  1290. $this->fonts[$font]=unserialize($tmp[0]);
  1291. if (!isset($this->fonts[$font]['_version_']) || $this->fonts[$font]['_version_']<1){
  1292. // if the font file is old, then clear it out and prepare for re-creation
  1293. $this->addMessage('openFont: clear out, make way for new version.');
  1294. unset($this->fonts[$font]);
  1295. }
  1296. }
  1297. if (!isset($this->fonts[$font]) && file_exists($dir.$name.'.afm')){
  1298. // then rebuild the php_<font>.afm file from the <font>.afm file
  1299. $this->addMessage('openFont: build php file from '.$dir.$name.'.afm');
  1300. $data = array();
  1301. $file = file($dir.$name.'.afm');
  1302. foreach ($file as $rowA){
  1303. $row=trim($rowA);
  1304. $pos=strpos($row,' ');
  1305. if ($pos){
  1306. // then there must be some keyword
  1307. $key = substr($row,0,$pos);
  1308. switch ($key){
  1309. case 'FontName':
  1310. case 'FullName':
  1311. case 'FamilyName':
  1312. case 'Weight':
  1313. case 'ItalicAngle':
  1314. case 'IsFixedPitch':
  1315. case 'CharacterSet':
  1316. case 'UnderlinePosition':
  1317. case 'UnderlineThickness':
  1318. case 'Version':
  1319. case 'EncodingScheme':
  1320. case 'CapHeight':
  1321. case 'XHeight':
  1322. case 'Ascender':
  1323. case 'Descender':
  1324. case 'StdHW':
  1325. case 'StdVW':
  1326. case 'StartCharMetrics':
  1327. $data[$key]=trim(substr($row,$pos));
  1328. break;
  1329. case 'FontBBox':
  1330. $data[$key]=explode(' ',trim(substr($row,$pos)));
  1331. break;
  1332. case 'C':
  1333. //C 39 ; WX 222 ; N quoteright ; B 53 463 157 718 ;
  1334. $bits=explode(';',trim($row));
  1335. $dtmp=array();
  1336. foreach($bits as $bit){
  1337. $bits2 = explode(' ',trim($bit));
  1338. if (strlen($bits2[0])){
  1339. if (count($bits2)>2){
  1340. $dtmp[$bits2[0]]=array();
  1341. for ($i=1;$i<count($bits2);$i++){
  1342. $dtmp[$bits2[0]][]=$bits2[$i];
  1343. }
  1344. } else if (count($bits2)==2){
  1345. $dtmp[$bits2[0]]=$bits2[1];
  1346. }
  1347. }
  1348. }
  1349. if ($dtmp['C']>=0){
  1350. $data['C'][$dtmp['C']]=$dtmp;
  1351. $data['C'][$dtmp['N']]=$dtmp;
  1352. } else {
  1353. $data['C'][$dtmp['N']]=$dtmp;
  1354. }
  1355. break;
  1356. case 'KPX':
  1357. //KPX Adieresis yacute -40
  1358. $bits=explode(' ',trim($row));
  1359. $data['KPX'][$bits[1]][$bits[2]]=$bits[3];
  1360. break;
  1361. }
  1362. }
  1363. }
  1364. $data['_version_']=1;
  1365. $this->fonts[$font]=$data;
  1366. $fp = fopen($dir.'php_'.$name.'.afm','w');
  1367. fwrite($fp,serialize($data));
  1368. fclose($fp);
  1369. } else if (!isset($this->fonts[$font])){
  1370. $this->addMessage('openFont: no font file found');
  1371. // echo 'Font not Found '.$font;
  1372. }
  1373. }
  1374. /**
  1375. * if the font is not loaded then load it and make the required object
  1376. * else just make it the current font
  1377. * the encoding array can contain 'encoding'=> 'none','WinAnsiEncoding','MacRomanEncoding' or 'MacExpertEncoding'
  1378. * note that encoding='none' will need to be used for symbolic fonts
  1379. * and 'differences' => an array of mappings between numbers 0->255 and character names.
  1380. *
  1381. */
  1382. function selectFont($fontName,$encoding='',$set=1){
  1383. if (!isset($this->fonts[$fontName])){
  1384. // load the file
  1385. $this->openFont($fontName);
  1386. if (isset($this->fonts[$fontName])){
  1387. $this->numObj++;
  1388. $this->numFonts++;
  1389. $pos=strrpos($fontName,'/');
  1390. // $dir=substr($fontName,0,$pos+1);
  1391. $name=substr($fontName,$pos+1);
  1392. if (substr($name,-4)=='.afm'){
  1393. $name=substr($name,0,strlen($name)-4);
  1394. }
  1395. $options=array('name'=>$name);
  1396. if (is_array($encoding)){
  1397. // then encoding and differences might be set
  1398. if (isset($encoding['encoding'])){
  1399. $options['encoding']=$encoding['encoding'];
  1400. }
  1401. if (isset($encoding['differences'])){
  1402. $options['differences']=$encoding['differences'];
  1403. }
  1404. } else if (strlen($encoding)){
  1405. // then perhaps only the encoding has been set
  1406. $options['encoding']=$encoding;
  1407. }
  1408. $fontObj = $this->numObj;
  1409. $this->o_font($this->numObj,'new',$options);
  1410. $this->fonts[$fontName]['fontNum']=$this->numFonts;
  1411. // if this is a '.afm' font, and there is a '.pfa' file to go with it ( as there
  1412. // should be for all non-basic fonts), then load it into an object and put the
  1413. // references into the font object
  1414. $basefile = substr($fontName,0,strlen($fontName)-4);
  1415. if (file_exists($basefile.'.pfb')){
  1416. $fbtype = 'pfb';
  1417. } else if (file_exists($basefile.'.ttf')){
  1418. $fbtype = 'ttf';
  1419. } else {
  1420. $fbtype='';
  1421. }
  1422. $fbfile = $basefile.'.'.$fbtype;
  1423. // $pfbfile = substr($fontName,0,strlen($fontName)-4).'.pfb';
  1424. // $ttffile = substr($fontName,0,strlen($fontName)-4).'.ttf';
  1425. $this->addMessage('selectFont: checking for - '.$fbfile);
  1426. if (substr($fontName,-4)=='.afm' && strlen($fbtype) ){
  1427. $adobeFontName = $this->fonts[$fontName]['FontName'];
  1428. // $fontObj = $this->numObj;
  1429. $this->addMessage('selectFont: adding font file - '.$fbfile.' - '.$adobeFontName);
  1430. // find the array of fond widths, and put that into an object.
  1431. $firstChar = -1;
  1432. $lastChar = 0;
  1433. $widths = array();
  1434. foreach ($this->fonts[$fontName]['C'] as $num=>$d){
  1435. if (intval($num)>0 || $num=='0'){
  1436. if ($lastChar>0 && $num>$lastChar+1){
  1437. for($i=$lastChar+1;$i<$num;$i++){
  1438. $widths[] = 0;
  1439. }
  1440. }
  1441. $widths[] = $d['WX'];
  1442. if ($firstChar==-1){
  1443. $firstChar = $num;
  1444. }
  1445. $lastChar = $num;
  1446. }
  1447. }
  1448. // also need to adjust the widths for the differences array
  1449. if (isset($options['differences'])){
  1450. foreach($options['differences'] as $charNum=>$charName){
  1451. if ($charNum>$lastChar){
  1452. for($i=$lastChar+1;$i<=$charNum;$i++){
  1453. $widths[]=0;
  1454. }
  1455. $lastChar=$charNum;
  1456. }
  1457. if (isset($this->fonts[$fontName]['C'][$charName])){
  1458. $widths[$charNum-$firstChar]=$this->fonts[$fontName]['C'][$charName]['WX'];
  1459. }
  1460. }
  1461. }
  1462. $this->addMessage('selectFont: FirstChar='.$firstChar);
  1463. $this->addMessage('selectFont: LastChar='.$lastChar);
  1464. $this->numObj++;
  1465. $this->o_contents($this->numObj,'new','raw');
  1466. $this->objects[$this->numObj]['c'].='[';
  1467. foreach($widths as $width){
  1468. $this->objects[$this->numObj]['c'].=' '.$width;
  1469. }
  1470. $this->objects[$this->numObj]['c'].=' ]';
  1471. $widthid = $this->numObj;
  1472. // load the pfb file, and put that into an object too.
  1473. // note that pdf supports only binary format type 1 font files, though there is a
  1474. // simple utility to convert them from pfa to pfb.
  1475. $fp = fopen($fbfile,'rb');
  1476. $tmp = get_magic_quotes_runtime();
  1477. set_magic_quotes_runtime(0);
  1478. $data = fread($fp,filesize($fbfile));
  1479. set_magic_quotes_runtime($tmp);
  1480. fclose($fp);
  1481. // create the font descriptor
  1482. $this->numObj++;
  1483. $fontDescriptorId = $this->numObj;
  1484. $this->numObj++;
  1485. $pfbid = $this->numObj;
  1486. // determine flags (more than a little flakey, hopefully will not matter much)
  1487. $flags=0;
  1488. if ($this->fonts[$fontName]['ItalicAngle']!=0){ $flags+=pow(2,6); }
  1489. if ($this->fonts[$fontName]['IsFixedPitch']=='true'){ $flags+=1; }
  1490. $flags+=pow(2,5); // assume non-sybolic
  1491. $list = array('Ascent'=>'Ascender','CapHeight'=>'CapHeight','Descent'=>'Descender','FontBBox'=>'FontBBox','ItalicAngle'=>'ItalicAngle');
  1492. $fdopt = array(
  1493. 'Flags'=>$flags
  1494. ,'FontName'=>$adobeFontName
  1495. ,'StemV'=>100 // don't know what the value for this should be!
  1496. );
  1497. foreach($list as $k=>$v){
  1498. if (isset($this->fonts[$fontName][$v])){
  1499. $fdopt[$k]=$this->fonts[$fontName][$v];
  1500. }
  1501. }
  1502. if ($fbtype=='pfb'){
  1503. $fdopt['FontFile']=$pfbid;
  1504. } else if ($fbtype=='ttf'){
  1505. $fdopt['FontFile2']=$pfbid;
  1506. }
  1507. $this->o_fontDescriptor($fontDescriptorId,'new',$fdopt);
  1508. // embed the font program
  1509. $this->o_contents($this->numObj,'new');
  1510. $this->objects[$pfbid]['c'].=$data;
  1511. // determine the cruicial lengths within this file
  1512. if ($fbtype=='pfb'){
  1513. $l1 = strpos($data,'eexec')+6;
  1514. $l2 = strpos($data,'00000000')-$l1;
  1515. $l3 = strlen($data)-$l2-$l1;
  1516. $this->o_contents($this->numObj,'add',array('Length1'=>$l1,'Length2'=>$l2,'Length3'=>$l3));
  1517. } else if ($fbtype=='ttf'){
  1518. $l1 = strlen($data);
  1519. $this->o_contents($this->numObj,'add',array('Length1'=>$l1));
  1520. }
  1521. // tell the font object about all this new stuff
  1522. $tmp = array('BaseFont'=>$adobeFontName,'Widths'=>$widthid
  1523. ,'FirstChar'=>$firstChar,'LastChar'=>$lastChar
  1524. ,'FontDescriptor'=>$fontDescriptorId);
  1525. if ($fbtype=='ttf'){
  1526. $tmp['SubType']='TrueType';
  1527. }
  1528. $this->addMessage('adding extra info to font.('.$fontObj.')');
  1529. foreach($tmp as $fk=>$fv){
  1530. $this->addMessage($fk." : ".$fv);
  1531. }
  1532. $this->o_font($fontObj,'add',$tmp);
  1533. } else {
  1534. $this->addMessage('selectFont: pfb or ttf file not found, ok if this is one of the 14 standard fonts');
  1535. }
  1536. // also set the differences here, note that this means that these will take effect only the
  1537. //first time that a font is selected, else they are ignored
  1538. if (isset($options['differences'])){
  1539. $this->fonts[$fontName]['differences']=$options['differences'];
  1540. }
  1541. }
  1542. }
  1543. if ($set && isset($this->fonts[$fontName])){
  1544. // so if for some reason the font was not set in the last one then it will not be selected
  1545. $this->currentBaseFont=$fontName;
  1546. // the next line means that if a new font is selected, then the current text state will be
  1547. // applied to it as well.
  1548. $this->setCurrentFont();
  1549. }
  1550. return $this->currentFontNum;
  1551. }
  1552. /**
  1553. * sets up the current f…

Large files files are truncated, but you can click here to view the full file