PageRenderTime 37ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/class.pdf.php

https://bitbucket.org/dgough/annamaria-daneswood-25102012
PHP | 3077 lines | 2175 code | 168 blank | 734 comment | 394 complexity | 13bb05826e45cd3854f5d19eeb366bc1 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * Cpdf
  4. *
  5. * http://www.ros.co.nz/pdf
  6. *
  7. * A PHP class to provide the basic functionality to create a pdf document without
  8. * any requirement for additional modules.
  9. *
  10. * Note that they companion class CezPdf can be used to extend this class and dramatically
  11. * simplify the creation of documents.
  12. *
  13. * IMPORTANT NOTE
  14. * there is no warranty, implied or otherwise with this software.
  15. *
  16. * LICENCE
  17. * This code has been placed in the Public Domain for all to enjoy.
  18. *
  19. * @author Wayne Munro <pdf@ros.co.nz>
  20. * @version 009
  21. * @package Cpdf
  22. */
  23. class Cpdf {
  24. /**
  25. * the current number of pdf objects in the document
  26. */
  27. var $numObj=0;
  28. /**
  29. * this array contains all of the pdf objects, ready for final assembly
  30. */
  31. var $objects = array();
  32. /**
  33. * the objectId (number within the objects array) of the document catalog
  34. */
  35. var $catalogId;
  36. /**
  37. * array carrying information about the fonts that the system currently knows about
  38. * used to ensure that a font is not loaded twice, among other things
  39. */
  40. var $fonts=array();
  41. /**
  42. * a record of the current font
  43. */
  44. var $currentFont='';
  45. /**
  46. * the current base font
  47. */
  48. var $currentBaseFont='';
  49. /**
  50. * the number of the current font within the font array
  51. */
  52. var $currentFontNum=0;
  53. /**
  54. *
  55. */
  56. var $currentNode;
  57. /**
  58. * object number of the current page
  59. */
  60. var $currentPage;
  61. /**
  62. * object number of the currently active contents block
  63. */
  64. var $currentContents;
  65. /**
  66. * number of fonts within the system
  67. */
  68. var $numFonts=0;
  69. /**
  70. * current colour for fill operations, defaults to inactive value, all three components should be between 0 and 1 inclusive when active
  71. */
  72. var $currentColour=array('r'=>-1,'g'=>-1,'b'=>-1);
  73. /**
  74. * current colour for stroke operations (lines etc.)
  75. */
  76. var $currentStrokeColour=array('r'=>-1,'g'=>-1,'b'=>-1);
  77. /**
  78. * current style that lines are drawn in
  79. */
  80. var $currentLineStyle='';
  81. /**
  82. * an array which is used to save the state of the document, mainly the colours and styles
  83. * it is used to temporarily change to another state, the change back to what it was before
  84. */
  85. var $stateStack = array();
  86. /**
  87. * number of elements within the state stack
  88. */
  89. var $nStateStack = 0;
  90. /**
  91. * number of page objects within the document
  92. */
  93. var $numPages=0;
  94. /**
  95. * object Id storage stack
  96. */
  97. var $stack=array();
  98. /**
  99. * number of elements within the object Id storage stack
  100. */
  101. var $nStack=0;
  102. /**
  103. * an array which contains information about the objects which are not firmly attached to pages
  104. * these have been added with the addObject function
  105. */
  106. var $looseObjects=array();
  107. /**
  108. * array contains infomation about how the loose objects are to be added to the document
  109. */
  110. var $addLooseObjects=array();
  111. /**
  112. * the objectId of the information object for the document
  113. * this contains authorship, title etc.
  114. */
  115. var $infoObject=0;
  116. /**
  117. * number of images being tracked within the document
  118. */
  119. var $numImages=0;
  120. /**
  121. * an array containing options about the document
  122. * it defaults to turning on the compression of the objects
  123. */
  124. var $options=array('compression'=>1);
  125. /**
  126. * the objectId of the first page of the document
  127. */
  128. var $firstPageId;
  129. /**
  130. * used to track the last used value of the inter-word spacing, this is so that it is known
  131. * when the spacing is changed.
  132. */
  133. var $wordSpaceAdjust=0;
  134. /**
  135. * the object Id of the procset object
  136. */
  137. var $procsetObjectId;
  138. /**
  139. * store the information about the relationship between font families
  140. * this used so that the code knows which font is the bold version of another font, etc.
  141. * the value of this array is initialised in the constuctor function.
  142. */
  143. var $fontFamilies = array();
  144. /**
  145. * track if the current font is bolded or italicised
  146. */
  147. var $currentTextState = '';
  148. /**
  149. * messages are stored here during processing, these can be selected afterwards to give some useful debug information
  150. */
  151. var $messages='';
  152. /**
  153. * the ancryption array for the document encryption is stored here
  154. */
  155. var $arc4='';
  156. /**
  157. * the object Id of the encryption information
  158. */
  159. var $arc4_objnum=0;
  160. /**
  161. * the file identifier, used to uniquely identify a pdf document
  162. */
  163. var $fileIdentifier='';
  164. /**
  165. * a flag to say if a document is to be encrypted or not
  166. */
  167. var $encrypted=0;
  168. /**
  169. * the ancryption key for the encryption of all the document content (structure is not encrypted)
  170. */
  171. var $encryptionKey='';
  172. /**
  173. * array which forms a stack to keep track of nested callback functions
  174. */
  175. var $callback = array();
  176. /**
  177. * the number of callback functions in the callback array
  178. */
  179. var $nCallback = 0;
  180. /**
  181. * store label->id pairs for named destinations, these will be used to replace internal links
  182. * done this way so that destinations can be defined after the location that links to them
  183. */
  184. var $destinations = array();
  185. /**
  186. * store the stack for the transaction commands, each item in here is a record of the values of all the
  187. * variables within the class, so that the user can rollback at will (from each 'start' command)
  188. * note that this includes the objects array, so these can be large.
  189. */
  190. var $checkpoint = '';
  191. /**
  192. * class constructor
  193. * this will start a new document
  194. * @var array array of 4 numbers, defining the bottom left and upper right corner of the page. first two are normally zero.
  195. */
  196. function Cpdf ($pageSize=array(0,0,612,792)){
  197. $this->newDocument($pageSize);
  198. // also initialize the font families that are known about already
  199. $this->setFontFamily('init');
  200. // $this->fileIdentifier = md5('xxxxxxxx'.time());
  201. }
  202. /**
  203. * Document object methods (internal use only)
  204. *
  205. * There is about one object method for each type of object in the pdf document
  206. * Each function has the same call list ($id,$action,$options).
  207. * $id = the object ID of the object, or what it is to be if it is being created
  208. * $action = a string specifying the action to be performed, though ALL must support:
  209. * 'new' - create the object with the id $id
  210. * 'out' - produce the output for the pdf object
  211. * $options = optional, a string or array containing the various parameters for the object
  212. *
  213. * These, in conjunction with the output function are the ONLY way for output to be produced
  214. * within the pdf 'file'.
  215. */
  216. /**
  217. *destination object, used to specify the location for the user to jump to, presently on opening
  218. */
  219. function o_destination($id,$action,$options=''){
  220. if ($action!='new'){
  221. $o =& $this->objects[$id];
  222. }
  223. switch($action){
  224. case 'new':
  225. $this->objects[$id]=array('t'=>'destination','info'=>array());
  226. $tmp = '';
  227. switch ($options['type']){
  228. case 'XYZ':
  229. case 'FitR':
  230. $tmp = ' '.$options['p3'].$tmp;
  231. case 'FitH':
  232. case 'FitV':
  233. case 'FitBH':
  234. case 'FitBV':
  235. $tmp = ' '.$options['p1'].' '.$options['p2'].$tmp;
  236. case 'Fit':
  237. case 'FitB':
  238. $tmp = $options['type'].$tmp;
  239. $this->objects[$id]['info']['string']=$tmp;
  240. $this->objects[$id]['info']['page']=$options['page'];
  241. }
  242. break;
  243. case 'out':
  244. $tmp = $o['info'];
  245. $res="\n".$id." 0 obj\n".'['.$tmp['page'].' 0 R /'.$tmp['string']."]\nendobj\n";
  246. return $res;
  247. break;
  248. }
  249. }
  250. /**
  251. * set the viewer preferences
  252. */
  253. function o_viewerPreferences($id,$action,$options=''){
  254. if ($action!='new'){
  255. $o =& $this->objects[$id];
  256. }
  257. switch ($action){
  258. case 'new':
  259. $this->objects[$id]=array('t'=>'viewerPreferences','info'=>array());
  260. break;
  261. case 'add':
  262. foreach($options as $k=>$v){
  263. switch ($k){
  264. case 'HideToolbar':
  265. case 'HideMenubar':
  266. case 'HideWindowUI':
  267. case 'FitWindow':
  268. case 'CenterWindow':
  269. case 'NonFullScreenPageMode':
  270. case 'Direction':
  271. $o['info'][$k]=$v;
  272. break;
  273. }
  274. }
  275. break;
  276. case 'out':
  277. $res="\n".$id." 0 obj\n".'<< ';
  278. foreach($o['info'] as $k=>$v){
  279. $res.="\n/".$k.' '.$v;
  280. }
  281. $res.="\n>>\n";
  282. return $res;
  283. break;
  284. }
  285. }
  286. /**
  287. * define the document catalog, the overall controller for the document
  288. */
  289. function o_catalog($id,$action,$options=''){
  290. if ($action!='new'){
  291. $o =& $this->objects[$id];
  292. }
  293. switch ($action){
  294. case 'new':
  295. $this->objects[$id]=array('t'=>'catalog','info'=>array());
  296. $this->catalogId=$id;
  297. break;
  298. case 'outlines':
  299. case 'pages':
  300. case 'openHere':
  301. $o['info'][$action]=$options;
  302. break;
  303. case 'viewerPreferences':
  304. if (!isset($o['info']['viewerPreferences'])){
  305. $this->numObj++;
  306. $this->o_viewerPreferences($this->numObj,'new');
  307. $o['info']['viewerPreferences']=$this->numObj;
  308. }
  309. $vp = $o['info']['viewerPreferences'];
  310. $this->o_viewerPreferences($vp,'add',$options);
  311. break;
  312. case 'out':
  313. $res="\n".$id." 0 obj\n".'<< /Type /Catalog';
  314. foreach($o['info'] as $k=>$v){
  315. switch($k){
  316. case 'outlines':
  317. $res.="\n".'/Outlines '.$v.' 0 R';
  318. break;
  319. case 'pages':
  320. $res.="\n".'/Pages '.$v.' 0 R';
  321. break;
  322. case 'viewerPreferences':
  323. $res.="\n".'/ViewerPreferences '.$o['info']['viewerPreferences'].' 0 R';
  324. break;
  325. case 'openHere':
  326. $res.="\n".'/OpenAction '.$o['info']['openHere'].' 0 R';
  327. break;
  328. }
  329. }
  330. $res.=" >>\nendobj";
  331. return $res;
  332. break;
  333. }
  334. }
  335. /**
  336. * object which is a parent to the pages in the document
  337. */
  338. function o_pages($id,$action,$options=''){
  339. if ($action!='new'){
  340. $o =& $this->objects[$id];
  341. }
  342. switch ($action){
  343. case 'new':
  344. $this->objects[$id]=array('t'=>'pages','info'=>array());
  345. $this->o_catalog($this->catalogId,'pages',$id);
  346. break;
  347. case 'page':
  348. if (!is_array($options)){
  349. // then it will just be the id of the new page
  350. $o['info']['pages'][]=$options;
  351. } else {
  352. // then it should be an array having 'id','rid','pos', where rid=the page to which this one will be placed relative
  353. // and pos is either 'before' or 'after', saying where this page will fit.
  354. if (isset($options['id']) && isset($options['rid']) && isset($options['pos'])){
  355. $i = array_search($options['rid'],$o['info']['pages']);
  356. if (isset($o['info']['pages'][$i]) && $o['info']['pages'][$i]==$options['rid']){
  357. // then there is a match
  358. // make a space
  359. switch ($options['pos']){
  360. case 'before':
  361. $k = $i;
  362. break;
  363. case 'after':
  364. $k=$i+1;
  365. break;
  366. default:
  367. $k=-1;
  368. break;
  369. }
  370. if ($k>=0){
  371. for ($j=count($o['info']['pages'])-1;$j>=$k;$j--){
  372. $o['info']['pages'][$j+1]=$o['info']['pages'][$j];
  373. }
  374. $o['info']['pages'][$k]=$options['id'];
  375. }
  376. }
  377. }
  378. }
  379. break;
  380. case 'procset':
  381. $o['info']['procset']=$options;
  382. break;
  383. case 'mediaBox':
  384. $o['info']['mediaBox']=$options; // which should be an array of 4 numbers
  385. break;
  386. case 'font':
  387. $o['info']['fonts'][]=array('objNum'=>$options['objNum'],'fontNum'=>$options['fontNum']);
  388. break;
  389. case 'xObject':
  390. $o['info']['xObjects'][]=array('objNum'=>$options['objNum'],'label'=>$options['label']);
  391. break;
  392. case 'out':
  393. if (count($o['info']['pages'])){
  394. $res="\n".$id." 0 obj\n<< /Type /Pages\n/Kids [";
  395. foreach($o['info']['pages'] as $k=>$v){
  396. $res.=$v." 0 R\n";
  397. }
  398. $res.="]\n/Count ".count($this->objects[$id]['info']['pages']);
  399. if ((isset($o['info']['fonts']) && count($o['info']['fonts'])) || isset($o['info']['procset'])){
  400. $res.="\n/Resources <<";
  401. if (isset($o['info']['procset'])){
  402. $res.="\n/ProcSet ".$o['info']['procset']." 0 R";
  403. }
  404. if (isset($o['info']['fonts']) && count($o['info']['fonts'])){
  405. $res.="\n/Font << ";
  406. foreach($o['info']['fonts'] as $finfo){
  407. $res.="\n/F".$finfo['fontNum']." ".$finfo['objNum']." 0 R";
  408. }
  409. $res.=" >>";
  410. }
  411. if (isset($o['info']['xObjects']) && count($o['info']['xObjects'])){
  412. $res.="\n/XObject << ";
  413. foreach($o['info']['xObjects'] as $finfo){
  414. $res.="\n/".$finfo['label']." ".$finfo['objNum']." 0 R";
  415. }
  416. $res.=" >>";
  417. }
  418. $res.="\n>>";
  419. if (isset($o['info']['mediaBox'])){
  420. $tmp=$o['info']['mediaBox'];
  421. $res.="\n/MediaBox [".sprintf('%.3f',$tmp[0]).' '.sprintf('%.3f',$tmp[1]).' '.sprintf('%.3f',$tmp[2]).' '.sprintf('%.3f',$tmp[3]).']';
  422. }
  423. }
  424. $res.="\n >>\nendobj";
  425. } else {
  426. $res="\n".$id." 0 obj\n<< /Type /Pages\n/Count 0\n>>\nendobj";
  427. }
  428. return $res;
  429. break;
  430. }
  431. }
  432. /**
  433. * define the outlines in the doc, empty for now
  434. */
  435. function o_outlines($id,$action,$options=''){
  436. if ($action!='new'){
  437. $o =& $this->objects[$id];
  438. }
  439. switch ($action){
  440. case 'new':
  441. $this->objects[$id]=array('t'=>'outlines','info'=>array('outlines'=>array()));
  442. $this->o_catalog($this->catalogId,'outlines',$id);
  443. break;
  444. case 'outline':
  445. $o['info']['outlines'][]=$options;
  446. break;
  447. case 'out':
  448. if (count($o['info']['outlines'])){
  449. $res="\n".$id." 0 obj\n<< /Type /Outlines /Kids [";
  450. foreach($o['info']['outlines'] as $k=>$v){
  451. $res.=$v." 0 R ";
  452. }
  453. $res.="] /Count ".count($o['info']['outlines'])." >>\nendobj";
  454. } else {
  455. $res="\n".$id." 0 obj\n<< /Type /Outlines /Count 0 >>\nendobj";
  456. }
  457. return $res;
  458. break;
  459. }
  460. }
  461. /**
  462. * an object to hold the font description
  463. */
  464. function o_font($id,$action,$options=''){
  465. if ($action!='new'){
  466. $o =& $this->objects[$id];
  467. }
  468. switch ($action){
  469. case 'new':
  470. $this->objects[$id]=array('t'=>'font','info'=>array('name'=>$options['name'],'SubType'=>'Type1'));
  471. $fontNum=$this->numFonts;
  472. $this->objects[$id]['info']['fontNum']=$fontNum;
  473. // deal with the encoding and the differences
  474. if (isset($options['differences'])){
  475. // then we'll need an encoding dictionary
  476. $this->numObj++;
  477. $this->o_fontEncoding($this->numObj,'new',$options);
  478. $this->objects[$id]['info']['encodingDictionary']=$this->numObj;
  479. } else if (isset($options['encoding'])){
  480. // we can specify encoding here
  481. switch($options['encoding']){
  482. case 'WinAnsiEncoding':
  483. case 'MacRomanEncoding':
  484. case 'MacExpertEncoding':
  485. $this->objects[$id]['info']['encoding']=$options['encoding'];
  486. break;
  487. case 'none':
  488. break;
  489. default:
  490. $this->objects[$id]['info']['encoding']='WinAnsiEncoding';
  491. break;
  492. }
  493. } else {
  494. $this->objects[$id]['info']['encoding']='WinAnsiEncoding';
  495. }
  496. // also tell the pages node about the new font
  497. $this->o_pages($this->currentNode,'font',array('fontNum'=>$fontNum,'objNum'=>$id));
  498. break;
  499. case 'add':
  500. foreach ($options as $k=>$v){
  501. switch ($k){
  502. case 'BaseFont':
  503. $o['info']['name'] = $v;
  504. break;
  505. case 'FirstChar':
  506. case 'LastChar':
  507. case 'Widths':
  508. case 'FontDescriptor':
  509. case 'SubType':
  510. $this->addMessage('o_font '.$k." : ".$v);
  511. $o['info'][$k] = $v;
  512. break;
  513. }
  514. }
  515. break;
  516. case 'out':
  517. $res="\n".$id." 0 obj\n<< /Type /Font\n/Subtype /".$o['info']['SubType']."\n";
  518. $res.="/Name /F".$o['info']['fontNum']."\n";
  519. $res.="/BaseFont /".$o['info']['name']."\n";
  520. if (isset($o['info']['encodingDictionary'])){
  521. // then place a reference to the dictionary
  522. $res.="/Encoding ".$o['info']['encodingDictionary']." 0 R\n";
  523. } else if (isset($o['info']['encoding'])){
  524. // use the specified encoding
  525. $res.="/Encoding /".$o['info']['encoding']."\n";
  526. }
  527. if (isset($o['info']['FirstChar'])){
  528. $res.="/FirstChar ".$o['info']['FirstChar']."\n";
  529. }
  530. if (isset($o['info']['LastChar'])){
  531. $res.="/LastChar ".$o['info']['LastChar']."\n";
  532. }
  533. if (isset($o['info']['Widths'])){
  534. $res.="/Widths ".$o['info']['Widths']." 0 R\n";
  535. }
  536. if (isset($o['info']['FontDescriptor'])){
  537. $res.="/FontDescriptor ".$o['info']['FontDescriptor']." 0 R\n";
  538. }
  539. $res.=">>\nendobj";
  540. return $res;
  541. break;
  542. }
  543. }
  544. /**
  545. * a font descriptor, needed for including additional fonts
  546. */
  547. function o_fontDescriptor($id,$action,$options=''){
  548. if ($action!='new'){
  549. $o =& $this->objects[$id];
  550. }
  551. switch ($action){
  552. case 'new':
  553. $this->objects[$id]=array('t'=>'fontDescriptor','info'=>$options);
  554. break;
  555. case 'out':
  556. $res="\n".$id." 0 obj\n<< /Type /FontDescriptor\n";
  557. foreach ($o['info'] as $label => $value){
  558. switch ($label){
  559. case 'Ascent':
  560. case 'CapHeight':
  561. case 'Descent':
  562. case 'Flags':
  563. case 'ItalicAngle':
  564. case 'StemV':
  565. case 'AvgWidth':
  566. case 'Leading':
  567. case 'MaxWidth':
  568. case 'MissingWidth':
  569. case 'StemH':
  570. case 'XHeight':
  571. case 'CharSet':
  572. if (strlen($value)){
  573. $res.='/'.$label.' '.$value."\n";
  574. }
  575. break;
  576. case 'FontFile':
  577. case 'FontFile2':
  578. case 'FontFile3':
  579. $res.='/'.$label.' '.$value." 0 R\n";
  580. break;
  581. case 'FontBBox':
  582. $res.='/'.$label.' ['.$value[0].' '.$value[1].' '.$value[2].' '.$value[3]."]\n";
  583. break;
  584. case 'FontName':
  585. $res.='/'.$label.' /'.$value."\n";
  586. break;
  587. }
  588. }
  589. $res.=">>\nendobj";
  590. return $res;
  591. break;
  592. }
  593. }
  594. /**
  595. * the font encoding
  596. */
  597. function o_fontEncoding($id,$action,$options=''){
  598. if ($action!='new'){
  599. $o =& $this->objects[$id];
  600. }
  601. switch ($action){
  602. case 'new':
  603. // the options array should contain 'differences' and maybe 'encoding'
  604. $this->objects[$id]=array('t'=>'fontEncoding','info'=>$options);
  605. break;
  606. case 'out':
  607. $res="\n".$id." 0 obj\n<< /Type /Encoding\n";
  608. if (!isset($o['info']['encoding'])){
  609. $o['info']['encoding']='WinAnsiEncoding';
  610. }
  611. if ($o['info']['encoding']!='none'){
  612. $res.="/BaseEncoding /".$o['info']['encoding']."\n";
  613. }
  614. $res.="/Differences \n[";
  615. $onum=-100;
  616. foreach($o['info']['differences'] as $num=>$label){
  617. if ($num!=$onum+1){
  618. // we cannot make use of consecutive numbering
  619. $res.= "\n".$num." /".$label;
  620. } else {
  621. $res.= " /".$label;
  622. }
  623. $onum=$num;
  624. }
  625. $res.="\n]\n>>\nendobj";
  626. return $res;
  627. break;
  628. }
  629. }
  630. /**
  631. * the document procset, solves some problems with printing to old PS printers
  632. */
  633. function o_procset($id,$action,$options=''){
  634. if ($action!='new'){
  635. $o =& $this->objects[$id];
  636. }
  637. switch ($action){
  638. case 'new':
  639. $this->objects[$id]=array('t'=>'procset','info'=>array('PDF'=>1,'Text'=>1));
  640. $this->o_pages($this->currentNode,'procset',$id);
  641. $this->procsetObjectId=$id;
  642. break;
  643. case 'add':
  644. // this is to add new items to the procset list, despite the fact that this is considered
  645. // obselete, the items are required for printing to some postscript printers
  646. switch ($options) {
  647. case 'ImageB':
  648. case 'ImageC':
  649. case 'ImageI':
  650. $o['info'][$options]=1;
  651. break;
  652. }
  653. break;
  654. case 'out':
  655. $res="\n".$id." 0 obj\n[";
  656. foreach ($o['info'] as $label=>$val){
  657. $res.='/'.$label.' ';
  658. }
  659. $res.="]\nendobj";
  660. return $res;
  661. break;
  662. }
  663. }
  664. /**
  665. * define the document information
  666. */
  667. function o_info($id,$action,$options=''){
  668. if ($action!='new'){
  669. $o =& $this->objects[$id];
  670. }
  671. switch ($action){
  672. case 'new':
  673. $this->infoObject=$id;
  674. $date='D:'.date('Ymd');
  675. $this->objects[$id]=array('t'=>'info','info'=>array('Creator'=>'R and OS php pdf writer, http://www.ros.co.nz','CreationDate'=>$date));
  676. break;
  677. case 'Title':
  678. case 'Author':
  679. case 'Subject':
  680. case 'Keywords':
  681. case 'Creator':
  682. case 'Producer':
  683. case 'CreationDate':
  684. case 'ModDate':
  685. case 'Trapped':
  686. $o['info'][$action]=$options;
  687. break;
  688. case 'out':
  689. if ($this->encrypted){
  690. $this->encryptInit($id);
  691. }
  692. $res="\n".$id." 0 obj\n<<\n";
  693. foreach ($o['info'] as $k=>$v){
  694. $res.='/'.$k.' (';
  695. if ($this->encrypted){
  696. $res.=$this->filterText($this->ARC4($v));
  697. } else {
  698. $res.=$this->filterText($v);
  699. }
  700. $res.=")\n";
  701. }
  702. $res.=">>\nendobj";
  703. return $res;
  704. break;
  705. }
  706. }
  707. /**
  708. * an action object, used to link to URLS initially
  709. */
  710. function o_action($id,$action,$options=''){
  711. if ($action!='new'){
  712. $o =& $this->objects[$id];
  713. }
  714. switch ($action){
  715. case 'new':
  716. if (is_array($options)){
  717. $this->objects[$id]=array('t'=>'action','info'=>$options,'type'=>$options['type']);
  718. } else {
  719. // then assume a URI action
  720. $this->objects[$id]=array('t'=>'action','info'=>$options,'type'=>'URI');
  721. }
  722. break;
  723. case 'out':
  724. if ($this->encrypted){
  725. $this->encryptInit($id);
  726. }
  727. $res="\n".$id." 0 obj\n<< /Type /Action";
  728. switch($o['type']){
  729. case 'ilink':
  730. // there will be an 'label' setting, this is the name of the destination
  731. $res.="\n/S /GoTo\n/D ".$this->destinations[(string)$o['info']['label']]." 0 R";
  732. break;
  733. case 'URI':
  734. $res.="\n/S /URI\n/URI (";
  735. if ($this->encrypted){
  736. $res.=$this->filterText($this->ARC4($o['info']));
  737. } else {
  738. $res.=$this->filterText($o['info']);
  739. }
  740. $res.=")";
  741. break;
  742. }
  743. $res.="\n>>\nendobj";
  744. return $res;
  745. break;
  746. }
  747. }
  748. /**
  749. * an annotation object, this will add an annotation to the current page.
  750. * initially will support just link annotations
  751. */
  752. function o_annotation($id,$action,$options=''){
  753. if ($action!='new'){
  754. $o =& $this->objects[$id];
  755. }
  756. switch ($action){
  757. case 'new':
  758. // add the annotation to the current page
  759. $pageId = $this->currentPage;
  760. $this->o_page($pageId,'annot',$id);
  761. // and add the action object which is going to be required
  762. switch($options['type']){
  763. case 'link':
  764. $this->objects[$id]=array('t'=>'annotation','info'=>$options);
  765. $this->numObj++;
  766. $this->o_action($this->numObj,'new',$options['url']);
  767. $this->objects[$id]['info']['actionId']=$this->numObj;
  768. break;
  769. case 'ilink':
  770. // this is to a named internal link
  771. $label = $options['label'];
  772. $this->objects[$id]=array('t'=>'annotation','info'=>$options);
  773. $this->numObj++;
  774. $this->o_action($this->numObj,'new',array('type'=>'ilink','label'=>$label));
  775. $this->objects[$id]['info']['actionId']=$this->numObj;
  776. break;
  777. }
  778. break;
  779. case 'out':
  780. $res="\n".$id." 0 obj\n<< /Type /Annot";
  781. switch($o['info']['type']){
  782. case 'link':
  783. case 'ilink':
  784. $res.= "\n/Subtype /Link";
  785. break;
  786. }
  787. $res.="\n/A ".$o['info']['actionId']." 0 R";
  788. $res.="\n/Border [0 0 0]";
  789. $res.="\n/H /I";
  790. $res.="\n/Rect [ ";
  791. foreach($o['info']['rect'] as $v){
  792. $res.= sprintf("%.4f ",$v);
  793. }
  794. $res.="]";
  795. $res.="\n>>\nendobj";
  796. return $res;
  797. break;
  798. }
  799. }
  800. /**
  801. * a page object, it also creates a contents object to hold its contents
  802. */
  803. function o_page($id,$action,$options=''){
  804. if ($action!='new'){
  805. $o =& $this->objects[$id];
  806. }
  807. switch ($action){
  808. case 'new':
  809. $this->numPages++;
  810. $this->objects[$id]=array('t'=>'page','info'=>array('parent'=>$this->currentNode,'pageNum'=>$this->numPages));
  811. if (is_array($options)){
  812. // then this must be a page insertion, array shoudl contain 'rid','pos'=[before|after]
  813. $options['id']=$id;
  814. $this->o_pages($this->currentNode,'page',$options);
  815. } else {
  816. $this->o_pages($this->currentNode,'page',$id);
  817. }
  818. $this->currentPage=$id;
  819. //make a contents object to go with this page
  820. $this->numObj++;
  821. $this->o_contents($this->numObj,'new',$id);
  822. $this->currentContents=$this->numObj;
  823. $this->objects[$id]['info']['contents']=array();
  824. $this->objects[$id]['info']['contents'][]=$this->numObj;
  825. $match = ($this->numPages%2 ? 'odd' : 'even');
  826. foreach($this->addLooseObjects as $oId=>$target){
  827. if ($target=='all' || $match==$target){
  828. $this->objects[$id]['info']['contents'][]=$oId;
  829. }
  830. }
  831. break;
  832. case 'content':
  833. $o['info']['contents'][]=$options;
  834. break;
  835. case 'annot':
  836. // add an annotation to this page
  837. if (!isset($o['info']['annot'])){
  838. $o['info']['annot']=array();
  839. }
  840. // $options should contain the id of the annotation dictionary
  841. $o['info']['annot'][]=$options;
  842. break;
  843. case 'out':
  844. $res="\n".$id." 0 obj\n<< /Type /Page";
  845. $res.="\n/Parent ".$o['info']['parent']." 0 R";
  846. if (isset($o['info']['annot'])){
  847. $res.="\n/Annots [";
  848. foreach($o['info']['annot'] as $aId){
  849. $res.=" ".$aId." 0 R";
  850. }
  851. $res.=" ]";
  852. }
  853. $count = count($o['info']['contents']);
  854. if ($count==1){
  855. $res.="\n/Contents ".$o['info']['contents'][0]." 0 R";
  856. } else if ($count>1){
  857. $res.="\n/Contents [\n";
  858. foreach ($o['info']['contents'] as $cId){
  859. $res.=$cId." 0 R\n";
  860. }
  861. $res.="]";
  862. }
  863. $res.="\n>>\nendobj";
  864. return $res;
  865. break;
  866. }
  867. }
  868. /**
  869. * the contents objects hold all of the content which appears on pages
  870. */
  871. function o_contents($id,$action,$options=''){
  872. if ($action!='new'){
  873. $o =& $this->objects[$id];
  874. }
  875. switch ($action){
  876. case 'new':
  877. $this->objects[$id]=array('t'=>'contents','c'=>'','info'=>array());
  878. if (strlen($options) && intval($options)){
  879. // then this contents is the primary for a page
  880. $this->objects[$id]['onPage']=$options;
  881. } else if ($options=='raw'){
  882. // then this page contains some other type of system object
  883. $this->objects[$id]['raw']=1;
  884. }
  885. break;
  886. case 'add':
  887. // add more options to the decleration
  888. foreach ($options as $k=>$v){
  889. $o['info'][$k]=$v;
  890. }
  891. case 'out':
  892. $tmp=$o['c'];
  893. $res= "\n".$id." 0 obj\n";
  894. if (isset($this->objects[$id]['raw'])){
  895. $res.=$tmp;
  896. } else {
  897. $res.= "<<";
  898. if (function_exists('gzcompress') && $this->options['compression']){
  899. // then implement ZLIB based compression on this content stream
  900. $res.=" /Filter /FlateDecode";
  901. $tmp = gzcompress($tmp);
  902. }
  903. if ($this->encrypted){
  904. $this->encryptInit($id);
  905. $tmp = $this->ARC4($tmp);
  906. }
  907. foreach($o['info'] as $k=>$v){
  908. $res .= "\n/".$k.' '.$v;
  909. }
  910. $res.="\n/Length ".strlen($tmp)." >>\nstream\n".$tmp."\nendstream";
  911. }
  912. $res.="\nendobj\n";
  913. return $res;
  914. break;
  915. }
  916. }
  917. /**
  918. * an image object, will be an XObject in the document, includes description and data
  919. */
  920. function o_image($id,$action,$options=''){
  921. if ($action!='new'){
  922. $o =& $this->objects[$id];
  923. }
  924. switch($action){
  925. case 'new':
  926. // make the new object
  927. $this->objects[$id]=array('t'=>'image','data'=>$options['data'],'info'=>array());
  928. $this->objects[$id]['info']['Type']='/XObject';
  929. $this->objects[$id]['info']['Subtype']='/Image';
  930. $this->objects[$id]['info']['Width']=$options['iw'];
  931. $this->objects[$id]['info']['Height']=$options['ih'];
  932. if (!isset($options['type']) || $options['type']=='jpg'){
  933. if (!isset($options['channels'])){
  934. $options['channels']=3;
  935. }
  936. switch($options['channels']){
  937. case 1:
  938. $this->objects[$id]['info']['ColorSpace']='/DeviceGray';
  939. break;
  940. default:
  941. $this->objects[$id]['info']['ColorSpace']='/DeviceRGB';
  942. break;
  943. }
  944. $this->objects[$id]['info']['Filter']='/DCTDecode';
  945. $this->objects[$id]['info']['BitsPerComponent']=8;
  946. } else if ($options['type']=='png'){
  947. $this->objects[$id]['info']['Filter']='/FlateDecode';
  948. $this->objects[$id]['info']['DecodeParms']='<< /Predictor 15 /Colors '.$options['ncolor'].' /Columns '.$options['iw'].' /BitsPerComponent '.$options['bitsPerComponent'].'>>';
  949. if (strlen($options['pdata'])){
  950. $tmp = ' [ /Indexed /DeviceRGB '.(strlen($options['pdata'])/3-1).' ';
  951. $this->numObj++;
  952. $this->o_contents($this->numObj,'new');
  953. $this->objects[$this->numObj]['c']=$options['pdata'];
  954. $tmp.=$this->numObj.' 0 R';
  955. $tmp .=' ]';
  956. $this->objects[$id]['info']['ColorSpace'] = $tmp;
  957. if (isset($options['transparency'])){
  958. switch($options['transparency']['type']){
  959. case 'indexed':
  960. $tmp=' [ '.$options['transparency']['data'].' '.$options['transparency']['data'].'] ';
  961. $this->objects[$id]['info']['Mask'] = $tmp;
  962. break;
  963. }
  964. }
  965. } else {
  966. $this->objects[$id]['info']['ColorSpace']='/'.$options['color'];
  967. }
  968. $this->objects[$id]['info']['BitsPerComponent']=$options['bitsPerComponent'];
  969. }
  970. // assign it a place in the named resource dictionary as an external object, according to
  971. // the label passed in with it.
  972. $this->o_pages($this->currentNode,'xObject',array('label'=>$options['label'],'objNum'=>$id));
  973. // also make sure that we have the right procset object for it.
  974. $this->o_procset($this->procsetObjectId,'add','ImageC');
  975. break;
  976. case 'out':
  977. $tmp=$o['data'];
  978. $res= "\n".$id." 0 obj\n<<";
  979. foreach($o['info'] as $k=>$v){
  980. $res.="\n/".$k.' '.$v;
  981. }
  982. if ($this->encrypted){
  983. $this->encryptInit($id);
  984. $tmp = $this->ARC4($tmp);
  985. }
  986. $res.="\n/Length ".strlen($tmp)." >>\nstream\n".$tmp."\nendstream\nendobj\n";
  987. return $res;
  988. break;
  989. }
  990. }
  991. /**
  992. * encryption object.
  993. */
  994. function o_encryption($id,$action,$options=''){
  995. if ($action!='new'){
  996. $o =& $this->objects[$id];
  997. }
  998. switch($action){
  999. case 'new':
  1000. // make the new object
  1001. $this->objects[$id]=array('t'=>'encryption','info'=>$options);
  1002. $this->arc4_objnum=$id;
  1003. // figure out the additional paramaters required
  1004. $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);
  1005. $len = strlen($options['owner']);
  1006. if ($len>32){
  1007. $owner = substr($options['owner'],0,32);
  1008. } else if ($len<32){
  1009. $owner = $options['owner'].substr($pad,0,32-$len);
  1010. } else {
  1011. $owner = $options['owner'];
  1012. }
  1013. $len = strlen($options['user']);
  1014. if ($len>32){
  1015. $user = substr($options['user'],0,32);
  1016. } else if ($len<32){
  1017. $user = $options['user'].substr($pad,0,32-$len);
  1018. } else {
  1019. $user = $options['user'];
  1020. }
  1021. $tmp = $this->md5_16($owner);
  1022. $okey = substr($tmp,0,5);
  1023. $this->ARC4_init($okey);
  1024. $ovalue=$this->ARC4($user);
  1025. $this->objects[$id]['info']['O']=$ovalue;
  1026. // now make the u value, phew.
  1027. $tmp = $this->md5_16($user.$ovalue.chr($options['p']).chr(255).chr(255).chr(255).$this->fileIdentifier);
  1028. $ukey = substr($tmp,0,5);
  1029. $this->ARC4_init($ukey);
  1030. $this->encryptionKey = $ukey;
  1031. $this->encrypted=1;
  1032. $uvalue=$this->ARC4($pad);
  1033. $this->objects[$id]['info']['U']=$uvalue;
  1034. $this->encryptionKey=$ukey;
  1035. // initialize the arc4 array
  1036. break;
  1037. case 'out':
  1038. $res= "\n".$id." 0 obj\n<<";
  1039. $res.="\n/Filter /Standard";
  1040. $res.="\n/V 1";
  1041. $res.="\n/R 2";
  1042. $res.="\n/O (".$this->filterText($o['info']['O']).')';
  1043. $res.="\n/U (".$this->filterText($o['info']['U']).')';
  1044. // and the p-value needs to be converted to account for the twos-complement approach
  1045. $o['info']['p'] = (($o['info']['p']^255)+1)*-1;
  1046. $res.="\n/P ".($o['info']['p']);
  1047. $res.="\n>>\nendobj\n";
  1048. return $res;
  1049. break;
  1050. }
  1051. }
  1052. /**
  1053. * ARC4 functions
  1054. * A series of function to implement ARC4 encoding in PHP
  1055. */
  1056. /**
  1057. * calculate the 16 byte version of the 128 bit md5 digest of the string
  1058. */
  1059. function md5_16($string){
  1060. $tmp = md5($string);
  1061. $out='';
  1062. for ($i=0;$i<=30;$i=$i+2){
  1063. $out.=chr(hexdec(substr($tmp,$i,2)));
  1064. }
  1065. return $out;
  1066. }
  1067. /**
  1068. * initialize the encryption for processing a particular object
  1069. */
  1070. function encryptInit($id){
  1071. $tmp = $this->encryptionKey;
  1072. $hex = dechex($id);
  1073. if (strlen($hex)<6){
  1074. $hex = substr('000000',0,6-strlen($hex)).$hex;
  1075. }
  1076. $tmp.= chr(hexdec(substr($hex,4,2))).chr(hexdec(substr($hex,2,2))).chr(hexdec(substr($hex,0,2))).chr(0).chr(0);
  1077. $key = $this->md5_16($tmp);
  1078. $this->ARC4_init(substr($key,0,10));
  1079. }
  1080. /**
  1081. * initialize the ARC4 encryption
  1082. */
  1083. function ARC4_init($key=''){
  1084. $this->arc4 = '';
  1085. // setup the control array
  1086. if (strlen($key)==0){
  1087. return;
  1088. }
  1089. $k = '';
  1090. while(strlen($k)<256){
  1091. $k.=$key;
  1092. }
  1093. $k=substr($k,0,256);
  1094. for ($i=0;$i<256;$i++){
  1095. $this->arc4 .= chr($i);
  1096. }
  1097. $j=0;
  1098. for ($i=0;$i<256;$i++){
  1099. $t = $this->arc4[$i];
  1100. $j = ($j + ord($t) + ord($k[$i]))%256;
  1101. $this->arc4[$i]=$this->arc4[$j];
  1102. $this->arc4[$j]=$t;
  1103. }
  1104. }
  1105. /**
  1106. * ARC4 encrypt a text string
  1107. */
  1108. function ARC4($text){
  1109. $len=strlen($text);
  1110. $a=0;
  1111. $b=0;
  1112. $c = $this->arc4;
  1113. $out='';
  1114. for ($i=0;$i<$len;$i++){
  1115. $a = ($a+1)%256;
  1116. $t= $c[$a];
  1117. $b = ($b+ord($t))%256;
  1118. $c[$a]=$c[$b];
  1119. $c[$b]=$t;
  1120. $k = ord($c[(ord($c[$a])+ord($c[$b]))%256]);
  1121. $out.=chr(ord($text[$i]) ^ $k);
  1122. }
  1123. return $out;
  1124. }
  1125. /**
  1126. * functions which can be called to adjust or add to the document
  1127. */
  1128. /**
  1129. * add a link in the document to an external URL
  1130. */
  1131. function addLink($url,$x0,$y0,$x1,$y1){
  1132. $this->numObj++;
  1133. $info = array('type'=>'link','url'=>$url,'rect'=>array($x0,$y0,$x1,$y1));
  1134. $this->o_annotation($this->numObj,'new',$info);
  1135. }
  1136. /**
  1137. * add a link in the document to an internal destination (ie. within the document)
  1138. */
  1139. function addInternalLink($label,$x0,$y0,$x1,$y1){
  1140. $this->numObj++;
  1141. $info = array('type'=>'ilink','label'=>$label,'rect'=>array($x0,$y0,$x1,$y1));
  1142. $this->o_annotation($this->numObj,'new',$info);
  1143. }
  1144. /**
  1145. * set the encryption of the document
  1146. * can be used to turn it on and/or set the passwords which it will have.
  1147. * also the functions that the user will have are set here, such as print, modify, add
  1148. */
  1149. function setEncryption($userPass='',$ownerPass='',$pc=array()){
  1150. $p=bindec(11000000);
  1151. $options = array(
  1152. 'print'=>4
  1153. ,'modify'=>8
  1154. ,'copy'=>16
  1155. ,'add'=>32
  1156. );
  1157. foreach($pc as $k=>$v){
  1158. if ($v && isset($options[$k])){
  1159. $p+=$options[$k];
  1160. } else if (isset($options[$v])){
  1161. $p+=$options[$v];
  1162. }
  1163. }
  1164. // implement encryption on the document
  1165. if ($this->arc4_objnum == 0){
  1166. // then the block does not exist already, add it.
  1167. $this->numObj++;
  1168. if (strlen($ownerPass)==0){
  1169. $ownerPass=$userPass;
  1170. }
  1171. $this->o_encryption($this->numObj,'new',array('user'=>$userPass,'owner'=>$ownerPass,'p'=>$p));
  1172. }
  1173. }
  1174. /**
  1175. * should be used for internal checks, not implemented as yet
  1176. */
  1177. function checkAllHere(){
  1178. }
  1179. /**
  1180. * return the pdf stream as a string returned from the function
  1181. */
  1182. function output($debug=0){
  1183. if ($debug){
  1184. // turn compression off
  1185. $this->options['compression']=0;
  1186. }
  1187. if ($this->arc4_objnum){
  1188. $this->ARC4_init($this->encryptionKey);
  1189. }
  1190. $this->checkAllHere();
  1191. $xref=array();
  1192. $content="%PDF-1.3\n%����\n";
  1193. // $content="%PDF-1.3\n";
  1194. $pos=strlen($content);
  1195. foreach($this->objects as $k=>$v){
  1196. $tmp='o_'.$v['t'];
  1197. $cont=$this->$tmp($k,'out');
  1198. $content.=$cont;
  1199. $xref[]=$pos;
  1200. $pos+=strlen($cont);
  1201. }
  1202. $content.="\nxref\n0 ".(count($xref)+1)."\n0000000000 65535 f \n";
  1203. foreach($xref as $p){
  1204. $content.=substr('0000000000',0,10-strlen($p)).$p." 00000 n \n";
  1205. }
  1206. $content.="\ntrailer\n << /Size ".(count($xref)+1)."\n /Root 1 0 R\n /Info ".$this->infoObject." 0 R\n";
  1207. // if encryption has been applied to this document then add the marker for this dictionary
  1208. if ($this->arc4_objnum > 0){
  1209. $content .= "/Encrypt ".$this->arc4_objnum." 0 R\n";
  1210. }
  1211. if (strlen($this->fileIdentifier)){
  1212. $content .= "/ID[<".$this->fileIdentifier."><".$this->fileIdentifier.">]\n";
  1213. }
  1214. $content .= " >>\nstartxref\n".$pos."\n%%EOF\n";
  1215. return $content;
  1216. }
  1217. /**
  1218. * intialize a new document
  1219. * if this is called on an existing document results may be unpredictable, but the existing document would be lost at minimum
  1220. * this function is called automatically by the constructor function
  1221. *
  1222. * @access private
  1223. */
  1224. function newDocument($pageSize=array(0,0,612,792)){
  1225. $this->numObj=0;
  1226. $this->objects = array();
  1227. $this->numObj++;
  1228. $this->o_catalog($this->numObj,'new');
  1229. $this->numObj++;
  1230. $this->o_outlines($this->numObj,'new');
  1231. $this->numObj++;
  1232. $this->o_pages($this->numObj,'new');
  1233. $this->o_pages($this->numObj,'mediaBox',$pageSize);
  1234. $this->currentNode = 3;
  1235. $this->numObj++;
  1236. $this->o_procset($this->numObj,'new');
  1237. $this->numObj++;
  1238. $this->o_info($this->numObj,'new');
  1239. $this->numObj++;
  1240. $this->o_page($this->numObj,'new');
  1241. // need to store the first page id as there is no way to get it to the user during
  1242. // startup
  1243. $this->firstPageId = $this->currentContents;
  1244. }
  1245. /**
  1246. * open the font file and return a php structure containing it.
  1247. * first check if this one has been done before and saved in a form more suited to php
  1248. * note that if a php serialized version does not exist it will try and make one, but will
  1249. * require write access to the directory to do it... it is MUCH faster to have these serialized
  1250. * files.
  1251. *
  1252. * @access private
  1253. */
  1254. function openFont($font){
  1255. // assume that $font contains both the path and perhaps the extension to the file, split them
  1256. $pos=strrpos($font,'/');
  1257. if ($pos===false){
  1258. $dir = './media/';
  1259. $name = $font;
  1260. } else {
  1261. //$dir=substr($font,0,$pos+1);
  1262. $dir="./media/";
  1263. $name=substr($font,$pos+1);
  1264. }
  1265. if (substr($name,-4)=='.afm'){
  1266. $name=substr($name,0,strlen($name)-4);
  1267. }
  1268. $this->addMessage('openFont: '.$font.' - '.$name);
  1269. if (file_exists($dir.'php_'.$name.'.afm')){
  1270. $this->addMessage('openFont: php file exists '.$dir.'php_'.$name.'.afm');
  1271. $tmp = file($dir.'php_'.$name.'.afm');
  1272. $this->fonts[$font]=unserialize($tmp[0]);
  1273. if (!isset($this->fonts[$font]['_version_']) || $this->fonts[$font]['_version_']<1){
  1274. // if the font file is old, then clear it out and prepare for re-creation
  1275. $this->addMessage('openFont: clear out, make way for new version.');
  1276. unset($this->fonts[$font]);
  1277. }
  1278. }
  1279. if (!isset($this->fonts[$font]) && file_exists($dir.$name.'.afm')){
  1280. // then rebuild the php_<font>.afm file from the <font>.afm file
  1281. $this->addMessage('openFont: build php file from '.$dir.$name.'.afm');
  1282. $data = array();
  1283. $file = file($dir.$name.'.afm');
  1284. foreach ($file as $rowA){
  1285. $row=trim($rowA);
  1286. $pos=strpos($row,' ');
  1287. if ($pos){
  1288. // then there must be some keyword
  1289. $key = substr($row,0,$pos);
  1290. switch ($key){
  1291. case 'FontName':
  1292. case 'FullName':
  1293. case 'FamilyName':
  1294. case 'Weight':
  1295. case 'ItalicAngle':
  1296. case 'IsFixedPitch':
  1297. case 'CharacterSet':
  1298. case 'UnderlinePosition':
  1299. case 'UnderlineThickness':
  1300. case 'Version':
  1301. case 'EncodingScheme':
  1302. case 'CapHeight':
  1303. case 'XHeight':
  1304. case 'Ascender':
  1305. case 'Descender':
  1306. case 'StdHW':
  1307. case 'StdVW':
  1308. case 'StartCharMetrics':
  1309. $data[$key]=trim(substr($row,$pos));
  1310. break;
  1311. case 'FontBBox':
  1312. $data[$key]=explode(' ',trim(substr($row,$pos)));
  1313. break;
  1314. case 'C':
  1315. //C 39 ; WX 222 ; N quoteright ; B 53 463 157 718 ;
  1316. $bits=explode(';',trim($row));
  1317. $dtmp=array();
  1318. foreach($bits as $bit){
  1319. $bits2 = explode(' ',trim($bit));
  1320. if (strlen($bits2[0])){
  1321. if (count($bits2)>2){
  1322. $dtmp[$bits2[0]]=array();
  1323. for ($i=1;$i<count($bits2);$i++){
  1324. $dtmp[$bits2[0]][]=$bits2[$i];
  1325. }
  1326. } else if (count($bits2)==2){
  1327. $dtmp[$bits2[0]]=$bits2[1];
  1328. }
  1329. }
  1330. }
  1331. if ($dtmp['C']>=0){
  1332. $data['C'][$dtmp['C']]=$dtmp;
  1333. $data['C'][$dtmp['N']]=$dtmp;
  1334. } else {
  1335. $data['C'][$dtmp['N']]=$dtmp;
  1336. }
  1337. break;
  1338. case 'KPX':
  1339. //KPX Adieresis yacute -40
  1340. $bits=explode(' ',trim($row));
  1341. $data['KPX'][$bits[1]][$bits[2]]=$bits[3];
  1342. break;
  1343. }
  1344. }
  1345. }
  1346. $data['_version_']=1;
  1347. $this->fonts[$font]=$data;
  1348. $fp = @fopen($dir.'php_'.$name.'.afm','w') or die ("Please make sure your \"media\" directory is writeable (CHMOD 777).");
  1349. fwrite($fp,serialize($data));
  1350. fclose($fp);
  1351. mosChmod($dir.'php_'.$name.'.afm');
  1352. } else if (!isset($this->fonts[$font])){
  1353. $this->addMessage('openFont: no font file found');
  1354. // echo 'Font not Found '.$font;
  1355. }
  1356. }
  1357. /**
  1358. * if the font is not loaded then load it and make the required object
  1359. * else just make it the current font
  1360. * the encoding array can contain 'encoding'=> 'none','WinAnsiEncoding','MacRomanEncoding' or 'MacExpertEncoding'
  1361. * note that encoding='none' will need to be used for symbolic fonts
  1362. * and 'differences' => an array of mappings between numbers 0->255 and character names.
  1363. *
  1364. */
  1365. function selectFont($fontName,$encoding='',$set=1){
  1366. if (!isset($this->fonts[$fontName])){
  1367. // load the file
  1368. $this->openFont($fontName);
  1369. if (isset($this->fonts[$fontName])){
  1370. $this->numObj++;
  1371. $this->numFonts++;
  1372. $pos=strrpos($fontName,'/');
  1373. // $dir=substr($fontName,0,$pos+1);
  1374. $name=substr($fontName,$pos+1);
  1375. if (substr($name,-4)=='.afm'){
  1376. $name=substr($name,0,strlen($name)-4);
  1377. }
  1378. $options=array('name'=>$name);
  1379. if (is_array($encoding)){
  1380. // then encoding and differences might be set
  1381. if (isset($encoding['encoding'])){
  1382. $options['encoding']=$encoding['encoding'];
  1383. }
  1384. if (isset($encoding['differences'])){
  1385. $options['differences']=$encoding['differences'];
  1386. }
  1387. } else if (strlen($encoding)){
  1388. // then perhaps only the encoding has been set
  1389. $options['encoding']=$encoding;
  1390. }
  1391. $fontObj = $this->numObj;
  1392. $this->o_font($this->numObj,'new',$options);
  1393. $this->fonts[$fontName]['fontNum']=$this->numFonts;
  1394. // if this is a '.afm' font, and there is a '.pfa' file to go with it ( as there
  1395. // should be for all non-basic fonts), then load it into an object and put the
  1396. // references into the font object
  1397. $basefile = substr($fontName,0,strlen($fontName)-4);
  1398. if (file_exists($basefile.'.pfb')){
  1399. $fbtype = 'pfb';
  1400. } else if (file_exists($basefile.'.ttf')){
  1401. $fbtype = 'ttf';
  1402. } else {
  1403. $fbtype='';
  1404. }
  1405. $fbfile = $basefile.'.'.$fbtype;
  1406. // $pfbfile = substr($fontName,0,strlen($fontName)-4).'.pfb';
  1407. // $ttffile = substr($fontName,0,strlen($fontName)-4).'.ttf';
  1408. $this->addMessage('selectFont: checking for - '.$fbfile);
  1409. if (substr($fontName,-4)=='.afm' && strlen($fbtype) ){
  1410. $adobeFontName = $this->fonts[$fontName]['FontName'];
  1411. // $fontObj = $this->numObj;
  1412. $this->addMessage('selectFont: adding font file - '.$fbfile.' - '.$adobeFontName);
  1413. // find the array of fond widths, and put that into an object.
  1414. $firstChar = -1;
  1415. $lastChar = 0;
  1416. $widths = array();
  1417. foreach ($this->fonts[$fontName]['C'] as $num=>$d){
  1418. if (intval($num)>0 || $num=='0'){
  1419. if ($lastChar>0 && $num>$lastChar+1){
  1420. for($i=$lastChar+1;$i<$num;$i++){
  1421. $widths[] = 0;
  1422. }
  1423. }
  1424. $widths[] = $d['WX'];
  1425. if ($firstChar==-1){
  1426. $firstChar = $num;
  1427. }
  1428. $lastChar = $num;
  1429. }
  1430. }
  1431. // also need to adjust the widths for the differences array
  1432. if (isset($options['differences'])){
  1433. foreach($options['differences'] as $charNum=>$charName){
  1434. if ($charNum>$lastChar){
  1435. for($i=$lastChar+1;$i<=$charNum;$i++){
  1436. $widths[]=0;
  1437. }
  1438. $lastChar=$charNum;
  1439. }
  1440. if (isset($this->fonts[$fontName]['C'][$charName])){
  1441. $widths[$charNum-$firstChar]=$this->fonts[$fontName]['C'][$charName]['WX'];
  1442. }
  1443. }
  1444. }
  1445. $this->addMessage('selectFont: FirstChar='.$firstChar);
  1446. $this->addMessage('selectFont: LastChar='.$lastChar);
  1447. $this->numObj++;
  1448. $this->o_contents($this->numObj,'new','raw');
  1449. $this->objects[$this->numObj]['c'].='[';
  1450. foreach($widths as $width){
  1451. $this->objects[$this->numObj]['c'].=' '.$width;
  1452. }
  1453. $this->objects[$this->numObj]['c'].=' ]';
  1454. $widthid = $this->numObj;
  1455. // load the pfb file, and put that into an object too.
  1456. // note that pdf supports only binary format type 1 font files, though there is a
  1457. // simple utility to convert them from pfa to pfb.
  1458. $fp = fopen($fbfile,'rb');
  1459. $tmp = get_magic_quotes_runtime();
  1460. set_magic_quotes_runtime(0);
  1461. $data = fread($fp,filesize($fbfile));
  1462. set_magic_quotes_runtime($tmp);
  1463. fclose($fp);
  1464. // create the font descriptor
  1465. $this->numObj++;
  1466. $fontDescriptorId = $this->numObj;
  1467. $this->numObj++;
  1468. $pfbid = $this->numObj;
  1469. // determine flags (more than a little flakey, hopefully will not matter much)
  1470. $flags=0;
  1471. if ($this->fonts[$fontName]['ItalicAngle']!=0){ $flags+=pow(2,6); }
  1472. if ($this->fonts[$fontName]['IsFixedPitch']=='true'){ $flags+=1; }
  1473. $flags+=pow(2,5); // assume non-sybolic
  1474. $list = array('Ascent'=>'Ascender','CapHeight'=>'CapHeight','Descent'=>'Descender','FontBBox'=>'FontBBox','ItalicAngle'=>'ItalicAngle');
  1475. $fdopt = array(
  1476. 'Flags'=>$flags
  1477. ,'FontName'=>$adobeFontName
  1478. ,'StemV'=>100 // don't know what the value for this should be!
  1479. );
  1480. foreach($list as $k=>$v){
  1481. if (isset($this->fonts[$fontName][$v])){
  1482. $fdopt[$k]=$this->fonts[$fontName][$v];
  1483. }
  1484. }
  1485. if ($fbtype=='pfb'){
  1486. $fdopt['FontFile']=$pfbid;
  1487. } else if ($fbtype=='ttf'){
  1488. $fdopt['FontFile2']=$pfbid;
  1489. }
  1490. $this->o_fontDescriptor($fontDescriptorId,'new',$fdopt);
  1491. // embed the font program
  1492. $this->o_contents($this->numObj,'new');
  1493. $this->objects[$pfbid]['c'].=$data;
  1494. // determine the cruicial lengths within this file
  1495. if ($fbtype=='pfb'){
  1496. $l1 = strpos($data,'eexec')+6;
  1497. $l2 = strpos($data,'00000000')-$l1;
  1498. $l3 = strlen($data)-$l2-$l1;
  1499. $this->o_contents($this->numObj,'add',array('Length1'=>$l1,'Length2'=>$l2,'Length3'=>$l3));
  1500. } else if ($fbtype=='ttf'){
  1501. $l1 = strlen($data);
  1502. $this->o_contents($this->numObj,'add',array('Length1'=>$l1));
  1503. }
  1504. // tell the font object about all this new stuff
  1505. $tmp = array('BaseFont'=>$adobeFontName,'Widths'=>$widthid
  1506. ,'FirstChar'=>$firstChar,'LastChar'=>$lastChar
  1507. ,'FontDescriptor'=>$fontDescriptorId);
  1508. if ($fbtype=='ttf'){
  1509. $tmp['SubType']='TrueType';
  1510. }
  1511. $this->addMessage('adding extra info to font.('.$fontObj.')');
  1512. foreach($tmp as $fk=>$fv){
  1513. $this->addMessage($fk." : ".$fv);
  1514. }
  1515. $this->o_font($fontObj,'add',$tmp);
  1516. } else {
  1517. $this->addMessage('selectFont: pfb or ttf file not found, ok if this is one of the 14 standard fonts');
  1518. }
  1519. // also set the differences here, note that this means that these will take effect only the
  1520. //first time that a font is selected, else they are ignored
  1521. if (isset($options['differences'])){
  1522. $this->fonts[$fontName]['differences']=$options['differences'];
  1523. }
  1524. }
  1525. }
  1526. if ($set && isset($this->fonts[$fontName])){
  1527. // so if for some reason the font was not set in the last one then it will not be selected
  1528. $this->currentBaseFont=$fontName;
  1529. // the next line means that if a new font is selected, then the current text state will be
  1530. // applied to it as well.
  1531. $this->setCurrentFont();
  1532. }
  1533. return $this->currentFontNum;
  1534. }
  1535. /**
  1536. * sets up the current font, based on the font families, and the current text state
  1537. * note that this system is quite flexible, a <b><i> font can be completely different to a
  1538. * <i><b> font, and even <b><b> will have to be defined within the family to have meaning
  1539. * This function is to be called whenever the currentTextState is changed, it will update
  1540. * the currentFont setting to whatever the appropriatte family one is.
  1541. * If the user calls selectFont themselves then that will reset the currentBaseFont, and the currentFont
  1542. * This function will change the currentFont to whatever it should be, but will not change the
  1543. * currentBaseFont.
  1544. *
  1545. * @access private
  1546. */
  1547. function setCurrentFont(){
  1548. if (strlen($this->currentBaseFont)==0){
  1549. // then assume an initial font
  1550. $this->selectFont('./fonts/Helvetica.afm');
  1551. }
  1552. $cf = substr($this->currentBaseFont,strrpos($this->currentBaseFont,'/')+1);
  1553. if (strlen($this->currentTextState)
  1554. && isset($this->fontFamilies[$cf])
  1555. && isset($this->fontFamilies[$cf][$this->currentTextState])){
  1556. // then we are in some state or another
  1557. // and this font has a family, and the current setting exists within it
  1558. // select the font, then return it
  1559. $nf = substr($this->currentBaseFont,0,strrpos($this->currentBaseFont,'/')+1).$this->fontFamilies[$cf][$this->currentTextState];
  1560. $this->selectFont($nf,'',0);
  1561. $this->currentFont = $nf;
  1562. $this->currentFontNum = $this->fonts[$nf]['fontNum'];
  1563. } else {
  1564. // the this font must not have the right family member for the current state
  1565. // simply assume the base font
  1566. $this->currentFont = $this->currentBaseFont;
  1567. $this->currentFontNum = $this->fonts[$this->currentFont]['fontNum'];
  1568. }
  1569. }
  1570. /**
  1571. * function for the user to find out what the ID is of the first page that was created during
  1572. * startup - useful if they wish to add something to it later.
  1573. */
  1574. function getFirstPageId(){
  1575. return $this->firstPageId;
  1576. }
  1577. /**
  1578. * add content to the currently active object
  1579. *
  1580. * @access private
  1581. */
  1582. function addContent($content){
  1583. $this->objects[$this->currentContents]['c'].=$content;
  1584. }
  1585. /**
  1586. * sets the colour for fill operations
  1587. */
  1588. function setColor($r,$g,$b,$force=0){
  1589. if ($r>=0 && ($force || $r!=$this->currentColour['r'] || $g!=$this->currentColour['g'] || $b!=$this->currentColour['b'])){
  1590. $this->objects[$this->currentContents]['c'].="\n".sprintf('%.3f',$r).' '.sprintf('%.3f',$g).' '.sprintf('%.3f',$b).' rg';
  1591. $this->currentColour=array('r'=>$r,'g'=>$g,'b'=>$b);
  1592. }
  1593. }
  1594. /**
  1595. * sets the colour for stroke operations
  1596. */
  1597. function setStrokeColor($r,$g,$b,$force=0){
  1598. if ($r>=0 && ($force || $r!=$this->currentStrokeColour['r'] || $g!=$this->currentStrokeColour['g'] || $b!=$this->currentStrokeColour['b'])){
  1599. $this->objects[$this->currentContents]['c'].="\n".sprintf('%.3f',$r).' '.sprintf('%.3f',$g).' '.sprintf('%.3f',$b).' RG';
  1600. $this->currentStrokeColour=array('r'=>$r,'g'=>$g,'b'=>$b);
  1601. }
  1602. }
  1603. /**
  1604. * draw a line from one set of coordinates to another
  1605. */
  1606. function line($x1,$y1,$x2,$y2){
  1607. $this->objects[$this->currentContents]['c'].="\n".sprintf('%.3f',$x1).' '.sprintf('%.3f',$y1).' m '.sprintf('%.3f',$x2).' '.sprintf('%.3f',$y2).' l S';
  1608. }
  1609. /**
  1610. * draw a bezier curve based on 4 control points
  1611. */
  1612. function curve($x0,$y0,$x1,$y1,$x2,$y2,$x3,$y3){
  1613. // in the current line style, draw a bezier curve from (x0,y0) to (x3,y3) using the other two points
  1614. // as the control points for the curve.
  1615. $this->objects[$this->currentContents]['c'].="\n".sprintf('%.3f',$x0).' '.sprintf('%.3f',$y0).' m '.sprintf('%.3f',$x1).' '.sprintf('%.3f',$y1);
  1616. $this->objects[$this->currentContents]['c'].= ' '.sprintf('%.3f',$x2).' '.sprintf('%.3f',$y2).' '.sprintf('%.3f',$x3).' '.sprintf('%.3f',$y3).' c S';
  1617. }
  1618. /**
  1619. * draw a part of an ellipse
  1620. */
  1621. function partEllipse($x0,$y0,$astart,$afinish,$r1,$r2=0,$angle=0,$nSeg=8){
  1622. $this->ellipse($x0,$y0,$r1,$r2,$angle,$nSeg,$astart,$afinish,0);
  1623. }
  1624. /**
  1625. * draw a filled ellipse
  1626. */
  1627. function filledEllipse($x0,$y0,$r1,$r2=0,$angle=0,$nSeg=8,$astart=0,$afinish=360){
  1628. return $this->ellipse($x0,$y0,$r1,$r2=0,$angle,$nSeg,$astart,$afinish,1,1);
  1629. }
  1630. /**
  1631. * draw an ellipse
  1632. * note that the part and filled ellipse are just special cases of this function
  1633. *
  1634. * draws an ellipse in the current line style
  1635. * centered at $x0,$y0, radii $r1,$r2
  1636. * if $r2 is not set, then a circle is drawn
  1637. * nSeg is not allowed to be less than 2, as this will simply draw a line (and will even draw a
  1638. * pretty crappy shape at 2, as we are approximating with bezier curves.
  1639. */
  1640. function ellipse($x0,$y0,$r1,$r2=0,$angle=0,$nSeg=8,$astart=0,$afinish=360,$close=1,$fill=0){
  1641. if ($r1==0){
  1642. return;
  1643. }
  1644. if ($r2==0){
  1645. $r2=$r1;
  1646. }
  1647. if ($nSeg<2){
  1648. $nSeg=2;
  1649. }
  1650. $astart = deg2rad((float)$astart);
  1651. $afinish = deg2rad((float)$afinish);
  1652. $totalAngle =$afinish-$astart;
  1653. $dt = $totalAngle/$nSeg;
  1654. $dtm = $dt/3;
  1655. if ($angle != 0){
  1656. $a = -1*deg2rad((float)$angle);
  1657. $tmp = "\n q ";
  1658. $tmp .= sprintf('%.3f',cos($a)).' '.sprintf('%.3f',(-1.0*sin($a))).' '.sprintf('%.3f',sin($a)).' '.sprintf('%.3f',cos($a)).' ';
  1659. $tmp .= sprintf('%.3f',$x0).' '.sprintf('%.3f',$y0).' cm';
  1660. $this->objects[$this->currentContents]['c'].= $tmp;
  1661. $x0=0;
  1662. $y0=0;
  1663. }
  1664. $t1 = $astart;
  1665. $a0 = $x0+$r1*cos($t1);
  1666. $b0 = $y0+$r2*sin($t1);
  1667. $c0 = -$r1*sin($t1);
  1668. $d0 = $r2*cos($t1);
  1669. $this->objects[$this->currentContents]['c'].="\n".sprintf('%.3f',$a0).' '.sprintf('%.3f',$b0).' m ';
  1670. for ($i=1;$i<=$nSeg;$i++){
  1671. // draw this bit of the total curve
  1672. $t1 = $i*$dt+$astart;
  1673. $a1 = $x0+$r1*cos($t1);
  1674. $b1 = $y0+$r2*sin($t1);
  1675. $c1 = -$r1*sin($t1);
  1676. $d1 = $r2*cos($t1);
  1677. $this->objects[$this->currentContents]['c'].="\n".sprintf('%.3f',($a0+$c0*$dtm)).' '.sprintf('%.3f',($b0+$d0*$dtm));
  1678. $this->objects[$this->currentContents]['c'].= ' '.sprintf('%.3f',($a1-$c1*$dtm)).' '.sprintf('%.3f',($b1-$d1*$dtm)).' '.sprintf('%.3f',$a1).' '.sprintf('%.3f',$b1).' c';
  1679. $a0=$a1;
  1680. $b0=$b1;
  1681. $c0=$c1;
  1682. $d0=$d1;
  1683. }
  1684. if ($fill){
  1685. $this->objects[$this->currentContents]['c'].=' f';
  1686. } else {
  1687. if ($close){
  1688. $this->objects[$this->currentContents]['c'].=' s'; // small 's' signifies closing the path as well
  1689. } else {
  1690. $this->objects[$this->currentContents]['c'].=' S';
  1691. }
  1692. }
  1693. if ($angle !=0){
  1694. $this->objects[$this->currentContents]['c'].=' Q';
  1695. }
  1696. }
  1697. /**
  1698. * this sets the line drawing style.
  1699. * width, is the thickness of the line in user units
  1700. * cap is the type of cap to put on the line, values can be 'butt','round','square'
  1701. * where the diffference between 'square' and 'butt' is that 'square' projects a flat end past the
  1702. * end of the line.
  1703. * join can be 'miter', 'round', 'bevel'
  1704. * dash is an array which sets the dash pattern, is a series of length values, which are the lengths of the
  1705. * on and off dashes.
  1706. * (2) represents 2 on, 2 off, 2 on , 2 off ...
  1707. * (2,1) is 2 on, 1 off, 2 on, 1 off.. etc
  1708. * phase is a modifier on the dash pattern which is used to shift the point at which the pattern starts.
  1709. */
  1710. function setLineStyle($width=1,$cap='',$join='',$dash='',$phase=0){
  1711. // this is quite inefficient in that it sets all the parameters whenever 1 is changed, but will fix another day
  1712. $string = '';
  1713. if ($width>0){
  1714. $string.= $width.' w';
  1715. }
  1716. $ca = array('butt'=>0,'round'=>1,'square'=>2);
  1717. if (isset($ca[$cap])){
  1718. $string.= ' '.$ca[$cap].' J';
  1719. }
  1720. $ja = array('miter'=>0,'round'=>1,'bevel'=>2);
  1721. if (isset($ja[$join])){
  1722. $string.= ' '.$ja[$join].' j';
  1723. }
  1724. if (is_array($dash)){
  1725. $string.= ' [';
  1726. foreach ($dash as $len){
  1727. $string.=' '.$len;
  1728. }
  1729. $string.= ' ] '.$phase.' d';
  1730. }
  1731. $this->currentLineStyle = $string;
  1732. $this->objects[$this->currentContents]['c'].="\n".$string;
  1733. }
  1734. /**
  1735. * draw a polygon, the syntax for this is similar to the GD polygon command
  1736. */
  1737. function polygon($p,$np,$f=0){
  1738. $this->objects[$this->currentContents]['c'].="\n";
  1739. $this->objects[$this->currentContents]['c'].=sprintf('%.3f',$p[0]).' '.sprintf('%.3f',$p[1]).' m ';
  1740. for ($i=2;$i<$np*2;$i=$i+2){
  1741. $this->objects[$this->currentContents]['c'].= sprintf('%.3f',$p[$i]).' '.sprintf('%.3f',$p[$i+1]).' l ';
  1742. }
  1743. if ($f==1){
  1744. $this->objects[$this->currentContents]['c'].=' f';
  1745. } else {
  1746. $this->objects[$this->currentContents]['c'].=' S';
  1747. }
  1748. }
  1749. /**
  1750. * a filled rectangle, note that it is the width and height of the rectangle which are the secondary paramaters, not
  1751. * the coordinates of the upper-right corner
  1752. */
  1753. function filledRectangle($x1,$y1,$width,$height){
  1754. $this->objects[$this->currentContents]['c'].="\n".sprintf('%.3f',$x1).' '.sprintf('%.3f',$y1).' '.sprintf('%.3f',$width).' '.sprintf('%.3f',$height).' re f';
  1755. }
  1756. /**
  1757. * draw a rectangle, note that it is the width and height of the rectangle which are the secondary paramaters, not
  1758. * the coordinates of the upper-right corner
  1759. */
  1760. function rectangle($x1,$y1,$width,$height){
  1761. $this->objects[$this->currentContents]['c'].="\n".sprintf('%.3f',$x1).' '.sprintf('%.3f',$y1).' '.sprintf('%.3f',$width).' '.sprintf('%.3f',$height).' re S';
  1762. }
  1763. /**
  1764. * add a new page to the document
  1765. * this also makes the new page the current active object
  1766. */
  1767. function newPage($insert=0,$id=0,$pos='after'){
  1768. // if there is a state saved, then go up the stack closing them
  1769. // then on the new page, re-open them with the right setings
  1770. if ($this->nStateStack){
  1771. for ($i=$this->nStateStack;$i>=1;$i--){
  1772. $this->restoreState($i);
  1773. }
  1774. }
  1775. $this->numObj++;
  1776. if ($insert){
  1777. // the id from the ezPdf class is the od of the contents of the page, not the page object itself
  1778. // query that object to find the parent
  1779. $rid = $this->objects[$id]['onPage'];
  1780. $opt= array('rid'=>$rid,'pos'=>$pos);
  1781. $this->o_page($this->numObj,'new',$opt);
  1782. } else {
  1783. $this->o_page($this->numObj,'new');
  1784. }
  1785. // if there is a stack saved, then put that onto the page
  1786. if ($this->nStateStack){
  1787. for ($i=1;$i<=$this->nStateStack;$i++){
  1788. $this->saveState($i);
  1789. }
  1790. }
  1791. // and if there has been a stroke or fill colour set, then transfer them
  1792. if ($this->currentColour['r']>=0){
  1793. $this->setColor($this->currentColour['r'],$this->currentColour['g'],$this->currentColour['b'],1);
  1794. }
  1795. if ($this->currentStrokeColour['r']>=0){
  1796. $this->setStrokeColor($this->currentStrokeColour['r'],$this->currentStrokeColour['g'],$this->currentStrokeColour['b'],1);
  1797. }
  1798. // if there is a line style set, then put this in too
  1799. if (strlen($this->currentLineStyle)){
  1800. $this->objects[$this->currentContents]['c'].="\n".$this->currentLineStyle;
  1801. }
  1802. // the call to the o_page object set currentContents to the present page, so this can be returned as the page id
  1803. return $this->currentContents;
  1804. }
  1805. /**
  1806. * output the pdf code, streaming it to the browser
  1807. * the relevant headers are set so that hopefully the browser will recognise it
  1808. */
  1809. function stream($options=''){
  1810. // setting the options allows the adjustment of the headers
  1811. // values at the moment are:
  1812. // 'Content-Disposition'=>'filename' - sets the filename, though not too sure how well this will
  1813. // work as in my trial the browser seems to use the filename of the php file with .pdf on the end
  1814. // 'Accept-Ranges'=>1 or 0 - if this is not set to 1, then this header is not included, off by default
  1815. // this header seems to have caused some problems despite tha fact that it is supposed to solve
  1816. // them, so I am leaving it off by default.
  1817. // 'compress'=> 1 or 0 - apply content stream compression, this is on (1) by default
  1818. if (!is_array($options)){
  1819. $options=array();
  1820. }
  1821. if ( isset($options['compress']) && $options['compress']==0){
  1822. $tmp = $this->output(1);
  1823. } else {
  1824. $tmp = $this->output();
  1825. }
  1826. header("Content-type: application/pdf");
  1827. header("Content-Length: ".strlen(ltrim($tmp)));
  1828. $fileName = (isset($options['Content-Disposition'])?$options['Content-Disposition']:'file.pdf');
  1829. header("Content-Disposition: inline; filename=".$fileName);
  1830. if (isset($options['Accept-Ranges']) && $options['Accept-Ranges']==1){
  1831. header("Accept-Ranges: ".strlen(ltrim($tmp)));
  1832. }
  1833. echo ltrim($tmp);
  1834. }
  1835. /**
  1836. * return the height in units of the current font in the given size
  1837. */
  1838. function getFontHeight($size){
  1839. if (!$this->numFonts){
  1840. $this->selectFont('./fonts/Helvetica');
  1841. }
  1842. // for the current font, and the given size, what is the height of the font in user units
  1843. $h = $this->fonts[$this->currentFont]['FontBBox'][3]-$this->fonts[$this->currentFont]['FontBBox'][1];
  1844. return $size*$h/1000;
  1845. }
  1846. /**
  1847. * return the font decender, this will normally return a negative number
  1848. * if you add this number to the baseline, you get the level of the bottom of the font
  1849. * it is in the pdf user units
  1850. */
  1851. function getFontDecender($size){
  1852. // note that this will most likely return a negative value
  1853. if (!$this->numFonts){
  1854. $this->selectFont('./fonts/Helvetica');
  1855. }
  1856. $h = $this->fonts[$this->currentFont]['FontBBox'][1];
  1857. return $size*$h/1000;
  1858. }
  1859. /**
  1860. * filter the text, this is applied to all text just before being inserted into the pdf document
  1861. * it escapes the various things that need to be escaped, and so on
  1862. *
  1863. * @access private
  1864. */
  1865. function filterText($text){
  1866. $text = str_replace('\\','\\\\',$text);
  1867. $text = str_replace('(','\(',$text);
  1868. $text = str_replace(')','\)',$text);
  1869. $text = str_replace('&lt;','<',$text);
  1870. $text = str_replace('&gt;','>',$text);
  1871. $text = str_replace('&#039;','\'',$text);
  1872. $text = str_replace('&quot;','"',$text);
  1873. $text = str_replace('&amp;','&',$text);
  1874. return $text;
  1875. }
  1876. /**
  1877. * given a start position and information about how text is to be laid out, calculate where
  1878. * on the page the text will end
  1879. *
  1880. * @access private
  1881. */
  1882. function PRVTgetTextPosition($x,$y,$angle,$size,$wa,$text){
  1883. // given this information return an array containing x and y for the end position as elements 0 and 1
  1884. $w = $this->getTextWidth($size,$text);
  1885. // need to adjust for the number of spaces in this text
  1886. $words = explode(' ',$text);
  1887. $nspaces=count($words)-1;
  1888. $w += $wa*$nspaces;
  1889. $a = deg2rad((float)$angle);
  1890. return array(cos($a)*$w+$x,-sin($a)*$w+$y);
  1891. }
  1892. /**
  1893. * wrapper function for PRVTcheckTextDirective1
  1894. *
  1895. * @access private
  1896. */
  1897. function PRVTcheckTextDirective(&$text,$i,&$f){
  1898. $x=0;
  1899. $y=0;
  1900. return $this->PRVTcheckTextDirective1($text,$i,$f,0,$x,$y);
  1901. }
  1902. /**
  1903. * checks if the text stream contains a control directive
  1904. * if so then makes some changes and returns the number of characters involved in the directive
  1905. * this has been re-worked to include everything neccesary to fins the current writing point, so that
  1906. * the location can be sent to the callback function if required
  1907. * if the directive does not require a font change, then $f should be set to 0
  1908. *
  1909. * @access private
  1910. */
  1911. function PRVTcheckTextDirective1(&$text,$i,&$f,$final,&$x,&$y,$size=0,$angle=0,$wordSpaceAdjust=0){
  1912. $directive = 0;
  1913. $j=$i;
  1914. if ($text[$j]=='<'){
  1915. $j++;
  1916. switch($text[$j]){
  1917. case '/':
  1918. $j++;
  1919. if (strlen($text) <= $j){
  1920. return $directive;
  1921. }
  1922. switch($text[$j]){
  1923. case 'b':
  1924. case 'i':
  1925. $j++;
  1926. if ($text[$j]=='>'){
  1927. $p = strrpos($this->currentTextState,$text[$j-1]);
  1928. if ($p !== false){
  1929. // then there is one to remove
  1930. $this->currentTextState = substr($this->currentTextState,0,$p).substr($this->currentTextState,$p+1);
  1931. }
  1932. $directive=$j-$i+1;
  1933. }
  1934. break;
  1935. case 'c':
  1936. // this this might be a callback function
  1937. $j++;
  1938. $k = strpos($text,'>',$j);
  1939. if ($k!==false && $text[$j]==':'){
  1940. // then this will be treated as a callback directive
  1941. $directive = $k-$i+1;
  1942. $f=0;
  1943. // split the remainder on colons to get the function name and the paramater
  1944. $tmp = substr($text,$j+1,$k-$j-1);
  1945. $b1 = strpos($tmp,':');
  1946. if ($b1!==false){
  1947. $func = substr($tmp,0,$b1);
  1948. $parm = substr($tmp,$b1+1);
  1949. } else {
  1950. $func=$tmp;
  1951. $parm='';
  1952. }
  1953. if (!isset($func) || !strlen(trim($func))){
  1954. $directive=0;
  1955. } else {
  1956. // only call the function if this is the final call
  1957. if ($final){
  1958. // need to assess the text position, calculate the text width to this point
  1959. // can use getTextWidth to find the text width I think
  1960. $tmp = $this->PRVTgetTextPosition($x,$y,$angle,$size,$wordSpaceAdjust,substr($text,0,$i));
  1961. $info = array('x'=>$tmp[0],'y'=>$tmp[1],'angle'=>$angle,'status'=>'end','p'=>$parm,'nCallback'=>$this->nCallback);
  1962. $x=$tmp[0];
  1963. $y=$tmp[1];
  1964. $ret = $this->$func($info);
  1965. if (is_array($ret)){
  1966. // then the return from the callback function could set the position, to start with, later will do font colour, and font
  1967. foreach($ret as $rk=>$rv){
  1968. switch($rk){
  1969. case 'x':
  1970. case 'y':
  1971. $$rk=$rv;
  1972. break;
  1973. }
  1974. }
  1975. }
  1976. // also remove from to the stack
  1977. // for simplicity, just take from the end, fix this another day
  1978. $this->nCallback--;
  1979. if ($this->nCallback<0){
  1980. $this->nCallBack=0;
  1981. }
  1982. }
  1983. }
  1984. }
  1985. break;
  1986. }
  1987. break;
  1988. case 'b':
  1989. case 'i':
  1990. $j++;
  1991. if ($text[$j]=='>'){
  1992. $this->currentTextState.=$text[$j-1];
  1993. $directive=$j-$i+1;
  1994. }
  1995. break;
  1996. case 'C':
  1997. $noClose=1;
  1998. case 'c':
  1999. // this this might be a callback function
  2000. $j++;
  2001. $k = strpos($text,'>',$j);
  2002. if ($k!==false && $text[$j]==':'){
  2003. // then this will be treated as a callback directive
  2004. $directive = $k-$i+1;
  2005. $f=0;
  2006. // split the remainder on colons to get the function name and the paramater
  2007. // $bits = explode(':',substr($text,$j+1,$k-$j-1));
  2008. $tmp = substr($text,$j+1,$k-$j-1);
  2009. $b1 = strpos($tmp,':');
  2010. if ($b1!==false){
  2011. $func = substr($tmp,0,$b1);
  2012. $parm = substr($tmp,$b1+1);
  2013. } else {
  2014. $func=$tmp;
  2015. $parm='';
  2016. }
  2017. if (!isset($func) || !strlen(trim($func))){
  2018. $directive=0;
  2019. } else {
  2020. // only call the function if this is the final call, ie, the one actually doing printing, not measurement
  2021. if ($final){
  2022. // need to assess the text position, calculate the text width to this point
  2023. // can use getTextWidth to find the text width I think
  2024. // also add the text height and decender
  2025. $tmp = $this->PRVTgetTextPosition($x,$y,$angle,$size,$wordSpaceAdjust,substr($text,0,$i));
  2026. $info = array('x'=>$tmp[0],'y'=>$tmp[1],'angle'=>$angle,'status'=>'start','p'=>$parm,'f'=>$func,'height'=>$this->getFontHeight($size),'decender'=>$this->getFontDecender($size));
  2027. $x=$tmp[0];
  2028. $y=$tmp[1];
  2029. if (!isset($noClose) || !$noClose){
  2030. // only add to the stack if this is a small 'c', therefore is a start-stop pair
  2031. $this->nCallback++;
  2032. $info['nCallback']=$this->nCallback;
  2033. $this->callback[$this->nCallback]=$info;
  2034. }
  2035. $ret = $this->$func($info);
  2036. if (is_array($ret)){
  2037. // then the return from the callback function could set the position, to start with, later will do font colour, and font
  2038. foreach($ret as $rk=>$rv){
  2039. switch($rk){
  2040. case 'x':
  2041. case 'y':
  2042. $$rk=$rv;
  2043. break;
  2044. }
  2045. }
  2046. }
  2047. }
  2048. }
  2049. }
  2050. break;
  2051. }
  2052. }
  2053. return $directive;
  2054. }
  2055. /**
  2056. * add text to the document, at a specified location, size and angle on the page
  2057. */
  2058. function addText($x,$y,$size,$text,$angle=0,$wordSpaceAdjust=0){
  2059. if (!$this->numFonts){$this->selectFont('./fonts/Helvetica');}
  2060. // if there are any open callbacks, then they should be called, to show the start of the line
  2061. if ($this->nCallback>0){
  2062. for ($i=$this->nCallback;$i>0;$i--){
  2063. // call each function
  2064. $info = array('x'=>$x,'y'=>$y,'angle'=>$angle,'status'=>'sol','p'=>$this->callback[$i]['p'],'nCallback'=>$this->callback[$i]['nCallback'],'height'=>$this->callback[$i]['height'],'decender'=>$this->callback[$i]['decender']);
  2065. $func = $this->callback[$i]['f'];
  2066. $this->$func($info);
  2067. }
  2068. }
  2069. if ($angle==0){
  2070. $this->objects[$this->currentContents]['c'].="\n".'BT '.sprintf('%.3f',$x).' '.sprintf('%.3f',$y).' Td';
  2071. } else {
  2072. $a = deg2rad((float)$angle);
  2073. $tmp = "\n".'BT ';
  2074. $tmp .= sprintf('%.3f',cos($a)).' '.sprintf('%.3f',(-1.0*sin($a))).' '.sprintf('%.3f',sin($a)).' '.sprintf('%.3f',cos($a)).' ';
  2075. $tmp .= sprintf('%.3f',$x).' '.sprintf('%.3f',$y).' Tm';
  2076. $this->objects[$this->currentContents]['c'] .= $tmp;
  2077. }
  2078. if ($wordSpaceAdjust!=0 || $wordSpaceAdjust != $this->wordSpaceAdjust){
  2079. $this->wordSpaceAdjust=$wordSpaceAdjust;
  2080. $this->objects[$this->currentContents]['c'].=' '.sprintf('%.3f',$wordSpaceAdjust).' Tw';
  2081. }
  2082. $len=strlen($text);
  2083. $start=0;
  2084. for ($i=0;$i<$len;$i++){
  2085. $f=1;
  2086. $directive = $this->PRVTcheckTextDirective($text,$i,$f);
  2087. if ($directive){
  2088. // then we should write what we need to
  2089. if ($i>$start){
  2090. $part = substr($text,$start,$i-$start);
  2091. $this->objects[$this->currentContents]['c'].=' /F'.$this->currentFontNum.' '.sprintf('%.1f',$size).' Tf ';
  2092. $this->objects[$this->currentContents]['c'].=' ('.$this->filterText($part).') Tj';
  2093. }
  2094. if ($f){
  2095. // then there was nothing drastic done here, restore the contents
  2096. $this->setCurrentFont();
  2097. } else {
  2098. $this->objects[$this->currentContents]['c'] .= ' ET';
  2099. $f=1;
  2100. $xp=$x;
  2101. $yp=$y;
  2102. $directive = $this->PRVTcheckTextDirective1($text,$i,$f,1,$xp,$yp,$size,$angle,$wordSpaceAdjust);
  2103. // restart the text object
  2104. if ($angle==0){
  2105. $this->objects[$this->currentContents]['c'].="\n".'BT '.sprintf('%.3f',$xp).' '.sprintf('%.3f',$yp).' Td';
  2106. } else {
  2107. $a = deg2rad((float)$angle);
  2108. $tmp = "\n".'BT ';
  2109. $tmp .= sprintf('%.3f',cos($a)).' '.sprintf('%.3f',(-1.0*sin($a))).' '.sprintf('%.3f',sin($a)).' '.sprintf('%.3f',cos($a)).' ';
  2110. $tmp .= sprintf('%.3f',$xp).' '.sprintf('%.3f',$yp).' Tm';
  2111. $this->objects[$this->currentContents]['c'] .= $tmp;
  2112. }
  2113. if ($wordSpaceAdjust!=0 || $wordSpaceAdjust != $this->wordSpaceAdjust){
  2114. $this->wordSpaceAdjust=$wordSpaceAdjust;
  2115. $this->objects[$this->currentContents]['c'].=' '.sprintf('%.3f',$wordSpaceAdjust).' Tw';
  2116. }
  2117. }
  2118. // and move the writing point to the next piece of text
  2119. $i=$i+$directive-1;
  2120. $start=$i+1;
  2121. }
  2122. }
  2123. if ($start<$len){
  2124. $part = substr($text,$start);
  2125. $this->objects[$this->currentContents]['c'].=' /F'.$this->currentFontNum.' '.sprintf('%.1f',$size).' Tf ';
  2126. $this->objects[$this->currentContents]['c'].=' ('.$this->filterText($part).') Tj';
  2127. }
  2128. $this->objects[$this->currentContents]['c'].=' ET';
  2129. // if there are any open callbacks, then they should be called, to show the end of the line
  2130. if ($this->nCallback>0){
  2131. for ($i=$this->nCallback;$i>0;$i--){
  2132. // call each function
  2133. $tmp = $this->PRVTgetTextPosition($x,$y,$angle,$size,$wordSpaceAdjust,$text);
  2134. $info = array('x'=>$tmp[0],'y'=>$tmp[1],'angle'=>$angle,'status'=>'eol','p'=>$this->callback[$i]['p'],'nCallback'=>$this->callback[$i]['nCallback'],'height'=>$this->callback[$i]['height'],'decender'=>$this->callback[$i]['decender']);
  2135. $func = $this->callback[$i]['f'];
  2136. $this->$func($info);
  2137. }
  2138. }
  2139. }
  2140. /**
  2141. * calculate how wide a given text string will be on a page, at a given size.
  2142. * this can be called externally, but is alse used by the other class functions
  2143. */
  2144. function getTextWidth($size,$text){
  2145. // this function should not change any of the settings, though it will need to
  2146. // track any directives which change during calculation, so copy them at the start
  2147. // and put them back at the end.
  2148. $store_currentTextState = $this->currentTextState;
  2149. if (!$this->numFonts){
  2150. $this->selectFont('./fonts/Helvetica');
  2151. }
  2152. // converts a number or a float to a string so it can get the width
  2153. $text = "$text";
  2154. // hmm, this is where it all starts to get tricky - use the font information to
  2155. // calculate the width of each character, add them up and convert to user units
  2156. $w=0;
  2157. $len=strlen($text);
  2158. $cf = $this->currentFont;
  2159. for ($i=0;$i<$len;$i++){
  2160. $f=1;
  2161. $directive = $this->PRVTcheckTextDirective($text,$i,$f);
  2162. if ($directive){
  2163. if ($f){
  2164. $this->setCurrentFont();
  2165. $cf = $this->currentFont;
  2166. }
  2167. $i=$i+$directive-1;
  2168. } else {
  2169. $char=ord($text[$i]);
  2170. if (isset($this->fonts[$cf]['differences'][$char])){
  2171. // then this character is being replaced by another
  2172. $name = $this->fonts[$cf]['differences'][$char];
  2173. if (isset($this->fonts[$cf]['C'][$name]['WX'])){
  2174. $w+=$this->fonts[$cf]['C'][$name]['WX'];
  2175. }
  2176. } else if (isset($this->fonts[$cf]['C'][$char]['WX'])){
  2177. $w+=$this->fonts[$cf]['C'][$char]['WX'];
  2178. }
  2179. }
  2180. }
  2181. $this->currentTextState = $store_currentTextState;
  2182. $this->setCurrentFont();
  2183. return $w*$size/1000;
  2184. }
  2185. /**
  2186. * do a part of the calculation for sorting out the justification of the text
  2187. *
  2188. * @access private
  2189. */
  2190. function PRVTadjustWrapText($text,$actual,$width,&$x,&$adjust,$justification){
  2191. switch ($justification){
  2192. case 'left':
  2193. return;
  2194. break;
  2195. case 'right':
  2196. $x+=$width-$actual;
  2197. break;
  2198. case 'center':
  2199. case 'centre':
  2200. $x+=($width-$actual)/2;
  2201. break;
  2202. case 'full':
  2203. // count the number of words
  2204. $words = explode(' ',$text);
  2205. $nspaces=count($words)-1;
  2206. if ($nspaces>0){
  2207. $adjust = ($width-$actual)/$nspaces;
  2208. } else {
  2209. $adjust=0;
  2210. }
  2211. break;
  2212. }
  2213. }
  2214. /**
  2215. * add text to the page, but ensure that it fits within a certain width
  2216. * if it does not fit then put in as much as possible, splitting at word boundaries
  2217. * and return the remainder.
  2218. * justification and angle can also be specified for the text
  2219. */
  2220. function addTextWrap($x,$y,$width,$size,$text,$justification='left',$angle=0,$test=0){
  2221. // this will display the text, and if it goes beyond the width $width, will backtrack to the
  2222. // previous space or hyphen, and return the remainder of the text.
  2223. // $justification can be set to 'left','right','center','centre','full'
  2224. // need to store the initial text state, as this will change during the width calculation
  2225. // but will need to be re-set before printing, so that the chars work out right
  2226. $store_currentTextState = $this->currentTextState;
  2227. if (!$this->numFonts){$this->selectFont('./fonts/Helvetica');}
  2228. if ($width<=0){
  2229. // error, pretend it printed ok, otherwise risking a loop
  2230. return '';
  2231. }
  2232. $w=0;
  2233. $break=0;
  2234. $breakWidth=0;
  2235. $len=strlen($text);
  2236. $cf = $this->currentFont;
  2237. $tw = $width/$size*1000;
  2238. for ($i=0;$i<$len;$i++){
  2239. $f=1;
  2240. $directive = $this->PRVTcheckTextDirective($text,$i,$f);
  2241. if ($directive){
  2242. if ($f){
  2243. $this->setCurrentFont();
  2244. $cf = $this->currentFont;
  2245. }
  2246. $i=$i+$directive-1;
  2247. } else {
  2248. $cOrd = ord($text[$i]);
  2249. if (isset($this->fonts[$cf]['differences'][$cOrd])){
  2250. // then this character is being replaced by another
  2251. $cOrd2 = $this->fonts[$cf]['differences'][$cOrd];
  2252. } else {
  2253. $cOrd2 = $cOrd;
  2254. }
  2255. if (isset($this->fonts[$cf]['C'][$cOrd2]['WX'])){
  2256. $w+=$this->fonts[$cf]['C'][$cOrd2]['WX'];
  2257. }
  2258. if ($w>$tw){
  2259. // then we need to truncate this line
  2260. if ($break>0){
  2261. // then we have somewhere that we can split :)
  2262. if ($text[$break]==' '){
  2263. $tmp = substr($text,0,$break);
  2264. } else {
  2265. $tmp = substr($text,0,$break+1);
  2266. }
  2267. $adjust=0;
  2268. $this->PRVTadjustWrapText($tmp,$breakWidth,$width,$x,$adjust,$justification);
  2269. // reset the text state
  2270. $this->currentTextState = $store_currentTextState;
  2271. $this->setCurrentFont();
  2272. if (!$test){
  2273. $this->addText($x,$y,$size,$tmp,$angle,$adjust);
  2274. }
  2275. return substr($text,$break+1);
  2276. } else {
  2277. // just split before the current character
  2278. $tmp = substr($text,0,$i);
  2279. $adjust=0;
  2280. $ctmp=ord($text[$i]);
  2281. if (isset($this->fonts[$cf]['differences'][$ctmp])){
  2282. $ctmp=$this->fonts[$cf]['differences'][$ctmp];
  2283. }
  2284. $tmpw=($w-$this->fonts[$cf]['C'][$ctmp]['WX'])*$size/1000;
  2285. $this->PRVTadjustWrapText($tmp,$tmpw,$width,$x,$adjust,$justification);
  2286. // reset the text state
  2287. $this->currentTextState = $store_currentTextState;
  2288. $this->setCurrentFont();
  2289. if (!$test){
  2290. $this->addText($x,$y,$size,$tmp,$angle,$adjust);
  2291. }
  2292. return substr($text,$i);
  2293. }
  2294. }
  2295. if ($text[$i]=='-'){
  2296. $break=$i;
  2297. $breakWidth = $w*$size/1000;
  2298. }
  2299. if ($text[$i]==' '){
  2300. $break=$i;
  2301. $ctmp=ord($text[$i]);
  2302. if (isset($this->fonts[$cf]['differences'][$ctmp])){
  2303. $ctmp=$this->fonts[$cf]['differences'][$ctmp];
  2304. }
  2305. $breakWidth = ($w-$this->fonts[$cf]['C'][$ctmp]['WX'])*$size/1000;
  2306. }
  2307. }
  2308. }
  2309. // then there was no need to break this line
  2310. if ($justification=='full'){
  2311. $justification='left';
  2312. }
  2313. $adjust=0;
  2314. $tmpw=$w*$size/1000;
  2315. $this->PRVTadjustWrapText($text,$tmpw,$width,$x,$adjust,$justification);
  2316. // reset the text state
  2317. $this->currentTextState = $store_currentTextState;
  2318. $this->setCurrentFont();
  2319. if (!$test){
  2320. $this->addText($x,$y,$size,$text,$angle,$adjust,$angle);
  2321. }
  2322. return '';
  2323. }
  2324. /**
  2325. * this will be called at a new page to return the state to what it was on the
  2326. * end of the previous page, before the stack was closed down
  2327. * This is to get around not being able to have open 'q' across pages
  2328. *
  2329. */
  2330. function saveState($pageEnd=0){
  2331. if ($pageEnd){
  2332. // this will be called at a new page to return the state to what it was on the
  2333. // end of the previous page, before the stack was closed down
  2334. // This is to get around not being able to have open 'q' across pages
  2335. $opt = $this->stateStack[$pageEnd]; // ok to use this as stack starts numbering at 1
  2336. $this->setColor($opt['col']['r'],$opt['col']['g'],$opt['col']['b'],1);
  2337. $this->setStrokeColor($opt['str']['r'],$opt['str']['g'],$opt['str']['b'],1);
  2338. $this->objects[$this->currentContents]['c'].="\n".$opt['lin'];
  2339. // $this->currentLineStyle = $opt['lin'];
  2340. } else {
  2341. $this->nStateStack++;
  2342. $this->stateStack[$this->nStateStack]=array(
  2343. 'col'=>$this->currentColour
  2344. ,'str'=>$this->currentStrokeColour
  2345. ,'lin'=>$this->currentLineStyle
  2346. );
  2347. }
  2348. $this->objects[$this->currentContents]['c'].="\nq";
  2349. }
  2350. /**
  2351. * restore a previously saved state
  2352. */
  2353. function restoreState($pageEnd=0){
  2354. if (!$pageEnd){
  2355. $n = $this->nStateStack;
  2356. $this->currentColour = $this->stateStack[$n]['col'];
  2357. $this->currentStrokeColour = $this->stateStack[$n]['str'];
  2358. $this->objects[$this->currentContents]['c'].="\n".$this->stateStack[$n]['lin'];
  2359. $this->currentLineStyle = $this->stateStack[$n]['lin'];
  2360. unset($this->stateStack[$n]);
  2361. $this->nStateStack--;
  2362. }
  2363. $this->objects[$this->currentContents]['c'].="\nQ";
  2364. }
  2365. /**
  2366. * make a loose object, the output will go into this object, until it is closed, then will revert to
  2367. * the current one.
  2368. * this object will not appear until it is included within a page.
  2369. * the function will return the object number
  2370. */
  2371. function openObject(){
  2372. $this->nStack++;
  2373. $this->stack[$this->nStack]=array('c'=>$this->currentContents,'p'=>$this->currentPage);
  2374. // add a new object of the content type, to hold the data flow
  2375. $this->numObj++;
  2376. $this->o_contents($this->numObj,'new');
  2377. $this->currentContents=$this->numObj;
  2378. $this->looseObjects[$this->numObj]=1;
  2379. return $this->numObj;
  2380. }
  2381. /**
  2382. * open an existing object for editing
  2383. */
  2384. function reopenObject($id){
  2385. $this->nStack++;
  2386. $this->stack[$this->nStack]=array('c'=>$this->currentContents,'p'=>$this->currentPage);
  2387. $this->currentContents=$id;
  2388. // also if this object is the primary contents for a page, then set the current page to its parent
  2389. if (isset($this->objects[$id]['onPage'])){
  2390. $this->currentPage = $this->objects[$id]['onPage'];
  2391. }
  2392. }
  2393. /**
  2394. * close an object
  2395. */
  2396. function closeObject(){
  2397. // close the object, as long as there was one open in the first place, which will be indicated by
  2398. // an objectId on the stack.
  2399. if ($this->nStack>0){
  2400. $this->currentContents=$this->stack[$this->nStack]['c'];
  2401. $this->currentPage=$this->stack[$this->nStack]['p'];
  2402. $this->nStack--;
  2403. // easier to probably not worry about removing the old entries, they will be overwritten
  2404. // if there are new ones.
  2405. }
  2406. }
  2407. /**
  2408. * stop an object from appearing on pages from this point on
  2409. */
  2410. function stopObject($id){
  2411. // if an object has been appearing on pages up to now, then stop it, this page will
  2412. // be the last one that could contian it.
  2413. if (isset($this->addLooseObjects[$id])){
  2414. $this->addLooseObjects[$id]='';
  2415. }
  2416. }
  2417. /**
  2418. * after an object has been created, it wil only show if it has been added, using this function.
  2419. */
  2420. function addObject($id,$options='add'){
  2421. // add the specified object to the page
  2422. if (isset($this->looseObjects[$id]) && $this->currentContents!=$id){
  2423. // then it is a valid object, and it is not being added to itself
  2424. switch($options){
  2425. case 'all':
  2426. // then this object is to be added to this page (done in the next block) and
  2427. // all future new pages.
  2428. $this->addLooseObjects[$id]='all';
  2429. case 'add':
  2430. if (isset($this->objects[$this->currentContents]['onPage'])){
  2431. // then the destination contents is the primary for the page
  2432. // (though this object is actually added to that page)
  2433. $this->o_page($this->objects[$this->currentContents]['onPage'],'content',$id);
  2434. }
  2435. break;
  2436. case 'even':
  2437. $this->addLooseObjects[$id]='even';
  2438. $pageObjectId=$this->objects[$this->currentContents]['onPage'];
  2439. if ($this->objects[$pageObjectId]['info']['pageNum']%2==0){
  2440. $this->addObject($id); // hacky huh :)
  2441. }
  2442. break;
  2443. case 'odd':
  2444. $this->addLooseObjects[$id]='odd';
  2445. $pageObjectId=$this->objects[$this->currentContents]['onPage'];
  2446. if ($this->objects[$pageObjectId]['info']['pageNum']%2==1){
  2447. $this->addObject($id); // hacky huh :)
  2448. }
  2449. break;
  2450. case 'next':
  2451. $this->addLooseObjects[$id]='all';
  2452. break;
  2453. case 'nexteven':
  2454. $this->addLooseObjects[$id]='even';
  2455. break;
  2456. case 'nextodd':
  2457. $this->addLooseObjects[$id]='odd';
  2458. break;
  2459. }
  2460. }
  2461. }
  2462. /**
  2463. * add content to the documents info object
  2464. */
  2465. function addInfo($label,$value=0){
  2466. // this will only work if the label is one of the valid ones.
  2467. // modify this so that arrays can be passed as well.
  2468. // if $label is an array then assume that it is key=>value pairs
  2469. // else assume that they are both scalar, anything else will probably error
  2470. if (is_array($label)){
  2471. foreach ($label as $l=>$v){
  2472. $this->o_info($this->infoObject,$l,$v);
  2473. }
  2474. } else {
  2475. $this->o_info($this->infoObject,$label,$value);
  2476. }
  2477. }
  2478. /**
  2479. * set the viewer preferences of the document, it is up to the browser to obey these.
  2480. */
  2481. function setPreferences($label,$value=0){
  2482. // this will only work if the label is one of the valid ones.
  2483. if (is_array($label)){
  2484. foreach ($label as $l=>$v){
  2485. $this->o_catalog($this->catalogId,'viewerPreferences',array($l=>$v));
  2486. }
  2487. } else {
  2488. $this->o_catalog($this->catalogId,'viewerPreferences',array($label=>$value));
  2489. }
  2490. }
  2491. /**
  2492. * extract an integer from a position in a byte stream
  2493. *
  2494. * @access private
  2495. */
  2496. function PRVT_getBytes(&$data,$pos,$num){
  2497. // return the integer represented by $num bytes from $pos within $data
  2498. $ret=0;
  2499. for ($i=0;$i<$num;$i++){
  2500. $ret=$ret*256;
  2501. $ret+=ord($data[$pos+$i]);
  2502. }
  2503. return $ret;
  2504. }
  2505. /**
  2506. * add a PNG image into the document, from a file
  2507. * this should work with remote files
  2508. */
  2509. function addPngFromFile($file,$x,$y,$w=0,$h=0){
  2510. // read in a png file, interpret it, then add to the system
  2511. $error=0;
  2512. $tmp = get_magic_quotes_runtime();
  2513. set_magic_quotes_runtime(0);
  2514. $fp = @fopen($file,'rb');
  2515. if ($fp){
  2516. $data='';
  2517. while(!feof($fp)){
  2518. $data .= fread($fp,1024);
  2519. }
  2520. fclose($fp);
  2521. } else {
  2522. $error = 1;
  2523. $errormsg = 'trouble opening file: '.$file;
  2524. }
  2525. set_magic_quotes_runtime($tmp);
  2526. if (!$error){
  2527. $header = chr(137).chr(80).chr(78).chr(71).chr(13).chr(10).chr(26).chr(10);
  2528. if (substr($data,0,8)!=$header){
  2529. $error=1;
  2530. $errormsg = 'this file does not have a valid header';
  2531. }
  2532. }
  2533. if (!$error){
  2534. // set pointer
  2535. $p = 8;
  2536. $len = strlen($data);
  2537. // cycle through the file, identifying chunks
  2538. $haveHeader=0;
  2539. $info=array();
  2540. $idata='';
  2541. $pdata='';
  2542. while ($p<$len){
  2543. $chunkLen = $this->PRVT_getBytes($data,$p,4);
  2544. $chunkType = substr($data,$p+4,4);
  2545. // echo $chunkType.' - '.$chunkLen.'<br>';
  2546. switch($chunkType){
  2547. case 'IHDR':
  2548. // this is where all the file information comes from
  2549. $info['width']=$this->PRVT_getBytes($data,$p+8,4);
  2550. $info['height']=$this->PRVT_getBytes($data,$p+12,4);
  2551. $info['bitDepth']=ord($data[$p+16]);
  2552. $info['colorType']=ord($data[$p+17]);
  2553. $info['compressionMethod']=ord($data[$p+18]);
  2554. $info['filterMethod']=ord($data[$p+19]);
  2555. $info['interlaceMethod']=ord($data[$p+20]);
  2556. //print_r($info);
  2557. $haveHeader=1;
  2558. if ($info['compressionMethod']!=0){
  2559. $error=1;
  2560. $errormsg = 'unsupported compression method';
  2561. }
  2562. if ($info['filterMethod']!=0){
  2563. $error=1;
  2564. $errormsg = 'unsupported filter method';
  2565. }
  2566. break;
  2567. case 'PLTE':
  2568. $pdata.=substr($data,$p+8,$chunkLen);
  2569. break;
  2570. case 'IDAT':
  2571. $idata.=substr($data,$p+8,$chunkLen);
  2572. break;
  2573. case 'tRNS':
  2574. //this chunk can only occur once and it must occur after the PLTE chunk and before IDAT chunk
  2575. //print "tRNS found, color type = ".$info['colorType']."<BR>";
  2576. $transparency = array();
  2577. if ($info['colorType'] == 3) { // indexed color, rbg
  2578. /* corresponding to entries in the plte chunk
  2579. Alpha for palette index 0: 1 byte
  2580. Alpha for palette index 1: 1 byte
  2581. ...etc...
  2582. */
  2583. // there will be one entry for each palette entry. up until the last non-opaque entry.
  2584. // set up an array, stretching over all palette entries which will be o (opaque) or 1 (transparent)
  2585. $transparency['type']='indexed';
  2586. $numPalette = strlen($pdata)/3;
  2587. $trans=0;
  2588. for ($i=$chunkLen;$i>=0;$i--){
  2589. if (ord($data[$p+8+$i])==0){
  2590. $trans=$i;
  2591. }
  2592. }
  2593. $transparency['data'] = $trans;
  2594. } elseif($info['colorType'] == 0) { // grayscale
  2595. /* corresponding to entries in the plte chunk
  2596. Gray: 2 bytes, range 0 .. (2^bitdepth)-1
  2597. */
  2598. // $transparency['grayscale']=$this->PRVT_getBytes($data,$p+8,2); // g = grayscale
  2599. $transparency['type']='indexed';
  2600. $transparency['data'] = ord($data[$p+8+1]);
  2601. } elseif($info['colorType'] == 2) { // truecolor
  2602. /* corresponding to entries in the plte chunk
  2603. Red: 2 bytes, range 0 .. (2^bitdepth)-1
  2604. Green: 2 bytes, range 0 .. (2^bitdepth)-1
  2605. Blue: 2 bytes, range 0 .. (2^bitdepth)-1
  2606. */
  2607. $transparency['r']=$this->PRVT_getBytes($data,$p+8,2); // r from truecolor
  2608. $transparency['g']=$this->PRVT_getBytes($data,$p+10,2); // g from truecolor
  2609. $transparency['b']=$this->PRVT_getBytes($data,$p+12,2); // b from truecolor
  2610. } else {
  2611. //unsupported transparency type
  2612. }
  2613. // KS End new code
  2614. break;
  2615. default:
  2616. break;
  2617. }
  2618. $p += $chunkLen+12;
  2619. }
  2620. if(!$haveHeader){
  2621. $error = 1;
  2622. $errormsg = 'information header is missing';
  2623. }
  2624. if (isset($info['interlaceMethod']) && $info['interlaceMethod']){
  2625. $error = 1;
  2626. $errormsg = 'There appears to be no support for interlaced images in pdf.';
  2627. }
  2628. }
  2629. if (!$error && $info['bitDepth'] > 8){
  2630. $error = 1;
  2631. $errormsg = 'only bit depth of 8 or less is supported';
  2632. }
  2633. if (!$error){
  2634. if ($info['colorType']!=2 && $info['colorType']!=0 && $info['colorType']!=3){
  2635. $error = 1;
  2636. $errormsg = 'transparancey alpha channel not supported, transparency only supported for palette images.';
  2637. } else {
  2638. switch ($info['colorType']){
  2639. case 3:
  2640. $color = 'DeviceRGB';
  2641. $ncolor=1;
  2642. break;
  2643. case 2:
  2644. $color = 'DeviceRGB';
  2645. $ncolor=3;
  2646. break;
  2647. case 0:
  2648. $color = 'DeviceGray';
  2649. $ncolor=1;
  2650. break;
  2651. }
  2652. }
  2653. }
  2654. if ($error){
  2655. $this->addMessage('PNG error - ('.$file.') '.$errormsg);
  2656. return;
  2657. }
  2658. if ($w==0){
  2659. $w=$h/$info['height']*$info['width'];
  2660. }
  2661. if ($h==0){
  2662. $h=$w*$info['height']/$info['width'];
  2663. }
  2664. //print_r($info);
  2665. // so this image is ok... add it in.
  2666. $this->numImages++;
  2667. $im=$this->numImages;
  2668. $label='I'.$im;
  2669. $this->numObj++;
  2670. // $this->o_image($this->numObj,'new',array('label'=>$label,'data'=>$idata,'iw'=>$w,'ih'=>$h,'type'=>'png','ic'=>$info['width']));
  2671. $options = array('label'=>$label,'data'=>$idata,'bitsPerComponent'=>$info['bitDepth'],'pdata'=>$pdata
  2672. ,'iw'=>$info['width'],'ih'=>$info['height'],'type'=>'png','color'=>$color,'ncolor'=>$ncolor);
  2673. if (isset($transparency)){
  2674. $options['transparency']=$transparency;
  2675. }
  2676. $this->o_image($this->numObj,'new',$options);
  2677. $this->objects[$this->currentContents]['c'].="\nq";
  2678. $this->objects[$this->currentContents]['c'].="\n".sprintf('%.3f',$w)." 0 0 ".sprintf('%.3f',$h)." ".sprintf('%.3f',$x)." ".sprintf('%.3f',$y)." cm";
  2679. $this->objects[$this->currentContents]['c'].="\n/".$label.' Do';
  2680. $this->objects[$this->currentContents]['c'].="\nQ";
  2681. }
  2682. /**
  2683. * add a JPEG image into the document, from a file
  2684. */
  2685. function addJpegFromFile($img,$x,$y,$w=0,$h=0){
  2686. // attempt to add a jpeg image straight from a file, using no GD commands
  2687. // note that this function is unable to operate on a remote file.
  2688. if (!file_exists($img)){
  2689. return;
  2690. }
  2691. $tmp=getimagesize($img);
  2692. $imageWidth=$tmp[0];
  2693. $imageHeight=$tmp[1];
  2694. if (isset($tmp['channels'])){
  2695. $channels = $tmp['channels'];
  2696. } else {
  2697. $channels = 3;
  2698. }
  2699. if ($w<=0 && $h<=0){
  2700. $w=$imageWidth;
  2701. }
  2702. if ($w==0){
  2703. $w=$h/$imageHeight*$imageWidth;
  2704. }
  2705. if ($h==0){
  2706. $h=$w*$imageHeight/$imageWidth;
  2707. }
  2708. $fp=fopen($img,'rb');
  2709. $tmp = get_magic_quotes_runtime();
  2710. set_magic_quotes_runtime(0);
  2711. $data = fread($fp,filesize($img));
  2712. set_magic_quotes_runtime($tmp);
  2713. fclose($fp);
  2714. $this->addJpegImage_common($data,$x,$y,$w,$h,$imageWidth,$imageHeight,$channels);
  2715. }
  2716. /**
  2717. * add an image into the document, from a GD object
  2718. * this function is not all that reliable, and I would probably encourage people to use
  2719. * the file based functions
  2720. */
  2721. function addImage(&$img,$x,$y,$w=0,$h=0,$quality=75){
  2722. // add a new image into the current location, as an external object
  2723. // add the image at $x,$y, and with width and height as defined by $w & $h
  2724. // note that this will only work with full colour images and makes them jpg images for display
  2725. // later versions could present lossless image formats if there is interest.
  2726. // there seems to be some problem here in that images that have quality set above 75 do not appear
  2727. // not too sure why this is, but in the meantime I have restricted this to 75.
  2728. if ($quality>75){
  2729. $quality=75;
  2730. }
  2731. // if the width or height are set to zero, then set the other one based on keeping the image
  2732. // height/width ratio the same, if they are both zero, then give up :)
  2733. $imageWidth=imagesx($img);
  2734. $imageHeight=imagesy($img);
  2735. if ($w<=0 && $h<=0){
  2736. return;
  2737. }
  2738. if ($w==0){
  2739. $w=$h/$imageHeight*$imageWidth;
  2740. }
  2741. if ($h==0){
  2742. $h=$w*$imageHeight/$imageWidth;
  2743. }
  2744. // gotta get the data out of the img..
  2745. // so I write to a temp file, and then read it back.. soo ugly, my apologies.
  2746. $tmpDir='/tmp';
  2747. $tmpName=tempnam($tmpDir,'img');
  2748. imagejpeg($img,$tmpName,$quality);
  2749. $fp=fopen($tmpName,'rb');
  2750. $tmp = get_magic_quotes_runtime();
  2751. set_magic_quotes_runtime(0);
  2752. $fp = @fopen($tmpName,'rb');
  2753. if ($fp){
  2754. $data='';
  2755. while(!feof($fp)){
  2756. $data .= fread($fp,1024);
  2757. }
  2758. fclose($fp);
  2759. } else {
  2760. $error = 1;
  2761. $errormsg = 'trouble opening file';
  2762. }
  2763. // $data = fread($fp,filesize($tmpName));
  2764. set_magic_quotes_runtime($tmp);
  2765. // fclose($fp);
  2766. unlink($tmpName);
  2767. $this->addJpegImage_common($data,$x,$y,$w,$h,$imageWidth,$imageHeight);
  2768. }
  2769. /**
  2770. * common code used by the two JPEG adding functions
  2771. *
  2772. * @access private
  2773. */
  2774. function addJpegImage_common(&$data,$x,$y,$w=0,$h=0,$imageWidth,$imageHeight,$channels=3){
  2775. // note that this function is not to be called externally
  2776. // it is just the common code between the GD and the file options
  2777. $this->numImages++;
  2778. $im=$this->numImages;
  2779. $label='I'.$im;
  2780. $this->numObj++;
  2781. $this->o_image($this->numObj,'new',array('label'=>$label,'data'=>$data,'iw'=>$imageWidth,'ih'=>$imageHeight,'channels'=>$channels));
  2782. $this->objects[$this->currentContents]['c'].="\nq";
  2783. $this->objects[$this->currentContents]['c'].="\n".sprintf('%.3f',$w)." 0 0 ".sprintf('%.3f',$h)." ".sprintf('%.3f',$x)." ".sprintf('%.3f',$y)." cm";
  2784. $this->objects[$this->currentContents]['c'].="\n/".$label.' Do';
  2785. $this->objects[$this->currentContents]['c'].="\nQ";
  2786. }
  2787. /**
  2788. * specify where the document should open when it first starts
  2789. */
  2790. function openHere($style,$a=0,$b=0,$c=0){
  2791. // this function will open the document at a specified page, in a specified style
  2792. // the values for style, and the required paramters are:
  2793. // 'XYZ' left, top, zoom
  2794. // 'Fit'
  2795. // 'FitH' top
  2796. // 'FitV' left
  2797. // 'FitR' left,bottom,right
  2798. // 'FitB'
  2799. // 'FitBH' top
  2800. // 'FitBV' left
  2801. $this->numObj++;
  2802. $this->o_destination($this->numObj,'new',array('page'=>$this->currentPage,'type'=>$style,'p1'=>$a,'p2'=>$b,'p3'=>$c));
  2803. $id = $this->catalogId;
  2804. $this->o_catalog($id,'openHere',$this->numObj);
  2805. }
  2806. /**
  2807. * create a labelled destination within the document
  2808. */
  2809. function addDestination($label,$style,$a=0,$b=0,$c=0){
  2810. // associates the given label with the destination, it is done this way so that a destination can be specified after
  2811. // it has been linked to
  2812. // styles are the same as the 'openHere' function
  2813. $this->numObj++;
  2814. $this->o_destination($this->numObj,'new',array('page'=>$this->currentPage,'type'=>$style,'p1'=>$a,'p2'=>$b,'p3'=>$c));
  2815. $id = $this->numObj;
  2816. // store the label->idf relationship, note that this means that labels can be used only once
  2817. $this->destinations["$label"]=$id;
  2818. }
  2819. /**
  2820. * define font families, this is used to initialize the font families for the default fonts
  2821. * and for the user to add new ones for their fonts. The default bahavious can be overridden should
  2822. * that be desired.
  2823. */
  2824. function setFontFamily($family,$options=''){
  2825. if (!is_array($options)){
  2826. if ($family=='init'){
  2827. // set the known family groups
  2828. // these font families will be used to enable bold and italic markers to be included
  2829. // within text streams. html forms will be used... <b></b> <i></i>
  2830. $this->fontFamilies['Helvetica.afm']=array(
  2831. 'b'=>'Helvetica-Bold.afm'
  2832. ,'i'=>'Helvetica-Oblique.afm'
  2833. ,'bi'=>'Helvetica-BoldOblique.afm'
  2834. ,'ib'=>'Helvetica-BoldOblique.afm'
  2835. );
  2836. $this->fontFamilies['Courier.afm']=array(
  2837. 'b'=>'Courier-Bold.afm'
  2838. ,'i'=>'Courier-Oblique.afm'
  2839. ,'bi'=>'Courier-BoldOblique.afm'
  2840. ,'ib'=>'Courier-BoldOblique.afm'
  2841. );
  2842. $this->fontFamilies['Times-Roman.afm']=array(
  2843. 'b'=>'Times-Bold.afm'
  2844. ,'i'=>'Times-Italic.afm'
  2845. ,'bi'=>'Times-BoldItalic.afm'
  2846. ,'ib'=>'Times-BoldItalic.afm'
  2847. );
  2848. }
  2849. } else {
  2850. // the user is trying to set a font family
  2851. // note that this can also be used to set the base ones to something else
  2852. if (strlen($family)){
  2853. $this->fontFamilies[$family] = $options;
  2854. }
  2855. }
  2856. }
  2857. /**
  2858. * used to add messages for use in debugging
  2859. */
  2860. function addMessage($message){
  2861. $this->messages.=$message."\n";
  2862. }
  2863. /**
  2864. * a few functions which should allow the document to be treated transactionally.
  2865. */
  2866. function transaction($action){
  2867. switch ($action){
  2868. case 'start':
  2869. // store all the data away into the checkpoint variable
  2870. $data = get_object_vars($this);
  2871. $this->checkpoint = $data;
  2872. unset($data);
  2873. break;
  2874. case 'commit':
  2875. if (is_array($this->checkpoint) && isset($this->checkpoint['checkpoint'])){
  2876. $tmp = $this->checkpoint['checkpoint'];
  2877. $this->checkpoint = $tmp;
  2878. unset($tmp);
  2879. } else {
  2880. $this->checkpoint='';
  2881. }
  2882. break;
  2883. case 'rewind':
  2884. // do not destroy the current checkpoint, but move us back to the state then, so that we can try again
  2885. if (is_array($this->checkpoint)){
  2886. // can only abort if were inside a checkpoint
  2887. $tmp = $this->checkpoint;
  2888. foreach ($tmp as $k=>$v){
  2889. if ($k != 'checkpoint'){
  2890. $this->$k=$v;
  2891. }
  2892. }
  2893. unset($tmp);
  2894. }
  2895. break;
  2896. case 'abort':
  2897. if (is_array($this->checkpoint)){
  2898. // can only abort if were inside a checkpoint
  2899. $tmp = $this->checkpoint;
  2900. foreach ($tmp as $k=>$v){
  2901. $this->$k=$v;
  2902. }
  2903. unset($tmp);
  2904. }
  2905. break;
  2906. }
  2907. }
  2908. } // end of class
  2909. ?>