PageRenderTime 80ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/rospdf/pdf-php/src/Cpdf.php

https://bitbucket.org/openemr/openemr
PHP | 3947 lines | 2624 code | 367 blank | 956 comment | 573 complexity | be951a90dd3e14c160d3a7204b945f02 MD5 | raw file
Possible License(s): Apache-2.0, AGPL-1.0, GPL-2.0, LGPL-3.0, BSD-3-Clause, Unlicense, MPL-2.0, GPL-3.0, LGPL-2.1
  1. <?php
  2. @include_once('include/TTFsubset.php');
  3. /**
  4. * Create pdf documents without additional modules
  5. * Note that the companion class Document_CezPdf can be used to extend this class and
  6. * simplify the creation of documents.
  7. * <pre>
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see http://www.gnu.org/licenses/
  20. * </pre>
  21. *
  22. * **Document object methods**
  23. *
  24. * There is about one object method for each type of object in the pdf document<br>
  25. * Each function has the same call list ($id,$action,$options).<br>
  26. * <pre>
  27. * $id = the object ID of the object, or what it is to be if it is being created
  28. * $action = a string specifying the action to be performed, though ALL must support:
  29. * 'new' - create the object with the id $id
  30. * 'out' - produce the output for the pdf object
  31. * $options = optional, a string or array containing the various parameters for the object
  32. * </pre>
  33. * These, in conjunction with the output function are the ONLY way for output to be produced
  34. * within the pdf 'file'.
  35. *
  36. * @category Documents
  37. * @package Cpdf
  38. * @version $Id: Cpdf.php 274 2014-03-21 12:25:04Z ole1986 $
  39. * @author Wayne Munro (inactive) <pdf@ros.co.nz>
  40. * @author Lars Olesen <lars@legestue.net>
  41. * @author Sune Jensen <sj@sunet.dk>
  42. * @author Ole Koeckemann <ole1986@users.sourceforge.net>
  43. * @copyright 2007 - 2014 The authors
  44. * @license GNU General Public License v3
  45. * @link http://pdf-php.sf.net
  46. */
  47. class Cpdf
  48. {
  49. /**
  50. * PDF version
  51. * This value may vary dependent on which methods and/or features are used.
  52. * For instance setEncryption may cause the pdf version to increase to $this->pdfversion = 1.4
  53. *
  54. * Minimum 1.3
  55. * @var string $pdfversion default is 1.3
  56. */
  57. protected $pdfversion = 1.3;
  58. /**
  59. * allow the programmer to output debug messages on several places<br>
  60. * 'none' = no debug output at all
  61. * 'error_log' = use error_log
  62. * 'variable' = store in a variable called $this->messages
  63. * @var string $DEBUG Default is error_log
  64. */
  65. public $DEBUG = 'error_log';
  66. /**
  67. * Set the debug level
  68. * E_USER_ERROR = only errors
  69. * E_USER_WARNING = errors and warning
  70. * E_USER_NOTICE = nearly everything
  71. *
  72. * @var int $DEBUGLEVEL Default E_USER_WARNING
  73. */
  74. public $DEBUGLEVEL = E_USER_WARNING;
  75. /**
  76. * Reversed char string to allow arabic or Hebrew
  77. * @todo incomplete implementation
  78. */
  79. public $rtl = false;
  80. /**
  81. * flag to validate the output and if output method has be executed
  82. * This option is not really in use but is set to true in checkAllHere method
  83. * @var bool
  84. */
  85. protected $valid = false;
  86. /**
  87. * temporary path used for image and font caching.
  88. * Need to get changed when using XAMPP
  89. * @var string
  90. */
  91. public $tempPath = '/tmp';
  92. /**
  93. * the current number of pdf objects in the document
  94. * @var integer
  95. */
  96. protected $numObj=0;
  97. /**
  98. * contains pdf objects ready for the final assembly
  99. * @var array
  100. */
  101. protected $objects = array();
  102. /**
  103. * set to true allows object being hashed. Primary used for images
  104. * @var bool
  105. */
  106. public $hashed = true;
  107. /**
  108. * Object hash array used to free pdf from redundancies
  109. * @var array
  110. */
  111. private $objectHash = array();
  112. /**
  113. * the objectId (number within the objects array) of the document catalog
  114. * @var integer
  115. */
  116. private $catalogId;
  117. /**
  118. * default encoding for NON-UNICODE text
  119. @var string default encoding is IS0-8859-1
  120. */
  121. public $targetEncoding = 'ISO-8859-1';
  122. /**
  123. * set this to true allows TTF font being parsed as unicode in PDF output.
  124. * This also converts all text output into utf16_be
  125. * @var boolean default is false
  126. */
  127. public $isUnicode = false;
  128. /**
  129. * define the tags being allowed in any text input, like addText or addTextWrap (default: bold, italic and links)
  130. * @var string
  131. */
  132. public $allowedTags = 'b|strong|i|uline|alink:?.*?|ilink:?.*?|color:?.*?';
  133. /**
  134. * used to either embed or not embed the ttf/pfb font program
  135. * @var boolean default embed the font program
  136. */
  137. protected $embedFont = true;
  138. /**
  139. * font cache timeout in seconds
  140. * @var integer default is 86400 which is 1 day
  141. */
  142. public $cacheTimeout = 86400;
  143. /**
  144. * stores the font family information for either core fonts or any other TTF font program.
  145. * Once the font family is defined, directives like bold and italic
  146. * @var array
  147. */
  148. private $fontFamilies = array(
  149. 'Helvetica' => array(
  150. 'b'=>'Helvetica-Bold',
  151. 'i'=>'Helvetica-Oblique',
  152. 'bi'=>'Helvetica-BoldOblique',
  153. 'ib'=>'Helvetica-BoldOblique',
  154. ),
  155. 'Courier' => array(
  156. 'b'=>'Courier-Bold',
  157. 'i'=>'Courier-Oblique',
  158. 'bi'=>'Courier-BoldOblique',
  159. 'ib'=>'Courier-BoldOblique',
  160. ),
  161. 'Times-Roman' => array(
  162. 'b'=>'Times-Bold',
  163. 'i'=>'Times-Italic',
  164. 'bi'=>'Times-BoldItalic',
  165. 'ib'=>'Times-BoldItalic',
  166. )
  167. );
  168. /**
  169. * all CoreFonts available in PDF by default.
  170. * This array is used check if TTF font need to get attached and/or is unicode
  171. * @var array
  172. */
  173. private $coreFonts = array('courier', 'courier-bold', 'courier-oblique', 'courier-boldoblique',
  174. 'helvetica', 'helvetica-bold', 'helvetica-oblique', 'helvetica-boldoblique',
  175. 'times-roman', 'times-bold', 'times-italic', 'times-bolditalic',
  176. 'symbol', 'zapfdingbats');
  177. /**
  178. * array carrying information about the fonts that the system currently knows about
  179. * used to ensure that a font is not loaded twice, among other things
  180. * @var array
  181. */
  182. protected $fonts = array();
  183. /**
  184. * font path location
  185. * @since 0.12-rc8
  186. */
  187. public $fontPath = './';
  188. /**
  189. * a record of the current font
  190. * @var string
  191. */
  192. protected $currentFont='';
  193. /**
  194. * the current base font
  195. * @var string
  196. */
  197. protected $currentBaseFont='';
  198. /**
  199. * the number of the current font within the font array
  200. * @var integer
  201. */
  202. protected $currentFontNum=0;
  203. /**
  204. * no clue for what this is used
  205. * @var integer
  206. */
  207. private $currentNode;
  208. /**
  209. * object number of the current page
  210. * @var integer
  211. */
  212. protected $currentPage;
  213. /**
  214. * object number of the currently active contents block
  215. * @var integer
  216. */
  217. protected $currentContents;
  218. /**
  219. * number of fonts within the system
  220. * @var integer
  221. */
  222. protected $numFonts = 0;
  223. /**
  224. * current colour for fill operations, defaults to inactive value, all three components should be between 0 and 1 inclusive when active
  225. */
  226. protected $currentColour = array('r' => -1, 'g' => -1, 'b' => -1);
  227. /**
  228. * current colour for stroke operations (lines etc.)
  229. */
  230. protected $currentStrokeColour = array('r' => -1, 'g' => -1, 'b' => -1);
  231. /**
  232. * current style that lines are drawn in
  233. */
  234. protected $currentLineStyle='';
  235. /**
  236. * an array which is used to save the state of the document, mainly the colours and styles
  237. * it is used to temporarily change to another state, the change back to what it was before
  238. */
  239. private $stateStack = array();
  240. /**
  241. * number of elements within the state stack
  242. */
  243. private $nStateStack = 0;
  244. /**
  245. * number of page objects within the document
  246. */
  247. protected $numPages=0;
  248. /**
  249. * object Id storage stack
  250. */
  251. protected $stack = array();
  252. /**
  253. * number of elements within the object Id storage stack
  254. */
  255. private $nStack=0;
  256. /**
  257. * an array which contains information about the objects which are not firmly attached to pages
  258. * these have been added with the addObject function
  259. */
  260. private $looseObjects=array();
  261. /**
  262. * array contains infomation about how the loose objects are to be added to the document
  263. */
  264. private $addLooseObjects=array();
  265. /**
  266. * the objectId of the information object for the document
  267. * this contains authorship, title etc.
  268. */
  269. private $infoObject=0;
  270. /**
  271. * number of images being tracked within the document
  272. */
  273. private $numImages=0;
  274. /**
  275. * some additional options while generation
  276. * currently used for compression only
  277. * Default: 'compression' => -1 which will set gzcompress to the default level of 6
  278. */
  279. public $options=array('compression'=>-1);
  280. /**
  281. * the objectId of the first page of the document
  282. */
  283. private $firstPageId;
  284. /**
  285. * used to track the last used value of the inter-word spacing, this is so that it is known
  286. * when the spacing is changed.
  287. */
  288. private $wordSpaceAdjust=0;
  289. /**
  290. * tracks the status of the current font style, like bold or italic
  291. */
  292. private $currentTextState = '';
  293. /**
  294. * messages are stored here during processing, these can be selected afterwards to give some useful debug information
  295. */
  296. public $messages='';
  297. /**
  298. * the encryption array for the document encryption is stored here
  299. */
  300. private $arc4='';
  301. /**
  302. * the object Id of the encryption information
  303. */
  304. private $arc4_objnum=0;
  305. /**
  306. * the file identifier, used to uniquely identify a pdf document
  307. */
  308. public $fileIdentifier;
  309. /**
  310. * Set the encryption mode
  311. * 0 = no encryption
  312. * 1 = RC40bit
  313. * 2 = RC128bit (since PDF Version 1.4)
  314. */
  315. private $encryptionMode = 0;
  316. /**
  317. * the encryption key for the encryption of all the document content (structure is not encrypted)
  318. *
  319. * @var string
  320. */
  321. private $encryptionKey='';
  322. /**
  323. * encryption padding fetched from the Adobe PDF reference
  324. */
  325. private $encryptionPad;
  326. /**
  327. * array which forms a stack to keep track of nested callback functions
  328. *
  329. * @var array
  330. */
  331. private $callback = array();
  332. /**
  333. * the number of callback functions in the callback array
  334. *
  335. * @var integer
  336. */
  337. private $nCallback = 0;
  338. /**
  339. * store label->id pairs for named destinations, these will be used to replace internal links
  340. * done this way so that destinations can be defined after the location that links to them
  341. *
  342. * @var array
  343. */
  344. private $destinations = array();
  345. /**
  346. * store the stack for the transaction commands, each item in here is a record of the values of all the
  347. * variables within the class, so that the user can rollback at will (from each 'start' command)
  348. * note that this includes the objects array, so these can be large.
  349. *
  350. * @var string
  351. */
  352. protected $checkpoint = '';
  353. /**
  354. * Constructor - start with a new PDF document
  355. *
  356. * @param array $pageSize Array of 4 numbers, defining the bottom left and upper right corner of the page. first two are normally zero.
  357. * @param bool $isUnicode
  358. * @return void
  359. */
  360. public function __construct($pageSize = array(0, 0, 612, 792), $isUnicode = false)
  361. {
  362. $this->isUnicode = $isUnicode;
  363. // set the hardcoded encryption pad
  364. $this->encryptionPad = 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);
  365. $this->newDocument($pageSize);
  366. if ( in_array('Windows-1252', mb_list_encodings()) ) {
  367. $this->targetEncoding = 'Windows-1252';
  368. }
  369. // use the md5 to have a unique identifier for all documents created with R&OS pdf class
  370. $this->fileIdentifier = md5('ROSPDF'.time());
  371. // set the default font path to [...]/src/fonts
  372. $this->fontPath = dirname(__FILE__).'/fonts';
  373. }
  374. /**
  375. * destination object, used to specify the location for the user to jump to, presently on opening
  376. */
  377. private function o_destination($id,$action,$options='')
  378. {
  379. if ($action!='new'){
  380. $o =& $this->objects[$id];
  381. }
  382. switch($action){
  383. case 'new':
  384. $this->objects[$id]=array('t'=>'destination','info'=>array());
  385. $tmp = '';
  386. switch ($options['type']){
  387. case 'XYZ':
  388. case 'FitR':
  389. $tmp = ' '.$options['p3'].$tmp;
  390. case 'FitH':
  391. case 'FitV':
  392. case 'FitBH':
  393. case 'FitBV':
  394. $tmp = ' '.$options['p1'].' '.$options['p2'].$tmp;
  395. case 'Fit':
  396. case 'FitB':
  397. $tmp = $options['type'].$tmp;
  398. $this->objects[$id]['info']['string']=$tmp;
  399. $this->objects[$id]['info']['page']=$options['page'];
  400. }
  401. break;
  402. case 'out':
  403. $tmp = $o['info'];
  404. $res="\n".$id." 0 obj\n".'['.$tmp['page'].' 0 R /'.$tmp['string']."]\nendobj";
  405. return $res;
  406. break;
  407. }
  408. }
  409. /**
  410. * sets the viewer preferences
  411. */
  412. private function o_viewerPreferences($id,$action,$options='')
  413. {
  414. if ($action!='new'){
  415. $o =& $this->objects[$id];
  416. }
  417. switch ($action){
  418. case 'new':
  419. $this->objects[$id]=array('t'=>'viewerPreferences','info'=>array());
  420. break;
  421. case 'add':
  422. foreach($options as $k=>$v){
  423. switch ($k){
  424. case 'HideToolbar':
  425. case 'HideMenubar':
  426. case 'HideWindowUI':
  427. case 'FitWindow':
  428. case 'CenterWindow':
  429. case 'DisplayDocTitle': // since PDF 1.4
  430. case 'NonFullScreenPageMode':
  431. case 'Direction':
  432. $o['info'][$k]=$v;
  433. break;
  434. }
  435. }
  436. break;
  437. case 'out':
  438. $res="\n".$id." 0 obj\n".'<< ';
  439. foreach($o['info'] as $k=>$v){
  440. $res.="\n/".$k.' '.$v;
  441. }
  442. $res.="\n>>\n";
  443. return $res;
  444. break;
  445. }
  446. }
  447. /**
  448. * define the document catalog, the overall controller for the document
  449. */
  450. private function o_catalog($id, $action, $options = '')
  451. {
  452. if ($action!='new'){
  453. $o =& $this->objects[$id];
  454. }
  455. switch ($action){
  456. case 'new':
  457. $this->objects[$id]=array('t'=>'catalog','info'=>array());
  458. $this->catalogId=$id;
  459. break;
  460. case 'outlines':
  461. case 'pages':
  462. case 'openHere':
  463. $o['info'][$action]=$options;
  464. break;
  465. case 'viewerPreferences':
  466. if (!isset($o['info']['viewerPreferences'])){
  467. $this->numObj++;
  468. $this->o_viewerPreferences($this->numObj,'new');
  469. $o['info']['viewerPreferences']=$this->numObj;
  470. }
  471. $vp = $o['info']['viewerPreferences'];
  472. $this->o_viewerPreferences($vp,'add',$options);
  473. break;
  474. case 'out':
  475. $res="\n".$id." 0 obj\n".'<< /Type /Catalog';
  476. foreach($o['info'] as $k=>$v){
  477. switch($k){
  478. case 'outlines':
  479. $res.=' /Outlines '.$v.' 0 R';
  480. break;
  481. case 'pages':
  482. $res.=' /Pages '.$v.' 0 R';
  483. break;
  484. case 'viewerPreferences':
  485. $res.=' /ViewerPreferences '.$o['info']['viewerPreferences'].' 0 R';
  486. break;
  487. case 'openHere':
  488. $res.=' /OpenAction '.$o['info']['openHere'].' 0 R';
  489. break;
  490. }
  491. }
  492. $res.=" >>\nendobj";
  493. return $res;
  494. break;
  495. }
  496. }
  497. /**
  498. * object which is a parent to the pages in the document
  499. */
  500. private function o_pages($id,$action,$options='')
  501. {
  502. if ($action!='new'){
  503. $o =& $this->objects[$id];
  504. }
  505. switch ($action){
  506. case 'new':
  507. $this->objects[$id]=array('t'=>'pages','info'=>array());
  508. $this->o_catalog($this->catalogId,'pages',$id);
  509. break;
  510. case 'page':
  511. if (!is_array($options)){
  512. // then it will just be the id of the new page
  513. $o['info']['pages'][]=$options;
  514. } else {
  515. // then it should be an array having 'id','rid','pos', where rid=the page to which this one will be placed relative
  516. // and pos is either 'before' or 'after', saying where this page will fit.
  517. if (isset($options['id']) && isset($options['rid']) && isset($options['pos'])){
  518. $i = array_search($options['rid'],$o['info']['pages']);
  519. if (isset($o['info']['pages'][$i]) && $o['info']['pages'][$i]==$options['rid']){
  520. // then there is a match make a space
  521. switch ($options['pos']){
  522. case 'before':
  523. $k = $i;
  524. break;
  525. case 'after':
  526. $k=$i+1;
  527. break;
  528. default:
  529. $k=-1;
  530. break;
  531. }
  532. if ($k>=0){
  533. for ($j=count($o['info']['pages'])-1;$j>=$k;$j--){
  534. $o['info']['pages'][$j+1]=$o['info']['pages'][$j];
  535. }
  536. $o['info']['pages'][$k]=$options['id'];
  537. }
  538. }
  539. }
  540. }
  541. break;
  542. case 'procset':
  543. $o['info']['procset']=$options;
  544. break;
  545. case 'mediaBox':
  546. $o['info']['mediaBox']=$options; // which should be an array of 4 numbers
  547. break;
  548. case 'font':
  549. $o['info']['fonts'][]=array('objNum'=>$options['objNum'],'fontNum'=>$options['fontNum']);
  550. break;
  551. case 'xObject':
  552. $o['info']['xObjects'][]=array('objNum'=>$options['objNum'],'label'=>$options['label']);
  553. break;
  554. case 'out':
  555. if (count($o['info']['pages'])){
  556. $res="\n".$id." 0 obj\n<< /Type /Pages /Kids [";
  557. foreach($o['info']['pages'] as $k=>$v){
  558. $res.=$v." 0 R ";
  559. }
  560. $res.="] /Count ".count($this->objects[$id]['info']['pages']);
  561. if ((isset($o['info']['fonts']) && count($o['info']['fonts'])) || isset($o['info']['procset'])){
  562. $res.=" /Resources <<";
  563. if (isset($o['info']['procset'])){
  564. $res.=" /ProcSet ".$o['info']['procset'];
  565. }
  566. if (isset($o['info']['fonts']) && count($o['info']['fonts'])){
  567. $res.=" /Font << ";
  568. foreach($o['info']['fonts'] as $finfo){
  569. $res.=" /F".$finfo['fontNum']." ".$finfo['objNum']." 0 R";
  570. }
  571. $res.=" >>";
  572. }
  573. if (isset($o['info']['xObjects']) && count($o['info']['xObjects'])){
  574. $res.=" /XObject << ";
  575. foreach($o['info']['xObjects'] as $finfo){
  576. $res.=" /".$finfo['label']." ".$finfo['objNum']." 0 R";
  577. }
  578. $res.=" >>";
  579. }
  580. $res.=" >>";
  581. if (isset($o['info']['mediaBox'])){
  582. $tmp=$o['info']['mediaBox'];
  583. $res.=" /MediaBox [".sprintf('%.3F',$tmp[0]).' '.sprintf('%.3F',$tmp[1]).' '.sprintf('%.3F',$tmp[2]).' '.sprintf('%.3F',$tmp[3]).']';
  584. }
  585. }
  586. $res.=" >>\nendobj";
  587. } else {
  588. $res="\n".$id." 0 obj\n<< /Type /Pages\n/Count 0\n>>\nendobj";
  589. }
  590. return $res;
  591. break;
  592. }
  593. }
  594. /**
  595. * defines the outlines in the doc, empty for now
  596. */
  597. private function o_outlines($id,$action,$options='')
  598. {
  599. if ($action!='new'){
  600. $o =& $this->objects[$id];
  601. }
  602. switch ($action){
  603. case 'new':
  604. $this->objects[$id]=array('t'=>'outlines','info'=>array('outlines'=>array()));
  605. $this->o_catalog($this->catalogId,'outlines',$id);
  606. break;
  607. case 'outline':
  608. $o['info']['outlines'][]=$options;
  609. break;
  610. case 'out':
  611. if (count($o['info']['outlines'])){
  612. $res="\n".$id." 0 obj\n<< /Type /Outlines /Kids [";
  613. foreach($o['info']['outlines'] as $k=>$v){
  614. $res.=$v." 0 R ";
  615. }
  616. $res.="] /Count ".count($o['info']['outlines'])." >>\nendobj";
  617. } else {
  618. $res="\n".$id." 0 obj\n<< /Type /Outlines /Count 0 >>\nendobj";
  619. }
  620. return $res;
  621. break;
  622. }
  623. }
  624. /**
  625. * an object to hold the font description
  626. */
  627. private function o_font($id,$action,$options=''){
  628. if ($action!='new'){
  629. $o =& $this->objects[$id];
  630. }
  631. switch ($action){
  632. case 'new':
  633. $this->objects[$id]=array('t'=>'font','info'=>array('name'=>$options['name'], 'fontFileName'=> $options['fontFileName'], 'SubType'=>'Type1'));
  634. $fontFileName = &$options['fontFileName'];
  635. $fontNum=$this->numFonts;
  636. $this->objects[$id]['info']['fontNum']=$fontNum;
  637. // deal with the encoding and the differences
  638. if (isset($options['differences'])){
  639. // then we'll need an encoding dictionary
  640. $this->numObj++;
  641. $this->o_fontEncoding($this->numObj,'new',$options);
  642. $this->objects[$id]['info']['encodingDictionary']=$this->numObj;
  643. } else if (isset($options['encoding'])){
  644. // we can specify encoding here
  645. switch($options['encoding']){
  646. case 'WinAnsiEncoding':
  647. case 'MacRomanEncoding':
  648. case 'MacExpertEncoding':
  649. $this->objects[$id]['info']['encoding']=$options['encoding'];
  650. break;
  651. case 'none':
  652. break;
  653. default:
  654. $this->objects[$id]['info']['encoding']='WinAnsiEncoding';
  655. break;
  656. }
  657. } else {
  658. $this->objects[$id]['info']['encoding']='WinAnsiEncoding';
  659. }
  660. if ($this->fonts[$fontFileName]['isUnicode']) {
  661. // For Unicode fonts, we need to incorporate font data into
  662. // sub-sections that are linked from the primary font section.
  663. // Look at o_fontGIDtoCID and o_fontDescendentCID functions
  664. // for more informaiton.
  665. //
  666. // All of this code is adapted from the excellent changes made to
  667. // transform FPDF to TCPDF (http://tcpdf.sourceforge.net/)
  668. $toUnicodeId = ++$this->numObj;
  669. $this->o_contents($toUnicodeId, 'new', 'raw');
  670. $this->objects[$id]['info']['toUnicode'] = $toUnicodeId;
  671. $stream = "/CIDInit /ProcSet findresource begin\n12 dict begin\nbegincmap\n/CIDSystemInfo <</Registry (Adobe) /Ordering (UCS) /Supplement 0 >> def\n/CMapName /Adobe-Identity-UCS def\n/CMapType 2 def\n1 begincodespacerange\n<0000> <FFFF>\nendcodespacerange\n1 beginbfrange\n<0000> <FFFF> <0000>\nendbfrange\nendcmap\nCMapName currentdict /CMap defineresource pop\nend\nend\n";
  672. $res = "<</Length " . mb_strlen($stream, '8bit') . " >>\n";
  673. $res .= "stream\n" . $stream . "\nendstream";
  674. $this->objects[$toUnicodeId]['c'] = $res;
  675. $cidFontId = ++$this->numObj;
  676. $this->o_fontDescendentCID($cidFontId, 'new', $options);
  677. $this->objects[$id]['info']['cidFont'] = $cidFontId;
  678. }
  679. // also tell the pages node about the new font
  680. $this->o_pages($this->currentNode,'font',array('fontNum'=>$fontNum,'objNum'=>$id));
  681. break;
  682. case 'add':
  683. foreach ($options as $k=>$v){
  684. switch ($k){
  685. case 'BaseFont':
  686. $o['info']['name'] = $v;
  687. break;
  688. case 'FirstChar':
  689. case 'LastChar':
  690. case 'Widths':
  691. case 'FontDescriptor':
  692. case 'SubType':
  693. $this->debug('o_font '.$k." : ".$v, E_USER_NOTICE);
  694. $o['info'][$k] = $v;
  695. break;
  696. }
  697. }
  698. // pass values down to descendent font
  699. if (isset($o['info']['cidFont'])) {
  700. $this->o_fontDescendentCID($o['info']['cidFont'], 'add', $options);
  701. }
  702. break;
  703. case 'out':
  704. $fontFileName = &$o['info']['fontFileName'];
  705. // when font program is embedded and its not a coreFont, attach the font either as subset or completely
  706. if($this->embedFont && !in_array(strtolower($o['info']['name']), $this->coreFonts)){
  707. // when TrueType font is used
  708. if(isset($this->objects[$o['info']['FontDescriptor']]['info']['FontFile2'])){
  709. // find font program id for TTF fonts (FontFile2)
  710. $pfbid = $this->objects[$o['info']['FontDescriptor']]['info']['FontFile2'];
  711. // if subsetting is set
  712. if($this->fonts[$fontFileName]['isSubset'] && $this->fonts[$fontFileName]['isUnicode']){
  713. $this->debug('subset font for ' . $fontFileName, E_USER_NOTICE);
  714. $subsetFontName = "AAAAAD+" . $o['info']['name'];
  715. $o['info']['name'] = $subsetFontName;
  716. // find descendant font
  717. $this->objects[$o['info']['cidFont']]['info']['name'] = $subsetFontName;
  718. // find font descriptor
  719. $this->objects[$o['info']['FontDescriptor']]['info']['FontName'] = $subsetFontName;
  720. // use TTF subset script from http://www.4real.gr/technical-documents-ttf-subset.html
  721. $t = new TTFsubset();
  722. // combine all used characters as string
  723. $s = implode('',array_keys($this->fonts[$fontFileName]['subset']));
  724. // submit the string to TTFsubset class to return the subset (as binary)
  725. $data = $t->doSubset($this->fontPath.'/'.$fontFileName . '.ttf', $s, null);
  726. // $data is the new (subset) of the font font
  727. //file_put_contents('/tmp/'.$o['info']['name'] . '.ttf', $data);
  728. $newcidwidth = array();
  729. $cidwidth = &$this->fonts[$fontFileName]['CIDWidths'];
  730. foreach($t->TTFchars as $TTFchar){
  731. if(!empty($TTFchar->charCode) && isset($cidwidth[$TTFchar->charCode])){
  732. $newcidwidth[$TTFchar->charCode] = $cidwidth[$TTFchar->charCode];
  733. }
  734. }
  735. $cidwidth = $newcidwidth;
  736. } else {
  737. $data = file_get_contents($this->fontPath.'/'.$fontFileName. '.ttf');
  738. }
  739. // TODO: cache the subset
  740. $l1 = strlen($data);
  741. $this->objects[$pfbid]['c'].= $data;
  742. $this->o_contents($pfbid,'add',array('Length1'=>$l1));
  743. } else if(isset($this->objects[$o['info']['FontDescriptor']]['info']['FontFile'])) {
  744. // find FontFile id - used for PFB fonts
  745. $pfbid = $this->objects[$o['info']['FontDescriptor']]['info']['FontFile'];
  746. $data = file_get_contents($this->fontPath.'/'.$fontFileName. '.pfb');
  747. $l1 = strpos($data,'eexec')+6;
  748. $l2 = strpos($data,'00000000')-$l1;
  749. $l3 = strlen($data)-$l2-$l1;
  750. $this->o_contents($pfbid,'add',array('Length1'=>$l1,'Length2'=>$l2,'Length3'=>$l3));
  751. } else {
  752. $this->debug("Failed to select the correct font program", E_USER_WARNING);
  753. }
  754. }
  755. if ($this->fonts[$fontFileName]['isUnicode']) {
  756. // For Unicode fonts, we need to incorporate font data into
  757. // sub-sections that are linked from the primary font section.
  758. // Look at o_fontGIDtoCID and o_fontDescendentCID functions
  759. // for more informaiton.
  760. //
  761. // All of this code is adapted from the excellent changes made to
  762. // transform FPDF to TCPDF (http://tcpdf.sourceforge.net/)
  763. $res = "\n$id 0 obj\n<</Type /Font /Subtype /Type0 /BaseFont /".$o['info']['name']."";
  764. // The horizontal identity mapping for 2-byte CIDs; may be used
  765. // with CIDFonts using any Registry, Ordering, and Supplement values.
  766. $res.= " /Encoding /Identity-H /DescendantFonts [".$o['info']['cidFont']." 0 R] /ToUnicode ".$o['info']['toUnicode']." 0 R >>\n";
  767. $res.= "endobj";
  768. } else {
  769. $res="\n".$id." 0 obj\n<< /Type /Font /Subtype /".$o['info']['SubType']." ";
  770. $res.="/Name /F".$o['info']['fontNum']." ";
  771. $res.="/BaseFont /".$o['info']['name']." ";
  772. if (isset($o['info']['encodingDictionary'])){
  773. // then place a reference to the dictionary
  774. $res.="/Encoding ".$o['info']['encodingDictionary']." 0 R ";
  775. } else if (isset($o['info']['encoding'])){
  776. // use the specified encoding
  777. $res.="/Encoding /".$o['info']['encoding']." ";
  778. }
  779. if (isset($o['info']['FirstChar'])){
  780. $res.="/FirstChar ".$o['info']['FirstChar']." ";
  781. }
  782. if (isset($o['info']['LastChar'])){
  783. $res.="/LastChar ".$o['info']['LastChar']." ";
  784. }
  785. if (isset($o['info']['Widths'])){
  786. $res.="/Widths ".$o['info']['Widths']." 0 R ";
  787. }
  788. if (isset($o['info']['FontDescriptor'])){
  789. $res.="/FontDescriptor ".$o['info']['FontDescriptor']." 0 R ";
  790. }
  791. $res.=">>\nendobj";
  792. }
  793. return $res;
  794. break;
  795. }
  796. }
  797. /**
  798. * a font descriptor, needed for including additional fonts
  799. */
  800. private function o_fontDescriptor($id, $action, $options = '')
  801. {
  802. if ($action!='new'){
  803. $o =& $this->objects[$id];
  804. }
  805. switch ($action){
  806. case 'new':
  807. $this->objects[$id]=array('t'=>'fontDescriptor','info'=>$options);
  808. break;
  809. case 'out':
  810. $res="\n".$id." 0 obj\n<< /Type /FontDescriptor ";
  811. foreach ($o['info'] as $label => $value){
  812. switch ($label){
  813. case 'Ascent':
  814. case 'CapHeight':
  815. case 'Descent':
  816. case 'Flags':
  817. case 'ItalicAngle':
  818. case 'StemV':
  819. case 'AvgWidth':
  820. case 'Leading':
  821. case 'MaxWidth':
  822. case 'MissingWidth':
  823. case 'StemH':
  824. case 'XHeight':
  825. case 'CharSet':
  826. if (strlen($value)){
  827. $res.='/'.$label.' '.$value." ";
  828. }
  829. break;
  830. case 'FontFile':
  831. case 'FontFile2':
  832. case 'FontFile3':
  833. $res.='/'.$label.' '.$value." 0 R ";
  834. break;
  835. case 'FontBBox':
  836. $res.='/'.$label.' ['.$value[0].' '.$value[1].' '.$value[2].' '.$value[3]."] ";
  837. break;
  838. case 'FontName':
  839. $res.='/'.$label.' /'.$value." ";
  840. break;
  841. }
  842. }
  843. $res.=">>\nendobj";
  844. return $res;
  845. break;
  846. }
  847. }
  848. /**
  849. * the font encoding
  850. */
  851. private function o_fontEncoding($id,$action,$options=''){
  852. if ($action!='new'){
  853. $o =& $this->objects[$id];
  854. }
  855. switch ($action){
  856. case 'new':
  857. // the options array should contain 'differences' and maybe 'encoding'
  858. $this->objects[$id]=array('t'=>'fontEncoding','info'=>$options);
  859. break;
  860. case 'out':
  861. $res="\n".$id." 0 obj\n<< /Type /Encoding ";
  862. if (!isset($o['info']['encoding'])){
  863. $o['info']['encoding']='WinAnsiEncoding';
  864. }
  865. if ($o['info']['encoding']!='none'){
  866. $res.="/BaseEncoding /".$o['info']['encoding']." ";
  867. }
  868. $res.="/Differences [";
  869. $onum=-100;
  870. foreach($o['info']['differences'] as $num=>$label){
  871. if ($num!=$onum+1){
  872. // we cannot make use of consecutive numbering
  873. $res.= " ".$num." /".$label;
  874. } else {
  875. $res.= " /".$label;
  876. }
  877. $onum=$num;
  878. }
  879. $res.="] >>\nendobj";
  880. return $res;
  881. break;
  882. }
  883. }
  884. /**
  885. * a descendent cid font, needed for unicode fonts
  886. */
  887. private function o_fontDescendentCID($id, $action, $options = '') {
  888. if ($action !== 'new') {
  889. $o = & $this->objects[$id];
  890. }
  891. switch ($action) {
  892. case 'new':
  893. $this->objects[$id] = array('t' => 'fontDescendentCID', 'info' => $options);
  894. // and a CID to GID map
  895. if($this->embedFont){
  896. $cidToGidMapId = ++$this->numObj;
  897. $this->o_fontGIDtoCIDMap($cidToGidMapId, 'new', $options);
  898. $this->objects[$id]['info']['cidToGidMap'] = $cidToGidMapId;
  899. }
  900. break;
  901. case 'add':
  902. foreach ($options as $k => $v) {
  903. switch ($k) {
  904. case 'BaseFont':
  905. $o['info']['name'] = $v;
  906. break;
  907. case 'FirstChar':
  908. case 'LastChar':
  909. case 'MissingWidth':
  910. case 'FontDescriptor':
  911. case 'SubType':
  912. $this->debug("o_fontDescendentCID $k : $v", E_USER_NOTICE);
  913. $o['info'][$k] = $v;
  914. break;
  915. }
  916. }
  917. // pass values down to cid to gid map
  918. if($this->embedFont){
  919. $this->o_fontGIDtoCIDMap($o['info']['cidToGidMap'], 'add', $options);
  920. }
  921. break;
  922. case 'out':
  923. $fontFileName = &$o['info']['fontFileName'];
  924. $res = "\n$id 0 obj\n";
  925. $res.= "<</Type /Font /Subtype /CIDFontType2 /BaseFont /".$o['info']['name']." /CIDSystemInfo << /Registry (Adobe) /Ordering (Identity) /Supplement 0 >>";
  926. if (isset($o['info']['FontDescriptor'])) {
  927. $res.= " /FontDescriptor ".$o['info']['FontDescriptor']." 0 R";
  928. }
  929. if (isset($o['info']['MissingWidth'])) {
  930. $res.= " /DW ".$o['info']['MissingWidth']."";
  931. }
  932. if (isset($fontFileName) && isset($this->fonts[$fontFileName]['CIDWidths'])) {
  933. $cid_widths = &$this->fonts[$fontFileName]['CIDWidths'];
  934. $res.= ' /W [';
  935. reset($cid_widths);
  936. $opened = false;
  937. while (list($k,$v) = each($cid_widths)) {
  938. list($nextk, $nextv) = each($cid_widths);
  939. //echo "\n$k ($v) == $nextk ($nextv)";
  940. if(($k + 1) == $nextk){
  941. if(!$opened){
  942. $res.= " $k [$v";
  943. $opened = true;
  944. } else if($opened) {
  945. $res.= ' '.$v;
  946. }
  947. prev($cid_widths);
  948. } else {
  949. if($opened){
  950. $res.=" $v]";
  951. } else {
  952. $res.= " $k [$v]";
  953. }
  954. $opened = false;
  955. prev($cid_widths);
  956. }
  957. }
  958. if(isset($nextk) && isset($nextv)){
  959. if($opened){
  960. $res.= "]";
  961. }
  962. $res.= " $nextk [$nextv]";
  963. }
  964. $res.= ' ]';
  965. }
  966. if($this->embedFont){
  967. $res.= " /CIDToGIDMap ".$o['info']['cidToGidMap']." 0 R";
  968. }
  969. $res.= " >>\n";
  970. $res.= "endobj";
  971. return $res;
  972. }
  973. }
  974. /**
  975. * a font glyph to character map, needed for unicode fonts
  976. */
  977. private function o_fontGIDtoCIDMap($id, $action, $options = '') {
  978. if ($action !== 'new') {
  979. $o = & $this->objects[$id];
  980. }
  981. switch ($action) {
  982. case 'new':
  983. $this->objects[$id] = array('t' => 'fontGIDtoCIDMap', 'info' => $options);
  984. break;
  985. case 'out':
  986. $res = "\n$id 0 obj\n";
  987. $fontFileName = &$o['info']['fontFileName'];
  988. $tmp = $this->fonts[$fontFileName]['CIDtoGID'] = base64_decode($this->fonts[$fontFileName]['CIDtoGID']);
  989. if (isset($o['raw'])) {
  990. $res.= $tmp;
  991. } else {
  992. $res.= "<<";
  993. if (function_exists('gzcompress') && $this->options['compression']) {
  994. // then implement ZLIB based compression on this content stream
  995. $tmp = gzcompress($tmp, $this->options['compression']);
  996. $res.= " /Filter /FlateDecode";
  997. }
  998. $res.= " /Length ".mb_strlen($tmp, '8bit') .">>\nstream\n$tmp\nendstream";
  999. }
  1000. $res.= "\nendobj";
  1001. return $res;
  1002. }
  1003. }
  1004. /**
  1005. * define the document information
  1006. */
  1007. private function o_info($id,$action,$options=''){
  1008. if ($action!='new'){
  1009. $o =& $this->objects[$id];
  1010. }
  1011. switch ($action){
  1012. case 'new':
  1013. $this->infoObject=$id;
  1014. $date='D:'.date('YmdHis')."-00'00";
  1015. $this->objects[$id]=array('t'=>'info','info'=>array('Creator'=>'R&OS php pdf class, http://pdf-php.sf.net/','CreationDate'=>$date));
  1016. break;
  1017. case 'Title':
  1018. case 'Author':
  1019. case 'Subject':
  1020. case 'Keywords':
  1021. case 'Creator':
  1022. case 'Producer':
  1023. case 'CreationDate':
  1024. case 'ModDate':
  1025. case 'Trapped':
  1026. $o['info'][$action]=$options;
  1027. break;
  1028. case 'out':
  1029. if ($this->encryptionMode > 0){
  1030. $this->encryptInit($id);
  1031. }
  1032. $res="\n".$id." 0 obj\n<< ";
  1033. foreach ($o['info'] as $k=>$v){
  1034. $res.='/'.$k.' (';
  1035. if ($this->encryptionMode > 0){
  1036. $res.=$this->filterText($this->ARC4($v), true, false);
  1037. } else {
  1038. $res.=$this->filterText($v, true, false);
  1039. }
  1040. $res.=") ";
  1041. }
  1042. $res.=">>\nendobj";
  1043. return $res;
  1044. break;
  1045. }
  1046. }
  1047. /**
  1048. * an action object, used to link to URLS initially
  1049. * In version >= 0.12.2 internal and external links are handled in o_annotation directly
  1050. * Additional actions, like SubmitForm, ResetForm, ImportData, Javascript will be part of
  1051. * o_actions. Unless we also do not handle them similar to Links.
  1052. *
  1053. */
  1054. private function o_action($id,$action,$options=''){
  1055. if ($action!='new'){
  1056. $o =& $this->objects[$id];
  1057. }
  1058. switch ($action){
  1059. case 'new':
  1060. if (is_array($options)){
  1061. $this->objects[$id]=array('t'=>'action','info'=>$options,'type'=>$options['type']);
  1062. } else {
  1063. // then assume a URI action
  1064. $this->objects[$id]=array('t'=>'action','info'=>$options,'type'=>'URI');
  1065. }
  1066. break;
  1067. case 'out':
  1068. if ($this->encryptionMode > 0){
  1069. $this->encryptInit($id);
  1070. }
  1071. $res="\n".$id." 0 obj\n<< /Type /Action";
  1072. switch($o['type']){
  1073. case 'ilink':
  1074. // there will be an 'label' setting, this is the name of the destination
  1075. $res.=" /S /GoTo /D ".$this->destinations[(string)$o['info']['label']]." 0 R";
  1076. break;
  1077. case 'URI':
  1078. $res.=" /S /URI /URI (";
  1079. if ($this->encryptionMode > 0){
  1080. $res.=$this->filterText($this->ARC4($o['info']), true, false);
  1081. } else {
  1082. $res.=$this->filterText($o['info'], true, false);
  1083. }
  1084. $res.=")";
  1085. break;
  1086. }
  1087. $res.=" >>\nendobj";
  1088. return $res;
  1089. break;
  1090. }
  1091. }
  1092. /**
  1093. * an annotation object, this will add an annotation to the current page.
  1094. * initially will support just link annotations
  1095. */
  1096. private function o_annotation($id,$action,$options=''){
  1097. if ($action!='new'){
  1098. $o =& $this->objects[$id];
  1099. }
  1100. switch ($action){
  1101. case 'new':
  1102. // add the annotation to the current page
  1103. $pageId = $this->currentPage;
  1104. $this->o_page($pageId,'annot',$id);
  1105. // and add the action object which is going to be required
  1106. switch($options['type']){
  1107. case 'link':
  1108. $this->objects[$id]=array('t'=>'annotation','info'=>$options);
  1109. //$this->numObj++;
  1110. //$this->o_action($this->numObj,'new',$options['url']);
  1111. //$this->objects[$id]['info']['actionId']=$this->numObj;
  1112. break;
  1113. case 'ilink':
  1114. // this is to a named internal link
  1115. $label = $options['label'];
  1116. $this->objects[$id]=array('t'=>'annotation','info'=>$options);
  1117. //$this->numObj++;
  1118. //$this->o_action($this->numObj,'new',array('type'=>'ilink','label'=>$label));
  1119. //$this->objects[$id]['info']['actionId']=$this->numObj;
  1120. break;
  1121. case 'text':
  1122. $this->objects[$id]=array('t'=>'annotation','info'=>$options);
  1123. break;
  1124. }
  1125. break;
  1126. case 'out':
  1127. $res="\n".$id." 0 obj\n<< /Type /Annot";
  1128. switch($o['info']['type']){
  1129. case 'link':
  1130. $res.= " /Subtype /Link";
  1131. $res.=" /A << /S /URI /URI (".$o['info']['url'].") >>";
  1132. $res.=" /Border [0 0 0]";
  1133. $res.=" /H /I";
  1134. break;
  1135. case 'ilink':
  1136. $res.= " /Subtype /Link";
  1137. if(isset($this->destinations[(string)$o['info']['label']])){
  1138. $res.=" /A << /S /GoTo /D ".$this->destinations[(string)$o['info']['label']]." 0 R >>";
  1139. }
  1140. $res.=" /Border [0 0 0]";
  1141. $res.=" /H /I";
  1142. break;
  1143. case 'text':
  1144. $res.= " /Subtype /Text";
  1145. $res.= " /T (".$this->filterText($o['info']['title'], false, false).") /Contents (".$o['info']['content'].")";
  1146. break;
  1147. }
  1148. $res.=" /Rect [ ";
  1149. foreach($o['info']['rect'] as $v){
  1150. $res.= sprintf("%.4F ",$v);
  1151. }
  1152. $res.="]";
  1153. $res.=" >>\nendobj";
  1154. return $res;
  1155. break;
  1156. }
  1157. }
  1158. /**
  1159. * a page object, it also creates a contents object to hold its contents
  1160. */
  1161. private function o_page($id,$action,$options=''){
  1162. if ($action!='new'){
  1163. $o =& $this->objects[$id];
  1164. }
  1165. switch ($action){
  1166. case 'new':
  1167. $this->numPages++;
  1168. $this->objects[$id]=array('t'=>'page','info'=>array('parent'=>$this->currentNode,'pageNum'=>$this->numPages));
  1169. if (is_array($options)){
  1170. // then this must be a page insertion, array shoudl contain 'rid','pos'=[before|after]
  1171. $options['id']=$id;
  1172. $this->o_pages($this->currentNode,'page',$options);
  1173. } else {
  1174. $this->o_pages($this->currentNode,'page',$id);
  1175. }
  1176. $this->currentPage=$id;
  1177. // make a contents object to go with this page
  1178. $this->numObj++;
  1179. $this->o_contents($this->numObj,'new',$id);
  1180. $this->currentContents=$this->numObj;
  1181. $this->objects[$id]['info']['contents']=array();
  1182. $this->objects[$id]['info']['contents'][]=$this->numObj;
  1183. $match = ($this->numPages%2 ? 'odd' : 'even');
  1184. foreach($this->addLooseObjects as $oId=>$target){
  1185. if ($target=='all' || $match==$target){
  1186. $this->objects[$id]['info']['contents'][]=$oId;
  1187. }
  1188. }
  1189. break;
  1190. case 'content':
  1191. $o['info']['contents'][]=$options;
  1192. break;
  1193. case 'annot':
  1194. // add an annotation to this page
  1195. if (!isset($o['info']['annot'])){
  1196. $o['info']['annot']=array();
  1197. }
  1198. // $options should contain the id of the annotation dictionary
  1199. $o['info']['annot'][]=$options;
  1200. break;
  1201. case 'out':
  1202. $res="\n".$id." 0 obj\n<< /Type /Page";
  1203. $res.=" /Parent ".$o['info']['parent']." 0 R";
  1204. if (isset($o['info']['annot'])){
  1205. $res.=" /Annots [";
  1206. foreach($o['info']['annot'] as $aId){
  1207. $res.=" ".$aId." 0 R";
  1208. }
  1209. $res.=" ]";
  1210. }
  1211. $count = count($o['info']['contents']);
  1212. if ($count==1){
  1213. $res.=" /Contents ".$o['info']['contents'][0]." 0 R";
  1214. } else if ($count>1){
  1215. $res.=" /Contents [ ";
  1216. foreach ($o['info']['contents'] as $cId){
  1217. $res.=$cId." 0 R ";
  1218. }
  1219. $res.="]";
  1220. }
  1221. $res.=" >>\nendobj";
  1222. return $res;
  1223. break;
  1224. }
  1225. }
  1226. /**
  1227. * the contents objects hold all of the content which appears on pages
  1228. */
  1229. private function o_contents($id,$action,$options=''){
  1230. if ($action!='new'){
  1231. $o =& $this->objects[$id];
  1232. }
  1233. switch ($action){
  1234. case 'new':
  1235. $this->objects[$id]=array('t'=>'contents','c'=>'','info'=>array());
  1236. if (strlen($options) && intval($options)){
  1237. // then this contents is the primary for a page
  1238. $this->objects[$id]['onPage']=$options;
  1239. } else if ($options=='raw'){
  1240. // then this page contains some other type of system object
  1241. $this->objects[$id]['raw']=1;
  1242. }
  1243. break;
  1244. case 'add':
  1245. // add more options to the decleration
  1246. foreach ($options as $k=>$v){
  1247. $o['info'][$k]=$v;
  1248. }
  1249. case 'out':
  1250. $tmp=$o['c'];
  1251. $res= "\n".$id." 0 obj\n";
  1252. if (isset($this->objects[$id]['raw'])){
  1253. $res.=$tmp;
  1254. } else {
  1255. $res.= "<<";
  1256. if (function_exists('gzcompress') && $this->options['compression']){
  1257. // then implement ZLIB based compression on this content stream
  1258. $res.=" /Filter /FlateDecode";
  1259. $tmp = gzcompress($tmp, $this->options['compression']);
  1260. }
  1261. if ($this->encryptionMode > 0){
  1262. $this->encryptInit($id);
  1263. $tmp = $this->ARC4($tmp);
  1264. }
  1265. foreach($o['info'] as $k=>$v){
  1266. $res .= " /".$k.' '.$v;
  1267. }
  1268. $res.=" /Length ".strlen($tmp)." >> stream\n".$tmp."\nendstream";
  1269. }
  1270. $res.="\nendobj";
  1271. return $res;
  1272. break;
  1273. }
  1274. }
  1275. /**
  1276. * an image object, will be an XObject in the document, includes description and data
  1277. */
  1278. private function o_image($id,$action,$options=''){
  1279. if ($action!='new'){
  1280. $o =& $this->objects[$id];
  1281. }
  1282. switch($action){
  1283. case 'new':
  1284. // make the new object
  1285. $this->objects[$id]=array('t'=>'image','data'=>$options['data'],'info'=>array());
  1286. $this->objects[$id]['info']['Type']='/XObject';
  1287. $this->objects[$id]['info']['Subtype']='/Image';
  1288. $this->objects[$id]['info']['Width']=$options['iw'];
  1289. $this->objects[$id]['info']['Height']=$options['ih'];
  1290. if (!isset($options['type']) || $options['type']=='jpg'){
  1291. if (!isset($options['channels'])){
  1292. $options['channels']=3;
  1293. }
  1294. switch($options['channels']){
  1295. case 1:
  1296. $this->objects[$id]['info']['ColorSpace']='/DeviceGray';
  1297. break;
  1298. default:
  1299. $this->objects[$id]['info']['ColorSpace']='/DeviceRGB';
  1300. break;
  1301. }
  1302. $this->objects[$id]['info']['Filter']='/DCTDecode';
  1303. $this->objects[$id]['info']['BitsPerComponent']=8;
  1304. } else if ($options['type']=='png'){
  1305. if (strlen($options['pdata'])){
  1306. $this->numObj++;
  1307. $this->objects[$this->numObj]=array('t'=>'image','c'=>'','info'=>array());
  1308. $this->objects[$this->numObj]['info'] = array('Type'=>'/XObject', 'Subtype'=>'/Image', 'Width'=> $options['iw'], 'Height'=> $options['ih'], 'ColorSpace'=>'/DeviceGray', 'BitsPerComponent'=>'8', 'DecodeParms'=>'<< /Predictor 15 /Colors 1 /BitsPerComponent 8 /Columns '.$options['iw'].' >>');
  1309. $this->objects[$this->numObj]['data']=$options['pdata'];
  1310. if (isset($options['transparency'])){
  1311. switch($options['transparency']['type']){
  1312. case 'indexed':
  1313. // temporary no transparency for indexed PNG images
  1314. //$tmp=' [ '.$options['transparency']['data'].' '.$options['transparency']['data'].'] ';
  1315. //$this->objects[$id]['info']['Mask'] = $tmp;
  1316. $this->objects[$id]['info']['ColorSpace'] = ' [ /Indexed /DeviceRGB '.(strlen($options['pdata'])/3-1).' '.$this->numObj.' 0 R ]';
  1317. break;
  1318. case 'alpha':
  1319. $this->objects[$this->numObj]['info']['Filter'] = '/FlateDecode';
  1320. $this->objects[$id]['info']['SMask'] = $this->numObj.' 0 R';
  1321. $this->objects[$id]['info']['ColorSpace'] = '/'.$options['color'];
  1322. break;
  1323. }
  1324. }
  1325. } else {
  1326. $this->objects[$id]['info']['ColorSpace']='/'.$options['color'];
  1327. }
  1328. $this->objects[$id]['info']['BitsPerComponent']=$options['bitsPerComponent'];
  1329. $this->objects[$id]['info']['Filter']='/FlateDecode';
  1330. $this->objects[$id]['data'] = $options['data'];
  1331. $this->objects[$id]['info']['DecodeParms']='<< /Predictor 15 /Colors '.$options['ncolor'].' /Columns '.$options['iw'].' /BitsPerComponent '.$options['bitsPerComponent'].'>>';
  1332. }
  1333. // assign it a place in the named resource dictionary as an external object, according to
  1334. // the label passed in with it.
  1335. $this->o_pages($this->currentNode,'xObject',array('label'=>$options['label'],'objNum'=>$id));
  1336. break;
  1337. case 'out':
  1338. $tmp=$o['data'];
  1339. $res= "\n".$id." 0 obj\n<<";
  1340. foreach($o['info'] as $k=>$v){
  1341. $res.=" /".$k.' '.$v;
  1342. }
  1343. if ($this->encryptionMode > 0){
  1344. $this->encryptInit($id);
  1345. $tmp = $this->ARC4($tmp);
  1346. }
  1347. $res.=" /Length ".strlen($tmp)." >> stream\n".$tmp."\nendstream\nendobj";
  1348. return $res;
  1349. break;
  1350. }
  1351. }
  1352. /**
  1353. * encryption object.
  1354. */
  1355. private function o_encryption($id,$action,$options=''){
  1356. if ($action!='new'){
  1357. $o =& $this->objects[$id];
  1358. }
  1359. switch($action){
  1360. case 'new':
  1361. // make the new object
  1362. $this->objects[$id]=array('t'=>'encryption','info'=>$options);
  1363. $this->arc4_objnum=$id;
  1364. // Pad or truncate the owner password
  1365. $owner = substr($options['owner'].$this->encryptionPad,0,32);
  1366. $user = substr($options['user'].$this->encryptionPad,0,32);
  1367. $this->debug("o_encryption: user password (".$options['user'].") / owner password (".$options['owner'].")");
  1368. // convert permission set into binary string
  1369. $permissions = sprintf("%c%c%c%c", ($options['p'] & 255), (($options['p'] >> 8) & 255) , (($options['p'] >> 16) & 255), (($options['p'] >> 24) & 255));
  1370. // Algo 3.3 Owner Password being set into /O Dictionary
  1371. $this->objects[$id]['info']['O'] = $this->encryptOwner($owner, $user);
  1372. // Algo 3.5 User Password - START
  1373. $this->objects[$id]['info']['U'] = $this->encryptUser($user, $this->objects[$id]['info']['O'], $permissions);
  1374. // encryption key is set in encryptUser function
  1375. break;
  1376. case 'out':
  1377. $res= "\n".$id." 0 obj\n<<";
  1378. $res.=' /Filter /Standard';
  1379. if($this->encryptionMode > 1){ // RC4 128bit encryption
  1380. $res.=' /V 2';
  1381. $res.=' /R 3';
  1382. $res.=' /Length 128';
  1383. } else { // RC4 40bit encryption
  1384. $res.=' /V 1';
  1385. $res.=' /R 2';
  1386. }
  1387. // use hex string instead of char code - char codes can make troubles (E.g. CR or LF)
  1388. $res.=' /O <'.$this->strToHex($o['info']['O']).'>';
  1389. $res.=' /U <'.$this->strToHex($o['info']['U']).'>';
  1390. // and the p-value needs to be converted to account for the twos-complement approach
  1391. //$o['info']['p'] = (($o['info']['p'] ^ 0xFFFFFFFF)+1)*-1;
  1392. $res.=' /P '.($o['info']['p']);
  1393. $res.=" >>\nendobj";
  1394. return $res;
  1395. break;
  1396. }
  1397. }
  1398. /**
  1399. * owner part of the encryption
  1400. * @param $owner - owner password plus padding
  1401. * @param $user - user password plus padding
  1402. */
  1403. private function encryptOwner($owner, $user){
  1404. $keylength = 5;
  1405. if($this->encryptionMode > 1){
  1406. $keylength = 16;
  1407. }
  1408. $ownerHash = $this->md5_16($owner); // PDF 1.4 - repeat this 50 times in revision 3
  1409. if($this->encryptionMode > 1) { // if it is the RC4 128bit encryption
  1410. for($i = 0; $i < 50; $i++){
  1411. $ownerHash = $this->md5_16($ownerHash);
  1412. }
  1413. }
  1414. $ownerKey = substr($ownerHash,0,$keylength); // PDF 1.4 - Create the encryption key (IMPORTANT: need to check Length)
  1415. $this->ARC4_init($ownerKey); // 5 bytes of the encryption key (hashed 50 times)
  1416. $ovalue=$this->ARC4($user); // PDF 1.4 - Encrypt the padded user password using RC4
  1417. if($this->encryptionMode > 1){
  1418. $len = strlen($ownerKey);
  1419. for($i = 1;$i<=19; ++$i){
  1420. $ek = '';
  1421. for($j=0; $j < $len; $j++){
  1422. $ek .= chr( ord($ownerKey[$j]) ^ $i );
  1423. }
  1424. $this->ARC4_init($ek);
  1425. $ovalue = $this->ARC4($ovalue);
  1426. }
  1427. }
  1428. return $ovalue;
  1429. }
  1430. /**
  1431. *
  1432. * user part of the encryption
  1433. * @param $user - user password plus padding
  1434. * @param $ownerDict - encrypted owner entry
  1435. * @param $permissions - permission set (print, copy, modify, ...)
  1436. */
  1437. function encryptUser($user,$ownerDict, $permissions){
  1438. $keylength = 5;
  1439. if($this->encryptionMode > 1){
  1440. $keylength = 16;
  1441. }
  1442. // make hash with user, encrypted owner, permission set and fileIdentifier
  1443. $hash = $this->md5_16($user.$ownerDict.$permissions.$this->hexToStr($this->fileIdentifier));
  1444. // loop thru the hash process when it is revision 3 of encryption routine (usually RC4 128bit)
  1445. if($this->encryptionMode > 1) {
  1446. for ($i = 0; $i < 50; ++$i) {
  1447. $hash = $this->md5_16(substr($hash, 0, $keylength)); // use only length of encryption key from the previous hash
  1448. }
  1449. }
  1450. $this->encryptionKey = substr($hash,0,$keylength); // PDF 1.4 - Create the encryption key (IMPORTANT: need to check Length)
  1451. if($this->encryptionMode > 1){ // if it is the RC4 128bit encryption
  1452. // make a md5 hash from padding string (hardcoded by Adobe) and the fileIdenfier
  1453. $userHash = $this->md5_16($this->encryptionPad.$this->hexToStr($this->fileIdentifier));
  1454. // encrypt the hash from the previous method by using the encryptionKey
  1455. $this->ARC4_init($this->encryptionKey);
  1456. $uvalue=$this->ARC4($userHash);
  1457. $len = strlen($this->encryptionKey);
  1458. for($i = 1;$i<=19; ++$i){
  1459. $ek = '';
  1460. for($j=0; $j< $len; $j++){
  1461. $ek .= chr( ord($this->encryptionKey[$j]) ^ $i );
  1462. }
  1463. $this->ARC4_init($ek);
  1464. $uvalue = $this->ARC4($uvalue);
  1465. }
  1466. $uvalue .= substr($this->encryptionPad,0,16);
  1467. }else{ // if it is the RC4 40bit encryption
  1468. $this->ARC4_init($this->encryptionKey);
  1469. $uvalue=$this->ARC4($this->encryptionPad);
  1470. }
  1471. return $uvalue;
  1472. }
  1473. /**
  1474. * internal method to convert string to hexstring (used for owner and user dictionary)
  1475. * @param $string - any string value
  1476. */
  1477. private function strToHex($string)
  1478. {
  1479. $hex = '';
  1480. for ($i=0; $i < strlen($string); $i++)
  1481. $hex .= sprintf("%02x",ord($string[$i]));
  1482. return $hex;
  1483. }
  1484. private function hexToStr($hex)
  1485. {
  1486. $str = '';
  1487. for($i=0;$i<strlen($hex);$i+=2)
  1488. $str .= chr(hexdec(substr($hex,$i,2)));
  1489. return $str;
  1490. }
  1491. /**
  1492. * calculate the 16 byte version of the 128 bit md5 digest of the string
  1493. */
  1494. private function md5_16($string){
  1495. $tmp = md5($string);
  1496. $out = pack("H*", $tmp);
  1497. return $out;
  1498. }
  1499. /**
  1500. * initialize the encryption for processing a particular object
  1501. */
  1502. private function encryptInit($id){
  1503. $tmp = $this->encryptionKey;
  1504. $hex = dechex($id);
  1505. if (strlen($hex)<6){
  1506. $hex = substr('000000',0,6-strlen($hex)).$hex;
  1507. }
  1508. $tmp.= chr(hexdec(substr($hex,4,2))).chr(hexdec(substr($hex,2,2))).chr(hexdec(substr($hex,0,2))).chr(0).chr(0);
  1509. $key = $this->md5_16($tmp);
  1510. if($this->encryptionMode > 1){
  1511. $this->ARC4_init(substr($key,0,16)); // use max 16 bytes for RC4 128bit encryption key
  1512. } else {
  1513. $this->ARC4_init(substr($key,0,10)); // use (n + 5 bytes) for RC4 40bit encryption key
  1514. }
  1515. }
  1516. /**
  1517. * initialize the ARC4 encryption
  1518. */
  1519. private function ARC4_init($key=''){
  1520. $this->arc4 = '';
  1521. // setup the control array
  1522. if (strlen($key)==0){
  1523. return;
  1524. }
  1525. $k = '';
  1526. while(strlen($k)<256){
  1527. $k.=$key;
  1528. }
  1529. $k=substr($k,0,256);
  1530. for ($i=0;$i<256;$i++){
  1531. $this->arc4 .= chr($i);
  1532. }
  1533. $j=0;
  1534. for ($i=0;$i<256;$i++){
  1535. $t = $this->arc4[$i];
  1536. $j = ($j + ord($t) + ord($k[$i]))%256;
  1537. $this->arc4[$i]=$this->arc4[$j];
  1538. $this->arc4[$j]=$t;
  1539. }
  1540. }
  1541. /**
  1542. * ARC4 encrypt a text string
  1543. */
  1544. private function ARC4($text){
  1545. $len=strlen($text);
  1546. $a=0;
  1547. $b=0;
  1548. $c = $this->arc4;
  1549. $out='';
  1550. for ($i=0;$i<$len;$i++){
  1551. $a = ($a+1)%256;
  1552. $t= $c[$a];
  1553. $b = ($b+ord($t))%256;
  1554. $c[$a]=$c[$b];
  1555. $c[$b]=$t;
  1556. $k = ord($c[(ord($c[$a])+ord($c[$b]))%256]);
  1557. $out.=chr(ord($text[$i]) ^ $k);
  1558. }
  1559. return $out;
  1560. }
  1561. public function addComment($title, $text, $x, $y){
  1562. $this->numObj++;
  1563. $info = array('type'=>'text','title'=>$title, 'content'=>$text,'rect'=>array($x,$y,$x,$y));
  1564. $this->o_annotation($this->numObj,'new',$info);
  1565. }
  1566. /**
  1567. * add a link in the document to an external URL
  1568. * @param string $url URL address
  1569. * @param float $x0 bottom-left position in a rectangle
  1570. * @param float $y0 top-left position in a rectangle
  1571. * @param float $x0 bottom-right position in a rectangle
  1572. * @param float $x0 top-right position in a rectangle
  1573. */
  1574. public function addLink($url,$x0,$y0,$x1,$y1){
  1575. $this->numObj++;
  1576. $info = array('type'=>'link','url'=>$url,'rect'=>array($x0,$y0,$x1,$y1));
  1577. $this->o_annotation($this->numObj,'new',$info);
  1578. }
  1579. /**
  1580. * add a link in the document to an internal destination (ie. within the document)
  1581. * @param string $label label name of the destination
  1582. * @param float $x0 bottom-left position in a rectangle
  1583. * @param float $y0 top-left position in a rectangle
  1584. * @param float $x0 bottom-right position in a rectangle
  1585. * @param float $x0 top-right position in a rectangle
  1586. */
  1587. public function addInternalLink($label,$x0,$y0,$x1,$y1){
  1588. $this->numObj++;
  1589. $info = array('type'=>'ilink','label'=>$label,'rect'=>array($x0,$y0,$x1,$y1));
  1590. $this->o_annotation($this->numObj,'new',$info);
  1591. }
  1592. /**
  1593. * set the encryption of the document
  1594. * can be used to turn it on and/or set the passwords which it will have.
  1595. * also the functions that the user will have are set here, such as print, modify, add
  1596. */
  1597. public function setEncryption($userPass = '',$ownerPass = '',$pc = array(), $mode = 1){
  1598. if($mode > 1){
  1599. // increase the pdf version to support 128bit encryption
  1600. if($this->pdfversion < 1.4) $this->pdfversion = 1.4;
  1601. $p=bindec('01111111111111111111000011000000'); // revision 3 is using bit 3 - 6 AND 9 - 12
  1602. }else{
  1603. $mode = 1; // make sure at least the 40bit encryption is set
  1604. $p=bindec('01111111111111111111111111000000'); // while revision 2 is using bit 3 - 6 only
  1605. }
  1606. $options = array(
  1607. 'print'=>4
  1608. ,'modify'=>8
  1609. ,'copy'=>16
  1610. ,'add'=>32
  1611. ,'fill'=>256
  1612. ,'extract'=>512
  1613. ,'assemble'=>1024
  1614. ,'represent'=>2048
  1615. );
  1616. foreach($pc as $k=>$v){
  1617. if ($v && isset($options[$k])){
  1618. $p+=$options[$k];
  1619. } else if (isset($options[$v])){
  1620. $p+=$options[$v];
  1621. }
  1622. }
  1623. // set the encryption mode to either RC4 40bit or RC4 128bit
  1624. $this->encryptionMode = $mode;
  1625. // implement encryption on the document
  1626. if ($this->arc4_objnum == 0){
  1627. // then the block does not exist already, add it.
  1628. $this->numObj++;
  1629. if (strlen($ownerPass)==0){
  1630. $ownerPass=$userPass;
  1631. }
  1632. $this->o_encryption($this->numObj,'new',array('user'=>$userPass,'owner'=>$ownerPass,'p'=>$p));
  1633. }
  1634. }
  1635. /**
  1636. * should be used for internal checks, not implemented as yet
  1637. */
  1638. function checkAllHere() {
  1639. // set the validation flag to true when everything is ok.
  1640. // currently it only checks if output function has been called
  1641. $this->valid = true;
  1642. }
  1643. /**
  1644. * intialize a new document
  1645. * if this is called on an existing document results may be unpredictable, but the existing document would be lost at minimum
  1646. * this function is called automatically by the constructor function
  1647. *
  1648. */
  1649. protected function newDocument($pageSize=array(0,0,612,792)){
  1650. $this->numObj=0;
  1651. $this->objects = array();
  1652. $this->numObj++;
  1653. $this->o_catalog($this->numObj,'new');
  1654. $this->numObj++;
  1655. $this->o_outlines($this->numObj,'new');
  1656. $this->numObj++;
  1657. $this->o_pages($this->numObj,'new');
  1658. $this->o_pages($this->numObj,'mediaBox',$pageSize);
  1659. $this->currentNode = 3;
  1660. $this->o_pages($this->numObj, 'procset', '[/PDF/TEXT/ImageB/ImageC/ImageI]');
  1661. $this->numObj++;
  1662. $this->o_info($this->numObj,'new');
  1663. $this->numObj++;
  1664. $this->o_page($this->numObj,'new');
  1665. // need to store the first page id as there is no way to get it to the user during
  1666. // startup
  1667. $this->firstPageId = $this->currentContents;
  1668. }
  1669. /**
  1670. * open the font file and return a php structure containing it.
  1671. * first check if this one has been done before and saved in a form more suited to php
  1672. * note that if a php serialized version does not exist it will try and make one, but will
  1673. * require write access to the directory to do it... it is MUCH faster to have these serialized
  1674. * files.
  1675. *
  1676. * @param string $font Font name (can contain both path and extension)
  1677. *
  1678. * @return bool true on success, false on error
  1679. */
  1680. protected function openFont($font) {
  1681. // $font should only contain the font name
  1682. $fullFontPath = $this->fontPath.'/'.$font;
  1683. $this->debug('openFont: '.$fullFontPath.' / IsUnicode: '.$this->isUnicode);
  1684. // PATCH #13 - isUnicode cachedFile (font) problem | thank you jafjaf
  1685. if ($this->isUnicode){
  1686. $cachedFile = 'cached'.$font.'unicode.php';
  1687. } else {
  1688. $cachedFile = 'cached'.$font.'.php';
  1689. }
  1690. // use the temp folder to read/write cached font data
  1691. if (file_exists($this->tempPath.'/'.$cachedFile)) {
  1692. $cacheDate = filemtime($this->tempPath.'/'.$cachedFile);
  1693. if(($cacheDate + $this->cacheTimeout) >= time() ) {
  1694. $this->debug('openFont: font cache found in '.$this->tempPath.'/'.$cachedFile);
  1695. $this->fonts[$font] = require($this->tempPath.'/'.$cachedFile);
  1696. if (isset($this->fonts[$font]['_version_']) && $this->fonts[$font]['_version_'] == 3) {
  1697. // cache is valid - but without checking for a valid font path
  1698. return true;
  1699. }
  1700. }
  1701. }
  1702. // if no cache is found, parse the font file and rebuild the cache
  1703. $this->debug('openFont: rebuilding font cache '.$cachedFile, E_USER_NOTICE);
  1704. if(file_exists($fullFontPath.'.ttf') && class_exists('TTF')){
  1705. $ttf = new TTF(file_get_contents($fullFontPath.'.ttf'));
  1706. $head = $ttf->unmarshalHead();
  1707. $uname = $ttf->unmarshalName();
  1708. $hhea = $ttf->unmarshalHhea();
  1709. $post = $ttf->unmarshalPost(true);
  1710. $maxp = $ttf->unmarshalMAXP();
  1711. $cmap = $ttf->unmarshalCmap();
  1712. $cachedFont = array(
  1713. 'isUnicode' => $this->isUnicode,
  1714. 'ItalicAngle' => $post['italicAngle'],
  1715. 'UnderlineThickness' => $post['underlineThickness'],
  1716. 'UnderlinePosition' => $post['underlinePosition'],
  1717. 'IsFixedPitch' => ($post['isFixedPitch'] == 0)? false : true,
  1718. 'Ascender' => $hhea['ascender'],
  1719. 'Descender' => $hhea['descender'],
  1720. 'LineGap' => $hhea['lineGap'],
  1721. 'FontName' => $font
  1722. );
  1723. foreach($uname['nameRecords'] as $v){
  1724. if($v['nameID'] == 1 && $v['languageID'] == 0){
  1725. // fetch FontFamily from Default language (en?)
  1726. $cachedFont['FamilyName'] = preg_replace('/\x00/','',$v['value']);
  1727. } else if($v['nameID'] == 2 && $v['languageID'] == 0){
  1728. // fetch font weight from Default language (en?)
  1729. $cachedFont['Weight'] = preg_replace('/\x00/','',$v['value']);
  1730. } else if($v['nameID'] == 3 && $v['languageID'] == 0){
  1731. // fetch Unique font name from Default language (en?)
  1732. $cachedFont['UniqueName'] = preg_replace('/\x00/','',$v['value']);
  1733. } else if($v['nameID'] == 4 && $v['languageID'] == 0){
  1734. // fetch font name (full style) from Default language (en?)
  1735. $cachedFont['FullName'] = preg_replace('/\x00/','',$v['value']);
  1736. } else if($v['nameID'] == 5 && $v['languageID'] == 0){
  1737. // fetch version from Default language (en?)
  1738. $cachedFont['Version'] = preg_replace('/\x00/','',$v['value']);
  1739. }
  1740. }
  1741. // calculate the bounding box properly by using 'units per em' property
  1742. $cachedFont['FontBBox'] = array(
  1743. intval($head['xMin'] / ($head['unitsPerEm'] / 1000)),
  1744. intval($head['yMin'] / ($head['unitsPerEm'] / 1000)),
  1745. intval($head['xMax'] / ($head['unitsPerEm'] / 1000)),
  1746. intval($head['yMax'] / ($head['unitsPerEm'] / 1000))
  1747. );
  1748. $cachedFont['UnitsPerEm'] = $head['unitsPerEm'];
  1749. $encodingTable = array();
  1750. $hmetrics = $ttf->unmarshalHmtx($hhea['numberOfHMetrics'],$maxp['numGlyphs']);
  1751. // get format 6 or format 4 as primary cmap table map glyph with character
  1752. foreach($cmap['tables'] as $v){
  1753. if(isset($v['format']) && $v['format'] == "4"){
  1754. $encodingTable = $v;
  1755. break;
  1756. }
  1757. }
  1758. if($encodingTable['format'] == '4') {
  1759. $glyphsIndices = range(1, $maxp['numGlyphs']);
  1760. $charToGlyph = array();
  1761. $segCount = $encodingTable['segCount'];
  1762. $endCountArray = $encodingTable['endCountArray'];
  1763. $startCountArray = $encodingTable['startCountArray'];
  1764. $idDeltaArray = $encodingTable['idDeltaArray'];
  1765. $idRangeOffsetArray = $encodingTable['idRangeOffsetArray'];
  1766. $glyphIdArray = $encodingTable['glyphIdArray'];
  1767. for ($seg = 0; $seg < $segCount; $seg++) {
  1768. $endCount = $endCountArray[$seg];
  1769. $startCount = $startCountArray[$seg];
  1770. $idDelta = $idDeltaArray[$seg];
  1771. $idRangeOffset = $idRangeOffsetArray[$seg];
  1772. for ($charCode = $startCount; $charCode <= $endCount; $charCode++) {
  1773. if ($idRangeOffset != 0) {
  1774. $j = $charCode - $startCount + $seg + $idRangeOffset / 2 - $segCount;
  1775. $gid0 = $glyphIdArray[$j];
  1776. } else {
  1777. $gid0 = $idDelta + $charCode;
  1778. }
  1779. $gid0 %= 65536;
  1780. if (in_array($gid0, $glyphsIndices)) {
  1781. $charToGlyph[sprintf("%d", $charCode)] = $gid0;
  1782. }
  1783. }
  1784. }
  1785. if($this->isUnicode)
  1786. $cidtogid = str_pad('', 256*256*2, "\x00");
  1787. $cachedFont['C'] = array();
  1788. foreach($charToGlyph as $char => $glyphIndex){
  1789. if(!empty($char)){
  1790. $m = TTF::getHMetrics($hmetrics, $hhea['numberOfHMetrics'], $glyphIndex);
  1791. // calculate the correct char width by dividing it with 'units per em'
  1792. $cachedFont['C'][$char] = intval($m[0] / ($head['unitsPerEm'] / 1000));
  1793. if($this->isUnicode){
  1794. if($char >= 0){
  1795. if ($char >= 0 && $char < 0xFFFF && $glyphIndex) {
  1796. $cidtogid[$char*2] = chr($glyphIndex >> 8);
  1797. $cidtogid[$char*2 + 1] = chr($glyphIndex & 0xFF);
  1798. }
  1799. }
  1800. }
  1801. }
  1802. }
  1803. } else {
  1804. $this->debug('openFont: font file does not contain format 4 cmap', E_USER_WARNING);
  1805. }
  1806. if(isset($cidtogid))
  1807. $cachedFont['CIDtoGID'] = base64_encode($cidtogid);
  1808. } else if(file_exists($fullFontPath.'.afm')){
  1809. // use the core font program
  1810. $cachedFont = array('isUnicode' => false );
  1811. $file = file($fullFontPath.'.afm');
  1812. foreach ($file as $row) {
  1813. $row=trim($row);
  1814. $pos=strpos($row,' ');
  1815. if ($pos) {
  1816. // then there must be some keyword
  1817. $key = substr($row,0,$pos);
  1818. switch ($key) {
  1819. case 'FontName':
  1820. case 'FullName':
  1821. case 'FamilyName':
  1822. case 'Weight':
  1823. case 'ItalicAngle':
  1824. case 'IsFixedPitch':
  1825. case 'CharacterSet':
  1826. case 'UnderlinePosition':
  1827. case 'UnderlineThickness':
  1828. case 'Version':
  1829. case 'EncodingScheme':
  1830. case 'CapHeight':
  1831. case 'XHeight':
  1832. case 'Ascender':
  1833. case 'Descender':
  1834. case 'StdHW':
  1835. case 'StdVW':
  1836. case 'StartCharMetrics':
  1837. $cachedFont[$key]=trim(substr($row,$pos));
  1838. break;
  1839. case 'FontBBox':
  1840. $cachedFont[$key]=explode(' ',trim(substr($row,$pos)));
  1841. break;
  1842. case 'C':
  1843. // C 39 ; WX 222 ; N quoteright ; B 53 463 157 718 ;
  1844. // use preg_match instead to improve performace
  1845. // IMPORTANT: if "L i fi ; L l fl ;" is required preg_match must be amended
  1846. $r = preg_match('/C (-?\d+) ; WX (-?\d+) ; N (\w+) ; B (-?\d+) (-?\d+) (-?\d+) (-?\d+) ;/', $row, $m);
  1847. if($r == 1){
  1848. //$dtmp = array('C'=> $m[1],'WX'=> $m[2], 'N' => $m[3], 'B' => array($m[4], $m[5], $m[6], $m[7]));
  1849. $c = (int)$m[1];
  1850. $n = $m[3];
  1851. $width = floatval($m[2]);
  1852. if($c >= 0){
  1853. if ($c != hexdec($n)) {
  1854. $cachedFont['codeToName'][$c] = $n;
  1855. }
  1856. $cachedFont['C'][$c] = $width;
  1857. $cachedFont['C'][$n] = $width;
  1858. }else{
  1859. $cachedFont['C'][$n] = $width;
  1860. }
  1861. if (!isset($cachedFont['MissingWidth']) && $c == -1 && $n === '.notdef') {
  1862. $cachedFont['MissingWidth'] = $width;
  1863. }
  1864. }
  1865. break;
  1866. }
  1867. }
  1868. }
  1869. } else {
  1870. $this->debug(sprintf('openFont: no font file found for "%s" IsUnicode: %b', $font, $this->isUnicode), E_USER_ERROR);
  1871. return false;
  1872. }
  1873. $cachedFont['_version_']=3;
  1874. // store the data in as cached file and in $this->fonts array
  1875. $this->fonts[$font]=$cachedFont;
  1876. $fp = fopen($this->tempPath.'/'.$cachedFile,'w'); // use the temp folder to write cached font data
  1877. fwrite($fp,'<?php /* R&OS php pdf class font cache file */ return '.var_export($cachedFont,true).'; ?>');
  1878. fclose($fp);
  1879. return true;
  1880. }
  1881. /**
  1882. * if the font is not loaded then load it and make the required object
  1883. * else just make it the current font
  1884. * the encoding array can contain 'encoding'=> 'none','WinAnsiEncoding','MacRomanEncoding' or 'MacExpertEncoding'
  1885. * note that encoding='none' will need to be used for symbolic fonts
  1886. * and 'differences' => an array of mappings between numbers 0->255 and character names.
  1887. *
  1888. * @param string $fontName Name of the font incl. path
  1889. * @param string $encoding Which encoding to use
  1890. * @param integer $set used to force set the selected font
  1891. * @param bool $subsetFont allow font subsetting
  1892. * @return void
  1893. */
  1894. public function selectFont($fontName, $encoding = '', $set = 1, $subsetFont = false)
  1895. {
  1896. if($subsetFont && !class_exists('TTFsubset')){
  1897. $this->debug("TTFsubset class not found. Falling back to complete font program", E_USER_WARNING);
  1898. $subsetFont = false;
  1899. }
  1900. // old font selection containing full path
  1901. $pos = strrpos($fontName, '/');
  1902. if ($pos !== false) {
  1903. $fontName = substr($fontName, $pos + 1);
  1904. }
  1905. // file extension found
  1906. $pos = strrpos($fontName, '.');
  1907. if($pos){
  1908. $ext = substr($fontName, $pos + 1);
  1909. $fontName = substr($fontName, 0, $pos);
  1910. } else {
  1911. // default extension is ttf
  1912. $ext = 'ttf';
  1913. }
  1914. if (!isset($this->fonts[$fontName])){
  1915. // check and load the font file, on no errors $ok = true
  1916. $ok = $this->openFont($fontName);
  1917. if(!$ok){
  1918. $fontName = 'Helvetica';
  1919. if(!isset($this->fonts[$fontName])){
  1920. $this->debug("Error while loading coreFont - check \$pdf->fontPath and/or define one coreFont as fallback", E_USER_ERROR);
  1921. die;
  1922. }
  1923. } else if(isset($this->fonts[$fontName])) {
  1924. $this->numObj++;
  1925. $this->numFonts++;
  1926. $font = &$this->fonts[$fontName];
  1927. $options = array('name' => $fontName, 'fontFileName'=>$fontName); // orgFontName is necessary when font subsetting is used
  1928. if (is_array($encoding)){
  1929. // then encoding and differences might be set
  1930. if (isset($encoding['encoding'])){
  1931. $options['encoding'] = $encoding['encoding'];
  1932. }
  1933. if (isset($encoding['differences'])){
  1934. $options['differences'] = $encoding['differences'];
  1935. }
  1936. } else if (strlen($encoding)){
  1937. // then perhaps only the encoding has been set
  1938. $options['encoding'] = $encoding;
  1939. }
  1940. $fontObj = $this->numObj;
  1941. $this->o_font($fontObj, 'new', $options);
  1942. $font['fontNum'] = $this->numFonts;
  1943. // if this is a '.afm' font, and there is a '.pfa' file to go with it (as there
  1944. // should be for all non-basic fonts), then load it into an object and put the
  1945. // references into the font object
  1946. $fbtype = '';
  1947. if (file_exists($this->fontPath.'/'.$fontName.'.pfb')){
  1948. $fbtype = 'pfb';
  1949. } else if (file_exists($this->fontPath.'/'.$fontName.'.ttf')){
  1950. $fbtype = 'ttf';
  1951. }
  1952. if ($fbtype){
  1953. $adobeFontName = $font['FontName'];
  1954. $this->debug('selectFont: adding font "'.$fontName.'" to pdf');
  1955. // find the array of fond widths, and put that into an object.
  1956. $firstChar = -1;
  1957. $lastChar = 0;
  1958. $widths = array();
  1959. $cid_widths = array();
  1960. if(!$font['isUnicode']){
  1961. for($i = 0; $i < 255; $i++){
  1962. if (isset($options['differences']) && isset($options['differences'][$i])){
  1963. // set the correct width of the diffence by using its name
  1964. $widths[] = $font['C'][$options['differences'][$i]];
  1965. } else if(isset($font['C'][$i]))
  1966. $widths[] = $font['C'][$i];
  1967. else
  1968. $widths[] = 0;
  1969. }
  1970. $firstChar = 0;
  1971. $lastChar = 255;
  1972. }
  1973. // also need to adjust the widths for the differences array
  1974. /*if (isset($options['differences'])){
  1975. foreach ($options['differences'] as $charNum => $charName){
  1976. if ($charNum>$lastChar){
  1977. for($i = $lastChar + 1; $i <= $charNum; $i++) {
  1978. $widths[]=0;
  1979. }
  1980. $lastChar = $charNum;
  1981. }
  1982. if (isset($font['C'][$charName])){
  1983. $widths[$charNum-$firstChar]=$font['C'][$charName];
  1984. if($font['isUnicode']){
  1985. $cid_widths[$charName] = $font['C'][$charName];
  1986. }
  1987. }
  1988. }
  1989. }*/
  1990. if($font['isUnicode']){
  1991. $font['CIDWidths'] = $font['C'];
  1992. }
  1993. $this->debug('selectFont: FirstChar='.$firstChar);
  1994. $this->debug('selectFont: LastChar='.$lastChar);
  1995. $widthid = -1;
  1996. if(!$font['isUnicode']){
  1997. $this->numObj++;
  1998. $this->o_contents($this->numObj, 'new', 'raw');
  1999. $this->objects[$this->numObj]['c'].='['.implode(' ', $widths).']';
  2000. $widthid = $this->numObj;
  2001. }
  2002. $missing_width = 500;
  2003. $stemV = 70;
  2004. if (isset($font['MissingWidth'])) {
  2005. $missing_width = $font['MissingWidth'];
  2006. }
  2007. if (isset($font['StdVW'])) {
  2008. $stemV = $font['StdVW'];
  2009. } else if (isset($font['Weight']) && preg_match('!(bold|black)!i', $font['Weight'])) {
  2010. $stemV = 120;
  2011. }
  2012. // create the font descriptor
  2013. $fontDescriptorId = ++$this->numObj;
  2014. // determine flags (more than a little flakey, hopefully will not matter much)
  2015. $flags=0;
  2016. if ($font['ItalicAngle']!=0){
  2017. $flags+=pow(2,6);
  2018. }
  2019. if ($font['IsFixedPitch']=='true'){
  2020. $flags+=1;
  2021. }
  2022. $flags+=pow(2,5); // assume non-sybolic
  2023. $list = array('Ascent'=>'Ascender','CapHeight'=>'CapHeight','Descent'=>'Descender','FontBBox'=>'FontBBox','ItalicAngle'=>'ItalicAngle');
  2024. $fdopt = array(
  2025. 'Flags' => $flags,
  2026. 'FontName' => $adobeFontName,
  2027. 'StemV' => $stemV
  2028. );
  2029. foreach($list as $k=>$v){
  2030. if (isset($font[$v])){
  2031. $fdopt[$k]=$font[$v];
  2032. }
  2033. }
  2034. // setup the basic properties for o_font output
  2035. $tmp = array('BaseFont'=>$adobeFontName,'Widths'=>$widthid
  2036. ,'FirstChar'=>$firstChar,'LastChar'=>$lastChar
  2037. ,'FontDescriptor'=>$fontDescriptorId);
  2038. // binary content of pfb or ttf file
  2039. $pfbid = ++$this->numObj;
  2040. // embed the font program
  2041. // to allow font subsets embedding fonts is proceed in o_font 'output'
  2042. if($this->embedFont){
  2043. if ($fbtype=='pfb'){
  2044. $fdopt['FontFile']=$pfbid;
  2045. } else if ($fbtype=='ttf'){
  2046. $fdopt['FontFile2']=$pfbid;
  2047. $tmp['SubType']='TrueType'; // Declare basic font as TrueType
  2048. }
  2049. $this->o_fontDescriptor($fontDescriptorId,'new',$fdopt);
  2050. $this->o_contents($pfbid,'new');
  2051. }
  2052. $this->debug('selectFont: adding extra info to font.('.$fontObj.')');
  2053. foreach($tmp as $fk=>$fv){
  2054. $this->debug($fk." : ".$fv);
  2055. }
  2056. $this->o_font($fontObj,'add',$tmp);
  2057. } else if(!in_array(strtolower($fontName), $this->coreFonts)) {
  2058. $this->debug('selectFont: No pfb/ttf file found for "'.$fontName.'"', E_USER_WARNING);
  2059. }
  2060. // also set the differences here, note that this means that these will take effect only the
  2061. // first time that a font is selected, else they are ignored
  2062. if (isset($options['differences'])){
  2063. $font['differences']=$options['differences'];
  2064. }
  2065. }
  2066. }
  2067. $this->fonts[$fontName]['isSubset'] = $subsetFont;
  2068. if(!isset($this->fonts[$fontName]['subset'])) {
  2069. $this->fonts[$fontName]['subset'] = array();
  2070. }
  2071. if ($set && isset($this->fonts[$fontName])){
  2072. // so if for some reason the font was not set in the last one then it will not be selected
  2073. $this->currentBaseFont=$fontName;
  2074. // the next line means that if a new font is selected, then the current text state will be
  2075. // applied to it as well.
  2076. $this->setCurrentFont();
  2077. }
  2078. //return $this->currentFontNum;
  2079. }
  2080. /**
  2081. * sets up the current font, based on the font families, and the current text state
  2082. * note that this system is quite flexible, a <b><i> font can be completely different to a
  2083. * <i><b> font, and even <b><b> will have to be defined within the family to have meaning
  2084. * This function is to be called whenever the currentTextState is changed, it will update
  2085. * the currentFont setting to whatever the appropriatte family one is.
  2086. * If the user calls selectFont themselves then that will reset the currentBaseFont, and the currentFont
  2087. * This function will change the currentFont to whatever it should be, but will not change the
  2088. * currentBaseFont.
  2089. *
  2090. */
  2091. protected function setCurrentFont(){
  2092. if (strlen($this->currentBaseFont)==0){
  2093. // then assume an initial font
  2094. $this->selectFont('Helvetica');
  2095. }
  2096. $cf = $this->currentBaseFont;
  2097. if (strlen($this->currentTextState)
  2098. && isset($this->fontFamilies[$cf])
  2099. && isset($this->fontFamilies[$cf][$this->currentTextState])){
  2100. // then we are in some state or another
  2101. // and this font has a family, and the current setting exists within it
  2102. // select the font, then return it
  2103. $nf = $this->fontFamilies[$cf][$this->currentTextState];
  2104. // PATCH #14 - subset file fix when using font family | thank you johannes
  2105. $isSubset = false;
  2106. if (isset($this->fonts[$this->currentBaseFont]['isSubset'])) $isSubset = $this->fonts[$this->currentBaseFont]['isSubset'];
  2107. $this->selectFont($nf,'', 0, $isSubset);
  2108. $this->currentFont = $nf;
  2109. $this->currentFontNum = $this->fonts[$nf]['fontNum'];
  2110. } else {
  2111. // the this font must not have the right family member for the current state
  2112. // simply assume the base font
  2113. $this->currentFont = $cf;
  2114. $this->currentFontNum = $this->fonts[$cf]['fontNum'];
  2115. }
  2116. }
  2117. /**
  2118. * get the current font name being used
  2119. * @since 0.12-rc12
  2120. * @param bool $withStyle force to receive the style font name, instead of the base font
  2121. * @return string current font name
  2122. */
  2123. public function getCurrentFont($withStyle = false){
  2124. if($withStyle){
  2125. return $this->currentFont;
  2126. }
  2127. return $this->currentBaseFont;
  2128. }
  2129. /**
  2130. * function for the user to find out what the ID is of the first page that was created during
  2131. * startup - useful if they wish to add something to it later.
  2132. */
  2133. protected function getFirstPageId(){
  2134. return $this->firstPageId;
  2135. }
  2136. /**
  2137. * add content to the currently active object
  2138. */
  2139. protected function addContent($content){
  2140. $this->objects[$this->currentContents]['c'].=$content;
  2141. }
  2142. /**
  2143. * sets the colour for fill operations
  2144. */
  2145. public function setColor($r,$g,$b,$force=0){
  2146. if ($r>=0 && ($force || $r!=$this->currentColour['r'] || $g!=$this->currentColour['g'] || $b!=$this->currentColour['b'])){
  2147. $this->objects[$this->currentContents]['c'].="\n".sprintf('%.3F',$r).' '.sprintf('%.3F',$g).' '.sprintf('%.3F',$b).' rg';
  2148. $this->currentColour=array('r'=>$r,'g'=>$g,'b'=>$b);
  2149. }
  2150. }
  2151. /**
  2152. * sets the colour for stroke operations
  2153. */
  2154. public function setStrokeColor($r,$g,$b,$force=0){
  2155. if ($r>=0 && ($force || $r!=$this->currentStrokeColour['r'] || $g!=$this->currentStrokeColour['g'] || $b!=$this->currentStrokeColour['b'])){
  2156. $this->objects[$this->currentContents]['c'].="\n".sprintf('%.3F',$r).' '.sprintf('%.3F',$g).' '.sprintf('%.3F',$b).' RG';
  2157. $this->currentStrokeColour=array('r'=>$r,'g'=>$g,'b'=>$b);
  2158. }
  2159. }
  2160. /**
  2161. * draw a line from one set of coordinates to another
  2162. */
  2163. public function line($x1,$y1,$x2,$y2){
  2164. $this->objects[$this->currentContents]['c'].="\n".sprintf('%.3F',$x1).' '.sprintf('%.3F',$y1).' m '.sprintf('%.3F',$x2).' '.sprintf('%.3F',$y2).' l S';
  2165. }
  2166. /**
  2167. * draw a bezier curve based on 4 control points
  2168. */
  2169. public function curve($x0,$y0,$x1,$y1,$x2,$y2,$x3,$y3){
  2170. // in the current line style, draw a bezier curve from (x0,y0) to (x3,y3) using the other two points
  2171. // as the control points for the curve.
  2172. $this->objects[$this->currentContents]['c'].="\n".sprintf('%.3F',$x0).' '.sprintf('%.3F',$y0).' m '.sprintf('%.3F',$x1).' '.sprintf('%.3F',$y1);
  2173. $this->objects[$this->currentContents]['c'].= ' '.sprintf('%.3F',$x2).' '.sprintf('%.3F',$y2).' '.sprintf('%.3F',$x3).' '.sprintf('%.3F',$y3).' c S';
  2174. }
  2175. /**
  2176. * draw a part of an ellipse
  2177. */
  2178. public function partEllipse($x0,$y0,$astart,$afinish,$r1,$r2=0,$angle=0,$nSeg=8){
  2179. $this->ellipse($x0,$y0,$r1,$r2,$angle,$nSeg,$astart,$afinish,0);
  2180. }
  2181. /**
  2182. * draw a filled ellipse
  2183. */
  2184. public function filledEllipse($x0,$y0,$r1,$r2=0,$angle=0,$nSeg=8,$astart=0,$afinish=360){
  2185. return $this->ellipse($x0,$y0,$r1,$r2=0,$angle,$nSeg,$astart,$afinish,1,1);
  2186. }
  2187. /**
  2188. * draw an ellipse
  2189. * note that the part and filled ellipse are just special cases of this function
  2190. *
  2191. * draws an ellipse in the current line style
  2192. * centered at $x0,$y0, radii $r1,$r2
  2193. * if $r2 is not set, then a circle is drawn
  2194. * nSeg is not allowed to be less than 2, as this will simply draw a line (and will even draw a
  2195. * pretty crappy shape at 2, as we are approximating with bezier curves.
  2196. */
  2197. public function ellipse($x0,$y0,$r1,$r2=0,$angle=0,$nSeg=8,$astart=0,$afinish=360,$close=1,$fill=0){
  2198. if ($r1==0){
  2199. return;
  2200. }
  2201. if ($r2==0){
  2202. $r2=$r1;
  2203. }
  2204. if ($nSeg<2){
  2205. $nSeg=2;
  2206. }
  2207. $astart = deg2rad((float)$astart);
  2208. $afinish = deg2rad((float)$afinish);
  2209. $totalAngle =$afinish-$astart;
  2210. $dt = $totalAngle/$nSeg;
  2211. $dtm = $dt/3;
  2212. if ($angle != 0){
  2213. $a = -1*deg2rad((float)$angle);
  2214. $tmp = "\n q ";
  2215. $tmp .= sprintf('%.3F',cos($a)).' '.sprintf('%.3F',(-1.0*sin($a))).' '.sprintf('%.3F',sin($a)).' '.sprintf('%.3F',cos($a)).' ';
  2216. $tmp .= sprintf('%.3F',$x0).' '.sprintf('%.3F',$y0).' cm';
  2217. $this->objects[$this->currentContents]['c'].= $tmp;
  2218. $x0=0;
  2219. $y0=0;
  2220. }
  2221. $t1 = $astart;
  2222. $a0 = $x0+$r1*cos($t1);
  2223. $b0 = $y0+$r2*sin($t1);
  2224. $c0 = -$r1*sin($t1);
  2225. $d0 = $r2*cos($t1);
  2226. $this->objects[$this->currentContents]['c'].="\n".sprintf('%.3F',$a0).' '.sprintf('%.3F',$b0).' m ';
  2227. for ($i=1;$i<=$nSeg;$i++){
  2228. // draw this bit of the total curve
  2229. $t1 = $i*$dt+$astart;
  2230. $a1 = $x0+$r1*cos($t1);
  2231. $b1 = $y0+$r2*sin($t1);
  2232. $c1 = -$r1*sin($t1);
  2233. $d1 = $r2*cos($t1);
  2234. $this->objects[$this->currentContents]['c'].="\n".sprintf('%.3F',($a0+$c0*$dtm)).' '.sprintf('%.3F',($b0+$d0*$dtm));
  2235. $this->objects[$this->currentContents]['c'].= ' '.sprintf('%.3F',($a1-$c1*$dtm)).' '.sprintf('%.3F',($b1-$d1*$dtm)).' '.sprintf('%.3F',$a1).' '.sprintf('%.3F',$b1).' c';
  2236. $a0=$a1;
  2237. $b0=$b1;
  2238. $c0=$c1;
  2239. $d0=$d1;
  2240. }
  2241. if ($fill){
  2242. $this->objects[$this->currentContents]['c'].=' f';
  2243. } else {
  2244. if ($close){
  2245. $this->objects[$this->currentContents]['c'].=' s'; // small 's' signifies closing the path as well
  2246. } else {
  2247. $this->objects[$this->currentContents]['c'].=' S';
  2248. }
  2249. }
  2250. if ($angle !=0){
  2251. $this->objects[$this->currentContents]['c'].=' Q';
  2252. }
  2253. }
  2254. /**
  2255. * this sets the line drawing style.
  2256. * width, is the thickness of the line in user units
  2257. * cap is the type of cap to put on the line, values can be 'butt','round','square'
  2258. * where the diffference between 'square' and 'butt' is that 'square' projects a flat end past the
  2259. * end of the line.
  2260. * join can be 'miter', 'round', 'bevel'
  2261. * dash is an array which sets the dash pattern, is a series of length values, which are the lengths of the
  2262. * on and off dashes.
  2263. * (2) represents 2 on, 2 off, 2 on , 2 off ...
  2264. * (2,1) is 2 on, 1 off, 2 on, 1 off.. etc
  2265. * phase is a modifier on the dash pattern which is used to shift the point at which the pattern starts.
  2266. */
  2267. public function setLineStyle($width=1,$cap='',$join='',$dash='',$phase=0){
  2268. // this is quite inefficient in that it sets all the parameters whenever 1 is changed, but will fix another day
  2269. $string = '';
  2270. if ($width>0){
  2271. $string.= $width.' w';
  2272. }
  2273. $ca = array('butt'=>0,'round'=>1,'square'=>2);
  2274. if (isset($ca[$cap])){
  2275. $string.= ' '.$ca[$cap].' J';
  2276. }
  2277. $ja = array('miter'=>0,'round'=>1,'bevel'=>2);
  2278. if (isset($ja[$join])){
  2279. $string.= ' '.$ja[$join].' j';
  2280. }
  2281. if (is_array($dash)){
  2282. $string.= ' [';
  2283. foreach ($dash as $len){
  2284. $string.=' '.$len;
  2285. }
  2286. $string.= ' ] '.$phase.' d';
  2287. }
  2288. $this->currentLineStyle = $string;
  2289. $this->objects[$this->currentContents]['c'].="\n".$string;
  2290. }
  2291. /**
  2292. * draw a polygon, the syntax for this is similar to the GD polygon command
  2293. */
  2294. public function polygon($p,$np,$f=0){
  2295. $this->objects[$this->currentContents]['c'].="\n";
  2296. $this->objects[$this->currentContents]['c'].=sprintf('%.3F',$p[0]).' '.sprintf('%.3F',$p[1]).' m ';
  2297. for ($i=2;$i<$np*2;$i=$i+2){
  2298. $this->objects[$this->currentContents]['c'].= sprintf('%.3F',$p[$i]).' '.sprintf('%.3F',$p[$i+1]).' l ';
  2299. }
  2300. if ($f==1){
  2301. $this->objects[$this->currentContents]['c'].=' f';
  2302. } else {
  2303. $this->objects[$this->currentContents]['c'].=' S';
  2304. }
  2305. }
  2306. /**
  2307. * a filled rectangle, note that it is the width and height of the rectangle which are the secondary paramaters, not
  2308. * the coordinates of the upper-right corner
  2309. */
  2310. public function filledRectangle($x1,$y1,$width,$height){
  2311. $this->objects[$this->currentContents]['c'].="\n".sprintf('%.3F',$x1).' '.sprintf('%.3F',$y1).' '.sprintf('%.3F',$width).' '.sprintf('%.3F',$height).' re f';
  2312. }
  2313. /**
  2314. * draw a rectangle, note that it is the width and height of the rectangle which are the secondary paramaters, not
  2315. * the coordinates of the upper-right corner
  2316. */
  2317. public function rectangle($x1,$y1,$width,$height){
  2318. $this->objects[$this->currentContents]['c'].="\n".sprintf('%.3F',$x1).' '.sprintf('%.3F',$y1).' '.sprintf('%.3F',$width).' '.sprintf('%.3F',$height).' re S';
  2319. }
  2320. /**
  2321. * add a new page to the document
  2322. * this also makes the new page the current active object
  2323. */
  2324. public function newPage($insert=0,$id=0,$pos='after'){
  2325. // if there is a state saved, then go up the stack closing them
  2326. // then on the new page, re-open them with the right setings
  2327. if ($this->nStateStack){
  2328. for ($i=$this->nStateStack;$i>=1;$i--){
  2329. $this->restoreState($i);
  2330. }
  2331. }
  2332. $this->numObj++;
  2333. if ($insert){
  2334. // the id from the ezPdf class is the od of the contents of the page, not the page object itself
  2335. // query that object to find the parent
  2336. $rid = $this->objects[$id]['onPage'];
  2337. $opt= array('rid'=>$rid,'pos'=>$pos);
  2338. $this->o_page($this->numObj,'new',$opt);
  2339. } else {
  2340. $this->o_page($this->numObj,'new');
  2341. }
  2342. // if there is a stack saved, then put that onto the page
  2343. if ($this->nStateStack){
  2344. for ($i=1;$i<=$this->nStateStack;$i++){
  2345. $this->saveState($i);
  2346. }
  2347. }
  2348. // and if there has been a stroke or fill colour set, then transfer them
  2349. if ($this->currentColour['r']>=0){
  2350. $this->setColor($this->currentColour['r'],$this->currentColour['g'],$this->currentColour['b'],1);
  2351. }
  2352. if ($this->currentStrokeColour['r']>=0){
  2353. $this->setStrokeColor($this->currentStrokeColour['r'],$this->currentStrokeColour['g'],$this->currentStrokeColour['b'],1);
  2354. }
  2355. // if there is a line style set, then put this in too
  2356. if (strlen($this->currentLineStyle)){
  2357. $this->objects[$this->currentContents]['c'].="\n".$this->currentLineStyle;
  2358. }
  2359. // the call to the o_page object set currentContents to the present page, so this can be returned as the page id
  2360. return $this->currentContents;
  2361. }
  2362. /**
  2363. * return the pdf stream as a string returned from the function
  2364. * This method is protect to force user to use ezOutput from Cezpdf.php
  2365. */
  2366. function output($debug=0){
  2367. if ($debug){
  2368. // turn compression off
  2369. $this->options['compression']=0;
  2370. }
  2371. if ($this->arc4_objnum){
  2372. $this->ARC4_init($this->encryptionKey);
  2373. }
  2374. if($this->valid){
  2375. $this->debug('The output method has been executed again', E_USER_WARNING);
  2376. }
  2377. $this->checkAllHere();
  2378. $xref=array();
  2379. // set the pdf version dynamically, depended on the objects being used
  2380. $content="%PDF-".sprintf('%.1F', $this->pdfversion)."\n%\xe2\xe3\xcf\xd3";
  2381. $pos=strlen($content);
  2382. foreach($this->objects as $k=>$v){
  2383. $tmp='o_'.$v['t'];
  2384. $cont=$this->$tmp($k,'out');
  2385. $content.=$cont;
  2386. $xref[]=$pos;
  2387. $pos+=strlen($cont);
  2388. }
  2389. ++$pos;
  2390. $content.="\nxref\n0 ".(count($xref)+1)."\n0000000000 65535 f \n";
  2391. foreach($xref as $p){
  2392. $content.=substr('0000000000',0,10-strlen($p+1)).($p+1)." 00000 n \n";
  2393. }
  2394. $content.="trailer\n<< /Size ".(count($xref)+1)." /Root 1 0 R /Info ".$this->infoObject." 0 R";
  2395. // if encryption has been applied to this document then add the marker for this dictionary
  2396. if ($this->arc4_objnum > 0){
  2397. $content .= " /Encrypt ".$this->arc4_objnum." 0 R";
  2398. }
  2399. if ($this->fileIdentifier){
  2400. $content .= " /ID [<".$this->fileIdentifier."><".$this->fileIdentifier.">]";
  2401. }
  2402. $content .= " >>\nstartxref\n".$pos."\n%%EOF\n";
  2403. return $content;
  2404. }
  2405. /**
  2406. * output the pdf code, streaming it to the browser
  2407. * the relevant headers are set so that hopefully the browser will recognise it
  2408. * this method is protected to force user to use ezStream method from Cezpdf.php
  2409. */
  2410. protected function stream($options=''){
  2411. // setting the options allows the adjustment of the headers
  2412. // values at the moment are:
  2413. // 'Content-Disposition'=>'filename' - sets the filename, though not too sure how well this will
  2414. // work as in my trial the browser seems to use the filename of the php file with .pdf on the end
  2415. // 'Accept-Ranges'=>1 or 0 - if this is not set to 1, then this header is not included, off by default
  2416. // this header seems to have caused some problems despite tha fact that it is supposed to solve
  2417. // them, so I am leaving it off by default.
  2418. // 'compress'=> 1 or 0 - apply content stream compression, this is on (1) by default
  2419. // 'download'=> 1 or 0 - provide download dialog
  2420. if (!is_array($options)){
  2421. $options=array();
  2422. }
  2423. if ( isset($options['compress']) && $options['compress']==0){
  2424. $tmp = $this->output(1);
  2425. } else {
  2426. $tmp = $this->output();
  2427. }
  2428. ob_start();
  2429. echo $tmp;
  2430. $length = ob_get_length();
  2431. header("Content-Type: application/pdf");
  2432. header("Content-Length: ".$length);
  2433. $fileName = (isset($options['Content-Disposition'])?$options['Content-Disposition']:'file.pdf');
  2434. if(isset($options['download']) && $options['download'] == 1)
  2435. $attached = 'attachment';
  2436. else
  2437. $attached = 'inline';
  2438. header("Content-Disposition: $attached; filename=".$fileName);
  2439. if (isset($options['Accept-Ranges']) && $options['Accept-Ranges']==1){
  2440. header("Accept-Ranges: ".$length);
  2441. }
  2442. ob_end_flush();
  2443. }
  2444. /**
  2445. * return the height in units of the current font in the given size
  2446. */
  2447. public function getFontHeight($size){
  2448. if (!$this->numFonts){
  2449. $this->selectFont('./fonts/Helvetica');
  2450. }
  2451. $font = &$this->fonts[$this->currentFont];
  2452. // for the current font, and the given size, what is the height of the font in user units
  2453. $h = $font['FontBBox'][3] - $font['FontBBox'][1];
  2454. return $size*$h/1000;
  2455. }
  2456. /**
  2457. * return the font descender, this will normally return a negative number
  2458. * if you add this number to the baseline, you get the level of the bottom of the font
  2459. * it is in the pdf user units
  2460. */
  2461. public function getFontDescender($size){
  2462. // note that this will most likely return a negative value
  2463. if (!$this->numFonts){
  2464. $this->selectFont('./fonts/Helvetica');
  2465. }
  2466. $h = $this->fonts[$this->currentFont]['Descender'];
  2467. return $size*$h/1000;
  2468. }
  2469. /**
  2470. * filter the text, this is applied to all text just before being inserted into the pdf document
  2471. * it escapes the various things that need to be escaped, and so on
  2472. *
  2473. */
  2474. protected function filterText($text, $bom = true, $convert_encoding = true){
  2475. $cf = $this->currentFont;
  2476. if ($convert_encoding &&isset($this->fonts[$cf]) && $this->fonts[$cf]['isUnicode']) {
  2477. $text = mb_convert_encoding($text, 'UTF-16BE','UTF-8');
  2478. // store all used characters if subset font is set to true
  2479. if($this->fonts[$cf]['isSubset']){
  2480. for($i = 0; $i < mb_strlen($text,'UTF-16BE'); $i++)
  2481. $this->fonts[$cf]['subset'][mb_substr($text,$i, 1, 'UTF-16BE')] = true;
  2482. }
  2483. } else if(!$this->fonts[$cf]['isUnicode']) {
  2484. $text = mb_convert_encoding($text, $this->targetEncoding, 'UTF-8');
  2485. // store all used characters if subset font is set to true
  2486. if($this->fonts[$cf]['isSubset']){
  2487. for($i = 0; $i < strlen($text); $i++)
  2488. $this->fonts[$cf]['subset'][$text[$i]] = true;
  2489. }
  2490. }
  2491. $text = strtr($text, array(')' => '\\)', '(' => '\\(', '\\' => '\\\\', chr(8) => '\\b', chr(9) => '\\t', chr(10) => '\\n', chr(12) => '\\f' ,chr(13) => '\\r', '&lt;'=>'<', '&gt;'=>'>', '&amp;'=>'&') );
  2492. if($this->rtl){
  2493. $text = strrev($text);
  2494. }
  2495. return $text;
  2496. }
  2497. private function getDirectives(&$text, &$x, &$y, $width = 0, $size = 0, $justification = 'left',$angle = 0, &$wordSpaceAdjust = 0, $noCB = false){
  2498. $regex = "/<\/?([cC]:|)(".$this->allowedTags.")\>/";
  2499. $cb = array();
  2500. $r = preg_match_all($regex, $text, $regs, PREG_OFFSET_CAPTURE);
  2501. if($r){
  2502. // backup current font style
  2503. $store_currentTextState = $this->currentTextState;
  2504. $nx = $x;
  2505. $ny = $y;
  2506. if($this->nCallback > 0){
  2507. $cb[0] = $this->callback[1];
  2508. $cb[0]['x'] = $x;
  2509. $cb[0]['y'] = $y;
  2510. $cb[0]['startTag'] = 0;
  2511. $cb[0]['endTag'] = 0;
  2512. }
  2513. $restWidth = $width;
  2514. reset($regs[0]);
  2515. $prevEndTagIndex = 0;
  2516. while(list($k,$curTag) = each($regs[0])){
  2517. $curTagIndex = mb_strlen(substr($text, 0, $curTag[1]), 'UTF-8');
  2518. $endTagIndex = $curTagIndex + strlen($curTag[0]);
  2519. $tmpstr = mb_substr($text, $prevEndTagIndex, $curTagIndex - $prevEndTagIndex, 'UTF-8');
  2520. $tmpstr = $this->filterText($tmpstr, false, false);
  2521. $tmp = $this->getTextLength($size, $tmpstr, $restWidth, $angle, $wordSpaceAdjust);
  2522. // if the text does not fit to $width, $tmp[2] contains the length to break the line
  2523. if($tmp[2] > 0){
  2524. // position where the line break occurs
  2525. $lbpos = $prevEndTagIndex + $tmp[2];
  2526. // adjust to position if justification is set
  2527. if($nx > $x){
  2528. $tmpw = ($width - $restWidth) + $tmp[0];
  2529. // force word wrap when something already written
  2530. /*$lbpos -= $tmp[2];
  2531. if($prevEndTagIndex == $lbpos){
  2532. $lastCallback = array_pop($cb);
  2533. if(isset($lastCallback)){
  2534. $lbpos -= $lastCallback['endTag'] - $lastCallback['startTag'];
  2535. $tmp[0] = 0;
  2536. if(isset($lastCallback['nCallback']))
  2537. $this->nCallback--;
  2538. }
  2539. }*/
  2540. } else{
  2541. $tmpw = $tmp[0];
  2542. }
  2543. $this->adjustWrapText($tmpstr, $tmpw, $width, $x, $wordSpaceAdjust, $justification);
  2544. // set position array by using the current break position minus offset
  2545. $cb[$lbpos] = array('x'=> ($nx + $tmp[0]), 'y'=> $ny + $tmp[1], 'f'=>'linebreak', 'p' => $tmp[3], 'width'=>$tmp[0]);
  2546. // restore previous stored font style
  2547. $this->currentTextState = $store_currentTextState;
  2548. $this->setCurrentFont();
  2549. return $cb;
  2550. }
  2551. $prevEndTagIndex = $endTagIndex;
  2552. $restWidth -= $tmp[0];
  2553. $nx += $tmp[0];
  2554. $ny += $tmp[1];
  2555. if(!empty($regs[1][$k][0])){
  2556. // these are custom callbacks (with parameters)
  2557. $pos = strpos($regs[2][$k][0], ':');
  2558. if($pos){
  2559. $func = substr($regs[2][$k][0], 0, $pos);
  2560. $parm = substr($regs[2][$k][0], $pos + 1);
  2561. } else {
  2562. $func = $regs[2][$k][0];
  2563. $parm = '';
  2564. }
  2565. // adjust the coordinates if justification is set
  2566. // end tag for custom callbacks
  2567. if(substr($curTag[0], 0, 2) == "</"){
  2568. $cb[$curTagIndex] = array('x'=> $nx, 'y'=>$ny,'angle'=>$angle,'status'=>'end', 'f'=>$func, 'p'=>$parm,'nCallback'=>$this->nCallback, 'startTag' => $curTagIndex, 'endTag' => $endTagIndex );
  2569. if(!$noCB){
  2570. $this->nCallback--;
  2571. if ($this->nCallback<0){
  2572. $this->nCallback=0;
  2573. }
  2574. }
  2575. } else {
  2576. $noClose = ($regs[1][$k][0] == 'C:')? true: false;
  2577. $cb[$curTagIndex] = array('x'=> $nx, 'y'=>$ny, 'angle'=>$angle,'status'=>'start','p'=>$parm,'f'=>$func,'height'=>$this->getFontHeight($size),'descender'=>$this->getFontDescender($size),
  2578. 'startTag' => $curTagIndex, 'endTag' => $endTagIndex , 'noClose' => $noClose);
  2579. if(!$noCB){
  2580. if(!$noClose){
  2581. $this->nCallback++;
  2582. $cb[$curTagIndex]['nCallback']=$this->nCallback;
  2583. $this->callback[$this->nCallback]=$cb[$curTagIndex];
  2584. }
  2585. }
  2586. }
  2587. } else {
  2588. $parm = $regs[2][$k][0];
  2589. if(substr($curTag[0] ,0 , 2) == "</"){
  2590. $cb[$curTagIndex] = array('x'=> $nx, 'y'=>$ny, 'angle'=>$angle,'status'=>'end', 'p'=>$parm,'f'=>'defaultFormatting', 'startTag' => $curTagIndex, 'endTag' => $endTagIndex );
  2591. } else {
  2592. $cb[$curTagIndex] = array('x'=> $nx, 'y'=>$ny, 'angle'=>$angle,'status'=>'start','p'=>$parm,'f'=>'defaultFormatting', 'startTag' => $curTagIndex, 'endTag' => $endTagIndex );
  2593. }
  2594. // do a dry formatting to fetch the correct text width
  2595. $this->defaultFormatting($cb[$curTagIndex]);
  2596. $this->setCurrentFont();
  2597. }
  2598. }
  2599. $l = mb_strlen($text, 'UTF-8');
  2600. if($prevEndTagIndex < $l){
  2601. $tmpstr = mb_substr($text, $prevEndTagIndex, $l - $prevEndTagIndex, 'UTF-8');
  2602. $tmp = $this->getTextLength($size, $tmpstr, $restWidth, $angle, $wordSpaceAdjust);
  2603. // if the text does not fit to $width, $tmp[2] contains the length
  2604. if($tmp[2] > 0){
  2605. // restore previous stored font style
  2606. $this->currentTextState = $store_currentTextState;
  2607. $this->setCurrentFont();
  2608. $tmpstr = mb_substr($text, 0, $prevEndTagIndex + $tmp[2], 'UTF-8');
  2609. // adjust to position if justification is set
  2610. $this->adjustWrapText($tmpstr, $width - ($restWidth - $tmp[0]), $width, $x, $wordSpaceAdjust, $justification);
  2611. // set position array by using the current break position minus offset
  2612. $lbpos = $prevEndTagIndex + $tmp[2];
  2613. $cb[$lbpos] = array('x'=> ($nx + $tmp[0]), 'y'=> $ny + $tmp[1], 'f'=>'linebreak', 'p' => $tmp[3], 'width'=>$tmp[0]);
  2614. return $cb;
  2615. } else {
  2616. $restWidth -= $tmp[0];
  2617. }
  2618. }
  2619. // restore previous stored font style
  2620. $this->currentTextState = $store_currentTextState;
  2621. $this->setCurrentFont();
  2622. // adjust to position if justification is set
  2623. $tmpx = $x;
  2624. if($justification != 'full') {
  2625. $this->adjustWrapText($tmpstr, $width - $restWidth, $width, $x, $wordSpaceAdjust, $justification);
  2626. }
  2627. foreach($cb as &$v){
  2628. if($v['status'] == 'start'){
  2629. $v['x'] += $x - $tmpx;
  2630. } else {
  2631. $v['x'] += $x - $tmpx;
  2632. }
  2633. }
  2634. } else {
  2635. $tmp = $this->getTextLength($size, $text, $width, $angle, $wordSpaceAdjust);
  2636. // if the text does not fit to $width, $tmp[2] contains the length
  2637. if($tmp[2] > 0){
  2638. $tmpstr = mb_substr($text, 0, $tmp[2], 'UTF-8');
  2639. // adjust to position if justification is set
  2640. $this->adjustWrapText($tmpstr, $tmp[0], $width, $x, $wordSpaceAdjust, $justification);
  2641. // set position array by using the current break position minus offset
  2642. $lbpos = $tmp[2];
  2643. $cb[$lbpos] = array('x'=> ($x + $tmp[0]), 'y'=> $y + $tmp[1], 'f'=>'linebreak', 'p' => $tmp[3], 'width'=>$tmp[0]);
  2644. return $cb;
  2645. } else if($justification != 'full') {
  2646. $this->adjustWrapText($text, $tmp[0], $width, $x, $wordSpaceAdjust, $justification);
  2647. }
  2648. }
  2649. return $cb;
  2650. }
  2651. protected function defaultFormatting($info){
  2652. $tag = $info['p'];
  2653. switch($tag){
  2654. case 'strong':
  2655. $tag = 'b';
  2656. case 'i':
  2657. case 'b':
  2658. if($info['status'] == 'start'){
  2659. $this->currentTextState.= $tag;
  2660. } else {
  2661. $p = strrpos($this->currentTextState, $tag);
  2662. if ($p !== false){
  2663. // then there is one to remove
  2664. $this->currentTextState = substr($this->currentTextState,0,$p).substr($this->currentTextState,$p+1);
  2665. }
  2666. }
  2667. break;
  2668. }
  2669. }
  2670. /**
  2671. * add text to the document, at a specified location, size and angle on the page
  2672. */
  2673. public function addText($x, $y, $size, $text, $width = 0, $justification = 'left', $angle = 0, $wordSpaceAdjust = 0, $test=0) {
  2674. if($text == "") return '';
  2675. if (!$this->numFonts) {
  2676. $this->selectFont(dirname(__FILE__) . '/fonts/Helvetica');
  2677. }
  2678. // if there are any open callbacks, then they should be called, to show the start of the line
  2679. if ($this->nCallback > 0){
  2680. for ($i = $this->nCallback; $i > 0; $i--){
  2681. // call each function
  2682. $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'],'descender'=>$this->callback[$i]['descender']);
  2683. $func = $this->callback[$i]['f'];
  2684. $this->$func($info);
  2685. }
  2686. }
  2687. $cf = $this->currentFont;
  2688. // convert ISO-8859-1 into utf8 if necessary
  2689. if(mb_detect_encoding($text) != 'UTF-8'){
  2690. $text=utf8_encode($text);
  2691. }
  2692. // get length of its unicode string
  2693. $len=mb_strlen($text,'UTF-8');
  2694. $directives = $this->getDirectives($text, $x, $y, $width, $size, $justification, $angle, $wordSpaceAdjust);
  2695. //print_r($directives);
  2696. if ($angle == 0) {
  2697. $this->addContent(sprintf("\nBT %.3F %.3F Td", $x, $y));
  2698. } else {
  2699. $a = deg2rad((float)$angle);
  2700. $this->addContent(sprintf("\nBT %.3F %.3F %.3F %.3F %.3F %.3F Tm", cos($a), -sin($a), sin($a), cos($a), $x, $y));
  2701. }
  2702. // only single-byte character will work with word spacing according to PDF 1.3 reference (Chapter 5.2.2)
  2703. if (!$this->fonts[$cf]['isUnicode'] && ($wordSpaceAdjust != 0 || $wordSpaceAdjust != $this->wordSpaceAdjust)) {
  2704. $this->wordSpaceAdjust = $wordSpaceAdjust;
  2705. $this->addContent(sprintf(" %.3F Tw", $wordSpaceAdjust));
  2706. }
  2707. $start=0;
  2708. foreach($directives as $pos => $directive){
  2709. if($pos > $start){
  2710. $part = mb_substr($text,$start,$pos-$start, 'UTF-8');
  2711. $this->addContent(' /F'.$this->currentFontNum.' '.sprintf('%.1F',$size).' Tf ');
  2712. $place_text = $this->filterText($part, false);
  2713. if ($this->fonts[$cf]['isUnicode'] && $wordSpaceAdjust != 0) {
  2714. $s = $this->fonts[$this->currentFont]['C'][32];
  2715. $space_scale = (1000 / $size) * $wordSpaceAdjust + $s;
  2716. $place_text = str_replace("\x00\x20", ') '.(-round($space_scale)).' (', $place_text);
  2717. $this->addContent(" [(".$place_text.")] TJ");
  2718. } else {
  2719. $this->addContent(' ('.$place_text.') Tj');
  2720. }
  2721. }
  2722. $func = $directive['f'];
  2723. if($func == 'defaultFormatting'){ // default font style, bold, italic, ...
  2724. $this->defaultFormatting($directive);
  2725. $this->setCurrentFont();
  2726. } else if($func == 'linebreak') { // line break
  2727. $this->addContent(' ET');
  2728. if ($this->nCallback > 0) {
  2729. for ($j = $this->nCallback; $j > 0; $j--) {
  2730. $info = array(
  2731. 'x' => $directive['x'],
  2732. 'y' => $directive['y'],
  2733. 'angle' => $angle,
  2734. 'status' => 'eol',
  2735. 'p' => $this->callback[$j]['p'],
  2736. 'nCallback' => $this->callback[$j]['nCallback'],
  2737. 'height' => $this->callback[$j]['height'],
  2738. 'descender' => isset($this->callback[$j]['descender'])?$this->callback[$j]['descender']:null
  2739. );
  2740. $func = $this->callback[$j]['f'];
  2741. $this->$func($info);
  2742. }
  2743. }
  2744. return mb_substr($text,$pos + $directive['p'], $len, 'UTF-8');
  2745. } else { // custom callbacks
  2746. $this->addContent(' ET');
  2747. $this->$func($directive);
  2748. $xp = $directive['x'];
  2749. $yp = $directive['y'];
  2750. // restart the text object
  2751. if ($angle==0){
  2752. $this->addContent("\n".'BT '.sprintf('%.3F',$xp).' '.sprintf('%.3F',$yp).' Td');
  2753. } else {
  2754. $a = deg2rad((float)$angle);
  2755. $tmp = "\n".'BT ';
  2756. $tmp .= sprintf('%.3F',cos($a)).' '.sprintf('%.3F',(-1.0*sin($a))).' '.sprintf('%.3F',sin($a)).' '.sprintf('%.3F',cos($a)).' ';
  2757. $tmp .= sprintf('%.3F',$xp).' '.sprintf('%.3F',$yp).' Tm';
  2758. $this->addContent($tmp);
  2759. }
  2760. // only single-byte character will work with word spacing according to PDF 1.3 reference (Chapter 5.2.2)
  2761. if (!$this->fonts[$cf]['isUnicode'] && ($wordSpaceAdjust!=0 || $wordSpaceAdjust != $this->wordSpaceAdjust)){
  2762. $this->wordSpaceAdjust=$wordSpaceAdjust;
  2763. $this->addContent(' '.sprintf('%.3F',$wordSpaceAdjust).' Tw');
  2764. }
  2765. }
  2766. $start = $directive['endTag'];
  2767. }
  2768. if ($start < $len) {
  2769. $part = mb_substr($text,$start, $len - $start,'UTF-8');
  2770. $place_text = $this->filterText($part, false);
  2771. // according to PDF 1.3 reference (Chapter 5.2.2) word spacing works only for single-byte codes
  2772. // So for multiple-byte codes we have to do it manually
  2773. if ($this->fonts[$cf]['isUnicode'] && $wordSpaceAdjust != 0) {
  2774. $space_scale = 1000 / $size;
  2775. $place_text = str_replace(' ', ' ) '.(-round($space_scale*$wordSpaceAdjust)).' (', $place_text);
  2776. }
  2777. $this->addContent(" /F$this->currentFontNum ".sprintf('%.1F Tf', $size));
  2778. $this->addContent(" (".$place_text.") Tj");
  2779. }
  2780. $this->addContent(' ET');
  2781. // if there are any open callbacks, then they should be called, to show the end of the line
  2782. if ($this->nCallback > 0) {
  2783. for ($i = $this->nCallback; $i > 0; $i--) {
  2784. // call each function
  2785. $tmp = $this->getTextLength($size, $text, $width, $angle, $wordSpaceAdjust);
  2786. //$tmp = $this->getTextPosition($x, $y, $angle, $size, $wordSpaceAdjust, $text);
  2787. $info = array(
  2788. 'x' => $tmp[0] + $x,
  2789. 'y' => $tmp[1] + $y,
  2790. 'angle' => $angle,
  2791. 'status' => 'eol',
  2792. 'p' => $this->callback[$i]['p'],
  2793. 'nCallback' => $this->callback[$i]['nCallback'],
  2794. 'height' => $this->callback[$i]['height'],
  2795. 'descender' => $this->callback[$i]['descender']
  2796. );
  2797. $func = $this->callback[$i]['f'];
  2798. $this->$func($info);
  2799. }
  2800. }
  2801. // return text position of the last line break or 0 (if error -1)
  2802. return '';
  2803. }
  2804. public function addTextWrap($x, $y, $size, $text, $width = 0, $justification = 'left', $angle = 0, $wordSpaceAdjust = 0, $test=0){
  2805. while($text) {
  2806. $text = $this->addText($x, $y, $size, $text, $width, $justification, $angle, $wordSpaceAdjust, $test);
  2807. if($text) $y-= $this->getFontHeight($size);
  2808. }
  2809. }
  2810. /*
  2811. * unicode version of php ord to get the decimal of an utf-8 character
  2812. */
  2813. private function uniord($c)
  2814. {
  2815. // important condition to allow char "0" (zero) being converted to decimal
  2816. if(strlen($c) <= 0) return false;
  2817. $ord0 = ord($c{0}); if ($ord0>=0 && $ord0<=127) return $ord0;
  2818. $ord1 = ord($c{1}); if ($ord0>=192 && $ord0<=223) return ($ord0-192)*64 + ($ord1-128);
  2819. $ord2 = ord($c{2}); if ($ord0>=224 && $ord0<=239) return ($ord0-224)*4096 + ($ord1-128)*64 + ($ord2-128);
  2820. $ord3 = ord($c{3}); if ($ord0>=240 && $ord0<=247) return ($ord0-240)*262144 + ($ord1-128)*4096 + ($ord2-128)*64 + ($ord3-128);
  2821. return false;
  2822. }
  2823. /**
  2824. * calculate how wide a given text string will be on a page, at a given size.
  2825. * this can be called externally, but is alse used by the other class functions
  2826. */
  2827. public function getTextWidth($size,$text){
  2828. $regex = "/<\/?([cC]:|)(".$this->allowedTags.")\>/";
  2829. $text=preg_replace($regex, '', $text);
  2830. $tmp = $this->getTextLength($size, $text);
  2831. return $tmp[0];
  2832. }
  2833. private function getTextLength($size, $text, $maxWidth = 0, $angle = 0, $wa = 0){
  2834. // Used to identify any space char for line breaks (either in Unicode or ANSI)
  2835. $spaces = array(32,5760,6158,8192,8193,8194,8195,8196,8197,8198,8200,8201,8202,8203,8204,8205,8287,8288,12288);
  2836. if (!$this->numFonts){
  2837. $this->selectFont('./fonts/Helvetica');
  2838. }
  2839. // convert ISO-8859-1 into utf8 if necessary
  2840. if(mb_detect_encoding($text) != 'UTF-8'){
  2841. $text=utf8_encode($text);
  2842. }
  2843. $a = deg2rad((float)$angle);
  2844. // get length of its unicode string
  2845. $len=mb_strlen($text);
  2846. $cf = $this->currentFont;
  2847. $tw = $maxWidth/$size*1000;
  2848. $break=0;
  2849. $w=0;
  2850. for ($i=0;$i< $len ;$i++){
  2851. $c = mb_substr($text, $i, 1, 'UTF-8');
  2852. $cOrd = $this->uniord($c);
  2853. if($cOrd == 0){
  2854. continue;
  2855. }
  2856. if (isset($this->fonts[$cf]['differences'][$cOrd])){
  2857. // then this character is being replaced by another
  2858. $cOrd2 = $this->fonts[$cf]['differences'][$cOrd];
  2859. } else {
  2860. $cOrd2 = $cOrd;
  2861. }
  2862. if (isset($this->fonts[$cf]['C'][$cOrd2])){
  2863. $w+=$this->fonts[$cf]['C'][$cOrd2];
  2864. }
  2865. // word space adjust
  2866. if($wa > 0 && in_array($cOrd2, $spaces)){
  2867. $w += $wa;
  2868. }
  2869. if($maxWidth > 0 && (cos($a)*$w) > $tw){
  2870. if ($break>0){
  2871. return array(cos($a)*$breakWidth, -sin($a)*$breakWidth, $break, 1);
  2872. } else {
  2873. $ctmp = $cOrd;
  2874. if (isset($this->fonts[$cf]['differences'][$ctmp])){
  2875. $ctmp=$this->fonts[$cf]['differences'][$ctmp];
  2876. }
  2877. $tmpw=($w-$this->fonts[$cf]['C'][$ctmp])*$size/1000;
  2878. // just split before the current character
  2879. return array(cos($a)*$tmpw, -sin($a)*$tmpw, $i, 0);
  2880. }
  2881. }
  2882. // find space or minus for a clean line break
  2883. if(in_array($cOrd2, $spaces) && $maxWidth > 0){
  2884. $break=$i;
  2885. $breakWidth = ($w-$this->fonts[$cf]['C'][$cOrd2])*$size/1000;
  2886. } else if($cOrd2 == 45 && $maxWidth > 0){
  2887. $break=$i;
  2888. $breakWidth = $w*$size/1000;
  2889. }
  2890. }
  2891. $tmpw=$w*$size/1000;
  2892. return array(cos($a)*$tmpw, -sin($a)*$tmpw, 0, 0);
  2893. }
  2894. /**
  2895. * do a part of the calculation for sorting out the justification of the text
  2896. *
  2897. */
  2898. private function adjustWrapText($text,$actual,$width,&$x,&$adjust,$justification){
  2899. switch ($justification){
  2900. case 'left':
  2901. return;
  2902. break;
  2903. case 'right':
  2904. $x+=$width-$actual;
  2905. break;
  2906. case 'center':
  2907. case 'centre':
  2908. $x+=($width-$actual)/2;
  2909. break;
  2910. case 'full':
  2911. // count the number of words
  2912. $words = explode(' ',$text);
  2913. $nspaces=count($words) - 1;
  2914. if ($nspaces>0){
  2915. $adjust = ($width-$actual)/$nspaces;
  2916. } else {
  2917. $adjust=0;
  2918. }
  2919. break;
  2920. }
  2921. }
  2922. /**
  2923. * this will be called at a new page to return the state to what it was on the
  2924. * end of the previous page, before the stack was closed down
  2925. * This is to get around not being able to have open 'q' across pages
  2926. */
  2927. public function saveState($pageEnd=0){
  2928. if ($pageEnd){
  2929. // this will be called at a new page to return the state to what it was on the
  2930. // end of the previous page, before the stack was closed down
  2931. // This is to get around not being able to have open 'q' across pages
  2932. $opt = $this->stateStack[$pageEnd]; // ok to use this as stack starts numbering at 1
  2933. $this->setColor($opt['col']['r'],$opt['col']['g'],$opt['col']['b'],1);
  2934. $this->setStrokeColor($opt['str']['r'],$opt['str']['g'],$opt['str']['b'],1);
  2935. $this->objects[$this->currentContents]['c'].="\n".$opt['lin'];
  2936. // $this->currentLineStyle = $opt['lin'];
  2937. } else {
  2938. $this->nStateStack++;
  2939. $this->stateStack[$this->nStateStack]=array(
  2940. 'col'=>$this->currentColour
  2941. ,'str'=>$this->currentStrokeColour
  2942. ,'lin'=>$this->currentLineStyle
  2943. );
  2944. }
  2945. $this->objects[$this->currentContents]['c'].="\nq";
  2946. }
  2947. /**
  2948. * restore a previously saved state
  2949. */
  2950. public function restoreState($pageEnd=0){
  2951. if (!$pageEnd){
  2952. $n = $this->nStateStack;
  2953. $this->currentColour = $this->stateStack[$n]['col'];
  2954. $this->currentStrokeColour = $this->stateStack[$n]['str'];
  2955. $this->objects[$this->currentContents]['c'].="\n".$this->stateStack[$n]['lin'];
  2956. $this->currentLineStyle = $this->stateStack[$n]['lin'];
  2957. unset($this->stateStack[$n]);
  2958. $this->nStateStack--;
  2959. }
  2960. $this->objects[$this->currentContents]['c'].="\nQ";
  2961. }
  2962. /**
  2963. * make a loose object, the output will go into this object, until it is closed, then will revert to
  2964. * the current one.
  2965. * this object will not appear until it is included within a page.
  2966. * the function will return the object number
  2967. */
  2968. public function openObject(){
  2969. $this->nStack++;
  2970. $this->stack[$this->nStack]=array('c'=>$this->currentContents,'p'=>$this->currentPage);
  2971. // add a new object of the content type, to hold the data flow
  2972. $this->numObj++;
  2973. $this->o_contents($this->numObj,'new');
  2974. $this->currentContents=$this->numObj;
  2975. $this->looseObjects[$this->numObj]=1;
  2976. return $this->numObj;
  2977. }
  2978. /**
  2979. * open an existing object for editing
  2980. */
  2981. public function reopenObject($id){
  2982. $this->nStack++;
  2983. $this->stack[$this->nStack]=array('c'=>$this->currentContents,'p'=>$this->currentPage);
  2984. $this->currentContents=$id;
  2985. // also if this object is the primary contents for a page, then set the current page to its parent
  2986. if (isset($this->objects[$id]['onPage'])){
  2987. $this->currentPage = $this->objects[$id]['onPage'];
  2988. }
  2989. }
  2990. /**
  2991. * close an object
  2992. */
  2993. public function closeObject(){
  2994. // close the object, as long as there was one open in the first place, which will be indicated by
  2995. // an objectId on the stack.
  2996. if ($this->nStack>0){
  2997. $this->currentContents=$this->stack[$this->nStack]['c'];
  2998. $this->currentPage=$this->stack[$this->nStack]['p'];
  2999. $this->nStack--;
  3000. // easier to probably not worry about removing the old entries, they will be overwritten
  3001. // if there are new ones.
  3002. }
  3003. }
  3004. /**
  3005. * stop an object from appearing on pages from this point on
  3006. */
  3007. public function stopObject($id){
  3008. // if an object has been appearing on pages up to now, then stop it, this page will
  3009. // be the last one that could contian it.
  3010. if (isset($this->addLooseObjects[$id])){
  3011. $this->addLooseObjects[$id]='';
  3012. }
  3013. }
  3014. /**
  3015. * after an object has been created, it wil only show if it has been added, using this function.
  3016. */
  3017. public function addObject($id,$options='add'){
  3018. // add the specified object to the page
  3019. if (isset($this->looseObjects[$id]) && $this->currentContents!=$id){
  3020. // then it is a valid object, and it is not being added to itself
  3021. switch($options){
  3022. case 'all':
  3023. // then this object is to be added to this page (done in the next block) and
  3024. // all future new pages.
  3025. $this->addLooseObjects[$id]='all';
  3026. case 'add':
  3027. if (isset($this->objects[$this->currentContents]['onPage'])){
  3028. // then the destination contents is the primary for the page
  3029. // (though this object is actually added to that page)
  3030. $this->o_page($this->objects[$this->currentContents]['onPage'],'content',$id);
  3031. }
  3032. break;
  3033. case 'even':
  3034. $this->addLooseObjects[$id]='even';
  3035. $pageObjectId=$this->objects[$this->currentContents]['onPage'];
  3036. if ($this->objects[$pageObjectId]['info']['pageNum']%2==0){
  3037. $this->addObject($id); // hacky huh :)
  3038. }
  3039. break;
  3040. case 'odd':
  3041. $this->addLooseObjects[$id]='odd';
  3042. $pageObjectId=$this->objects[$this->currentContents]['onPage'];
  3043. if ($this->objects[$pageObjectId]['info']['pageNum']%2==1){
  3044. $this->addObject($id); // hacky huh :)
  3045. }
  3046. break;
  3047. case 'next':
  3048. $this->addLooseObjects[$id]='all';
  3049. break;
  3050. case 'nexteven':
  3051. $this->addLooseObjects[$id]='even';
  3052. break;
  3053. case 'nextodd':
  3054. $this->addLooseObjects[$id]='odd';
  3055. break;
  3056. }
  3057. }
  3058. }
  3059. /**
  3060. * add content to the documents info object
  3061. */
  3062. public function addInfo($label,$value=0){
  3063. // this will only work if the label is one of the valid ones.
  3064. // modify this so that arrays can be passed as well.
  3065. // if $label is an array then assume that it is key=>value pairs
  3066. // else assume that they are both scalar, anything else will probably error
  3067. if (is_array($label)){
  3068. foreach ($label as $l=>$v){
  3069. $this->o_info($this->infoObject,$l,$v);
  3070. }
  3071. } else {
  3072. $this->o_info($this->infoObject,$label,$value);
  3073. }
  3074. }
  3075. /**
  3076. * set the viewer preferences of the document, it is up to the browser to obey these.
  3077. */
  3078. public function setPreferences($label,$value=0){
  3079. // this will only work if the label is one of the valid ones.
  3080. if (is_array($label)){
  3081. foreach ($label as $l=>$v){
  3082. $this->o_catalog($this->catalogId,'viewerPreferences',array($l=>$v));
  3083. }
  3084. } else {
  3085. $this->o_catalog($this->catalogId,'viewerPreferences',array($label=>$value));
  3086. }
  3087. }
  3088. /**
  3089. * extract an integer from a position in a byte stream
  3090. *
  3091. */
  3092. private function getBytes(&$data,$pos,$num){
  3093. // return the integer represented by $num bytes from $pos within $data
  3094. $ret=0;
  3095. for ($i=0;$i<$num;$i++){
  3096. $ret=$ret*256;
  3097. $ret+=ord($data[$pos+$i]);
  3098. }
  3099. return $ret;
  3100. }
  3101. /**
  3102. * reads the PNG chunk
  3103. * @param $data - binary part of the png image
  3104. */
  3105. private function readPngChunks(&$data){
  3106. $default = array('info'=> array(), 'transparency'=> null, 'idata'=> null, 'pdata'=> null, 'haveHeader'=> false);
  3107. // set pointer
  3108. $p = 8;
  3109. $len = strlen($data);
  3110. // cycle through the file, identifying chunks
  3111. while ($p<$len){
  3112. $chunkLen = $this->getBytes($data,$p,4);
  3113. $chunkType = substr($data,$p+4,4);
  3114. //error_log($chunkType. ' - '.$chunkLen);
  3115. switch($chunkType){
  3116. case 'IHDR':
  3117. //this is where all the file information comes from
  3118. $default['info']['width']=$this->getBytes($data,$p+8,4);
  3119. $default['info']['height']=$this->getBytes($data,$p+12,4);
  3120. $default['info']['bitDepth']=ord($data[$p+16]);
  3121. $default['info']['colorType']=ord($data[$p+17]);
  3122. $default['info']['compressionMethod']=ord($data[$p+18]);
  3123. $default['info']['filterMethod']=ord($data[$p+19]);
  3124. $default['info']['interlaceMethod']=ord($data[$p+20]);
  3125. $this->debug('readPngChunks: ColorType is' . $default['info']['colorType'], E_USER_NOTICE);
  3126. $default['haveHeader'] = true;
  3127. if ($default['info']['compressionMethod']!=0){
  3128. $error = true;
  3129. $errormsg = "unsupported compression method";
  3130. }
  3131. if ($default['info']['filterMethod']!=0){
  3132. $error = true;
  3133. $errormsg = "unsupported filter method";
  3134. }
  3135. $default['transparency'] = array('type'=> null, 'data' => null);
  3136. if ($default['info']['colorType'] == 3) { // indexed color, rbg
  3137. // corresponding to entries in the plte chunk
  3138. // Alpha for palette index 0: 1 byte
  3139. // Alpha for palette index 1: 1 byte
  3140. // ...etc...
  3141. // there will be one entry for each palette entry. up until the last non-opaque entry.
  3142. // set up an array, stretching over all palette entries which will be o (opaque) or 1 (transparent)
  3143. $default['transparency']['type']='indexed';
  3144. //$numPalette = strlen($default['pdata'])/3;
  3145. $trans=0;
  3146. for ($i=$chunkLen;$i>=0;$i--){
  3147. if (ord($data[$p+8+$i])==0){
  3148. $trans=$i;
  3149. }
  3150. }
  3151. $default['transparency']['data'] = $trans;
  3152. } elseif($default['info']['colorType'] == 0) { // grayscale
  3153. // corresponding to entries in the plte chunk
  3154. // Gray: 2 bytes, range 0 .. (2^bitdepth)-1
  3155. // $transparency['grayscale']=$this->getBytes($data,$p+8,2); // g = grayscale
  3156. $default['transparency']['type']='indexed';
  3157. $default['transparency']['data'] = ord($data[$p+8+1]);
  3158. } elseif($default['info']['colorType'] == 2) { // truecolor
  3159. // corresponding to entries in the plte chunk
  3160. // Red: 2 bytes, range 0 .. (2^bitdepth)-1
  3161. // Green: 2 bytes, range 0 .. (2^bitdepth)-1
  3162. // Blue: 2 bytes, range 0 .. (2^bitdepth)-1
  3163. $default['transparency']['r']=$this->getBytes($data,$p+8,2); // r from truecolor
  3164. $default['transparency']['g']=$this->getBytes($data,$p+10,2); // g from truecolor
  3165. $default['transparency']['b']=$this->getBytes($data,$p+12,2); // b from truecolor
  3166. } else if($default['info']['colorType'] == 6 || $default['info']['colorType'] == 4) {
  3167. // set transparency type to "alpha" and proceed with it in $this->o_image later
  3168. $default['transparency']['type'] = 'alpha';
  3169. $img = imagecreatefromstring($data);
  3170. $imgalpha = imagecreate($default['info']['width'], $default['info']['height']);
  3171. // generate gray scale palette (0 -> 255)
  3172. for ($c = 0; $c < 256; ++$c) {
  3173. ImageColorAllocate($imgalpha, $c, $c, $c);
  3174. }
  3175. // extract alpha channel
  3176. for ($xpx = 0; $xpx < $default['info']['width']; ++$xpx) {
  3177. for ($ypx = 0; $ypx < $default['info']['height']; ++$ypx) {
  3178. $colorBits = imagecolorat($img, $xpx, $ypx);
  3179. $color = imagecolorsforindex($img, $colorBits);
  3180. $color['alpha'] = (((127 - $color['alpha']) / 127) * 255);
  3181. imagesetpixel($imgalpha, $xpx, $ypx, $color['alpha']);
  3182. }
  3183. }
  3184. $tmpfile_alpha=tempnam($this->tempPath,'ezImg');
  3185. imagepng($imgalpha, $tmpfile_alpha);
  3186. imagedestroy($imgalpha);
  3187. $alphaData = file_get_contents($tmpfile_alpha);
  3188. // nested method call to receive info on alpha image
  3189. $alphaImg = $this->readPngChunks($alphaData);
  3190. // use 'pdate' to fill alpha image as "palette". But it s the alpha channel
  3191. $default['pdata'] = $alphaImg['idata'];
  3192. // generate true color image with no alpha channel
  3193. $tmpfile_tt=tempnam($this->tempPath,'ezImg');
  3194. $imgplain = imagecreatetruecolor($default['info']['width'], $default['info']['height']);
  3195. imagecopy($imgplain, $img, 0, 0, 0, 0, $default['info']['width'], $default['info']['height']);
  3196. imagepng($imgplain, $tmpfile_tt);
  3197. imagedestroy($imgplain);
  3198. $ttData = file_get_contents($tmpfile_tt);
  3199. $ttImg = $this->readPngChunks($ttData);
  3200. $default['idata'] = $ttImg['idata'];
  3201. // remove temp files
  3202. unlink($tmpfile_alpha);
  3203. unlink($tmpfile_tt);
  3204. // return to addPngImage prematurely. IDAT has already been read and PLTE is not required
  3205. return $default;
  3206. }
  3207. break;
  3208. case 'PLTE':
  3209. $default['pdata'] = substr($data,$p+8,$chunkLen);
  3210. break;
  3211. case 'IDAT':
  3212. $default['idata'] .= substr($data,$p+8,$chunkLen);
  3213. break;
  3214. case 'tRNS': // this HEADER info is optional. More info: rfc2083 (http://tools.ietf.org/html/rfc2083)
  3215. // error_log('OPTIONAL HEADER -tRNS- exist:');
  3216. // this chunk can only occur once and it must occur after the PLTE chunk and before IDAT chunk
  3217. // KS End new code
  3218. break;
  3219. default:
  3220. break;
  3221. }
  3222. $p += $chunkLen+12;
  3223. }
  3224. return $default;
  3225. }
  3226. /**
  3227. * add a PNG image into the document, from a file
  3228. * this should work with remote files
  3229. */
  3230. public function addPngFromFile($file,$x,$y,$w=0,$h=0,$angle=0){
  3231. // read in a png file, interpret it, then add to the system
  3232. $error = false;
  3233. $errormsg = "";
  3234. $this->debug('addPngFromFile: opening image ' . $file);
  3235. $data = file_get_contents($file);
  3236. if($data === false){
  3237. $this->debug('addPngFromFile: trouble opening file ' . $file, E_USER_WARNING);
  3238. return;
  3239. }
  3240. $header = chr(137).chr(80).chr(78).chr(71).chr(13).chr(10).chr(26).chr(10);
  3241. if (substr($data,0,8)!=$header){
  3242. $this->debug('addPngFromFile: Invalid PNG header for file: ' . $file, E_USER_WARNING);
  3243. return;
  3244. }
  3245. $iChunk = $this->readPngChunks($data);
  3246. if(!$iChunk['haveHeader']){
  3247. $error = true;
  3248. $errormsg = "information header is missing.";
  3249. }
  3250. if (isset($iChunk['info']['interlaceMethod']) && $iChunk['info']['interlaceMethod']){
  3251. $error = true;
  3252. $errormsg = "There appears to be no support for interlaced images in pdf.";
  3253. }
  3254. if ($iChunk['info']['bitDepth'] > 8){
  3255. $error = true;
  3256. $errormsg = "only bit depth of 8 or less is supported.";
  3257. }
  3258. if ($iChunk['info']['colorType'] == 1 || $iChunk['info']['colorType'] == 5 || $iChunk['info']['colorType']== 7){
  3259. $error = true;
  3260. $errormsg = 'Unsupported PNG color type: '.$iChunk['info']['colorType'];
  3261. } else if(isset($iChunk['info'])) {
  3262. switch ($iChunk['info']['colorType']){
  3263. case 3:
  3264. $color = 'DeviceRGB';
  3265. $ncolor=1;
  3266. break;
  3267. case 6:
  3268. case 2:
  3269. $color = 'DeviceRGB';
  3270. $ncolor=3;
  3271. break;
  3272. case 4:
  3273. case 0:
  3274. $color = 'DeviceGray';
  3275. $ncolor=1;
  3276. break;
  3277. }
  3278. }
  3279. if ($error){
  3280. $this->debug('addPngFromFile: '.$errormsg, E_USER_WARNING);
  3281. return;
  3282. }
  3283. if ($w==0){
  3284. $w=$h/$iChunk['info']['height']*$iChunk['info']['width'];
  3285. }
  3286. if ($h==0){
  3287. $h=$w*$iChunk['info']['height']/$iChunk['info']['width'];
  3288. }
  3289. if($this->hashed){
  3290. $oHash = md5($iChunk['idata']);
  3291. }
  3292. if(isset($oHash) && isset($this->objectHash[$oHash])){
  3293. $label = $this->objectHash[$oHash];
  3294. }else{
  3295. $this->numImages++;
  3296. $label='I'.$this->numImages;
  3297. $this->numObj++;
  3298. if(isset($oHash)){
  3299. $this->objectHash[$oHash] = $label;
  3300. }
  3301. $options = array('label'=>$label,'data'=>$iChunk['idata'],'bitsPerComponent'=>$iChunk['info']['bitDepth'],'pdata'=>$iChunk['pdata']
  3302. ,'iw'=>$iChunk['info']['width'],'ih'=>$iChunk['info']['height'],'type'=>'png','color'=>$color,'ncolor'=>$ncolor);
  3303. if (isset($iChunk['transparency'])){
  3304. $options['transparency']=$iChunk['transparency'];
  3305. }
  3306. $this->o_image($this->numObj,'new',$options);
  3307. }
  3308. $this->objects[$this->currentContents]['c'].="\nq";
  3309. if($angle != 0)
  3310. {
  3311. // add the angle if other than zero
  3312. $a = deg2rad((float)$angle);
  3313. $cx = ($w/2);
  3314. $cy = ($h/2);
  3315. $this->objects[$this->currentContents]['c'].= sprintf(' 1 0 0 1 %.3F %.3F cm', $x + $cx, $y + $cy);
  3316. $this->objects[$this->currentContents]['c'].= sprintf(' %.3F %.3F %.3F %.3F 0 0 cm',cos($a), sin($a), -1*sin($a), cos($a));
  3317. $this->objects[$this->currentContents]['c'].= sprintf(' %.3F 0 0 %.3F %.3F %.3F cm',$w, $h, -$cx, -$cy);
  3318. } else {
  3319. $this->objects[$this->currentContents]['c'].= sprintf(' %.3F 0 0 %.3F %.3F %.3F cm',$w, $h, $x, $y);
  3320. }
  3321. $this->objects[$this->currentContents]['c'].=" /".$label.' Do';
  3322. $this->objects[$this->currentContents]['c'].=" Q";
  3323. }
  3324. /**
  3325. * add a JPEG image into the document, from a file
  3326. */
  3327. public function addJpegFromFile($img,$x,$y,$w=0,$h=0,$angle=0){
  3328. // attempt to add a jpeg image straight from a file, using no GD commands
  3329. // note that this function is unable to operate on a remote file.
  3330. if (!file_exists($img)){
  3331. return;
  3332. }
  3333. $tmp=getimagesize($img);
  3334. $imageWidth=$tmp[0];
  3335. $imageHeight=$tmp[1];
  3336. if (isset($tmp['channels'])){
  3337. $channels = $tmp['channels'];
  3338. } else {
  3339. $channels = 3;
  3340. }
  3341. if ($w<=0 && $h<=0){
  3342. $w=$imageWidth;
  3343. }
  3344. if ($w==0){
  3345. $w=$h/$imageHeight*$imageWidth;
  3346. }
  3347. if ($h==0){
  3348. $h=$w*$imageHeight/$imageWidth;
  3349. }
  3350. $data = file_get_contents($img);
  3351. $this->addJpegImage_common($data,$x,$y,$w,$h,$angle,$imageWidth,$imageHeight,$channels);
  3352. }
  3353. /**
  3354. * read gif image from file, converts it into an JPEG (no transparancy) and display it
  3355. * @param $img - file path ti gif image
  3356. * @param $x - coord x
  3357. * @param $y - y cord
  3358. * @param $w - width
  3359. * @param $h - height
  3360. */
  3361. public function addGifFromFile($img, $x, $y, $w=0, $h=0){
  3362. if (!file_exists($img)){
  3363. return;
  3364. }
  3365. if(!function_exists("imagecreatefromgif")){
  3366. $this->debug('addGifFromFile: Missing GD function imageCreateFromGif', E_USER_ERROR);
  3367. return;
  3368. }
  3369. $tmp=getimagesize($img);
  3370. $imageWidth=$tmp[0];
  3371. $imageHeight=$tmp[1];
  3372. if ($w<=0 && $h<=0){
  3373. $w=$imageWidth;
  3374. }
  3375. if ($w==0){
  3376. $w=$h/$imageHeight*$imageWidth;
  3377. }
  3378. if ($h==0){
  3379. $h=$w*$imageHeight/$imageWidth;
  3380. }
  3381. $imgres = imagecreatefromgif($img);
  3382. $tmpName=tempnam($this->tempPath,'img');
  3383. imagejpeg($imgres,$tmpName,90);
  3384. $this->addJpegFromFile($tmpName,$x,$y,$w,$h);
  3385. }
  3386. /**
  3387. * add an image into the document, from a GD object
  3388. * this function is not all that reliable, and I would probably encourage people to use
  3389. * the file based functions
  3390. * @param $img - gd image resource
  3391. * @param $x coord x
  3392. * @param $y coord y
  3393. * @param $w width
  3394. * @param $h height
  3395. * @param $quality image quality
  3396. */
  3397. protected function addImage(&$img,$x,$y,$w=0,$h=0,$quality=75,$angle=0){
  3398. // add a new image into the current location, as an external object
  3399. // add the image at $x,$y, and with width and height as defined by $w & $h
  3400. // note that this will only work with full colour images and makes them jpg images for display
  3401. // later versions could present lossless image formats if there is interest.
  3402. // there seems to be some problem here in that images that have quality set above 75 do not appear
  3403. // not too sure why this is, but in the meantime I have restricted this to 75.
  3404. if ($quality>75){
  3405. $quality=75;
  3406. }
  3407. // if the width or height are set to zero, then set the other one based on keeping the image
  3408. // height/width ratio the same, if they are both zero, then give up :)
  3409. $imageWidth=imagesx($img);
  3410. $imageHeight=imagesy($img);
  3411. if ($w<=0 && $h<=0){
  3412. return;
  3413. }
  3414. if ($w==0){
  3415. $w=$h/$imageHeight*$imageWidth;
  3416. }
  3417. if ($h==0){
  3418. $h=$w*$imageHeight/$imageWidth;
  3419. }
  3420. $tmpName=tempnam($this->tempPath,'img');
  3421. imagejpeg($img,$tmpName,$quality);
  3422. $data = file_get_contents($tmpName);
  3423. if($data === false) {
  3424. $this->debug('addImage: trouble opening image resource', E_USER_WARNING);
  3425. }
  3426. unlink($tmpName);
  3427. $this->addJpegImage_common($data,$x,$y,$w,$h,$angle,$imageWidth,$imageHeight);
  3428. }
  3429. /**
  3430. * common code used by the two JPEG adding functions
  3431. */
  3432. private function addJpegImage_common(&$data,$x,$y,$w=0,$h=0,$angle,$imageWidth,$imageHeight,$channels=3){
  3433. // note that this function is not to be called externally
  3434. // it is just the common code between the GD and the file options
  3435. if($this->hashed){
  3436. $oHash = md5($data);
  3437. }
  3438. if(isset($oHash) && isset($this->objectHash[$oHash])){
  3439. $label = $this->objectHash[$oHash];
  3440. }else{
  3441. $this->numImages++;
  3442. $label='I'.$this->numImages;
  3443. $this->numObj++;
  3444. if(isset($oHash)){
  3445. $this->objectHash[$oHash] = $label;
  3446. }
  3447. $this->o_image($this->numObj,'new',array('label'=>$label,'data'=>$data,'iw'=>$imageWidth,'ih'=>$imageHeight,'channels'=>$channels));
  3448. }
  3449. $this->objects[$this->currentContents]['c'].="\nq";
  3450. if($angle != 0)
  3451. {
  3452. // add the angle if other than zero
  3453. $a = deg2rad((float)$angle);
  3454. $cx = ($w/2);
  3455. $cy = ($h/2);
  3456. $this->objects[$this->currentContents]['c'].= sprintf(' 1 0 0 1 %.3F %.3F cm', $x + $cx, $y + $cy);
  3457. $this->objects[$this->currentContents]['c'].= sprintf(' %.3F %.3F %.3F %.3F 0 0 cm',cos($a), sin($a), -1*sin($a), cos($a));
  3458. $this->objects[$this->currentContents]['c'].= sprintf(' %.3F 0 0 %.3F %.3F %.3F cm',$w, $h, -$cx, -$cy);
  3459. } else {
  3460. $this->objects[$this->currentContents]['c'].= sprintf(' %.3F 0 0 %.3F %.3F %.3F cm',$w, $h, $x, $y);
  3461. }
  3462. $this->objects[$this->currentContents]['c'].=" /".$label.' Do';
  3463. $this->objects[$this->currentContents]['c'].=" Q";
  3464. }
  3465. /**
  3466. * specify where the document should open when it first starts
  3467. */
  3468. public function openHere($style,$a=0,$b=0,$c=0){
  3469. // this function will open the document at a specified page, in a specified style
  3470. // the values for style, and the required paramters are:
  3471. // 'XYZ' left, top, zoom
  3472. // 'Fit'
  3473. // 'FitH' top
  3474. // 'FitV' left
  3475. // 'FitR' left,bottom,right
  3476. // 'FitB'
  3477. // 'FitBH' top
  3478. // 'FitBV' left
  3479. $this->numObj++;
  3480. $this->o_destination($this->numObj,'new',array('page'=>$this->currentPage,'type'=>$style,'p1'=>$a,'p2'=>$b,'p3'=>$c));
  3481. $id = $this->catalogId;
  3482. $this->o_catalog($id,'openHere',$this->numObj);
  3483. }
  3484. /**
  3485. * create a labelled destination within the document
  3486. */
  3487. public function addDestination($label,$style,$a=0,$b=0,$c=0){
  3488. // associates the given label with the destination, it is done this way so that a destination can be specified after
  3489. // it has been linked to
  3490. // styles are the same as the 'openHere' function
  3491. $this->numObj++;
  3492. $this->o_destination($this->numObj,'new',array('page'=>$this->currentPage,'type'=>$style,'p1'=>$a,'p2'=>$b,'p3'=>$c));
  3493. $id = $this->numObj;
  3494. // store the label->idf relationship, note that this means that labels can be used only once
  3495. $this->destinations["$label"]=$id;
  3496. }
  3497. /**
  3498. * define font families, this is used to initialize the font families for the default fonts
  3499. * and for the user to add new ones for their fonts. The default bahavious can be overridden should
  3500. * that be desired.
  3501. */
  3502. public function setFontFamily($family, $options = ''){
  3503. if (is_array($options)) {
  3504. // the user is trying to set a font family
  3505. // note that this can also be used to set the base ones to something else
  3506. if (strlen($family)){
  3507. $this->fontFamilies[$family] = $options;
  3508. }
  3509. }
  3510. }
  3511. /**
  3512. * used to add messages for use in debugging
  3513. */
  3514. protected function debug($message, $error_type = E_USER_NOTICE)
  3515. {
  3516. if($error_type <= $this->DEBUGLEVEL){
  3517. switch(strtolower($this->DEBUG)){
  3518. default:
  3519. case 'none':
  3520. break;
  3521. case 'error_log':
  3522. error_log($message);
  3523. break;
  3524. case 'variable':
  3525. $this->messages.=$message."\n";
  3526. break;
  3527. }
  3528. }
  3529. }
  3530. /**
  3531. * a few functions which should allow the document to be treated transactionally.
  3532. *
  3533. * @param string $action WHAT IS THIS?
  3534. * @return void
  3535. */
  3536. public function transaction($action){
  3537. switch ($action){
  3538. case 'start':
  3539. // store all the data away into the checkpoint variable
  3540. $data = get_object_vars($this);
  3541. $this->checkpoint = $data;
  3542. unset($data);
  3543. break;
  3544. case 'commit':
  3545. if (is_array($this->checkpoint) && isset($this->checkpoint['checkpoint'])){
  3546. $tmp = $this->checkpoint['checkpoint'];
  3547. $this->checkpoint = $tmp;
  3548. unset($tmp);
  3549. } else {
  3550. $this->checkpoint='';
  3551. }
  3552. break;
  3553. case 'rewind':
  3554. // do not destroy the current checkpoint, but move us back to the state then, so that we can try again
  3555. if (is_array($this->checkpoint)){
  3556. // can only abort if were inside a checkpoint
  3557. $tmp = $this->checkpoint;
  3558. foreach ($tmp as $k=>$v){
  3559. if ($k != 'checkpoint'){
  3560. $this->$k=$v;
  3561. }
  3562. }
  3563. unset($tmp);
  3564. }
  3565. break;
  3566. case 'abort':
  3567. if (is_array($this->checkpoint)){
  3568. // can only abort if were inside a checkpoint
  3569. $tmp = $this->checkpoint;
  3570. foreach ($tmp as $k=>$v){
  3571. $this->$k=$v;
  3572. }
  3573. unset($tmp);
  3574. }
  3575. break;
  3576. }
  3577. }
  3578. } // end of class
  3579. ?>