PageRenderTime 82ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/includes/cezpdf/Cpdf.php

https://bitbucket.org/adrianmm/pr-ctica-de-facturaci-n-de-2-daw
PHP | 3899 lines | 2616 code | 358 blank | 925 comment | 573 complexity | e2a5849c82c34ff5b1d6951d0e7fc281 MD5 | raw file
Possible License(s): GPL-3.0
  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 [0.12-rc12] $Id: Cpdf.php 207 2013-11-19 16:18:14Z 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. private $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('Ymd');
  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. * return the pdf stream as a string returned from the function
  1645. * This method is protect to force user to use ezOutput from Cezpdf.php
  1646. */
  1647. function output($debug=0){
  1648. if ($debug){
  1649. // turn compression off
  1650. $this->options['compression']=0;
  1651. }
  1652. if ($this->arc4_objnum){
  1653. $this->ARC4_init($this->encryptionKey);
  1654. }
  1655. if($this->valid){
  1656. $this->debug('The output method has been executed again', E_USER_WARNING);
  1657. }
  1658. $this->checkAllHere();
  1659. $xref=array();
  1660. // set the pdf version dynamically, depended on the objects being used
  1661. $content="%PDF-".sprintf('%.1F', $this->pdfversion)."\n%\xe2\xe3\xcf\xd3";
  1662. $pos=strlen($content);
  1663. foreach($this->objects as $k=>$v){
  1664. $tmp='o_'.$v['t'];
  1665. $cont=$this->$tmp($k,'out');
  1666. $content.=$cont;
  1667. $xref[]=$pos;
  1668. $pos+=strlen($cont);
  1669. }
  1670. ++$pos;
  1671. $content.="\nxref\n0 ".(count($xref)+1)."\n0000000000 65535 f \n";
  1672. foreach($xref as $p){
  1673. $content.=substr('0000000000',0,10-strlen($p+1)).($p+1)." 00000 n \n";
  1674. }
  1675. $content.="trailer\n<< /Size ".(count($xref)+1)." /Root 1 0 R /Info ".$this->infoObject." 0 R";
  1676. // if encryption has been applied to this document then add the marker for this dictionary
  1677. if ($this->arc4_objnum > 0){
  1678. $content .= " /Encrypt ".$this->arc4_objnum." 0 R";
  1679. }
  1680. if (strlen($this->fileIdentifier)){
  1681. $content .= " /ID [<".$this->fileIdentifier."><".$this->fileIdentifier.">]";
  1682. }
  1683. $content .= " >>\nstartxref\n".$pos."\n%%EOF\n";
  1684. return $content;
  1685. }
  1686. /**
  1687. * intialize a new document
  1688. * if this is called on an existing document results may be unpredictable, but the existing document would be lost at minimum
  1689. * this function is called automatically by the constructor function
  1690. *
  1691. */
  1692. protected function newDocument($pageSize=array(0,0,612,792)){
  1693. $this->numObj=0;
  1694. $this->objects = array();
  1695. $this->numObj++;
  1696. $this->o_catalog($this->numObj,'new');
  1697. $this->numObj++;
  1698. $this->o_outlines($this->numObj,'new');
  1699. $this->numObj++;
  1700. $this->o_pages($this->numObj,'new');
  1701. $this->o_pages($this->numObj,'mediaBox',$pageSize);
  1702. $this->currentNode = 3;
  1703. $this->o_pages($this->numObj, 'procset', '[/PDF/TEXT/ImageB/ImageC/ImageI]');
  1704. $this->numObj++;
  1705. $this->o_info($this->numObj,'new');
  1706. $this->numObj++;
  1707. $this->o_page($this->numObj,'new');
  1708. // need to store the first page id as there is no way to get it to the user during
  1709. // startup
  1710. $this->firstPageId = $this->currentContents;
  1711. }
  1712. /**
  1713. * open the font file and return a php structure containing it.
  1714. * first check if this one has been done before and saved in a form more suited to php
  1715. * note that if a php serialized version does not exist it will try and make one, but will
  1716. * require write access to the directory to do it... it is MUCH faster to have these serialized
  1717. * files.
  1718. *
  1719. * @param string $font Font name (can contain both path and extension)
  1720. *
  1721. * @return bool true on success, false on error
  1722. */
  1723. protected function openFont($font) {
  1724. // $font should only contain the font name
  1725. $fullFontPath = $this->fontPath.'/'.$font;
  1726. $this->debug('openFont: '.$fullFontPath.' / IsUnicode: '.$this->isUnicode);
  1727. // PATCH #13 - isUnicode cachedFile (font) problem | thank you jafjaf
  1728. if ($this->isUnicode){
  1729. $cachedFile = 'cached'.$font.'unicode.php';
  1730. } else {
  1731. $cachedFile = 'cached'.$font.'.php';
  1732. }
  1733. // use the temp folder to read/write cached font data
  1734. if (file_exists($this->tempPath.'/'.$cachedFile)) {
  1735. $cacheDate = filemtime($this->tempPath.'/'.$cachedFile);
  1736. if(($cacheDate + $this->cacheTimeout) >= time() ) {
  1737. $this->debug('openFont: font cache found in '.$this->tempPath.'/'.$cachedFile);
  1738. $this->fonts[$font] = require($this->tempPath.'/'.$cachedFile);
  1739. if (isset($this->fonts[$font]['_version_']) && $this->fonts[$font]['_version_'] == 3) {
  1740. // cache is valid - but without checking for a valid font path
  1741. return true;
  1742. }
  1743. }
  1744. }
  1745. // if no cache is found, parse the font file and rebuild the cache
  1746. $this->debug('openFont: rebuilding font cache '.$cachedFile, E_USER_NOTICE);
  1747. if(file_exists($fullFontPath.'.ttf') && class_exists('TTF')){
  1748. $ttf = new TTF(file_get_contents($fullFontPath.'.ttf'));
  1749. $head = $ttf->unmarshalHead();
  1750. $uname = $ttf->unmarshalName();
  1751. $hhea = $ttf->unmarshalHhea();
  1752. $post = $ttf->unmarshalPost(true);
  1753. $maxp = $ttf->unmarshalMAXP();
  1754. $cmap = $ttf->unmarshalCmap();
  1755. $cachedFont = array(
  1756. 'isUnicode' => $this->isUnicode,
  1757. 'ItalicAngle' => $post['italicAngle'],
  1758. 'UnderlineThickness' => $post['underlineThickness'],
  1759. 'UnderlinePosition' => $post['underlinePosition'],
  1760. 'IsFixedPitch' => ($post['isFixedPitch'] == 0)? false : true,
  1761. 'Ascender' => $hhea['ascender'],
  1762. 'Descender' => $hhea['descender'],
  1763. 'LineGap' => $hhea['lineGap'],
  1764. 'FontName' => $font
  1765. );
  1766. foreach($uname['nameRecords'] as $v){
  1767. if($v['nameID'] == 1 && $v['languageID'] == 0){
  1768. // fetch FontFamily from Default language (en?)
  1769. $cachedFont['FamilyName'] = preg_replace('/\x00/','',$v['value']);
  1770. } else if($v['nameID'] == 2 && $v['languageID'] == 0){
  1771. // fetch font weight from Default language (en?)
  1772. $cachedFont['Weight'] = preg_replace('/\x00/','',$v['value']);
  1773. } else if($v['nameID'] == 3 && $v['languageID'] == 0){
  1774. // fetch Unique font name from Default language (en?)
  1775. $cachedFont['UniqueName'] = preg_replace('/\x00/','',$v['value']);
  1776. } else if($v['nameID'] == 4 && $v['languageID'] == 0){
  1777. // fetch font name (full style) from Default language (en?)
  1778. $cachedFont['FullName'] = preg_replace('/\x00/','',$v['value']);
  1779. } else if($v['nameID'] == 5 && $v['languageID'] == 0){
  1780. // fetch version from Default language (en?)
  1781. $cachedFont['Version'] = preg_replace('/\x00/','',$v['value']);
  1782. }
  1783. }
  1784. // calculate the bounding box properly by using 'units per em' property
  1785. $cachedFont['FontBBox'] = array(
  1786. intval($head['xMin'] / ($head['unitsPerEm'] / 1000)),
  1787. intval($head['yMin'] / ($head['unitsPerEm'] / 1000)),
  1788. intval($head['xMax'] / ($head['unitsPerEm'] / 1000)),
  1789. intval($head['yMax'] / ($head['unitsPerEm'] / 1000))
  1790. );
  1791. $cachedFont['UnitsPerEm'] = $head['unitsPerEm'];
  1792. $encodingTable = array();
  1793. $hmetrics = $ttf->unmarshalHmtx($hhea['numberOfHMetrics'],$maxp['numGlyphs']);
  1794. // get format 6 or format 4 as primary cmap table map glyph with character
  1795. foreach($cmap['tables'] as $v){
  1796. if(isset($v['format']) && $v['format'] == "4"){
  1797. $encodingTable = $v;
  1798. break;
  1799. }
  1800. }
  1801. if($encodingTable['format'] == '4') {
  1802. $glyphsIndices = range(1, $maxp['numGlyphs']);
  1803. $charToGlyph = array();
  1804. $segCount = $encodingTable['segCount'];
  1805. $endCountArray = $encodingTable['endCountArray'];
  1806. $startCountArray = $encodingTable['startCountArray'];
  1807. $idDeltaArray = $encodingTable['idDeltaArray'];
  1808. $idRangeOffsetArray = $encodingTable['idRangeOffsetArray'];
  1809. $glyphIdArray = $encodingTable['glyphIdArray'];
  1810. for ($seg = 0; $seg < $segCount; $seg++) {
  1811. $endCount = $endCountArray[$seg];
  1812. $startCount = $startCountArray[$seg];
  1813. $idDelta = $idDeltaArray[$seg];
  1814. $idRangeOffset = $idRangeOffsetArray[$seg];
  1815. for ($charCode = $startCount; $charCode <= $endCount; $charCode++) {
  1816. if ($idRangeOffset != 0) {
  1817. $j = $charCode - $startCount + $seg + $idRangeOffset / 2 - $segCount;
  1818. $gid0 = $glyphIdArray[$j];
  1819. } else {
  1820. $gid0 = $idDelta + $charCode;
  1821. }
  1822. $gid0 %= 65536;
  1823. if (in_array($gid0, $glyphsIndices)) {
  1824. $charToGlyph[sprintf("%d", $charCode)] = $gid0;
  1825. }
  1826. }
  1827. }
  1828. $cidtogid = str_pad('', 256*256*2, "\x00");
  1829. $cachedFont['C'] = array();
  1830. foreach($charToGlyph as $char => $glyphIndex){
  1831. if(!empty($char)){
  1832. $m = TTF::getHMetrics($hmetrics, $hhea['numberOfHMetrics'], $glyphIndex);
  1833. // calculate the correct char width by dividing it with 'units per em'
  1834. $cachedFont['C'][$char] = intval($m[0] / ($head['unitsPerEm'] / 1000));
  1835. if($this->isUnicode){
  1836. if($char >= 0){
  1837. if ($char >= 0 && $char < 0xFFFF && $glyphIndex) {
  1838. $cidtogid[$char*2] = chr($glyphIndex >> 8);
  1839. $cidtogid[$char*2 + 1] = chr($glyphIndex & 0xFF);
  1840. }
  1841. }
  1842. }
  1843. }
  1844. }
  1845. } else {
  1846. $this->debug('openFont: font file does not contain format 4 cmap', E_USER_WARNING);
  1847. }
  1848. $cachedFont['CIDtoGID'] = base64_encode($cidtogid);
  1849. } else if(file_exists($fullFontPath.'.afm')){
  1850. // use the core font program
  1851. $cachedFont = array('isUnicode' => false );
  1852. $file = file($fullFontPath.'.afm');
  1853. foreach ($file as $row) {
  1854. $row=trim($row);
  1855. $pos=strpos($row,' ');
  1856. if ($pos) {
  1857. // then there must be some keyword
  1858. $key = substr($row,0,$pos);
  1859. switch ($key) {
  1860. case 'FontName':
  1861. case 'FullName':
  1862. case 'FamilyName':
  1863. case 'Weight':
  1864. case 'ItalicAngle':
  1865. case 'IsFixedPitch':
  1866. case 'CharacterSet':
  1867. case 'UnderlinePosition':
  1868. case 'UnderlineThickness':
  1869. case 'Version':
  1870. case 'EncodingScheme':
  1871. case 'CapHeight':
  1872. case 'XHeight':
  1873. case 'Ascender':
  1874. case 'Descender':
  1875. case 'StdHW':
  1876. case 'StdVW':
  1877. case 'StartCharMetrics':
  1878. $cachedFont[$key]=trim(substr($row,$pos));
  1879. break;
  1880. case 'FontBBox':
  1881. $cachedFont[$key]=explode(' ',trim(substr($row,$pos)));
  1882. break;
  1883. case 'C':
  1884. // C 39 ; WX 222 ; N quoteright ; B 53 463 157 718 ;
  1885. // use preg_match instead to improve performace
  1886. // IMPORTANT: if "L i fi ; L l fl ;" is required preg_match must be amended
  1887. $r = preg_match('/C (-?\d+) ; WX (-?\d+) ; N (\w+) ; B (-?\d+) (-?\d+) (-?\d+) (-?\d+) ;/', $row, $m);
  1888. if($r == 1){
  1889. //$dtmp = array('C'=> $m[1],'WX'=> $m[2], 'N' => $m[3], 'B' => array($m[4], $m[5], $m[6], $m[7]));
  1890. $c = (int)$m[1];
  1891. $n = $m[3];
  1892. $width = floatval($m[2]);
  1893. if($c >= 0){
  1894. if ($c != hexdec($n)) {
  1895. $cachedFont['codeToName'][$c] = $n;
  1896. }
  1897. $cachedFont['C'][$c] = $width;
  1898. $cachedFont['C'][$n] = $width;
  1899. }else{
  1900. $cachedFont['C'][$n] = $width;
  1901. }
  1902. if (!isset($cachedFont['MissingWidth']) && $c == -1 && $n === '.notdef') {
  1903. $cachedFont['MissingWidth'] = $width;
  1904. }
  1905. }
  1906. break;
  1907. }
  1908. }
  1909. }
  1910. } else {
  1911. $this->debug(sprintf('openFont: no font file found for "%s" IsUnicode: %b', $font, $this->isUnicode), E_USER_ERROR);
  1912. return false;
  1913. }
  1914. $cachedFont['_version_']=3;
  1915. // store the data in as cached file and in $this->fonts array
  1916. $this->fonts[$font]=$cachedFont;
  1917. $fp = fopen($this->tempPath.'/'.$cachedFile,'w'); // use the temp folder to write cached font data
  1918. fwrite($fp,'<?php /* R&OS php pdf class font cache file */ return '.var_export($cachedFont,true).'; ?>');
  1919. fclose($fp);
  1920. return true;
  1921. }
  1922. /**
  1923. * if the font is not loaded then load it and make the required object
  1924. * else just make it the current font
  1925. * the encoding array can contain 'encoding'=> 'none','WinAnsiEncoding','MacRomanEncoding' or 'MacExpertEncoding'
  1926. * note that encoding='none' will need to be used for symbolic fonts
  1927. * and 'differences' => an array of mappings between numbers 0->255 and character names.
  1928. *
  1929. * @param string $fontName Name of the font incl. path
  1930. * @param string $encoding Which encoding to use
  1931. * @param integer $set What is this
  1932. *
  1933. * @return void
  1934. */
  1935. public function selectFont($fontName, $encoding = '', $set = 1, $subsetFont = false)
  1936. {
  1937. if($subsetFont && !class_exists('TTFsubset')){
  1938. $this->debug("TTFsubset class not found. Falling back to complete font program", E_USER_WARNING);
  1939. $subsetFont = false;
  1940. }
  1941. // old font selection containing full path
  1942. $pos = strrpos($fontName, '/');
  1943. if ($pos !== false) {
  1944. $fontName = substr($fontName, $pos + 1);
  1945. }
  1946. // file extension found
  1947. $pos = strrpos($fontName, '.');
  1948. if($pos){
  1949. $ext = substr($fontName, $pos + 1);
  1950. $fontName = substr($fontName, 0, $pos);
  1951. } else {
  1952. // default extension is ttf
  1953. $ext = 'ttf';
  1954. }
  1955. if (!isset($this->fonts[$fontName])){
  1956. // check and load the font file, on no errors $ok = true
  1957. $ok = $this->openFont($fontName);
  1958. if(!$ok){
  1959. $fontName = 'Helvetica';
  1960. if(!isset($this->fonts[$fontName])){
  1961. $this->debug("Error while loading coreFont - check \$pdf->fontPath and/or define one coreFont as fallback", E_USER_ERROR);
  1962. die;
  1963. }
  1964. } else if(isset($this->fonts[$fontName])) {
  1965. $this->numObj++;
  1966. $this->numFonts++;
  1967. $font = &$this->fonts[$fontName];
  1968. $options = array('name' => $fontName, 'fontFileName'=>$fontName); // orgFontName is necessary when font subsetting is used
  1969. if (is_array($encoding)){
  1970. // then encoding and differences might be set
  1971. if (isset($encoding['encoding'])){
  1972. $options['encoding'] = $encoding['encoding'];
  1973. }
  1974. if (isset($encoding['differences'])){
  1975. $options['differences'] = $encoding['differences'];
  1976. }
  1977. } else if (strlen($encoding)){
  1978. // then perhaps only the encoding has been set
  1979. $options['encoding'] = $encoding;
  1980. }
  1981. $fontObj = $this->numObj;
  1982. $this->o_font($fontObj, 'new', $options);
  1983. $font['fontNum'] = $this->numFonts;
  1984. // if this is a '.afm' font, and there is a '.pfa' file to go with it (as there
  1985. // should be for all non-basic fonts), then load it into an object and put the
  1986. // references into the font object
  1987. $fbtype = '';
  1988. if (file_exists($this->fontPath.'/'.$fontName.'.pfb')){
  1989. $fbtype = 'pfb';
  1990. } else if (file_exists($this->fontPath.'/'.$fontName.'.ttf')){
  1991. $fbtype = 'ttf';
  1992. }
  1993. if ($fbtype){
  1994. $adobeFontName = $font['FontName'];
  1995. $this->debug('selectFont: adding font "'.$fontName.'" to pdf');
  1996. // find the array of fond widths, and put that into an object.
  1997. $firstChar = -1;
  1998. $lastChar = 0;
  1999. $widths = array();
  2000. $cid_widths = array();
  2001. foreach ($font['C'] as $num => $d){
  2002. if (intval($num) > 0 || $num == '0'){
  2003. if(!$font['isUnicode']){
  2004. if ($lastChar > 0 && $num > $lastChar + 1){
  2005. for($i = $lastChar + 1; $i < $num; $i++){
  2006. $widths[] = 0;
  2007. }
  2008. }
  2009. }
  2010. $widths[] = $d;
  2011. if ($font['isUnicode']) {
  2012. $cid_widths[$num] = $d;
  2013. }
  2014. if ($firstChar == -1){
  2015. $firstChar = $num;
  2016. }
  2017. $lastChar = $num;
  2018. }
  2019. }
  2020. // also need to adjust the widths for the differences array
  2021. if (isset($options['differences'])){
  2022. foreach ($options['differences'] as $charNum => $charName){
  2023. if ($charNum>$lastChar){
  2024. for($i = $lastChar + 1; $i <= $charNum; $i++) {
  2025. $widths[]=0;
  2026. }
  2027. $lastChar = $charNum;
  2028. }
  2029. if (isset($font['C'][$charName])){
  2030. $widths[$charNum-$firstChar]=$font['C'][$charName];
  2031. if($font['isUnicode']){
  2032. $cid_widths[$charName] = $font['C'][$charName];
  2033. }
  2034. }
  2035. }
  2036. }
  2037. if($font['isUnicode']){
  2038. $font['CIDWidths'] = $cid_widths;
  2039. }
  2040. $this->debug('selectFont: FirstChar='.$firstChar);
  2041. $this->debug('selectFont: LastChar='.$lastChar);
  2042. $widthid = -1;
  2043. if(!$font['isUnicode']){
  2044. $this->numObj++;
  2045. $this->o_contents($this->numObj, 'new', 'raw');
  2046. $this->objects[$this->numObj]['c'].='['.implode(' ', $widths).']';
  2047. $widthid = $this->numObj;
  2048. }
  2049. $missing_width = 500;
  2050. $stemV = 70;
  2051. if (isset($font['MissingWidth'])) {
  2052. $missing_width = $font['MissingWidth'];
  2053. }
  2054. if (isset($font['StdVW'])) {
  2055. $stemV = $font['StdVW'];
  2056. } else if (isset($font['Weight']) && preg_match('!(bold|black)!i', $font['Weight'])) {
  2057. $stemV = 120;
  2058. }
  2059. // create the font descriptor
  2060. $fontDescriptorId = ++$this->numObj;
  2061. // determine flags (more than a little flakey, hopefully will not matter much)
  2062. $flags=0;
  2063. if ($font['ItalicAngle']!=0){
  2064. $flags+=pow(2,6);
  2065. }
  2066. if ($font['IsFixedPitch']=='true'){
  2067. $flags+=1;
  2068. }
  2069. $flags+=pow(2,5); // assume non-sybolic
  2070. $list = array('Ascent'=>'Ascender','CapHeight'=>'CapHeight','Descent'=>'Descender','FontBBox'=>'FontBBox','ItalicAngle'=>'ItalicAngle');
  2071. $fdopt = array(
  2072. 'Flags' => $flags,
  2073. 'FontName' => $adobeFontName,
  2074. 'StemV' => $stemV
  2075. );
  2076. foreach($list as $k=>$v){
  2077. if (isset($font[$v])){
  2078. $fdopt[$k]=$font[$v];
  2079. }
  2080. }
  2081. // setup the basic properties for o_font output
  2082. $tmp = array('BaseFont'=>$adobeFontName,'Widths'=>$widthid
  2083. ,'FirstChar'=>$firstChar,'LastChar'=>$lastChar
  2084. ,'FontDescriptor'=>$fontDescriptorId);
  2085. // binary content of pfb or ttf file
  2086. $pfbid = ++$this->numObj;
  2087. // embed the font program
  2088. // to allow font subsets embedding fonts is proceed in o_font 'output'
  2089. if($this->embedFont){
  2090. if ($fbtype=='pfb'){
  2091. $fdopt['FontFile']=$pfbid;
  2092. } else if ($fbtype=='ttf'){
  2093. $fdopt['FontFile2']=$pfbid;
  2094. $tmp['SubType']='TrueType'; // Declare basic font as TrueType
  2095. }
  2096. $this->o_fontDescriptor($fontDescriptorId,'new',$fdopt);
  2097. $this->o_contents($pfbid,'new');
  2098. }
  2099. $this->debug('selectFont: adding extra info to font.('.$fontObj.')');
  2100. foreach($tmp as $fk=>$fv){
  2101. $this->debug($fk." : ".$fv);
  2102. }
  2103. $this->o_font($fontObj,'add',$tmp);
  2104. } else if(!in_array(strtolower($fontName), $this->coreFonts)) {
  2105. $this->debug('selectFont: No pfb/ttf file found for "'.$fontName.'"', E_USER_WARNING);
  2106. }
  2107. // also set the differences here, note that this means that these will take effect only the
  2108. // first time that a font is selected, else they are ignored
  2109. if (isset($options['differences'])){
  2110. $font['differences']=$options['differences'];
  2111. }
  2112. }
  2113. }
  2114. $this->fonts[$fontName]['isSubset'] = $subsetFont;
  2115. if(!isset($this->fonts[$fontName]['subset'])) {
  2116. $this->fonts[$fontName]['subset'] = array();
  2117. }
  2118. if ($set && isset($this->fonts[$fontName])){
  2119. // so if for some reason the font was not set in the last one then it will not be selected
  2120. $this->currentBaseFont=$fontName;
  2121. // the next line means that if a new font is selected, then the current text state will be
  2122. // applied to it as well.
  2123. $this->setCurrentFont();
  2124. }
  2125. //return $this->currentFontNum;
  2126. }
  2127. /**
  2128. * sets up the current font, based on the font families, and the current text state
  2129. * note that this system is quite flexible, a <b><i> font can be completely different to a
  2130. * <i><b> font, and even <b><b> will have to be defined within the family to have meaning
  2131. * This function is to be called whenever the currentTextState is changed, it will update
  2132. * the currentFont setting to whatever the appropriatte family one is.
  2133. * If the user calls selectFont themselves then that will reset the currentBaseFont, and the currentFont
  2134. * This function will change the currentFont to whatever it should be, but will not change the
  2135. * currentBaseFont.
  2136. *
  2137. */
  2138. protected function setCurrentFont(){
  2139. if (strlen($this->currentBaseFont)==0){
  2140. // then assume an initial font
  2141. $this->selectFont('Helvetica');
  2142. }
  2143. $cf = $this->currentBaseFont;
  2144. if (strlen($this->currentTextState)
  2145. && isset($this->fontFamilies[$cf])
  2146. && isset($this->fontFamilies[$cf][$this->currentTextState])){
  2147. // then we are in some state or another
  2148. // and this font has a family, and the current setting exists within it
  2149. // select the font, then return it
  2150. $nf = $this->fontFamilies[$cf][$this->currentTextState];
  2151. // PATCH #14 - subset file fix when using font family | thank you johannes
  2152. $isSubset = false;
  2153. if (isset($this->fonts[$this->currentBaseFont]['isSubset'])) $isSubset = $this->fonts[$this->currentBaseFont]['isSubset'];
  2154. $this->selectFont($nf,'', 0, $isSubset);
  2155. $this->currentFont = $nf;
  2156. $this->currentFontNum = $this->fonts[$nf]['fontNum'];
  2157. } else {
  2158. // the this font must not have the right family member for the current state
  2159. // simply assume the base font
  2160. $this->currentFont = $cf;
  2161. $this->currentFontNum = $this->fonts[$cf]['fontNum'];
  2162. }
  2163. }
  2164. /**
  2165. * get the current font name being used
  2166. * @param bool withStyle force to receive the style font name, instead of the base font
  2167. * @since 0.12-rc12
  2168. * @return string current font name
  2169. */
  2170. public function getCurrentFont($withStyle = false){
  2171. if($withStyle){
  2172. return $this->currentFont;
  2173. }
  2174. return $this->currentBaseFont;
  2175. }
  2176. /**
  2177. * function for the user to find out what the ID is of the first page that was created during
  2178. * startup - useful if they wish to add something to it later.
  2179. */
  2180. protected function getFirstPageId(){
  2181. return $this->firstPageId;
  2182. }
  2183. /**
  2184. * add content to the currently active object
  2185. */
  2186. protected function addContent($content){
  2187. $this->objects[$this->currentContents]['c'].=$content;
  2188. }
  2189. /**
  2190. * sets the colour for fill operations
  2191. */
  2192. public function setColor($r,$g,$b,$force=0){
  2193. if ($r>=0 && ($force || $r!=$this->currentColour['r'] || $g!=$this->currentColour['g'] || $b!=$this->currentColour['b'])){
  2194. $this->objects[$this->currentContents]['c'].="\n".sprintf('%.3F',$r).' '.sprintf('%.3F',$g).' '.sprintf('%.3F',$b).' rg';
  2195. $this->currentColour=array('r'=>$r,'g'=>$g,'b'=>$b);
  2196. }
  2197. }
  2198. /**
  2199. * sets the colour for stroke operations
  2200. */
  2201. public function setStrokeColor($r,$g,$b,$force=0){
  2202. if ($r>=0 && ($force || $r!=$this->currentStrokeColour['r'] || $g!=$this->currentStrokeColour['g'] || $b!=$this->currentStrokeColour['b'])){
  2203. $this->objects[$this->currentContents]['c'].="\n".sprintf('%.3F',$r).' '.sprintf('%.3F',$g).' '.sprintf('%.3F',$b).' RG';
  2204. $this->currentStrokeColour=array('r'=>$r,'g'=>$g,'b'=>$b);
  2205. }
  2206. }
  2207. /**
  2208. * draw a line from one set of coordinates to another
  2209. */
  2210. public function line($x1,$y1,$x2,$y2){
  2211. $this->objects[$this->currentContents]['c'].="\n".sprintf('%.3F',$x1).' '.sprintf('%.3F',$y1).' m '.sprintf('%.3F',$x2).' '.sprintf('%.3F',$y2).' l S';
  2212. }
  2213. /**
  2214. * draw a bezier curve based on 4 control points
  2215. */
  2216. public function curve($x0,$y0,$x1,$y1,$x2,$y2,$x3,$y3){
  2217. // in the current line style, draw a bezier curve from (x0,y0) to (x3,y3) using the other two points
  2218. // as the control points for the curve.
  2219. $this->objects[$this->currentContents]['c'].="\n".sprintf('%.3F',$x0).' '.sprintf('%.3F',$y0).' m '.sprintf('%.3F',$x1).' '.sprintf('%.3F',$y1);
  2220. $this->objects[$this->currentContents]['c'].= ' '.sprintf('%.3F',$x2).' '.sprintf('%.3F',$y2).' '.sprintf('%.3F',$x3).' '.sprintf('%.3F',$y3).' c S';
  2221. }
  2222. /**
  2223. * draw a part of an ellipse
  2224. */
  2225. public function partEllipse($x0,$y0,$astart,$afinish,$r1,$r2=0,$angle=0,$nSeg=8){
  2226. $this->ellipse($x0,$y0,$r1,$r2,$angle,$nSeg,$astart,$afinish,0);
  2227. }
  2228. /**
  2229. * draw a filled ellipse
  2230. */
  2231. public function filledEllipse($x0,$y0,$r1,$r2=0,$angle=0,$nSeg=8,$astart=0,$afinish=360){
  2232. return $this->ellipse($x0,$y0,$r1,$r2=0,$angle,$nSeg,$astart,$afinish,1,1);
  2233. }
  2234. /**
  2235. * draw an ellipse
  2236. * note that the part and filled ellipse are just special cases of this function
  2237. *
  2238. * draws an ellipse in the current line style
  2239. * centered at $x0,$y0, radii $r1,$r2
  2240. * if $r2 is not set, then a circle is drawn
  2241. * nSeg is not allowed to be less than 2, as this will simply draw a line (and will even draw a
  2242. * pretty crappy shape at 2, as we are approximating with bezier curves.
  2243. */
  2244. public function ellipse($x0,$y0,$r1,$r2=0,$angle=0,$nSeg=8,$astart=0,$afinish=360,$close=1,$fill=0){
  2245. if ($r1==0){
  2246. return;
  2247. }
  2248. if ($r2==0){
  2249. $r2=$r1;
  2250. }
  2251. if ($nSeg<2){
  2252. $nSeg=2;
  2253. }
  2254. $astart = deg2rad((float)$astart);
  2255. $afinish = deg2rad((float)$afinish);
  2256. $totalAngle =$afinish-$astart;
  2257. $dt = $totalAngle/$nSeg;
  2258. $dtm = $dt/3;
  2259. if ($angle != 0){
  2260. $a = -1*deg2rad((float)$angle);
  2261. $tmp = "\n q ";
  2262. $tmp .= sprintf('%.3F',cos($a)).' '.sprintf('%.3F',(-1.0*sin($a))).' '.sprintf('%.3F',sin($a)).' '.sprintf('%.3F',cos($a)).' ';
  2263. $tmp .= sprintf('%.3F',$x0).' '.sprintf('%.3F',$y0).' cm';
  2264. $this->objects[$this->currentContents]['c'].= $tmp;
  2265. $x0=0;
  2266. $y0=0;
  2267. }
  2268. $t1 = $astart;
  2269. $a0 = $x0+$r1*cos($t1);
  2270. $b0 = $y0+$r2*sin($t1);
  2271. $c0 = -$r1*sin($t1);
  2272. $d0 = $r2*cos($t1);
  2273. $this->objects[$this->currentContents]['c'].="\n".sprintf('%.3F',$a0).' '.sprintf('%.3F',$b0).' m ';
  2274. for ($i=1;$i<=$nSeg;$i++){
  2275. // draw this bit of the total curve
  2276. $t1 = $i*$dt+$astart;
  2277. $a1 = $x0+$r1*cos($t1);
  2278. $b1 = $y0+$r2*sin($t1);
  2279. $c1 = -$r1*sin($t1);
  2280. $d1 = $r2*cos($t1);
  2281. $this->objects[$this->currentContents]['c'].="\n".sprintf('%.3F',($a0+$c0*$dtm)).' '.sprintf('%.3F',($b0+$d0*$dtm));
  2282. $this->objects[$this->currentContents]['c'].= ' '.sprintf('%.3F',($a1-$c1*$dtm)).' '.sprintf('%.3F',($b1-$d1*$dtm)).' '.sprintf('%.3F',$a1).' '.sprintf('%.3F',$b1).' c';
  2283. $a0=$a1;
  2284. $b0=$b1;
  2285. $c0=$c1;
  2286. $d0=$d1;
  2287. }
  2288. if ($fill){
  2289. $this->objects[$this->currentContents]['c'].=' f';
  2290. } else {
  2291. if ($close){
  2292. $this->objects[$this->currentContents]['c'].=' s'; // small 's' signifies closing the path as well
  2293. } else {
  2294. $this->objects[$this->currentContents]['c'].=' S';
  2295. }
  2296. }
  2297. if ($angle !=0){
  2298. $this->objects[$this->currentContents]['c'].=' Q';
  2299. }
  2300. }
  2301. /**
  2302. * this sets the line drawing style.
  2303. * width, is the thickness of the line in user units
  2304. * cap is the type of cap to put on the line, values can be 'butt','round','square'
  2305. * where the diffference between 'square' and 'butt' is that 'square' projects a flat end past the
  2306. * end of the line.
  2307. * join can be 'miter', 'round', 'bevel'
  2308. * dash is an array which sets the dash pattern, is a series of length values, which are the lengths of the
  2309. * on and off dashes.
  2310. * (2) represents 2 on, 2 off, 2 on , 2 off ...
  2311. * (2,1) is 2 on, 1 off, 2 on, 1 off.. etc
  2312. * phase is a modifier on the dash pattern which is used to shift the point at which the pattern starts.
  2313. */
  2314. public function setLineStyle($width=1,$cap='',$join='',$dash='',$phase=0){
  2315. // this is quite inefficient in that it sets all the parameters whenever 1 is changed, but will fix another day
  2316. $string = '';
  2317. if ($width>0){
  2318. $string.= $width.' w';
  2319. }
  2320. $ca = array('butt'=>0,'round'=>1,'square'=>2);
  2321. if (isset($ca[$cap])){
  2322. $string.= ' '.$ca[$cap].' J';
  2323. }
  2324. $ja = array('miter'=>0,'round'=>1,'bevel'=>2);
  2325. if (isset($ja[$join])){
  2326. $string.= ' '.$ja[$join].' j';
  2327. }
  2328. if (is_array($dash)){
  2329. $string.= ' [';
  2330. foreach ($dash as $len){
  2331. $string.=' '.$len;
  2332. }
  2333. $string.= ' ] '.$phase.' d';
  2334. }
  2335. $this->currentLineStyle = $string;
  2336. $this->objects[$this->currentContents]['c'].="\n".$string;
  2337. }
  2338. /**
  2339. * draw a polygon, the syntax for this is similar to the GD polygon command
  2340. */
  2341. public function polygon($p,$np,$f=0){
  2342. $this->objects[$this->currentContents]['c'].="\n";
  2343. $this->objects[$this->currentContents]['c'].=sprintf('%.3F',$p[0]).' '.sprintf('%.3F',$p[1]).' m ';
  2344. for ($i=2;$i<$np*2;$i=$i+2){
  2345. $this->objects[$this->currentContents]['c'].= sprintf('%.3F',$p[$i]).' '.sprintf('%.3F',$p[$i+1]).' l ';
  2346. }
  2347. if ($f==1){
  2348. $this->objects[$this->currentContents]['c'].=' f';
  2349. } else {
  2350. $this->objects[$this->currentContents]['c'].=' S';
  2351. }
  2352. }
  2353. /**
  2354. * a filled rectangle, note that it is the width and height of the rectangle which are the secondary paramaters, not
  2355. * the coordinates of the upper-right corner
  2356. */
  2357. public function filledRectangle($x1,$y1,$width,$height){
  2358. $this->objects[$this->currentContents]['c'].="\n".sprintf('%.3F',$x1).' '.sprintf('%.3F',$y1).' '.sprintf('%.3F',$width).' '.sprintf('%.3F',$height).' re f';
  2359. }
  2360. /**
  2361. * draw a rectangle, note that it is the width and height of the rectangle which are the secondary paramaters, not
  2362. * the coordinates of the upper-right corner
  2363. */
  2364. public function rectangle($x1,$y1,$width,$height){
  2365. $this->objects[$this->currentContents]['c'].="\n".sprintf('%.3F',$x1).' '.sprintf('%.3F',$y1).' '.sprintf('%.3F',$width).' '.sprintf('%.3F',$height).' re S';
  2366. }
  2367. /**
  2368. * add a new page to the document
  2369. * this also makes the new page the current active object
  2370. */
  2371. public function newPage($insert=0,$id=0,$pos='after'){
  2372. // if there is a state saved, then go up the stack closing them
  2373. // then on the new page, re-open them with the right setings
  2374. if ($this->nStateStack){
  2375. for ($i=$this->nStateStack;$i>=1;$i--){
  2376. $this->restoreState($i);
  2377. }
  2378. }
  2379. $this->numObj++;
  2380. if ($insert){
  2381. // the id from the ezPdf class is the od of the contents of the page, not the page object itself
  2382. // query that object to find the parent
  2383. $rid = $this->objects[$id]['onPage'];
  2384. $opt= array('rid'=>$rid,'pos'=>$pos);
  2385. $this->o_page($this->numObj,'new',$opt);
  2386. } else {
  2387. $this->o_page($this->numObj,'new');
  2388. }
  2389. // if there is a stack saved, then put that onto the page
  2390. if ($this->nStateStack){
  2391. for ($i=1;$i<=$this->nStateStack;$i++){
  2392. $this->saveState($i);
  2393. }
  2394. }
  2395. // and if there has been a stroke or fill colour set, then transfer them
  2396. if ($this->currentColour['r']>=0){
  2397. $this->setColor($this->currentColour['r'],$this->currentColour['g'],$this->currentColour['b'],1);
  2398. }
  2399. if ($this->currentStrokeColour['r']>=0){
  2400. $this->setStrokeColor($this->currentStrokeColour['r'],$this->currentStrokeColour['g'],$this->currentStrokeColour['b'],1);
  2401. }
  2402. // if there is a line style set, then put this in too
  2403. if (strlen($this->currentLineStyle)){
  2404. $this->objects[$this->currentContents]['c'].="\n".$this->currentLineStyle;
  2405. }
  2406. // the call to the o_page object set currentContents to the present page, so this can be returned as the page id
  2407. return $this->currentContents;
  2408. }
  2409. /**
  2410. * output the pdf code, streaming it to the browser
  2411. * the relevant headers are set so that hopefully the browser will recognise it
  2412. * this method is protected to force user to use ezStream method from Cezpdf.php
  2413. */
  2414. protected function stream($options=''){
  2415. // setting the options allows the adjustment of the headers
  2416. // values at the moment are:
  2417. // 'Content-Disposition'=>'filename' - sets the filename, though not too sure how well this will
  2418. // work as in my trial the browser seems to use the filename of the php file with .pdf on the end
  2419. // 'Accept-Ranges'=>1 or 0 - if this is not set to 1, then this header is not included, off by default
  2420. // this header seems to have caused some problems despite tha fact that it is supposed to solve
  2421. // them, so I am leaving it off by default.
  2422. // 'compress'=> 1 or 0 - apply content stream compression, this is on (1) by default
  2423. // 'download'=> 1 or 0 - provide download dialog
  2424. if (!is_array($options)){
  2425. $options=array();
  2426. }
  2427. if ( isset($options['compress']) && $options['compress']==0){
  2428. $tmp = $this->output(1);
  2429. } else {
  2430. $tmp = $this->output();
  2431. }
  2432. header("Content-Type: application/pdf");
  2433. header("Content-Length: ".strlen(ltrim($tmp)));
  2434. $fileName = (isset($options['Content-Disposition'])?$options['Content-Disposition']:'file.pdf');
  2435. if(isset($options['download']) && $options['download'] == 1)
  2436. $attached = 'attachment';
  2437. else
  2438. $attached = 'inline';
  2439. header("Content-Disposition: $attached; filename=".$fileName);
  2440. if (isset($options['Accept-Ranges']) && $options['Accept-Ranges']==1){
  2441. header("Accept-Ranges: ".strlen(ltrim($tmp)));
  2442. }
  2443. echo ltrim($tmp);
  2444. }
  2445. /**
  2446. * return the height in units of the current font in the given size
  2447. */
  2448. public function getFontHeight($size){
  2449. if (!$this->numFonts){
  2450. $this->selectFont('./fonts/Helvetica');
  2451. }
  2452. $font = &$this->fonts[$this->currentFont];
  2453. // for the current font, and the given size, what is the height of the font in user units
  2454. $h = $font['FontBBox'][3] - $font['FontBBox'][1];
  2455. return $size*$h/1000;
  2456. }
  2457. /**
  2458. * return the font decender, this will normally return a negative number
  2459. * if you add this number to the baseline, you get the level of the bottom of the font
  2460. * it is in the pdf user units
  2461. */
  2462. public function getFontDecender($size){
  2463. // note that this will most likely return a negative value
  2464. if (!$this->numFonts){
  2465. $this->selectFont('./fonts/Helvetica');
  2466. }
  2467. $h = $this->fonts[$this->currentFont]['Descender'];
  2468. return $size*$h/1000;
  2469. }
  2470. /**
  2471. * filter the text, this is applied to all text just before being inserted into the pdf document
  2472. * it escapes the various things that need to be escaped, and so on
  2473. *
  2474. */
  2475. protected function filterText($text, $bom = true, $convert_encoding = true){
  2476. $cf = $this->currentFont;
  2477. if ($convert_encoding &&isset($this->fonts[$cf]) && $this->fonts[$cf]['isUnicode']) {
  2478. $text = mb_convert_encoding($text, 'UTF-16BE','UTF-8');
  2479. // store all used characters if subset font is set to true
  2480. if($this->fonts[$cf]['isSubset']){
  2481. for($i = 0; $i < mb_strlen($text,'UTF-16BE'); $i++)
  2482. $this->fonts[$cf]['subset'][mb_substr($text,$i, 1, 'UTF-16BE')] = true;
  2483. }
  2484. } else if(!$this->fonts[$cf]['isUnicode']) {
  2485. $text = mb_convert_encoding($text, $this->targetEncoding);
  2486. // store all used characters if subset font is set to true
  2487. if($this->fonts[$cf]['isSubset']){
  2488. for($i = 0; $i < strlen($text); $i++)
  2489. $this->fonts[$cf]['subset'][$text[$i]] = true;
  2490. }
  2491. }
  2492. $text = strtr($text, array(')' => '\\)', '(' => '\\(', '\\' => '\\\\', chr(8) => '\\b', chr(9) => '\\t', chr(10) => '\\n', chr(12) => '\\f' ,chr(13) => '\\r', '&lt;'=>'<', '&gt;'=>'>', '&amp;'=>'&') );
  2493. if($this->rtl){
  2494. $text = strrev($text);
  2495. }
  2496. return $text;
  2497. }
  2498. private function getDirectives(&$text, &$x, &$y, $width = 0, $size = 0, $justification = 'left',$angle = 0, &$wordSpaceAdjust = 0, $noCB = false){
  2499. $regex = "/<\/?([cC]:|)(".$this->allowedTags.")\>/";
  2500. $cb = array();
  2501. $r = preg_match_all($regex, $text, $regs, PREG_OFFSET_CAPTURE);
  2502. if($r){
  2503. // backup current font style
  2504. $store_currentTextState = $this->currentTextState;
  2505. $nx = $x;
  2506. $ny = $y;
  2507. if($this->nCallback > 0){
  2508. $cb[0] = $this->callback[1];
  2509. $cb[0]['x'] = $x;
  2510. $cb[0]['y'] = $y;
  2511. $cb[0]['startTag'] = 0;
  2512. $cb[0]['endTag'] = 0;
  2513. }
  2514. $restWidth = $width;
  2515. reset($regs[0]);
  2516. $prevEndTagIndex = 0;
  2517. while(list($k,$curTag) = each($regs[0])){
  2518. $curTagIndex = mb_strlen(substr($text, 0, $curTag[1]), 'UTF-8');
  2519. $endTagIndex = $curTagIndex + strlen($curTag[0]);
  2520. $tmpstr = mb_substr($text, $prevEndTagIndex, $curTagIndex - $prevEndTagIndex, 'UTF-8');
  2521. $tmpstr = $this->filterText($tmpstr, false, false);
  2522. $tmp = $this->getTextLength($size, $tmpstr, $restWidth, $angle, $wordSpaceAdjust);
  2523. // if the text does not fit to $width, $tmp[2] contains the length to break the line
  2524. if($tmp[2] > 0){
  2525. // position where the line break occurs
  2526. $lbpos = $prevEndTagIndex + $tmp[2];
  2527. // adjust to position if justification is set
  2528. if($nx > $x){
  2529. $tmpw = ($width - $restWidth) + $tmp[0];
  2530. // force word wrap when something already written
  2531. $lbpos -= $tmp[2];
  2532. if($prevEndTagIndex == $lbpos){
  2533. $lastCallback = array_pop($cb);
  2534. if(isset($lastCallback)){
  2535. $lbpos -= $lastCallback['endTag'] - $lastCallback['startTag'];
  2536. $tmp[0] = 0;
  2537. if(isset($lastCallback['nCallback']))
  2538. $this->nCallback--;
  2539. }
  2540. }
  2541. } else{
  2542. $tmpw = $tmp[0];
  2543. }
  2544. $this->adjustWrapText($tmpstr, $tmpw, $width, $x, $wordSpaceAdjust, $justification);
  2545. // set position array by using the current break position minus offset
  2546. $cb[$lbpos] = array('x'=> ($nx + $tmp[0]), 'y'=> $ny + $tmp[1], 'f'=>'linebreak', 'p' => $tmp[3], 'width'=>$tmp[0]);
  2547. // restore previous stored font style
  2548. $this->currentTextState = $store_currentTextState;
  2549. $this->setCurrentFont();
  2550. return $cb;
  2551. }
  2552. $prevEndTagIndex = $endTagIndex;
  2553. $restWidth -= $tmp[0];
  2554. $nx += $tmp[0];
  2555. $ny += $tmp[1];
  2556. if(!empty($regs[1][$k][0])){
  2557. // these are custom callbacks (with parameters)
  2558. $pos = strpos($regs[2][$k][0], ':');
  2559. if($pos){
  2560. $func = substr($regs[2][$k][0], 0, $pos);
  2561. $parm = substr($regs[2][$k][0], $pos + 1);
  2562. } else {
  2563. $func = $regs[2][$k][0];
  2564. $parm = '';
  2565. }
  2566. // adjust the coordinates if justification is set
  2567. // end tag for custom callbacks
  2568. if(substr($curTag[0], 0, 2) == "</"){
  2569. $cb[$curTagIndex] = array('x'=> $nx, 'y'=>$ny,'angle'=>$angle,'status'=>'end', 'f'=>$func, 'p'=>$parm,'nCallback'=>$this->nCallback, 'startTag' => $curTagIndex, 'endTag' => $endTagIndex );
  2570. if(!$noCB){
  2571. $this->nCallback--;
  2572. if ($this->nCallback<0){
  2573. $this->nCallback=0;
  2574. }
  2575. }
  2576. } else {
  2577. $noClose = ($regs[1][$k][0] == 'C:')? true: false;
  2578. $cb[$curTagIndex] = array('x'=> $nx, 'y'=>$ny, 'angle'=>$angle,'status'=>'start','p'=>$parm,'f'=>$func,'height'=>$this->getFontHeight($size),'descender'=>$this->getFontDecender($size),
  2579. 'startTag' => $curTagIndex, 'endTag' => $endTagIndex , 'noClose' => $noClose);
  2580. if(!$noCB){
  2581. if(!$noClose){
  2582. $this->nCallback++;
  2583. $cb[$curTagIndex]['nCallback']=$this->nCallback;
  2584. $this->callback[$this->nCallback]=$cb[$curTagIndex];
  2585. }
  2586. }
  2587. }
  2588. } else {
  2589. $parm = $regs[2][$k][0];
  2590. if(substr($curTag[0] ,0 , 2) == "</"){
  2591. $cb[$curTagIndex] = array('x'=> $nx, 'y'=>$ny, 'angle'=>$angle,'status'=>'end', 'p'=>$parm,'f'=>'defaultFormatting', 'startTag' => $curTagIndex, 'endTag' => $endTagIndex );
  2592. } else {
  2593. $cb[$curTagIndex] = array('x'=> $nx, 'y'=>$ny, 'angle'=>$angle,'status'=>'start','p'=>$parm,'f'=>'defaultFormatting', 'startTag' => $curTagIndex, 'endTag' => $endTagIndex );
  2594. }
  2595. // do a dry formatting to fetch the correct text width
  2596. $this->defaultFormatting($cb[$curTagIndex]);
  2597. $this->setCurrentFont();
  2598. }
  2599. }
  2600. $l = mb_strlen($text, 'UTF-8');
  2601. if($prevEndTagIndex < $l){
  2602. $tmpstr = mb_substr($text, $prevEndTagIndex, $l - $prevEndTagIndex, 'UTF-8');
  2603. $tmp = $this->getTextLength($size, $tmpstr, $restWidth, $angle, $wordSpaceAdjust);
  2604. // if the text does not fit to $width, $tmp[2] contains the length
  2605. if($tmp[2] > 0){
  2606. // restore previous stored font style
  2607. $this->currentTextState = $store_currentTextState;
  2608. $this->setCurrentFont();
  2609. $tmpstr = mb_substr($text, 0, $prevEndTagIndex + $tmp[2], 'UTF-8');
  2610. // adjust to position if justification is set
  2611. $this->adjustWrapText($tmpstr, $width - ($restWidth - $tmp[0]), $width, $x, $wordSpaceAdjust, $justification);
  2612. // set position array by using the current break position minus offset
  2613. $lbpos = $prevEndTagIndex + $tmp[2];
  2614. $cb[$lbpos] = array('x'=> ($nx + $tmp[0]), 'y'=> $ny + $tmp[1], 'f'=>'linebreak', 'p' => $tmp[3], 'width'=>$tmp[0]);
  2615. return $cb;
  2616. } else {
  2617. $restWidth -= $tmp[0];
  2618. }
  2619. }
  2620. // restore previous stored font style
  2621. $this->currentTextState = $store_currentTextState;
  2622. $this->setCurrentFont();
  2623. // adjust to position if justification is set
  2624. $tmpx = $x;
  2625. if($justification != 'full') {
  2626. $this->adjustWrapText($tmpstr, $width - $restWidth, $width, $x, $wordSpaceAdjust, $justification);
  2627. }
  2628. foreach($cb as &$v){
  2629. if($v['status'] == 'start'){
  2630. $v['x'] += $x - $tmpx;
  2631. } else {
  2632. $v['x'] += $x - $tmpx;
  2633. }
  2634. }
  2635. } else {
  2636. $tmp = $this->getTextLength($size, $text, $width, $angle, $wordSpaceAdjust);
  2637. // if the text does not fit to $width, $tmp[2] contains the length
  2638. if($tmp[2] > 0){
  2639. $tmpstr = mb_substr($text, 0, $tmp[2], 'UTF-8');
  2640. // adjust to position if justification is set
  2641. $this->adjustWrapText($tmpstr, $tmp[0], $width, $x, $wordSpaceAdjust, $justification);
  2642. // set position array by using the current break position minus offset
  2643. $lbpos = $tmp[2];
  2644. $cb[$lbpos] = array('x'=> ($x + $tmp[0]), 'y'=> $y + $tmp[1], 'f'=>'linebreak', 'p' => $tmp[3], 'width'=>$tmp[0]);
  2645. return $cb;
  2646. } else if($justification != 'full') {
  2647. $this->adjustWrapText($text, $tmp[0], $width, $x, $wordSpaceAdjust, $justification);
  2648. }
  2649. }
  2650. return $cb;
  2651. }
  2652. protected function defaultFormatting($info){
  2653. $tag = $info['p'];
  2654. switch($tag){
  2655. case 'strong':
  2656. $tag = 'b';
  2657. case 'i':
  2658. case 'b':
  2659. if($info['status'] == 'start'){
  2660. $this->currentTextState.= $tag;
  2661. } else {
  2662. $p = strrpos($this->currentTextState, $tag);
  2663. if ($p !== false){
  2664. // then there is one to remove
  2665. $this->currentTextState = substr($this->currentTextState,0,$p).substr($this->currentTextState,$p+1);
  2666. }
  2667. }
  2668. break;
  2669. }
  2670. }
  2671. /**
  2672. * add text to the document, at a specified location, size and angle on the page
  2673. */
  2674. public function addText($x, $y, $size, $text, $width = 0, $justification = 'left', $angle = 0, $wordSpaceAdjust = 0, $test=0) {
  2675. if($text == "") return '';
  2676. if (!$this->numFonts) {
  2677. $this->selectFont(dirname(__FILE__) . '/fonts/Helvetica');
  2678. }
  2679. // if there are any open callbacks, then they should be called, to show the start of the line
  2680. if ($this->nCallback > 0){
  2681. for ($i = $this->nCallback; $i > 0; $i--){
  2682. // call each function
  2683. $info = array('x'=>$x,'y'=>$y,'angle'=>$angle,'status'=>'sol','p'=>$this->callback[$i]['p'],'nCallback'=>$this->callback[$i]['nCallback'],'height'=>$this->callback[$i]['height'],'decender'=>$this->callback[$i]['decender']);
  2684. $func = $this->callback[$i]['f'];
  2685. $this->$func($info);
  2686. }
  2687. }
  2688. $cf = $this->currentFont;
  2689. // convert ISO-8859-1 into utf8 if necessary
  2690. if(mb_detect_encoding($text) != 'UTF-8'){
  2691. $text=utf8_encode($text);
  2692. }
  2693. // get length of its unicode string
  2694. $len=mb_strlen($text,'UTF-8');
  2695. $directives = $this->getDirectives($text, $x, $y, $width, $size, $justification, $angle, $wordSpaceAdjust);
  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. $this->addContent(' ('.$this->filterText($part, false).') Tj');
  2713. }
  2714. $func = $directive['f'];
  2715. if($func == 'defaultFormatting'){ // default font style, bold, italic, ...
  2716. $this->defaultFormatting($directive);
  2717. $this->setCurrentFont();
  2718. } else if($func == 'linebreak') { // line break
  2719. $this->addContent(' ET');
  2720. if ($this->nCallback > 0) {
  2721. for ($j = $this->nCallback; $j > 0; $j--) {
  2722. $info = array(
  2723. 'x' => $directive['x'],
  2724. 'y' => $directive['y'],
  2725. 'angle' => $angle,
  2726. 'status' => 'eol',
  2727. 'p' => $this->callback[$j]['p'],
  2728. 'nCallback' => $this->callback[$j]['nCallback'],
  2729. 'height' => $this->callback[$j]['height'],
  2730. 'descender' => isset($this->callback[$j]['descender'])?$this->callback[$j]['descender']:null
  2731. );
  2732. $func = $this->callback[$j]['f'];
  2733. $this->$func($info);
  2734. }
  2735. }
  2736. return mb_substr($text,$pos + $directive['p'], $len, 'UTF-8');
  2737. } else { // custom callbacks
  2738. $this->addContent(' ET');
  2739. $this->$func($directive);
  2740. $xp = $directive['x'];
  2741. $yp = $directive['y'];
  2742. // restart the text object
  2743. if ($angle==0){
  2744. $this->addContent("\n".'BT '.sprintf('%.3F',$xp).' '.sprintf('%.3F',$yp).' Td');
  2745. } else {
  2746. $a = deg2rad((float)$angle);
  2747. $tmp = "\n".'BT ';
  2748. $tmp .= sprintf('%.3F',cos($a)).' '.sprintf('%.3F',(-1.0*sin($a))).' '.sprintf('%.3F',sin($a)).' '.sprintf('%.3F',cos($a)).' ';
  2749. $tmp .= sprintf('%.3F',$xp).' '.sprintf('%.3F',$yp).' Tm';
  2750. $this->addContent($tmp);
  2751. }
  2752. // only single-byte character will work with word spacing according to PDF 1.3 reference (Chapter 5.2.2)
  2753. if (!$this->fonts[$cf]['isUnicode'] && ($wordSpaceAdjust!=0 || $wordSpaceAdjust != $this->wordSpaceAdjust)){
  2754. $this->wordSpaceAdjust=$wordSpaceAdjust;
  2755. $this->addContent(' '.sprintf('%.3F',$wordSpaceAdjust).' Tw');
  2756. }
  2757. }
  2758. $start = $directive['endTag'];
  2759. }
  2760. if ($start < $len) {
  2761. $part = mb_substr($text,$start, $len - $start,'UTF-8');
  2762. $place_text = $this->filterText($part, false);
  2763. // according to PDF 1.3 reference (Chapter 5.2.2) word spacing works only for single-byte codes
  2764. // So for multiple-byte codes we have to do it manually
  2765. if ($this->fonts[$cf]['isUnicode'] && $wordSpaceAdjust != 0) {
  2766. $space_scale = 1000 / $size;
  2767. $place_text = str_replace(' ', ' ) '.(-round($space_scale*$wordSpaceAdjust)).' (', $place_text);
  2768. }
  2769. $this->addContent(" /F$this->currentFontNum ".sprintf('%.1F Tf', $size));
  2770. $this->addContent(" (".$place_text.") Tj");
  2771. }
  2772. $this->addContent(' ET');
  2773. // if there are any open callbacks, then they should be called, to show the end of the line
  2774. if ($this->nCallback > 0) {
  2775. for ($i = $this->nCallback; $i > 0; $i--) {
  2776. // call each function
  2777. $tmp = $this->getTextLength($size, $text, $width, $angle, $wordSpaceAdjust);
  2778. //$tmp = $this->getTextPosition($x, $y, $angle, $size, $wordSpaceAdjust, $text);
  2779. $info = array(
  2780. 'x' => $tmp[0] + $x,
  2781. 'y' => $tmp[1] + $y,
  2782. 'angle' => $angle,
  2783. 'status' => 'eol',
  2784. 'p' => $this->callback[$i]['p'],
  2785. 'nCallback' => $this->callback[$i]['nCallback'],
  2786. 'height' => $this->callback[$i]['height'],
  2787. 'descender' => $this->callback[$i]['descender']
  2788. );
  2789. $func = $this->callback[$i]['f'];
  2790. $this->$func($info);
  2791. }
  2792. }
  2793. // return text position of the last line break or 0 (if error -1)
  2794. return '';
  2795. }
  2796. /*
  2797. * unicode version of php ord to get the decimal of an utf-8 character
  2798. */
  2799. private function uniord($c)
  2800. {
  2801. // important condition to allow char "0" (zero) being converted to decimal
  2802. if(strlen($c) <= 0) return false;
  2803. $ord0 = ord($c{0}); if ($ord0>=0 && $ord0<=127) return $ord0;
  2804. $ord1 = ord($c{1}); if ($ord0>=192 && $ord0<=223) return ($ord0-192)*64 + ($ord1-128);
  2805. $ord2 = ord($c{2}); if ($ord0>=224 && $ord0<=239) return ($ord0-224)*4096 + ($ord1-128)*64 + ($ord2-128);
  2806. $ord3 = ord($c{3}); if ($ord0>=240 && $ord0<=247) return ($ord0-240)*262144 + ($ord1-128)*4096 + ($ord2-128)*64 + ($ord3-128);
  2807. return false;
  2808. }
  2809. /**
  2810. * calculate how wide a given text string will be on a page, at a given size.
  2811. * this can be called externally, but is alse used by the other class functions
  2812. */
  2813. public function getTextWidth($size,$text){
  2814. $regex = "/<\/?([cC]:|)(".$this->allowedTags.")\>/";
  2815. $text=preg_replace($regex, '', $text);
  2816. $tmp = $this->getTextLength($size, $text);
  2817. return $tmp[0];
  2818. }
  2819. private function getTextLength($size, $text, $maxWidth = 0, $angle = 0, $wa = 0){
  2820. // Used to identify any space char for line breaks (either in Unicode or ANSI)
  2821. $spaces = array(32,5760,6158,8192,8193,8194,8195,8196,8197,8198,8200,8201,8202,8203,8204,8205,8287,8288,12288);
  2822. if (!$this->numFonts){
  2823. $this->selectFont('./fonts/Helvetica');
  2824. }
  2825. // convert ISO-8859-1 into utf8 if necessary
  2826. if(mb_detect_encoding($text) != 'UTF-8'){
  2827. $text=utf8_encode($text);
  2828. }
  2829. $a = deg2rad((float)$angle);
  2830. // get length of its unicode string
  2831. $len=mb_strlen($text);
  2832. $cf = $this->currentFont;
  2833. $tw = $maxWidth/$size*1000;
  2834. $break=0;
  2835. $w=0;
  2836. for ($i=0;$i< $len ;$i++){
  2837. $c = mb_substr($text, $i, 1, 'UTF-8');
  2838. $cOrd = $this->uniord($c);
  2839. if($cOrd == 0){
  2840. continue;
  2841. }
  2842. if (isset($this->fonts[$cf]['differences'][$cOrd])){
  2843. // then this character is being replaced by another
  2844. $cOrd2 = $this->fonts[$cf]['differences'][$cOrd];
  2845. } else {
  2846. $cOrd2 = $cOrd;
  2847. }
  2848. if (isset($this->fonts[$cf]['C'][$cOrd2])){
  2849. $w+=$this->fonts[$cf]['C'][$cOrd2];
  2850. }
  2851. // word space adjust
  2852. if($wa > 0 && in_array($cOrd2, $spaces)){
  2853. $w += $wa;
  2854. }
  2855. if($maxWidth > 0 && (cos($a)*$w) > $tw){
  2856. if ($break>0){
  2857. return array(cos($a)*$breakWidth, -sin($a)*$breakWidth, $break, 1);
  2858. } else {
  2859. $ctmp = $cOrd;
  2860. if (isset($this->fonts[$cf]['differences'][$ctmp])){
  2861. $ctmp=$this->fonts[$cf]['differences'][$ctmp];
  2862. }
  2863. $tmpw=($w-$this->fonts[$cf]['C'][$ctmp])*$size/1000;
  2864. // just split before the current character
  2865. return array(cos($a)*$tmpw, -sin($a)*$tmpw, $i, 0);
  2866. }
  2867. }
  2868. // find space or minus for a clean line break
  2869. if(in_array($cOrd2, $spaces) && $maxWidth > 0){
  2870. $break=$i;
  2871. $breakWidth = ($w-$this->fonts[$cf]['C'][$cOrd2])*$size/1000;
  2872. } else if($cOrd2 == 45 && $maxWidth > 0){
  2873. $break=$i;
  2874. $breakWidth = $w*$size/1000;
  2875. }
  2876. }
  2877. $tmpw=$w*$size/1000;
  2878. return array(cos($a)*$tmpw, -sin($a)*$tmpw, 0, 0);
  2879. }
  2880. /**
  2881. * do a part of the calculation for sorting out the justification of the text
  2882. *
  2883. */
  2884. private function adjustWrapText($text,$actual,$width,&$x,&$adjust,$justification){
  2885. switch ($justification){
  2886. case 'left':
  2887. return;
  2888. break;
  2889. case 'right':
  2890. $x+=$width-$actual;
  2891. break;
  2892. case 'center':
  2893. case 'centre':
  2894. $x+=($width-$actual)/2;
  2895. break;
  2896. case 'full':
  2897. // count the number of words
  2898. $words = explode(' ',$text);
  2899. $nspaces=count($words)-1;
  2900. if ($nspaces>0){
  2901. $adjust = ($width-$actual)/$nspaces;
  2902. } else {
  2903. $adjust=0;
  2904. }
  2905. break;
  2906. }
  2907. }
  2908. /**
  2909. * this will be called at a new page to return the state to what it was on the
  2910. * end of the previous page, before the stack was closed down
  2911. * This is to get around not being able to have open 'q' across pages
  2912. */
  2913. public function saveState($pageEnd=0){
  2914. if ($pageEnd){
  2915. // this will be called at a new page to return the state to what it was on the
  2916. // end of the previous page, before the stack was closed down
  2917. // This is to get around not being able to have open 'q' across pages
  2918. $opt = $this->stateStack[$pageEnd]; // ok to use this as stack starts numbering at 1
  2919. $this->setColor($opt['col']['r'],$opt['col']['g'],$opt['col']['b'],1);
  2920. $this->setStrokeColor($opt['str']['r'],$opt['str']['g'],$opt['str']['b'],1);
  2921. $this->objects[$this->currentContents]['c'].="\n".$opt['lin'];
  2922. // $this->currentLineStyle = $opt['lin'];
  2923. } else {
  2924. $this->nStateStack++;
  2925. $this->stateStack[$this->nStateStack]=array(
  2926. 'col'=>$this->currentColour
  2927. ,'str'=>$this->currentStrokeColour
  2928. ,'lin'=>$this->currentLineStyle
  2929. );
  2930. }
  2931. $this->objects[$this->currentContents]['c'].="\nq";
  2932. }
  2933. /**
  2934. * restore a previously saved state
  2935. */
  2936. public function restoreState($pageEnd=0){
  2937. if (!$pageEnd){
  2938. $n = $this->nStateStack;
  2939. $this->currentColour = $this->stateStack[$n]['col'];
  2940. $this->currentStrokeColour = $this->stateStack[$n]['str'];
  2941. $this->objects[$this->currentContents]['c'].="\n".$this->stateStack[$n]['lin'];
  2942. $this->currentLineStyle = $this->stateStack[$n]['lin'];
  2943. unset($this->stateStack[$n]);
  2944. $this->nStateStack--;
  2945. }
  2946. $this->objects[$this->currentContents]['c'].="\nQ";
  2947. }
  2948. /**
  2949. * make a loose object, the output will go into this object, until it is closed, then will revert to
  2950. * the current one.
  2951. * this object will not appear until it is included within a page.
  2952. * the function will return the object number
  2953. */
  2954. public function openObject(){
  2955. $this->nStack++;
  2956. $this->stack[$this->nStack]=array('c'=>$this->currentContents,'p'=>$this->currentPage);
  2957. // add a new object of the content type, to hold the data flow
  2958. $this->numObj++;
  2959. $this->o_contents($this->numObj,'new');
  2960. $this->currentContents=$this->numObj;
  2961. $this->looseObjects[$this->numObj]=1;
  2962. return $this->numObj;
  2963. }
  2964. /**
  2965. * open an existing object for editing
  2966. */
  2967. public function reopenObject($id){
  2968. $this->nStack++;
  2969. $this->stack[$this->nStack]=array('c'=>$this->currentContents,'p'=>$this->currentPage);
  2970. $this->currentContents=$id;
  2971. // also if this object is the primary contents for a page, then set the current page to its parent
  2972. if (isset($this->objects[$id]['onPage'])){
  2973. $this->currentPage = $this->objects[$id]['onPage'];
  2974. }
  2975. }
  2976. /**
  2977. * close an object
  2978. */
  2979. public function closeObject(){
  2980. // close the object, as long as there was one open in the first place, which will be indicated by
  2981. // an objectId on the stack.
  2982. if ($this->nStack>0){
  2983. $this->currentContents=$this->stack[$this->nStack]['c'];
  2984. $this->currentPage=$this->stack[$this->nStack]['p'];
  2985. $this->nStack--;
  2986. // easier to probably not worry about removing the old entries, they will be overwritten
  2987. // if there are new ones.
  2988. }
  2989. }
  2990. /**
  2991. * stop an object from appearing on pages from this point on
  2992. */
  2993. public function stopObject($id){
  2994. // if an object has been appearing on pages up to now, then stop it, this page will
  2995. // be the last one that could contian it.
  2996. if (isset($this->addLooseObjects[$id])){
  2997. $this->addLooseObjects[$id]='';
  2998. }
  2999. }
  3000. /**
  3001. * after an object has been created, it wil only show if it has been added, using this function.
  3002. */
  3003. public function addObject($id,$options='add'){
  3004. // add the specified object to the page
  3005. if (isset($this->looseObjects[$id]) && $this->currentContents!=$id){
  3006. // then it is a valid object, and it is not being added to itself
  3007. switch($options){
  3008. case 'all':
  3009. // then this object is to be added to this page (done in the next block) and
  3010. // all future new pages.
  3011. $this->addLooseObjects[$id]='all';
  3012. case 'add':
  3013. if (isset($this->objects[$this->currentContents]['onPage'])){
  3014. // then the destination contents is the primary for the page
  3015. // (though this object is actually added to that page)
  3016. $this->o_page($this->objects[$this->currentContents]['onPage'],'content',$id);
  3017. }
  3018. break;
  3019. case 'even':
  3020. $this->addLooseObjects[$id]='even';
  3021. $pageObjectId=$this->objects[$this->currentContents]['onPage'];
  3022. if ($this->objects[$pageObjectId]['info']['pageNum']%2==0){
  3023. $this->addObject($id); // hacky huh :)
  3024. }
  3025. break;
  3026. case 'odd':
  3027. $this->addLooseObjects[$id]='odd';
  3028. $pageObjectId=$this->objects[$this->currentContents]['onPage'];
  3029. if ($this->objects[$pageObjectId]['info']['pageNum']%2==1){
  3030. $this->addObject($id); // hacky huh :)
  3031. }
  3032. break;
  3033. case 'next':
  3034. $this->addLooseObjects[$id]='all';
  3035. break;
  3036. case 'nexteven':
  3037. $this->addLooseObjects[$id]='even';
  3038. break;
  3039. case 'nextodd':
  3040. $this->addLooseObjects[$id]='odd';
  3041. break;
  3042. }
  3043. }
  3044. }
  3045. /**
  3046. * add content to the documents info object
  3047. */
  3048. public function addInfo($label,$value=0){
  3049. // this will only work if the label is one of the valid ones.
  3050. // modify this so that arrays can be passed as well.
  3051. // if $label is an array then assume that it is key=>value pairs
  3052. // else assume that they are both scalar, anything else will probably error
  3053. if (is_array($label)){
  3054. foreach ($label as $l=>$v){
  3055. $this->o_info($this->infoObject,$l,$v);
  3056. }
  3057. } else {
  3058. $this->o_info($this->infoObject,$label,$value);
  3059. }
  3060. }
  3061. /**
  3062. * set the viewer preferences of the document, it is up to the browser to obey these.
  3063. */
  3064. public function setPreferences($label,$value=0){
  3065. // this will only work if the label is one of the valid ones.
  3066. if (is_array($label)){
  3067. foreach ($label as $l=>$v){
  3068. $this->o_catalog($this->catalogId,'viewerPreferences',array($l=>$v));
  3069. }
  3070. } else {
  3071. $this->o_catalog($this->catalogId,'viewerPreferences',array($label=>$value));
  3072. }
  3073. }
  3074. /**
  3075. * extract an integer from a position in a byte stream
  3076. *
  3077. */
  3078. private function getBytes(&$data,$pos,$num){
  3079. // return the integer represented by $num bytes from $pos within $data
  3080. $ret=0;
  3081. for ($i=0;$i<$num;$i++){
  3082. $ret=$ret*256;
  3083. $ret+=ord($data[$pos+$i]);
  3084. }
  3085. return $ret;
  3086. }
  3087. /**
  3088. * reads the PNG chunk
  3089. * @param $data - binary part of the png image
  3090. */
  3091. private function readPngChunks(&$data){
  3092. $default = array('info'=> array(), 'transparency'=> null, 'idata'=> null, 'pdata'=> null, 'haveHeader'=> false);
  3093. // set pointer
  3094. $p = 8;
  3095. $len = strlen($data);
  3096. // cycle through the file, identifying chunks
  3097. while ($p<$len){
  3098. $chunkLen = $this->getBytes($data,$p,4);
  3099. $chunkType = substr($data,$p+4,4);
  3100. //error_log($chunkType. ' - '.$chunkLen);
  3101. switch($chunkType){
  3102. case 'IHDR':
  3103. //this is where all the file information comes from
  3104. $default['info']['width']=$this->getBytes($data,$p+8,4);
  3105. $default['info']['height']=$this->getBytes($data,$p+12,4);
  3106. $default['info']['bitDepth']=ord($data[$p+16]);
  3107. $default['info']['colorType']=ord($data[$p+17]);
  3108. $default['info']['compressionMethod']=ord($data[$p+18]);
  3109. $default['info']['filterMethod']=ord($data[$p+19]);
  3110. $default['info']['interlaceMethod']=ord($data[$p+20]);
  3111. $this->debug('readPngChunks: ColorType is' . $default['info']['colorType'], E_USER_NOTICE);
  3112. $default['haveHeader'] = true;
  3113. if ($default['info']['compressionMethod']!=0){
  3114. $error = true;
  3115. $errormsg = "unsupported compression method";
  3116. }
  3117. if ($default['info']['filterMethod']!=0){
  3118. $error = true;
  3119. $errormsg = "unsupported filter method";
  3120. }
  3121. $default['transparency'] = array('type'=> null, 'data' => null);
  3122. if ($default['info']['colorType'] == 3) { // indexed color, rbg
  3123. // corresponding to entries in the plte chunk
  3124. // Alpha for palette index 0: 1 byte
  3125. // Alpha for palette index 1: 1 byte
  3126. // ...etc...
  3127. // there will be one entry for each palette entry. up until the last non-opaque entry.
  3128. // set up an array, stretching over all palette entries which will be o (opaque) or 1 (transparent)
  3129. $default['transparency']['type']='indexed';
  3130. //$numPalette = strlen($default['pdata'])/3;
  3131. $trans=0;
  3132. for ($i=$chunkLen;$i>=0;$i--){
  3133. if (ord($data[$p+8+$i])==0){
  3134. $trans=$i;
  3135. }
  3136. }
  3137. $default['transparency']['data'] = $trans;
  3138. } elseif($default['info']['colorType'] == 0) { // grayscale
  3139. // corresponding to entries in the plte chunk
  3140. // Gray: 2 bytes, range 0 .. (2^bitdepth)-1
  3141. // $transparency['grayscale']=$this->getBytes($data,$p+8,2); // g = grayscale
  3142. $default['transparency']['type']='indexed';
  3143. $default['transparency']['data'] = ord($data[$p+8+1]);
  3144. } elseif($default['info']['colorType'] == 2) { // truecolor
  3145. // corresponding to entries in the plte chunk
  3146. // Red: 2 bytes, range 0 .. (2^bitdepth)-1
  3147. // Green: 2 bytes, range 0 .. (2^bitdepth)-1
  3148. // Blue: 2 bytes, range 0 .. (2^bitdepth)-1
  3149. $default['transparency']['r']=$this->getBytes($data,$p+8,2); // r from truecolor
  3150. $default['transparency']['g']=$this->getBytes($data,$p+10,2); // g from truecolor
  3151. $default['transparency']['b']=$this->getBytes($data,$p+12,2); // b from truecolor
  3152. } else if($default['info']['colorType'] == 6 || $default['info']['colorType'] == 4) {
  3153. // set transparency type to "alpha" and proceed with it in $this->o_image later
  3154. $default['transparency']['type'] = 'alpha';
  3155. $img = imagecreatefromstring($data);
  3156. $imgalpha = imagecreate($default['info']['width'], $default['info']['height']);
  3157. // generate gray scale palette (0 -> 255)
  3158. for ($c = 0; $c < 256; ++$c) {
  3159. ImageColorAllocate($imgalpha, $c, $c, $c);
  3160. }
  3161. // extract alpha channel
  3162. for ($xpx = 0; $xpx < $default['info']['width']; ++$xpx) {
  3163. for ($ypx = 0; $ypx < $default['info']['height']; ++$ypx) {
  3164. $colorBits = imagecolorat($img, $xpx, $ypx);
  3165. $color = imagecolorsforindex($img, $colorBits);
  3166. $color['alpha'] = (((127 - $color['alpha']) / 127) * 255);
  3167. imagesetpixel($imgalpha, $xpx, $ypx, $color['alpha']);
  3168. }
  3169. }
  3170. $tmpfile_alpha=tempnam($this->tempPath,'ezImg');
  3171. imagepng($imgalpha, $tmpfile_alpha);
  3172. imagedestroy($imgalpha);
  3173. $alphaData = file_get_contents($tmpfile_alpha);
  3174. // nested method call to receive info on alpha image
  3175. $alphaImg = $this->readPngChunks($alphaData);
  3176. // use 'pdate' to fill alpha image as "palette". But it s the alpha channel
  3177. $default['pdata'] = $alphaImg['idata'];
  3178. // generate true color image with no alpha channel
  3179. $tmpfile_tt=tempnam($this->tempPath,'ezImg');
  3180. $imgplain = imagecreatetruecolor($default['info']['width'], $default['info']['height']);
  3181. imagecopy($imgplain, $img, 0, 0, 0, 0, $default['info']['width'], $default['info']['height']);
  3182. imagepng($imgplain, $tmpfile_tt);
  3183. imagedestroy($imgplain);
  3184. $ttData = file_get_contents($tmpfile_tt);
  3185. $ttImg = $this->readPngChunks($ttData);
  3186. $default['idata'] = $ttImg['idata'];
  3187. // remove temp files
  3188. unlink($tmpfile_alpha);
  3189. unlink($tmpfile_tt);
  3190. // return to addPngImage prematurely. IDAT has already been read and PLTE is not required
  3191. return $default;
  3192. }
  3193. break;
  3194. case 'PLTE':
  3195. $default['pdata'] = substr($data,$p+8,$chunkLen);
  3196. break;
  3197. case 'IDAT':
  3198. $default['idata'] .= substr($data,$p+8,$chunkLen);
  3199. break;
  3200. case 'tRNS': // this HEADER info is optional. More info: rfc2083 (http://tools.ietf.org/html/rfc2083)
  3201. // error_log('OPTIONAL HEADER -tRNS- exist:');
  3202. // this chunk can only occur once and it must occur after the PLTE chunk and before IDAT chunk
  3203. // KS End new code
  3204. break;
  3205. default:
  3206. break;
  3207. }
  3208. $p += $chunkLen+12;
  3209. }
  3210. return $default;
  3211. }
  3212. /**
  3213. * add a PNG image into the document, from a file
  3214. * this should work with remote files
  3215. */
  3216. public function addPngFromFile($file,$x,$y,$w=0,$h=0){
  3217. // read in a png file, interpret it, then add to the system
  3218. $error = false;
  3219. $errormsg = "";
  3220. $this->debug('addPngFromFile: opening image ' . $file);
  3221. $data = file_get_contents($file);
  3222. if($data === false){
  3223. $this->debug('addPngFromFile: trouble opening file ' . $file, E_USER_WARNING);
  3224. return;
  3225. }
  3226. $header = chr(137).chr(80).chr(78).chr(71).chr(13).chr(10).chr(26).chr(10);
  3227. if (substr($data,0,8)!=$header){
  3228. $this->debug('addPngFromFile: Invalid PNG header for file: ' . $file, E_USER_WARNING);
  3229. return;
  3230. }
  3231. $iChunk = $this->readPngChunks($data);
  3232. if(!$iChunk['haveHeader']){
  3233. $error = true;
  3234. $errormsg = "information header is missing.";
  3235. }
  3236. if (isset($iChunk['info']['interlaceMethod']) && $iChunk['info']['interlaceMethod']){
  3237. $error = true;
  3238. $errormsg = "There appears to be no support for interlaced images in pdf.";
  3239. }
  3240. if ($iChunk['info']['bitDepth'] > 8){
  3241. $error = true;
  3242. $errormsg = "only bit depth of 8 or less is supported.";
  3243. }
  3244. if ($iChunk['info']['colorType'] == 1 || $iChunk['info']['colorType'] == 5 || $iChunk['info']['colorType']== 7){
  3245. $error = true;
  3246. $errormsg = 'Unsupported PNG color type: '.$iChunk['info']['colorType'];
  3247. } else if(isset($iChunk['info'])) {
  3248. switch ($iChunk['info']['colorType']){
  3249. case 3:
  3250. $color = 'DeviceRGB';
  3251. $ncolor=1;
  3252. break;
  3253. case 6:
  3254. case 2:
  3255. $color = 'DeviceRGB';
  3256. $ncolor=3;
  3257. break;
  3258. case 4:
  3259. case 0:
  3260. $color = 'DeviceGray';
  3261. $ncolor=1;
  3262. break;
  3263. }
  3264. }
  3265. if ($error){
  3266. $this->debug('addPngFromFile: '.$errormsg, E_USER_WARNING);
  3267. return;
  3268. }
  3269. if ($w==0){
  3270. $w=$h/$iChunk['info']['height']*$iChunk['info']['width'];
  3271. }
  3272. if ($h==0){
  3273. $h=$w*$iChunk['info']['height']/$iChunk['info']['width'];
  3274. }
  3275. if($this->hashed){
  3276. $oHash = md5($iChunk['idata']);
  3277. }
  3278. if(isset($oHash) && isset($this->objectHash[$oHash])){
  3279. $label = $this->objectHash[$oHash];
  3280. }else{
  3281. $this->numImages++;
  3282. $label='I'.$this->numImages;
  3283. $this->numObj++;
  3284. if(isset($oHash)){
  3285. $this->objectHash[$oHash] = $label;
  3286. }
  3287. $options = array('label'=>$label,'data'=>$iChunk['idata'],'bitsPerComponent'=>$iChunk['info']['bitDepth'],'pdata'=>$iChunk['pdata']
  3288. ,'iw'=>$iChunk['info']['width'],'ih'=>$iChunk['info']['height'],'type'=>'png','color'=>$color,'ncolor'=>$ncolor);
  3289. if (isset($iChunk['transparency'])){
  3290. $options['transparency']=$iChunk['transparency'];
  3291. }
  3292. $this->o_image($this->numObj,'new',$options);
  3293. }
  3294. $this->objects[$this->currentContents]['c'].="\nq ".sprintf('%.3F',$w)." 0 0 ".sprintf('%.3F',$h)." ".sprintf('%.3F',$x)." ".sprintf('%.3F',$y)." cm";
  3295. $this->objects[$this->currentContents]['c'].=" /".$label.' Do';
  3296. $this->objects[$this->currentContents]['c'].=" Q";
  3297. }
  3298. /**
  3299. * add a JPEG image into the document, from a file
  3300. */
  3301. public function addJpegFromFile($img,$x,$y,$w=0,$h=0){
  3302. // attempt to add a jpeg image straight from a file, using no GD commands
  3303. // note that this function is unable to operate on a remote file.
  3304. if (!file_exists($img)){
  3305. return;
  3306. }
  3307. $tmp=getimagesize($img);
  3308. $imageWidth=$tmp[0];
  3309. $imageHeight=$tmp[1];
  3310. if (isset($tmp['channels'])){
  3311. $channels = $tmp['channels'];
  3312. } else {
  3313. $channels = 3;
  3314. }
  3315. if ($w<=0 && $h<=0){
  3316. $w=$imageWidth;
  3317. }
  3318. if ($w==0){
  3319. $w=$h/$imageHeight*$imageWidth;
  3320. }
  3321. if ($h==0){
  3322. $h=$w*$imageHeight/$imageWidth;
  3323. }
  3324. $data = file_get_contents($img);
  3325. $this->addJpegImage_common($data,$x,$y,$w,$h,$imageWidth,$imageHeight,$channels);
  3326. }
  3327. /**
  3328. * read gif image from file, converts it into an JPEG (no transparancy) and display it
  3329. * @param $img - file path ti gif image
  3330. * @param $x - coord x
  3331. * @param $y - y cord
  3332. * @param $w - width
  3333. * @param $h - height
  3334. */
  3335. public function addGifFromFile($img, $x, $y, $w=0, $h=0){
  3336. if (!file_exists($img)){
  3337. return;
  3338. }
  3339. if(!function_exists("imagecreatefromgif")){
  3340. $this->debug('addGifFromFile: Missing GD function imageCreateFromGif', E_USER_ERROR);
  3341. return;
  3342. }
  3343. $tmp=getimagesize($img);
  3344. $imageWidth=$tmp[0];
  3345. $imageHeight=$tmp[1];
  3346. if ($w<=0 && $h<=0){
  3347. $w=$imageWidth;
  3348. }
  3349. if ($w==0){
  3350. $w=$h/$imageHeight*$imageWidth;
  3351. }
  3352. if ($h==0){
  3353. $h=$w*$imageHeight/$imageWidth;
  3354. }
  3355. $imgres = imagecreatefromgif($img);
  3356. $tmpName=tempnam($this->tempPath,'img');
  3357. imagejpeg($imgres,$tmpName,90);
  3358. $this->addJpegFromFile($tmpName,$x,$y,$w,$h);
  3359. }
  3360. /**
  3361. * add an image into the document, from a GD object
  3362. * this function is not all that reliable, and I would probably encourage people to use
  3363. * the file based functions
  3364. * @param $img - gd image resource
  3365. * @param $x coord x
  3366. * @param $y coord y
  3367. * @param $w width
  3368. * @param $h height
  3369. * @param $quality image quality
  3370. */
  3371. protected function addImage(&$img,$x,$y,$w=0,$h=0,$quality=75){
  3372. // add a new image into the current location, as an external object
  3373. // add the image at $x,$y, and with width and height as defined by $w & $h
  3374. // note that this will only work with full colour images and makes them jpg images for display
  3375. // later versions could present lossless image formats if there is interest.
  3376. // there seems to be some problem here in that images that have quality set above 75 do not appear
  3377. // not too sure why this is, but in the meantime I have restricted this to 75.
  3378. if ($quality>75){
  3379. $quality=75;
  3380. }
  3381. // if the width or height are set to zero, then set the other one based on keeping the image
  3382. // height/width ratio the same, if they are both zero, then give up :)
  3383. $imageWidth=imagesx($img);
  3384. $imageHeight=imagesy($img);
  3385. if ($w<=0 && $h<=0){
  3386. return;
  3387. }
  3388. if ($w==0){
  3389. $w=$h/$imageHeight*$imageWidth;
  3390. }
  3391. if ($h==0){
  3392. $h=$w*$imageHeight/$imageWidth;
  3393. }
  3394. $tmpName=tempnam($this->tempPath,'img');
  3395. imagejpeg($img,$tmpName,$quality);
  3396. $data = file_get_contents($tmpName);
  3397. if($data === false) {
  3398. $this->debug('addImage: trouble opening image resource', E_USER_WARNING);
  3399. }
  3400. unlink($tmpName);
  3401. $this->addJpegImage_common($data,$x,$y,$w,$h,$imageWidth,$imageHeight);
  3402. }
  3403. /**
  3404. * common code used by the two JPEG adding functions
  3405. */
  3406. private function addJpegImage_common(&$data,$x,$y,$w=0,$h=0,$imageWidth,$imageHeight,$channels=3){
  3407. // note that this function is not to be called externally
  3408. // it is just the common code between the GD and the file options
  3409. if($this->hashed){
  3410. $oHash = md5($data);
  3411. }
  3412. if(isset($oHash) && isset($this->objectHash[$oHash])){
  3413. $label = $this->objectHash[$oHash];
  3414. }else{
  3415. $this->numImages++;
  3416. $label='I'.$this->numImages;
  3417. $this->numObj++;
  3418. if(isset($oHash)){
  3419. $this->objectHash[$oHash] = $label;
  3420. }
  3421. $this->o_image($this->numObj,'new',array('label'=>$label,'data'=>$data,'iw'=>$imageWidth,'ih'=>$imageHeight,'channels'=>$channels));
  3422. }
  3423. $this->objects[$this->currentContents]['c'].="\nq ".sprintf('%.3F',$w)." 0 0 ".sprintf('%.3F',$h)." ".sprintf('%.3F',$x)." ".sprintf('%.3F',$y)." cm";
  3424. $this->objects[$this->currentContents]['c'].=" /".$label.' Do';
  3425. $this->objects[$this->currentContents]['c'].=" Q";
  3426. }
  3427. /**
  3428. * specify where the document should open when it first starts
  3429. */
  3430. public function openHere($style,$a=0,$b=0,$c=0){
  3431. // this function will open the document at a specified page, in a specified style
  3432. // the values for style, and the required paramters are:
  3433. // 'XYZ' left, top, zoom
  3434. // 'Fit'
  3435. // 'FitH' top
  3436. // 'FitV' left
  3437. // 'FitR' left,bottom,right
  3438. // 'FitB'
  3439. // 'FitBH' top
  3440. // 'FitBV' left
  3441. $this->numObj++;
  3442. $this->o_destination($this->numObj,'new',array('page'=>$this->currentPage,'type'=>$style,'p1'=>$a,'p2'=>$b,'p3'=>$c));
  3443. $id = $this->catalogId;
  3444. $this->o_catalog($id,'openHere',$this->numObj);
  3445. }
  3446. /**
  3447. * create a labelled destination within the document
  3448. */
  3449. public function addDestination($label,$style,$a=0,$b=0,$c=0){
  3450. // associates the given label with the destination, it is done this way so that a destination can be specified after
  3451. // it has been linked to
  3452. // styles are the same as the 'openHere' function
  3453. $this->numObj++;
  3454. $this->o_destination($this->numObj,'new',array('page'=>$this->currentPage,'type'=>$style,'p1'=>$a,'p2'=>$b,'p3'=>$c));
  3455. $id = $this->numObj;
  3456. // store the label->idf relationship, note that this means that labels can be used only once
  3457. $this->destinations["$label"]=$id;
  3458. }
  3459. /**
  3460. * define font families, this is used to initialize the font families for the default fonts
  3461. * and for the user to add new ones for their fonts. The default bahavious can be overridden should
  3462. * that be desired.
  3463. */
  3464. public function setFontFamily($family, $options = ''){
  3465. if (is_array($options)) {
  3466. // the user is trying to set a font family
  3467. // note that this can also be used to set the base ones to something else
  3468. if (strlen($family)){
  3469. $this->fontFamilies[$family] = $options;
  3470. }
  3471. }
  3472. }
  3473. /**
  3474. * used to add messages for use in debugging
  3475. */
  3476. protected function debug($message, $error_type = E_USER_NOTICE)
  3477. {
  3478. if($error_type <= $this->DEBUGLEVEL){
  3479. switch(strtolower($this->DEBUG)){
  3480. default:
  3481. case 'none':
  3482. break;
  3483. case 'error_log':
  3484. error_log($message);
  3485. break;
  3486. case 'variable':
  3487. $this->messages.=$message."\n";
  3488. break;
  3489. }
  3490. }
  3491. }
  3492. /**
  3493. * a few functions which should allow the document to be treated transactionally.
  3494. *
  3495. * @param string $action WHAT IS THIS?
  3496. * @return void
  3497. */
  3498. public function transaction($action){
  3499. switch ($action){
  3500. case 'start':
  3501. // store all the data away into the checkpoint variable
  3502. $data = get_object_vars($this);
  3503. $this->checkpoint = $data;
  3504. unset($data);
  3505. break;
  3506. case 'commit':
  3507. if (is_array($this->checkpoint) && isset($this->checkpoint['checkpoint'])){
  3508. $tmp = $this->checkpoint['checkpoint'];
  3509. $this->checkpoint = $tmp;
  3510. unset($tmp);
  3511. } else {
  3512. $this->checkpoint='';
  3513. }
  3514. break;
  3515. case 'rewind':
  3516. // do not destroy the current checkpoint, but move us back to the state then, so that we can try again
  3517. if (is_array($this->checkpoint)){
  3518. // can only abort if were inside a checkpoint
  3519. $tmp = $this->checkpoint;
  3520. foreach ($tmp as $k=>$v){
  3521. if ($k != 'checkpoint'){
  3522. $this->$k=$v;
  3523. }
  3524. }
  3525. unset($tmp);
  3526. }
  3527. break;
  3528. case 'abort':
  3529. if (is_array($this->checkpoint)){
  3530. // can only abort if were inside a checkpoint
  3531. $tmp = $this->checkpoint;
  3532. foreach ($tmp as $k=>$v){
  3533. $this->$k=$v;
  3534. }
  3535. unset($tmp);
  3536. }
  3537. break;
  3538. }
  3539. }
  3540. } // end of class
  3541. ?>